function editor.click()
	if preTransform or mouse.locked or not stageHovered then return end 

	mouse.set(function ()
		editor.clickedModel = editor.modelHovered
		if currentRoom.room then 
			if scriptsPanel.selectingTarget then 
				editor.selectTargetID(hoveredEntity.uid)		
			else
				if not (editState == "Panning" or lkeyboard.isDown("space")) then 
					if shiftDown  then 
						if hoveredEntity ~= nil then 
							if scene.entitySelected(hoveredEntity) then 
								editor.deleteFromSelectedEntities(hoveredEntity)						
							else 
								table.insert(selectedEntities,hoveredEntity)						
							end
						end
					else
						lume.clear(selectedEntities)		
						if hoveredEntity ~= nil then 
							editor.pushUndoState("Start Drag")
							editor.startDrag(hoveredEntity)			
						end
					end
				end
			end
		end
	end)

	mouse.setR( function()
		editor.clickedModel = editor.modelHovered
		local props = {}
		editor.checkForSimples()
		local items = {}
		if (editState == "Normal" or not editor.transforming) and not lkeyboard.isDown("space") then
			-- test = {enabled=true,thing1={enabled=true,property={enabled=false}},thing2={enabled=true,property={enabled=false}}}
			-- table.insert(items,editor.makeToggle(test,"enabled",
			-- 		{
			-- 			editor.makeToggle(test.thing1,"enabled",{
			-- 				editor.makeToggle(test.thing1.property,"enabled")
			-- 			},{name="thing1"}),
			-- 			editor.makeToggle(test.thing2,"enabled",{
			-- 				editor.makeToggle(test.thing2.property,"enabled")
			-- 			},{name="thing2"})
			-- 		},{name="test"}
			-- 	)
			-- )

			if not animationPanel.open and currentRoom then 
				if  roomsView then  
					table.insert(items, {{	name="Enter Room", 	func = function() game.enterRoom() end }, editor.contextRooms()})
				else
					table.insert(items, {{	name="Exit Room", 	func = function() game.exitRoom() end }, editor.contextRooms()})
				end 
			end
			if editState=="Transforming" then hoveredEntity = selectedEntities[1] end
			if hoveredEntity ~= nil  then 

				local entity 	= hoveredEntity

				if not anySelectedSimple then 

					if #selectedEntities==0 then
						table.insert(selectedEntities,entity)	
					end 

					if (scene.entitySelected(entity) and #selectedEntities==1) or #selectedEntities==0    then 
						table.insert(items, { 	name = "Properties" ,	items={
									{
										{
											name 	= "Name: "..entity.name,  
											tip 	= "Instance of: "..objects[entity.key].name,		
											func  = function() editor.renameInstance(entity) end
										},
										editor.colorSquare(entity,"color")
									}
									,{name = "Reset Transform", func = function() editor.resetTransform() end}
									,editor.makeToggle(entity,"gravity")
									,editor.makeToggle(entity,"visible")
									,editor.makeToggle(entity,"static")
									,editor.makeToggle(entity.light,{"strength","enabled"},{name="light",colors={"color"},parent=entity},entity.light.isLight and {
										editor.makeModes(entity.light,"type",{"point","spot","direction"},{align="left"}),
										editor.makeSlider(entity.light,"range",{min=25,max=2500,pow=2}),
										editor.makeToggle(entity.light,"shadow",nil,{
											editor.makeToggle(entity.light,{"softness","soft"},{name="softness"})
										--[[editor.makeToggle(entity.light,"cache"),]]
										}),
										editor.makeToggle(entity.light,"lock",{name="lock target"}),
										editor.makeSlider(entity.light,"falloff",{name="Spotlight Falloff"}),
										editor.makeSlider(entity.light,"aperture",{name="Spotlight Aperture",min=5,max=90}),
									} or {})
								}
							}
						)

						if not (imageLoader:isLoading(entity.key) or entity.light.isLight) then 
							table.insert(items, {	{
									name = animationPanel.instance and "Resume Drawing" or "Graphics", 
									func = animationPanel.instance and 
										function() animationPanel.open=true end or 
										function() editor.openAnimationPanel( entity , true, entity.default) end 
								},{
									name 	= "···",
									col 	= {a=0.5},
									size 	= -1,
									items = (not animationPanel.instance) and editor.contextAnims(entity) or nil 
								}
							})
						end
						table.insert(items,  editor.contextScripts(entity) )
					end
				end
				
				if pathsPanel.open and (#selectedEntities==0 or #selectedEntities==1) then 
					selectedEntities={entity}
					if pathsPanel.mode == "ramps" and (entity.pointIndex==#pathsPanel.points or entity.pointIndex==1)  then
						selectedEntities = {entity}

						if #pathsPanel.points>(pathsPanel.mode=="floors" and 3 or 2) then 
							table.insert(items, {	name="Delete", 	func = function() 	editor.deleteSelected() end, col = {r=0.8,g=0.2,b=0.2,a=0.4} } )
						end
					elseif #pathsPanel.points>(pathsPanel.mode=="floors" and 3 or 2) then
						editor.deleteSelected()
					end
				elseif scene.entitySelected(entity) then 
					--table.insert(items, 2,{	name="Copy", 	func = function() 	editor.copyEntities() 	end } )
					table.insert(items, {	name="Delete", 	func = function() 	editor.deleteSelected() end, col = {r=0.8,g=0.2,b=0.2,a=0.4} } )
				end    
				-- SET PARENT SOMEWHERE HERE
			else
				
				if roomsView and roomHovered==nil and not animationPanel.open then 
					-- table.insert(items, editor.editBackground(currentSet)) 
					if not  currentRoom then 
						table.insert(items, editor.contextRooms(true)) 
					end
					table.insert(items, {	name="Levels…", 		func = function() editor.openContextLevels() return "reopen" end	}) 
					table.insert(items, {	name="New Room", 	func = function() editor.newRoom(currentRooms,true) end 	}) 
				else
					lume.clear(selectedEntities)	
					if #selectedEntities==0 and not (pathsPanel.open or animationPanel.open) then 
						if editor.modelHovered  then 
							if editor.modelHovered.mode then 
								table.insert(items, {	
									{
										name="Edit "..capFirst(editor.modelHovered.mode:sub(1,-2)), 		
										func = function() 	editor.openPathsPanel( room, editor.modelHovered.id, editor.modelHovered.mode )		 end
									},
									{	
										name="···",col={a=0.5},size=-1, subWidth = 220,
										items=editor.contextPath(room, editor.modelHovered.mode,editor.modelHovered.id) , 
									}
								}) 
							end

							if editor.modelHovered.mode and not (editor.modelHovered.isFloor or editor.modelHovered.isRoof  or editor.modelHovered.isRamp)  then 
								local sub = {}
								table.insert(sub, editor.makeToggle(room[editor.modelHovered.mode][editor.modelHovered.id].points[editor.modelHovered.index],"walled"))

								if editor.roomLink then 
									table.insert(sub, {	
										name="Stop Link", 	
										func = function() 	
											editor.stopRoomLink ({
												room = currentRoom.key, 
												type = "edge", 
												mode = editor.modelHovered.mode, 
												id   = editor.modelHovered.id, 
												index= editor.modelHovered.index 
											}) 
										end 
									})
								else
									table.insert(sub, {	
										name="Start Link", 	
										func = function() 	
											editor.startRoomLink({
												room = currentRoom.key, 
												type = "edge", 
												mode = editor.modelHovered.mode, 
												id   = editor.modelHovered.id, 
												index= editor.modelHovered.index 
											}) 
										end 
									})
								end
								if editor.modelHovered.isWall then 
									table.insert(sub, {
										{	name="Cut Rect", 	func = function() editor.startWallCut(room[editor.modelHovered.mode][editor.modelHovered.id],editor.modelHovered.index,"rect") end},
										{	name="Cut Circle", 	func = function() editor.startWallCut(room[editor.modelHovered.mode][editor.modelHovered.id],editor.modelHovered.index,"circ") end}
										})
									table.insert(sub, {	name="Cut Out Multi", 	func = function() editor.startWallCut(room[editor.modelHovered.mode][editor.modelHovered.id],editor.modelHovered.index,"rect",true) end})
									
									local wallCuts = 0
									
									for c,cut in ipairs(room[editor.modelHovered.mode][editor.modelHovered.id].cuts) do
										if cut.index == editor.modelHovered.index then 
											wallCuts=wallCuts+1
										end
									end

									if wallCuts>0 then 
										table.insert(sub, {	name="Remove Cuts On Wall ("..wallCuts..")", 	func = function() editor.removeWallCuts(room[editor.modelHovered.mode][editor.modelHovered.id],editor.modelHovered.index) end})
									end
									if #room[editor.modelHovered.mode][editor.modelHovered.id].cuts>0 then 
										table.insert(sub, {	name="Remove All Cuts On Floor ("..#room[editor.modelHovered.mode][editor.modelHovered.id].cuts..")", 	func = function() editor.removeWallCuts(room[editor.modelHovered.mode][editor.modelHovered.id]) end})
									end
								else 
									table.insert(sub, {	name="New Ramp", 	func = function() 
										editor.openPathsPanel(room,nil,"ramps") 
										editor.newPath(pathsPanel,room[editor.modelHovered.mode][editor.modelHovered.id],editor.modelHovered.index)  
									end})
								end
								if #sub>1 then 
									table.insert(items, {{name="Edit "..(editor.modelHovered.isWall and "Wall" or "Edge"),func= function ()  editor.startWallCut(room[editor.modelHovered.mode][editor.modelHovered.id],editor.modelHovered.index,"rect") end, items=sub},{name="···",col={a=0.5},size=-1}})
								end
							end

						end
						local numItems = #items
						table.insert(items, editor.newSymbolContext()) 
						--table.insert(items, 			editor.contextBuild(room))
						table.insert(items, 	  	editor.contextScripts( room ))
						if not sidebar.visible then 
							table.insert(items, {name="Gallery", 		func = function() 	galleryPanel.width=350 editor.floatPanel(galleryPanel,contextMenu) end }) 
						end

						table.insert(items, editor.moreContextOptions())
					else
						if pathsPanel.open then 
							if room[pathsPanel.mode][pathsPanel.current] then 
								-- 	table.insert(items, {	name="New Point", 	func = function() editor.updateEntityTransform(editor.startDrag(editor.newPathPoint(),true)) end })
								items = editor.contextPath(room, pathsPanel.mode,pathsPanel.current)
								table.insert(items, {	name="Done", func = function() editor.closePathsPanel() end, 	col = {r=0.2,g=0.7,b=0.3,a=0.3}, }) 
								props.width = 180
							end

						elseif animationPanel.open then

							--table.insert(items, editor.contextHSV(drawingPanel.color))
							--table.insert(items, editor.contextRGB(drawingPanel.color))
							--TOOLS

							local toolNames = {
								{"Brush (B)"	, "brush"		},
								{"Pen (P)"		, "pen"			},
								{"Eraser (E)"	, "erase"		},	
								{"Fill (F)"		, "fill"		},	
								{"Lasso (L)"	, "select"	},
								{"Move (V)"		, "move"		},	
							}
							local tools = {}

							for i,t in ipairs(toolNames) do 
								table.insert(tools,{
									size = -1,
									tip  = t[1],
									img  = "tools_"..t[2],
									tool = t[2],
									col  = {a=0.5},
									func = function()
										drawingPanel.tool 		= t[2]
										if t[2] == "pen" then 
											drawingPanel.vector 				= true
											drawingPanel.tool 		= "brush"	
										elseif t[2] == "brush" then 
											drawingPanel.vector 				= false
										elseif t[2]=="fill" then 
											if drawingPanel.vector then editor.prepareFill(animationPanel) end
										end
									end,
									update = function(item)
										item.col = {a=0.3}
										if drawingPanel.tool=="brush" then 
											if item.tool=="pen" and animationPanel.vector  then 
												item.col = {a=1}
											elseif item.tool=="brush" and not animationPanel.vector  then
												item.col = {a=1}
											end
										elseif drawingPanel.tool==item.tool then 
											item.col = {a=1}
										end
									end
								})
							end
							table.insert(tools,{size=1,static=true})
							table.insert(tools, editor.colorSquare(drawingPanel,"color"))


							local file,view = {name 		= "File",subitems	={}},{name = "View", subitems={}}
							local empty = {}	

							file.subitems	= {
								{
									name="Save", 
									func=function() editor.saveAnimation(animationPanel) end
								},
								editor.makeSlider(
									animationPanel,
									"maxUndos",
									{name="Max Undos",max=100,min=10,round=1,tip="Careful with this, might just crash if you're low on memory"}
								),
								editor.makeToggle(
									editor,
									{"autoSaveTime","autoSaveEnabled"},
									{name="",namef=function(item) item.name= "Auto Save every "..tostring(editor.saveTime/3600).." minutes" end,noval=true},
									"slideToggle"
								),
								{
									name="Default Format",
									func= function(item)
										animationPanel.defaultFormat = cycleTable(animationPanel.formats)
										item.name = "Default Format: "..capFirst(animationPanel.defaultFormat=="r8" and "limited" or animationPanel.defaultFormat)
									end
								},
							}
							if level.isAnim then 
								classicConcat(file.subitems,{
									{
										{name="Canvas Size",size = 3, static=true},
										{
											name=tostring(animationPanel.stageWidth),
											align="center",
											func=function(item)
												namer:typeString("width",tostring(animationPanel.stageWidth),{x=item.x+item.width/2+2,y=item.y+item.height/2+2,width=item.width,notri=true},function(x) 
													if tonumber(x) ~= nil then 
														x=math.max(32,math.abs(x))
														if animationPanel.lockAspect then 
															animationPanel.stageHeight=animationPanel.stageHeight/(animationPanel.stageWidth/x)
														end
														animationPanel.stageWidth = x 
													end
												end,nil,true)
												return true
											end,
											update = function(item)
												item.name = tostring(animationPanel.stageWidth)
											end
										},
										{
											name=tostring(animationPanel.stageHeight),
											align="center",
											func=function(item)
												namer:typeString("width",tostring(animationPanel.stageHeight),{x=item.x+item.width/2+2,y=item.y+item.height/2+2,width=item.width,notri=true},function(x) 
													if tonumber(x) ~= nil then 
														x=math.max(32,math.abs(x))
														if animationPanel.lockAspect then 
															animationPanel.stageWidth=animationPanel.stageWidth/(animationPanel.stageHeight/x)
														end
														animationPanel.stageHeight = x 
													end
												end,nil,true)
												return true
											end,
											update = function(item)
												item.name = tostring(animationPanel.stageHeight)
											end
										},
										editor.makeCheckbox(animationPanel,"lockAspect","Maintain Aspect")
									},
									{
										lume.merge(editor.makeSlider(animationPanel,"exportScale",{col={r=1,g=1,b=1,a=1},name="Export Size",noval=true})[1],{size=3}),
										{name="",static=true,align="center",update=function(item)item.name=tostring(roundToNearest(animationPanel.stageWidth*animationPanel.exportScale,2))end},
										{name="",static=true,align="center",update=function(item)item.name=tostring(roundToNearest(animationPanel.stageHeight*animationPanel.exportScale,2))end},
										editor.makeCheckbox(animationPanel,"scaleExport","Resize Export")
									},
									editor.makeToggle(animationPanel,"exportSeparate",{name="Export Layers Separately"}),
									editor.makeToggle(animationPanel.exportFormats,"mp4",{name="Export MP4"}),
									editor.makeToggle(animationPanel.exportFormats,"gif",{name="Export GIF"}),
									classicConcat({{name="Export Sequence: ",static=true,size=2}},editor.makeModes(animationPanel.exportFormats,"sequence",animationPanel.imageFormats)),
									{name="Hide Rough/Note on Export",func=function() animationPanel.exportLinesOnly=not animationPanel.exportLinesOnly end},
									{name="Export",func=function() editor.exportAnim(animationPanel) end},
									{
										name="Exit to start screen",
										func=exitProject,
										warning="open"
									}
								})
							end
							

							view.subitems	= {
								{
									name="Show Shortcuts and Help",
									func=function() 
										if not (traceEnabled and helpEnabled) then 
											traceEnabled = true 
											helpEnabled = true 
										else 
											traceEnabled = false 
											helpEnabled = false
										end
										return true 
									end
								},
								editor.makeToggle(animationPanel.onion,{"amount","enabled"},{name="Onion Skin"},{
									editor.makeToggle(animationPanel.onion.before,{"amount","enabled"},{name="before",colors={"tint"}}),
									editor.makeToggle(animationPanel.onion.after,{"amount","enabled"},{name="after",colors={"tint"}})
								}),
								editor.makeToggle(animationPanel,"showPasteboard",{name="Show Pasteboard"}),
								editor.makeToggle(animationPanel,"timelineTracking",{name="Timeline Tracking",tip="Auto scroll timeline to current frame"}),
								editor.makeToggle(animationPanel,{"antiAliasing","antiAlias"},{name="Antialiasing",min=1,max=8,round=1},"slideToggle"),
								
								-- editor.makeToggle(config,"flatUI",{name="Flat UI"}),
								editor.makeToggle(animationPanel,"frameShift",{name="Swap move/extend frame keys",tip="toggles 3/4 with shift+(3/4)"}),
								editor.makeToggle(animationPanel,"phaseClear",{name="2 Phase Clear",tip="clears the current line group first, then everything else"}),
								editor.makeToggle(animationPanel,"debugView",{name="Debug View",click=editor.refreshStrokes}),
								editor.makeToggle(config,"tempTips",{name="Enable Temp Tips",tip="The little black box that pops up in the middle of the screen when you do something"}),
								-- editor.makeSlider(animationPanel,"fillDivs",{name="fill divisions",min=1,max=8,round=1}),
								-- editor.makeSlider(animationPanel,"fillDivs",{name="fill Divisions",min=1,max=8,round=1}),
								editor.makeToggle(animationPanel,"forceRefill",{name="force refill"}),
								-- editor.makeToggle(animationPanel,"threadFill",{name="thread fill"}),
								editor.makeSlider(config,"catmull",{name="Curve Detail",min=0,max=4,round=1,slide=editor.refreshStrokes}),
								editor.makeToggle(config,"flashFrames",{name="oppa flash frame style",click=function() keyTime=5 end}),
								editor.makeToggle(config,"showLoaded",{name="show loaded frames",click=function() keyTime=5 end}),
								editor.makeSlider(animationPanel,"row",{name="row size",min=16,max=32,round=1,slide=function() keyTime=5 end}),
							}

							if LOVE then 
								table.insert(view.subitems,{
									name="Trace GFX stats",
									func=function() 
										editor.debug.gfx_stats = not editor.debug.gfx_stats
										return true 
									end
								})
							end

							table.insert(tools, {	
								size 	 = -1,
								name 	 = "···",  
								align 	 = "center" , 	
								col 	 = {r=0,g=0,b=0,a=settings and 0.5 or 0}, 
								subWidth = 220, 
								subHeight = 260,
								sameY 	 = true,
								hoverOnly= true,
								sub = "File",
								rollOn  = function(item) item.expand = true end,
								items = {{file,view}}
							})

							table.insert(items, tools)

							table.insert(items, editor.showRecentColors(drawingPanel))

							if drawingPanel.tool=="brush" then 
								if drawingPanel.vector then 
									table.insert(items,editor.makeSlider(drawingPanel.pen,"size",{round=1,min=1,max=16}))
									table.insert(items,editor.makeSlider(drawingPanel.pen,"stabilizer"))
									table.insert(items,editor.makeSlider(drawingPanel.pen,"smoothing"))
									table.insert(items,editor.makeSlider(drawingPanel.brush,"opacity"))
									table.insert(items,editor.makeModes(drawingPanel.pen,"ends",{"round","flat","tapered"}))
									table.insert(items,editor.makeModes(drawingPanel.pen,"group",drawingPanel.penGroups))
								else
									local brushSettings = {}
									for s,slider in pairs(drawingPanel.sliders) do 
										local setting = {
											editor.makeSlider(drawingPanel.brush,slider.name,{min=slider.min,max=slider.max,col={r=1,g=1,b=1,a=1},name=slider.name,nocap=true})[1],
											editor.makeSlider(slider,"jitter",{name="jitter",nocap=true,noval=true})[1]
										}
										setting[1].size = 3
										setting[2].size = 1
										table.insert(brushSettings, setting )
									end
									table.insert(brushSettings, editor.makeToggle(drawingPanel.brush,"antialias"))
									classicConcat(items,brushSettings)
								end
								table.insert(items,editor.makeToggle(drawingPanel.brush,"pressureSensitivity",{name="pressure sensitivity"}))
							end

							if drawingPanel.tool=="fill" then 
								-- table.insert(items,editor.makeModes(drawingPanel,"fillMode",drawingPanel.fillModes))
								if animationPanel.l.format == "vector" then 
									table.insert(items,editor.makeSlider(drawingPanel,"fillGap",{name="fill gap",max=32,round=1}))
									table.insert(items,editor.makeSlider(drawingPanel,"fillRadius",{name="fill radius",max=100}))
									table.insert(items,editor.makeToggle(drawingPanel,"fillAll",{name="fill all"}))
								end
								if animationPanel.l.format == "normal" then 
									table.insert(items,editor.makeSlider(drawingPanel,"fillTolerance",{name="tolerance"}))
									table.insert(items,editor.makeToggle(drawingPanel,"fillBehind",{name="fill behind"}))
								end
							end

							if drawingPanel.tool=="erase" then 
								if animationPanel.vector then 
									table.insert(items,editor.makeModes(drawingPanel,"eraseMode",animationPanel.l.format=="vector" and drawingPanel.eraseModesVec or drawingPanel.eraseModes))
									table.insert(items,editor.makeModes(drawingPanel,"eraseGroup",drawingPanel.eraseGroups))
								end
							end

							if drawingPanel.tool=="select" then 
								if animationPanel.vector then 
									table.insert(items,editor.makeModes(drawingPanel,"selectGroup",drawingPanel.eraseGroups))
								end
							end

							if animationPanel.selection.mesh then 
								if animationPanel.selection.canvas then 
									table.insert(items, {
										name="Flip Horizontal",	
										func=function() 
											animationPanel.selection.scale.x=animationPanel.selection.scale.x*-1 return true 
										end
									})
									table.insert(items, {
										name="Flip Vertical",		
										func=function() 
											animationPanel.selection.scale.y=animationPanel.selection.scale.y*-1 return true 
										end
									})
									table.insert(items, {
										name="Move to New Layer",
										func=function() 
											editor.moveToNewLayer(animationPanel) 
										end
									})
								end
							end

							

							props.width = 180	
						else
							if #editor.pasteboard>0 then
								table.insert(items, {	name="Paste", 	func = editor.pasteEntities } )
							end
						end
					end
				end
			end
			editor.openContextMenu (items,props)
		end
	end , true)
end

function editor.newSymbolContext()
	return {{	name="New", 	func = function() 	editor.drawNewImage  ("camera") 			end },{name="···",col={a=0.5},size=-1, sub="Drawing", items=lume.concat(
		{{
			{
				name="Other", align="center",subitems={
					{name="Player spawn",	func =  editor.newPlayerSpawn },
					{name="Actor spawn", 	func =  nothing},
					{name="Point of interest",	func =  editor.newPlayerSpawn },
					{name="Point light",	func = editor.newPointLight },
					{name="Spot light", 	func =  nothing},
					{name="Directional light", 	func =  nothing},
				}
			},
			{
				name="Symbol", align="center",subitems={
					{name="Resolution: ", 			slider= {target=editor,  style="pct", prop="drawingResolution", min=1, max=0.2,	round=0.1,noval=true}
																					,update=function(x) x.name="Resolution: "..round(1.2-editor.drawingResolution,2) end	 },
					{name="No Transform", 						func = function() 	editor.drawNewImage  () 						end },
					{name="Facing Camera", 						func = function() 	editor.drawNewImage  ("camera") 		end },
					{name="Facing Camera (XZ only)", 	func = function() 	editor.drawNewImage  ("camera x") 	end },
					editor.modelHovered and 
					{name="Facing Surface ", 					func = function() 	editor.drawNewImage  ("surface") 		end } or {},
				}
			},
			{
				name="Shape", align="center",subitems={
					{name="Cube", 			func = function() 	editor.createNewModel  	("Cube")			end },
					{name="Cyclinder", 	func = function() 	editor.createNewModel  	("Cyclinder") end },
					{name="Sphere", 		func = function() 	editor.createNewModel  	("Sphere") 		end },
					editor.debug.draw_tests and {name="test", 		func = function() 	editor.createNewModel  	("test") 		end } or nil,
				}
			},
			
		}})
	}}
end

function editor.newPointLight()
	scene.newEntity({
		position = -editor.lastIntersect+editor.intersectNormal*50,
		light = {
			isLight = true,
			enabled=true,
			strength = 0.5,
			range = 300,
			color = {r=1,g=1,b=1},
		}
	})
end

function editor.moreContextOptions()
	 return {{	name="More"},{name="",size=0.01,subWidth = 180,sub="Debug",items = lume.concat(
	 	{{
		 	{ 	name = "Debug" ,	 size =1 ,subitems=editor.debugOptions()},
			{ 	name = "Projects" ,size =1 ,subitems=editor.projectOptions()},
	}})}}
end

function editor.debugOptions()
	return {
		editor.makeToggle(editor,			"show_help"),
		editor.makeToggle(editor,			"autoSaveEnabled"),
		editor.makeToggle(editor.debug,"no_scripts"),
		editor.makeToggle(editor.debug,"wireframe"),
		editor.makeToggle(editor.debug,"no_lights"),
	  editor.makeSlider(editor.debug,{"num_test_lights","test_lights","test_lights"},{mim=1,max=16,round=1}),
	  editor.makeToggle(editor.debug,"move_testlights"),
	  editor.makeToggle(editor.debug,"soft_lights"),
	  editor.makeToggle(editor.debug,"single_channel_lights"),
	  editor.makeToggle(editor.debug,"draw_shadow_maps"),
		editor.makeToggle(editor.debug,"no_icons"),
		editor.makeToggle(editor.debug,"no_links"),
		editor.makeToggle(editor.debug,"draw_tests"),
		editor.makeToggle(editor.debug,"gfx_stats"),
		editor.makeToggle(editor.debug,"trace_camera"),
		editor.makeToggle(editor.debug,"trace_mouse_intersect"),
		editor.makeToggle(editor.debug,"trace_mesh_times"),
		editor.makeToggle(editor.debug,"draw_ground_planes"),
		editor.makeToggle(editor.debug,"draw_frame_bounds"),
		editor.makeToggle(editor.debug,"hide_terrain"),
		editor.makeToggle(editor.debug,"bounding_boxes"),
		editor.makeToggle(editor.debug,"force_build"),
		editor.makeToggle(editor.debug,"rebuild_on_focus"),
	  editor.makeToggle(editor.debug,"trace_active_rooms"),
	  editor.makeToggle(editor.debug,"trace_loaded_images"),
		editor.makeToggle(editor.debug,"feet"),
		editor.makeToggle(editor.debug,"show_patterns"),
	  editor.makeToggle(editor.debug,"dither_lights"),
	  editor.makeToggle(editor.debug,"anti_alias"),
	  editor.makeToggle(editor.debug,"bench"),
	}
end

function editor.projectOptions()
	local projects = {{
										name="Exit to start screen",
										func=exitProject,
										warning="open"
									}}
	
	for f,folder in ipairs(lfs.getDirectoryItems( "projects" )) do
		if folder:sub(1,1)~="." then 
			table.insert(projects,{{
					name=folder,
					col = (CURRENT_PROJECT==folder) and colors.black50 or nil ,
					func=function() loadProject(folder) end
				},{
					name="",
					size=0.15,
					tip="rename"
				}
			})
		end
	end
	table.insert(projects,{
		{name="New Game",func=function() newProject("Untitled Game") 			end,col=colors.green},
		{name="New Anim",func=function() newProject("Untitled Anim",true) end,col=colors.green}
	})
	return projects
end

function editor.clickStage()
	if lkeyboard.isDown("space") then
		mouse.pushClick(function()
			editor.startPan()
		end)
	else
		if 			currentTool == "Select" 	then 	 editor.startSelect()
		elseif 	currentTool == "Brush" 		then 	 editor.startDrawing()
		end
	end
end

function editor.clickStageR()
	if lkeyboard.isDown("space") then
		editor.startPan(2)
	end
end

function editor.newPlayerSpawn()
	editor.newEntity(lume.concat(mouseLocalDrop,{spawn="player"}))
end

function editor.newSymbol(props)
	local newImg  				= imageLoader:newSymbol()
	mouseLocalDrop.dropped = false

	return editor.newEntity(newImg,mouseLocalDrop)
end

function editor.drawNewImage(mode)
	local entity  				= editor.newSymbol()
	scene.orientEntity(entity,mode)
	editor.openAnimationPanel 	( entity , true) 
	animationPanel.newImage 	= true
	animationPanel.blankAnim 	= true
	animationPanel.f.blank  	= true
	return entity
end

function editor.startDrawing() 
	if not altDown and not mouseIn(animationPanel) and stageHovered then 
		mouse.pushClick(function()
			levelClicked 			= true
			animationPanel.drawn 	= 0
		end)
	end
end

function editor.select()
	if levelClicked and (not animationPanel.open or animationPanel.l.remodel or animationPanel.l.repose) and (editState=="Normal" or editState=="Transforming")  then

		editor.selectingRect = true
		selectBounds.x2 = mouse.x/game.resolution
		selectBounds.y2 = mouse.y/game.resolution
		selectBounds.x1 = selectBounds.x1 or mouse.x
		selectBounds.y1 = selectBounds.y1 or mouse.y
		selectBounds.h = math.abs(selectBounds.y2-selectBounds.y1)
		selectBounds.w = math.abs(selectBounds.x2-selectBounds.x1)
		selectBounds.x = selectBounds.x2>selectBounds.x1 and selectBounds.x1 or selectBounds.x2
		selectBounds.y = selectBounds.y2>selectBounds.y1 and selectBounds.y1 or selectBounds.y2
		selectAlpha    = 0.8

		editor.modelHovered = nil

	else 
		editor.selectingRect = false
	end

	if selectBounds.h and selectBounds.h>4 and selectBounds.w>4 then
		gfx.setColor(1,1,1,selectAlpha/8)
		gfx.rectangle("fill",selectBounds.x,selectBounds.y,selectBounds.w,selectBounds.h)
		gfx.setLineWidth(1)
		gfx.setColor(1,1,1,selectAlpha)
		gfx.rectangle("line",selectBounds.x,selectBounds.y,selectBounds.w,selectBounds.h,2,2)
		gfx.setColor(0,0,0,selectAlpha)
		--gfx.rectangle("line",selectBounds.x+1,selectBounds.y+1,selectBounds.w,selectBounds.h)
	end
	selectAlpha    = math.max(0,selectAlpha-0.1)
end

function editor.startSelect()
	if stageHovered and hoveredEntity == nil and editState=="Normal" then 
		mouse.pushClick(function()
			editor.pushUndoState("Start Selection")
			editor.forceProject = true
			levelClicked 	= true
			selectPoints 	= {}
			selectLine 	 	= {}
			lume.clear 		(shiftDown and selectedEntities or {}) 
			numSelected 	= #selectedEntities
			selectBounds 	= {x=mouse.x,y=mouse.y,x1=mouse.x/game.resolution,y1=mouse.y/game.resolution,x2=mouse.x,y2=mouse.y,h=2,w=2}
			
		end)
	end
end

function editor.startPan(b)
	editor.panStarted=true
	cam.stored 		= {x=cam.x,y=cam.y,z=cam.z,b=b or 1}
	cam.storedOffset= {x=cam.offset.x,y=cam.offset.y,z=cam.offset.z,b=b or 1}
	editState = "Panning"
end

function editor.startDrag(entity,setpos)
	lume.clear 				(selectedEntities)		
	selectedEntities[1] 	= entity
	editState  					= "Transforming"
	multiTool.dragging 			= true	
	entity.selected 		= true
	multiTool.name  = "move"
	return entity	
end

function editor.release()

	if editor.transforming then 
		editor.applyTransforms()
		multiTool.translation = vec3(0,0,0)
	end

	animationPanel.brushPrev 	= nil

	levelClicked 				= false

	draggingPlayer				= false
	multiTool.dragging 				= false
	multiTool.rotating 				= false
	multiTool.scaling 				= false
	multiTool.sorting 				= false

	multiTool.draggings				= {}
	clickedEntityIndex 			= 0
	editor.forceProject 	= false

	lmouse.setCursor(defaultCursor) 

	-- REVERT EDIT MODE
	if 	multicheckOR(editState, {"Dragging","Panning", "Resizing Panel", "Transforming","Viewport"} ) then 
		editState 	= "Normal" 
	end 

	-- DROP IN NEW ITEM
	for i,item in ipairs(galleryPanel.selected) do
		if stageHovered and droppingItem and objects[item.key]~=nil then 
			if not mouseIn(sidebar) then
				editor.newEntity(item.key,mouseLocalDrop) 
			end
		end  
	end
	droppingItem = false
end 


