From 95f03bae31ab695f55c4cb4b166e0af80814d841 Mon Sep 17 00:00:00 2001 From: jussi Date: Thu, 21 Mar 2024 23:18:35 +0200 Subject: Raygui lib callbacks to single table. --- examples/raygui_extensions/main.lua | 105 ++++++++++++++++----------- examples/raygui_extensions/property_list.lua | 20 ++--- examples/raygui_extensions/sprite_button.lua | 12 +-- examples/raygui_extensions/tree_item.lua | 6 +- examples/raygui_extensions/tree_view.lua | 16 ++-- 5 files changed, 89 insertions(+), 70 deletions(-) (limited to 'examples/raygui_extensions') diff --git a/examples/raygui_extensions/main.lua b/examples/raygui_extensions/main.lua index 6efa18d..cdfd7b8 100644 --- a/examples/raygui_extensions/main.lua +++ b/examples/raygui_extensions/main.lua @@ -55,13 +55,13 @@ local function addSpriteButtons() 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 ) + addButton( bounds, "Start New Game", { pressed = function() print( "New Game!" ) end } ) bounds.y = bounds.y + gap - addButton( bounds, "Load Game", function() print( "Load Game!" ) end ) + addButton( bounds, "Load Game", { pressed = function() print( "Load Game!" ) end } ) bounds.y = bounds.y + gap - addButton( bounds, "Options", function() print( "Options!" ) end ) + addButton( bounds, "Options", { pressed = function() print( "Options!" ) end } ) bounds.y = bounds.y + gap - addButton( bounds, "Quit", function() RL.CloseWindow() end ) + addButton( bounds, "Quit", { pressed = function() RL.CloseWindow() end } ) end local function getTextValue( text ) @@ -74,10 +74,10 @@ local function addPropertyList() PropertyList = Gui:PropertyList( Rect:new( 20, 20, 256, 328 ), "Property List", - nil, -- Callback. - function( self ) Gui:set2Top( self ) end, -- Grab callback. - nil, -- Drag callback. - { + { -- Callbacks. + grab = function( self ) Gui:set2Top( self ) end, + }, + { -- Styles. properties = { -- { RL.SCROLLBAR, RL.ARROWS_VISIBLE, RL.ARROWS_VISIBLE }, { RL.LISTVIEW, RL.BORDER_COLOR_FOCUSED, RL.GuiGetStyle( RL.LISTVIEW, RL.BORDER_COLOR_NORMAL ) }, @@ -106,11 +106,13 @@ local function addPropertyList() cat.dest.x, 32, false, - function( self ) - self.value = getTextValue( self.text ) - self.text = tostring( self.value ) - cat.dest.x = self.value - end, + { -- Callbacks. + edit = function( self ) + self.value = getTextValue( self.text ) + self.text = tostring( self.value ) + cat.dest.x = self.value + end + }, nil, "Position X" ), transformGroup, true ) @@ -119,11 +121,13 @@ local function addPropertyList() cat.dest.y, 32, false, - function( self ) - self.value = getTextValue( self.text ) - self.text = tostring( self.value ) - cat.dest.y = self.value - end, + { -- Callbacks. + edit = function( self ) + self.value = getTextValue( self.text ) + self.text = tostring( self.value ) + cat.dest.y = self.value + end + }, nil, "Position Y" ), transformGroup ) @@ -137,11 +141,13 @@ local function addPropertyList() cat.dest.x, 32, false, - function( self ) - self.value = getTextValue( self.text ) - self.text = tostring( self.value ) - cat.origin.x = self.value - end, + { -- Callbacks. + edit = function( self ) + self.value = getTextValue( self.text ) + self.text = tostring( self.value ) + cat.origin.x = self.value + end + }, nil, "Origin X" ), transformGroup, true ) @@ -150,11 +156,13 @@ local function addPropertyList() cat.dest.y, 32, false, - function( self ) - self.value = getTextValue( self.text ) - self.text = tostring( self.value ) - cat.origin.y = self.value - end, + { -- Callbacks. + edit = function( self ) + self.value = getTextValue( self.text ) + self.text = tostring( self.value ) + cat.origin.y = self.value + end + }, nil, "Origin Y" ), transformGroup ) @@ -166,11 +174,13 @@ local function addPropertyList() 0, 0, 360, - function( self ) - self.value = Util.round( self.value ) - cat.rotation = self.value - self.textRight = self.value - end, + { -- Callbacks. + edit = function( self ) + self.value = Util.round( self.value ) + cat.rotation = self.value + self.textRight = self.value + end + }, nil, "Rotation angle" ), transformGroup ) @@ -179,7 +189,9 @@ local function addPropertyList() Rect:new( 0, 0, 20, 20 ), "Flipped", cat.flipped, - function( self ) cat.flipped = self.checked end, + { -- Callbacks. + pressed = function( self ) cat.flipped = self.checked end + }, nil, "Flips the image" ), transformGroup ) @@ -192,7 +204,9 @@ local function addPropertyList() Rect:new( 0, 0, 20, 20 ), "Visible", cat.visible, - function( self ) cat.visible = self.checked end, + { -- Callbacks. + pressed = function( self ) cat.visible = self.checked end + }, { properties = { { RL.CHECKBOX, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_RIGHT }, @@ -206,7 +220,9 @@ local function addPropertyList() Rect:new( 0, 0, 128, 128 ), "Color Picker", Color:new(), - function( self ) cat.tint = self.color end + { -- Callbacks. + edit = function( self ) cat.tint = self.color end + } ), tintGroup ) PropertyList:addControl( PropertyList.gui:Line( @@ -219,7 +235,9 @@ local function addPropertyList() "Dog\nGiraffe\nLion\nHorse", 0, false, - function( self ) print( self:getItem( self.active ) ) end + { -- Callbacks. + select = function( self ) print( self:getItem( self.active ) ) end + } ) ) local test = PropertyList:addGroup( "Test", false ) @@ -229,7 +247,9 @@ local function addPropertyList() Rect:new( 128, 0, 20, 20 ), i.."_Visible", false, - function( self ) print( "Checked" ) end, + { -- Callbacks. + pressed = function( self ) print( "Checked" ) end + }, { properties = { -- { RL.CHECKBOX, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_LEFT }, @@ -250,10 +270,11 @@ local function addTreeView() TreeView = Gui:TreeView( Rect:new( 600, 20, 256, 328 ), "Tree View", - function( controls ) selected( controls ) end, -- Callback. - function( self ) Gui:set2Top( self ) end, -- Grab callback. - nil, -- Drag callback. - { + { -- Callbacks. + select = function( controls ) selected( controls ) end, + grab = function( self ) Gui:set2Top( self ) end, + }, + { -- Styles. properties = { -- { RL.SCROLLBAR, RL.ARROWS_VISIBLE, RL.ARROWS_VISIBLE }, { RL.LISTVIEW, RL.BORDER_COLOR_FOCUSED, RL.GuiGetStyle( RL.LISTVIEW, RL.BORDER_COLOR_NORMAL ) }, diff --git a/examples/raygui_extensions/property_list.lua b/examples/raygui_extensions/property_list.lua index 492f394..6d218e6 100644 --- a/examples/raygui_extensions/property_list.lua +++ b/examples/raygui_extensions/property_list.lua @@ -1,7 +1,7 @@ local PropertyList = {} PropertyList.__index = PropertyList -function PropertyList:new( bounds, text, callback, grabCallback, dragCallback, styles, tooltip ) +function PropertyList:new( bounds, text, callbacks, styles, tooltip ) local object = setmetatable( {}, self ) object._gui = nil @@ -12,9 +12,7 @@ function PropertyList:new( bounds, text, callback, grabCallback, dragCallback, s object.text = text object.scroll = Vec2:new() object.view = Rect:new() - object.callback = callback - object.grabCallback = grabCallback - object.dragCallback = dragCallback + object.callbacks = callbacks -- scroll, grab, drag. object.styles = styles object.tooltip = tooltip @@ -140,8 +138,10 @@ function PropertyList:addGroup( name, active, group ) self:getDefaultBounds(), setGroupText( name, active ), active, - function( this ) this.text = setGroupText( name, this.active ) self:updateContent() end, - { + { -- Callbacks. + pressed = function( this ) this.text = setGroupText( name, this.active ) self:updateContent() end, + }, + { -- Styles. properties = { { RL.TOGGLE, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_LEFT } } @@ -194,8 +194,8 @@ function PropertyList:draw() self:updateMouseOffset() self.gui.view:set( -self.scroll.x, -self.scroll.y, self.view.width, self.view.height ) - if self.callback ~= nil then - self.callback( self ) + if self.callbacks.scroll ~= nil then + self.callbacks.scroll( self ) end end @@ -250,8 +250,8 @@ function PropertyList:setSize( size ) end function PropertyList:register( gui ) - function gui:PropertyList( bounds, text, callback, grabCallback, dragCallback, styles, tooltip ) - return self:addControl( PropertyList:new( bounds, text, callback, grabCallback, dragCallback, styles, tooltip ) ) + function gui:PropertyList( bounds, text, callbacks, styles, tooltip ) + return self:addControl( PropertyList:new( bounds, text, callbacks, styles, tooltip ) ) end end diff --git a/examples/raygui_extensions/sprite_button.lua b/examples/raygui_extensions/sprite_button.lua index ba4b704..8a3027b 100644 --- a/examples/raygui_extensions/sprite_button.lua +++ b/examples/raygui_extensions/sprite_button.lua @@ -1,7 +1,7 @@ local SpriteButton = {} SpriteButton.__index = SpriteButton -function SpriteButton:new( bounds, text, texture, nPatchNormal, nPatchPressed, callback, styles, tooltip ) +function SpriteButton:new( bounds, text, texture, nPatchNormal, nPatchPressed, callbacks, styles, tooltip ) local object = setmetatable( {}, self ) object._gui = nil @@ -10,7 +10,7 @@ function SpriteButton:new( bounds, text, texture, nPatchNormal, nPatchPressed, c object.buttonTexture = texture object.nPatchNormal = nPatchNormal object.nPatchPressed = nPatchPressed - object.callback = callback + object.callbacks = callbacks -- pressed. object.styles = styles object.tooltip = tooltip @@ -33,8 +33,8 @@ function SpriteButton:draw() local result = RL.GuiLabelButton( self.bounds, self.text ) - if result == 1 and self.callback ~= nil and self._gui:clickedInBounds( self.bounds ) then - self.callback( self ) + if result == 1 and self.callbacks.pressed ~= nil and self._gui:clickedInBounds( self.bounds ) then + self.callbacks.pressed( self ) end end @@ -44,8 +44,8 @@ function SpriteButton:setPosition( pos ) end function SpriteButton:register( gui ) - function gui:SpriteButton( bounds, text, texture, nPatchNormal, nPatchPressed, callback, styles, tooltip ) - return self:addControl( SpriteButton:new( bounds, text, texture, nPatchNormal, nPatchPressed, callback, styles, tooltip ) ) + function gui:SpriteButton( bounds, text, texture, nPatchNormal, nPatchPressed, callbacks, styles, tooltip ) + return self:addControl( SpriteButton:new( bounds, text, texture, nPatchNormal, nPatchPressed, callbacks, styles, tooltip ) ) end end diff --git a/examples/raygui_extensions/tree_item.lua b/examples/raygui_extensions/tree_item.lua index 043e348..1986faf 100644 --- a/examples/raygui_extensions/tree_item.lua +++ b/examples/raygui_extensions/tree_item.lua @@ -9,7 +9,7 @@ function TreeItem:new( bounds, text, callbacks, styles, tooltip ) object.bounds = bounds:clone() object.text = text - object.callbacks = callbacks -- toggle, open. + object.callbacks = callbacks -- select, open. object.controls = {} @@ -68,8 +68,8 @@ function TreeItem:draw() local oldActive = self.active _, self.active = RL.GuiToggle( toggleRect, self.text, self.active ) - if self.callbacks.toggle and oldActive ~= self.active then - self.callbacks.toggle( self ) + if self.callbacks.select and oldActive ~= self.active then + self.callbacks.select( self ) end if hasContainer then diff --git a/examples/raygui_extensions/tree_view.lua b/examples/raygui_extensions/tree_view.lua index 5b231be..ceb0359 100644 --- a/examples/raygui_extensions/tree_view.lua +++ b/examples/raygui_extensions/tree_view.lua @@ -10,7 +10,7 @@ TreeView.MOVE_ITEM_IN = 1 TreeView.MOVE_ITEM_UP = 2 TreeView.MOVE_ITEM_DOWN = 3 -function TreeView:new( bounds, text, callback, grabCallback, dragCallback, styles, tooltip ) +function TreeView:new( bounds, text, callbacks, styles, tooltip ) local object = setmetatable( {}, self ) object._gui = nil @@ -22,9 +22,7 @@ function TreeView:new( bounds, text, callback, grabCallback, dragCallback, style object.text = text object.scroll = Vec2:new() object.view = Rect:new() - object.callback = callback - object.grabCallback = grabCallback - object.dragCallback = dragCallback + object.callbacks = callbacks -- select, grab, drag. object.styles = styles object.tooltip = tooltip @@ -132,7 +130,7 @@ function TreeView:addItem( name, group ) name, { -- Callbacks. open = function( this ) self:updateContent() end, - toggle = function( this ) self:itemSelect( this ) end, + select = function( this ) self:itemSelect( this ) end, }, { -- Styles. properties = { @@ -262,8 +260,8 @@ function TreeView:itemSelect( item ) self._lastActiveItem = item -- Old clicked. end - if self.callback ~= nil then - -- self.callback( self.selectedItems ) + if self.callbacks.select ~= nil then + self.callbacks.select( self.selectedItems ) end end @@ -399,8 +397,8 @@ function TreeView:setSize( size ) end function TreeView:register( gui ) - function gui:TreeView( bounds, text, callback, grabCallback, dragCallback, styles, tooltip ) - return self:addControl( TreeView:new( bounds, text, callback, grabCallback, dragCallback, styles, tooltip ) ) + function gui:TreeView( bounds, text, callbacks, styles, tooltip ) + return self:addControl( TreeView:new( bounds, text, callbacks, styles, tooltip ) ) end end -- cgit v1.2.3