/
tool.go
96 lines (80 loc) · 2.61 KB
/
tool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package image
import (
"fmt"
"image/color"
"github.com/gregjohnson2017/tabula-editor/pkg/ui"
"github.com/veandco/go-sdl2/sdl"
)
// Tool defines the behavior of tools used for the image view.
type Tool interface {
OnClick(evt *sdl.MouseButtonEvent, iv *View)
OnMotion(evt *sdl.MouseMotionEvent, iv *View)
fmt.Stringer
}
// Make sure the tools satisfy the interface
var _ Tool = Tool(EmptyTool{})
var _ Tool = Tool(&PixelSelectionTool{})
var _ Tool = Tool(&PixelColorTool{})
// EmptyTool does nothing.
type EmptyTool struct {
}
// OnClick does nothing.
func (t EmptyTool) OnClick(evt *sdl.MouseButtonEvent, iv *View) {
}
// OnMotion does nothing.
func (t EmptyTool) OnMotion(evt *sdl.MouseMotionEvent, iv *View) {
}
func (t EmptyTool) String() string {
return "image.EmptyTool"
}
// PixelSelectionTool selects any clicked pixels
type PixelSelectionTool struct {
lastDrag sdl.Point
}
// OnClick is called when the user clicks within the Image View's region and the
// tool is currently active for the image view.
func (t *PixelSelectionTool) OnClick(evt *sdl.MouseButtonEvent, iv *View) {
if evt.Button == sdl.BUTTON_LEFT && evt.State == sdl.PRESSED {
_ = iv.SelectPixel(iv.mousePix)
t.lastDrag = iv.mousePix
}
}
// OnMotion is called when the user clicks within the Image View's region and
// the tool is currently active for the image view.
func (t *PixelSelectionTool) OnMotion(evt *sdl.MouseMotionEvent, iv *View) {
if evt.State == sdl.ButtonLMask() {
points := ui.Interpolate(iv.mousePix, t.lastDrag)
for _, p := range points {
_ = iv.SelectPixel(p)
}
t.lastDrag = iv.mousePix
}
}
func (t *PixelSelectionTool) String() string {
return "image.PixelSelectionTool"
}
// PixelColorTool colors any clicked pixels purple
type PixelColorTool struct {
lastDrag sdl.Point
}
// OnClick is called when the user clicks within the Image View's region and the
// tool is currently active for the image view.
func (t *PixelColorTool) OnClick(evt *sdl.MouseButtonEvent, iv *View) {
if evt.Button == sdl.BUTTON_LEFT && evt.State == sdl.PRESSED {
_ = iv.setPixel(iv.mousePix, color.RGBA{R: 0xFF, G: 0x00, B: 0xFF, A: 0xFF})
t.lastDrag = iv.mousePix
}
}
// OnMotion is called when the user clicks within the Image View's region and
// the tool is currently active for the image view.
func (t *PixelColorTool) OnMotion(evt *sdl.MouseMotionEvent, iv *View) {
if evt.State == sdl.ButtonLMask() {
for _, p := range ui.Interpolate(iv.mousePix, t.lastDrag) {
_ = iv.setPixel(p, color.RGBA{R: 0xFF, G: 0x00, B: 0xFF, A: 0xFF})
}
t.lastDrag = iv.mousePix
}
}
func (t *PixelColorTool) String() string {
return "image.PixelColorTool"
}