Working basic elements.

This commit is contained in:
jussi
2022-12-05 16:56:41 +02:00
parent bdbd475ae3
commit b3d2a9c0a2
5 changed files with 202 additions and 194 deletions

View File

@@ -53,14 +53,14 @@ function Color:new( r, g, b, a )
a = 255 a = 255
end end
local o = { local object = setmetatable( {}, Color.meta )
r = r,
g = g, object.r = r
b = b, object.g = g
a = a, object.b = b
} object.a = a
setmetatable( o, Color.meta )
return o return object
end end
function Color:set( r, g, b, a ) function Color:set( r, g, b, a )

View File

@@ -30,6 +30,7 @@ Gui = {
RECTANGLE_ROUNDED_LINES = 8, RECTANGLE_ROUNDED_LINES = 8,
}, },
mouseButton = MOUSE_BUTTON_LEFT,
font = 0, font = 0,
fontSize = 30, fontSize = 30,
padding = 2, padding = 2,
@@ -37,7 +38,7 @@ Gui = {
scrollbarWidth = 8, scrollbarWidth = 8,
scrollAmount = 10, scrollAmount = 10,
_elements = {}, _cells = {},
_mousePos = Vec2:new( 0, 0 ), -- Last mouse position that was passed to Gui.process. _mousePos = Vec2:new( 0, 0 ), -- Last mouse position that was passed to Gui.process.
heldCallback = nil, heldCallback = nil,
} }
@@ -55,25 +56,8 @@ function Gui.process( mousePosition )
Gui._mousePos = mousePosition Gui._mousePos = mousePosition
for _, element in ipairs( Gui._elements ) do
if element.notMouseOver ~= nil then
element:notMouseOver()
end
-- Mousewheel scrolling. Note this would be detected through other elements.
if mouseWheel ~= 0 and element.scrollable and RL_CheckCollisionPointRec( mousePosition, element.bounds ) then
local pos = Vec2:new( element._scrollRect.x, element._scrollRect.y )
local scrollVec = Vec2:new( 0, element.scrollAmount * mouseWheel )
if RL_IsKeyDown( KEY_LEFT_SHIFT ) then
scrollVec = Vec2:new( element.scrollAmount * mouseWheel, 0 )
end
element:scroll( pos - scrollVec )
end
end
if Gui.heldCallback ~= nil then if Gui.heldCallback ~= nil then
if RL_IsMouseButtonDown( MOUSE_BUTTON_LEFT ) then if RL_IsMouseButtonDown( Gui.mouseButton ) then
Gui.heldCallback() Gui.heldCallback()
else else
Gui.heldCallback = nil Gui.heldCallback = nil
@@ -81,25 +65,49 @@ function Gui.process( mousePosition )
return return
end end
-- Go backwards on process check so we trigger the top most ui first and stop there. -- Go backwards on process check so we trigger the top most ui first and stop there.
for i = #Gui._elements, 1, -1 do local foundFirst = false
local element = Gui._elements[i]
if element.isMouseOver ~= nil and element:isMouseOver( mousePosition ) and not element.disabled then for i = #Gui._cells, 1, -1 do
if RL_IsMouseButtonPressed( MOUSE_BUTTON_LEFT ) and element.onClicked ~= nil then local cell = Gui._cells[i]
element:onClicked()
if not foundFirst and cell.isMouseOver ~= nil and cell:isMouseOver( mousePosition ) and not cell.disabled then
-- On clicked.
if RL_IsMouseButtonPressed( Gui.mouseButton ) and cell.onClicked ~= nil then
cell:onClicked()
end end
if RL_IsMouseButtonDown( MOUSE_BUTTON_LEFT ) and element.onHeld ~= nil then -- On held.
element:onHeld() if RL_IsMouseButtonDown( Gui.mouseButton ) and cell.onHeld ~= nil then
cell:onHeld()
end
-- Mouse wheel scrolling.
if mouseWheel ~= 0 then
if cell._parent ~= nil and cell._parent.scrollable then
cell = cell._parent
end end
break if cell.scrollable then
local pos = Vec2:new( cell._scrollRect.x, cell._scrollRect.y )
local scrollVec = Vec2:new( 0, cell.scrollAmount * mouseWheel )
if RL_IsKeyDown( KEY_LEFT_SHIFT ) then
scrollVec = Vec2:new( cell.scrollAmount * mouseWheel, 0 )
end
cell:scroll( pos - scrollVec )
end
end
foundFirst = true
elseif cell.notMouseOver ~= nil then
cell:notMouseOver()
end end
end end
end end
function Gui.draw() function Gui.draw()
for _, element in ipairs( Gui._elements ) do for _, element in ipairs( Gui._cells ) do
if element.draw ~= nil and element.visible then if element.draw ~= nil and element.visible then
element:draw() element:draw()
end end
@@ -114,25 +122,25 @@ Text = {}
Text.__index = Text Text.__index = Text
function Text:new( set ) function Text:new( set )
local o = { local object = setmetatable( {}, Text )
bounds = Rect:new( 0, 0, 0, 0 ),
HAling = setProperty( set, "HAling", Gui.ALING.LEFT ),
VAling = setProperty( set, "VAling", Gui.ALING.BOTTOM ),
font = setProperty( set, "font", Gui.font ), object.bounds = Rect:new( 0, 0, 0, 0 )
text = setProperty( set, "text", "" ), object.HAling = setProperty( set, "HAling", Gui.ALING.LEFT )
fontSize = setProperty( set, "fontSize", Gui.fontSize ), object.VAling = setProperty( set, "VAling", Gui.ALING.BOTTOM )
spacing = setProperty( set, "spacing", Gui.spacing ),
color = setProperty( set, "color", Color:new( BLACK ) ),
maxTextLen = setProperty( set, "maxTextLen", nil ),
visible = setProperty( set, "visible", true ), object.font = setProperty( set, "font", Gui.font )
_parent = nil, object.text = setProperty( set, "text", "" )
} object.fontSize = setProperty( set, "fontSize", Gui.fontSize )
setmetatable( o, self ) object.spacing = setProperty( set, "spacing", Gui.spacing )
o:set( o.text ) -- To measure bounds. object.color = setProperty( set, "color", Color:new( BLACK ) )
object.maxTextLen = setProperty( set, "maxTextLen", nil )
return o object.visible = setProperty( set, "visible", true )
object._parent = nil
object:set( object.text )
return object
end end
function Text:set( text ) function Text:set( text )
@@ -153,7 +161,7 @@ function Text:draw()
return return
end end
RL_DrawText( self.font, self.text, { self.parent.bounds.x + self.bounds.x, self.parent.bounds.y + self.bounds.y }, self.fontSize, self.spacing, self.color ) RL_DrawText( self.font, self.text, { self._prante.bounds.x + self.bounds.x, self._prante.bounds.y + self.bounds.y }, self.fontSize, self.spacing, self.color )
end end
-- Texture. -- Texture.
@@ -162,25 +170,24 @@ Texture = {}
Texture.__index = Texture Texture.__index = Texture
function Texture:new( set ) function Texture:new( set )
local o = { local object = setmetatable( {}, Texture )
bounds = setProperty( set, "bounds", Rect:new( 0, 0, 0, 0 ) ),
HAling = setProperty( set, "HAling", Gui.ALING.LEFT ),
VAling = setProperty( set, "VAling", Gui.ALING.CENTER ),
texture = setProperty( set, "texture", nil ), object.bounds = setProperty( set, "bounds", Rect:new( 0, 0, 0, 0 ) )
source = setProperty( set, "source", Rect:new( 0, 0, 0, 0 ) ), object.HAling = setProperty( set, "HAling", Gui.ALING.LEFT )
origin = setProperty( set, "origin", Vec2:new( 0, 0 ) ), object.VAling = setProperty( set, "VAling", Gui.ALING.CENTER )
rotation = setProperty( set, "rotation", 0 ),
color = setProperty( set, "color", Color:new( WHITE ) ),
visible = setProperty( set, "visible", true ), object.texture = setProperty( set, "texture", nil )
_parent = nil, object.source = setProperty( set, "source", Rect:new( 0, 0, 0, 0 ) )
} object.origin = setProperty( set, "origin", Vec2:new( 0, 0 ) )
object.rotation = setProperty( set, "rotation", 0 )
object.color = setProperty( set, "color", Color:new( WHITE ) )
setmetatable( o, self ) object.visible = setProperty( set, "visible", true )
o:set( o.texture ) -- To measure bounds. object._parent = nil
return o object:set( object.texture ) -- To measure bounds.
return object
end end
function Texture:set( texture ) function Texture:set( texture )
@@ -208,8 +215,8 @@ function Texture:draw()
end end
local dst = { local dst = {
self.bounds.x + self.parent.bounds.x, self.bounds.x + self._prante.bounds.x,
self.bounds.y + self.parent.bounds.y, self.bounds.y + self._prante.bounds.y,
self.bounds.width, self.bounds.width,
self.bounds.height self.bounds.height
} }
@@ -223,35 +230,35 @@ Shape = {}
Shape.__index = Shape Shape.__index = Shape
function Shape:new( set ) function Shape:new( set )
local o = { local object = setmetatable( {}, Shape )
bounds = setProperty( set, "bounds", Rect:new( 0, 0, 0, 0 ) ),
HAling = setProperty( set, "HAling", Gui.ALING.LEFT ),
VAling = setProperty( set, "VAling", Gui.ALING.CENTER ),
shape = setProperty( set, "shape", Gui.SHAPE.RECTANGLE ), object.bounds = setProperty( set, "bounds", Rect:new( 0, 0, 0, 0 ) )
object.HAling = setProperty( set, "HAling", Gui.ALING.LEFT )
object.VAling = setProperty( set, "VAling", Gui.ALING.CENTER )
object.shape = setProperty( set, "shape", Gui.SHAPE.RECTANGLE )
-- Line. -- Line.
startPos = setProperty( set, "startPos", Vec2:new( 0, 0 ) ), object.startPos = setProperty( set, "startPos", Vec2:new( 0, 0 ) )
endPos = setProperty( set, "endPos", Vec2:new( 0, 0 ) ), object.endPos = setProperty( set, "endPos", Vec2:new( 0, 0 ) )
thickness = setProperty( set, "thickness", 3.0 ), object.thickness = setProperty( set, "thickness", 3.0 )
-- Circle. -- Circle.
center = setProperty( set, "center", Vec2:new( 0, 0 ) ), object.center = setProperty( set, "center", Vec2:new( 0, 0 ) )
radius = setProperty( set, "radius", 0 ), object.radius = setProperty( set, "radius", 0 )
-- Ellipse. -- Ellipse.
radiusH = setProperty( set, "radiusH", 0 ), object.radiusH = setProperty( set, "radiusH", 0 )
radiusV = setProperty( set, "radiusV", 0 ), object.radiusV = setProperty( set, "radiusV", 0 )
-- Rectangle rounded. -- Rectangle rounded.
roundness = setProperty( set, "roundness", 1 ), object.roundness = setProperty( set, "roundness", 1 )
segments = setProperty( set, "segments", 4 ), object.segments = setProperty( set, "segments", 4 )
color = setProperty( set, "color", Color:new( WHITE ) ), object.color = setProperty( set, "color", Color:new( WHITE ) )
visible = setProperty( set, "visible", true ), object.visible = setProperty( set, "visible", true )
_parent = nil, object._parent = nil
}
setmetatable( o, self )
o:set( o.shape )
return o object:set( object.shape )
return object
end end
-- Set to default shape values. -- Set to default shape values.
@@ -274,7 +281,7 @@ function Shape:draw()
return return
end end
local pos = Vec2:new( self.parent.bounds.x, self.parent.bounds.y ) local pos = Vec2:new( self._prante.bounds.x, self._prante.bounds.y )
if self.shape == Gui.SHAPE.LINE then if self.shape == Gui.SHAPE.LINE then
RL_DrawLine( self.startPos + pos, self.endPos + pos, self.thickness, self.color ) RL_DrawLine( self.startPos + pos, self.endPos + pos, self.thickness, self.color )
@@ -305,28 +312,29 @@ Element = {}
Element.__index = Element Element.__index = Element
function Element:new( set ) function Element:new( set )
local o = { local object = setmetatable( {}, Element )
_ID = #Gui._elements + 1,
bounds = setProperty( set, "bounds", Rect:new( 0, 0, 0, 0 ) ),
padding = setProperty( set, "padding", Gui.padding ),
visible = setProperty( set, "visible", true ),
disabled = setProperty( set, "disabled", false ),
drawBounds = setProperty( set, "drawBounds", false ),
color = setProperty( set, "color", Color:new( GRAY ) ),
items = {}, object._ID = #Gui._cells + 1
object.bounds = setProperty( set, "bounds", Rect:new( 0, 0, 0, 0 ) )
object.padding = setProperty( set, "padding", Gui.padding )
object.visible = setProperty( set, "visible", true )
object.disabled = setProperty( set, "disabled", false )
object.drawBounds = setProperty( set, "drawBounds", false )
object.color = setProperty( set, "color", Color:new( GRAY ) )
_visibilityBounds = nil, object.items = {}
object._visibilityBounds = nil
-- Callbacks. -- Callbacks.
onMouseOver = setProperty( set, "onMouseOver", nil ), object.onMouseOver = setProperty( set, "onMouseOver", nil )
notMouseOver = setProperty( set, "notMouseOver", nil ), object.notMouseOver = setProperty( set, "notMouseOver", nil )
onClicked = setProperty( set, "onClicked", nil ), object.onClicked = setProperty( set, "onClicked", nil )
onHeld = setProperty( set, "onHeld", nil ), object.onHeld = setProperty( set, "onHeld", nil )
} object._parent = nil
setmetatable( o, self )
table.insert( Gui._elements, o )
return o table.insert( Gui._cells, object )
return object
end end
function Element:update() function Element:update()
@@ -358,7 +366,7 @@ end
function Element:add( item ) function Element:add( item )
table.insert( self.items, item ) table.insert( self.items, item )
item.parent = self item._prante = self
self:update() self:update()
end end
@@ -407,15 +415,15 @@ function Element:draw()
end end
function Element:delete() function Element:delete()
table.remove( Gui._elements, self._ID ) table.remove( Gui._cells, self._ID )
end end
function Element:set2Top() function Element:set2Top()
util.tableMove( Gui._elements, self._ID, 1, #Gui._elements ) util.tableMove( Gui._cells, self._ID, 1, #Gui._cells )
end end
function Element:set2Back() function Element:set2Back()
util.tableMove( Gui._elements, self._ID, 1, 1 ) util.tableMove( Gui._cells, self._ID, 1, 1 )
end end
-- Container. -- Container.
@@ -424,37 +432,36 @@ Container = {}
Container.__index = Container Container.__index = Container
function Container:new( set ) function Container:new( set )
local o = { local object = setmetatable( {}, Container )
_ID = #Gui._elements + 1,
bounds = setProperty( set, "bounds", Rect:new( 0, 0, 0, 0 ) ),
spacing = setProperty( set, "spacing", Gui.spacing ),
type = setProperty( set, "type", Gui.CONTAINER.VERTICAL ),
HAling = setProperty( set, "HAling", Gui.ALING.LEFT ),
VAling = setProperty( set, "VAling", Gui.ALING.TOP ),
visible = setProperty( set, "visible", true ), object._ID = #Gui._cells + 1
disabled = setProperty( set, "disabled", false ), object.bounds = setProperty( set, "bounds", Rect:new( 0, 0, 0, 0 ) )
-- drawBounds = setProperty( set, "drawBounds", false ), object.spacing = setProperty( set, "spacing", Gui.spacing )
-- color = setProperty( set, "color", Color:new( DARKGRAY ) ), object.type = setProperty( set, "type", Gui.CONTAINER.VERTICAL )
scrollable = setProperty( set, "scrollable", false ), object.HAling = setProperty( set, "HAling", Gui.ALING.LEFT )
showScrollbar = setProperty( set, "showScrollbar", false ), object.VAling = setProperty( set, "VAling", Gui.ALING.TOP )
scrollbarWidth = setProperty( set, "scrollbarWidth", Gui.scrollbarWidth ),
scrollAmount = setProperty( set, "scrollAmount", Gui.scrollAmount ), -- When using mouse scroll.
cells = {}, object.visible = setProperty( set, "visible", true )
object.disabled = setProperty( set, "disabled", false )
object.scrollable = setProperty( set, "scrollable", false )
object.showScrollbar = setProperty( set, "showScrollbar", false )
object.scrollbarWidth = setProperty( set, "scrollbarWidth", Gui.scrollbarWidth )
object.scrollAmount = setProperty( set, "scrollAmount", Gui.scrollAmount ) -- When using mouse scroll.
_visibilityBounds = nil, -- Will give this to it's children. object.cells = {}
_scrollRect = Rect:new( 0, 0, 0, 0 ),
_VScrollbarRect = Rect:new( 0, 0, 0, 0 ),
_HScrollbarRect = Rect:new( 0, 0, 0, 0 ),
_VScrollbar = nil, object._visibilityBounds = nil -- Will give this to it's children.
_HScrollbar = nil, object._scrollRect = Rect:new( 0, 0, 0, 0 )
} object._VScrollbarRect = Rect:new( 0, 0, 0, 0 )
setmetatable( o, self ) object._HScrollbarRect = Rect:new( 0, 0, 0, 0 )
table.insert( Gui._elements, o )
return o object._VScrollbar = nil
object._HScrollbar = nil
object._parent = nil
table.insert( Gui._cells, object )
return object
end end
function Container:setPosition( pos ) function Container:setPosition( pos )
@@ -469,6 +476,19 @@ function Container:add( cell )
self:update() self:update()
-- Highest container becomes the parent.
if self._parent ~= nil then
cell._parent = self._parent
else
cell._parent = self
end
if cell.cells ~= nil then
for _, sCell in ipairs( cell.cells ) do
sCell._parent = cell._parent
end
end
if cell.update ~= nil then if cell.update ~= nil then
cell:update() cell:update()
end end
@@ -501,21 +521,21 @@ function Container:mouseScroll( v )
self:scroll( mousePos ) self:scroll( mousePos )
end end
-- function Container:isMouseOver( mousePosition )
-- -- print( "Over container" )
-- local over = RL_CheckCollisionPointRec( mousePosition, self.bounds )
-- if over and self._visibilityBounds ~= nil then -- //TODO Add this
-- over = RL_CheckCollisionPointRec( mousePosition, self._visibilityBounds ) function Container:isMouseOver( mousePosition )
-- print( "Over container" ) local over = RL_CheckCollisionPointRec( mousePosition, self.bounds )
-- end
-- -- if over and self.onMouseOver ~= nil then if over and self._visibilityBounds ~= nil then
-- -- self:onMouseOver() over = RL_CheckCollisionPointRec( mousePosition, self._visibilityBounds )
-- -- end end
-- -- return over if over and self.onMouseOver ~= nil then
-- end self:onMouseOver()
end
return over
end
function Container:updateScrollbar() function Container:updateScrollbar()
if self.bounds.height < self._scrollRect.height then if self.bounds.height < self._scrollRect.height then
@@ -631,20 +651,8 @@ function Container:update()
end end
end end
function Container:draw()
-- if self.drawBounds then
-- RL_DrawRectangle( self.bounds, self.color )
-- RL_DrawRectangleLines( {
-- self.bounds.x - self._scrollRect.x,
-- self.bounds.y - self._scrollRect.y,
-- self._scrollRect.width,
-- self._scrollRect.height,
-- }, RED )
-- end
end
function Container:delete() function Container:delete()
table.remove( Gui._elements, self._ID ) table.remove( Gui._cells, self._ID )
end end
--Assingments. --Assingments.

View File

@@ -49,14 +49,14 @@ function Rectangle:new( x, y, width, height )
x, y, width, height = 0, 0, 0, 0 x, y, width, height = 0, 0, 0, 0
end end
local o = { local object = setmetatable( {}, Rectangle.meta )
x = x,
y = y, object.x = x
width = width, object.y = y
height = height, object.width = width
} object.height = height
setmetatable( o, Rectangle.meta )
return o return object
end end
function Rectangle:set( x, y, width, height ) function Rectangle:set( x, y, width, height )

View File

@@ -49,12 +49,12 @@ function Vector2:new( x, y )
x, y = 0, 0 x, y = 0, 0
end end
local o = { local object = setmetatable( {}, Vector2.meta )
x = x,
y = y, object.x = x
} object.y = y
setmetatable( o, Vector2.meta )
return o return object
end end
function Vector2:set( x, y ) function Vector2:set( x, y )

View File

@@ -47,13 +47,13 @@ function Vector3:new( x, y, z )
x, y, z = table.unpack( x ) x, y, z = table.unpack( x )
end end
local o = { local object = setmetatable( {}, Vector3.meta )
x = x,
y = y, object.x = x
z = z, object.y = y
} object.z = z
setmetatable( o, Vector3.meta )
return o return object
end end
function Vector3:set( x, y, z ) function Vector3:set( x, y, z )