48_Hello3DUI.as 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /// A 3D UI demonstration based on the HelloGUI sample. Renders UI alternatively
  2. /// either to a 3D scene object using UIComponent, or directly to the backbuffer.
  3. #include "Scripts/Utilities/Sample.as"
  4. Window@ window;
  5. IntVector2 dragBeginPosition = IntVector2::ZERO;
  6. UIElement@ textureRoot;
  7. WeakHandle current;
  8. bool renderOnCube = false;
  9. bool drawDebug_ = false;
  10. bool animateCube = true;
  11. void Start()
  12. {
  13. // Execute the common startup for samples
  14. SampleStart();
  15. // Enable OS cursor
  16. input.mouseVisible = true;
  17. // Load XML file containing default UI style sheet
  18. XMLFile@ style = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
  19. // Set the loaded style as default style
  20. ui.root.defaultStyle = style;
  21. // Initialize Scene
  22. InitScene();
  23. // Initialize Window
  24. InitWindow();
  25. // Create and add some controls to the Window
  26. InitControls();
  27. // Create a draggable Fish
  28. CreateDraggableFish();
  29. // Create 3D UI rendered on a cube.
  30. Init3DUI();
  31. // Set the mouse mode to use in the sample
  32. SampleInitMouseMode(MM_FREE);
  33. }
  34. void InitControls()
  35. {
  36. // Create a CheckBox
  37. CheckBox@ checkBox = CheckBox();
  38. checkBox.name = "CheckBox";
  39. // Create a Button
  40. Button@ button = Button();
  41. button.name = "Button";
  42. button.minHeight = 24;
  43. // Create a LineEdit
  44. LineEdit@ lineEdit = LineEdit();
  45. lineEdit.name = "LineEdit";
  46. lineEdit.minHeight = 24;
  47. // Add controls to Window
  48. window.AddChild(checkBox);
  49. window.AddChild(button);
  50. window.AddChild(lineEdit);
  51. // Apply previously set default style
  52. checkBox.SetStyleAuto();
  53. button.SetStyleAuto();
  54. lineEdit.SetStyleAuto();
  55. Text@ instructions = Text();
  56. instructions.SetStyleAuto();
  57. instructions.text = "[TAB] - toggle between rendering on screen or cube.\n"
  58. "[Space] - toggle cube rotation.";
  59. ui.root.AddChild(instructions);
  60. }
  61. void InitScene()
  62. {
  63. scene_ = Scene();
  64. scene_.CreateComponent("Octree");
  65. Zone@ zone = scene_.CreateComponent("Zone");
  66. zone.boundingBox = BoundingBox(-1000.0f, 1000.0f);
  67. zone.fogColor = Color::GRAY;
  68. zone.fogStart = 100.0f;
  69. zone.fogEnd = 300.0f;
  70. // Create a child scene node (at world origin) and a StaticModel component into it.
  71. Node@ boxNode = scene_.CreateChild("Box");
  72. boxNode.scale = Vector3(5.0f, 5.0f, 5.0f);
  73. boxNode.rotation = Quaternion(90, Vector3::LEFT);
  74. // Create a box model and hide it initially.
  75. StaticModel@ boxModel = boxNode.CreateComponent("StaticModel");
  76. boxModel.model = cache.GetResource("Model", "Models/Box.mdl");
  77. boxNode.enabled = false;
  78. // Create a camera.
  79. cameraNode = scene_.CreateChild("Camera");
  80. cameraNode.CreateComponent("Camera");
  81. // Set an initial position for the camera scene node.
  82. cameraNode.position = Vector3(0.0f, 0.0f, -10.0f);
  83. // Set up a viewport so 3D scene can be visible.
  84. Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
  85. renderer.viewports[0] = viewport;
  86. // Subscribe to update event and animate cube and handle input.
  87. SubscribeToEvent("Update", "HandleUpdate");
  88. }
  89. void InitWindow()
  90. {
  91. // Create the Window and add it to the UI's root node
  92. window = Window();
  93. ui.root.AddChild(window);
  94. // Set Window size and layout settings
  95. window.minWidth = 384;
  96. window.SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6));
  97. window.SetAlignment(HA_CENTER, VA_CENTER);
  98. window.name = "Window";
  99. // Create Window 'titlebar' container
  100. UIElement@ titleBar = UIElement();
  101. titleBar.SetMinSize(0, 24);
  102. titleBar.verticalAlignment = VA_TOP;
  103. titleBar.layoutMode = LM_HORIZONTAL;
  104. // Create the Window title Text
  105. Text@ windowTitle = Text();
  106. windowTitle.name = "WindowTitle";
  107. windowTitle.text = "Hello GUI!";
  108. // Create the Window's close button
  109. Button@ buttonClose = Button();
  110. buttonClose.name = "CloseButton";
  111. // Add the controls to the title bar
  112. titleBar.AddChild(windowTitle);
  113. titleBar.AddChild(buttonClose);
  114. // Add the title bar to the Window
  115. window.AddChild(titleBar);
  116. // Create a list.
  117. ListView@ list = window.CreateChild("ListView");
  118. list.selectOnClickEnd = true;
  119. list.highlightMode = HM_ALWAYS;
  120. list.minHeight = 200;
  121. for (int i = 0; i < 32; i++)
  122. {
  123. Text@ text = Text();
  124. text.SetStyleAuto();
  125. text.text = "List item " + i;
  126. text.name = "Item " + i;
  127. list.AddItem(text);
  128. }
  129. // Apply styles
  130. window.SetStyleAuto();
  131. list.SetStyleAuto();
  132. windowTitle.SetStyleAuto();
  133. buttonClose.style = "CloseButton";
  134. // Subscribe to buttonClose release (following a 'press') events
  135. SubscribeToEvent(buttonClose, "Released", "HandleClosePressed");
  136. // Subscribe also to all UI mouse clicks just to see where we have clicked
  137. SubscribeToEvent("UIMouseClick", "HandleControlClicked");
  138. }
  139. void CreateDraggableFish()
  140. {
  141. // Create a draggable Fish button
  142. Button@ draggableFish = ui.root.CreateChild("Button", "Fish");
  143. draggableFish.texture = cache.GetResource("Texture2D", "Textures/UrhoDecal.dds"); // Set texture
  144. draggableFish.blendMode = BLEND_ADD;
  145. draggableFish.SetSize(128, 128);
  146. draggableFish.SetPosition((graphics.width - draggableFish.width) / 2, 200);
  147. // Add a tooltip to Fish button
  148. ToolTip@ toolTip = draggableFish.CreateChild("ToolTip");
  149. toolTip.position = IntVector2(draggableFish.width + 5, draggableFish.width/2); // slightly offset from fish
  150. BorderImage@ textHolder = toolTip.CreateChild("BorderImage");
  151. textHolder.SetStyle("ToolTipBorderImage");
  152. Text@ toolTipText = textHolder.CreateChild("Text");
  153. toolTipText.SetStyle("ToolTipText");
  154. toolTipText.text = "Please drag me!";
  155. // Subscribe draggableFish to Drag Events (in order to make it draggable)
  156. // See "Event list" in documentation's Main Page for reference on available Events and their eventData
  157. SubscribeToEvent(draggableFish, "DragBegin", "HandleDragBegin");
  158. SubscribeToEvent(draggableFish, "DragMove", "HandleDragMove");
  159. SubscribeToEvent(draggableFish, "DragEnd", "HandleDragEnd");
  160. }
  161. void HandleDragBegin(StringHash eventType, VariantMap& eventData)
  162. {
  163. // Get UIElement relative position where input (touch or click) occurred (top-left = IntVector2(0,0))
  164. dragBeginPosition = IntVector2(eventData["ElementX"].GetInt(), eventData["ElementY"].GetInt());
  165. }
  166. void HandleDragMove(StringHash eventType, VariantMap& eventData)
  167. {
  168. IntVector2 dragCurrentPosition = IntVector2(eventData["X"].GetInt(), eventData["Y"].GetInt());
  169. // Get the element (fish) that is being dragged. GetPtr() returns a RefCounted handle which can be cast implicitly
  170. UIElement@ draggedElement = eventData["Element"].GetPtr();
  171. draggedElement.position = dragCurrentPosition - dragBeginPosition;
  172. }
  173. void HandleDragEnd(StringHash eventType, VariantMap& eventData) // For reference (not used here)
  174. {
  175. }
  176. void HandleClosePressed(StringHash eventType, VariantMap& eventData)
  177. {
  178. engine.Exit();
  179. }
  180. void HandleControlClicked(StringHash eventType, VariantMap& eventData)
  181. {
  182. // Get the Text control acting as the Window's title
  183. Text@ windowTitle = window.GetChild("WindowTitle", true);
  184. // Get control that was clicked
  185. UIElement@ clicked = eventData["Element"].GetPtr();
  186. String name = "...?";
  187. if (clicked !is null)
  188. {
  189. // Get the name of the control that was clicked
  190. name = clicked.name;
  191. }
  192. // Update the Window's title text
  193. windowTitle.text = "Hello " + name + "!";
  194. }
  195. void Init3DUI()
  196. {
  197. // Node that will get UI rendered on it.
  198. Node@ boxNode = scene_.GetChild("Box");
  199. // Create a component that sets up UI rendering. It sets material to StaticModel of the node.
  200. UIComponent@ component = boxNode.CreateComponent("UIComponent");
  201. // Optionally modify material. Technique is changed so object is visible without any lights.
  202. component.material.SetTechnique(0, cache.GetResource("Technique", "Techniques/DiffUnlit.xml"));
  203. // Save root element of texture UI for later use.
  204. textureRoot = component.root;
  205. // Set size of root element. This is size of texture as well.
  206. textureRoot.SetSize(512, 512);
  207. }
  208. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  209. {
  210. float timeStep = eventData["TimeStep"].GetFloat();
  211. Node@ node = scene_.GetChild("Box");
  212. if (current.Get() !is null && drawDebug_)
  213. ui.DebugDraw(cast<UIElement>(current.Get()));
  214. if (input.mouseButtonPress[MOUSEB_LEFT])
  215. current = ui.GetElementAt(input.mousePosition);
  216. if (input.keyPress[KEY_TAB])
  217. {
  218. renderOnCube = !renderOnCube;
  219. // Toggle between rendering on screen or to texture.
  220. if (renderOnCube)
  221. {
  222. node.enabled = true;
  223. textureRoot.AddChild(window);
  224. }
  225. else
  226. {
  227. node.enabled = false;
  228. ui.root.AddChild(window);
  229. }
  230. }
  231. if (input.keyPress[KEY_SPACE])
  232. animateCube = !animateCube;
  233. if (input.keyPress[KEY_F2])
  234. drawDebug_ = !drawDebug_;
  235. if (animateCube)
  236. {
  237. node.Yaw(6.0f * timeStep * 1.5f);
  238. node.Roll(-6.0f * timeStep * 1.5f);
  239. node.Pitch(-6.0f * timeStep * 1.5f);
  240. }
  241. }
  242. // Create XML patch instructions for screen joystick layout specific to this sample app
  243. String patchInstructions =
  244. "<patch>" +
  245. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" +
  246. " <attribute name=\"Is Visible\" value=\"false\" />" +
  247. " </add>" +
  248. "</patch>";