-----------------------------------------------------------
-- KEYBOARD COMMANDS
-----------------------------------------------------------
function editor.editorKeys()
	if keypressed("return") then 
				print(roomsView ,not cmdDown , editState=="Normal")
			end
	if keypressed("escape") then
		editor.closeContextMenu()
		overviewPanel.open = false
	end

	if keypressed("`") then
		editor.refreshSelection = true
		delayedCall(function() editor.refreshSelection = false end,0.1)
		if cmdDown then 
			renderSelection = not renderSelection 
		else
			if level.isAnim then 

			else
				if animationPanel.anims then 
					animationPanel.open = not animationPanel.open
					if not animationPanel.open then 
						lmouse.setVisible(true)  
						currentTool = "Select"
						scene.deleteEntity(animationPanel.transformer)
					else 
						editor.updateFrameTransformer(animationPanel)
						animationPanel.changed = true
					end
					lume.clear(selectedEntities)
				else
					overviewPanel.open = not overviewPanel.open
					if overviewPanel.open then 
						editor.openOverview(overviewPanel)
					end
				end
			end
		end
	end
	

	if contextMenu.open or  namer.enabled then return end

	if keypressed("backspace") then 
		if editState == "Cutting" then 
			editor.removeWallCuts(room[pathsPanel.cutTarget[1]][pathsPanel.cutTarget[2]],pathsPanel.cutPoint)
		end
	end

	if altDown and keypressed("v") then 
		currentRoom.room.lighting.vct.showVoxels=not currentRoom.room.lighting.vct.showVoxels
	end

	if cmdDown then
	
		if not animationPanel.open and shiftDown and keypressed("escape") then
			exitProject()
		end


		
		-- TILT
		--if keypressed("[") then
		--	log("tilt",cam.tilt)
		--	cam.tilt = cam.tilt-0.1
		--end
		--if keypressed("]") then
		--	cam.tilt = cam.tilt+0.1
		--end
		--if keypressed("p") then
		--	cam.tilt = 0
		--end

		

		-- FOCAL LENGTH
		if shiftDown then
			if keypressed("-") then
				cam.focalL = cam.focalL-0.05
			end
			if keypressed("=") then
				cam.focalL = cam.focalL+0.05
			end
			if keypressed("0") then
				cam.focalL = 0
			end
		elseif not altDown then
			-- ZOOM 
			if keypressed("-") then
				effects.tween(cam,{soom=mc(0.81,cam.soom+0.05,1.1)},0.2,"easeOut",1)
				print("cam.soom",cam.soom)
			end
			if keypressed("=") then
				effects.tween(cam,{soom=mc(0.81,cam.soom-0.05,1.1)},0.2,"easeOut",1)
				print("cam.soom",cam.soom)
			end
			if keypressed("0") then
				effects.tween(cam,{soom=1},0.25,"easeOut",1)
				-- cam.x=0
				-- cam.y=0
				-- cam.offset.x=0
				-- cam.offset.y=0
				-- cam.focalL=0
			end
		end

		-- RELOAD
		if keypressed("r") then 
			tempTip("checking for code changed...")
			requireSources(true)
			collectgarbage()
			editor.refresh = true
		end

		-- EMPTY LOG
		-- if keypressed("e") then 
		-- 	tempTip("Emptied Log")
		-- 	toLog={}
		-- 	logNum=0
		-- end

		-- GRID
		if keypressed("g") then 
			editor.gridEnabled=not editor.gridEnabled
		end

		-- GFX STATS
		if keypressed("u") then 
			gfxStats=not gfxStats
		end

		-- TOGGLE FLAT UI
		if keypressed("9") then
			-- sidebar.flatUI = not sidebar.flatUI
		end

	else
		if keypressed("-") and keypressed("=") then
			if cam.locked then 
				effects.tween(cam,{x = 0,y = -2},0.25,"easeOut",1)
			else
				effects.tween(cam.offset,{x = 0,y = -2},0.25,"easeOut",1)
			end
		end
	end



	-- SAVE
	if not animationPanel.open then 
		if cmdDown and keypressed("s") then 
			tempTip("Saved")
			editor.saveStep=0
			editor.save() 
		end
	end

	if altDown and keypressed("b") then 
		createBoiserieMaps()
	end

	if namer.enabled==false then 
		for i=1,8,1 do
			if keypressed(tostring(i)) then
				if keyDown("c") then 
					if shiftDown then 
						editor.setCamera(i)
					else 
						editor.resetCamera(i)
					end
				elseif keyDown("v") then 
					if shiftDown then 
						editor.setViewport(i) 
					else 
						editor.resetViewport(i)
					end
				end
			end
		end
	end

	if cmdDown and keypressed("o") then 
		cam.ortho = not cam.ortho 
		cam.fov = 0
	end

	if not (animationPanel.open or overviewPanel.open)  then  

		if cmdDown then
			--UNDO
			if keyRepeat("z")  then
				editor:undo()
			end

			-- SAVE
			if keypressed("s") then 
				tempTip("Saved")
				editor.saveStep=0
				editor.save() 
			end

			-- SLECT ALL
			if keypressed("a")then 
				tempTip("Select All")
				selectAll=true
			end

			-- DEPTH
			if keypressed("d") then 
				--tempTip("Depth "..((cam.focalL>0) and "Off" or "On"))
				--effects.tween(cam,{focalL=(cam.focalL>0) and 0 or 0.5},0.25,"easeOut",1)
			end

			-- CAMERAS
			if  namer.enabled==false and animationPanel.open==false then 
				for i=1, 3 do
					if keypressed(tostring(i))  then
						--room:setCam(i)
					end
				end
			end

			if keypressed("l") then
				cam.locked = not cam.locked
				tempTip("Camera "..(cam.locked and "Locked" or "Unlocked"))
			end

			-- COPY
			if keypressed("c") then 
				tempTip("Copied")
				editor.copyEntities()
			end

			-- PASTE 
			if cmdDown and keypressed("v") then 
				editor.pasteEntities()
			end

			-- TOGGLE BLUR
			if keypressed("b") then 
				--cam.blur=not cam.blur
			end

			-- TOGGLE LINES
			if keypressed("l") then 
				--editor.toggleLines()
			end

			-- TOGGLE DARKMODE
			if keypressed("8") then 
				sidebar.darkMode = not sidebar.darkMode
			end

			if keypressed("return") or controls.startButtonPressed then 
				if not shiftDown then 
					editor.keepCamera = true
				end
				editor.playLevel()
				return
			end
		end

		
		
		if not cmdDown and stageHovered then 

			if keypressed("escape") then 
				if pathsPanel.wallCutting then 
					pathsPanel.wallCutting=false
				elseif not roomsView and editState=="Normal" and not pathsPanel.open   then 
					game.exitRoom()
				end
			end
			
			if roomsView and not cmdDown and editState=="Normal" and keypressed("return") then 
				print("try2")
				game.enterRoom()
			end

		end

	end

	

