02_HelloGUI.lua 6.1 KB

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