BsHandleSlider.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "BsVector2I.h"
  6. #include "BsMatrix4.h"
  7. #include "BsQuaternion.h"
  8. namespace BansheeEngine
  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] ray Ray in world space to try to interect with geometry.
  41. * @param[in] t Position of the intersection along the ray. Only if intersection happened.
  42. * @return Whether an intersection was detected.
  43. */
  44. virtual bool intersects(const Ray& ray, float& t) const = 0;
  45. /**
  46. * Updates a slider that is currently active (being dragged).
  47. *
  48. * @param[in] camera Camera through which we're interacting with the slider.
  49. * @param[in] inputDelta Pointer movement since the last time this method was called.
  50. */
  51. virtual void handleInput(const CameraPtr& camera, const Vector2I& inputDelta) = 0;
  52. /**
  53. * Updates the state of the slider. Must be called every frame.
  54. *
  55. * @param[in] camera Camera through which we're interacting with the slider.
  56. */
  57. void update(const CameraPtr& camera);
  58. /** Returns the state the slider is currently in. */
  59. State getState() const { return mState; }
  60. /**
  61. * Returns if fixed scale is enabled. If enabled the handle slider will always try to maintain the same visible
  62. * area in the viewport regardless of distance from camera.
  63. */
  64. bool getFixedScale() const { return mFixedScale; }
  65. /** Returns a layer that determines which sliders are interacted with from a specific camera. */
  66. UINT64 getLayer() const { return mLayer; }
  67. /** Sets the world position of the slider. */
  68. void setPosition(const Vector3& position);
  69. /** Sets the world rotation of the slider. */
  70. void setRotation(const Quaternion& rotation);
  71. /** Sets the scale of the slider. */
  72. void setScale(const Vector3& scale);
  73. /** Enables or disabled the slider, making it interactable or not. */
  74. void setEnabled(bool enabled);
  75. /** Gets the world position of the slider. */
  76. const Vector3& getPosition() const { return mPosition; }
  77. /** Gets the world rotation of the slider. */
  78. const Quaternion& getRotation() const { return mRotation; }
  79. /** Gets the scale of the slider. */
  80. const Vector3& getScale() const { return mScale; }
  81. /** Checks whether the slider can be interacted with or not. */
  82. bool getEnabled() const { return mEnabled; }
  83. protected:
  84. friend class HandleSliderManager;
  85. /** Toggles the slider state to inactive. */
  86. void setInactive();
  87. /**
  88. * Toggles the slider state to active.
  89. *
  90. * @param[in] camera Camera through which the slider was activated.
  91. * @param[in] pointerPos Position of the pointer when the slider was activated.
  92. */
  93. void setActive(const CameraPtr& camera, const Vector2I& pointerPos);
  94. /** Toggles the slider state to hovered. */
  95. void setHover();
  96. /** Gets the slider transform depending on set position, rotation and scale values. */
  97. const Matrix4& getTransform() const;
  98. /** Gets the inverse of the slider transform depending on set position, rotation and scale values. */
  99. const Matrix4& getTransformInv() const;
  100. /** Triggered when the slider state is changed to active. */
  101. virtual void activate(const CameraPtr& camera, const Vector2I& pointerPos) { }
  102. /** Triggered when the slider state is changed from active to some other state. */
  103. virtual void reset() { }
  104. /** Updates the internal transform from the stored position, rotation and scale values. */
  105. void updateCachedTransform() const;
  106. /**
  107. * Calculates amount of movement along the provided ray depending on pointer movement.
  108. *
  109. * @param[in] camera Camera on which the pointer movement is occurring.
  110. * @param[in] position Position of the ray to calculate movement on.
  111. * @param[in] direction Direction of the ray to calculate movement on. Must be normalized.
  112. * @param[in] pointerStart Starting position of the pointer when movement started, in pixels relative to
  113. * provided camera.
  114. * @param[in] pointerEnd Current position of the pointer, in pixels relative to provided camera.
  115. */
  116. float calcDelta(const CameraPtr& camera, const Vector3& position, const Vector3& direction,
  117. const Vector2I& pointerStart, const Vector2I& pointerEnd);
  118. bool mFixedScale;
  119. UINT64 mLayer;
  120. Vector3 mPosition;
  121. Quaternion mRotation;
  122. Vector3 mScale;
  123. float mDistanceScale;
  124. Vector2I mStartPointerPos;
  125. Vector2I mCurrentPointerPos;
  126. State mState;
  127. bool mEnabled;
  128. mutable bool mTransformDirty;
  129. mutable Matrix4 mTransform;
  130. mutable Matrix4 mTransformInv;
  131. };
  132. /** @} */
  133. }