goo.luadoc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. --[[ GUI Library.
  2. <br/>Goo is a gui lib that uses inherited positioning.
  3. <br/>What this means is that a object's position is always relative to it's parent's position.
  4. <br/>Some of you may recognise this as a scene graph.
  5. <br/>Goo also uses skins and styles which means you can easily change and seperate the object look from it's logic.
  6. <br/>
  7. <br/>To load place the following functions in their respective love function.
  8. <pre class="example">
  9. goo = require 'goo/goo'<br/>
  10. goo:load()<br/>
  11. goo:update(dt)<br/>
  12. goo:draw()<br/>
  13. goo:mousepressed(x,y,button)<br/>
  14. goo:mousereleased(x,y,button)<br/>
  15. goo:keypressed(key,unicode)<br/>
  16. goo:keyreleased(key,unicode)
  17. </pre>
  18. @class module
  19. @name goo
  20. ]]
  21. module 'goo'
  22. --[[ Loads the goo library. ]]
  23. function goo:load()
  24. --[[ Updates all goo objects. ]]
  25. function goo:update(dt)
  26. --[[ Draws all goo objects. ]]
  27. function goo:draw()
  28. --[[ Send mousepress to goo ]]
  29. function goo:mousepressed(x,y,button)
  30. --[[ Send mouserelease to goo ]]
  31. function goo:mouserelease(x,y,button)
  32. --[[ Send keypress to goo ]]
  33. function goo:keypressed(key,unicode)
  34. --[[ Send keyrelease to goo ]]
  35. function goo:keyreleased(key,unicode)
  36. --[[ Draw a debug interface.
  37. <br/>Will show the mouse position and the name of the goo object the mouse is over.
  38. ]]
  39. function goo:debugdraw()
  40. --[[ Creates a goo object.
  41. <br/>This is the main method to create gui objects, replace goo.object with a goo object.
  42. <br/>All goo objects inherit methods from <a href='goo.object.html'>goo.object</a>
  43. @name goo.object:new
  44. @class function
  45. @param parent:goo_object the parent object to use. Position and scale will be inherited from it's parent.
  46. @return goo_object: A goo object instance. Use links below to see the methods you can call on the table.
  47. @usage <pre class="example"> panel = goo.panel:new(); button = goo.button:new( panel ) </pre>
  48. @see goo_object
  49. @see goo.object, goo.panel, goo.text, goo.button, goo.textinput, goo.colorpick, goo.null, goo.progressbar, goo.closebutton
  50. @see goo.bigbutton, goo.checkbox
  51. ]]
  52. function goo.object:new( parent )
  53. --[[ Standard goo object structure.
  54. @class table
  55. @name goo_object
  56. @field parent The parent instance of the object.
  57. @field super The parent class of the object. To hook an internal function rather than override use super.function(self,...), see usage below.
  58. @field children A list of all children instances.
  59. @field x The x position.
  60. @field y The y position.
  61. @field w The width.
  62. @field h The height.
  63. @field bounds The bounding box. Represented by a table: {x1,y1,x2,y2}. Used for detecting mouse events.
  64. @field xscale The x scale.
  65. @field yscale The y scale.
  66. @field opacity The opacity of the object.
  67. @field visible Boolean representing if the object is being drawn.
  68. @field mouseHover Boolean representing if the mouse is ontop of the object.
  69. @usage <pre class='example'>
  70. button = goo.button:new()
  71. button.draw = function(self)
  72. super.draw(self)
  73. love.graphics.circle( 'line', 0, 0, 20 ) -- we use 0 for x,y because position is always relative to itself.
  74. end
  75. -- will draw a button with a circle ontop, not just a circle.</pre>
  76. ]]
  77. --[[ Sets the skin of goo, default is 'default'.
  78. <br/>Skins are kept in goo/skins/
  79. <br/>If you have already created objects you should use goo:setSkinAllObjects
  80. @param skin_name:string the name of the skin, i.e. 'default', 'dark'.
  81. @see goo:setSkinAllObjects
  82. ]]
  83. function goo:setSkin( skin_name )
  84. --[[ Sets the skin and updates all objects to use that skin.
  85. <br/>any objects with overriden styles will be set to the skin's style.
  86. @param skin_name:string the name of the skin, i.e. 'default', 'dark'.
  87. @see goo:setSkin
  88. ]]
  89. function goo:setSkinAllObjects( skin_name )
  90. --[[ Style table structure. Possible keys:
  91. @class table
  92. @name style
  93. @field backgroundColor
  94. @field backgroundColorHover
  95. @field borderColor
  96. @field borderColorHover
  97. @field borderWidth
  98. @field textColor
  99. @field textColorHover
  100. @field textFont
  101. @field color
  102. @field colorHover
  103. @field titleColor
  104. @field titleColorHover
  105. @field lineHeight
  106. @usage excerpt from default/style.lua <pre class="example">
  107. style['goo button'] = {
  108. backgroundColor = {100,100,100},
  109. backgroundColorHover = {131,203,21},
  110. borderColor = {0,0,0,255},
  111. borderColorHover = {0,0,0},
  112. textColor = {255,255,255},
  113. textColorHover = {255,255,255},
  114. textFont = fonts.oldsans12
  115. }
  116. </pre>
  117. ]]
  118. --[[ Show a panel with a button that prints a message on click.
  119. @name Panel with button.
  120. @class example
  121. @usage <pre class="example">
  122. local panel = goo.panel:new()
  123. panel:setPos( 10, 10 )
  124. panel:setSize( 200, 200 )
  125. panel:setTitle( 'I am a panel' )
  126. local button = goo.button:new( panel )
  127. button:setPos( 10, 20 )
  128. print( button:getAbsolutePos() ) -- 20, 30
  129. button:setText( 'Click me' )
  130. button:sizeToText()
  131. button.onClick = function( self, button )
  132. print( 'I have been clicked' )
  133. end
  134. </pre>
  135. ]]
  136. --[[ Show a panel that slides in. The panel has a checkbox which ticks when the animation is finished.
  137. @name Slide in panel.
  138. @class example
  139. @usage <pre class="example">
  140. local panel = goo.panel:new()
  141. panel:setPos( -250, 50 )
  142. panel:setSize( 200, 200 )
  143. panel:setTitle( 'I am a panel' )
  144. local checkbox = goo.checkbox:new( panel )
  145. checkbox:setPos( 10, 20 )
  146. function checkbox:onClick( button )
  147. print( self.class.name .. ' has been clicked' )
  148. end
  149. function checkbox:enterHover()
  150. print( self.class.name .. ' enter hover')
  151. end
  152. local slideIn = anim:new{
  153. table = panel,
  154. key = 'x',
  155. finish = 350,
  156. time = 2,
  157. style = 'expoOut'
  158. }
  159. slideIn:play()
  160. function slideIn:onFinish()
  161. checkbox:setChecked( true )
  162. end
  163. </pre>
  164. ]]
  165. --[[ Show a panel that zooms in bouncily with animated opacity too.
  166. The panel has a color picker which updates a textinput when pressed.
  167. @name Zoom in panel.
  168. @class example
  169. @usage <pre class="example">
  170. local testPanel = goo.panel:new()
  171. testPanel:setPos( 100, 50 )
  172. testPanel:setSize( 370, 500 )
  173. testPanel:setTitle( "This is a Color Panel." )
  174. testPanel:setOpacity( 10 )
  175. local colorPicker = goo.colorpick:new( testPanel )
  176. colorPicker:setPos(0,20)
  177. local input = goo.textinput:new( testPanel )
  178. input:setPos(3,486)
  179. input:setSize(360,20)
  180. input:setText('hello!')
  181. function colorPicker:onClick()
  182. local r,g,b = self:getColor()
  183. local str = string.format( 'Red = %i, Green = %i, Blue = %i', r,g,b)
  184. input:setText( str )
  185. end
  186. anim:easy( testPanel, 'xscale', 0, 1, 3, 'elastic')
  187. anim:easy( testPanel, 'yscale', 0.9, 1, 3, 'elastic')
  188. anim:easy( testPanel, 'opacity', 0, 255, 0.5, 'quadInOut')
  189. </pre>
  190. ]]
  191. --[[ Show a panel with two buttons, one of which has an overridden style.
  192. @name Overriding styles.
  193. @class example
  194. @usage <pre class="example">
  195. local panel = goo.panel:new()
  196. panel:setPos( 10, 10 )
  197. panel:setSize( 200, 200 )
  198. panel:setTitle( 'I am a panel' )
  199. local button = goo.button:new( panel )
  200. button:setPos( 10, 20 )
  201. print( button:getAbsolutePos() ) -- 20, 30
  202. button:setText( 'Click me' )
  203. button:sizeToText()
  204. button.onClick = function( self, button )
  205. print( 'I have been clicked' )
  206. end
  207. local redButton = goo.button:new( panel )
  208. redButton:setPos( 10, 100 )
  209. redButton:setText( 'My style has been overridden' )
  210. redButton:sizeToText( 10 )
  211. local redStyle = {
  212. backgroundColor = {255,0,0},
  213. backgroundColorHover = {125,0,0},
  214. borderColor = {0,0,0,0},
  215. borderColorHover = {0,0,0,0},
  216. }
  217. redButton:setStyle( redStyle )
  218. </pre>
  219. ]]