document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. -- ############################################
  2. -- btn_ctrl
  3. -- version 0.1
  4. -- http://hevohevo.hatenablog.com/
  5.  
  6. -- ################## config ##################
  7. CtrlMonSide = "top"
  8. ----------------------------
  9. -- btns[1]  btns[2]  btns[3]
  10. -- btns[4]  btns[5]  btns[6]
  11. -- btns[7]  btns[8]  btns[9]
  12. ----------------------------
  13. Btns = {}
  14. Btns[1] = {name="atk",   cmd="turtle.attack()"}
  15. Btns[2] = {name="dig",   cmd="turtle.dig()"}
  16. Btns[3] = {name="place", cmd="turtle.place()"}
  17. Btns[4] = {name="<=",    cmd="turtle.turnLeft()"}
  18. Btns[5] = {name="suck",  cmd="turtle.suck()"}
  19. Btns[6] = {name="=>",    cmd="turtle.turnRight()"}
  20. Btns[7] = {name="sel1",  cmd="turtle.select(1)"}
  21. Btns[8] = {name="sel2",  cmd="turtle.select(2)"}
  22. Btns[9] = {name="sel16", cmd="turtle.select(16)"}
  23.  
  24. -- ################## functions ###############
  25. -- evaluate a function-string
  26. -- ex. eval("turltle.select(1)")
  27. function eval(s)
  28.   print("  ",s)
  29.   assert(loadstring(s))()
  30. end
  31.  
  32. -- return btns-table
  33. function makeNineButtons(ctrl_mon, Btns)
  34.   local mon_w, mon_h = ctrl_mon.getSize()
  35.   local btn_w = math.floor(mon_w/3)
  36.   local btn_h = math.floor(mon_h/3)
  37.   local btns = {}
  38.   local i=1 -- table index
  39.   for row=1,3 do
  40.     for col=1,3 do
  41.       btns[i] = {
  42.         name=Btns[i]["name"], cmd=Btns[i]["cmd"],
  43.         min_x = 1+(btn_w)*(col-1), min_y = 1+(btn_h)*(row-1),
  44.         max_x = btn_w*col, max_y = btn_h*row}
  45.       i = i+1
  46.     end
  47.   end
  48.   return btns
  49. end
  50.  
  51. function drowButtons(mon, buttons)
  52.   for i,b in pairs(buttons) do
  53.     local center_x = math.floor((b.min_x + b.max_x)/2)
  54.     local center_y = math.floor((b.min_y + b.max_y)/2)
  55.     local center_label = math.floor(string.len(b.name)/2)
  56.     mon.setCursorPos(center_x - center_label, center_y)
  57.     mon.write(b.name)
  58.   end
  59. end
  60.  
  61. -- whichButton(buttons, 1, 1) => btn     (btn-table)
  62. --                            => false   (don\'t pushed)
  63. function whichButton(buttons, x, y)
  64.   local function within(min_num, max_num, num)
  65.           return (min_num <= num and max_num >= num)
  66.         end
  67.   local pushed_btn = false
  68.  
  69.   for i,v in pairs(buttons) do
  70.     if within(v.min_x, v.max_x, x) and within(v.min_y, v.max_y, y) then
  71.       pushed_btn = v
  72.       break
  73.     end
  74.   end
  75.  
  76.   return pushed_btn
  77. end
  78.  
  79. -- event_name, pushed_btn = pullPushButtonEvent(buttons_table, "top")
  80. function pullPushButtonEvent(buttons, mon_dir)
  81.   local pushed_btn = false
  82.   repeat
  83.     local event, dir, x, y = os.pullEvent("monitor_touch")
  84.     if mon_dir == dir  then
  85.       pushed_btn = whichButton(buttons, x, y)
  86.     end
  87.   until  pushed_btn
  88.  
  89.   return "push_button", pushed_btn
  90. end
  91.  
  92. -- ################## main #####################
  93. -- init ctrl monitor
  94. local CtrlMon = peripheral.wrap(CtrlMonSide)
  95. CtrlMon.setTextScale(0.5)
  96. CtrlMon.clear()
  97.  
  98. -- make buttons and drow on ctrl monitor
  99. local mon_btns = makeNineButtons(CtrlMon, Btns)
  100. drowButtons(CtrlMon, mon_btns)
  101.  
  102. -- main loop
  103. while true do
  104.   local event_name, pushed_btn = pullPushButtonEvent(mon_btns, CtrlMonSide)
  105.   if event_name == "push_button" then
  106.     print("pushed: ",pushed_btn.name)
  107.     eval(pushed_btn.cmd)
  108.   end
  109.  
  110.   sleep(0) -- "monitor_touch" event is too sensitive.
  111. end
');