--CreateMapMarker(-1.916667,-2.520833,0.72,0.72,0.72)

edit_sprites={	
"spr_sein",
"spr_vsein",
"spr_sammas",
"spr_pikksein",
"spr_sakilinesein",
"spr_zombi1",
"spr_sininezombi",
"spr_szombi",
"spr_mskeleton",
"spr_s6dur",
"spr_punane_s6dur",
"spr_hall_s6dur",
"spr_stiled",
"spr_tiled",
"spr_puru",
"spr_metallprand",
"spr_itemedit",
"spr_uks",
"spr_sein",
"spr_objectedit",
"spr_kivid",
"spr_raudtee",
"spr_m88bel",
"spr_textuurid",
"spr_puud",
"spr_praod",
"spr_puustuks",
"spr_node",
"spr_portalroom",
"spr_floors",
"spr_dg_tiles",
"spr_dg_taust",
"spr_sein",
"spr_sein",
"spr_punanerobot",
"spr_srzombi",
"spr_turret",
"spr_editorkast",
"spr_node",
"spr_test_set",
}
edit_type=1
edit_mousex=0
edit_mousey=0
edit_dir=0
edit_frame=0

edit_trigfunc=""
edit_trigradius=1
edit_trigdelay=10

arcgen={};
edit_circlerad=5;
edit_randmult=0.1;
edit_circlestart=0;
edit_circleend=360;
edit_circlestep=10;

edit_makecircle=function()
  for dg=edit_circlestart,edit_circleend,edit_circlestep do
    local rand=math.random()*edit_randmult
    local rd=math.rad(dg);
    local x=edit_mousex+math.cos(rd+edit_dir)*(edit_circlerad+rand)
    local y=edit_mousey+math.sin(rd+edit_dir)*(edit_circlerad+rand)
    rand=math.random(-10,10)
    rand=rand/100.0
    CreateBlock(x,y,rd+edit_dir,edit_frame,0)
  end
end

edit_rendercircle=function()
  for dg=edit_circlestart,edit_circleend,edit_circlestep do
    local rand=math.random()*edit_randmult
    local rd=math.rad(dg);
    local x=math.cos(rd+edit_dir)*(12*(edit_circlerad+rand))
    local y=math.sin(rd+edit_dir)*(12*(edit_circlerad+rand))
    rand=math.random(-10,10)
    rand=rand/100.0
    
    local mx,my=GetMousePosition();
    DrawSprite("spr_sein",mx+x,my+y,rd+edit_dir,edit_frame,1,1,1,1,1,1)
  end
end


edit_rendercircle2=function()
  local radius=5
  SetTexture()
  SetColor(1,1,1,1)

  local mx,my=GetMousePosition()
  DrawCircle(mx,my,12*radius)

  local x=math.cos(edit_dir)*(12*radius)
  local y=math.sin(edit_dir)*(12*radius)
  DrawLine(mx,my,mx+x,my+y)

  radius=0.05
  DrawCircle(mx,my,12*radius)
end

edit_renderdirctrl=function()
	SetTexture()
	SetColor(1,1,1,1)
	local mx,my=GetMousePosition()
	DrawCircle(mx,my,12*edit_rad)

	local x=math.cos(edit_dir)*(12*edit_rad)
	local y=math.sin(edit_dir)*(12*edit_rad)

	DrawLine(mx,my,mx+x,my+y)
end

edit_rendertrigger=function()
  local radius=edit_trigradius
  SetTexture()
  DrawCircle(mx,my,12*radius)
  DrawText(mx,my,edit_trigfunc)
end




local zstate={}
zstate.normal=0
zstate.gun=1
zstate.lay=2

local edit_itemsframes={}

local edit_creategenericbitmap=function() 
  CreateBitmap(edit_mousex,edit_mousey,-edit_dir,edit_sprites[edit_type],edit_frame) 
end

local edit_createzombie=function()
  local bluebit=cbool[edit_sprites[edit_type]=="spr_sininezombi"]
  local laybit=cbool[edit_frame==12]
  local gunbit=0
  local statestr=laybit..bluebit..gunbit
  CreateZombie(edit_mousex,edit_mousey,statestr)
end



local edit_itemrender=function()
  local mx,my=GetMousePosition();
  DrawSprite(itemtable[edit_itemtype].sprite,mx,my,edit_dir,itemtable[edit_itemtype].frame,1,1,1,1,1,1)
end

local edit_defaultrender=function()
  local mx,my=GetMousePosition();
  DrawSprite(edit_sprites[edit_type],mx,my,edit_dir,edit_frame,1,1,1,1,1,1)
	
