Swatch no longer embeds tomo.CanvasBox

This commit is contained in:
Sasha Koshka 2024-08-24 22:12:43 -04:00
parent d9d758b5fc
commit 32eae0bcca

View File

@ -10,12 +10,14 @@ import "git.tebibyte.media/tomo/tomo/canvas"
import "git.tebibyte.media/tomo/objects/layouts"
import "git.tebibyte.media/tomo/objects/internal"
var _ tomo.Object = new(Swatch)
// Swatch displays a color, allowing the user to edit it by clicking on it.
type Swatch struct {
tomo.CanvasBox
value color.Color
box tomo.CanvasBox
value color.Color
editing bool
label string
label string
on struct {
valueChange event.FuncBroadcaster
confirm event.FuncBroadcaster
@ -25,20 +27,32 @@ type Swatch struct {
// NewSwatch creates a new swatch with the given color.
func NewSwatch (value color.Color) *Swatch {
swatch := &Swatch {
CanvasBox: tomo.NewCanvasBox(),
box: tomo.NewCanvasBox(),
}
swatch.SetRole(tomo.R("objects", "Swatch"))
swatch.SetDrawer(swatch)
swatch.box.SetRole(tomo.R("objects", "Swatch"))
swatch.box.SetDrawer(swatch)
swatch.SetValue(value)
swatch.OnButtonDown(swatch.handleButtonDown)
swatch.OnButtonUp(swatch.handleButtonUp)
swatch.OnKeyDown(swatch.handleKeyDown)
swatch.OnKeyUp(swatch.handleKeyUp)
swatch.SetFocusable(true)
swatch.box.OnButtonDown(swatch.handleButtonDown)
swatch.box.OnButtonUp(swatch.handleButtonUp)
swatch.box.OnKeyDown(swatch.handleKeyDown)
swatch.box.OnKeyUp(swatch.handleKeyUp)
swatch.box.SetFocusable(true)
return swatch
}
// GetBox returns the underlying box.
func (this *Swatch) GetBox () tomo.Box {
return this.box
}
// SetFocused sets whether or not this swatch has keyboard focus. If set to
// true, this method will steal focus away from whichever object currently has
// focus.
func (this *Swatch) SetFocused (focused bool) {
this.box.SetFocused(focused)
}
// Value returns the color of the swatch.
func (this *Swatch) Value () color.Color {
return this.value
@ -48,7 +62,7 @@ func (this *Swatch) Value () color.Color {
func (this *Swatch) SetValue (value color.Color) {
this.value = value
if value == nil { value = color.Transparent }
this.Invalidate()
this.box.Invalidate()
}
// OnValueChange specifies a function to be called when the swatch's color
@ -75,7 +89,7 @@ func (this *Swatch) Choose () {
var err error
var window tomo.Window
if parent := this.Window(); parent != nil {
if parent := this.box.Window(); parent != nil {
window, err = parent.NewChild(image.Rectangle { })
} else {
window, err = tomo.NewWindow(image.Rectangle { })
@ -155,13 +169,13 @@ func (this *Swatch) Draw (can canvas.Canvas) {
// transparency slash
pen.Stroke(color.RGBA { R: 255, A: 255 })
pen.StrokeWeight(1)
pen.Path(this.Bounds().Min, this.Bounds().Max)
pen.Path(this.box.Bounds().Min, this.box.Bounds().Max)
// color
if this.value != nil {
pen.StrokeWeight(0)
pen.Fill(this.value)
pen.Rectangle(this.Bounds())
pen.Rectangle(this.box.Bounds())
}
}
@ -188,7 +202,7 @@ func (this *Swatch) handleButtonDown (button input.Button) bool {
func (this *Swatch) handleButtonUp (button input.Button) bool {
if !isClickingButton(button) { return false }
if this.Window().MousePosition().In(this.Bounds()) {
if this.box.Window().MousePosition().In(this.box.Bounds()) {
this.Choose()
}
return true