2023-01-12 17:51:42 +00:00
|
|
|
package testing
|
2023-01-09 06:03:19 +00:00
|
|
|
|
|
|
|
import "image"
|
|
|
|
import "image/color"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
2023-01-11 20:46:48 +00:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/theme"
|
2023-01-09 06:03:19 +00:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/artist"
|
2023-01-10 02:25:11 +00:00
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/elements/core"
|
2023-01-09 06:03:19 +00:00
|
|
|
|
2023-01-12 17:51:42 +00: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-10 02:25:11 +00:00
|
|
|
*core.Core
|
|
|
|
core core.CoreControl
|
2023-01-10 22:34:40 +00:00
|
|
|
drawing bool
|
2023-01-14 06:54:57 +00:00
|
|
|
color artist.Pattern
|
2023-01-10 22:34:40 +00:00
|
|
|
lastMousePos image.Point
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 17:51:42 +00:00
|
|
|
// NewMouse creates a new mouse test element.
|
|
|
|
func NewMouse () (element *Mouse) {
|
|
|
|
element = &Mouse { }
|
2023-01-10 02:25:11 +00:00
|
|
|
element.Core, element.core = core.NewCore(element)
|
2023-01-09 06:03:19 +00:00
|
|
|
element.core.SetMinimumSize(32, 32)
|
2023-01-10 22:34:40 +00:00
|
|
|
element.color = artist.NewUniform(color.Black)
|
2023-01-09 06:03:19 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-12 17:51:42 +00:00
|
|
|
func (element *Mouse) Handle (event tomo.Event) {
|
2023-01-09 06:03:19 +00:00
|
|
|
switch event.(type) {
|
|
|
|
case tomo.EventResize:
|
|
|
|
resizeEvent := event.(tomo.EventResize)
|
|
|
|
element.core.AllocateCanvas (
|
|
|
|
resizeEvent.Width,
|
|
|
|
resizeEvent.Height)
|
2023-01-14 06:54:57 +00:00
|
|
|
artist.FillRectangle (
|
2023-01-11 01:31:18 +00:00
|
|
|
element.core,
|
2023-01-15 02:01:00 +00:00
|
|
|
theme.AccentPattern(),
|
2023-01-14 06:54:57 +00:00
|
|
|
element.Bounds())
|
2023-01-14 18:57:12 +00:00
|
|
|
artist.StrokeRectangle (
|
|
|
|
element.core,
|
2023-01-14 18:59:07 +00:00
|
|
|
artist.NewUniform(color.Black), 1,
|
2023-01-14 18:57:12 +00:00
|
|
|
element.Bounds())
|
2023-01-09 06:03:19 +00:00
|
|
|
artist.Line (
|
2023-01-15 01:08:55 +00:00
|
|
|
element.core, artist.NewUniform(color.White), 3,
|
2023-01-11 01:31:18 +00:00
|
|
|
image.Pt(1, 1),
|
|
|
|
image.Pt(resizeEvent.Width - 2, resizeEvent.Height - 2))
|
2023-01-09 06:03:19 +00:00
|
|
|
artist.Line (
|
|
|
|
element.core, artist.NewUniform(color.White), 1,
|
2023-01-11 01:31:18 +00:00
|
|
|
image.Pt(1, resizeEvent.Height - 2),
|
|
|
|
image.Pt(resizeEvent.Width - 2, 1))
|
2023-01-09 06:03:19 +00:00
|
|
|
|
2023-01-10 22:34:40 +00:00
|
|
|
case tomo.EventMouseDown:
|
|
|
|
element.drawing = true
|
|
|
|
mouseDownEvent := event.(tomo.EventMouseDown)
|
|
|
|
element.lastMousePos = image.Pt (
|
|
|
|
mouseDownEvent.X,
|
|
|
|
mouseDownEvent.Y)
|
|
|
|
|
|
|
|
case tomo.EventMouseUp:
|
|
|
|
element.drawing = false
|
|
|
|
mouseUpEvent := event.(tomo.EventMouseUp)
|
|
|
|
mousePos := image.Pt (
|
|
|
|
mouseUpEvent.X,
|
|
|
|
mouseUpEvent.Y)
|
|
|
|
element.core.PushRegion (artist.Line (
|
|
|
|
element.core, element.color, 1,
|
|
|
|
element.lastMousePos, mousePos))
|
|
|
|
element.lastMousePos = mousePos
|
|
|
|
|
|
|
|
case tomo.EventMouseMove:
|
2023-01-14 19:00:57 +00:00
|
|
|
if !element.drawing { return }
|
2023-01-10 22:34:40 +00:00
|
|
|
mouseMoveEvent := event.(tomo.EventMouseMove)
|
|
|
|
mousePos := image.Pt (
|
|
|
|
mouseMoveEvent.X,
|
|
|
|
mouseMoveEvent.Y)
|
|
|
|
element.core.PushRegion (artist.Line (
|
|
|
|
element.core, element.color, 1,
|
|
|
|
element.lastMousePos, mousePos))
|
|
|
|
element.lastMousePos = mousePos
|
2023-01-09 06:03:19 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|