02_HelloGUI.lua 6.2 KB

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