toolTip 		= 	{text="",prev="",x=0,y=0}


function editor.tempTip(tip,time)
	if not config.tempTips then return end
	editor.setToolTip(tip,{temp=true,time=time or 1.5})
end

function editor.setToolTip(tip,args)

	toolTip 		= {text=tostring(tip)}
	
	if args then
		args.time 		= args.time*60
		args.x 			= args.x or screenWidth/2
		args.y 			= args.y or screenHeight/2
		toolTip 		= lume.merge(toolTip,args)
	end
end

function editor.toolTip()
	mouseStill = (toolTip.text == "" ) and mouseStill-1 or 
						galleryPanel.hovered and 	(galleryPanel.anyHoveredItem and mouseStill+2 or mouseStill+1) 
								or ((mouse.x==savedMouse.x and mouse.y==savedMouse.y)  and mouseStill+1 or mouseStill-0.1)
	
	local toolTime = 20

	mouseStill = mc(0,mouseStill,toolTime+10)


	toolTip.a 		= mc(0,toolTip.time and math.min(toolTime,toolTip.time/60) or (mouseStill-toolTime)/10,1)


	if toolTip.time then 
		toolTip.time=toolTip.time-2 
	end
	
	if (mouseStill > toolTime or (toolTip.time and toolTip.time > 0)) and (editState=="Normal" or editState=="Context Menu") and not namer.enabled then 
		

		editor.drawTip(toolTip)
	

	end

	toolTip.prev 	= toolTip.text

	toolTip.text 	= (toolTip.time and toolTip.time>0) and toolTip.text or ""
end

local shadow

function editor.drawTip(tip)
	gfx_copy.setFont(fonts.inconsolata,11)
	local tip 	= lume.clone(tip)
	local otip 	= lume.clone(tip)
	local targ 	= (tip.y) and true or false
	local h 	= (tip.h) and true or false
	local tri 	= ((not tip.time or tip.time <= 0) and not (tip.notri))
	local notip = false

	local lines 
	if not tip.text or tip.text=="" then 
		tip.text=""
		tip.lines=0
	else
		tip.wrapped,lines,tip.longest 	=  lume.wordwrap(tip.text,25)
		tip.text = lume.trim(tip.text == "" and "" or tip.wrapped)
		if tip.text =="" then 
			tip.lines=1
		else
			_,tip.lines = (tip.text..""):gsub('\n', '\n')
		end
	end
	
	local bound = tip.free and function(a,b,c) return b end or mc
	local fw,fh = gfx_copy.currentFont:getWidth(tip.longest or tip.text), gfx_copy.currentFont:getHeight()
		
	pushPost()
	gfx.setFont(fonts.inconsolata,11)

	tip.a = tip.a or 1
	tip.w = mf(tip.w or mc(80,fw+16,300))
	tip.h = mf(tip.h or 8+fh*(tip.lines+1) + fh*(tip.lines)*0.5 )
	tip.x = mf(bound(8, mf((tip.x or mouse.x)-tip.w/2),screenWidth-tip.w-8))

	local top 	= 8--editor and (editor.roomLink and editor.roomLinkPreview.y+editor.roomLinkPreview.h/3 or ((editor.stackEnd>0 and tip.x<editor.stackEnd) and 60 or 16)) or 8

	tip.y = mf(bound(top,tip.y and tip.y-tip.h or mouse.y-tip.h,screenHeight-tip.h-8))-(tri and 8 or -8)

	if tip.nodraw then
		return tip
	end

	if tip.direction then 
	if tip.direction == "down" then 

		end
	end

	gfx.setBlendMode('alpha')

	if tip.backing then
		gfx.setColor(0.1,0.1,0.1,(tip.a^1.3)*0.5)
		gfx.rectangle("fill",tip.backing.x,tip.backing.y,tip.backing.w,tip.backing.h,2)
	end

	if not tip.noshadow  then 
		 shadow =  shadow or getImage("ContextShadowShort")
		local sx,sy =  (tip.w/512)*1.4, (tip.h/shadow:getHeight())*2
		local w,h = sx*shadow:getWidth()/2, sy*shadow:getHeight()/2
		gfx.setColor(0,0,0,tip.a^2)
		gfx.draw(shadow , tip.x+tip.w/2 - w , tip.y+tip.h/2 - h , 0, sx,sy )
	end

	local tipCol = tip.temp and 0.1 or 0.9
	local textCol = tip.temp and 1 or 0
	gfx.setColor(tipCol,tipCol,tipCol,(tip.amult and tip.a * tip.amult or tip.a  ))
	if type(tip.dome)=="number" then 
		gfx.rectangle("fill",tip.x,tip.y+tip.dome,tip.w,tip.h-tip.dome,3)
	elseif tip.dome then 
		gfx.setLineWidth(4)
		gfx.circle("fill",tip.x+tip.w/2,tip.y+tip.w/2, tip.w/2,128)
		gfx.circle("line",tip.x+tip.w/2,tip.y+tip.w/2, tip.w/2,128)
		gfx.rectangle("fill",tip.x,tip.y+tip.w/2,tip.w,tip.h-tip.w/2,3)
		gfx.rectangle("line",tip.x,tip.y+tip.w/2,tip.w,tip.h-tip.w/2,3)
	else
		gfx.rectangle("fill",tip.x,tip.y,tip.w,tip.h,3)
	end

	tip.hovered = mouseIn(tip)

	if tri then 
		local x = bound(18,targ and otip.x or mouse.x,screenWidth-18)
		gfx.polygon("fill",x-8,tip.y-1+tip.h,x,targ and tip.y-1+tip.h+6 or mouse.y-2,x+8,tip.y-1+tip.h)
		gfx.setColor(tipCol,tipCol,tipCol,((tip.amult and tip.a * tip.amult or tip.a  )^2))
		gfx.setLineWidth(1)
		--gfx.line(x-8,tip.y+tip.h,x,targ and tip.y+tip.h+6 or mouse.y-2,x+8,tip.y+tip.h)
	end


	gfx.setColor(textCol,textCol,textCol,tip.a*(tip.ta or 1))
	gfx.printf(tip.text,tip.x,(h or tip.lines==0) and tip.y+3 or tip.y+3,tip.w-4,tip.align or "center")
	gfx.printf(tip.text,tip.x,((h or tip.lines==0) and tip.y+3 or tip.y+3)+0.2,tip.w-4+0.3,tip.align or "center")

	popPost()
	return tip
end

tempTip = editor.tempTip