end

local edit_modulerender=function()
---[[
	if placedmodlist then
		SetColor(1,1,1,1)
		SetTexture(0)
		for i,v in pairs(placedmodlist) do
			gl.PushMatrix()
			gl.Scale(12,12,12)
			gl.Translate(v.x,v.y,0)
			gl.Rotate(v.a*180/3.141,0,0,1)
			gl.Begin(gl.LINE_LOOP)
			gl.Vertex(v.w/2,v.h/2)
			gl.Vertex(-v.w/2,v.h/2)
			gl.Vertex(-v.w/2,-v.h/2)
			gl.Vertex(v.w/2,-v.h/2)
			gl.End()
			gl.PopMatrix()
		end
	end --]]
	local mx,my=GetMousePosition();
	DrawSprite(edit_sprites[edit_type],mx,my,edit_dir,edit_frame,1,1,1,1,1,1)
end

local edit_objectrender=function()
  local mx,my=GetMousePosition();
  DrawSprite(objectlist[edit_objecttable[edit_frame]].sprite,mx,my,edit_dir,objectlist[edit_objecttable[edit_frame]].frame,1,1,1,1,1,1)
end

local edit_createobject=function()
  CreateObject(edit_mousex,edit_mousey,edit_dir,edit_objecttable[edit_frame])
end


local edit_createsoldier=function()
	if edit_frame==5 then
		CreateSoldier(edit_mousex,edit_mousey,"pystol")
	elseif edit_frame==8 then
		CreateSoldier(edit_mousex,edit_mousey,"automaat")
	elseif edit_frame==11 then
			CreateSoldier(edit_mousex,edit_mousey,"pump")
	elseif edit_frame==24 then
			CreateSoldier(edit_mousex,edit_mousey,"automaathv")
	elseif edit_frame==32 then
			CreateSoldier(edit_mousex,edit_mousey,"rifle")
	end
end

local edit_creatersoldier=function()
	if edit_frame==5 then
		CreateRedSoldier(edit_mousex,edit_mousey,"pystol")
	elseif edit_frame==8 then
		CreateRedSoldier(edit_mousex,edit_mousey,"automaat")
	elseif edit_frame==11 then
			CreateRedSoldier(edit_mousex,edit_mousey,"pump")
	elseif edit_frame==24 then
			CreateRedSoldier(edit_mousex,edit_mousey,"automaathv")
	elseif edit_frame==32 then
			CreateRedSoldier(edit_mousex,edit_mousey,"rifle")
	end
end
local edit_creategsoldier=function()
	if edit_frame==5 then
		CreateGreySoldier(edit_mousex,edit_mousey,"pystol")
	elseif edit_frame==8 then
		CreateGreySoldier(edit_mousex,edit_mousey,"automaat")
	elseif edit_frame==11 then
			CreateGreySoldier(edit_mousex,edit_mousey,"pump")
	elseif edit_frame==24 then
			CreateGreySoldier(edit_mousex,edit_mousey,"automaathv")
	elseif edit_frame==32 then
			CreateGreySoldier(edit_mousex,edit_mousey,"rifle")
	end
end
local edit_createtrigger=function()
  CreateTrigger(edit_mousex,edit_mousey,edit_trigradius,edit_trigfunc,edit_trigdelay)
end

local edit_createitem=function()
  CreateItem(edit_mousex,edit_mousey,edit_itemtable[edit_frame])
end

local edit_itemrender=function()
  local mx,my=GetMousePosition();
  DrawSprite(itemtable[edit_itemtable[edit_frame]].sprite,mx,my,edit_dir,itemtable[edit_itemtable[edit_frame]].frame,1,1,1,1,1,1)
end


edit_doorlength=10

edit_createdoor=function()
  for i=-edit_doorlength/2,edit_doorlength/2,1 do
    local x=edit_mousex+math.sin(-edit_dir)*i*4/12
    local y=edit_mousey+math.cos(-edit_dir)*i*4/12
    CreateBreakableBlock(x,y,edit_dir,0)
  end
end


edit_renderdoor=function()
  for i=-edit_doorlength/2,edit_doorlength/2,1 do
    local x=math.cos(edit_dir+math.pi/2)*i*4
    local y=math.sin(edit_dir+math.pi/2)*i*4
    local mx,my=GetMousePosition();
    DrawSprite("spr_puustuks",mx+x,my+y,edit_dir,edit_frame,1,1,1,1,1,1)
  end
end

