Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Windowscraft --
- --[[
- This is free and unencumbered software released into the public domain.
- Anyone is free to copy, modify, publish, use, compile, sell, or
- distribute this software, either in source code form or as a compiled
- binary, for any purpose, commercial or non-commercial, and by any
- means.
- In jurisdictions that recognize copyright laws, the author or authors
- of this software dedicate any and all copyright interest in the
- software to the public domain. We make this dedication for the benefit
- of the public at large and to the detriment of our heirs and
- successors. We intend this dedication to be an overt act of
- relinquishment in perpetuity of all present and future rights to this
- software under copyright law.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
- IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
- OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
- ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- OTHER DEALINGS IN THE SOFTWARE.
- For more information, please refer to <http://unlicense.org/>
- ]]
- local function error(e) -- put this before everything because load order issues
- term.setBackgroundColor(colors.blue) term.setTextColor(colors.white) term.setCursorPos(1,1) term.clear()
- print("Windowscraft has crashed!")
- print("")
- print("Error Report:") term.setTextColor(colors.lime)
- print(e)
- print("")
- sleep(1) setText("white")
- print("Press any key to reboot...")
- os.pullEvent("key")
- os.reboot()
- end
- -- ENGINE V1 *** MAIN ENGINE *** *** MAIN ENGINE *** *** MAIN ENGINE *** ENGINE V1 --
- -- FUNCTIONS --
- local w,h = term.getSize()
- function valid(value,expected) if type(value) == expected then return true else return false end end
- function verify(value, expected) return valid(value,expected) end
- function clr() term.clear() end
- function cp(x,y) if not valid(x,"number") then return error("Expected x to be a number, got "..type(x)) end if not valid(y,"number") then return error("Expected y to be a number, got "..type(y)) end term.setCursorPos(x,y) end
- function setText(col) if not valid(col,"string") then return error("Expected col to be a string, got "..type(x)) else return term.setTextColor(colors[col]) end end
- function setBack(col) if not valid(col,"string") then return error("Expected col to be a string, got "..type(x)) else return term.setBackgroundColor(colors[col]) end end
- function notnil(vari) if vari == nil then return false else return true end end
- function lt(tab) if not valid(tab,"table") then return error("Expected tab to be a table, got "..type(tab)) end local lt = 0 for i=1, #tab do if string.len(tab[i]) > lt then lt = string.len(tab[i]) end end return lt end
- function pullEvents(...) -- for pulling multiple events at a time, first event get priority
- local args = {...}
- --if #args == 0 then return os.pullEvent() end
- while true do
- sleep(.000000001)
- event, arg1, arg2, arg3, arg4 = os.pullEvent()
- for i=1, #args do
- if args[i] == event then
- return event, arg1, arg2, arg3, arg4
- end
- end
- end
- end
- function getTableSize(image) -- Returns width and length of a table
- if not valid(image,"table") then return error("Expected table for image, got "..type(image)) end
- local lng = 0
- for i=1, #image do
- if string.len(image[i]) > lng then
- lng = string.len(image[i])
- end
- end
- return lng, #image
- end
- -- FUNCTIONS --
- -- COLLISION --
- col = {}
- function createCol(name,x,y,x2,y2,status)
- name = name or "name_"..#col table.insert(col,{}) status = status or true
- col[#col].x, col[#col].y, col[#col].x2, col[#col].y2, col[#col].name, col[#col].status = x, y, x2, y2, name, status
- end
- function colExists(name)
- for i=1, #col do
- if col[i].name == name then return i end
- end
- return false
- end
- function removeCol(name)
- if colExists(name) ~= false then table.remove(col, colExists(name)) end
- end
- function moveCol(name,newx,newy)
- if not colExists(name) then return error("Collision does not exists") end
- name = colExists(name)
- col[name].x2,col[name].y2 = col[name].x2+newx-col[name].x, col[name].y2+newy-col[name].y
- col[name].x,col[name].y = newx,newy
- end
- function drawCol(name, color)
- if not notnil(color) then if col[name].status then color = colors.green else color = colors.red end end
- for i=1, #col do
- if name == col[i].name then
- return paintutils.drawFilledBox(col[i].x,col[i].y,col[i].x2,col[i].y2,color)
- end
- end
- return false
- end
- function checkCol(x,y)
- for i=1, #col do
- if x >= col[i].x and x <= col[i].x2 and y >= col[i].y and y <= col[i].y2 then
- return col[i].name
- end
- end
- return false
- end
- -- COLLISION --
- -- COLOR RELATED --
- function renderImage(image,x,y) -- used to display custom images fast and easy
- if not valid(image,"table") then return error("Expected table for image, got "..type(image)) end
- if not valid(x,"number") then return error("Expected number for x, got "..type(x)) end
- if not valid(y,"number") then return error("Expected number for y, got "..type(y)) end
- cp(x,y)
- for i=1, #image do
- term.blit(string.rep(" ", #image[i]),string.rep("0", #image[i]),image[i])
- cp(x,y+i)
- end
- end
- function loadImage(image)
- if not valid(image,"string") then return error("Expected string for image, got "..type(image)) end
- if not fs.exists(image) then return false end
- local img = {}
- local file = fs.open(image, "r")
- local tmp = ""
- repeat
- tmp = file.readLine()
- if notnil(tmp) then
- img[#img+1] = tmp
- end
- until not notnil(tmp)
- file.close()
- return img
- end
- -- COLOR RELATED --
- -- RCM --
- local rcm_data = {}
- rcm_data.menu = {"Test 1", "Test 2", "Test 3"}
- rcm_data.active = false
- rcm_data.x, rcm_data.y = 1,1
- function rcm_click(x,y)
- if x >= rcm_data.x and x <= rcm_data.x+lt(rcm_data.menu) and y >= rcm_data.y and y <= rcm_data.y+#rcm_data.menu then
- return true
- end
- return false
- end
- function setRcmMenu(tab)
- if not valid(tab,"table") then return error("Expected tab to be a table, got "..type(tab)) end
- rcm_data.menu = tab
- end
- function rcm(x,y,options,wid,hei)
- if not valid(x,"number") then return error("Expected number for x, got "..type(x)) end
- if not valid(y,"number") then return error("Expected number for y, got "..type(y)) end
- if not valid(options,"table") then return error("Expected table for image, got "..type(options)) end
- local lnt = lt(options) -- short for longest term (used to set width of menu)
- local w,h if not notnil(wid) or not notnil(hei) then w,h = term.getSize() else w,h = wid,hei end
- if x >= w-lnt then
- if y > h-#options-1 then
- paintutils.drawBox(x-1,y-1,x-lnt-1,y-#options-1,colors.lightGray)
- paintutils.drawFilledBox(x,y,x-lnt,y-#options,colors.white)
- --cp(x,y) write("1")
- setText("black")
- for i=1, #options do
- cp(x-lnt,y-#options+i-1) write(options[i])
- end
- else
- paintutils.drawBox(x-lnt-1,y+1,x-1,y+#options+1,colors.lightGray)
- paintutils.drawFilledBox(x-lnt,y,x,y+#options,colors.white)
- --cp(x,y) write("2")
- setText("black")
- for i=1, #options do
- cp(x-lnt,y+i-1) write(options[i])
- end
- end
- elseif x < w-lnt then
- if y > h-#options-1 then
- paintutils.drawBox(x+1,y-#options-1,x+lnt+1,y-1,colors.lightGray)
- paintutils.drawFilledBox(x,y-#options,x+lnt,y,colors.white)
- --cp(x,y) write("3")
- setText("black")
- for i=1, #options do
- cp(x,y-#options+i-1) write(options[i])
- end
- else
- paintutils.drawBox(x+1,y+1,x+lnt+1,y+#options+1,colors.lightGray)
- paintutils.drawFilledBox(x,y,x+lnt,y+#options,colors.white)
- --cp(x,y) write("4")
- setText("black")
- for i=1, #options do
- cp(x,y+i-1) write(options[i])
- end
- end
- end
- sleep(.0001)
- end
- -- RCM --
- -- WINDOWS & SHORTCUTS --
- process = {}
- local processLimit = 5
- for i=1, processLimit do process[i] = 0 end
- function createWindow(name, fullscreen, program)
- local selectedProcess = 0
- for i=1, #process do
- if process[#process-i+1] == 0 then selectedProcess = #process-i+1 end
- end
- if selectedProcess == 0 then return false end
- if not valid(program,"function") then return false end
- if fullscreen then
- process[selectedProcess] = window.create(term.current(),1,1,w,h)
- else
- process[selectedProcess] = window.create(term.current(),1,2,w,h-1)
- end
- process[selectedProcess].name = name
- process[selectedProcess].program = program
- end
- function deleteWindow(name)
- if windowExists(name) then
- if type(name) == "string" then name = getWindow(name) end
- process[name] = 0
- end
- end
- function createPopup(name, program)
- --
- end
- function getWindow(id) -- returns info for window if exists.
- if process[id] ~= 0 then return process[id].name, process[id].fullscreen else return false end
- end
- function windowExists(id) -- only returns true or false.
- if type(id) == "number" then
- if process[id] ~= 0 then return true else return false end
- elseif type(id) == "string" then
- for i=1, #process do
- if process[i] ~= 0 then if process[i].name == id then return true end end
- end
- return false
- end
- end
- function taskManager()
- setBack("black") setText("white") cp(1,1) clr() print("test") sleep(5)
- end
- -- WINDOWS & SHORTCUTS --
- -- SHELL SECURITY --
- shell = {
- run = function(program)
- if not notnil(program) or not fs.exists(program) then return error("No such program.") end
- local file = fs.open(program,"r")
- local file_info = file.readAll() file.close()
- local syntax = {"fs","error","shell.run","return"}
- local trustLevel = {}
- for i=1, #syntax do trustLevel[i] = false end
- local run = true
- for i=1, #syntax do
- if notnil(string.find(file_info,syntax[i])) then trustLevel[i] = true run = false end
- end
- if run then
- os.run({}, program)
- else
- sleep(.5)
- for i=1, #syntax do
- print(syntax[i].." - "..tostring(trustLevel[i]))
- end
- print("Press 'enter' to run. otherwise continue...")
- a,i = pullEvents("key") if i == keys.enter then os.run({}, program) end
- end
- end
- }
- -- SHELL SECURITY --
- -- ENGINE V1 *** MAIN ENGINE *** *** MAIN ENGINE *** *** MAIN ENGINE *** ENGINE V1 --
- local dir = "Windowscraft"
- local images = {
- {
- filename = "boot.nfp",
- "eeeeeddddd",
- "eeeeeddddd",
- "eeeeeddddd",
- "eeeeeddddd",
- "bbbbb11111",
- "bbbbb11111",
- "bbbbb11111",
- "bbbbb11111",
- },
- }
- local raw_images = {
- desktop = {}
- }
- for i=1, h-1 do -- default blue
- table.insert(raw_images["desktop"], string.rep("9",w))
- end
- function updateBackgroundImage()
- if fs.exists(dir.."/images/desktop.nfp") then
- local img = loadImage(dir.."/images/desktop.nfp")
- local iw,ih = getTableSize(img)
- if iw == w and ih == h-1 then
- raw_images["desktop"] = img
- end
- end
- end
- updateBackgroundImage()
- function boot()
- setText("white") setBack("black") clr()
- for i=1, #images do
- if not fs.exists(dir.."/images/"..images[i].filename) then
- local file = fs.open(dir.."/images/"..images[i].filename, "w")
- for ii=1, #images[i] do
- file.writeLine(images[i][ii])
- end
- file.close()
- end
- end
- for i=1, #images do
- images[string.gsub(images[i].filename,".nfp","")] = paintutils.loadImage(dir.."/images/"..images[i].filename)
- end
- paintutils.drawImage(images["boot"],w/2-(#images["boot"][1]/2),h/2-(#images["boot"]/2)) sleep(1.5)
- cp(w/2-(#images["boot"][1]/2),h/2-(#images["boot"]/2)-2)
- paintutils.drawLine(w/2-(#images["boot"][1]/2), h/2+(#images["boot"]/2)+2, w/2+(#images["boot"][1]/2)-1,h/2+(#images["boot"]/2)+2, colors.gray) sleep(.5)
- for i=1, #images["boot"][1] do
- paintutils.drawPixel(w/2-(#images["boot"][1]/2)+i-1, h/2+(#images["boot"]/2)+2,colors.white)
- sleep(math.random(2,4)*.1)
- end
- setText("white") setBack("black") clr() sleep(1)
- end
- local functions = {
- debug = function() return error("Debug Crash") end,
- exit = function() running = false end,
- debugwindow = function() createWindow("Window Test",false,function() taskManager() end) end,
- }
- function forceUpdate()
- update = false
- renderImage(raw_images["desktop"],1,1)
- paintutils.drawLine(1,h,w,h,colors.white)
- paintutils.drawPixel(1,h,colors.blue)
- local current = term.current()
- for i=1, #process do
- if process[i] ~= 0 then
- --updateWindow(i)
- end
- end
- term.redirect(current)
- if rcm_data.active == true then
- rcm(rcm_data.x,rcm_data.y,rcm_data.menu,w,h-1)
- end
- end
- running = true
- local update = true
- setBack("black") clr()
- function runtime()
- setRcmMenu({"Debug", "Exit", "Debug Window"})
- while running do
- if update then
- forceUpdate()
- end
- a,i,x,y = pullEvents("mouse_click", "mouse_drag")
- if a == "mouse_click" then
- if i == 1 then
- if rcm_data.active then
- if rcm_click(x,y) then
- rcm_data.active = false if notnil(functions[string.gsub(string.lower(rcm_data.menu[y-rcm_data.y+1] or "")," ","")]) then functions[string.gsub(string.lower(rcm_data.menu[y-rcm_data.y+1] or "")," ","")]() end
- end
- rcm_data.active = false
- update = true
- else
- if checkCol(x,y) ~= false then
- moveWindow(1,x,y)
- end
- end
- rcm_data.active = false
- elseif i == 2 then
- rcm_data.active = true rcm_data.x,rcm_data.y = x,y update = true
- end
- elseif a == "mouse_drag" then
- end
- end
- end
- function engine_1() -- this will run windows that run programs that keep runtime running. runtime controls actions to the window
- en = 1
- while running do
- sleep(1)
- if type(process[en]) == "table" then
- term.redirect(process[en])
- process[en].program()
- term.redirect(term.native())
- deleteWindow(process[en].name) forceUpdate()
- end
- end
- end
- function engine_2() -- this will run windows that run programs that keep runtime running. runtime controls actions to the window
- en = 2
- while running do
- sleep(1)
- if type(process[en]) == "table" then
- term.redirect(process[en])
- process[en].program()
- term.redirect(term.native())
- deleteWindow(process[en].name) forceUpdate()
- end
- end
- end
- function engine_3() -- this will run windows that run programs that keep runtime running. runtime controls actions to the window
- en = 3
- while running do
- sleep(1)
- if type(process[en]) == "table" then
- term.redirect(process[en])
- process[en].program()
- term.redirect(term.native())
- deleteWindow(process[en].name) forceUpdate()
- end
- end
- end
- function engine_4() -- this will run windows that run programs that keep runtime running. runtime controls actions to the window
- en = 4
- while running do
- sleep(1)
- if type(process[en]) == "table" then
- term.redirect(process[en])
- process[en].program()
- term.redirect(term.native())
- deleteWindow(process[en].name) forceUpdate()
- end
- end
- end
- function engine_5() -- this will run windows that run programs that keep runtime running. runtime controls actions to the window
- en = 5
- while running do
- sleep(1)
- if type(process[en]) == "table" then
- term.redirect(process[en])
- process[en].program()
- term.redirect(term.native())
- deleteWindow(process[en].name) forceUpdate()
- end
- end
- end
- boot() local r,e
- parallel.waitForAny(function() r,e = pcall(runtime) end, engine_1, engine_2, engine_3, engine_4, engine_5)
- if not r then error(e) end
- setBack("black") setText("white") cp(1,1) clr() print("Windowscraft") sleep(.1)
Advertisement
Add Comment
Please, Sign In to add comment