fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
29daa42
GUI
1local gui =require ( "gui" ) 2 3require ( "extensions.table" ) 4 5local PANEL ={} 6 7local graphics =love . graphics 8 9function PANEL : Initialize () 10self . m_tChildren ={} 11self . m_tOrphans ={} 12self . m_iWorldPosX =0 13self . m_iWorldPosY =0 14self . m_fScaleX =1 15self . m_fScaleY =1 16self . m_iPosX =0 17self . m_iPosY =0 18self . m_iWidth =42 19self . m_iHeight =24 20self . m_bHovered =false 21self . m_pParent =nil 22self . m_iZPos =0 23self . m_bValidated =false 24self . m_bVisible =true 25self . m_bFocusable =true 26self . m_bOrphaned =false 27self . m_bDeleted =false 28self . m_iDock =0 29self . m_tDockMargins ={ left =4 , top =4 , right =4 , bottom =4 } 30self . m_tDockPadding ={ left =4 , top =4 , right =4 , bottom =4 } 31end 32 33function PANEL : GetClassName () 34return self . __classname 35end 36 37function PANEL : __tostring () 38return ( "Panel[%q]" ): format ( self . __classname ) 39end 40 41function PANEL : GetConfig () 42local w , h =self : GetActualSize () 43local sx , sy =self : GetScale () 44 45local config ={ 46classname =self : GetClassName (), 47pos ={ x =self : GetX (), y =self : GetY (), z =self : GetZPos () } , 48size ={ width =w , height =h } , 49scale ={ x =sx , y =sy } , 50visible =self : IsVisible (), 51accessors =self . __accessors , 52} 53 54if self . m_iDock ~= 0 then 55config . dock ={ 56mode =self . m_iDock , 57margins =self . m_tDockMargins , 58padding =self . m_tDockPadding , 59} 60end 61 62return config 63end 64 65function PANEL : SetVisible ( b ) 66self . m_bVisible =b 67end 68 69function PANEL : IsVisible () 70return self . m_bVisible 71end 72 73function PANEL : SetFocusable ( b ) 74self . m_bFocusable =b 75end 76 77function PANEL : IsFocusable () 78return self . m_bFocusable 79end 80 81function PANEL : BringToFront () 82local parent =self . m_pParent 83if not parent then return end -- There's nothing for it to go in front of! 84 85local highest =nil 86 87for _ , child in ipairs ( parent . m_tChildren ) do 88if not highest or child : GetZPos () > highest : GetZPos () then 89highest =child 90end 91end 92 93-- No panels or the highest is already ourself 94if not highest or highest == self then return end 95 96-- Get the position of the highest panel 97local replace =highest : GetZPos () 98 99-- Set the highest panel to our current position 100highest : SetZPos ( self : GetZPos ()) 101 102-- Set our position to where the highest panel used to be 103self : SetZPos ( replace ) 104end 105 106function PANEL : Dock ( i ) 107self . m_iDock =i 108self : InvalidateLayout () 109end 110 111function PANEL : GetDock () 112return self . m_iDock 113end 114 115function PANEL : DockMargin ( left , top , right , bottom ) 116self . m_tDockMargins . left =left 117self . m_tDockMargins . top =top 118self . m_tDockMargins . right =right 119self . m_tDockMargins . bottom =bottom 120end 121 122function PANEL : GetDockMargin () 123return self . m_tDockMargins 124end 125 126function PANEL : DockPadding ( left , top , right , bottom ) 127self . m_tDockPadding . left =left 128self . m_tDockPadding . top =top 129self . m_tDockPadding . right =right 130self . m_tDockPadding . bottom =bottom 131end 132 133function PANEL : GetDockPadding () 134return self . m_tDockPadding 135end 136 137function PANEL : GetWidthPlusMargin () 138if not self : IsVisible () then return 0 end 139local margins =self : GetDockMargin () 140return margins . left + margins . right + self : GetWidth () 141end 142 143function PANEL : GetHeightPlusMargin () 144if not self : IsVisible () then return 0 end 145local margins =self : GetDockMargin () 146return margins . top + margins . bottom + self : GetHeight () 147end 148 149function PANEL : GetSizePlusMargin () 150local w , h =self : GetWidthPlusMargin (), self : GetHeightPlusMargin () 151 152--[[for _,child in ipairs(self.m_tChildren) do 153if child:IsVisible() then 154margins = child:GetDockMargin() 155padding = child:GetDockPadding() 156w = w + margins.left + margins.right + padding.left + padding.right 157h = h + margins.top + margins.bottom + padding.top + padding.bottom 158end 159end]] 160 161return w , h 162end 163 164--[[function PANEL:GetSpaceAround(panel) 165local margins = panel:GetDockMargin() 166local padding = panel:GetDockPadding() 167 168local w = margins.left + margins.right + padding.left + padding.right 169local h = margins.top + margins.bottom + padding.top + padding.bottom 170 171for _,child in ipairs(self.m_tChildren) do 172if child ~= panel and child:IsVisible() then 173local margins = child:GetDockMargin() 174local padding = child:GetDockPadding() 175w = w + margins.left + margins.right + padding.left + padding.right + child:GetWidth() 176h = h + margins.top + margins.bottom + padding.top + padding.bottom + child:GetHeight() 177end 178end 179 180local width, height = self:GetSize() 181 182return w, h 183end]] 184 185function PANEL : SetZPos ( i ) 186self . m_iZPos =i 187self : GetParent (): ReorderChildren () 188end 189 190function PANEL : ReorderChildren () 191table . sort ( self . m_tChildren , function ( a , b ) return a . m_iZPos < b . m_iZPos end ) 192end 193 194function PANEL : GetZPos () 195return self . m_iZPos 196end 197 198function PANEL : CallAll ( func , ... ) 199if not self : IsVisible () then return end 200self [ func ]( self , ... ) 201for _ , child in ipairs ( self . m_tChildren ) do 202child : CallAll ( func , ... ) 203end 204end 205 206function PANEL : CallSelfAndParents ( func , ... ) 207if not self : IsVisible () then return end 208if self [ func ]( self , ... ) then return end 209local parent =self . m_pParent 210if not parent then return end 211parent : CallSelfAndParents ( func , ... ) 212end 213 214function PANEL : Remove () 215local parent =self : GetParent () 216if parent then 217parent : OnChildRemoved ( self ) 218end 219 220self . m_bDeleted =true 221 222for zpos , child in ipairs ( self . m_tChildren ) do 223child : Remove () 224end 225end 226 227function PANEL : Clear () 228for zpos , child in ipairs ( self . m_tChildren ) do 229child : Remove () 230end 231end 232 233function PANEL : InvalidateLayout () 234self . m_bValidated =false 235for _ , child in ipairs ( self . m_tChildren ) do 236child : InvalidateLayout () 237end 238end 239 240function PANEL : InvalidateParent () 241if not self . m_pParent then return end 242self . m_pParent : InvalidateLayout () 243end 244 245function PANEL : MarkAsOrphan () 246self . m_bOrphaned =true 247end 248 249function PANEL : CleanupOrphans () 250for key , child in reversedipairs ( self . m_tChildren ) do 251child : CleanupOrphans () 252if child . m_bOrphaned then 253table . remove ( self . m_tChildren , key ) 254end 255end 256end 257 258function PANEL : CleanupDeleted () 259for key , child in reversedipairs ( self . m_tChildren ) do 260child : CleanupDeleted () 261if child . m_bDeleted then 262self . m_tChildren [ key ] =nil 263table . remove ( self . m_tChildren , key ) 264end 265end 266end 267 268function PANEL : SetParent ( parent ) 269if self . m_pParent then 270self : MarkAsOrphan () 271end 272self . m_pParent =parent 273if parent then 274table . insert ( parent . m_tChildren , self ) 275parent : OnChildAdded ( self ) 276self : SetZPos ( # parent . m_tChildren ) 277end 278end 279 280function PANEL : HasParent () 281return self : GetParent () ~= nil 282end 283 284function PANEL : GetParent () 285return self . m_pParent 286end 287 288function PANEL : GetParents () 289local parents ={} 290 291local parent =self : GetParent () 292 293while parent do 294table . insert ( parents , parent ) 295parent =parent : GetParent () 296end 297return parents 298end 299 300function PANEL : GetChildren () 301return self . m_tChildren 302end 303 304function PANEL : GetFamily ( family ) 305local family =family or {} 306 307local children =self . m_tChildren 308family [ self ] =children 309 310for _ , child in ipairs ( children ) do 311child : GetFamily ( family [ self ]) 312end 313 314return family 315end 316 317function PANEL : SizeToScreen () 318self : SetSize ( graphics . getPixelDimensions ()) 319end 320 321function PANEL : SizeToChildren ( doWidth , doHeight ) 322local w , h =0 , 0 323local padding =self : GetDockPadding () 324 325w =padding . left -- + padding.right 326h =padding . top -- + padding.bottom 327 328local lw , lh =0 , 0 329 330for _ , child in ipairs ( self . m_tChildren ) do 331if child : IsVisible () then 332local x , y =child : GetPos () 333local margin =child : GetDockMargin () 334local cw , ch =child : GetSize () 335 336-- Position + size + margins = maximum bounds 337cw =x + cw + margin . left + margin . right 338ch =y + ch + margin . top + margin . bottom 339 340-- Update the largest widths and heights 341if cw > lw then lw =cw end 342if ch > lh then lh =ch end 343end 344end 345if doWidth then self : SetWidth ( w + lw ) end 346if doHeight then self : SetHeight ( h + lh ) end 347end 348 349function PANEL : Add ( class ) 350return gui . create ( class , self ) 351end 352 353function PANEL : SetScale ( x , y ) 354y =y or x 355self . m_fScaleX , self . m_fScaleY =x , y 356end 357 358function PANEL : GetScale () 359return self . m_fScaleX , self . m_fScaleY 360end 361 362function PANEL : SetPos ( x , y ) 363self . m_iPosX , self . m_iPosY =x , y 364end 365 366function PANEL : GetPos () 367return self . m_iPosX , self . m_iPosY 368end 369 370function PANEL : SetWorldPos ( x , y ) 371self . m_iWorldPosX , self . m_iWorldPosY =x , y 372end 373 374function PANEL : GetWorldPos () 375return self . m_iWorldPosX , self . m_iWorldPosY 376end 377 378function PANEL : LocalToWorld ( x , y ) 379local sx , sy =self : GetPos () 380x =x + sx 381y =y + sy 382 383local parent =self : GetParent () 384 385while parent do 386local px , py =parent : GetPos () 387x =x + px 388y =y + py 389parent =parent : GetParent () 390end 391 392return x , y 393end 394PANEL . LocalToScreen =PANEL . LocalToWorld 395 396function PANEL : WorldToLocal ( x , y ) 397local sx , sy =self : LocalToScreen ( 0 , 0 ) 398return x - sx , y - sy 399end 400PANEL . ScreenToLocal =PANEL . WorldToLocal 401 402function PANEL : SetX ( x ) 403self . m_iPosX =x 404end 405 406function PANEL : GetX () 407return self . m_iPosX 408end 409 410function PANEL : SetY ( y ) 411self . m_iPosY =y 412end 413 414function PANEL : GetY () 415return self . m_iPosY 416end 417 418function PANEL : SetSize ( w , h ) 419self . m_iWidth , self . m_iHeight =w , h 420self : OnResize ( w , h ) 421end 422 423function PANEL : GetSize () 424return self . m_iWidth * self . m_fScaleX , self . m_iHeight * self . m_fScaleY 425end 426 427function PANEL : GetActualSize () 428return self . m_iWidth , self . m_iHeight 429end 430 431function PANEL : SetWidth ( w ) 432if self . m_iWidth ~= w then 433self . m_iWidth =w 434--self:InvalidateLayout() 435end 436end 437 438function PANEL : GetWidth () 439return self . m_iWidth * self . m_fScaleX 440end 441 442function PANEL : SetHeight ( h ) 443if self . m_iHeight ~= h then 444self . m_iHeight =h 445--self:InvalidateLayout() 446end 447end 448 449function PANEL : GetHeight () 450return self . m_iHeight * self . m_fScaleY 451end 452 453function PANEL : IsWorldPointInside ( x , y ) 454local px , py =self : LocalToScreen ( 0 , 0 ) 455return x > px and x < px + self : GetWidth () and y > py and y < py + self : GetHeight () 456end 457 458function PANEL : Render () 459if not self : IsVisible () then return end 460 461local x , y =self : LocalToScreen ( 0 , 0 ) 462local w , h =self : GetSize () 463 464local parent =self : GetParent () 465 466-- Start the scissor position and size with our own values 467local sx , sy =x , y 468local sw , sh =w , h 469 470while parent do 471-- If we have a parent, fit the scissor to fit inside their bounds 472local px , py =parent : LocalToScreen ( 0 , 0 ) 473local pw , ph =parent : GetSize () 474 475if sx < px then 476sw =math . max ( 0 , sw + sx - px ) 477sx =px 478end 479if sx + sw > px + pw then 480sw =math . max ( 0 , sw - (( sx + sw ) - ( px + pw ))) 481sx =math . min ( sx , px + pw ) 482end 483if sy < py then 484sh =math . max ( 0 , sh + sy - py ) 485sy =py 486end 487if sy + sh > py + ph then 488sh =math . max ( 0 , sh - (( sy + sh ) - ( py + ph ))) 489sy =math . min ( sy , py + ph ) 490end 491 492parent =parent : GetParent () 493end 494 495graphics . push () -- Push the current graphics state 496graphics . setScissor ( sx , sy , sw , sh ) -- Set our scissor so things can't be drawn outside the panel 497graphics . translate ( x , y ) -- Translate so Paint has localized position values for drawing objects 498graphics . scale ( self : GetScale ()) 499local uw , uh =self : GetActualSize () 500self : SetWorldPos ( x , y ) 501graphics . setColor ( 255 , 255 , 255 , 255 ) 502self : PrePaint ( uw , uh ) 503graphics . setColor ( 255 , 255 , 255 , 255 ) 504self : Paint ( uw , uh ) 505graphics . setColor ( 255 , 255 , 255 , 255 ) 506self : PostPaint ( uw , uh ) 507graphics . origin () 508graphics . setScissor () 509graphics . pop () -- Reset the graphics state to what it was 510 511-- recently added panels are drawn last, thus, ontop of older panels 512for _ , child in ipairs ( self . m_tChildren ) do 513child : Render () 514end 515 516graphics . push () 517graphics . setScissor ( sx , sy , sw , sh ) 518graphics . translate ( x , y ) 519graphics . scale ( self : GetScale ()) 520graphics . setColor ( 255 , 255 , 255 , 255 ) 521self : PaintOverlay ( self : GetActualSize ()) 522graphics . origin () 523graphics . setScissor () 524graphics . pop () -- Reset the graphics state to what it was 525 526if self . m_bDebug then 527-- Debug the scissor rect 528graphics . setColor ( 255 , 0 , 0 , 25 ) 529graphics . rectangle ( "fill" , sx , sy , sw , sh ) 530end 531end 532 533function PANEL : ValidateLayout () 534if not self . m_bValidated then 535self . m_bValidated =true 536self : DockLayout () 537self : PerformLayout () 538end 539 540for _ , child in ipairs ( self . m_tChildren ) do 541child : ValidateLayout () 542end 543end 544 545function PANEL : Center ( vertical , horizontal ) 546local parent =self : GetParent () 547 548local w , h =self : GetSize () 549local pw , ph =graphics . getPixelDimensions () -- Default to window size if no parent 550if parent then 551pw , ph =parent : GetSize () 552end 553 554-- If both vertical and horizontal aren't set, center to both? 555local all =not vertical and not horizontal 556 557if vertical or all then 558self : SetY (( ph / 2 ) - ( h / 2 )) 559end 560if horizontal or all then 561self : SetX (( pw / 2 ) - ( w / 2 )) 562end 563end 564 565function PANEL : CenterVertical () 566self : Center ( true , false ) 567end 568 569function PANEL : CenterHorizontal () 570self : Center ( false , true ) 571end 572 573function PANEL : DockLayout () 574local x , y =0 , 0 575local w , h =self : GetSize () 576 577local padding =self . m_tDockPadding 578 579for _ , child in ipairs ( self . m_tChildren ) do 580local margin =child . m_tDockMargins 581 582local dx =x + padding . left 583local dy =y + padding . top 584local dw =w - ( padding . left + padding . right ) 585local dh =h - ( padding . top + padding . bottom ) 586 587local dock =child . m_iDock 588if dock ~= DOCK_NONE and child : IsVisible () then 589if ( dock == DOCK_TOP ) then 590child : SetPos ( dx + margin . left , dy + margin . top ) 591child : SetSize ( dw - margin . left - margin . right , child : GetHeight ()) 592local height =margin . top + margin . bottom + child : GetHeight () 593y =y + height 594h =h - height 595elseif ( dock == DOCK_LEFT ) then 596child : SetPos ( dx + margin . left , dy + margin . top ) 597child : SetSize ( child : GetWidth (), dh - margin . top - margin . bottom ) 598local width =margin . left + margin . right + child : GetWidth () 599x =x + width 600w =w - width 601elseif ( dock == DOCK_RIGHT ) then 602child : SetPos (( dx + dw ) - child : GetWidth () - margin . right , dy + margin . top ) 603child : SetSize ( child : GetWidth (), dh - margin . top - margin . bottom ) 604local width =margin . left + margin . right + child : GetWidth () 605w =w - width 606elseif ( dock == DOCK_BOTTOM ) then 607child : SetPos ( dx + margin . left , ( dy + dh ) - child : GetHeight () - margin . bottom ) 608child : SetSize ( dw - margin . left - margin . right , child : GetHeight ()) 609h =h - ( child : GetHeight () + margin . bottom + margin . top ) 610end 611end 612end 613 614for _ , child in ipairs ( self . m_tChildren ) do 615local dock =child . m_iDock 616 617if dock ~= DOCK_NONE and child : IsVisible () then 618local margin =child . m_tDockMargins 619 620local dx =x + padding . left 621local dy =y + padding . top 622local dw =w - ( padding . left + padding . right ) 623local dh =h - ( padding . top + padding . bottom ) 624 625if ( dock == DOCK_FILL ) then 626child : SetPos ( dx + margin . left , dy + margin . top ) 627child : SetSize ( dw - margin . left - margin . right , dh - margin . top - margin . bottom ) 628end 629end 630end 631end 632 633function PANEL : GiveFocus () 634gui . setFocusedPanel ( self ) 635end 636 637function PANEL : MakePopup () 638self : BringToFront () 639self : GiveFocus () 640end 641 642function PANEL : HasFocus ( checkchildren ) 643if checkchildren then 644for _ , child in ipairs ( self . m_tChildren ) do 645if child : HasFocus ( checkchildren ) then 646return true 647end 648end 649end 650return gui . getFocusedPanel () == self 651end 652 653function PANEL : IsHovered () 654return gui . getHoveredPanel () == self 655end 656 657function PANEL : GetHoveredChild ( x , y , ignore ) 658local panel =nil 659for _ , child in reversedipairs ( self . m_tChildren ) do 660if child : IsVisible () and child : IsFocusable () and child : IsWorldPointInside ( x , y ) and (( ignore and child ~= ignore ) or not ignore ) then 661panel =child : GetHoveredPanel ( x , y ) 662break 663end 664end 665return panel 666end 667 668function PANEL : GetHoveredPanel ( x , y , ignore ) 669local panel =self 670for _ , child in reversedipairs ( self . m_tChildren ) do 671if child : IsVisible () and child : IsFocusable () and child : IsWorldPointInside ( x , y ) and (( ignore and child ~= ignore ) or not ignore ) then 672panel =child : GetHoveredPanel ( x , y ) 673break 674end 675end 676return panel 677end 678 679-- PANEL OVERRIDE DEFAULTS 680 681function PANEL : PerformLayout () 682-- Called when we are Validating the layout. 683-- Good for manually positioning child panels. 684end 685 686function PANEL : PrePaint ( w , h ) 687end 688 689function PANEL : Paint ( w , h ) 690-- Called every frame, when we are drawing the panel to the screen. 691-- Used to draw custom things within the panel. 692end 693 694function PANEL : PostPaint ( w , h ) 695end 696 697function PANEL : PaintOverlay ( w , h ) 698end 699 700function PANEL : OnResize ( w , h ) 701-- Called when the size of the panel has changed. 702-- Good for manually positioning child panels, like in PerformLayout. 703end 704 705function PANEL : OnFocusChanged ( b ) 706-- Called when the panels focus has either been gained or lost 707end 708 709function PANEL : OnKeyPressed ( key , hex ) 710-- Called when the panel is focused, and a keyboard key has been pressed 711end 712 713function PANEL : OnKeyReleased ( key ) 714-- Called when the panel is focused, and a keyboard key has been released 715end 716 717function PANEL : OnTextInput ( text ) 718-- Called when the panel is focused. 719-- Good for a text input panel. 720end 721 722function PANEL : OnJoyPressed ( joy , but ) 723-- Override 724end 725 726function PANEL : OnJoyReleased ( joy , but ) 727-- Override 728end 729 730function PANEL : OnMouseMoved ( x , y , dx , dy , istouch ) 731--[[ 732Called when when the mouse has been moved. 733 734number x 735The mouse position on the x-axis. 736number y 737The mouse position on the y-axis. 738number dx 739The amount moved along the x-axis since the last time love.mousemoved was called. 740number dy 741The amount moved along the y-axis since the last time love.mousemoved was called. 742 743Returning true will stop the event from going up the family tree 744Child->Parent->Parent->Parent->etc, etc.. 745]] 746 747return false 748end 749 750function PANEL : OnMousePressed ( x , y , button , istouch , presses ) 751--[[ 752Called when a mouse press event has been made. 753 754number x 755The mouse position on the x-axis. 756number y 757The mouse position on the y-axis. 758number dx 759The amount moved along the x-axis since the last time love.mousemoved was called. 760number dy 761The amount moved along the y-axis since the last time love.mousemoved was called. 762 763Returning true will stop the event from going up the family tree 764Child->Parent->Parent->Parent->etc, etc.. 765]] 766 767return false 768end 769 770function PANEL : OnMouseReleased ( x , y , button , istouch , presses ) 771--[[ 772Called when a mouse release event has been made. 773 774number x 775The mouse position on the x-axis. 776number y 777The mouse position on the y-axis. 778number dx 779The amount moved along the x-axis since the last time love.mousemoved was called. 780number dy 781The amount moved along the y-axis since the last time love.mousemoved was called. 782 783Returning true will stop the event from going up the family tree 784Child->Parent->Parent->Parent->etc, etc.. 785]] 786 787return false 788end 789 790function PANEL : OnMouseWheeled ( x , y ) 791--[[ 792Called when the scrollwheel has been turned. 793 794number x 795Amount of horizontal mouse wheel movement. Positive values indicate movement to the right. 796number y 797Amount of vertical mouse wheel movement. Positive values indicate upward movement. 798 799See an example in.. 800gui/panels/core/scrollpanel.lua 801& 802gui/panels/core/scrollbar.lua 803 804Returning true will stop the event from going up the family tree 805Child->Parent->Parent->Parent->etc, etc.. 806]] 807 808return false 809end 810 811function PANEL : OnChildAdded ( panel ) 812-- Called when a panel has been added 813end 814 815function PANEL : OnChildRemoved ( panel ) 816-- Called when a panel has been removed 817end 818 819function PANEL : Think ( dt ) 820-- Called every frame 821end 822 823gui . register ( "Base" , PANEL )