BsHandleSlider.h 5.6 KB

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