Hello3DUI.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Urho3D/Core/CoreEvents.h>
  23. #include <Urho3D/Engine/Engine.h>
  24. #include <Urho3D/Graphics/Graphics.h>
  25. #include <Urho3D/Graphics/Texture2D.h>
  26. #include <Urho3D/Graphics/Zone.h>
  27. #include <Urho3D/Graphics/StaticModel.h>
  28. #include <Urho3D/Graphics/Model.h>
  29. #include <Urho3D/Graphics/Technique.h>
  30. #include <Urho3D/Graphics/Octree.h>
  31. #include <Urho3D/Input/Input.h>
  32. #include <Urho3D/Resource/ResourceCache.h>
  33. #include <Urho3D/UI/Button.h>
  34. #include <Urho3D/UI/CheckBox.h>
  35. #include <Urho3D/UI/LineEdit.h>
  36. #include <Urho3D/UI/Text.h>
  37. #include <Urho3D/UI/ToolTip.h>
  38. #include <Urho3D/UI/UI.h>
  39. #include <Urho3D/UI/UIEvents.h>
  40. #include <Urho3D/UI/Window.h>
  41. #include <Urho3D/UI/ListView.h>
  42. #include <Urho3D/UI/UIComponent.h>
  43. #include "Hello3DUI.h"
  44. #include <Urho3D/DebugNew.h>
  45. URHO3D_DEFINE_APPLICATION_MAIN(Hello3DUI)
  46. Hello3DUI::Hello3DUI(Context* context) :
  47. Sample(context),
  48. uiRoot_(GetSubsystem<UI>()->GetRoot()),
  49. dragBeginPosition_(IntVector2::ZERO),
  50. animateCube_(true),
  51. renderOnCube_(false),
  52. drawDebug_(false)
  53. {
  54. }
  55. void Hello3DUI::Start()
  56. {
  57. // Execute base class startup
  58. Sample::Start();
  59. // Enable OS cursor
  60. GetSubsystem<Input>()->SetMouseVisible(true);
  61. // Load XML file containing default UI style sheet
  62. auto* cache = GetSubsystem<ResourceCache>();
  63. auto* style = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  64. // Set the loaded style as default style
  65. uiRoot_->SetDefaultStyle(style);
  66. // Initialize Scene
  67. InitScene();
  68. // Initialize Window
  69. InitWindow();
  70. // Create and add some controls to the Window
  71. InitControls();
  72. // Create a draggable Fish
  73. CreateDraggableFish();
  74. // Create 3D UI rendered on a cube.
  75. Init3DUI();
  76. // Set the mouse mode to use in the sample
  77. Sample::InitMouseMode(MM_FREE);
  78. }
  79. void Hello3DUI::InitControls()
  80. {
  81. // Create a CheckBox
  82. auto* checkBox = new CheckBox(context_);
  83. checkBox->SetName("CheckBox");
  84. // Create a Button
  85. auto* button = new Button(context_);
  86. button->SetName("Button");
  87. button->SetMinHeight(24);
  88. // Create a LineEdit
  89. auto* lineEdit = new LineEdit(context_);
  90. lineEdit->SetName("LineEdit");
  91. lineEdit->SetMinHeight(24);
  92. // Add controls to Window
  93. window_->AddChild(checkBox);
  94. window_->AddChild(button);
  95. window_->AddChild(lineEdit);
  96. // Apply previously set default style
  97. checkBox->SetStyleAuto();
  98. button->SetStyleAuto();
  99. lineEdit->SetStyleAuto();
  100. instructions_ = new Text(context_);
  101. instructions_->SetStyleAuto();
  102. instructions_->SetText("[TAB] - toggle between rendering on screen or cube.\n"
  103. "[Space] - toggle cube rotation.");
  104. uiRoot_->AddChild(instructions_);
  105. }
  106. void Hello3DUI::InitWindow()
  107. {
  108. // Create the Window and add it to the UI's root node
  109. window_ = new Window(context_);
  110. uiRoot_->AddChild(window_);
  111. // Set Window size and layout settings
  112. window_->SetMinWidth(384);
  113. window_->SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6));
  114. window_->SetAlignment(HA_CENTER, VA_CENTER);
  115. window_->SetName("Window");
  116. // Create Window 'titlebar' container
  117. auto* titleBar = new UIElement(context_);
  118. titleBar->SetMinSize(0, 24);
  119. titleBar->SetVerticalAlignment(VA_TOP);
  120. titleBar->SetLayoutMode(LM_HORIZONTAL);
  121. // Create the Window title Text
  122. auto* windowTitle = new Text(context_);
  123. windowTitle->SetName("WindowTitle");
  124. windowTitle->SetText("Hello GUI!");
  125. // Create the Window's close button
  126. auto* buttonClose = new Button(context_);
  127. buttonClose->SetName("CloseButton");
  128. // Add the controls to the title bar
  129. titleBar->AddChild(windowTitle);
  130. titleBar->AddChild(buttonClose);
  131. // Add the title bar to the Window
  132. window_->AddChild(titleBar);
  133. // Create a list.
  134. auto* list = window_->CreateChild<ListView>();
  135. list->SetSelectOnClickEnd(true);
  136. list->SetHighlightMode(HM_ALWAYS);
  137. list->SetMinHeight(200);
  138. for (int i = 0; i < 32; i++)
  139. {
  140. auto* text = new Text(context_);
  141. text->SetStyleAuto();
  142. text->SetText(ToString("List item %d", i));
  143. text->SetName(ToString("Item %d", i));
  144. list->AddItem(text);
  145. }
  146. // Apply styles
  147. window_->SetStyleAuto();
  148. list->SetStyleAuto();
  149. windowTitle->SetStyleAuto();
  150. buttonClose->SetStyle("CloseButton");
  151. // Subscribe to buttonClose release (following a 'press') events
  152. SubscribeToEvent(buttonClose, E_RELEASED, URHO3D_HANDLER(Hello3DUI, HandleClosePressed));
  153. // Subscribe also to all UI mouse clicks just to see where we have clicked
  154. SubscribeToEvent(E_UIMOUSECLICK, URHO3D_HANDLER(Hello3DUI, HandleControlClicked));
  155. }
  156. void Hello3DUI::InitScene()
  157. {
  158. auto* cache = GetSubsystem<ResourceCache>();
  159. scene_ = new Scene(context_);
  160. scene_->CreateComponent<Octree>();
  161. auto* zone = scene_->CreateComponent<Zone>();
  162. zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
  163. zone->SetFogColor(Color::GRAY);
  164. zone->SetFogStart(100.0f);
  165. zone->SetFogEnd(300.0f);
  166. // Create a child scene node (at world origin) and a StaticModel component into it.
  167. Node* boxNode = scene_->CreateChild("Box");
  168. boxNode->SetScale(Vector3(5.0f, 5.0f, 5.0f));
  169. boxNode->SetRotation(Quaternion(90, Vector3::LEFT));
  170. // Create a box model and hide it initially.
  171. auto* boxModel = boxNode->CreateComponent<StaticModel>();
  172. boxModel->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
  173. boxNode->SetEnabled(false);
  174. // Create a camera.
  175. cameraNode_ = scene_->CreateChild("Camera");
  176. cameraNode_->CreateComponent<Camera>();
  177. // Set an initial position for the camera scene node.
  178. cameraNode_->SetPosition(Vector3(0.0f, 0.0f, -10.0f));
  179. // Set up a viewport so 3D scene can be visible.
  180. auto* renderer = GetSubsystem<Renderer>();
  181. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  182. renderer->SetViewport(0, viewport);
  183. // Subscribe to update event and animate cube and handle input.
  184. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(Hello3DUI, HandleUpdate));
  185. }
  186. void Hello3DUI::CreateDraggableFish()
  187. {
  188. auto* cache = GetSubsystem<ResourceCache>();
  189. auto* graphics = GetSubsystem<Graphics>();
  190. // Create a draggable Fish button
  191. auto* draggableFish = new Button(context_);
  192. draggableFish->SetTexture(cache->GetResource<Texture2D>("Textures/UrhoDecal.dds")); // Set texture
  193. draggableFish->SetBlendMode(BLEND_ADD);
  194. draggableFish->SetSize(128, 128);
  195. draggableFish->SetPosition((graphics->GetWidth() - draggableFish->GetWidth()) / 2, 200);
  196. draggableFish->SetName("Fish");
  197. uiRoot_->AddChild(draggableFish);
  198. // Add a tooltip to Fish button
  199. auto* toolTip = new ToolTip(context_);
  200. draggableFish->AddChild(toolTip);
  201. toolTip->SetPosition(IntVector2(draggableFish->GetWidth() + 5, draggableFish->GetWidth() / 2)); // slightly offset from close button
  202. auto* textHolder = new BorderImage(context_);
  203. toolTip->AddChild(textHolder);
  204. textHolder->SetStyle("ToolTipBorderImage");
  205. auto* toolTipText = new Text(context_);
  206. textHolder->AddChild(toolTipText);
  207. toolTipText->SetStyle("ToolTipText");
  208. toolTipText->SetText("Please drag me!");
  209. // Subscribe draggableFish to Drag Events (in order to make it draggable)
  210. // See "Event list" in documentation's Main Page for reference on available Events and their eventData
  211. SubscribeToEvent(draggableFish, E_DRAGBEGIN, URHO3D_HANDLER(Hello3DUI, HandleDragBegin));
  212. SubscribeToEvent(draggableFish, E_DRAGMOVE, URHO3D_HANDLER(Hello3DUI, HandleDragMove));
  213. SubscribeToEvent(draggableFish, E_DRAGEND, URHO3D_HANDLER(Hello3DUI, HandleDragEnd));
  214. }
  215. void Hello3DUI::HandleDragBegin(StringHash eventType, VariantMap& eventData)
  216. {
  217. // Get UIElement relative position where input (touch or click) occurred (top-left = IntVector2(0,0))
  218. dragBeginPosition_ = IntVector2(eventData["ElementX"].GetInt(), eventData["ElementY"].GetInt());
  219. }
  220. void Hello3DUI::HandleDragMove(StringHash eventType, VariantMap& eventData)
  221. {
  222. IntVector2 dragCurrentPosition = IntVector2(eventData["X"].GetInt(), eventData["Y"].GetInt());
  223. UIElement* draggedElement = static_cast<UIElement*>(eventData["Element"].GetPtr());
  224. draggedElement->SetPosition(dragCurrentPosition - dragBeginPosition_);
  225. }
  226. void Hello3DUI::HandleDragEnd(StringHash eventType, VariantMap& eventData) // For reference (not used here)
  227. {
  228. }
  229. void Hello3DUI::HandleClosePressed(StringHash eventType, VariantMap& eventData)
  230. {
  231. if (GetPlatform() != "Web")
  232. engine_->Exit();
  233. }
  234. void Hello3DUI::HandleControlClicked(StringHash eventType, VariantMap& eventData)
  235. {
  236. // Get the Text control acting as the Window's title
  237. auto* windowTitle = window_->GetChildStaticCast<Text>("WindowTitle", true);
  238. // Get control that was clicked
  239. auto* clicked = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
  240. String name = "...?";
  241. if (clicked)
  242. {
  243. // Get the name of the control that was clicked
  244. name = clicked->GetName();
  245. }
  246. // Update the Window's title text
  247. windowTitle->SetText("Hello " + name + "!");
  248. }
  249. void Hello3DUI::Init3DUI()
  250. {
  251. auto* cache = GetSubsystem<ResourceCache>();
  252. // Node that will get UI rendered on it.
  253. Node* boxNode = scene_->GetChild("Box");
  254. // Create a component that sets up UI rendering. It sets material to StaticModel of the node.
  255. auto* component = boxNode->CreateComponent<UIComponent>();
  256. // Optionally modify material. Technique is changed so object is visible without any lights.
  257. component->GetMaterial()->SetTechnique(0, cache->GetResource<Technique>("Techniques/DiffUnlit.xml"));
  258. // Save root element of texture UI for later use.
  259. textureRoot_ = component->GetRoot();
  260. // Set size of root element. This is size of texture as well.
  261. textureRoot_->SetSize(512, 512);
  262. }
  263. void Hello3DUI::HandleUpdate(StringHash, VariantMap& eventData)
  264. {
  265. using namespace Update;
  266. float timeStep = eventData[P_TIMESTEP].GetFloat();
  267. auto* input = GetSubsystem<Input>();
  268. Node* node = scene_->GetChild("Box");
  269. if (current_.NotNull() && drawDebug_)
  270. GetSubsystem<UI>()->DebugDraw(current_);
  271. if (input->GetMouseButtonPress(MOUSEB_LEFT))
  272. current_ = GetSubsystem<UI>()->GetElementAt(input->GetMousePosition());
  273. if (input->GetKeyPress(KEY_TAB))
  274. {
  275. renderOnCube_ = !renderOnCube_;
  276. // Toggle between rendering on screen or to texture.
  277. if (renderOnCube_)
  278. {
  279. node->SetEnabled(true);
  280. textureRoot_->AddChild(window_);
  281. }
  282. else
  283. {
  284. node->SetEnabled(false);
  285. uiRoot_->AddChild(window_);
  286. }
  287. }
  288. if (input->GetKeyPress(KEY_SPACE))
  289. animateCube_ = !animateCube_;
  290. if (input->GetKeyPress(KEY_F2))
  291. drawDebug_ = !drawDebug_;
  292. if (animateCube_)
  293. {
  294. node->Yaw(6.0f * timeStep * 1.5f);
  295. node->Roll(-6.0f * timeStep * 1.5f);
  296. node->Pitch(-6.0f * timeStep * 1.5f);
  297. }
  298. }