Moved element core to new package
This commit is contained in:
@@ -4,10 +4,11 @@ import "image"
|
||||
import "git.tebibyte.media/sashakoshka/tomo"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
||||
|
||||
type Button struct {
|
||||
*Core
|
||||
core CoreControl
|
||||
*core.Core
|
||||
core core.CoreControl
|
||||
|
||||
pressed bool
|
||||
enabled bool
|
||||
@@ -20,7 +21,7 @@ type Button struct {
|
||||
|
||||
func NewButton (text string) (element *Button) {
|
||||
element = &Button { enabled: true }
|
||||
element.Core, element.core = NewCore(element)
|
||||
element.Core, element.core = core.NewCore(element)
|
||||
element.drawer.SetFace(theme.FontFaceRegular())
|
||||
element.SetText(text)
|
||||
return
|
||||
|
||||
@@ -1,135 +0,0 @@
|
||||
package basic
|
||||
|
||||
import "image"
|
||||
import "image/color"
|
||||
import "git.tebibyte.media/sashakoshka/tomo"
|
||||
|
||||
// Core is a struct that implements some core functionality common to most
|
||||
// widgets. It is meant to be embedded directly into a struct.
|
||||
type Core struct {
|
||||
canvas *image.RGBA
|
||||
parent tomo.Element
|
||||
|
||||
metrics struct {
|
||||
minimumWidth int
|
||||
minimumHeight int
|
||||
}
|
||||
|
||||
hooks tomo.ParentHooks
|
||||
}
|
||||
|
||||
// NewCore creates a new element core and its corresponding control.
|
||||
func NewCore (parent tomo.Element) (core *Core, control CoreControl) {
|
||||
core = &Core { parent: parent }
|
||||
control = CoreControl { core: core }
|
||||
return
|
||||
}
|
||||
|
||||
func (core Core) ColorModel () (model color.Model) {
|
||||
return color.RGBAModel
|
||||
}
|
||||
|
||||
func (core Core) At (x, y int) (pixel color.Color) {
|
||||
if core.canvas == nil { return color.RGBA { } }
|
||||
pixel = core.canvas.At(x, y)
|
||||
return
|
||||
}
|
||||
|
||||
func (core Core) RGBAAt (x, y int) (pixel color.RGBA) {
|
||||
if core.canvas == nil { return color.RGBA { } }
|
||||
pixel = core.canvas.RGBAAt(x, y)
|
||||
return
|
||||
}
|
||||
|
||||
func (core Core) Bounds () (bounds image.Rectangle) {
|
||||
if core.canvas != nil { bounds = core.canvas.Bounds() }
|
||||
return
|
||||
}
|
||||
|
||||
func (core *Core) SetParentHooks (hooks tomo.ParentHooks) {
|
||||
core.hooks = hooks
|
||||
}
|
||||
|
||||
func (core Core) MinimumSize () (width, height int) {
|
||||
return core.metrics.minimumWidth, core.metrics.minimumHeight
|
||||
}
|
||||
|
||||
// CoreControl is a struct that can exert control over a control struct. It can
|
||||
// be used as a canvas. It must not be directly embedded into an element, but
|
||||
// instead kept as a private member.
|
||||
type CoreControl struct {
|
||||
*image.RGBA
|
||||
core *Core
|
||||
}
|
||||
|
||||
func (control CoreControl) HasImage () (has bool) {
|
||||
has = control.RGBA != nil
|
||||
return
|
||||
}
|
||||
|
||||
func (control CoreControl) Select () {
|
||||
control.core.hooks.RunSelectionRequest()
|
||||
}
|
||||
|
||||
func (control CoreControl) PushRegion (bounds image.Rectangle) {
|
||||
control.core.hooks.RunDraw(control.SubImage(bounds).(*image.RGBA))
|
||||
}
|
||||
|
||||
func (control CoreControl) PushAll () {
|
||||
control.PushRegion(control.Bounds())
|
||||
}
|
||||
|
||||
func (control *CoreControl) AllocateCanvas (width, height int) {
|
||||
core := control.core
|
||||
width, height, _ = control.ConstrainSize(width, height)
|
||||
core.canvas = image.NewRGBA(image.Rect (0, 0, width, height))
|
||||
control.RGBA = core.canvas
|
||||
}
|
||||
|
||||
func (control CoreControl) SetMinimumSize (width, height int) {
|
||||
core := control.core
|
||||
if width != core.metrics.minimumWidth ||
|
||||
height != core.metrics.minimumHeight {
|
||||
|
||||
core.metrics.minimumWidth = width
|
||||
core.metrics.minimumHeight = height
|
||||
core.hooks.RunMinimumSizeChange(width, height)
|
||||
|
||||
// if there is an image buffer, and the current size is less
|
||||
// than this new minimum size, send core.parent a resize event.
|
||||
if control.HasImage() {
|
||||
bounds := control.Bounds()
|
||||
imageWidth,
|
||||
imageHeight,
|
||||
constrained := control.ConstrainSize (
|
||||
bounds.Dx(),
|
||||
bounds.Dy())
|
||||
if constrained {
|
||||
core.parent.Handle (tomo.EventResize {
|
||||
Width: imageWidth,
|
||||
Height: imageHeight,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (control CoreControl) ConstrainSize (
|
||||
inWidth, inHeight int,
|
||||
) (
|
||||
outWidth, outHeight int,
|
||||
constrained bool,
|
||||
) {
|
||||
core := control.core
|
||||
outWidth = inWidth
|
||||
outHeight = inHeight
|
||||
if outWidth < core.metrics.minimumWidth {
|
||||
outWidth = core.metrics.minimumWidth
|
||||
constrained = true
|
||||
}
|
||||
if outHeight < core.metrics.minimumHeight {
|
||||
outHeight = core.metrics.minimumHeight
|
||||
constrained = true
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -4,10 +4,11 @@ import "image"
|
||||
import "git.tebibyte.media/sashakoshka/tomo"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
||||
|
||||
type Label struct {
|
||||
*Core
|
||||
core CoreControl
|
||||
*core.Core
|
||||
core core.CoreControl
|
||||
|
||||
text string
|
||||
drawer artist.TextDrawer
|
||||
@@ -15,7 +16,7 @@ type Label struct {
|
||||
|
||||
func NewLabel (text string) (element *Label) {
|
||||
element = &Label { }
|
||||
element.Core, element.core = NewCore(element)
|
||||
element.Core, element.core = core.NewCore(element)
|
||||
face := theme.FontFaceRegular()
|
||||
element.drawer.SetFace(face)
|
||||
element.SetText(text)
|
||||
|
||||
@@ -4,17 +4,18 @@ import "image"
|
||||
import "image/color"
|
||||
import "git.tebibyte.media/sashakoshka/tomo"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
||||
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
||||
|
||||
// Test is a simple element that can be used as a placeholder.
|
||||
type Test struct {
|
||||
*Core
|
||||
core CoreControl
|
||||
*core.Core
|
||||
core core.CoreControl
|
||||
}
|
||||
|
||||
// NewTest creates a new test element.
|
||||
func NewTest () (element *Test) {
|
||||
element = &Test { }
|
||||
element.Core, element.core = NewCore(element)
|
||||
element.Core, element.core = core.NewCore(element)
|
||||
element.core.SetMinimumSize(32, 32)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user