Raygui wrapper examples.

This commit is contained in:
jussi
2023-04-17 17:55:27 +03:00
parent e732b936d4
commit 8182c486e6
6 changed files with 486 additions and 117 deletions

View File

@@ -0,0 +1,160 @@
Calculator = {}
Calculator.__index = Calculator
Calculator.OPERATIONS = {
ADD = 0,
SUB = 1,
DIV = 2,
MUL = 3,
EQUAL = 4,
CLEAR = 5,
}
function Calculator:new( pos )
local object = setmetatable( {}, Calculator )
object.window = Raygui.WindowBox:new(
Rect:new( pos.x, pos.y, 188, 216 ),
"Calculator",
-- Close callback.
function() object:setVisible( false ) end,
-- Grab callback.
function() object:set2Top() end,
-- Drag callback.
function( self ) object:setPosition( Vec2:new( self.bounds.x, self.bounds.y ) ) end
)
object.display = Raygui.Label:new(
Rect:new( 0, 0, 180, 20 ),
""
)
object.display.position = Vec2:new( 10, 32 )
object.buttons = {}
local buttons = {
{ "7", function() object:addNumber( 7 ) end },
{ "8", function() object:addNumber( 8 ) end },
{ "9", function() object:addNumber( 9 ) end },
{ "/", function() object:addOperation( self.OPERATIONS.DIV ) end },
{ "4", function() object:addNumber( 4 ) end },
{ "5", function() object:addNumber( 5 ) end },
{ "6", function() object:addNumber( 6 ) end },
{ "*", function() object:addOperation( self.OPERATIONS.MUL ) end },
{ "1", function() object:addNumber( 1 ) end },
{ "2", function() object:addNumber( 2 ) end },
{ "3", function() object:addNumber( 3 ) end },
{ "-", function() object:addOperation( self.OPERATIONS.SUB ) end },
{ "0", function() object:addNumber( 0 ) end },
{ "C", function() object:addOperation( self.OPERATIONS.CLEAR ) end },
{ "=", function() object:addOperation( self.OPERATIONS.EQUAL ) end },
{ "+", function() object:addOperation( self.OPERATIONS.ADD ) end },
}
local rowCount = 4
local buttonRect = Rect:new( 5, 64, 40, 32 )
local startPos = Vec2:new( buttonRect.x, buttonRect.y )
local buttonSpacing = 6
for i, button in ipairs( buttons ) do
table.insert( object.buttons, Raygui.Button:new(
buttonRect:clone(),
button[1],
button[2]
) )
-- Set position in window.
object.buttons[i].position = Vec2:new( buttonRect.x, buttonRect.y )
if i % rowCount == 0 then
buttonRect.x = startPos.x
buttonRect.y = buttonRect.y + buttonRect.height + buttonSpacing
else
buttonRect.x = buttonRect.x + buttonRect.width + buttonSpacing
end
end
object.visible = true
object.numbers = { "", "" }
object.op = nil
object:setPosition( pos )
return object
end
function Calculator:addNumber( num )
if self.op == nil then
self.numbers[1] = self.numbers[1]..tostring( num )
else
self.numbers[2] = self.numbers[2]..tostring( num )
end
self:updateDisplay()
end
function Calculator:addOperation( op )
if op == self.OPERATIONS.EQUAL then
if self.op == self.OPERATIONS.ADD then
self.numbers[1] = tonumber( self.numbers[1] ) + tonumber( self.numbers[2] )
elseif self.op == self.OPERATIONS.SUB then
self.numbers[1] = tonumber( self.numbers[1] ) - tonumber( self.numbers[2] )
elseif self.op == self.OPERATIONS.MUL then
self.numbers[1] = tonumber( self.numbers[1] ) * tonumber( self.numbers[2] )
elseif self.op == self.OPERATIONS.DIV then
self.numbers[1] = tonumber( self.numbers[1] ) / tonumber( self.numbers[2] )
end
self.numbers[2] = ""
self.op = nil
elseif op == self.OPERATIONS.CLEAR then
self.numbers[1] = ""
self.numbers[2] = ""
self.op = nil
else
self.op = op
end
self:updateDisplay()
end
function Calculator:updateDisplay()
self.display.text = self.numbers[1]
if self.op == self.OPERATIONS.ADD then
self.display.text = self.display.text.."+"
elseif self.op == self.OPERATIONS.SUB then
self.display.text = self.display.text.."-"
elseif self.op == self.OPERATIONS.MUL then
self.display.text = self.display.text.."*"
elseif self.op == self.OPERATIONS.DIV then
self.display.text = self.display.text.."/"
end
self.display.text = self.display.text..self.numbers[2]
end
function Calculator:setPosition( pos )
self.display:setPosition( pos + self.display.position )
for _, button in ipairs( self.buttons ) do
button:setPosition( pos + button.position )
end
end
function Calculator:set2Top()
Raygui.set2Top( self.window )
Raygui.set2Top( self.display )
for _, button in ipairs( self.buttons ) do
Raygui.set2Top( button )
end
end
function Calculator:setVisible( visible )
self.visible = visible
self.window.visible = visible
self.display.visible = visible
for _, button in ipairs( self.buttons ) do
button.visible = visible
end
end
return Calculator

View File

@@ -0,0 +1,44 @@
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" )
Raygui = require( "raygui" )
Calculator = require( "calculator" )
local calculator = nil
local calculator2 = nil
function RL.init()
local monitor = 0
local mPos = Vec2:new( RL.GetMonitorPosition( monitor ) )
local mSize = Vec2:new( RL.GetMonitorSize( monitor ) )
local winSize = Vec2:new( 1024, 800 )
RL.SetWindowTitle( "Raygui examples" )
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 )
calculator = Calculator:new( Vec2:new( 32, 32 ) )
calculator2 = Calculator:new( Vec2:new( 64, 65 ) )
end
function RL.process( delta )
-- Gui:process()
Raygui:process()
end
function RL.draw()
RL.ClearBackground( RL.DARKBLUE )
-- Gui:draw()
Raygui:draw()
end