Raygui styles include properties, texture and font.

This commit is contained in:
jussi
2024-02-05 22:40:31 +02:00
parent d21512d88c
commit eda4eacfce
8 changed files with 97 additions and 82 deletions

View File

@@ -35,11 +35,13 @@ local function addButton( bounds, text, callback )
{ source = { 48, 0, 48, 48 }, left = 16, top = 16, right = 16, bottom = 16, layout = RL.NPATCH_NINE_PATCH },
callback,
{
{ RL.LABEL, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_CENTER },
{ RL.DEFAULT, RL.TEXT_SIZE, 32 },
{ 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 ) },
properties = {
{ RL.LABEL, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_CENTER },
{ RL.DEFAULT, RL.TEXT_SIZE, 32 },
{ 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
@@ -75,7 +77,9 @@ local function addPropertyList()
function( self ) Gui:set2Top( self ) end,
nil,
{
{ RL.SCROLLBAR, RL.ARROWS_VISIBLE, RL.ARROWS_VISIBLE },
properties = {
{ RL.SCROLLBAR, RL.ARROWS_VISIBLE, RL.ARROWS_VISIBLE },
}
}
)
RL.GuiSetStyle( RL.SPINNER, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_LEFT )
@@ -148,9 +152,6 @@ local function addPropertyList()
"Flipped",
cat.flipped,
function( self ) cat.flipped = self.checked end
-- {
-- { RL.CHECKBOX, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_RIGHT }
-- }
), transformGroup )
-- Visibility.
@@ -163,7 +164,9 @@ local function addPropertyList()
cat.visible,
function( self ) cat.visible = self.checked end,
{
{ RL.CHECKBOX, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_RIGHT }
properties = {
{ RL.CHECKBOX, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_RIGHT },
}
}
), visibilityGroup )
@@ -199,8 +202,10 @@ local function addPropertyList()
false,
function( self ) print( "Checked" ) end,
{
-- { RL.CHECKBOX, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_LEFT },
{ RL.DEFAULT, RL.TEXT_SIZE, 32 }
properties = {
-- { RL.CHECKBOX, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_LEFT },
{ RL.DEFAULT, RL.TEXT_SIZE, 32 }
}
}
), test )
end

View File

@@ -141,7 +141,9 @@ function PropertyList:addGroup( name, active, group )
active,
function( this ) this.text = setGroupText( name, this.active ) self:updateContent() end,
{
{ RL.TOGGLE, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_LEFT }
properties = {
{ RL.TOGGLE, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_LEFT }
}
}
)
control._controls = {} -- Prefix _ to try to prevent clashing with control definition.

View File

@@ -70,13 +70,14 @@ function RL.init()
"Dog",
function() toggleGroup:setText( "Dog;Cat\nEagle" ) end,
{
{ RL.DEFAULT, RL.TEXT_COLOR_NORMAL, RL.ColorToInt( RL.RED ) },
{ RL.DEFAULT, RL.TEXT_COLOR_FOCUSED, RL.ColorToInt( RL.ORANGE ) },
{ RL.DEFAULT, RL.TEXT_COLOR_PRESSED, RL.ColorToInt( RL.GREEN ) },
properties = {
{ RL.DEFAULT, RL.TEXT_COLOR_NORMAL, RL.ColorToInt( RL.RED ) },
{ RL.DEFAULT, RL.TEXT_COLOR_FOCUSED, RL.ColorToInt( RL.ORANGE ) },
{ RL.DEFAULT, RL.TEXT_COLOR_PRESSED, RL.ColorToInt( RL.GREEN ) },
},
texture = { texture = texture, rect = textureRect },
}
)
button.texture = texture
button.textureRect = textureRect
local checkbox = Gui:CheckBox(
Rect:new( 116, 128, 20, 20 ),
"Visible",
@@ -140,10 +141,11 @@ function RL.init()
0,
0,
100,
function( self ) print( "Changed value "..self.value ) end
function( self ) print( "Changed value "..self.value ) end,
{
texture = { texture = texture, rect = textureRect },
}
)
slider.texture = texture
slider.textureRect = textureRect
local sliderbar = Gui:SliderBar(
Rect:new( 50, 550, 256, 32 ),
"min",
@@ -185,11 +187,12 @@ function RL.init()
function( self ) Gui:set2Top( self ) end,
nil,
{
{ RL.DEFAULT, RL.TEXT_COLOR_NORMAL, RL.ColorToInt( RL.RED ) },
properties = {
{ RL.DEFAULT, RL.TEXT_COLOR_NORMAL, RL.ColorToInt( RL.RED ) },
},
texture = { texture = texture, rect = textureRect },
}
)
windowbox.texture = texture
windowbox.textureRect = textureRect
local groupbox = Gui:GroupBox(
Rect:new( 400, 700, 256, 256 ),
@@ -206,9 +209,11 @@ function RL.init()
function( self ) Gui:set2Top( self ) end,
nil,
{
{ RL.DEFAULT, RL.TEXT_COLOR_NORMAL, RL.ColorToInt( RL.MAGENTA ) },
{ RL.DEFAULT, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_CENTER },
{ RL.DEFAULT, RL.BACKGROUND_COLOR, RL.ColorToInt( RL.DARKBLUE ) },
properties = {
{ RL.DEFAULT, RL.TEXT_COLOR_NORMAL, RL.ColorToInt( RL.MAGENTA ) },
{ RL.DEFAULT, RL.TEXT_ALIGNMENT, RL.TEXT_ALIGN_CENTER },
{ RL.DEFAULT, RL.BACKGROUND_COLOR, RL.ColorToInt( RL.DARKBLUE ) },
}
}
)
tabBar = Gui:GuiTabBar(
@@ -243,13 +248,14 @@ function RL.init()
0,
function( self ) print( self:getItem( self.active ) ) end,
{
{ RL.DEFAULT, RL.TEXT_COLOR_NORMAL, RL.ColorToInt( RL.RED ) },
{ RL.DEFAULT, RL.TEXT_COLOR_FOCUSED, RL.ColorToInt( RL.ORANGE ) },
{ RL.DEFAULT, RL.TEXT_COLOR_PRESSED, RL.ColorToInt( RL.GREEN ) },
properties = {
{ RL.DEFAULT, RL.TEXT_COLOR_NORMAL, RL.ColorToInt( RL.RED ) },
{ RL.DEFAULT, RL.TEXT_COLOR_FOCUSED, RL.ColorToInt( RL.ORANGE ) },
{ RL.DEFAULT, RL.TEXT_COLOR_PRESSED, RL.ColorToInt( RL.GREEN ) },
},
texture = { texture = texture, rect = textureRect },
}
)
listviewex.texture = texture
listviewex.textureRect = textureRect
local messagebox = Gui:MessageBox(
Rect:new( 1100, 150, 300, 128 ),
"Title",

View File

@@ -1701,10 +1701,11 @@ function Raygui:new()
object.textEdit = false
object.defaultTexture = RL.GetTextureDefault()
object.defaultRect = Rect:new( 0, 0, 1, 1 ) -- For texture.
object.defaultFont = RL.GetFontDefault()
object.mouseOffset = Vec2:new()
object.view = Rect:new() -- Active if larger than 0. Then only controls in view will be processed and drawn.
object._lastStyles = {}
object._lastProperties = {}
object._mousePressPos = Vec2:new( -1, -1 ) -- Use to check if release and check are inside bounds.
return object
@@ -1774,17 +1775,17 @@ function Raygui:drag( control )
return mouseOver
end
-- Add style before current draw that we can then return them.
function Raygui:_addLastStyle( style )
local lastStyle = { style[1], style[2], RL.GuiGetStyle( style[1], style[2] ) }
-- Add property before current draw that we can then return them.
function Raygui:_addLastProperty( property )
local lastProperty = { property[1], property[2], RL.GuiGetStyle( property[1], property[2] ) }
for i, slot in ipairs( self._lastStyles ) do
for i, slot in ipairs( self._lastProperties ) do
if slot == 0 then
self._lastStyles[i] = lastStyle
self._lastProperties[i] = lastProperty
return
end
end
table.insert( self._lastStyles, lastStyle )
table.insert( self._lastProperties, lastProperty )
end
function Raygui:draw()
@@ -1804,43 +1805,21 @@ function Raygui:draw()
-- Set mouse ofset if gui is for example embedded to some control.
RL.SetMouseOffset( self.mouseOffset )
for i, control in ipairs( self.controls ) do
if not self.locked and not self.disabled and i == self.focused then
RL.GuiUnlock()
end
for i, control in ipairs( self.controls ) do
if not self.locked and not self.disabled and i == self.focused then
RL.GuiUnlock()
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
RL.GuiDisable()
else
RL.GuiEnable()
end
if control.styles ~= nil then
for _, style in ipairs( control.styles ) do
self:_addLastStyle( style )
RL.GuiSetStyle( style[1], style[2], style[3] )
end
end
if control.texture ~= nil then
RL.SetShapesTexture( control.texture, control.textureRect )
end
control:draw()
if control.texture ~= nil then
RL.SetShapesTexture( self.defaultTexture, self.defaultRect )
end
-- Set previous styles back.
if 0 < #self._lastStyles then
for j, style in ipairs( self._lastStyles ) do
if type( style ) == "table" then
RL.GuiSetStyle( style[1], style[2], style[3] )
end
self._lastStyles[j] = 0
end
end
end
end
self:drawControl( control )
end
end
RL.GuiUnlock()
RL.GuiEnable()
RL.SetMouseOffset( { 0, 0 } )
@@ -1902,24 +1881,42 @@ function Raygui:editMode( editMode )
self.textEdit = not editMode
end
-- Mainly to set focusBounds and viewBounds by drawing control.
function Raygui:applyStyles( control )
function Raygui:drawControl( control )
if control == nil or control.draw == nil then
return
end
local oldStyles = {}
if control.styles ~= nil then
for _, style in ipairs( control.styles ) do
local oldStyle = { style[1], style[2], RL.GuiGetStyle( style[1], style[2] ) }
table.insert( oldStyles, oldStyle )
RL.GuiSetStyle( style[1], style[2], style[3] )
for name, style in pairs( control.styles ) do
if name == "properties" then
for _, property in ipairs( style ) do
self:_addLastProperty( property )
RL.GuiSetStyle( property[1], property[2], property[3] )
end
elseif name == "texture" then
RL.SetShapesTexture( style.texture, style.rect )
elseif name == "font" then
RL.GuiSetFont( style )
end
end
end
control:draw()
if control.styles ~= nil then
for _, style in ipairs( oldStyles ) do
RL.GuiSetStyle( style[1], style[2], style[3] )
for name, _ in pairs( control.styles ) do
if name == "properties" then
for j, property in ipairs( self._lastProperties ) do
if type( property ) == "table" then
RL.GuiSetStyle( property[1], property[2], property[3] )
self._lastProperties[j] = 0
end
end
elseif name == "texture" then
RL.SetShapesTexture( self.defaultTexture, self.defaultRect )
elseif name == "font" then
RL.GuiSetFont( self.defaultFont )
end
end
end
end
@@ -1928,7 +1925,8 @@ end
function Raygui:addControl( control )
control._parent = self
self:applyStyles( control )
-- self:applyStyles( control )
self:drawControl( control )
table.insert( self.controls, control )
return control
end