Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- - @brief 電卓
- - @author Ryoga
- - @since 2014/12/27
- -
- - マウスイベントを用いるため、アドバンスド(コンピュータ|タートル)のみに対応
- - 計算にはevalを使ってしまった
- ]]
- --[[
- - ボタンオブジェクト
- ]]
- local Button = {
- --[[
- - ボタンオブジェクトの生成
- - @param text ボタンに表示される文字列
- - @param x ボタンの表示X座標
- - @param y ボタンの表示Y座標
- - @param onClicked クリック時に呼び出されるコールバック関数
- ]]
- new = function(text, x, y, onClicked)
- return {
- text = text,
- x = x,
- y = y,
- width = string.len(text),
- callback = onClicked,
- }
- end,
- --[[
- - ボタンの表示
- - @param b 表示するボタン
- ]]
- render = function(b)
- term.setCursorPos(b.x, b.y);
- term.write(b.text)
- end,
- --[[
- - 渡された座標がボタンの上に乗っているか
- - @param b 確認するボタン
- - @param x ボタン上にあるかを確認する点のX座標
- - @param y ボタン上にあるかを確認する点のY座標
- - @return (x,y)がボタン'b'上の点であった場合trueを返す
- ]]
- isCollidedWithPoint = function(b, x, y)
- return (b.x <= x) and (x <= b.x+b.width) and (b.y == y)
- end,
- }
- local isQuit = false
- local expr = ""
- --[[
- - 計算を行う
- - 式に誤りがあった場合exprに"ERROR"
- - 式に誤りがなかった場合exprに計算結果を代入する
- ]]
- local function calculate()
- local result = loadstring("return "..expr)
- expr = (result == nil) and "ERROR" or result()
- end
- -- ボタンの生成
- local buttons = {
- Button.new(" 0 ", 2, 9, function() expr = expr.."0" end),
- Button.new("1" , 2, 7, function() expr = expr.."1" end),
- Button.new("2" , 4, 7, function() expr = expr.."2" end),
- Button.new("3" , 6, 7, function() expr = expr.."3" end),
- Button.new("4" , 2, 5, function() expr = expr.."4" end),
- Button.new("5" , 4, 5, function() expr = expr.."5" end),
- Button.new("6" , 6, 5, function() expr = expr.."6" end),
- Button.new("7" , 2, 3, function() expr = expr.."7" end),
- Button.new("8" , 4, 3, function() expr = expr.."8" end),
- Button.new("9" , 6, 3, function() expr = expr.."9" end),
- Button.new("." , 6, 9, function() expr = expr.."." end),
- Button.new("+" , 8, 3, function() expr = expr.."+" end),
- Button.new("-" , 8, 5, function() expr = expr.."-" end),
- Button.new("*" , 8, 7, function() expr = expr.."*" end),
- Button.new("/" , 8, 9, function() expr = expr.."/" end),
- Button.new("=" , 10, 9, calculate),
- Button.new("<" , 10, 5, function() expr = string.sub(expr, 1, -2) end),
- Button.new("C" , 10, 7, function() expr = "" end),
- Button.new("END", 12, 9, function() isQuit = true end),
- }
- -- メインループ
- while not isQuit do
- -- 画面のクリア
- term.setBackgroundColor(colors.black)
- term.clear()
- -- 文字色と背景色の設定
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- -- 式の表示
- term.setCursorPos(2, 1)
- write(expr)
- -- ボタンの表示
- for i, b in ipairs(buttons) do
- Button.render(b)
- end
- -- クリックイベントの取得
- local event, mouseButton, x, y = os.pullEvent("mouse_click")
- -- ボタンに応じたイベントの呼び出し
- for i, b in ipairs(buttons) do
- if(Button.isCollidedWithPoint(b, x, y)) then
- -- ボタンが押された
- if(b.callback) then
- b.callback()
- end
- end
- end
- end
- -- 終了した後はカーソルを戻す
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement