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

71 lines
2.0 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"
import "image/color"
import "git.tebibyte.media/sashakoshka/tomo"
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
color artist.Pattern
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) {
element = &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
}
func (element *Mouse) draw () {
2023-01-24 14:41:12 -07:00
bounds := element.Bounds()
pattern, _ := theme.AccentPattern(theme.PatternState { })
artist.FillRectangle(element.core, pattern, bounds)
2023-01-15 22:35:05 -07:00
artist.StrokeRectangle (
element.core,
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-24 14:41:12 -07:00
element.core, artist.NewUniform(color.White), 1,
2023-01-15 22:35:05 -07:00
image.Pt(1, 1),
2023-01-24 14:41:12 -07:00
image.Pt(bounds.Dx() - 2, bounds.Dy() - 2))
2023-01-15 22:35:05 -07:00
artist.Line (
element.core, artist.NewUniform(color.White), 1,
2023-01-24 14:41:12 -07:00
image.Pt(1, bounds.Dy() - 2),
image.Pt(bounds.Dx() - 2, 1))
2023-01-15 22:35:05 -07:00
}
2023-01-10 15:34:40 -07:00
2023-01-15 22:35:05 -07:00
func (element *Mouse) HandleMouseDown (x, y int, button tomo.Button) {
element.drawing = true
element.lastMousePos = image.Pt(x, y)
}
2023-01-10 15:34:40 -07:00
2023-01-15 22:35:05 -07:00
func (element *Mouse) HandleMouseUp (x, y int, button tomo.Button) {
element.drawing = false
mousePos := image.Pt(x, y)
2023-01-19 13:05:13 -07:00
element.core.DamageRegion (artist.Line (
2023-01-15 22:35:05 -07:00
element.core, element.color, 1,
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-15 22:35:05 -07:00
element.core, element.color, 1,
element.lastMousePos, mousePos))
element.lastMousePos = mousePos
}
2023-01-19 13:05:13 -07:00
func (element *Mouse) HandleMouseScroll (x, y int, deltaX, deltaY float64) { }