Initial commit

This commit is contained in:
2023-01-09 01:03:19 -05:00
commit 00d75d4488
27 changed files with 3036 additions and 0 deletions

176
elements/basic/button.go Normal file
View File

@@ -0,0 +1,176 @@
package basic
import "image"
import "image/color"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/artist"
type Button struct {
core Core
pressed bool
enabled bool
onClick func ()
text string
drawer artist.TextDrawer
}
func NewButton (text string) (element *Button) {
element = &Button { enabled: true }
element.core = NewCore(element)
element.drawer.SetFace(theme.FontFaceRegular())
element.SetText(text)
return
}
func (element *Button) Handle (event tomo.Event) {
switch event.(type) {
case tomo.EventResize:
resizeEvent := event.(tomo.EventResize)
element.core.AllocateCanvas (
resizeEvent.Width,
resizeEvent.Height)
element.draw()
case tomo.EventMouseDown:
if !element.enabled { break }
mouseDownEvent := event.(tomo.EventMouseDown)
if mouseDownEvent.Button != tomo.ButtonLeft { break }
element.pressed = true
if element.core.HasImage() {
element.draw()
element.core.PushAll()
}
case tomo.EventMouseUp:
if !element.enabled { break }
mouseUpEvent := event.(tomo.EventMouseUp)
if mouseUpEvent.Button != tomo.ButtonLeft { break }
element.pressed = false
if element.core.HasImage() {
element.draw()
element.core.PushAll()
}
within := image.Point { mouseUpEvent.X, mouseUpEvent.Y }.
In(element.Bounds())
if within && element.onClick != nil {
element.onClick()
}
// TODO: handle selection events, and the enter key
}
return
}
func (element *Button) SetEnabled (enabled bool) {
if element.enabled == enabled { return }
element.enabled = enabled
if element.core.HasImage () {
element.draw()
element.core.PushAll()
}
}
func (element *Button) SetText (text string) {
if element.text == text { return }
element.text = text
element.drawer.SetText(text)
textBounds := element.drawer.LayoutBounds()
element.core.SetMinimumSize (
theme.Padding() * 2 + textBounds.Dx(),
theme.Padding() * 2 + textBounds.Dy())
if element.core.HasImage () {
element.draw()
element.core.PushAll()
}
}
func (element *Button) OnClick (callback func ()) {
element.onClick = callback
}
func (element *Button) ColorModel () (model color.Model) {
return color.RGBAModel
}
func (element *Button) At (x, y int) (pixel color.Color) {
pixel = element.core.At(x, y)
return
}
func (element *Button) RGBAAt (x, y int) (pixel color.RGBA) {
pixel = element.core.RGBAAt(x, y)
return
}
func (element *Button) Bounds () (bounds image.Rectangle) {
bounds = element.core.Bounds()
return
}
func (element *Button) SetDrawCallback (draw func (region tomo.Image)) {
element.core.SetDrawCallback(draw)
}
func (element *Button) SetMinimumSizeChangeCallback (
notify func (width, height int),
) {
element.core.SetMinimumSizeChangeCallback(notify)
}
func (element *Button) Selectable () (selectable bool) {
selectable = true
return
}
func (element *Button) MinimumWidth () (minimum int) {
minimum = element.core.MinimumWidth()
return
}
func (element *Button) MinimumHeight () (minimum int) {
minimum = element.core.MinimumHeight()
return
}
func (element *Button) draw () {
bounds := element.core.Bounds()
artist.ChiseledRectangle (
element.core,
theme.RaisedProfile(element.pressed, element.enabled),
bounds)
innerBounds := bounds
innerBounds.Min.X += theme.Padding()
innerBounds.Min.Y += theme.Padding()
innerBounds.Max.X -= theme.Padding()
innerBounds.Max.Y -= theme.Padding()
textBounds := element.drawer.LayoutBounds()
offset := image.Point {
X: theme.Padding() + (innerBounds.Dx() - textBounds.Dx()) / 2,
Y: theme.Padding() + (innerBounds.Dy() - textBounds.Dy()) / 2,
}
// account for the fact that the bounding rectangle will be shifted over
// due to the bounds origin being at the baseline of the first line
offset.Y -= textBounds.Min.Y
offset.X -= textBounds.Min.X
if element.pressed {
offset = offset.Add(theme.SinkOffsetVector())
}
foreground := theme.ForegroundImage()
if !element.enabled {
foreground = theme.DisabledForegroundImage()
}
element.drawer.Draw(element.core, foreground, offset)
}

