-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.go
More file actions
152 lines (130 loc) · 2.68 KB
/
Copy pathinput.go
File metadata and controls
152 lines (130 loc) · 2.68 KB
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
package microui
import "github.com/user/microui-go/types"
// MouseButton represents a mouse button.
type MouseButton int
const (
MouseLeft MouseButton = iota
MouseRight
MouseMiddle
)
// Key represents a keyboard key.
type Key int
const (
KeyShift Key = iota
KeyCtrl
KeyAlt
KeyEnter
KeyBackspace
KeyDelete
KeyEscape
KeyLeft
KeyRight
KeyUp
KeyDown
KeyHome
KeyEnd
KeyPageUp
KeyPageDown
KeyTab
KeySpace
)
// InputEvent is a union type for input events.
type InputEvent interface {
isInput()
}
// MouseEvent represents a mouse event.
type MouseEvent struct {
X, Y int
Btn MouseButton
Down bool
}
func (MouseEvent) isInput() {}
// KeyEvent represents a keyboard event.
type KeyEvent struct {
Key Key
Down bool
}
func (KeyEvent) isInput() {}
// TextEvent represents text input.
type TextEvent struct {
Rune rune
}
func (TextEvent) isInput() {}
// MouseMove updates the mouse position.
func (u *UI) MouseMove(x, y int) {
u.mu.Lock()
u.input.MousePos = types.Vec2{X: x, Y: y}
u.mu.Unlock()
}
// MouseDown handles a mouse button press.
func (u *UI) MouseDown(x, y int, btn MouseButton) {
u.mu.Lock()
u.input.MousePos = types.Vec2{X: x, Y: y}
u.input.MouseDown[btn] = true
u.input.MousePressed[btn] = true
u.mu.Unlock()
}
// MouseUp handles a mouse button release.
func (u *UI) MouseUp(x, y int, btn MouseButton) {
u.mu.Lock()
u.input.MousePos = types.Vec2{X: x, Y: y}
u.input.MouseDown[btn] = false
u.mu.Unlock()
}
// Scroll handles mouse wheel scrolling.
// dx, dy are scroll deltas (positive = scroll right/down).
func (u *UI) Scroll(dx, dy int) {
u.mu.Lock()
u.input.ScrollDelta.X += dx
u.input.ScrollDelta.Y += dy
u.mu.Unlock()
}
// KeyDown handles a key press.
func (u *UI) KeyDown(key Key) {
u.mu.Lock()
if !u.input.KeyDown[key] {
u.input.KeyPressed[key] = true // Only set on initial press
}
u.input.KeyDown[key] = true
u.mu.Unlock()
}
// KeyUp handles a key release.
func (u *UI) KeyUp(key Key) {
u.mu.Lock()
delete(u.input.KeyDown, key)
u.mu.Unlock()
}
// TextChar handles single character text input.
func (u *UI) TextChar(r rune) {
u.mu.Lock()
u.input.TextInput += string(r)
u.mu.Unlock()
}
// TextInput adds text input for the current frame.
func (u *UI) TextInput(text string) {
u.mu.Lock()
u.input.TextInput += text
u.mu.Unlock()
}
// InputChan returns the channel for sending input events.
func (u *UI) InputChan() chan InputEvent {
return u.inputCh
}
func (u *UI) handleInput(ev InputEvent) {
switch e := ev.(type) {
case MouseEvent:
if e.Down {
u.MouseDown(e.X, e.Y, e.Btn)
} else {
u.MouseUp(e.X, e.Y, e.Btn)
}
case KeyEvent:
if e.Down {
u.KeyDown(e.Key)
} else {
u.KeyUp(e.Key)
}
case TextEvent:
u.TextChar(e.Rune)
}
}