-----------------------------------------------------------
-- GALLERY PANEL
-----------------------------------------------------------
function editor.defineGalleryPanel()
	galleryPanel 	= 
	{	name 			= "Gallery",
		open 			= true,
		docked			= true,
		height 			= sidebar.titleHp,
		y 				= 0,
		tags 			= {"Game"},
		prevTags 		= "",
		numTags			= 0,
		keys 			= {},
		items 			= {},
		selected 		= {},
		canvas 			= nil,
		scrollSpeed 	= 0,
		bounds 			= {x=1,y=1,w=1,h=1},
		hovered 		= false,
		anyHoveredItem 	= false,
		delete 				= false,
		searching 		= false,
		showNames 		= false,
		searchFade 		= 0,
		sorts 			= {"name","size","modified","created"},
		sort 			= "name",
		psort 			= "name",
		thumbSize 		= config.galleryThumb or 48,
		fullThumbSize 	= config.galleryThumb or 50,
		iconBorder 		= 4,
		iconColumns 	= 3,
		row 			= sidebar.row,
		alpha 			= 1,
		
		update 			= function(x) editor.galleryPanelUpdate(x) end,
		tabs 			= {}
	}
	editor.resetTagTabs(galleryPanel)
	galleryPanel.tab 	= galleryPanel.tabs[1]
	galleryCoords 		= {x=0,y=0,width=0,an="images"}
end

function editor.galleryPanelUpdate(panel)
	
	panel.hide = (pathsPanel.open)

	gfx.fontSize(10)	
	
	if sidebar.visible then panel.docked = true end
	if panel.docked then 
		editor.togglePanel(panel) 
	else
		panel.drag=true
	end
	if (not panel.open and panel.docked) or panel.hide then return 0 end 

	panel.height =  sidebar.titleH

	editor.gallerySearch(panel)
	editor.deleteTags(panel)
	editor.getSelectedTab(panel)
	editor.getSelectedTags(panel)

	if panel.tab==nil then return end

	panel.fullThumbSize 	=	(panel.thumbSize+panel.iconBorder)

	editor.scrollGallery	(panel)
	editor.setGalleryBounds (panel)

	editor.drawGalleryBack 	(panel) 
	
	editor.setIconColumns 	(panel)
	editor.processItems 	(panel)
	editor.search 			(panel.items,panel.searchTerm,"name")
	editor.sortItems 		(panel)
	editor.updateItems 		(panel)

	editor.drawGalleryShadow(panel)
	editor.drawGalleryCanvas(panel)
	editor.iconSizeSlider 	(panel)
	editor.galleryItemSort 	(panel)

	gfx.setShader()
end

-----------------------------------------------------------
-- SETUP
-----------------------------------------------------------
function editor.scrollGallery(panel)

	if mouseIn(panel.bounds) then
		panel.scrollSpeed 	= panel.scrollSpeed-mouseScroll.y*2
	end

	panel.scrollSpeed 	= easeTo(panel.scrollSpeed,0,4)
	panel.tab.scrollPos = panel.tab.scrollPos+panel.scrollSpeed
	panel.tab.rows 		= mu(lume.count(panel.items)/panel.iconColumns)
	panel.tab.maxScroll = math.max(0, (panel.tab.rows-1)*panel.fullThumbSize-panel.height-sidebar.titleH*2)
	panel.tab.upperBound= (panel.tab.maxScroll<0) and 0 or panel.tab.maxScroll
	panel.tab.scrollPos = sling(0,panel.tab.scrollPos,panel.tab.upperBound,4)
end

function editor.setGalleryBounds(panel)
	panel.bounds = (panel.sizeSlider and panel.sizeSlider.sliding) and panel.bounds or {
		x = panel.x+4,
		y = panel.y+panel.height,
		w = panel.width-8, 
		h = math.min(panel.docked and 9999 or screenHeight/2.5 ,math.min(screenHeight-panel.y-panel.height-32, easeTo(panel.bounds.h or 10, math.max(128,(panel.tab.rows+1) * panel.fullThumbSize),4)))
	}
end

