HelloGui3D.cpp 11 KB

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