From 973d902a16b35258629d2a0b228ad9c3f49b6198 Mon Sep 17 00:00:00 2001 From: jussi Date: Thu, 8 Dec 2022 18:24:41 +0200 Subject: ReiLuaGui File explorer. --- examples/ReiLuaGui/main.lua | 161 ------------ examples/ReiLuaGui_calculator/main.lua | 354 -------------------------- examples/ReiLuaGui_examples/calculator.lua | 282 ++++++++++++++++++++ examples/ReiLuaGui_examples/file_explorer.lua | 319 +++++++++++++++++++++++ examples/ReiLuaGui_examples/main.lua | 87 +++++++ examples/ReiLuaGui_test/main.lua | 161 ++++++++++++ examples/resources/images/LICENCE | 5 +- examples/resources/images/files.png | Bin 0 -> 7500 bytes examples/resources/images/open-folder.png | Bin 0 -> 6243 bytes examples/resources/images/previous-button.png | Bin 0 -> 6820 bytes examples/resources/lib/gui.lua | 23 +- examples/resources/lib/utillib.lua | 4 + 12 files changed, 879 insertions(+), 517 deletions(-) delete mode 100644 examples/ReiLuaGui/main.lua delete mode 100644 examples/ReiLuaGui_calculator/main.lua create mode 100644 examples/ReiLuaGui_examples/calculator.lua create mode 100644 examples/ReiLuaGui_examples/file_explorer.lua create mode 100644 examples/ReiLuaGui_examples/main.lua create mode 100644 examples/ReiLuaGui_test/main.lua create mode 100644 examples/resources/images/files.png create mode 100644 examples/resources/images/open-folder.png create mode 100644 examples/resources/images/previous-button.png (limited to 'examples') diff --git a/examples/ReiLuaGui/main.lua b/examples/ReiLuaGui/main.lua deleted file mode 100644 index 9890646..0000000 --- a/examples/ReiLuaGui/main.lua +++ /dev/null @@ -1,161 +0,0 @@ -package.path = package.path..";"..RL_GetBasePath().."../resources/lib/?.lua" - -util = require( "utillib" ) -Vec2 = require( "vector2" ) -Rect = require( "rectangle" ) -Color = require( "color" ) -Gui = require( "gui" ) - -local container = {} --- local circleTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/circle.png" ) -local circleTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/plain-circle.png" ) -local checkTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/check-mark.png" ) -local borderTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/ui_border.png" ) -local textInput - -RL_GenTextureMipmaps( circleTexture ) -RL_GenTextureMipmaps( checkTexture ) -RL_SetTextureFilter( circleTexture, TEXTURE_FILTER_TRILINEAR ) -RL_SetTextureFilter( checkTexture, TEXTURE_FILTER_TRILINEAR ) - -function initGui() - -- local label = Gui.label:new( { text = "Dog", bounds = { 32, 32, 96, 96 }, drawBounds = true, Haling = Gui.ALING.CENTER, Valing = Gui.ALING.TOP } ) - local panel = Gui.element:new( { bounds = Rect:new( 60, 32, 128, 128 ), drawBounds = true } ) - - container = Gui.container:new( { - bounds = Rect:new( 256, 120, 128, 128 ), - -- spacing = 14, - -- drawScrollRect = true, - -- HAling = Gui.ALING.RIGHT, - -- HAling = Gui.ALING.CENTER, - -- type = Gui.CONTAINER.HORIZONTAL, - -- VAling = Gui.ALING.CENTER, - -- type = Gui.CONTAINER.GRID, - -- columns = 2, - -- rows = 2, - scrollable = true, - showScrollbar = true, - } ) - - -- local container = Gui.container:new( { bounds = Rect:new( 256, 120, 128, 128 ), drawBounds = true, type = Gui.CONTAINER.HORIZONTAL } ) - - -- local itemBounds = { 0, 0, container.bounds.width - container.spacing * 2, 36 } - local itemBounds = Rect:new( 0, 0, 64, 36 ) - - local dog = Gui.element:new( { - bounds = Rect:new( 0, 0, 128, 36 ), - onClicked = function() panel:setPosition( Vec2:new( 290, 120 ) ) end, - onMouseOver = function( self ) self.items[1].color = RED end, - notMouseOver = function( self ) self.items[1].color = BLACK end, - drawBounds = true, - } ) - - dog:add( Gui.text:new( { text = "Dog", HAling = Gui.ALING.LEFT } ) ) - - dog:add( Gui.texture:new( { - bounds = dog.bounds:clone(), - texture = borderTexture, - HAling = Gui.ALING.CENTER, - VAling = Gui.ALING.CENTER, - visible = true, - nPatchInfo = { source = { 0, 0, 24, 24 }, left = 8, top = 8, right = 8, bottom = 8, layout = NPATCH_NINE_PATCH }, - } ) ) - - dog:add( Gui.texture:new( { bounds = Rect:new( 0, 0, 24, 24 ), texture = circleTexture, HAling = Gui.ALING.RIGHT, color = Color:new( 150, 150, 255 ) } ) ) - dog:add( Gui.texture:new( { bounds = Rect:new( 0, 0, 24, 24 ), texture = checkTexture, HAling = Gui.ALING.RIGHT, visible = true } ) ) - -- dog:add( Gui.text:new( { text = "Cat", HAling = Gui.ALING.RIGHT } ) ) - -- dog:add( Gui.shape:new( { bounds = Rect:new( 0, 0, 128, 36 ), shape = Gui.SHAPE.RECTANGLE_ROUNDED, color = Color:new( GREEN ) } ) ) - - container:add( dog ) - - -- container:add( Gui.element:new( { - -- text = "Cat", - -- bounds = Rect:new( 0, 0, 78, 24 ), - -- onClicked = function() panel:setPosition( Vec2:new( 290, 120 ) ) end, - -- onMouseOver = function( self ) self.color = RED end, - -- notMouseOver = function( self ) self.color = BLACK end, - -- drawBounds = true, - -- } ) ) - - for i = 1, 5 do - local element = Gui.element:new( { - bounds = Rect:new( 0, 0, 120, 30 ), - onClicked = function() panel:setPosition( Vec2:new( 340, 380 ) ) end, - onMouseOver = function( self ) self.color = Color:new( DARKBLUE ) end, - notMouseOver = function( self ) self.color = Color:new( LIGHTGRAY ) end, - drawBounds = true, - } ) - - element:add( Gui.text:new( { text = "Giraffe" } ) ) - container:add( element ) - end - - local container2 = Gui.container:new( { - bounds = Rect:new( 0, 0, 154, 64 ), - type = Gui.CONTAINER.HORIZONTAL, - } ) - - local element = Gui.element:new( { - bounds = itemBounds:clone(), - onMouseOver = function( self ) self.color = Color:new( DARKBLUE ) end, - notMouseOver = function( self ) self.color = Color:new( LIGHTGRAY ) end, - drawBounds = true, - } ) - element:add( Gui.text:new( { text = "Dog" } ) ) - container2:add( element ) - - element = Gui.element:new( { - bounds = Rect:new( 0, 0, 78, 24 ), - -- bounds = Rect:new( 0, 0, 78, 64 ), - onMouseOver = function( self ) self.color = Color:new( DARKBLUE ) end, - notMouseOver = function( self ) self.color = Color:new( LIGHTGRAY ) end, - drawBounds = true, - } ) - element:add( Gui.text:new( { text = "Cat" } ) ) - container2:add( element ) - - container:add( container2 ) - - panel:set2Top() - - -- Text input. - - textInput = Gui.element:new( { - bounds = Rect:new( 64, 360, 300, 32 ), - drawBounds = true, - color = Color:new( LIGHTGRAY ), - onClicked = function() Gui.setInputFocus( textInput ) end, - inputFocus = function() textInput.color = Color:new( BLUE ) end, - -- inputFocus = function() container:delete() end, - -- inputFocus = function() panel:set2Back() end, - inputUnfocus = function() textInput.color = Color:new( LIGHTGRAY ) end, - } ) - - textInput:add( Gui.text:new( { text = "", maxTextLen = 16, allowLineBreak = false } ) ) -end - -function init() - local monitor = 0 - local mPos = RL_GetMonitorPosition( monitor ) - local mSize = RL_GetMonitorSize( monitor ) - winSize = RL_GetWindowSize() - - RL_SetWindowTitle( "ReiLua Gui" ) - 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() - RL_DrawText( 0, "Work in progress GuiLib test.", { 10, 10 }, 30, 4, BLACK ) -end diff --git a/examples/ReiLuaGui_calculator/main.lua b/examples/ReiLuaGui_calculator/main.lua deleted file mode 100644 index 57ba9a1..0000000 --- a/examples/ReiLuaGui_calculator/main.lua +++ /dev/null @@ -1,354 +0,0 @@ -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 diff --git a/examples/ReiLuaGui_examples/calculator.lua b/examples/ReiLuaGui_examples/calculator.lua new file mode 100644 index 0000000..6f2023b --- /dev/null +++ b/examples/ReiLuaGui_examples/calculator.lua @@ -0,0 +1,282 @@ +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( WHITE ) end, + notMouseOver = function( self ) self.color = Color:new( LIGHTGRAY ) 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 + +return Calculator diff --git a/examples/ReiLuaGui_examples/file_explorer.lua b/examples/ReiLuaGui_examples/file_explorer.lua new file mode 100644 index 0000000..692093e --- /dev/null +++ b/examples/ReiLuaGui_examples/file_explorer.lua @@ -0,0 +1,319 @@ +FileExplorer = {} +FileExplorer.__index = FileExplorer + +function FileExplorer: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, 500, 330 ) + 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 = "File Explorer", 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 }, + } ) ) + + -- Path. + + object.pathBox = Gui.element:new( { + bounds = Rect:new( 0, 0, object.windowRect.width - 16 - 64, object.HANDLE_HIGHT ), + drawBounds = true, + color = Color:new( WHITE ), + -- onClicked = function() Gui.setInputFocus( object.pathBox ) end, + -- inputFocus = function() object.pathBox.color = Color:new( BLUE ) end, + -- inputUnfocus = function() object.pathBox.color = Color:new( WHITE ) end, + } ) + + object.pathBox:add( Gui.text:new( { text = "", maxTextLen = 30, allowLineBreak = false, VAling = Gui.ALING.CENTER } ) ) + + -- Back button. + + object.backButton = Gui.element:new( { + bounds = Rect:new( 0, 0, 56, object.HANDLE_HIGHT ), + drawBounds = true, + onMouseOver = function( self ) self.color = Color:new( WHITE ) end, + notMouseOver = function( self ) self.color = Color:new( LIGHTGRAY ) end, + onClicked = function() object:backDir() end, + } ) + + object.backButton:add( Gui.texture:new( { + bounds = Rect:new( 0, 0, object.HANDLE_HIGHT, object.HANDLE_HIGHT ), + texture = backTexture, + HAling = Gui.ALING.CENTER, + color = Color:new( BLACK ) + } ) ) + + -- Files. + + object.files = Gui.container:new( { + bounds = Rect:new( 0, 0, object.windowRect.width - 24, 200 ), + drawBounds = true, + scrollable = true, + showScrollbar = true, + -- drawScrollRect = true, + } ) + + -- File name. + + object.fileName = Gui.element:new( { + bounds = Rect:new( 0, 0, object.windowRect.width - 16 - 70, object.HANDLE_HIGHT ), + drawBounds = true, + color = Color:new( WHITE ), + } ) + + object.fileName:add( Gui.text:new( { text = "", maxTextLen = 32, allowLineBreak = false, VAling = Gui.ALING.CENTER } ) ) + + -- Open button. + + object.openButton = Gui.element:new( { + bounds = Rect:new( 0, 0, 64, object.HANDLE_HIGHT ), + drawBounds = true, + color = Color:new( WHITE ), + onClicked = function() object:openFile() end, + onMouseOver = function( self ) self.color = Color:new( WHITE ) end, + notMouseOver = function( self ) self.color = Color:new( LIGHTGRAY ) end, + } ) + + object.openButton:add( Gui.text:new( { text = "Open", VAling = Gui.ALING.CENTER, HAling = Gui.ALING.CENTER } ) ) + + -- Variables. + + object.path = RL_GetBasePath() + + -- Take last '/' away. + if util.utf8Sub( object.path, utf8.len( object.path ), utf8.len( object.path ) ) == "/" then + object.path = util.utf8Sub( object.path, 1, utf8.len( object.path ) - 1 ) + end + object.file = "" + + -- Update. + + object:setPosition( Vec2:new( object.windowRect.x, object.windowRect.y ) ) + object:updatePath() + + return object +end + +function FileExplorer:updatePath() + local maxLen = self.pathBox.items[1].maxTextLen + local pathLen = utf8.len( self.path ) + local text = "" + + if maxLen < pathLen then + text = self.path:sub( pathLen - maxLen + 1, pathLen ) + else + text = self.path + end + + self.pathBox.items[1]:set( text ) + + self:updateFiles() +end + +function FileExplorer:changeDir( path ) + self.path = path + + self:updatePath() +end + +function FileExplorer:backDir() + self.path = RL_GetPrevDirectoryPath( self.path ) + + self:updatePath() +end + +function FileExplorer:fileSelect( file ) + self.file = file + + self.fileName.items[1]:set( RL_GetFileName( file ) ) +end + +function FileExplorer:openFile() + print( self.file ) +end + +function FileExplorer:updateFiles() + self.files:scroll( Vec2:new( 0, 0 ) ) + + for _, cell in ipairs( self.files.cells ) do + cell:delete() + end + + self.files.cells = {} + + local files = {} + local folders = {} + + for _, file in ipairs( RL_LoadDirectoryFiles( self.path ) ) do + if RL_IsPathFile( file ) then + table.insert( files, file ) + else + table.insert( folders, file ) + end + end + + table.sort( files, function( a, b ) return a < b end ) + table.sort( folders, function( a, b ) return a < b end ) + + for _, folder in ipairs( folders ) do + self:addFileToList( folder, folderTexture, { 150, 120, 80 }, function() self:changeDir( folder ) end ) + end + + for _, file in ipairs( files ) do + self:addFileToList( file, filesTexture, WHITE, function() self:fileSelect( file ) end ) + end +end + +function FileExplorer:addFileToList( file, texture, color, func ) + self.files:add( Gui.element:new( { + bounds = Rect:new( 0, 0, self.windowRect.width - 16 - 20, 20 ), + padding = 4, + onClicked = func, + } ) ) + + local element = self.files.cells[ #self.files.cells ] + + element:add( Gui.text:new( { + bounds = Rect:new( 28, 0, 20, 20 ), + text = RL_GetFileName( file ), + fontSize = 20, + HAling = Gui.ALING.NONE, + VAling = Gui.ALING.CENTER, + } ) ) + + element:add( Gui.texture:new( { + bounds = Rect:new( 0, 0, 20, 20 ), + texture = texture, + VAling = Gui.ALING.CENTER, + color = Color:new( color ), + } ) ) + + element.bounds.width = element.items[1].bounds.width + element.padding + element.items[2].bounds.width +end + +function FileExplorer: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 FileExplorer: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.pathBox:setPosition( Vec2:new( pos.x + 8, pos.y + self.HANDLE_HIGHT + 8 ) ) + self.backButton:setPosition( Vec2:new( pos.x + self.pathBox.bounds.width + 16, pos.y + self.HANDLE_HIGHT + 8 ) ) + self.files:setPosition( Vec2:new( pos.x + 8, pos.y + self.HANDLE_HIGHT * 2 + 16 ) ) + self.fileName:setPosition( Vec2:new( pos.x + 8, pos.y + self.HANDLE_HIGHT * 2 + 16 + 208 ) ) + self.openButton:setPosition( Vec2:new( pos.x + 8 + 420, pos.y + self.HANDLE_HIGHT * 2 + 16 + 208 ) ) +end + +function FileExplorer:setVisible( visible ) + self.handle.visible = visible + self.handle.disabled = not visible + self.panel.visible = visible + self.panel.disabled = not visible + self.closeButton.visible = visible + self.closeButton.disabled = not visible + self.pathBox.visible = visible + self.pathBox.disabled = not visible + self.backButton.visible = visible + self.backButton.disabled = not visible + self.files.visible = visible + self.files.disabled = not visible + self.fileName.visible = visible + self.fileName.disabled = not visible + self.openButton.visible = visible + self.openButton.disabled = not visible + + self.files:update() +end + +function FileExplorer:set2Top() + self.panel:set2Top() + self.handle:set2Top() + self.closeButton:set2Top() + self.pathBox:set2Top() + self.backButton:set2Top() + self.files:set2Top() + self.fileName:set2Top() + self.openButton:set2Top() +end + +return FileExplorer diff --git a/examples/ReiLuaGui_examples/main.lua b/examples/ReiLuaGui_examples/main.lua new file mode 100644 index 0000000..3a54c30 --- /dev/null +++ b/examples/ReiLuaGui_examples/main.lua @@ -0,0 +1,87 @@ +package.path = package.path..";"..RL_GetBasePath().."?.lua" +package.path = package.path..";"..RL_GetBasePath().."../resources/lib/?.lua" + +util = require( "utillib" ) +Vec2 = require( "vector2" ) +Rect = require( "rectangle" ) +Color = require( "color" ) +Gui = require( "gui" ) + +Calculator = require( "calculator" ) +FileExplorer = require( "file_explorer" ) + +-- Textures. + +-- Note that textures are global. +cancelTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/cancel.png" ) +backTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/previous-button.png" ) +folderTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/open-folder.png" ) +filesTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/files.png" ) +borderTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/ui_border.png" ) +bgrTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/ui_bgr.png" ) + +RL_GenTextureMipmaps( cancelTexture ) +RL_GenTextureMipmaps( backTexture ) +RL_GenTextureMipmaps( folderTexture ) +RL_GenTextureMipmaps( filesTexture ) +RL_GenTextureMipmaps( borderTexture ) +RL_GenTextureMipmaps( bgrTexture ) + +RL_SetTextureFilter( cancelTexture, TEXTURE_FILTER_TRILINEAR ) +RL_SetTextureFilter( backTexture, TEXTURE_FILTER_TRILINEAR ) +RL_SetTextureFilter( folderTexture, TEXTURE_FILTER_TRILINEAR ) +RL_SetTextureFilter( filesTexture, TEXTURE_FILTER_TRILINEAR ) +RL_SetTextureFilter( borderTexture, TEXTURE_FILTER_TRILINEAR ) +RL_SetTextureFilter( bgrTexture, TEXTURE_FILTER_TRILINEAR ) + +RL_SetTextureWrap( borderTexture, TEXTURE_WRAP_REPEAT ) +RL_SetTextureWrap( bgrTexture, TEXTURE_WRAP_REPEAT ) + +-- End of calculator definition. + +local calculator = nil +local fileExplorer = nil +local showButton = nil + +function initGui() + showButton = Gui.element:new( { + bounds = Rect:new( 0, 0, 96, 32 ), + drawBounds = true, + onClicked = function() + calculator:setVisible( true ) + fileExplorer: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( 32, 96 ) ) + fileExplorer = FileExplorer:new( Vec2:new( 280, 96 ) ) +end + +function init() + local monitor = 0 + local mPos = RL_GetMonitorPosition( monitor ) + local mSize = RL_GetMonitorSize( monitor ) + winSize = RL_GetWindowSize() + + RL_SetWindowTitle( "ReiLuaGui examples" ) + 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 diff --git a/examples/ReiLuaGui_test/main.lua b/examples/ReiLuaGui_test/main.lua new file mode 100644 index 0000000..ecf3084 --- /dev/null +++ b/examples/ReiLuaGui_test/main.lua @@ -0,0 +1,161 @@ +package.path = package.path..";"..RL_GetBasePath().."../resources/lib/?.lua" + +util = require( "utillib" ) +Vec2 = require( "vector2" ) +Rect = require( "rectangle" ) +Color = require( "color" ) +Gui = require( "gui" ) + +local container = {} +-- local circleTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/circle.png" ) +local circleTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/plain-circle.png" ) +local checkTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/check-mark.png" ) +local borderTexture = RL_LoadTexture( RL_GetBasePath().."../resources/images/ui_border.png" ) +local textInput + +RL_GenTextureMipmaps( circleTexture ) +RL_GenTextureMipmaps( checkTexture ) +RL_SetTextureFilter( circleTexture, TEXTURE_FILTER_TRILINEAR ) +RL_SetTextureFilter( checkTexture, TEXTURE_FILTER_TRILINEAR ) + +function initGui() + -- local label = Gui.label:new( { text = "Dog", bounds = { 32, 32, 96, 96 }, drawBounds = true, Haling = Gui.ALING.CENTER, Valing = Gui.ALING.TOP } ) + local panel = Gui.element:new( { bounds = Rect:new( 60, 32, 128, 128 ), drawBounds = true } ) + + container = Gui.container:new( { + bounds = Rect:new( 256, 120, 128, 128 ), + -- spacing = 14, + -- drawScrollRect = true, + -- HAling = Gui.ALING.RIGHT, + -- HAling = Gui.ALING.CENTER, + -- type = Gui.CONTAINER.HORIZONTAL, + -- VAling = Gui.ALING.CENTER, + -- type = Gui.CONTAINER.GRID, + -- columns = 2, + -- rows = 2, + scrollable = true, + showScrollbar = true, + } ) + + -- local container = Gui.container:new( { bounds = Rect:new( 256, 120, 128, 128 ), drawBounds = true, type = Gui.CONTAINER.HORIZONTAL } ) + + -- local itemBounds = { 0, 0, container.bounds.width - container.spacing * 2, 36 } + local itemBounds = Rect:new( 0, 0, 64, 36 ) + + local dog = Gui.element:new( { + bounds = Rect:new( 0, 0, 128, 36 ), + onClicked = function() panel:setPosition( Vec2:new( 290, 120 ) ) end, + onMouseOver = function( self ) self.items[1].color = RED end, + notMouseOver = function( self ) self.items[1].color = BLACK end, + drawBounds = true, + } ) + + dog:add( Gui.text:new( { text = "Dog", HAling = Gui.ALING.LEFT } ) ) + + dog:add( Gui.texture:new( { + bounds = dog.bounds:clone(), + texture = borderTexture, + HAling = Gui.ALING.CENTER, + VAling = Gui.ALING.CENTER, + visible = true, + nPatchInfo = { source = { 0, 0, 24, 24 }, left = 8, top = 8, right = 8, bottom = 8, layout = NPATCH_NINE_PATCH }, + } ) ) + + dog:add( Gui.texture:new( { bounds = Rect:new( 0, 0, 24, 24 ), texture = circleTexture, HAling = Gui.ALING.RIGHT, color = Color:new( 150, 150, 255 ) } ) ) + dog:add( Gui.texture:new( { bounds = Rect:new( 0, 0, 24, 24 ), texture = checkTexture, HAling = Gui.ALING.RIGHT, visible = true } ) ) + -- dog:add( Gui.text:new( { text = "Cat", HAling = Gui.ALING.RIGHT } ) ) + -- dog:add( Gui.shape:new( { bounds = Rect:new( 0, 0, 128, 36 ), shape = Gui.SHAPE.RECTANGLE_ROUNDED, color = Color:new( GREEN ) } ) ) + + container:add( dog ) + + -- container:add( Gui.element:new( { + -- text = "Cat", + -- bounds = Rect:new( 0, 0, 78, 24 ), + -- onClicked = function() panel:setPosition( Vec2:new( 290, 120 ) ) end, + -- onMouseOver = function( self ) self.color = RED end, + -- notMouseOver = function( self ) self.color = BLACK end, + -- drawBounds = true, + -- } ) ) + + for i = 1, 5 do + local element = Gui.element:new( { + bounds = Rect:new( 0, 0, 120, 30 ), + onClicked = function() panel:setPosition( Vec2:new( 340, 380 ) ) end, + onMouseOver = function( self ) self.color = Color:new( DARKBLUE ) end, + notMouseOver = function( self ) self.color = Color:new( LIGHTGRAY ) end, + drawBounds = true, + } ) + + element:add( Gui.text:new( { text = "Giraffe" } ) ) + container:add( element ) + end + + local container2 = Gui.container:new( { + bounds = Rect:new( 0, 0, 154, 64 ), + type = Gui.CONTAINER.HORIZONTAL, + } ) + + local element = Gui.element:new( { + bounds = itemBounds:clone(), + onMouseOver = function( self ) self.color = Color:new( DARKBLUE ) end, + notMouseOver = function( self ) self.color = Color:new( LIGHTGRAY ) end, + drawBounds = true, + } ) + element:add( Gui.text:new( { text = "Dog" } ) ) + container2:add( element ) + + element = Gui.element:new( { + bounds = Rect:new( 0, 0, 78, 24 ), + -- bounds = Rect:new( 0, 0, 78, 64 ), + onMouseOver = function( self ) self.color = Color:new( DARKBLUE ) end, + notMouseOver = function( self ) self.color = Color:new( LIGHTGRAY ) end, + drawBounds = true, + } ) + element:add( Gui.text:new( { text = "Cat" } ) ) + container2:add( element ) + + container:add( container2 ) + + panel:set2Top() + + -- Text input. + + textInput = Gui.element:new( { + bounds = Rect:new( 64, 360, 300, 32 ), + drawBounds = true, + color = Color:new( LIGHTGRAY ), + onClicked = function() Gui.setInputFocus( textInput ) end, + inputFocus = function() textInput.color = Color:new( BLUE ) end, + -- inputFocus = function() container:delete() end, + -- inputFocus = function() panel:set2Back() end, + inputUnfocus = function() textInput.color = Color:new( LIGHTGRAY ) end, + } ) + + textInput:add( Gui.text:new( { text = "", maxTextLen = 16, allowLineBreak = false } ) ) +end + +function init() + local monitor = 0 + local mPos = RL_GetMonitorPosition( monitor ) + local mSize = RL_GetMonitorSize( monitor ) + winSize = RL_GetWindowSize() + + RL_SetWindowTitle( "ReiLuaGui Test" ) + 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() + RL_DrawText( 0, "Work in progress GuiLib test.", { 10, 10 }, 30, 4, BLACK ) +end diff --git a/examples/resources/images/LICENCE b/examples/resources/images/LICENCE index bfe263f..3cf6909 100644 --- a/examples/resources/images/LICENCE +++ b/examples/resources/images/LICENCE @@ -9,4 +9,7 @@ ui_bgr.png Jussi Viitala CC0 check-mark.png Delapouite Creative Commons 3.0 https://game-icons.net Resized circle.png Delapouite Creative Commons 3.0 https://game-icons.net Resized plain-circle.png Delapouite Creative Commons 3.0 https://game-icons.net Resized -cancel.png Sbed Creative Commons 3.0 https://game-icons.net Resized \ No newline at end of file +previous-button.png Delapouite Creative Commons 3.0 https://game-icons.net Resized +open-folder.png Delapouite Creative Commons 3.0 https://game-icons.net Resized +files.png Delapouite Creative Commons 3.0 https://game-icons.net Resized +cancel.png Sbed Creative Commons 3.0 https://game-icons.net Resized diff --git a/examples/resources/images/files.png b/examples/resources/images/files.png new file mode 100644 index 0000000..503e0d9 Binary files /dev/null and b/examples/resources/images/files.png differ diff --git a/examples/resources/images/open-folder.png b/examples/resources/images/open-folder.png new file mode 100644 index 0000000..8a14119 Binary files /dev/null and b/examples/resources/images/open-folder.png differ diff --git a/examples/resources/images/previous-button.png b/examples/resources/images/previous-button.png new file mode 100644 index 0000000..fa4ba7e Binary files /dev/null and b/examples/resources/images/previous-button.png differ diff --git a/examples/resources/lib/gui.lua b/examples/resources/lib/gui.lua index c7609c1..4e4ee7e 100644 --- a/examples/resources/lib/gui.lua +++ b/examples/resources/lib/gui.lua @@ -226,7 +226,7 @@ Text.__index = Text function Text:new( set ) local object = setmetatable( {}, Text ) - object.bounds = Rect:new( 0, 0, 0, 0 ) + object.bounds = setProperty( set, "bounds", Rect:new( 0, 0, 0, 0 ) ) object.HAling = setProperty( set, "HAling", Gui.ALING.LEFT ) object.VAling = setProperty( set, "VAling", Gui.ALING.BOTTOM ) @@ -557,6 +557,8 @@ function Container:new( set ) object.showScrollbar = setProperty( set, "showScrollbar", false ) object.scrollbarWidth = setProperty( set, "scrollbarWidth", Gui.scrollbarWidth ) object.scrollAmount = setProperty( set, "scrollAmount", Gui.scrollAmount ) -- When using mouse scroll. + object.color = setProperty( set, "color", Color:new( WHITE ) ) + object.drawBounds = setProperty( set, "drawBounds", false ) object.drawScrollRect = setProperty( set, "drawScrollRect", false ) -- For grid container. Do not set both. object.columns = setProperty( set, "columns", nil ) @@ -661,6 +663,12 @@ function Container:updateScrollbar() self._VScrollbar.items[1].bounds.width = self.scrollbarWidth self._VScrollbar.items[1].bounds.height = self.bounds.height / self._scrollRect.height * self.bounds.height self._VScrollbar.items[1].bounds.y = self._scrollRect.y / self._scrollRect.height * self.bounds.height + + self._VScrollbar.visible = self.visible + self._VScrollbar.disabled = self.disabled + else + self._VScrollbar.visible = false + self._VScrollbar.disabled = true end if self.bounds.width < self._scrollRect.width then @@ -672,6 +680,12 @@ function Container:updateScrollbar() self._HScrollbar.items[1].bounds.width = self.bounds.width / self._scrollRect.width * self.bounds.width self._HScrollbar.items[1].bounds.height = self.scrollbarWidth self._HScrollbar.items[1].bounds.x = self._scrollRect.x / self._scrollRect.width * self.bounds.width + + self._HScrollbar.visible = self.visible + self._HScrollbar.disabled = self.disabled + else + self._HScrollbar.visible = false + self._HScrollbar.disabled = true end end @@ -727,6 +741,9 @@ function Container:update() cell._visibilityBounds = self._visibilityBounds end + cell.visible = self.visible + cell.disabled = self.disabled + if self.type == Gui.CONTAINER.VERTICAL then if self.HAling == Gui.ALING.CENTER then pos.x = self.bounds.x + self.bounds.width / 2 - cell.bounds.width / 2 @@ -838,6 +855,10 @@ function Container:set2Back() end function Container:draw() + if self.drawBounds then + RL_DrawRectangle( self.bounds, self.color ) + end + if self.drawScrollRect then RL_DrawRectangleLines( { self.bounds.x - self._scrollRect.x, diff --git a/examples/resources/lib/utillib.lua b/examples/resources/lib/utillib.lua index d6e5019..7fb7405 100644 --- a/examples/resources/lib/utillib.lua +++ b/examples/resources/lib/utillib.lua @@ -152,6 +152,10 @@ function utillib.toBoolean( v ) return false end +function utillib.boo2Number( bool ) + return bool and 1 or 0 +end + -- Print table content. function utillib.printt( t ) print( tostring(t).." = {" ) -- cgit v1.2.3