02_HelloGUI.lua 3.9 KB

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