02_HelloGUI.as 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // A simple 'HelloWorld' GUI created purely from code.
  2. // This sample demonstrates:
  3. // - Creation of controls and building a UI hierarchy
  4. // - Loading UI style from XML and applying it to controls
  5. // - Handling of global and per-control events
  6. // For more advanced users (beginners can skip this section):
  7. // - Dragging UIElements
  8. // - Displaying tooltips
  9. // - Accessing available Events data (eventData)
  10. #include "Scripts/Utilities/Sample.as"
  11. Window@ window;
  12. IntVector2 dragBeginPosition = IntVector2(0, 0);
  13. void Start()
  14. {
  15. // Execute the common startup for samples
  16. SampleStart();
  17. // Enable OS cursor
  18. input.mouseVisible = true;
  19. // Load XML file containing default UI style sheet
  20. XMLFile@ style = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
  21. // Set the loaded style as default style
  22. ui.root.defaultStyle = style;
  23. // Initialize Window
  24. InitWindow();
  25. // Create and add some controls to the Window
  26. InitControls();
  27. // Create a draggable Fish
  28. CreateDraggableFish();
  29. }
  30. void InitControls()
  31. {
  32. // Create a CheckBox
  33. CheckBox@ checkBox = CheckBox();
  34. checkBox.name = "CheckBox";
  35. // Create a Button
  36. Button@ button = Button();
  37. button.name = "Button";
  38. button.minHeight = 24;
  39. // Create a LineEdit
  40. LineEdit@ lineEdit = LineEdit();
  41. lineEdit.name = "LineEdit";
  42. lineEdit.minHeight = 24;
  43. // Add controls to Window
  44. window.AddChild(checkBox);
  45. window.AddChild(button);
  46. window.AddChild(lineEdit);
  47. // Apply previously set default style
  48. checkBox.SetStyleAuto();
  49. button.SetStyleAuto();
  50. lineEdit.SetStyleAuto();
  51. }
  52. void InitWindow()
  53. {
  54. // Create the Window and add it to the UI's root node
  55. window = Window();
  56. ui.root.AddChild(window);
  57. // Set Window size and layout settings
  58. window.SetMinSize(384, 192);
  59. window.SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6));
  60. window.SetAlignment(HA_CENTER, VA_CENTER);
  61. window.name = "Window";
  62. // Create Window 'titlebar' container
  63. UIElement@ titleBar = UIElement();
  64. titleBar.SetMinSize(0, 24);
  65. titleBar.verticalAlignment = VA_TOP;
  66. titleBar.layoutMode = LM_HORIZONTAL;
  67. // Create the Window title Text
  68. Text@ windowTitle = Text();
  69. windowTitle.name = "WindowTitle";
  70. windowTitle.text = "Hello GUI!";
  71. // Create the Window's close button
  72. Button@ buttonClose = Button();
  73. buttonClose.name = "CloseButton";
  74. // Add the controls to the title bar
  75. titleBar.AddChild(windowTitle);
  76. titleBar.AddChild(buttonClose);
  77. // Add the title bar to the Window
  78. window.AddChild(titleBar);
  79. // Apply styles
  80. window.SetStyleAuto();
  81. windowTitle.SetStyleAuto();
  82. buttonClose.style = "CloseButton";
  83. // Subscribe to buttonClose release (following a 'press') events
  84. SubscribeToEvent(buttonClose, "Released", "HandleClosePressed");
  85. // Subscribe also to all UI mouse clicks just to see where we have clicked
  86. SubscribeToEvent("UIMouseClick", "HandleControlClicked");
  87. }
  88. void CreateDraggableFish()
  89. {
  90. // Create a draggable Fish button
  91. Button@ draggableFish = ui.root.CreateChild("Button", "Fish");
  92. draggableFish.texture = cache.GetResource("Texture2D", "Textures/UrhoDecal.dds"); // Set texture
  93. draggableFish.blendMode = BLEND_ADD;
  94. draggableFish.SetSize(128, 128);
  95. draggableFish.SetPosition((graphics.width - draggableFish.width) / 2, 200);
  96. // Add a tooltip to Fish button
  97. ToolTip@ toolTip = draggableFish.CreateChild("ToolTip");
  98. toolTip.position = IntVector2(draggableFish.width + 5, draggableFish.width/2); // slightly offset from fish
  99. BorderImage@ textHolder = toolTip.CreateChild("BorderImage");
  100. textHolder.SetStyle("ToolTipBorderImage");
  101. Text@ toolTipText = textHolder.CreateChild("Text");
  102. toolTipText.SetStyle("ToolTipText");
  103. toolTipText.text = "Please drag me!";
  104. // Subscribe draggableFish to Drag Events (in order to make it draggable)
  105. // See "Event list" in documentation's Main Page for reference on available Events and their eventData
  106. SubscribeToEvent(draggableFish, "DragBegin", "HandleDragBegin");
  107. SubscribeToEvent(draggableFish, "DragMove", "HandleDragMove");
  108. SubscribeToEvent(draggableFish, "DragEnd", "HandleDragEnd");
  109. }
  110. void HandleDragBegin(StringHash eventType, VariantMap& eventData)
  111. {
  112. // Get UIElement relative position where input (touch or click) occured (top-left = IntVector2(0,0))
  113. dragBeginPosition = IntVector2(eventData["ElementX"].GetInt(), eventData["ElementY"].GetInt());
  114. }
  115. void HandleDragMove(StringHash eventType, VariantMap& eventData)
  116. {
  117. IntVector2 dragCurrentPosition = IntVector2(eventData["X"].GetInt(), eventData["Y"].GetInt());
  118. // Get the element (fish) that is being dragged. GetPtr() returns a RefCounted handle which can be cast implicitly
  119. UIElement@ draggedElement = eventData["Element"].GetPtr();
  120. draggedElement.position = dragCurrentPosition - dragBeginPosition;
  121. }
  122. void HandleDragEnd(StringHash eventType, VariantMap& eventData) // For reference (not used here)
  123. {
  124. }
  125. void HandleClosePressed(StringHash eventType, VariantMap& eventData)
  126. {
  127. engine.Exit();
  128. }
  129. void HandleControlClicked(StringHash eventType, VariantMap& eventData)
  130. {
  131. // Get the Text control acting as the Window's title
  132. Text@ windowTitle = window.GetChild("WindowTitle", true);
  133. // Get control that was clicked
  134. UIElement@ clicked = eventData["Element"].GetPtr();
  135. String name = "...?";
  136. if (clicked !is null)
  137. {
  138. // Get the name of the control that was clicked
  139. name = clicked.name;
  140. }
  141. // Update the Window's title text
  142. windowTitle.text = "Hello " + name + "!";
  143. }
  144. // Create XML patch instructions for screen joystick layout specific to this sample app
  145. String patchInstructions =
  146. "<patch>" +
  147. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" +
  148. " <attribute name=\"Is Visible\" value=\"false\" />" +
  149. " </add>" +
  150. "</patch>";