This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/elements/testing/mouse.go

98 lines
2.6 KiB
Go
Raw Normal View History

2023-01-12 10:51:42 -07:00
package testing
2023-01-08 23:03:19 -07:00
import "image"
2023-03-30 23:06:29 -06:00
import "git.tebibyte.media/sashakoshka/tomo"
2023-02-01 23:48:16 -07:00
import "git.tebibyte.media/sashakoshka/tomo/input"
2023-01-08 23:03:19 -07:00
import "git.tebibyte.media/sashakoshka/tomo/artist"
2023-04-12 21:46:29 -06:00
import "git.tebibyte.media/sashakoshka/tomo/canvas"
2023-02-26 12:27:38 -07:00
import "git.tebibyte.media/sashakoshka/tomo/artist/shapes"
2023-03-30 23:06:29 -06:00
import "git.tebibyte.media/sashakoshka/tomo/default/theme"
import "git.tebibyte.media/sashakoshka/tomo/default/config"
2023-01-08 23:03:19 -07:00
2023-01-12 10:51:42 -07:00
// Mouse is an element capable of testing mouse input. When the mouse is clicked
// and dragged on it, it draws a trail.
type Mouse struct {
2023-04-12 21:46:29 -06:00
entity tomo.Entity
pressed bool
2023-01-10 15:34:40 -07:00
lastMousePos image.Point
2023-02-08 12:36:14 -07:00
2023-03-30 23:06:29 -06:00
config config.Wrapped
theme theme.Wrapped
2023-01-08 23:03:19 -07:00
}
2023-01-12 10:51:42 -07:00
// NewMouse creates a new mouse test element.
func NewMouse () (element *Mouse) {
2023-03-30 23:06:29 -06:00
element = &Mouse { }
2023-04-12 21:46:29 -06:00
element.theme.Case = tomo.C("tomo", "mouse")
2023-04-15 16:30:22 -06:00
element.entity = tomo.NewEntity(element)
element.entity.SetMinimumSize(32, 32)
2023-01-08 23:03:19 -07:00
return
}
2023-04-15 16:30:22 -06:00
func (element *Mouse) Entity () tomo.Entity {
return element.entity
2023-02-08 12:36:14 -07:00
}
2023-04-12 21:46:29 -06:00
func (element *Mouse) Draw (destination canvas.Canvas) {
bounds := element.entity.Bounds()
2023-02-26 20:20:17 -07:00
accent := element.theme.Color (
2023-03-30 23:06:29 -06:00
tomo.ColorAccent,
tomo.State { })
2023-04-12 21:46:29 -06:00
shapes.FillColorRectangle(destination, accent, bounds)
2023-02-26 12:27:38 -07:00
shapes.StrokeColorRectangle (
2023-04-12 21:46:29 -06:00
destination,
2023-02-26 12:27:38 -07:00
artist.Hex(0x000000FF),
bounds, 1)
shapes.ColorLine (
2023-04-12 21:46:29 -06:00
destination, artist.Hex(0xFFFFFFFF), 1,
2023-01-31 16:39:17 -07:00
bounds.Min.Add(image.Pt(1, 1)),
bounds.Min.Add(image.Pt(bounds.Dx() - 2, bounds.Dy() - 2)))
2023-02-26 12:27:38 -07:00
shapes.ColorLine (
2023-04-12 21:46:29 -06:00
destination, artist.Hex(0xFFFFFFFF), 1,
2023-01-31 16:39:17 -07:00
bounds.Min.Add(image.Pt(1, bounds.Dy() - 2)),
bounds.Min.Add(image.Pt(bounds.Dx() - 2, 1)))
2023-04-12 21:46:29 -06:00
if element.pressed {
2023-04-21 19:48:38 -06:00
midpoint := bounds.Min.Add(bounds.Max.Sub(bounds.Min).Div(2))
2023-04-12 21:46:29 -06:00
shapes.ColorLine (
destination, artist.Hex(0x000000FF), 1,
2023-04-21 19:48:38 -06:00
midpoint, element.lastMousePos)
2023-04-12 21:46:29 -06:00
}
}
// SetTheme sets the element's theme.
func (element *Mouse) SetTheme (new tomo.Theme) {
element.theme.Theme = new
element.entity.Invalidate()
}
// SetConfig sets the element's configuration.
func (element *Mouse) SetConfig (new tomo.Config) {
element.config.Config = new
element.entity.Invalidate()
2023-01-15 22:35:05 -07:00
}
2023-01-10 15:34:40 -07:00
2023-04-21 19:48:38 -06:00
func (element *Mouse) HandleMouseDown (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
2023-04-12 21:46:29 -06:00
element.pressed = true
2023-04-21 19:48:38 -06:00
element.lastMousePos = position
element.entity.Invalidate()
2023-01-15 22:35:05 -07:00
}
2023-01-10 15:34:40 -07:00
2023-04-21 19:48:38 -06:00
func (element *Mouse) HandleMouseUp (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
2023-04-12 21:46:29 -06:00
element.pressed = false
2023-04-21 19:48:38 -06:00
element.entity.Invalidate()
2023-01-08 23:03:19 -07:00
}
2023-01-15 22:35:05 -07:00
2023-04-21 19:48:38 -06:00
func (element *Mouse) HandleMotion (position image.Point) {
2023-04-12 21:46:29 -06:00
if !element.pressed { return }
2023-04-21 19:48:38 -06:00
element.lastMousePos = position
2023-04-12 21:46:29 -06:00
element.entity.Invalidate()
2023-01-15 22:35:05 -07:00
}