-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbase.gfx.gui.arrowbutton.bmx
executable file
·206 lines (153 loc) · 5.12 KB
/
base.gfx.gui.arrowbutton.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
Rem
===========================================================
GUI Button with arrows
===========================================================
End Rem
SuperStrict
Import "base.gfx.gui.bmx"
Import "base.gfx.gui.label.bmx"
Import "base.util.registry.spriteloader.bmx"
Type TGUIArrowButton Extends TGUISpriteButton
Field direction:String
Method New()
spriteBaseName = "gfx_gui_icon_arrow"
End Method
Method GetClassName:String()
Return "tguiarrowbutton"
End Method
Method Create:TGUIArrowButton(pos:SVec2I, dimension:SVec2I, direction:String="LEFT", limitState:String = "")
Return Create(new TVec2D.Init(pos.x, pos.y), new TVec2D.Init(dimension.x, dimension.y), direction, limitState)
End Method
Method Create:TGUIArrowButton(pos:TVec2D, dimension:TVec2D, direction:String="LEFT", limitState:String = "")
Super.Create(pos, dimension, spriteBaseName, limitState)
SetDirection(direction)
Return Self
End Method
'override to add direction
Method _GetSprite:TSprite()
'refresh cache if not set or wrong sprite name
if not sprite or sprite.GetName() <> (spriteBaseName + direction)
sprite = GetSpriteFromRegistry(spriteBaseName + direction)
endif
return sprite
End Method
Method SetDirection(direction:String="LEFT")
direction = direction.ToUpper()
if self.direction = direction then return
Select direction
Case "LEFT" Self.direction="Left"
Case "UP" Self.direction="Up"
Case "RIGHT" Self.direction="Right"
Case "DOWN" Self.direction="Down"
Default Self.direction="Left"
EndSelect
sprite = null
End Method
End Type
Type TGUISpriteButton Extends TGUIObject
Field sprite:TSprite
Field buttonSprite:TSprite
Field spriteBaseName:String = ""
Field spriteButtonBaseName:String = "gfx_gui_button.round"
Field spriteButtonOptions:int = SHOW_BUTTON_NORMAL + SHOW_BUTTON_HOVER + SHOW_BUTTON_ACTIVE
CONST SHOW_BUTTON_NORMAL:int = 1
CONST SHOW_BUTTON_HOVER:int = 2
CONST SHOW_BUTTON_ACTIVE:int = 4
Method Create:TGUISpriteButton(pos:TVec2D, dimension:TVec2D, spriteName:String="", limitState:String = "")
'setup base widget
Super.CreateBase(pos, dimension, limitState)
SetSpriteName(spriteName)
value = ""
'let the guimanager manage the button
GUIManager.Add(Self)
Return Self
End Method
Method HasSpriteButtonOption:Int(option:Int)
Return (spriteButtonOptions & option) <> 0
End Method
Method SetSpriteButtonOption(option:Int, enable:Int=True)
If enable
spriteButtonOptions :| option
Else
spriteButtonOptions :& ~option
EndIf
End Method
Method SetSprite(sprite:TSprite)
if sprite
self.sprite = sprite
self.spriteBaseName = sprite.GetName()
else
self.sprite = null
self.spriteBaseName = ""
endif
End Method
Method SetSpriteName(spriteName:string)
if self.spriteBaseName <> spriteName
self.spriteBaseName = spriteName
'force cache reset
self.sprite = null
endif
End Method
'override so we have a minimum size
'size 0, 0 is not possible (leads to autosize)
Method SetSize(w:Float = 0, h:Float = 0)
if w <= 0 then w = rect.dimension.GetX()
if h <= 0 then h = rect.dimension.GetY()
'set to minimum size or bigger
local spriteDimension:SRect = _GetButtonSprite().GetNinePatchInformation().borderDimension
w = Max(w, spriteDimension.GetLeft() + spriteDimension.GetRight())
h = Max(h, spriteDimension.GetTop() + spriteDimension.GetBottom())
Super.SetSize(w, h)
End Method
'private getter
'acts as cache
Method _GetButtonSprite:TSprite()
'refresh cache if not set or wrong sprite name
if spriteButtonBaseName
if not buttonSprite or buttonSprite.GetName() <> (spriteButtonBaseName + self.GetStateSpriteAppendix())
local newSprite:TSprite = GetSpriteFromRegistry(spriteButtonBaseName + self.GetStateSpriteAppendix(), spriteButtonBaseName)
if not buttonSprite or newSprite.GetName() <> buttonSprite.GetName()
buttonSprite = newSprite
'new image - resize
SetSize(-1, -1)
endif
endif
return buttonSprite
else
return null
endif
End Method
'private getter
'acts as cache
Method _GetSprite:TSprite()
'refresh cache if not set or wrong sprite name
if not sprite or sprite.GetName() <> spriteBaseName
sprite = GetSpriteFromRegistry(spriteBaseName)
endif
return sprite
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
SetAlpha oldColA * GetScreenAlpha()
'draw button (background)
local bs:TSprite
if IsActive()
if HasSpriteButtonOption(SHOW_BUTTON_ACTIVE) then bs = _GetButtonSprite()
elseif IsHovered()
if HasSpriteButtonOption(SHOW_BUTTON_HOVER) then bs = _GetButtonSprite()
elseif HasSpriteButtonOption(SHOW_BUTTON_NORMAL)
bs = _GetButtonSprite()
endif
if bs then bs.DrawArea(atPoint.getX(), atPoint.getY(), rect.GetW(), rect.GetH())
'draw arrow at center of button
_GetSprite().Draw(atPoint.getX() + int(rect.GetW()/2), atPoint.getY() + int(rect.GetH()/2), -1, new TVec2D.Init(0.5, 0.5))
SetColor(oldCol)
SetAlpha(oldColA)
End Method
Method UpdateLayout()
End Method
End Type