main.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. -- Filename: main.lua
  2. -- Author: Luke Perkin
  3. -- Date: 2010-02-25
  4. -- Initialization
  5. goo = require 'goo/goo'
  6. anim = require 'anim/anim'
  7. panels = {}
  8. panels.name = { 'Open panel with button', 'Slide in panel with checkbox', 'Zoom in panel with textinput and colorpick', 'using the null object', 'Change skin', 'Multiline Text Input.'}
  9. function love.load()
  10. goo:load()
  11. love.graphics.setBackgroundColor(255,255,255)
  12. createButtons()
  13. end
  14. -- Logic
  15. function love.update(dt)
  16. goo:update(dt)
  17. anim:update(dt)
  18. end
  19. -- Scene Drawing
  20. function love.draw()
  21. goo:draw()
  22. end
  23. -- Input
  24. function love.keypressed( key, unicode )
  25. goo:keypressed( key, unicode )
  26. end
  27. function love.textinput( key )
  28. goo:charinput( key )
  29. end
  30. function love.keyreleased( key )
  31. goo:keyreleased( key )
  32. end
  33. function love.mousepressed( x, y, button )
  34. goo:mousepressed( x, y, button )
  35. end
  36. function love.mousereleased( x, y, button )
  37. goo:mousereleased( x, y, button )
  38. end
  39. function createButtons()
  40. for i,v in ipairs( panels ) do
  41. local button = goo.button:new()
  42. button:setPos( -350, i*25 )
  43. button:setText( panels.name[i] )
  44. button:sizeToText()
  45. button.onClick = function(self,button)
  46. if button == goo.mouseL then
  47. panels[i]()
  48. end
  49. end
  50. anim:new{ table = button, key = 'x', finish = 10, style = 'quartOut', delay = (i-1)/2 }:play()
  51. end
  52. end
  53. -- Open panel with button.
  54. panels[1] = function()
  55. local panel = goo.panel:new()
  56. panel:setPos( 10, 10 )
  57. panel:setSize( 200, 200 )
  58. panel:setTitle( 'I am a panel' )
  59. local button = goo.button:new( panel )
  60. button:setPos( 10, 20 )
  61. print( button:getAbsolutePos() ) -- 20, 30
  62. button:setText( 'Click me' )
  63. button:sizeToText()
  64. button.onClick = function( self, button )
  65. print( 'I have been clicked' )
  66. end
  67. local redButton = goo.button:new( panel )
  68. redButton:setPos( 10, 100 )
  69. redButton:setText( 'My style has been overridden' )
  70. redButton:sizeToText( 10 )
  71. local redStyle = {
  72. backgroundColor = {255,0,0},
  73. backgroundColorHover = {125,0,0},
  74. borderColor = {0,0,0,0},
  75. borderColorHover = {0,0,0,0},
  76. }
  77. redButton:setStyle( redStyle )
  78. end
  79. -- Slide in panel with checkbox.
  80. panels[2] = function()
  81. local panel = goo.panel:new()
  82. panel:setPos( 10, 50 )
  83. panel:setSize( 200, 200 )
  84. panel:setTitle( 'I am a panel' )
  85. local checkbox = goo.checkbox:new( panel )
  86. checkbox:setPos( 10, 20 )
  87. function checkbox:onClick( button )
  88. print( self.class.name .. ' has been clicked' )
  89. end
  90. function checkbox:enterHover()
  91. print( self.class.name .. ' enter hover')
  92. end
  93. local slideIn = anim:new{
  94. table = panel,
  95. key = 'x',
  96. start = -250,
  97. finish = 350,
  98. time = 2,
  99. style = 'expoOut'
  100. }
  101. slideIn:play()
  102. function slideIn:onFinish()
  103. checkbox:setChecked( true )
  104. end
  105. end
  106. -- Zoom in panel with textinput and colorpick
  107. panels[3] = function()
  108. local testPanel = goo.panel:new()
  109. testPanel:setPos( 100, 50 )
  110. testPanel:setSize( 370, 500 )
  111. testPanel:setTitle( "This is a Color Panel." )
  112. testPanel:setOpacity( 10 )
  113. local colorPicker = goo.colorpick:new( testPanel )
  114. colorPicker:setPos(0,20)
  115. local input = goo.textinput:new( testPanel )
  116. input:setPos(3,486)
  117. input:setSize(360,20)
  118. input:setText('hello!')
  119. function colorPicker:onClick()
  120. local r,g,b = self:getColor()
  121. local str = string.format( 'Red = %i, Green = %i, Blue = %i', r,g,b)
  122. input:setText( str )
  123. end
  124. anim:easy( testPanel, 'xscale', 0, 1, 3, 'elastic')
  125. anim:easy( testPanel, 'yscale', 0.9, 1, 3, 'elastic')
  126. anim:easy( testPanel, 'opacity', 0, 255, 0.5, 'quadInOut')
  127. end
  128. -- Using the null object.
  129. panels[4] = function()
  130. local group = goo.null()
  131. group:setPos( 300, 300 )
  132. local n = 1
  133. function group:update(dt)
  134. self:setPos( 300 + n, 300 )
  135. n=n+1
  136. self:recurse('children','updateBounds')
  137. end
  138. local panel_style = {
  139. titleColor = {0,0,0,0},
  140. separatorColor = {0,0,0,0}
  141. }
  142. local p1 = goo.panel:new( group )
  143. p1:setPos( 0, 0 )
  144. p1:setSize( 50, 50 )
  145. p1:showCloseButton( false )
  146. p1:setStyle( panel_style )
  147. p1:setDraggable( false )
  148. local p2 = goo.panel:new( group )
  149. p2:setPos( 75, 0 )
  150. p2:setSize( 50, 50 )
  151. p2:setStyle( panel_style )
  152. p2:setDraggable( false )
  153. end
  154. -- Changing the style
  155. panels[5] = function()
  156. local panel = goo.panel:new()
  157. panel:setPos( 100, 100 )
  158. panel:setSize( 200, 100 )
  159. local button = goo.button:new( panel )
  160. button:setPos( 15, 15 )
  161. button:setText( 'toggle theme' )
  162. button:sizeToText( 15 )
  163. themeSwitch = true
  164. function button:onClick()
  165. if themeSwitch then
  166. goo:setSkinAllObjects( 'dark' )
  167. themeSwitch = not themeSwitch
  168. else
  169. goo:setSkinAllObjects( 'default' )
  170. themeSwitch = not themeSwitch
  171. end
  172. end
  173. end
  174. panels[6] = function()
  175. local panel = goo.panel:new()
  176. panel:setPos( 100, 100 )
  177. panel:setSize( 200, 300 )
  178. local input = goo.textinput:new( panel )
  179. input:setMultiline( true )
  180. input:setPos( 10, 15 )
  181. input:setSize( 180, 280 )
  182. end