Guest User

Error - Rightclickmenu - Mikkel809h

a guest
Mar 29th, 2014
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.09 KB | None | 0 0
  1. local BACKGROUNDCOLOR = colors.lightGray
  2. local TEXTCOLOR = colors.white
  3.  
  4. local tbl = {}
  5. tbl = {
  6.   menu = function( self )
  7.     local content = self.content
  8.     local maxS=0
  9.     for k,v in pairs( content ) do
  10.       if v.text then
  11.         if string.len( v.text ) > maxS then
  12.          maxS = string.len( v.text )
  13.         end
  14.       end
  15.     end
  16.     local menLength = maxS+2
  17.     local menHeight = #content
  18.     for k,v in pairs(content) do
  19.       if not v.text then
  20.         v.text = string.rep( v.repeatString, menLength )
  21.       end
  22.     end
  23.     local width, height = term.getSize()
  24.     local _X, _Y = nil, nil
  25.     if self.x > ( width - menLength + 1 ) then
  26.       _X = width - menLength + 1;
  27.     end
  28.     if self.y > ( height - menHeight + 1 ) then
  29.       _Y = height - menHeight + 1;
  30.     end
  31.     term.setCursorPos( _X or self.x, _Y or self.y )
  32.     local position = { term.getCursorPos() }
  33.     local curY = position[2]
  34.     for k,v in ipairs( content ) do
  35.       term.setBackgroundColor( v.backgroundColor )
  36.       term.setTextColor( v.textColor )
  37.       term.setCursorPos( math.floor( position[1] ), curY )
  38.       local startPos = math.floor( position[1] )
  39.       local xPos = math.floor( position[1] + menLength / 2 - string.len(v.text) / 2 )
  40.       local endPos = math.floor( position[1] + menLength )
  41.       local currentPos = { term.getCursorPos() }
  42.       local yMin = curY;
  43.       local yMax = curY;
  44.       local xMin = startPos;
  45.       local xMax = endPos;
  46.       for x = startPos, endPos do
  47.         term.setCursorPos( currentPos[1], currentPos[2] )
  48.         if currentPos[1] < xPos then
  49.           term.write(" ")
  50.         elseif currentPos[1] > xPos then
  51.           term.write(" ")
  52.         elseif currentPos[1] == xPos then
  53.           write(v.text)
  54.         end
  55.         currentPos = { term.getCursorPos() }
  56.         x = currentPos[1]
  57.         yPos = currentPos[2]
  58.         if x >= endPos then
  59.           break
  60.         end
  61.       end
  62.       v["position"] = {}
  63.       v["position"]["xPos"] = xPos;
  64.       v["position"]["xMin"] = startPos;
  65.       v["position"]["xMax"] = endPos;
  66.       v["position"]["yMin"] = yMin;
  67.       v["position"]["yMax"] = yMax;
  68.       curY = curY + 1
  69.     end
  70.     return {
  71.       initialize = function( x, y )
  72.         local con = self.content;
  73.         local flash = function(menu)
  74.           term.setBackgroundColor( colors.gray )
  75.           term.setTextColor( colors.white )
  76.           term.setCursorPos( con[menu]["position"]["xMin"], con[menu]["position"]["yMin"] )
  77.           local currentPos = { term.getCursorPos() }
  78.           for y = con[menu]["position"]["yMin"], con[menu]["position"]["yMax"] do
  79.             for x = con[menu]["position"]["xMin"], con[menu]["position"]["xMax"] do
  80.               term.setCursorPos( currentPos[1], currentPos[2] )
  81.               if x == con[menu]["position"]["xPos"] then
  82.                 write(con[menu].text)
  83.               else
  84.                 term.write(" ")
  85.               end
  86.               currentPos = { term.getCursorPos() }
  87.               x = currentPos[1]
  88.               if x >= con[menu]["position"]["xMax"] then
  89.                 break
  90.               end
  91.             end
  92.           end
  93.           sleep(0.1)
  94.           term.setBackgroundColor( con[menu].backgroundColor )
  95.           term.setTextColor( con[menu].textColor )
  96.           term.setCursorPos( con[menu]["position"]["xMin"], con[menu]["position"]["yMin"] )
  97.           local currentPos = { term.getCursorPos() }
  98.           for y = con[menu]["position"]["yMin"], con[menu]["position"]["yMax"] do
  99.             for x = con[menu]["position"]["xMin"], con[menu]["position"]["xMax"] do
  100.               term.setCursorPos( currentPos[1], currentPos[2] )
  101.               if x == con[menu]["position"]["xPos"] then
  102.                 write(con[menu].text)
  103.               else
  104.                 term.write(" ")
  105.               end
  106.               currentPos = { term.getCursorPos() }
  107.               x = currentPos[1]
  108.               if x >= con[menu]["position"]["xMax"] then
  109.                 break
  110.               end
  111.             end
  112.           end
  113.           sleep(0.1)
  114.         end
  115.         for k,v in ipairs(con) do
  116.           if x >= v["position"]["xMin"]
  117.           and x < v["position"]["xMax"]
  118.           and y >= v["position"]["yMin"]
  119.           and y <= v["position"]["yMax"]
  120.           then
  121.             if v["func"] then
  122.               flash(k)
  123.               --v["func"]()
  124.               return true
  125.             end
  126.           end
  127.         end
  128.         self.content = nil
  129.         term.setBackgroundColor( BACKGROUNDCOLOR )
  130.         term.clear()
  131.         return false
  132.       end;
  133.       remove = function()
  134.         local con = self.content;
  135.         term.setBackgroundColor( BACKGROUNDCOLOR )
  136.         term.setTextColor( TEXTCOLOR )
  137.         for menu, v in pairs(con) do
  138.           term.setCursorPos( con[menu]["position"]["xMin"], con[menu]["position"]["yMin"] )
  139.           local currentPos = { term.getCursorPos() }
  140.           for y = con[menu]["position"]["yMin"], con[menu]["position"]["yMax"] do
  141.             for x = con[menu]["position"]["xMin"], con[menu]["position"]["xMax"] do
  142.               term.setCursorPos( currentPos[1], currentPos[2] )
  143.               term.write(" ")
  144.               currentPos = { term.getCursorPos() }
  145.               x = currentPos[1]
  146.               if x >= con[menu]["position"]["xMax"] then
  147.                 break
  148.               end
  149.             end
  150.           end
  151.         end
  152.         self.content = nil
  153.         return true
  154.       end;
  155.       ["content"]=self.content;
  156.     }
  157.   end;
  158. }
  159.  
  160. term.setBackgroundColor( BACKGROUNDCOLOR )
  161. term.setTextColor( TEXTCOLOR )
  162. term.clear()
  163. while true do
  164.   e = { coroutine.yield() }
  165.   if e[1] == "terminate" then
  166.     os.reboot()
  167.   elseif e[1] == "mouse_click" then
  168.     --term.setBackgroundColor( colors.gray )
  169.     --term.clear()
  170.     if e[2] == 1 then
  171.       if clickMenu ~= nil then
  172.         local resp = clickMenu.initialize( e[3], e[4] )
  173.         if resp == true then
  174.           clickMenu.remove()
  175.           clickMenu = nil
  176.         end
  177.       else
  178.       end
  179.     elseif e[2] == 2 then
  180.       if clickMenu ~= nil then
  181.         clickMenu.remove()
  182.         clickMenu = nil
  183.       end
  184.       mytbl={
  185.         x=e[3];
  186.         y=e[4];
  187.         content = {
  188.           {
  189.             text="New";
  190.             backgroundColor=colors.white;
  191.             textColor=colors.lime;
  192.             func=true;
  193.           };
  194.           {
  195.             text=false;
  196.             repeatString="-";
  197.             backgroundColor=colors.red;
  198.             textColor=colors.white;
  199.             func=false;
  200.           };
  201.           {
  202.             text="Nsd";
  203.             backgroundColor=colors.white;
  204.             textColor=colors.green;
  205.             func=true;
  206.           };
  207.           {
  208.             text="Set";
  209.             backgroundColor=colors.white;
  210.             textColor=colors.orange;
  211.             func=true;
  212.           };
  213.           {
  214.             text="stp";
  215.             backgroundColor=colors.white;
  216.             textColor=colors.red;
  217.             func=true;
  218.           };
  219.         };
  220.       }
  221.       setmetatable( mytbl, { __index = tbl } )
  222.       clickMenu = mytbl:menu()
  223.     end
  224.   end
  225. end
  226. term.setCursorPos(1,18)
Advertisement
Add Comment
Please, Sign In to add comment