02_HelloGUI.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 InitControls()
  29. -- Create a CheckBox
  30. local checkBox = CheckBox:new(context)
  31. checkBox:SetName("CheckBox")
  32. -- Create a Button
  33. local button = Button:new(context)
  34. button:SetName("Button")
  35. button.minHeight = 24
  36. -- Create a LineEdit
  37. local lineEdit = LineEdit:new(context)
  38. lineEdit:SetName("LineEdit")
  39. lineEdit.minHeight = 24
  40. -- Add controls to Window
  41. window:AddChild(checkBox)
  42. window:AddChild(button)
  43. window:AddChild(lineEdit)
  44. -- Apply previously set default style
  45. checkBox:SetStyleAuto()
  46. button:SetStyleAuto()
  47. lineEdit:SetStyleAuto()
  48. end
  49. function InitWindow()
  50. -- Create the Window and add it to the UI's root node
  51. window = Window:new(context)
  52. ui.root:AddChild(window)
  53. -- Set Window size and layout settings
  54. window:SetMinSize(384, 192)
  55. window:SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6))
  56. window:SetAlignment(HA_CENTER, VA_CENTER)
  57. window:SetName("Window")
  58. -- Create Window 'titlebar' container
  59. local titleBar = UIElement:new(context)
  60. titleBar:SetMinSize(0, 24)
  61. titleBar.verticalAlignment = VA_TOP
  62. titleBar.layoutMode = LM_HORIZONTAL
  63. -- Create the Window title Text
  64. local windowTitle = Text:new(context)
  65. windowTitle.name = "WindowTitle"
  66. windowTitle.text = "Hello GUI!"
  67. -- Create the Window's close button
  68. local buttonClose = Button:new(context)
  69. buttonClose:SetName("CloseButton")
  70. -- Add the controls to the title bar
  71. titleBar:AddChild(windowTitle)
  72. titleBar:AddChild(buttonClose)
  73. -- Add the title bar to the Window
  74. window:AddChild(titleBar)
  75. -- Apply styles
  76. window:SetStyleAuto()
  77. windowTitle:SetStyleAuto()
  78. buttonClose:SetStyle("CloseButton")
  79. -- Lastly, subscribe to buttonClose release (following a 'press') events
  80. SubscribeToEvent(buttonClose, "Released", "HandleClosePressed")
  81. end
  82. function SubscribeToEvents()
  83. -- Subscribe handler invoked whenever a mouse click event is dispatched
  84. SubscribeToEvent("UIMouseClick", "HandleControlClicked")
  85. end
  86. function HandleClosePressed(eventType, eventData)
  87. engine:Exit()
  88. end
  89. function HandleControlClicked(eventType, eventData)
  90. -- Get the Text control acting as the Window's title
  91. local element = window:GetChild("WindowTitle", true)
  92. local windowTitle = tolua.cast(element, 'Text')
  93. -- Get control that was clicked
  94. -- Note difference to C++: in C++ we would call GetPtr() and cast the function pointer to UIElement, here we must specify
  95. -- what kind of object we are getting. Null will be returned on type mismatch
  96. local clicked = eventData:GetPtr("UIElement", "Element")
  97. local name = "...?"
  98. if clicked ~= nil then
  99. -- Get the name of the control that was clicked
  100. name = clicked.name
  101. end
  102. -- Update the Window's title text
  103. windowTitle.text = "Hello " .. name .. "!"
  104. end