end

function editor.animationKeys(panel)
	if namer.enabled or lmouse.getRelativeMode()   then return end

	if not cmdDown then 
		if keypressed("escape") then 
			editor.dropSelection(panel)
		end
		if keypressed("return") then
			editor.dropSelection(panel)
		end
	end
	-- SAVE ANIMATION
	if cmdDown  then
		if keypressed("s") then
			editor.saveAnimation(panel)
		end
		if not level.isAnim and keypressed("return") then
			editor.saveGraphics(panel)
			editor.closeAnimationPanel(panel)
			lmouse.setVisible(true)  
			shapeToUpdate 	= panel.anim
		end
	end

	-- TOGGLE ONION SKIN
	if keypressed("o") then  
		editor.tempTip("Onion Skin "..(panel.onion.enabled and "Disabled" or "Enabled"))
		panel.onion.enabled = not panel.onion.enabled
	elseif keypressed(",") then  
		editor.tempTip("Prev Skin "..(panel.onion.before.enabled and "Disabled" or "Enabled"))
		panel.onion.before.enabled = not panel.onion.before.enabled
	elseif keypressed(".") then  
		editor.tempTip("Next Skin "..(panel.onion.after.enabled and "Disabled" or "Enabled"))
		panel.onion.after.enabled = not panel.onion.after.enabled
	end

	-- TOGGLE FRAMEBOUNDS
	if cmdDown and keypressed("j") then
		-- panel.frameBounds = not panel.frameBounds
	end

	-- BOTTOM TOGGLE
	if cmdDown and keypressed("b") then 
		panel.fullWidth = not panel.fullWidth 
		--panel.bottom=not panel.bottom
		--editor.checkTimelineDocked(panel)
	end

	

	-- BACKGROUND TOGGLE
	if cmdDown and keypressed("b") then 
		--drawingPanel.backing = not drawingPanel.backing
	end

	-- NEW ANIMATION
	-- if cmdDown and shiftDown and keypressed("n") then 
	-- 	editor.newAnimation()
	-- end
	
	-- PLAY ANIMATION
	if keytapped("space") and not panel.panned and not panel.canvasClicked then 
		editor.playAnimation()
	end
	if not keyDown("space") and not panel.canvasClicked then 
		panel.panned = false
	end
	if keypressed("return") then
		if cmdDown then 
			panel.scrollPos=0
			editor.playAnimation(nil,1)
		end
	end

	-- EXIT
	if cmdDown and keypressed("escape") then  
		if panel.playing then 
			panel.playing = false 
		else 
			if level.isAnim then 

			else
				lume.clear(keysPressed)
				namer:warning("Exit without saving",function() 
					if panel.newImage then
						editor.deleteImage(panel.instance.key)
					end
					editor.closeAnimationPanel(panel) 
				end)
			end
		end
	end
