Advertisement
hevohevo

ComputerCraft Tutorial: btn_ctrl0_2

Jan 8th, 2014
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.75 KB | None | 0 0
  1. -- ############################################
  2. -- btn_ctrl
  3. -- version 0.2
  4. -- http://hevohevo.hatenablog.com/
  5.  
  6. -- ################## config ##################
  7. CtrlMonSide = "top"
  8. CtrlMon = peripheral.wrap(CtrlMonSide)
  9. ----------------------------
  10. -- btns[1]  btns[2]  btns[3]
  11. -- btns[4]  btns[5]  btns[6]
  12. -- btns[7]  btns[8]  btns[9]
  13. ----------------------------
  14. Btns = {}
  15. Btns[1] = {name="atk",   cmd="turtle.attack()"}
  16. Btns[2] = {name="dig",   cmd="turtle.dig()"}
  17. Btns[3] = {name="place", cmd="turtle.place()"}
  18. Btns[4] = {name="<=",    cmd="turtle.turnLeft()"}
  19. Btns[5] = {name="suck",  cmd="turtle.suck()"}
  20. Btns[6] = {name="=>",    cmd="turtle.turnRight()"}
  21. Btns[7] = {name="<sel", cmd="turtle.selectPrev()"}
  22. Btns[8] = {name="sel1",  cmd="turtle.select(1)"}
  23. Btns[9] = {name="sel>", cmd="turtle.selectNext()"}
  24.  
  25. -- ################## functions ###############
  26. -- new turtle commands
  27. turtle.select(1)
  28. CurrentSlot = 1 -- for retaining current selected slot number
  29. OrgSelectCmd = turtle.select -- for redefining function
  30.  
  31. function turtle.select(slot) -- redefine function
  32.   CurrentSlot = slot
  33.   OrgSelectCmd(slot)
  34.   print("  slot: ", slot)
  35. end
  36.  
  37. function return1_16(num)
  38.   if (num % 16) == 0 then
  39.     return 16
  40.   else
  41.     return (num % 16)
  42.   end
  43. end
  44.  
  45. function turtle.selectNext() -- define new function
  46.   turtle.select(return1_16(CurrentSlot + 1))
  47. end
  48.  
  49. function turtle.selectPrev() -- define new-function
  50.   turtle.select(return1_16(CurrentSlot - 1))
  51. end
  52.  
  53. -- evaluate a function-string
  54. -- ex. eval("turltle.select(1)")
  55. function eval(s)
  56.   print("  ",s)
  57.   assert(loadstring(s))()
  58. end
  59.  
  60. -- return btns-table
  61. function makeNineButtons(ctrl_mon, Btns)
  62.   local mon_w, mon_h = ctrl_mon.getSize()
  63.   local btn_w = math.floor(mon_w/3)
  64.   local btn_h = math.floor(mon_h/3)
  65.   local btns = {}
  66.   local i=1 -- table index
  67.   for row=1,3 do
  68.     for col=1,3 do
  69.       btns[i] = {
  70.         name=Btns[i]["name"], cmd=Btns[i]["cmd"],
  71.         min_x = 1+(btn_w)*(col-1), min_y = 1+(btn_h)*(row-1),
  72.         max_x = btn_w*col, max_y = btn_h*row}
  73.       i = i+1
  74.     end
  75.   end
  76.   return btns
  77. end
  78.  
  79. function drowButtons(mon, buttons)
  80.   for i,b in pairs(buttons) do
  81.     local center_x = math.floor((b.min_x + b.max_x)/2)
  82.     local center_y = math.floor((b.min_y + b.max_y)/2)
  83.     local center_label = math.floor(string.len(b.name)/2)
  84.     mon.setCursorPos(center_x - center_label, center_y)
  85.     mon.write(b.name)
  86.   end
  87. end
  88.  
  89. -- whichButton(buttons, 1, 1) => btn     (btn-table)
  90. --                            => false   (don't pushed)
  91. function whichButton(buttons, x, y)
  92.   local function within(min_num, max_num, num)
  93.           return (min_num <= num and max_num >= num)
  94.         end
  95.   local pushed_btn = false
  96.  
  97.   for i,v in pairs(buttons) do
  98.     if within(v.min_x, v.max_x, x) and within(v.min_y, v.max_y, y) then
  99.       pushed_btn = v
  100.       break
  101.     end
  102.   end
  103.  
  104.   return pushed_btn
  105. end
  106.  
  107. -- event_name, pushed_btn = pullPushButtonEvent(buttons_table, "top")
  108. function pullPushButtonEvent(buttons, mon_dir)
  109.   local pushed_btn = false
  110.   repeat
  111.     local event, dir, x, y = os.pullEvent("monitor_touch")
  112.     if mon_dir == dir  then
  113.       pushed_btn = whichButton(buttons, x, y)
  114.     end
  115.   until  pushed_btn
  116.  
  117.   return "push_button", pushed_btn
  118. end
  119.  
  120. -- ################## main #####################
  121. -- init ctrl monitor
  122. CtrlMon.setTextScale(0.5)
  123. CtrlMon.clear()
  124.  
  125. -- make buttons and drow on ctrl monitor
  126. local mon_btns = makeNineButtons(CtrlMon, Btns)
  127. drowButtons(CtrlMon, mon_btns)
  128.  
  129. -- main loop
  130. while true do
  131.   local event_name, pushed_btn = pullPushButtonEvent(mon_btns, CtrlMonSide)
  132.   if event_name == "push_button" then
  133.     print("pushed: ",pushed_btn.name)
  134.     eval(pushed_btn.cmd)
  135.   end
  136.  
  137.   sleep(0) -- "monitor_touch" event is too sensitive.
  138. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement