Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Nibbles

making a slider control for my gui.. so confoodled...

This topic is 5780 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Basically i have made a slider control that has a value for CurrentValue, MinValue, and MaxValue (all floats). now when i set it''s properties like so: Slider.CurrentValue = 0; Slider.MinValue = 0; Slider.MaxValue = 255; and when i click on the slider handle with the mouse, i''d like to be able to drag it. now, that part does work, but i''d only like to show the percent that the slider is at. for example, if the slider is drawn to be 128 units wide, i''d like it if when Slider.CurrentValue == Slider.MaxValue, that the slider handle is at the far end of the slider (ie: 128) i really hope that made some kind of sense; i just want the slider to use percent, instead of actual pixels. I''d really appreciate some insight. Thanks, Scott
------------------------------------------------------------
Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
------------------------------------------------------------

Share this post


Link to post
Share on other sites
Advertisement
slider.handle.position = ((slider.currentValue - slider.minValue) / (slider.maxValue - slider.minValue)) * slider.width;

The first term is just a percentage - it will be a value between 0 and 1, inclusive. A value of 0 (when slider.currentValue == slider.minValue) will set the handle position at 0*slider.width, and a value of 1 (when slider.currentValue == slider.minValue) will set the handle position at 1*slider.width.

hope this helps ~
riley

Share this post


Link to post
Share on other sites
that does help, but what could i do to make slider.currentValue equal to (slider.x - mouse.x)? basically the mouse cursor''s position determines slider.currentValue while still using the formula you posted to determine it''s percentage?

thanks again!
Scott


------------------------------------------------------------
Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
------------------------------------------------------------

Share this post


Link to post
Share on other sites
To get value from position instead of position from value, you can just sort of invert that formula I posted earlier:

slider.currentValue = ((slider.currentX - slider.minX) / (slider.maxX - slider.minX)) * slider.maxValue;

The first term is just a percentage (of current x to total width), and it behaves accordingly as a multiplier for maxValue. slider.currentX == slider.minX yields 0 for currentValue, and slider.currentX == slider.maxX yeilds maxValue for currentValue.

So, to handle mouse stuff, you can set currentX to mouse.x and recalculate the value of the scrollbar with that formula.

Share this post


Link to post
Share on other sites
thank you! finally works!


------------------------------------------------------------
Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
------------------------------------------------------------

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!