Advertisement
DigitalZilla

mystCraft_PortalComputer

Sep 14th, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.79 KB | None | 0 0
  1. TURTLE = 96;
  2. Data = {
  3.     {
  4.         name = "Plant",
  5.         slot = 2,
  6.         color = colors.cyan,
  7.     },
  8.     {
  9.         name = "Factory",
  10.         slot = 3,
  11.         color = colors.lightGray,
  12.     },
  13.     {
  14.         name = "MtFluff",
  15.         slot = 4,
  16.         color = colors.lightBlue,
  17.     },
  18.     {
  19.         name = "Tree",
  20.         slot = 5,
  21.         color = colors.green,
  22.     },
  23. };
  24.  
  25. rednet.open("top");
  26. monitor = peripheral.wrap("back");
  27. monitor.clear();
  28.  
  29. SelectedIndex = 0;
  30.  
  31. function write(text, x, y, fc, bc)
  32.     fc = fc or colors.white;
  33.     bc = bc or colors.black;
  34.     x = x or 1;
  35.     y = y or 1;
  36.  
  37.     monitor.setCursorPos(x, y);
  38.     monitor.setBackgroundColor(bc);
  39.     monitor.setTextColor(fc);
  40.     monitor.write(text);
  41. end
  42.  
  43. function getCenteredOffset(text)
  44.     local width = monitor.getSize();
  45.     return math.floor((width - #text) / 2) + 1;
  46. end
  47.  
  48. function getButtonOffset()
  49.     local width, height = monitor.getSize();
  50.     return (height - ((#Data * 4) - 1)) / 2;
  51. end
  52.  
  53. function getHitButton(yPos)
  54.     local yOffset = getButtonOffset();
  55.     return math.max(1, math.floor((yPos - yOffset - 1) / 4) + 1);
  56. end
  57.  
  58. function drawScreen()
  59.     monitor.setBackgroundColor(colors.black);
  60.     monitor.clear();
  61.  
  62.     local width, height = monitor.getSize();
  63.     local yOffset = getButtonOffset();
  64.  
  65.     for i = 1, #Data do
  66.         local button = Data[i];
  67.         local y = ((i - 1) * 4) + 1 + yOffset;
  68.         write(button.name, getCenteredOffset(button.name), y);
  69.  
  70.         -- draw button borders
  71.         for j = 1, 2 do
  72.             write(" ", 1, y + j, nil, colors.white);
  73.             write(" ", width, y + j, nil, colors.white);
  74.  
  75.             -- highlight button
  76.             if (i == SelectedIndex) then
  77.                 write(string.rep(" ", width - 2), 2, y + j, nil, button.color)
  78.             end
  79.         end
  80.     end
  81. end
  82.  
  83. while true do
  84.     drawScreen();
  85.     local event, side, x, y = os.pullEvent("monitor_touch");
  86.  
  87.     print(x, y);
  88.  
  89.     SelectedIndex = getHitButton(y);
  90.     rednet.send(TURTLE, Data[SelectedIndex].slot);
  91. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement