fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
a8034e9
main
1local PANEL =class . create ( "ScrollBarGrip" , "Button" ) 2 3function PANEL : ScrollBarGrip () 4self : super () -- Initialize our baseclass 5end 6 7function PANEL : OnMousePressed ( x , y , but ) 8self . m_bPressed =true 9self : GetParent (): Grip ( self ) 10self : SetBGColor ( color ( 240 , 240 , 240 , 255 )) 11return true 12end 13 14function PANEL : OnMouseReleased () 15self . m_bPressed =false 16self : GetParent (): OnMouseReleased () 17end 18 19function PANEL : IsPressed () 20return self . m_bPressed 21end 22 23function PANEL : Paint ( w , h ) 24gui . skinHook ( "Paint" , "ScrollBarGrip" , self , w , h ) 25end 26 27local PANEL =class . create ( "ScrollBar" , "Panel" ) 28 29function PANEL : ScrollBar () 30self : super () -- Initialize our baseclass 31 32self : SetWidth ( 16 ) 33self . m_iOffset =0 34self . m_iScroll =0 35self . m_iCanvasSize =1 36self . m_iBarSize =1 37 38self . m_pScrollGrip =self : Add ( "ScrollBarGrip" ) 39 40self . m_pUpButton =self : Add ( "Button" ) 41self . m_pUpButton : SetText ( "" ) 42self . m_pUpButton . OnClick =function ( self ) self : GetParent (): AddScroll ( - 1 ) end 43self . m_pUpButton . Paint =function ( panel , w , h ) gui . skinHook ( "Paint" , "ScrollBarButtonUp" , panel , w , h ) end 44 45self . m_pDownButton =self : Add ( "Button" ) 46self . m_pDownButton : SetText ( "" ) 47self . m_pDownButton . OnClick =function ( self ) self : GetParent (): AddScroll ( 1 ) end 48self . m_pDownButton . Paint =function ( panel , w , h ) gui . skinHook ( "Paint" , "ScrollBarButtonDown" , panel , w , h ) end 49end 50 51function PANEL : SetEnabled ( b ) 52if not b then 53self . m_iOffset =0 54self : SetScroll ( 0 ) 55self . m_bHasChanged =true 56end 57 58self : SetVisible ( b ) 59 60-- We're probably changing the width of something in our parent 61-- by appearing or hiding, so tell them to re-do their layout. 62if self . m_bEnabled ~= b then 63self : GetParent (): InvalidateLayout () 64if self : GetParent (). OnScrollbarAppear then 65self : GetParent (): OnScrollbarAppear () 66end 67end 68 69self . m_bEnabled =b 70end 71 72function PANEL : Value () 73return self . Pos 74end 75 76function PANEL : BarScale () 77if self . m_iBarSize == 0 then return 1 end 78return self . m_iBarSize / ( self . m_iCanvasSize + self . m_iBarSize ) 79end 80 81function PANEL : SetUp ( bsize , csize ) 82self . m_iBarSize =bsize 83self . m_iCanvasSize =math . max ( csize - bsize , 1 ) 84self : SetEnabled ( csize > bsize ) 85--self:InvalidateLayout() 86end 87 88function PANEL : OnMouseWheeled ( x , y ) 89if not self : IsVisible () then return false end 90return self : AddScroll ( y * - 1 ) 91end 92 93function PANEL : AddScroll ( dir ) 94local oldScroll =self : GetScroll () 95self : SetScroll ( self : GetScroll () + ( dir * 25 )) 96return oldScroll ~= self : GetScroll () 97end 98 99function PANEL : SetScroll ( scrll ) 100if not self . m_bEnabled then self . m_iScroll =0 return end 101 102-- Clamp the scroll position between 0 and the canvas height 103self . m_iScroll =math . clamp ( scrll , 0 , self . m_iCanvasSize ) 104 105-- Manually perform a layout to position our gripper 106self : PerformLayout () 107 108-- If our parent has a OnVScroll function use that 109local func =self : GetParent (). OnVScroll 110if func then 111func ( self : GetParent (), self : GetOffset ()) 112end 113end 114 115function PANEL : GetScroll () 116if not self . m_bEnabled then self . m_iScroll =0 end 117return self . m_iScroll 118end 119 120function PANEL : GetOffset () 121if not self . m_bEnabled then return 0 end 122return self . m_iScroll * - 1 123end 124 125function PANEL : Paint ( w , h ) 126gui . skinHook ( "Paint" , "Panel" , self , w , h ) 127end 128 129function PANEL : OnMousePressed ( x , y , but ) 130if but ~= 1 then return end 131local pageSize =self . m_iBarSize 132if y > self . m_pScrollGrip . m_iPosY then 133self : SetScroll ( self : GetScroll () + pageSize ) 134else 135self : SetScroll ( self : GetScroll () - pageSize ) 136end 137return true 138end 139 140function PANEL : OnMouseReleased ( x , y , but ) 141self . m_bDragging =false 142end 143 144function PANEL : Grip ( panel ) 145if not self . m_bEnabled then return end 146if self . m_iBarSize == 0 then return end 147 148local mx , my =love . mouse . getPosition () 149local px , py =panel : GetPos () 150 151self . m_bDragging =true 152self . m_iHoldPos =my - py 153end 154 155function PANEL : Think ( dt ) 156if not self . m_bEnabled then return end 157if not self . m_bDragging then return end 158 159local trackSize =self : GetHeight () - self : GetWidth () * 2 - self . m_pScrollGrip : GetHeight () 160 161local mx , my =love . mouse . getPosition () 162my =( my - self . m_iHoldPos - self . m_pUpButton : GetHeight ()) / trackSize 163 164self : SetScroll ( my * self . m_iCanvasSize ) 165end 166 167function PANEL : PerformLayout () 168local wide =self : GetWidth () 169local scroll =self : GetScroll () / self . m_iCanvasSize 170local barsize =math . max ( self : BarScale () * ( self : GetHeight () - ( wide * 2 )), 10 ) 171local track =self : GetHeight () - ( wide * 2 ) - barsize 172track =track + 1 173 174scroll =scroll * track 175 176self . m_pScrollGrip : SetPos ( 0 , wide + scroll ) 177self . m_pScrollGrip : SetSize ( wide , barsize ) 178 179self . m_pUpButton : SetPos ( 0 , 0 ) 180self . m_pUpButton : SetSize ( wide , wide ) 181 182self . m_pDownButton : SetPos ( 0 , self : GetHeight () - wide ) 183self . m_pDownButton : SetSize ( wide , wide ) 184end