Raygui update.

This commit is contained in:
jussi
2024-10-02 14:06:50 +03:00
parent 30c7a5eda2
commit 02c7dd9ca8
5 changed files with 87 additions and 79 deletions

View File

@@ -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_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_PRESSED, RL.ColorToInt( { 84/2, 59/2, 22/2 } ) },
{ RL.LABEL, RL.TEXT_COLOR_FOCUSED, RL.ColorToInt( RL.GREEN ) }, { 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 text
@@ -324,7 +326,7 @@ function RL.init()
end end
function RL.update( delta ) function RL.update( delta )
Gui:update() Gui:update( delta )
end end
function RL.draw() function RL.draw()

View File

@@ -2,18 +2,18 @@ local PropertyList = {}
PropertyList.__index = PropertyList PropertyList.__index = PropertyList
function PropertyList:new( bounds, text, callbacks, styles, tooltip ) function PropertyList:new( bounds, text, callbacks, styles, tooltip )
local object = setmetatable( {}, self ) local object = setmetatable( {}, self )
object._gui = nil object._gui = nil
object.padding = 4 -- Content edges. object.padding = 4 -- Content edges.
object.spacing = 4 -- Between controls. object.spacing = 4 -- Between controls.
object.contentPadding = Vector2:new( 0, 0 ) -- Extra padding for content rect. object.contentPadding = Vector2:new( 0, 0 ) -- Extra padding for content rect.
object.bounds = bounds:clone() object.bounds = bounds:clone()
object.text = text object.text = text
object.scroll = Vector2:new( 0, 0 ) object.scroll = Vector2:new( 0, 0 )
object.view = Rectangle:new( 0, 0, 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.styles = styles
object.tooltip = tooltip 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:setSize( Vector2:new( object.bounds.width, object.bounds.height ) )
object._forceCheckScroll = false
object._posY = 0 -- In control list update. object._posY = 0 -- In control list update.
object:updateMouseOffset() object:updateMouseOffset()
@@ -103,7 +102,7 @@ function PropertyList:updateContent()
self.content.width + self.padding + self.contentPadding.y, self.content.width + self.padding + self.contentPadding.y,
self.content.height + self.padding + self.contentPadding.x self.content.height + self.padding + self.contentPadding.x
) )
self._forceCheckScroll = true self:updateMouseOffset()
end end
-- Leave control bounds size to 0 to use default. Optional group for parameter 2. -- 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 return control
end end
function PropertyList:update() function PropertyList:update( delta )
if not self.view:checkCollisionPoint( RL.GetMousePosition() ) then if not self.view:checkCollisionPoint( RL.GetMousePosition() ) then
self.gui.locked = true self.gui.locked = true
else else
self.gui.locked = false self.gui.locked = false
end end
self.gui:update() self.gui:update( delta )
return self._gui:drag( self ) return self._gui:drag( self )
end end
function PropertyList:draw() 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.view:setT( view )
self.scroll:setT( scroll ) self.scroll:setT( scroll )
if 0 < result or self._forceCheckScroll then if self.scroll ~= oldScroll then
if not self._forceCheckScroll then
self._gui:checkScrolling()
end
self._forceCheckScroll = false
self:updateMouseOffset() self:updateMouseOffset()
self.gui.view:set( -self.scroll.x, -self.scroll.y, self.view.width, self.view.height ) 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 ) self.defaultControlSize = Vector2:new( self.content.width, self.defaultControlHeight )
local _, _, view = RL.GuiScrollPanel( self.bounds, self.text, self.content, self.scroll, self.view ) local _, _, view = RL.GuiScrollPanel( self.bounds, self.text, self.content, self.scroll, self.view )
self.view = Rectangle:newT( view ) self.view = Rectangle:newT( view )
self.gui.view = Rectangle:new( 0, 0, self.view.width, self.view.height ) self.gui.view = Rectangle:new( 0, 0, self.view.width, self.view.height )
self:updateContent() self:updateContent()

View File

