BsHandleSlider.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. */
  32. HandleSlider(bool fixedScale);
  33. virtual ~HandleSlider() { }
  34. /**
  35. * @brief Attempts to find an intersection between the provided ray and the slider geometry.
  36. *
  37. * @param ray Ray in world space to try to interect with geometry.
  38. * @param t Position of the intersection along the ray. Only if intersection happened.
  39. *
  40. * @return Whether an intersection was detected.
  41. */
  42. virtual bool intersects(const Ray& ray, float& t) const = 0;
  43. /**
  44. * @brief Updates a slider that is currently active (being dragged).
  45. *
  46. * @param camera Camera through which we're interacting with the slider.
  47. * @param inputDelta Pointer movement since the last time this method was called.
  48. */
  49. virtual void handleInput(const CameraPtr& camera, const Vector2I& inputDelta) = 0;
  50. /**
  51. * @brief Updates the state of the slider. Must be called every frame.
  52. *
  53. * @param camera Camera through which we're interacting with the slider.
  54. */
  55. void update(const CameraPtr& camera);
  56. /**
  57. * @brief Returns the state the slider is currently in.
  58. */
  59. State getState() const { return mState; }
  60. /**
  61. * @brief Returns if fixed scale is enabled. If enabled the handle slider will
  62. * always try to maintain the same visible area in the viewport regardless
  63. * of distance from camera.
  64. */
  65. bool getFixedScale() const { return mFixedScale; }
  66. /**
  67. * @brief Sets the world position of the slider.
  68. */
  69. void setPosition(const Vector3& position);
  70. /**
  71. * @brief Sets the world rotation of the slider.
  72. */
  73. void setRotation(const Quaternion& rotation);
  74. /**
  75. * @brief Sets the scale of the slider.
  76. */
  77. void setScale(const Vector3& scale);
  78. /**
  79. * @brief Gets the world position of the slider.
  80. */
  81. const Vector3& getPosition() const { return mPosition; }
  82. /**
  83. * @brief Gets the world rotation of the slider.
  84. */
  85. const Quaternion& getRotation() const { return mRotation; }
  86. /**
  87. * @brief Gets the scale of the slider.
  88. */
  89. const Vector3& getScale() const { return mScale; }
  90. protected:
  91. friend class HandleSliderManager;
  92. /**
  93. * @brief Toggles the slider state to inactive.
  94. */
  95. void setInactive();
  96. /**
  97. * @brief Toggles the slider state to active.
  98. *
  99. * @param camera Camera through which the slider was activated.
  100. * @param pointerPos Position of the pointer when the slider was activated.
  101. */
  102. void setActive(const CameraPtr& camera, const Vector2I& pointerPos);
  103. /**
  104. * @brief Toggles the slider state to hovered.
  105. */
  106. void setHover();
  107. /**
  108. * @brief Gets the slider transform depending on set position, rotation and scale values.
  109. */
  110. const Matrix4& getTransform() const;
  111. /**
  112. * @brief Gets the inverse of the slider transform depending on
  113. * set position, rotation and scale values.
  114. */
  115. const Matrix4& getTransformInv() const;
  116. /**
  117. * @brief Triggered when the slider state is changed to active.
  118. */
  119. virtual void activate(const CameraPtr& camera, const Vector2I& pointerPos) { }
  120. /**
  121. * @brief Triggered when the slider state is changed from active to some other state.
  122. */
  123. virtual void reset() { }
  124. /**
  125. * @brief Updates the internal transform from the stored position, rotation and scale values.
  126. */
  127. void updateCachedTransform() const;
  128. /**
  129. * @brief Calculates amount of movement along the provided ray depending on pointer movement.
  130. *
  131. * @param camera Camera on which the pointer movement is occurring.
  132. * @param position Position of the ray to calculate movement on.
  133. * @param direction Direction of the ray to calculate movement on. Must be normalized.
  134. * @param pointerStart Starting position of the pointer when movement started, in pixels relative to provided camera.
  135. * @param pointerEnd Current position of the pointer, in pixels relative to provided camera.
  136. */
  137. float calcDelta(const CameraPtr& camera, const Vector3& position, const Vector3& direction,
  138. const Vector2I& pointerStart, const Vector2I& pointerEnd);
  139. bool mFixedScale;
  140. Vector3 mPosition;
  141. Quaternion mRotation;
  142. Vector3 mScale;
  143. float mDistanceScale;
  144. Vector2I mStartPointerPos;
  145. Vector2I mCurrentPointerPos;
  146. State mState;
  147. mutable bool mTransformDirty;
  148. mutable Matrix4 mTransform;
  149. mutable Matrix4 mTransformInv;
  150. };
  151. }