SceneView3D.cpp 24 KB

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