main.lua 4.7 KB

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