fork/m-overlay
git clone https://git.julia.blue/fork/m-overlay
27686d7
settings-overhaul
1local PANEL =class . create ( "Slider" , "Panel" ) 2 3PANEL . NotchColor =color ( 100 , 100 , 100 ) 4PANEL . GrabberColor =color ( 0 , 162 , 232 ) 5 6PANEL : ACCESSOR ( "Min" , "m_iMin" , 0 ) -- minumum allowed value 7PANEL : ACCESSOR ( "Max" , "m_iMax" , 100 ) -- maximum allowed value 8PANEL : ACCESSOR ( "Value" , "m_iValue" , 0 ) -- starting value 9PANEL : ACCESSOR ( "Steps" , "m_iSteps" , 1 ) -- steps/max = number of potential values 10PANEL : ACCESSOR ( "Increments" , "m_iIncrements" , 5 ) -- "minor" notches 11PANEL : ACCESSOR ( "Notches" , "m_iNotches" , 4 ) -- "major" notches 12 13function PANEL : Slider () 14self : super () -- Initialize our baseclass 15self : SetWidth ( 128 ) 16 17self : SetFocusable ( true ) 18 19self . m_bGrabbed =false 20end 21 22function PANEL : Paint ( w , h ) 23gui . skinHook ( "Paint" , "Slider" , self , w , h ) 24 25-- Draw a line down the middle 26graphics . setColor ( self . NotchColor ) 27graphics . line ( 0 , h / 2 , w , h / 2 ) 28 29local x =0 30 31if self . m_iIncrements > 0 then 32local range =self : GetRange () 33 34local increments =( range / self . m_iIncrements ) 35local gap =w / increments 36 37for i =1 , increments - 1 do 38x =0 + gap * i 39graphics . line ( x , 8 , x , h - 8 ) 40end 41end 42 43if self . m_iNotches > 0 then 44gap =w / self . m_iNotches 45 46for i =1 , self . m_iNotches - 1 do 47x =0 + gap * i 48graphics . line ( x , 4 , x , h - 4 ) 49end 50end 51end 52 53function PANEL : PaintOverlay ( w , h ) 54gui . skinHook ( "PaintOverlay" , "FocusPanel" , self , w , h ) 55 56local perct =( self . m_iValue - self . m_iMin ) / ( self . m_iMax - self . m_iMin ) 57local xpos =perct * w 58 59graphics . setColor ( self . GrabberColor ) 60graphics . rectangle ( "fill" , xpos - 4 , 4 , 8 , h - 8 ) 61 62graphics . setColor ( color_black ) 63graphics . rectangle ( "line" , xpos - 4 , 4 , 8 , h - 8 ) 64end 65 66function PANEL : SetValue ( i ) 67-- Round value to nearest step 68local value =math . floor (( i / self . m_iSteps ) + 0.5 ) * self . m_iSteps 69-- Clamp value to min/max 70self . m_iValue =math . min ( self . m_iMax , math . max ( self . m_iMin , value )) 71self : OnValueChanged ( self . m_iValue ) 72end 73 74function PANEL : GetValue () 75return self . m_iValue 76end 77 78function PANEL : GetRange () 79return self . m_iMax - self . m_iMin 80end 81 82function PANEL : SetValueFromMouseX ( x ) 83local w =self : GetWidth () 84local range =self : GetRange () 85self : SetValue ( math . floor ( self . m_iMin + (( x / w ) * range ) + 0.5 )) 86end 87 88function PANEL : OnMousePressed ( x , y , but ) 89if not self : IsEnabled () or but ~= 1 then return end 90 91local w =self : GetWidth () 92 93local perct =( self . m_iValue - self . m_iMin ) / ( self . m_iMax - self . m_iMin ) 94local xpos =perct * w 95 96if x <= xpos + 4 and x >= xpos - 4 then 97self . m_bGrabbed =true 98else 99self : SetValueFromMouseX ( x ) 100end 101return true 102end 103 104function PANEL : OnMouseMoved ( x , y , dx , dy , istouch ) 105if self . m_bGrabbed then 106self : SetValueFromMouseX ( x ) 107end 108end 109 110function PANEL : OnMouseWheeled ( x , y ) 111if not self : IsEnabled () then return end 112self : SetValue ( self . m_iValue + ( y * self . m_iSteps )) 113return true 114end 115 116function PANEL : OnMouseReleased ( x , y , but ) 117if not self : IsEnabled () or but ~= 1 then return end 118if self . m_bGrabbed then 119self . m_bGrabbed =false 120end 121end 122 123function PANEL : OnValueChanged ( i ) 124 125end