SceneView3D.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. 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. 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, HANDLER(SceneView3D, HandleUpdate));
  103. SubscribeToEvent(E_POSTRENDERUPDATE, HANDLER(SceneView3D, HandlePostRenderUpdate));
  104. SubscribeToEvent(E_MOUSEMOVE, HANDLER(SceneView3D,HandleMouseMove));
  105. SubscribeToEvent(this, E_DRAGENTERWIDGET, HANDLER(SceneView3D, HandleDragEnterWidget));
  106. SubscribeToEvent(this, E_DRAGEXITWIDGET, HANDLER(SceneView3D, HandleDragExitWidget));
  107. SubscribeToEvent(this, E_DRAGENDED, HANDLER(SceneView3D, HandleDragEnded));
  108. SubscribeToEvent(E_UIUNHANDLEDSHORTCUT, HANDLER(SceneView3D, HandleUIUnhandledShortcut));
  109. SubscribeToEvent(E_UIWIDGETFOCUSESCAPED, 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_ = 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. if (camera_->IsOrthographic() && mouseInView && !orbitting)
  269. {
  270. if (input->GetMouseMoveWheel() > 0)
  271. camera_->SetZoom(camera_->GetZoom() + ZOOM_INCREMENT);
  272. if (input->GetMouseMoveWheel() < 0)
  273. camera_->SetZoom(camera_->GetZoom() - ZOOM_INCREMENT);
  274. }
  275. else
  276. camera_->SetZoom(DEFAULT_ZOOM);
  277. }
  278. void SceneView3D::SnapCameraToView(int snapView)
  279. {
  280. // the distance the camera snaps from the selected object
  281. const int DISTANCE = 5;
  282. fromOrthographic_ = true;
  283. // if no object is selected will snap to view relative to camera position
  284. if (sceneEditor_->GetSelection()->GetNodes().Size() > 0)
  285. {
  286. Vector3 selectedNodePos = sceneEditor_->GetSelection()->GetSelectedNode(0)->GetPosition();
  287. switch (snapView)
  288. {
  289. case CAMERASNAP_TOP:
  290. yaw_ = 0;
  291. pitch_ = 90;
  292. selectedNodePos.y_ += DISTANCE;
  293. break;
  294. case CAMERASNAP_BOTTOM:
  295. yaw_ = 0;
  296. pitch_ = -90;
  297. selectedNodePos.y_ -= DISTANCE;
  298. break;
  299. case CAMERASNAP_LEFT:
  300. yaw_ = -90;
  301. pitch_ = 0;
  302. selectedNodePos.x_ += DISTANCE;
  303. break;
  304. case CAMERASNAP_RIGHT:
  305. yaw_ = 90;
  306. pitch_ = 0;
  307. selectedNodePos.x_ -= DISTANCE;
  308. break;
  309. case CAMERASNAP_FRONT:
  310. yaw_ = 0;
  311. pitch_ = 0;
  312. selectedNodePos.z_ -= DISTANCE;
  313. break;
  314. case CAMERASNAP_BACK:
  315. yaw_ = 180;
  316. pitch_ = 0;
  317. selectedNodePos.z_ += DISTANCE;
  318. break;
  319. }
  320. cameraNode_->SetPosition(selectedNodePos);
  321. camera_->SetOrthographic(true);
  322. }
  323. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0));
  324. }
  325. void SceneView3D::SelectView()
  326. {
  327. if (MouseInView())
  328. {
  329. Input* input = GetSubsystem<Input>();
  330. int snapView = 0;
  331. if (!camera_->IsOrthographic() && !fromOrthographic_)
  332. SavePerspectiveCameraPosition();
  333. if (input->GetKeyPress(KEY_1))
  334. snapView = CAMERASNAP_TOP;
  335. if (input->GetKeyPress(KEY_2))
  336. snapView = CAMERASNAP_BOTTOM;
  337. if (input->GetKeyPress(KEY_3))
  338. snapView = CAMERASNAP_LEFT;
  339. if (input->GetKeyPress(KEY_4))
  340. snapView = CAMERASNAP_RIGHT;
  341. if (input->GetKeyPress(KEY_5))
  342. snapView = CAMERASNAP_FRONT;
  343. if (input->GetKeyPress(KEY_6))
  344. snapView = CAMERASNAP_BACK;
  345. if (snapView != 0)
  346. SnapCameraToView(snapView);
  347. if (input->GetKeyPress(KEY_P))
  348. {
  349. fromOrthographic_ = false;
  350. camera_->SetOrthographic(false);
  351. pitch_ = perspectivePitch_;
  352. yaw_ = perspectiveYaw_;
  353. cameraNode_->SetPosition(perspectCamPosition_);
  354. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0));
  355. }
  356. if (input->GetKeyPress(KEY_O))
  357. {
  358. if (!camera_->IsOrthographic())
  359. SavePerspectiveCameraPosition();
  360. camera_->SetOrthographic(!camera_->IsOrthographic());
  361. }
  362. }
  363. }
  364. void SceneView3D::SavePerspectiveCameraPosition()
  365. {
  366. perspectCamPosition_ = cameraNode_->GetPosition();
  367. perspectivePitch_ = pitch_;
  368. perspectiveYaw_ = yaw_;
  369. }
  370. Ray SceneView3D::GetCameraRay()
  371. {
  372. Ray camRay;
  373. Input* input = GetSubsystem<Input>();
  374. IntVector2 cpos = input->GetMousePosition();
  375. IntRect rect = GetRect();
  376. if (!rect.Width() || !rect.Height())
  377. return camRay;
  378. int x = rect.left_;
  379. int y = rect.top_;
  380. GetInternalWidget()->ConvertToRoot(x, y);
  381. return camera_->GetScreenRay(float(cpos.x_ - x) / rect.Width(),
  382. float(cpos.y_ - y) / rect.Height());
  383. }
  384. bool SceneView3D::MouseInView()
  385. {
  386. if (!GetInternalWidget())
  387. return false;
  388. if (!TBWidget::hovered_widget || TBWidget::hovered_widget->GetDelegate() != this)
  389. return false;
  390. Input* input = GetSubsystem<Input>();
  391. IntVector2 pos = input->GetMousePosition();
  392. IntRect rect = GetRect();
  393. GetInternalWidget()->ConvertToRoot(rect.left_, rect.top_);
  394. GetInternalWidget()->ConvertToRoot(rect.right_, rect.bottom_);
  395. return rect.IsInside(pos);
  396. }
  397. void SceneView3D::HandleUIUnhandledShortcut(StringHash eventType, VariantMap& eventData)
  398. {
  399. if (!enabled_)
  400. return;
  401. unsigned id = eventData[UIUnhandledShortcut::P_REFID].GetUInt();
  402. if (id == TBIDC("undo"))
  403. sceneEditor_->Undo();
  404. else if (id == TBIDC("redo"))
  405. sceneEditor_->Redo();
  406. else if (id == TBIDC("copy"))
  407. sceneEditor_->Copy();
  408. else if (id == TBIDC("cut"))
  409. sceneEditor_->Cut();
  410. else if (id == TBIDC("paste"))
  411. sceneEditor_->Paste();
  412. return;
  413. }
  414. void SceneView3D::HandleUIWidgetFocusEscaped(StringHash eventType, VariantMap& eventData)
  415. {
  416. if (!enabled_)
  417. return;
  418. SetFocus();
  419. }
  420. void SceneView3D::HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData)
  421. {
  422. if (!MouseInView() || GetOrbitting())
  423. return;
  424. Input* input = GetSubsystem<Input>();
  425. mouseLeftDown_ = false;
  426. bool shiftDown = input->GetKeyDown(KEY_LSHIFT) || input->GetKeyDown(KEY_RSHIFT);
  427. if (input->GetMouseButtonPress(MOUSEB_LEFT))
  428. {
  429. SetFocus();
  430. if (!mouseMoved_ && !sceneEditor_->GetGizmo()->Selected())
  431. {
  432. Ray camRay = GetCameraRay();
  433. PODVector<RayQueryResult> result;
  434. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  435. octree_->RaycastSingle(query);
  436. if (query.result_.Size())
  437. {
  438. const RayQueryResult& r = result[0];
  439. if (r.drawable_)
  440. {
  441. VariantMap neventData;
  442. Node* node = r.drawable_->GetNode();
  443. // if temporary, this is a prefab
  444. // TODO: if we use temporary for other stuff
  445. // fix this to look for prefab
  446. if (node->IsTemporary())
  447. node = node->GetParent();
  448. if (sceneEditor_->GetSelection()->Contains(node) && shiftDown)
  449. {
  450. sceneEditor_->GetSelection()->RemoveNode(node);
  451. }
  452. else
  453. {
  454. sceneEditor_->GetSelection()->AddNode(node, !shiftDown);
  455. }
  456. }
  457. }
  458. else
  459. {
  460. sceneEditor_->GetSelection()->Clear();
  461. }
  462. }
  463. mouseMoved_ = false;
  464. }
  465. else if (!input->GetMouseButtonDown(MOUSEB_LEFT))
  466. {
  467. Ray camRay = GetCameraRay();
  468. PODVector<RayQueryResult> result;
  469. mouseMoved_ = false;
  470. /*
  471. Array<int> pickModeDrawableFlags = {
  472. DRAWABLE_GEOMETRY,
  473. DRAWABLE_LIGHT,
  474. DRAWABLE_ZONE
  475. };
  476. */
  477. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  478. octree_->RaycastSingle(query);
  479. if (query.result_.Size())
  480. {
  481. const RayQueryResult& r = result[0];
  482. if (r.drawable_)
  483. {
  484. debugRenderer_->AddNode(r.drawable_->GetNode(), 1.0, false);
  485. r.drawable_->DrawDebugGeometry(debugRenderer_, false);
  486. }
  487. }
  488. }
  489. else
  490. {
  491. mouseLeftDown_ = true;
  492. if (Abs(input->GetMouseMoveX() > 3 || input->GetMouseMoveY() > 3))
  493. {
  494. mouseMoved_ = true;
  495. }
  496. }
  497. }
  498. bool SceneView3D::OnEvent(const TBWidgetEvent &ev)
  499. {
  500. if (ev.type == EVENT_TYPE_SHORTCUT)
  501. {
  502. if (ev.ref_id == TBIDC("close"))
  503. return false;
  504. }
  505. if (ev.type == EVENT_TYPE_KEY_UP)
  506. {
  507. if (ev.special_key == TB_KEY_ESC)
  508. {
  509. sceneEditor_->GetSelection()->Clear();
  510. }
  511. }
  512. if (ev.type == EVENT_TYPE_KEY_DOWN)
  513. {
  514. Input* input = GetSubsystem<Input>();
  515. if (input->GetKeyPress(KEY_G))
  516. gridEnabled_ = !gridEnabled_;
  517. SelectView();
  518. }
  519. return sceneEditor_->OnEvent(ev);
  520. }
  521. void SceneView3D::HandleUpdate(StringHash eventType, VariantMap& eventData)
  522. {
  523. // parent is the contentRoot for our tab, when tab isn't active it will not be visible
  524. if (!GetParent() || GetParent()->GetVisibility() != UI_WIDGET_VISIBILITY_VISIBLE)
  525. {
  526. Disable();
  527. return;
  528. }
  529. Enable();
  530. // Timestep parameter is same no matter what event is being listened to
  531. float timeStep = eventData[Update::P_TIMESTEP].GetFloat();
  532. MoveCamera(timeStep);
  533. QueueUpdate();
  534. if (gridEnabled_)
  535. debugRenderer_->CreateGrid(Color::GRAY, true, cameraNode_->GetPosition());
  536. if (preloadResourceScene_.NotNull())
  537. {
  538. if (preloadResourceScene_->GetAsyncProgress() == 1.0f)
  539. {
  540. ResourceCache* cache = GetSubsystem<ResourceCache>();
  541. XMLFile* xml = cache->GetResource<XMLFile>(dragAssetGUID_);
  542. if (dragNode_.NotNull())
  543. {
  544. dragNode_->LoadXML(xml->GetRoot());
  545. UpdateDragNode(0, 0);
  546. AnimationController* controller = dragNode_->GetComponent<AnimationController>();
  547. if (controller)
  548. {
  549. controller->PlayExclusive("Idle", 0, true);
  550. dragNode_->GetScene()->SetUpdateEnabled(true);
  551. }
  552. }
  553. preloadResourceScene_ = 0;
  554. dragAssetGUID_ = "";
  555. }
  556. }
  557. }
  558. void SceneView3D::UpdateDragNode(int mouseX, int mouseY)
  559. {
  560. if (dragNode_.Null())
  561. return;
  562. Ray ray = GetCameraRay();
  563. Vector3 pos = ray.origin_;
  564. pos += ray.direction_ * 10;
  565. dragNode_->SetWorldPosition(pos);
  566. }
  567. void SceneView3D::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  568. {
  569. if (dragNode_.Null())
  570. return;
  571. Input* input = GetSubsystem<Input>();
  572. if (!input->IsMouseVisible())
  573. return;
  574. using namespace MouseMove;
  575. int x = eventData[P_X].GetInt();
  576. int y = eventData[P_Y].GetInt();
  577. UpdateDragNode(x, y);
  578. }
  579. void SceneView3D::HandleDragEnterWidget(StringHash eventType, VariantMap& eventData)
  580. {
  581. using namespace DragEnterWidget;
  582. UIWidget* widget = static_cast<UIWidget*>(eventData[P_WIDGET].GetPtr());
  583. if (widget != this)
  584. return;
  585. UIDragObject* dragObject = static_cast<UIDragObject*>(eventData[P_DRAGOBJECT].GetPtr());
  586. Object* object = dragObject->GetObject();
  587. if (!object)
  588. return;
  589. if (object->GetType() == Asset::GetTypeStatic())
  590. {
  591. Asset* asset = (Asset*) object;
  592. dragNode_ = asset->InstantiateNode(scene_, asset->GetName());
  593. if (dragNode_.NotNull())
  594. {
  595. Input* input = GetSubsystem<Input>();
  596. IntVector2 pos = input->GetMousePosition();
  597. UpdateDragNode(pos.x_, pos.y_);
  598. }
  599. //LOGINFOF("Dropped %s : %s on SceneView3D", asset->GetPath().CString(), asset->GetGUID().CString());
  600. }
  601. }
  602. void SceneView3D::HandleDragExitWidget(StringHash eventType, VariantMap& eventData)
  603. {
  604. if (preloadResourceScene_.NotNull())
  605. {
  606. preloadResourceScene_->StopAsyncLoading();
  607. preloadResourceScene_ = 0;
  608. }
  609. if (dragNode_.NotNull())
  610. {
  611. scene_->RemoveChild(dragNode_);
  612. }
  613. dragAssetGUID_ = "";
  614. dragNode_ = 0;
  615. }
  616. void SceneView3D::HandleDragEnded(StringHash eventType, VariantMap& eventData)
  617. {
  618. using namespace DragEnded;
  619. UIDragObject* dragObject = static_cast<UIDragObject*>(eventData[P_DRAGOBJECT].GetPtr());
  620. if (dragNode_.NotNull())
  621. {
  622. VariantMap nodeCreatedEvent;
  623. nodeCreatedEvent[SceneEditNodeCreated::P_NODE] = dragNode_;
  624. scene_->SendEvent(E_SCENEEDITNODECREATED, nodeCreatedEvent);
  625. }
  626. if (dragObject && dragObject->GetObject()->GetType() == ToolCore::Asset::GetTypeStatic())
  627. {
  628. Asset* asset = (ToolCore::Asset*) dragObject->GetObject();
  629. if (asset->GetImporterTypeName() == "MaterialImporter") {
  630. Material* material = GetSubsystem<ResourceCache>()->GetResource<Material>(asset->GetPath());
  631. if (material) {
  632. material = material;
  633. Ray camRay = GetCameraRay();
  634. PODVector<RayQueryResult> result;
  635. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  636. octree_->RaycastSingle(query);
  637. if (query.result_.Size())
  638. {
  639. const RayQueryResult& r = result[0];
  640. if (r.drawable_ && (r.drawable_->GetType() == StaticModel::GetTypeStatic() || r.drawable_->GetType() == AnimatedModel::GetTypeStatic()))
  641. {
  642. ((StaticModel*)r.drawable_)->SetMaterial(material);
  643. }
  644. }
  645. }
  646. }
  647. }
  648. dragAssetGUID_ = "";
  649. dragNode_ = 0;
  650. }
  651. void SceneView3D::FrameSelection()
  652. {
  653. BoundingBox bbox;
  654. sceneEditor_->GetSelection()->GetBounds(bbox);
  655. if (!bbox.defined_)
  656. return;
  657. Sphere sphere(bbox);
  658. if (sphere.radius_ < .01f || sphere.radius_ > 512)
  659. return;
  660. framedBBox_ = bbox;
  661. cameraMoveStart_ = cameraNode_->GetWorldPosition();
  662. cameraMoveTarget_ = bbox.Center() - (cameraNode_->GetWorldDirection() * sphere.radius_ * 3);
  663. cameraMoveTime_ = 0.0f;
  664. cameraMove_ = true;
  665. }
  666. }