BsHandleSlider.h 5.9 KB

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