if LevelOS then
	LevelOS.self.window.icon = {"\4","e"}
	LevelOS.maximize()
end
local customHold = false
if not lOS or not lUtils then
	shell.run("pastebin run zZszFdWe")
	customHold = true
end
if not fs.exists("blittle") then
	shell.run("pastebin get ujchRSnU blittle")
end
if not blittle then
	os.loadAPI("blittle")
end
local canvas = window.create(term.current(),1,1,term.getSize())
local objs = {}
local oterm = term.current()
local slides = {{win=window.create(term.current(),1,1,51,19,true),objs={},c=1}}
--local slides = {{win=window.create(term.current(),1,1,51,19,true),objs={},c=1},{win=window.create(term.current(),1,1,51,19,true),objs={},c=2},{win=window.create(term.current(),1,1,51,19,true),objs={},c=3}}
local slide = slides[1]
local selShape

local drag

local sPath

local uline = {w=1,x=1}

local pDir = "AppData/Shapescape/temp/"..lUtils.randStr(5)

fs.makeDir(pDir)

if LevelOS then
	function LevelOS.close()
		fs.delete(pDir)
	end
end

local shape = {}

local themeCol = {bg=colors.gray,fg=colors.lightGray,txt=colors.white,topbar={txt=colors.white,bg=colors.lightGray,inactive=colors.gray}}

local sCol = {colors.cyan,colors.blue,colors.black}

local bottomMsg

local scrOpt = {}
--scrOpt["Change color \31"] = function(x,y) end -- ugh bored
scrOpt["None"] = function(self) selShape.event[self[1]:sub(3)] = {function() end,-1} end

local cID = 0

local aligning = {}
local al = aligning
al.options = {{"Snap left","Snap right","Snap center"},{"Snap top","Snap bottom","Snap center"}}
local m = al.options[1][1]
local m2 = al.options[2][1]
al["Bottom"] = m2
al["Top"] = m2
al["Left"] = m
al["Right"] = m
m = nil
m2 = nil
al = nil

local e

local assets = {}
--function addScript(tShp,id,ev,assets,LevelOS,slides)


local function save(path)
	-- make later
	if not path then return false end
	-- PUT ASSETS HERE DUMB FUCK
	local sData = ""
	local pAssets = lUtils.instantiate(assets)
	for key,value in pairs(pAssets) do
		if type(key) ~= "string" then
			pAssets[key] = nil
		else
			local aDir = fs.combine(pDir,"assets")
			local f = fs.combine(aDir,key)
			value.content = lUtils.fread(f)
		end
	end
	sData = sData.."local assets = "..textutils.serialize(pAssets).."\n\n"
	sData = sData.."local nAssets = {}\n"
	sData = sData..'for key,value in pairs(assets) do nAssets[key] = value nAssets[assets[key].id] = assets[key] end\n'
	sData = sData..'assets = nAssets\nnAssets = nil\n\n'
	for s=1,#slides do
		for o=1,#slides[s].objs do
			slides[s].objs[o].tEnv = nil
		end
	end
	local pSlides = lUtils.instantiate(slides)
	for p=1,#pSlides do
		local s = pSlides[p]
		s.win = nil
		for t=1,#s.objs do
			local obj = s.objs[t]
			obj.render = nil
			obj.update = nil
			obj.window = nil
			obj.fRender = nil
			obj.fUpdate = nil
			obj.coroutine = nil
			obj.remove = nil
			obj.tEnv = nil
			if obj.event then
				for key,value in pairs(obj.event) do
					obj.event[key][1] = nil
				end
			end
		end
	end
	sData = sData.."local slides = "..textutils.serialize(pSlides).."\n\n"
	sData = sData..[[for s=1,#slides do
	local slide = slides[s]
	for o=1,#slide.objs do
		local obj = slide.objs[o]
		for key,value in pairs(obj.event) do
			if assets[ value[2] ] then
				lUtils.shapescape.addScript(obj,value[2],key,assets,LevelOS,slides)
			else
				obj.event[key] = {function() end,-1}
			end
		end
	end
end

	]]
	sData = sData..[[
local tArgs = {...}
if tArgs[1] and tArgs[1] == "load" then
	return {assets=assets,slides=slides}
end


return lUtils.shapescape.run(slides,...)]]
	lUtils.fwrite(path,sData)
	return true
end


shape.current = "None"
--[[function shape.rect(x1,y1,x2,y2)
	local tObj = {event={mouse_click={function() end,-1},mouse_up={function() end,-1},focus={function() end,-1},update={function() end,-1},render={function() end,-1},Coroutine={function() end,-1}},type="rect",color=sCol[1],border={type=1,color=sCol[2]},x1=x1,y1=y1,x2=x2,y2=y2}
	slide.objs[#slide.objs+1] = tObj
	return tObj
end


function shape.text(txt,x1,y1,x2,y2)
	local tObj = shape.rect(x1,y1,x2,y2)
	tObj.type = "text"
	tObj.color = 0
	tObj.border.color = 0
	tObj.txtcolor = sCol[3]
	tObj.txt = txt
	return tObj
end


function shape.window(x1,y1,x2,y2)
	local tObj = shape.rect(x1,y1,x2,y2)
	tObj.type = "window"
	tObj.color = colors.black
	tObj.border.color = 0
	return tObj
end

function shape.input(x1,y1,x2,y2)
	local tObj = shape.text("",x1,y1,x2,y2)
	tObj.type = "input"
	tObj.color = sCol[1]
	return tObj
end]]
local createShape = {}
createShape.rect = lUtils.shapescape.createRectangle
createShape.text = lUtils.shapescape.createText
createShape.window = lUtils.shapescape.createWindow
createShape.input = lUtils.shapescape.createInputbox
for k,v in pairs(createShape) do
	shape[k] = function(...) local tObj = v(...) tObj.render = nil tObj.update = nil tObj.remove = nil slide.objs[#slide.objs+1] = tObj return tObj end
end


local function swap()
	local fg,bg = term.getTextColor(),term.getBackgroundColor()
	term.setTextColor(bg)
	term.setBackgroundColor(fg)
end


function selColor(x,y,sColor,trans,img)
	local tColors = {colors.red,colors.orange,colors.yellow,colors.lime,colors.green,colors.cyan,colors.blue,colors.lightBlue,colors.pink,colors.magenta,colors.purple,colors.brown,colors.black,colors.gray,colors.lightGray,colors.white}
	local cObjs = {}
	local c = 1
	term.setBackgroundColor(themeCol.bg)
	term.setTextColor(themeCol.fg)
	local y2 = y+12
	if trans then
		y2 = y2+3
	end
	local imgPos
	if img then
		imgPos = y2+1
		y2 = y2+2
	end
	lUtils.border(x,y,x+16,y2,"fill")
	for y1=1,4 do
		for x1=1,4 do
			local o = {tColors[c],x=(x+1+(x1-1)*4),y=(y+1+(y1-1)*3)}
			cObjs[#cObjs+1] = o
			term.setBackgroundColor(o[1])
			if sColor == o[1] then
				term.setTextColor(colors.red)
			else
				term.setTextColor(themeCol.fg)
			end
			lUtils.border(o.x,o.y,o.x+2,o.y+1,"fill")
			c = c+1
		end
	end
	if trans then
		local o = {0,x=(x+1),y=(y+1+(4)*3)}
		cObjs[#cObjs+1] = o
		local blWin = blittle.createWindow(term.current(),o.x,o.y,3,2)
		local tpTerm = term.current()
		term.redirect(blWin)
		term.setBackgroundColor(themeCol.bg)
		term.clear()
		if sColor == 0 then
			term.setBackgroundColor(colors.red)
		else
			term.setBackgroundColor(themeCol.fg)
		end
		term.setCursorPos(1,1)
		term.write("      ")
		term.setCursorPos(1,6)
		term.write("      ")
		for ty=2,5 do
			term.setCursorPos(1,ty)
			term.write(" ")
			term.setCursorPos(6,ty)
			term.write(" ")
			term.setCursorPos(ty,ty)
			term.write(" ")
			term.setCursorPos(6-(ty-1),ty)
			term.write(" ")
		end
		term.redirect(tpTerm)
		term.setCursorPos(o.x+4,o.y)
		term.setTextColor(themeCol.txt)
		term.setBackgroundColor(themeCol.bg)
		term.write("None")
		term.setCursorPos(o.x+4,o.y+1)
		term.write("Transparent")
	end
	if img then
		term.setCursorPos(x+1,imgPos)
		term.setTextColor(themeCol.txt)
		term.setBackgroundColor(themeCol.bg)
		term.write("Image...")
	end
	while true do
		e = {os.pullEventRaw()}
		if e[1] == "mouse_up" then
			if e[3] >= x and e[4] >= y and e[3] <= x+16 and e[4] <= y2 then
				for t=1,#cObjs do
					if e[3] >= cObjs[t].x and e[4] >= cObjs[t].y and e[3] <= cObjs[t].x+2 and e[4] <= cObjs[t].y+1 then
						term.setTextColor(colors.orange)
						if cObjs[t][1] == 0 then
							term.setBackgroundColor(themeCol.bg)
						else
							term.setBackgroundColor(cObjs[t][1])
						end
						o = cObjs[t]
						lUtils.border(o.x,o.y,o.x+2,o.y+1,"fill")
						os.sleep(0.01)
						return cObjs[t][1]
					end
				end
			else
				return false
			end
			if e[3] >= x+1 and e[3] <= x+15 and e[4] == imgPos then
				term.setTextColor(themeCol.fg)
				term.setCursorPos(x+1,imgPos)
				term.setBackgroundColor(themeCol.bg)
				term.write("Image...")
				os.sleep(0.01)
				local f = lUtils.explorer("User","SelFile")
				if f then
					local tImg = lUtils.fread(f)
					if textutils.unserialize(tImg) then
						return textutils.unserialize(tImg)[1]
					else
						return tImg
					end
				end
			end
		end
	end
end


local clipboard = {}

local function paste(theobj)
	local obj = theobj
	obj.render = nil
	obj.update = nil
	obj.window = nil
	obj.fRender = nil
	obj.fUpdate = nil
	obj.coroutine = nil
	obj.remove = nil
	obj.tEnv = nil
	if obj.event then
		for key,value in pairs(obj.event) do
			obj.event[key][1] = nil
		end
	end
	obj = lUtils.instantiate(obj)
	obj.render = nil
	obj.update = nil
	if obj.event then
		for key,value in pairs(obj.event) do
			if assets[value[2]] then
				lUtils.shapescape.addScript(obj,value[2],key,assets,LevelOS,slides)
			else
				obj.event[key] = {function() end,-1}
			end
		end
	end
	local col = true
	while col == true do
		local tCol = false
		for o=1,#slide.objs do
			local ob = slide.objs[o]
			if ob.x1 == obj.x1 and ob.y1 == obj.y1 then
				obj.x1 = obj.x1 + 1
				obj.y1 = obj.y1 + 1
				obj.x2 = obj.x2 + 1
				obj.y2 = obj.y2 + 1
				tCol = true
			end
		end
		col = tCol
	end
	slide.objs[#slide.objs+1] = obj
	selShape = obj
end

local function copy(...)
	if ccemux then
		ccemux.setClipboard(" ")
	end
	clipboard = {...}
end

slide.win.setBackgroundColor(colors.white)
slide.win.clear()






local function assignScript(self,selfobj)
	local sOptions = {{},{}}
	-- do pairs for scrOpt and assets THIS IS THE BEST WAY IF U DONT THINK SO U ARE WRONG
	for key, value in pairs(scrOpt) do
		if type(key) ~= "number" then
			sOptions[1][#sOptions[1]+1] = key
		end
	end
	for key, value in pairs(assets) do
		if type(key) ~= "number" then
			sOptions[2][#sOptions[2]+1] = key
		end
	end
	local dis = {}
	if assets[selShape.event[self[1]:sub(3)][2]] then
		dis[assets[selShape.event[self[1]:sub(3)][2]].name] = true
	end
	local a = {lUtils.oclickmenu(self.x-1,self.y+1,20,sOptions,nil,dis)}
	if a[1] then
		if a[2] == 1 then
			scrOpt[a[3]](self)
		elseif a[2] == 2 then
			local id = assets[a[3]].id
			local shp = selShape
			lUtils.shapescape.addScript(shp,id,self[1]:sub(3),assets,LevelOS,slides)
		end
	end
end


local w,h = term.getSize()
local menu = {left={vis=true,w=math.min(math.floor(w/4),20),scroll=0},top={vis=true,h=7},right={vis=false,w=math.floor(w/4)},bottom={vis=true,h=2}}

menu.top.cTab = {}
menu.top.tabs = {
	{
		"File",
		objs={}
	},
	{
		"Start",
		objs={}
	},
	{
		"Align",
		objs={
			{
				{
					"Left",
					h=1,
					y=4,
					func = function(self,selfobj)
						local a = {lUtils.oclickmenu(self.x+selfobj.w-11,self.y+1,15,aligning.options[1])}
						if a[1] then
							selShape.snap[self[1]] = a[3]
						end
					end,
					req = function(self)
						if selShape then
							return true
						end
					end
				},
				{
					"Right",
					h=1,
					y=6,
					func = function(self,selfobj)
						local a = {lUtils.oclickmenu(self.x+selfobj.w-11,self.y+1,15,aligning.options[1])}
						if a[1] then
							selShape.snap[self[1]] = a[3]
						end
					end,
					req = function(self)
						if selShape then
							return true
						end
					end
				},
				w=5+11
			},
			{
				{
					"Top",
					h=1,
					y=4,
					func = function(self,selfobj)
						local a = {lUtils.oclickmenu(self.x+selfobj.w-11,self.y+1,15,aligning.options[2])}
						if a[1] then
							selShape.snap[self[1]] = a[3]
						end
					end,
					req = function(self)
						if selShape then
							return true
						end
					end
				},
				{
					"Bottom",
					h=1,
					y=6,
					func = function(self,selfobj)
						local a = {lUtils.oclickmenu(self.x+selfobj.w-11,self.y+1,15,aligning.options[2])}
						if a[1] then
							selShape.snap[self[1]] = a[3]
						end
					end,
					req = function(self)
						if selShape then
							return true
						end
					end
				},
				w=6+11
			},
			{
				{
					"Auto-align",
					h=1,
					y=4,
					func = function(self,selfobj)
						-- check for each side of object what its closest to (center or one of the sides)
					end,
					req = function(self)
						if selShape then
							return true
						end
					end
				},
				{
					"Preview",
					h=1,
					y=6,
					func = function(self,selfobj)
						local tcc = themeCol
						term.setBackgroundColor(tcc.bg)
						term.clear()
						local w,h = term.getSize()
						local sX1,sY1 = 1,1 -- screen coords
						local sX2,sY2 = w,h
						local cX,cY = math.floor((sX1+sX2)/2),math.floor((sY1+sY2)/2) -- center
						local n = 1.05
						local c = 0.05
						local oSW,oSH = slide.win.getSize() -- original slide width, original slide height
						while n > 1 do
							term.setBackgroundColor(tcc.bg)
							term.clear()
							local sW,sH = math.floor(oSW*n),math.floor(oSH*n)
							slide.win.reposition(cX-math.floor(sW/2),cY-math.floor(sH/2),sW,sH)
							lUtils.shapescape.renderSlide(slide,true)
							slide.win.redraw()
							os.sleep(0.1)
							n = n+c
							if n >= 1.5 and c > 0 then
								c = c*-1
							end
						end
						local sW,sH = oSW,oSH
						slide.win.reposition(cX-math.floor(sW/2),cY-math.floor(sH/2),sW,sH)
					end,
				},
				w = 0
			}
		}
	},
	{
		"Events",
		objs={
			{
				{
					"\31 mouse_click",
					y=3,
					h=1,
					func = assignScript,
					req = function(self)
						if selShape then
							return true
						end
					end
				},
				{
					"\31 mouse_up",
					y=5,
					h=1,
					func = assignScript,
					req = function(self)
						if selShape then
							return true
						end
					end
				},
				w=0
			},
			{
				{
					"\31 focus",
					y=3,
					h=1,
					func = assignScript,
					req = function(self)
						if selShape then
							return true
						end
					end
				},
				{
					"\31 update",
					y=5,
					h=1,
					func = assignScript,
					req = function(self)
						if selShape then
							return true
						end
					end
				},
				w=0
			},
			{
				{
					"\31 render",
					y=3,
					h=1,
					func = assignScript,
					req = function(self)
						if selShape then
							return true
						end
					end
				},
				{
					"\31 Coroutine",
					y=5,
					h=1,
					func = assignScript,
					req = function(self)
						if selShape then
							return true
						end
					end
				},
				w=0
			}
		}
	},
	{
		"Scripts",
		objs={
			{
				{
					"Import",
					h=1,
					y=menu.top.h-1,
					func = function()
						--error("fking dont",2)
						local tempFiles = {lUtils.explorer("User/Scripts","SelFile")}
						local p = 0
						for t=1,#tempFiles do
							local tempFile = tempFiles[t]
							if tempFile then
								if assets[tempFile] then
									lUtils.popup("Shapescape","This file already exists!",27,8,{"OK"})
								else
									if #tempFiles == 1 then
										bottomMsg = "Added "..fs.getName(tempFile).." to assets!"
									end
									p = p+1
									local tFile = fs.getName(tempFile)
									assets[tFile] = {id=cID,content=lUtils.fread(tempFile),name=tFile}
									assets[cID] = assets[tFile]
									local aDir = fs.combine(pDir,"assets")
									local f = fs.combine(aDir,tFile)
									lUtils.fwrite(f,assets[tFile].content)
									cID = cID+1
								end
							end
						end
						if #tempFiles >= 2 then
							bottomMsg = "Added "..p.." files to assets!"
						end
					end,
				},
				w=0
			},
			{
				{
					"Create",
					h=1,
					y=menu.top.h-1,
					func = function()
						local c = {lUtils.inputbox("Shapescape","New script name:",27,9,{"Confirm","Cancel"})}
						if c[1] ~= "" and c[2] == true and c[4] == "Confirm" then
							if lUtils.getFileType(c[1]) == "" then
								c[1] = c[1]..".lua"
							end
							if not assets[c[1]] then
								assets[c[1]] = {id=cID,content="",name=c[1]}
								assets[cID] = assets[c[1]]
								local aDir = fs.combine(pDir,"assets")
								local f = fs.combine(aDir,c[1])
								lUtils.fwrite(f,"")
								cID = cID+1
								--lUtils.openWin("Editing "..tFile,"rom/programs/edit.lua "..f,6,6,w-11,h-11,false,false)
								if fs.exists("Program_Files/Slime_Text") then
									lOS.execute("Program_Files/Slime_Text "..f)
								else
									lOS.execute("rom/programs/edit.lua "..f)
								end
							else
								lUtils.popup("Shapescape","This file already exists!",27,8,{"OK"})
							end
						end
					end,
				},
				w=0
			},
			{
				{
					"Scripts \31",
					h=1,
					y=3,
					func = function(self,selfobj)
						local files = {}
						for key, value in pairs(assets) do
							if type(key) ~= "number" then
								files[#files+1] = key
							end
						end
						table.sort(files)
						local fOptions = {}
						for k,v in ipairs(files) do
							local submenu = {{"Edit","Delete","Rename","Duplicate"}}
							submenu.file = v
							submenu.id = k
							fOptions[k] = {txt=v,action=submenu}
						end
						local w,h = term.getSize()
						local dividers = true
						if h <= 20 then
							dividers = false
						end
						local clck = lOS.contextmenu(self.x-1,self.y+1,0,fOptions,themeCol,dividers)
						local aDir = fs.combine(pDir,"assets")
						--local a = {lUtils.oclickmenu(self.x-1,self.y+1,mW,files,nil,nil,themeCol)}
						if clck then
							local tFile = clck.parent.file
							--local b = {lUtils.oclickmenu(self.x+mW-2,self.y+(a[2]*2)-1,11,{{"Edit","Delete","Rename","Duplicate"}})}
							if clck.txt == "Edit" then
								local f = fs.combine(aDir,tFile)
								local w,h = term.getSize()
								--lUtils.fwrite(f,assets[tFile].content)
								--lUtils.openWin("Editing "..tFile,"rom/programs/edit.lua "..f,6,6,w-11,h-11,false,false)
								if fs.exists("Program_Files/Slime_Text") then
									lOS.execute("Program_Files/Slime_Text "..f)
								else
									lOS.execute("rom/programs/edit.lua "..f)
								end
								--assets[tFile].content = lUtils.fread(f)
								--fs.delete(f)
							elseif clck.txt == "Delete" then
								local c = {lUtils.popup("Shapescape","Are you sure you want to remove this file? (THIS ACTION CANNOT BE UNDONE)",27,9,{"Remove","Cancel"})}
								if c[1] == true and c[3] == "Remove" then
									-- remove all instances of script in slide
									fs.delete(fs.combine(aDir,tFile))
									assets[assets[tFile].id] = nil
									assets[tFile] = nil
									bottomMsg = "Removed "..tFile.." from assets!"
								end
							elseif clck.txt == "Rename" then
								local c = {lUtils.inputbox("Shapescape","New script name:",27,9,{"Confirm","Cancel"})}
								if c[1] ~= "" and c[2] == true and c[4] == "Confirm" then
									if not assets[c[1]] then
										assets[c[1]] = assets[tFile]
										assets[tFile] = nil
										assets[c[1]].name = c[1]
										fs.move(fs.combine(aDir,tFile),fs.combine(aDir,c[1]))
										bottomMsg = tFile.." renamed to "..c[1].."!"
									else
										lUtils.popup("Shapescape","This file already exists!",27,8,{"OK"})
									end
								end
							elseif clck.txt == "Duplicate" then
								local c = {lUtils.inputbox("Shapescape","Name of duplicate:",27,9,{"Confirm","Cancel"})}
								if c[1] ~= "" and c[2] == true and c[4] == "Confirm" then
									if not assets[c[1]] then
										assets[c[1]] = {id=cID,content=assets[tFile].content,name=c[1]}
										assets[cID] = assets[c[1]]
										lUtils.fwrite(fs.combine(aDir,c[1]),assets[c[1]].content)
										cID = cID+1
										bottomMsg = tFile.." duplicated to "..c[1].."!"
									else
										lUtils.popup("Shapescape","This file already exists!",27,8,{"OK"})
									end
								end
							end
						end
					end,
					req = function()
						for key,value in pairs(assets) do
							if key then
								return true
							end
						end
						return false
					end
				},
				{
					"Inspector \17",
					h=1,
					y=menu.top.h-1,
					func = function(self,selfobj)
						if menu.right then
							if menu.right.vis then
								menu.right.vis = false
							else
								menu.right.vis = true
							end
						end
					end,
					req = function()
						if menu.right then
							return true
						else
							return false
						end
					end
				},
				w=0
			}
		}
	},
	{
		"View",
		objs={
			{
				{
					"Theme \31",
					h=1,
					y=3,
					func = function(self)
						local a = {lUtils.oclickmenu(self.x-1,self.y+1,15,{{"Black Mode","Dark Mode"},{"Gray Mode","Gray Mode 2","Light Mode"}})}
						if a[1] == true then
							if a[3] == "Black Mode" then
								themeCol = {bg=colors.black,fg=colors.gray,txt=colors.white,topbar={txt=colors.white,bg=colors.black,inactive=colors.lightGray}}
							elseif a[3] == "Dark Mode" then
								themeCol = {bg=colors.gray,fg=colors.lightGray,txt=colors.white,topbar={txt=colors.white,bg=colors.gray,inactive=colors.lightGray}}
							elseif a[3] == "Gray Mode" then
								themeCol = {bg=colors.gray,fg=colors.lightGray,txt=colors.white,topbar={txt=colors.black,bg=colors.lightGray,inactive=colors.gray}}
							elseif a[3] == "Gray Mode 2" then
								themeCol = {bg=colors.gray,fg=colors.lightGray,txt=colors.white,topbar={txt=colors.white,bg=colors.lightGray,inactive=colors.gray}}
							elseif a[3] == "Light Mode" then
								themeCol = {bg=colors.lightGray,fg=colors.gray,txt=colors.black,topbar={txt=colors.black,bg=colors.white,inactive=colors.lightGray}}
							end
						end
					end
				},
				w = 0
			}
		}
	}
}
menu.top.objs = {}
menu.top.tabs[2].objs = {
	{
		{
			"Add Screen",
			h=1,
			y=menu.top.h-2,
			func = function()
				local w,h
				local n = #slides
				if slides[n] and slides[n].w and slides[n].h then
					w,h = slides[n].w,slides[n].h
				else
					w,h = 51,19
				end
				local tWin = window.create(term.current(),1,1,w,h,true)
				tWin.setBackgroundColor(colors.white)
				tWin.clear()
				slides[#slides+1] = {win=tWin,objs={},c=#slides+1}
			end
		},
		w=0
	},
	{
		{
			"Copy",
			h=1,
			y=3,
			func = function()
				if selShape then
					copy(selShape)
				end
			end,
			req = function()
				if selShape then
					return true
				else
					return false
				end
			end,
		},
		{
			"Paste",
			h=1,
			y=5,
			func = function()
				if clipboard[1] then
					paste(clipboard[1])
				end
			end,
			req = function()
				if clipboard[1] then
					return true
				else
					return false
				end
			end,
		},
		w=0
	},
	{
		{
			"Elements",
			h=2,
			y=menu.top.h-2,
			func = function(self)
				local dis = {}
				dis["Oval"] = true
				dis["Triangle"] = true
				dis[shape.current] = true
				local a = {lUtils.oclickmenu(self.x,self.y+2,15,{{"Text","Input Box","Window","Rectangle","Oval"}},true,dis)}
				if a[1] == true then
					selShape = nil
					shape.current = a[3]
				end
			end
		},
		w=0
	},
	{
		{
			"\2",
			h=2,
			y=3,
			func = function()
				if selShape then
					selShape.color = sCol[1]
				end
			end,
			req = function()
				if selShape then
					return true
				end
			end,
		},
		{
			"\1",
			h=2,
			y=5,
			func = function()
				if selShape then
					selShape.border.color = sCol[2]
				end
			end,
			req = function()
				if selShape then
					return true
				end
			end,
		},
		w=0
	},
	{
		{
			"Fill \31",
			h=1,
			y=3,
			func = function(self)
				if selShape then
					local a = selColor(self.x-1,self.y+1,sCol[1],true,true)
					if a and type(a) == "number" then
						sCol[1] = a
						selShape.color = sCol[1]
					elseif a then
						selShape.image = a
					end
				end
			end,
			req = function()
				if selShape then
					return true
				end
			end,
		},
		{
			"Border \31",
			h=1,
			y=5,
			func = function(self)
				if selShape then
					local a = selColor(self.x-1,self.y+1,sCol[2],true)
					if a then
						sCol[2] = a
						selShape.border.color = sCol[2]
					end
				end
			end,
			req = function()
				if selShape then
					return true
				end
			end,
		},
		w=0
	},
	{
		{
			"A",
			h=2,
			y=3,
			func = function()
				if selShape and (selShape.type == "text" or selShape.type == "input") then
					selShape.txtcolor = sCol[3]
				end
			end,
			req = function()
				if selShape and (selShape.type == "text" or selShape.type == "input") then
					return true
				end
			end,
		},
		{
			"\165",
			h=1,
			y=5,
			func = function(self)
				-- symbol func
			end,
			req = function()
				if selShape and selShape.type == "text" then
					return true
				end
			end,
		}
	},
	{
		{
			"\31",
			h=1,
			y=3,
			func = function(self)
				if selShape then
					local a = selColor(self.x-1,self.y+1,sCol[3])
					if a then
						sCol[3] = a
						selShape.txtcolor = sCol[3]
					end
				end
			end,
			req = function()
				if selShape and (selShape.type == "text" or selShape.type == "input") then
					return true
				end
			end,
		},
		{
			"\31",
			h=1,
			y=5,
			func = function(self)
				-- symbol func
			end,
			req = function()
				if selShape and selShape.type == "text" then
					return true
				end
			end,
		}
	}
}

for t=1,#menu.top.tabs[2].objs do
	if menu.top.tabs[2].objs[t].w == nil then
		menu.top.tabs[2].objs[t].w = 0
	end
end

menu.top.cTab = menu.top.tabs[2]

local function designer()
	local tcc = themeCol
	term.setBackgroundColor(tcc.bg)
	term.clear()
	local w,h = term.getSize()
	local sX1,sY1 = 1,1 -- screen coords
	local sX2,sY2 = w,h
	if menu.left.vis then
		sX1 = sX1+menu.left.w+1
	end
	if menu.top.vis then
		sY1 = sY1+menu.top.h
	end
	if menu.bottom.vis then
		sY2 = sY2-menu.bottom.h
	end
	if menu.right.vis then
		sX2 = sX2-menu.right.w
	end
	local cX,cY = math.floor((sX1+sX2)/2),math.floor((sY1+sY2)/2) -- center
	local sW,sH = slide.win.getSize()
	slide.win.reposition(cX-math.floor(sW/2),cY-math.floor(sH/2))
	lUtils.shapescape.renderSlide(slide,true)
	slide.win.redraw()
	term.setTextColor(tcc.fg)
	slide.x,slide.y = slide.win.getPosition()
	slide.w,slide.h = slide.win.getSize()
	if selShape then
		for x=selShape.x1-1,selShape.x2+1 do
			local c = "-"
			if x == selShape.x1-1 or x == selShape.x2+1 or x == math.floor((selShape.x2+(selShape.x1))/2) then
				c = "O"
			end
			if drag and drag[1] == "resize" and drag.y == 1 and (x == math.floor((selShape.x2+(selShape.x1))/2) and drag.x == 0) then
				term.setTextColor(colors.red)
			else
				if lUtils.toColor(({lUtils.getPixel(term.current(),(slide.x-1)+x,(slide.y-1)+selShape.y1-1)})[3]) == colors.lightGray then
					term.setTextColor(colors.gray)
				else
					term.setTextColor(colors.lightGray)
				end
			end
			term.setBackgroundColor(lUtils.toColor(({lUtils.getPixel(term.current(),(slide.x-1)+x,(slide.y-1)+selShape.y1-1)})[3]))
			term.setCursorPos((slide.x-1)+x,(slide.y-1)+selShape.y1-1)
			term.write(c)
			if drag and drag[1] == "resize" and drag.y == 2 and (x == math.floor((selShape.x2+(selShape.x1))/2) and drag.x == 0) then
				term.setTextColor(colors.red)
			else
				if lUtils.toColor(({lUtils.getPixel(term.current(),(slide.x-1)+x,(slide.y-1)+selShape.y2+1)})[3]) == colors.lightGray then
					term.setTextColor(colors.gray)
				else
					term.setTextColor(colors.lightGray)
				end
			end
			term.setBackgroundColor(lUtils.toColor(({lUtils.getPixel(term.current(),(slide.x-1)+x,(slide.y-1)+selShape.y2+1)})[3]))
			term.setCursorPos((slide.x-1)+x,(slide.y-1)+selShape.y2+1)
			term.write(c)
		end
		for y=selShape.y1-1,selShape.y2+1 do
			local c = "|"
			if y == selShape.y1-1 or y == selShape.y2+1 or y == math.floor((selShape.y2+(selShape.y1))/2) then
				c = "O"
			end
			term.setBackgroundColor(lUtils.toColor(({lUtils.getPixel(term.current(),(slide.x-1)+selShape.x1-1,(slide.y-1)+y)})[3]))
			term.setCursorPos((slide.x-1)+selShape.x1-1,(slide.y-1)+y)
			if drag and drag[1] == "resize" and drag.x == 1 and ((y == selShape.y1-1 and drag.y == 1) or (y == selShape.y2+1 and drag.y == 2) or (y == math.floor((selShape.y2+(selShape.y1))/2) and drag.y == 0)) then
				term.setTextColor(colors.red)
			else
				if lUtils.toColor(({lUtils.getPixel(term.current(),(slide.x-1)+selShape.x1-1,(slide.y-1)+y)})[3]) == colors.lightGray then
					term.setTextColor(colors.gray)
				else
					term.setTextColor(colors.lightGray)
				end
			end
			term.write(c)
			term.setBackgroundColor(lUtils.toColor(({lUtils.getPixel(term.current(),(slide.x-1)+selShape.x2+1,(slide.y-1)+y)})[3]))
			term.setCursorPos((slide.x-1)+selShape.x2+1,(slide.y-1)+y)
			if drag and drag[1] == "resize" and drag.x == 2 and ((y == selShape.y1-1 and drag.y == 1) or (y == selShape.y2+1 and drag.y == 2) or (y == math.floor((selShape.y2+(selShape.y1))/2) and drag.y == 0)) then
				term.setTextColor(colors.red)
			else
				if lUtils.toColor(({lUtils.getPixel(term.current(),(slide.x-1)+selShape.x2+1,(slide.y-1)+y)})[3]) == colors.lightGray then
					term.setTextColor(colors.gray)
				else
					term.setTextColor(colors.lightGray)
				end
			end
			term.write(c)
		end
		
	end
	if menu.left.vis then
		local m = menu.left
		if m.w < 5 then m.w = 5 end
		local th = 0
		if menu.top.vis then
			th = menu.top.h
		end
		th = th-m.scroll
		term.setBackgroundColor(tcc.bg)
		lOS.boxClear(1,th+1,m.w,h)
		term.setTextColor(tcc.bg)
		term.setBackgroundColor(tcc.fg)
		for t=th+1,h do
			term.setCursorPos(m.w+1,t)
			term.write("\149")
		end
		for t=1,#slides do
			local sW = m.w-3
			local sH = math.floor(({slides[t].win.getSize()})[2]/(({slides[t].win.getSize()})[1]/sW))
			if slide == slides[t] then
				term.setBackgroundColor(tcc.bg)
				term.setTextColor(colors.red)
				lUtils.border(2,(th+2)+(t-1)*(sH+2),m.w,((th+2)+(t-1)*(sH+2))+sH+1,nil,1)
			end
			term.setBackgroundColor(colors.white)
			local lWin = blittle.createWindow(term.current(),3,(th+3)+(t-1)*(sH+2),sW,sH)
			local dWin = window.create(term.current(),1,1,({lWin.getSize()})[1],({lWin.getSize()})[2],false)
			term.redirect(dWin)
			lUtils.littlewin(slides[t].win,lWin.getSize()).render(1,1)
			term.redirect(oterm)
			for l=1,math.floor(({lWin.getSize()})[2]) do
				--term.setCursorPos(1,1)
				--term.write(l.."/"..({lWin.getSize()})[2])
				lWin.setCursorPos(1,l)
				lWin.blit(dWin.getLine(l))
			end
		end
	end
	if menu.bottom.vis then
		term.setCursorPos(1,h-1)
		term.setBackgroundColor(tcc.bg)
		term.setTextColor(tcc.fg)
		term.write(string.rep("\131",w))
		term.setCursorPos(1,h)
		term.setTextColor(tcc.txt)
		term.clearLine()
		term.write(" Screen "..slide.c.." of "..#slides.." | "..#slide.objs.." objects | w: "..slide.w.."  h: "..slide.h)
		if shape.current ~= "None" then
			term.write(" | Creating "..shape.current)
		end
		if selShape and selShape.input then
			term.write(" | Typing")
		end
		if drag and drag[1] == "resize" then
			term.write(" | Resizing shape (w: "..(drag.shape.x2-drag.shape.x1+1)..", h: "..(drag.shape.y2-drag.shape.y1+1)..")")
		end
		if bottomMsg then
			term.write(" | "..bottomMsg)
		end
	end
	if menu.top.vis then
		local tc = themeCol.topbar
		if #menu.top.objs > 0 then
			if tcc.bg == tc.bg and (menu.top.cTab[1] == "Align" --[[or menu.top.cTab[1] == "Events"]]) then
				menu.top.h = 8
			else
				menu.top.h = 7
			end
		else
			menu.top.h = 2
		end
		local cX = 3
		term.setBackgroundColor(tc.bg)
		for t=1,menu.top.h do
			term.setCursorPos(1,t)
			term.clearLine()
		end
		term.setBackgroundColor(tc.bg)
		term.setTextColor(tc.txt)
		for c=1,#menu.top.objs do
			for o=1,#menu.top.objs[c] do
				local obj = menu.top.objs[c][o]
				obj.x = cX
				if not obj.w then
					obj.w = string.len(obj[1])
				end
				if menu.top.objs[c].w > obj.w then
					obj.w = menu.top.objs[c].w
				else
					menu.top.objs[c].w = obj.w
				end
				if obj.selected or (obj.req and not obj.req()) then
					--term.setBackgroundColor(colors.gray)
					--lOS.boxClear(obj.x,obj.y,obj.x+(obj.w-1),obj.y+(obj.h-1))
					term.setTextColor(tc.inactive)
				else
					term.setTextColor(tc.txt)
				end
				term.setCursorPos(obj.x,obj.y)
				term.setBackgroundColor(tc.bg)
				term.write(obj[1])
				if obj[1] == "Add Screen" then
					lUtils.border(obj.x+2, obj.y-2, obj.x+(obj.w-1)-2, obj.y-1)
					term.setCursorPos(obj.x+2,obj.y-2)
					term.write("+")
				elseif obj[1] == "Import" then
					local ico = {{"\131  ","144","444"},{"\151\140\148","499","944"}}
					for i=1,2 do
						term.setCursorPos(obj.x+1,(obj.y-4)+i)
						term.blit(unpack(ico[i]))
					end
				elseif obj[1] == "Create" then
					local tTxt = term.getTextColor()
					term.setCursorPos(obj.x+2,obj.y-3)
					term.setBackgroundColor(tTxt)
					term.setTextColor(tc.bg)
					term.write("\131")
					term.setCursorPos(obj.x+1,obj.y-2)
					term.setBackgroundColor(tc.bg)
					term.setTextColor(tTxt)
					term.write("\143 \143")
					term.setCursorPos(obj.x+2,obj.y-1)
					term.write("\131")
					term.setBackgroundColor(tTxt)
					term.setCursorPos(obj.x+2,obj.y-2)
					term.write(" ")
					term.setBackgroundColor(tc.bg)
				elseif obj[1] == "Elements" then
					term.setCursorPos(obj.x+3,obj.y+1)
					term.write("\31\31")
					term.setBackgroundColor(colors.cyan)
					term.setTextColor(colors.lightBlue)
					lOS.boxClear(obj.x+2,obj.y-2,obj.x+(obj.w-3),obj.y-1)
					lUtils.border(obj.x+2,obj.y-2,obj.x+(obj.w-3),obj.y-1)
				elseif obj[1] == "\2" or obj[1] == "\1" or obj[1] == "A" then
					term.setCursorPos(obj.x-1,obj.y+1)
					term.setBackgroundColor(tc.bg)
					local trans = false
					if term.getTextColor() == tc.inactive then
						-- nothing
					elseif obj[1] == "\2" then
						if sCol[1] == 0 then
							term.setTextColor(colors.red)
							trans = true
						else
							term.setTextColor(sCol[1])
						end
					elseif obj[1] == "\1" then
						if sCol[2] == 0 then
							term.setTextColor(colors.red)
							trans = true
						else
							term.setTextColor(sCol[2])
						end
					else
						term.setTextColor(sCol[3])
					end
					if not trans then
						term.write("\130\131\129")
					else
						term.write("×××")
					end
				elseif obj[1] == "Top" or obj[1] == "Bottom" or obj[1] == "Left" or obj[1] == "Right" then
					local txtcol = term.getTextColor()
					term.setBackgroundColor(themeCol.topbar.bg)
					if themeCol.topbar.bg == colors.lightGray then
						term.setTextColor(colors.gray)
					elseif themeCol.topbar.bg == colors.black then
						term.setTextColor(colors.lightGray)
					elseif themeCol.topbar.bg == themeCol.fg then
						term.setTextColor(colors.lightGray)
					else
						term.setTextColor(themeCol.fg)
					end
					local x,y = obj.x+menu.top.objs[c].w-10,obj.y
					term.setCursorPos(x-1,y-1)
					if y < 5 then
						swap()
						term.write("\159"..string.rep("\143",10))
						swap()
						term.write("\144")
					else
						swap()
						term.write("\157"..string.rep("\140",10))
						swap()
						term.write("\145")
					end
					swap()
					term.setCursorPos(x-1,y)
					term.write("\149")
					term.setCursorPos(x+10,y)
					swap()
					term.write("\149")
					term.setCursorPos(x-1,y+1)
					term.write("\130"..string.rep("\131",10).."\129")
					if selShape then
						if themeCol.topbar.bg == colors.lightGray then
							term.setBackgroundColor(colors.white)
							term.setTextColor(colors.black)
						elseif themeCol.topbar.bg ~= themeCol.fg then
							term.setBackgroundColor(themeCol.fg)
							term.setTextColor(themeCol.topbar.txt)
						else
							term.setBackgroundColor(themeCol.bg)
							term.setTextColor(themeCol.topbar.txt)
						end
					else
						term.setBackgroundColor(themeCol.topbar.bg)
					end
					term.setCursorPos(x,y)
					term.write(string.rep(" ",9).."\31")
					if selShape then
						term.setCursorPos(x,y)
						if not selShape.snap then
							selShape.snap = {}
						end
						if not selShape.snap[obj[1]] then
							selShape.snap[obj[1]] = aligning[obj[1]]
						end
						term.write(string.gsub(string.sub(selShape.snap[obj[1]],6,14),"^%l",string.upper))
					end
				end
			end
			cX = cX+menu.top.objs[c].w+2
			term.setTextColor(tc.txt)
			term.setBackgroundColor(tc.bg)
		end

		-- ALERT! BUTTON COLLISIONS WILL HAVE TO BE CALCULATED IWTH THE PARENT WIDTH NOT THE OBJECT WIDTH


		term.setBackgroundColor(tc.bg)
		term.setCursorPos(1,1)
		term.clearLine()
		term.setCursorPos(1,1)
		local cX = 3
		for t=1,#menu.top.tabs do
			term.setCursorPos(cX,1)
			term.write(menu.top.tabs[t][1])
			menu.top.tabs[t].x = cX
			menu.top.tabs[t].w = string.len(menu.top.tabs[t][1])
			cX = cX+menu.top.tabs[t].w+3
		end
		if tcc.bg == tc.bg then
			term.setCursorPos(2,menu.top.h)
			term.setBackgroundColor(tcc.fg)
			term.setTextColor(tc.bg)
			term.write(string.rep("\143",w-2))
		end
		term.setCursorPos(1,2)
		term.setBackgroundColor(tc.bg)
		term.setTextColor(colors.gray)
		local w,h = term.getSize()
		--term.write(string.rep("\131",w))
		local cTab = menu.top.cTab
		term.setTextColor(tc.txt)
		term.setBackgroundColor(tc.bg)
		while cTab[1] and (uline.w ~= cTab.w or uline.x ~= cTab.x) do
			term.setCursorPos(1,2)
			term.clearLine()
			term.setCursorPos(uline.x,2)
			term.write(string.rep("\131",uline.w))
			if uline.w < cTab.w then
				uline.w = uline.w + 1
			elseif uline.w > cTab.w then
				uline.w = uline.w - 1
			end
			if uline.x < cTab.x-15 then
				uline.x = uline.x + 5
			elseif uline.x < cTab.x-10 then
				uline.x = uline.x + 3
			elseif uline.x < cTab.x-6 then
				uline.x = uline.x + 2
			elseif uline.x < cTab.x then
				uline.x = uline.x + 1
			elseif uline.x > cTab.x+15 then
				uline.x = uline.x - 5
			elseif uline.x > cTab.x+10 then
				uline.x = uline.x - 3
			elseif uline.x > cTab.x+6 then
				uline.x = uline.x - 2
			elseif uline.x > cTab.x then
				uline.x = uline.x - 1
			end
			os.sleep(0.05)
		end
		term.setCursorPos(uline.x,2)
		term.write(string.rep("\131",uline.w))
	end
end
local function refreshC()
	for s=1,#slides do
		slides[s].c = s
	end
end


function _G.gibtext()
	while true do
		for a=1,#slide.objs do
			local o = slide.objs[a]
			if o.input then

				-- tmemp global
				_G.iTxt = lUtils.makeEditBox("",10,2)
				iTxt.lines[1] = o.txt
				local ppWin = window.create(term.current(),1,1,10,10,false)
				local opWin = term.current()
				local b = coroutine.create(function() iTxt = lUtils.drawEditBox(iTxt,1,1,nil,nil,nil,nil,true) end)
				-- draw to ppwin in coroutine so that its invisible
				while o.input do
					e = {os.pullEventRaw()}
					if not string.find(e[1],"mouse") then
						term.redirect(ppWin)
						coroutine.resume(b,unpack(e))
						term.redirect(opWin)
					end
					o.txt = ""
					for t=1,#iTxt.lines do
						if t > 1 then
							o.txt = o.txt.."\n"
						end
						o.txt = o.txt..iTxt.lines[t]
					end
					if coroutine.status(b) == "dead" then
						o.input = false
					end
					term.redirect(slide.win)
					o.render()
					term.redirect(opWin)
				end
			end
		end
		coroutine.yield()
	end
end

local textcor = coroutine.create(gibtext)
local hold = {}

if customHold then
	function lUtils.isHolding(key)
		if type(key) == "string" then
			key = keys[key]
		end
		if hold[key] then return true else return false end
	end
end

local function cHold()
	while true do
		e = {os.pullEvent()}
		if e[1] == "key" then
			hold[e[2]] = true
		elseif e[1] == "key_up" then
			hold[e[2]] = nil
		end
	end
end

if not lOS.pDebug then lOS.pDebug = {} end

local tArgs = {...}

function loadScape(tempFile)
	local newSc = loadfile(tempFile)("load")
	cID = 0
	lOS.pDebug.newSc = newSc
	assets,slides = newSc.assets,newSc.slides
	local aDir = fs.combine(pDir,"assets")
	if fs.exists(aDir) then
		fs.delete(aDir)
	end
	fs.makeDir(aDir)
	for key,value in pairs(assets) do
		if value.id > cID then
			cID = value.id
		end
		if type(key) == "string" then
			lUtils.fwrite(fs.combine(aDir,key),value.content)
		end
	end
	cID = cID+1
	sPath = tempFile
	for t=1,#slides do
		slides[t].win = window.create(term.current(),1,1,slides[t].w or 51,slides[t].h or 19)
		local oterm = term.current()
		term.redirect(slides[t].win)
		term.setBackgroundColor(colors.white)
		term.clear()
		lUtils.shapescape.renderSlide(slides[t],true)
		term.redirect(oterm)
	end
	slide = slides[1]
	bottomMsg = fs.getName(tempFile).." loaded."
	local recent = lUtils.asset.load("AppData/Shapescape/recent.txt") or {}
	local t = 1
	while t <= #recent do
		if recent[t] == tempFile then
			table.remove(recent,t)
		else
			t = t+1
		end
	end
	table.insert(recent,1,tempFile)
	if #recent > 10 then
		for t=11,#recent do
			recent[t] = nil
		end
	end
	lUtils.asset.save(recent,"AppData/Shapescape/recent.txt")
end

local tempFile = tArgs[1]
if tempFile and fs.exists(tempFile) then
	loadScape(tempFile)
end

while true do
	designer()
	refreshC()
	lOS.pDebug.shapes = slide.objs
	for o=1,#slide.objs do
		if slide.objs[o].input and not (selShape and selShape == slide.objs[o]) then
			slide.objs[o].input = false
		end
	end
	if e then
		coroutine.resume(textcor,unpack(e))
	end
	if selShape and selShape.input then
		term.setCursorBlink(true)
	end
	e = {os.pullEvent()}
	term.setCursorBlink(false)
	refreshC()
	if lUtils.isHolding("leftCtrl") and e[1] == "key" then
		if e[2] == keys.c then
			if selShape then
				copy(selShape)
			end
		end
	end
	if e[1] == "paste" and e[2] == "" then
		if clipboard[1] then
			for t=1,#clipboard do
				paste(clipboard[t])
			end
		end
	end
	--_G.debugclip = clipboard
	if e[1] == "mouse_click" or e[1] == "mouse_up" or e[1] == "mouse_drag" then

		local OGterm = term.current()
		local OGwin = {lines={}}
		local w,h = term.getSize()
		for t=1,h do
        	OGwin.lines[t] = {OGterm.getLine(t)}
    	end

        function OGwin.render()
        	for l=1,#OGwin.lines do
            	term.setCursorPos(1,l)
            	term.blit(table.unpack(OGwin.lines[l]))
        	end
    	end

		-- TEMP TESTING
		slide.x,slide.y = slide.win.getPosition()
		slide.w,slide.h = slide.win.getSize()
		local x1,y1,x2,y2 = e[3]-(slide.x-1),e[4]-(slide.y-1),e[3]-(slide.x-1),e[4]-(slide.y-1)
		if string.find(e[1],"mouse") and ((e[3] >= slide.x and e[4] >= slide.y and e[3] <= slide.x+(slide.w-1) and e[4] <= slide.y+(slide.h-1)) or (selShape and x1 >= selShape.x1-1 and y1 >= selShape.y1-1 and x1 <= selShape.x2+1 and y1 <= selShape.y2+1) or drag) then
			if e[1] == "mouse_drag" then
				local x,y = e[3]-(slide.x-1),e[4]-(slide.y-1)
				if drag then
					drag.shape.ox1 = nil
					drag.shape.oy1 = nil
					drag.shape.ox2 = nil
					drag.shape.oy2 = nil
					if drag[1] == "resize" then
						if drag.x == 1 then
							drag.shape.x1 = x+1
						elseif drag.x == 2 then
							drag.shape.x2 = x-1
						end
						if drag.y == 1 then
							drag.shape.y1 = y+1
						elseif drag.y == 2 then
							drag.shape.y2 = y-1
						end
					elseif drag[1] == "move" then
						drag.shape.x1 = drag.shape.x1 + (e[3]-drag.xs)
						drag.shape.x2 = drag.shape.x2 + (e[3]-drag.xs)
						drag.shape.y1 = drag.shape.y1 + (e[4]-drag.ys)
						drag.shape.y2 = drag.shape.y2 + (e[4]-drag.ys)
						drag.xs,drag.ys = e[3],e[4]
					end
				end
			elseif e[1] == "mouse_up" then
				drag = nil
			elseif e[1] == "mouse_click" then
				--shape.rect(e[3]-(slide.x-1),e[4]-(slide.y-1),(e[3]-(slide.x-1))+5,(e[4]-(slide.y-1))+3)
				local STOP = false
				if selShape or not (e[3] >= slide.x and e[4] >= slide.y and e[3] <= slide.x+(slide.w-1) and e[4] <= slide.y+(slide.h-1)) then
					STOP = true
				end
				drag = nil
				if selShape then
					if y1 == selShape.y1-1 then
						if x1 == selShape.x1-1 then
							drag = {"resize",x=1,y=1,shape=selShape}
						elseif x1 == selShape.x2+1 then
							drag = {"resize",x=2,y=1,shape=selShape}
						elseif x1 == math.floor((selShape.x2+(selShape.x1))/2) then
							drag = {"resize",x=0,y=1,shape=selShape}
						end
					elseif y1 == selShape.y2+1 then
						if x1 == selShape.x1-1 then
							drag = {"resize",x=1,y=2,shape=selShape}
						elseif x1 == selShape.x2+1 then
							drag = {"resize",x=2,y=2,shape=selShape}
						elseif x1 == math.floor((selShape.x2+(selShape.x1))/2) then
							drag = {"resize",x=0,y=2,shape=selShape}
						end
					elseif y1 == math.floor((selShape.y2+(selShape.y1))/2) then
						if x1 == selShape.x1-1 then
							drag = {"resize",x=1,y=0,shape=selShape}
						elseif x1 == selShape.x2+1 then
							drag = {"resize",x=2,y=0,shape=selShape}
						end
					end
					if not drag and x1 >= selShape.x1-1 and y1 >= selShape.y1-1 and x1 <= selShape.x2+1 and y1 <= selShape.y2+1 then
						if selShape.type == "rect" or not (x1 >= selShape.x1 and y1 >= selShape.y1 and x1 <= selShape.x2 and y1 <= selShape.y2) then
							drag = {"move",xs=e[3],ys=e[4],shape=selShape}
						elseif selShape.type == "text" then
							selShape.input = true
						end
					end
				end
				if not drag and shape.current == "None" then
					--selShape.input = false
					selShape = nil
					for o=1,#slide.objs do
						if x1 >= slide.objs[o].x1 and y1 >= slide.objs[o].y1 and x2 <= slide.objs[o].x2 and y2 <= slide.objs[o].y2 then
							--slide.objs[o].color = colors.red
							selShape = slide.objs[o]
						end
					end
				end
				if shape.current == "None" then
					STOP = true
				end
				--dragging = selShape
				while not selShape and not STOP do
					e2 = {os.pullEvent()}
					if e2[1] == "mouse_up" and e2[3] == e[3] and e2[4] == e[4] then
						if shape.current == "Rectangle" then
							selShape = shape.rect(e[3]-(slide.x-1),e[4]-(slide.y-1),(e[3]-(slide.x-1))+6,(e[4]-(slide.y-1))+4,sCol[1],sCol[2])
						elseif shape.current == "Text" then
							selShape = shape.text("",e[3]-(slide.x-1),e[4]-(slide.y-1),(e[3]-(slide.x-1))+8,(e[4]-(slide.y-1)),0,0,sCol[3])
						elseif shape.current == "Window" then
							selShape = shape.window(e[3]-(slide.x-1),e[4]-(slide.y-1),(e[3]-(slide.x-1))+6,(e[4]-(slide.y-1))+4)
						elseif shape.current == "Input Box" then
							selShape = shape.input(e[3]-(slide.x-1),e[4]-(slide.y-1),(e[3]-(slide.x-1))+8,(e[4]-(slide.y-1)),sCol[1],0,sCol[3])
						end
						shape.current = "None"
						break
					elseif e2[1] == "mouse_up" then
						x2 = e2[3]-(slide.x-1)
						y2 = e2[4]-(slide.y-1)
						local tx1,ty1,tx2,ty2 = x1,y1,x2,y2
						if tx1 > tx2 then
							tx1,tx2 = tx2,tx1
						end
						if ty1 > ty2 then
							ty1,ty2 = ty2,ty1
						end
						if shape.current == "Rectangle" then
							selShape = shape.rect(tx1,ty1,tx2,ty2)
						elseif shape.current == "Text" then
							selShape = shape.text("",tx1,ty1,tx2,ty2)
						elseif shape.current == "Window" then
							selShape = shape.window(tx1,ty1,tx2,ty2)
						elseif shape.current == "Input Box" then
							selShape = shape.input(tx1,ty1,tx2,ty2)
						end
						shape.current = "None"
						break
					elseif e2[1] == "mouse_drag" then
						x2 = e2[3]-(slide.x-1)
						y2 = e2[4]-(slide.y-1)
					end
					OGwin.render()
					if shape.current == "Rectangle" then
						term.setBackgroundColor(colors.lightBlue)
						lOS.boxClear((slide.x-1)+x1,(slide.y-1)+y1,(slide.x-1)+x2,(slide.y-1)+y2)
					else
						if shape.current == "Window" then
							term.setTextColor(colors.red)
						else
							term.setTextColor(colors.lightGray)
						end
						local tx1,ty1,tx2,ty2 = (slide.x-1)+x1,(slide.y-1)+y1,(slide.x-1)+x2,(slide.y-1)+y2
						if tx1 > tx2 then
							tx1,tx2 = tx2,tx1
						end
						if ty1 > ty2 then
							ty1,ty2 = ty2,ty1
						end
						lUtils.border(tx1,ty1,tx2,ty2,"transparent")
					end
				end
			else
				--term.setBackgroundColor(colors.lightGray)
				--term.setTextColor(colors.gray)
				--term.setCursorPos(1,1)
				--print(table.concat(e,", ").."\n"..slide.x..", "..slide.y..", "..slide.w..", "..slide.h)
				--os.sleep(0.1)
			end
		end

		-- END OF TEMP TESTING



		if menu.top.vis and e[4] <= menu.top.h then
			if e[4] == 1 and e[1] == "mouse_up" then
				for c=1,#menu.top.tabs do
					local tab = menu.top.tabs[c]
					if e[3] >= tab.x and e[3] <= tab.x+(tab.w-1) then
						if tab[1] == "File" then
							local recent = lUtils.asset.load("AppData/Shapescape/recent.txt") or {}
							local dis = false
							if #recent == 0 then
								dis = true
							end
							--local a = {lUtils.oclickmenu(tab.x-1,2,15,{{"New","Open...","Open recent >","Save","Save as..."},{"Test"},{"Export"},{"Quit"}},nil,dis)}
							local opt = {{"New","Open...",{txt="Open recent",action=recent,disabled=dis},"Save","Save as..."},{"Test"},{"Export"},{"Quit"}}
							local clck = lOS.contextmenu(tab.x-1,2,15,opt,themeCol,true)
							if clck then
								if clck.parent ~= opt then
									if #recent > 0 then
										if fs.exists(clck.txt) then
											loadScape(clck.txt)
										end
									end
								elseif clck.txt == "Open..." then
									local tempFile = lUtils.explorer("User/Documents","SelFile false")
									if tempFile and fs.exists(tempFile) then
										loadScape(tempFile)
									end
								elseif clck.txt == "Save" or clck.txt == "Save as..." then
									if not sPath or clck.txt == "Save as..." then
										local b = {lUtils.inputbox("Shapescape","Please enter a filepath:",27,11,{"Save","Cancel"})}
										if b[2] == true and b[4] ~= "Cancel" then
											if lUtils.getFileType(b[1]) == "" then
												b[1] = b[1]..".sgui"
											end
											if fs.exists(b[1]) then
												local c = {lUtils.popup("Shapescape","This file already exists! Do you want to overwrite it?",27,10,{"Overwrite","Cancel"})}
												if c[1] == true and c[3] == "Overwrite" then
													sPath = b[1]
													if save(sPath) then
														bottomMsg = fs.getName(sPath).." saved."
													end
												end
											else
												sPath = b[1]
												if save(sPath) then
													bottomMsg = fs.getName(sPath).." saved."
												end
											end

										end
									else
										if save(sPath) then
											bottomMsg = fs.getName(sPath).." saved."
										end
									end
								elseif clck.txt == "Test" then
									local p = fs.combine(pDir,"test_"..lUtils.randStr(5)..".lua")
									if fs.exists(p) then fs.delete(p) end
									save(p)
									lOS.execute(p)
									--os.sleep(1)
									--fs.delete(p)
								end
							end
						else
							menu.top.cTab = tab
						end
					end
				end
			end
			for c=1,#menu.top.objs do
				for o=1,#menu.top.objs[c] do
					local obj = menu.top.objs[c][o]
					if e[3] >= obj.x and e[4] >= obj.y and e[3] <= obj.x+(obj.w-1) and e[4] <= obj.y+(obj.h-1) and not (obj.req and not obj.req(obj,menu.top.objs[c])) then
						if e[1] == "mouse_click" then
							obj.selected = true
						elseif obj.func then
							obj.func(obj,menu.top.objs[c])
						end
					end
					if e[1] == "mouse_up" then
						obj.selected = false
					end
				end
			end
		end
	elseif e[1] == "mouse_scroll" then
		if menu.left.vis and e[3] <= menu.left.w then
			if menu.left.scroll+e[2] >= 0 then
				menu.left.scroll = menu.left.scroll+e[2]
			end
		end
	elseif e[1] == "key" and lUtils.isHolding(keys.leftCtrl) then
		if e[2] == keys.right then
			slide.w = slide.w+1
		elseif e[2] == keys.left then
			slide.w = slide.w-1
		elseif e[2] == keys.up then
			slide.h = slide.h-1
		elseif e[2] == keys.down then
			slide.h = slide.h+1
		end
		slide.win.reposition(slide.x,slide.y,slide.w,slide.h)
	elseif e[1] == "key" then
		if e[2] == keys.left then
			if (menu.left.w == 6 or lUtils.isHolding(keys.leftShift)) and menu.left.vis then
				menu.left.vis = false
			elseif menu.left.vis then
				menu.left.w = menu.left.w - 1
			end
		elseif e[2] == keys.right then
			if menu.left.vis then
				menu.left.w = menu.left.w + 1
			else
				menu.left.vis = true
			end
		elseif e[2] == keys.tab then
			if menu.top.vis == true then
				menu.top.vis = false
				menu.bottom.vis = false
			else
				menu.top.vis = true
				menu.bottom.vis = true
			end
		elseif e[2] == keys.down then
			if slides[slide.c+1] ~= nil then
				selShape = nil
				slide = slides[slide.c+1]
			end
		elseif e[2] == keys.up then
			if slides[slide.c-1] ~= nil then
				selShape = nil
				slide = slides[slide.c-1]
			end
		--elseif e[2] == keys.one then
			--slide = slides[1]
		--elseif e[2] == keys.two then
			--slide = slides[2]
		--elseif e[2] == keys.three then
			--slide = slides[3]
		elseif e[2] == keys.delete then
			if selShape then
				for t=1,#slide.objs do
					if selShape == slide.objs[t] then
						selShape.input = false
						table.remove(slide.objs,t)
						break
					end
				end
				selShape = nil
			elseif #slides > 1 then
				table.remove(slides,slide.c) -- CHANGE THIS LATER TO ONLY BEING POSSIBLE AFTER YOU FIRST CLICKED THE SLIDE
				slide = slides[slide.c] or slides[slide.c-1]
				refreshC()
			end
		end
	end
	if menu.top.cTab.objs then
		menu.top.objs = menu.top.cTab.objs
	end
end