BsSceneViewHandler.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "BsSceneViewHandler.h"
  2. #include "BsRendererManager.h"
  3. #include "BsRenderer.h"
  4. #include "BsGizmoManager.h"
  5. #include "BsHandleManager.h"
  6. #include "BsSceneGrid.h"
  7. #include "BsSelection.h"
  8. #include "BsScenePicking.h"
  9. #include "BsCamera.h"
  10. #include "BsEditorApplication.h"
  11. #include "BsEditorWidget.h"
  12. #include "BsEditorWindowBase.h"
  13. #include "BsRenderWindow.h"
  14. #include "BsCursor.h"
  15. #include "BsSelectionRenderer.h"
  16. #include "BsDebug.h"
  17. using namespace std::placeholders;
  18. namespace BansheeEngine
  19. {
  20. SceneViewHandler::SceneViewHandler(EditorWidgetBase* parentWidget, const SPtr<Camera>& camera)
  21. :mCamera(camera), mSceneGrid(nullptr), mParentWidget(parentWidget)
  22. {
  23. mSelectionRenderer = bs_new<SelectionRenderer>();
  24. mSceneGrid = bs_new<SceneGrid>(mCamera);
  25. mSceneGrid->setSettings(gEditorApplication().getEditorSettings());
  26. HandleManager::instance().setSettings(gEditorApplication().getEditorSettings());
  27. }
  28. SceneViewHandler::~SceneViewHandler()
  29. {
  30. bs_delete(mSceneGrid);
  31. bs_delete(mSelectionRenderer);
  32. if (GizmoManager::isStarted()) // If not active, we don't care
  33. GizmoManager::instance().clearRenderData();
  34. }
  35. void SceneViewHandler::update()
  36. {
  37. GizmoManager::instance().update(mCamera);
  38. mSceneGrid->update();
  39. }
  40. void SceneViewHandler::updateHandle(const Vector2I& position, const Vector2I& delta)
  41. {
  42. // If mouse wrapped around last frame then we need to compensate for the jump amount
  43. Vector2I realDelta = delta - mMouseDeltaCompensate;
  44. mMouseDeltaCompensate = Vector2I::ZERO;
  45. if (HandleManager::instance().isHandleActive())
  46. mMouseDeltaCompensate = wrapCursorToWindow();
  47. HandleManager::instance().update(mCamera, position, realDelta);
  48. }
  49. void SceneViewHandler::updateSelection()
  50. {
  51. // Call this after handle update to ensure its drawn at the right place
  52. mSelectionRenderer->update(mCamera);
  53. }
  54. void SceneViewHandler::trySelectHandle(const Vector2I& position)
  55. {
  56. HandleManager::instance().trySelect(mCamera, position);
  57. }
  58. bool SceneViewHandler::isHandleActive() const
  59. {
  60. return HandleManager::instance().isHandleActive();
  61. }
  62. void SceneViewHandler::clearHandleSelection()
  63. {
  64. HandleManager::instance().clearSelection();
  65. }
  66. void SceneViewHandler::pickObject(const Vector2I& position, bool additive)
  67. {
  68. // TODO - Handle multi-selection (i.e. selection rectangle when dragging)
  69. HSceneObject pickedObject = ScenePicking::instance().pickClosestObject(mCamera, position, Vector2I(1, 1));
  70. if (pickedObject)
  71. {
  72. if (additive) // Append to existing selection
  73. {
  74. Vector<HSceneObject> selectedSOs = Selection::instance().getSceneObjects();
  75. auto iterFind = std::find_if(selectedSOs.begin(), selectedSOs.end(),
  76. [&](const HSceneObject& obj) { return obj == pickedObject; }
  77. );
  78. if (iterFind != selectedSOs.end())
  79. selectedSOs.push_back(pickedObject);
  80. Selection::instance().setSceneObjects(selectedSOs);
  81. }
  82. else
  83. {
  84. Vector<HSceneObject> selectedSOs = { pickedObject };
  85. Selection::instance().setSceneObjects(selectedSOs);
  86. }
  87. }
  88. else
  89. Selection::instance().clearSceneSelection();
  90. }
  91. Vector2I SceneViewHandler::wrapCursorToWindow()
  92. {
  93. if (mParentWidget == nullptr)
  94. return Vector2I();
  95. RenderWindowPtr parentWindow = mParentWidget->getParentWindow()->getRenderWindow();
  96. Vector2I windowPos = parentWindow->screenToWindowPos(Cursor::instance().getScreenPosition());
  97. const RenderWindowProperties& rwProps = parentWindow->getProperties();
  98. INT32 maxWidth = std::max(0, (INT32)rwProps.getWidth() - 1);
  99. INT32 maxHeight = std::max(0, (INT32)rwProps.getHeight() - 1);
  100. Vector2I offset;
  101. if (windowPos.x <= 0)
  102. offset.x = maxWidth;
  103. else if (windowPos.x >= maxWidth)
  104. offset.x = -maxWidth;
  105. if (windowPos.y <= 0)
  106. offset.y = maxHeight;
  107. else if (windowPos.y >= maxHeight)
  108. offset.y = -maxHeight;
  109. windowPos += offset;
  110. Vector2I wrappedScreenPos = parentWindow->windowToScreenPos(windowPos);
  111. Cursor::instance().setScreenPosition(wrappedScreenPos);
  112. return offset;
  113. }
  114. }