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

87 lines
2.1 KiB
Go
Raw Permalink Normal View History

2023-01-12 17:51:42 +00:00
package testing
2023-01-09 06:03:19 +00:00
import "image"
2023-05-03 23:40:30 +00:00
import "tomo"
import "tomo/input"
import "art"
import "art/shapes"
import "art/artutil"
2023-04-30 22:18:45 +00:00
var mouseCase = tomo.C("tomo", "mouse")
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-05-03 02:19:29 +00:00
entity tomo.Entity
2023-04-13 03:46:29 +00:00
pressed bool
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) {
2023-03-31 05:06:29 +00:00
element = &Mouse { }
2023-05-03 02:19:29 +00:00
element.entity = tomo.GetBackend().NewEntity(element)
2023-04-15 22:30:22 +00:00
element.entity.SetMinimumSize(32, 32)
2023-01-09 06:03:19 +00:00
return
}
2023-04-15 22:30:22 +00:00
func (element *Mouse) Entity () tomo.Entity {
return element.entity
2023-02-08 19:36:14 +00:00
}
func (element *Mouse) Draw (destination art.Canvas) {
2023-04-13 03:46:29 +00:00
bounds := element.entity.Bounds()
2023-04-30 22:18:45 +00:00
accent := element.entity.Theme().Color (
2023-03-31 05:06:29 +00:00
tomo.ColorAccent,
2023-04-30 22:18:45 +00:00
tomo.State { },
mouseCase)
2023-04-13 03:46:29 +00:00
shapes.FillColorRectangle(destination, accent, bounds)
2023-02-26 19:27:38 +00:00
shapes.StrokeColorRectangle (
2023-04-13 03:46:29 +00:00
destination,
2023-04-30 22:18:45 +00:00
artutil.Hex(0x000000FF),
2023-02-26 19:27:38 +00:00
bounds, 1)
shapes.ColorLine (
2023-04-30 22:18:45 +00:00
destination, artutil.Hex(0xFFFFFFFF), 1,
2023-01-31 23:39:17 +00:00
bounds.Min.Add(image.Pt(1, 1)),
bounds.Min.Add(image.Pt(bounds.Dx() - 2, bounds.Dy() - 2)))
2023-02-26 19:27:38 +00:00
shapes.ColorLine (
2023-04-30 22:18:45 +00:00
destination, artutil.Hex(0xFFFFFFFF), 1,
2023-01-31 23:39:17 +00:00
bounds.Min.Add(image.Pt(1, bounds.Dy() - 2)),
bounds.Min.Add(image.Pt(bounds.Dx() - 2, 1)))
2023-04-13 03:46:29 +00:00
if element.pressed {
2023-04-22 01:48:38 +00:00
midpoint := bounds.Min.Add(bounds.Max.Sub(bounds.Min).Div(2))
2023-04-13 03:46:29 +00:00
shapes.ColorLine (
2023-04-30 22:18:45 +00:00
destination, artutil.Hex(0x000000FF), 1,
2023-04-22 01:48:38 +00:00
midpoint, element.lastMousePos)
2023-04-13 03:46:29 +00:00
}
}
2023-04-30 22:18:45 +00:00
func (element *Mouse) HandleThemeChange (new tomo.Theme) {
2023-04-13 03:46:29 +00:00
element.entity.Invalidate()
2023-01-16 05:35:05 +00:00
}
2023-01-10 22:34:40 +00:00
2023-04-22 01:48:38 +00:00
func (element *Mouse) HandleMouseDown (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
2023-04-13 03:46:29 +00:00
element.pressed = true
2023-04-22 01:48:38 +00:00
element.lastMousePos = position
element.entity.Invalidate()
2023-01-16 05:35:05 +00:00
}
2023-01-10 22:34:40 +00:00
2023-04-22 01:48:38 +00:00
func (element *Mouse) HandleMouseUp (
position image.Point,
button input.Button,
modifiers input.Modifiers,
) {
2023-04-13 03:46:29 +00:00
element.pressed = false
2023-04-22 01:48:38 +00:00
element.entity.Invalidate()
2023-01-09 06:03:19 +00:00
}
2023-01-16 05:35:05 +00:00
2023-04-22 01:48:38 +00:00
func (element *Mouse) HandleMotion (position image.Point) {
2023-04-13 03:46:29 +00:00
if !element.pressed { return }
2023-04-22 01:48:38 +00:00
element.lastMousePos = position
2023-04-13 03:46:29 +00:00
element.entity.Invalidate()
2023-01-16 05:35:05 +00:00
}