From 0a17df91fc4545617ecd0785db82cde14da5a9dc Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Tue, 21 Aug 2018 10:07:23 -0400 Subject: [PATCH] Added Image and renamed the BrushType constants respectively. --- draw.go | 14 ++++++------ image.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ zz_histogram.go | 4 ++-- 3 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 image.go diff --git a/draw.go b/draw.go index 237e9c7..fd21a75 100644 --- a/draw.go +++ b/draw.go @@ -206,10 +206,10 @@ type DrawContext struct { // TODO disclaimer type BrushType int const ( - Solid BrushType = iota - LinearGradient - RadialGradient - Image // presently unimplemented + BrushTypeSolid BrushType = iota + BrushTypeLinearGradient + BrushTypeRadialGradient + BrushTypeImage // presently unimplemented ) // TODO @@ -270,12 +270,12 @@ func (b *Brush) toC() *C.uiDrawBrush { cb := C.newBrush() cb.Type = C.uiDrawBrushType(b.Type) switch b.Type { - case Solid: + case BrushTypeSolid: cb.R = C.double(b.R) cb.G = C.double(b.G) cb.B = C.double(b.B) cb.A = C.double(b.A) - case LinearGradient, RadialGradient: + case BrushTypeLinearGradient, BrushTypeRadialGradient: cb.X0 = C.double(b.X0) cb.Y0 = C.double(b.Y0) cb.X1 = C.double(b.X1) @@ -291,7 +291,7 @@ func (b *Brush) toC() *C.uiDrawBrush { C.double(s.B), C.double(s.A)) } - case Image: + case BrushTypeImage: panic("unimplemented") default: panic("invalid brush type in Brush.toC()") diff --git a/image.go b/image.go new file mode 100644 index 0000000..9b791fe --- /dev/null +++ b/image.go @@ -0,0 +1,58 @@ +// 21 august 2018 + +package ui + +import ( + "image" +) + +// #include +// #include "ui.h" +import "C" + +// Image stores an image for display on screen. +// +// Images are built from one or more representations, each with the +// same aspect ratio but a different pixel size. Package ui +// automatically selects the most appropriate representation for +// drawing the image when it comes time to draw the image; what +// this means depends on the pixel density of the target context. +// Therefore, one can use Image to draw higher-detailed images on +// higher-density displays. The typical use cases are either: +// +// - have just a single representation, at which point all screens +// use the same image, and thus uiImage acts like a simple +// bitmap image, or +// - have two images, one at normal resolution and one at 2x +// resolution; this matches the current expectations of some +// desktop systems at the time of writing (mid-2018) +// +// Image allocates OS resources; you must explicitly free an Image +// when you are finished with it. +type Image struct { + i *C.uiImage +} + +// NewImage creates a new Image with the given width and +// height. This width and height should be the size in points of the +// image in the device-independent case; typically this is the 1x size. +func NewImage(width, height float64) *Image { + return &Image{ + i: C.uiNewImage(C.double(width), C.double(height)), + } +} + +// Free frees the Image. +func (i *Image) Free() { + C.uiFreeImage(i.i) +} + +// Append adds the given image as a representation of the Image. +func (i *Image) Append(img *image.NRGBA) { + cpix := C.CBytes(img.Pix) + defer C.free(cpix) + C.uiImageAppend(i.i, cpix, + C.int(img.Rect.Dx()), + C.int(img.Rect.Dy()), + C.int(img.Stride)) +} diff --git a/zz_histogram.go b/zz_histogram.go index 3c98528..fae5aae 100644 --- a/zz_histogram.go +++ b/zz_histogram.go @@ -31,7 +31,7 @@ const ( // helper to quickly set a brush color func mkSolidBrush(color uint32, alpha float64) *ui.Brush { brush := new(ui.Brush) - brush.Type = ui.Solid + brush.Type = ui.BrushTypeSolid component := uint8((color >> 16) & 0xFF) brush.R = float64(component) / 255 component = uint8((color >> 8) & 0xFF) @@ -125,7 +125,7 @@ func (areaHandler) Draw(a *ui.Area, p *ui.AreaDrawParams) { // now get the color for the graph itself and set up the brush graphR, graphG, graphB, graphA := colorButton.Color() - brush.Type = ui.Solid + brush.Type = ui.BrushTypeSolid brush.R = graphR brush.G = graphG brush.B = graphB