79 lines
1.3 KiB
Go
79 lines
1.3 KiB
Go
package widget
|
|
|
|
import (
|
|
"image"
|
|
"image/draw"
|
|
|
|
"github.com/BeatGlow/display"
|
|
)
|
|
|
|
// Empty display.
|
|
type Empty struct {
|
|
// Display this widget renders on.
|
|
Display display.Display
|
|
}
|
|
|
|
func (w Empty) Render(_ bool) bool {
|
|
w.Display.Clear()
|
|
return true
|
|
}
|
|
|
|
// Image displays an image.
|
|
type Image struct {
|
|
// Display this widget renders on.
|
|
Display display.Display
|
|
|
|
// Position on the display.
|
|
Position image.Point
|
|
|
|
// SourcePoint is the point from which this image is copied onto the display.
|
|
SourcePoint image.Point
|
|
|
|
// DrawOp is the drawing operation.
|
|
DrawOp draw.Op
|
|
|
|
buffer image.Image
|
|
changed bool
|
|
}
|
|
|
|
func NewImage(display display.Display, at image.Point, im image.Image) *Image {
|
|
return &Image{
|
|
Display: display,
|
|
Position: at,
|
|
DrawOp: draw.Src,
|
|
buffer: im,
|
|
}
|
|
}
|
|
|
|
func (w *Image) Set(i image.Image) {
|
|
w.buffer = i
|
|
w.changed = true
|
|
}
|
|
|
|
func (w *Image) Render(redraw bool) bool {
|
|
if !(redraw || w.changed) {
|
|
return false
|
|
}
|
|
|
|
if w.buffer != nil {
|
|
r := w.buffer.Bounds()
|
|
if w.Position.Eq(Center) {
|
|
b := w.buffer.Bounds().Size()
|
|
r.Min = image.Point{}
|
|
if x := r.Dx(); b.X < x {
|
|
r.Min.X = (x - b.X) / 2
|
|
}
|
|
if y := r.Dy(); b.Y < y {
|
|
r.Min.Y = (y - b.Y) / 2
|
|
}
|
|
} else {
|
|
r.Min = r.Min.Add(w.Position)
|
|
}
|
|
|
|
draw.Draw(w.Display, r, w.buffer, w.SourcePoint, w.DrawOp)
|
|
}
|
|
|
|
w.changed = false
|
|
return true
|
|
}
|