
os.cpu(333)

dofile("APP/paint/entorno.lua")


savex=0
savey=0


c1=rojo
c2=verde

tamaño=1

herramienta="grect"

img=image.create(480,272)
radio=5
nothing=true
start_p=false
almax=0
almay=0
zoom=1
first=nil

mx=0
my=0



croma={x=380,y=22,w=100,h=100,i="",e=true}
bh={x=0,y=22,w=120,h=250,i="",e=true}
super={x=0,y=0,w=480,h=22}

pestañas={}
pestañas[1]={x=0,y=0,w=78,h=22}
pestañas[2]={x=95,y=0,w=86,h=22}
pestañas[3]={x=199,y=0,w=68,h=22}
pestañas[4]={x=289,y=0,w=78,h=22}
pestañas[5]={x=387,y=0,w=78,h=22}

ico={}

speed=4


function funciones(a)
	if a=="Nuevo" then
		while #paint!=0 do
			table.remove(paint)
		end
		img=image.create(480,272)
	end
	if a=="Abrir" then
		explorador("ms0:/")
	end
	if a=="Guardar" then
		aco="save"
	end
	if a=="Cerrar" then
		sistema("PlutuX",true)
	end
	if a=="Deshacer" then
		deshacer()
	end
	if a=="Rehacer" then
		rehacer()
	end
	if a=="Copiar" then
		aco="copy"
	end
	if a=="Pegar" then
		herramienta="posimg"
		table.insert(paint,{x=p.x,y=p.y,e=true,t="img"})
	end
	if a=="Redimensionar" then
		w=os.osk("Ancho","",3,1,1)
		h=os.osk("Alto","",3,1,1)
		if not w=="" and not h=="" then
			w,h=tonumber(w),tonumber(h)
			if w>480 then w=480 end
			if h>272 then h=272 end
			aco="redi"
		end
	end
	if a=="Voltear verticalmente" then
		aco="mirrorv"
	end
	if a=="Voltear horizontalmente" then
		aco="mirrorh"
	end
	if a=="Girar 90º (Derecha)" then
		aco="90"
	end
	if a=="Girar 90º (Izquierda)" then
		aco="-90"
	end
	if a=="Acoplar" then
		aco=""
	end
	if a=="Blanco & Negro" then
		aco="1"
	end
	if a=="Sepia" then
		aco="2"
	end
	if a=="Invertir colores" then
		aco="3"
	end
	if a=="Luminosidad" then
		aco="4"
	end
	if a=="Valor R" then
		aco="5"
	end
	if a=="Valor G" then
		aco="6"
	end
	if a=="Valor B" then
		aco="7"
	end
	if a=="Tamaño TXT" then
		tamaño=os.osk("","",1,1,1)
		if tamaño!="" then
			tamaño=tonumber(tamaño)
			if tamaño>3 then tamaño=3 end
		else
			tamaño=1
		end
	end
	if a=="Brillo" then
		brillo=os.osk("","",2,1,1)
		if not brillo=="" then
			brillo=tonumber(brillo)
			if brillo>90 then brillo=90 end
			screen.brightness(brillo)
		end
	end	
	if a=="Velocidad cursor" then
		speed=os.osk("","",1,1,1)
		if speed!="" then
			speed=tonumber(speed)
			if speed>4 then speed=4 end
		else
			speed=3
		end
	end
	if a=="Herramientas" then
		if bh.e then bh.e=false else bh.e=true end
	end	
	if a=="Colores" then
		if croma.e then croma.e=false else croma.e=true end
	end
end


function barrah()
for i=1,3 do
ico[i]={x=bh.x+36*i-24,y=bh.y+34,w=22,h=22}
end
for i=1,3 do
ico[i+3]={x=bh.x+36*i-24,y=bh.y+67,w=22,h=22}
end
for i=1,3 do
ico[i+6]={x=bh.x+36*i-24,y=bh.y+103,w=22,h=22}
end
for i=1,3 do
ico[i+9]={x=bh.x+36*i-24,y=bh.y+132,w=22,h=22}
end
for i=1,3 do
ico[i+12]={x=bh.x+36*i-24,y=bh.y+163,w=22,h=22}
end
for i=1,3 do
ico[i+15]={x=bh.x+36*i-24,y=bh.y+195,w=22,h=22}
end
for i=1,3 do
ico[i+18]={x=bh.x+36*i-24,y=bh.y+226,w=22,h=22}
end
ico[15].w=25
ico[15].h=18
end

