SceneView3D.cpp 23 KB

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