@@ -2,15 +2,15 @@ local SpriteButton = {}
SpriteButton.__index = SpriteButton SpriteButton.__index = SpriteButton
function SpriteButton:new( bounds, text, texture, nPatchNormal, nPatchPressed, callbacks, styles, tooltip ) function SpriteButton:new( bounds, text, texture, nPatchNormal, nPatchPressed, callbacks, styles, tooltip )
local object = setmetatable( {}, self ) local object = setmetatable( {}, self )
object._gui = nil object._gui = nil
object.bounds = bounds:clone() object.bounds = bounds:clone()
object.text = text object.text = text
object.buttonTexture = texture object.buttonTexture = texture
object.nPatchNormal = nPatchNormal object.nPatchNormal = nPatchNormal
object.nPatchPressed = nPatchPressed object.nPatchPressed = nPatchPressed
object.callbacks = callbacks -- pressed. object.callbacks = callbacks -- pressed.
object.styles = styles object.styles = styles
object.tooltip = tooltip object.tooltip = tooltip
@@ -21,21 +21,28 @@ function SpriteButton:new( bounds, text, texture, nPatchNormal, nPatchPressed, c
end end
function SpriteButton:update() function SpriteButton:update()
return RL.CheckCollisionPointRec( RL.GetMousePosition(), self.bounds ) return RL.CheckCollisionPointRec( RL.GetMousePosition(), self.bounds )
end end
function SpriteButton:draw() function SpriteButton:draw()
if RL.IsMouseButtonDown( RL.MOUSE_BUTTON_LEFT ) and self:update() and not RL.GuiIsLocked() and not self._gui.scrolling then if RL.IsMouseButtonDown( RL.MOUSE_BUTTON_LEFT ) and self:update() and not RL.GuiIsLocked()
RL.DrawTextureNPatchRepeat( self.buttonTexture, self.nPatchPressed, self.bounds, { 0, 0 }, 0.0, RL.WHITE ) 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 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 end
local result = RL.GuiLabelButton( self.bounds, self.text ) 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 ) self.callbacks.pressed( self )
end end
end end
function SpriteButton:setPosition( pos ) function SpriteButton:setPosition( pos )

View File

@@ -46,7 +46,6 @@ function TreeView:new( bounds, text, callbacks, styles, tooltip )
object:setSize( Vector2:new( object.bounds.width, object.bounds.height ) ) object:setSize( Vector2:new( object.bounds.width, object.bounds.height ) )
object._forceCheckScroll = false
object._posY = 0 -- In control list update. object._posY = 0 -- In control list update.
object._curDepth = 0 -- Current indentation. object._curDepth = 0 -- Current indentation.
object._curId = 0 -- Running number to give id's for controls. 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.width + self.padding,
self.content.height + self.padding self.content.height + self.padding
) )
self._forceCheckScroll = true self:updateMouseOffset()
end end
function TreeView:addItem( name, group ) function TreeView:addItem( name, group )
@@ -289,12 +288,7 @@ function TreeView:draw()
self.view:setT( view ) self.view:setT( view )
self.scroll:setT( scroll ) self.scroll:setT( scroll )
if 0 < result or self._forceCheckScroll then if 0 < result then
if not self._forceCheckScroll then
self._gui:checkScrolling()
end
self._forceCheckScroll = false
self:updateMouseOffset() self:updateMouseOffset()
self.gui.view:set( -self.scroll.x, -self.scroll.y, self.view.width, self.view.height ) self.gui.view:set( -self.scroll.x, -self.scroll.y, self.view.width, self.view.height )
end end

View File

