summaryrefslogtreecommitdiff
path: root/examples/ReiLuaGui_calculator/main.lua
blob: 57ba9a1e8f6f4504bc69b493fe7b9fb5ddde47b5 (plain)
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
package.path = package.path..";"..RL_GetBasePath().."../resources/lib/?.lua"

util = require( "utillib" )
Vec2 = require( "vector2" )
Rect = require( "rectangle" )
Color = require( "color" )
Gui = require( "gui" )

-- Textures.

local cancelTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/cancel.png" )
local borderTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/ui_border.png" )
local bgrTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/ui_bgr.png" )

RL_GenTextureMipmaps( cancelTexture )
RL_GenTextureMipmaps( bgrTexture )

RL_SetTextureFilter( cancelTexture, TEXTURE_FILTER_TRILINEAR )
RL_SetTextureFilter( bgrTexture, TEXTURE_FILTER_TRILINEAR )

RL_SetTextureWrap( borderTexture, TEXTURE_WRAP_REPEAT )
RL_SetTextureWrap( bgrTexture, TEXTURE_WRAP_REPEAT )

-- Calculator definition.

Calculator = {}
Calculator.__index = Calculator

function Calculator:new( pos )
	pos = pos or Vec2:new( 0, 0 )

	local object = setmetatable( {}, self )

	object.HANDLE_HIGHT = 32
	object.DISPLAY_HIGHT = 40
	object.OPERATIONS = { ADD = 0, SUB = 1, MUL = 2, DIV = 3 }

	object.windowRect = Rect:new( pos.x, pos.y, 196, 244 )
	object.dragPos = Vec2:new( 0, 0 )

	-- Handle

	object.handle = Gui.element:new( {
		bounds = Rect:new( 0, 0, object.windowRect.width, object.HANDLE_HIGHT ),
		padding = 10,
		onClicked = function()
			object:set2Top()
			object.dragPos = Vec2:new( RL_GetMousePosition() ) - Vec2:new( object.handle.bounds.x, object.handle.bounds.y )
			Gui.heldCallback = function() object:drag() end
		end,
	} )

	object.handle:add( Gui.texture:new( {
		bounds = object.handle.bounds:clone(),
		texture = bgrTexture,
		HAling = Gui.ALING.CENTER,
		VAling = Gui.ALING.CENTER,
		color = Color:new( LIGHTGRAY ),
	} ) )
	
	object.handle:add( Gui.texture:new( {
		bounds = object.handle.bounds:clone(),
		texture = borderTexture,
		HAling = Gui.ALING.CENTER,
		VAling = Gui.ALING.CENTER,
		color = Color:new( LIGHTGRAY ),
		nPatchInfo = { source = { 0, 0, 24, 24 }, left = 8, top = 8, right = 8, bottom = 8, layout = NPATCH_NINE_PATCH },
	} ) )

	object.handle:add( Gui.text:new( { text = "Calculator", fontSize = 20, VAling = Gui.ALING.CENTER } ) )

	-- Close button.

	object.closeButton = Gui.element:new( {
		bounds = Rect:new( 0, 0, object.HANDLE_HIGHT, object.HANDLE_HIGHT ),
		onClicked = function()
			object:setVisible( false )
		end,
		onMouseOver = function( self ) self.items[1].color = Color:new( WHITE ) end,
		notMouseOver = function( self ) self.items[1].color = Color:new( BLACK ) end,
	} )

	object.closeButton:add( Gui.texture:new( {
		bounds = object.closeButton.bounds:clone(),
		texture = cancelTexture,
		HAling = Gui.ALING.CENTER,
		VAling = Gui.ALING.CENTER,
	} ) )

	-- Panel.

	object.panel = Gui.element:new( {
		bounds = Rect:new( 0, 0, object.windowRect.width, object.windowRect.height - object.HANDLE_HIGHT ),
	} )

	object.panel:add( Gui.texture:new( {
		bounds = object.panel.bounds:clone(),
		texture = bgrTexture,
		HAling = Gui.ALING.CENTER,
		VAling = Gui.ALING.CENTER,
		color = Color:new( GRAY ),
	} ) )

	object.panel:add( Gui.texture:new( {
		bounds = object.panel.bounds:clone(),
		texture = borderTexture,
		HAling = Gui.ALING.CENTER,
		VAling = Gui.ALING.CENTER,
		color = Color:new( LIGHTGRAY ),
		nPatchInfo = { source = { 0, 0, 24, 24 }, left = 8, top = 8, right = 8, bottom = 8, layout = NPATCH_NINE_PATCH },
	} ) )

	-- Display.

	object.display = Gui.element:new( {
		bounds = Rect:new( 0, 0, object.windowRect.width - 16, object.DISPLAY_HIGHT ),
		padding = 10,
		drawBounds = true,
		color = Color:new( WHITE )
	} )

	object.display:add( Gui.text:new( { text = "", fontSize = 30, VAling = Gui.ALING.CENTER, maxTextLen = 8 } ) )

	-- Buttons.
	
	local buttonStrings = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", "C", "=", "+" }
	object.buttons = {}

	for y = 0, 3 do
		for x = 0, 3 do
			local i = x + y * 4

			table.insert( object.buttons, Gui.element:new( {
				bounds = Rect:new( 0, 0, 40, 32 ),
				drawBounds = true,
				onMouseOver = function( self ) self.color = Color:new( LIGHTGRAY ) end,
				notMouseOver = function( self ) self.color = Color:new( GRAY ) end,
			} ) )

			object.buttons[ #object.buttons ].pos = Vec2:new( 8 + x * 46, object.HANDLE_HIGHT + object.DISPLAY_HIGHT + 16 + y * 38 )
			object.buttons[ #object.buttons ]:add( Gui.text:new( { text = buttonStrings[i+1], fontSize = 30, HAling = Gui.ALING.CENTER, VAling = Gui.ALING.CENTER } ) )

			if buttonStrings[i+1] == "/" then
				object.buttons[ #object.buttons ].onClicked = function() object:setOperation( object.OPERATIONS.DIV ) end
			elseif buttonStrings[i+1] == "*" then
				object.buttons[ #object.buttons ].onClicked = function() object:setOperation( object.OPERATIONS.MUL ) end
			elseif buttonStrings[i+1] == "-" then
				object.buttons[ #object.buttons ].onClicked = function() object:setOperation( object.OPERATIONS.SUB ) end
			elseif buttonStrings[i+1] == "+" then
				object.buttons[ #object.buttons ].onClicked = function() object:setOperation( object.OPERATIONS.ADD ) end
			elseif buttonStrings[i+1] == "C" then
				object.buttons[ #object.buttons ].onClicked = function() object:clear() end
			elseif buttonStrings[i+1] == "=" then
				object.buttons[ #object.buttons ].onClicked = function() object:equals() end
			else
				object.buttons[ #object.buttons ].onClicked = function() object:addValue( tonumber( buttonStrings[i+1] ) ) end
			end
		end
	end

	-- Calculator variables.

	object.value1 = ""
	object.value2 = ""
	object.operation = nil

	-- Set position.

	object:setPosition( Vec2:new( object.windowRect.x, object.windowRect.y ) )

    return object
end

function Calculator:setPosition( pos )
	self.windowRect.x = pos.x
	self.windowRect.y = pos.y

	self.handle:setPosition( pos )
	self.closeButton:setPosition( Vec2:new( pos.x + self.windowRect.width - self.HANDLE_HIGHT, pos.y ) )
	self.panel:setPosition( Vec2:new( pos.x, pos.y + self.HANDLE_HIGHT ) )
	self.display:setPosition( Vec2:new( pos.x + 8, pos.y + self.HANDLE_HIGHT + 8 ) )

	for _, button in ipairs( self.buttons ) do
		button:setPosition( pos + button.pos )
	end
end

function Calculator:drag()
	local mousePos = Vec2:new( RL_GetMousePosition() )
	local winPos = Vec2:new( self.handle.bounds.x, self.handle.bounds.y )

	self:setPosition( mousePos - self.dragPos )
end

function Calculator:setVisible( visible )
	self.handle.visible = visible
	self.handle.disabled = not visible

	self.closeButton.visible = visible
	self.closeButton.disabled = not visible
	
	self.panel.visible = visible
	self.panel.disabled = not visible

	self.display.visible = visible
	self.display.disabled = not visible
	
	for _, button in ipairs( self.buttons ) do
		button.visible = visible
		button.disabled = not visible
	end
end

function Calculator:set2Top()
	self.panel:set2Top()
	
	for _, button in ipairs( self.buttons ) do
		button:set2Top()
	end

	self.handle:set2Top()
	self.closeButton:set2Top()
	self.display:set2Top()
end

function Calculator:addValue( value )
	if self.operation == nil then
		self.value1 = self.value1..value
	else
		self.value2 = self.value2..value
	end

	self:updateDisplay()
end

function Calculator:setOperation( operation )
	if self.value1 ~= "" then
		self.operation = operation
	end

	if self.value2 ~= "" then
		self:equals()
	end

	self:updateDisplay()
end

function Calculator:clear()
	self.value1 = ""
	self.value2 = ""
	self.operation = nil

	self:updateDisplay()
end

function Calculator:updateDisplay( setText )
	local text = setText or ""

	if self.value1 ~= "" then
		text = self.value1
	end

	if self.operation ~= nil then
		if self.operation == self.OPERATIONS.ADD then
			text = text.." + "
		elseif self.operation == self.OPERATIONS.SUB then
			text = text.." - "
		elseif self.operation == self.OPERATIONS.MUL then
			text = text.." * "
		elseif self.operation == self.OPERATIONS.DIV then
			text = text.." / "
		end
	end

	if self.value2 ~= "" then
		text = text..self.value2
	end

	self.display.items[1]:set( text )
end

function Calculator:equals()
	local result = 0

	if self.operation == self.OPERATIONS.ADD then
		result = tonumber( self.value1 ) + tonumber( self.value2 )
	elseif self.operation == self.OPERATIONS.SUB then
		result = tonumber( self.value1 ) - tonumber( self.value2 )
	elseif self.operation == self.OPERATIONS.MUL then
		result = tonumber( self.value1 ) * tonumber( self.value2 )
	elseif self.operation == self.OPERATIONS.DIV then
		if tonumber( self.value2 ) == 0 then
			self:clear()
			self:updateDisplay( "Error" )

			return
		else
			result = tonumber( self.value1 ) / tonumber( self.value2 )
		end
	end

	self:clear()
	self.value1 = tostring( result )
	self:updateDisplay()
end

-- End of calculator definition.

local calculator = nil
local calculator2 = nil
local showButton = nil

function initGui()
	showButton = Gui.element:new( {
		bounds = Rect:new( 0, 0, 96, 32 ),
		drawBounds = true,
		onClicked = function()
			calculator:setVisible( true )
			calculator2:setVisible( true )
		end,
		onMouseOver = function( self ) self.color = Color:new( LIGHTGRAY ) end,
		notMouseOver = function( self ) self.color = Color:new( GRAY ) end,
	} )

	showButton:add( Gui.text:new( { text = "Show", VAling = Gui.ALING.CENTER, HAling = Gui.ALING.CENTER } ) )

	calculator = Calculator:new( Vec2:new( 128, 96 ) )
	calculator2 = Calculator:new( Vec2:new( 340, 96 ) )
end

function init()
	local monitor = 0
	local mPos = RL_GetMonitorPosition( monitor )
	local mSize = RL_GetMonitorSize( monitor )
	winSize = RL_GetWindowSize()

	RL_SetWindowTitle( "Calculator" )
	RL_SetWindowState( FLAG_WINDOW_RESIZABLE )
	RL_SetWindowState( FLAG_VSYNC_HINT )
	RL_SetWindowSize( winSize )
	RL_SetWindowPosition( { mPos[1] + mSize[1] / 2 - winSize[1] / 2, mPos[2] + mSize[2] / 2 - winSize[2] / 2 } )

	initGui()
end

function process( delta )
	Gui.process( Vec2:new( RL_GetMousePosition() ) )
end

function draw()
	RL_ClearBackground( RAYWHITE )

	Gui.draw()
end