2023-01-12 10:51:42 -07:00
|
|
|
package testing
|
2023-01-08 23:03:19 -07:00
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "image/color"
|
2023-02-01 23:48:16 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/input"
|
2023-02-08 12:36:14 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/config"
|
2023-01-11 13:46:48 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
2023-01-08 23:03:19 -07:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
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
|
2023-01-13 23:54:57 -07:00
|
|
|
color artist.Pattern
|
2023-01-10 15:34:40 -07:00
|
|
|
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-02-08 12:36:14 -07:00
|
|
|
element = &Mouse { c: theme.C("testing", "mouse") }
|
|
|
|
element.Core, element.core = core.NewCore(element.draw)
|
2023-01-08 23:03:19 -07:00
|
|
|
element.core.SetMinimumSize(32, 32)
|
2023-01-10 15:34:40 -07:00
|
|
|
element.color = artist.NewUniform(color.Black)
|
2023-01-08 23:03:19 -07:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:57:02 -07:00
|
|
|
func (element *Mouse) draw () {
|
2023-01-24 14:41:12 -07:00
|
|
|
bounds := element.Bounds()
|
2023-02-08 12:36:14 -07:00
|
|
|
pattern := element.theme.Pattern (
|
|
|
|
theme.PatternAccent,
|
|
|
|
element.c,
|
|
|
|
theme.PatternState { })
|
2023-01-31 14:18:10 -07:00
|
|
|
artist.FillRectangle(element, pattern, bounds)
|
2023-01-15 22:35:05 -07:00
|
|
|
artist.StrokeRectangle (
|
2023-01-31 14:18:10 -07:00
|
|
|
element,
|
2023-01-15 22:35:05 -07:00
|
|
|
artist.NewUniform(color.Black), 1,
|
2023-01-24 14:41:12 -07:00
|
|
|
bounds)
|
2023-01-15 22:35:05 -07:00
|
|
|
artist.Line (
|
2023-01-31 14:18:10 -07:00
|
|
|
element, artist.NewUniform(color.White), 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-01-15 22:35:05 -07:00
|
|
|
artist.Line (
|
2023-01-31 14:18:10 -07:00
|
|
|
element, artist.NewUniform(color.White), 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-01-19 13:05:13 -07:00
|
|
|
element.core.DamageRegion (artist.Line (
|
2023-01-31 14:18:10 -07:00
|
|
|
element, element.color, 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) HandleMouseMove (x, y int) {
|
|
|
|
if !element.drawing { return }
|
|
|
|
mousePos := image.Pt(x, y)
|
2023-01-19 13:05:13 -07:00
|
|
|
element.core.DamageRegion (artist.Line (
|
2023-01-31 14:18:10 -07:00
|
|
|
element, element.color, 1,
|
2023-01-15 22:35:05 -07:00
|
|
|
element.lastMousePos, mousePos))
|
|
|
|
element.lastMousePos = mousePos
|
|
|
|
}
|
|
|
|
|
2023-01-19 13:05:13 -07:00
|
|
|
func (element *Mouse) HandleMouseScroll (x, y int, deltaX, deltaY float64) { }
|