Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ############################################
- -- btn_ctrl
- -- version 0.1
- -- http://hevohevo.hatenablog.com/
- -- ################## config ##################
- CtrlMonSide = "top"
- ----------------------------
- -- btns[1] btns[2] btns[3]
- -- btns[4] btns[5] btns[6]
- -- btns[7] btns[8] btns[9]
- ----------------------------
- Btns = {}
- Btns[1] = {name="atk", cmd="turtle.attack()"}
- Btns[2] = {name="dig", cmd="turtle.dig()"}
- Btns[3] = {name="place", cmd="turtle.place()"}
- Btns[4] = {name="<=", cmd="turtle.turnLeft()"}
- Btns[5] = {name="suck", cmd="turtle.suck()"}
- Btns[6] = {name="=>", cmd="turtle.turnRight()"}
- Btns[7] = {name="sel1", cmd="turtle.select(1)"}
- Btns[8] = {name="sel2", cmd="turtle.select(2)"}
- Btns[9] = {name="sel16", cmd="turtle.select(16)"}
- -- ################## functions ###############
- -- evaluate a function-string
- -- ex. eval("turltle.select(1)")
- function eval(s)
- print(" ",s)
- assert(loadstring(s))()
- end
- -- return btns-table
- function makeNineButtons(ctrl_mon, Btns)
- local mon_w, mon_h = ctrl_mon.getSize()
- local btn_w = math.floor(mon_w/3)
- local btn_h = math.floor(mon_h/3)
- local btns = {}
- local i=1 -- table index
- for row=1,3 do
- for col=1,3 do
- btns[i] = {
- name=Btns[i]["name"], cmd=Btns[i]["cmd"],
- min_x = 1+(btn_w)*(col-1), min_y = 1+(btn_h)*(row-1),
- max_x = btn_w*col, max_y = btn_h*row}
- i = i+1
- end
- end
- return btns
- end
- function drowButtons(mon, buttons)
- for i,b in pairs(buttons) do
- local center_x = math.floor((b.min_x + b.max_x)/2)
- local center_y = math.floor((b.min_y + b.max_y)/2)
- local center_label = math.floor(string.len(b.name)/2)
- mon.setCursorPos(center_x - center_label, center_y)
- mon.write(b.name)
- end
- end
- -- whichButton(buttons, 1, 1) => btn (btn-table)
- -- => false (don't pushed)
- function whichButton(buttons, x, y)
- local function within(min_num, max_num, num)
- return (min_num <= num and max_num >= num)
- end
- local pushed_btn = false
- for i,v in pairs(buttons) do
- if within(v.min_x, v.max_x, x) and within(v.min_y, v.max_y, y) then
- pushed_btn = v
- break
- end
- end
- return pushed_btn
- end
- -- event_name, pushed_btn = pullPushButtonEvent(buttons_table, "top")
- function pullPushButtonEvent(buttons, mon_dir)
- local pushed_btn = false
- repeat
- local event, dir, x, y = os.pullEvent("monitor_touch")
- if mon_dir == dir then
- pushed_btn = whichButton(buttons, x, y)
- end
- until pushed_btn
- return "push_button", pushed_btn
- end
- -- ################## main #####################
- -- init ctrl monitor
- local CtrlMon = peripheral.wrap(CtrlMonSide)
- CtrlMon.setTextScale(0.5)
- CtrlMon.clear()
- -- make buttons and drow on ctrl monitor
- local mon_btns = makeNineButtons(CtrlMon, Btns)
- drowButtons(CtrlMon, mon_btns)
- -- main loop
- while true do
- local event_name, pushed_btn = pullPushButtonEvent(mon_btns, CtrlMonSide)
- if event_name == "push_button" then
- print("pushed: ",pushed_btn.name)
- eval(pushed_btn.cmd)
- end
- sleep(0) -- "monitor_touch" event is too sensitive.
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement