02_HelloGUI.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. local dragBeginPosition = IntVector2(0, 0)
  17. function Start()
  18. -- Execute the common startup for samples
  19. SampleStart()
  20. -- Enable OS cursor
  21. input.mouseVisible = true
  22. -- Load XML file containing default UI style sheet
  23. local style = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
  24. -- Set the loaded style as default style
  25. ui.root.defaultStyle = style
  26. -- Initialize Window
  27. InitWindow()
  28. -- Create and add some controls to the Window
  29. InitControls()
  30. -- Create a draggable Fish
  31. CreateDraggableFish()
  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. -- Subscribe to buttonClose release (following a 'press') events
  85. SubscribeToEvent(buttonClose, "Released", "HandleClosePressed")
  86. -- Subscribe also to all UI mouse clicks just to see where we have clicked
  87. SubscribeToEvent("UIMouseClick", "HandleControlClicked")
  88. end
  89. function CreateDraggableFish()
  90. -- Create a draggable Fish button
  91. local draggableFish = ui.root:CreateChild("Button", "Fish")
  92. draggableFish.texture = cache:GetResource("Texture2D", "Textures/UrhoDecal.dds") -- Set texture
  93. draggableFish.blendMode = BLEND_ADD
  94. draggableFish:SetSize(128, 128)
  95. draggableFish:SetPosition((GetGraphics().width - draggableFish.width) / 2, 200)
  96. -- Add a tooltip to Fish button
  97. local toolTip = draggableFish:CreateChild("ToolTip")
  98. toolTip.position = IntVector2(draggableFish.width + 5, draggableFish.width/2) -- Slightly offset from fish
  99. local textHolder = toolTip:CreateChild("BorderImage")
  100. textHolder:SetStyle("ToolTipBorderImage")
  101. local toolTipText = textHolder:CreateChild("Text")
  102. toolTipText:SetStyle("ToolTipText")
  103. toolTipText.text = "Please drag me!"
  104. -- Subscribe draggableFish to Drag Events (in order to make it draggable)
  105. -- See "Event list" in documentation's Main Page for reference on available Events and their eventData
  106. SubscribeToEvent(draggableFish, "DragBegin", "HandleDragBegin")
  107. SubscribeToEvent(draggableFish, "DragMove", "HandleDragMove")
  108. SubscribeToEvent(draggableFish, "DragEnd", "HandleDragEnd")
  109. end
  110. function HandleDragBegin(eventType, eventData)
  111. -- Get UIElement relative position where input (touch or click) occured (top-left = IntVector2(0,0))
  112. dragBeginPosition = IntVector2(eventData:GetInt("ElementX"), eventData:GetInt("ElementY"))
  113. end
  114. function HandleDragMove(eventType, eventData)
  115. local dragCurrentPosition = IntVector2(eventData:GetInt("X"), eventData:GetInt("Y"))
  116. -- Get the dragged fish element
  117. -- Note difference to C++: in C++ we would call GetPtr() and cast the pointer to UIElement, here we must specify
  118. -- what kind of object we are getting. Null will be returned on type mismatch
  119. local draggedElement = eventData:GetPtr("UIElement", "Element")
  120. draggedElement:SetPosition(dragCurrentPosition - dragBeginPosition)
  121. end
  122. function HandleDragEnd(eventType, eventData) -- For reference (not used here)
  123. end
  124. function HandleClosePressed(eventType, eventData)
  125. engine:Exit()
  126. end
  127. function HandleControlClicked(eventType, eventData)
  128. -- Get the Text control acting as the Window's title
  129. local element = window:GetChild("WindowTitle", true)
  130. local windowTitle = tolua.cast(element, 'Text')
  131. -- Get control that was clicked
  132. local clicked = eventData:GetPtr("UIElement", "Element")
  133. local name = "...?"
  134. if clicked ~= nil then
  135. -- Get the name of the control that was clicked
  136. name = clicked.name
  137. end
  138. -- Update the Window's title text
  139. windowTitle.text = "Hello " .. name .. "!"
  140. end