diff options
Diffstat (limited to 'examples/resources/lib')
| -rw-r--r-- | examples/resources/lib/color.lua | 11 | ||||
| -rw-r--r-- | examples/resources/lib/gui.lua | 2 | ||||
| -rw-r--r-- | examples/resources/lib/quaternion.lua | 2 | ||||
| -rw-r--r-- | examples/resources/lib/raygui.lua | 270 | ||||
| -rw-r--r-- | examples/resources/lib/rectangle.lua | 15 | ||||
| -rw-r--r-- | examples/resources/lib/vector2.lua | 2 | ||||
| -rw-r--r-- | examples/resources/lib/vector3.lua | 2 |
7 files changed, 189 insertions, 115 deletions
diff --git a/examples/resources/lib/color.lua b/examples/resources/lib/color.lua index 8948ac7..6e5d445 100644 --- a/examples/resources/lib/color.lua +++ b/examples/resources/lib/color.lua @@ -32,11 +32,14 @@ Color.meta = { __idiv = function( c, v ) return Color:new( c.r // v, c.g // v, c.b // v, c.a // v ) end, - __len = function( _ ) + __len = function() return 4 end, __eq = function( c1, c2 ) - return c1.r == c2.r and c1.g == c2.g and c1.b == c2.b and c1.a == c2.a + return math.floor( c1.r ) == math.floor( c2.r ) + and math.floor( c1.g ) == math.floor( c2.g ) + and math.floor( c1.b ) == math.floor( c2.b ) + and math.floor( c1.a ) == math.floor( c2.a ) end, } @@ -65,7 +68,7 @@ function Color:set( r, g, b, a ) if type( r ) == "table" then r, g, b, a = table.unpack( r ) elseif type( r ) == "nil" then - r, g, b, a = 0, 0, 0, 0 + r, g, b, a = 0, 0, 0, 255 end if a == nil then @@ -87,7 +90,7 @@ function Color:unpack() end function Color:clone() - return Color:new( self ) + return Color:new( self.r, self.g, self.b, self.a ) end function Color:scale( scalar ) diff --git a/examples/resources/lib/gui.lua b/examples/resources/lib/gui.lua index 4e3b744..3ee1a1c 100644 --- a/examples/resources/lib/gui.lua +++ b/examples/resources/lib/gui.lua @@ -264,7 +264,7 @@ function Text:draw() return 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.DrawTextEx( self.font, self.text, { self._parent.bounds.x + self.bounds.x, self._parent.bounds.y + self.bounds.y }, self.fontSize, self.spacing, self.color ) end -- Texture. diff --git a/examples/resources/lib/quaternion.lua b/examples/resources/lib/quaternion.lua index 4c42265..8c19cc4 100644 --- a/examples/resources/lib/quaternion.lua +++ b/examples/resources/lib/quaternion.lua @@ -27,7 +27,7 @@ Quaternion.meta = { __unm = function( q ) return Quaternion:new( RL.QuaternionInvert( q ) ) end, - __len = function( _ ) + __len = function() return 4 end, __eq = function( q1, q2 ) diff --git a/examples/resources/lib/raygui.lua b/examples/resources/lib/raygui.lua index 69f5694..6fcd87b 100644 --- a/examples/resources/lib/raygui.lua +++ b/examples/resources/lib/raygui.lua @@ -1,9 +1,9 @@ -- Wrapper for raygui that should make using it a bit more convenient. -Util = require( "utillib" ) -Rect = require( "rectangle" ) -Vec2 = require( "vector2" ) -Color = require( "color" ) +local Util = require( "utillib" ) +local Rect = require( "rectangle" ) +local Vec2 = require( "vector2" ) +local Color = require( "color" ) ---@param text string ---@return integer count, table rowItemCounts @@ -54,6 +54,7 @@ local Raygui = { focused = 0, dragging = nil, grabPos = Vec2:new(), + scrolling = false, } function Raygui.process() @@ -110,7 +111,11 @@ function Raygui.drag( element ) end function Raygui.draw() - RL.GuiLock() + if not Raygui.scrolling then + RL.GuiLock() + elseif RL.IsMouseButtonReleased( RL.MOUSE_BUTTON_LEFT ) then + Raygui.scrolling = false + end for i, element in ipairs( Raygui.elements ) do if i == Raygui.focused then @@ -143,6 +148,16 @@ function Raygui.set2Back( element ) end end +function Raygui.remove( element ) + for i, curElement in ipairs( Raygui.elements ) do + if element == curElement then + table.remove( Raygui.elements, i ) + + return + end + end +end + --[[ Container/separator controls, useful for controls organization ]]-- @@ -189,7 +204,9 @@ function WindowBox:process() end function WindowBox:draw() - if RL.GuiWindowBox( self.bounds, self.text ) and self.callback ~= nil then + local result = RL.GuiWindowBox( self.bounds, self.text ) + + if result == 1 and self.callback ~= nil then self.callback( self ) end end @@ -304,6 +321,52 @@ function Panel:setPosition( pos ) self.bounds.y = pos.y end +-- GuiTabBar. + +--- Tab Bar control, returns TAB to be closed or -1 +GuiTabBar = {} +GuiTabBar.__index = GuiTabBar + +function GuiTabBar:new( bounds, text, active, callback, closeCallback ) + local object = setmetatable( {}, GuiTabBar ) + + object.bounds = bounds:clone() + object.text = text + object.active = active + + object.visible = true + object.callback = callback + object.closeCallback = closeCallback + + table.insert( Raygui.elements, object ) + + return object +end + +function GuiTabBar:process() + return RL.CheckCollisionPointRec( RL.GetMousePosition(), self.bounds ) +end + +function GuiTabBar:draw() + local oldActive = self.active + local result = -1 + + result, self.active = RL.GuiTabBar( self.bounds, self.text, self.active ) + + if self.active ~= oldActive and self.callback ~= nil then + self.callback( self ) + end + + if 0 <= result and self.closeCallback ~= nil then + self.closeCallback( self, result ) + end +end + +function GuiTabBar:setPosition( pos ) + self.bounds.x = pos.x + self.bounds.y = pos.y +end + -- ScrollPanel. --- Scroll Panel control @@ -336,14 +399,17 @@ end function ScrollPanel:draw() local oldScroll = self.scroll:clone() - - local view, scroll = RL.GuiScrollPanel( self.bounds, self.text, self.content, self.scroll ) + 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 and self.callback ~= nil then - self.callback( self ) + if self.scroll ~= oldScroll then + Raygui.scrolling = true + + if self.callback ~= nil then + self.callback( self ) + end end end @@ -401,7 +467,6 @@ function Button:new( bounds, text, callback ) object.text = text object.callback = callback - object.clicked = false object.visible = true table.insert( Raygui.elements, object ) @@ -414,9 +479,9 @@ function Button:process() end function Button:draw() - self.clicked = RL.GuiButton( self.bounds, self.text ) + local result = RL.GuiButton( self.bounds, self.text ) - if self.clicked and self.callback ~= nil then + if result == 1 and self.callback ~= nil then self.callback( self ) end end @@ -439,7 +504,6 @@ function LabelButton:new( bounds, text, callback ) object.text = text object.callback = callback - object.clicked = false object.visible = true table.insert( Raygui.elements, object ) @@ -452,9 +516,9 @@ function LabelButton:process() end function LabelButton:draw() - self.clicked = RL.GuiLabelButton( self.bounds, self.text ) + local result = RL.GuiLabelButton( self.bounds, self.text ) - if self.clicked and self.callback ~= nil then + if result == 1 and self.callback ~= nil then self.callback( self ) end end @@ -492,7 +556,7 @@ end function Toggle:draw() local oldActive = self.active - self.active = RL.GuiToggle( self.bounds, self.text, self.active ) + _, self.active = RL.GuiToggle( self.bounds, self.text, self.active ) if self.active ~= oldActive and self.callback ~= nil then self.callback( self ) @@ -573,15 +637,11 @@ end function ToggleGroup:draw() local oldActive = self.active - self.active = RL.GuiToggleGroup( self.bounds, self.text, self.active ) + _, self.active = RL.GuiToggleGroup( self.bounds, self.text, self.active ) if self.active ~= oldActive and self.callback ~= nil then self.callback( self ) end - - -- for _, rect in ipairs( self.focusBounds ) do - -- RL.DrawRectangleLines( rect, RL.RED ) - -- end end function ToggleGroup:setPosition( pos ) @@ -658,7 +718,7 @@ end function CheckBox:draw() local oldChecked = self.checked - self.checked = RL.GuiCheckBox( self.bounds, self.text, self.checked ) + _, self.checked = RL.GuiCheckBox( self.bounds, self.text, self.checked ) if self.checked ~= oldChecked and self.callback ~= nil then self.callback( self ) @@ -702,7 +762,7 @@ end function ComboBox:draw() local oldActive = self.active - self.active = RL.GuiComboBox( self.bounds, self.text, self.active ) + _, self.active = RL.GuiComboBox( self.bounds, self.text, self.active ) if self.active ~= oldActive and self.callback ~= nil then self.callback( self ) @@ -768,11 +828,11 @@ function DropdownBox:process() end function DropdownBox:draw() - local pressed = false + local result = 0 - pressed, self.active = RL.GuiDropdownBox( self.bounds, self.text, self.active, self.editMode ) + result, self.active = RL.GuiDropdownBox( self.bounds, self.text, self.active, self.editMode ) - if pressed then + if result == 1 then self.editMode = not self.editMode if not self.editMode and self.callback ~= nil then @@ -815,12 +875,12 @@ function Spinner:process() end function Spinner:draw() - local pressed = false + local result = 0 local oldValue = self.value - pressed, self.value = RL.GuiSpinner( self.bounds, self.text, self.value, self.minValue, self.maxValue, self.editMode ) + result, self.value = RL.GuiSpinner( self.bounds, self.text, self.value, self.minValue, self.maxValue, self.editMode ) - if pressed then + if result == 1 then self.editMode = not self.editMode end @@ -863,12 +923,12 @@ function ValueBox:process() end function ValueBox:draw() - local pressed = false + local result = 0 local oldValue = self.value - pressed, self.value = RL.GuiValueBox( self.bounds, self.text, self.value, self.minValue, self.maxValue, self.editMode ) + result, self.value = RL.GuiValueBox( self.bounds, self.text, self.value, self.minValue, self.maxValue, self.editMode ) - if pressed then + if result == 1 then self.editMode = not self.editMode end @@ -910,19 +970,19 @@ function TextBox:process() end function TextBox:draw() - local pressed = false + local result = 0 if self.scissorMode then RL.BeginScissorMode( self.bounds ) end - pressed, self.text = RL.GuiTextBox( self.bounds, self.text, self.textSize, self.editMode ) + result, self.text = RL.GuiTextBox( self.bounds, self.text, self.textSize, self.editMode ) if self.scissorMode then RL.EndScissorMode() end - if pressed then + if result == 1 then self.editMode = not self.editMode if not self.editMode and self.callback ~= nil then @@ -936,51 +996,6 @@ function TextBox:setPosition( pos ) self.bounds.y = pos.y end --- TextBoxMulti. - --- Text Box control with multiple lines -TextBoxMulti = {} -TextBoxMulti.__index = TextBoxMulti - -function TextBoxMulti:new( bounds, text, textSize, editMode, callback ) - local object = setmetatable( {}, TextBoxMulti ) - - object.bounds = bounds:clone() - object.text = text - object.textSize = textSize - object.editMode = editMode - object.callback = callback - - object.visible = true - - table.insert( Raygui.elements, object ) - - return object -end - -function TextBoxMulti:process() - return RL.CheckCollisionPointRec( RL.GetMousePosition(), self.bounds ) -end - -function TextBoxMulti:draw() - local pressed = false - - pressed, self.text = RL.GuiTextBoxMulti( self.bounds, self.text, self.textSize, self.editMode ) - - if pressed then - self.editMode = not self.editMode - - if not self.editMode and self.callback ~= nil then - self.callback( self ) - end - end -end - -function TextBoxMulti:setPosition( pos ) - self.bounds.x = pos.x - self.bounds.y = pos.y -end - -- Slider. --- Slider control @@ -1012,10 +1027,14 @@ end function Slider:draw() local oldValue = self.value - self.value = RL.GuiSlider( self.bounds, self.textLeft, self.textRight, self.value, self.minValue, self.maxValue ) + _, self.value = RL.GuiSlider( self.bounds, self.textLeft, self.textRight, self.value, self.minValue, self.maxValue ) if self.value ~= oldValue then - self.callback( self ) + Raygui.scrolling = true + + if self.callback ~= nil then + self.callback( self ) + end end end @@ -1055,10 +1074,14 @@ end function SliderBar:draw() local oldValue = self.value - self.value = RL.GuiSliderBar( self.bounds, self.textLeft, self.textRight, self.value, self.minValue, self.maxValue ) + _, self.value = RL.GuiSliderBar( self.bounds, self.textLeft, self.textRight, self.value, self.minValue, self.maxValue ) if self.value ~= oldValue then - self.callback( self ) + Raygui.scrolling = true + + if self.callback ~= nil then + self.callback( self ) + end end end @@ -1098,7 +1121,7 @@ end function ProgressBar:draw() local oldValue = self.value - self.value = RL.GuiProgressBar( self.bounds, self.textLeft, self.textRight, self.value, self.minValue, self.maxValue ) + _, self.value = RL.GuiProgressBar( self.bounds, self.textLeft, self.textRight, self.value, self.minValue, self.maxValue ) if self.value ~= oldValue then self.callback( self ) @@ -1189,7 +1212,7 @@ function Grid:new( bounds, text, spacing, subdivs, callback ) object.subdivs = subdivs object.callback = callback - object.cell = Vec2:new() + object.mouseCell = Vec2:new() object.visible = true table.insert( Raygui.elements, object ) @@ -1202,11 +1225,13 @@ function Grid:process() end function Grid:draw() - local oldCell = self.cell:clone() + local oldCell = self.mouseCell:clone() + local mouseCell = {} - self.cell = Vec2:new( RL.GuiGrid( self.bounds, self.text, self.spacing, self.subdivs ) ) + _, mouseCell = RL.GuiGrid( self.bounds, self.text, self.spacing, self.subdivs, self.mouseCell ) + self.mouseCell = Vec2:new( mouseCell ) - if oldCell ~= self.cell and self.callback ~= nil then + if oldCell ~= self.mouseCell and self.callback ~= nil then self.callback( self ) end end @@ -1251,9 +1276,13 @@ end function ListView:draw() local oldActive = self.active + local oldScrollIndex = self.scrollIndex - self.active, self.scrollIndex = RL.GuiListView( self.bounds, self.text, self.scrollIndex, self.active ) + _, self.scrollIndex, self.active = RL.GuiListView( self.bounds, self.text, self.scrollIndex, self.active ) + if self.scrollIndex ~= oldScrollIndex then + Raygui.scrolling = true + end if oldActive ~= self.active and self.callback ~= nil then self.callback( self ) end @@ -1270,14 +1299,14 @@ end ListViewEx = {} ListViewEx.__index = ListViewEx -function ListViewEx:new( bounds, text, focus, scrollIndex, active, callback ) +function ListViewEx:new( bounds, text, scrollIndex, active, focus, callback ) local object = setmetatable( {}, ListViewEx ) object.bounds = bounds:clone() object.text = text - object.focus = focus object.scrollIndex = scrollIndex object.active = active + object.focus = focus object.callback = callback object.visible = true @@ -1296,9 +1325,13 @@ end function ListViewEx:draw() local oldActive = self.active + local oldScrollIndex = self.scrollIndex - self.active, self.scrollIndex, self.focus = RL.GuiListViewEx( self.bounds, self.text, self.focus, self.scrollIndex, self.active ) + _, self.scrollIndex, self.active, self.focus = RL.GuiListViewEx( self.bounds, self.text, self.scrollIndex, self.active, self.focus ) + if self.scrollIndex ~= oldScrollIndex then + Raygui.scrolling = true + end if oldActive ~= self.active and self.callback ~= nil then self.callback( self ) end @@ -1421,24 +1454,41 @@ function ColorPicker:new( bounds, text, color, callback ) object.callback = callback object.visible = true + object.focusBounds = Rect:new() + object:updateFocusBounds() table.insert( Raygui.elements, object ) return object end function ColorPicker:process() - return RL.CheckCollisionPointRec( RL.GetMousePosition(), self.bounds ) + return RL.CheckCollisionPointRec( RL.GetMousePosition(), self.focusBounds ) +end + +function ColorPicker:updateFocusBounds() + local boundsHue = Rect:new( + self.bounds.x + self.bounds.width + RL.GuiGetStyle( RL.COLORPICKER, RL.HUEBAR_PADDING ), + self.bounds.y, + RL.GuiGetStyle( RL.COLORPICKER, RL.HUEBAR_WIDTH ), + self.bounds.height + ) + + self.focusBounds = self.bounds:fit( boundsHue ) end function ColorPicker:draw() local oldColor = self.color:clone() - local color = RL.GuiColorPicker( self.bounds, self.text, self.color ) + local _, color = RL.GuiColorPicker( self.bounds, self.text, self.color ) self.color = Color:new( color ) - if oldColor ~= self.color and self.callback ~= nil then - self.callback( self ) + if self.color ~= oldColor then + Raygui.scrolling = true + + if self.callback ~= nil then + self.callback( self ) + end end end @@ -1474,7 +1524,7 @@ end function ColorPanel:draw() local oldColor = self.color:clone() - local color = RL.GuiColorPanel( self.bounds, self.text, self.color ) + local _, color = RL.GuiColorPanel( self.bounds, self.text, self.color ) self.color = Color:new( color ) @@ -1515,10 +1565,14 @@ end function ColorBarAlpha:draw() local oldAlpha = self.alpha - self.alpha = RL.GuiColorBarAlpha( self.bounds, self.text, self.alpha ) + _, self.alpha = RL.GuiColorBarAlpha( self.bounds, self.text, self.alpha ) - if self.alpha ~= oldAlpha and self.callback ~= nil then - self.callback( self ) + if self.alpha ~= oldAlpha then + Raygui.scrolling = true + + if self.callback ~= nil then + self.callback( self ) + end end end @@ -1554,10 +1608,14 @@ end function ColorBarHue:draw() local oldValue = self.value - self.value = RL.GuiColorBarHue( self.bounds, self.text, self.value ) + _, self.value = RL.GuiColorBarHue( self.bounds, self.text, self.value ) - if self.value ~= oldValue and self.callback ~= nil then - self.callback( self ) + if self.value ~= oldValue then + Raygui.scrolling = true + + if self.callback ~= nil then + self.callback( self ) + end end end @@ -1572,6 +1630,7 @@ Raygui.WindowBox = WindowBox Raygui.GroupBox = GroupBox Raygui.Line = Line Raygui.Panel = Panel +Raygui.GuiTabBar = GuiTabBar Raygui.ScrollPanel = ScrollPanel Raygui.Label = Label @@ -1585,7 +1644,6 @@ Raygui.DropdownBox = DropdownBox Raygui.Spinner = Spinner Raygui.ValueBox = ValueBox Raygui.TextBox = TextBox -Raygui.TextBoxMulti = TextBoxMulti Raygui.Slider = Slider Raygui.SliderBar = SliderBar Raygui.ProgressBar = ProgressBar diff --git a/examples/resources/lib/rectangle.lua b/examples/resources/lib/rectangle.lua index 0b84b54..93f2bb3 100644 --- a/examples/resources/lib/rectangle.lua +++ b/examples/resources/lib/rectangle.lua @@ -3,6 +3,8 @@ if table.unpack == nil then table.unpack = unpack end +local Vector2 = require( "vector2" ) + Rectangle = {} Rectangle.meta = { @@ -34,7 +36,7 @@ Rectangle.meta = { __idiv = function( r, v ) return Rectangle:new( r.x // v, r.y // v, r.width // v, r.height // v ) end, - __len = function( _ ) + __len = function() return 4 end, __eq = function( r1, r2 ) @@ -108,6 +110,17 @@ function Rectangle:area() return self.width * self.height end +function Rectangle:fit( rec ) + local pos = Vector2:new( math.min( self.x, rec.x ), math.min( self.y, rec.y ) ) + + return Rectangle:new( + pos.x, + pos.y, + math.max( self.x + self.width - pos.x, rec.x + rec.width - pos.x ), + math.max( self.y + self.height - pos.y, rec.y + rec.height - pos.y ) + ) +end + function Rectangle:checkCollisionRec( rec ) return RL.CheckCollisionRecs( self, rec ) end diff --git a/examples/resources/lib/vector2.lua b/examples/resources/lib/vector2.lua index b50ac1b..c6efd5c 100644 --- a/examples/resources/lib/vector2.lua +++ b/examples/resources/lib/vector2.lua @@ -30,7 +30,7 @@ Vector2.meta = { __unm = function( v ) return Vector2:new( -v.x, -v.y ) end, - __len = function( _ ) + __len = function() return 2 end, __eq = function( v1, v2 ) diff --git a/examples/resources/lib/vector3.lua b/examples/resources/lib/vector3.lua index 725a38d..7059690 100644 --- a/examples/resources/lib/vector3.lua +++ b/examples/resources/lib/vector3.lua @@ -30,7 +30,7 @@ Vector3.meta = { __unm = function( v ) return Vector3:new( -v.x, -v.y, -v.z ) end, - __len = function( _ ) + __len = function() return 3 end, __eq = function( v1, v2 ) |
