SceneView3D.cpp 24 KB

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