Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/studiopalette.h"
Toshihiro Shimizu 890ddd
#include "tfiletype.h"
Toshihiro Shimizu 890ddd
#include "tstream.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
#include "tlevel_io.h"
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
Toshihiro Shimizu 890ddd
#include "traster.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "tcolorstyles.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzfolders.h"
Toshihiro Shimizu 890ddd
#include "toonz/tproject.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzscene.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <time.h></time.h>
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#include <sys types.h=""></sys>
Toshihiro Shimizu 890ddd
#include <sys timeb.h=""></sys>
Toshihiro Shimizu 890ddd
#include <string.h></string.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath makeUniqueName(TFilePath fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (TFileStatus(fp).doesExist() == false)
Toshihiro Shimizu 890ddd
		return fp;
Shinya Kitaoka 3bfa54
	std::wstring name = fp.getWideName();
Toshihiro Shimizu 890ddd
	int index = 2;
Toshihiro Shimizu 890ddd
	int j = name.find_last_not_of(L"0123456789");
Shinya Kitaoka 3bfa54
	if (j != (int)std::wstring::npos && j + 1 < (int)name.length()) {
Toshihiro Shimizu 890ddd
		index = toInt(name.substr(j + 1)) + 1;
Toshihiro Shimizu 890ddd
		name = name.substr(0, j + 1);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	for (;;) {
Toshihiro Shimizu 890ddd
		fp = fp.withName(name + toWideString(index));
Toshihiro Shimizu 890ddd
		if (TFileStatus(fp).doesExist() == false)
Toshihiro Shimizu 890ddd
			return fp;
Toshihiro Shimizu 890ddd
		index++;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPalette *loadPliPalette(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TLevelReaderP lr(fp);
Toshihiro Shimizu 890ddd
	TLevelP level = lr->loadInfo();
Toshihiro Shimizu 890ddd
	int frameCount = level->getFrameCount();
Toshihiro Shimizu 890ddd
	if (frameCount < 1)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	TPalette *palette = level->getPalette();
Toshihiro Shimizu 890ddd
	if (!palette)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	return palette->clone();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPalette *loadTplPalette(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPersist *p = 0;
Toshihiro Shimizu 890ddd
	TIStream is(fp);
Toshihiro Shimizu 890ddd
	is >> p;
Toshihiro Shimizu 890ddd
	TPalette *palette = dynamic_cast<tpalette *="">(p);</tpalette>
Toshihiro Shimizu 890ddd
	return palette;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPalette *loadToonz46Palette(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TImageP pltImg;
Toshihiro Shimizu 890ddd
	TImageReader::load(fp, pltImg);
Toshihiro Shimizu 890ddd
	if (!pltImg)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	TRasterImageP pltRasImg(pltImg);
Toshihiro Shimizu 890ddd
	if (!pltRasImg)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	TRaster32P rasPlt = pltRasImg->getRaster();
Toshihiro Shimizu 890ddd
	if (!rasPlt)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	TPalette *palette = new TPalette();
Toshihiro Shimizu 890ddd
	const int offset = 0; // FirstUserStyle-1;
Toshihiro Shimizu 890ddd
	assert(rasPlt->getLy() == 2);
Toshihiro Shimizu 890ddd
	rasPlt->lock();
Toshihiro Shimizu 890ddd
	TPixel32 *pixelRow = rasPlt->pixels(0);
Toshihiro Shimizu 890ddd
	int x;
Toshihiro Shimizu 890ddd
	for (x = 1; x < rasPlt->getLx(); ++x) {
Toshihiro Shimizu 890ddd
		TPixel32 color = pixelRow[x];
Toshihiro Shimizu 890ddd
		int styleId = offset + x;
Toshihiro Shimizu 890ddd
		if (styleId < palette->getStyleCount())
Toshihiro Shimizu 890ddd
			palette->setStyle(styleId, color);
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			int j = palette->addStyle(color);
Toshihiro Shimizu 890ddd
			assert(j == styleId);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// aggiungo solo i colori usati (salvo il BG)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	pixelRow = rasPlt->pixels(1);
Toshihiro Shimizu 890ddd
	TPalette::Page *page = palette->getPage(0);
Toshihiro Shimizu 890ddd
	for (x = 1; x < rasPlt->getLx(); ++x) {
Toshihiro Shimizu 890ddd
		if (pixelRow[x].r == 255)
Toshihiro Shimizu 890ddd
			page->addStyle(offset + x); // palette->addStyleToPage(offset+x, L"colors");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	rasPlt->unlock();
Toshihiro Shimizu 890ddd
	return palette;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
std::wstring readPaletteGlobalName(TFilePath path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		TIStream is(path);
Toshihiro Shimizu 890ddd
		if (!is)
Toshihiro Shimizu 890ddd
			return L"";
Shinya Kitaoka 3bfa54
		std::string tagName;
Toshihiro Shimizu 890ddd
		if (!is.matchTag(tagName) || tagName != "palette")
Toshihiro Shimizu 890ddd
			return L"";
Shinya Kitaoka 3bfa54
		std::string name;
Toshihiro Shimizu 890ddd
		if (is.getTagParam("name", name))
Toshihiro Shimizu 890ddd
			return toWideString(name);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return L"";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
TFilePath searchPalette(TFilePath path, std::wstring paletteId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePathSet q;
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		TSystem::readDirectory(q, path);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (TFilePathSet::iterator i = q.begin(); i != q.end(); ++i) {
Toshihiro Shimizu 890ddd
		TFilePath fp = *i;
Toshihiro Shimizu 890ddd
		if (fp.getType() == "tpl") {
Shinya Kitaoka 3bfa54
			std::wstring gname = readPaletteGlobalName(fp);
Toshihiro Shimizu 890ddd
			if (gname == paletteId)
Toshihiro Shimizu 890ddd
				return fp;
Toshihiro Shimizu 890ddd
		} else if (TFileStatus(fp).isDirectory()) {
Toshihiro Shimizu 890ddd
			TFilePath palettePath = searchPalette(fp, paletteId);
Toshihiro Shimizu 890ddd
			if (palettePath != TFilePath())
Toshihiro Shimizu 890ddd
				return palettePath;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return TFilePath();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool studioPaletteHasBeenReferred = false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
static std::map<std::wstring, tfilepath=""> table;</std::wstring,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// StudioPalette
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
StudioPalette::StudioPalette()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		m_root = ToonzFolder::getStudioPaletteFolder();
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (!TFileStatus(m_root).doesExist()) {
Toshihiro Shimizu 890ddd
		try {
Toshihiro Shimizu 890ddd
			TSystem::mkDir(m_root);
Toshihiro Shimizu 890ddd
			FolderListenerManager::instance()->notifyFolderChanged(m_root.getParentDir());
Toshihiro Shimizu 890ddd
		} catch (...) {
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		try {
Toshihiro Shimizu 890ddd
			TSystem::mkDir(getLevelPalettesRoot());
Toshihiro Shimizu 890ddd
			FolderListenerManager::instance()->notifyFolderChanged(getLevelPalettesRoot().getParentDir());
Toshihiro Shimizu 890ddd
		} catch (...) {
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
StudioPalette::~StudioPalette()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool StudioPalette::m_enabled = true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StudioPalette::enable(bool enabled)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(studioPaletteHasBeenReferred == false);
Toshihiro Shimizu 890ddd
	m_enabled = enabled;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
StudioPalette *StudioPalette::instance()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	static StudioPalette _instance;
Toshihiro Shimizu 890ddd
	studioPaletteHasBeenReferred = true;
Toshihiro Shimizu 890ddd
	assert(m_enabled);
Toshihiro Shimizu 890ddd
	return &_instance;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath StudioPalette::getLevelPalettesRoot()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_root + "Toonz Palettes";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath StudioPalette::getProjectPalettesRoot()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TProjectP p = TProjectManager::instance()->getCurrentProject();
Toshihiro Shimizu 890ddd
	TFilePath folderName = p->getFolder(TProject::Palettes);
Toshihiro Shimizu 890ddd
	if (folderName.isEmpty())
Toshihiro Shimizu 890ddd
		return TFilePath();
Toshihiro Shimizu 890ddd
	if (folderName.isAbsolute())
Toshihiro Shimizu 890ddd
		return folderName;
Toshihiro Shimizu 890ddd
	return p->getProjectFolder() + folderName;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool loadRefImg(TPalette *palette, TFilePath dir)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(palette);
Toshihiro Shimizu 890ddd
	TFilePath fp = palette->getRefImgPath();
Toshihiro Shimizu 890ddd
	if (fp == TFilePath() || !TSystem::doesExistFileOrLevel(fp))
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	if (!fp.isAbsolute())
Toshihiro Shimizu 890ddd
		fp = dir + fp;
Toshihiro Shimizu 890ddd
	TLevelReaderP lr(fp);
Toshihiro Shimizu 890ddd
	if (!lr)
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	TLevelP level = lr->loadInfo();
Toshihiro Shimizu 890ddd
	if (!level || level->getFrameCount() == 0)
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	TLevel::Iterator it = level->begin();
Toshihiro Shimizu 890ddd
	TImageP img = lr->getFrameReader(it->first)->load();
Toshihiro Shimizu 890ddd
	if (!img)
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	img->setPalette(0);
Toshihiro Shimizu 890ddd
	palette->setRefImg(img);
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPalette *StudioPalette::getPalette(
Toshihiro Shimizu 890ddd
	const TFilePath &path,
Toshihiro Shimizu 890ddd
	bool loadRefImgFlag)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		if (path.getType() != "tpl")
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
		TPalette *palette = load(path);
Toshihiro Shimizu 890ddd
		if (!palette)
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
		if (loadRefImgFlag)
Toshihiro Shimizu 890ddd
			loadRefImg(palette, path.getParentDir());
Toshihiro Shimizu 890ddd
		// palette->addRef(); // ci va??
Toshihiro Shimizu 890ddd
		return palette;
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StudioPalette::movePalette(const TFilePath &dstPath, const TFilePath &srcPath)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		TSystem::renameFile(dstPath, srcPath);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Shinya Kitaoka 3bfa54
	std::wstring id = readPaletteGlobalName(dstPath);
Toshihiro Shimizu 890ddd
	table.erase(id);
Toshihiro Shimizu 890ddd
	FolderListenerManager::instance()->notifyFolderChanged(dstPath.getParentDir());
Toshihiro Shimizu 890ddd
	notifyMove(dstPath, srcPath);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int StudioPalette::getChildren(std::vector<tfilepath> &fps, const TFilePath &folderPath)</tfilepath>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePathSet q;
Toshihiro Shimizu 890ddd
	if (TFileStatus(folderPath).isDirectory()) {
Toshihiro Shimizu 890ddd
		try {
Toshihiro Shimizu 890ddd
			TSystem::readDirectory(q, folderPath, false, false);
Toshihiro Shimizu 890ddd
		} catch (...) {
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (TFilePathSet::iterator i = q.begin(); i != q.end(); ++i)
Toshihiro Shimizu 890ddd
		if (isFolder(*i) || isPalette(*i))
Toshihiro Shimizu 890ddd
			fps.push_back(*i);
Toshihiro Shimizu 890ddd
	//  fps.push_back(m_root+"butta.tpl");
Toshihiro Shimizu 890ddd
	return fps.size();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int StudioPalette::getChildCount(const TFilePath &folderPath)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePathSet q;
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		TSystem::readDirectory(q, folderPath);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return q.size();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool StudioPalette::isFolder(const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return TFileStatus(path).isDirectory();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool StudioPalette::isReadOnly(const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return !TFileStatus(path).isWritable();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool StudioPalette::isPalette(const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return path.getType() == "tpl";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! check if the palette is studio palette or level palette in order to separate icons in the StudioPaletteTree.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
bool StudioPalette::hasGlobalName(const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return (readPaletteGlobalName(path) != L"");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool StudioPalette::isLevelPalette(const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPalette *palette = getPalette(path);
Toshihiro Shimizu 890ddd
	if (!palette)
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	bool ret = !palette->isCleanupPalette();
Toshihiro Shimizu 890ddd
	delete palette;
Toshihiro Shimizu 890ddd
	return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath StudioPalette::createFolder(const TFilePath &parentFolderPath)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath path = makeUniqueName(parentFolderPath + "new folder");
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		TSystem::mkDir(path);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		return TFilePath();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	FolderListenerManager::instance()->notifyFolderChanged(parentFolderPath);
Toshihiro Shimizu 890ddd
	notifyTreeChange();
Toshihiro Shimizu 890ddd
	return path;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
void StudioPalette::createFolder(const TFilePath &parentFolderPath, std::wstring name)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath fp = parentFolderPath + name;
Toshihiro Shimizu 890ddd
	if (TFileStatus(fp).doesExist())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		TSystem::mkDir(fp);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	FolderListenerManager::instance()->notifyFolderChanged(parentFolderPath);
Toshihiro Shimizu 890ddd
	notifyTreeChange();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
TFilePath StudioPalette::createPalette(const TFilePath &folderPath, std::string name)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPalette *palette = 0;
Toshihiro Shimizu 890ddd
	if (name == "")
Toshihiro Shimizu 890ddd
		name = "new palette";
Toshihiro Shimizu 890ddd
	palette = new TPalette();
Toshihiro Shimizu 890ddd
	TFilePath fp = makeUniqueName(folderPath + (name + ".tpl"));
Toshihiro Shimizu 890ddd
	time_t ltime;
Toshihiro Shimizu 890ddd
	time(<ime);
Shinya Kitaoka 3bfa54
	std::wstring gname = toWideString((int)ltime) + L"_" + toWideString(rand());
Toshihiro Shimizu 890ddd
	palette->setGlobalName(gname);
Toshihiro Shimizu 890ddd
	setStylesGlobalNames(palette);
Toshihiro Shimizu 890ddd
	save(fp, palette);
Toshihiro Shimizu 890ddd
	delete palette;
Toshihiro Shimizu 890ddd
	notifyTreeChange();
Toshihiro Shimizu 890ddd
	return fp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StudioPalette::setPalette(const TFilePath &palettePath, const TPalette *plt, bool notifyPaletteChanged)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(palettePath.getType() == "tpl");
Toshihiro Shimizu 890ddd
	TPalette *palette = plt->clone();
Toshihiro Shimizu 890ddd
	palette->setIsLocked(plt->isLocked());
Toshihiro Shimizu 890ddd
	palette->addRef();
Shinya Kitaoka 3bfa54
	std::wstring pgn = palette->getGlobalName();
Toshihiro Shimizu 890ddd
	if (TFileStatus(palettePath).doesExist())
Toshihiro Shimizu 890ddd
		pgn = readPaletteGlobalName(palettePath);
Toshihiro Shimizu 890ddd
	palette->setGlobalName(pgn);
Toshihiro Shimizu 890ddd
	setStylesGlobalNames(palette);
Toshihiro Shimizu 890ddd
	save(palettePath, palette);
Toshihiro Shimizu 890ddd
	palette->release();
Toshihiro Shimizu 890ddd
	if (notifyPaletteChanged)
Toshihiro Shimizu 890ddd
		notifyPaletteChange(palettePath);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StudioPalette::deletePalette(const TFilePath &palettePath)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(palettePath.getType() == "tpl");
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		TSystem::deleteFile(palettePath);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	notifyTreeChange();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StudioPalette::deleteFolder(const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		TSystem::rmDirTree(path);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	notifyTreeChange();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath StudioPalette::importPalette(
Toshihiro Shimizu 890ddd
	const TFilePath &dstFolder,
Toshihiro Shimizu 890ddd
	const TFilePath &srcPath)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPaletteP palette;
Shinya Kitaoka 3bfa54
	std::string ext = srcPath.getType();
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		if (ext == "plt")
Toshihiro Shimizu 890ddd
			palette = loadToonz46Palette(srcPath);
Toshihiro Shimizu 890ddd
		else if (ext == "pli")
Toshihiro Shimizu 890ddd
			palette = loadPliPalette(srcPath);
Toshihiro Shimizu 890ddd
		else if (ext == "tpl")
Toshihiro Shimizu 890ddd
			palette = loadTplPalette(srcPath);
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!palette)
Toshihiro Shimizu 890ddd
		return TFilePath();
Shinya Kitaoka 3bfa54
	std::wstring name = srcPath.getWideName();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(!palette->isCleanupPalette());
Toshihiro Shimizu 890ddd
	//    convertToLevelPalette(palette.getPointer());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFilePath fp = makeUniqueName(dstFolder + (name + L".tpl"));
Toshihiro Shimizu 890ddd
	time_t ltime;
Toshihiro Shimizu 890ddd
	time(<ime);
Shinya Kitaoka 3bfa54
	std::wstring gname = toWideString((int)ltime) + L"_" + toWideString(rand());
Toshihiro Shimizu 890ddd
	palette->setGlobalName(gname);
Toshihiro Shimizu 890ddd
	setStylesGlobalNames(palette.getPointer());
Toshihiro Shimizu 890ddd
	TSystem::touchParentDir(fp);
Toshihiro Shimizu 890ddd
	save(fp, palette.getPointer());
Toshihiro Shimizu 890ddd
	notifyTreeChange();
Toshihiro Shimizu 890ddd
	return fp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//TFilePath StudioPalette::getRefImage(const TFilePath palette)
Toshihiro Shimizu 890ddd
//{
Toshihiro Shimizu 890ddd
//  return palette.withType("pli");
Toshihiro Shimizu 890ddd
//}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
void foobar(std::wstring paletteId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	table.erase(paletteId);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
TFilePath StudioPalette::getPalettePath(std::wstring paletteId)
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::map<std::wstring, tfilepath="">::iterator it = table.find(paletteId);</std::wstring,>
Toshihiro Shimizu 890ddd
	if (it != table.end())
Toshihiro Shimizu 890ddd
		return it->second;
Toshihiro Shimizu 890ddd
	TFilePath fp = searchPalette(m_root, paletteId);
Toshihiro Shimizu 890ddd
	if (fp == TFilePath()) {
Toshihiro Shimizu 890ddd
		fp = searchPalette(getProjectPalettesRoot(), paletteId);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	table[paletteId] = fp;
Toshihiro Shimizu 890ddd
	return fp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
TPalette *StudioPalette::getPalette(std::wstring paletteId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath palettePath = getPalettePath(paletteId);
Toshihiro Shimizu 890ddd
	if (palettePath != TFilePath())
Toshihiro Shimizu 890ddd
		return getPalette(palettePath);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
TColorStyle *StudioPalette::getStyle(std::wstring styleId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
std::pair<tfilepath, int=""> StudioPalette::getSourceStyle(TColorStyle *cs)</tfilepath,>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	std::pair<tfilepath, int=""> ret(TFilePath(), -1);</tfilepath,>
Toshihiro Shimizu 890ddd
	if (!cs)
Toshihiro Shimizu 890ddd
		return ret;
Shinya Kitaoka 3bfa54
	std::wstring gname = cs->getGlobalName();
Toshihiro Shimizu 890ddd
	if (gname == L"")
Toshihiro Shimizu 890ddd
		return ret;
Toshihiro Shimizu 890ddd
	int k = gname.find_first_of(L'-', 1);
Shinya Kitaoka 3bfa54
	if (k == (int)std::wstring::npos)
Toshihiro Shimizu 890ddd
		return ret;
Shinya Kitaoka 3bfa54
	std::wstring paletteId = gname.substr(1, k - 1);
Toshihiro Shimizu 890ddd
	ret.first = getPalettePath(paletteId) - m_root;
Toshihiro Shimizu 890ddd
	ret.second = toInt(gname.substr(k + 1));
Toshihiro Shimizu 890ddd
	return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! return if any style in the palette is changed
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
bool StudioPalette::updateLinkedColors(TPalette *palette)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	bool paletteIsChanged = false;
Shinya Kitaoka 3bfa54
	std::map<std::wstring, tpalettep=""> table;</std::wstring,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < palette->getStyleCount(); i++) {
Toshihiro Shimizu 890ddd
		TColorStyle *cs = palette->getStyle(i);
Shinya Kitaoka 3bfa54
		std::wstring gname = cs->getGlobalName();
Toshihiro Shimizu 890ddd
		if (gname == L"" || gname[0] != L'+')
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		int k = gname.find_first_of(L'-', 1);
Shinya Kitaoka 3bfa54
		if (k == (int)std::wstring::npos)
Toshihiro Shimizu 890ddd
			continue;
Shinya Kitaoka 3bfa54
		std::wstring paletteId = gname.substr(1, k - 1);
Shinya Kitaoka 3bfa54
		std::map<std::wstring, tpalettep="">::iterator it;</std::wstring,>
Toshihiro Shimizu 890ddd
		it = table.find(paletteId);
Toshihiro Shimizu 890ddd
		TPalette *spPalette = 0;
Toshihiro Shimizu 890ddd
		if (it == table.end()) {
Toshihiro Shimizu 890ddd
			spPalette =
Toshihiro Shimizu 890ddd
				StudioPalette::instance()
Toshihiro Shimizu 890ddd
					->getPalette(paletteId);
Toshihiro Shimizu 890ddd
			if (!spPalette)
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
			table[paletteId] = spPalette;
Toshihiro Shimizu 890ddd
			// spPalette->release();
Toshihiro Shimizu 890ddd
			assert(spPalette->getRefCount() == 1);
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			spPalette = it->second.getPointer();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int j = toInt(gname.substr(k + 1));
Toshihiro Shimizu 890ddd
		if (spPalette && 0 <= j && j < spPalette->getStyleCount()) {
Toshihiro Shimizu 890ddd
			TColorStyle *spStyle = spPalette->getStyle(j);
Toshihiro Shimizu 890ddd
			assert(spStyle);
Toshihiro Shimizu 890ddd
			spStyle = spStyle->clone();
Toshihiro Shimizu 890ddd
			spStyle->setGlobalName(gname);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			//put the style name in the studio palette into the original name
Toshihiro Shimizu 890ddd
			spStyle->setOriginalName(spStyle->getName());
Toshihiro Shimizu 890ddd
			//.. and keep the style name unchanged
Toshihiro Shimizu 890ddd
			spStyle->setName(cs->getName());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			palette->setStyle(i, spStyle);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			paletteIsChanged = true;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return paletteIsChanged;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StudioPalette::setStylesGlobalNames(TPalette *palette)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (int i = 0; i < palette->getStyleCount(); i++) {
Toshihiro Shimizu 890ddd
		TColorStyle *cs = palette->getStyle(i);
Toshihiro Shimizu 890ddd
		// set global name only to the styles of which the global name is empty
Toshihiro Shimizu 890ddd
		if (cs->getGlobalName() == L"") {
Shinya Kitaoka 3bfa54
			std::wstring gname = L"-" + palette->getGlobalName() + L"-" + toWideString(i);
Toshihiro Shimizu 890ddd
			cs->setGlobalName(gname);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StudioPalette::save(const TFilePath &path, TPalette *palette)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TOStream os(path);
Shinya Kitaoka 3bfa54
	std::map<std::string, std::string=""> attr;</std::string,>
Toshihiro Shimizu 890ddd
	attr["name"] = toString(palette->getGlobalName());
Toshihiro Shimizu 890ddd
	os.openChild("palette", attr);
Toshihiro Shimizu 890ddd
	palette->saveData(os);
Toshihiro Shimizu 890ddd
	os.closeChild();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPalette *StudioPalette::load(const TFilePath &path)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		TIStream is(path);
Toshihiro Shimizu 890ddd
		if (!is)
Toshihiro Shimizu 890ddd
			return 0;
Shinya Kitaoka 3bfa54
		std::string tagName;
Toshihiro Shimizu 890ddd
		if (!is.matchTag(tagName) || tagName != "palette")
Toshihiro Shimizu 890ddd
			return 0;
Shinya Kitaoka 3bfa54
		std::string gname;
Toshihiro Shimizu 890ddd
		is.getTagParam("name", gname);
Toshihiro Shimizu 890ddd
		TPalette *palette = new TPalette();
Toshihiro Shimizu 890ddd
		palette->loadData(is);
Toshihiro Shimizu 890ddd
		palette->setGlobalName(toWideString(gname));
Toshihiro Shimizu 890ddd
		is.matchEndTag();
Toshihiro Shimizu 890ddd
		palette->setPaletteName(path.getWideName());
Toshihiro Shimizu 890ddd
		return palette;
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StudioPalette::addListener(Listener *listener)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (std::find(m_listeners.begin(), m_listeners.end(), listener) == m_listeners.end())
Toshihiro Shimizu 890ddd
		m_listeners.push_back(listener);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StudioPalette::removeListener(Listener *listener)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_listeners.erase(
Toshihiro Shimizu 890ddd
		std::remove(m_listeners.begin(), m_listeners.end(), listener),
Toshihiro Shimizu 890ddd
		m_listeners.end());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StudioPalette::notifyTreeChange()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (std::vector<listener *="">::iterator it = m_listeners.begin(); it != m_listeners.end(); ++it)</listener>
Toshihiro Shimizu 890ddd
		(*it)->onStudioPaletteTreeChange();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StudioPalette::notifyMove(const TFilePath &dstPath, const TFilePath &srcPath)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (std::vector<listener *="">::iterator it = m_listeners.begin(); it != m_listeners.end(); ++it)</listener>
Toshihiro Shimizu 890ddd
		(*it)->onStudioPaletteMove(dstPath, srcPath);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StudioPalette::notifyPaletteChange(const TFilePath &palette)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (std::vector<listener *="">::iterator it = m_listeners.begin(); it != m_listeners.end(); ++it)</listener>
Toshihiro Shimizu 890ddd
		(*it)->onStudioPaletteChange(palette);
Toshihiro Shimizu 890ddd
}