Game Development Community

GuiSliderCtrl - add a auto-snap-to default value

by Orion Elenzil · 07/03/2006 (11:27 am) · 2 comments

add new variables to the class:
GuiSliderCtrl.h
add this just below "F32 mValue;":
F32  mDefaultValue;                 // optional
   F32  mSnapToDefaultRangePercent;    // eg "0.1" means snap-to-default when within the 10% of mRange centered on mDefaultValue. 0 means no snapping.

initialize the new variables:
GuiSliderCtrl.cc
add this just below "mValue = 0.5f;":
mDefaultValue              = mValue;
   mSnapToDefaultRangePercent = 0;

expose the new variables to script:
GuiSliderCtrl.cc
add this just below " addField("displayValue"....":
addField("defaultValue",              TypeF32,     Offset(mDefaultValue,              GuiSliderCtrl));
   addField("snapToDefaultRangePercent", TypeF32,     Offset(mSnapToDefaultRangePercent, GuiSliderCtrl));


and finally do something with the new variables:
GuiSliderCtrl.cc
after the block which opens with "if ((event.modifier & SI_SHIFT) && mTicks > 2) {",
add this else statement:
else if (mSnapToDefaultRangePercent > 0.0) {
      // check for snapping to default value.
      Point2F defaultZone;
      F32 rangeRadius = mFabs(mRange.y - mRange.x) * 0.5f * mSnapToDefaultRangePercent;
      defaultZone.x   = mDefaultValue - rangeRadius;
      defaultZone.y   = mDefaultValue + rangeRadius;

      if (value > defaultZone.x && value < defaultZone.y) {
         value = mDefaultValue;
         AssertFatal(value <= mRange.y && value >= mRange.x, "Error, out of bounds value generated from snap-to-default of slider");
      }
   }


.. it's that easy!

example usage from a .gui:
myAwesomeInterface.gui
new GuiSliderCtrl(BrightnessSlider) {
         profile = "SliderProfile";
         horizSizing = "right";
         vertSizing = "bottom";
         position = "50 121";
         extent = "109 21";
         minExtent = "8 2";
         visible = "1";
         variable = "0";
         altCommand = "BrightnessSlider.applySettings();";
         range = "0.000000 1.000000";
         ticks = "10";
         value = "0.4";[b]
         defaultValue = "0.4";
         snapToDefaultRangePercent = "0.1";[/b]
         displayValue = "0";
      };

#1
11/05/2006 (1:45 pm)
this is a really nice little feature I noticed in the Lounge Orion, didn't realize you made it happen.