SceneView3D.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  166. if ((mouseInView && input->GetMouseButtonDown(MOUSEB_RIGHT)) || orbitting)
  167. {
  168. SetFocus();
  169. IntVector2 mouseMove = input->GetMouseMove();
  170. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  171. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  172. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  173. }
  174. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  175. Quaternion q(pitch_, yaw_, 0.0f);
  176. if (!zooming || !changingCameraSpeed)
  177. cameraNode_->SetRotation(q);
  178. if (orbitting)
  179. {
  180. zooming = false;
  181. BoundingBox bbox;
  182. sceneEditor_->GetSelection()->GetBounds(bbox);
  183. if (bbox.defined_)
  184. {
  185. Vector3 centerPoint = bbox.Center();
  186. Vector3 d = cameraNode_->GetWorldPosition() - centerPoint;
  187. cameraNode_->SetWorldPosition(centerPoint - q * Vector3(0.0, 0.0, d.Length()));
  188. }
  189. }
  190. if (zooming)
  191. {
  192. orbitting = false;
  193. Ray ray = GetCameraRay();
  194. Vector3 wpos = cameraNode_->GetWorldPosition();
  195. wpos += ray.direction_ * (float(input->GetMouseMoveWheel()) * ZOOM_TEMPO);
  196. cameraNode_->SetWorldPosition(wpos);
  197. }
  198. if (changingCameraSpeed)
  199. {
  200. int mouseWheel = input->GetMouseMoveWheel();
  201. // Apple decided to change the direction of mousewheel input to match touch devices
  202. #ifdef ATOMIC_PLATFORM_OSX
  203. mouseWheel = -mouseWheel;
  204. #endif
  205. if (mouseWheel)
  206. cameraMoveSpeed_ += mouseWheel * CAMERA_MOVE_TEMPO;
  207. CheckCameraSpeedBounds();
  208. }
  209. #ifdef ATOMIC_PLATFORM_WINDOWS
  210. bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
  211. #else
  212. bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
  213. #endif
  214. if (!orbitting && mouseInView && !superdown && input->GetMouseButtonDown(MOUSEB_RIGHT)) {
  215. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  216. // Use the Translate() function (default local space) to move relative to the node's orientation.
  217. if (input->GetKeyDown(KEY_W))
  218. {
  219. SetFocus();
  220. cameraNode_->Translate(Vector3::FORWARD * cameraMoveSpeed_ * timeStep);
  221. }
  222. if (input->GetKeyDown(KEY_S))
  223. {
  224. SetFocus();
  225. cameraNode_->Translate(Vector3::BACK * cameraMoveSpeed_ * timeStep);
  226. }
  227. if (input->GetKeyDown(KEY_A))
  228. {
  229. SetFocus();
  230. cameraNode_->Translate(Vector3::LEFT * cameraMoveSpeed_ * timeStep);
  231. }
  232. if (input->GetKeyDown(KEY_D))
  233. {
  234. SetFocus();
  235. cameraNode_->Translate(Vector3::RIGHT * cameraMoveSpeed_ * timeStep);
  236. }
  237. if (input->GetKeyDown(KEY_E))
  238. {
  239. SetFocus();
  240. cameraNode_->Translate(Vector3::UP * cameraMoveSpeed_ * timeStep);
  241. }
  242. if (input->GetKeyDown(KEY_Q))
  243. {
  244. SetFocus();
  245. cameraNode_->Translate(Vector3::DOWN * cameraMoveSpeed_ * timeStep);
  246. }
  247. }
  248. if (cameraMove_)
  249. {
  250. cameraMoveTime_ += timeStep * 3.0f;
  251. if (cameraMoveTime_ > 1.0f)
  252. {
  253. cameraMove_ = false;
  254. cameraMoveTime_ = 1.0f;
  255. }
  256. Vector3 pos = cameraMoveStart_.Lerp(cameraMoveTarget_, cameraMoveTime_);
  257. cameraNode_->SetWorldPosition(pos);
  258. }
  259. }
  260. void SceneView3D::SnapCameraToView(int snapView)
  261. {
  262. Vector3 selectedNodePos(0, 0, 0);
  263. // the distance the camera snaps from the selected object
  264. const int DISTANCE = 5;
  265. int xVal = 0;
  266. int yVal = 0;
  267. int zVal = 0;
  268. camera_->SetOrthographic(true);
  269. perspectivePositionSaved_ = true;
  270. // if no object is selected will snap to view relative to camera position
  271. if (sceneEditor_->GetSelection()->GetNodes().Size() > 0)
  272. {
  273. selectedNodePos = sceneEditor_->GetSelection()->GetSelectedNode(0)->GetPosition();
  274. switch (snapView)
  275. {
  276. case 1:
  277. selectedNodePos.y_ = selectedNodePos.y_ + DISTANCE;
  278. break;
  279. case 2:
  280. selectedNodePos.y_ = selectedNodePos.y_ - DISTANCE;
  281. break;
  282. case 3:
  283. selectedNodePos.x_ = selectedNodePos.x_ + DISTANCE;
  284. break;
  285. case 4:
  286. selectedNodePos.x_ = selectedNodePos.x_ - DISTANCE;
  287. break;
  288. case 5:
  289. selectedNodePos.z_ = selectedNodePos.z_ - DISTANCE;
  290. break;
  291. case 6:
  292. selectedNodePos.z_ = selectedNodePos.z_ + DISTANCE;
  293. break;
  294. }
  295. cameraNode_->SetPosition(selectedNodePos);
  296. }
  297. else
  298. cameraNode_->SetPosition(cameraNode_->GetPosition());
  299. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0));
  300. }
  301. void SceneView3D::SelectView()
  302. {
  303. Input* input = GetSubsystem<Input>();
  304. Vector3 camPos(0, 0, 0);
  305. int snapView = 0;
  306. if (MouseInView())
  307. {
  308. if (input->GetKeyPress(KEY_1))
  309. {
  310. snapView = 1;
  311. SetPerspectiveCameraPosition();
  312. }
  313. if (input->GetKeyPress(KEY_2))
  314. {
  315. snapView = 2;
  316. SetPerspectiveCameraPosition();
  317. }
  318. if (input->GetKeyPress(KEY_3))
  319. {
  320. snapView = 3;
  321. SetPerspectiveCameraPosition();
  322. }
  323. if (input->GetKeyPress(KEY_4))
  324. {
  325. snapView = 4;
  326. SetPerspectiveCameraPosition();
  327. }
  328. if (input->GetKeyPress(KEY_5))
  329. {
  330. snapView = 5;
  331. SetPerspectiveCameraPosition();
  332. }
  333. if (input->GetKeyPress(KEY_6))
  334. {
  335. snapView = 6;
  336. SetPerspectiveCameraPosition();
  337. }
  338. if (input->GetKeyPress(KEY_P))
  339. {
  340. perspectivePositionSaved_ = false;
  341. camera_->SetOrthographic(false);
  342. pitch_ = perspectivePitch_;
  343. yaw_ = perspectiveYaw_;
  344. cameraNode_->SetPosition(perspectCamPosition_);
  345. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0));
  346. }
  347. if (input->GetKeyPress(KEY_O))
  348. {
  349. if (camera_->IsOrthographic())
  350. perspectivePositionSaved_ = true;
  351. if (!camera_->IsOrthographic())
  352. perspectivePositionSaved_ = false;
  353. camera_->SetOrthographic(!camera_->IsOrthographic());
  354. }
  355. }
  356. switch (snapView)
  357. {
  358. case 1:
  359. // View top
  360. yaw_ = 0;
  361. pitch_ = 90;
  362. SnapCameraToView(1);
  363. break;
  364. case 2:
  365. // View bottom
  366. yaw_ = 0;
  367. pitch_ = -90;
  368. SnapCameraToView(2);
  369. break;
  370. case 3:
  371. // View left
  372. yaw_ = -90;
  373. pitch_ = 0;
  374. SnapCameraToView(3);
  375. break;
  376. case 4:
  377. // View right
  378. yaw_ = 90;
  379. pitch_ = 0;
  380. SnapCameraToView(4);
  381. break;
  382. case 5:
  383. // View front
  384. yaw_ = 0;
  385. pitch_ = 0;
  386. SnapCameraToView(5);
  387. break;
  388. case 6:
  389. // View back
  390. yaw_ = 180;
  391. pitch_ = 0;
  392. SnapCameraToView(6);
  393. break;
  394. }
  395. }
  396. void SceneView3D::SetPerspectiveCameraPosition()
  397. {
  398. if (perspectivePositionSaved_)
  399. return;
  400. if (!perspectivePositionSaved_ && !camera_->IsOrthographic())
  401. {
  402. perspectCamPosition_ = cameraNode_->GetPosition();
  403. perspectivePitch_ = pitch_;
  404. perspectiveYaw_ = yaw_;
  405. }
  406. }
  407. Ray SceneView3D::GetCameraRay()
  408. {
  409. Ray camRay;
  410. Input* input = GetSubsystem<Input>();
  411. IntVector2 cpos = input->GetMousePosition();
  412. IntRect rect = GetRect();
  413. if (!rect.Width() || !rect.Height())
  414. return camRay;
  415. int x = rect.left_;
  416. int y = rect.top_;
  417. GetInternalWidget()->ConvertToRoot(x, y);
  418. return camera_->GetScreenRay(float(cpos.x_ - x) / rect.Width(),
  419. float(cpos.y_ - y) / rect.Height());
  420. }
  421. bool SceneView3D::MouseInView()
  422. {
  423. if (!GetInternalWidget())
  424. return false;
  425. if (!TBWidget::hovered_widget || TBWidget::hovered_widget->GetDelegate() != this)
  426. return false;
  427. Input* input = GetSubsystem<Input>();
  428. IntVector2 pos = input->GetMousePosition();
  429. IntRect rect = GetRect();
  430. GetInternalWidget()->ConvertToRoot(rect.left_, rect.top_);
  431. GetInternalWidget()->ConvertToRoot(rect.right_, rect.bottom_);
  432. return rect.IsInside(pos);
  433. }
  434. void SceneView3D::HandleUIUnhandledShortcut(StringHash eventType, VariantMap& eventData)
  435. {
  436. if (!enabled_)
  437. return;
  438. unsigned id = eventData[UIUnhandledShortcut::P_REFID].GetUInt();
  439. if (id == TBIDC("undo"))
  440. sceneEditor_->Undo();
  441. else if (id == TBIDC("redo"))
  442. sceneEditor_->Redo();
  443. else if (id == TBIDC("copy"))
  444. sceneEditor_->Copy();
  445. else if (id == TBIDC("cut"))
  446. sceneEditor_->Cut();
  447. else if (id == TBIDC("paste"))
  448. sceneEditor_->Paste();
  449. return;
  450. }
  451. void SceneView3D::HandleUIWidgetFocusEscaped(StringHash eventType, VariantMap& eventData)
  452. {
  453. if (!enabled_)
  454. return;
  455. SetFocus();
  456. }
  457. void SceneView3D::HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData)
  458. {
  459. if (!MouseInView() || GetOrbitting())
  460. return;
  461. Input* input = GetSubsystem<Input>();
  462. mouseLeftDown_ = false;
  463. bool shiftDown = input->GetKeyDown(KEY_LSHIFT) || input->GetKeyDown(KEY_RSHIFT);
  464. if (input->GetMouseButtonPress(MOUSEB_LEFT))
  465. {
  466. SetFocus();
  467. if (!mouseMoved_ && !sceneEditor_->GetGizmo()->Selected())
  468. {
  469. Ray camRay = GetCameraRay();
  470. PODVector<RayQueryResult> result;
  471. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  472. octree_->RaycastSingle(query);
  473. if (query.result_.Size())
  474. {
  475. const RayQueryResult& r = result[0];
  476. if (r.drawable_)
  477. {
  478. VariantMap neventData;
  479. Node* node = r.drawable_->GetNode();
  480. // if temporary, this is a prefab
  481. // TODO: if we use temporary for other stuff
  482. // fix this to look for prefab
  483. if (node->IsTemporary())
  484. node = node->GetParent();
  485. if (sceneEditor_->GetSelection()->Contains(node) && shiftDown)
  486. {
  487. sceneEditor_->GetSelection()->RemoveNode(node);
  488. }
  489. else
  490. {
  491. sceneEditor_->GetSelection()->AddNode(node, !shiftDown);
  492. }
  493. }
  494. }
  495. else
  496. {
  497. sceneEditor_->GetSelection()->Clear();
  498. }
  499. }
  500. mouseMoved_ = false;
  501. }
  502. else if (!input->GetMouseButtonDown(MOUSEB_LEFT))
  503. {
  504. Ray camRay = GetCameraRay();
  505. PODVector<RayQueryResult> result;
  506. mouseMoved_ = false;
  507. /*
  508. Array<int> pickModeDrawableFlags = {
  509. DRAWABLE_GEOMETRY,
  510. DRAWABLE_LIGHT,
  511. DRAWABLE_ZONE
  512. };
  513. */
  514. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  515. octree_->RaycastSingle(query);
  516. if (query.result_.Size())
  517. {
  518. const RayQueryResult& r = result[0];
  519. if (r.drawable_)
  520. {
  521. debugRenderer_->AddNode(r.drawable_->GetNode(), 1.0, false);
  522. r.drawable_->DrawDebugGeometry(debugRenderer_, false);
  523. }
  524. }
  525. }
  526. else
  527. {
  528. mouseLeftDown_ = true;
  529. if (Abs(input->GetMouseMoveX() > 3 || input->GetMouseMoveY() > 3))
  530. {
  531. mouseMoved_ = true;
  532. }
  533. }
  534. }
  535. bool SceneView3D::OnEvent(const TBWidgetEvent &ev)
  536. {
  537. if (ev.type == EVENT_TYPE_SHORTCUT)
  538. {
  539. if (ev.ref_id == TBIDC("close"))
  540. return false;
  541. }
  542. if (ev.type == EVENT_TYPE_KEY_UP)
  543. {
  544. if (ev.special_key == TB_KEY_ESC)
  545. {
  546. sceneEditor_->GetSelection()->Clear();
  547. }
  548. }
  549. if (ev.type == EVENT_TYPE_KEY_DOWN)
  550. {
  551. SelectView();
  552. }
  553. Input* input = GetSubsystem<Input>();
  554. if (input->GetKeyPress(KEY_G))
  555. gridEnabled_ = !gridEnabled_;
  556. return sceneEditor_->OnEvent(ev);
  557. }
  558. void SceneView3D::HandleUpdate(StringHash eventType, VariantMap& eventData)
  559. {
  560. // parent is the contentRoot for our tab, when tab isn't active it will not be visible
  561. if (!GetParent() || GetParent()->GetVisibility() != UI_WIDGET_VISIBILITY_VISIBLE)
  562. {
  563. Disable();
  564. return;
  565. }
  566. Enable();
  567. // Timestep parameter is same no matter what event is being listened to
  568. float timeStep = eventData[Update::P_TIMESTEP].GetFloat();
  569. MoveCamera(timeStep);
  570. QueueUpdate();
  571. if (gridEnabled_)
  572. debugRenderer_->CreateGrid(Color::GRAY, true, cameraNode_->GetPosition());
  573. if (preloadResourceScene_.NotNull())
  574. {
  575. if (preloadResourceScene_->GetAsyncProgress() == 1.0f)
  576. {
  577. ResourceCache* cache = GetSubsystem<ResourceCache>();
  578. XMLFile* xml = cache->GetResource<XMLFile>(dragAssetGUID_);
  579. if (dragNode_.NotNull())
  580. {
  581. dragNode_->LoadXML(xml->GetRoot());
  582. UpdateDragNode(0, 0);
  583. AnimationController* controller = dragNode_->GetComponent<AnimationController>();
  584. if (controller)
  585. {
  586. controller->PlayExclusive("Idle", 0, true);
  587. dragNode_->GetScene()->SetUpdateEnabled(true);
  588. }
  589. }
  590. preloadResourceScene_ = 0;
  591. dragAssetGUID_ = "";
  592. }
  593. }
  594. }
  595. void SceneView3D::UpdateDragNode(int mouseX, int mouseY)
  596. {
  597. if (dragNode_.Null())
  598. return;
  599. Ray ray = GetCameraRay();
  600. Vector3 pos = ray.origin_;
  601. pos += ray.direction_ * 10;
  602. dragNode_->SetWorldPosition(pos);
  603. }
  604. void SceneView3D::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  605. {
  606. if (dragNode_.Null())
  607. return;
  608. Input* input = GetSubsystem<Input>();
  609. if (!input->IsMouseVisible())
  610. return;
  611. using namespace MouseMove;
  612. int x = eventData[P_X].GetInt();
  613. int y = eventData[P_Y].GetInt();
  614. UpdateDragNode(x, y);
  615. }
  616. void SceneView3D::HandleDragEnterWidget(StringHash eventType, VariantMap& eventData)
  617. {
  618. using namespace DragEnterWidget;
  619. UIWidget* widget = static_cast<UIWidget*>(eventData[P_WIDGET].GetPtr());
  620. if (widget != this)
  621. return;
  622. UIDragObject* dragObject = static_cast<UIDragObject*>(eventData[P_DRAGOBJECT].GetPtr());
  623. Object* object = dragObject->GetObject();
  624. if (!object)
  625. return;
  626. if (object->GetType() == Asset::GetTypeStatic())
  627. {
  628. Asset* asset = (Asset*) object;
  629. dragNode_ = asset->InstantiateNode(scene_, asset->GetName());
  630. if (dragNode_.NotNull())
  631. {
  632. Input* input = GetSubsystem<Input>();
  633. IntVector2 pos = input->GetMousePosition();
  634. UpdateDragNode(pos.x_, pos.y_);
  635. }
  636. //LOGINFOF("Dropped %s : %s on SceneView3D", asset->GetPath().CString(), asset->GetGUID().CString());
  637. }
  638. }
  639. void SceneView3D::HandleDragExitWidget(StringHash eventType, VariantMap& eventData)
  640. {
  641. if (preloadResourceScene_.NotNull())
  642. {
  643. preloadResourceScene_->StopAsyncLoading();
  644. preloadResourceScene_ = 0;
  645. }
  646. if (dragNode_.NotNull())
  647. {
  648. scene_->RemoveChild(dragNode_);
  649. }
  650. dragAssetGUID_ = "";
  651. dragNode_ = 0;
  652. }
  653. void SceneView3D::HandleDragEnded(StringHash eventType, VariantMap& eventData)
  654. {
  655. using namespace DragEnded;
  656. UIDragObject* dragObject = static_cast<UIDragObject*>(eventData[P_DRAGOBJECT].GetPtr());
  657. if (dragNode_.NotNull())
  658. {
  659. VariantMap nodeCreatedEvent;
  660. nodeCreatedEvent[SceneEditNodeCreated::P_NODE] = dragNode_;
  661. scene_->SendEvent(E_SCENEEDITNODECREATED, nodeCreatedEvent);
  662. }
  663. if (dragObject && dragObject->GetObject()->GetType() == ToolCore::Asset::GetTypeStatic())
  664. {
  665. Asset* asset = (ToolCore::Asset*) dragObject->GetObject();
  666. if (asset->GetImporterTypeName() == "MaterialImporter") {
  667. Material* material = GetSubsystem<ResourceCache>()->GetResource<Material>(asset->GetPath());
  668. if (material) {
  669. material = material;
  670. Ray camRay = GetCameraRay();
  671. PODVector<RayQueryResult> result;
  672. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  673. octree_->RaycastSingle(query);
  674. if (query.result_.Size())
  675. {
  676. const RayQueryResult& r = result[0];
  677. if (r.drawable_ && (r.drawable_->GetType() == StaticModel::GetTypeStatic() || r.drawable_->GetType() == AnimatedModel::GetTypeStatic()))
  678. {
  679. ((StaticModel*)r.drawable_)->SetMaterial(material);
  680. }
  681. }
  682. }
  683. }
  684. }
  685. dragAssetGUID_ = "";
  686. dragNode_ = 0;
  687. }
  688. void SceneView3D::FrameSelection()
  689. {
  690. BoundingBox bbox;
  691. sceneEditor_->GetSelection()->GetBounds(bbox);
  692. if (!bbox.defined_)
  693. return;
  694. Sphere sphere(bbox);
  695. if (sphere.radius_ < .01f || sphere.radius_ > 512)
  696. return;
  697. framedBBox_ = bbox;
  698. cameraMoveStart_ = cameraNode_->GetWorldPosition();
  699. cameraMoveTarget_ = bbox.Center() - (cameraNode_->GetWorldDirection() * sphere.radius_ * 3);
  700. cameraMoveTime_ = 0.0f;
  701. cameraMove_ = true;
  702. }
  703. }