forked from aarzilli/nucular
/
input.go
122 lines (100 loc) · 2.69 KB
/
input.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package nucular
import (
"image"
"github.com/Varunram/nucular/rect"
"golang.org/x/mobile/event/key"
"golang.org/x/mobile/event/mouse"
)
type mouseButton struct {
Down bool
Clicked bool
ClickedPos image.Point
}
type MouseInput struct {
valid bool
clip rect.Rect
Buttons [4]mouseButton
Pos image.Point
Prev image.Point
Delta image.Point
ScrollDelta int
}
type KeyboardInput struct {
Keys []key.Event
Text string
}
type Input struct {
Keyboard KeyboardInput
Mouse MouseInput
}
func (win *Window) Input() *Input {
if !win.toplevel() {
return &Input{}
}
win.ctx.Input.Mouse.clip = win.cmds.Clip
return &win.ctx.Input
}
func (win *Window) KeyboardOnHover(bounds rect.Rect) KeyboardInput {
if !win.toplevel() || !win.ctx.Input.Mouse.HoveringRect(bounds) {
return KeyboardInput{}
}
return win.ctx.Input.Keyboard
}
func (i *MouseInput) HasClickInRect(id mouse.Button, b rect.Rect) bool {
btn := &i.Buttons[id]
return unify(b, i.clip).Contains(btn.ClickedPos)
}
func (i *MouseInput) IsClickInRect(id mouse.Button, b rect.Rect) bool {
return i.IsClickDownInRect(id, b, false)
}
func (i *MouseInput) IsClickDownInRect(id mouse.Button, b rect.Rect, down bool) bool {
btn := &i.Buttons[id]
return i.HasClickInRect(id, b) && btn.Down == down && btn.Clicked
}
func (i *MouseInput) AnyClickInRect(b rect.Rect) bool {
return i.IsClickInRect(mouse.ButtonLeft, b) || i.IsClickInRect(mouse.ButtonMiddle, b) || i.IsClickInRect(mouse.ButtonRight, b)
}
func (i *MouseInput) HoveringRect(rect rect.Rect) bool {
return i.valid && unify(rect, i.clip).Contains(i.Pos)
}
func (i *MouseInput) PrevHoveringRect(rect rect.Rect) bool {
return i.valid && unify(rect, i.clip).Contains(i.Prev)
}
func (i *MouseInput) Clicked(id mouse.Button, rect rect.Rect) bool {
if !i.HoveringRect(rect) {
return false
}
return i.IsClickInRect(id, rect)
}
func (i *MouseInput) Down(id mouse.Button) bool {
return i.Buttons[id].Down
}
func (i *MouseInput) Pressed(id mouse.Button) bool {
return i.Buttons[id].Down && i.Buttons[id].Clicked
}
func (i *MouseInput) Released(id mouse.Button) bool {
return !(i.Buttons[id].Down) && i.Buttons[id].Clicked
}
func (i *KeyboardInput) Pressed(key key.Code) bool {
for _, k := range i.Keys {
if k.Code == key {
return true
}
}
return false
}
func (win *Window) inputMaybe(widgetValid bool) *Input {
if widgetValid && win.toplevel() && win.flags&windowEnabled != 0 {
win.ctx.Input.Mouse.clip = win.cmds.Clip
return &win.ctx.Input
}
return &Input{}
}
func (win *Window) toplevel() bool {
for i := len(win.ctx.Windows) - 1; i >= 0; i-- {
if win.ctx.Windows[i].flags&windowTooltip == 0 {
return win.idx == i
}
}
return false
}