Guest User

items

a guest
Mar 22nd, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.38 KB | None | 0 0
  1. function initialize(side)
  2.   m = peripheral.wrap(side)
  3.   m.setBackgroundColor(colors.black)
  4.   m.clear()
  5.   m.setCursorPos(1,1)
  6.   items = {}
  7.   m_x,m_y = m.getSize()
  8. end
  9.  
  10.  
  11. function clear()
  12.   m.clear()
  13.   for i = 1, #items do
  14.     items[i]["loc"] = {}
  15.   end
  16. end
  17.  
  18.  
  19. function mkItem(...)
  20.   local id = #items + 1
  21.   items[id] = {}
  22.   items[id]["type"] = arg[1]
  23.   items[id]["name"] = arg[2]
  24.  
  25.   if arg[1] == "btn" then
  26.     items[id]["text"] = arg[3]
  27.     items[id]["w"] = arg[4]
  28.     items[id]["h"] = arg[5]
  29.     items[id]["txtColor"] = arg[6]
  30.     items[id]["bgColor"] = arg[7]
  31.     items[id]["fn"] = arg[8]
  32.     items[id]["args"] = arg[9]
  33.     items[id]["loc"] = {}
  34.   elseif arg[1] == "scn" then
  35.     items[id]["items"] = arg[3]
  36.     items[id]["bgColor"] = arg[4]
  37.   end
  38. end
  39.  
  40.  
  41. function dispItem(...)
  42.  
  43.   for i = 1, #items do
  44.     if items[i]["name"] == arg[1] then
  45.       item = items[i]
  46.       if item["type"] ~= "scn" then
  47.         items[i]["loc"][#items[i]["loc"] + 1] = {arg[2],arg[3]}
  48.       end
  49.       break
  50.     end
  51.   end
  52.  
  53.   if item["type"] == "btn" then
  54.  
  55.     m.setBackgroundColor(item["bgColor"])
  56.     m.setTextColor(item["txtColor"])
  57.    
  58.     for i = 1, item["h"] do
  59.       m.setCursorPos(arg[2], arg[3]+i-1)
  60.       if i ~= math.ceil(item["h"]/2) then
  61.         for j = 1, item["w"] do
  62.           m.write(" ")
  63.         end
  64.       else
  65.         for j = 1, math.floor((item["w"] - string.len(item["text"])) / 2) do
  66.           m.write(" ")
  67.         end
  68.         m.write(item["text"])
  69.         for j = 1, math.ceil( (item["w"] - string.len(item["text"])) / 2) do
  70.           m.write(" ")
  71.         end
  72.       end
  73.     end
  74.    
  75.   elseif item["type"] == "scn" then
  76.     clear()
  77.     m.setBackgroundColor(item["bgColor"])
  78.     for i = 1, m_y do
  79.       m.setCursorPos(1,i)
  80.       for j = 1, m_x do
  81.         m.write(" ")
  82.       end
  83.     end
  84.     for i = 1, #item["items"] do
  85.       dispItem(item["items"][i][1],item["items"][i][2],item["items"][i][3])
  86.     end
  87.  
  88.   end
  89.  
  90. end
  91.  
  92.  
  93. function waitEvent()
  94.   _,_,x,y = os.pullEvent("monitor_touch")
  95.   for i = 1, #items do
  96.     if items[i]["type"] == "btn" then
  97.       for j = 1, #items[i]["loc"] do
  98.         if x >= items[i]["loc"][j][1] and x < items[i]["loc"][j][1] + items[i]["w"] and y >= items[i]["loc"][j][2] and y < items[i]["loc"][j][2] + items[i]["h"] then
  99.           items[i]["fn"](items[i]["args"])
  100.         end
  101.       end
  102.     end
  103.   end
  104. end
Advertisement
Add Comment
Please, Sign In to add comment