02_HelloGUI.lua 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. require "LuaScripts/Utilities/Sample"
  11. local window = nil
  12. local cache = GetCache()
  13. local engine = GetEngine()
  14. local input = GetInput()
  15. local ui = GetUI()
  16. function Start()
  17. -- Execute the common startup for samples
  18. SampleStart()
  19. -- Enable OS cursor
  20. input.mouseVisible = true
  21. -- Load XML file containing default UI style sheet
  22. local style = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
  23. -- Set the loaded style as default style
  24. ui.root.defaultStyle = style
  25. -- Initialize Window
  26. InitWindow()
  27. -- Create and add some controls to the Window
  28. InitControls()
  29. -- Create a draggable Fish
  30. CreateDraggableFish()
  31. SubscribeToEvents()
  32. end
  33. function InitControls()
  34. -- Create a CheckBox
  35. local checkBox = CheckBox:new()
  36. checkBox:SetName("CheckBox")
  37. -- Create a Button
  38. local button = Button:new()
  39. button:SetName("Button")
  40. button.minHeight = 24
  41. -- Create a LineEdit
  42. local lineEdit = LineEdit:new()
  43. lineEdit:SetName("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. end
  54. function InitWindow()
  55. -- Create the Window and add it to the UI's root node
  56. window = Window:new()
  57. ui.root:AddChild(window)
  58. -- Set Window size and layout settings
  59. window:SetMinSize(384, 192)
  60. window:SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6))
  61. window:SetAlignment(HA_CENTER, VA_CENTER)
  62. window:SetName("Window")
  63. -- Create Window 'titlebar' container
  64. local titleBar = UIElement:new()
  65. titleBar:SetMinSize(0, 24)
  66. titleBar.verticalAlignment = VA_TOP
  67. titleBar.layoutMode = LM_HORIZONTAL
  68. -- Create the Window title Text
  69. local windowTitle = Text:new()
  70. windowTitle.name = "WindowTitle"
  71. windowTitle.text = "Hello GUI!"
  72. -- Create the Window's close button
  73. local buttonClose = Button:new()
  74. buttonClose:SetName("CloseButton")
  75. -- Add the controls to the title bar
  76. titleBar:AddChild(windowTitle)
  77. titleBar:AddChild(buttonClose)
  78. -- Add the title bar to the Window
  79. window:AddChild(titleBar)
  80. -- Apply styles
  81. window:SetStyleAuto()
  82. windowTitle:SetStyleAuto()
  83. buttonClose:SetStyle("CloseButton")
  84. -- Lastly, subscribe to buttonClose release (following a 'press') events
  85. SubscribeToEvent(buttonClose, "Released", "HandleClosePressed")
  86. end
  87. function SubscribeToEvents()
  88. -- Subscribe handler invoked whenever a mouse click event is dispatched
  89. SubscribeToEvent("UIMouseClick", "HandleControlClicked")
  90. end
  91. function HandleClosePressed(eventType, eventData)
  92. engine:Exit()
  93. end
  94. function HandleControlClicked(eventType, eventData)
  95. -- Get the Text control acting as the Window's title
  96. local element = window:GetChild("WindowTitle", true)
  97. local windowTitle = tolua.cast(element, 'Text')
  98. -- Get control that was clicked
  99. -- Note difference to C++: in C++ we would call GetPtr() and cast the function pointer to UIElement, here we must specify
  100. -- what kind of object we are getting. Null will be returned on type mismatch
  101. local clicked = eventData:GetPtr("UIElement", "Element")
  102. local name = "...?"
  103. if clicked ~= nil then
  104. -- Get the name of the control that was clicked
  105. name = clicked.name
  106. end
  107. -- Update the Window's title text
  108. windowTitle.text = "Hello " .. name .. "!"
  109. end
  110. ----------------------------------------------------- Dragging / Tooltips ----------------------------------------------
  111. local dragBeginPosition = IntVector2(0, 0)
  112. function CreateDraggableFish()
  113. -- Create a draggable Fish button
  114. local draggableFish = ui.root:CreateChild("Button", "Fish")
  115. draggableFish.texture = cache:GetResource("Texture2D", "Textures/UrhoDecal.dds") -- Set texture
  116. draggableFish.blendMode = BLEND_ADD
  117. draggableFish:SetSize(128, 128)
  118. draggableFish:SetPosition((GetGraphics().width - draggableFish.width) / 2, 200)
  119. -- Add a tooltip to Fish button
  120. local toolTip = draggableFish:CreateChild("ToolTip")
  121. toolTip.position = IntVector2(draggableFish.width + 5, draggableFish.width/2) -- Slightly offset from fish
  122. local textHolder = toolTip:CreateChild("BorderImage")
  123. textHolder:SetStyle("ToolTipBorderImage")
  124. local toolTipText = textHolder:CreateChild("Text")
  125. toolTipText:SetStyle("ToolTipText")
  126. toolTipText.text = "Please drag me!"
  127. -- Subscribe draggableFish to Drag Events (in order to make it draggable)
  128. -- See "Event list" in documentation's Main Page for reference on available Events and their eventData
  129. SubscribeToEvent(draggableFish, "DragBegin", "HandleDragBegin")
  130. SubscribeToEvent(draggableFish, "DragMove", "HandleDragMove")
  131. SubscribeToEvent(draggableFish, "DragEnd", "HandleDragEnd")
  132. end
  133. function HandleDragBegin(eventType, eventData)
  134. -- Get UIElement relative position where input (touch or click) occured (top-left = IntVector2(0,0))
  135. dragBeginPosition = IntVector2(eventData:GetInt("ElementX"), eventData:GetInt("ElementY"))
  136. end
  137. function HandleDragMove(eventType, eventData)
  138. local dragCurrentPosition = IntVector2(eventData:GetInt("X"), eventData:GetInt("Y"))
  139. local draggedElement = eventData:GetPtr("UIElement", "Element")
  140. draggedElement:SetPosition(dragCurrentPosition - dragBeginPosition)
  141. end
  142. function HandleDragEnd(eventType, eventData) -- For reference (not used here)
  143. end