edit_renderautodoor=function()
  for i=-1,1,2 do
    local x=math.cos(edit_dir)*i*6
    local y=math.sin(edit_dir)*i*6
    local mx,my=GetMousePosition();
    DrawSprite("spr_uks",mx+x,my+y,edit_dir,edit_frame,1,1,1,1,1,1)
  end
end

edit_rad=2
edit_commands={
  function() CreateBlock(edit_mousex,edit_mousey,edit_dir,edit_frame,0) end,
  function() CreateBlock(edit_mousex,edit_mousey,edit_dir,edit_frame,1) end,
  function() CreateBlock(edit_mousex,edit_mousey,edit_dir,edit_frame,2) end,
  function() CreateBlock(edit_mousex,edit_mousey,edit_dir,edit_frame,3) end,
  function() CreateBlock(edit_mousex,edit_mousey,edit_dir,edit_frame,4) end,
  edit_createzombie,
  edit_createzombie,
  function() CreateBigZombie(edit_mousex,edit_mousey) end,
  function() CreateMSkeleton(edit_mousex,edit_mousey) end,
  edit_createsoldier,
  edit_creatersoldier,
  edit_creategsoldier,
  edit_creategenericbitmap,
  edit_creategenericbitmap,
  edit_creategenericbitmap,
  edit_creategenericbitmap,
  edit_createitem,
  function() CreateDoor(edit_mousex,edit_mousey,edit_dir,edit_frame) end,
  edit_makecircle,
  edit_createobject,
  edit_creategenericbitmap,
  edit_creategenericbitmap,
  edit_creategenericbitmap,
  edit_creategenericbitmap,
  edit_creategenericbitmap,--puud
  edit_creategenericbitmap,--praod ja poriloigud
  edit_createdoor,   
  function() CreateNode(edit_mousex,edit_mousey,edit_dir,edit_frame) end,
  edit_creategenericbitmap,
  edit_creategenericbitmap,
  edit_creategenericbitmap,--spr_dg_tiles
  edit_creategenericbitmap,--spr_dg_taust
  function () CreateZSpawner(edit_mousex,edit_mousey,edit_dir,zspawncount) end,
  edit_createtrigger,
  function() CreateRedRobot(edit_mousex,edit_mousey) end,
  function() CreateBigSoldier(edit_mousex,edit_mousey) end,
  function() CreateTurret(edit_mousex,edit_mousey,edit_dir) end,
  function() 
	if edit_frame==0 then
		CreateContainer(edit_mousex,edit_mousey,edit_dir,"spr_metallkast")
	elseif edit_frame==1 then
		CreateContainer(edit_mousex,edit_mousey,edit_dir,"spr_hiteckast")
	elseif edit_frame==2 then
		CreateContainer(edit_mousex,edit_mousey,edit_dir,"spr_puitkast")
	elseif edit_frame==3 then
		CreateContainer(edit_mousex,edit_mousey,edit_dir,"spr_sinine_kapp")
	end
  end,
  function() CreateDirectionController(edit_mousex,edit_mousey,edit_dir,edit_rad) end,
  edit_creategenericbitmap,--test_set
 }
edit_commands.length=#edit_commands

edit_rendercmds={
  edit_defaultrender,     --seinad
  edit_defaultrender,	  --
  edit_defaultrender,	  --
  edit_defaultrender,	  --
  edit_defaultrender,     --
  edit_defaultrender,  	  --zombi
  edit_defaultrender,  	  --sinine zombi
  edit_defaultrender,     --suur zombi
  edit_defaultrender,     --möögaga luukere
  edit_defaultrender,     --s?dur
  edit_defaultrender,     --s?dur
  edit_defaultrender,     --s?dur
  edit_defaultrender,     --taustapraht
  edit_defaultrender,     --
  edit_defaultrender,     --
  edit_defaultrender,     --metallprand
  edit_itemrender,     --asjad
  edit_renderautodoor,	  --uksed
  edit_rendercircle,      --kaar seintest
  edit_objectrender,	  --lambi objektid
  edit_defaultrender,     --kivid
  edit_defaultrender,	  --raudtee
  edit_defaultrender,	  --Mööbel
  edit_defaultrender,	  --textuurid
  edit_defaultrender,	  --puud ja p??sad
  edit_defaultrender,	  --praod ja poriloigud
  edit_renderdoor,	  		--katkilastavad
  edit_modulerender,	  --yhenduskohad
  edit_defaultrender, 		--portalroom
  edit_defaultrender, 		--spr_floors
  edit_defaultrender,--spr_dg_tiles
  edit_defaultrender,--spr_dg_taust
  edit_rendercircle2,--spr_dg_taust
  edit_rendertrigger,--spr_dg_taust
  edit_defaultrender, 		--punanerobot
  edit_defaultrender, 		--roheline zombi
  edit_defaultrender, 		--turret
  edit_defaultrender, 		--kastid
  edit_renderdirctrl, 		--suunavärgid
  edit_defaultrender,       --testset
  
  }

  
