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

93 lines
2.5 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-02-01 23:48:16 -07:00
import "git.tebibyte.media/sashakoshka/tomo/input"
import "git.tebibyte.media/sashakoshka/tomo/theme"
2023-02-26 12:27:38 -07:00
import "git.tebibyte.media/sashakoshka/tomo/config"
2023-01-08 23:03:19 -07:00
import "git.tebibyte.media/sashakoshka/tomo/artist"
2023-02-26 12:27:38 -07:00
import "git.tebibyte.media/sashakoshka/tomo/artist/shapes"
2023-01-09 19:25:11 -07:00
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
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-01-09 19:25:11 -07:00
*core.Core
core core.CoreControl
2023-01-10 15:34:40 -07:00
drawing bool
lastMousePos image.Point
2023-02-08 12:36:14 -07:00
config config.Config
theme theme.Theme
c theme.Case
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 21:19:04 -06:00
element = &Mouse { c: theme.C("tomo", "mouse") }
element.Core, element.core = core.NewCore(element, element.draw)
2023-01-08 23:03:19 -07:00
element.core.SetMinimumSize(32, 32)
return
}
2023-02-08 12:36:14 -07:00
// SetTheme sets the element's theme.
func (element *Mouse) SetTheme (new theme.Theme) {
element.theme = new
element.redo()
}
// SetConfig sets the element's configuration.
func (element *Mouse) SetConfig (new config.Config) {
element.config = new
element.redo()
}
2023-02-03 16:32:22 -07:00
func (element *Mouse) redo () {
if !element.core.HasImage() { return }
element.draw()
element.core.DamageAll()
}
func (element *Mouse) draw () {
2023-01-24 14:41:12 -07:00
bounds := element.Bounds()
2023-02-26 20:20:17 -07:00
accent := element.theme.Color (
theme.ColorAccent,
theme.State { },
2023-02-12 23:49:33 -07:00
element.c)
2023-02-26 20:20:17 -07:00
shapes.FillColorRectangle(element.core, accent, bounds)
2023-02-26 12:27:38 -07:00
shapes.StrokeColorRectangle (
2023-02-12 23:49:33 -07:00
element.core,
2023-02-26 12:27:38 -07:00
artist.Hex(0x000000FF),
bounds, 1)
shapes.ColorLine (
element.core, 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 (
element.core, 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-01-15 22:35:05 -07:00
}
2023-01-10 15:34:40 -07:00
2023-02-01 23:48:16 -07:00
func (element *Mouse) HandleMouseDown (x, y int, button input.Button) {
2023-01-15 22:35:05 -07:00
element.drawing = true
element.lastMousePos = image.Pt(x, y)
}
2023-01-10 15:34:40 -07:00
2023-02-01 23:48:16 -07:00
func (element *Mouse) HandleMouseUp (x, y int, button input.Button) {
2023-01-15 22:35:05 -07:00
element.drawing = false
mousePos := image.Pt(x, y)
2023-02-26 12:27:38 -07:00
element.core.DamageRegion (shapes.ColorLine (
element.core, artist.Hex(0x000000FF), 1,
2023-01-15 22:35:05 -07:00
element.lastMousePos, mousePos))
element.lastMousePos = mousePos
2023-01-08 23:03:19 -07:00
}
2023-01-15 22:35:05 -07:00
func (element *Mouse) HandleMotion (x, y int) {
2023-01-15 22:35:05 -07:00
if !element.drawing { return }
mousePos := image.Pt(x, y)
2023-02-26 12:27:38 -07:00
element.core.DamageRegion (shapes.ColorLine (
element.core, artist.Hex(0x000000FF), 1,
2023-01-15 22:35:05 -07:00
element.lastMousePos, mousePos))
element.lastMousePos = mousePos
}