function editor.hoverButton(button)
	if button.x<-(button.w or button.width) or 
		button.y<-(button.h or button.height) or 
		button.x>screenWidth or 
		button.y>screenHeight then return end

	button.hovered 	= mouseIn(button) 
	if button.hovered  then 
		if button.tip then 
			editor.setToolTip(button.tip)
		end
		if button.pos == nil and button.pct==nil then
			lmouse.setCursor(hand)
		end
		if button.func 			then 	mouse.set (button.func)			  end
		if button.release   then 	mouse.set (button.release  ,true) end
		if button.funcR 		then 	mouse.setR(button.funcR 		) end
		if button.releaseR  then 	mouse.setR(button.releaseR ,true) end
	end

end

local bcol,btcol = {r=0,g=0,b=0,a=0.5},{r=1,g=1,b=1,a=1}
local col,tcol
local hover 
local dotOff
local ellipseOff
local edge
local textX,textY

function editor.buttonVisible(button)
	return not (button.x<-(button.w or button.width) or 
		button.y<-(button.h or button.height) or 
		button.x>screenWidth or 
		button.y>screenHeight )
end
local gradient
function editor.drawButton(button,moose)
	if not button then return end
	if not button or button.x<-(button.w or button.width) or 
		button.y<-(button.h or button.height) or 
		button.x>screenWidth or 
		button.y>screenHeight then editor.buttonsClipped = editor.buttonsClipped +1  return end

	editor.buttonsDrawn = editor.buttonsDrawn +1

	button.x=mf(type(button.x)=="number" and button.x or button.x())
	button.y=mf(type(button.y)=="number" and button.y or button.y())
	button.w=mf(button.w or button.width)
	button.h=mf(button.h or button.height)
	button.h=button.h or gfx.currentFont:getHeight()

	button.center= button.center or {}
	button.center.x,button.center.y = button.x+button.w/2,button.y+button.h/2



	col  = button.col or button.color or bcol 
	for k,v in pairs(bcol) do col[k] = col[k] or v end
	tcol  = button.tcol or button.textColor or btcol 
	for k,v in pairs(btcol) do tcol[k] = tcol[k] or v end

	if not button.static  and ( moose or mouse )then 
		if not button.noClick then 
			button.hovered 	= mouseIn(button,moose or mouse) 
			if button.hovered then 
				if button.tip then 
					editor.setToolTip(button.tip)
				end
				if button.pos == nil and button.pct==nil then
					--lmouse.setCursor(hand)
				end
				if button.func 			then 	mouse.set (button.func)			  end
				if button.click 		then 	mouse.set (button.click)			  end
				if button.release   then 	mouse.set (button.release  ,true) end
				if button.funcR 		then 	mouse.setR(button.funcR 		) end
				if button.releaseR  then 	mouse.setR(button.releaseR ,true) end
			end
		end
	end

	

	if not button.static then 
		hover = button.hoverTime and button.hoverTime*100+155 or ((button.hovered or (button.invert and not button.hovered)) and 230 or 50)

		if hiSpec and button.blur then 
			editor.drawContextBack(button)
		else
			gfx.setColor	(col.r,col.g,col.b,(button.alpha or 1)*(button.a or col.a*(button.invert and (button.hovered and 0.5 or 1) or (button.hovered and 1 or 0.5)) ))  	
			gfx.rectangle	("fill",button.x+1,button.y+1,button.w-2,button.h-2, button.corner or 2)
		end

		if button.gradient then 
			if type(button.gradient)=="table" then 
			-- button.gradient = gradientMesh("horizontal",button.gradient[1],button.gradient[2])
			end
			if button.gradient then 
				gfx.setColor(1,1,1,1)
				-- gfx.draw(button.gradient,button.x,button.y,0,button.width, button.height)
			end
		end


		if button.pos or button.pct then 
			if button.pct then 
				gfx.setColor	(0,0,0,0.8)  	
				gfx.rectangle	("fill",button.x+2,button.y+1,(button.w-4)*button.pct,button.h-4,(button.w-4)*button.pct>4 and 2 or 0)
				gfx.setColor	(1,1,1,.6)  	
				gfx.rectangle	("fill",button.x+2,button.y+button.h-4,(button.w-4)*button.pct,2,(button.w-4)*button.pct>4 and 2 or 0)
			end

			if button.pos then 
				gfx.setColor	(1,1,1,0.4)  	
				gfx.rectangle	("fill",button.x + button.pos*(button.w-2),button.y+3,2,button.h-9)
				gfx.setColor	(0,0,0,0.6)  	
				gfx.rectangle	("fill",button.x+1 + button.pos*(button.w-2),button.y+3,2,button.h-9)
			end
		end
	end



	gfx.setLineWidth(1) 


	buttonImages(button)

	if button.text~=nil and   button.text~="" then 

		gfx.setColor 	(tcol.r,tcol.g,tcol.b,(button.alpha or 1)*tcol.a)
		if type(button.text)=="string" then 
			if button.toggle and button.toggle.targ[button.toggle.prop] then 
				print("FUCK")
			end
			if button.text:sub(1,2)=="-l" then
				button.imgActual = true
				button.img = "path"..capFirst(pathsPanel.loopTypes[tonumber(button.text:sub(3,3))])
				buttonImages(button)
			elseif button.text:sub(1,2)=="-w" then
				button.imgActual = true
				button.img = "wall"..capFirst(pathsPanel.wallTypes[tonumber(button.text:sub(3,3))])
				buttonImages(button)
			elseif button.text == "unticked" then

			 		--gfx.rectangle	("line",button.x+3,button.y+3,button.w-6,button.h-6)
		 	elseif button.text == "ticked" then
		 		print("FUCK",time)
		 		gfx.rectangle	("line",button.x+3,button.y+3,button.w-6,button.h-6)
		 		gfx.line	(	button.x+5, 			button.y+button.h/2,	
		 						button.x+button.w/2-2,	button.y+button.h-7,	
		 						button.x+button.w-6, 	button.y+5)
		 	elseif button.text=="╳" then
		 		local border = 5
		 		gfx.line	(button.x+border,button.y+border,button.x+button.w-border,button.y+button.h-border)
		 		gfx.line	(button.x+button.w-border,button.y+border,button.x+border,button.y+button.h-border)
		 	elseif button.text=="🔍" and button.w<button.h*1.5 then
		 		local dim = (button.h>button.w and button.w or button.h)
		 		gfx.circle 	("line",button.x+button.w/2.3,button.y+button.h/2.3,dim/4,16)
		 		gfx.line	(button.x+button.w/1.7,button.y+button.h/1.7, button.x+button.w/1.3,button.y+button.h/1.3)
		 	--else
		 	--	gfx.printf("?", button.x, mf(button.y+button.h/6), button.w, "center")
		 	--end

			else
				dotOff = button.text=="···" and 3 or 0
				ellipseOff = button.text=="…" and 3 or 0
				edge = button.full and 2 or 4
				textX = button.x+edge-dotOff
				textY =  button.full and button.y+2 or mf(button.y+(button.h-gfx.currentFontSize)/2-1)-ellipseOff
				if button.harsh then gfx.setShader(shaders.harsh) end
				if button.cacheText then 
					button.textObject = (not button.refreshText) and button.textObject or gfx.newText( gfx.currentFont, button.text )
					button.refreshText = false
				end
				if button.textObject then 
					gfx.draw(button.textObject,textX,textY)
				else
					gfx.printf(button.full and button.text or ellipses(button.text,button.w-10), textX,textY, button.w-edge*2+dotOff*2, button.align or "center")
				end
				if button.harsh then gfx.setShader() end
			end
		else
			gfx.draw(button.text,button.x,button.y)
		end
	end

end



function buttonImages(button)
	if button.img then 
		if button.img~="none" then
			button.center =button.center or  {x=button.x+button.w/2,y=button.y+button.h/2}

			local img= type(button.img)=="string" and getImage(button.img) or button.img
			gfx.setColor(1,1,1,button.col and button.col.a or button.a  or  1)
			if button.checkers then 
				gfx.draw(
					getImage("checkersRounded"),
					button.x+button.w/2,
					button.y+button.h/2,0, 
					button.w/thumbSize-0.05, 
					button.h/thumbSize-0.05, 
					thumbSize/2,thumbSize/2)
				return
			end
			local buttonImg = {w=img:getWidth(),h=img:getHeight(),s=button.h}
			local scale 	= (buttonImg.w>buttonImg.h) 	
									and 	buttonImg.s/buttonImg.w 
									or 		buttonImg.s/buttonImg.h
			scale = 1--button.imgScaleOverride or (scale * (button.imgScale or 1 ))


			local thumbOffset 	= (buttonImg.w>buttonImg.h) 	
									and 	{ x=0,y=(buttonImg.s-buttonImg.h*scale)/2 } 
									or 		{ x=(buttonImg.s-buttonImg.w*scale)/2,y=0 }
			scale = button.imgActual and 1 or scale
			if getImageName(button.img)=="checkers" then 

			else
				gfx.draw(img, button.center.x,button.center.y, 0, scale, scale,buttonImg.w/2,buttonImg.h/2)
			end
		end
	end
