Advertisement
br1wr2el3

Code 06 - Calculator

Apr 19th, 2013
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. --# Code 06 - Calculator Main
  2. -- Can be uploaded to Codea
  3.  
  4. -- Calculator Controller
  5.  
  6. -- Bruce Elliott
  7. -- April 2013
  8.  
  9. -- Model: CalculatorModel
  10. -- View: Button and SetKeyboard
  11. -- Controller: Main
  12.  
  13. -- Use this function to perform your initial setup
  14. function setup()
  15. -- Initialize Keyboard
  16. -- Initialize Calculator Model
  17. print("Calculator Project")
  18.  
  19. SetKeyboard:init()
  20. CalculatorModel:init()
  21.  
  22. end
  23.  
  24. -- This function gets called once every frame
  25. function draw()
  26. -- Do calculator processing
  27. -- Set value for the display (output results)
  28. -- Draw calculator face
  29. -- This sets a dark background color
  30. background(40, 40, 50)
  31.  
  32. -- Do your drawing here
  33. CalculatorModel:draw()
  34. kTab[17] = Button(showValue, 200, HEIGHT - HEIGHT/8)
  35. SetKeyboard:drawButton()
  36.  
  37. end
  38.  
  39. function touched(touch)
  40. -- Check each key for press
  41. for k, v in pairs(kTab) do
  42. temp= v:touched(touch)
  43. end
  44. end
  45.  
  46. function buttonPressed(newName)
  47. -- Tell CalculatorModel which button was pressed
  48. -- print("main "..newName)
  49. CalculatorModel:buttonPressed(newName)
  50. end
  51.  
  52. --# Book 06 - CalculatorModel
  53. CalculatorModel = class()
  54.  
  55. function CalculatorModel:init()
  56. -- Setup calculator model
  57. -- Operations use two operands valueOne and valueTwo
  58. -- Set display to zero
  59. -- Default operation set to addition (+)
  60. -- you can accept and set parameters here
  61.  
  62. valueOne = 0
  63. valueTwo = 0
  64. operation = "+"
  65. display = 0
  66. end
  67.  
  68. function CalculatorModel:draw()
  69. -- Send display value to controller
  70.  
  71. showValue = display
  72.  
  73. end
  74.  
  75.  
  76. function CalculatorModel:buttonPressed(keyName)
  77. -- process each key press
  78.  
  79. thisName = keyName
  80.  
  81. if keyName == "C" then
  82. -- "C" Clear all do rest
  83. C()
  84. elseif keyName == "+" then
  85. -- "+" Store plus operation
  86. plus()
  87. elseif keyName == "-" then
  88. -- "-" Store minus operation
  89. minus()
  90. elseif keyName == "*" then
  91. -- "*" Store product operation
  92. times()
  93. elseif keyName == "/" then
  94. -- "/" Store divide operation
  95. divide()
  96. elseif keyName == "=" then
  97. -- "=" process operation to get new result
  98. equal()
  99. else
  100. -- everything esle means process digit
  101. digit()
  102. end
  103. end
  104.  
  105. function C()
  106. -- Set model to defaults
  107. valueOne = 0
  108. valueTwo = 0
  109. operation = "+"
  110. display = 0
  111. end
  112.  
  113. function plus()
  114. -- store display, set operation to "+"
  115. valueOne = display
  116. display = 0
  117. operation = "+"
  118. end
  119. function minus()
  120. -- store display, set operation to "-"
  121. valueOne = display
  122. display = 0
  123. operation = "-"
  124. end
  125. function times()
  126. -- store display, set operation to "*"
  127. valueOne = display
  128. display = 0
  129. operation = "*"
  130. end
  131. function divide()
  132. -- store display, set operation to "/"
  133. -- no divide by zero check system generates "inf"
  134. valueOne = display
  135. display = 0
  136. operation = "/"
  137. end
  138.  
  139. function digit()
  140. --add digit to display
  141. if display == 0 then
  142. display = thisName
  143. else
  144. display = display..thisName
  145. end
  146. end
  147.  
  148. function equal()
  149. -- generate result
  150. valueTwo = display
  151.  
  152. if operation == "+" then
  153. result = valueOne + valueTwo
  154.  
  155. elseif operation == "-" then
  156. result = valueOne - valueTwo
  157.  
  158. elseif operation == "*" then
  159. result = valueOne * valueTwo
  160.  
  161. elseif operation == "/" then
  162. result = valueOne / valueTwo
  163.  
  164. end
  165. -- display result, save result
  166. display = result
  167. valueOne = result
  168. valueTwo = 0
  169. operation ="+"
  170. end
  171.  
  172. --# Book 06 Button
  173. Button = class()
  174.  
  175. function Button:init(displayName, x, y)
  176. -- Initialize individual btton object
  177. -- you can accept and set parameters here
  178. self.displayName = displayName
  179.  
  180. self.pos = vec2(x,y)
  181. self.size = vec2(0,0)
  182. self.action = nil
  183. self.color = color(113,66,190,255)
  184. -- print(self.displayName.." "..self.pos.x.." "..self.pos.y)
  185. end
  186.  
  187. function Button:draw()
  188. -- Draw individual buttons
  189. -- Codea does not automatically call this method
  190.  
  191. -- pushStyle saves any style info
  192. pushStyle()
  193.  
  194. font("ArialRoundedMTBold")
  195. fontSize(22)
  196.  
  197. -- Determine string size
  198. local w,h = textSize(self.displayName)
  199. w = w + 20
  200. h = h + 2
  201.  
  202. -- Draw rectangle
  203. rect(self.pos.x - w/2,
  204. self.pos.y - h/2,
  205. w, h)
  206. self.size = vec2(w,h)
  207.  
  208. -- Put label on key
  209. textMode(CENTER)
  210. fill(54,65,96,255)
  211. text(self.displayName,self.pos.x,self.pos.y)
  212.  
  213. -- restore previous style info
  214. popStyle()
  215. end
  216.  
  217. function Button:hit(p)
  218. -- Did touch hit one of our keys.
  219. local w,h = textSize(self.displayName)
  220. w = w + 20
  221. h = h + 40
  222. local l = self.pos.x - w/2
  223. local r = self.pos.x + w/2
  224. local t = self.pos.y + h/2
  225. local b = self.pos.y - h/2
  226.  
  227. if p.x > l and p.x < r
  228. and p.y > b and p.y < t then
  229. -- key was pressed
  230. return true
  231. end
  232. -- key not pressed
  233. return false
  234. end
  235.  
  236.  
  237. function Button:touched(touch)
  238. -- React to a touch
  239. -- Codea does not automatically call this method
  240. if touch.state == ENDED and
  241. self:hit(vec2(touch.x,touch.y)) then
  242. -- a calc key was pressed
  243. if self.action then
  244. newName = self.displayName
  245. self.action()
  246.  
  247. end
  248. end
  249.  
  250. end
  251.  
  252. function Button:action()
  253. -- Send button name to controller
  254. buttonPressed(newName)
  255. end
  256.  
  257. --# Book 06 SetKeyboard
  258. SetKeyboard = class()
  259.  
  260. function SetKeyboard:init()
  261. -- Setup calculator button objects
  262. -- you can accept and set parameters here
  263. kTab={}
  264. kTab[1] = Button("1", 100, HEIGHT-HEIGHT/4)
  265. kTab[2] = Button("2", 200,HEIGHT-HEIGHT/4)
  266. kTab[3] = Button("3",300,HEIGHT-HEIGHT/4)
  267. kTab[11] = Button("+",400,HEIGHT-HEIGHT/4)
  268. kTab[4] = Button("4",100,HEIGHT-HEIGHT/4-80)
  269. kTab[5] = Button("5",200,HEIGHT-HEIGHT/4-80)
  270. kTab[6] = Button("6",300,HEIGHT-HEIGHT/4-80)
  271. kTab[12] = Button("-",400,HEIGHT-HEIGHT/4 - 80)
  272. kTab[7] = Button("7",100,HEIGHT-HEIGHT/4-160)
  273. kTab[8] = Button("8",200,HEIGHT-HEIGHT/4-160)
  274. kTab[9] = Button("9",300,HEIGHT-HEIGHT/4-160)
  275. kTab[13] = Button("*",400,HEIGHT-HEIGHT/4-160)
  276. kTab[10] = Button("0",200,HEIGHT-HEIGHT/4-240)
  277. kTab[15] = Button("C",100,HEIGHT-HEIGHT/4-240)
  278. kTab[14] = Button("/",400,HEIGHT-HEIGHT/4-240)
  279. kTab[16] = Button("=",300,HEIGHT-HEIGHT/4-240)
  280.  
  281. kTab[17] = Button("0",300,HEIGHT-HEIGHT/8)
  282.  
  283. end
  284.  
  285. function SetKeyboard:drawButton()
  286. -- Draw the keys for the keyboard
  287. for k, v in pairs(kTab) do
  288. v:draw()
  289. end
  290.  
  291. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement