Advertisement
Guest User

init.lua

a guest
Mar 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.98 KB | None | 0 0
  1. local comp = require "component"
  2. local colors = require "colors"
  3. local gpu = comp.gpu
  4. local string = require "string"
  5. local computer = require "computer"
  6. local width,height = gpu.getResolution()
  7. local steps = 100
  8. local API = {}
  9. local screen = {}
  10. local colors = {}
  11. colors["red"]=0xff0000
  12. colors["green"]=0x00ff00
  13. colors["blue"]=0x0000ff
  14. colors["yellow"]=0xffff00
  15. colors["purple"]=0xff00ff
  16. colors["teal"]=0xffff00
  17.  
  18. local function getColors(total_colors)
  19. bar_colors = {}
  20. red = 0xff
  21. green = 0x00
  22. z = 0
  23. w = 0
  24.  
  25. while z <= total_colors-1 do
  26.   if green < 0xff then
  27.     green = math.floor(z*2*256/total_colors)
  28.     if green > 0xff then
  29.       green = 0xff
  30.       print(z,green)
  31.     end
  32.   else
  33.     w=w+1
  34.     red = 0xff - math.floor(w*2*256/total_colors)
  35.     if red < 0 then
  36.       red = 0
  37.     end
  38.   end
  39.   bar_colors[z] = red*0x010000+green*0x000100
  40.   z = z+1
  41. end
  42. bar_colors["total"]=z
  43. return bar_colors
  44. end
  45.  
  46.  
  47. API_changed = true
  48.  
  49. function API.createScreen(name,half)
  50.     screen[name]={}
  51.     screen[name]["active"]=false
  52.     screen[name]["button_list"]={}
  53.     screen[name]["bar_list"]={}
  54.     screen[name]["half"]=half
  55.     screen[name]["side"]=nil
  56.     screen[name]["xOffset"]=0
  57.     screen[name]["yOffset"]=0
  58.     screen[name]["width"]=nil
  59.     screen[name]["height"]=nil
  60.     screen[name]["changed"]=true
  61.     end
  62.  
  63. function API.createButton(scname,name,x,y,width,height,color,pressedcolor,text,toggle)
  64.     screen[scname].button_list[name]={}
  65.     screen[scname].button_list[name]["x"]=x
  66.     screen[scname].button_list[name]["y"]=y
  67.     screen[scname].button_list[name]["width"]=width
  68.     screen[scname].button_list[name]["height"]=height
  69.     screen[scname].button_list[name]["pressedcolor"]=colors[pressedcolor]
  70.     screen[scname].button_list[name]["color"]=colors[color]
  71.     screen[scname].button_list[name]["text"]=text
  72.     screen[scname].button_list[name]["toggle"]=toggle
  73.     screen[scname].button_list[name]["pressed"]=false
  74.     screen[scname].button_list[name]["true_y"]=0
  75.     screen[scname].button_list[name]["true_x"]=0
  76.     screen[scname].button_list[name]["true_width"]=0
  77.     screen[scname].button_list[name]["true_height"]=0
  78.     screen[scname].button_list[name]["parent"]=scname
  79.     screen[scname].button_list[name]["changed"]=true
  80.     end
  81.  
  82. function API.createBar(scname,x,y,total_width,perc,name,reverse)
  83.     screen[scname].bar_list[name]={}
  84.     screen[scname].bar_list[name]["x"]=x
  85.     screen[scname].bar_list[name]["y"]=y
  86.     screen[scname].bar_list[name]["width"]=total_width
  87.     screen[scname].bar_list[name]["perc"]=perc
  88.     screen[scname].bar_list[name]["reverse"]=reverse
  89.     screen[scname].bar_list[name]["true_y"]=0
  90.     screen[scname].bar_list[name]["true_x"]=0
  91.     screen[scname].bar_list[name]["true_width"]=0
  92.     screen[scname].bar_list[name]["parent"]=scname
  93.     screen[scname].bar_list[name]["changed"]=true
  94.   end
  95.  
  96. function API.updateBar(name,perc,sc)
  97.     screen[sc].bar_list[name]["perc"]=perc
  98.     screen[sc].bar_list[name]["changed"]=true
  99.     API_changed = true
  100.   end
  101.  
  102. function API.activateScreen(name,side)
  103.     screen[name]["active"]=true
  104.     if screen[name]["half"] then
  105.       screen[name]["side"]=side
  106.       if side == "right" then
  107.         screen[name]["xOffset"]=width/2
  108.         screen[name]["width"]=width/2
  109.         screen[name]["height"]=height
  110.       elseif side == "left" then
  111.         screen[name]["width"]=width/2
  112.         screen[name]["height"]=height
  113.       elseif side == "bottom" then
  114.         screen[name]["yOffset"]=height/2
  115.         screen[name]["width"]=width
  116.         screen[name]["height"]=height/2
  117.       elseif side == "top" then
  118.         screen[name]["width"]=width
  119.         screen[name]["height"]=height/2
  120.       end
  121.     else
  122.       screen[name]["width"]=width
  123.       screen[name]["height"]=height
  124.     end
  125.     for bt,btdata in pairs(screen[name].button_list) do
  126.         screen[name].button_list[bt]["true_y"]=math.floor(btdata["y"]*screen[name]["height"]+screen[name]["yOffset"])
  127.         screen[name].button_list[bt]["true_x"]=math.floor(btdata["x"]*screen[name]["width"]+screen[name]["xOffset"])
  128.         screen[name].button_list[bt]["true_width"]=math.floor(btdata["width"]*screen[name]["width"])
  129.         screen[name].button_list[bt]["true_height"]=math.floor(btdata["height"]*screen[name]["height"])
  130.         screen[name].button_list[bt]["changed"]=true
  131.     end
  132.     for bar,bardata in pairs(screen[name].bar_list) do
  133.         screen[name].bar_list[bar]["true_y"]=math.floor(bardata["y"]*screen[name]["height"]+screen[name]["yOffset"])
  134.         screen[name].bar_list[bar]["true_x"]=math.floor(bardata["x"]*screen[name]["width"]+screen[name]["xOffset"])
  135.         screen[name].bar_list[bar]["true_width"]=math.floor(bardata["width"]*screen[name]["width"])
  136.         screen[name].bar_list[bar]["changed"]=true
  137.     end
  138.     API_changed=true
  139. end
  140.  
  141. function API.deactivateScreen(name)
  142.     screen[name]["active"]=false
  143.     gpu.fill(screen[name]["xOffset"],screen[name]["yOffset"],screen[name]["width"],screen[name]["height"]," ")
  144.     screen[name]["xOffset"] = 0
  145.     screen[name]["yOffset"] = 0
  146.     screen[name]["side"]=nil
  147.     API_changed=true
  148. end
  149.  
  150. function API.clear()
  151.   local screen = {}
  152.   end
  153.  
  154. function API.render()
  155.   if API_changed == true then
  156.     --gpu.fill(1,1,width,height,' ')
  157.     for sc,scdata in pairs(screen) do
  158.       if scdata["active"] then
  159.         for name,data in pairs(screen[sc].button_list) do
  160.           if data["changed"] then
  161.             gpu.fill(data["true_x"],data["true_y"],data["true_width"],data["true_height"]," ")
  162.             API.drawButton(data["true_x"],data['true_y'],data['true_width'],data['true_height'],data['color'],data['text'])
  163.             screen[sc].button_list[name]["changed"]=false
  164.           end
  165.         end
  166.         for name,data in pairs(screen[sc].bar_list) do
  167.       if data["changed"] then
  168.             gpu.fill(data["true_x"],data["true_y"],data["true_width"],2," ")
  169.             API.drawBar(data["true_x"],data["true_y"],data["true_width"],name,data["perc"],data["reverse"])
  170.       screen[sc].bar_list[name]["changed"]=false
  171.       end
  172.         end
  173.       end
  174.     end
  175.   end
  176.   API_changed = false
  177. end
  178.  
  179. function API.drawButton(x,y,w,h,color,text)
  180.     oldColor = gpu.getBackground()
  181.     gpu.setBackground(color)
  182.     gpu.fill(math.floor(x),math.floor(y),math.floor(w),math.floor(h)," ")
  183.     gpu.set(x+math.floor(w/2)-math.floor(string.len(text)/2),y+math.floor(h/2)-1,text)
  184.     gpu.setBackground(oldColor)
  185.     end
  186.  
  187. function API.drawBar(x,y,total_width,name,perc,reverse)
  188.   oldColor = gpu.getBackground()
  189.   oldFore = gpu.getForeground()
  190.   str_width = string.len(name)
  191.   bar_width = total_width-str_width-1
  192.   bar_colors = getColors(bar_width)
  193.   bar_color_step = math.floor(bar_width/bar_colors["total"])
  194.   total_colors=bar_colors["total"]
  195.   rest = bar_width%bar_colors["total"]
  196.   if rest == bar_width then
  197.     rest = 0
  198.   end
  199.   n=0
  200.   gpu.setForeground(0)
  201.   if reverse then
  202.     gpu.setBackground(bar_colors[total_colors-n-1])
  203.   else
  204.     gpu.setBackground(bar_colors[n])
  205.   end
  206.   gpu.fill(x,y,rest,2,"|")
  207.   while n <= bar_colors["total"]-1 do
  208.     if reverse then
  209.       gpu.setBackground(bar_colors[(total_colors-n-1)])
  210.     else
  211.       gpu.setBackground(bar_colors[n])
  212.     end
  213.     gpu.fill(x+rest+bar_color_step*n,y,bar_color_step,2,"|")
  214.     n=n+1
  215.   end
  216.   pixel_per_perc = bar_width/100
  217.   gpu.setBackground(oldColor)
  218.   gpu.setForeground(oldFore)
  219.   gpu.fill(x+perc*pixel_per_perc,y,math.ceil((100-perc)*pixel_per_perc),2," ")
  220.   --print(x+total_width-str_width,name)
  221.   gpu.set(x+total_width-str_width,y,name)
  222.   gpu.set(x+total_width-1-string.len(tostring(perc)),y+1,tostring(perc).."%")
  223. end
  224.  
  225. function API.press(name,data,sc)
  226.     if data['toggle'] then
  227.         txt = screen[sc].button_list[name]['text']
  228.         clr1 = screen[sc].button_list[name]['color']
  229.         txt2 = screen[sc].button_list[name]['toggle']
  230.         clr2 = screen[sc].button_list[name]['pressedcolor']
  231.         screen[sc].button_list[name]['text']=txt2
  232.         screen[sc].button_list[name]['toggle']=txt
  233.         screen[sc].button_list[name]['color']=clr2
  234.         screen[sc].button_list[name]['pressedcolor']=clr1
  235.         if data['pressed'] then
  236.           computer.pushSignal("toggled_off",name)
  237.         else
  238.           computer.pushSignal("toggled_on",name)
  239.         end
  240.         screen[sc].button_list[name]["pressed"] = not screen[sc].button_list[name]["pressed"]
  241.     else
  242.         API.drawButton(data['true_x'],data['true_y'],data['true_width'],data['true_height'],data['pressedcolor'],data['text'])
  243.         os.sleep(0.1)
  244.         computer.pushSignal("clicked",name)
  245.     end
  246.     screen[sc].button_list[name]['changed']=true
  247.     API_changed = true
  248. end
  249.  
  250. function API.click(x,y)
  251.   for sc, scdata in pairs(screen) do
  252.     if scdata["active"] then
  253.       for name, data in pairs(scdata.button_list) do
  254.         if x>=data['true_x'] and x<=data['true_x']+data['true_width'] then
  255.           if y>=data['true_y'] and y<=data['true_y']+data['true_height'] then
  256.             API.press(name,data,sc)
  257.           end
  258.         end
  259.       end
  260.     for name, data in pairs(scdata.bar_list) do
  261.       if x>=data['true_x'] and x <= data['true_x']+data['true_width'] then
  262.         if y>=data['true_y'] and y <= data['true_y']+2 then
  263.           computer.pushSignal("barClicked",name)
  264.          end
  265.         end
  266.       end
  267.     end
  268.   end
  269. end
  270.  
  271. return API
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement