-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbase.gfx.gui.tabgroup.bmx
314 lines (233 loc) · 8.06 KB
/
base.gfx.gui.tabgroup.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
SuperStrict
Import "base.gfx.gui.button.bmx"
Type TGUIToggleButton Extends TGUIButton
Field toggled:Int = False
Field valueUntoggled:String
Field valueToggled:String
Field spriteNameToggled:String
Field spriteNameUntoggled:String
Method GetClassName:String()
Return "tguitogglebutton"
End Method
Method Create:TGUIToggleButton(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
'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
SetToggled(1 - Istoggled())
End Method
Method SetToggled:Int(toggled:Int=True, informOthers:Int=True)
'if already same state - do nothing
If Self.toggled = toggled Then Return False
Self.toggled = toggled
If informOthers
TriggerBaseEvent(GUIEventKeys.GUIToggleButton_OnSetToggled, New TData.AddNumber("toggled", toggled), Self )
EndIf
Return True
End Method
Method IsToggled:Int()
Return toggled
End Method
'override to get value depending on toggled state
Method GetValue:String()
If IsToggled() Then Return valueToggled
Return valueUntoggled
End Method
Method SetCaptionValues:Int(toggledValue:String, untoggledValue:String)
Self.valueToggled = toggledValue
Self.valueUntoggled = untoggledValue
If Self.caption Then SetValue(GetValue())
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 valueUntoggled = "" And valueToggled = ""
valueUntoggled = text
valueToggled = text
EndIf
End Method
Rem
'private getter
'acts as cache
Method GetToggledSprite:TSprite()
'refresh cache if not set or wrong sprite name
if not toggledSprite or toggledSprite.GetName() <> spriteNameToggled
toggledSprite = GetSpriteFromRegistry(spriteNameToggled)
endif
return toggledSprite
End Method
'private getter
'acts as cache
Method GetUntoggledSprite:TSprite()
if not spriteNameUntoggled then return Null
'refresh cache if not set or wrong sprite name
if not untoggledSprite or untoggledSprite.GetName() <> spriteNameUntoggled
untoggledSprite = GetSpriteFromRegistry(spriteNameUntoggled)
endif
return untoggledSprite
End Method
endrem
'override
'acts as cache
Method GetSprite:TSprite()
If toggled And spriteNameToggled
spriteName = spriteNameToggled
ElseIf spriteNameUntoggled
spriteName = spriteNameUntoggled
EndIf
'refresh cache if not set or wrong sprite name
If Not _sprite Or _sprite.GetName() <> spriteName
_sprite = GetSpriteFromRegistry(spriteName)
'new -non default- sprite: adjust appearance
If _sprite.GetName() <> "defaultsprite"
SetAppearanceChanged(True)
EndIf
EndIf
Return _sprite
End Method
Method UpdateLayout()
End Method
End Type
Type TGUITabGroup Extends TGUIObject
Field buttons:TGUIToggleButton[]
Field toggledButtonIndex:Int = -1
Method GetClassName:String()
Return "tguitabgroup"
End Method
Method Create:TGUITabGroup(position:TVec2D, dimension:TVec2D, limitState:String="")
Super.CreateBase(position, dimension, limitState)
Self.SetSize(dimension.GetX(), dimension.GetY() )
GUIManager.Add( Self )
Return Self
End Method
Method AddButton(button:TGUIToggleButton, index:Int=-1)
If Not buttons Then buttons = New TGUIToggleButton[0]
If index = -1 Then index = buttons.length + 1
If buttons.length <= index Then buttons = buttons[.. index + 1]
buttons[index] = button
AddChild(button)
'adjust button skin
For Local i:Int = 0 Until buttons.length
If Not buttons[i] Then Continue
If i = 0
buttons[i].spriteNameToggled = "gfx_gui_tabgroup.left.toggled.default"
buttons[i].spriteNameUntoggled = "gfx_gui_tabgroup.left.default"
ElseIf i = buttons.length - 1
buttons[i].spriteNameToggled = "gfx_gui_tabgroup.right.toggled.default"
buttons[i].spriteNameUntoggled = "gfx_gui_tabgroup.right.default"
Else
buttons[i].spriteNameToggled = "gfx_gui_tabgroup.center.toggled.default"
buttons[i].spriteNameUntoggled = "gfx_gui_tabgroup.center.default"
EndIf
buttons[i].SetAppearanceChanged(True)
Next
'align buttons
Local totalButtonsWidth:Int = 0
Local visibleButtonsCount:Int = 0
For Local i:Int = 0 Until buttons.length
If Not buttons[i] Then Continue
If buttons[i].autoSizeModeWidth = TGUIBUtton.AUTO_SIZE_MODE_TEXT
totalButtonsWidth :+ buttons[i].GetFont().getWidth(buttons[i].value) + 8 'TODO: button padding
Else
totalButtonsWidth :+ buttons[i].GetScreenRect().GetW()
EndIf
visibleButtonsCount :+ 1
Next
'print "AddButton: visibleButtonsCount="+visibleButtonsCount+" totalButtonsWidth="+totalButtonsWidth
Local buttonSpacing:Int = 0
Local buttonX:Int = 0
If visibleButtonsCount > 0 Then buttonSpacing = totalButtonsWidth / visibleButtonsCount
'center buttons?
buttonX = 0.5 * (GetScreenRect().GetW() - totalButtonsWidth)
For Local i:Int = 0 Until buttons.length
If Not buttons[i] Then Continue
'print " buttons["+i+"].rect.position.SetX("+buttonX+") screenWidth="+buttons[i].GetScreenRect().GetW()
buttons[i].rect.position.SetX(buttonX)
buttonX :+ buttons[i].GetScreenRect().GetW() + Max(0, (buttonSpacing - buttons[i].GetScreenRect().GetW()))
Next
If toggledButtonIndex = -1
button.SetToggled(True)
toggledButtonIndex = index
EndIf
AddEventListener(EventManager.registerListenerMethod(GUIEventKeys.GUIToggleButton_OnSetToggled, Self, "onSetToggled", button))
End Method
Method GetToggledButtonIndex:Int()
If Not buttons Then Return -1
toggledButtonIndex = -1
For Local i:Int = 0 Until buttons.length
If buttons[i] And buttons[i].IsToggled()
toggledButtonIndex = i
Return i
EndIf
Next
Return -1
End Method
Method SetToggledButtonIndex:Int(i:Int)
If buttons.length <= i Or i < 0
i = -1
EndIf
Local button:TGUIToggleButton = buttons[i]
If button
If Not button.IsToggled()
button.SetToggled(True)
EndIf
button.SetOption(GUI_OBJECT_CLICKABLE, False)
EndIf
If toggledButtonIndex <> -1
If buttons[toggledButtonIndex] And buttons[toggledButtonIndex] <> button
buttons[toggledButtonIndex].SetToggled(False)
buttons[toggledButtonIndex].SetOption(GUI_OBJECT_CLICKABLE, True)
EndIf
'refresh
GetToggledButtonIndex()
EndIf
TriggerBaseEvent(GUIEventKeys.GUITabGroup_OnSetToggledButton, New TData.AddNumber("index", i), Self )
End Method
Method GetButtonIndex:Int(button:TGUIToggleButton)
If Not buttons Then Return -1
For Local i:Int = 0 Until buttons.length
If buttons[i] = button Then Return i
Next
Return -1
End Method
Method onSetToggled:Int(triggerEvent:TEventBase)
Local toggledButton:TGUIToggleButton = TGUIToggleButton(triggerEvent._sender)
If Not toggledButton Then Return False
Local gotToggled:Int = toggledButton.IsToggled()
If Not gotToggled Then Return False
SetToggledButtonIndex( GetButtonIndex(toggledButton) )
Rem
if toggledButtonIndex <> -1
if buttons[toggledButtonIndex] and buttons[toggledButtonIndex] <> toggledButton
buttons[toggledButtonIndex].SetToggled(False)
buttons[toggledButtonIndex].SetOption(GUI_OBJECT_CLICKABLE, True)
endif
'refresh
GetToggledButtonIndex()
endif
toggledButton.SetOption(GUI_OBJECT_CLICKABLE, False)
endrem
End Method
Method Update:Int()
' If informOthers then TriggerBaseEvent(GUIEventKeys.GUICheckBox_OnSetChecked, new TData.AddNumber("checked", checked), Self )
Return Super.Update()
End Method
Method DrawContent()
Local oldColA:Float = GetAlpha()
SetAlpha(oldColA * 0.3)
GetSpriteFromRegistry("gfx_gui_slider.gauge").DrawArea(GetScreenRect().GetX(), GetScreenRect().GetY() + GetScreenRect().GetH()*0.5 - 8, GetScreenRect().GetW(), 6)
SetAlpha(oldColA)
' Super.DrawContent()
End Method
Method UpdateLayout()
End Method
End Type