UISlider.h 2.5 KB

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