HelloGui3D.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. //
  2. // Copyright (c) 2014-2017, THUNDERBEAST GAMES LLC All rights reserved
  3. // Copyright (c) 2008-2016 the Urho3D project.
  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/Core/CoreEvents.h>
  24. #include <Atomic/Core/ProcessUtils.h>
  25. #include <Atomic/Input/Input.h>
  26. #include <Atomic/Graphics/Graphics.h>
  27. #include <Atomic/Graphics/Camera.h>
  28. #include <Atomic/Graphics/Graphics.h>
  29. #include <Atomic/Graphics/Material.h>
  30. #include <Atomic/Graphics/Model.h>
  31. #include <Atomic/Graphics/Octree.h>
  32. #include <Atomic/Graphics/Renderer.h>
  33. #include <Atomic/Graphics/StaticModel.h>
  34. #include <Atomic/Graphics/Texture2D.h>
  35. #include <Atomic/Graphics/Technique.h>
  36. #include <Atomic/Graphics/ParticleEffect.h>
  37. #include <Atomic/Graphics/ParticleEmitter.h>
  38. #include <Atomic/UI/UI.h>
  39. #include <Atomic/UI/UIEvents.h>
  40. #include <Atomic/UI/UIFontDescription.h>
  41. #include <Atomic/UI/UIView.h>
  42. #include <Atomic/UI/UIComponent.h>
  43. #include <Atomic/UI/UILayout.h>
  44. #include <Atomic/UI/UICheckBox.h>
  45. #include <Atomic/UI/UIImageWidget.h>
  46. #include <Atomic/UI/UITextField.h>
  47. #include <Atomic/UI/UIButton.h>
  48. #include <Atomic/UI/UIEditField.h>
  49. #include <Atomic/UI/UISeparator.h>
  50. #include <Atomic/UI/UIWindow.h>
  51. #include <Atomic/Resource/ResourceCache.h>
  52. #include <Atomic/Scene/Scene.h>
  53. #include "FeatureExamples.h"
  54. #include "HelloGui3D.h"
  55. #include <Atomic/DebugNew.h>
  56. HelloGui3D::HelloGui3D(Context* context) :
  57. Sample(context)
  58. {
  59. }
  60. void HelloGui3D::Start()
  61. {
  62. // Execute base class startup
  63. Sample::Start();
  64. SimpleCreateInstructions("Shift click on 3D UI to spawn fire");
  65. // Create 2D "Hello GUI"
  66. CreateUI();
  67. // Create the scene content
  68. CreateScene();
  69. SetupViewport();
  70. // Finally subscribe to the update event. Note that by subscribing events at this point we have already missed some events
  71. // like the ScreenMode event sent by the Graphics subsystem when opening the application window. To catch those as well we
  72. // could subscribe in the constructor instead.
  73. SubscribeToEvents();
  74. // Set the mouse mode to use in the sample
  75. Sample::InitMouseMode(MM_FREE);
  76. }
  77. void HelloGui3D::Cleanup()
  78. {
  79. if (view3D_)
  80. {
  81. view3D_->Remove();
  82. }
  83. }
  84. void HelloGui3D::SetupViewport()
  85. {
  86. Renderer* renderer = GetSubsystem<Renderer>();
  87. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
  88. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  89. // use, but now we just use full screen and default render path configured in the engine command line options
  90. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  91. renderer->SetViewport(0, viewport);
  92. }
  93. void HelloGui3D::CreateScene()
  94. {
  95. ResourceCache* cache = GetSubsystem<ResourceCache>();
  96. scene_ = new Scene(context_);
  97. // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  98. // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it
  99. // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  100. // optimizing manner
  101. scene_->CreateComponent<Octree>();
  102. // Create a child scene node (at world origin) and a StaticModel component into it. Set the StaticModel to show a simple
  103. // plane mesh with a "stone" material. Note that naming the scene nodes is optional. Scale the scene node larger
  104. // (100 x 100 world units)
  105. Node* planeNode = scene_->CreateChild("Box");
  106. planeNode->SetScale(Vector3(5.0f, 5.0f, 5.0f));
  107. planeNode->SetRotation(Quaternion(90, Vector3::LEFT));
  108. StaticModel* planeObject = planeNode->CreateComponent<StaticModel>();
  109. planeObject->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
  110. // Create a UIComponent and set it to render on the box model
  111. uiComponent_ = planeNode->CreateComponent<UIComponent>();
  112. uiComponent_->SetStaticModel(planeObject);
  113. // Create the same UIView as 2D, though for 3D, and set it as component's view
  114. uiComponent_->SetUIView(CreateUI(true));
  115. // Create a directional light to the world so that we can see something. The light scene node's orientation controls the
  116. // light direction; we will use the SetDirection() function which calculates the orientation from a forward direction vector.
  117. // The light will use default settings (white light, no shadows)
  118. Node* lightNode = scene_->CreateChild("DirectionalLight");
  119. lightNode->SetDirection(Vector3::FORWARD); // The direction vector does not need to be normalized
  120. Light* light = lightNode->CreateComponent<Light>();
  121. light->SetLightType(LIGHT_DIRECTIONAL);
  122. // Create a scene node for the camera, which we will move around
  123. // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  124. cameraNode_ = scene_->CreateChild("Camera");
  125. cameraNode_->CreateComponent<Camera>();
  126. // Set an initial position for the camera scene node above the plane
  127. cameraNode_->SetPosition(Vector3(-3.0f, 0.0f, -30.0f));
  128. }
  129. UIView* HelloGui3D::CreateUI(bool renderToTexture)
  130. {
  131. int size = 220;
  132. UIView* view = renderToTexture ? new UIView(context_) : FeatureExamples::GetUIView();
  133. if (renderToTexture)
  134. {
  135. view3D_ = view;
  136. view->SetRenderToTexture(true, size, size);
  137. }
  138. UILayout* mainLayout = new UILayout(context_);
  139. mainLayout->SetAxis(UI_AXIS_Y);
  140. mainLayout->SetSpacing(16);
  141. UILayout* topLayout = new UILayout(context_);
  142. topLayout->SetAxis(UI_AXIS_X);
  143. topLayout->SetSpacing(8);
  144. UIImageWidget* imageWidget = new UIImageWidget(context_);
  145. imageWidget->SetImage("Textures/atomic_logo.png");
  146. topLayout->AddChild(imageWidget);
  147. UILayout* sideLayout = new UILayout(context_);
  148. sideLayout->SetAxis(UI_AXIS_Y);
  149. UICheckBox* checkBox = new UICheckBox(context_);
  150. checkBox->SetId("Checkbox");
  151. sideLayout->AddChild(checkBox);
  152. UIButton* button = new UIButton(context_);
  153. button->SetText("Button");
  154. button->SetId("Button");
  155. sideLayout->AddChild(button);
  156. topLayout->AddChild(sideLayout);
  157. mainLayout->AddChild(topLayout);
  158. UISeparator* sep = new UISeparator(context_);
  159. mainLayout->AddChild(sep);
  160. UIEditField* edit = new UIEditField(context_);
  161. edit->SetLayoutMinWidth(220);
  162. edit->SetId("EditField");
  163. edit->SetTextAlign(UI_TEXT_ALIGN_CENTER);
  164. edit->SetText(renderToTexture ? "I crave adventure!" : "I enjoy a simple life.");
  165. mainLayout->AddChild(edit);
  166. SharedPtr<UIWindow> window( new UIWindow(context_) );
  167. UI_WINDOW_SETTINGS settings = (UI_WINDOW_SETTINGS) (renderToTexture ? (UI_WINDOW_SETTINGS_NONE) :
  168. (UI_WINDOW_SETTINGS_TITLEBAR | UI_WINDOW_SETTINGS_CLOSE_BUTTON));
  169. window->SetSettings(settings);
  170. window->SetText(renderToTexture ? "GUI 3D" : "GUI 2D");
  171. window->AddChild(mainLayout);
  172. window->SetSize(renderToTexture ? view->GetWidth() : size, renderToTexture ? view->GetHeight() : size);
  173. view->AddChild(window);
  174. if (!renderToTexture)
  175. {
  176. window->SetPosition(view->GetWidth()/4, view->GetHeight()/2 - (size/2));
  177. }
  178. else
  179. {
  180. window->Center();
  181. }
  182. return view;
  183. }
  184. bool HelloGui3D::Raycast(float maxDistance, Vector3& hitPos, Vector3& hitNormal, Drawable*& hitDrawable)
  185. {
  186. hitDrawable = 0;
  187. Input* input = GetSubsystem<Input>();
  188. IntVector2 pos = input->GetMousePosition();
  189. // Check the cursor is visible and there is no UI element in front of the cursor
  190. if (!input->IsMouseVisible())
  191. return false;
  192. Graphics* graphics = GetSubsystem<Graphics>();
  193. Camera* camera = cameraNode_->GetComponent<Camera>();
  194. Ray cameraRay = camera->GetScreenRay((float)pos.x_ / graphics->GetWidth(), (float)pos.y_ / graphics->GetHeight());
  195. // Pick only geometry objects, not eg. zones or lights, only get the first (closest) hit
  196. PODVector<RayQueryResult> results;
  197. RayOctreeQuery query(results, cameraRay, RAY_TRIANGLE, maxDistance, DRAWABLE_GEOMETRY);
  198. scene_->GetComponent<Octree>()->Raycast(query);
  199. for (unsigned i = 0; i < results.Size(); i++)
  200. {
  201. RayQueryResult& result = results[i];
  202. if (uiComponent_->GetStaticModel() != result.drawable_)
  203. continue;
  204. hitPos = result.position_;
  205. hitNormal = result.normal_;
  206. hitDrawable = result.drawable_;
  207. return true;
  208. }
  209. return false;
  210. }
  211. void HelloGui3D::SubscribeToEvents()
  212. {
  213. // Subscribe HandleUpdate() function for processing update events
  214. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(HelloGui3D, HandleUpdate));
  215. }
  216. void HelloGui3D::HandleUpdate(StringHash eventType, VariantMap& eventData)
  217. {
  218. using namespace Update;
  219. if (!view3D_)
  220. return;
  221. Vector3 hitPos;
  222. Vector3 hitNormal;
  223. Drawable* hitDrawable;
  224. bool result = Raycast(250.0f, hitPos, hitNormal, hitDrawable);
  225. Input* input = GetSubsystem<Input>();
  226. if (!input->GetMouseButtonDown(MOUSEB_LEFT))
  227. {
  228. if (!result)
  229. {
  230. view3D_->ResignFocus();
  231. }
  232. else
  233. {
  234. view3D_->SetFocus();
  235. }
  236. }
  237. if (input->GetMouseButtonPress(MOUSEB_LEFT) && input->GetQualifierDown(QUAL_SHIFT))
  238. {
  239. ResourceCache* cache = GetSubsystem<ResourceCache>();
  240. Node* node = uiComponent_->GetNode()->CreateChild("GreatBallOfFire");
  241. node->SetScale(0.3f);
  242. node->SetWorldPosition(hitPos + (hitNormal * .15f));
  243. node->SetWorldDirection(hitNormal);
  244. node->Pitch(90);
  245. ParticleEmitter* emitter = node->CreateComponent<ParticleEmitter>();
  246. emitter->SetEffect(cache->GetResource<ParticleEffect>("Particle/UIFire.xml"));
  247. }
  248. // Take the frame time step, which is stored as a float
  249. float timeStep = eventData[P_TIMESTEP].GetFloat();
  250. Node* node = uiComponent_->GetStaticModel()->GetNode();
  251. node->Yaw(6.0f * timeStep * 1.5f);
  252. node->Roll(-6.0f * timeStep * 1.5f);
  253. node->Pitch(-6.0f * timeStep * 1.5f);
  254. Time* time = GetSubsystem<Time>();
  255. Vector3 pos = node->GetPosition();
  256. pos.z_ = 3.0f * Sin<float>(time->GetElapsedTime() * 100.0f * 1.5f);
  257. //node->SetPosition(pos);
  258. }