Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local comp = require "component"
- local colors = require "colors"
- local gpu = comp.gpu
- local string = require "string"
- local computer = require "computer"
- local width,height = gpu.getResolution()
- local steps = 100
- local API = {}
- local screen = {}
- local colors = {}
- colors["red"]=0xff0000
- colors["green"]=0x00ff00
- colors["blue"]=0x0000ff
- colors["yellow"]=0xffff00
- colors["purple"]=0xff00ff
- colors["teal"]=0xffff00
- local function getColors(total_colors)
- bar_colors = {}
- red = 0xff
- green = 0x00
- z = 0
- w = 0
- while z <= total_colors-1 do
- if green < 0xff then
- green = math.floor(z*2*256/total_colors)
- if green > 0xff then
- green = 0xff
- print(z,green)
- end
- else
- w=w+1
- red = 0xff - math.floor(w*2*256/total_colors)
- if red < 0 then
- red = 0
- end
- end
- bar_colors[z] = red*0x010000+green*0x000100
- z = z+1
- end
- bar_colors["total"]=z
- return bar_colors
- end
- API_changed = true
- function API.createScreen(name,half)
- screen[name]={}
- screen[name]["active"]=false
- screen[name]["button_list"]={}
- screen[name]["bar_list"]={}
- screen[name]["half"]=half
- screen[name]["side"]=nil
- screen[name]["xOffset"]=0
- screen[name]["yOffset"]=0
- screen[name]["width"]=nil
- screen[name]["height"]=nil
- screen[name]["changed"]=true
- end
- function API.createButton(scname,name,x,y,width,height,color,pressedcolor,text,toggle)
- screen[scname].button_list[name]={}
- screen[scname].button_list[name]["x"]=x
- screen[scname].button_list[name]["y"]=y
- screen[scname].button_list[name]["width"]=width
- screen[scname].button_list[name]["height"]=height
- screen[scname].button_list[name]["pressedcolor"]=colors[pressedcolor]
- screen[scname].button_list[name]["color"]=colors[color]
- screen[scname].button_list[name]["text"]=text
- screen[scname].button_list[name]["toggle"]=toggle
- screen[scname].button_list[name]["pressed"]=false
- screen[scname].button_list[name]["true_y"]=0
- screen[scname].button_list[name]["true_x"]=0
- screen[scname].button_list[name]["true_width"]=0
- screen[scname].button_list[name]["true_height"]=0
- screen[scname].button_list[name]["parent"]=scname
- screen[scname].button_list[name]["changed"]=true
- end
- function API.createBar(scname,x,y,total_width,perc,name,reverse)
- screen[scname].bar_list[name]={}
- screen[scname].bar_list[name]["x"]=x
- screen[scname].bar_list[name]["y"]=y
- screen[scname].bar_list[name]["width"]=total_width
- screen[scname].bar_list[name]["perc"]=perc
- screen[scname].bar_list[name]["reverse"]=reverse
- screen[scname].bar_list[name]["true_y"]=0
- screen[scname].bar_list[name]["true_x"]=0
- screen[scname].bar_list[name]["true_width"]=0
- screen[scname].bar_list[name]["parent"]=scname
- screen[scname].bar_list[name]["changed"]=true
- end
- function API.updateBar(name,perc,sc)
- screen[sc].bar_list[name]["perc"]=perc
- screen[sc].bar_list[name]["changed"]=true
- API_changed = true
- end
- function API.activateScreen(name,side)
- screen[name]["active"]=true
- if screen[name]["half"] then
- screen[name]["side"]=side
- if side == "right" then
- screen[name]["xOffset"]=width/2
- screen[name]["width"]=width/2
- screen[name]["height"]=height
- elseif side == "left" then
- screen[name]["width"]=width/2
- screen[name]["height"]=height
- elseif side == "bottom" then
- screen[name]["yOffset"]=height/2
- screen[name]["width"]=width
- screen[name]["height"]=height/2
- elseif side == "top" then
- screen[name]["width"]=width
- screen[name]["height"]=height/2
- end
- else
- screen[name]["width"]=width
- screen[name]["height"]=height
- end
- for bt,btdata in pairs(screen[name].button_list) do
- screen[name].button_list[bt]["true_y"]=math.floor(btdata["y"]*screen[name]["height"]+screen[name]["yOffset"])
- screen[name].button_list[bt]["true_x"]=math.floor(btdata["x"]*screen[name]["width"]+screen[name]["xOffset"])
- screen[name].button_list[bt]["true_width"]=math.floor(btdata["width"]*screen[name]["width"])
- screen[name].button_list[bt]["true_height"]=math.floor(btdata["height"]*screen[name]["height"])
- screen[name].button_list[bt]["changed"]=true
- end
- for bar,bardata in pairs(screen[name].bar_list) do
- screen[name].bar_list[bar]["true_y"]=math.floor(bardata["y"]*screen[name]["height"]+screen[name]["yOffset"])
- screen[name].bar_list[bar]["true_x"]=math.floor(bardata["x"]*screen[name]["width"]+screen[name]["xOffset"])
- screen[name].bar_list[bar]["true_width"]=math.floor(bardata["width"]*screen[name]["width"])
- screen[name].bar_list[bar]["changed"]=true
- end
- API_changed=true
- end
- function API.deactivateScreen(name)
- screen[name]["active"]=false
- gpu.fill(screen[name]["xOffset"],screen[name]["yOffset"],screen[name]["width"],screen[name]["height"]," ")
- screen[name]["xOffset"] = 0
- screen[name]["yOffset"] = 0
- screen[name]["side"]=nil
- API_changed=true
- end
- function API.clear()
- local screen = {}
- end
- function API.render()
- if API_changed == true then
- --gpu.fill(1,1,width,height,' ')
- for sc,scdata in pairs(screen) do
- if scdata["active"] then
- for name,data in pairs(screen[sc].button_list) do
- if data["changed"] then
- gpu.fill(data["true_x"],data["true_y"],data["true_width"],data["true_height"]," ")
- API.drawButton(data["true_x"],data['true_y'],data['true_width'],data['true_height'],data['color'],data['text'])
- screen[sc].button_list[name]["changed"]=false
- end
- end
- for name,data in pairs(screen[sc].bar_list) do
- if data["changed"] then
- gpu.fill(data["true_x"],data["true_y"],data["true_width"],2," ")
- API.drawBar(data["true_x"],data["true_y"],data["true_width"],name,data["perc"],data["reverse"])
- screen[sc].bar_list[name]["changed"]=false
- end
- end
- end
- end
- end
- API_changed = false
- end
- function API.drawButton(x,y,w,h,color,text)
- oldColor = gpu.getBackground()
- gpu.setBackground(color)
- gpu.fill(math.floor(x),math.floor(y),math.floor(w),math.floor(h)," ")
- gpu.set(x+math.floor(w/2)-math.floor(string.len(text)/2),y+math.floor(h/2)-1,text)
- gpu.setBackground(oldColor)
- end
- function API.drawBar(x,y,total_width,name,perc,reverse)
- oldColor = gpu.getBackground()
- oldFore = gpu.getForeground()
- str_width = string.len(name)
- bar_width = total_width-str_width-1
- bar_colors = getColors(bar_width)
- bar_color_step = math.floor(bar_width/bar_colors["total"])
- total_colors=bar_colors["total"]
- rest = bar_width%bar_colors["total"]
- if rest == bar_width then
- rest = 0
- end
- n=0
- gpu.setForeground(0)
- if reverse then
- gpu.setBackground(bar_colors[total_colors-n-1])
- else
- gpu.setBackground(bar_colors[n])
- end
- gpu.fill(x,y,rest,2,"|")
- while n <= bar_colors["total"]-1 do
- if reverse then
- gpu.setBackground(bar_colors[(total_colors-n-1)])
- else
- gpu.setBackground(bar_colors[n])
- end
- gpu.fill(x+rest+bar_color_step*n,y,bar_color_step,2,"|")
- n=n+1
- end
- pixel_per_perc = bar_width/100
- gpu.setBackground(oldColor)
- gpu.setForeground(oldFore)
- gpu.fill(x+perc*pixel_per_perc,y,math.ceil((100-perc)*pixel_per_perc),2," ")
- --print(x+total_width-str_width,name)
- gpu.set(x+total_width-str_width,y,name)
- gpu.set(x+total_width-1-string.len(tostring(perc)),y+1,tostring(perc).."%")
- end
- function API.press(name,data,sc)
- if data['toggle'] then
- txt = screen[sc].button_list[name]['text']
- clr1 = screen[sc].button_list[name]['color']
- txt2 = screen[sc].button_list[name]['toggle']
- clr2 = screen[sc].button_list[name]['pressedcolor']
- screen[sc].button_list[name]['text']=txt2
- screen[sc].button_list[name]['toggle']=txt
- screen[sc].button_list[name]['color']=clr2
- screen[sc].button_list[name]['pressedcolor']=clr1
- if data['pressed'] then
- computer.pushSignal("toggled_off",name)
- else
- computer.pushSignal("toggled_on",name)
- end
- screen[sc].button_list[name]["pressed"] = not screen[sc].button_list[name]["pressed"]
- else
- API.drawButton(data['true_x'],data['true_y'],data['true_width'],data['true_height'],data['pressedcolor'],data['text'])
- os.sleep(0.1)
- computer.pushSignal("clicked",name)
- end
- screen[sc].button_list[name]['changed']=true
- API_changed = true
- end
- function API.click(x,y)
- for sc, scdata in pairs(screen) do
- if scdata["active"] then
- for name, data in pairs(scdata.button_list) do
- if x>=data['true_x'] and x<=data['true_x']+data['true_width'] then
- if y>=data['true_y'] and y<=data['true_y']+data['true_height'] then
- API.press(name,data,sc)
- end
- end
- end
- for name, data in pairs(scdata.bar_list) do
- if x>=data['true_x'] and x <= data['true_x']+data['true_width'] then
- if y>=data['true_y'] and y <= data['true_y']+2 then
- computer.pushSignal("barClicked",name)
- end
- end
- end
- end
- end
- end
- return API
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement