02_HelloGUI.as 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include "Scripts/Utilities/Sample.as"
  7. Window@ window;
  8. void Start()
  9. {
  10. // Execute the common startup for samples
  11. SampleStart();
  12. // Enable OS cursor
  13. input.mouseVisible = true;
  14. // Load XML file containing default UI style sheet
  15. XMLFile@ style = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
  16. // Set the loaded style as default style
  17. ui.root.defaultStyle = style;
  18. // Initialize Window
  19. InitWindow();
  20. // Create and add some controls to the Window
  21. InitControls();
  22. SubscribeToEvents();
  23. }
  24. void InitControls()
  25. {
  26. // Create a CheckBox
  27. CheckBox@ checkBox = CheckBox();
  28. checkBox.name = "CheckBox";
  29. // Create a Button
  30. Button@ button = Button();
  31. button.name = "Button";
  32. button.minHeight = 24;
  33. // Create a LineEdit
  34. LineEdit@ lineEdit = LineEdit();
  35. lineEdit.name = "LineEdit";
  36. lineEdit.minHeight = 24;
  37. // Add controls to Window
  38. window.AddChild(checkBox);
  39. window.AddChild(button);
  40. window.AddChild(lineEdit);
  41. // Apply previously set default style
  42. checkBox.SetStyleAuto();
  43. button.SetStyleAuto();
  44. lineEdit.SetStyleAuto();
  45. }
  46. void InitWindow()
  47. {
  48. // Create the Window and add it to the UI's root node
  49. window = Window();
  50. ui.root.AddChild(window);
  51. // Set Window size and layout settings
  52. window.SetMinSize(384, 192);
  53. window.SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6));
  54. window.SetAlignment(HA_CENTER, VA_CENTER);
  55. window.name = "Window";
  56. // Create Window 'titlebar' container
  57. UIElement@ titleBar = UIElement();
  58. titleBar.SetMinSize(0, 24);
  59. titleBar.verticalAlignment = VA_TOP;
  60. titleBar.layoutMode = LM_HORIZONTAL;
  61. // Create the Window title Text
  62. Text@ windowTitle = Text();
  63. windowTitle.name = "WindowTitle";
  64. windowTitle.text = "Hello GUI!";
  65. // Create the Window's close button
  66. Button@ buttonClose = Button();
  67. buttonClose.name = "CloseButton";
  68. // Add the controls to the title bar
  69. titleBar.AddChild(windowTitle);
  70. titleBar.AddChild(buttonClose);
  71. // Add the title bar to the Window
  72. window.AddChild(titleBar);
  73. // Apply styles
  74. window.SetStyleAuto();
  75. windowTitle.SetStyleAuto();
  76. buttonClose.style = "CloseButton";
  77. // Lastly, subscribe to buttonClose release (following a 'press') events
  78. SubscribeToEvent(buttonClose, "Released", "HandleClosePressed");
  79. }
  80. void SubscribeToEvents()
  81. {
  82. // Subscribe handler; invoked whenever a mouse click event is dispatched
  83. SubscribeToEvent("UIMouseClick", "HandleControlClicked");
  84. }
  85. void HandleClosePressed(StringHash eventType, VariantMap& eventData)
  86. {
  87. engine.Exit();
  88. }
  89. void HandleControlClicked(StringHash eventType, VariantMap& eventData)
  90. {
  91. // Get the Text control acting as the Window's title
  92. Text@ windowTitle = window.GetChild("WindowTitle", true);
  93. // Get control that was clicked
  94. // Note difference to C++: in C++ we would call GetPtr() and cast the void pointer to UIElement, here we must specify
  95. // what kind of object we are getting. Null will be returned on type mismatch
  96. UIElement@ clicked = eventData["Element"].GetUIElement();
  97. String name = "...?";
  98. if (clicked !is null)
  99. {
  100. // Get the name of the control that was clicked
  101. name = clicked.name;
  102. }
  103. // Update the Window's title text
  104. windowTitle.text = "Hello " + name + "!";
  105. }