Advertisement
hevohevo

ComputerCraft Tutorial: monitor_button0_1

Jan 5th, 2014
1,399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.18 KB | None | 0 0
  1. -- ############################################
  2. -- monitor_button
  3. -- version 0.1
  4. -- http://hevohevo.hatenablog.com/
  5.  
  6. -- ################## config ##################
  7. CtrlMonSide = "top"
  8. MainMonSide = "right"
  9. CharSymbol = "@"
  10.  
  11. -- ################## functions ###############
  12. function writeEventInfo(mon, event_name, btn)
  13.   mon.setCursorPos(1,1)
  14.   mon.clearLine()
  15.   mon.write(string.format("%s [ %s ]", event_name, btn.name))
  16. end
  17.  
  18. function writePosInfo(mon, goal_x, goal_y)
  19.   mon.setCursorPos(1,2)
  20.   mon.clearLine()
  21.   mon.write(string.format("(%d,%d)", goal_x, goal_y))
  22. end
  23.  
  24. -- local current_x, current_y = moveSymbol(mon, start_x, start_y, goal_x, goal_y)
  25. function moveSymbol(mon, start_x, start_y, goal_x, goal_y)
  26.   writePosInfo(mon, goal_x, goal_y)  
  27.   mon.setCursorPos(start_x, start_y)
  28.   mon.write(" ")
  29.   mon.setCursorPos(goal_x,goal_y)
  30.   mon.write(CharSymbol)
  31.  
  32.   return goal_x, goal_y
  33. end
  34.  
  35. -- return btns-table
  36. function makeSixButtons(ctrl_mon)
  37.   local names = {"reset", "up", "quit", "left", "down", "right"}
  38.   local mon_w, mon_h = ctrl_mon.getSize()
  39.   local btn_w = math.floor(mon_w/3)
  40.   local btn_h = math.floor(mon_h/2)
  41.   ----------------------------
  42.   -- btns[1]  btns[2]  btns[3]
  43.   -- btns[4]  btns[5]  btns[6]
  44.   ----------------------------
  45.   local btns = {}
  46.   local i = 1 -- btn index
  47.   for row=1,2 do
  48.     for col=1,3 do
  49.       btns[i] = {name=names[i], min_x=1+(btn_w)*(col-1), max_x=btn_w*col, min_y=1+(btn_h)*(row-1), max_y=btn_h*row }
  50.       i = i+1
  51.     end
  52.   end
  53.  
  54.   return btns
  55. end
  56.  
  57. function drowButtons(mon, buttons)
  58.   for i,b in pairs(buttons) do
  59.     local center_x = math.floor((b.min_x + b.max_x)/2)
  60.     local center_y = math.floor((b.min_y + b.max_y)/2)
  61.     local center_label = math.floor(string.len(b.name)/2)
  62.     mon.setCursorPos(center_x - center_label, center_y)
  63.     mon.write(b.name)
  64.   end
  65. end
  66.  
  67. -- whichButton(buttons, 1, 1) => btn     (btn-table)
  68. --                            => false   (don't pushed)
  69. function whichButton(buttons, clicked_x, clicked_y)
  70.   local function within(min_num, max_num, num)
  71.           return (min_num <= num and max_num >= num)
  72.         end
  73.   local pushed_btn = false
  74.  
  75.   for i,v in pairs(buttons) do
  76.     if within(v.min_x, v.max_x, clicked_x) and within(v.min_y, v.max_y, clicked_y) then
  77.       pushed_btn = v
  78.       break
  79.     end
  80.   end
  81.  
  82.   return pushed_btn
  83. end
  84.  
  85. -- event_name, pushed_btn = pullPushButtonEvent(buttons_table, "top")
  86. function pullPushButtonEvent(buttons, mon_dir)
  87.   local pushed_btn = false
  88.   repeat
  89.     local event, dir, x, y = os.pullEvent("monitor_touch")
  90.     if mon_dir == dir then
  91.       pushed_btn = whichButton(buttons, x, y)
  92.     end
  93.   until  pushed_btn
  94.  
  95.   return "push_button", pushed_btn
  96. end
  97.  
  98. -- ################## main #####################
  99.  
  100. -- init main monitor
  101. local main_mon = peripheral.wrap(MainMonSide)
  102. main_mon.setTextScale(1)
  103. main_mon.clear()
  104.  
  105. -- drow a charactor on main monitor
  106. local main_mon_w, main_mon_h = main_mon.getSize()
  107. local init_x = math.floor(main_mon_w/2)  -- charactor's initial pos.
  108. local init_y = math.floor(main_mon_h/2)
  109. local x, y = moveSymbol(main_mon, init_x, init_y, init_x, init_y) -- charactor's current pos.
  110.  
  111. -- init ctrl monitor
  112. local ctrl_mon = peripheral.wrap(CtrlMonSide)
  113. ctrl_mon.setTextScale(0.5)
  114. ctrl_mon.clear()
  115.  
  116. -- make buttons and drow on ctrl monitor
  117. local buttons = makeSixButtons(ctrl_mon)
  118. drowButtons(ctrl_mon, buttons)
  119.  
  120. -- main loop
  121. repeat
  122.   local event_name, pushed_btn = pullPushButtonEvent(buttons, CtrlMonSide)
  123.   writeEventInfo(main_mon, event_name, pushed_btn)
  124.  
  125.   if pushed_btn.name=="reset" then -- move char to initial pos.
  126.     x, y = moveSymbol(main_mon, x, y, init_x, init_y)
  127.   elseif pushed_btn.name=="up" and y > 4 then
  128.     x, y = moveSymbol(main_mon, x, y, x, y-1)
  129.   elseif pushed_btn.name=="down" and y < main_mon_h  then
  130.     x, y = moveSymbol(main_mon, x, y, x, y+1)
  131.   elseif pushed_btn.name=="left" and x > 1 then
  132.     x, y = moveSymbol(main_mon, x, y, x-1, y)
  133.   elseif pushed_btn.name=="right" and x < main_mon_w then
  134.     x, y = moveSymbol(main_mon, x, y, x+1, y)
  135.   else
  136.     -- do nothing
  137.   end
  138.  
  139.   sleep(0) -- Don't delete because "monitor_touch" event is too sensitive.
  140. until pushed_btn.name=="quit"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement