if mwm == nil then _G.mwm = {} end
mwm.color = colors.white -- current color
mwm.rCvs = {} -- real canvas
mwm.orCvs = {}
mwm.oCvs = {} -- objects canvas
function genCvs()
	mwm.rCvs = {}
	local tX,tY = term.getSize()
	for x=1,tX do
		mwm.rCvs[x] = {}
		for y=1,tY do
			mwm.rCvs[x][y] = {"",term.getBackgroundColor()} -- empty pixel = ""
		end
	end
end
function tabletext(text)
	local tTable = {}
	for a=1,string.len(text) do
		tTable[a] = string.sub(text,a,a)
	end
	return tTable
end
function mwm.setColor(color)
	mwm.color = color
end
function mwm.rect(x,y,w,h)
	if tonumber(x) == nil or tonumber(y) == nil or tonumber(w) == nil or tonumber(h) == nil then
		return
	end
	mwm.oCvs[#mwm.oCvs+1] = {type="rect",x=x,y=y,width=w,height=h,color=mwm.color}
	return #mwm.oCvs,mwm.oCvs[#mwm.oCvs],true
end
function mwm.print(text,x,y)
	if text == nil or tonumber(x) == nil or tonumber(y) == nil then
		return
	end
	mwm.oCvs[#mwm.oCvs+1] = {type="text",txt=text,x=x,y=y,color=mwm.color}
	return #mwm.oCvs,mwm.oCvs[#mwm.oCvs],true
end
function mwm.sprite(file,x,y,w,h)
	if fs.exists(file) == false then
		return
	end
	opensprite = fs.open(file,"r")
	local sprite = opensprite.readAll()
	opensprite.close()
	sprite = textutils.unserialize(sprite)
	if sprite == nil then return end
	mwm.oCvs[#mwm.oCvs+1] = {type="sprite",texture=sprite,x=x,y=y,width=w,height=h}
	return #mwm.oCvs,mwm.oCvs[#mwm.oCvs]
end
-- Support circle rendering. This can be done by calculating with math.cos how many pixels it is from one side of one y coord to the other side of the same y coord and putting that in a string. repeat this for every y coord then draw
function mwm.clear()
	mwm.oCvs = {}
end
function oldrender()
	genCvs()
	for a=1,#mwm.oCvs do
		local o = mwm.oCvs[a]
		if o[1] == "rect" then
			term.setBackgroundColor(o[6])
			for b=1,o[5] do
				local dObj = ""
				for c=1,o[4] do
					dObj = dObj.." "
					local tX,tY = term.getSize()
					if o[2]+(c-1) <= tX and o[3]+(b-1) <= tY then
						mwm.rCvs[o[2]+(c-1)][o[3]+(b-1)] = {" ",o[6]}
					end
				end
				term.setCursorPos(o[2],o[3]+(b-1))
				term.write(dObj)
			end
		elseif o[1] == "text" then
			dObj = tabletext(o[2])
			for b=1,#dObj do
				mwm.rCvs[o[3]+(b-1)][o[4]][1] = dObj[b]
				mwm.rCvs[o[3]+(b-1)][o[4]][3] = o[5]
			end
		end
		coroutine.yield()
	end
	for x=1,#mwm.rCvs do
		for y=1,#mwm.rCvs[x] do
			if mwm.rCvs[x][y][1] == "" then
				term.setCursorPos(x,y)
				term.setBackgroundColor(mwm.rCvs[x][y][2])
				term.write(" ")
			elseif mwm.rCvs[x][y][1] ~= " " then
				term.setCursorPos(x,y)
				term.setBackgroundColor(mwm.rCvs[x][y][2])
				term.setTextColor(mwm.rCvs[x][y][3])
				term.write(tostring(mwm.rCvs[x][y][1]))
			end
		end
		coroutine.yield()
	end
end
function mwm.render2()
	genCvs()
	bgcolor = term.getBackgroundColor()
	for a=1,#mwm.oCvs do
		local o = mwm.oCvs[a]
		o.x = math.floor(o.x+0.5)
		o.y = math.floor(o.y+0.5)
		if o.type == "rect" then
			for b=1,o.height do
				for c=1,o.width do
					local tX,tY = term.getSize()
					if o.x+(c-1) <= tX and o.y+(b-1) <= tY and o.x+(c-1) > 0 and o.y+(b-1) > 0 then
						mwm.rCvs[o.x+(c-1)][o.y+(b-1)] = {" ",o.color}
					end
				end
			end
		elseif o.type == "sprite" then
			for b=1,o.height do
				for c=1,o.width do
					if o.texture[c] ~= nil then
						local tX,tY = term.getSize()
						if o.x+(c-1) <= tX and o.y+(b-1) <= tY and o.x+(c-1) > 0 and o.y+(b-1) > 0 and o.texture[c][b] ~= nil then
							mwm.rCvs[o.x+(c-1)][o.y+(b-1)] = o.texture[c][b]
						end
					end
				end
			end
		elseif o.type == "text" then
			dObj = tabletext(o.txt)
			for b=1,#dObj do
				local tX,tY = term.getSize()
				if o.x+(b-1) <= tX and o.y <= tY and o.x+(b-1) > 0 and o.y > 0 then
					mwm.rCvs[o.x+(b-1)][o.y][1] = dObj[b]
					mwm.rCvs[o.x+(b-1)][o.y][3] = o.color
				end
			end
		end
	end
	for x=1,#mwm.rCvs do
		for y=1,#mwm.rCvs[x] do
			if mwm.rCvs[x][y][1] == "" then
				term.setCursorPos(x,y)
				term.setBackgroundColor(mwm.rCvs[x][y][2])
				term.write(" ")
			else
				term.setCursorPos(x,y)
				term.setBackgroundColor(mwm.rCvs[x][y][2])
				if mwm.rCvs[x][y][3] ~= nil then
					term.setTextColor(mwm.rCvs[x][y][3])
				end
				term.write(tostring(mwm.rCvs[x][y][1]))
			end
		end
	end
	term.setBackgroundColor(bgcolor)
end
function mwm.render()
	genCvs()
	bgcolor = term.getBackgroundColor()
	for a=1,#mwm.oCvs do
		local o = mwm.oCvs[a]
		o.x = math.floor(o.x+0.5)
		o.y = math.floor(o.y+0.5)
		if o.type == "rect" then
			for b=1,o.height do
				for c=1,o.width do
					local tX,tY = term.getSize()
					if o.x+(c-1) <= tX and o.y+(b-1) <= tY and o.x+(c-1) > 0 and o.y+(b-1) > 0 then
						mwm.rCvs[o.x+(c-1)][o.y+(b-1)] = {" ",o.color}
					end
				end
			end
		elseif o.type == "sprite" then
			for b=1,o.height do
				for c=1,o.width do
					if o.texture[c] ~= nil then
						local tX,tY = term.getSize()
						if o.x+(c-1) <= tX and o.y+(b-1) <= tY and o.x+(c-1) > 0 and o.y+(b-1) > 0 and o.texture[c][b] ~= nil then
							mwm.rCvs[o.x+(c-1)][o.y+(b-1)] = o.texture[c][b]
						end
					end
				end
			end
		elseif o.type == "text" then
			dObj = tabletext(o.txt)
			for b=1,#dObj do
				local tX,tY = term.getSize()
				if o.x+(b-1) <= tX and o.y <= tY and o.x+(b-1) > 0 and o.y > 0 then
					mwm.rCvs[o.x+(b-1)][o.y][1] = dObj[b]
					mwm.rCvs[o.x+(b-1)][o.y][3] = o.color
				end
			end
		end
	end
	for x=1,#mwm.rCvs do
		for y=1,#mwm.rCvs[x] do
			if not (mwm.orCvs[x] ~= nil and mwm.orCvs[x][y] == mwm.rCvs[x][y]) then
				if mwm.rCvs[x][y][1] == "" then
					term.setCursorPos(x,y)
					term.setBackgroundColor(mwm.rCvs[x][y][2])
					term.write(" ")
				else
					term.setCursorPos(x,y)
					term.setBackgroundColor(mwm.rCvs[x][y][2])
					if mwm.rCvs[x][y][3] ~= nil then
						term.setTextColor(mwm.rCvs[x][y][3])
					end
					term.write(tostring(mwm.rCvs[x][y][1]))
				end
			end
		end
	end
	mwm.orCvs = mwm.rCvs
	term.setBackgroundColor(bgcolor)
end