Advertisement
br1wr2el3

Code 03 - Button Extended

Apr 19th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. -- Code 03 - Button Extended
  2. -- Can be uploaded to Codea
  3. -- uses code by David Such
  4. -- Bruce Elliott
  5. -- April 2013
  6.  
  7. -- Button returns its label when pressed
  8.  
  9. --# Button
  10. Button = class()
  11.  
  12. function Button:init(displayName)
  13. -- you can accept and set
  14. -- parameters here
  15.  
  16. self.displayName = displayName
  17. self.pos = vec2(0,0)
  18. self.size = vec2(0,0)
  19. self.action = nil
  20. self.color = color(68, 65, 190, 255)
  21. end
  22.  
  23. function Button:draw()
  24. -- Codea does not automatically
  25. -- Determine button size based on displayName
  26. -- Draw the button (uses rect)
  27. -- Use text to draw text on button
  28.  
  29. pushStyle()
  30. fill(self.color)
  31. font("ArialRoundedMTBold")
  32. fontSize(22)
  33.  
  34. local w, h = textSize(self.displayName)
  35. w = w + 20
  36. h = h + 20
  37.  
  38. rect(self.pos.x - w/2,
  39. self.pos.y - h/2,
  40. w, h)
  41. self.size = vec2(w, h)
  42.  
  43. textMode(CENTER)
  44. fill(54,65,96,255)
  45. text(self.displayName,
  46. self.pos.x, self.pos.y)
  47.  
  48. popStyle()
  49. end
  50.  
  51. function Button:hit(p)
  52. -- Accepts p - x and y of the touch
  53. -- Button knows its position
  54. -- If touch is inside button return true
  55. -- otherwise return false
  56.  
  57. local l = self.pos.x - self.pos.x/2
  58. local r = self.pos.x + self.pos.x/2
  59. local t = self.pos.y + self.pos.y/2
  60. local b = self.pos.y - self.pos.y/2
  61.  
  62. if p.x > l and p.x < r
  63. and p.y > b and p.y < t then
  64.  
  65. return true
  66. end
  67.  
  68. return false
  69. end
  70.  
  71. function Button:touched(touch)
  72. -- if touch has ended and the touch was on the button
  73. -- set newName and call action
  74.  
  75. if touch.state == ENDED and
  76. self:hit(vec2(touch.x, touch.y)) then
  77. if self.action then
  78. newName = self.displayName
  79. self.action()
  80. end
  81. end
  82. end
  83.  
  84. function Button:action()
  85. -- call buttonPressed in main to display newName
  86.  
  87. buttonPressed(newName)
  88. end
  89.  
  90. --# Main
  91. -- ButtonDemo
  92.  
  93. -- Use this function to perform
  94. -- our initial setup
  95. function setup()
  96. -- Create button with a name
  97.  
  98. print("Button Test Project")
  99. button = Button("Test")
  100. end
  101.  
  102. -- This function gets called once every frame
  103. function draw()
  104. -- calls drawButton to put button on screen
  105.  
  106. -- This sets a dark background color
  107. background(0)
  108.  
  109. -- This sets the line thickness
  110. -- strokeWidth(5)
  111.  
  112. -- Do your drawing here
  113. drawButton()
  114. end
  115.  
  116. function drawButton()
  117. -- set button position
  118. -- call button draw
  119.  
  120. button.pos = vec2(400, HEIGHT/2)
  121. button:draw()
  122. end
  123.  
  124. function touched(touch)
  125. -- if touched call button:touched
  126. button:touched(touch)
  127. end
  128.  
  129. function buttonPressed(keyName)
  130. -- display button name if it was pressed
  131.  
  132. print("Button: "..keyName)
  133. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement