UISlider.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <UI/UITexturedQuad.h>
  5. #include <UI/UIButton.h>
  6. /// Slider control with up/down button and thumb to select a value
  7. class UISlider : public UIElement
  8. {
  9. public:
  10. JPH_DECLARE_RTTI_VIRTUAL(UISlider)
  11. using ValueChangedAction = function<void(float)>;
  12. /// Properties
  13. void SetValue(float inValue) { mCurrentValue = inValue; }
  14. void SetRange(float inMin, float inMax, float inStep) { mMinValue = inMin; mMaxValue = inMax; mStepValue = inStep; }
  15. void SetDecreaseButton(UIButton *inDecreaseButton) { mDecreaseButton = inDecreaseButton; }
  16. void SetIncreaseButton(UIButton *inIncreaseButton) { mIncreaseButton = inIncreaseButton; }
  17. void SetStaticText(UIStaticText *inStaticText) { mStaticText = inStaticText; UpdateStaticText(); }
  18. void SetSlider(const UITexturedQuad &inSlider) { mSlider = inSlider; }
  19. void SetThumb(const UITexturedQuad &inThumb) { mThumb = inThumb; }
  20. void SetValueChangedAction(ValueChangedAction inAction) { mValueChangedAction = inAction; }
  21. /// Cloning / copying
  22. virtual void CopyTo(UIElement *ioElement) const override;
  23. /// Actions
  24. virtual bool MouseDown(int inX, int inY) override;
  25. virtual bool MouseUp(int inX, int inY) override;
  26. virtual bool MouseMove(int inX, int inY) override;
  27. virtual void MouseCancel() override;
  28. /// Draw element
  29. virtual void Draw() const override;
  30. /// Event handling (returns true if the event has been handled)
  31. virtual bool HandleUIEvent(EUIEvent inEvent, UIElement *inSender) override;
  32. /// Calculate auto layout
  33. virtual void AutoLayout() override;
  34. protected:
  35. /// Pixel range of slider relative to parent
  36. void GetSliderRange(int &outSliderStart, int &outSliderEnd) const;
  37. /// X coordinate of thumb start
  38. int GetThumbStart(int inSliderStart, int inSliderEnd) const;
  39. /// Internal function to update the value
  40. void SetValueInternal(float inValue);
  41. /// Update static text box
  42. void UpdateStaticText();
  43. /// Properties
  44. float mCurrentValue = 0.0f;
  45. float mMinValue = 0.0f;
  46. float mMaxValue = 1.0f;
  47. float mStepValue = 0.1f;
  48. UIButton * mDecreaseButton = nullptr;
  49. UIButton * mIncreaseButton = nullptr;
  50. UIStaticText * mStaticText = nullptr;
  51. int mSpaceBetweenButtonAndSlider = 5;
  52. UITexturedQuad mSlider;
  53. UITexturedQuad mThumb;
  54. ValueChangedAction mValueChangedAction;
  55. /// State
  56. int mThumbDragPoint = -1;
  57. };