2
0

Main.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. -- **********************************************************************
  2. -- This demo shows various UI controls you can use in your code
  3. -- **********************************************************************
  4. screen = Screen()
  5. screen.rootEntity.snapToPixels = true
  6. demoShape = ScreenLabel("DEMO", 64, "mono")
  7. demoShape.positionAtBaseline = false
  8. demoShape:setPositionMode(ScreenEntity.POSITION_CENTER)
  9. screen:addChild(demoShape)
  10. demoShape.position.x = 600
  11. demoShape.position.y = 200
  12. -- **********************************************************************
  13. -- UIWindow is a draggable window container
  14. -- **********************************************************************
  15. window = UIWindow("Demo window", 300, 400)
  16. window.position.x = 50
  17. window.position.y = 50
  18. screen:addChild(window)
  19. -- **********************************************************************
  20. -- UIButton lets you create a simple text button
  21. -- and listen to its click event
  22. -- **********************************************************************
  23. button = UIButton("Rotate me", 100, 24)
  24. window:addChild(button)
  25. button.position.x = 20
  26. button.position.y = 40
  27. function onRotateButton(t, event)
  28. demoShape.rotation.roll = demoShape.rotation.roll + 30
  29. end
  30. button:addEventListener(nil, onRotateButton, UIEvent.CLICK_EVENT)
  31. -- **********************************************************************
  32. -- UITextInput is a text input field
  33. -- **********************************************************************
  34. input = UITextInput(false, 100, 16)
  35. input:setText("DEMO")
  36. window:addChild(input)
  37. input.position.x = 140
  38. input.position.y = 40
  39. function onTextChange(t, event)
  40. demoShape:setText(input:getText())
  41. end
  42. input:addEventListener(nil, onTextChange, UIEvent.CHANGE_EVENT)
  43. -- **********************************************************************
  44. -- UICheckBox is a simple check box
  45. -- **********************************************************************
  46. checkBox = UICheckBox("Auto rotate", false)
  47. window:addChild(checkBox)
  48. checkBox.position.x = 20
  49. checkBox.position.y = 80
  50. function Update(elapsed)
  51. if checkBox:isChecked() then
  52. demoShape.rotation.roll = demoShape.rotation.roll + (elapsed * 100)
  53. end
  54. end
  55. -- **********************************************************************
  56. -- UIHSlider is a horizontal value slider that changes
  57. -- a value between two preset values
  58. -- **********************************************************************
  59. label = ScreenLabel("Scale me", 12, "sans")
  60. window:addChild(label)
  61. label.position.x = 20
  62. label.position.y = 110
  63. slider = UIHSlider(1.0, 3.0, 100)
  64. window:addChild(slider)
  65. slider.position.x = 115
  66. slider.position.y = 116
  67. function onSliderChange(t, event)
  68. demoShape.scale.x = slider:getSliderValue()
  69. end
  70. slider:addEventListener(nil, onSliderChange, UIEvent.CHANGE_EVENT)
  71. -- **********************************************************************
  72. -- UIColorBox is a box that lets you pick colors
  73. -- to use it, you must also create a UIColorPicker that should be
  74. -- shared between all UIColorBox instances
  75. -- **********************************************************************
  76. colorPicker = UIColorPicker()
  77. screen:addChild(colorPicker)
  78. colorPicker.position.x = 300
  79. colorPicker.position.y = 300
  80. colorBox = UIColorBox(colorPicker, Color(1.0, 1.0, 1.0, 1.0), 50, 30)
  81. window:addChild(colorBox)
  82. colorBox.position.x = 20
  83. colorBox.position.y = 130
  84. function onColorChange(t, event)
  85. demoShape.color = colorBox:getSelectedColor()
  86. end
  87. colorBox:addEventListener(nil, onColorChange, UIEvent.CHANGE_EVENT)
  88. -- **********************************************************************
  89. -- UIComboBox lets you create a drop down combo box
  90. -- UIComboBox uses a UIGlobalMenu, which should be shared between all
  91. -- controls that require a menu.
  92. -- **********************************************************************
  93. globalMenu = UIGlobalMenu()
  94. screen:addChild(globalMenu)
  95. comboBox = UIComboBox(globalMenu, 200)
  96. window:addChild(comboBox)
  97. comboBox.position.x = 20
  98. comboBox.position.y = 180
  99. comboBox:addComboItem("No Border")
  100. comboBox:addComboItem("Thin Border")
  101. comboBox:addComboItem("Thick Border")
  102. comboBox:setSelectedIndex(0)
  103. function onBlendingChange(t, event)
  104. if comboBox:getSelectedIndex() == 0 then
  105. demoShape.strokeEnabled = false
  106. elseif comboBox:getSelectedIndex() == 1 then
  107. demoShape.strokeEnabled = true
  108. demoShape.strokeWidth = 1.0
  109. else
  110. demoShape.strokeEnabled = true
  111. demoShape.strokeWidth = 3.0
  112. end
  113. end
  114. comboBox:addEventListener(nil, onBlendingChange, UIEvent.CHANGE_EVENT)