function draw.circle(x,y,r,color)
	local x0, y0 = r, 0
		i=0
		while i<89 do
			local x1,y1 = r*math.cos( math.rad( i )), r*math.sin(math.rad( i ));
			draw.line(x+x1,y+y1,x+x0,y+y0,color);
			draw.line(x+x0,y-y0,x+x1,y-y1,color);
			draw.line(x-x0,y+y0,x-x1,y+y1,color);
			draw.line(x-x0,y-y0,x-x1,y-y1,color);
			x0, y0 = x1, y1;
			i=i+1
		end
	
end



function efectos_imagen(tipo)
	save=image.create(480,272,0)
	img:blit(0,0)
	screen.print(240,30,"Cargando... Por favor, espere.",0.7,blanco,verde,"center")
	screen.flip()
	for y=0,272 do
		for x=0,480 do
			save:pixel(x,y,img:pixel(x,y))
		end
	end
	if tipo==1 then
		for y = 0,272 do
			for x = 0,480 do
				local blend = math.floor((color.R(image.pixel(img, x, y))+color.G(image.pixel(img, x, y))+color.B(image.pixel(img, x, y)))/2)
				image.pixel(img, x, y, color.new(blend, blend, blend, color.A(image.pixel(img, x, y))))
				if x==480 then
					if y%2==0 then
						img:blit(0,0)
						screen.flip()
					end
				end
			end
		end
	elseif tipo==2 then
		for y = 0,272 do
			for x = 0,480 do
				local blend = math.floor((color.R(image.pixel(img, x, y))+color.G(image.pixel(img, x, y))+color.B(image.pixel(img, x, y)))/2)
				image.pixel(img, x, y, color.mix(color.new(blend, blend, blend, color.A(image.pixel(img, x, y))), color.new(112, 66, 20, color.A(image.pixel(img, x, y))), 100, 65))
				if x==480 then
					if y%2==0 then
						img:blit(0,0)
						screen.flip()
					end
				end
			end
		end
	elseif tipo==3 then
		for y = 0,272 do
			for x = 0,480 do
				image.pixel(img, x, y, color.new(255-color.R(image.pixel(img, x, y)), 255-color.G(image.pixel(img, x, y)), 255-color.B(image.pixel(img, x, y)), color.A(image.pixel(img, x, y))))
				if x==480 then
					if y%2==0 then
						img:blit(0,0)
						screen.flip()
					end
				end
			end
		end
	elseif tipo==4 then
		valor=os.osk("Porcentaje de luminosidad","",3,1,1)
		if valor=="" then valor=200 end
		valor=tonumber(valor/100)
		for y=0,272 do
			for x=0,480 do
				img:pixel(x,y,color.new(color.R(img:pixel(x,y))*valor,color.G(img:pixel(x,y))*valor,color.B(img:pixel(x,y))*valor))
				if x==480 then
					if y%2==0 then
						img:blit(0,0)
						screen.flip()
					end
				end
			end
		end
	elseif tipo==5 then
		valor=os.osk("Porcentaje de rojo","",3,1,1)
		if valor=="" then valor=200 end
		valor=tonumber(valor/100)
		for y=0,272 do
			for x=0,480 do
				img:pixel(x,y,color.new(color.R(img:pixel(x,y))*valor,color.G(img:pixel(x,y)),color.B(img:pixel(x,y))))
				if x==480 then
					if y%2==0 then
						img:blit(0,0)
						screen.flip()
					end
				end
			end
		end
	elseif tipo==6 then
		valor=os.osk("Porcentaje de verde","",3,1,1)
		if valor=="" then valor=200 end
		valor=tonumber(valor/100)
		for y=0,272 do
			for x=0,480 do
				img:pixel(x,y,color.new(color.R(img:pixel(x,y)),color.G(img:pixel(x,y))*valor,color.B(img:pixel(x,y))))
				if x==480 then
					if y%2==0 then
						img:blit(0,0)
						screen.flip()
					end
				end
			end
		end
	else
		valor=os.osk("Porcentaje de azul","",3,1,1)
		if valor=="" then valor=200 end
		valor=tonumber(valor/100)
		for y=0,272 do
			for x=0,480 do
				img:pixel(x,y,color.new(color.R(img:pixel(x,y))*valor,color.G(img:pixel(x,y)),color.B(img:pixel(x,y))*valor))
				if x==480 then
					if y%2==0 then
						img:blit(0,0)
						screen.flip()
					end
				end
			end
		end
	end
	respuesta=os.message("¿Deseas mantener la imagen así?",1)
	if respuesta!=1 then
		for y = 272,0,-1 do
			for x = 0,480 do
				img:pixel(x,y,save:pixel(x,y))
				if x==480 then
					if y%2==0 then
						img:blit(0,0)
						screen.flip()
					end
				end
			end
		end
	end

end



selec={e=false,x=0,y=0,w=0,h=0,i=nil}


function accionar(ix,iy,name,vez)
	if vez then
		if name=="rect" then
			table.insert(paint,{x=ix,y=iy,w=p.x-ix,h=p.y-iy,cc=c1,e=true,t=name})
		end
		if name=="line" then
			table.insert(paint,{x=ix,y=iy,w=p.x,h=p.y,c=c1,e=true,t=name})
		end
		if name=="fill" then
			table.insert(paint,{x=ix,y=iy,w=p.x-ix,h=p.y-iy,c=c1,e=true,t=name})
		end
		if name=="frect" then
			table.insert(paint,{x=ix,y=iy,w=p.x-ix,h=p.y-iy,c=c1,cc=c2,e=true,t=name})
		end
		if name=="dibu" or name=="pince" then
			table.insert(paint,{i=image.create(480,272),e=true,t=name})
		end
		if name=="grect" then
			table.insert(paint,{x=ix,y=iy,w=p.x-ix,h=p.y-iy,c=c1,cc=c2,e=true,t=name})
		end
		if name=="sele" then
			selec.e=true
			selec.x=ix
			selec.y=iy
			selec.w=p.x-ix
			selec.h=p.y-iy
		end
		if name=="circ" then
			table.insert(paint,{x=ix,y=iy,r=radio,c=c1,e=true,t=name})
		end
		if name=="txt" then
			table.insert(paint,{x=p.x,y=p.y,n=os.osk("","",30,1,0),tam=tamaño,c=c1,e=true,t=name})
		end
	else
		if name=="rect" or name=="fill" or name=="grect" or name=="frect" then
			paint[#paint].w=p.x-paint[#paint].x
			paint[#paint].h=p.y-paint[#paint].y
		end
		if name=="line" then
			paint[#paint].w=p.x
			paint[#paint].h=p.y
		end
		if name=="dibu" then
			paint[#paint].i:pixel(p.x,p.y,c1)
		end
		if name=="pince" then
			paint[#paint].i:pixel(p.x,p.y,c1)
			paint[#paint].i:pixel(p.x+1,p.y,c1)
			paint[#paint].i:pixel(p.x-1,p.y,c1)
			paint[#paint].i:pixel(p.x,p.y+1,c1)
			paint[#paint].i:pixel(p.x,p.y-1,c1)
		end
		if name=="sele" then
			selec.w=p.x-selec.x
			selec.h=p.y-selec.y
		end
	end
end

function rehacer()
if #paint>0 then
i=1
while paint[i].e do
i=i+1
if i==#paint then break end
end
paint[i].e=true
end
end

function deshacer()

if #paint>0 then
	i=#paint
	while paint[i].e==false do
		i=i-1
		if i==0 then i=1 break end
	end
	paint[i].e=false
end
end


function acoplar(remo)
if first==nil then first=true end
screen.flip()
img=screen.toimage()
while #paint>0 do
	table.remove(paint)
end
end


function plutuX()
actual=0
paint={}
liberar(12)


while true do
controls.read()
screen.clear()

if first==nil then imgini:blit(0,0) end

if not controls.cross() then start_p=false end

barrah()

nothing=true
if croma.e and col(p,croma) then nothing=false end
if bh.e and col(p,bh) then nothing=false end
if col(p,super) then nothing=false end
if tareas_p then nothing=false end

if controls.press("l") then deshacer() end
if controls.press("r") then rehacer() end



if nothing then
	if herramienta=="sele" then
		if controls.press("circle") then selec.e=false end
	end
	if herramienta=="posimg" and paint[#paint].t=="img" then
		paint[#paint].x=p.x
		paint[#paint].y=p.y
		if controls.press("cross") then
			aco=""
			herramienta=""
		end
	end

	if herramienta=="borra" then
		if controls.cross() then
			for i=1,#paint do
				if paint[i].x!=nil then
					x=p.x-paint[i].x
					y=p.y-paint[i].y
					if x>-20 and x>20 and y>-20 and y<20 then table.remove(paint,i) break end
				end
			end
		end
	end
	if herramienta=="colo" then
		if controls.press("cross") then c1=a:pixel(p.x,p.y) end
		if controls.press("triangle") then c2=a:pixel(p.x,p.y) end
	else
		if controls.press("cross") then
			savex=p.x
			savey=p.y
			accionar(savex,savey,herramienta,true)
			start_p=true
		end
		if controls.cross() and start_p then
			accionar(savex,savey,herramienta)
		end
	end
end

if herramienta!="move" then

if controls.down() then p.y=p.y+speed end
if controls.up() then p.y=p.y-speed end
if controls.left() then p.x=p.x-speed end
if controls.right() then p.x=p.x+speed end

if p.x>479 then p.x=479 elseif p.x<0 then p.x=0 end
if p.y>271 then p.y=271 elseif p.y<0 then p.y=0 end

else

if controls.down() then my=my+speed end
if controls.up() then my=my-speed end
if controls.left() then mx=mx-speed end
if controls.right() then mx=mx+speed end

if controls.press("cross") or controls.press("circle") then herramienta="" end
end

if first then img:blit(mx,my) end

for i=1,#paint do
	if paint[i].e then
		if paint[i].t=="fill" or paint[i].t=="frect"  then draw.fillrect(paint[i].x,paint[i].y,paint[i].w,paint[i].h,paint[i].c) end
		if paint[i].t=="rect" or paint[i].t=="frect" then draw.rect(paint[i].x,paint[i].y,paint[i].w,paint[i].h,paint[i].cc) end
		if paint[i].t=="grect" then draw.gradrect(paint[i].x,paint[i].y,paint[i].w,paint[i].h,paint[i].c,paint[i].cc,paint[i].cc,paint[i].c) end
		if paint[i].t=="line" then draw.line(paint[i].x,paint[i].y,paint[i].w,paint[i].h,paint[i].c) end
		if paint[i].t=="dibu" or paint[i].t=="pince" then paint[i].i:blit(0,0) end
		if paint[i].t=="circ" then draw.circle(paint[i].x,paint[i].y,paint[i].r,paint[i].c) end
		if paint[i].t=="txt" then screen.print(paint[i].x,paint[i].y,paint[i].n,paint[i].tam,paint[i].c,0,"center") end
		if paint[i].t=="img" then selec.i:blit(paint[i].x,paint[i].y) end
	end
end

if controls.press("select") then acoplar() end
if aco!=nil then
	acoplar()
	if aco=="mirrorh" then img:mirrorh() 
	elseif aco=="mirrorv" then img:mirrorv()
	elseif aco=="90" then img:rotate(90)
	elseif aco=="-90" then img:rotate(-90)
	elseif aco=="redi" then img:resize(w,h)
	elseif aco=="redi" then img:resize(w,h)
	elseif aco=="1" then efectos_imagen(1)
	elseif aco=="2" then efectos_imagen(2)
	elseif aco=="3" then efectos_imagen(3)
	elseif aco=="4" then efectos_imagen(4)
	elseif aco=="5" then efectos_imagen(5)
	elseif aco=="6" then efectos_imagen(6)
	elseif aco=="7" then efectos_imagen(7)
	elseif aco=="copy" then
		if selec.w<0 then selec.x=selec.x+selec.w selec.w=0-selec.w end
		if selec.h<0 then selec.y=selec.y+selec.h selec.h=0-selec.h end
		selec.i=image.create(selec.w,selec.h,blanco)
		for x=0,selec.w do
			for y=0,selec.h do
				selec.i:pixel(x,y,img:pixel(selec.x+x,selec.y+y))
			end
		end
	end
end
if aco=="save" then image.save(img,trabajo..os.osk("Nombre para la imagen","",20,1,0)..".png") end
if aco!="mirrorh" then aco=nil else aco="" end



if selec.e then
	draw.fillrect(selec.x,selec.y,selec.w,selec.h,selec_c)
	draw.rect(selec.x,selec.y,selec.w,selec.h,negro)
end

entorno()

mouse:blit(p.x,p.y)
if herramienta=="borra" then draw.rect(p.x-10,p.y-10,10,10,blanco) end

if herramienta=="circ" then 
	if controls.triangle() then radio=radio+0.1 elseif controls.square() then radio=radio-0.1 end
	draw.circle(p.x,p.y,radio,blanco)
end

if controls.press("start") then sistema("PlutuX") end

if herramienta=="colo" and nothing then 
	if almax!=bh.x or almay!=bh.y then
		a=screen.toimage() 
	end
almax=bh.x
almay=bh.y
end

screen.flip()
end
end

