Advertisement
Pirnogion

rcmenu

Oct 30th, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.73 KB | None | 0 0
  1. os.loadAPI("utils")
  2.  
  3. --constant
  4. local MAXCHAR = 20
  5. local MAXITEMS = 5
  6. local BLINKTIME = 0.2
  7.  
  8. --buffer variables
  9. local dy = 0
  10. local dx = 0
  11.  
  12. local bf = {}
  13. local rClickedPos = {0, 0}
  14.  
  15. --Private functions
  16. local CloseMenu = function()
  17.  local _clsstr = utils.FillString("", MAXCHAR)
  18.  
  19.  for i = 0, #bf-1, 1 do
  20.   term.setCursorPos(rClickedPos[1]-dx, rClickedPos[2]+i-dy)
  21.   print(_clsstr)
  22.  end
  23. end
  24.  
  25. local BLINKTIMEItemMenu = function(num)
  26.  utils.SetPalette("select")
  27.  term.setCursorPos(rClickedPos[1]-dx, rClickedPos[2]+num-dy)
  28.  print(bf[num+1]["text"])
  29.  
  30.  os.sleep(BLINKTIME)
  31.  
  32.  utils.SetPalette("os")
  33.  term.setCursorPos(rClickedPos[1]-dx, rClickedPos[2]+num-dy)
  34.  
  35.  print(bf[num+1]["text"])
  36. end
  37.  
  38. local DrawMenu = function()
  39.  local _bflen = #bf
  40.  
  41.  dx = 0
  42.  dy = 0
  43.  _ww, _wh = term.getSize();
  44.  
  45.  if (rClickedPos[1]+MAXCHAR > _ww) then dx = MAXCHAR end
  46.  if (rClickedPos[2]+_bflen > _wh-1) then dy = _bflen end
  47.  
  48.  utils.SetPalette("standart")
  49.  
  50.  for i = 0, _bflen-1, 1 do
  51.   term.setCursorPos(rClickedPos[1]-dx, rClickedPos[2]+i-dy)
  52.   print(bf[i+1]["text"])
  53.  end
  54.  
  55.  utils.SetPalette("os")
  56. end
  57.  
  58. --MainFunction
  59. Update = function()
  60.  if (#bf == 0) then error("Error: expected at least one element.") end
  61.  
  62.  local event, btn, xPos, yPos = os.pullEvent()
  63.  
  64.  if (event ~= "mouse_click") then return end
  65.  
  66.  if (btn == 2) then
  67.   CloseMenu()
  68.   rClickedPos[1] = xPos
  69.   rClickedPos[2] = yPos
  70.   DrawMenu()
  71.  elseif (btn == 1) then
  72.  
  73.   for i = 0, #bf-1, 1 do  
  74.    if ((xPos >= rClickedPos[1] and xPos <= rClickedPos[1]+MAXCHAR) and yPos == rClickedPos[2]+i-dy) then
  75.     BLINKTIMEItemMenu(i)
  76.     CloseMenu()
  77.     bf[i+1]["func"]()
  78.     break
  79.    end
  80.   end
  81.  
  82.   CloseMenu()
  83.    
  84.  end
  85.  
  86. end
  87.  
  88. AddItem = function(id, text, func)
  89.  if (type(id) ~= "number") then
  90.   error("Error: Expected number, got " .. type(text) .. ".")
  91.  elseif (type(text) ~= "string") then
  92.   error("Error: Expected string, got " .. type(text) .. ".")
  93.  elseif (type(func) ~= "function") then
  94.   error("Error: Expected function, got " .. type(text) .. ".")
  95.  elseif (id <= 0) then
  96.   error("Error: Id <= 0.")
  97.  elseif (id > MAXITEMS) then
  98.   error("Error: Id > " .. MAXITEMS .. ".")
  99.  elseif (#text > MAXCHAR) then
  100.   error("Error: The maximum length of this string " .. MAXCHAR .. ".")
  101.  elseif (#bf == MAXITEMS) then
  102.   error("Error: The maximum number of elements " .. MAXITEMS .. ".")
  103.  end
  104.  
  105.  bf[id] = {}
  106.  bf[id]["text"] = utils.FillString(text, MAXCHAR)
  107.  bf[id]["func"] = func
  108. end
  109.  
  110. RemoveItem = function(id)
  111.  if (type(id) ~= "number") then
  112.   error("Error: Expected number, got " .. type(text) .. ".")
  113.  elseif (id <= 0) then
  114.   error("Error: Id <= 0.")
  115.  elseif (id > MAXITEMS) then
  116.   error("Error: Id > " .. MAXITEMS .. ".")
  117.  end
  118.  
  119.  table.remove(bf, id)
  120. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement