02_HelloGUI.as 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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::ZERO;
  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. // Set the mouse mode to use in the sample
  30. SampleInitMouseMode(MM_FREE);
  31. }
  32. void InitControls()
  33. {
  34. // Create a CheckBox
  35. CheckBox@ checkBox = CheckBox();
  36. checkBox.name = "CheckBox";
  37. // Create a Button
  38. Button@ button = Button();
  39. button.name = "Button";
  40. button.minHeight = 24;
  41. // Create a LineEdit
  42. LineEdit@ lineEdit = LineEdit();
  43. lineEdit.name = "LineEdit";
  44. lineEdit.minHeight = 24;
  45. // Add controls to Window
  46. window.AddChild(checkBox);
  47. window.AddChild(button);
  48. window.AddChild(lineEdit);
  49. // Apply previously set default style
  50. checkBox.SetStyleAuto();
  51. button.SetStyleAuto();
  52. lineEdit.SetStyleAuto();
  53. }
  54. void InitWindow()
  55. {
  56. // Create the Window and add it to the UI's root node
  57. window = Window();
  58. ui.root.AddChild(window);
  59. // Set Window size and layout settings
  60. window.minWidth = 384;
  61. window.SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6));
  62. window.SetAlignment(HA_CENTER, VA_CENTER);
  63. window.name = "Window";
  64. // Create Window 'titlebar' container
  65. UIElement@ titleBar = UIElement();
  66. titleBar.SetMinSize(0, 24);
  67. titleBar.verticalAlignment = VA_TOP;
  68. titleBar.layoutMode = LM_HORIZONTAL;
  69. // Create the Window title Text
  70. Text@ windowTitle = Text();
  71. windowTitle.name = "WindowTitle";
  72. windowTitle.text = "Hello GUI!";
  73. // Create the Window's close button
  74. Button@ buttonClose = Button();
  75. buttonClose.name = "CloseButton";
  76. // Add the controls to the title bar
  77. titleBar.AddChild(windowTitle);
  78. titleBar.AddChild(buttonClose);
  79. // Add the title bar to the Window
  80. window.AddChild(titleBar);
  81. // Apply styles
  82. window.SetStyleAuto();
  83. windowTitle.SetStyleAuto();
  84. buttonClose.style = "CloseButton";
  85. // Subscribe to buttonClose release (following a 'press') events
  86. SubscribeToEvent(buttonClose, "Released", "HandleClosePressed");
  87. // Subscribe also to all UI mouse clicks just to see where we have clicked
  88. SubscribeToEvent("UIMouseClick", "HandleControlClicked");
  89. }
  90. void CreateDraggableFish()
  91. {
  92. // Create a draggable Fish button
  93. Button@ draggableFish = ui.root.CreateChild("Button", "Fish");
  94. draggableFish.texture = cache.GetResource("Texture2D", "Textures/UrhoDecal.dds"); // Set texture
  95. draggableFish.blendMode = BLEND_ADD;
  96. draggableFish.SetSize(128, 128);
  97. draggableFish.SetPosition((graphics.width - draggableFish.width) / 2, 200);
  98. // Add a tooltip to Fish button
  99. ToolTip@ toolTip = draggableFish.CreateChild("ToolTip");
  100. toolTip.position = IntVector2(draggableFish.width + 5, draggableFish.width/2); // slightly offset from fish
  101. BorderImage@ textHolder = toolTip.CreateChild("BorderImage");
  102. textHolder.SetStyle("ToolTipBorderImage");
  103. Text@ toolTipText = textHolder.CreateChild("Text");
  104. toolTipText.SetStyle("ToolTipText");
  105. toolTipText.text = "Please drag me!";
  106. // Subscribe draggableFish to Drag Events (in order to make it draggable)
  107. // See "Event list" in documentation's Main Page for reference on available Events and their eventData
  108. SubscribeToEvent(draggableFish, "DragBegin", "HandleDragBegin");
  109. SubscribeToEvent(draggableFish, "DragMove", "HandleDragMove");
  110. SubscribeToEvent(draggableFish, "DragEnd", "HandleDragEnd");
  111. }
  112. void HandleDragBegin(StringHash eventType, VariantMap& eventData)
  113. {
  114. // Get UIElement relative position where input (touch or click) occurred (top-left = IntVector2(0,0))
  115. dragBeginPosition = IntVector2(eventData["ElementX"].GetInt(), eventData["ElementY"].GetInt());
  116. }
  117. void HandleDragMove(StringHash eventType, VariantMap& eventData)
  118. {
  119. IntVector2 dragCurrentPosition = IntVector2(eventData["X"].GetInt(), eventData["Y"].GetInt());
  120. // Get the element (fish) that is being dragged. GetPtr() returns a RefCounted handle which can be cast implicitly
  121. UIElement@ draggedElement = eventData["Element"].GetPtr();
  122. draggedElement.position = dragCurrentPosition - dragBeginPosition;
  123. }
  124. void HandleDragEnd(StringHash eventType, VariantMap& eventData) // For reference (not used here)
  125. {
  126. }
  127. void HandleClosePressed(StringHash eventType, VariantMap& eventData)
  128. {
  129. engine.Exit();
  130. }
  131. void HandleControlClicked(StringHash eventType, VariantMap& eventData)
  132. {
  133. // Get the Text control acting as the Window's title
  134. Text@ windowTitle = window.GetChild("WindowTitle", true);
  135. // Get control that was clicked
  136. UIElement@ clicked = eventData["Element"].GetPtr();
  137. String name = "...?";
  138. if (clicked !is null)
  139. {
  140. // Get the name of the control that was clicked
  141. name = clicked.name;
  142. }
  143. // Update the Window's title text
  144. windowTitle.text = "Hello " + name + "!";
  145. }
  146. // Create XML patch instructions for screen joystick layout specific to this sample app
  147. String patchInstructions =
  148. "<patch>" +
  149. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" +
  150. " <attribute name=\"Is Visible\" value=\"false\" />" +
  151. " </add>" +
  152. "</patch>";