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

88 lines
2.3 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-30 16:18:45 -06:00
import "git.tebibyte.media/sashakoshka/tomo/ability"
2023-02-26 12:27:38 -07:00
import "git.tebibyte.media/sashakoshka/tomo/artist/shapes"
2023-04-30 16:18:45 -06:00
import "git.tebibyte.media/sashakoshka/tomo/artist/artutil"
var mouseCase = tomo.C("tomo", "mouse")
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-30 16:18:45 -06:00
entity ability.ThemeableEntity
2023-04-12 21:46:29 -06:00
pressed bool
2023-01-10 15:34:40 -07:00
lastMousePos image.Point
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-30 16:18:45 -06:00
element.entity = tomo.GetBackend().NewEntity(element).(ability.ThemeableEntity)
2023-04-15 16:30:22 -06:00
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-30 16:18:45 -06:00
func (element *Mouse) Draw (destination artist.Canvas) {
2023-04-12 21:46:29 -06:00
bounds := element.entity.Bounds()
2023-04-30 16:18:45 -06:00
accent := element.entity.Theme().Color (
2023-03-30 23:06:29 -06:00
tomo.ColorAccent,
2023-04-30 16:18:45 -06:00
tomo.State { },
mouseCase)
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-04-30 16:18:45 -06:00
artutil.Hex(0x000000FF),
2023-02-26 12:27:38 -07:00
bounds, 1)
shapes.ColorLine (
2023-04-30 16:18:45 -06:00
destination, artutil.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-30 16:18:45 -06:00
destination, artutil.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 (
2023-04-30 16:18:45 -06:00
destination, artutil.Hex(0x000000FF), 1,
2023-04-21 19:48:38 -06:00
midpoint, element.lastMousePos)
2023-04-12 21:46:29 -06:00
}
}
2023-04-30 16:18:45 -06:00
func (element *Mouse) HandleThemeChange (new tomo.Theme) {
2023-04-12 21:46:29 -06:00
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
}