forked from g3n/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.go
346 lines (286 loc) · 7.39 KB
/
edit.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gui
import (
"github.com/leapar/engine/math32"
"github.com/leapar/engine/text"
"github.com/leapar/engine/window"
"strings"
"time"
)
// Edit represents a text edit box GUI element
type Edit struct {
Label // Embedded label
MaxLength int // Maximum number of characters
width int // edit width in pixels
placeHolder string // place holder string
text string // current edit text
col int // current column
focus bool // key focus flag
cursorOver bool
blinkID int
caretOn bool
styles *EditStyles
}
// EditStyle contains the styling of an Edit
type EditStyle struct {
Border RectBounds
Paddings RectBounds
BorderColor math32.Color4
BgColor math32.Color4
BgAlpha float32
FgColor math32.Color4
HolderColor math32.Color4
}
// EditStyles contains an EditStyle for each valid GUI state
type EditStyles struct {
Normal EditStyle
Over EditStyle
Focus EditStyle
Disabled EditStyle
}
const (
editMarginX = 4
blinkTime = 1000
)
// NewEdit creates and returns a pointer to a new edit widget
func NewEdit(width int, placeHolder string) *Edit {
ed := new(Edit)
ed.width = width
ed.placeHolder = placeHolder
ed.styles = &StyleDefault().Edit
ed.text = ""
ed.MaxLength = 80
ed.col = 0
ed.focus = false
ed.Label.initialize("", StyleDefault().Font)
ed.Label.Subscribe(OnKeyDown, ed.onKey)
ed.Label.Subscribe(OnKeyRepeat, ed.onKey)
ed.Label.Subscribe(OnChar, ed.onChar)
ed.Label.Subscribe(OnMouseDown, ed.onMouse)
ed.Label.Subscribe(OnCursorEnter, ed.onCursor)
ed.Label.Subscribe(OnCursorLeave, ed.onCursor)
ed.Label.Subscribe(OnEnable, func(evname string, ev interface{}) { ed.update() })
ed.update()
return ed
}
// SetText sets this edit text
func (ed *Edit) SetText(text string) *Edit {
// Remove new lines from text
ed.text = strings.Replace(text, "\n", "", -1)
ed.update()
return ed
}
// Text returns the current edited text
func (ed *Edit) Text() string {
return ed.text
}
// SetFontSize sets label font size (overrides Label.SetFontSize)
func (ed *Edit) SetFontSize(size float64) *Edit {
ed.Label.SetFontSize(size)
ed.redraw(ed.focus)
return ed
}
// SetStyles set the button styles overriding the default style
func (ed *Edit) SetStyles(es *EditStyles) {
ed.styles = es
ed.update()
}
// LostKeyFocus satisfies the IPanel interface and is called by gui root
// container when the panel loses the key focus
func (ed *Edit) LostKeyFocus() {
ed.focus = false
ed.update()
ed.root.ClearTimeout(ed.blinkID)
}
// CursorPos sets the position of the cursor at the
// specified column if possible
func (ed *Edit) CursorPos(col int) {
if col <= text.StrCount(ed.text) {
ed.col = col
ed.redraw(ed.focus)
}
}
// CursorLeft moves the edit cursor one character left if possible
func (ed *Edit) CursorLeft() {
if ed.col > 0 {
ed.col--
ed.redraw(ed.focus)
}
}
// CursorRight moves the edit cursor one character right if possible
func (ed *Edit) CursorRight() {
if ed.col < text.StrCount(ed.text) {
ed.col++
ed.redraw(ed.focus)
}
}
// CursorBack deletes the character at left of the cursor if possible
func (ed *Edit) CursorBack() {
if ed.col > 0 {
ed.col--
ed.text = text.StrRemove(ed.text, ed.col)
ed.redraw(ed.focus)
ed.Dispatch(OnChange, nil)
}
}
// CursorHome moves the edit cursor to the beginning of the text
func (ed *Edit) CursorHome() {
ed.col = 0
ed.redraw(ed.focus)
}
// CursorEnd moves the edit cursor to the end of the text
func (ed *Edit) CursorEnd() {
ed.col = text.StrCount(ed.text)
ed.redraw(ed.focus)
}
// CursorDelete deletes the character at the right of the cursor if possible
func (ed *Edit) CursorDelete() {
if ed.col < text.StrCount(ed.text) {
ed.text = text.StrRemove(ed.text, ed.col)
ed.redraw(ed.focus)
ed.Dispatch(OnChange, nil)
}
}
// CursorInput inserts the specified string at the current cursor position
func (ed *Edit) CursorInput(s string) {
if text.StrCount(ed.text) >= ed.MaxLength {
return
}
// Set new text with included input
var newText string
if ed.col < text.StrCount(ed.text) {
newText = text.StrInsert(ed.text, s, ed.col)
} else {
newText = ed.text + s
}
// Checks if new text exceeds edit width
width, _ := ed.Label.font.MeasureText(newText)
if float32(width)+editMarginX+float32(1) >= ed.Label.ContentWidth() {
return
}
ed.text = newText
ed.col++
ed.Dispatch(OnChange, nil)
ed.redraw(ed.focus)
}
// redraw redraws the text showing the caret if specified
func (ed *Edit) redraw(caret bool) {
line := 0
if !caret {
line = -1
}
ed.Label.setTextCaret(ed.text, editMarginX, ed.width, line, ed.col)
}
// onKey receives subscribed key events
func (ed *Edit) onKey(evname string, ev interface{}) {
kev := ev.(*window.KeyEvent)
switch kev.Keycode {
case window.KeyLeft:
ed.CursorLeft()
case window.KeyRight:
ed.CursorRight()
case window.KeyHome:
ed.CursorHome()
case window.KeyEnd:
ed.CursorEnd()
case window.KeyBackspace:
ed.CursorBack()
case window.KeyDelete:
ed.CursorDelete()
default:
return
}
ed.root.StopPropagation(Stop3D)
}
// onChar receives subscribed char events
func (ed *Edit) onChar(evname string, ev interface{}) {
cev := ev.(*window.CharEvent)
ed.CursorInput(string(cev.Char))
}
// onMouseEvent receives subscribed mouse down events
func (ed *Edit) onMouse(evname string, ev interface{}) {
e := ev.(*window.MouseEvent)
if e.Button != window.MouseButtonLeft {
return
}
// Set key focus to this panel
ed.root.SetKeyFocus(ed)
// Find clicked column
var nchars int
for nchars = 1; nchars <= text.StrCount(ed.text); nchars++ {
width, _ := ed.Label.font.MeasureText(text.StrPrefix(ed.text, nchars))
posx := e.Xpos - ed.pospix.X
if posx < editMarginX+float32(width) {
break
}
}
if !ed.focus {
ed.focus = true
ed.blinkID = ed.root.SetInterval(750*time.Millisecond, nil, ed.blink)
}
ed.CursorPos(nchars - 1)
ed.root.StopPropagation(Stop3D)
}
// onCursor receives subscribed cursor events
func (ed *Edit) onCursor(evname string, ev interface{}) {
if evname == OnCursorEnter {
ed.root.SetCursorText()
ed.cursorOver = true
ed.update()
ed.root.StopPropagation(Stop3D)
return
}
if evname == OnCursorLeave {
ed.root.SetCursorNormal()
ed.cursorOver = false
ed.update()
ed.root.StopPropagation(Stop3D)
return
}
}
// blink blinks the caret
func (ed *Edit) blink(arg interface{}) {
if !ed.focus {
return
}
if !ed.caretOn {
ed.caretOn = true
} else {
ed.caretOn = false
}
ed.redraw(ed.caretOn)
}
// update updates the visual state
func (ed *Edit) update() {
if !ed.Enabled() {
ed.applyStyle(&ed.styles.Disabled)
return
}
if ed.cursorOver {
ed.applyStyle(&ed.styles.Over)
return
}
if ed.focus {
ed.applyStyle(&ed.styles.Focus)
return
}
ed.applyStyle(&ed.styles.Normal)
}
// applyStyle applies the specified style
func (ed *Edit) applyStyle(s *EditStyle) {
ed.SetBordersFrom(&s.Border)
ed.SetBordersColor4(&s.BorderColor)
ed.SetPaddingsFrom(&s.Paddings)
ed.Label.SetColor4(&s.FgColor)
ed.Label.SetBgColor4(&s.BgColor)
//ed.Label.SetBgAlpha(s.BgAlpha)
if !ed.focus && len(ed.text) == 0 && len(ed.placeHolder) > 0 {
ed.Label.SetColor4(&s.HolderColor)
ed.Label.setTextCaret(ed.placeHolder, editMarginX, ed.width, -1, ed.col)
} else {
ed.Label.SetColor4(&s.FgColor)
ed.redraw(ed.focus)
}
}