@@ -282,8 +282,6 @@ function ScrollPanel:draw()
self.scroll:setT( scroll ) self.scroll:setT( scroll )
if 0 < result then if 0 < result then
self._gui:checkScrolling()
if self.callbacks.scroll then if self.callbacks.scroll then
self.callbacks.scroll( self ) self.callbacks.scroll( self )
end end
@@ -958,7 +956,6 @@ function Slider:draw()
if 0 < result then if 0 < result then
if self._gui:clickedInBounds( self.bounds ) then if self._gui:clickedInBounds( self.bounds ) then
self._gui:checkScrolling()
self.value = value self.value = value
if self.callbacks.edit then if self.callbacks.edit then
@@ -1019,7 +1016,6 @@ function SliderBar:draw()
if 0 < result then if 0 < result then
if self._gui:clickedInBounds( self.bounds ) then if self._gui:clickedInBounds( self.bounds ) then
self._gui:checkScrolling()
self.value = value self.value = value
if self.callbacks.edit then 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 ) 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 if 0 < result and self.callbacks.select then
self.callbacks.select( self ) self.callbacks.select( self )
end 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 ) 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 if 0 < result and self.callbacks.select then
self.callbacks.select( self ) self.callbacks.select( self )
end end
@@ -1457,7 +1447,6 @@ function ColorPicker:draw()
if self._gui:clickedInBounds( self.focusBounds ) then if self._gui:clickedInBounds( self.focusBounds ) then
self.color = Color:newT( color ) self.color = Color:newT( color )
end end
self._gui:checkScrolling()
if self.callbacks.edit then if self.callbacks.edit then
self.callbacks.edit( self ) self.callbacks.edit( self )
@@ -1549,7 +1538,6 @@ function ColorBarAlpha:draw()
if 0 < result then if 0 < result then
if self._gui:clickedInBounds( self.bounds ) then if self._gui:clickedInBounds( self.bounds ) then
self._gui:checkScrolling()
self.alpha = alpha self.alpha = alpha
if self.callbacks.edit then if self.callbacks.edit then
@@ -1596,7 +1584,6 @@ function ColorBarHue:draw()
if 0 < result then if 0 < result then
if self._gui:clickedInBounds( self.bounds ) then if self._gui:clickedInBounds( self.bounds ) then
self._gui:checkScrolling()
self.value = value self.value = value
if self.callbacks.edit then if self.callbacks.edit then
@@ -1645,7 +1632,6 @@ function GuiScrollBar:draw()
if self.value ~= value then if self.value ~= value then
if self._gui:clickedInBounds( self.bounds ) then if self._gui:clickedInBounds( self.bounds ) then
self._gui:checkScrolling()
self.value = value self.value = value
if self.callbacks.scroll then if self.callbacks.scroll then
@@ -1676,12 +1662,14 @@ function Raygui:new()
object.focused = 0 object.focused = 0
object.dragging = nil object.dragging = nil
object.grabPos = Vector2:new( 0, 0 ) object.grabPos = Vector2:new( 0, 0 )
object.scrolling = false
object.textEdit = false object.textEdit = false
object.textEditControl = nil object.textEditControl = nil
object.defaultTexture = RL.GetTextureDefault() object.defaultTexture = RL.GetTextureDefault()
object.defaultRect = Rectangle:new( 0, 0, 1, 1 ) -- For texture. 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.mouseOffset = Vector2:new( 0, 0 )
object.mouseScale = Vector2:new( 1, 1 ) 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. 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, timer = 0.0,
focused = 0 focused = 0
} }
object._lastProperties = {} object._lastProperties = {}
object._mousePressPos = Vector2:new( -1, -1 ) -- Use to check if release and check are inside bounds. 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 ) return self.view.width == 0 or self.view.height == 0 or self.view:checkCollisionRec( control.viewBounds or control.focusBounds or control.bounds )
end end
function Raygui:update() function Raygui:update( delta )
if self.disabled or self.locked then if self.disabled or self.locked then
self.focused = 0
return return
end end
-- If dragging, don't update control masking. -- If dragging, don't update control masking.
@@ -1730,7 +1718,7 @@ function Raygui:update()
local control = self.controls[i] local control = self.controls[i]
if control.visible and not control.noUpdate and control.update ~= nil and self:inView( control ) then 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 self.focused = i
if i ~= self.tooltip.focused then if i ~= self.tooltip.focused then
@@ -1742,7 +1730,7 @@ function Raygui:update()
self.tooltip.focused = i self.tooltip.focused = i
if self.tooltip.timer < self.tooltip.delay then if self.tooltip.timer < self.tooltip.delay then
self.tooltip.timer = self.tooltip.timer + RL.GetFrameTime() self.tooltip.timer = self.tooltip.timer + delta
else else
self.tooltip.text = control.tooltip self.tooltip.text = control.tooltip
self.tooltip.position = Vector2:newT( RL.GetMousePosition() ) + self.tooltip.offset self.tooltip.position = Vector2:newT( RL.GetMousePosition() ) + self.tooltip.offset
@@ -1806,12 +1794,17 @@ end
function Raygui:drawTooltip() function Raygui:drawTooltip()
local textSize = Vector2:tempT( RL.MeasureTextEx( local textSize = Vector2:tempT( RL.MeasureTextEx(
self.defaultFont, self.defaultFont.font,
self.tooltip.text, self.tooltip.text,
RL.GuiGetStyle( RL.DEFAULT, RL.TEXT_SIZE ), RL.GuiGetStyle( RL.DEFAULT, RL.TEXT_SIZE ),
RL.GuiGetStyle( RL.DEFAULT, RL.TEXT_SPACING ) 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() local view = self.view:clone()
-- If no view size, clamp to window size. -- If no view size, clamp to window size.
if view.width == 0 or view.height == 0 then if view.width == 0 or view.height == 0 then
@@ -1829,13 +1822,12 @@ function Raygui:draw()
end end
if self.disabled then if self.disabled then
RL.GuiDisable() RL.GuiDisable()
else
RL.GuiEnable()
end end
if not self.locked and not self.disabled then -- Drawing is done from back to front so we want to lock the ui on begin.
if not self.scrolling and not self.textEdit then if not self.textEdit then
RL.GuiLock() RL.GuiLock()
elseif RL.IsMouseButtonReleased( RL.MOUSE_BUTTON_LEFT ) then
self.scrolling = false
end
end end
local oldTextEditText = "" -- For checking if text has changed so we can call input callback. 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.SetMouseOffset( self.mouseOffset )
RL.SetMouseScale( self.mouseScale ) 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 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() RL.GuiUnlock()
end end
if control.visible and control.draw ~= nil and self:inView( control ) then if control.visible and control.draw ~= nil and self:inView( control ) then
if control.disabled then if control.disabled then
RL.GuiDisable() RL.GuiDisable()
else elseif not self.disabled then
RL.GuiEnable() RL.GuiEnable()
end end
@@ -1866,27 +1864,20 @@ function Raygui:draw()
end end
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 and self.tooltip.delay <= self.tooltip.timer then
self:drawTooltip() self:drawTooltip()
end end
if self.textEdit and oldTextEditText ~= self.textEditControl.text and self.textEditControl.callbacks.textEdit ~= nil then if self.textEdit and oldTextEditText ~= self.textEditControl.text and self.textEditControl.callbacks.textEdit ~= nil then
self.textEditControl.callbacks.textEdit( self.textEditControl ) self.textEditControl.callbacks.textEdit( self.textEditControl )
end end
RL.GuiUnlock()
RL.GuiEnable()
RL.SetMouseOffset( mouseOffset ) RL.SetMouseOffset( mouseOffset )
RL.SetMouseScale( mouseScale ) RL.SetMouseScale( mouseScale )
end 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 ) function Raygui:clickedInBounds( bounds )
return RL.CheckCollisionPointRec( self._mousePressPos, bounds ) return RL.CheckCollisionPointRec( self._mousePressPos, bounds )
end end
@@ -1921,6 +1912,25 @@ function Raygui:remove( control )
end end
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() function Raygui:clear()
for _, control in ipairs( self.controls ) do for _, control in ipairs( self.controls ) do
table.remove( control ) table.remove( control )
@@ -1978,7 +1988,7 @@ function Raygui:drawControl( control )
elseif name == "texture" then elseif name == "texture" then
RL.SetShapesTexture( self.defaultTexture, self.defaultRect ) RL.SetShapesTexture( self.defaultTexture, self.defaultRect )
elseif name == "font" then elseif name == "font" then
RL.GuiSetFont( self.defaultFont ) RL.GuiSetFont( self.defaultFont.font )
end end
end end
end end