end


function editor.button(panel,config,relative)

	if editor.buttons[panel.name..config.name]==nil and config.name then 
		editor.buttons[panel.name..config.name] = lume.clone(config)
		editor.buttons[panel.name..config.name].hoverTime = 0
	else
		editor.buttons[panel.name..config.name] =  lume.merge(editor.buttons[panel.name..config.name], lume.pick(config, "x","y","w","h","col"))
	end

	local button = (config.name~=nil) and editor.buttons[panel.name..config.name] or config

	if relative then 
		button.x=button.x+panel.x
		button.y=button.y+panel.y
	end

	buttonImages 	(button)

	button.hovered = mouseIn(button)
	if button.hovered then
		button.hoverTime =mc(0,button.hoverTime+0.2,1)

		if mouse.click() then
			button.func()
		end
	else
		button.hoverTime = mc(0,button.hoverTime-0.1,1)
	end

	editor.drawButton(button)
	
	return button
end



function editor.drawInstanceProperties(slider)
	local vert = slider.vert or false
	local col = lume.merge({r=0,g=0,b=0,a=1}, slider.col or {})
	local tcol = lume.merge({r=1,g=1,b=1,a=1}, slider.tcol or {})
	slider.width = slider.width or slider.w
	slider.height = slider.height or slider.h
	slider.width = mf(slider.width)
	slider.x = mf(slider.x)
	if slider.state=="non" then 
		gfx.setColor(	0,0,0, 0.3)  	
		gfx.rectangle(	"fill",	mf(slider.x),	mf(slider.y+1),	mf(slider.width),	mf(slider.height-2), 3)
	end
	gfx.setColor(	col.r,	col.g,		col.b,		slider.hovered and 0.8 or 0.4 *col.a)  
	if not vert then
		gfx.rectangle(	"fill",	mf(slider.x),	mf(slider.y+1),	 mf(mc(3,slider.width*(slider.state=="non" and slider.pct or 1),slider.width)),	mf(slider.height-2) , 2)
	else
		local barHeight =  slider.mid and slider.height*(slider.pct-0.5) or mc(3,slider.height*slider.pct,slider.height)
		local barY 		=  slider.mid and slider.y+slider.height/2-barHeight	or mf(slider.y+slider.height-mf(barHeight))
		gfx.rectangle(	"fill",	slider.x,	barY, slider.width, mf(barHeight)	 )
			
		if slider.pct2 and not slider.mid and slider.pct2>0.1 then 
			gfx.setColor(	1,0.3,0.2, 0.2)  
			gfx.rectangle(	"fill",	slider.x,	barY, slider.width, mf(barHeight*slider.pct2)-1)
		end
		gfx.setColor(0.1,0.1,0.1, 1)  	
		gfx.setLineWidth(2)
		--gfx.rectangle(	"line",	slider.x-1,	slider.y,	slider.width+1,	slider.height, 3)
	end
	gfx.setLineWidth(1)

	gfx.setColor(	col.r,	col.g,		col.b, 		(slider.state=="non") and 1 or 0)  

	if slider.state =="non" then 	
		gfx.setColor(	1,1,1,(slider.pct==0 and 0.2 or tcol.a)) 	
	elseif slider.state == "off" then  	
		gfx.setColor(	1,1,1,0.6)	
	else
		gfx.setColor(	1,1,1,1)	
	end

	gfx.printf(slider.name:sub(1,(slider.width)/(gfx.currentFontSize*0.5)), slider.x, mf(slider.y+slider.height/(slider.vert and 4 or 6)-0.5), slider.width, "center")
end