BsHandleSlider.h 5.8 KB

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