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