October 17, 2018, 10:36:42 PM

Author Topic: My slider control code not working properly .  (Read 219 times)

Offline NRJ

  • Jr. Member
  • **
  • Posts: 74
  • English is not my mother tongue...
My slider control code not working properly .
« on: July 16, 2018, 09:34:48 AM »
Below is the code for the slider control ,but it is not working properly ,if I move the mouse quickly ,it looses the control over the slider circle .Any better way to achieve this ?






Code: [Select]
Graphics 800,600
AutoMidHandle True

Global mp:TImage=LoadImage("pointer.png")
Global sliderframe:TImage=LoadImage("sliderframe.png")
Global scntrl:TImage=LoadImage("scntrl.png")

Global px:Int=166
Global mx:Int,my:Int
 

HideMouse()
While Not KeyHit(key_escape)
Cls
SetBlend alphablend
mx=MouseX()
my=MouseY()

draw_slider()
update_slider()

DrawText px,10,10
DrawImage mp,mx,my
Flip
Wend
End

Function draw_slider()
DrawImage sliderframe,400,300
DrawImage scntrl,px,300
End Function

Function update_slider()
If MouseDown(1) Then
If ImagesCollide(mp,mx,my,0,scntrl,px,300,0) Then
px=MouseX()

If px<166 Then px=166
If px>634 Then px=634
EndIf
EndIf
End Function
IF YOU CAN DREAM IT,  YOU CAN DO IT...

http://www.nksoftgames.blogspot.com

Offline TomToad

  • Sr. Member
  • ****
  • Posts: 327
Re: My slider control code not working properly .
« Reply #1 on: July 16, 2018, 10:41:56 AM »
This is because the mouse moves further in one frame than the width of the slider, so ImagesCollide() fails.  You need to set a flag to detect when the mouse is held down.

Code: BlitzMax
  1. Graphics 800,600
  2. AutoMidHandle True
  3.  
  4. Global mp:TImage=LoadImage("pointer.png")
  5. Global sliderframe:TImage=LoadImage("sliderframe.png")
  6. Global scntrl:TImage=LoadImage("scntrl.png")
  7.  
  8. Global px:Int=166
  9. Global mx:Int,my:Int
  10.  
  11.  
  12. HideMouse()
  13. While Not KeyHit(key_escape)
  14. Cls
  15. SetBlend alphablend
  16. mx=MouseX()
  17. my=MouseY()
  18.  
  19. draw_slider()
  20. update_slider()
  21.  
  22. DrawText px,10,10
  23. DrawImage mp,mx,my
  24. Flip
  25. Wend
  26. End
  27.  
  28. Function draw_slider()
  29.         DrawImage sliderframe,400,300
  30.         DrawImage scntrl,px,300
  31. End Function
  32.  
  33. Function update_slider()
  34.         Global MouseFlag:Int = False
  35.        
  36.         If MouseFlag
  37.                 If MouseDown(1) Then
  38.                         px=MouseX()
  39.  
  40.                         If px<166 Then px=166
  41.                         If px>634 Then px=634
  42.                 Else
  43.                         MouseFlag = False
  44.                 EndIf
  45.         Else
  46.                 If MouseDown(1) And ImagesCollide(mp,mx,my,0,scntrl,px,300,0)
  47.                         MouseFlag = True
  48.                 EndIf
  49.         EndIf
  50. End Function
  51.  
------------------------------------------------
8 rabbits equals 1 rabbyte.

Offline Holzchopf

  • Full Member
  • ***
  • Posts: 112
Re: My slider control code not working properly .
« Reply #2 on: July 16, 2018, 10:43:40 AM »
Ah yes, the typical mouse-leaves-handle problem.

Simplest solution:

1) Add a global that tells if the slider is currently grabbed or not. E.g: Global slidergrabbed:Int
2) Set this global to true, when you first start grabbing the slider handle.
3) Change the expression that checks if the slider is grabbed, so that not only colliding the mouse with the handle fires true but also when slidergrabbed is.
4) Unset slidergrabbed whenever you want the slider to stop respond. This could be the case when another gadget gets focus, the program is suspended or at least when the mouse is released, e.g:
Code: [Select]
Function update_slider()
If MouseDown(1) Then
If ImagesCollide(mp,mx,my,0,scntrl,px,300,0) Or slidergrabbed Then
slidergrabbed = True
px=MouseX()

If px<166 Then px=166
If px>634 Then px=634
Endif
Else
slidergrabbed = False
EndIf
End Function


Offline NRJ

  • Jr. Member
  • **
  • Posts: 74
  • English is not my mother tongue...
Re: My slider control code not working properly .
« Reply #3 on: July 16, 2018, 03:23:01 PM »
Thank you TomToad and Holzchopf ,now it is working fine.
IF YOU CAN DREAM IT,  YOU CAN DO IT...

http://www.nksoftgames.blogspot.com

Offline Derron

  • Hero Member
  • *****
  • Posts: 1354
Re: My slider control code not working properly .
« Reply #4 on: July 16, 2018, 09:09:17 PM »
@ globals

GUI widgets should not need globals for individual widget stuff. Widgets need to know whether they are focused (eg. mouse clicked in input field, or slider is active - means a cursor-right-left should move the slider handle...). They also need to know about click events (eg. one clicked on the gauge of the slider widget to directly jump to a value).
Sliders need to store if their handle is pressed now. So this sounds similar to what holzchopf wrote. BUT ... you do not need to have a "is a slider handle pressed" global, nope - you need to understand what it means "input wise". If your widget handled the "mouse is down" then _this_ should be marked somewhere so to avoid that further processed widgets are also reacting to a kept-down mouse button (imagine you are pressing the slider handle and then are moving the mouse cursor over another button - or another slider).

So instead of requiring other widgets (or a "widget manager"-master) to know about the slider class itself ("if not sliderhandlepressed then ...") you handle the input - which was the original source of the "pressed handle"-action happening in this logic cycle/loop. This allows for widgets to stay independend from each other - or from polluting a "main type" with widget-specific stuff.


bye
Ron