end


function editor.timelineKeys(panel)


	if contextMenu.open or namer.enabled or  panel.canvasClicked or lmouse.getRelativeMode() or editor.orbiting then return end

	-- KEY COMMANDS 

	if keypressed("escape") then  
		editor.deselectRegion(panel)
	end

	local frameShift
	if panel.frameShift then  frameShift= not shiftDown  else frameShift= shiftDown end


	if 		keypressed("up") or (cmdDown and keypressed("1")) then -- UP LAYER
		if editor.changeLayer(panel.layer+1) then 
			editor.tempTip(""..panel.layers[panel.layer].name)
		end

		
 	elseif 	keypressed("down") or (cmdDown and keypressed("2")) then -- DOWN LAYER
 		if editor.changeLayer(panel.layer-1) then 
	 		editor.tempTip(""..panel.layers[panel.layer].name)
	 	end



	elseif  keypressed("5") then -- SOLO LAYER
 		panel.solo = panel.solo>0 and 0 or panel.layer
		editor.tempTip("Solo "..(panel.solo>0 and "Off" or "On"))
	 
	elseif panel.l.remodel then 
		for i=1,6,1 do
			if keypressed(tostring(i)) then
				if i>3 then 
					panel[panel.meshViewToggles[i-3]] =  not panel[panel.meshViewToggles[i-3]] 
				else
					editor.selectMeshSelectMode(panel,i)
				end
			end
		end

		if not cmdDown or editor.mouseRelative then 
			if not panel.meshTransform or not panel.meshTransform.mode then 
				if 	 	 keypressed("g") then -- GRAB
					editor.startMeshTranslate(panel)

				elseif keypressed("s") and not lmouse.isDown(3)  then -- SCALE
					editor.startMeshScale(panel)

				elseif keypressed("r") then -- ROTATE
					editor.startMeshRotate(panel)

				end

				if 	 	 keypressed("i") then -- INSET
					editor.startMeshInset(panel)
				elseif keypressed("e") then -- EXTRUDE
					editor.startMeshExtrude(panel)
				end

			elseif anykeypressed({"g","s","r","i","e"}) then
				editor.stopMeshTransform(panel)
			end


			if keypressed("a") then -- (DE)SELECT ALL
				if altDown then 
					editor.invertMeshSelect(panel)
				else 
					editor.toggleMeshSelect(panel)
				end
			end



		else

		end
	else
		-- if keypressed("j") then 
		-- 	editor.releaseFrames   	(panel)
		-- end
		if 	 	 keypressed("g") then -- COLOR
			editor.openColorTip(drawingPanel,"color")
		end
		if  keyRepeat("1") then -- PREV FRAME
			editor.deselectRegion(panel)
			if shiftDown then 
				local nk 
				local i = panel.frame
				while not nk do 
					i=i==1 and #panel.frames or i-1
	 				local frame = panel.frames[i]
	 				if frame and frame.isKey then 
						nk = i
						break
					end
					if i==panel.frame then 
						break
					end
				end
				panel.frame=nk or panel.frame
			else
				panel.frame = 	editor.getAbsoluteFrame(panel)==panel.currentFrame and panel.frame - 1 or panel.frame
			end
			editor.setAbsoluteFrame(panel)
			editor.tempTip("F "..panel.frame)
			
	 	elseif  keyRepeat("2") then -- NEXT FRAME
	 		editor.deselectRegion(panel)
	 		if shiftDown then 
	 			local nk 
	 			for i=panel.frame+1,#panel.frames+panel.frame,1 do
	 				if panel.frames[i>#panel.frames and i-#panel.frames or i].isKey then 
	 					nk = i
	 					break
	 				end
	 			end
	 			panel.frame=nk or panel.frame
	 		else
		 		panel.frame =	panel.frame + 1 
	 		end
	 		editor.setAbsoluteFrame(panel)
	 		editor.tempTip("F "..panel.frame)
	 		
		elseif 	 frameShift and keyRepeat("3") then -- SHORTEN FRAME
			panel.pushUndoState("Shorten") 
			panel.frames[panel.frame].l = math.max( panel.frames[panel.frame].l -1,panel.currentFrame- editor.getAbsoluteFrame(panel)+1 ) 
			
		elseif 	 frameShift and keyRepeat("4") then -- LENGTHEN FRAME
			panel.pushUndoState("Lengthen") 
			panel.frames[panel.frame].l = panel.frames[panel.frame].l +1

		elseif not frameShift and keyRepeat("3") then -- MOVE FRAME LEFT
			panel.pushUndoState() 
			if panel.frame>1 then 
				if panel.frames[panel.frame-1].l==1 then 
					local l1,l2 = panel.frames[panel.frame-1].l,panel.frames[panel.frame].l
					panel.frames[panel.frame-1],panel.frames[panel.frame]=panel.frames[panel.frame],panel.frames[panel.frame-1]
					panel.frames[panel.frame].l,panel.frames[panel.frame-1].l=l2+1,l1
					panel.frame=panel.frame-1
					if panel.frame > 1 then 
						panel.frames[panel.frame-1].l=math.max(1,panel.frames[panel.frame-1].l-1)
					end
				else 
					panel.frames[panel.frame-1].l=math.max(1,panel.frames[panel.frame-1].l-1)
					panel.frames[panel.frame].l=math.max(1,panel.frames[panel.frame].l+1)
				end
				panel.currentFrame=panel.currentFrame-1
				editor.setAbsoluteFrame(panel)
			end
			
		elseif not frameShift and keyRepeat("4") then -- MOVE FRAME RIGHT
			panel.pushUndoState() 
			if panel.frame>1 then 
					panel.frames[panel.frame-1].l=math.max(1,panel.frames[panel.frame-1].l+1)
				if panel.frame<#panel.frames then 
					if panel.f.l==1 then 
						local l1,l2 = panel.frames[panel.frame].l,panel.frames[panel.frame+1].l
						panel.frames[panel.frame+1],panel.frames[panel.frame]=panel.frames[panel.frame],panel.frames[panel.frame+1]
						panel.frames[panel.frame].l,panel.frames[panel.frame+1].l=l1,l2
						panel.frame=panel.frame+1
					end
				end
				panel.frames[panel.frame].l=math.max(1,panel.frames[panel.frame].l-1)
				panel.currentFrame=panel.currentFrame+1
				editor.setAbsoluteFrame(panel)
			end
			
		elseif 	not shiftDown and keyRepeat("q") then -- ADD FRAME
			editor.addBlankFrame(panel)

		elseif 	not cmdDown and keypressed("z") then -- ROUGHS
			editor.setPenGroup("rough")

		elseif 	not cmdDown and keypressed("x") then -- LINES
			editor.setPenGroup("line")

		elseif 	not cmdDown and keypressed("c") then -- NOTES
			editor.setPenGroup("note")

		elseif  shiftDown and keyRepeat("q") then -- DUPLICATE FRAME
			panel.pushUndoState("Duplicate") 
			table.insert(panel.frames, panel.frame+1, editor.copyFrame(panel,panel.f))
			panel.frame=panel.frame+1
			applyTransform(panel.frames[panel.frame])
			editor.setAbsoluteFrame(panel)
			editor.tempTip("Duplicate Frame")

		elseif not cmdDown and keypressed("s") then  -- SPLIT FRAME
			editor.deselectRegion(panel)
			if panel.f.l>1 then 
				panel.pushUndoState()
				editor.setAbsoluteFrame(panel)
				editor.setKeyFrame(panel)
				local nl = panel.f.l/2
				panel.f.l=mu(nl)
				editor.addBlankFrame(panel,mf(nl))
				editor.tempTip("Split Frame")
			end

		elseif not cmdDown and keypressed("k") then  -- TOGGLE KEY
			panel.pushUndoState()
			panel.f.isKey = not panel.f.isKey 
			editor.tempTip("Toggle Key")

		elseif cmdDown and keypressed("p") then  -- PRUNE FRAME
			panel.pushUndoState() 
			editor.clipFrame( panel.f )
			editor.tempTip("Clipped Frame")

		elseif keypressed("t") then  -- TWEEN FRAME
			panel.pushUndoState() 
			panel.f.tween = panel.f.tween or {}
			panel.f.tween.enabled = not panel.f.tween.enabled 
			editor.tempTip("Tween "..(panel.f.tween.enabled and "Enabled" or "Disabled"))

		elseif keypressed("y") then  -- BLEND ANIMS
			panel.pushUndoState() 
			panel.f.tween.blend = not panel.f.tween.blend 
			editor.tempTip("Blend "..(panel.f.tween.blend and "Enabled" or "Disabled"))

		-- elseif keypressed("d") then  -- ORIENT FRAME TO CAMERA --temp
		-- 	if panel.l.type == "drawing" then 
		-- 		panel.pushUndoState() 
		-- 		panel.f.orientation =  quat.rotate(cam.angle.y,axes.x)
		-- 		panel.f.orientation = quat.rotate(math.angle(cam.x,cam.z,cam.lookTarg.x,cam.lookTarg.z)-pi/2,axes.y)
		-- 		applyTransform()
		-- 		editor.tempTip("Orient Frame")
		-- 	end

		elseif 	(((altDown or cmdDown) and keyRepeat("backspace")) or keyRepeat("delete")) and #panel.frames>1 then -- REMOVE FRAMES
			
			if panel.region and panel.region.frames then 
				panel.pushUndoState("Region Edit") 
				editor.deleteRegion(panel,panel.region)
				panel.region=nil
				editor.tempTip("Removed Frames")
			else
				panel.pushUndoState() 
				local alt 
				if frameShift then 
					alt = altDown 
				else 
					alt = not altDown 
				end
				local ladd = alt and panel.f.l or 0
				table.remove(panel.frames, panel.frame)
				panel.frame = 	panel.frame - 1
				editor.setAbsoluteFrame(panel)
				panel.frames[panel.frame].l = panel.frames[panel.frame].l +ladd
				editor.tempTip("Removed Frame")
			end

		elseif 	not (cmdDown and shiftDown) and  keypressed("backspace") then  -- CLEAR FRAME
			if panel.region and panel.region.frames then 
				-- CLEAR ALL FRAMES IN REGION
			else
				if panel.selection.selected then 
					editor.cutStrokeSelection(panel,true)
					panel.selection={}
				else
					panel.pushUndoState() 
					-- panel.clearingFrame 				= shiftDown -- temp
					local strokes = panel.frames[panel.frame].strokes
					local cleared = false 
					if strokes and type(strokes)=="table" then 
						for s,stroke in lume.ripairs(strokes) do 
							if drawingPanel.tool=="fill" then 
								if stroke.group == "fill" then 
									table.remove(panel.frames[panel.frame].strokes,s)	
									cleared = true
								end
							elseif stroke.group == drawingPanel.pen.group then 
								table.remove(panel.frames[panel.frame].strokes,s)
								cleared = true
							end
						end
					end
					cleared = false
					if cleared then 
						editor.refreshStrokes()
					else 
						-- panel.frames[panel.frame].strokes   = {}
						panel.frames[panel.frame] 	= editor.defaultFrame(panel)
					end
				end
			end

		elseif 	keypressed("r") then 
			panel.pushUndoState() 
			panel.f.position = vec3(0)
			panel.f.scale = vec3(1)
			panel.f.orientation = panel.l.iqm and quat.from_angle_axis(-pi/2,vec3(1,0,0)) or quat(0,0.001,0,1)
			applyTransform(panel.f)
			panel.changed=true

		elseif cmdDown and (keypressed("c") or keypressed("x")) then -- COPY FRAME
			editor.copyFrameContents(panel,keypressed("x"))

		elseif cmdDown and keypressed("v") then -- PASTE FRAME
			if shiftDown then 
				if altDown then 
					panel.copiedFrame.l=1
				end
				editor.pasteFrame(panel)
				editor.tempTip("Paste Frame")
			else
				editor.pasteFrameContents(panel)
		 	end

		elseif not shiftDown and  cmdDown and keypressed("n") then -- NEW LAYER
			editor.newLayer(panel,panel.layer+1)
		end

		if 	keyRepeat("left") then
			panel.currentFrame = 	panel.currentFrame - 1
			editor.loopAnimation(panel)
		elseif shiftDown and keyDown("left") then -- PREV FRAME
			editor.updateFrame( panel , -1 )
		end
			
	 	if 	keyRepeat("right") then 
	 		panel.currentFrame = 	panel.currentFrame + 1
	 		editor.loopAnimation(panel)
	 	elseif shiftDown and keyDown("right") then -- NEXT FRAME
	 		editor.updateFrame( panel  )
	 	end

	 	if cmdDown and shiftDown and keypressed("[") then
			editor.cycleAnimations(panel,-1)
		elseif cmdDown and shiftDown and keypressed("]") then
			editor.cycleAnimations(panel,1)
		end
	end

	if panel.l.type=="drawing" then   -- VECTOR TOGGLE
		-- if keypressed("k") then 
		-- 	panel.pushUndoState() 
		-- 	panel.vector=not panel.vector
		-- 	editor.tempTip("Drawing: "..(panel.vector and "Vector" or "Raster"))
		-- end
		if panel.vector then 
			if keypressed("]") then 
				drawingPanel.pen.size=mc(1,drawingPanel.pen.size+2,16)
				if shiftDown then 
					editor.refreshStrokes()
					for s,stroke in pairs(panel.f.strokes) do 
						stroke.size = stroke.size + 0.1
					end
				end
			elseif keypressed("[") then 
				if shiftDown then 
					editor.refreshStrokes()
					for s,stroke in pairs(panel.f.strokes) do 
						stroke.size = stroke.size - 0.1
					end
				end
				drawingPanel.pen.size=mc(1,drawingPanel.pen.size-2,16)
			end
		end
	else
		if cmdDown and keypressed("w")   then -- WIRE FRAME
			panel.pushUndoState() 
			panel.l.wireframe = not panel.l.wireframe
			editor.tempTip("Wireframe "..(panel.l.wireframe and "Enabled" or "Disabled"))
		end
	end

	if keypressed("tab") then 
		panel.visible = not panel.visible
	end

	if keypressed("m") and panel.l.type=="model" then 
		if not lmouse.isDown(3) then  -- EDIT MESH
			panel.pushUndoState() 
			panel.l.remodel = not panel.l.remodel
			panel.l.updots = true
			editor.tempTip("Mesh edit "..(panel.l.remodel and "Enabled" or "Disabled"))
			if panel.l.remodel then 
				editor.clearBones(panel)
			end
		end
	end

	if level.isAnim and cmdDown and keypressed("e") then
		if  shiftDown   then  -- EXPORT
			-- export wholeanim
		else
			-- export cut
			editor.exportAnim(panel)
		end
	end

 	editor.setKeyFrame(panel)
end

function keyRepeat(k,fast)
	return keypressed(k) or (keyDown(k,16) and (ltimer.fourstep==2 or fast))
end

function editor.copyFrameContents(panel,cut)
	if panel.selection.selected then 
		panel.selectionBuffer = deepcopy(panel.selection)
		local x,y,w,h = panel.selection.quad:getViewport()
		local sw,sh=panel.selection.quad:getTextureDimensions()
		panel.selectionBuffer.quad   =  gfx.newQuad(x,y,w,h,sw,sh)
		panel.selectionBuffer.mesh   = gfx.newMesh(zformat,panel.selection.verts,"triangles")
		panel.selectionBuffer.canvas = copyCanvas(panel.selection.canvas)
		panel.selectionBuffer.strokes = editor.cutStrokeSelection(panel,cut)
		panel.selection.frame  = panel.frame
		if cut then 
			panel.selection = {}
			editor.tempTip("Cut Selection")
		else 
			editor.tempTip("Copy Selection")
		end
	else
		panel.selectionBuffer = nil
		panel.copiedFrame 		= lume.merge(deepcopy(panel.f),{strokes=copyStrokes(panel.f.strokes),canvas=copyCanvas(panel.f.canvas)})
		editor.tempTip("Copy Frame Contents")
	end
end

function editor.cutFrameContents(panel)
	editor.copyFrameContents(panel,true)
end

function editor.pasteFrameContents(panel)
	panel.pushUndoState() 
	if panel.selectionBuffer then 
		
		
		editor.pasteSelectionContents(panel)	

		drawingPanel.tool = "move"
		local x,y,w,h = panel.selectionBuffer.quad:getViewport()
		local sw,sh=panel.selectionBuffer.quad:getTextureDimensions()
		panel.selection = deepcopy(panel.selectionBuffer)

		if panel.selectionBuffer and panel.selectionBuffer.strokes then 
			panel.selection.strokes = {}
			for s,stroke in ipairs(panel.selectionBuffer.strokes) do 
				table.insert(panel.f.strokes,stroke)
				 panel.selection.strokes[#panel.f.strokes] = true
			end
			editor.refreshStrokes()
		end
		
		log("diff",vec2(panel.selection.diff))
		log("pos ",vec2(panel.selection.position))

		if panel.frame~= panel.selection.frame then 
			panel.selection.position = {x=panel.f.w/2+panel.selection.diff.x,y=panel.f.h/2+panel.selection.diff.y}
		end

		panel.selection.quad   =  gfx.newQuad(x,y,w,h,sw,sh)
		panel.selection.mesh   = gfx.newMesh(zformat,panel.selectionBuffer.verts,"triangles")
		panel.selection.canvas = copyCanvas(panel.selectionBuffer.canvas)
		
		editor.tempTip("Paste Selection")
	else
		if  panel.copiedFrame then 
			panel.pushUndoState()  
			--panel.f.canvas=copyCanvas(panel.copiedFrame.canvas)
			local l = panel.animation.layers[panel.layer].frames[panel.frame].l
			panel.animation.layers[panel.layer].frames[panel.frame] = lume.merge(editor.defaultFrame(panel,deepcopy(panel.copiedFrame)),{blank=false,strokes=copyStrokes(panel.copiedFrame.strokes),canvas=copyCanvas(panel.copiedFrame.canvas)})
			panel.animation.layers[panel.layer].frames[panel.frame].l = l
			panel.pasted = true
			log(#(panel.animation.layers[panel.layer].frames[panel.frame].strokes or {1}))
	 		editor.tempTip("Paste Frame Contents")
	 	end
 	end
end

function editor.moveToNewLayer(panel)
	editor.cutFrameContents(panel)
	editor.newLayer(panel)
	editor.pasteFrameContents(panel)
end
 	

function editor.cycleAnimations(panel,n)
	local anims = sortBy(panel.anims,"name")
	local v,i = lume.match(anims,function(x) return x.key==panel.anim end)
	local i2 = i+n
	editor.switchAnimation(anims[i2<1 and #anims or (i2>#anims and 1 or i2)].key) 

end
