-
Notifications
You must be signed in to change notification settings - Fork 2
/
guiwidgettextbox.monkey
211 lines (176 loc) · 6.47 KB
/
guiwidgettextbox.monkey
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
Import horizon.guiwidgetframe
Import horizon.color
Import horizon.guisystem
Import horizon.blitzmaxfunctions
Class GuiWidgetTextbox Extends GuiWidgetFrame
Field PADDING:Int = 2
Field font:BitmapFont
Field multiline:Bool = False
Field text:String
Field maxLen : Int
Field cPos : Int
Field cursorJump:Bool
Field allowedChars:String
Field textColor:Color
Field keyboard?
Function CreateTextbox : GuiWidgetTextbox(x:Int, y:Int, w:Int, h:Int)
Local t:GuiWidgetTextbox = New GuiWidgetTextBox
t.rect.x = x
t.rect.y = y
t.rect.w = w
t.rect.h = h
t.style = 0
t.text = ""
Return t
End Function
Method SetText(t:String)
text = t[0..maxLen+1]
End Method
Method New()
keyboard = False
topBar = LoadImage(GuiSystem.SKIN_PATH + "window/bottomborder.png")
rightBorder = LoadImage(GuiSystem.SKIN_PATH + "window/rightborder.png")
leftBorder = LoadImage(GuiSystem.SKIN_PATH + "window/leftborder.png")
bottomBorder = LoadImage(GuiSystem.SKIN_PATH + "window/bottomborder.png")
maxLen = 10000
allowedChars = ""
textColor = Color.Create(0,0,0)
End Method
Method Render()
SetScissor(GetInnerWindowX() * Application.GetInstance().zoomFactorX, GetInnerWindowY() * Application.GetInstance().zoomFactorY, GetInnerWidth() * Application.GetInstance().zoomFactorX, GetInnerHeight() * Application.GetInstance().zoomFactorY)
If (GuiSystem.activeElement = Self)
SetColor($AA,$AA,$AA)
Else
SetColor($88,$88,$88)
If (GetWidgetState() = HOVER) Then SetColor($99,$99,$99)
End If
Local x : Int = GetInnerWindowX()
Local y : Int = GetInnerWindowY()
DrawRect x,y, GetInnerWidth(), GetInnerHeight()
x += PADDING
y += PADDING
For Local pos:Int = -1 To text.Length
If pos >= 0
SetColor(textColor.r, textColor.g, textColor.b)
Local char:String = Mid(text, pos, 1)
Local break : Bool = False
Local tmpX : Int = x
Local boundX : Int = GetInnerWindowX() + GetInnerWidth()
Local c : Int = 0
While (pos + c <= text.Length)
Local nc : String = Mid(text, pos + c, 1)
c += 1
tmpX += font.GetTxtWidth(nc) * 0.9
If tmpX >= boundX Then break = True ; Exit
If nc = " " Then Exit
Wend
If break Or char = CHAR_ENTER
y += font.GetFontHeight()
x = GetInnerWindowX() + PADDING
End If
font.DrawText char, x, y
x += font.GetTxtWidth(char) * 0.9
End
If (GuiSystem.activeElement = Self And cPos = pos And Millisecs() / 400 Mod 2 = 0)
SetColor(255 - textColor.r, 255 - textColor.g, 255 - textColor.b)
SetColor(0, 0, 0)
If cPos = text.Length-1
DrawRect x+2,y, 1, font.GetFontHeight()-3
Else
DrawRect x,y, 1, font.GetFontHeight()-3
End If
End If
If (cursorJump)
If (GuiSystem.mouse.GetX() > x And GuiSystem.mouse.GetY() > y)
cPos = pos
If cPos > text.Length-1 Then cPos = text.Length-1
End If
End If
If (pos >= maxLen) Then Exit
Next
SetColor(255,255,255)
Super.Render()
SetScissor(0,0,DeviceWidth(), DeviceHeight())
End Method
Method CursorToEnd()
cPos = text.Length-1
End Method
Method DrawTopBar()
UtilImage.DrawRepeated(topBar, GetX(), GetY(), rect.w, ImageHeight(topBar))
End Method
Method GetInnerHeight : Int()
Return rect.h - ImageHeight(topBar) - ImageHeight(bottomBorder)
End Method
Method DrawBottomBorder()
UtilImage.DrawRepeated(bottomBorder, GetX(), GetY() + rect.h - ImageHeight(bottomBorder), rect.w, ImageHeight(bottomBorder))
End Method
Method Update()
cursorJump = False
Super.Update()
If (Self = GuiSystem.activeElement)
If (Not keyboard)
keyboard = True
EnableKeyboard()
End
If KeyHit(CHAR_LEFT)
cPos -= 1
If cPos < 0 Then cPos = 0
End
If KeyHit(CHAR_RIGHT)
cPos += 1
If cPos > text.Length-1 Then cPos = text.Length-1
End If
If KeyHit(CHAR_DELETE) And cPos < text.Length-1 Then RemoveChar(cPos+1) ; cPos += 1
If KeyHit(CHAR_HOME) Then cPos = 0
If KeyHit(CHAR_END) Then cPos = text.Length-1
Local c:Int = GetChar()
While (c > 0)
Select c
Case CHAR_BACKSPACE
RemoveChar(cPos)
Case CHAR_ENTER
If (multiline)
InsertChar(String.FromChar(c))
Else
GuiSystem.activeElement = Null
End If
Default
InsertChar(String.FromChar(c))
End Select
c = GetChar()
Wend
Else
If (keyboard)
keyboard = False
DisableKeyboard()
End
End
End Method
Method InsertChar(c:String)
If text.Length >= maxLen Then Return
If allowedChars <> ""
Local found:Bool = False
For Local i:Int = 0 To allowedChars.Length
If Mid(allowedChars, i, 1) = c Then found = True ; Exit
Next
If (Not found) Then Return
End If
Local firstSegment : String = Mid(text, 0, cPos + 1)
text = firstSegment + c + Right(text, text.Length - firstSegment.Length)
cPos += 1
End Method
Method RemoveChar(pos:Int)
If text.Length > 0 And cPos >= 0
Local firstSegment : String = Mid(text, 0, pos)
text = firstSegment + Right(text, text.Length - firstSegment.Length - 1)
cPos -= 1
End If
End Method
Method OnActivate()
Super.OnActivate()
End Method
Method OnMouseHit()
GuiSystem.activeElement = Self
cursorJump = True
End Method
End