-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbase.gfx.gui.checkbox.bmx
executable file
·342 lines (258 loc) · 8.7 KB
/
base.gfx.gui.checkbox.bmx
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
Rem
===========================================================
GUI Button with arrows
===========================================================
End Rem
SuperStrict
Import "base.gfx.gui.bmx"
Import "base.gfx.gui.button.bmx"
Import "base.util.registry.spriteloader.bmx"
Type TGUICheckBox Extends TGUIButton
Field checked:Int = False
Field uncheckedSprite:TSprite
Field uncheckedSpriteName:String = ""
Field checkedSprite:TSprite
Field checkedSpriteName:String = "gfx_gui_icon_check"
Field checkboxDimension:TVec2D
Field checkboxDimensionAutocalculated:int = True
Field valueChecked:string = ""
Field valueUnchecked:string = ""
Field captionDisplacement:TVec2D = new TVec2D.Init(5,0)
Field uncheckedTintColor:TColor
Field checkedTintColor:TColor
Field tintColor:TColor
Field tintEnabled:int = True
Global _checkboxMinDimension:TVec2D = new TVec2D.Init(20,20)
Global _typeDefaultFont:TBitmapFont
Method GetClassName:String()
Return "tguicheckbox"
End Method
Method Create:TGUICheckbox(pos:SVec2I, dimension:SVec2I, value:String, State:String = "")
Return Create(new TVec2D.Init(pos.x, pos.y), new TVec2D.Init(dimension.x, dimension.y), value, State)
End Method
Method Create:TGUICheckbox(pos:TVec2D, dimension:TVec2D, value:String, limitState:String="")
'use another sprite name (assign before initing super)
spriteName = "gfx_gui_button.round"
SetCaptionValues(value,value)
Super.Create(pos, dimension, value, limitState)
return self
End Method
Method SetCaptionValues:Int(checkedValue:string, uncheckedValue:string)
self.valueChecked = checkedValue
self.valueUnchecked = uncheckedValue
if self.caption then setValue(GetValue())
End Method
Method SetChecked:Int(checked:Int=True, informOthers:Int=True)
if checked <> 0 then checked = True
'if already same state - do nothing
If self.checked = checked Then Return FALSE
self.checked = checked
If informOthers
TriggerBaseEvent(GUIEventKeys.GuiCheckbox_OnSetChecked, new TData.AddNumber("checked", checked), Self )
EndIf
return True
End Method
Method IsChecked:Int()
Return checked
End Method
'override to get value depending on checked state
Method GetValue:String()
if IsChecked() then return valueChecked
return valueUnchecked
End Method
Method SetCheckboxDimension:int(dimension:TVec2D)
if not dimension
checkboxDimension = null
checkboxDimensionAutocalculated = True
else
if not checkboxDimension then checkboxDimension = new TVec2D
checkboxDimension.CopyFrom(dimension)
checkboxDimensionAutocalculated = False
endif
End Method
Method GetCheckboxDimension:TVec2D()
if not checkboxDimension
local dim:SRect = GetSprite().GetNinePatchInformation().borderDimension
checkboxDimension = new TVec2D.Init(..
Max(_checkboxMinDimension.x, dim.GetLeft() + dim.GetRight()), ..
Max(_checkboxMinDimension.y, dim.GetTop() + dim.GetBottom()) ..
)
endif
return checkboxDimension
End Method
Function SetTypeFont:Int(font:TBitmapFont)
_typeDefaultFont = font
End Function
'override in extended classes if wanted
Function GetTypeFont:TBitmapFont()
return _typeDefaultFont
End Function
Method SetCaption:Int(text:String, setValueChecked:Int, setValueUnchecked:Int, color:TColor=Null)
If setValueChecked then valueChecked = ""
If setValueUnChecked then valueUnChecked = ""
Return SetCaption(text, color)
End Method
'override for a differing alignment
Method SetCaption:Int(text:String, color:TColor=Null)
Super.SetCaption(text, color)
'only overwrite this values if they weren't set yet
if valueChecked = "" and valueUnchecked = ""
valueChecked = text
valueUnchecked = text
endif
if caption
caption.SetContentAlignment(ALIGN_LEFT, ALIGN_TOP)
caption.SetValueEffect(3, 0.2)
caption.SetValueColor(TColor.CreateGrey(100))
endif
InvalidateScreenRect()
End Method
Method ShowCaption:Int(bool:Int=True)
if bool
caption.Show()
else
caption.Hide()
endif
End Method
'private getter
'acts as cache
Method GetCheckedSprite:TSprite()
'refresh cache if not set or wrong sprite name
if not checkedSprite or checkedSprite.GetName() <> checkedSpriteName
checkedSprite = GetSpriteFromRegistry(checkedSpriteName)
endif
return checkedSprite
End Method
'private getter
'acts as cache
Method GetUncheckedSprite:TSprite()
if not uncheckedSpriteName then return Null
'refresh cache if not set or wrong sprite name
if not uncheckedSprite or uncheckedSprite.GetName() <> uncheckedSpriteName
uncheckedSprite = GetSpriteFromRegistry(uncheckedSpriteName)
endif
return uncheckedSprite
End Method
Method SetTintColor(color:TColor, copyColor:int = True)
if copyColor
if color
self.tintColor = color.Copy()
else
self.tintColor = null
endif
else
self.tintColor = color
endif
End Method
Method SetCheckedTintColor(color:TColor, copyColor:int = True)
if copyColor
if color
self.checkedTintColor = color.Copy()
else
self.checkedTintColor = null
endif
else
self.checkedTintColor = color
endif
End Method
Method SetUncheckedTintColor(color:TColor, copyColor:int = True)
if copyColor
if color
self.uncheckedTintColor = color.Copy()
else
self.uncheckedTintColor = null
endif
else
self.uncheckedTintColor = color
endif
End Method
'override default to add checkbox+caption
Method _UpdateScreenW:float()
Super._UpdateScreenW()
if caption and caption.IsVisible() and caption.GetValue() <> ""
_screenRect.SetW( GetCheckboxDimension().x + caption.rect.position.x + caption.GetValueDimension().x )
else
_screenRect.SetW( GetCheckboxDimension().x )
endif
return _screenRect.GetW()
End Method
'override default to add checkbox+caption
Method _UpdateScreenH:float()
Super._UpdateScreenH()
if caption and caption.IsVisible() and caption.GetValue() <> ""
_screenRect.SetH( Max(GetCheckboxDimension().y, caption.rect.position.y + caption.GetValueDimension().y + 5) )
else
_screenRect.SetH( GetCheckboxDimension().y )
endif
return _screenRect.GetH()
End Method
'override default to (un)check box
Method onClick:Int(triggerEvent:TEventBase)
local button:int = triggerEvent.GetData().GetInt("button", -1)
'only react to left mouse button
if button <> 1 then return FALSE
'set box (un)checked
SetChecked(1 - isChecked())
End Method
'override default to handle image changes
Method onAppearanceChanged:int()
Super.onAppearanceChanged()
'reset autocalculated checkbox dimension
if checkboxDimensionAutocalculated then SetCheckboxDimension(null)
End Method
'override so caption gets positioned next to checkbox instead
'of within
Method RepositionCaption:Int()
if not caption then return False
caption.rect.dimension.x = rect.GetW() - GetCheckboxDimension().x - captionDisplacement.x
caption.rect.position.x = GetCheckboxDimension().x + captionDisplacement.x
'center first line to checkbox center
caption.rect.position.y = (GetCheckboxDimension().y - GetFont().GetMaxCharHeight()) / 2 + captionDisplacement.y
End Method
'override default draw-method
Method DrawContent()
Local atPoint:TVec2D = GetScreenRect().position
Local oldCol:SColor8; GetColor(oldCol)
Local oldColA:Float = GetAlpha()
'SetColor 255, 255, 255
if tintEnabled
if IsChecked() and checkedTintColor
SetAlpha( oldColA * GetScreenAlpha() * checkedTintColor.a )
checkedTintColor.SetRGB()
elseif not IsChecked() and uncheckedTintColor
SetAlpha( oldColA * GetScreenAlpha() * uncheckedTintColor.a )
uncheckedTintColor.SetRGB()
elseif tintColor
SetAlpha( oldColA * GetScreenAlpha() * tintcolor.a )
tintColor.SetRGB()
else
SetAlpha( oldColA * GetScreenAlpha() )
endif
endif
Local sprite:TSprite = GetSprite()
if IsActive() or IsHovered() Then sprite = GetSpriteFromRegistry(GetSpriteName() + GetStateSpriteAppendix(), sprite)
if sprite then sprite.DrawArea(atPoint.getX(), atPoint.getY(), GetCheckboxDimension().x, GetCheckboxDimension().y)
'draw (un)checked mark at center of button
local useCheckSprite:TSprite
If IsChecked()
useCheckSprite = GetCheckedSprite()
Else
useCheckSprite = GetUncheckedSprite()
EndIf
If useCheckSprite Then useCheckSprite.Draw(atPoint.getX() + int(GetCheckboxDimension().x/2), atPoint.getY() + int(GetCheckboxDimension().y/2), -1, new TVec2D.Init(0.5, 0.5))
SetColor(oldCol)
If caption and caption.IsVisible()
caption.SetValue(GetValue())
Local oldCol:SColor8 = caption.color
if tintEnabled
If isChecked() Then caption.color = SColor8AdjustFactor(caption.color, -60)
If isHovered() Then caption.color = SColor8AdjustFactor(caption.color, -30)
endif
caption.Draw()
'reset color
caption.color = oldCol
EndIf
SetColor(oldCol)
SetAlpha(oldColA)
End Method
End Type