forked from GWRon/Dig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.gfx.gui.dropdown.bmx
459 lines (338 loc) · 12.8 KB
/
base.gfx.gui.dropdown.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
Rem
====================================================================
GUI DropDown classes
====================================================================
Code contains:
- TGUIDropDown: widget showing - on click - a list to select from
- TGUIDropDownItem: item of a drop down widget
The dropdown widget is grouping multiple objects together. Utilizing
a button and a select list which is hidden until activation
====================================================================
LICENCE
Copyright (C) 2002-now Ronny Otto, digidea.de
This software is provided 'as-is', without any express or
implied warranty. In no event will the authors be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
====================================================================
End Rem
SuperStrict
Import "base.gfx.gui.input.bmx"
'Import "base.gfx.gui.button.bmx"
Import "base.gfx.gui.list.selectlist.bmx"
Type TGUIDropDown Extends TGUIInput
Field lastPosition:TVec2D
'height of the opened drop down
Field open:int = FALSE
'close dropdown as soon as an item gets selected
Field closeOnSelect:int = True
Field selectedEntry:TGUIObject
Field list:TGUISelectList
Field listHeight:int = 100
Global defaultSpriteName:string = "gfx_gui_input.default"
Global defaultOverlaySpriteName:string = "gfx_gui_icon_arrowDown"
Method Create:TGUIDropDown(position:TVec2D = null, dimension:TVec2D = null, value:string="", maxLength:Int=128, limitState:String = "")
'setup base widget (input)
Super.Create(position, dimension, value, maxLength, limitState)
'but this element does not react to keystrokes
SetOption(GUI_OBJECT_CAN_RECEIVE_KEYSTROKES, False)
'=== STYLE BUTTON ===
'use another sprite than the default button
if not spriteName then spriteName = defaultSpriteName
SetOverlayPosition("right")
SetOverlay(defaultOverlaySpriteName)
SetEditable(False)
'=== ENTRY LIST ===
'create and style list
if list then list.Remove()
list = new TGUISelectList.Create(new TVec2D.Init(0, self.rect.GetH()), new TVec2D.Init(rect.GetW(), listHeight), "")
'do not add as child - we position it on our own when updating
'hide list to begin
SetOpen(false)
'set the list to ignore focus requests (avoids onRemoveFocus-events)
list.setOption(GUI_OBJECT_CAN_GAIN_FOCUS, False)
'list ignores screen limits of parents ("overflow")
list.setOption(GUI_OBJECT_IGNORE_PARENTLIMITS, True)
'avoid auto-sorting dropdown elements by default
list.SetAutosortItems(False)
'draw the open list on top of nearly everything
list.SetZIndex(20000)
'add bg to list
local bg:TGUIBackgroundBox = new TGUIBackgroundBox.Create(new TVec2D, new TVec2D)
bg.spriteBaseName = spriteName
list.SetBackground(bg)
'use padding from background
list.SetPadding(bg.GetPadding().getTop(), bg.GetPadding().getLeft(), bg.GetPadding().getBottom(), bg.GetPadding().getRight())
'=== REGISTER EVENTS ===
'to close the list automatically if the object looses focus
AddEventListener(EventManager.registerListenerMethod("guiobject.onRemoveFocus", Self, "onRemoveFocus", self ))
'listen to clicks to dropdown-items
AddEventListener(EventManager.registerListenerMethod("GUIDropDownItem.onClick", Self.list, "onClickOnEntry" ))
'someone uses the mouse wheel to scroll over the panel
AddEventListener(EventManager.registerListenerFunction( "guiobject.OnScrollwheel", onScrollWheel, Self))
'to register if an item was selected
AddEventListener(EventManager.registerListenerMethod("guiselectlist.onSelectEntry", self, "onSelectEntry", self.list ))
Return Self
End Method
Method Resize(w:Float = 0, h:Float = 0)
Super.Resize(w, h)
if list then list.Resize(w, -1)
End Method
Method Remove:int()
Super.Remove()
list.Remove()
End Method
'check if the drop down has to close
Method onRemoveFocus:int(triggerEvent:TEventBase)
'skip indeep checks if already closed
if not IsOpen() then return False
local sender:TGuiObject = TGUIObject(triggerEvent.GetSender())
local receiver:TGuiObject = TGUIObject(triggerEvent.GetReceiver())
if not sender then return False
Rem
'if the receiver is an entry of the list - close and "click"
if self.list.HasItem(receiver)
SetOpen(False)
print "clicked"
endif
EndRem
'close on click on a list item
if receiver and list.HasItem(receiver)
SetSelectedEntry(receiver)
'handled mouse button click to avoid clicks below
MouseManager.SetClickHandled(1)
SetOpen(False)
endif
'skip when loosing focus to self->list or list->self
if receiver
local senderBelongsToWidget:int = False
local receiverBelongsToWidget:int = False
if sender = self
senderBelongsToWidget = True
elseif sender.HasParent(self.list)
senderBelongsToWidget = True
endif
if senderBelongsToWidget and receiver
if receiver = self
receiverBelongsToWidget = True
elseif receiver.HasParent(self.list)
receiverBelongsToWidget = True
endif
endif
'keep the widgets list "open" if new focus is now at a sub element
'of the widget
if senderBelongsToWidget and receiverBelongsToWidget then return False
endif
SetOpen(False)
End Method
Method onSelectEntry:int(triggerEvent:TEventBase)
local guiobj:TGUIObject = TGUIObject(triggerEvent.GetData().Get("entry"))
local item:TGUIDropDownItem = TGUIDropDownItem(triggerEvent.GetData().Get("entry"))
'clicked item is of a different type
if not item then return False
SetSelectedEntry(item)
if closeOnSelect then SetOpen(0)
EndMethod
'override onClick to add open/close toggle
Method onClick:int(triggerEvent:TEventBase)
local button:int = triggerEvent.GetData().GetInt("button")
if button = 1 'left button
'handled mouse button click to avoid clicks below
MouseManager.SetClickHandled(1)
SetOpen(1- IsOpen())
endif
End Method
'handle clicks on the up/down-buttons and inform others about changes
Function onScrollWheel:Int( triggerEvent:TEventBase )
Local dropdown:TGUIDropDown = TGUIDropDown(triggerEvent.GetSender())
Local value:Int = triggerEvent.GetData().getInt("value",0)
If Not dropdown Or value=0 Then Return False
local newEntryPos:int = -1
If value >= 1
newEntryPos = Min(dropdown.list.entries.count()-1, dropdown.GetEntryPos(dropdown.GetSelectedEntry()) + 1)
else
newEntryPos = Max(0, dropdown.GetEntryPos(dropdown.GetSelectedEntry()) - 1)
endif
dropdown.SetSelectedEntryByPos( newEntryPos )
'set to accepted so that nobody else receives the event
triggerEvent.SetAccepted(True)
End Function
Method SetSelectedEntry(item:TGUIObject)
selectedEntry = item
SetValue(item.GetValue())
EventManager.triggerEvent( TEventSimple.Create("GUIDropDown.onSelectEntry", null, Self, item) )
End Method
Method GetSelectedEntry:TGUIObject()
return selectedEntry
End Method
Method SetSelectedEntryByPos:Int(itemPos:int=0)
local item:TGUIObject = GetEntryByPos(itemPos)
if item then SetSelectedEntry(item)
End Method
Method GetEntryByPos:TGUIObject(itemPos:int=0)
local item:TGUIObject = TGUIObject(list.entries.ValueAtIndex(itemPos))
if not item then return Null
return Item
End Method
Method GetEntryPos:int(entry:TGUIObject)
if not entry then return -1
For local i:int = 0 until list.entries.count()
if entry = TGUIObject(list.entries.ValueAtIndex(i)) then return i
Next
return -1
End Method
Method GetEntries:TList()
return list.entries
End Method
'sets the height of the lists content area (ignoring padding)
Method SetListContentHeight:int(height:Float)
list.Resize(list.rect.GetW(), height + list.GetPadding().GetTop() + list.GetPadding().GetBottom())
End Method
Method SetOpen:Int(bool:int)
open = bool
if open
'update z index to be above parent's child widgets
if GetParent() <> self
if list.GetZIndex() <= GetZIndex() + 1
list.SetZIndex( GetZIndex() + 2)
endif
endif
list.Show()
else
list.Hide()
endif
End Method
Method IsOpen:Int()
return open
End Method
Method AddItem:Int(item:TGUIDropDownItem)
if not list then return false
list.AddItem(item)
End Method
Method onStatusAppearanceChange:Int()
'update list position
MoveListIntoPosition()
End Method
Method MoveListIntoPosition()
'move list to our position
local listPosY:int = GetScreenY() + GetScreenHeight()
'if list ends below screen end we might move it above the button
if listPosY + list.GetScreenHeight() > GetGraphicsManager().GetHeight()
if list.GetScreenHeight() < GetScreenY()
listPosY = GetScreenY() - list.GetScreenHeight()
endif
endif
list.SetPosition( GetScreenX(), listPosY )
End Method
'override default update-method
Method Update:Int()
Super.Update()
'if the list is open, an "escape" is used to "abort the action"
if isOpen() and KeyManager.IsHit(KEY_ESCAPE)
'do not allow another ESC-press for 250ms
KeyManager.blockKey(KEY_ESCAPE, 250)
'close the list
SetOpen(False)
'remove focus from gui object
GuiManager.ResetFocus()
endif
'close with right click
if isOpen() and (MouseManager.IsClicked(2) or MouseManager.IsLongClicked(1))
'close the list
SetOpen(False)
'remove focus from gui object
GuiManager.ResetFocus()
'avoid clicks
'remove right click - to avoid leaving the room
MouseManager.ResetClicked(2)
'also avoid long click (touch screen)
MouseManager.ResetLongClicked(1)
endif
local screenPos:TVec2D = GetScreenPos()
if not lastPosition or not lastPosition.IsSame(screenPos)
lastPosition = screenPos
'update list position (as it is not maintained as "child")
MoveListIntoPosition()
endif
End Method
End Type
'as it is similar to a list - extend from a list item
'using "TGUISelectListItem" provides ability to easily
'add them to the list contained in TGUIDropDown
Type TGUIDropDownItem Extends TGUISelectListItem
Method Create:TGUIDropDownItem(position:TVec2D=null, dimension:TVec2D=null, value:String="")
if not dimension then dimension = new TVec2D.Init(80,20)
'no "super.Create..." as we do not need events and dragable and...
Super.CreateBase(position, dimension, "")
SetValue(value)
GUIManager.add(Self)
Return Self
End Method
Method GetScreenWidth:Float()
local listParent:TGUIListBase = TGUIListBase(GetParent("TGUIListBase"))
if not listParent or not listParent.guiEntriesPanel
Return GetParent().GetScreenWidth()
else
Return listParent.guiEntriesPanel.GetContentScreenWidth()
endif
End Method
Method DrawBackground()
if not isHovered() and not isSelected() then return
local oldCol:TColor = new TColor.Get()
SetAlpha oldCol.a * GetScreenAlpha()
local upperParent:TGUIObject = GetParent("TGUIListBase")
upperParent.RestrictContentViewPort()
If isHovered()
SetColor 250,210,100
DrawRect(getScreenX(), getScreenY(), GetScreenWidth(), GetScreenHeight())
SetColor 255,255,255
ElseIf isSelected()
SetAlpha GetAlpha()*0.5
SetColor 250,210,100
DrawRect(getScreenX(), getScreenY(), GetScreenWidth(), GetScreenHeight())
SetColor 255,255,255
SetAlpha GetAlpha()*2.0
EndIf
upperParent.ResetViewPort()
oldCol.SetRGBA()
End Method
'override onClick to close parental list
Method OnClick:int(triggerEvent:TEventBase)
Super.OnClick(triggerEvent)
local button:int = triggerEvent.GetData().GetInt("button")
if button = 1 'left button
'inform others that a dropdownitem was clicked
'this makes the "dropdownitem-clicked"-event filterable even
'if the itemclass gets extended (compared to the general approach
'of "guiobject.onclick")
EventManager.triggerEvent(TEventSimple.Create("GUIDropDownItem.onClick", new TData.AddNumber("button", button), Self, triggerEvent.GetReceiver()) )
endif
End Method
Method DrawValue()
GetFont().DrawBlock(value, getScreenX()+2, GetScreenY(), GetScreenWidth()-4, GetScreenHeight(), ALIGN_LEFT_CENTER, valueColor)
End Method
'override to select another parent
Method GetRestrictingViewPortObject:TGUIObject()
return GetParent("TGUIListBase")
End Method
'override
Method DrawContent()
local oldCol:TColor = new TColor.Get()
SetAlpha oldCol.a * GetScreenAlpha()
local upperParent:TGUIObject = GetParent("TGUIListBase")
upperParent.RestrictContentViewPort()
DrawValue()
upperParent.ResetViewPort()
oldCol.SetRGBA()
End Method
End Type