ModelResourceEditor.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include "AtomicEditor.h"
  5. #include <Atomic/Core/CoreEvents.h>
  6. #include <Atomic/Scene/Scene.h>
  7. #include <Atomic/Graphics/Octree.h>
  8. #include <Atomic/Graphics/Camera.h>
  9. #include <Atomic/Graphics/Zone.h>
  10. #include <Atomic/Graphics/AnimatedModel.h>
  11. #include <Atomic/Graphics/Animation.h>
  12. #include <Atomic/Graphics/AnimationState.h>
  13. #include <Atomic/Graphics/AnimationController.h>
  14. #include <Atomic/Graphics/Material.h>
  15. #include <Atomic/Graphics/DebugRenderer.h>
  16. #include <Atomic/Resource/ResourceCache.h>
  17. #include <Atomic/Input/Input.h>
  18. #include "../AEEditor.h"
  19. #include "ModelResourceEditor.h"
  20. #include <Atomic/UI/TBUI.h>
  21. #include <Atomic/UI/UI.h>
  22. #include <Atomic/UI/View3D.h>
  23. namespace AtomicEditor
  24. {
  25. ModelResourceEditor ::ModelResourceEditor(Context* context, const String &fullpath, TBTabContainer *container) :
  26. ResourceEditor(context, fullpath, container),
  27. layout_(0),
  28. view3DContainer_(0),
  29. yaw_(0.0f),
  30. pitch_(0.0f)
  31. {
  32. layout_ = new TBLayout();
  33. layout_->SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
  34. layout_->SetLayoutSize(LAYOUT_SIZE_AVAILABLE);
  35. layout_->SetSize(container_->GetRect().w, container_->GetRect().h);
  36. view3DContainer_ = new TBContainer();
  37. view3DContainer_->SetGravity(WIDGET_GRAVITY_ALL);
  38. TBLayout* verticalLayout = new TBLayout();
  39. verticalLayout->SetAxis(AXIS_Y);
  40. verticalLayout->SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
  41. TBButton* button = new TBButton();
  42. button->SetText("Idle");
  43. button->SetID(TBIDC("Idle"));
  44. button->SetGravity(WIDGET_GRAVITY_LEFT_RIGHT);
  45. verticalLayout->AddChild(button);
  46. button = new TBButton();
  47. button->SetText("Walk");
  48. button->SetGravity(WIDGET_GRAVITY_LEFT_RIGHT);
  49. button->SetID(TBIDC("Walk"));
  50. verticalLayout->AddChild(button);
  51. button = new TBButton();
  52. button->SetText("Run");
  53. button->SetGravity(WIDGET_GRAVITY_LEFT_RIGHT);
  54. button->SetID(TBIDC("Run"));
  55. verticalLayout->AddChild(button);
  56. button = new TBButton();
  57. button->SetText("Turn Left");
  58. button->SetGravity(WIDGET_GRAVITY_LEFT_RIGHT);
  59. button->SetID(TBIDC("TurnLeft"));
  60. verticalLayout->AddChild(button);
  61. button = new TBButton();
  62. button->SetText("Turn Right");
  63. button->SetGravity(WIDGET_GRAVITY_LEFT_RIGHT);
  64. button->SetID(TBIDC("TurnRight"));
  65. verticalLayout->AddChild(button);
  66. button = new TBButton();
  67. button->SetText("Walk Backwards");
  68. button->SetGravity(WIDGET_GRAVITY_LEFT_RIGHT);
  69. button->SetID(TBIDC("WalkBackwards"));
  70. verticalLayout->AddChild(button);
  71. button = new TBButton();
  72. button->SetText("Attack");
  73. button->SetGravity(WIDGET_GRAVITY_LEFT_RIGHT);
  74. button->SetID(TBIDC("Attack"));
  75. verticalLayout->AddChild(button);
  76. layout_->AddChild(view3DContainer_);
  77. layout_->AddChild(verticalLayout);
  78. container_->GetContentRoot()->AddChild(layout_);
  79. ResourceCache* cache = GetSubsystem<ResourceCache>();
  80. scene_ = new Scene(context_);
  81. scene_ = new Scene(context_);
  82. scene_->CreateComponent<Octree>();
  83. scene_->CreateComponent<DebugRenderer>();
  84. Node* zoneNode = scene_->CreateChild("Zone");
  85. Zone* zone = zoneNode->CreateComponent<Zone>();
  86. zone->SetBoundingBox(BoundingBox(-10000.0f, 10000.f));
  87. zone->SetAmbientColor(Color(1, 1, 1));
  88. cameraNode_ = scene_->CreateChild("Camera");
  89. camera_ = cameraNode_->CreateComponent<Camera>();
  90. cameraNode_->SetPosition(Vector3(0.0f, .9f, -2.75f));
  91. // Create a smaller box at camera position
  92. modelNode_ = scene_->CreateChild("ModelNode");
  93. AnimatedModel* amodel = modelNode_->CreateComponent<AnimatedModel>();
  94. Model* model = cache->GetResource<Model>("Models/AS_FatZombie_FBX_FatZombie_LOD0.mdl");
  95. amodel->SetModel(model);
  96. amodel->SetMaterial(cache->GetResource<Material>("Materials/AS_FatZombie_Base.xml"));
  97. amodel->SetCastShadows(true);
  98. AnimationController* controller = modelNode_->CreateComponent<AnimationController>();
  99. controller->PlayExclusive("Models/AS_FatZombie_FBX_FatZombie_LOD0_Idle.ani", 0, true);
  100. modelNode_->Yaw(180);
  101. view3D_ = new View3D(context_);
  102. view3D_->SetView(scene_, camera_);
  103. view3D_->SetAutoUpdate(false);
  104. GetSubsystem<TBUI>()->AddChild(view3D_);
  105. SubscribeToEvent(E_UPDATE, HANDLER(ModelResourceEditor, HandleUpdate));
  106. }
  107. ModelResourceEditor::~ModelResourceEditor()
  108. {
  109. }
  110. bool ModelResourceEditor::OnEvent(const TBWidgetEvent &ev)
  111. {
  112. if (ev.type == EVENT_TYPE_CLICK)
  113. {
  114. AnimationController* controller = modelNode_->GetComponent<AnimationController>();
  115. if (ev.target->GetID() == TBIDC("Walk"))
  116. {
  117. controller->PlayExclusive("Models/AS_FatZombie_FBX_FatZombie_LOD0_Walk.ani", 0.0f, true);
  118. }
  119. else if (ev.target->GetID() == TBIDC("Run"))
  120. {
  121. controller->PlayExclusive("Models/AS_FatZombie_FBX_FatZombie_LOD0_Run.ani", 0.0f, true);
  122. }
  123. else if (ev.target->GetID() == TBIDC("Idle"))
  124. {
  125. controller->PlayExclusive("Models/AS_FatZombie_FBX_FatZombie_LOD0_Idle.ani", 0.0f, true);
  126. }
  127. else if (ev.target->GetID() == TBIDC("TurnLeft"))
  128. {
  129. controller->PlayExclusive("Models/AS_FatZombie_FBX_FatZombie_LOD0_Turn_Left.ani", 0.0f, true);
  130. }
  131. else if (ev.target->GetID() == TBIDC("TurnRight"))
  132. {
  133. controller->PlayExclusive("Models/AS_FatZombie_FBX_FatZombie_LOD0_Turn_Right.ani", 0.0f, true);
  134. }
  135. else if (ev.target->GetID() == TBIDC("WalkBackwards"))
  136. {
  137. controller->PlayExclusive("Models/AS_FatZombie_FBX_FatZombie_LOD0_Walk_Backwards.ani", 0.0f, true);
  138. }
  139. else if (ev.target->GetID() == TBIDC("Attack"))
  140. {
  141. controller->PlayExclusive("Models/AS_FatZombie_FBX_FatZombie_LOD0_Attack_Melee.ani", 0.0f, true);
  142. }
  143. }
  144. return false;
  145. }
  146. void ModelResourceEditor::MoveCamera(float timeStep)
  147. {
  148. // Do not move if the UI has a focused element (the console)
  149. if (GetSubsystem<UI>()->GetFocusElement())
  150. return;
  151. Input* input = GetSubsystem<Input>();
  152. // Movement speed as world units per second
  153. const float MOVE_SPEED = 1.5f;
  154. // Mouse sensitivity as degrees per pixel
  155. const float MOUSE_SENSITIVITY = 0.2f;
  156. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  157. if (input->GetKeyDown('F'))
  158. {
  159. IntVector2 mouseMove = input->GetMouseMove();
  160. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  161. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  162. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  163. }
  164. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  165. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  166. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  167. // Use the Translate() function (default local space) to move relative to the node's orientation.
  168. if (input->GetKeyDown('W'))
  169. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  170. if (input->GetKeyDown('S'))
  171. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  172. if (input->GetKeyDown('Q'))
  173. cameraNode_->Translate(Vector3::UP * MOVE_SPEED * timeStep);
  174. if (input->GetKeyDown('E'))
  175. cameraNode_->Translate(Vector3::DOWN * MOVE_SPEED * timeStep);
  176. if (input->GetKeyDown('A'))
  177. modelNode_->Yaw(-timeStep * 100);
  178. if (input->GetKeyDown('D'))
  179. modelNode_->Yaw(timeStep * 100);
  180. }
  181. void ModelResourceEditor::HandleUpdate(StringHash eventType, VariantMap& eventData)
  182. {
  183. if ((layout_->GetVisibility() != WIDGET_VISIBILITY_VISIBLE) || GetSubsystem<Editor>()->IsPlayingProject())
  184. {
  185. if (view3D_->IsVisible())
  186. {
  187. view3D_->SetVisible(false);
  188. }
  189. return;
  190. }
  191. if (!view3D_->IsVisible())
  192. view3D_->SetVisible(true);
  193. // Timestep parameter is same no matter what event is being listened to
  194. float timeStep = eventData[Update::P_TIMESTEP].GetFloat();
  195. MoveCamera(timeStep);
  196. bool dirty = false;
  197. TBRect rect = view3DContainer_->GetRect();
  198. TBWidget* parent = view3DContainer_->GetParent();
  199. while (parent)
  200. {
  201. TBRect prect = parent->GetRect();
  202. rect.x += prect.x;
  203. rect.y += prect.y;
  204. parent = parent->GetParent();
  205. }
  206. const IntVector2& pos = view3D_->GetPosition();
  207. if (pos.x_ != rect.x || pos.y_ != rect.y)
  208. dirty = true;
  209. const IntVector2& size = view3D_->GetScreenPosition();
  210. if (size.x_ != rect.w || size.y_ != rect.h)
  211. dirty = true;
  212. if (dirty)
  213. {
  214. view3D_->SetPosition(rect.x, rect.y);
  215. view3D_->SetWidth(rect.w);
  216. view3D_->SetHeight(rect.h);
  217. }
  218. view3D_->QueueUpdate();
  219. }
  220. }