Added Image and renamed the BrushType constants respectively.

This commit is contained in:
Pietro Gagliardi 2018-08-21 10:07:23 -04:00
parent 3983f63048
commit 0a17df91fc
3 changed files with 67 additions and 9 deletions

14
draw.go
View File

@ -206,10 +206,10 @@ type DrawContext struct {
// TODO disclaimer // TODO disclaimer
type BrushType int type BrushType int
const ( const (
Solid BrushType = iota BrushTypeSolid BrushType = iota
LinearGradient BrushTypeLinearGradient
RadialGradient BrushTypeRadialGradient
Image // presently unimplemented BrushTypeImage // presently unimplemented
) )
// TODO // TODO
@ -270,12 +270,12 @@ func (b *Brush) toC() *C.uiDrawBrush {
cb := C.newBrush() cb := C.newBrush()
cb.Type = C.uiDrawBrushType(b.Type) cb.Type = C.uiDrawBrushType(b.Type)
switch b.Type { switch b.Type {
case Solid: case BrushTypeSolid:
cb.R = C.double(b.R) cb.R = C.double(b.R)
cb.G = C.double(b.G) cb.G = C.double(b.G)
cb.B = C.double(b.B) cb.B = C.double(b.B)
cb.A = C.double(b.A) cb.A = C.double(b.A)
case LinearGradient, RadialGradient: case BrushTypeLinearGradient, BrushTypeRadialGradient:
cb.X0 = C.double(b.X0) cb.X0 = C.double(b.X0)
cb.Y0 = C.double(b.Y0) cb.Y0 = C.double(b.Y0)
cb.X1 = C.double(b.X1) cb.X1 = C.double(b.X1)
@ -291,7 +291,7 @@ func (b *Brush) toC() *C.uiDrawBrush {
C.double(s.B), C.double(s.B),
C.double(s.A)) C.double(s.A))
} }
case Image: case BrushTypeImage:
panic("unimplemented") panic("unimplemented")
default: default:
panic("invalid brush type in Brush.toC()") panic("invalid brush type in Brush.toC()")

58
image.go Normal file
View File

@ -0,0 +1,58 @@
// 21 august 2018
package ui
import (
"image"
)
// #include <stdlib.h>
// #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))
}

View File

@ -31,7 +31,7 @@ const (
// helper to quickly set a brush color // helper to quickly set a brush color
func mkSolidBrush(color uint32, alpha float64) *ui.Brush { func mkSolidBrush(color uint32, alpha float64) *ui.Brush {
brush := new(ui.Brush) brush := new(ui.Brush)
brush.Type = ui.Solid brush.Type = ui.BrushTypeSolid
component := uint8((color >> 16) & 0xFF) component := uint8((color >> 16) & 0xFF)
brush.R = float64(component) / 255 brush.R = float64(component) / 255
component = uint8((color >> 8) & 0xFF) 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 // now get the color for the graph itself and set up the brush
graphR, graphG, graphB, graphA := colorButton.Color() graphR, graphG, graphB, graphA := colorButton.Color()
brush.Type = ui.Solid brush.Type = ui.BrushTypeSolid
brush.R = graphR brush.R = graphR
brush.G = graphG brush.G = graphG
brush.B = graphB brush.B = graphB