138
elements/basic/core.go Normal file
View File

@@ -0,0 +1,138 @@
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 possible to embed this directly into a struct, but this is not
// reccomended as it exposes internal functionality.
type Core struct {
*image.RGBA
parent tomo.Element
drawCallback func (region tomo.Image)
minimumSizeChangeCallback func (width, height int)
metrics struct {
minimumWidth int
minimumHeight int
}
}
// Core creates a new element core.
func NewCore (parent tomo.Element) (core Core) {
core = Core { parent: parent }
return
}
func (core Core) ColorModel () (model color.Model) {
return color.RGBAModel
}
func (core Core) At (x, y int) (pixel color.Color) {
if core.RGBA == nil { return color.RGBA { } }
pixel = core.RGBA.At(x, y)
return
}
func (core Core) RGBAAt (x, y int) (pixel color.RGBA) {
if core.RGBA == nil { return color.RGBA { } }
pixel = core.RGBA.RGBAAt(x, y)
return
}
func (core Core) Bounds () (bounds image.Rectangle) {
if core.RGBA != nil { bounds = core.RGBA.Bounds() }
return
}
func (core *Core) SetDrawCallback (draw func (region tomo.Image)) {
core.drawCallback = draw
}
func (core *Core) SetMinimumSizeChangeCallback (
notify func (width, height int),
) {
core.minimumSizeChangeCallback = notify
}
func (core Core) HasImage () (has bool) {
has = core.RGBA != nil
return
}
func (core Core) PushRegion (bounds image.Rectangle) {
if core.drawCallback != nil {
core.drawCallback(core.SubImage(bounds).
(*image.RGBA))
}
}
func (core Core) PushAll () {
core.PushRegion(core.Bounds())
}
func (core *Core) AllocateCanvas (width, height int) {
width, height, _ = core.ConstrainSize(width, height)
core.RGBA = image.NewRGBA(image.Rect (0, 0, width, height))
}
func (core Core) MinimumWidth () (minimum int) {
minimum = core.metrics.minimumWidth
return
}
func (core Core) MinimumHeight () (minimum int) {
minimum = core.metrics.minimumHeight
return
}
func (core *Core) SetMinimumSize (width, height int) {
if width != core.metrics.minimumWidth ||
height != core.metrics.minimumHeight {
core.metrics.minimumWidth = width
core.metrics.minimumHeight = height
if core.minimumSizeChangeCallback != nil {
core.minimumSizeChangeCallback(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 core.HasImage() {
bounds := core.Bounds()
imageWidth,
imageHeight,
constrained := core.ConstrainSize (
bounds.Dx(),
bounds.Dy())
if constrained {
core.parent.Handle (tomo.EventResize {
Width: imageWidth,
Height: imageHeight,
})
}
}
}
}
func (core Core) ConstrainSize (
inWidth, inHeight int,
) (
outWidth, outHeight int,
constrained bool,
) {
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
}

114
elements/basic/label.go Normal file
View File

@@ -0,0 +1,114 @@
package basic
import "image"
import "image/color"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/artist"
type Label struct {
core Core
text string
drawer artist.TextDrawer
}
func NewLabel (text string) (element *Label) {
element = &Label { }
element.core = NewCore(element)
face := theme.FontFaceRegular()
element.drawer.SetFace(face)
element.SetText(text)
// FIXME: set the minimum size to one char
metrics := face.Metrics()
emspace, _ := face.GlyphAdvance('M')
intEmspace := emspace.Round()
if intEmspace < 1 { intEmspace = theme.Padding()}
element.core.SetMinimumSize(intEmspace, metrics.Height.Round())
return
}
func (element *Label) Handle (event tomo.Event) {
switch event.(type) {
case tomo.EventResize:
resizeEvent := event.(tomo.EventResize)
element.core.AllocateCanvas (
resizeEvent.Width,
resizeEvent.Height)
element.drawer.SetMaxWidth (resizeEvent.Width)
element.drawer.SetMaxHeight(resizeEvent.Height)
element.draw()
}
return
}
func (element *Label) SetText (text string) {
if element.text == text { return }
element.text = text
element.drawer.SetText(text)
if element.core.HasImage () {
element.draw()
element.core.PushAll()
}
}
func (element *Label) ColorModel () (model color.Model) {
return color.RGBAModel
}
func (element *Label) At (x, y int) (pixel color.Color) {
pixel = element.core.At(x, y)
return
}
func (element *Label) RGBAAt (x, y int) (pixel color.RGBA) {
pixel = element.core.RGBAAt(x, y)
return
}
func (element *Label) Bounds () (bounds image.Rectangle) {
bounds = element.core.Bounds()
return
}
func (element *Label) SetDrawCallback (draw func (region tomo.Image)) {
element.core.SetDrawCallback(draw)
}
func (element *Label) SetMinimumSizeChangeCallback (
notify func (width, height int),
) {
element.core.SetMinimumSizeChangeCallback(notify)
}
func (element *Label) Selectable () (selectable bool) {
return
}
func (element *Label) MinimumWidth () (minimum int) {
minimum = element.core.MinimumWidth()
return
}
func (element *Label) MinimumHeight () (minimum int) {
minimum = element.core.MinimumHeight()
return
}
func (element *Label) draw () {
bounds := element.core.Bounds()
artist.Rectangle (
element.core,
theme.BackgroundImage(),
nil, 0,
bounds)
textBounds := element.drawer.LayoutBounds()
foreground := theme.ForegroundImage()
element.drawer.Draw (element.core, foreground, image.Point {
X: 0 - textBounds.Min.X,
Y: 0 - textBounds.Min.Y,
})
}

90
elements/basic/test.go Normal file
View File

@@ -0,0 +1,90 @@
package basic
import "image"
import "image/color"
import "git.tebibyte.media/sashakoshka/tomo"
import "git.tebibyte.media/sashakoshka/tomo/artist"
// Test is a simple element that can be used as a placeholder.
type Test struct {
core Core
}
// NewTest creates a new test element.
func NewTest () (element *Test) {
element = &Test { }
element.core = NewCore(element)
element.core.SetMinimumSize(32, 32)
return
}
func (element *Test) Handle (event tomo.Event) {
switch event.(type) {
case tomo.EventResize:
resizeEvent := event.(tomo.EventResize)
element.core.AllocateCanvas (
resizeEvent.Width,
resizeEvent.Height)
for y := 0; y < resizeEvent.Height; y ++ {
for x := 0; x < resizeEvent.Width; x ++ {
pixel := color.RGBA {
R: 0x40, G: 0x80, B: 0x90, A: 0xFF,
}
element.core.SetRGBA (x, y, pixel)
}}
artist.Line (
element.core, artist.NewUniform(color.White), 1,
image.Pt(0, 0),
image.Pt(resizeEvent.Width, resizeEvent.Height))
artist.Line (
element.core, artist.NewUniform(color.White), 1,
image.Pt(0, resizeEvent.Height),
image.Pt(resizeEvent.Width, 0))
default:
}
return
}
func (element *Test) ColorModel () (model color.Model) {
return color.RGBAModel
}
func (element *Test) At (x, y int) (pixel color.Color) {
pixel = element.core.At(x, y)
return
}
func (element *Test) RGBAAt (x, y int) (pixel color.RGBA) {
pixel = element.core.RGBAAt(x, y)
return
}
func (element *Test) Bounds () (bounds image.Rectangle) {
bounds = element.core.Bounds()
return
}
func (element *Test) SetDrawCallback (draw func (region tomo.Image)) {
element.core.SetDrawCallback(draw)
}
func (element *Test) SetMinimumSizeChangeCallback (
notify func (width, height int),
) {
element.core.SetMinimumSizeChangeCallback(notify)
}
func (element *Test) Selectable () (selectable bool) {
return
}
func (element *Test) MinimumWidth () (minimum int) {
minimum = element.core.MinimumWidth()
return
}
func (element *Test) MinimumHeight () (minimum int) {
minimum = element.core.MinimumHeight()
return
}