forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 2
/
EventHandler.go
160 lines (132 loc) · 4.05 KB
/
EventHandler.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package giu
var _ Disposable = &eventHandlerState{}
type eventHandlerState struct {
isActive bool
}
// Dispose implements Disposable interface
func (s *eventHandlerState) Dispose() {
// noop
}
type mouseEvent struct {
mouseButton MouseButton
callback func()
cond func(MouseButton) bool
}
type keyEvent struct {
key Key
callback func()
cond func(Key) bool
}
var _ Widget = &EventHandler{}
// EventHandler is a universal event handler for giu widgets.
// put giu.Event()... after any widget to handle any event
type EventHandler struct {
hover func()
mouseEvents []mouseEvent
keyEvents []keyEvent
onActivate,
onDeactivate func()
}
// Event adds a new event to widget above
func Event() *EventHandler {
return &EventHandler{
mouseEvents: make([]mouseEvent, 0),
keyEvents: make([]keyEvent, 0),
}
}
// OnHover sets callback when item gets hovered
func (eh *EventHandler) OnHover(onHover func()) *EventHandler {
eh.hover = onHover
return eh
}
// OnActivate sets callback when item gets activated
func (eh *EventHandler) OnActivate(cb func()) *EventHandler {
eh.onActivate = cb
return eh
}
// OnDeactivate sets callback when item gets deactivated
func (eh *EventHandler) OnDeactivate(cb func()) *EventHandler {
eh.onDeactivate = cb
return eh
}
// Key events
// OnKeyDown sets callback when key `key` is down
func (eh *EventHandler) OnKeyDown(key Key, cb func()) *EventHandler {
eh.keyEvents = append(eh.keyEvents, keyEvent{key, cb, IsKeyDown})
return eh
}
// OnKeyPressed sets callback when key `key` is pressed
func (eh *EventHandler) OnKeyPressed(key Key, cb func()) *EventHandler {
eh.keyEvents = append(eh.keyEvents, keyEvent{key, cb, IsKeyPressed})
return eh
}
// OnKeyReleased sets callback when key `key` is released
func (eh *EventHandler) OnKeyReleased(key Key, cb func()) *EventHandler {
eh.keyEvents = append(eh.keyEvents, keyEvent{key, cb, IsKeyReleased})
return eh
}
// Mouse events
// OnClick sets callback when mouse button `mouseButton` is clicked
func (eh *EventHandler) OnClick(mouseButton MouseButton, callback func()) *EventHandler {
eh.mouseEvents = append(eh.mouseEvents, mouseEvent{mouseButton, callback, IsMouseClicked})
return eh
}
// OnDClick sets callback when mouse button `mouseButton` is double-clicked
func (eh *EventHandler) OnDClick(mouseButton MouseButton, callback func()) *EventHandler {
eh.mouseEvents = append(eh.mouseEvents, mouseEvent{mouseButton, callback, IsMouseDoubleClicked})
return eh
}
// OnMouseDown sets callback when mouse button `mouseButton` is down
func (eh *EventHandler) OnMouseDown(mouseButton MouseButton, callback func()) *EventHandler {
eh.mouseEvents = append(eh.mouseEvents, mouseEvent{mouseButton, callback, IsMouseDown})
return eh
}
// OnMouseReleased sets callback when mouse button `mouseButton` is released
func (eh *EventHandler) OnMouseReleased(mouseButton MouseButton, callback func()) *EventHandler {
eh.mouseEvents = append(eh.mouseEvents, mouseEvent{mouseButton, callback, IsMouseReleased})
return eh
}
// Build implements Widget interface
// nolint:gocognit,gocyclo // will fix later
func (eh *EventHandler) Build() {
if eh.onActivate != nil || eh.onDeactivate != nil {
isActive := IsItemActive()
var state *eventHandlerState
stateID := GenAutoID("eventHandlerState")
if s := Context.GetState(stateID); s != nil {
state = s.(*eventHandlerState)
} else {
newState := &eventHandlerState{}
Context.SetState(stateID, newState)
state = newState
}
if eh.onActivate != nil && isActive && !state.isActive {
state.isActive = true
eh.onActivate()
}
if eh.onDeactivate != nil && !isActive && state.isActive {
state.isActive = false
eh.onDeactivate()
}
}
if !IsItemHovered() {
return
}
if eh.hover != nil {
eh.hover()
}
if len(eh.keyEvents) > 0 {
for _, event := range eh.keyEvents {
if event.callback != nil && event.cond(event.key) {
event.callback()
}
}
}
if len(eh.mouseEvents) > 0 {
for _, event := range eh.mouseEvents {
if event.callback != nil && event.cond(event.mouseButton) {
event.callback()
}
}
}
}