Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tMenus = {
- ["Notifications"] = { x = 20, y = 3, w = 29, h = 13, label = "Notifications", type = "menu", active = false },
- ["Control Panel"] = { x = 20, y = 3, w = 29, h = 11, label = "Control Panel", type = "menu", active = false, slc = 0 },
- ["Window Creator"] = { x = 20, y = 3, w = 30, h = 14, label = "Window Creator", type = "menu", active = false }
- }
- local tButtons = {
- ["CP_Appearance"] = { x = 22, y = 5, w = 25, h = 3, label = "Appearance", type = "button" },
- ["CP_Users"] = { x = 22, y = 9, w = 25, h = 3, label = "Users", type = "button" },
- ["WC_Path"] = { x = 22, y = 5, w = 26, h = 1, label = "Path:", type = "button_input", data = nil },
- ["WC_Width"] = { x = 22, y = 7, w = 26, h = 1, label = "Width:", type = "button_input", data = nil },
- ["WC_Height"] = { x = 22, y = 9, w = 26, h = 1, label = "Height:", type = "button_input", data = nil },
- ["WC_Create"] = { x = 22, y = 12, w = 26, h = 3, label = "Create", type = "button" },
- ["MSG_Input"] = { x = 5, y = 13, w = 31, h = 4, label = "Input Box", type = "message_input", prompt = "", data = "", active = false }
- }
- local tNotifications = {
- [1] = { label = "", func = function() end, time = "", type = "notification", id = 1 },
- [2] = { label = "", func = function() end, time = "", type = "notification", id = 2 },
- [3] = { label = "", func = function() end, time = "", type = "notification", id = 3 },
- [4] = { label = "", func = function() end, time = "", type = "notification", id = 4 },
- [5] = { label = "", func = function() end, time = "", type = "notification", id = 5 },
- [6] = { label = "", func = function() end, time = "", type = "notification", id = 6 },
- [7] = { label = "", func = function() end, time = "", type = "notification", id = 7 },
- [8] = { label = "", func = function() end, time = "", type = "notification", id = 8 },
- [9] = { label = "", func = function() end, time = "", type = "notification", id = 9 },
- [10] = { label = "", func = function() end, time = "", type = "notification", id = 10 }
- }
- local tColourPalette = {
- ["white"] = 1,
- ["orange"] = 2,
- ["magenta"] = 4,
- ["light blue"] = 8,
- ["lightblue"] = 8,
- ["yellow"] = 16,
- ["lime"] = 32,
- ["pink"] = 64,
- ["gray"] = 128,
- ["light gray"] = 256,
- ["lightgray"] = 256,
- ["cyan"] = 512,
- ["purple"] = 1024,
- ["blue"] = 2048,
- ["brown"] = 4096,
- ["green"] = 8192,
- ["red"] = 16384,
- ["black"] = 32768
- }
- local tWindows = {}
- local tIcons = {}
- local tFolders = {}
- local tColors = {
- ["main"] = {},
- ["menu"] = {},
- ["button"] = {},
- ["taskbar"] = {},
- ["icon"] = {},
- ["folder"] = {},
- ["bar"] = {}
- }
- local tBackgroundEvents = {
- "alarm",
- "timer",
- "redstone",
- "rednet_message",
- "disk",
- "disk_eject",
- "peripheral",
- "peripheral_detach",
- "modem_message"
- }
- local tPrograms = fs.list("/rom/programs/")
- local fs_config = "/djos/dj.cfg"
- local fs_activeDir = "/desktop/"
- local bRunning = true
- local rUpdate = os.startTimer(30)
- local rTime = os.startTimer(1)
- local term_width, term_height = term.getSize()
- local original_term = term.current()
- local bMenu_Start = false
- local bMenu_File = false
- local bMenu_Logout = false
- local activeWindow = nil
- local rDragTime = 0
- local bDragging = false
- if not fs.exists("/desktop") then
- fs.makeDir("/desktop")
- end
- if not fs.exists("/djos") then
- fs.makeDir("/djos")
- end
- -- Load complete settings of computer
- local function loadConfig()
- if fs.exists(fs_config) then
- settings.load(fs_config)
- tColors["main"].bg = settings.get("dj_main.bg")
- tColors["main"].fg = settings.get("dj_main.fg")
- tColors["menu"].bg = settings.get("dj_menu.bg")
- tColors["menu"].fg = settings.get("dj_menu.fg")
- tColors["taskbar"].bg = settings.get("dj_taskbar.bg")
- tColors["taskbar"].fg = settings.get("dj_taskbar.fg")
- tColors["taskbar"].start = settings.get("dj_taskbar.start")
- tColors["taskbar"].other = settings.get("dj_taskbar.other")
- tColors["button"].bg = settings.get("dj_button.bg")
- tColors["button"].fg = settings.get("dj_button.fg")
- tColors["bar"].bg = settings.get("dj_bar.bg")
- tColors["bar"].fg = settings.get("dj_bar.fg")
- tColors["bar"].drag = settings.get("dj_bar.drag")
- tColors["bar"].close = settings.get("dj_bar.close")
- tColors["bar"].edit = settings.get("dj_bar.edit")
- tColors["folder"].bg = settings.get("dj_folder.bg")
- tColors["folder"].fg = settings.get("dj_folder.fg")
- tColors["icon"].bg = settings.get("dj_icon.bg")
- tColors["icon"].fg = settings.get("dj_icon.fg")
- else
- settings.set("dj_main.bg",colors.white)
- settings.set("dj_main.fg",colors.black)
- settings.set("dj_menu.bg",colors.blue)
- settings.set("dj_menu.fg",colors.white)
- settings.set("dj_taskbar.bg",colors.blue)
- settings.set("dj_taskbar.fg",colors.white)
- settings.set("dj_taskbar.start",colors.green)
- settings.set("dj_taskbar.other",colors.lightBlue)
- settings.set("dj_button.bg",colors.lightBlue)
- settings.set("dj_button.fg",colors.white)
- settings.set("dj_bar.bg",colors.gray)
- settings.set("dj_bar.fg",colors.white)
- settings.set("dj_bar.drag",colors.yellow)
- settings.set("dj_bar.close",colors.red)
- settings.set("dj_bar.edit",colors.green)
- settings.set("dj_folder.bg",colors.yellow)
- settings.set("dj_folder.fg",colors.lightGray)
- settings.set("dj_icon.bg",colors.orange)
- settings.set("dj_icon.fg",colors.lightGray)
- settings.save(fs_config)
- loadConfig()
- end
- end
- -- Load current directory config
- local function loadDirConfig()
- tIcons = {}
- tFolders = {}
- local dir_list = fs.list(fs_activeDir)
- local n = 0
- for i=1,#dir_list do
- local type = "icon"
- local tChoice = tIcons
- if fs.isDir(fs_activeDir..dir_list[i]) then
- tChoice = tFolders
- type = "folder"
- end
- if settings.get(fs_activeDir..dir_list[i]..".x") then
- table.insert(tChoice,fs_activeDir..dir_list[i])
- tChoice[fs_activeDir..dir_list[i]] = {}
- tChoice[fs_activeDir..dir_list[i]].x = settings.get(fs_activeDir..dir_list[i]..".x")
- tChoice[fs_activeDir..dir_list[i]].y = settings.get(fs_activeDir..dir_list[i]..".y")
- tChoice[fs_activeDir..dir_list[i]].w = settings.get(fs_activeDir..dir_list[i]..".w")
- tChoice[fs_activeDir..dir_list[i]].h = settings.get(fs_activeDir..dir_list[i]..".h")
- tChoice[fs_activeDir..dir_list[i]].type = settings.get(fs_activeDir..dir_list[i]..".type")
- tChoice[fs_activeDir..dir_list[i]].label = settings.get(fs_activeDir..dir_list[i]..".label")
- else
- settings.set(fs_activeDir..dir_list[i]..".x",4+(5*n))
- settings.set(fs_activeDir..dir_list[i]..".y",3)
- settings.set(fs_activeDir..dir_list[i]..".w",4)
- settings.set(fs_activeDir..dir_list[i]..".h",3)
- settings.set(fs_activeDir..dir_list[i]..".type",type)
- settings.set(fs_activeDir..dir_list[i]..".label",dir_list[i])
- settings.save(fs_config)
- loadDirConfig()
- end
- n = n + 1
- end
- end
- -- Save file/folder configs in current directory
- local function saveDirConfig()
- local dir_list = fs.list(fs_activeDir)
- for i=1,#dir_list do
- if fs.exists(fs_activeDir..dir_list[i]) and fs.isDir(fs_activeDir..dir_list[i]) then
- settings.set(fs_activeDir..dir_list[i]..".x",tFolders[fs_activeDir..dir_list[i]].x)
- settings.set(fs_activeDir..dir_list[i]..".y",tFolders[fs_activeDir..dir_list[i]].y)
- settings.set(fs_activeDir..dir_list[i]..".w",tFolders[fs_activeDir..dir_list[i]].w)
- settings.set(fs_activeDir..dir_list[i]..".h",tFolders[fs_activeDir..dir_list[i]].h)
- settings.set(fs_activeDir..dir_list[i]..".type",tFolders[fs_activeDir..dir_list[i]].type)
- settings.set(fs_activeDir..dir_list[i]..".label",tFolders[fs_activeDir..dir_list[i]].label)
- elseif fs.exists(fs_activeDir..dir_list[i]) then
- settings.set(fs_activeDir..dir_list[i]..".x",tIcons[fs_activeDir..dir_list[i]].x)
- settings.set(fs_activeDir..dir_list[i]..".y",tIcons[fs_activeDir..dir_list[i]].y)
- settings.set(fs_activeDir..dir_list[i]..".w",tIcons[fs_activeDir..dir_list[i]].w)
- settings.set(fs_activeDir..dir_list[i]..".h",tIcons[fs_activeDir..dir_list[i]].h)
- settings.set(fs_activeDir..dir_list[i]..".type",tIcons[fs_activeDir..dir_list[i]].type)
- settings.set(fs_activeDir..dir_list[i]..".label",tIcons[fs_activeDir..dir_list[i]].label)
- end
- end
- settings.save(fs_config)
- end
- -- Create a new window
- local function window_new(path,w,h)
- if not tWindows[path] then
- tWindows[path] = {}
- local startIndex = 0
- local bPossible = false
- for i=1,#path do
- if string.sub(path,i,i) == "/" then
- if bPossible == false then
- bPossible = true
- startIndex = i + 1
- else
- startIndex = i + 1
- end
- end
- end
- tWindows[path].x = 4
- tWindows[path].y = 8
- tWindows[path].w = w
- tWindows[path].h = h-1
- tWindows[path].func = function() shell.run(path) end
- tWindows[path].label = string.sub(path,startIndex,#path)
- tWindows[path].type = "window"
- tWindows[path].cor = coroutine.create(tWindows[path].func)
- tWindows[path].active = true
- tWindows[path].obj = window.create(original_term,tWindows[path].x,tWindows[path].y,tWindows[path].w,tWindows[path].h,true)
- return tWindows[path]
- end
- return false
- end
- function notification_new(label,func,time)
- local bChosen = 0
- for i=1,#tNotifications do
- if tNotifications[i].label == "" and bChosen == 0 then
- bChosen = i
- end
- end
- if bChosen == 0 then
- bChosen = 10
- for i=1,9 do
- tNotifications[i].label = tNotifications[i+1].label
- tNotifications[i].func = tNotifications[i+1].func
- tNotifications[i].time = tNotifications[i+1].time
- end
- end
- tNotifications[bChosen].label = label
- tNotifications[bChosen].func = func
- tNotifications[bChosen].time = time
- end
- -- Update Control Panel buttons to be on the right coords
- local function updateControlPanel()
- tButtons["CP_Appearance"].x = tMenus["Control Panel"].x + 2
- tButtons["CP_Appearance"].y = tMenus["Control Panel"].y + 2
- tButtons["CP_Users"].x = tMenus["Control Panel"].x + 2
- tButtons["CP_Users"].y = tMenus["Control Panel"].y + 6
- end
- -- Update Window Creator buttons to be on the right coords
- local function updateWindowCreator()
- tButtons["WC_Path"].x = tMenus["Window Creator"].x + 2
- tButtons["WC_Path"].y = tMenus["Window Creator"].y + 2
- tButtons["WC_Width"].x = tMenus["Window Creator"].x + 2
- tButtons["WC_Width"].y = tMenus["Window Creator"].y + 4
- tButtons["WC_Height"].x = tMenus["Window Creator"].x + 2
- tButtons["WC_Height"].y = tMenus["Window Creator"].y + 6
- tButtons["WC_Create"].x = tMenus["Window Creator"].x + 2
- tButtons["WC_Create"].y = tMenus["Window Creator"].y + 9
- end
- -- Update notifications, making sure they are in order
- local function updateNotifications()
- for i=1,#tNotifications do
- if i >= 2 then
- if tNotifications[i-1].label == "" then
- tNotifications[i-1].label = tNotifications[i].label
- tNotifications[i-1].func = tNotifications[i].func
- tNotifications[i-1].time = tNotifications[i].time
- if tNotifications[i+1] then
- tNotifications[i].label = tNotifications[i+1].label
- tNotifications[i].func = tNotifications[i+1].func
- tNotifications[i].time = tNotifications[i+1].time
- if tNotifications[i+2] then
- tNotifications[i+1].label = tNotifications[i+2].label
- tNotifications[i+1].func = tNotifications[i+2].func
- tNotifications[i+1].time = tNotifications[i+2].time
- if tNotifications[i+3] then
- tNotifications[i+2].label = tNotifications[i+3].label
- tNotifications[i+2].func = tNotifications[i+3].func
- tNotifications[i+2].time = tNotifications[i+3].time
- if tNotifications[i+4] then
- tNotifications[i+3].label = tNotifications[i+4].label
- tNotifications[i+3].func = tNotifications[i+4].func
- tNotifications[i+3].time = tNotifications[i+4].time
- if tNotifications[i+5] then
- tNotifications[i+4].label = tNotifications[i+5].label
- tNotifications[i+4].func = tNotifications[i+5].func
- tNotifications[i+4].time = tNotifications[i+5].time
- if tNotifications[i+6] then
- tNotifications[i+5].label = tNotifications[i+6].label
- tNotifications[i+5].func = tNotifications[i+6].func
- tNotifications[i+5].time = tNotifications[i+6].time
- if tNotifications[i+7] then
- tNotifications[i+6].label = tNotifications[i+7].label
- tNotifications[i+6].func = tNotifications[i+7].func
- tNotifications[i+6].time = tNotifications[i+7].time
- if tNotifications[i+8] then
- tNotifications[i+7].label = tNotifications[i+8].label
- tNotifications[i+7].func = tNotifications[i+8].func
- tNotifications[i+7].time = tNotifications[i+8].time
- if tNotifications[i+9] then
- tNotifications[i+8].label = tNotifications[i+9].label
- tNotifications[i+8].func = tNotifications[i+9].func
- tNotifications[i+8].time = tNotifications[i+9].time
- else
- tNotifications[i+8].label = ""
- tNotifications[i+8].func = function() end
- tNotifications[i+8].time = ""
- end
- else
- tNotifications[i+7].label = ""
- tNotifications[i+7].func = function() end
- tNotifications[i+7].time = ""
- end
- else
- tNotifications[i+6].label = ""
- tNotifications[i+6].func = function() end
- tNotifications[i+6].time = ""
- end
- else
- tNotifications[i+5].label = ""
- tNotifications[i+5].func = function() end
- tNotifications[i+5].time = ""
- end
- else
- tNotifications[i+4].label = ""
- tNotifications[i+4].func = function() end
- tNotifications[i+4].time = ""
- end
- else
- tNotifications[i+3].label = ""
- tNotifications[i+3].func = function() end
- tNotifications[i+3].time = ""
- end
- else
- tNotifications[i+2].label = ""
- tNotifications[i+2].func = function() end
- tNotifications[i+2].time = ""
- end
- else
- tNotifications[i+1].label = ""
- tNotifications[i+1].func = function() end
- tNotifications[i+1].time = ""
- end
- else
- tNotifications[i].label = ""
- tNotifications[i].func = function() end
- tNotifications[i].time = ""
- end
- end
- end
- end
- end
- -- Convert a colour's number to it's name
- local function convertNumToCol(num)
- for k,v in pairs(tColourPalette) do
- if v == num then
- return k
- end
- end
- end
- -- Draw a box with specific data
- local function drawBox(x,y,w,h,colour,bar)
- term.setBackgroundColor(colour)
- term.setCursorPos(x,y)
- for i=1,h do
- for i=1,w do
- write(" ")
- end
- term.setCursorPos(x,y+i)
- end
- term.setTextColor(tColors["bar"].bg)
- term.setCursorPos(x,y)
- if bar then
- for i=1,h do
- write(string.char(133))
- term.setCursorPos(x,y+i)
- end
- term.setCursorPos(x+w-1,y)
- for i=1,h do
- write(string.char(138))
- term.setCursorPos(x+w-1,y+i)
- end
- term.setCursorPos(x,y+h-1)
- for i=1,w do
- write(string.char(140))
- term.setCursorPos(x+i,y+h-1)
- end
- term.setCursorPos(x,y+h-1)
- write(string.char(141))
- term.setCursorPos(x+w-1,y+h-1)
- write(string.char(142))
- end
- end
- -- Draw status bar
- local function drawBar(x,y,w,bC,bD,bE,text)
- term.setBackgroundColor(tColors["bar"].bg)
- term.setCursorPos(x,y)
- for i=1,w do
- write(" ")
- end
- if bC then
- term.setCursorPos(x+w-1,y)
- term.setTextColor(tColors["bar"].close)
- print(string.char(7))
- end
- if bE then
- term.setCursorPos(x+w-2,y)
- term.setTextColor(tColors["bar"].edit)
- print(string.char(7))
- end
- if bD then
- term.setCursorPos(x,y)
- term.setTextColor(tColors["bar"].drag)
- print(string.char(7))
- end
- if text then
- term.setCursorPos(x+(w/2)-(#text/2),y)
- term.setTextColor(tColors["bar"].fg)
- print(text)
- end
- end
- -- Draw an object (object being a table of data)
- local function drawObject(obj)
- if obj.type == "notification" then
- if obj.label ~= "" then
- term.setBackgroundColor(tColors["menu"].bg)
- term.setTextColor(colors.white)
- if obj.id == 2 or obj.id == 4 or obj.id == 6 or obj.id == 8 or obj.id == 10 then
- term.setTextColor(colors.black)
- end
- term.setCursorPos(tMenus["Notifications"].x+1,tMenus["Notifications"].y+obj.id+1)
- print(obj.label)
- term.setCursorPos(tMenus["Notifications"].x+tMenus["Notifications"].w-2,tMenus["Notifications"].y+obj.id+1)
- term.setTextColor(tColors["bar"].close)
- print(string.char(7))
- term.setCursorPos(tMenus["Notifications"].x+tMenus["Notifications"].w-3,tMenus["Notifications"].y+obj.id+1)
- term.setTextColor(tColors["bar"].edit)
- print(string.char(7))
- end
- elseif obj.type == "folder" then
- drawBox(obj.x,obj.y,obj.w,obj.h,tColors["folder"].bg)
- drawBar(obj.x,obj.y,obj.w,true,true,true,"")
- term.setCursorPos(obj.x,obj.y+obj.h-1)
- term.setBackgroundColor(tColors["folder"].bg)
- term.setTextColor(tColors["folder"].fg)
- if #obj.label >= obj.w then
- print(string.sub(obj.label,1,obj.w-1)..".")
- else
- print(obj.label)
- end
- elseif obj.type == "icon" then
- drawBox(obj.x,obj.y,obj.w,obj.h,tColors["icon"].bg)
- drawBar(obj.x,obj.y,obj.w,true,true,true,"")
- term.setCursorPos(obj.x,obj.y+obj.h-1)
- term.setBackgroundColor(tColors["icon"].bg)
- term.setTextColor(tColors["icon"].fg)
- if #obj.label >= obj.w then
- print(string.sub(obj.label,1,obj.w-1)..".")
- else
- print(obj.label)
- end
- elseif obj.type == "button" then
- drawBox(obj.x,obj.y,obj.w,obj.h,tColors["button"].bg)
- term.setBackgroundColor(tColors["button"].bg)
- term.setTextColor(tColors["button"].fg)
- if obj.h == 3 then
- term.setCursorPos(obj.x+(obj.w/2)-(#obj.label/2),obj.y+1)
- else
- term.setCursorPos(obj.x+(obj.w/2)-(#obj.label/2),obj.y)
- end
- print(obj.label)
- elseif obj.type == "menu" then
- drawBox(obj.x,obj.y,obj.w,obj.h,tColors["menu"].bg,true)
- drawBar(obj.x,obj.y,obj.w,true,true,false,obj.label)
- elseif obj.type == "message_input" then
- drawBox(obj.x,obj.y,obj.w,obj.h,tColors["menu"].bg,true)
- drawBar(obj.x,obj.y,obj.w,true,true,false,obj.label)
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(obj.x+#obj.prompt+2,obj.y+2)
- for i=1,obj.w-(#obj.prompt+3) do
- write(" ")
- end
- term.setCursorPos(obj.x+#obj.prompt+2,obj.y+2)
- term.setTextColor(colors.black)
- write(obj.data)
- term.setBackgroundColor(tColors["menu"].bg)
- term.setTextColor(tColors["menu"].fg)
- term.setCursorPos(obj.x+1,obj.y+2)
- write(obj.prompt..":")
- elseif obj.type == "button_input" then
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(obj.x+#obj.label,obj.y)
- for i=1,obj.w-(#obj.label) do
- write(" ")
- end
- if obj.data then
- term.setTextColor(colors.black)
- term.setCursorPos(obj.x+#obj.label,obj.y)
- write(obj.data)
- end
- term.setCursorPos(obj.x,obj.y)
- term.setTextColor(tColors["menu"].fg)
- term.setBackgroundColor(tColors["menu"].bg)
- write(obj.label)
- elseif obj.type == "window" then
- drawBar(obj.x,obj.y-1,obj.w,true,true,true,obj.label)
- end
- end
- -- Draw folders/icons on desktop
- local function drawObjects()
- for k,v in pairs(tFolders) do
- drawObject(v)
- end
- for k,v in pairs(tIcons) do
- drawObject(v)
- end
- end
- -- Draw the time
- local function drawTime()
- term.setCursorPos((term_width - #textutils.formatTime(os.time(),false)) + 1,term_height)
- term.setBackgroundColor(tColors["taskbar"].bg)
- term.setTextColor(tColors["taskbar"].fg)
- write(textutils.formatTime(os.time(),false))
- end
- -- Draw the Control Panel
- local function drawMenu_ControlPanel()
- drawObject(tMenus["Control Panel"])
- if tMenus["Control Panel"].slc == 0 then
- drawObject(tButtons["CP_Appearance"])
- drawObject(tButtons["CP_Users"])
- elseif tMenus["Control Panel"].slc == 1 then
- term.setCursorPos((tMenus["Control Panel"].x+tMenus["Control Panel"].w-2),tMenus["Control Panel"].y)
- term.setBackgroundColor(tColors["bar"].bg)
- term.setTextColor(tColors["bar"].edit)
- print(string.char(17))
- elseif tMenus["Control Panel"].slc == 2 then
- term.setCursorPos((tMenus["Control Panel"].x+tMenus["Control Panel"].w-2),tMenus["Control Panel"].y)
- term.setBackgroundColor(tColors["bar"].bg)
- term.setTextColor(tColors["bar"].edit)
- print(string.char(17))
- end
- end
- -- Draw the Notifications Menu
- local function drawMenu_Notifications()
- drawObject(tMenus["Notifications"])
- for i=1,#tNotifications do
- drawObject(tNotifications[i])
- end
- end
- -- Draw the Window Creator menu
- local function drawMenu_WindowCreator()
- drawObject(tMenus["Window Creator"])
- drawObject(tButtons["WC_Path"])
- drawObject(tButtons["WC_Width"])
- drawObject(tButtons["WC_Height"])
- drawObject(tButtons["WC_Create"])
- end
- -- Draw the Start menu
- local function drawMenu_Start()
- term.setTextColor(tColors["taskbar"].fg)
- term.setBackgroundColor(tColors["taskbar"].start)
- term.setCursorPos(1,term_height-5)
- print(" ")
- term.setCursorPos(1,term_height-4)
- print(" CraftOS ")
- term.setCursorPos(1,term_height-3)
- print(" Control Panel ")
- term.setCursorPos(1,term_height-2)
- print(" Notifications ")
- term.setCursorPos(1,term_height-1)
- print(" ")
- end
- -- Draw the File menu
- local function drawMenu_File()
- term.setTextColor(tColors["taskbar"].fg)
- term.setBackgroundColor(tColors["taskbar"].other)
- term.setCursorPos(9,term_height-4)
- print(" ")
- term.setCursorPos(9,term_height-3)
- print(" New Window ")
- term.setCursorPos(9,term_height-2)
- print(" Set Directory ")
- term.setCursorPos(9,term_height-1)
- print(" ")
- end
- -- Draw the Logout menu
- local function drawMenu_Logout()
- term.setTextColor(tColors["taskbar"].fg)
- term.setBackgroundColor(tColors["taskbar"].other)
- term.setCursorPos(16,term_height-4)
- print(" ")
- term.setCursorPos(16,term_height-3)
- print(" Reboot ")
- term.setCursorPos(16,term_height-2)
- print(" Shutdown ")
- term.setCursorPos(16,term_height-1)
- print(" ")
- end
- -- Draw the desktop
- local function drawBackground()
- term.redirect(original_term)
- term.setBackgroundColor(tColors["main"].bg)
- term.clear()
- term.setCursorPos(1,term_height)
- term.setBackgroundColor(tColors["taskbar"].bg)
- term.clearLine()
- term.setBackgroundColor(tColors["taskbar"].start)
- term.setTextColor(tColors["taskbar"].fg)
- write(" Start ")
- term.setCursorPos(9,term_height)
- term.setBackgroundColor(tColors["taskbar"].other)
- write(" File ")
- term.setCursorPos(16,term_height)
- write(" Logout ")
- if activeWindow then
- term.setBackgroundColor(tColors["taskbar"].bg)
- term.setTextColor(tColors["taskbar"].fg)
- term.setCursorPos(25,term_height)
- write(activeWindow.label)
- end
- drawObjects()
- for k,v in pairs(tMenus) do
- if v.active then
- if v.label == "Control Panel" then
- drawMenu_ControlPanel()
- elseif v.label == "Notifications" then
- drawMenu_Notifications()
- elseif v.label == "Window Creator" then
- drawMenu_WindowCreator()
- else
- drawObject(v)
- end
- end
- end
- for k,v in pairs(tButtons) do
- if v.active then
- drawObject(v)
- end
- end
- for k,v in pairs(tWindows) do
- if v.active then
- v.obj.setVisible(true)
- v.obj.redraw()
- drawObject(v)
- end
- end
- if activeWindow then
- if activeWindow == tMenus["Control Panel"] then
- drawMenu_ControlPanel()
- elseif activeWindow == tMenus["Notifications"] then
- drawMenu_Notifications()
- elseif activeWindow == tMenus["Window Creator"] then
- drawMenu_WindowCreator()
- else
- if activeWindow.active then
- drawObject(activeWindow)
- end
- end
- end
- if bMenu_Start then
- drawMenu_Start()
- elseif bMenu_File then
- drawMenu_File()
- elseif bMenu_Logout then
- drawMenu_Logout()
- end
- drawTime()
- end
- -- Trigger Function for: Close
- local function trigger_Close(x,y,v)
- local n = 0
- if v.type == "window" then
- n = 1
- end
- if x == v.x + v.w - 1 and y == v.y-n then
- return true
- end
- return false
- end
- -- Trigger Function for: Edit
- local function trigger_Edit(x,y,v)
- local n = 0
- if v.type == "window" then
- n = 1
- end
- if x == v.x + v.w - 2 and y == v.y-n then
- return true
- end
- return false
- end
- -- Trigger Function for: Drag
- local function trigger_Drag(x,y,v)
- local n = 0
- if v.type == "window" then
- n = 1
- end
- if x == v.x and y == v.y-n then
- return true
- end
- return false
- end
- -- Trigger Function for: Start Menu
- local function trigger_Start(x,y)
- if x >= 1 and x <= 7 and y == term_height then
- return true
- end
- return false
- end
- -- Trigger Function for: File Menu
- local function trigger_File(x,y)
- if x >= 9 and x <= 14 and y == term_height then
- return true
- end
- return false
- end
- -- Trigger Function for: Logout Menu
- local function trigger_Logout(x,y)
- if x >= 16 and x <= 23 and y == term_height then
- return true
- end
- return false
- end
- -- Trigger Function: For Buttons
- local function trigger_Button(x,y,button)
- if x >= button.x and x <= button.x + button.w - 1 and y >= button.y and y <= button.y + button.h - 1 then
- return true
- end
- return false
- end
- -- Trigger Function: For all pop-up menus
- local function trigger_Menu(x,y,menuX,menuXW)
- if x >= menuX and x <= menuXW and y == term_height - 2 then
- return 1
- elseif x >= menuX and x <= menuXW and y == term_height - 3 then
- return 2
- elseif x >= menuX and x <= menuXW and y == term_height - 4 then
- return 3
- end
- end
- local function trigger_Notification(x,y,type)
- local n = 0
- if type == "run" then
- n = 1
- end
- if x <= tMenus["Notifications"].x + tMenus["Notifications"].w - (2 + n) then
- if y == tMenus["Notifications"].y + 2 then
- return 1
- elseif y == tMenus["Notifications"].y + 3 then
- return 2
- elseif y == tMenus["Notifications"].y + 4 then
- return 3
- elseif y == tMenus["Notifications"].y + 5 then
- return 4
- elseif y == tMenus["Notifications"].y + 6 then
- return 5
- elseif y == tMenus["Notifications"].y + 7 then
- return 6
- elseif y == tMenus["Notifications"].y + 8 then
- return 7
- elseif y == tMenus["Notifications"].y + 9 then
- return 8
- elseif y == tMenus["Notifications"].y + 10 then
- return 9
- elseif y == tMenus["Notifications"].y + 11 then
- return 10
- end
- end
- return false
- end
- -- Trigger Function: MSG Input
- local function trigger_Input(x,y)
- if x >= tButtons["MSG_Input"].x+#tButtons["MSG_Input"].prompt+2 and x <= (tButtons["MSG_Input"].x+#tButtons["MSG_Input"].prompt+2) + tButtons["MSG_Input"].w-(#tButtons["MSG_Input"].prompt+4) and y == tButtons["MSG_Input"].y + tButtons["MSG_Input"].h-2 then
- return true
- end
- return false
- end
- -- Trigger Function: Button Input
- local function trigger_InputB(x,y,obj)
- if x >= obj.x+#obj.label and x <= obj.x+#obj.label+(obj.w-#obj.label-1) and y == obj.y then
- return true
- end
- return false
- end
- -- Check if clicked inside the specified window
- local function clickedWithin(x,y,obj)
- if obj ~= nil then
- if obj.active then
- if x >= obj.x and x <= obj.x + obj.w - 1 and y >= obj.y and y <= obj.y + obj.h - 1 then
- return true
- end
- end
- end
- return false
- end
- -- Reassign the active window
- local function reassignActiveWindow(pref)
- if pref.active then
- activeWindow = pref
- else
- activeWindow = nil
- for k,v in pairs(tMenus) do
- if v.active then
- activeWindow = v
- end
- end
- if activeWindow == nil then
- for k,v in pairs(tWindows) do
- if v.active then
- activeWindow = v
- end
- end
- end
- end
- end
- -- Do the thing with the input message box
- local function activateMSGInput(i)
- if tButtons["MSG_Input"].label == "Set Directory" then
- if fs.exists(i) and string.sub(i,1,1) == "/" and string.sub(i,#i,#i) == "/" then
- saveDirConfig()
- fs_activeDir = i
- tButtons["MSG_Input"].data = i
- loadDirConfig()
- end
- end
- end
- notification_new("Test1",function() term.clear() print("Test1") sleep(1) end, textutils.formatTime(os.time(),false))
- notification_new("Test2",function() term.clear() print("Test2") sleep(1) end, textutils.formatTime(os.time(),false))
- notification_new("Test3",function() term.clear() print("Test3") sleep(1) end, textutils.formatTime(os.time(),false))
- loadConfig()
- loadDirConfig()
- drawBackground()
- while bRunning do
- local e = { os.pullEvent() }
- for k,v in pairs(tWindows) do
- if v.active and v == activeWindow then
- term.redirect(activeWindow.obj)
- if not bMenu_File and not bMenu_Start and not bMenu_Logout then
- activeWindow.obj.restoreCursor()
- activeWindow.obj.redraw()
- end
- coroutine.resume(activeWindow.cor,unpack(e))
- else
- local stat = coroutine.status(v.cor)
- if stat == "dead" then
- v.obj.setVisible(false)
- v.active = false
- v = nil
- else
- term.redirect(v.obj)
- local b = false
- for i=1,#tBackgroundEvents do
- if e[1] == tBackgroundEvents[i] then
- b = true
- end
- end
- if b == false then
- coroutine.resume(v.cor)
- else
- coroutine.resume(v.cor,unpack(e))
- end
- end
- end
- end
- term.redirect(original_term)
- for i=1,#tBackgroundEvents do
- if e[1] == tBackgroundEvents[i] and not e[1] == "timer" then
- notification_new(e[1], function() tButtons["MSG_Notification"].active = true end, textutils.formatTime(os.time(),false))
- break
- end
- end
- local button = e[2] or nil
- local X = e[3] or nil
- local Y = e[4] or nil
- if e[1] == "mouse_click" then
- _X = X
- _Y = Y
- if button == 1 then
- if trigger_Start(X,Y) then
- bMenu_Start = not bMenu_Start
- bMenu_File = false
- bMenu_Logout = false
- drawBackground()
- elseif trigger_File(X,Y) then
- bMenu_File = not bMenu_File
- bMenu_Start = false
- bMenu_Logout = false
- drawBackground()
- elseif trigger_Logout(X,Y) then
- bMenu_Logout = not bMenu_Logout
- bMenu_Start = false
- bMenu_File = false
- drawBackground()
- end
- if clickedWithin(X,Y,tMenus["Notifications"]) then
- if activeWindow == tMenus["Notifications"] then
- if trigger_Close(X,Y,tMenus["Notifications"]) then
- tMenus["Notifications"].active = false
- reassignActiveWindow(tMenus["Control Panel"])
- drawBackground()
- else
- local _nE = trigger_Notification(X,Y,"run")
- local _nC = trigger_Notification(X,Y,"close")
- if not _nE == false then
- tNotifications[_nE].func()
- drawBackground()
- elseif not _nC == false then
- tNotifications[_nC].label = ""
- tNotifications[_nC].func = function() end
- tNotifications[_nC].time = ""
- updateNotifications()
- drawBackground()
- end
- end
- else
- if not clickedWithin(X,Y,activeWindow) then
- reassignActiveWindow(tMenus["Notifications"])
- drawBackground()
- end
- end
- end
- if clickedWithin(X,Y,tMenus["Control Panel"]) then
- if activeWindow == tMenus["Control Panel"] then
- if trigger_Close(X,Y,tMenus["Control Panel"]) then
- tMenus["Control Panel"].active = false
- reassignActiveWindow(tMenus["Notifications"])
- drawBackground()
- else
- if tMenus["Control Panel"].slc == 0 then
- if trigger_Button(X,Y,tButtons["CP_Appearance"]) then
- tMenus["Control Panel"].slc = 1
- drawBackground()
- elseif trigger_Button(X,Y,tButtons["CP_Users"]) then
- tMenus["Control Panel"].slc = 2
- drawBackground()
- end
- else
- if trigger_Edit(X,Y,tMenus["Control Panel"]) then
- tMenus["Control Panel"].slc = 0
- drawBackground()
- end
- end
- if tMenus["Control Panel"].slc == 1 then
- elseif tMenus["Control Panel"].slc == 2 then
- end
- end
- else
- if not clickedWithin(X,Y,activeWindow) then
- reassignActiveWindow(tMenus["Control Panel"])
- drawBackground()
- end
- end
- end
- if clickedWithin(X,Y,tMenus["Window Creator"]) then
- if activeWindow == tMenus["Window Creator"] then
- if trigger_Close(X,Y,tMenus["Window Creator"]) then
- tMenus["Window Creator"].active = false
- reassignActiveWindow(tMenus["Control Panel"])
- drawBackground()
- else
- if trigger_InputB(X,Y,tButtons["WC_Path"]) then
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(tButtons["WC_Path"].x+#tButtons["WC_Path"].label,tButtons["WC_Path"].y)
- for i=1,tButtons["WC_Path"].w-(#tButtons["WC_Path"].label) do
- write(" ")
- end
- term.setTextColor(colors.black)
- term.setCursorPos(tButtons["WC_Path"].x+#tButtons["WC_Path"].label,tButtons["WC_Path"].y)
- local i = read()
- if fs.exists(i) and not fs.isDir(i) then
- tButtons["WC_Path"].data = i
- else
- local nEnd = 0
- for x=1,#i do
- if string.sub(i,x,x) == " " then
- nEnd = x
- break
- end
- end
- for x=1,#tPrograms do
- if string.sub(i,1,nEnd-1) == tPrograms[x] and not fs.isDir("/rom/programs/"..tPrograms[x]) then
- tButtons["WC_Path"].data = i
- end
- end
- end
- drawBackground()
- elseif trigger_InputB(X,Y,tButtons["WC_Width"]) then
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(tButtons["WC_Width"].x+#tButtons["WC_Width"].label,tButtons["WC_Width"].y)
- for i=1,tButtons["WC_Width"].w-(#tButtons["WC_Width"].label) do
- write(" ")
- end
- term.setTextColor(colors.black)
- term.setCursorPos(tButtons["WC_Width"].x+#tButtons["WC_Width"].label,tButtons["WC_Width"].y)
- local i = tonumber(read())
- if i then
- tButtons["WC_Width"].data = i
- end
- drawBackground()
- elseif trigger_InputB(X,Y,tButtons["WC_Height"]) then
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(tButtons["WC_Height"].x+#tButtons["WC_Height"].label,tButtons["WC_Height"].y)
- for i=1,tButtons["WC_Height"].w-(#tButtons["WC_Height"].label) do
- write(" ")
- end
- term.setTextColor(colors.black)
- term.setCursorPos(tButtons["WC_Height"].x+#tButtons["WC_Height"].label,tButtons["WC_Height"].y)
- local i = tonumber(read())
- if i then
- tButtons["WC_Height"].data = i
- end
- drawBackground()
- elseif trigger_Button(X,Y,tButtons["WC_Create"]) then
- if tButtons["WC_Height"] and tButtons["WC_Width"] and tButtons["WC_Path"] then
- local newWindow = window_new(tButtons["WC_Path"].data,tButtons["WC_Width"].data,tButtons["WC_Height"].data)
- reassignActiveWindow(newWindow)
- tMenus["Window Creator"].active = false
- drawBackground()
- end
- end
- end
- else
- if not clickedWithin(X,Y,activeWindow) then
- reassignActiveWindow(tMenus["Window Creator"])
- drawBackground()
- end
- end
- end
- if clickedWithin(X,Y,tButtons["MSG_Input"]) then
- if activeWindow == tButtons["MSG_Input"] then
- if trigger_Close(X,Y,tButtons["MSG_Input"]) then
- tButtons["MSG_Input"].active = false
- reassignActiveWindow(tMenus["Control Panel"])
- drawBackground()
- elseif trigger_Input(X,Y,tButtons["MSG_Input"]) then
- term.setBackgroundColor(colors.lightGray)
- term.setCursorPos(tButtons["MSG_Input"].x+#tButtons["MSG_Input"].prompt+2,tButtons["MSG_Input"].y+2)
- for i=1,tButtons["MSG_Input"].w-(#tButtons["MSG_Input"].prompt+3) do
- write(" ")
- end
- term.setCursorPos(tButtons["MSG_Input"].x+#tButtons["MSG_Input"].prompt+2,tButtons["MSG_Input"].y+2)
- term.setTextColor(colors.black)
- local inputData = read()
- activateMSGInput(inputData)
- drawBackground()
- end
- else
- if not clickedWithin(X,Y,activeWindow) then
- reassignActiveWindow(tButtons["MSG_Input"])
- drawBackground()
- end
- end
- end
- if bMenu_Start then
- -- 1 = Notifications, 2 = Control Panel, 3 = CraftOS
- local t = trigger_Menu(X,Y,1,15)
- if t == 1 then
- tMenus["Notifications"].active = true
- bMenu_Start = false
- reassignActiveWindow(tMenus["Notifications"])
- drawBackground()
- elseif t == 2 then
- tMenus["Control Panel"].active = true
- bMenu_Start = false
- reassignActiveWindow(tMenus["Control Panel"])
- drawBackground()
- elseif t == 3 then
- saveDirConfig()
- bRunning = false
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setTextColor(colors.yellow)
- term.setCursorPos(1,1)
- print(os.version())
- term.setCursorPos(1,2)
- term.setTextColor(colors.white)
- end
- end
- if bMenu_File then
- -- 1 = Set Directory, 2 = New Window
- local t = trigger_Menu(X,Y,9,23)
- if t == 1 then
- bMenu_File = false
- tButtons["MSG_Input"].active = true
- tButtons["MSG_Input"].label = "Set Directory"
- tButtons["MSG_Input"].prompt = "Path"
- reassignActiveWindow(tButtons["MSG_Input"])
- drawBackground()
- elseif t == 2 then
- bMenu_File = false
- tMenus["Window Creator"].active = true
- reassignActiveWindow(tMenus["Window Creator"])
- drawBackground()
- end
- end
- if bMenu_Logout then
- -- 1 = Shutdown, 2 = Reboot
- local t = trigger_Menu(X,Y,16,25)
- if t == 1 then
- os.shutdown()
- elseif t == 2 then
- os.reboot()
- end
- end
- for k,v in pairs(tWindows) do
- if activeWindow ~= v then
- if clickedWithin(X,Y,v) and not clickedWithin(X,Y,activeWindow) then
- reassignActiveWindow(v)
- drawBackground()
- break
- end
- else
- if trigger_Close(X,Y,v) then
- v.obj.setVisible(false)
- v.active = false
- v = nil
- reassignActiveWindow(tMenus["Control Panel"])
- drawBackground()
- end
- end
- end
- end
- elseif e[1] == "timer" then
- if button == rTime then
- drawTime()
- rTime = os.startTimer(3)
- elseif button == rUpdate then
- drawBackground()
- rUpdate = os.startTimer(30)
- elseif button == rDragTime then
- bDragging = false
- end
- elseif e[1] == "mouse_drag" then
- if button == 1 then
- if tMenus["Control Panel"].active and trigger_Drag(_X,_Y,tMenus["Control Panel"]) then
- bDragging = true
- rDragTime = os.startTimer(1)
- if X >= 1 and X <= (term_width - tMenus["Control Panel"].w + 1) and Y >= 1 and Y <= (term_height - tMenus["Control Panel"].h) then
- tMenus["Control Panel"].x = X
- tMenus["Control Panel"].y = Y
- _X = X
- _Y = Y
- updateControlPanel()
- drawBackground()
- end
- elseif tMenus["Notifications"].active and trigger_Drag(_X,_Y,tMenus["Notifications"]) then
- bDragging = true
- rDragTime = os.startTimer(1)
- if X >= 1 and X <= (term_width - tMenus["Notifications"].w + 1) and Y >= 1 and Y <= (term_height - tMenus["Notifications"].h) then
- tMenus["Notifications"].x = X
- tMenus["Notifications"].y = Y
- _X = X
- _Y = Y
- updateNotifications()
- drawBackground()
- end
- elseif tMenus["Window Creator"].active and trigger_Drag(_X,_Y,tMenus["Window Creator"]) then
- bDragging = true
- rDragTime = os.startTimer(1)
- if X >= 1 and X <= (term_width - tMenus["Window Creator"].w + 1) and Y >= 1 and Y <= (term_height - tMenus["Window Creator"].h) then
- tMenus["Window Creator"].x = X
- tMenus["Window Creator"].y = Y
- _X = X
- _Y = Y
- updateWindowCreator()
- drawBackground()
- end
- elseif tButtons["MSG_Input"].active and trigger_Drag(_X,_Y,tButtons["MSG_Input"]) then
- bDragging = true
- rDragTime = os.startTimer(1)
- if X >= 1 and X <= (term_width - tButtons["MSG_Input"].w + 1) and Y >= 1 and Y <= (term_height - tButtons["MSG_Input"].h) then
- tButtons["MSG_Input"].x = X
- tButtons["MSG_Input"].y = Y
- _X = X
- _Y = Y
- drawBackground()
- end
- else
- for k,v in pairs(tWindows) do
- if v.active and trigger_Drag(_X,_Y,v) then
- bDragging = true
- rDragTime = os.startTimer(1)
- if X >= 1 and X <= (term_width - v.w + 1) and Y >= 1 and Y <= (term_height - v.h) then
- v.x = X
- v.y = Y + 1
- _X = X
- _Y = Y
- v.obj.reposition(v.x,v.y,v.w,v.h)
- v.obj.redraw()
- drawBackground()
- end
- end
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement