BsHandleSlider.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #pragma once
  2. #include "BsEditorPrerequisites.h"
  3. #include "BsVector2I.h"
  4. #include "BsMatrix4.h"
  5. #include "BsQuaternion.h"
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * Base class for all handle sliders. A handle slider is geometry that the user can interact with by selecting or
  10. * dragging (i.e. sliding) it. Sliders generally output a one- or multi-dimensional delta value that signals the drag
  11. * amount (and/or direction).
  12. */
  13. class BS_ED_EXPORT HandleSlider
  14. {
  15. public:
  16. /** Possible states the slider can be in. */
  17. enum class State
  18. {
  19. Inactive, /**< Slider is not being interacted with. */
  20. Active, /**< Slider is clicked on and being dragged. */
  21. Hover /**< Slider is being hovered over but isn't clicked on. */
  22. };
  23. /**
  24. * Constructs a new handle slider.
  25. *
  26. * @param[in] fixedScale If true the handle slider will always try to maintain the same visible area in the
  27. * viewport regardless of distance from camera.
  28. * @param[in] layer Layer that allows filtering of which sliders are interacted with from a specific camera.
  29. */
  30. HandleSlider(bool fixedScale, UINT64 layer);
  31. virtual ~HandleSlider() { }
  32. /**
  33. * Attempts to find an intersection between the provided ray and the slider geometry.
  34. *
  35. * @param[in] ray Ray in world space to try to interect with geometry.
  36. * @param[in] t Position of the intersection along the ray. Only if intersection happened.
  37. * @return Whether an intersection was detected.
  38. */
  39. virtual bool intersects(const Ray& ray, float& t) const = 0;
  40. /**
  41. * Updates a slider that is currently active (being dragged).
  42. *
  43. * @param[in] camera Camera through which we're interacting with the slider.
  44. * @param[in] inputDelta Pointer movement since the last time this method was called.
  45. */
  46. virtual void handleInput(const CameraPtr& camera, const Vector2I& inputDelta) = 0;
  47. /**
  48. * Updates the state of the slider. Must be called every frame.
  49. *
  50. * @param[in] camera Camera through which we're interacting with the slider.
  51. */
  52. void update(const CameraPtr& camera);
  53. /** Returns the state the slider is currently in. */
  54. State getState() const { return mState; }
  55. /**
  56. * Returns if fixed scale is enabled. If enabled the handle slider will always try to maintain the same visible
  57. * area in the viewport regardless of distance from camera.
  58. */
  59. bool getFixedScale() const { return mFixedScale; }
  60. /** Returns a layer that determines which sliders are interacted with from a specific camera. */
  61. UINT64 getLayer() const { return mLayer; }
  62. /** Sets the world position of the slider. */
  63. void setPosition(const Vector3& position);
  64. /** Sets the world rotation of the slider. */
  65. void setRotation(const Quaternion& rotation);
  66. /** Sets the scale of the slider. */
  67. void setScale(const Vector3& scale);
  68. /** Enables or disabled the slider, making it interactable or not. */
  69. void setEnabled(bool enabled);
  70. /** Gets the world position of the slider. */
  71. const Vector3& getPosition() const { return mPosition; }
  72. /** Gets the world rotation of the slider. */
  73. const Quaternion& getRotation() const { return mRotation; }
  74. /** Gets the scale of the slider. */
  75. const Vector3& getScale() const { return mScale; }
  76. /** Checks whether the slider can be interacted with or not. */
  77. bool getEnabled() const { return mEnabled; }
  78. protected:
  79. friend class HandleSliderManager;
  80. /** Toggles the slider state to inactive. */
  81. void setInactive();
  82. /**
  83. * Toggles the slider state to active.
  84. *
  85. * @param[in] camera Camera through which the slider was activated.
  86. * @param[in] pointerPos Position of the pointer when the slider was activated.
  87. */
  88. void setActive(const CameraPtr& camera, const Vector2I& pointerPos);
  89. /** Toggles the slider state to hovered. */
  90. void setHover();
  91. /** Gets the slider transform depending on set position, rotation and scale values. */
  92. const Matrix4& getTransform() const;
  93. /** Gets the inverse of the slider transform depending on set position, rotation and scale values. */
  94. const Matrix4& getTransformInv() const;
  95. /** Triggered when the slider state is changed to active. */
  96. virtual void activate(const CameraPtr& camera, const Vector2I& pointerPos) { }
  97. /** Triggered when the slider state is changed from active to some other state. */
  98. virtual void reset() { }
  99. /** Updates the internal transform from the stored position, rotation and scale values. */
  100. void updateCachedTransform() const;
  101. /**
  102. * Calculates amount of movement along the provided ray depending on pointer movement.
  103. *
  104. * @param[in] camera Camera on which the pointer movement is occurring.
  105. * @param[in] position Position of the ray to calculate movement on.
  106. * @param[in] direction Direction of the ray to calculate movement on. Must be normalized.
  107. * @param[in] pointerStart Starting position of the pointer when movement started, in pixels relative to
  108. * provided camera.
  109. * @param[in] pointerEnd Current position of the pointer, in pixels relative to provided camera.
  110. */
  111. float calcDelta(const CameraPtr& camera, const Vector3& position, const Vector3& direction,
  112. const Vector2I& pointerStart, const Vector2I& pointerEnd);
  113. bool mFixedScale;
  114. UINT64 mLayer;
  115. Vector3 mPosition;
  116. Quaternion mRotation;
  117. Vector3 mScale;
  118. float mDistanceScale;
  119. Vector2I mStartPointerPos;
  120. Vector2I mCurrentPointerPos;
  121. State mState;
  122. bool mEnabled;
  123. mutable bool mTransformDirty;
  124. mutable Matrix4 mTransform;
  125. mutable Matrix4 mTransformInv;
  126. };
  127. }