(maxgui) center slider cursor
BlitzMax Forums/BlitzMax Beginners Area/(maxgui) center slider cursor
| ||
| Hi ! when slider was created, what's the formulae to center the cursor (knob) ? (using setslidervalue). This is 'maxslidervalue' / 2 but is there a command to access or compute this value ? Thanks ! |
| ||
| SetSliderValue(slider:TGadget,value)? |
| ||
| i know setslidervalue() function. I'm searching how to determine the value to center the knob. |
| ||
| It depends on your slider range. Could you make a plain example of your slider. So we have something to work with. |
| ||
| Min+(Max-Min)/2 |
| ||
some code sample to explain more.
Strict
Local window:TGadget=CreateWindow("My Window",0,0,240,280,,WINDOW_TITLEBAR)
Global Panel : TGadget = CreatePanel:TGadget (10,10,200,200,window)
Global Canvas : TGadget = CreateCanvas (0,0,340,240,Panel)
Local sv : Tgadget = CreateSlider(210,10,16,200,window,SLIDER_VERTICAL)
Local sh : Tgadget = CreateSlider(10 , 210 , 200 , 16 , window , SLIDER_HORIZONTAL)
SetSliderRange sh , GadgetWidth(Panel) , GadgetWidth(Canvas)
SetSliderRange sv , GadgetHeight(Panel) , GadgetHeight(Canvas)
'SetSliderValue sh, ? ' how to determine value to center the knob
'SetSliderValue sv, ? ' how to determine value to center the knob
Local t:TTimer = CreateTimer(60)
While WaitEvent()
Print CurrentEvent.ToString()
Select EventID()
Case EVENT_TIMERTICK
SetGadgetShape (Canvas, -SliderValue (sh), -SliderValue (sv), GadgetWidth(Canvas), GadgetHeight(Canvas))
SetGraphics CanvasGraphics(Canvas)
Cls
DrawLine 0 , 0 , GadgetWidth(canvas) , GadgetHeight(canvas)
DrawLine 0, GadgetHeight(Canvas),GadgetWidth(canvas),0
Flip
Case EVENT_WINDOWCLOSE
End
End Select
Wend
|
| ||
| hope someone could help me with this ! i've tested setslider value, sh, (GadgetWidth(Panel)/GadgetWidth(canvas)) * 50, but it's not the answer ! in fact i don't know what'is the value computed by setsliderrange. ratio = 'GetSliderRange' sh , GadgetWidth(Panel) , GadgetWidth(Canvas) setslidervalue sh, ratio * 50 |
| ||
This seems to work: finding the range, calculating its center, and setting the slider values to half of that. (I think that TomToad was onto it, but I tried it hands-on rather than theoretical <g>.)Strict
Local window:TGadget=CreateWindow("My Window",300,100,240,280,,WINDOW_TITLEBAR)
Global Panel : TGadget = CreatePanel:TGadget (10,10,200,200,window)
Global Canvas : TGadget = CreateCanvas (0,0,340,240,Panel)
Local sv : Tgadget = CreateSlider(210,10,16,200,window,SLIDER_VERTICAL)
Local sh : Tgadget = CreateSlider(10 , 210 , 200 , 16 , window , SLIDER_HORIZONTAL)
Local h0 = GadgetWidth(Panel)
Local h1 = GadgetWidth(Canvas)
Local v0 = GadgetHeight(Panel)
Local v1 = GadgetHeight(Canvas)
Print "h/v ranges= " + h0 + "," + h1 + " / " + v0 + "," + v1
SetSliderRange sh , h0 , h1
SetSliderRange sv , v0 , v1
' calculate the the max values possible:
Local hmax = h1 - h0
Local vMax = v1 - v0
Print "hmax = " + hmax
Print "vmax = " + vmax
' set to center of range:
SetSliderValue sh, hmax/2
SetSliderValue sv, vmax/2
Local t:TTimer = CreateTimer(60)
Local oldh, oldv ' for printing values
While WaitEvent()
' print current values
If ( oldh <> SliderValue(sh) )
Print "sh = " + SliderValue(sh) + ", " + "range = " + h0 + " to " + h1
oldh = SliderValue(sh)
EndIf
If ( oldv <> SliderValue(sv) )
Print "sv = " + SliderValue(sv) + ", " + "range = " + v0 + " to " + v1
oldv = SliderValue(sv)
EndIf
'Print CurrentEvent.ToString()
Select EventID()
Case EVENT_TIMERTICK
SetGadgetShape (Canvas, -SliderValue (sh), -SliderValue (sv), GadgetWidth(Canvas), GadgetHeight(Canvas))
SetGraphics CanvasGraphics(Canvas)
Cls
DrawLine 0 , 0 , GadgetWidth(canvas) , GadgetHeight(canvas)
DrawLine 0, GadgetHeight(Canvas),GadgetWidth(canvas),0
Flip
Case EVENT_WINDOWCLOSE
End
End Select
Wend
It's the way that sliders calulate position that's a bit tricky. It's how far it is along from min to max, adjusted to make min into 0 (rather than where far left = min and far right = max). |
| ||
Too slow, but here's my code...
Strict
Local window:TGadget=CreateWindow("My Window",0,0,240,280,,WINDOW_TITLEBAR)
Global Panel : TGadget = CreatePanel:TGadget (10,10,200,200,window)
Global Canvas : TGadget = CreateCanvas (0,0,340,240,Panel)
Local sv : Tgadget = CreateSlider(210,10,16,200,window,SLIDER_VERTICAL)
Local sh : Tgadget = CreateSlider(10 , 210 , 200 , 16 , window , SLIDER_HORIZONTAL)
SetSliderRange sh , GadgetWidth(Panel) , GadgetWidth(Canvas)
SetSliderRange sv , GadgetHeight(Panel) , GadgetHeight(Canvas)
SetSliderValue sh, (GadgetWidth(Canvas)-GadgetWidth(Panel))/2
SetSliderValue sv, (GadgetHeight(Canvas)-GadgetHeight(Panel))/2
Local t:TTimer = CreateTimer(60)
While WaitEvent()
'Print CurrentEvent.ToString()
Select EventID()
Case EVENT_TIMERTICK
Print "SH: "+SliderValue(sh)+" and SV: "+SliderValue(sv)
SetGadgetShape (Canvas, -SliderValue (sh), -SliderValue (sv), GadgetWidth(Canvas), GadgetHeight(Canvas))
SetGraphics CanvasGraphics(Canvas)
Cls
DrawLine 0 , 0 , GadgetWidth(canvas) , GadgetHeight(canvas)
DrawLine 0, GadgetHeight(Canvas),GadgetWidth(canvas),0
Flip
Case EVENT_WINDOWCLOSE
End
End Select
Wend
P.S.: To speed this up, store the canvas size and panel size values in different vars. So you don't need to call the functions dozens of times every loop. |
| ||
| Many thanks for your code samples ! i've used Grisu code into my program. |