function EditorUpdate()
	if not editormodes.textinput then
		editormodes.current=ModifyChoiceWrap(editormodes.current,"m","n",#editormodes,1)
	end
	editormodes.textinput=false
	editormodes[editormodes.current]:Update()
end

function EditorRender()
	editormodes[editormodes.current]:Render()

end

function EditorRenderGUI()
	editormodes[editormodes.current]:RenderGUI()

end



function CBoolToString(value)
	if value==0 then return "false" else return "true" end
end

function RenderScale(x,y,h,min,max,start,str,step)
	step=step or 0.05
	DrawLine(x,y,x,y+h)
	DrawLine(x,y,x+20,y)
	DrawLine(x,y+h,x+20,y+h)
	DrawText(x+25,y+4,str.."="..max)
	DrawText(x+25,y+h-4,str.."="..min)

	if input.MousePressed("wheelup") then start=start+step end
	if input.MousePressed("wheeldn") then start=start-step end
	if start>max then start=max end
	if start<min then start=min end

	local liney=(1-start/(max-min))*h+y
	DrawLine(x,liney,x+15,liney)
	
	return start
end

function CreateBitmapArray(cx,cy,radius,count,angle,x1,y1,x2,y2,texname)
	local step=math.pi*2/math.floor(count)
	for i=0,math.pi*2,step do
		local x=math.cos(i)*radius
		local y=math.sin(i)*radius
		CreateBitmap(cx+x,cy+y,-i-angle,x1,y1,x2,y2,"tex_set1")
	end
end

function CreateObjectArray(cx,cy,radius,count,angle,objtype)
	local step=math.pi*2/math.floor(count)
	for i=0,math.pi*2,step do
		local x=math.cos(i)*radius
		local y=math.sin(i)*radius
		CreateObject(cx+x,cy+y,i+angle,objtype)
	end
end


function DrawSpriteArray(cx,cy,radius,count,angle,sprite,frame)
	local step=math.pi*2/math.floor(count)
	for i=0,math.pi*2,step do
		local x=math.cos(i)*radius
		local y=math.sin(i)*radius
		DrawSprite(sprite,cx+x,cy+y,i+angle,frame,1,1)
	end
end
editormodes={
	{
		name="Sound Emitters",
	
	
		Render=function(self)
			if self.modify==1 then
				local x,y=GetMousePosition()
				RenderSelectBoxesInCircle(x/12,y/12,1)
				SetTexture()
				SetColor(1,1,1,1)

				DrawCircle(x,y,12)
			else
				local x,y=GetMousePosition()
				SetTexture()
				SetColor(1,1,1,1)

				DrawCircle(x,y,12*self.maxradius)
				DrawCircle(x,y,12*self.minradius)
			end
			
		end,

		Update=function(self)
			self.minradius=ModifyValueClamp(self.minradius,"a","z",self.maxradius,0,0.18)
			self.maxradius=ModifyValueClamp(self.maxradius,"s","x",100,self.minradius,0.18)
			self.maxvolume=ModifyValueClamp(self.maxvolume,"d","c",1,0,0.0025)
			self.sound=ModifyChoiceWrap(self.sound,"f","v",#self.soundlist,1,1)
			self.pitch=ModifyValueClamp(self.pitch,"g","b",4,0.25,0.005)

			self.modify=ModifyValueBool(self.modify,"o")
			if input.KeyPressed("p") then
				self.previewchannel=PlaySound(self.soundlist[self.sound],1)
				ChannelVolume(self.previewchannel,self.maxvolume)
				ChannelPitch(self.previewchannel,self.pitch)
			end
			if input.KeyDown("p") and self.previewchannel then
				ChannelVolume(self.previewchannel,self.maxvolume)
				ChannelPitch(self.previewchannel,self.pitch)
			end
			if input.KeyReleased("p") then
				if self.previewchannel then 
					StopChannel(self.previewchannel) 
					self.previewchannel=nil
				end
			end
			if self.modify==1 then
				if input.MousePressed("left") then
					local mousex,mousey=GetMousePosition()
					local objects=GetObjectsInCircle(mousex/12,mousey/12,1)
					for i,v in ipairs(objects) do
						if v.SetParams then
							v:SetParams(self.minradius,self.maxradius,self.maxvolume,self.soundlist[self.sound],
										self.pitch)
						end
					end					
				end
				if input.MousePressed("right") then
					local mousex,mousey=GetMousePosition()
					local objects=GetObjectsInCircle(mousex/12,mousey/12,1)
					for i,v in ipairs(objects) do
						if v.SetParams then
							local sound
							self.minradius,self.maxradius,self.maxvolume,sound,self.pitch=v:GetParams()
							self.sound=table.find(sound,self.soundlist)
						end
					end					
				end
			else
				if input.MousePressed("left") then
					local mousex,mousey=GetMousePosition()
					CreateSoundEmitter(mousex/12,mousey/12,self.minradius,self.maxradius,self.maxvolume,self.soundlist[self.sound],self.pitch)
				end
			end
		end,
		RenderGUI=function(self)
			if input.KeyDown("tab") then
				DrawText(0,0,self.infotext)
			else
				DrawText(0,0,self.name)
				DrawText(0,6, "maxvolume=  "..self.maxvolume)
				DrawText(0,12,"sound=      "..self.soundlist[self.sound])
				DrawText(0,18,"pitch=      "..self.pitch)
				DrawText(0,24,"modify="     ..self.modify)
			end
		end,
		minradius=0,
		maxradius=5,
		maxvolume=0.2,
		sound=1,
		modify=0,
		previewchannel=nil,
		pitch=1,
		soundlist={
		"bg1",
		"bg2",
		"bg3",
		"bg4",
		"bg5",
		"bg6",
		"bg7",
		"bg8",
		"bg9",
		"bg10",
		"bg11",
		"bg12",
		"bg13",
		"bg14",
		"bg15",
		"bg16_summutatud_mootor",
		"bg17_sinised_masinad",
		"bg18_server",
		"bg19_teleporteerumis_kanal",
		"bg20_static",
		"bg21_suur_sinine_arvuti",
		"bg22_sine_drone",
		},
		infotext="A,Z - change inner radius\nS,X - change outer radius\nD,C - change max volume\nF,V - change sound\nG,B - change pitch\nP - preview sound"
	},
	{
		name="Paint Colors",
	
	
		Render=function(self)

			local x,y=GetMousePosition()
			SetTexture()
			SetBlend("multiply")
			DrawCircle(x,y,12*self.radius,self.r,self.g,self.b,1,1)
			SetBlend("alpha")
			RenderSelectBoxesInCircle(x/12,y/12,self.radius)
			end,

		Update=function(self)
			self.radius=ModifyValueClamp(self.radius,"f","v",10,0.2,0.1)


			if input.MouseDown("left") then
				local mousex,mousey=GetMousePosition()
				local objects=GetObjectsInCircle(mousex/12,mousey/12,self.radius)
				if input.KeyDown("lctrl") then
					local racc=0
					local gacc=0
					local bacc=0
					local count=0
					for i,v in ipairs(objects) do
						local r,g,b=v:GetColor()
						racc=racc+r
						gacc=gacc+g
						bacc=bacc+b
						count=count+1
					end
					local bmps=GetBitmapsInCircle(mousex/12,mousey/12,self.radius)
					for i,v in ipairs(bmps) do
						local r,g,b=GetBitmapColor(v)
						racc=racc+r
						gacc=gacc+g
						bacc=bacc+b
						count=count+1
					end
					if count>0 then 
						local h,s,l=ConvertRGB(racc/count,gacc/count,bacc/count)
						self.hue=h
						self.sat=s
						self.val=l
					end
				else
					for i,v in ipairs(objects) do
						v:SetColor(self.r,self.g,self.b)
					end	
					local bmps=GetBitmapsInCircle(mousex/12,mousey/12,self.radius)
					for i,v in ipairs(bmps) do
						SetBitmapColor(v,self.r,self.g,self.b)
					end	
				end
			end
		end,
		RenderGUI=function(self)
			if input.KeyDown("z") then
				self.hue=RenderScale(100,50,140,0,360,self.hue,"HUE",10)
			end
			if input.KeyDown("x") then
				local mx,my=input.MouseScreenPosition()
				self.sat=RenderScale(100,50,140,0,1,self.sat,"SAT",0.05)
			end
			if input.KeyDown("c") then
				local mx,my=input.MouseScreenPosition()
				self.val=RenderScale(100,50,140,0,1,self.val,"VAL",0.05)
			end
			self.r,self.g,self.b=ConvertHSL(self.hue,self.sat,self.val)

			if input.KeyDown("tab") then
				SetColor(1,1,1,1)
				DrawText(0,0,self.infotext)
			else
				SetColor(1,1,1,1)
				DrawText(0,0,self.name)
				DrawText(0,6, "hue="..self.hue)
				DrawText(0,12,"sat="..self.sat)
				DrawText(0,18,"val="..self.val)
				DrawText(0,24,"radius="..self.radius)
			end
		end,
		r=1,
		g=1,
		b=1,
		hue=0,
		sat=0,
		val=0,
		radius=1,
		infotext="F,V - change radius\nZ - change hue\nX - change saturation\nC - change lightness"
	},
	{
		name="Place Objects",
	
	
		Render=function(self)

			local x,y=GetMousePosition()
			SetTexture()
			local v=objectlist[self.currentobject]
			if input.KeyDown("lctrl") then
				DrawSpriteArray(x,y,self.radius*12,self.count,self.angle,v.sprite,v.frame)
			else
				DrawSprite(v.sprite,x,y,self.angle,v.frame,1,1)
			end
		end,

		Update=function(self)
			if not input.KeyDown("space") then
				self.angle=ModifyValueWrap(self.angle,"a","z",3.141,-3.141,0.03)
				self.radius=ModifyValueClamp(self.radius,"s","x",20,1,0.05)
				self.count=ModifyValueClamp(self.count,"d","c",200,1,0.2)


				if input.MousePressed("left") then
					if input.KeyDown("lctrl") then
						local mx,my=GetMousePosition()
						CreateObjectArray(mx/12,my/12,self.radius,self.count,self.angle,self.currentobject)
					else
						local mx,my=GetMousePosition()
						CreateObject(mx/12,my/12,self.angle,self.currentobject)
					end
				end
			end
		end,
		RenderGUI=function(self)
			if input.KeyDown("tab") then
				DrawText(0,0,self.infotext)
			else
				if input.KeyDown("space") then
					DrawRect(0,0,320,240,1,0.5,0,0.5,1)
					
					local tx=0
					local ty=0
					local maxheight=0
					for k,v in pairs(objectlist) do
						local fw,fh=GetFrameSize(v.sprite,v.frame)
						local scale=2
						fw=fw*2
						fh=fh*2

						if fw>50 or fh>50 then
							if fw>=fh then
								scale=50/fw*2
								fw=50
								fh=fh*scale/2
							else
								scale=50/fh*2
								fh=50
								fw=fw*scale/2
							end
						end

						DrawSprite(v.sprite,tx+fw/2,ty+fh/2-self.viewy,0,v.frame,scale,scale)
						
						--reaalne valimine
						local mx,my=input.MouseScreenPosition()
						my=my+self.viewy
						if mx>tx and mx<tx+fw and my>ty and my<ty+fh then
							DrawRect(tx,ty-self.viewy,tx+fw,ty+fh-self.viewy,0)
							DrawText(10,10,k)
							if input.MousePressed("left") then
								self.currentobject=k
							end
						end
						tx=tx+fw
						if fh>maxheight then maxheight=fh end
						if tx+fw>320 then
							tx=0
							ty=ty+maxheight
							maxheight=0
						end				
					end
				
					local mx,my=input.MouseScreenPosition()
					DrawRect(mx-1,my-1,mx+1,my+1,1)
					self.viewy=ModifyValueClamp(self.viewy,"s","w",ty+320,0,20)
				end
			end
		end,
		angle=0,
		viewy=0,
		radius=1,
		count=5,
		currentobject="valgekast",
		infotext="Hold space = show objects\nA,Z        = rotate object\n"
	},
	{
		name="Place Holes",
	
	
		Render=function(self)

			local x,y=GetMousePosition()
			SetTexture()
			if self.vertcount>0 then
				local x1=self.verts[0][1]*12
				local y1=self.verts[0][2]*12
				SetColor(0,0,0,1)
				for i=1,self.vertcount-1 do
					local x2=self.verts[i][1]*12
					local y2=self.verts[i][2]*12
					DrawLine(x1,y1,x2,y2)
					x1=x2
					y1=y2
				end
				DrawLine(x1,y1,x,y)
			else DrawRect(x-1,y-1,x+1,y+1,1) end
		end,

		Update=function(self)
			if input.MousePressed("left") then
				local mx,my=GetMousePosition()
				self.verts[self.vertcount]={mx/12,my/12}
				self.vertcount=self.vertcount+1
			end
			if input.MousePressed("right") then
				local cx=0
				local cy=0
				for k,v in pairs(self.verts) do
					cx=cx+v[1]
					cy=cy+v[2]
				end
				cx=cx/self.vertcount
				cy=cy/self.vertcount
				local vtable={}
				for i=0,self.vertcount-1 do
					vtable[i*2]=self.verts[i][1]-cx
					vtable[i*2+1]=self.verts[i][2]-cy
				end

				CreateHole(cx,cy,vtable)
				self.verts={}
				self.vertcount=0
			end
		end,
		RenderGUI=function(self)
			if input.KeyDown("tab") then
				DrawText(0,0,self.infotext)
			else
				SetColor(1,1,1,1)
				DrawText(0,0,self.name)
			end
		end,
		verts={},
		vertcount=0,
		radius=1,
		infotext="A,Z - change radius\n"
		
	},
	{
		name="Place Bitmaps",
	
	
		Render=function(self)
			if input.KeyDown("lctrl") then
				local x,y=GetMousePosition()
				DrawSpriteArray(x,y,self.radius*12,self.count,self.angle,"spr_editorcut",0)
			else
				local x,y=GetMousePosition()
				SetTexture()
				DrawSprite("spr_editorcut",x,y,self.angle,0,1,1)
			end
		end,

		Update=function(self)
			self.angle=ModifyValueWrap(self.angle,"z","x",3.141,-3.141,0.03)
			self.radius=ModifyValueClamp(self.radius,"u","i",20,1,0.01)
			self.count=ModifyValueClamp(self.count,"j","k",200,1,0.1)

			if input.KeyPressed("space") then self.showpalette=not self.showpalette end
			
			if self.showpalette then
				if input.MousePressed("left") then
					self.selectrectx1,self.selectrecty1=input.MouseScreenPosition()
					self.selectrectx2,self.selectrecty2=input.MouseScreenPosition()
					self.selectrectx1=math.floor((self.selectrectx1+self.viewx)/2)
					self.selectrecty1=math.floor((self.selectrecty1+self.viewy)/2)
					self.selectrectx2=math.floor((self.selectrectx2+self.viewx)/2)
					self.selectrecty2=math.floor((self.selectrecty2+self.viewy)/2)
				end
				if input.MouseDown("left") then
					self.selectrectx2,self.selectrecty2=input.MouseScreenPosition()
					self.selectrectx2=math.floor((self.selectrectx2+self.viewx)/2)
					self.selectrecty2=math.floor((self.selectrecty2+self.viewy)/2)
				end
				if input.MouseReleased("left") then
					SetFrame("spr_editorcut",0,self.selectrectx1,self.selectrecty1
										      ,self.selectrectx2,self.selectrecty2)
					local str=	self.selectrectx1..","..
								self.selectrecty1..","..
								self.selectrectx2..","..
								self.selectrecty2..")"
					CopyTextToClipboard(str)
				end
			else
				if input.MousePressed("left") then
					local mx,my=GetMousePosition()
					if input.KeyDown("lctrl") then
						CreateBitmapArray(mx/12,my/12,self.radius,self.count,self.angle,self.selectrectx1,
												  self.selectrecty1,
												  self.selectrectx2,
												  self.selectrecty2,"tex_set1")
					else
						CreateBitmap(mx/12,my/12,-self.angle,self.selectrectx1,
												  self.selectrecty1,
												  self.selectrectx2,
												  self.selectrecty2,"tex_set1")
					end
				end
			end
		end,
		RenderGUI=function(self)
			if input.KeyDown("tab") then
				DrawText(0,0,self.infotext)
			else
				if self.showpalette then
					local tx=0
					local ty=0
					local maxheight=0
					DrawRect(0,0,320,240,1,0,0,0,1)
					DrawSprite("spr_editorlarge",1024-self.viewx,1024-self.viewy,0,0,2,2)
					local mx,my=input.MouseScreenPosition()
					DrawRect(mx-1,my-1,mx+1,my+1,1)
					
					if input.KeyDown("d") then self.viewx=self.viewx+20 end
					if input.KeyDown("a")  then self.viewx=self.viewx-20 end
					
					if input.KeyDown("s")  then self.viewy=self.viewy+20 end
					if input.KeyDown("w")    then self.viewy=self.viewy-20 end

					self.viewx=clamp(0,1024-320,self.viewx)
					self.viewy=clamp(0,2048-240,self.viewy)

					if input.MouseDown("left") then
						DrawRect(self.selectrectx1*2-self.viewx,
 								 self.selectrecty1*2-self.viewy,
								 self.selectrectx2*2-self.viewx,
								 self.selectrecty2*2-self.viewy,1,1,1,1,0.2)
					end
				end
			end
		end,
		selectrectx1=0,
		selectrecty1=0,
		selectrectx2=32,
		selectrecty2=32,
		angle=0,
		viewx=0,
		viewy=0,
		radius=1,
		count=5,
		cutsprite=nil,
		showpalette=false,
		infotext="Hold space = show palette\nZ,X        = rotate bitmap\nU,I change radius\nJ,K change count\n"
		
	},
	{
		name="Script Tools",

		Render=function(self)
			if self.mode==1 or self.mode==4 then --place triggers
				local x,y=GetMousePosition()
				SetTexture()
				SetColor(1,1,1,1)
				DrawCircle(x,y,12*self.radius)	
			elseif self.mode==2 then --place switches
				local x,y=GetMousePosition()
				DrawSprite(self.switchsprites[self.currentsprite],x,y,self.angle,0)
			elseif self.mode==3 then --name objects
				local selected=GetSelectedObjects()
				for k,v in ipairs(selected) do
					v:RenderSelectBox()
				end
			elseif self.mode==5 then --place switches
				local x,y=GetMousePosition()
				DrawSprite("spr_terminal",x,y,self.angle,self.terminalframe)
			end
		end,

		Update=function(self)
			self.mode=ModifyChoiceWrap(self.mode,"f5","unknown",5,1)
			self.currentsprite=ModifyChoiceWrap(self.currentsprite,"d","unknown",2,1)
			if input.KeyPressed("z") then SetVariableFromConsole("objname",true) end
			if input.KeyPressed("x") then SetVariableFromConsole("triggername",true) end
		    if input.KeyPressed("c") then SetVariableFromConsole("mmarkername",true) end

			if self.mode==1 then 
				self.radius=ModifyValueClamp(self.radius,"f","v",10,0.2,0.1)
			elseif self.mode==2 then
				self.angle=ModifyValueWrap(self.angle,"f","v",math.pi*2,0,math.pi*0.05)
			elseif self.mode==3 then

			elseif self.mode==4 then
				self.radius=ModifyValueClamp(self.radius,"f","v",10,0.2,0.1)
			elseif self.mode==5 then
				self.terminalframe=ModifyChoiceWrap(self.terminalframe,"f","v",5,0)
			end

			if input.MousePressed("left") then
				local x,y=GetMousePosition()
				if self.mode==1 then --place triggers	
					CreateTrigger(x/12,y/12,self.radius,triggername,-1)
				elseif self.mode==2 then --place switches
					CreateSwitch(x/12,y/12,self.angle,triggername,self.switchsprites[self.currentsprite])
				
				elseif self.mode==3 then --name objects
					local selected=GetSelectedObjects()
					for k,v in ipairs(selected) do
						v:SetName(objname)
					end	
				elseif self.mode==4 then --name objects
					CreateMapMarker(x/12,y/12,self.radius,mmarkername)				
				elseif self.mode==5 then --place switches
					CreateTerminal(x/12,y/12,self.angle,self.terminalframe,triggername)
				end
			end		
		end,
		RenderGUI=function(self)
			if input.KeyDown("tab") then
				SetColor(1,1,1,1)
				DrawText(0,0,self.infotext)
			else
				SetColor(1,1,1,1)
				DrawText(0,0,"Mode:"..self.modenames[self.mode])
				DrawText(0,6,"Object  name:"..objname)
				DrawText(0,12,"Trigger name:"..triggername)
				DrawText(0,18,"Map Marker name:"..mmarkername)
			end
		end,
		angle=0,
		mode=1,
		modenames={"Place Triggers","Place Switches","Name Objects","Map Markers","Terminals"},
		radius=1,
		cursorpos=1,
		terminalframe=0,
		currentsprite=1,
		switchsprites={"spr_lyliti","spr_sinine_lyliti"},
		infotext=
[[Z         = Change object name
X         = Change trigger name
C         =Change mapmarker name
F,V       =Change radius
F5		  =Change mode
D	      =Change sprite
LEFT CLICK=action]]
		
	},
	
	
	
	current=1,
	textinput=false,
}

objname="tmp"
triggername="lala"
mmarkername="start"

function ModuleCopy()
	level.SaveSelectedAsModule("copypaste")
	dofile("mods/copypaste.lua")
end

function ModulePaste()
	local x,y=GetMousePosition()
	PlaceModule(x/12,y/12,0,"copypaste")
end
function SetVariableFromConsole(varname,isstring)
	console.Show()
	console.HideAfterReturn()
	input.ResetASCII()
	if isstring then
		local command=varname.."=\"\""
		console.SetCommand(command)
		console.SetCursorPos(command:len()-1)
	else
		local command=varname.."="
		console.SetCommand(command)
		console.SetCursorPos(command:len())
	end
end