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
|
package.path = package.path..";"..RL.GetBasePath().."?.lua"
package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"
Util = require( "utillib" )
Rect = require( "rectangle" )
Vec2 = require( "vector2" )
Color = require( "color" )
Rect = require( "rectangle" )
Raygui = require( "raygui" )
require( "sprite_button" ):register( Raygui )
require( "property_list" ):register( Raygui )
Gui = Raygui:new()
local buttonTexture = nil
local winSize = Vec2:new( 1024, 720 )
local cat = {
texture = nil,
source = Rect:new(),
dest = Rect:new(),
origin = Vec2:new(),
rotation = 0.0,
tint = Color:new( RL.WHITE ),
visible = true,
flipped = false
}
local function addButton( bounds, text, callback )
Gui:SpriteButton(
bounds,
text,
buttonTexture,
{ source = { 0, 0, 48, 48 }, left = 16, top = 16, right = 16, bottom = 16, layout = RL.NPATCH_NINE_PATCH },
{ source = { 48, 0, 48, 48 }, left = 16, top = 16, right = 16, bottom = 16, layout = RL.NPATCH_NINE_PATCH },
callback,
{
properties = {
{ RL.LABEL, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_CENTER },
{ RL.DEFAULT, RL.TEXT_SIZE, 32 },
{ RL.LABEL, RL.TEXT_COLOR_NORMAL, RL.ColorToInt( { 84, 59, 22 } ) },
{ RL.LABEL, RL.TEXT_COLOR_PRESSED, RL.ColorToInt( { 84/2, 59/2, 22/2 } ) },
{ RL.LABEL, RL.TEXT_COLOR_FOCUSED, RL.ColorToInt( RL.GREEN ) },
},
},
text
)
end
local function addSpriteButtons()
buttonTexture = RL.LoadTexture( RL.GetBasePath().."../resources/images/button.png" )
local buttonSize = Vec2:new( 216, 32 )
local bounds = Rect:new( winSize.x / 2 - buttonSize.x / 2, 200, 216, 32 )
local gap = buttonSize.y + 2
addButton( bounds, "Start New Game", function() print( "New Game!" ) end )
bounds.y = bounds.y + gap
addButton( bounds, "Load Game", function() print( "Load Game!" ) end )
bounds.y = bounds.y + gap
addButton( bounds, "Options", function() print( "Options!" ) end )
bounds.y = bounds.y + gap
addButton( bounds, "Quit", function() RL.CloseWindow() end )
end
local function getTextValue( text )
local value = tonumber( text )
if value == nil then value = 0 end
return value
end
local function addPropertyList()
PropertyList = Gui:PropertyList(
Rect:new( 20, 20, 256, 328 ),
"Property List",
-- Callback.
nil,
-- Grab callback.
function( self ) Gui:set2Top( self ) end,
nil,
{
properties = {
{ RL.SCROLLBAR, RL.ARROWS_VISIBLE, RL.ARROWS_VISIBLE },
}
}
)
RL.GuiSetStyle( RL.SPINNER, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_LEFT )
PropertyList:addControl( PropertyList.gui:Line(
Rect:new(),
"Cat Texture"
) )
--Transform.
local transformGroup = PropertyList:addGroup( "Transform", false )
-- Position.
PropertyList:addControl( PropertyList.gui:Label(
Rect:new(),
"Position:"
), transformGroup )
PropertyList:addControl( PropertyList.gui:TextBox(
Rect:new( 0, 0, 64, 22 ),
cat.dest.x,
32,
false,
function( self )
self.value = getTextValue( self.text )
self.text = tostring( self.value )
cat.dest.x = self.value
end,
nil,
"Position X"
), transformGroup, true )
PropertyList:addControl( PropertyList.gui:TextBox(
Rect:new( 74, 0, 64, 22 ),
cat.dest.y,
32,
false,
function( self )
self.value = getTextValue( self.text )
self.text = tostring( self.value )
cat.dest.y = self.value
end,
nil,
"Position Y"
), transformGroup )
-- Origin.
PropertyList:addControl( PropertyList.gui:Label(
Rect:new(),
"Origin:"
), transformGroup )
PropertyList:addControl( PropertyList.gui:TextBox(
Rect:new( 0, 0, 64, 22 ),
cat.dest.x,
32,
false,
function( self )
self.value = getTextValue( self.text )
self.text = tostring( self.value )
cat.origin.x = self.value
end,
nil,
"Origin X"
), transformGroup, true )
PropertyList:addControl( PropertyList.gui:TextBox(
Rect:new( 74, 0, 64, 22 ),
cat.dest.y,
32,
false,
function( self )
self.value = getTextValue( self.text )
self.text = tostring( self.value )
cat.origin.y = self.value
end,
nil,
"Origin Y"
), transformGroup )
-- Rotation.
PropertyList:addControl( PropertyList.gui:Slider(
Rect:new( 60, 0, PropertyList.defaultControlSize.x - 150, 22 ),
"Rotation",
"0",
0,
0,
360,
function( self )
self.value = Util.round( self.value )
cat.rotation = self.value
self.textRight = self.value
end,
nil,
"Rotation angle"
), transformGroup )
-- Flipped.
PropertyList:addControl( PropertyList.gui:CheckBox(
Rect:new( 0, 0, 20, 20 ),
"Flipped",
cat.flipped,
function( self ) cat.flipped = self.checked end,
nil,
"Flips the image"
), transformGroup )
-- Visibility.
local visibilityGroup = PropertyList:addGroup( "Visibility", false )
PropertyList:addControl( PropertyList.gui:CheckBox(
Rect:new( 0, 0, 20, 20 ),
"Visible",
cat.visible,
function( self ) cat.visible = self.checked end,
{
properties = {
{ RL.CHECKBOX, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_RIGHT },
}
}
), visibilityGroup )
local tintGroup = PropertyList:addGroup( "Tint", false, visibilityGroup )
PropertyList:addControl( PropertyList.gui:ColorPicker(
Rect:new( 0, 0, 128, 128 ),
"Color Picker",
Color:new(),
function( self ) cat.tint = self.color end
), tintGroup )
PropertyList:addControl( PropertyList.gui:Line(
Rect:new(),
"Testing"
) )
PropertyList:addControl( PropertyList.gui:DropdownBox(
Rect:new(),
"Dog\nGiraffe\nLion\nHorse",
0,
false,
function( self ) print( self:getItem( self.active ) ) end
) )
local test = PropertyList:addGroup( "Test", false )
for i = 1, 5 do
PropertyList:addControl( PropertyList.gui:CheckBox(
Rect:new( 128, 0, 20, 20 ),
i.."_Visible",
false,
function( self ) print( "Checked" ) end,
{
properties = {
-- { RL.CHECKBOX, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_LEFT },
{ RL.DEFAULT, RL.TEXT_SIZE, 32 }
}
}
), test )
end
end
function RL.init()
local monitor = 0
local mPos = Vec2:new( RL.GetMonitorPosition( monitor ) )
local mSize = Vec2:new( RL.GetMonitorSize( monitor ) )
RL.SetWindowState( RL.FLAG_WINDOW_RESIZABLE )
RL.SetWindowState( RL.FLAG_VSYNC_HINT )
RL.SetWindowSize( winSize )
RL.SetWindowPosition( { mPos.x + mSize.x / 2 - winSize.x / 2, mPos.y + mSize.y / 2 - winSize.y / 2 } )
-- RL.GuiSetStyle( RL.DEFAULT, RL.TEXT_SIZE, 20 )
-- RL.GuiSetStyle( RL.DEFAULT, RL.TEXT_SPACING, 4 )
-- RL.GuiSetStyle( RL.SPINNER, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_RIGHT )
-- RL.GuiSetStyle( RL.SPINNER, RL.TEXT_PADDING, 2 )
cat.texture = RL.LoadTexture( RL.GetBasePath().."../resources/images/cat.png" )
local texSize = Vec2:new( RL.GetTextureSize( cat.texture ) )
cat.source:set( 0, 0, texSize.x, texSize.y )
cat.dest = cat.source:clone()
RL.GuiLoadStyle( RL.GetBasePath().."../resources/styles/style_dark.rgs" )
addSpriteButtons()
addPropertyList()
end
function RL.update( delta )
Gui:update()
end
function RL.draw()
RL.ClearBackground( RL.DARKBLUE )
if cat.visible then
if cat.flipped then
RL.DrawTexturePro(
cat.texture,
{ cat.source.x, cat.source.y, -cat.source.width, cat.source.height },
cat.dest,
cat.origin,
cat.rotation,
cat.tint
)
else
RL.DrawTexturePro(
cat.texture,
cat.source,
cat.dest,
cat.origin,
cat.rotation,
cat.tint
)
end
end
Gui:draw()
end
|