Advertisement
Guest User

Untitled

a guest
Dec 27th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1.  
  2. --[[
  3. - @brief 電卓
  4. - @author Ryoga
  5. - @since 2014/12/27
  6. -
  7. - マウスイベントを用いるため、アドバンスド(コンピュータ|タートル)のみに対応
  8. - 計算にはevalを使ってしまった
  9. ]]
  10.  
  11. --[[
  12. - ボタンオブジェクト
  13. ]]
  14. local Button = {
  15. --[[
  16. - ボタンオブジェクトの生成
  17. - @param text ボタンに表示される文字列
  18. - @param x ボタンの表示X座標
  19. - @param y ボタンの表示Y座標
  20. - @param onClicked クリック時に呼び出されるコールバック関数
  21. ]]
  22. new = function(text, x, y, onClicked)
  23. return {
  24. text = text,
  25. x = x,
  26. y = y,
  27. width = string.len(text),
  28. callback = onClicked,
  29. }
  30. end,
  31.  
  32. --[[
  33. - ボタンの表示
  34. - @param b 表示するボタン
  35. ]]
  36. render = function(b)
  37. term.setCursorPos(b.x, b.y);
  38. term.write(b.text)
  39. end,
  40.  
  41. --[[
  42. - 渡された座標がボタンの上に乗っているか
  43. - @param b 確認するボタン
  44. - @param x ボタン上にあるかを確認する点のX座標
  45. - @param y ボタン上にあるかを確認する点のY座標
  46. - @return (x,y)がボタン'b'上の点であった場合trueを返す
  47. ]]
  48. isCollidedWithPoint = function(b, x, y)
  49. return (b.x <= x) and (x <= b.x+b.width) and (b.y == y)
  50. end,
  51. }
  52.  
  53. local isQuit = false
  54. local expr = ""
  55.  
  56. --[[
  57. - 計算を行う
  58. - 式に誤りがあった場合exprに"ERROR"
  59. - 式に誤りがなかった場合exprに計算結果を代入する
  60. ]]
  61. local function calculate()
  62. local result = loadstring("return "..expr)
  63. expr = (result == nil) and "ERROR" or result()
  64. end
  65.  
  66.  
  67. -- ボタンの生成
  68. local buttons = {
  69. Button.new(" 0 ", 2, 9, function() expr = expr.."0" end),
  70. Button.new("1" , 2, 7, function() expr = expr.."1" end),
  71. Button.new("2" , 4, 7, function() expr = expr.."2" end),
  72. Button.new("3" , 6, 7, function() expr = expr.."3" end),
  73. Button.new("4" , 2, 5, function() expr = expr.."4" end),
  74. Button.new("5" , 4, 5, function() expr = expr.."5" end),
  75. Button.new("6" , 6, 5, function() expr = expr.."6" end),
  76. Button.new("7" , 2, 3, function() expr = expr.."7" end),
  77. Button.new("8" , 4, 3, function() expr = expr.."8" end),
  78. Button.new("9" , 6, 3, function() expr = expr.."9" end),
  79. Button.new("." , 6, 9, function() expr = expr.."." end),
  80. Button.new("+" , 8, 3, function() expr = expr.."+" end),
  81. Button.new("-" , 8, 5, function() expr = expr.."-" end),
  82. Button.new("*" , 8, 7, function() expr = expr.."*" end),
  83. Button.new("/" , 8, 9, function() expr = expr.."/" end),
  84. Button.new("=" , 10, 9, calculate),
  85. Button.new("<" , 10, 5, function() expr = string.sub(expr, 1, -2) end),
  86. Button.new("C" , 10, 7, function() expr = "" end),
  87. Button.new("END", 12, 9, function() isQuit = true end),
  88. }
  89.  
  90. -- メインループ
  91. while not isQuit do
  92. -- 画面のクリア
  93. term.setBackgroundColor(colors.black)
  94. term.clear()
  95.  
  96. -- 文字色と背景色の設定
  97. term.setBackgroundColor(colors.white)
  98. term.setTextColor(colors.black)
  99.  
  100. -- 式の表示
  101. term.setCursorPos(2, 1)
  102. write(expr)
  103.  
  104. -- ボタンの表示
  105. for i, b in ipairs(buttons) do
  106. Button.render(b)
  107. end
  108.  
  109. -- クリックイベントの取得
  110. local event, mouseButton, x, y = os.pullEvent("mouse_click")
  111.  
  112. -- ボタンに応じたイベントの呼び出し
  113. for i, b in ipairs(buttons) do
  114. if(Button.isCollidedWithPoint(b, x, y)) then
  115. -- ボタンが押された
  116. if(b.callback) then
  117. b.callback()
  118. end
  119. end
  120. end
  121. end
  122.  
  123. -- 終了した後はカーソルを戻す
  124. term.setBackgroundColor(colors.black)
  125. term.setTextColor(colors.white)
  126. term.clear()
  127.  
  128. term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement