@@ -41,212 +41,205 @@ local Window = require(SLAB_PATH .. '.Internal.UI.Window')
41
41
42
42
local Button = {}
43
43
44
- local Pad = 10.0
45
- local MinWidth = 75.0
46
- local Radius = 8.0
47
- local ClickedId = nil
48
-
49
- function Button .Begin (Label , Options )
50
- local StatHandle = Stats .Begin (' Button' , ' Slab' )
51
-
52
- Options = Options == nil and {} or Options
53
- Options .Tooltip = Options .Tooltip == nil and " " or Options .Tooltip
54
- Options .Rounding = Options .Rounding == nil and Style .ButtonRounding or Options .Rounding
55
- Options .Invisible = Options .Invisible == nil and false or Options .Invisible
56
- Options .W = Options .W == nil and nil or Options .W
57
- Options .H = Options .H == nil and nil or Options .H
58
- Options .Disabled = Options .Disabled == nil and false or Options .Disabled
59
- Options .Image = Options .Image or nil
60
- Options .Color = Options .Color or Style .ButtonColor
61
- Options .HoverColor = Options .HoverColor or Style .ButtonHoveredColor
62
- Options .PressColor = Options .PressColor or Style .ButtonPressedColor
63
- Options .PadX = Options .PadX or Pad * 2.0
64
- Options .PadY = Options .PadY or Pad * 0.5
65
- Options .VLines = Options .VLines or 1
66
-
67
- local Id = Window .GetItemId (Label )
68
- local W , H = Button .GetSize (Label )
69
- H = H * Options .VLines
70
- local LabelW = Style .Font :getWidth (Label )
71
- local FontHeight = Style .Font :getHeight () * Options .VLines
72
- local TextColor = Options .Disabled and Style .ButtonDisabledTextColor or nil
44
+ local PAD = 10.0
45
+ local MINWIDTH = 75.0
46
+ local RADIUS = 8.0
47
+ local EMPTY = {}
48
+ local IGNORE = { Ignore = true }
49
+
50
+ local clickedId = nil
51
+ local labelColor = {}
52
+
53
+ function Button .Begin (label , options )
54
+ local statHandle = Stats .Begin (' Button' , ' Slab' )
55
+
56
+ options = options or EMPTY
57
+ local width , height = options .W , options .H
58
+ local disabled = options .Disabled
59
+ local image = options .Image
60
+ local color = options .Color or Style .ButtonColor
61
+ local hoverColor = options .HoverColor or Style .ButtonHoveredColor
62
+ local pressColor = options .PressColor or Style .ButtonPressedColor
63
+ local padX = options .PadX or PAD * 2.0
64
+ local padY = options .PadY or PAD * 0.5
65
+ local vLines = options .VLines or 1
66
+
67
+ local id = Window .GetItemId (label )
68
+ local w , h = Button .GetSize (label )
69
+ h = h * vLines
73
70
74
71
-- If a valid image was specified, then adjust the button size to match the requested image size. Also takes into account any sub UVs.
75
- local ImageW , ImageH = W , H
76
- if Options .Image ~= nil then
77
- local Object = Options .Image .Image and Options .Image .Image or Options .Image .Path
78
- ImageW , ImageH = Image .GetSize (Object )
72
+ local imageW , imageH = w , h
73
+ if image ~= nil then
74
+ imageW , imageH = Image .GetSize (image .Image or image .Path )
79
75
80
- ImageW = Options . Image . SubW or ImageW
81
- ImageH = Options . Image . SubH or ImageH
76
+ imageW = image . SubW or imageW
77
+ imageH = image . SubH or imageH
82
78
83
- ImageW = Options . W or ImageW
84
- ImageH = Options . H or ImageH
79
+ imageW = width or imageW
80
+ imageH = height or imageH
85
81
86
- Options . Image . W = ImageW
87
- Options . Image . H = ImageH
82
+ image . W = imageW
83
+ image . H = imageH
88
84
89
- if ImageW > 0 and ImageH > 0 then
90
- W = ImageW + Options . PadX
91
- H = ImageH + Options . PadY
85
+ if imageW > 0 and imageH > 0 then
86
+ w = imageW + padX
87
+ h = imageH + padY
92
88
end
93
89
end
94
90
95
- W = Options . W or W
96
- H = Options . H or H
91
+ w , h = LayoutManager . ComputeSize ( width or w , height or h )
92
+ LayoutManager . AddControl ( w , h , ' Button ' )
97
93
98
- W , H = LayoutManager .ComputeSize (W , H )
99
- LayoutManager .AddControl (W , H , ' Button' )
94
+ local x , y = Cursor .GetPosition ()
100
95
101
- local X , Y = Cursor . GetPosition ()
96
+ local result = false
102
97
103
- local Result = false
104
- local Color = Options .Color
98
+ do
99
+ local mouseX , mouseY = Window .GetMousePosition ()
100
+ if not Window .IsObstructedAtMouse () and x <= mouseX and mouseX <= x + w and y <= mouseY and mouseY <= y + h then
101
+ Tooltip .Begin (options .Tooltip or " " )
102
+ Window .SetHotItem (id )
105
103
106
- local MouseX , MouseY = Window . GetMousePosition ()
107
- if not Window . IsObstructedAtMouse () and X <= MouseX and MouseX <= X + W and Y <= MouseY and MouseY <= Y + H then
108
- Tooltip . Begin ( Options . Tooltip )
109
- Window . SetHotItem ( Id )
104
+ if not disabled then
105
+ if not Utility . IsMobile () then
106
+ color = hoverColor
107
+ end
110
108
111
- if not Options .Disabled then
112
- if not Utility .IsMobile () then
113
- Color = Options .HoverColor
114
- end
109
+ if clickedId == id then
110
+ color = pressColor
111
+ end
115
112
116
- if ClickedId == Id then
117
- Color = Options . PressColor
118
- end
113
+ if Mouse . IsClicked ( 1 ) then
114
+ clickedId = id
115
+ end
119
116
120
- if Mouse .IsClicked (1 ) then
121
- ClickedId = Id
122
- end
123
-
124
- if Mouse .IsReleased (1 ) and ClickedId == Id then
125
- Result = true
126
- ClickedId = nil
117
+ if Mouse .IsReleased (1 ) and clickedId == id then
118
+ result = true
119
+ clickedId = nil
120
+ end
127
121
end
128
122
end
129
123
end
130
124
131
- local LabelX = X + (W * 0.5 ) - (LabelW * 0.5 )
132
-
133
- if not Options .Invisible then
125
+ if not options .Invisible then
134
126
-- Draw the background.
135
- DrawCommands .Rectangle (' fill' , X , Y , W , H , Color , Options .Rounding )
127
+ DrawCommands .Rectangle (' fill' , x , y , w , h , color , options .Rounding or Style . ButtonRounding )
136
128
137
129
-- Draw the label or image. The layout of this control was already computed above. Ignore when adding sub-controls
138
130
-- such as text or an image.
139
- local CursorX , CursorY = Cursor .GetPosition ()
140
- LayoutManager .Begin (' Ignore' , { Ignore = true } )
141
- if Options . Image ~= nil then
142
- Cursor .SetX (X + W * 0.5 - ImageW * 0.5 )
143
- Cursor .SetY (Y + H * 0.5 - ImageH * 0.5 )
144
- Image .Begin (Id .. ' _Image' , Options . Image )
131
+ local cursorX , cursorY = Cursor .GetPosition ()
132
+ LayoutManager .Begin (' Ignore' , IGNORE )
133
+ if image ~= nil then
134
+ Cursor .SetX (x + w * 0.5 - imageW * 0.5 )
135
+ Cursor .SetY (y + h * 0.5 - imageH * 0.5 )
136
+ Image .Begin (id .. ' _Image' , image )
145
137
else
146
- Cursor .SetX (floor (LabelX ))
147
- Cursor .SetY (floor (Y + (H * 0.5 ) - (FontHeight * 0.5 )))
148
- Text .Begin (Label , {Color = TextColor })
138
+ local labelX = x + (w * 0.5 ) - (Style .Font :getWidth (label ) * 0.5 )
139
+ local fontHeight = Style .Font :getHeight () * vLines
140
+ Cursor .SetX (floor (labelX ))
141
+ Cursor .SetY (floor (y + (h * 0.5 ) - (fontHeight * 0.5 )))
142
+ labelColor .color = disabled and Style .ButtonDisabledTextColor or nil
143
+ Text .Begin (label , LabelColor )
149
144
end
150
145
LayoutManager .End ()
151
146
152
- Cursor .SetPosition (CursorX , CursorY )
147
+ Cursor .SetPosition (cursorX , cursorY )
153
148
end
154
149
155
- Cursor .SetItemBounds (X , Y , W , H )
156
- Cursor .AdvanceY (H )
150
+ Cursor .SetItemBounds (x , y , w , h )
151
+ Cursor .AdvanceY (h )
157
152
158
- Window .AddItem (X , Y , W , H , Id )
153
+ Window .AddItem (x , y , w , h , id )
159
154
160
- Stats .End (StatHandle )
155
+ Stats .End (statHandle )
161
156
162
- return Result
157
+ return result
163
158
end
164
159
165
- function Button .BeginRadio (Label , Options )
166
- local StatHandle = Stats .Begin (' RadioButton' , ' Slab' )
160
+ function Button .BeginRadio (label , options )
161
+ local statHandle = Stats .Begin (' RadioButton' , ' Slab' )
167
162
168
- Label = Label == nil and " " or Label
163
+ label = label or " "
169
164
170
- Options = Options == nil and {} or Options
171
- Options .Index = Options .Index == nil and 0 or Options .Index
172
- Options .SelectedIndex = Options .SelectedIndex == nil and 0 or Options .SelectedIndex
173
- Options .Tooltip = Options .Tooltip == nil and " " or Options .Tooltip
165
+ options = options or EMPTY
166
+ local index = options .index or 0
167
+ local selectedIndex = options .SelectedIndex or 0
174
168
175
- local Result = false
176
- local Id = Window .GetItemId (Label )
177
- local W , H = Radius * 2.0 , Radius * 2.0
178
- local IsObstructed = Window .IsObstructedAtMouse ()
179
- local Color = Style .ButtonColor
180
- local MouseX , MouseY = Window .GetMousePosition ()
169
+ local result = false
170
+ local id = Window .GetItemId (label )
171
+ local w , h = RADIUS * 2.0 , RADIUS * 2.0
172
+ local isObstructed = Window .IsObstructedAtMouse ()
173
+ local color = Style .ButtonColor
174
+ local mouseX , mouseY = Window .GetMousePosition ()
181
175
182
- if Label ~= " " then
183
- local TextW , TextH = Text .GetSize (Label )
184
- W = W + Cursor .PadX () + TextW
185
- H = max (H , TextH )
176
+ if label ~= " " then
177
+ local TextW , TextH = Text .GetSize (label )
178
+ w = w + Cursor .PadX () + TextW
179
+ h = max (h , TextH )
186
180
end
187
181
188
- LayoutManager .AddControl (W , H , ' Radio' )
182
+ LayoutManager .AddControl (w , h , ' Radio' )
189
183
190
- local X , Y = Cursor .GetPosition ()
191
- local CenterX , CenterY = X + Radius , Y + Radius
192
- local DX = MouseX - CenterX
193
- local DY = MouseY - CenterY
194
- local HoveredButton = not IsObstructed and (DX * DX ) + (DY * DY ) <= Radius * Radius
195
- if HoveredButton then
196
- Color = Style .ButtonHoveredColor
184
+ local x , y = Cursor .GetPosition ()
185
+ local centerX , centerY = x + RADIUS , y + RADIUS
186
+ local dx = mouseX - centerX
187
+ local dy = mouseY - centerY
188
+ if not isObstructed and (dx * dx ) + (dy * dy ) <= RADIUS * RADIUS then
189
+ color = Style .ButtonHoveredColor
197
190
198
- if ClickedId == Id then
199
- Color = Style .ButtonPressedColor
191
+ if clickedId == id then
192
+ color = Style .ButtonPressedColor
200
193
end
201
194
202
195
if Mouse .IsClicked (1 ) then
203
- ClickedId = Id
196
+ clickedId = id
204
197
end
205
198
206
- if Mouse .IsReleased (1 ) and ClickedId == Id then
207
- Result = true
208
- ClickedId = nil
199
+ if Mouse .IsReleased (1 ) and clickedId == id then
200
+ result = true
201
+ clickedId = nil
209
202
end
210
203
end
211
204
212
- DrawCommands .Circle (' fill' , CenterX , CenterY , Radius , Color )
205
+ DrawCommands .Circle (' fill' , centerX , centerY , RADIUS , color )
213
206
214
- if Options . Index > 0 and Options . Index == Options . SelectedIndex then
215
- DrawCommands .Circle (' fill' , CenterX , CenterY , Radius * 0.7 , Style .RadioButtonSelectedColor )
207
+ if index > 0 and index == selectedIndex then
208
+ DrawCommands .Circle (' fill' , centerX , centerY , RADIUS * 0.7 , Style .RadioButtonSelectedColor )
216
209
end
217
210
218
- if Label ~= " " then
219
- local CursorY = Cursor .GetY ()
220
- Cursor .AdvanceX (Radius * 2.0 )
221
- LayoutManager .Begin (' Ignore' , { Ignore = true } )
222
- Text .Begin (Label )
211
+ if label ~= " " then
212
+ local cursorY = Cursor .GetY ()
213
+ Cursor .AdvanceX (RADIUS * 2.0 )
214
+ LayoutManager .Begin (' Ignore' , IGNORE )
215
+ Text .Begin (label )
223
216
LayoutManager .End ()
224
- Cursor .SetY (CursorY )
217
+ Cursor .SetY (cursorY )
225
218
end
226
219
227
- if not IsObstructed and X <= MouseX and MouseX <= X + W and Y <= MouseY and MouseY <= Y + H then
228
- Tooltip .Begin (Options .Tooltip )
229
- Window .SetHotItem (Id )
220
+ if not isObstructed and x <= mouseX and mouseX <= x + w and y <= mouseY and mouseY <= y + h then
221
+ Tooltip .Begin (options .Tooltip or " " )
222
+ Window .SetHotItem (id )
230
223
end
231
224
232
- Cursor .SetItemBounds (X , Y , W , H )
233
- Cursor .AdvanceY (H )
225
+ Cursor .SetItemBounds (x , y , w , h )
226
+ Cursor .AdvanceY (h )
234
227
235
- Window .AddItem (X , Y , W , H )
228
+ Window .AddItem (x , y , w , h )
236
229
237
- Stats .End (StatHandle )
230
+ Stats .End (statHandle )
238
231
239
- return Result
232
+ return result
240
233
end
241
234
242
- function Button .GetSize (Label )
243
- local W = Style .Font :getWidth (Label )
244
- local H = Style .Font :getHeight ()
245
- return max (W , MinWidth ) + Pad * 2.0 , H + Pad * 0.5
235
+ function Button .GetSize (label )
236
+ local w = Style .Font :getWidth (label )
237
+ local h = Style .Font :getHeight ()
238
+ return max (w , MINWIDTH ) + PAD * 2.0 , h + PAD * 0.5
246
239
end
247
240
248
241
function Button .ClearClicked ()
249
- ClickedId = nil
242
+ clickedId = nil
250
243
end
251
244
252
245
return Button
0 commit comments