diff options
| author | jussi | 2024-01-05 23:15:22 +0200 |
|---|---|---|
| committer | jussi | 2024-01-05 23:15:22 +0200 |
| commit | 863f596b76043ec374fafa38f14cdc4a97d0b267 (patch) | |
| tree | 9fbf9c424e2a1644b52b795715cdb60b6ed04eae /examples/raygui_extensions | |
| parent | 70a2bcba18aa9855380c132f89e26b61bfd2cb40 (diff) | |
| download | reilua-enhanced-863f596b76043ec374fafa38f14cdc4a97d0b267.tar.gz reilua-enhanced-863f596b76043ec374fafa38f14cdc4a97d0b267.tar.bz2 reilua-enhanced-863f596b76043ec374fafa38f14cdc4a97d0b267.zip | |
Raygui lib enhancements and Raygui lib extensions example.
Diffstat (limited to 'examples/raygui_extensions')
| -rw-r--r-- | examples/raygui_extensions/main.lua | 229 | ||||
| -rw-r--r-- | examples/raygui_extensions/property_list.lua | 100 | ||||
| -rw-r--r-- | examples/raygui_extensions/sprite_button.lua | 50 |
3 files changed, 379 insertions, 0 deletions
diff --git a/examples/raygui_extensions/main.lua b/examples/raygui_extensions/main.lua new file mode 100644 index 0000000..6b87ce3 --- /dev/null +++ b/examples/raygui_extensions/main.lua @@ -0,0 +1,229 @@ +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 function addButton( bounds, text, callback ) + local button = 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 + ) + button.styles = { + { RL.LABEL, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_CENTER }, + { 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 ) }, + } +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 addPropertyList() + local propertyList = Gui:PropertyList( + Rect:new( 20, 20, 214, 256 ), + "Property List", + Rect:new( 0, 0, 200, 400 ), + -- Rect:new( 0, 0, 400, 400 ), + Vec2:new( 0, 0 ), + -- Callback. + nil, + -- Grab callback. + function( self ) Gui:set2Top( self ) end + ) + local bounds = Rect:new( 2, 2, 200 - 4, 22 ) + local gap = bounds.height + 2 + + propertyList.gui:Button( + bounds, + "Button", + function() print( "Button clicked" ) end + ) + ----- + bounds.y = bounds.y + gap + propertyList.gui:CheckBox( + Rect:new( bounds.x, bounds.y, 20, 20 ), + "Visible", + false, + function() print( "Checked!" ) end + ) + ----- + bounds.y = bounds.y + gap + propertyList.gui:Toggle( + bounds, + "Toggle", + false, + function( self ) print( "Toggled" ) end + ) + ----- + bounds.y = bounds.y + gap + local dropdown = propertyList.gui:DropdownBox( + bounds, + "Dog\nGiraffe\nLion\nHorse", + 0, + false, + function( self ) print( self:getItem( self.active ) ) end + ) + ----- + bounds.y = bounds.y + gap + propertyList.gui:Spinner( + Rect:new( bounds.x, bounds.y, 96, 20 ), + "Health", + 0, + 0, + 100, + false, + function( self ) print( "Spinner value changed to "..self.value ) end + ) + ----- + bounds.y = bounds.y + gap + propertyList.gui:TextBox( + bounds, + "Name", + 32, + false, + function( self ) print( "Set text "..self.text ) end + ) + ----- + bounds.y = bounds.y + gap + propertyList.gui:ColorBarAlpha( + bounds, + "", + 1.0 + ) + ----- + bounds.y = bounds.y + gap + local valueBox = propertyList.gui:ValueBox( + Rect:new( bounds.x, bounds.y, 96, 20 ), + "Mana", + 0, + 0, + 100, + false, + function( self ) print( "ValueBox value changed to "..self.value ) end + ) + valueBox.styles = { + { RL.VALUEBOX, RL.TEXT_PADDING, 2 }, + { RL.VALUEBOX, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_RIGHT }, + } + ----- + bounds.y = bounds.y + gap + propertyList.gui:Label( + bounds, + "Label" + ) + ----- + bounds.y = bounds.y + gap + propertyList.gui:Line( + bounds, + "Divider" + ) + ----- + bounds.y = bounds.y + gap + propertyList.gui:Slider( + Rect:new( bounds.x + 38, bounds.y, bounds.width - 80, bounds.height ), + "min", + "max", + 0, + 0, + 100, + function( self ) print( "Changed value "..self.value ) end + ) + ----- + bounds.y = bounds.y + gap + propertyList.gui:SliderBar( + Rect:new( bounds.x + 38, bounds.y, bounds.width - 80, bounds.height ), + "min", + "max", + 0, + 0, + 100, + function( self ) print( "Changed value "..self.value ) end + ) + ----- + bounds.y = bounds.y + gap + propertyList.gui:ProgressBar( + Rect:new( bounds.x + 38, bounds.y, bounds.width - 80, bounds.height ), + "min", + "max", + 0, + 0, + 100, + function( self ) print( "Changed value "..self.value ) end + ) + ----- + bounds.y = bounds.y + gap + propertyList.gui:ToggleGroup( + Rect:new( bounds.x, bounds.y, 64, bounds.height ), + "Cat;Dog;Car", + 0, + function( self ) print( self:getItem( self.active ) ) end + ) + ----- + bounds.y = bounds.y + gap + propertyList.gui:ColorPicker( + Rect:new( bounds.x, bounds.y, 128, 128 ), + "", + Color:new() + ) + + propertyList.content.height = bounds.y + 130 + propertyList.gui:set2Top( dropdown ) +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 ) + + addSpriteButtons() + addPropertyList() +end + +function RL.process( delta ) + Gui:process() +end + +function RL.draw() + RL.ClearBackground( { 50, 20, 75 } ) + Gui:draw() +end diff --git a/examples/raygui_extensions/property_list.lua b/examples/raygui_extensions/property_list.lua new file mode 100644 index 0000000..e05c84c --- /dev/null +++ b/examples/raygui_extensions/property_list.lua @@ -0,0 +1,100 @@ +local PropertyList = {} +PropertyList.__index = PropertyList + +function PropertyList:new( bounds, text, content, scroll, callback, grabCallback, dragCallback ) + local object = setmetatable( {}, self ) + object._parent = nil + + object.bounds = bounds:clone() + object.text = text + object.content = content:clone() + object.scroll = scroll:clone() + object.view = Rect:new() + object.callback = callback + object.grabCallback = grabCallback + object.dragCallback = dragCallback + + object.gui = Raygui:new() -- Contains full independent gui system. + + -- Set initial view and scroll. + local _, scrollP, view = RL.GuiScrollPanel( object.bounds, object.text, object.content, object.scroll, object.view ) + object.scroll = Vec2:new( scrollP ) + object.view = Rect:new( view ) + + object.gui.view = Rect:new( 0, 0, object.view.width, object.view.height ) + object.framebuffer = RL.LoadRenderTexture( { object.view.width, object.view.height } ) + + object.visible = true + object.disabled = false + object.draggable = true + object.mouseScale = 1 + + object:updateMouseOffset() + + return object +end + +function PropertyList:process() + if not RL.CheckCollisionRecs( self.view, RL.GetMousePosition() ) then + self.gui.locked = true + else + self.gui.locked = false + end + + self.gui:process() + + RL.BeginTextureMode( self.framebuffer ) + RL.ClearBackground( RL.BLANK ) + RL.rlTranslatef( { self.scroll.x, self.scroll.y, 0 } ) + self.gui:draw() + RL.EndTextureMode() + + return self._parent:drag( self ) +end + +function PropertyList:draw() + local oldScroll = self.scroll:clone() + local _, scroll, view = RL.GuiScrollPanel( self.bounds, self.text, self.content, self.scroll, self.view ) + + self.view = Rect:new( view ) + self.scroll = Vec2:new( scroll ) + + if self.scroll ~= oldScroll then + self._parent:checkScrolling() + + self.gui.view:set( -self.scroll.x, -self.scroll.y, self.view.width, self.view.height ) + self:updateMouseOffset() + + if self.callback ~= nil then + self.callback( self ) + end + end + + RL.DrawTexturePro( + RL.GetRenderTextureTexture( self.framebuffer ), + { 0, 0, self.view.width, -self.view.height }, + { math.floor( self.view.x ), math.floor( self.view.y ), self.view.width, self.view.height }, + { 0, 0 }, + 0.0, + RL.WHITE + ) +end + +function PropertyList:updateMouseOffset() + self.gui.mouseOffset = Vec2:new( -self.view.x - self.scroll.x, -self.view.y - self.scroll.y ):scale( self.mouseScale ) +end + +function PropertyList:setPosition( pos ) + self.bounds.x = pos.x + self.bounds.y = pos.y + + self:updateMouseOffset() +end + +function PropertyList:register( gui ) + function gui:PropertyList( bounds, text, texture, nPatchNormal, nPatchPressed, callback ) + return self:addElement( PropertyList:new( bounds, text, texture, nPatchNormal, nPatchPressed, callback ) ) + end +end + +return PropertyList diff --git a/examples/raygui_extensions/sprite_button.lua b/examples/raygui_extensions/sprite_button.lua new file mode 100644 index 0000000..57adc88 --- /dev/null +++ b/examples/raygui_extensions/sprite_button.lua @@ -0,0 +1,50 @@ +local SpriteButton = {} +SpriteButton.__index = SpriteButton + +function SpriteButton:new( bounds, text, texture, nPatchNormal, nPatchPressed, callback ) + local object = setmetatable( {}, self ) + object._parent = nil + + object.bounds = bounds:clone() + object.text = text + object.buttonTexture = texture + object.nPatchNormal = nPatchNormal + object.nPatchPressed = nPatchPressed + object.callback = callback + + object.visible = true + object.disabled = false + + return object +end + +function SpriteButton:process() + return RL.CheckCollisionPointRec( RL.GetMousePosition(), self.bounds ) +end + +function SpriteButton:draw() + if RL.IsMouseButtonDown( RL.MOUSE_BUTTON_LEFT ) and self:process() and not RL.GuiIsLocked() and not self._parent.scrolling then + RL.DrawTextureNPatchRepeat( self.buttonTexture, self.nPatchPressed, self.bounds, { 0, 0 }, 0.0, RL.WHITE ) + else + RL.DrawTextureNPatchRepeat( self.buttonTexture, self.nPatchNormal, self.bounds, { 0, 0 }, 0.0, RL.WHITE ) + end + + local result = RL.GuiLabelButton( self.bounds, self.text ) + + if result == 1 and self.callback ~= nil and self._parent:clickedInBounds( self.bounds ) then + self.callback( self ) + end +end + +function SpriteButton:setPosition( pos ) + self.bounds.x = pos.x + self.bounds.y = pos.y +end + +function SpriteButton:register( gui ) + function gui:SpriteButton( bounds, text, texture, nPatchNormal, nPatchPressed, callback ) + return self:addElement( SpriteButton:new( bounds, text, texture, nPatchNormal, nPatchPressed, callback ) ) + end +end + +return SpriteButton |
