fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
a8034e9
v2.1.4
1local PANEL =class . create ( "HorizontalSelect" , "Label" ) 2 3PANEL : ACCESSOR ( "Selection" , "m_iSelection" , 1 ) 4 5function PANEL : HorizontalSelect () 6self : super () -- Initialize our baseclass 7 8self : SetDrawPanel ( true ) 9self : SetFocusable ( true ) 10self : DockPadding ( 0 , 0 , 0 , 0 ) 11self : SetBGColor ( color_blank ) 12self : SetBorderColor ( color_blank ) 13self : SetTextAlignmentX ( "center" ) 14 15self . m_tOptions ={} 16 17self . m_pButLeft =self : Add ( "Button" ) 18--self.m_pButLeft:SetDrawLabel(false) 19self . m_pButLeft : SetText ( "<" ) 20self . m_pButLeft : Dock ( DOCK_LEFT ) 21self . m_pButLeft : SetWidth ( 20 ) 22function self . m_pButLeft : OnClick () 23self : GetParent (): SelectLeft () 24end 25 26self . m_pButRight =self : Add ( "Button" ) 27--self.m_pButRight:SetDrawLabel(false) 28self . m_pButRight : SetText ( ">" ) 29self . m_pButRight : Dock ( DOCK_RIGHT ) 30self . m_pButRight : SetWidth ( 20 ) 31function self . m_pButRight : OnClick () 32self : GetParent (): SelectRight () 33end 34end 35 36function PANEL : Skin () 37gui . skinHook ( "Init" , "HorizontalSelect" , self ) 38end 39 40-- Override base class SetEnabled with our own 41-- Toggle the left/right buttons at the same time 42function PANEL : SetEnabled ( b ) 43self . m_bEnabled =b 44self . m_pButLeft : SetEnabled ( b ) 45self . m_pButRight : SetEnabled ( b ) 46end 47 48function PANEL : Paint ( w , h ) 49self : super ( "Paint" , w , h ) -- Paint our label 50end 51 52function PANEL : PaintOverlay ( w , h ) 53gui . skinHook ( "PaintOverlay" , "FocusPanel" , self , w , h ) 54end 55 56function PANEL : AddOption ( str , default ) 57table . insert ( self . m_tOptions , str ) 58if default then 59self : SelectOption ( # self . m_tOptions ) 60end 61self : UpdateSelection () 62return # self . m_tOptions 63end 64 65function PANEL : SelectOption ( num , force ) 66if self . m_iSelection == num and not force then return end 67self . m_iSelection =num 68self : UpdateSelection () 69self : OnSelectOption ( self . m_iSelection ) 70end 71 72function PANEL : OnSelectOption ( num ) 73-- Override 74end 75 76function PANEL : OnMousePressed ( x , y , but ) 77end 78 79function PANEL : UpdateSelection () 80self : SetText ( self . m_tOptions [ self . m_iSelection ]) 81self . m_pButLeft : SetEnabled ( self . m_iSelection > 1 ) 82self . m_pButRight : SetEnabled ( self . m_iSelection < # self . m_tOptions ) 83end 84 85function PANEL : SelectLeft () 86self : SelectOption ( math . max ( 1 , self . m_iSelection - 1 )) 87end 88 89function PANEL : SelectRight () 90self : SelectOption ( math . min ( # self . m_tOptions , self . m_iSelection + 1 )) 91end 92 93function PANEL : OnMouseWheeled ( x , y ) 94if not self : HasFocus () or not self : IsEnabled () then return end 95if y > 0 then 96self : SelectLeft () 97else 98self : SelectRight () 99end 100return true 101end