SceneView3D.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. // Portions Copyright (c) 2008-2015 the Urho3D project.
  2. //
  3. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include <Atomic/IO/Log.h>
  24. #include <Atomic/Core/CoreEvents.h>
  25. #include <Atomic/Scene/SceneEvents.h>
  26. #include <Atomic/Scene/Scene.h>
  27. #include <Atomic/Scene/PrefabComponent.h>
  28. #include <Atomic/Graphics/Camera.h>
  29. #include <Atomic/Graphics/Graphics.h>
  30. #include <Atomic/Graphics/DebugRenderer.h>
  31. #include <Atomic/Graphics/Viewport.h>
  32. #include <Atomic/Graphics/Octree.h>
  33. #include <Atomic/Graphics/Material.h>
  34. #include <Atomic/Atomic3D/Model.h>
  35. #include <Atomic/Atomic3D/StaticModel.h>
  36. #include <Atomic/Atomic3D/AnimatedModel.h>
  37. #include <Atomic/Atomic3D/AnimationController.h>
  38. #include <Atomic/Input/Input.h>
  39. #include <Atomic/IO/FileSystem.h>
  40. #include <Atomic/Resource/ResourceCache.h>
  41. #include <Atomic/Resource/XMLFile.h>
  42. #include <Atomic/Physics/PhysicsWorld.h>
  43. #include <Atomic/UI/UI.h>
  44. #include <Atomic/UI/UIEvents.h>
  45. #include <Atomic/Resource/ResourceEvents.h>
  46. #include <ToolCore/Assets/Asset.h>
  47. #include <ToolCore/Assets/AssetDatabase.h>
  48. #include "../../EditorMode/AEEditorEvents.h"
  49. #include "SceneView3D.h"
  50. #include "SceneEditor3D.h"
  51. #include "SceneEditor3DEvents.h"
  52. #include "SceneSelection.h"
  53. using namespace ToolCore;
  54. namespace AtomicEditor
  55. {
  56. SceneView3D ::SceneView3D(Context* context, SceneEditor3D *sceneEditor) :
  57. UISceneView(context),
  58. yaw_(0.0f),
  59. pitch_(0.0f),
  60. mouseLeftDown_(false),
  61. mouseMoved_(false),
  62. enabled_(true),
  63. cameraMove_(false),
  64. cameraMoveSpeed_(20.0f),
  65. gridEnabled_(false),
  66. perspectCamPosition_(0, 0, 0),
  67. perspectiveYaw_(0),
  68. perspectivePitch_(0),
  69. perspectivePositionSaved_(false)
  70. {
  71. sceneEditor_ = sceneEditor;
  72. ResourceCache* cache = GetSubsystem<ResourceCache>();
  73. scene_ = sceneEditor->GetScene();
  74. debugRenderer_ = scene_->GetComponent<DebugRenderer>();
  75. if (debugRenderer_.Null())
  76. {
  77. debugRenderer_ = scene_->CreateComponent<DebugRenderer>();
  78. debugRenderer_->SetTemporary(true);
  79. }
  80. octree_ = scene_->GetComponent<Octree>();
  81. if (octree_.Null())
  82. {
  83. LOGWARNING("Scene without an octree loaded");
  84. octree_ = scene_->CreateComponent<Octree>();
  85. }
  86. cameraNode_ = scene_->CreateChild("__atomic_sceneview3d_camera");
  87. cameraNode_->SetTemporary(true);
  88. camera_ = cameraNode_->CreateComponent<Camera>();
  89. debugRenderer_ = scene_->GetComponent<DebugRenderer>();
  90. assert(debugRenderer_.NotNull());
  91. octree_ = scene_->GetComponent<Octree>();
  92. assert(octree_.NotNull());
  93. cameraNode_->SetPosition(Vector3(0, 0, -10));
  94. SetView(scene_, camera_);
  95. SetAutoUpdate(false);
  96. SubscribeToEvent(E_UPDATE, HANDLER(SceneView3D, HandleUpdate));
  97. SubscribeToEvent(E_POSTRENDERUPDATE, HANDLER(SceneView3D, HandlePostRenderUpdate));
  98. SubscribeToEvent(E_MOUSEMOVE, HANDLER(SceneView3D,HandleMouseMove));
  99. SubscribeToEvent(this, E_DRAGENTERWIDGET, HANDLER(SceneView3D, HandleDragEnterWidget));
  100. SubscribeToEvent(this, E_DRAGEXITWIDGET, HANDLER(SceneView3D, HandleDragExitWidget));
  101. SubscribeToEvent(this, E_DRAGENDED, HANDLER(SceneView3D, HandleDragEnded));
  102. SubscribeToEvent(E_UIUNHANDLEDSHORTCUT, HANDLER(SceneView3D, HandleUIUnhandledShortcut));
  103. SubscribeToEvent(E_UIWIDGETFOCUSESCAPED, HANDLER(SceneView3D, HandleUIWidgetFocusEscaped));
  104. SetIsFocusable(true);
  105. }
  106. SceneView3D::~SceneView3D()
  107. {
  108. }
  109. void SceneView3D::Enable()
  110. {
  111. if (enabled_)
  112. return;
  113. enabled_ = true;
  114. }
  115. void SceneView3D::Disable()
  116. {
  117. if (!enabled_)
  118. return;
  119. enabled_ = false;
  120. }
  121. bool SceneView3D::GetOrbitting()
  122. {
  123. Input* input = GetSubsystem<Input>();
  124. return framedBBox_.defined_ && MouseInView() && input->GetKeyDown(KEY_ALT) && input->GetMouseButtonDown(MOUSEB_LEFT);
  125. }
  126. bool SceneView3D::GetZooming()
  127. {
  128. Input* input = GetSubsystem<Input>();
  129. return MouseInView() && input->GetMouseMoveWheel() && !input->GetMouseButtonDown(MOUSEB_RIGHT);
  130. }
  131. bool SceneView3D::GetChangingCameraSpeed()
  132. {
  133. Input* input = GetSubsystem<Input>();
  134. return MouseInView() && input->GetMouseMoveWheel() && input->GetMouseButtonDown(MOUSEB_RIGHT);
  135. }
  136. void SceneView3D::CheckCameraSpeedBounds()
  137. {
  138. const float MAX_CAMERA_SPEED = 80.0f;
  139. const float MIN_CAMERA_SPEED = 2.0f;
  140. if (cameraMoveSpeed_ >= MAX_CAMERA_SPEED)
  141. {
  142. cameraMoveSpeed_ = MAX_CAMERA_SPEED;
  143. }
  144. if (cameraMoveSpeed_ <= MIN_CAMERA_SPEED)
  145. cameraMoveSpeed_ = MIN_CAMERA_SPEED;
  146. }
  147. void SceneView3D::MoveCamera(float timeStep)
  148. {
  149. // Mouse sensitivity as degrees per pixel
  150. const float MOUSE_SENSITIVITY = 0.2f;
  151. // Tempo at which mouse speed increases using mousewheel
  152. const float CAMERA_MOVE_TEMPO = 5.0f;
  153. // Tempo used when zooming in and out
  154. const float ZOOM_TEMPO = 0.6f;
  155. if (!enabled_ && !GetFocus())
  156. return;
  157. Input* input = GetSubsystem<Input>();
  158. bool shiftDown = false;
  159. if (input->GetKeyDown(KEY_LSHIFT) || input->GetKeyDown(KEY_RSHIFT))
  160. shiftDown = true;
  161. bool mouseInView = MouseInView();
  162. bool orbitting = GetOrbitting();
  163. bool zooming = GetZooming();
  164. bool changingCameraSpeed = GetChangingCameraSpeed();
  165. // Movement speed as world units per second
  166. float MOVE_SPEED = 20.0f;
  167. // Mouse sensitivity as degrees per pixel
  168. const float MOUSE_SENSITIVITY = 0.2f;
  169. // Orthographic zoom settings
  170. float currCameraZoom = camera_->GetZoom();
  171. const float ZOOM_INCREMENT = 0.1f;
  172. const float DEFAULT_ZOOM = 1.0f;
  173. if (shiftDown)
  174. MOVE_SPEED *= 3.0f;
  175. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  176. if ((mouseInView && input->GetMouseButtonDown(MOUSEB_RIGHT)) || orbitting)
  177. {
  178. SetFocus();
  179. IntVector2 mouseMove = input->GetMouseMove();
  180. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  181. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  182. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  183. }
  184. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  185. Quaternion q(pitch_, yaw_, 0.0f);
  186. if (!zooming || !changingCameraSpeed)
  187. cameraNode_->SetRotation(q);
  188. if (orbitting)
  189. {
  190. zooming = false;
  191. BoundingBox bbox;
  192. sceneEditor_->GetSelection()->GetBounds(bbox);
  193. if (bbox.defined_)
  194. {
  195. Vector3 centerPoint = bbox.Center();
  196. Vector3 d = cameraNode_->GetWorldPosition() - centerPoint;
  197. cameraNode_->SetWorldPosition(centerPoint - q * Vector3(0.0, 0.0, d.Length()));
  198. }
  199. }
  200. if (zooming)
  201. {
  202. orbitting = false;
  203. Ray ray = GetCameraRay();
  204. Vector3 wpos = cameraNode_->GetWorldPosition();
  205. wpos += ray.direction_ * (float(input->GetMouseMoveWheel()) * ZOOM_TEMPO);
  206. cameraNode_->SetWorldPosition(wpos);
  207. }
  208. if (changingCameraSpeed)
  209. {
  210. int mouseWheel = input->GetMouseMoveWheel();
  211. // Apple decided to change the direction of mousewheel input to match touch devices
  212. #ifdef ATOMIC_PLATFORM_OSX
  213. mouseWheel = -mouseWheel;
  214. #endif
  215. if (mouseWheel)
  216. cameraMoveSpeed_ += mouseWheel * CAMERA_MOVE_TEMPO;
  217. CheckCameraSpeedBounds();
  218. }
  219. #ifdef ATOMIC_PLATFORM_WINDOWS
  220. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  221. #else
  222. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  223. #endif
  224. if (!orbitting && mouseInView && !superdown && input->GetMouseButtonDown(MOUSEB_RIGHT)) {
  225. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  226. // Use the Translate() function (default local space) to move relative to the node's orientation.
  227. if (input->GetKeyDown(KEY_W))
  228. {
  229. SetFocus();
  230. cameraNode_->Translate(Vector3::FORWARD * cameraMoveSpeed_ * timeStep);
  231. }
  232. if (input->GetKeyDown(KEY_S))
  233. {
  234. SetFocus();
  235. cameraNode_->Translate(Vector3::BACK * cameraMoveSpeed_ * timeStep);
  236. }
  237. if (input->GetKeyDown(KEY_A))
  238. {
  239. SetFocus();
  240. cameraNode_->Translate(Vector3::LEFT * cameraMoveSpeed_ * timeStep);
  241. }
  242. if (input->GetKeyDown(KEY_D))
  243. {
  244. SetFocus();
  245. cameraNode_->Translate(Vector3::RIGHT * cameraMoveSpeed_ * timeStep);
  246. }
  247. if (input->GetKeyDown(KEY_E))
  248. {
  249. SetFocus();
  250. cameraNode_->Translate(Vector3::UP * cameraMoveSpeed_ * timeStep);
  251. }
  252. if (input->GetKeyDown(KEY_Q))
  253. {
  254. SetFocus();
  255. cameraNode_->Translate(Vector3::DOWN * cameraMoveSpeed_ * timeStep);
  256. }
  257. }
  258. if (cameraMove_)
  259. {
  260. cameraMoveTime_ += timeStep * 3.0f;
  261. if (cameraMoveTime_ > 1.0f)
  262. {
  263. cameraMove_ = false;
  264. cameraMoveTime_ = 1.0f;
  265. }
  266. Vector3 pos = cameraMoveStart_.Lerp(cameraMoveTarget_, cameraMoveTime_);
  267. cameraNode_->SetWorldPosition(pos);
  268. }
  269. if (camera_->IsOrthographic() && mouseInView && !orbitting)
  270. {
  271. if (input->GetMouseMoveWheel() > 0)
  272. camera_->SetZoom(currCameraZoom + ZOOM_INCREMENT);
  273. if (input->GetMouseMoveWheel() < 0)
  274. camera_->SetZoom(currCameraZoom - ZOOM_INCREMENT);
  275. }
  276. else
  277. camera_->SetZoom(DEFAULT_ZOOM);
  278. }
  279. void SceneView3D::SnapCameraToView(int snapView)
  280. {
  281. Vector3 selectedNodePos(0, 0, 0);
  282. // the distance the camera snaps from the selected object
  283. const int DISTANCE = 5;
  284. int xVal = 0;
  285. int yVal = 0;
  286. int zVal = 0;
  287. camera_->SetOrthographic(true);
  288. perspectivePositionSaved_ = true;
  289. // if no object is selected will snap to view relative to camera position
  290. if (sceneEditor_->GetSelection()->GetNodes().Size() > 0)
  291. {
  292. selectedNodePos = sceneEditor_->GetSelection()->GetSelectedNode(0)->GetPosition();
  293. switch (snapView)
  294. {
  295. case 1:
  296. selectedNodePos.y_ = selectedNodePos.y_ + DISTANCE;
  297. break;
  298. case 2:
  299. selectedNodePos.y_ = selectedNodePos.y_ - DISTANCE;
  300. break;
  301. case 3:
  302. selectedNodePos.x_ = selectedNodePos.x_ + DISTANCE;
  303. break;
  304. case 4:
  305. selectedNodePos.x_ = selectedNodePos.x_ - DISTANCE;
  306. break;
  307. case 5:
  308. selectedNodePos.z_ = selectedNodePos.z_ - DISTANCE;
  309. break;
  310. case 6:
  311. selectedNodePos.z_ = selectedNodePos.z_ + DISTANCE;
  312. break;
  313. }
  314. cameraNode_->SetPosition(selectedNodePos);
  315. }
  316. else
  317. cameraNode_->SetPosition(cameraNode_->GetPosition());
  318. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0));
  319. }
  320. void SceneView3D::SelectView()
  321. {
  322. Input* input = GetSubsystem<Input>();
  323. Vector3 camPos(0, 0, 0);
  324. int snapView = 0;
  325. if (MouseInView())
  326. {
  327. if (input->GetKeyPress(KEY_1))
  328. snapView = 1;
  329. if (input->GetKeyPress(KEY_2))
  330. snapView = 2;
  331. if (input->GetKeyPress(KEY_3))
  332. snapView = 3;
  333. if (input->GetKeyPress(KEY_4))
  334. snapView = 4;
  335. if (input->GetKeyPress(KEY_5))
  336. snapView = 5;
  337. if (input->GetKeyPress(KEY_6))
  338. snapView = 6;
  339. SetPerspectiveCameraPosition();
  340. if (input->GetKeyPress(KEY_P))
  341. {
  342. perspectivePositionSaved_ = false;
  343. camera_->SetOrthographic(false);
  344. pitch_ = perspectivePitch_;
  345. yaw_ = perspectiveYaw_;
  346. cameraNode_->SetPosition(perspectCamPosition_);
  347. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0));
  348. }
  349. if (input->GetKeyPress(KEY_O))
  350. {
  351. if (camera_->IsOrthographic())
  352. perspectivePositionSaved_ = true;
  353. if (!camera_->IsOrthographic())
  354. perspectivePositionSaved_ = false;
  355. camera_->SetOrthographic(!camera_->IsOrthographic());
  356. }
  357. }
  358. switch (snapView)
  359. {
  360. case 1:
  361. // View top
  362. yaw_ = 0;
  363. pitch_ = 90;
  364. SnapCameraToView(snapView);
  365. break;
  366. case 2:
  367. // View bottom
  368. yaw_ = 0;
  369. pitch_ = -90;
  370. SnapCameraToView(snapView);
  371. break;
  372. case 3:
  373. // View left
  374. yaw_ = -90;
  375. pitch_ = 0;
  376. SnapCameraToView(snapView);
  377. break;
  378. case 4:
  379. // View right
  380. yaw_ = 90;
  381. pitch_ = 0;
  382. SnapCameraToView(snapView);
  383. break;
  384. case 5:
  385. // View front
  386. yaw_ = 0;
  387. pitch_ = 0;
  388. SnapCameraToView(snapView);
  389. break;
  390. case 6:
  391. // View back
  392. yaw_ = 180;
  393. pitch_ = 0;
  394. SnapCameraToView(snapView);
  395. break;
  396. }
  397. }
  398. void SceneView3D::SetPerspectiveCameraPosition()
  399. {
  400. if (perspectivePositionSaved_)
  401. return;
  402. if (!perspectivePositionSaved_ && !camera_->IsOrthographic())
  403. {
  404. perspectCamPosition_ = cameraNode_->GetPosition();
  405. perspectivePitch_ = pitch_;
  406. perspectiveYaw_ = yaw_;
  407. }
  408. }
  409. Ray SceneView3D::GetCameraRay()
  410. {
  411. Ray camRay;
  412. Input* input = GetSubsystem<Input>();
  413. IntVector2 cpos = input->GetMousePosition();
  414. IntRect rect = GetRect();
  415. if (!rect.Width() || !rect.Height())
  416. return camRay;
  417. int x = rect.left_;
  418. int y = rect.top_;
  419. GetInternalWidget()->ConvertToRoot(x, y);
  420. return camera_->GetScreenRay(float(cpos.x_ - x) / rect.Width(),
  421. float(cpos.y_ - y) / rect.Height());
  422. }
  423. bool SceneView3D::MouseInView()
  424. {
  425. if (!GetInternalWidget())
  426. return false;
  427. if (!TBWidget::hovered_widget || TBWidget::hovered_widget->GetDelegate() != this)
  428. return false;
  429. Input* input = GetSubsystem<Input>();
  430. IntVector2 pos = input->GetMousePosition();
  431. IntRect rect = GetRect();
  432. GetInternalWidget()->ConvertToRoot(rect.left_, rect.top_);
  433. GetInternalWidget()->ConvertToRoot(rect.right_, rect.bottom_);
  434. return rect.IsInside(pos);
  435. }
  436. void SceneView3D::HandleUIUnhandledShortcut(StringHash eventType, VariantMap& eventData)
  437. {
  438. if (!enabled_)
  439. return;
  440. unsigned id = eventData[UIUnhandledShortcut::P_REFID].GetUInt();
  441. if (id == TBIDC("undo"))
  442. sceneEditor_->Undo();
  443. else if (id == TBIDC("redo"))
  444. sceneEditor_->Redo();
  445. else if (id == TBIDC("copy"))
  446. sceneEditor_->Copy();
  447. else if (id == TBIDC("cut"))
  448. sceneEditor_->Cut();
  449. else if (id == TBIDC("paste"))
  450. sceneEditor_->Paste();
  451. return;
  452. }
  453. void SceneView3D::HandleUIWidgetFocusEscaped(StringHash eventType, VariantMap& eventData)
  454. {
  455. if (!enabled_)
  456. return;
  457. SetFocus();
  458. }
  459. void SceneView3D::HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData)
  460. {
  461. if (!MouseInView() || GetOrbitting())
  462. return;
  463. Input* input = GetSubsystem<Input>();
  464. mouseLeftDown_ = false;
  465. bool shiftDown = input->GetKeyDown(KEY_LSHIFT) || input->GetKeyDown(KEY_RSHIFT);
  466. if (input->GetMouseButtonPress(MOUSEB_LEFT))
  467. {
  468. SetFocus();
  469. if (!mouseMoved_ && !sceneEditor_->GetGizmo()->Selected())
  470. {
  471. Ray camRay = GetCameraRay();
  472. PODVector<RayQueryResult> result;
  473. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  474. octree_->RaycastSingle(query);
  475. if (query.result_.Size())
  476. {
  477. const RayQueryResult& r = result[0];
  478. if (r.drawable_)
  479. {
  480. VariantMap neventData;
  481. Node* node = r.drawable_->GetNode();
  482. // if temporary, this is a prefab
  483. // TODO: if we use temporary for other stuff
  484. // fix this to look for prefab
  485. if (node->IsTemporary())
  486. node = node->GetParent();
  487. if (sceneEditor_->GetSelection()->Contains(node) && shiftDown)
  488. {
  489. sceneEditor_->GetSelection()->RemoveNode(node);
  490. }
  491. else
  492. {
  493. sceneEditor_->GetSelection()->AddNode(node, !shiftDown);
  494. }
  495. }
  496. }
  497. else
  498. {
  499. sceneEditor_->GetSelection()->Clear();
  500. }
  501. }
  502. mouseMoved_ = false;
  503. }
  504. else if (!input->GetMouseButtonDown(MOUSEB_LEFT))
  505. {
  506. Ray camRay = GetCameraRay();
  507. PODVector<RayQueryResult> result;
  508. mouseMoved_ = false;
  509. /*
  510. Array<int> pickModeDrawableFlags = {
  511. DRAWABLE_GEOMETRY,
  512. DRAWABLE_LIGHT,
  513. DRAWABLE_ZONE
  514. };
  515. */
  516. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  517. octree_->RaycastSingle(query);
  518. if (query.result_.Size())
  519. {
  520. const RayQueryResult& r = result[0];
  521. if (r.drawable_)
  522. {
  523. debugRenderer_->AddNode(r.drawable_->GetNode(), 1.0, false);
  524. r.drawable_->DrawDebugGeometry(debugRenderer_, false);
  525. }
  526. }
  527. }
  528. else
  529. {
  530. mouseLeftDown_ = true;
  531. if (Abs(input->GetMouseMoveX() > 3 || input->GetMouseMoveY() > 3))
  532. {
  533. mouseMoved_ = true;
  534. }
  535. }
  536. }
  537. bool SceneView3D::OnEvent(const TBWidgetEvent &ev)
  538. {
  539. if (ev.type == EVENT_TYPE_SHORTCUT)
  540. {
  541. if (ev.ref_id == TBIDC("close"))
  542. return false;
  543. }
  544. if (ev.type == EVENT_TYPE_KEY_UP)
  545. {
  546. if (ev.special_key == TB_KEY_ESC)
  547. {
  548. sceneEditor_->GetSelection()->Clear();
  549. }
  550. }
  551. if (ev.type == EVENT_TYPE_KEY_DOWN)
  552. {
  553. SelectView();
  554. }
  555. Input* input = GetSubsystem<Input>();
  556. if (input->GetKeyPress(KEY_G))
  557. gridEnabled_ = !gridEnabled_;
  558. return sceneEditor_->OnEvent(ev);
  559. }
  560. void SceneView3D::HandleUpdate(StringHash eventType, VariantMap& eventData)
  561. {
  562. // parent is the contentRoot for our tab, when tab isn't active it will not be visible
  563. if (!GetParent() || GetParent()->GetVisibility() != UI_WIDGET_VISIBILITY_VISIBLE)
  564. {
  565. Disable();
  566. return;
  567. }
  568. Enable();
  569. // Timestep parameter is same no matter what event is being listened to
  570. float timeStep = eventData[Update::P_TIMESTEP].GetFloat();
  571. MoveCamera(timeStep);
  572. QueueUpdate();
  573. if (gridEnabled_)
  574. debugRenderer_->CreateGrid(Color::GRAY, true, cameraNode_->GetPosition());
  575. if (preloadResourceScene_.NotNull())
  576. {
  577. if (preloadResourceScene_->GetAsyncProgress() == 1.0f)
  578. {
  579. ResourceCache* cache = GetSubsystem<ResourceCache>();
  580. XMLFile* xml = cache->GetResource<XMLFile>(dragAssetGUID_);
  581. if (dragNode_.NotNull())
  582. {
  583. dragNode_->LoadXML(xml->GetRoot());
  584. UpdateDragNode(0, 0);
  585. AnimationController* controller = dragNode_->GetComponent<AnimationController>();
  586. if (controller)
  587. {
  588. controller->PlayExclusive("Idle", 0, true);
  589. dragNode_->GetScene()->SetUpdateEnabled(true);
  590. }
  591. }
  592. preloadResourceScene_ = 0;
  593. dragAssetGUID_ = "";
  594. }
  595. }
  596. }
  597. void SceneView3D::UpdateDragNode(int mouseX, int mouseY)
  598. {
  599. if (dragNode_.Null())
  600. return;
  601. Ray ray = GetCameraRay();
  602. Vector3 pos = ray.origin_;
  603. pos += ray.direction_ * 10;
  604. dragNode_->SetWorldPosition(pos);
  605. }
  606. void SceneView3D::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  607. {
  608. if (dragNode_.Null())
  609. return;
  610. Input* input = GetSubsystem<Input>();
  611. if (!input->IsMouseVisible())
  612. return;
  613. using namespace MouseMove;
  614. int x = eventData[P_X].GetInt();
  615. int y = eventData[P_Y].GetInt();
  616. UpdateDragNode(x, y);
  617. }
  618. void SceneView3D::HandleDragEnterWidget(StringHash eventType, VariantMap& eventData)
  619. {
  620. using namespace DragEnterWidget;
  621. UIWidget* widget = static_cast<UIWidget*>(eventData[P_WIDGET].GetPtr());
  622. if (widget != this)
  623. return;
  624. UIDragObject* dragObject = static_cast<UIDragObject*>(eventData[P_DRAGOBJECT].GetPtr());
  625. Object* object = dragObject->GetObject();
  626. if (!object)
  627. return;
  628. if (object->GetType() == Asset::GetTypeStatic())
  629. {
  630. Asset* asset = (Asset*) object;
  631. dragNode_ = asset->InstantiateNode(scene_, asset->GetName());
  632. if (dragNode_.NotNull())
  633. {
  634. Input* input = GetSubsystem<Input>();
  635. IntVector2 pos = input->GetMousePosition();
  636. UpdateDragNode(pos.x_, pos.y_);
  637. }
  638. //LOGINFOF("Dropped %s : %s on SceneView3D", asset->GetPath().CString(), asset->GetGUID().CString());
  639. }
  640. }
  641. void SceneView3D::HandleDragExitWidget(StringHash eventType, VariantMap& eventData)
  642. {
  643. if (preloadResourceScene_.NotNull())
  644. {
  645. preloadResourceScene_->StopAsyncLoading();
  646. preloadResourceScene_ = 0;
  647. }
  648. if (dragNode_.NotNull())
  649. {
  650. scene_->RemoveChild(dragNode_);
  651. }
  652. dragAssetGUID_ = "";
  653. dragNode_ = 0;
  654. }
  655. void SceneView3D::HandleDragEnded(StringHash eventType, VariantMap& eventData)
  656. {
  657. using namespace DragEnded;
  658. UIDragObject* dragObject = static_cast<UIDragObject*>(eventData[P_DRAGOBJECT].GetPtr());
  659. if (dragNode_.NotNull())
  660. {
  661. VariantMap nodeCreatedEvent;
  662. nodeCreatedEvent[SceneEditNodeCreated::P_NODE] = dragNode_;
  663. scene_->SendEvent(E_SCENEEDITNODECREATED, nodeCreatedEvent);
  664. }
  665. if (dragObject && dragObject->GetObject()->GetType() == ToolCore::Asset::GetTypeStatic())
  666. {
  667. Asset* asset = (ToolCore::Asset*) dragObject->GetObject();
  668. if (asset->GetImporterTypeName() == "MaterialImporter") {
  669. Material* material = GetSubsystem<ResourceCache>()->GetResource<Material>(asset->GetPath());
  670. if (material) {
  671. material = material;
  672. Ray camRay = GetCameraRay();
  673. PODVector<RayQueryResult> result;
  674. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  675. octree_->RaycastSingle(query);
  676. if (query.result_.Size())
  677. {
  678. const RayQueryResult& r = result[0];
  679. if (r.drawable_ && (r.drawable_->GetType() == StaticModel::GetTypeStatic() || r.drawable_->GetType() == AnimatedModel::GetTypeStatic()))
  680. {
  681. ((StaticModel*)r.drawable_)->SetMaterial(material);
  682. }
  683. }
  684. }
  685. }
  686. }
  687. dragAssetGUID_ = "";
  688. dragNode_ = 0;
  689. }
  690. void SceneView3D::FrameSelection()
  691. {
  692. BoundingBox bbox;
  693. sceneEditor_->GetSelection()->GetBounds(bbox);
  694. if (!bbox.defined_)
  695. return;
  696. Sphere sphere(bbox);
  697. if (sphere.radius_ < .01f || sphere.radius_ > 512)
  698. return;
  699. framedBBox_ = bbox;
  700. cameraMoveStart_ = cameraNode_->GetWorldPosition();
  701. cameraMoveTarget_ = bbox.Center() - (cameraNode_->GetWorldDirection() * sphere.radius_ * 3);
  702. cameraMoveTime_ = 0.0f;
  703. cameraMove_ = true;
  704. }
  705. }