function editor.iconSizeSlider(panel)
	panel.sizeSlider = panel.sizeSlider or { 	
			x 		= 0,
			y 		= 0, 
			width 	= sidebar.minWidth/3, 
			height 	= 16,
			pct 	= (panel.thumbSize-32)/64,
			state 	= "non",
			opacity = 1,
			col 	= {r=0.05,g=0.05,b=0.05},
			name 	= "icon size",
			hovered = false,
			sliding = false,
	} 
	
	if panel.sizeSlider.hovered then
		mouse.set( function() 
			panel.sizeSlider.sliding = true  
		end)
	end

	mouse.pushRelease(function() 
		panel.sizeSlider.sliding = false 
	end, true)
	panel.sizeSlider.hovered  	= mouseIn(panel.sizeSlider)
	panel.sizeSlider.x 			= panel.x+panel.width-	panel.sizeSlider.width-2
	panel.sizeSlider.y 			= panel.y+panel.height-5
	panel.sizeSlider.pct 		= panel.sizeSlider.sliding and mc(0.05,(mouse.x-panel.sizeSlider.x)/panel.sizeSlider.width,1)  or panel.sizeSlider.pct 
	panel.thumbSize  			= 32+panel.sizeSlider.pct*64
	panel.height 				= panel.height+12

	editor.drawInstanceProperties( panel.sizeSlider)
	gfx.printf(#panel.items==0 and "no items" or (#panel.items.." item"..(#panel.items>1 and "s" or "")),panel.x+(panel.width-100)/2,panel.sizeSlider.y+2,100,"center")
end

function editor.galleryItemSort(panel)
	editor.drawButton({
		w	= sidebar.minWidth/3-panel.row,
		h	= 18,
		x	= panel.x,
		y	= panel.y+panel.height-18,
		text= panel.sort,
		col = {r=0,g=0,b=0,a=0.3},
		tcol= {a=0.8},
		tip = "sort by "..panel.sort,
		func=function () 
			local items = {}
			for s,sort in ipairs(panel.sorts) do 
				table.insert(items,{name=sort, func=function() panel.sort=sort end})
			end
			editor.openContextMenu(items, {
				x = panel.x,
				y = panel.y+panel.height+8,
				width=sidebar.minWidth/3-panel.row
			})
		end
	}) 

	editor.drawButton({
		w	= panel.row,
		h	= 18,
		x	= panel.x+sidebar.minWidth/3-panel.row+2,
		y	= panel.y+panel.height-18,
		text= panel.sortAsc and "↑" or "↓",
		col = {r=0,g=0,b=0,a=0.3},
		tcol= {a=0.8},
		tip = panel.sortAsc and "Ascending Order" or "Descending Order",
		func=function () 
			panel.sortAsc = not panel.sortAsc
		end
	}) 
end

-----------------------------------------------------------
-- PROCESS
-----------------------------------------------------------

function editor.setIconColumns(panel) 
	panel.iconColumns 			= mf((panel.bounds.w-8)/panel.fullThumbSize)
end

function editor.processItems(panel)
	local items = {}
	for t,tag in ipairs(panel.tags) do
		items = mergeIndexed(items,tags[tag])
	end
	if #panel.items~=#items or panel.prevTags ~= lume.serialize(panel.tags) then
		panel.items = {}
		gfx.fontSize(math.max(8,panel.thumbSize/6.5))
		for k,key in ipairs(items) do 
			editor.getGalItem(panel,k,key)
		end
	end
	panel.prevTags = lume.serialize(panel.tags)
end

function editor.sortItems(panel)
	panel.sorts = lume.concat({"name","size","modified","created"}, (panel.searchTerm and panel.searchTerm~="") and {"search"} or {})
	panel.items = lume.sort(panel.items,function(a,b) 
		if a[panel.sort] == b[panel.sort] then
			return a.name>b.name
		else
			return (panel.sortAsc and a or b)[panel.sort]<(panel.sortAsc and b or a)[panel.sort] 
		end
	end)
end

function editor.updateItems(panel)
	gfx.setLineWidth(2)
	panel.anyHoveredItem = false

	gfx.fontSize(math.max(8,panel.thumbSize/6.5))
	local t=1
	for i,item in ipairs(panel.items) do 
		if i>1 and not(panel.searchTerm and item.search==0) then 
			item.id = t
			t 		= t+1
			editor.updateGalItem(panel,item)
		end
	end
	gfx.fontSize(10)
end

function editor.updateGalItem(panel,item)
	local row 		= mf((item.id-1)/panel.iconColumns)
	local column 	= item.id-row*panel.iconColumns-1
	local remainder = ((panel.bounds.w-12)/panel.fullThumbSize - panel.iconColumns) * thumbSize
	local gap 		= remainder/(panel.iconColumns+1)

	item.x 			= easeTo(item.x, panel.bounds.x+column*(panel.fullThumbSize+gap)+gap+4 					, item.x==0 and 1 or 2)
	item.y 			= easeTo(item.y, panel.bounds.y+row*(panel.fullThumbSize+panel.thumbSize/6.5)+(row+1)*(gap)-panel.tab.scrollPos+4 , item.y==0 and 1 or 2)
	item.width 		= panel.thumbSize
	item.w			= panel.thumbSize
	item.height 	= panel.thumbSize
	item.h	 		= panel.thumbSize

	item.hovered = (mouseIn(panel.bounds) and mouseIn(item))

	if #panel.selected == 1 and panel.selected[1]~=item then
		item.selected = 0
	end

	if item.hovered then 
		editor.setToolTip(item.data.name)
		panel.anyHoveredItem = true
		editor.clickGalItem(panel,item)
	end

	editor.drawGalleryItem(panel,item)
end

function editor.clickGalItem(panel,item)
	mouse.set(function()
		if cmdDown then 

			if item.selected>0  then 
				table.remove 		(panel.selected,item.selected)
				item.selected  		= 0
			else 
				table.insert 		(panel.selected,item)
				item.selected  		= #panel.selected
				droppingItem 		= true 
				dropScale			= 1	
			end

		elseif shiftDown then

			
		else
			panel.selected 		= {item}
			item.selected  		= #panel.selected
			droppingItem 		= true 
			dropScale			= 1	
		end
	end )

	mouse.setR(	function() 
		if item.selected==0 then 
			panel.selected 		= {item}
			item.selected  		= #panel.selected
		end
		editor.openGalleryItemContextMenu(panel,item) 	
	end,true)
end

function editor.showGalleryItem(item)
	editor.showInFinder(PROJ_DIR.."images".."/"..item.data.key..".png")
end


function editor.openGalleryItemContextMenu(panel,item)
	local obj = objects[item.key]
	editor.getSelectedTags(panel)
	editor.openContextMenu ({
		-- temp
		--{name="Approx. Def LQ", func = function() editor.approxDef(trueItem) 	end},
		#panel.selected>1 and 1 or 		
			{name="Show In Finder", func = function() editor.showGalleryItem(item) end  },
		#panel.selected>1 and 1 or 		
			{name="Rename", 		func = function() editor.renameObject(panel,item) end  },
		#panel.selected>1 and 1 or 		
			{name="Set Background", items = {
				{name="Level" , func = function() 
					currentSet.sky.image.key = item.key; 	
					currentSet.sky.image.anim = objects[item.key].default; 
					currentSet.sky.image.color = {a=1,r=1,g=1,b=1}
				end  },
				{name="Room" ,  func = function() 
					room.sky.image.key  = item.key; 	
					room.sky.image.anim = objects[item.key].default; 
					room.sky.image.color = {a=1,r=1,g=1,b=1}
				end  }
			}},
			--{name="Duplicate", 		func  = function() editor.duplicateGalleryItems(panel)  end },
		#panel.selectedTags==0 and 1 or 
			{name="Edit Tags", 		items = editor.editTags(panel)  },
		{name="Add Tags", 		items = editor.addTags(panel,obj),  col={r=0.25,g=0.8,b=0.35,a=0.2} },
		{name="Delete", 		func  = function() editor.deleteGalleryItems(panel) end , warning="close",col={r=0.8,g=0.25,b=0.2,a=0.4}},
	})
end

-----------------------------------------------------------
-- FUNCS
-----------------------------------------------------------
function editor.renameObject(panel,item)
	namer:typeString("Rename Object",objects[item.key].name,{x=item.x+item.width/2,y=item.y},
		function(x) 
			objects[item.key].name = x 
			item.name = x
		end
	)
end

function editor.deleteGalleryItems(panel) 
	--namer:warning("Delete",function() 
		for i,item in lume.ripairs(sortBy(panel.selected,"id")) do
			editor.deleteGalleryItem(item) 
			print(item.id)
		end
		panel.selected= {}
		editor.save 	()
	--end)
end

function editor.deleteGalleryItem(item)
	lume.remove 		(galleryPanel.items, item)
	editor.deleteImage 	(item.key)
end

function editor.deleteImage(key)
	editor.clearTags 	(key)
	local name = objects[key].name
	os.execute 			(os.rm().." \""..PROJ_DIR.."images/"..name..".png\"")
	os.execute 			(os.rm().." \""..PROJ_DIR.."images/"..name.."\"")
	print(os.rm().." "..PROJ_DIR.."images/"..name)
	lfs.remove 			("images/"..name..".dds")
	lfs.remove 			("thumbnails/"..name..".png")
	objects[key]   		= nil 
	allSymbols[key] 		= nil
	loader:saveDef 		("objects",objects)
end

function editor.duplicateGalleryItems(panel)
	for i,item in lume.ripairs(panel.selected) do
		editor.duplicateItem(item)
	end
end

function editor.duplicateItem(item)
	local file 				= lfs.newFile("images".."/"..item.folder.."/"..item.key..".png")
	local thumb 			= getImageThumb(item.key)
	local thumbFile 		= lfs.newFileData("thumbnails".."/"..item.key..".png")
	local newkey 			= editor.dropFile(file, item.key)

	log(item.key,"->",newkey)
	log(objects[item.key].name)
	objects[newkey] 		= deepcopy(objects[item.key])
	objects[newkey].name 	= item.data.name.." copy"
	objects[newkey].key 	= newkey
	deepcopy()
	loader:saveDef 			("objects",objects)
	allSymbols[newkey].thumb = thumb
	lfs.write("thumbnails".."/"..objects[newkey].key..".png", thumbFile)
end

function editor.getGalItem(panel,t,item)
	local item 	= 	{ 
		x 			= 0,
		y 			= 0,
		width 		= panel.thumbSize,
		height 		= panel.thumbSize,
		data 		= objects[item],
		hovered 	= false,
		selected	= 0,
		key 		= item,
		name 		= objects[item].name,
		size 		= objects[item].size,
		--modified 	= objects[item].modified,
		--created 	= objects[item].created,
		folder 		= getImageType(item),
		id 			= t
	}

	log("fsize",item.size)
	
	table.insert(panel.items,item)

	editor.updateGalItem(panel,item)
	
	return item
end

-----------------------------------------------------------
-- TAGS
-----------------------------------------------------------
function editor.getSelectedTags(panel)
	panel.selectedTags = {}
	for i,item in ipairs(panel.selected) do 
		panel.selectedTags = mergeIndexed(panel.selectedTags,item.data.tags)
	end
end

function editor.editTags(panel)
	local contextItems = {}
	for t,tag in ipairs(panel.selectedTags) do
		table.insert(contextItems, {	
			{
				size = 1,
				align= "center",
				name = "-", 		
				col  = {r=200,g=60,b=40,a=60},
				func = function()
					for i,item in lume.ripairs(panel.selected) do 
						if lume.remove(item.data.tags,tag) then
							lume.remove(tags[tag],item.data.key)
							panel.items[item.id]=nil
						end
					end 
				end
			},
			{ 	
				size = 5,
				name=tagNames[tag], 		
				func = function() 
					panel.tags = {tag}
				end 
			},
			{
				size = 1,
				align= "center",
				name = "+",
				col  = {r=60,g=200,b=90,a=40},
				func = function()
					for i,item in ipairs(panel.selected) do 
						table.add(item.data.tags,tag)
						table.add(tags[tag],item.data.key)
					end 
				end
			},
		})
	end
	return contextItems
end

function editor.addTags(panel,obj)
	local addTags = {}
	for t,tag in pairs(tagNames) do
		if not multicheckOR(t,{"Game","Level","Room"}) then
			table.insert(addTags, {
				name = tag, 		
				func = function()
					for i,item in ipairs(panel.selected) do
						table.add(item.data.tags,t) 
						tagImage(item.data) 
					end
				end 
			})
		end
	end
	table.insert(addTags, {	name="New Tag", 	func = function() namer:typeString("New tag","",{x=panel.x+panel.width/2,y=panel.bounds.y+panel.bounds.h/2,notri=true,backing=panel.bounds},function(x) 
		local id 		= uuid()
		tagNames[id] 	= x
		tags[id] 		= {} 
		editor.newTagTab(id)
		for i,item in ipairs(panel.selected) do
			table.insert 	(item.data.tags,id)
			tagImage 		(item.data) 
		end
		table.insert 	(panel.selectedTags,x)
	end) end,col={r=0.3,g=0.8,b=0.3,a=0.2}})
	return addTags
end

function editor.newTagTab(t)
	table.insert(
		galleryPanel.tabs,
		{	
			name 		= tagNames[t], 
			key 		= t, 	
			scrollPos 	= 0, 
			hovered 	= false, 
			selected 	= true 	
		}
	)
end

function editor.resetTagTabs(panel)
	panel.tabs={}
	for t,tag in ipairs({"Game","Level","Room"}) do
		editor.newTagTab(tag)
	end
	local sortedTags =  lume.keys(tags)
	table.sort(sortedTags, function(a, b) return a > b end)
	for t,tag in ipairs(lume.reject(sortedTags,function(x) return multicheckOR(x,{"Game","Level","Room"}) end)) do
		editor.newTagTab(tag)
	end
end

function editor.clearTags(key)
	if objects[key] then
		for t,tag in ipairs (mergeIndexed(objects[key].tags,{"Game"})) do
			if tags[tag] then 
				lume.remove(tags[tag], objects[key].key)
			end
		end
	end
end

function editor.processTab(panel,tab,noSplit)
	tab.width 	= noSplit and 16+gfx.currentFont:getWidth(tab.name) or ((panel.width-4)/lume.count(panel.tabs))-2
	local over 	= (noSplit and noSplit.x+noSplit.width+tab.width+4>panel.x+panel.width)
	tab.height 	= 16
	tab.y 		= over and noSplit.y+tab.height+2 or (noSplit and noSplit.y or panel.y+panel.height)
	tab.x 		= over and panel.x+2 or (noSplit and noSplit.x+noSplit.width+4 or panel.x+2+(tab.t-1)*(tab.width+2))
	tab.hovered	= mouseIn({ x=tab.x, y=tab.y, width=tab.width, height=tab.height })
	panel.height = over and panel.height + tab.height or panel.height
end

function editor.setTab(panel,tab)
	tab.selected= multicheckOR(tab.key,panel.tags)
	if tab.hovered then 
		lmouse.setCursor(hand)
		mouse.set(function() 
			if cmdDown then 
				if tab.selected and #panel.tags>1 then
					lume.remove(panel.tags,tab.key)
				else
					table.insert(panel.tags,tab.key)
				end
			else
				panel.tags 			= {tab.key}
			end
		end) 
		mouse.setR(function() 
			editor.openContextMenu ({
				multicheckOR(tab.key,{"Game","Level","Room"}) and 0 or {	
					name 	= "Rename", 
					func  	= function() namer:typeString("Rename tag",tab.name, {x=tab.x+tab.width/2,y=tab.y}, function(x) tab.name = x; tagNames[tab.key] = x end) end  
				},
				(#panel.tags==1 or multicheckOR(tab.key,{"Game","Level","Room"})) and 0 or { 
					name 	= "Merge",  
					func  	= function() editor.mergeTags(panel,tab.key) 				end  
				},
				multicheckOR(tab.key,{"Game","Level","Room"}) and 0 or {
					name 	= "Delete", 
					func 	= function() panel.delete = tab.selected and "" or tab.key end  ,
					col 	= {r=0.8,g=0.25,b=0.15,a=0.4}
				}
			})
		end)
	end
end

function editor.mergeTags(panel,key)
	for t,tag in lume.ripairs(lume.reject(panel.tags,function(x) return multicheckOR(x,{"Game","Level","Room",key}) end)) do
		for i,item in lume.ripairs(tags[tag]) do
			lume.remove(objects[item].tags,tag)
			table.add(objects[item].tags,key)
		end
		tags[key]  		= mergeIndexed(tags[key],tags[tag])
		tags[tag]  		= nil
		tagNames[tag] 	= nil
	end
	panel.tags 			= {key}
	editor.resetTagTabs (panel)
	imageLoader:tagImages()
end

function editor.deleteTags(panel)
	
	if not panel.delete then return end

	if panel.delete~="" then 
		tags[panel.delete] 		= nil 
		tagNames[panel.delete] 	= nil
		for t,tab in ipairs(panel.tabs) do
			if tab.key==panel.delete and not multicheckOR(tab.key,{"Game","Level","Room"}) then  
				panel.tabs[t]=nil
			end
		end
	else
		for t,tag in lume.ripairs(panel.tags) do
			if not multicheckOR(tag,{"Game","Level","Room"}) then 
				tags[tag] 		= nil 
				tagNames[tag] 	= nil
			end
		end
		panel.tags 		= {"Game"}
		editor.resetTagTabs(panel)
	end
	panel.delete 	= false
	imageLoader:tagImages()
end

function editor.getSelectedTab(panel)
	gfx.setLineWidth(1)
	for t,tab in ipairs(panel.tabs) do 
		if tags[tab.key] then 
			tab.t = t
			tab.empty = (lume.count(tags[tab.key])==0)
			editor.processTab(panel,tab,panel.tabs[t-1] or false)
			editor.setTab(panel,tab)
			editor.drawTabTitle(tab)
		end
	end
	panel.height = panel.height +  sidebar.titleHp
end

function editor.gallerySearch(panel)
	local searchbox = {x=panel.x+panel.width-panel.width/6,y=panel.y+sidebar.titleH/2,w=panel.width/3,h=18,notri=true,noshadow=true,align="right",amult=0.5,alert=false}

	if panel.searchTerm then 
	--	panel.searchFade=easeTo(panel.searchFade, panel.searchTerm and 1 or 0,4)
	--	editor.drawTip(lume.merge(searchbox,{text=panel.searchTerm,a=panel.searchFade}))
	end

	local search 		= (panel.searchTerm and panel.searchTerm~="")
	local searchTyping 	= (namer.enabled and namer.type == "Gallery Search")
	panel.searchButton 	= { 
		x	=	(search or searchTyping) and searchbox.x-searchbox.w/2 or panel.x+(panel.width-panel.row*1.1),
		y	=	(search or searchTyping) and searchbox.y-11 or panel.y,
		w	=	(search or searchTyping) and searchbox.w or panel.row,
		h	=	panel.row,
		text= 	(search or searchTyping) and panel.searchTerm or  "🔍",
		col = 	{a=panel.searchTerm and (searchTyping and 0.5 or 0.2) or 0},
		align= 	"right",
		func= 	not searchTyping and function() 
			panel.psort = panel.sort
			panel.searchButton.text = ""
			namer:typeString("Gallery Search",panel.searchTerm or "", {limit=searchbox.w/8,hidden=true},nil,
				function(x)
					panel.searchTerm = x
					panel.sort 		 = "search"
					if keypressed("escape") then 
						panel.searchTerm = nil
					end
				end
			)
		end  or nil
	}
	editor.drawButton(panel.searchButton)
	if (namer.enabled and namer.type == "Gallery Search") and panel.searchTerm=="" then
		panel.searchTerm=nil
	end
	if search or searchTyping then 
		editor.drawButton({ 
			x 	= panel.searchButton.x-panel.row+1,
			y	= panel.searchButton.y+1,
			h	= panel.searchButton.h-2,
			w 	= panel.row-2,
			col = {r=0.8,g=0.2,b=0.2,a=0.8},
			text="╳",
			func=function()
				panel.searchTerm=nil
			end
		})
	elseif panel.sort == "search" then 
		panel.sort = panel.psort
	end
end

function editor.search(items,query,key)
	if not query then return end
	local results = {}
	local test
	for r,result in pairs(items) do 
		test = key and result[key] or r
		--table.insert(results,{
		--	search 	= 	fuzzy(test, query),
		--	text 	= 	test
		--})
		result.search = fuzzy(test, query)
	end
	--results = lume.sort(results,"search")
	--trace (results )
	return results
end

---------------------------------
-- DRAW 
---------------------------------
function editor.drawTabTitle(tab)
	local dark = tab.selected and 0.6 or 0.25
	dark=dark*(tab.empty and 0.5 or 1)
	gfx.setColor(0,0,0, dark*(tab.hovered and 1.5 or 1) )
	gfx.rectangle(	"fill", mf(tab.x), tab.y, tab.width, tab.height,	2)
	if tab.selected then 
		gfx.setColor(1,1,1,0.5 )
		gfx.rectangle(	"line", mf(tab.x), tab.y, tab.width, tab.height,	2)
	end

	gfx.setColor(1,1,1,tab.empty and 0.2 or 1 )
	gfx.printf(tab.name, mf(tab.x+4), mf(tab.y+tab.height/8), tab.width-8, "center")
	
end

function editor.drawGalleryBack(panel)
	gfx.setScissor(panel.bounds.x,panel.bounds.y,panel.bounds.w,panel.bounds.h)
end

function editor.drawGalleryItem(panel,item)
	local rounding = 3+(panel.thumbSize/thumbSize) *3
	gfx.setColor(1,1,1,220*panel.alpha) 
	gfx.draw(getImage("checkersRounded"),mf(item.x),mf(item.y),0,panel.thumbSize/thumbSize,panel.thumbSize/thumbSize)
	
	local backCol = (item.selected>0) and 0.7 or (item.hovered and 0.4 or 0)
	gfx.setColor(1,1,1,backCol) 
	gfx.rectangle(	"fill", mf(item.x), mf(item.y), item.width, item.height,	rounding) 

	if getImageThumb(item.key)~=nil then
		gfx.setColor(1,1,1,1) 
		gfx.setBlendMode('alpha','premultiplied')
		if getImageThumb(item.key) ~= getImage("checkers") then
			local w,h = getImageThumb(item.key):getWidth(), getImageThumb(item.key):getHeight()
			gfx.draw(getImageThumb(item.key),mf(item.x),mf(item.y),0,panel.thumbSize/w,panel.thumbSize/h)
		end
		gfx.setBlendMode('alpha')
	end
	local lineCol = 1
	-- if (panel.searchTerm and panel.searchTerm~="") or (namer.enabled and namer.type == "Gallery Search") then 
		gfx.setColor(1,1,1,1)
		gfx.printf(ellipses(item.name,item.width-4),mf(item.x), mf(item.y+item.height)+1,item.width-4,"center")
	-- end
	if item.selected>0 then 
		gfx.setColor(1,1,1,lineCol) -- OVER OUTLINE ITEM (WHITE)
		gfx.rectangle(	"line", mf(item.x), mf(item.y), item.width, item.height,	rounding)
		gfx.setColor(0,0,0,lineCol) -- OVER OUTLINE ITEM (WHITE)
		gfx.rectangle(	"line", mf(item.x)-1, mf(item.y)-1, item.width+2, item.height+2,	rounding)
	end
end

function editor.drawGalleryShadow(panel)
	gfx.setColor(1,1,1,0.7)
	local shadowOffsetTop = 0
	shadowOffsetTop = mc(-128,panel.tab.scrollPos-128,-64) 
	gfx.draw(getImage("panelShadowH"),panel.bounds.x-64,panel.bounds.y+shadowOffsetTop,0,1,1)

	local shadowOffsetBot 	= 0
	local rows 				= mu(#panel.items/panel.iconColumns)
	
	shadowOffsetBot 		= mc(32,(panel.tab.maxScroll-panel.tab.scrollPos),64) 
	gfx.draw(getImage("panelShadowH"),panel.bounds.x-64,panel.bounds.y+panel.bounds.h-shadowOffsetBot,0,1,1)
end

function editor.drawGalleryCanvas(panel)
	gfx.setScissor()

	gfx.setColor(0.05,0.05,0.05,0.1 )
	gfx.setLineWidth(4)
	--temp
	gfx.rectangle(	"line", panel.bounds.x+2, panel.bounds.y, panel.bounds.w-4, panel.bounds.h-2,	1)
	panel.height = panel.height+panel.bounds.h+8
end

