BsSceneViewHandler.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "BsCameraHandler.h"
  10. #include "BsEditorApplication.h"
  11. #include "BsEditorWidget.h"
  12. #include "BsEditorWindowBase.h"
  13. #include "BsRenderWindow.h"
  14. #include "BsCursor.h"
  15. using namespace std::placeholders;
  16. namespace BansheeEngine
  17. {
  18. SceneViewHandler::SceneViewHandler(const EditorWidgetBase* parentWidget, const SPtr<CameraHandler>& camera)
  19. :mCamera(camera), mSceneGrid(nullptr)
  20. {
  21. mRenderCallback = RendererManager::instance().getActive()->onRenderViewport.connect(std::bind(&SceneViewHandler::render, this, _1, _2));
  22. mSceneGrid = bs_new<SceneGrid>();
  23. mSceneGrid->setSettings(gEditorApplication().getEditorSettings());
  24. }
  25. SceneViewHandler::~SceneViewHandler()
  26. {
  27. bs_delete(mSceneGrid);
  28. mRenderCallback.disconnect();
  29. if (GizmoManager::isStarted()) // If not active, we don't care
  30. GizmoManager::instance().clearRenderData();
  31. }
  32. void SceneViewHandler::update(const Vector2I& position, const Vector2I& delta)
  33. {
  34. if (HandleManager::instance().isHandleActive())
  35. wrapCursorToWindow();
  36. GizmoManager::instance().update(mCamera);
  37. HandleManager::instance().update(mCamera, position, delta);
  38. mSceneGrid->update();
  39. }
  40. void SceneViewHandler::pointerPressed(const Vector2I& position)
  41. {
  42. HandleManager::instance().trySelect(mCamera, position);
  43. }
  44. void SceneViewHandler::pointerReleased(const Vector2I& position, bool controlHeld)
  45. {
  46. if (HandleManager::instance().isHandleActive())
  47. HandleManager::instance().clearSelection();
  48. else
  49. {
  50. // TODO - Handle multi-selection (i.e. selection rectangle when dragging)
  51. HSceneObject pickedObject = ScenePicking::instance().pickClosestObject(mCamera, position, Vector2I(1, 1));
  52. if (pickedObject)
  53. {
  54. if (controlHeld) // Append to existing selection
  55. {
  56. Vector<HSceneObject> selectedSOs = Selection::instance().getSceneObjects();
  57. auto iterFind = std::find_if(selectedSOs.begin(), selectedSOs.end(),
  58. [&](const HSceneObject& obj) { return obj == pickedObject; }
  59. );
  60. if (iterFind != selectedSOs.end())
  61. selectedSOs.push_back(pickedObject);
  62. Selection::instance().setSceneObjects(selectedSOs);
  63. }
  64. else
  65. {
  66. Vector<HSceneObject> selectedSOs = { pickedObject };
  67. Selection::instance().setSceneObjects(selectedSOs);
  68. }
  69. }
  70. else
  71. Selection::instance().clearSceneSelection();
  72. }
  73. }
  74. void SceneViewHandler::render(const Viewport* viewport, DrawList& drawList)
  75. {
  76. if (mCamera == nullptr)
  77. return;
  78. if (mCamera->getViewport().get() != viewport)
  79. return;
  80. mSceneGrid->render(mCamera, drawList);
  81. }
  82. void SceneViewHandler::wrapCursorToWindow()
  83. {
  84. RenderWindowPtr parentWindow = mParentWidget->getParentWindow()->_getRenderWindow();
  85. Vector2I windowPos = parentWindow->screenToWindowPos(Cursor::instance().getScreenPosition());
  86. const RenderWindowProperties& rwProps = parentWindow->getProperties();
  87. if (windowPos.x < 0)
  88. windowPos.x += rwProps.getWidth();
  89. else if (windowPos.x >= (INT32)rwProps.getWidth())
  90. windowPos.x -= rwProps.getWidth();
  91. if (windowPos.y < 0)
  92. windowPos.y += rwProps.getHeight();
  93. else if (windowPos.y >= (INT32)rwProps.getHeight())
  94. windowPos.y -= rwProps.getHeight();
  95. Vector2I wrappedScreenPos = parentWindow->windowToScreenPos(windowPos);
  96. Cursor::instance().setScreenPosition(wrappedScreenPos);
  97. }
  98. }