fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
7180fb3
settings-overhaul
1local PANEL =class . create ( "ScrollBarGrip" , "Panel" ) 2 3function PANEL : OnMousePressed ( x , y , but ) 4self . m_bPressed =true 5self : GetParent (): Grip ( self ) 6self : SetBGColor ( color ( 240 , 240 , 240 , 255 )) 7return true 8end 9 10function PANEL : OnMouseReleased () 11self . m_bPressed =false 12self : GetParent (): OnMouseReleased () 13end 14 15function PANEL : IsPressed () 16return self . m_bPressed 17end 18 19function PANEL : Paint ( w , h ) 20gui . skinHook ( "Paint" , "ScrollBarGrip" , self , w , h ) 21end 22 23local PANEL =class . create ( "ScrollBar" , "Panel" ) 24 25function PANEL : ScrollBar () 26self : super () -- Initialize our baseclass 27 28self : SetWidth ( 16 ) 29self . m_iOffset =0 30self . m_iScroll =0 31self . m_iCanvasSize =1 32self . m_iBarSize =1 33 34self . m_pScrollGrip =self : Add ( "ScrollBarGrip" ) 35 36self . m_pUpButton =self : Add ( "Button" ) 37self . m_pUpButton : SetText ( "" ) 38self . m_pUpButton . OnClick =function ( self ) self : GetParent (): AddScroll ( - 1 ) end 39self . m_pUpButton . Paint =function ( panel , w , h ) gui . skinHook ( "Paint" , "ScrollBarButtonUp" , panel , w , h ) end 40 41self . m_pDownButton =self : Add ( "Button" ) 42self . m_pDownButton : SetText ( "" ) 43self . m_pDownButton . OnClick =function ( self ) self : GetParent (): AddScroll ( 1 ) end 44self . m_pDownButton . Paint =function ( panel , w , h ) gui . skinHook ( "Paint" , "ScrollBarButtonDown" , panel , w , h ) end 45end 46 47function PANEL : SetEnabled ( b ) 48if not b then 49self . m_iOffset =0 50self : SetScroll ( 0 ) 51self . m_bHasChanged =true 52end 53 54self : SetVisible ( b ) 55 56-- We're probably changing the width of something in our parent 57-- by appearing or hiding, so tell them to re-do their layout. 58if self . m_bEnabled ~= b then 59self : GetParent (): InvalidateLayout () 60if self : GetParent (). OnScrollbarAppear then 61self : GetParent (): OnScrollbarAppear () 62end 63end 64 65self . m_bEnabled =b 66end 67 68function PANEL : Value () 69return self . Pos 70end 71 72function PANEL : BarScale () 73if self . m_iBarSize == 0 then return 1 end 74return self . m_iBarSize / ( self . m_iCanvasSize + self . m_iBarSize ) 75end 76 77function PANEL : SetUp ( bsize , csize ) 78self . m_iBarSize =bsize 79self . m_iCanvasSize =math . max ( csize - bsize , 1 ) 80self : SetEnabled ( csize > bsize ) 81self : InvalidateLayout () 82end 83 84function PANEL : OnMouseWheeled ( x , y ) 85if not self : IsVisible () then return false end 86return self : AddScroll ( y * - 1 ) 87end 88 89function PANEL : AddScroll ( dir ) 90local oldScroll =self : GetScroll () 91self : SetScroll ( self : GetScroll () + ( dir * 25 )) 92return oldScroll ~= self : GetScroll () 93end 94 95function PANEL : SetScroll ( scrll ) 96if not self . m_bEnabled then self . m_iScroll =0 return end 97 98self . m_iScroll =math . clamp ( scrll , 0 , self . m_iCanvasSize ) 99 100self : InvalidateLayout () 101 102-- If our parent has a OnVScroll function use that, if 103-- not then invalidate layout (which can be pretty slow) 104local func =self : GetParent (). OnVScroll 105if func then 106func ( self : GetParent (), self : GetOffset ()) 107else 108self : GetParent (): InvalidateLayout () 109end 110end 111 112function PANEL : GetScroll () 113if not self . m_bEnabled then self . m_iScroll =0 end 114return self . m_iScroll 115end 116 117function PANEL : GetOffset () 118if not self . m_bEnabled then return 0 end 119return self . m_iScroll * - 1 120end 121 122function PANEL : Paint ( w , h ) 123gui . skinHook ( "Paint" , "Panel" , self , w , h ) 124end 125 126function PANEL : OnMousePressed ( x , y , but ) 127if but ~= 1 then return end 128local PageSize =self . m_iBarSize 129if y > self . m_pScrollGrip . m_iPosY then 130self : SetScroll ( self : GetScroll () + PageSize ) 131else 132self : SetScroll ( self : GetScroll () - PageSize ) 133end 134return true 135end 136 137function PANEL : OnMouseReleased ( x , y , but ) 138self . m_bDragging =false 139self . DraggingCanvas =nil 140self . m_pScrollGrip . Depressed =false 141end 142 143function PANEL : Grip ( panel ) 144if not self . m_bEnabled then return end 145if self . m_iBarSize == 0 then return end 146 147local mx , my =love . mouse . getPosition () 148local px , py =panel : GetPos () 149 150self . m_bDragging =true 151self . HoldPos =my - py 152self . m_pScrollGrip . Depressed =true 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 . HoldPos - 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