Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Life Easier Commands --
- local w, h = term.getSize()
- local scp = term.setCursorPos
- local sbc = term.setBackgroundColor
- local stc = term.setTextColor
- local sp = textutils.slowPrint
- local cl = term.clear
- local needUpdate = false
- local iconContent = {}
- local clk = function(btext,x,y,color,time) sbc(color) scp(x,y) write(btext) sleep(time) end
- shell.setDir("")
- function checkUpdate()
- local res = http.get("http://www.pastebin.com/raw.php?i=mZi6TjgU")
- if res then
- con = res.readAll()
- res.close()
- f = fs.open(shell.getRunningProgram(),"r")
- compare = f.readAll()
- f.close()
- if(con ~= compare)then
- needUpdate = true
- else
- needUpdate = false
- end
- end
- end
- function UncodeColor(code)
- local code_colors = {
- ["0"] = colors.white,
- ["1"] = colors.orange,
- ["2"] = colors.magenta,
- ["3"] = colors.lightBlue,
- ["4"] = colors.yellow,
- ["5"] = colors.lime,
- ["6"] = colors.pink,
- ["7"] = colors.gray,
- ["8"] = colors.lightGray,
- ["9"] = colors.cyan,
- ["a"] = colors.purple,
- ["b"] = colors.blue,
- ["c"] = colors.brown,
- ["d"] = colors.green,
- ["e"] = colors.red,
- ["f"] = colors.black,
- }
- for k, v in pairs(code_colors) do
- if(code == k)then return v end
- end
- end
- function drawIcon(file,x,y)
- x = x - 1
- y = y - 1
- file = "tut/lol"
- -- get file and content from a 5x5 pixel area --
- f = fs.open(file,"r")
- for i = 1, 5 do
- table.insert(iconContent,f.readLine():sub(1,5))
- end
- f.close()
- -- now to draw the contents grabed from the icon
- for i = 1, #iconContent do
- -- how do i set the color?
- for cf = 1, 5 do
- scp(x+cf,y+i)
- sbc(UncodeColor(iconContent[i]:sub(cf,cf)))
- write(" ")
- end
- end
- end
- function fadeScr()
- local fadecolors = {colors.black,colors.gray,colors.lightGray,colors.white}
- for i = 1, #fadecolors do
- term.setBackgroundColor(fadecolors[i])
- term.clear()
- sleep(rate)
- end
- end
- function fadeout_complete(rate,text)
- local fadecolors = {colors.white,colors.lightGray,colors.gray,colors.black}
- local txtfcolors = {colors.black,colors.gray,colors.lightGray,colors.black}
- for i = 1, #fadecolors do
- term.setBackgroundColor(fadecolors[i])
- term.clear()
- term.setTextColor(txtfcolors[i])
- center_complete(text)
- sleep(rate)
- end
- end
- function center_x(text,y)
- term.setCursorPos((w/2) - (#text/2), y)
- print(text)
- end
- function center_y(text,x)
- term.setCursorPos(x, (h/2))
- print(text)
- end
- function center_complete(text,optional_yoffset)
- if(not optional_yoffset)then term.setCursorPos((w/2) - (#text/2), (h/2) ) end
- if(optional_yoffset)then term.setCursorPos((w/2) - (#text/2), (h/2)+optional_yoffset) end
- write(text)
- end
- -----------------------------
- -- Program Enviorment
- local programs = {
- prgs = {},
- progpath = ".RedOS/docs/",
- selected=false,
- inMenu = false,
- menutype = '',
- selID = 1,
- shortAddMenu = {
- {g=" "};
- {g=" Add Shortcut? "};
- {g=" "};
- {g=" "};
- {g=" [Accept] - [Cancel] "};
- },
- remMenu = {
- {g=" "};
- {g=" Delete Program? "};
- {g=" "};
- {g=" "};
- {g=" [Accept] - [Cancel] "};
- },
- shortRemMenu = {
- {g=" "};
- {g=" Remove Shortcut? "};
- {g=" "};
- {g=" "};
- {g=" [Accept] - [Cancel] "};
- },
- draw_short = function(self, state)
- if(state)then shm = self.shortRemMenu else shm = self.shortAddMenu end
- scp(1,(h/2) - (#shm)/2)
- for i = 1, #shm do
- sbc(colors.orange)
- stc(colors.white)
- scp(5,((h/2) - ((#shm)/2))+i)
- print(shm[i].g)
- sleep(0.01)
- end
- end,
- draw_del = function(self)
- shm = self.remMenu
- scp(1,(h/2) - (#shm)/2)
- for i = 1, #shm do
- sbc(colors.orange)
- stc(colors.white)
- scp(5,((h/2) - ((#shm)/2))+i)
- print(shm[i].g)
- sleep(0.01)
- end
- end,
- checkShort = function(self,name)
- return fs.isDir(".RedOS/desktop/"..name)
- end,
- deleteShort = function(self,name)
- fs.delete(".RedOS/desktop/"..name)
- end,
- makeShortcut = function(self,name,mkLoc)
- fs.makeDir(".RedOS/desktop/"..name)
- fs.copy(mkLoc, ".RedOS/desktop/"..name.."/"..name)
- shell.run("paint",".RedOS/desktop/"..name.."/icon")
- end,
- getprogs = function(self)
- self.prgs = {}
- for k, v in pairs(fs.list(self.progpath)) do
- self.prgs[k] = {sel=false,file=v,off=k,dir = fs.isDir(self.progpath..v)}
- end
- end,
- clear = function(self)
- sbc(colors.white)
- cl()
- sbc(colors.red)
- scp(1,1)
- term.clearLine()
- scp(1,1)
- stc(colors.white)
- write("Programs")
- sbc(colors.orange)
- scp(w,1)
- write("X")
- scp((w)-9,2)
- sbc(colors.red)
- stc(colors.white)
- self:getprogs()
- for i = 2, h do
- scp(w-9,i)
- write(string.rep(" ",10))
- end
- end,
- draw = function(self)
- self:clear()
- end,
- update = function(self)
- sbc(colors.orange)
- stc(colors.white)
- center_x("Downloaded Programs",3)
- stc(colors.black)
- sbc(colors.red)
- scp(w-8,3)
- write("Shortcut")
- scp(w-8,4)
- write("Delete")
- sbc(colors.white)
- -- print out programs--
- -- draw programs --
- for i = 1, #self.prgs do
- if(not self.prgs[i].dir)then
- scp(1,i+3)
- stc(colors.black)
- if(self.prgs[i].sel)then sbc(colors.lightBlue) else sbc(colors.white) end
- if(self:checkShort(self.prgs[i].file))then stc(colors.green) else stc(colors.black) end
- print(self.prgs[i].file)
- end
- end
- a = {os.pullEvent()}
- if( (a[1] == "mouse_drag" or a[1] == "mouse_click") and a[2] == 1)then
- if(a[3] >= w and a[4] == 1)then
- clk("X",w,1,colors.red,0.2)
- self.selected=false
- setState('desktop')
- end
- scp(1,2)
- stc(colors.black)
- --print(a[3].." | "..a[4])
- -- Button Clicks --
- if(a[3] >= w-10 and a[4] == 4 and self.selected)then -- Remove File
- self:draw_del()
- self.inMenu = true
- menutype = 'remove'
- end
- if(a[3] >= w-10 and a[4] == 3 and self.selected)then -- Manage Shortcut File
- if(self:checkShort(self.prgs[self.selID].file))then
- self:draw_short(true)
- self.inMenu = true
- menutype = 'shortcut-'
- else
- self:draw_short(false)
- self.inMenu = true
- menutype = 'shortcut+'
- end
- end
- -- cancel button --
- if(a[3] >= 29 and a[3] <= 36 and a[4] == 12 and self.inMenu == true)then
- self.selected=false
- self.inMenu=false
- self:clear()
- end
- -- accept button --
- if(a[3] >= 16 and a[3] <= 23 and a[4] == 12 and self.inMenu == true)then
- if(menutype == 'remove')then
- fs.delete(self.progpath..self.prgs[self.selID].file)
- if(self:checkShort(self.prgs[self.selID].file))then self:deleteShort(self.prgs[self.selID].file) end
- self:clear()
- self:update()
- self.inMenu = false
- self.selected=false
- elseif(menutype=='shortcut+')then
- self:makeShortcut(self.prgs[self.selID].file,self.progpath..self.prgs[self.selID].file)
- self.inMenu = false
- self.selected=false
- setState('desktop')
- elseif(menutype=='shortcut-')then
- self:deleteShort(self.prgs[self.selID].file)
- self:clear()
- self:update()
- self.inMenu = false
- self.selected=false
- end
- end
- -- program selection / management --
- for i = 1, #self.prgs do
- if(not self.prgs[i].dir)then
- if(a[3] >= 1 and a[3] <= #self.prgs[i].file and a[4] == i+3)then
- if(self.selected)then
- if(self.prgs[i].sel)then self.prgs[i].sel=false self.selected=false end
- else
- self.prgs[i].sel = true
- self.selected=true
- self.selID = i
- end
- self:update()
- end
- end
- end
- end
- end,
- }
- -- Downloader Enviorment
- local downloader = {
- downloading=false,
- dwnFiles = {
- {text="Kosy Mail",url="Tsjq5eVa",locdir = ".RedOS/docs/cozymail",down=false,install=true,has=false};
- {text="Kosy Chat",url="cj8SKTMj", locdir = ".RedOS/docs/cozychat",down=false,install=false,has=true};
- },
- draw = function(self)
- sbc(colors.white)
- cl()
- sbc(colors.red)
- scp(1,1)
- term.clearLine()
- scp(1,1)
- stc(colors.white)
- write("Downloader")
- sbc(colors.orange)
- scp(w,1)
- write("X")
- end,
- download = function(self)
- if(not http)then setState('desktop') end
- self.downloading=true
- sleep(1)
- for i = 1, #self.dwnFiles do
- if(self.dwnFiles[i].down)then
- local res = http.get("http://www.pastebin.com/raw.php?i="..textutils.urlEncode(self.dwnFiles[i].url) )
- if res then
- con = res.readAll()
- res.close()
- else
- changeState('desktop')
- end
- if(self.dwnFiles[i].install)then
- f = fs.open(".temp","w")
- f.write(con)
- f.close()
- shell.run(".temp")
- fs.delete(".temp")
- self.dwnFiles[i].down = false
- self.downloading=false
- else
- f = fs.open(self.dwnFiles[i].locdir,"w")
- f.write(con)
- f.close()
- self.dwnFiles[i].down = false
- self.downloading=false
- end
- end
- end
- end,
- update = function(self)
- if(self.downloading)then sbc(colors.red) else sbc(colors.green) end
- stc(colors.white)
- scp(1,h-1)
- write(string.rep(" ",w))
- center_x("Download",h-1)
- sbc(colors.orange)
- stc(colors.white)
- center_x("Available RedOS Software",3)
- sbc(colors.white)
- stc(colors.black)
- -- print out downloadablez --
- for i = 1, #self.dwnFiles do
- if(fs.exists(self.dwnFiles[i].locdir))then self.dwnFiles[i].has = true else self.dwnFiles[i].has = false end
- if(not self.dwnFiles[i].has)then stc(colors.black) else stc(colors.red) end
- if(self.dwnFiles[i].down)then sbc(colors.lightBlue) else sbc(colors.white) end
- scp(1,i+4)
- write(self.dwnFiles[i].text)
- scp(30,i+4)
- stc(colors.lightGray)
- sbc(colors.white)
- write(self.dwnFiles[i].url)
- end
- a = {os.pullEvent()}
- if( (a[1] == "mouse_drag" or a[1] == "mouse_click") and a[2] == 1)then
- if(a[3] >= w and a[4] == 1)then
- clk("X",w,1,colors.red,0.2)
- setState('desktop')
- end
- --Download--
- if(a[4] == 18)then
- self:download()
- end
- for i = 1, #self.dwnFiles do
- if(a[3] >= 1 and a[3] <= #self.dwnFiles[i].text
- and a[4] == i+4 and not self.dwnFiles[i].has)then
- if(not self.dwnFiles[i].down)then self.dwnFiles[i].down = true
- else self.dwnFiles[i].down = false end
- end
- end
- end
- end,
- clear = function(self)
- sbc(colors.white)
- cl()
- sbc(colors.red)
- scp(1,1)
- term.clearLine()
- scp(1,1)
- stc(colors.white)
- write("Downloader")
- sbc(colors.orange)
- scp(w,1)
- write("X")
- end,
- }
- -- Desktop Enviorment
- local desktop = {
- sMenuWidth = 10,
- inMenu = false,
- deskLoc = ".RedOS/desktop/",
- deskProgs = {};
- ficons = {},
- icon_width = 4,
- icon_height = 5,
- needUpdateInstance=false,
- get_desk = function(self)
- self.deskProgs = {}
- for k, v in pairs(fs.list(self.deskLoc)) do
- if(fs.isDir(self.deskLoc..v))then
- self.deskProgs[k] = {name=v,file=self.deskLoc..v.."/"..v,icon=self.deskLoc..v.."/icon"}
- end
- end
- end,
- loadBack = function(self)
- if(fs.exists(".RedOS/graphics/background"))then
- paintutils.drawImage(paintutils.loadImage(".RedOS/graphics/background"), 2, 1)
- end
- end,
- draw_desk = function(self)
- self.ficons = {}
- local c_tab = 0
- local sp_count = 0
- local mx_tab = 2
- local x_degree = 10
- local y_degree = 7
- for i = 1, #self.deskProgs do
- -- get current files icon
- if(c_tab == 2)then sp_count = sp_count + 1 c_tab = 0 end
- c_tab = c_tab + 1
- self.ficons[i] = {icon = self.deskProgs[i].icon, x = 2, y = i+((i-1)*y_degree)}
- local fileico = paintutils.loadImage(self.ficons[i].icon)
- local calc_x = sp_count*x_degree
- local calc_y = (c_tab-1)*y_degree
- self.ficons[i].x = self.ficons[i].x + calc_x
- self.ficons[i].y = calc_y + 2
- paintutils.drawImage(fileico, self.ficons[i].x, self.ficons[i].y)
- scp( self.ficons[i].x - 1, 1 + self.ficons[i].y + 3 )
- sbc(colors.white)
- stc(colors.black)
- print(self.deskProgs[i].name)
- end
- end,
- draw_avaupdate = function(self)
- scp(1,(h/2) - (#self.updateMenu)/2)
- for i = 1, #self.updateMenu do
- sbc(colors.red)
- stc(colors.white)
- scp(5,((h/2) - ((#self.updateMenu)/2))+i)
- print(self.updateMenu[i].g)
- sleep(0.01)
- end
- end,
- reboot = function(self)
- sbc(colors.white)
- cl()
- stc(colors.black)
- print()
- fadeout_complete(0.2,"Rebooting...")
- sleep(0.4)
- os.reboot()
- end,
- shutdown = function(self)
- sbc(colors.white)
- cl()
- stc(colors.black)
- print()
- fadeout_complete(0.2,"Shutting Down...")
- sleep(0.4)
- os.shutdown()
- end,
- startMenu = {
- {text=" ",run = function() end};
- {text=" Settings> ",run = function(self) self.inMenu = false shell.run("paint", ".RedOS/graphics/background") self:draw() end};
- {text=" Download> ",run = function() changeState('downloader') end};
- {text=" Explorer> ",run = function(self) shell.run(".RedOS/startmenu/apps/cozyfile") self.inMenu = false self:draw() end};
- {text=" Programs> ",run = function(self) changeState('programs') end};
- {text=" Shell ",run = function() sbc(colors.black) stc(colors.white) scp(1,1) cl() shell.run("shell") end};
- {text=" Reboot ",run = function(self) self:reboot() end};
- {text=" Shutdown ",run = function(self) self:shutdown() end};
- {text=" Update ",run = function(self) checkUpdate() self.inMenu =false self:draw() end};
- {text=" ",run = function() end};
- },
- updateMenu = {
- {g=" "};
- {g=" Update Available "};
- {g=" "};
- {g=" "};
- {g=" [Download] - [Not Now] "};
- },
- clear = function(self)
- fadeScr(0.08)
- self:loadBack()
- sbc(colors.red)
- scp(1,19)
- sleep(0.1)
- write(string.rep(" ",w))
- stc(colors.white)
- scp(1,19)
- checkUpdate()
- self.needUpdateInstance = needUpdate
- write("RedOS")
- if(not self.needUpdateInstance)then
- write(" Up to Date!")
- else
- write(" Needs Update!")
- end
- sbc(colors.black)
- self:get_desk()
- self:draw_desk()
- end,
- draw = function(self)
- sbc(colors.white)
- cl()
- self:loadBack()
- sbc(colors.red)
- scp(1,19)
- write(string.rep(" ",w))
- stc(colors.white)
- scp(1,19)
- write("RedOS")
- if(not self.needUpdateInstance)then
- write(" Up to Date!")
- else
- write(" Needs Update!")
- end
- sbc(colors.black)
- self:get_desk()
- end,
- update = function(self)
- --Events--
- self:draw_desk()
- --- Check if a update is needed --
- if(needUpdate)then
- self:draw_avaupdate()
- end
- if(self.inMenu)then
- -- Drawing Da Menu --
- for i =1, #self.startMenu do
- sbc(colors.gray)
- stc(colors.white)
- scp(1,18-#self.startMenu+i)
- write(self.startMenu[i].text)
- end
- end
- e = {os.pullEvent()}
- if(e[1] == "mouse_click" and not self.inMenu)then
- -- desktop item clicks brah --
- for i = 1, #self.deskProgs do
- -- Click detection --
- if(e[3] >= self.ficons[i].x and
- e[3] <= self.icon_width+self.ficons[i].x and
- e[4] >= self.ficons[i].y and
- e[4] <= self.icon_height+self.ficons[i].y ) then
- -- run program --
- sbc(colors.black)
- stc(colors.white)
- term.clear()
- if(e[2] == 1)then
- os.run({},self.deskProgs[i].file)
- center_complete("Press Any Key To Return to RedOS")
- center_complete("Program Ran: "..self.deskProgs[i].file,1)
- os.pullEvent("key")
- elseif(e[2] == 2)then
- shell.run("paint ",self.deskProgs[i].icon)
- end
- self:draw()
- end
- end
- end
- if(e[1] == "mouse_click" and e[2] == 1)then
- -- Start Menu Clicked --
- -- e[2] == mouse_button e[3] == mouse_x, e[4] == mouse_y,
- --scp(1,1)
- --print("X: "..e[3].."Y: "..e[4])
- if(needUpdate)then
- if(e[3] >= 14 and e[3] <= 23 and e[4] == 12)then
- -- download update --
- sbc(colors.white)
- cl()
- stc(colors.black)
- center_x("Downloading Update....",2)
- scp(1,4)
- stc(colors.lightGray)
- print("Pinging RedOS File...")
- local redRes = http.get("http://www.pastebin.com/raw.php?i=mZi6TjgU")
- if(redRes)then
- local con = redRes.readAll()
- redRes.close()
- stc(colors.green)
- print("Success!.")
- stc(colors.gray)
- print("Replaceing File...")
- fs.delete(shell.getRunningProgram())
- f = fs.open(shell.getRunningProgram(),"w")
- f.write(con)
- stc(colors.black)
- center_x("Press Any Key To Reboot",7)
- os.pullEvent("key")
- os.reboot()
- else
- stc(colors.red)
- print("Failed.")
- stc(colors.gray)
- print("Please check your connection to the internet")
- stc(colors.black)
- center_x("Press Any Key",7)
- os.pullEvent("key")
- self:draw()
- end
- end
- if(e[3] >= 29 and e[3] <= 37 and e[4] == 12)then
- -- not now
- needUpdate=false
- self:draw()
- end
- end
- if(e[3] >= 1 and e[3] <= 5 and e[4] == 19)then
- if(not self.inMenu) then self.inMenu = true;
- elseif(self.inMenu) then self.inMenu=false end
- sbc(colors.gray)
- scp(1,h)
- write("RedOS")
- sleep(0.1)
- self:draw()
- self:update()
- end
- -- Menu Button Clicks --
- for i = 1, #self.startMenu do
- if(e[3] >= 1 and e[3] <= self.sMenuWidth and e[4] >= h-#self.startMenu and e[4] <= h and self.inMenu)then
- if(self.inMenu and e[4] == i+((h-1) - #self.startMenu))then self.startMenu[i].run(self) end
- elseif(self.inMenu)then self.inMenu = false self:draw() end
- end
- end
- end,
- }
- --- Without transition --
- function setState(s)
- if(s == 'desktop')then desktop:draw() state = 'desktop' end
- if(s == 'downloader')then downloader:draw() state = 'downloader' end
- if(s == 'programs')then programs:draw() state = 'programs' end
- ros()
- end
- -- With Transition --
- function changeState(s)
- if(s == 'desktop')then desktop:clear() state = 'desktop' end
- if(s == 'downloader')then downloader:clear() state = 'downloader' end
- if(s == 'programs')then programs:clear() state = 'programs' end
- ros()
- end
- function ros()
- while true do
- sleep(0.001)
- if(state == 'desktop')then desktop:update() end
- if(state == 'downloader')then downloader:update() end
- if(state == 'programs')then programs:update() end
- end
- end
- changeState('desktop')
- ros()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement