Advertisement
br1wr2el3

Code 02b Button Demo

Apr 18th, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. --# Button
  2. -- Code 02b - Button Demo
  3. -- Tested for copying into Codea
  4.  
  5. Button = class()
  6.  
  7. function Button:init(displayName)
  8.  
  9. -- The init function gets called before setup().
  10. -- This is where you define and initialize your class and
  11. -- its member variables. The class variables are fairly
  12. -- self explanatory but for completeness:
  13. --
  14. -- displayName: Is the text displayed on your button.
  15. -- The button will scale up and down to fit the text.
  16. --
  17. -- pos: Defines the x and y - coordinates of the button
  18. -- using a vector (vec2).
  19. --
  20. -- size: Is a vector which contains the width and height
  21. --
  22. -- action: Is the function that you want called when
  23. -- the button is tapped. Here no action is defined.
  24. --
  25. -- color: Is the color of the button fill.
  26.  
  27. self.displayName = displayName
  28.  
  29. self.pos = vec2(0, 0)
  30. self.size = vec2(0, 0)
  31. self.action = nil
  32. self.color = color(113, 66, 190, 255)
  33. end
  34.  
  35. function Button:draw()
  36.  
  37. pushStyle()
  38.  
  39. -- pushStyle() saves the current graphic styles like stroke,
  40. -- width, etc. You can then do your thing and
  41. -- call popStyle at the end to return to this state.
  42.  
  43. fill(self.color)
  44.  
  45. -- fill is used initially to set the color of the button, then
  46. -- the font type and size is set. You could change this in
  47. -- your implementation of the button class if you wish.
  48.  
  49. font("ArialRoundedMTBold")
  50. fontSize(22)
  51.  
  52. -- use display name for size
  53.  
  54. local w, h = textSize(self.displayName)
  55. w = w + 20
  56. h = h + 30
  57.  
  58. -- As stated in the code, displayName is used to size
  59. -- the button and then we use the class rect to draw
  60. -- a rectangle
  61.  
  62. rect(self.pos.x - w/2,
  63. self.pos.y - h/2,
  64. w, h)
  65. self.size = vec2(w, h)
  66.  
  67. -- Note that class variables are designated using the
  68. -- self keyword. e.g. self.size. The next block of code sets
  69. -- the color of the button text and its position
  70. -- on the button.
  71.  
  72. textMode(CENTER)
  73. fill(54, 65, 96, 255)
  74. text(self.displayName, self.pos.x, self.pos.y)
  75.  
  76. -- Return the graphic style to what it was before you
  77. -- entered this function. This is considered polite
  78. -- behavior for a function because it can be hard to track
  79. -- down if the style is being changed deep within some
  80. -- function and you don't want it to.
  81.  
  82. popStyle()
  83.  
  84. end
  85.  
  86. --# ButtonDemo
  87.  
  88. function setup()
  89. print("Button Test Project")
  90.  
  91. -- Call Button:init()
  92. -- pass word to be displayed on button
  93. -- get a Button and save to button
  94.  
  95. button = Button("Test")
  96.  
  97. end
  98.  
  99. function draw()
  100.  
  101. -- set background color
  102. background(40,40,50)
  103.  
  104. -- call drawButton()
  105. drawButton()
  106. end
  107.  
  108. function drawButton()
  109.  
  110. -- Store button position into button.pos
  111. button.pos = vec2(400, HEIGHT / 2)
  112.  
  113. -- Call draw function for button
  114. button:draw()
  115. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement