From 02c7dd9ca80e522b987ebce34143f2eef1b95b64 Mon Sep 17 00:00:00 2001 From: jussi Date: Wed, 2 Oct 2024 14:06:50 +0300 Subject: Raygui update. --- examples/raygui_extensions/main.lua | 4 +- examples/raygui_extensions/property_list.lua | 31 ++++----- examples/raygui_extensions/sprite_button.lua | 27 +++++--- examples/raygui_extensions/tree_view.lua | 10 +-- examples/resources/lib/raygui.lua | 94 +++++++++++++++------------- 5 files changed, 87 insertions(+), 79 deletions(-) diff --git a/examples/raygui_extensions/main.lua b/examples/raygui_extensions/main.lua index 4cd537d..50b3103 100644 --- a/examples/raygui_extensions/main.lua +++ b/examples/raygui_extensions/main.lua @@ -42,6 +42,8 @@ local function addButton( bounds, text, callback ) { 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 ) }, + { RL.BUTTON, RL.BASE_COLOR_PRESSED, RL.ColorToInt( RL.WHITE ) }, + { RL.BUTTON, RL.BASE_COLOR_NORMAL, RL.ColorToInt( RL.WHITE ) }, }, }, text @@ -324,7 +326,7 @@ function RL.init() end function RL.update( delta ) - Gui:update() + Gui:update( delta ) end function RL.draw() diff --git a/examples/raygui_extensions/property_list.lua b/examples/raygui_extensions/property_list.lua index c65e598..d4c5126 100644 --- a/examples/raygui_extensions/property_list.lua +++ b/examples/raygui_extensions/property_list.lua @@ -2,18 +2,18 @@ local PropertyList = {} PropertyList.__index = PropertyList function PropertyList:new( bounds, text, callbacks, styles, tooltip ) - local object = setmetatable( {}, self ) + local object = setmetatable( {}, self ) object._gui = nil object.padding = 4 -- Content edges. object.spacing = 4 -- Between controls. object.contentPadding = Vector2:new( 0, 0 ) -- Extra padding for content rect. - object.bounds = bounds:clone() - object.text = text - object.scroll = Vector2:new( 0, 0 ) + object.bounds = bounds:clone() + object.text = text + object.scroll = Vector2:new( 0, 0 ) object.view = Rectangle:new( 0, 0, 0, 0 ) - object.callbacks = callbacks -- scroll, grab, drag. + object.callbacks = callbacks -- scroll, grab, drag. object.styles = styles object.tooltip = tooltip @@ -31,7 +31,6 @@ function PropertyList:new( bounds, text, callbacks, styles, tooltip ) object:setSize( Vector2:new( object.bounds.width, object.bounds.height ) ) - object._forceCheckScroll = false object._posY = 0 -- In control list update. object:updateMouseOffset() @@ -103,7 +102,7 @@ function PropertyList:updateContent() self.content.width + self.padding + self.contentPadding.y, self.content.height + self.padding + self.contentPadding.x ) - self._forceCheckScroll = true + self:updateMouseOffset() end -- Leave control bounds size to 0 to use default. Optional group for parameter 2. @@ -162,29 +161,25 @@ function PropertyList:addGroup( name, active, group ) return control end -function PropertyList:update() +function PropertyList:update( delta ) if not self.view:checkCollisionPoint( RL.GetMousePosition() ) then self.gui.locked = true else self.gui.locked = false end - self.gui:update() + self.gui:update( delta ) - return self._gui:drag( self ) + return self._gui:drag( self ) end function PropertyList:draw() - local result, scroll, view = RL.GuiScrollPanel( self.bounds, self.text, self.content, self.scroll, self.view ) + local oldScroll = Vector2:tempV( self.scroll ) + local _, scroll, view = RL.GuiScrollPanel( self.bounds, self.text, self.content, self.scroll, self.view ) self.view:setT( view ) self.scroll:setT( scroll ) - if 0 < result or self._forceCheckScroll then - if not self._forceCheckScroll then - self._gui:checkScrolling() - end - self._forceCheckScroll = false - + if self.scroll ~= oldScroll then self:updateMouseOffset() self.gui.view:set( -self.scroll.x, -self.scroll.y, self.view.width, self.view.height ) @@ -238,8 +233,8 @@ function PropertyList:setSize( size ) self.defaultControlSize = Vector2:new( self.content.width, self.defaultControlHeight ) local _, _, view = RL.GuiScrollPanel( self.bounds, self.text, self.content, self.scroll, self.view ) - self.view = Rectangle:newT( view ) + self.gui.view = Rectangle:new( 0, 0, self.view.width, self.view.height ) self:updateContent() diff --git a/examples/raygui_extensions/sprite_button.lua b/examples/raygui_extensions/sprite_button.lua index 8a3027b..201e617 100644 --- a/examples/raygui_extensions/sprite_button.lua +++ b/examples/raygui_extensions/sprite_button.lua @@ -2,15 +2,15 @@ local SpriteButton = {} SpriteButton.__index = SpriteButton function SpriteButton:new( bounds, text, texture, nPatchNormal, nPatchPressed, callbacks, styles, tooltip ) - local object = setmetatable( {}, self ) + local object = setmetatable( {}, self ) object._gui = nil - object.bounds = bounds:clone() - object.text = text + object.bounds = bounds:clone() + object.text = text object.buttonTexture = texture object.nPatchNormal = nPatchNormal object.nPatchPressed = nPatchPressed - object.callbacks = callbacks -- pressed. + object.callbacks = callbacks -- pressed. object.styles = styles object.tooltip = tooltip @@ -21,21 +21,28 @@ function SpriteButton:new( bounds, text, texture, nPatchNormal, nPatchPressed, c end function SpriteButton:update() - return RL.CheckCollisionPointRec( RL.GetMousePosition(), self.bounds ) + return RL.CheckCollisionPointRec( RL.GetMousePosition(), self.bounds ) end function SpriteButton:draw() - if RL.IsMouseButtonDown( RL.MOUSE_BUTTON_LEFT ) and self:update() and not RL.GuiIsLocked() and not self._gui.scrolling then - RL.DrawTextureNPatchRepeat( self.buttonTexture, self.nPatchPressed, self.bounds, { 0, 0 }, 0.0, RL.WHITE ) + if RL.IsMouseButtonDown( RL.MOUSE_BUTTON_LEFT ) and self:update() and not RL.GuiIsLocked() + and not RL.GuiGetSliderDragging() then + RL.DrawTextureNPatchRepeat( + self.buttonTexture, self.nPatchPressed, self.bounds, + { 0, 0 }, 0.0, RL.GetColor( RL.GuiGetStyle( RL.BUTTON, RL.BASE_COLOR_PRESSED ) ) + ) else - RL.DrawTextureNPatchRepeat( self.buttonTexture, self.nPatchNormal, self.bounds, { 0, 0 }, 0.0, RL.WHITE ) + RL.DrawTextureNPatchRepeat( + self.buttonTexture, self.nPatchNormal, self.bounds, + { 0, 0 }, 0.0, RL.GetColor( RL.GuiGetStyle( RL.BUTTON, RL.BASE_COLOR_NORMAL ) ) + ) end local result = RL.GuiLabelButton( self.bounds, self.text ) - if result == 1 and self.callbacks.pressed ~= nil and self._gui:clickedInBounds( self.bounds ) then + if result == 1 and self.callbacks.pressed ~= nil and self._gui:clickedInBounds( self.bounds ) then self.callbacks.pressed( self ) - end + end end function SpriteButton:setPosition( pos ) diff --git a/examples/raygui_extensions/tree_view.lua b/examples/raygui_extensions/tree_view.lua index 3c5cebe..3156886 100644 --- a/examples/raygui_extensions/tree_view.lua +++ b/examples/raygui_extensions/tree_view.lua @@ -46,7 +46,6 @@ function TreeView:new( bounds, text, callbacks, styles, tooltip ) object:setSize( Vector2:new( object.bounds.width, object.bounds.height ) ) - object._forceCheckScroll = false object._posY = 0 -- In control list update. object._curDepth = 0 -- Current indentation. object._curId = 0 -- Running number to give id's for controls. @@ -125,7 +124,7 @@ function TreeView:updateContent() self.content.width + self.padding, self.content.height + self.padding ) - self._forceCheckScroll = true + self:updateMouseOffset() end function TreeView:addItem( name, group ) @@ -289,12 +288,7 @@ function TreeView:draw() self.view:setT( view ) self.scroll:setT( scroll ) - if 0 < result or self._forceCheckScroll then - if not self._forceCheckScroll then - self._gui:checkScrolling() - end - self._forceCheckScroll = false - + if 0 < result then self:updateMouseOffset() self.gui.view:set( -self.scroll.x, -self.scroll.y, self.view.width, self.view.height ) end diff --git a/examples/resources/lib/raygui.lua b/examples/resources/lib/raygui.lua index 984b1ee..066bd37 100644 --- a/examples/resources/lib/raygui.lua +++ b/examples/resources/lib/raygui.lua @@ -282,8 +282,6 @@ function ScrollPanel:draw() self.scroll:setT( scroll ) if 0 < result then - self._gui:checkScrolling() - if self.callbacks.scroll then self.callbacks.scroll( self ) end @@ -958,7 +956,6 @@ function Slider:draw() if 0 < result then if self._gui:clickedInBounds( self.bounds ) then - self._gui:checkScrolling() self.value = value if self.callbacks.edit then @@ -1019,7 +1016,6 @@ function SliderBar:draw() if 0 < result then if self._gui:clickedInBounds( self.bounds ) then - self._gui:checkScrolling() self.value = value if self.callbacks.edit then @@ -1248,9 +1244,6 @@ function ListView:draw() result, self.scrollIndex, self.active = RL.GuiListView( self.bounds, self.text, self.scrollIndex, self.active ) - if self.scrollIndex ~= oldScrollIndex then - self._gui:checkScrolling() - end if 0 < result and self.callbacks.select then self.callbacks.select( self ) end @@ -1299,9 +1292,6 @@ function ListViewEx:draw() result, self.scrollIndex, self.active, self.focus = RL.GuiListViewEx( self.bounds, self.text, self.scrollIndex, self.active, self.focus ) - if self.scrollIndex ~= oldScrollIndex then - self._gui:checkScrolling() - end if 0 < result and self.callbacks.select then self.callbacks.select( self ) end @@ -1457,7 +1447,6 @@ function ColorPicker:draw() if self._gui:clickedInBounds( self.focusBounds ) then self.color = Color:newT( color ) end - self._gui:checkScrolling() if self.callbacks.edit then self.callbacks.edit( self ) @@ -1500,7 +1489,7 @@ end function ColorPanel:draw() local result, color = RL.GuiColorPanel( self.bounds, self.text, self.color ) - + if 0 < result then if self._gui:clickedInBounds( self.bounds ) then self.color:setT( color ) @@ -1549,7 +1538,6 @@ function ColorBarAlpha:draw() if 0 < result then if self._gui:clickedInBounds( self.bounds ) then - self._gui:checkScrolling() self.alpha = alpha if self.callbacks.edit then @@ -1596,7 +1584,6 @@ function ColorBarHue:draw() if 0 < result then if self._gui:clickedInBounds( self.bounds ) then - self._gui:checkScrolling() self.value = value if self.callbacks.edit then @@ -1645,7 +1632,6 @@ function GuiScrollBar:draw() if self.value ~= value then if self._gui:clickedInBounds( self.bounds ) then - self._gui:checkScrolling() self.value = value if self.callbacks.scroll then @@ -1676,12 +1662,14 @@ function Raygui:new() object.focused = 0 object.dragging = nil object.grabPos = Vector2:new( 0, 0 ) - object.scrolling = false object.textEdit = false object.textEditControl = nil object.defaultTexture = RL.GetTextureDefault() object.defaultRect = Rectangle:new( 0, 0, 1, 1 ) -- For texture. - object.defaultFont = RL.GuiGetFont() + object.defaultFont = { + font = RL.GuiGetFont(), + size = RL.GetFontBaseSize( RL.GuiGetFont() ), + } object.mouseOffset = Vector2:new( 0, 0 ) object.mouseScale = Vector2:new( 1, 1 ) object.view = Rectangle:new( 0, 0, 0, 0 ) -- Active if larger than 0. Then only controls in view will be updated and drawn. @@ -1692,7 +1680,6 @@ function Raygui:new() timer = 0.0, focused = 0 } - object._lastProperties = {} object._mousePressPos = Vector2:new( -1, -1 ) -- Use to check if release and check are inside bounds. @@ -1704,8 +1691,9 @@ function Raygui:inView( control ) return self.view.width == 0 or self.view.height == 0 or self.view:checkCollisionRec( control.viewBounds or control.focusBounds or control.bounds ) end -function Raygui:update() +function Raygui:update( delta ) if self.disabled or self.locked then + self.focused = 0 return end -- If dragging, don't update control masking. @@ -1730,7 +1718,7 @@ function Raygui:update() local control = self.controls[i] if control.visible and not control.noUpdate and control.update ~= nil and self:inView( control ) then - if control:update() then + if control:update( delta ) then self.focused = i if i ~= self.tooltip.focused then @@ -1742,7 +1730,7 @@ function Raygui:update() self.tooltip.focused = i if self.tooltip.timer < self.tooltip.delay then - self.tooltip.timer = self.tooltip.timer + RL.GetFrameTime() + self.tooltip.timer = self.tooltip.timer + delta else self.tooltip.text = control.tooltip self.tooltip.position = Vector2:newT( RL.GetMousePosition() ) + self.tooltip.offset @@ -1806,12 +1794,17 @@ end function Raygui:drawTooltip() local textSize = Vector2:tempT( RL.MeasureTextEx( - self.defaultFont, + self.defaultFont.font, self.tooltip.text, RL.GuiGetStyle( RL.DEFAULT, RL.TEXT_SIZE ), RL.GuiGetStyle( RL.DEFAULT, RL.TEXT_SPACING ) ) ) - local tooltipRect = Rectangle:new( self.tooltip.position.x, self.tooltip.position.y, textSize.x, textSize.y ) + local tooltipRect = Rectangle:new( + RL.Round( self.tooltip.position.x ), + RL.Round( self.tooltip.position.y ), + textSize.x, + textSize.y + ) local view = self.view:clone() -- If no view size, clamp to window size. if view.width == 0 or view.height == 0 then @@ -1829,13 +1822,12 @@ function Raygui:draw() end if self.disabled then RL.GuiDisable() + else + RL.GuiEnable() end - if not self.locked and not self.disabled then - if not self.scrolling and not self.textEdit then - RL.GuiLock() - elseif RL.IsMouseButtonReleased( RL.MOUSE_BUTTON_LEFT ) then - self.scrolling = false - end + -- Drawing is done from back to front so we want to lock the ui on begin. + if not self.textEdit then + RL.GuiLock() end local oldTextEditText = "" -- For checking if text has changed so we can call input callback. @@ -1850,15 +1842,21 @@ function Raygui:draw() RL.SetMouseOffset( self.mouseOffset ) RL.SetMouseScale( self.mouseScale ) + if self.defaultFont.font ~= RL.GuiGetFont() then + RL.GuiSetFont( self.defaultFont.font ) + RL.GuiSetStyle( RL.DEFAULT, RL.TEXT_SIZE, self.defaultFont.size ) + end + for i, control in ipairs( self.controls ) do - if not self.locked and not self.disabled and i == self.focused then + if not self.locked and not self.disabled + and i == self.focused and not control.locked then RL.GuiUnlock() end if control.visible and control.draw ~= nil and self:inView( control ) then if control.disabled then RL.GuiDisable() - else + elseif not self.disabled then RL.GuiEnable() end @@ -1866,27 +1864,20 @@ function Raygui:draw() end end - if self.tooltip.text ~= nil and self.controls[ self.tooltip.focused ]:update() + if not self.locked and not self.disabled and self.tooltip.text ~= nil + and self.controls[ self.tooltip.focused ]:update() and self.tooltip.delay <= self.tooltip.timer then self:drawTooltip() end + if self.textEdit and oldTextEditText ~= self.textEditControl.text and self.textEditControl.callbacks.textEdit ~= nil then self.textEditControl.callbacks.textEdit( self.textEditControl ) end - RL.GuiUnlock() - RL.GuiEnable() RL.SetMouseOffset( mouseOffset ) RL.SetMouseScale( mouseScale ) end -function Raygui:checkScrolling() - -- Don't set if scrolling with mouse wheel. - if RL.GetMouseWheelMove() == 0.0 then - self.scrolling = true - end -end - function Raygui:clickedInBounds( bounds ) return RL.CheckCollisionPointRec( self._mousePressPos, bounds ) end @@ -1921,6 +1912,25 @@ function Raygui:remove( control ) end end +function Raygui:getId( control ) + for i, curControl in ipairs( self.controls ) do + if control == curControl then + return i + end + end +end + +function Raygui:setDefaultFont( font ) + self.defaultFont.font = font + self.defaultFont.size = RL.GetFontBaseSize( font ) +end + +function Raygui:setProperties( properties ) + for _, property in ipairs( properties ) do + RL.GuiSetStyle( property[1], property[2], property[3] ) + end +end + function Raygui:clear() for _, control in ipairs( self.controls ) do table.remove( control ) @@ -1978,7 +1988,7 @@ function Raygui:drawControl( control ) elseif name == "texture" then RL.SetShapesTexture( self.defaultTexture, self.defaultRect ) elseif name == "font" then - RL.GuiSetFont( self.defaultFont ) + RL.GuiSetFont( self.defaultFont.font ) end end end -- cgit v1.2.3