SceneView3D.cpp 20 KB

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