Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tsimplecolorstyles.h"
Toshihiro Shimizu 890ddd
#include "timage_io.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
#include "tvectorimage.h"
Toshihiro Shimizu 890ddd
#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "tstream.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Qt includes
Toshihiro Shimizu 890ddd
#include <qmutexlocker></qmutexlocker>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
Campbell Barton 40cabe
#include <memory></memory>
Campbell Barton 40cabe
Toshihiro Shimizu 890ddd
PERSIST_IDENTIFIER(TPalette, "palette")
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPersistDeclarationT<tpalette> auxPaletteDeclaration("vectorpalette");</tpalette>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
DEFINE_CLASS_CODE(TPalette, 30)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*************************************************************************************
Toshihiro Shimizu 890ddd
//    Local namespace  stuff
Toshihiro Shimizu 890ddd
//*************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const int maxStyleIndex = 32765;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// TPalette::Page
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPalette::Page::Page(wstring name)
Toshihiro Shimizu 890ddd
	: m_name(name), m_index(-1), m_palette(0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TColorStyle *TPalette::Page::getStyle(int index) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(m_palette);
Toshihiro Shimizu 890ddd
	if (0 <= index && index < getStyleCount())
Toshihiro Shimizu 890ddd
		return m_palette->getStyle(m_styleIds[index]);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TPalette::Page::getStyleId(int index) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(m_palette);
Toshihiro Shimizu 890ddd
	if (0 <= index && index < getStyleCount())
Toshihiro Shimizu 890ddd
		return m_styleIds[index];
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TPalette::Page::addStyle(int styleId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(m_palette);
Toshihiro Shimizu 890ddd
	if (styleId < 0 || styleId >= m_palette->getStyleCount())
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	if (m_palette->m_styles[styleId].first != 0)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	m_palette->m_styles[styleId].first = this;
Toshihiro Shimizu 890ddd
	int indexInPage = int(m_styleIds.size());
Toshihiro Shimizu 890ddd
	m_styleIds.push_back(styleId);
Toshihiro Shimizu 890ddd
	return indexInPage;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TPalette::Page::addStyle(TColorStyle *style)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(m_palette);
Toshihiro Shimizu 890ddd
	int stylesCount = int(m_palette->m_styles.size());
Toshihiro Shimizu 890ddd
	int styleId;
Toshihiro Shimizu 890ddd
	for (styleId = 0; styleId < stylesCount; styleId++)
Toshihiro Shimizu 890ddd
		if (m_palette->m_styles[styleId].first == 0)
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
	if (styleId >= stylesCount - 1)
Toshihiro Shimizu 890ddd
		return addStyle(m_palette->addStyle(style));
Toshihiro Shimizu 890ddd
	m_palette->setStyle(styleId, style);
Toshihiro Shimizu 890ddd
	return addStyle(styleId);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TPalette::Page::addStyle(TPixel32 color)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return addStyle(new TSolidColorStyle(color));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::Page::insertStyle(int indexInPage, int styleId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(m_palette);
Toshihiro Shimizu 890ddd
	if (styleId < 0 || styleId >= m_palette->getStyleCount())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (m_palette->m_styles[styleId].first != 0)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	m_palette->m_styles[styleId].first = this;
Toshihiro Shimizu 890ddd
	if (indexInPage < 0)
Toshihiro Shimizu 890ddd
		indexInPage = 0;
Toshihiro Shimizu 890ddd
	else if (indexInPage > getStyleCount())
Toshihiro Shimizu 890ddd
		indexInPage = getStyleCount();
Toshihiro Shimizu 890ddd
	m_styleIds.insert(m_styleIds.begin() + indexInPage, styleId);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::Page::insertStyle(int indexInPage, TColorStyle *style)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(m_palette);
Toshihiro Shimizu 890ddd
	int styleId = m_palette->addStyle(style);
Toshihiro Shimizu 890ddd
	if (styleId >= 0)
Toshihiro Shimizu 890ddd
		insertStyle(indexInPage, styleId);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::Page::insertStyle(int indexInPage, TPixel32 color)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(m_palette);
Toshihiro Shimizu 890ddd
	int styleId = m_palette->addStyle(color);
Toshihiro Shimizu 890ddd
	if (styleId >= 0)
Toshihiro Shimizu 890ddd
		insertStyle(indexInPage, styleId);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::Page::removeStyle(int indexInPage)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (indexInPage < 0 || indexInPage >= getStyleCount())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	assert(m_palette);
Toshihiro Shimizu 890ddd
	int styleId = getStyleId(indexInPage);
Toshihiro Shimizu 890ddd
	assert(0 <= styleId && styleId < m_palette->getStyleCount());
Toshihiro Shimizu 890ddd
	assert(m_palette->m_styles[styleId].first == this);
Toshihiro Shimizu 890ddd
	m_palette->m_styles[styleId].first = 0;
Toshihiro Shimizu 890ddd
	m_styleIds.erase(m_styleIds.begin() + indexInPage);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TPalette::Page::search(int styleId) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	std::vector<int>::const_iterator it =</int>
Toshihiro Shimizu 890ddd
		std::find(m_styleIds.begin(), m_styleIds.end(), styleId);
Toshihiro Shimizu 890ddd
	if (it == m_styleIds.end())
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return it - m_styleIds.begin();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TPalette::Page::search(TColorStyle *style) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(style);
Toshihiro Shimizu 890ddd
	assert(m_palette);
Toshihiro Shimizu 890ddd
	for (int i = 0; i < getStyleCount(); i++)
Toshihiro Shimizu 890ddd
		if (m_palette->getStyle(m_styleIds[i]) == style)
Toshihiro Shimizu 890ddd
			return i;
Toshihiro Shimizu 890ddd
	return -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// TPalette
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPalette::TPalette()
Toshihiro Shimizu 890ddd
	: m_version(0), m_isCleanupPalette(false), m_currentFrame(-1), m_dirtyFlag(false), m_mutex(QMutex::Recursive), m_isLocked(false), m_askOverwriteFlag(false)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString tempName(QObject::tr("colors"));
Toshihiro Shimizu 890ddd
	wstring pageName = tempName.toStdWString();
Toshihiro Shimizu 890ddd
	Page *page = addPage(pageName);
Toshihiro Shimizu 890ddd
	page->addStyle(TPixel32(255, 255, 255, 0));
Toshihiro Shimizu 890ddd
	page->addStyle(TPixel32(0, 0, 0, 255));
Toshihiro Shimizu 890ddd
	getStyle(0)->setName(L"color_0");
Toshihiro Shimizu 890ddd
	getStyle(1)->setName(L"color_1");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < 10; i++)
Toshihiro Shimizu 890ddd
		m_shortcuts['0' + i] = i;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPalette::~TPalette()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	std::set<tcolorstyle *=""> table;</tcolorstyle>
Toshihiro Shimizu 890ddd
	int i = 0;
Toshihiro Shimizu 890ddd
	for (i = 0; i < getStyleCount(); i++) {
Toshihiro Shimizu 890ddd
		assert(table.find(getStyle(i)) == table.end());
Toshihiro Shimizu 890ddd
		table.insert(getStyle(i));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	clearPointerContainer(m_pages);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPalette *TPalette::clone() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPalette *palette = new TPalette;
Toshihiro Shimizu 890ddd
	palette->assign(this);
Toshihiro Shimizu 890ddd
	return palette;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TColorStyle *TPalette::getStyle(int index) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (0 <= index && index < getStyleCount())
Toshihiro Shimizu 890ddd
		return m_styles[index].second.getPointer();
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		static TSolidColorStyle *ss = new TSolidColorStyle(TPixel32::Red);
Toshihiro Shimizu 890ddd
		ss->addRef();
Toshihiro Shimizu 890ddd
		return ss;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TPalette::getStyleInPagesCount() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int styleInPagesCount = 0;
Toshihiro Shimizu 890ddd
	for (int i = 0; i < getStyleCount(); i++)
Toshihiro Shimizu 890ddd
		if (m_styles[i].first != 0)
Toshihiro Shimizu 890ddd
			styleInPagesCount++;
Toshihiro Shimizu 890ddd
	return styleInPagesCount;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TPalette::getFirstUnpagedStyle() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	for (int i = 0; i < getStyleCount(); i++)
Toshihiro Shimizu 890ddd
		if (m_styles[i].first == 0)
Toshihiro Shimizu 890ddd
			return i;
Toshihiro Shimizu 890ddd
	return -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*! Adding style with new styleId. Even if there are deleted styles in the palette, the new style will be appended to the end of the list.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
int TPalette::addStyle(TColorStyle *style)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//limit the number of cleanup style to 7
Toshihiro Shimizu 890ddd
	if (isCleanupPalette() && getStyleInPagesCount() >= 8)
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int styleId = int(m_styles.size());
Toshihiro Shimizu 890ddd
	if (styleId < 4096) {
Toshihiro Shimizu 890ddd
		//checking if the style is overlapped
Toshihiro Shimizu 890ddd
		int i = 0;
Toshihiro Shimizu 890ddd
		for (i = 0; i < styleId; i++)
Toshihiro Shimizu 890ddd
			if (getStyle(i) == style)
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
		if (i == styleId) {
Toshihiro Shimizu 890ddd
			m_styles.push_back(std::make_pair((Page *)0, style));
Toshihiro Shimizu 890ddd
			return styleId;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	delete style;
Toshihiro Shimizu 890ddd
	return -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TPalette::addStyle(const TPixel32 &color)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return addStyle(new TSolidColorStyle(color));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::setStyle(int styleId, TColorStyle *style)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	std::auto_ptr<tcolorstyle> styleOwner(style);</tcolorstyle>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int styleCount = getStyleCount();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (0 <= styleId && styleId < styleCount) {
Toshihiro Shimizu 890ddd
		// Find out if the supplied style is already in the palette
Toshihiro Shimizu 890ddd
		// with a different style id. In that case, bail out as a noop.
Toshihiro Shimizu 890ddd
		for (int i = 0; i < styleCount; ++i)
Toshihiro Shimizu 890ddd
			if (style == getStyle(i))
Toshihiro Shimizu 890ddd
				return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// Substitution can take place
Toshihiro Shimizu 890ddd
		if (typeid(*m_styles[styleId].second.getPointer()) != typeid(*style))
Toshihiro Shimizu 890ddd
			m_styleAnimationTable.erase(styleId);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_styles[styleId].second = styleOwner.release();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::setStyle(int styleId, const TPixelRGBM32 &color)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	setStyle(styleId, new TSolidColorStyle(color));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TPalette::getPageCount() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return int(m_pages.size());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPalette::Page *TPalette::getPage(int pageIndex)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (0 <= pageIndex && pageIndex < getPageCount()) {
Toshihiro Shimizu 890ddd
		Page *page = m_pages[pageIndex];
Toshihiro Shimizu 890ddd
		assert(page->getIndex() == pageIndex);
Toshihiro Shimizu 890ddd
		assert(page->m_palette == this);
Toshihiro Shimizu 890ddd
		return page;
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const TPalette::Page *TPalette::getPage(int pageIndex) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (0 <= pageIndex && pageIndex < getPageCount()) {
Toshihiro Shimizu 890ddd
		Page *page = m_pages[pageIndex];
Toshihiro Shimizu 890ddd
		assert(page->getIndex() == pageIndex);
Toshihiro Shimizu 890ddd
		assert(page->m_palette == this);
Toshihiro Shimizu 890ddd
		return page;
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPalette::Page *TPalette::addPage(wstring name)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Page *page = new Page(name);
Toshihiro Shimizu 890ddd
	page->m_index = getPageCount();
Toshihiro Shimizu 890ddd
	page->m_palette = this;
Toshihiro Shimizu 890ddd
	m_pages.push_back(page);
Toshihiro Shimizu 890ddd
	return page;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::erasePage(int index)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Page *page = getPage(index);
Toshihiro Shimizu 890ddd
	if (!page)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	m_pages.erase(m_pages.begin() + index);
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < getPageCount(); i++)
Toshihiro Shimizu 890ddd
		m_pages[i]->m_index = i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < page->getStyleCount(); i++)
Toshihiro Shimizu 890ddd
		m_styles[page->getStyleId(i)].first = 0;
Toshihiro Shimizu 890ddd
	page->m_palette = 0;
Toshihiro Shimizu 890ddd
	delete page;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::movePage(Page *page, int dstPageIndex)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(page);
Toshihiro Shimizu 890ddd
	assert(page->m_palette == this);
Toshihiro Shimizu 890ddd
	dstPageIndex = tcrop(dstPageIndex, 0, getPageCount() - 1);
Toshihiro Shimizu 890ddd
	if (dstPageIndex == page->getIndex())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	m_pages.erase(m_pages.begin() + page->getIndex());
Toshihiro Shimizu 890ddd
	m_pages.insert(m_pages.begin() + dstPageIndex, page);
Toshihiro Shimizu 890ddd
	for (int i = 0; i < getPageCount(); i++)
Toshihiro Shimizu 890ddd
		m_pages[i]->m_index = i;
Toshihiro Shimizu 890ddd
	assert(page->getIndex() == dstPageIndex);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPalette::Page *TPalette::getStylePage(int styleId) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (0 <= styleId && styleId < getStyleCount())
Toshihiro Shimizu 890ddd
		return m_styles[styleId].first;
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TPalette::getClosestStyle(const TPixel32 &color) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	struct locals {
Toshihiro Shimizu 890ddd
		static inline int getDistance2(const TPixel32 &a, const TPixel32 &b)
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			return (a.r - b.r) * (a.r - b.r) + (a.g - b.g) * (a.g - b.g) + (a.b - b.b) * (a.b - b.b) + (a.m - b.m) * (a.m - b.m);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}; // locals
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (color == TPixel32::Transparent)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	int bestIndex = -1;
Toshihiro Shimizu 890ddd
	int bestDistance = 255 * 255 * 4 + 1;
Toshihiro Shimizu 890ddd
	for (int i = 1; i < (int)m_styles.size(); i++) {
Toshihiro Shimizu 890ddd
		// if(i==FirstUserStyle+2) continue;
Toshihiro Shimizu 890ddd
		TSolidColorStyle *scs = dynamic_cast<tsolidcolorstyle *="">(m_styles[i].second.getPointer());</tsolidcolorstyle>
Toshihiro Shimizu 890ddd
		if (scs) {
Toshihiro Shimizu 890ddd
			int d = locals::getDistance2(scs->getMainColor(), color);
Toshihiro Shimizu 890ddd
			if (d < bestDistance) {
Toshihiro Shimizu 890ddd
				bestIndex = i;
Toshihiro Shimizu 890ddd
				bestDistance = d;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return bestIndex;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TPalette::getFxRects(const TRect &rect, TRect &rectIn, TRect &rectOut)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	bool ret = false;
Toshihiro Shimizu 890ddd
	int borderIn, borderOut, fullBorderIn = 0, fullBorderOut = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < (int)m_styles.size(); i++)
Toshihiro Shimizu 890ddd
		if (m_styles[i].second->isRasterStyle()) {
Toshihiro Shimizu 890ddd
			m_styles[i].second->getRasterStyleFx()->getEnlargement(borderIn, borderOut);
Toshihiro Shimizu 890ddd
			fullBorderIn = tmax(fullBorderIn, borderIn);
Toshihiro Shimizu 890ddd
			fullBorderOut = tmax(fullBorderOut, borderOut);
Toshihiro Shimizu 890ddd
			ret = true;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	rectIn = rect.enlarge(fullBorderIn);
Toshihiro Shimizu 890ddd
	rectOut = rect.enlarge(fullBorderOut);
Toshihiro Shimizu 890ddd
	return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// I/O
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class StyleWriter : public TOutputStreamInterface
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TOStream &m_os;
Toshihiro Shimizu 890ddd
	int m_index;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	static TFilePath m_rootDir;
Toshihiro Shimizu 890ddd
	StyleWriter(TOStream &os, int index) : m_os(os), m_index(index)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	static void setRootDir(const TFilePath &fp) { m_rootDir = fp; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TOutputStreamInterface &operator<<(double x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_os << x;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
	TOutputStreamInterface &operator<<(int x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_os << x;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
	TOutputStreamInterface &operator<<(string x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_os << x;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
	TOutputStreamInterface &operator<<(UCHAR x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_os << (int)x;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
	TOutputStreamInterface &operator<<(USHORT x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_os << (int)x;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
	TOutputStreamInterface &operator<<(const TPixel32 &x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_os << x;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
	TOutputStreamInterface &operator<<(const TRaster32P &ras)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		assert(m_rootDir != TFilePath());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		string name = "texture_" + toString(m_index);
Toshihiro Shimizu 890ddd
		m_os << name;
Toshihiro Shimizu 890ddd
		TFilePath filename =
Toshihiro Shimizu 890ddd
			((m_rootDir + "textures") + name).withType("bmp");
Toshihiro Shimizu 890ddd
		if (!TFileStatus(m_rootDir + "textures").doesExist()) {
Toshihiro Shimizu 890ddd
			try {
Toshihiro Shimizu 890ddd
				TSystem::mkDir(m_rootDir + "textures");
Toshihiro Shimizu 890ddd
			} catch (...) {
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TImageWriter::save(filename, ras);
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class StyleReader : public TInputStreamInterface
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TIStream &m_is;			 //!< Wrapped input stream.
Toshihiro Shimizu 890ddd
	VersionNumber m_version; //!< Palette version number (overrides m_is's one).
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	static TFilePath m_rootDir;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	StyleReader(TIStream &is, const VersionNumber &version)
Toshihiro Shimizu 890ddd
		: m_is(is), m_version(version) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	static void setRootDir(const TFilePath &fp) { m_rootDir = fp; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	virtual TInputStreamInterface &operator>>(double &x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_is >> x;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	virtual TInputStreamInterface &operator>>(int &x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_is >> x;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	virtual TInputStreamInterface &operator>>(string &x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_is >> x;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	virtual TInputStreamInterface &operator>>(UCHAR &x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		int v;
Toshihiro Shimizu 890ddd
		m_is >> v;
Toshihiro Shimizu 890ddd
		x = v;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	virtual TInputStreamInterface &operator>>(USHORT &x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		int v;
Toshihiro Shimizu 890ddd
		m_is >> v;
Toshihiro Shimizu 890ddd
		x = v;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	virtual TInputStreamInterface &operator>>(TRaster32P &x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		assert(m_rootDir != TFilePath());
Toshihiro Shimizu 890ddd
		string name;
Toshihiro Shimizu 890ddd
		m_is >> name;
Toshihiro Shimizu 890ddd
		TFilePath filename =
Toshihiro Shimizu 890ddd
			((m_rootDir + "textures") + name).withType("bmp");
Toshihiro Shimizu 890ddd
		TRasterP ras;
Toshihiro Shimizu 890ddd
		if (TImageReader::load(filename, ras)) {
Toshihiro Shimizu 890ddd
			x = ras;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	virtual TInputStreamInterface &operator>>(TPixel32 &x)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_is >> x;
Toshihiro Shimizu 890ddd
		return *this;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/*!
Toshihiro Shimizu 890ddd
    \details  Explicitly ovverrides the stream's version, returning m_version.
Toshihiro Shimizu 890ddd
              This is necessary since palettes have their \a own version number,
Toshihiro Shimizu 890ddd
              which is \a not the TIStream's file one.
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
	virtual VersionNumber versionNumber() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return m_version;
Toshihiro Shimizu 890ddd
	} //!< Returns the palette's version number.
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFilePath StyleWriter::m_rootDir = TFilePath();
Toshihiro Shimizu 890ddd
TFilePath StyleReader::m_rootDir = TFilePath();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::setRootDir(const TFilePath &fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	StyleWriter::setRootDir(fp);
Toshihiro Shimizu 890ddd
	StyleReader::setRootDir(fp);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::saveData(TOStream &os)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	os.child("version") << 71 << 0;				// Inserting the version tag at this level.
Toshihiro Shimizu 890ddd
												// This is necessary to support the tpl format
Toshihiro Shimizu 890ddd
	if (m_refImgPath != TFilePath())			// since it performs *untagged* stream output
Toshihiro Shimizu 890ddd
		os.child("refImgPath") << m_refImgPath; // (the palette is streamed directly).
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	os.openChild("styles");
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		for (int i = 0; i < getStyleCount(); ++i) {
Toshihiro Shimizu 890ddd
			os.openChild("style");
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				StyleWriter w(os, i);
Toshihiro Shimizu 890ddd
				m_styles[i].second->save(w);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			os.closeChild();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	os.closeChild();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	os.openChild("stylepages");
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		for (int i = 0; i < getPageCount(); ++i) {
Toshihiro Shimizu 890ddd
			Page *page = getPage(i);
Toshihiro Shimizu 890ddd
			os.openChild("page");
Toshihiro Shimizu 890ddd
			{
Toshihiro Shimizu 890ddd
				os.child("name") << page->getName();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				os.openChild("indices");
Toshihiro Shimizu 890ddd
				{
Toshihiro Shimizu 890ddd
					int m = page->getStyleCount();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					for (int j = 0; j < m; ++j)
Toshihiro Shimizu 890ddd
						os << page->getStyleId(j);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				os.closeChild();
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			os.closeChild();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	os.closeChild();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (isAnimated()) {
Toshihiro Shimizu 890ddd
		os.openChild("animation");
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			StyleAnimationTable::iterator sat, saEnd = m_styleAnimationTable.end();
Toshihiro Shimizu 890ddd
			for (sat = m_styleAnimationTable.begin(); sat != saEnd; ++sat) {
Toshihiro Shimizu 890ddd
				int styleId = sat->first;
Toshihiro Shimizu 890ddd
				StyleAnimation &animation = sat->second;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				std::map<string, string=""> attributes;</string,>
Toshihiro Shimizu 890ddd
				attributes["id"] = toString(styleId);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				os.openChild("style", attributes);
Toshihiro Shimizu 890ddd
				{
Toshihiro Shimizu 890ddd
					StyleAnimation::iterator kt, kEnd = animation.end();
Toshihiro Shimizu 890ddd
					for (kt = animation.begin(); kt != kEnd; ++kt) {
Toshihiro Shimizu 890ddd
						int frame = kt->first;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
						TColorStyle *cs = kt->second.getPointer();
Toshihiro Shimizu 890ddd
						assert(cs);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
						attributes.clear();
Toshihiro Shimizu 890ddd
						attributes["frame"] = toString(frame);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
						/*os.openChild("keycolor", attributes);                       // Up to Toonz 7.0, animations saved
Toshihiro Shimizu 890ddd
              os << cs->getMainColor();                                   // the main color only
Toshihiro Shimizu 890ddd
            os.closeChild();*/ //
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
						os.openChild("keyframe", attributes);
Toshihiro Shimizu 890ddd
						{
Toshihiro Shimizu 890ddd
							StyleWriter w(os, sat->first);
Toshihiro Shimizu 890ddd
							kt->second->save(w);
Toshihiro Shimizu 890ddd
						}
Toshihiro Shimizu 890ddd
						os.closeChild();
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				os.closeChild();
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		os.closeChild();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// salvo gli shortcuts solo se sono non standard
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 0; i < 10; ++i)
Toshihiro Shimizu 890ddd
		if (getShortcutValue('0' + i) != i)
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (i < 10) {
Toshihiro Shimizu 890ddd
		os.openChild("shortcuts");
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			for (i = 0; i < 10; i++)
Toshihiro Shimizu 890ddd
				os << getShortcutValue('0' + i);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		os.closeChild();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (isLocked()) {
Toshihiro Shimizu 890ddd
		os.openChild("lock");
Toshihiro Shimizu 890ddd
		os << 1;
Toshihiro Shimizu 890ddd
		os.closeChild();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::loadData(TIStream &is)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_styles.clear();
Toshihiro Shimizu 890ddd
	clearPointerContainer(m_pages);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	VersionNumber version = is.getVersion();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	string tagName;
Toshihiro Shimizu 890ddd
	while (is.openChild(tagName)) {
Toshihiro Shimizu 890ddd
		if (tagName == "version") {
Toshihiro Shimizu 890ddd
			is >> version.first >> version.second;
Toshihiro Shimizu 890ddd
			if (version > VersionNumber(71, 0))
Toshihiro Shimizu 890ddd
				throw TException("palette, unsupported version number");
Toshihiro Shimizu 890ddd
		} else if (tagName == "styles") {
Toshihiro Shimizu 890ddd
			while (!is.eos())										   // I think while(is.openChild(tagName))
Toshihiro Shimizu 890ddd
			{														   // would be better. However, I don't trust
Toshihiro Shimizu 890ddd
				if (!is.openChild(tagName) || tagName != "style")	  // TIStream's implementation very much. Keeping it
Toshihiro Shimizu 890ddd
					throw TException("palette, expected tag <style>"); // like this for now.</style>
Toshihiro Shimizu 890ddd
				{
Toshihiro Shimizu 890ddd
					StyleReader r(is, version);
Toshihiro Shimizu 890ddd
					TColorStyle *cs = TColorStyle::load(r);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					addStyle(cs);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				is.closeChild();
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else if (tagName == "stylepages") {
Toshihiro Shimizu 890ddd
			while (!is.eos()) {
Toshihiro Shimizu 890ddd
				if (!is.openChild(tagName) || tagName != "page")
Toshihiro Shimizu 890ddd
					throw TException("palette, expected tag <stylepage>");</stylepage>
Toshihiro Shimizu 890ddd
				{
Toshihiro Shimizu 890ddd
					wstring pageName;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					if (!is.openChild(tagName) || tagName != "name")
Toshihiro Shimizu 890ddd
						throw TException("palette, expected tag <name>");</name>
Toshihiro Shimizu 890ddd
					{
Toshihiro Shimizu 890ddd
						is >> pageName;
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					is.closeChild();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					Page *page = addPage(pageName);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					if (!is.openChild(tagName) || tagName != "indices")
Toshihiro Shimizu 890ddd
						throw TException("palette, expected tag <indices>");</indices>
Toshihiro Shimizu 890ddd
					{
Toshihiro Shimizu 890ddd
						while (!is.eos()) {
Toshihiro Shimizu 890ddd
							int index;
Toshihiro Shimizu 890ddd
							is >> index;
Toshihiro Shimizu 890ddd
							page->addStyle(index);
Toshihiro Shimizu 890ddd
						}
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
					is.closeChild();
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				is.closeChild();
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else if (tagName == "refImgPath")
Toshihiro Shimizu 890ddd
			is >> m_refImgPath;
Toshihiro Shimizu 890ddd
		else if (tagName == "animation") {
Toshihiro Shimizu 890ddd
			while (!is.eos()) {
Toshihiro Shimizu 890ddd
				if (!is.openChild(tagName) || tagName != "style")
Toshihiro Shimizu 890ddd
					throw TException("palette, expected tag <style>");</style>
Toshihiro Shimizu 890ddd
				{
Toshihiro Shimizu 890ddd
					int styleId = 0;
Toshihiro Shimizu 890ddd
					if (!is.getTagParam("id", styleId))
Toshihiro Shimizu 890ddd
						throw TException("palette, missing id attribute in tag <style>");</style>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					StyleAnimation animation;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					TColorStyle *style = getStyle(styleId);
Toshihiro Shimizu 890ddd
					assert(style);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					while (is.matchTag(tagName)) {
Toshihiro Shimizu 890ddd
						TColorStyle *cs = 0;
Toshihiro Shimizu 890ddd
						int frame = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
						if (tagName == "keycolor") {
Toshihiro Shimizu 890ddd
							if (!is.getTagParam("frame", frame))
Toshihiro Shimizu 890ddd
								throw TException("palette, missing frame attribute in tag <keycolor>");</keycolor>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
							TPixel32 color;
Toshihiro Shimizu 890ddd
							is >> color;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
							cs = style->clone();
Toshihiro Shimizu 890ddd
							cs->setMainColor(color);
Toshihiro Shimizu 890ddd
						} else if (tagName == "keyframe") {
Toshihiro Shimizu 890ddd
							if (!is.getTagParam("frame", frame))
Toshihiro Shimizu 890ddd
								throw TException("palette, missing frame attribute in tag <keyframe>");</keyframe>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
							StyleReader r(is, version);
Toshihiro Shimizu 890ddd
							cs = TColorStyle::load(r);
Toshihiro Shimizu 890ddd
						} else
Toshihiro Shimizu 890ddd
							throw TException("palette, expected <keyframe> tag");</keyframe>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
						animation[frame] = cs;
Toshihiro Shimizu 890ddd
						is.closeChild();
Toshihiro Shimizu 890ddd
					}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					m_styleAnimationTable[styleId] = animation;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				is.closeChild();
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else if (tagName == "stylepages") {
Toshihiro Shimizu 890ddd
			int key = '0';
Toshihiro Shimizu 890ddd
			while (!is.eos()) {
Toshihiro Shimizu 890ddd
				int styleId = 0;
Toshihiro Shimizu 890ddd
				is >> styleId;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (key <= '9')
Toshihiro Shimizu 890ddd
					setShortcutValue(key, styleId);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else if (tagName == "shortcuts") {
Toshihiro Shimizu 890ddd
			for (int i = 0; i < 10; ++i) {
Toshihiro Shimizu 890ddd
				int v;
Toshihiro Shimizu 890ddd
				is >> v;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				setShortcutValue('0' + i, v);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else if (tagName == "lock") {
Toshihiro Shimizu 890ddd
			int lockValue;
Toshihiro Shimizu 890ddd
			is >> lockValue;
Toshihiro Shimizu 890ddd
			m_isLocked = (bool)lockValue;
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			throw TException("palette, unknown tag: " + tagName);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		is.closeChild();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*! if the palette is copied from studio palette, this function will modify the original names. 
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void TPalette::assign(const TPalette *src, bool isFromStudioPalette)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (src == this)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	m_isCleanupPalette = src->isCleanupPalette();
Toshihiro Shimizu 890ddd
	//for(i=0;i
Toshihiro Shimizu 890ddd
	m_styles.clear();
Toshihiro Shimizu 890ddd
	clearPointerContainer(m_pages);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < src->getStyleCount(); i++) {
Toshihiro Shimizu 890ddd
		TColorStyle *srcStyle = src->getStyle(i);
Toshihiro Shimizu 890ddd
		TColorStyle *dstStyle = srcStyle->clone();
Toshihiro Shimizu 890ddd
		dstStyle->setName(srcStyle->getName());				// per un baco del TColorStyle::clone()
Toshihiro Shimizu 890ddd
		dstStyle->setGlobalName(srcStyle->getGlobalName()); // per un baco del TColorStyle::clone()
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//if the style is copied from studio palette, put its name to the original name.
Toshihiro Shimizu 890ddd
		//check if the style has the global name (i.e. it comes from studio palette)
Toshihiro Shimizu 890ddd
		if (isFromStudioPalette && srcStyle->getGlobalName() != L"") {
Toshihiro Shimizu 890ddd
			//If the original style has no original name (i.e. if the style is copied from the studio palette)
Toshihiro Shimizu 890ddd
			if (srcStyle->getOriginalName() == L"") {
Toshihiro Shimizu 890ddd
				//put the original style name to the "original name" of the pasted style.
Toshihiro Shimizu 890ddd
				dstStyle->setOriginalName(srcStyle->getName());
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int j = addStyle(dstStyle);
Toshihiro Shimizu 890ddd
		assert(i == j);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < src->getPageCount(); i++) {
Toshihiro Shimizu 890ddd
		const Page *srcPage = src->getPage(i);
Toshihiro Shimizu 890ddd
		Page *dstPage = addPage(srcPage->getName());
Toshihiro Shimizu 890ddd
		for (int j = 0; j < srcPage->getStyleCount(); j++)
Toshihiro Shimizu 890ddd
			dstPage->addStyle(srcPage->getStyleId(j));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	m_refImg = !!src->m_refImg ? src->m_refImg->cloneImage() : TImageP();
Toshihiro Shimizu 890ddd
	m_refImgPath = src->m_refImgPath;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	StyleAnimationTable::iterator it;
Toshihiro Shimizu 890ddd
	StyleAnimation::iterator j;
Toshihiro Shimizu 890ddd
	for (it = m_styleAnimationTable.begin();
Toshihiro Shimizu 890ddd
		 it != m_styleAnimationTable.end(); ++it) {
Toshihiro Shimizu 890ddd
		//for(j = it->second.begin(); j != it->second.end(); ++j)
Toshihiro Shimizu 890ddd
		//   delete j->second;
Toshihiro Shimizu 890ddd
		it->second.clear();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	m_styleAnimationTable.clear();
Toshihiro Shimizu 890ddd
	StyleAnimationTable::const_iterator cit;
Toshihiro Shimizu 890ddd
	for (cit = src->m_styleAnimationTable.begin();
Toshihiro Shimizu 890ddd
		 cit != src->m_styleAnimationTable.end(); ++cit) {
Toshihiro Shimizu 890ddd
		StyleAnimation animation = cit->second;
Toshihiro Shimizu 890ddd
		for (j = animation.begin(); j != animation.end(); j++)
Toshihiro Shimizu 890ddd
			j->second = j->second->clone();
Toshihiro Shimizu 890ddd
		m_styleAnimationTable[cit->first] = cit->second;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	m_globalName = src->getGlobalName();
Toshihiro Shimizu 890ddd
	m_shortcuts = src->m_shortcuts;
Toshihiro Shimizu 890ddd
	m_currentFrame = src->m_currentFrame;
Toshihiro Shimizu 890ddd
	//setDirtyFlag(true);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*!if the palette is merged from studio palette, this function will modify the original names. 
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void TPalette::merge(const TPalette *src, bool isFromStudioPalette)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	std::map<int, int=""> table;</int,>
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	for (i = 1; i < src->getStyleCount(); i++) {
Toshihiro Shimizu 890ddd
		TColorStyle *srcStyle = src->getStyle(i);
Toshihiro Shimizu 890ddd
		TColorStyle *dstStyle = srcStyle->clone();
Toshihiro Shimizu 890ddd
		dstStyle->setName(srcStyle->getName());
Toshihiro Shimizu 890ddd
		dstStyle->setGlobalName(srcStyle->getGlobalName());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//if the style is copied from studio palette, put its name to the original name.
Toshihiro Shimizu 890ddd
		//check if the style has the global name (i.e. it comes from studio palette)
Toshihiro Shimizu 890ddd
		if (isFromStudioPalette && srcStyle->getGlobalName() != L"") {
Toshihiro Shimizu 890ddd
			//If the original style has no original name (i.e. if the style is copied from the studio palette)
Toshihiro Shimizu 890ddd
			if (srcStyle->getOriginalName() == L"") {
Toshihiro Shimizu 890ddd
				//put the original style name to the "original name" of the pasted style.
Toshihiro Shimizu 890ddd
				dstStyle->setOriginalName(srcStyle->getName());
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		int j = addStyle(dstStyle);
Toshihiro Shimizu 890ddd
		table[i] = j;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int pageCount = src->getPageCount();
Toshihiro Shimizu 890ddd
	for (i = 0; i < pageCount; i++) {
Toshihiro Shimizu 890ddd
		const Page *srcPage = src->getPage(i);
Toshihiro Shimizu 890ddd
		wstring pageName = srcPage->getName();
Toshihiro Shimizu 890ddd
		if (pageName == L"colors" && src->getPaletteName() != L"")
Toshihiro Shimizu 890ddd
			pageName = src->getPaletteName();
Toshihiro Shimizu 890ddd
		Page *dstPage = addPage(pageName); //;
Toshihiro Shimizu 890ddd
		for (int j = 0; j < srcPage->getStyleCount(); j++) {
Toshihiro Shimizu 890ddd
			int styleId = srcPage->getStyleId(j);
Toshihiro Shimizu 890ddd
			if (styleId == 0)
Toshihiro Shimizu 890ddd
				continue;
Toshihiro Shimizu 890ddd
			assert(table.find(styleId) != table.end());
Toshihiro Shimizu 890ddd
			dstPage->addStyle(table[styleId]);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		assert(dstPage->m_palette == this);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::setIsCleanupPalette(bool on)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_isCleanupPalette = on;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::setRefImg(const TImageP &img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_refImg = img;
Toshihiro Shimizu 890ddd
	if (img) {
Toshihiro Shimizu 890ddd
		assert(img->getPalette() == 0);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::setRefLevelFids(const std::vector<tframeid> fids)</tframeid>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_refLevelFids = fids;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
std::vector<tframeid> TPalette::getRefLevelFids()</tframeid>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_refLevelFids;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::setRefImgPath(const TFilePath &refImgPath)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_refImgPath = refImgPath;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TPalette::isAnimated() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return !m_styleAnimationTable.empty();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TPalette::getFrame() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_currentFrame;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::setFrame(int frame)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QMutexLocker muLock(&m_mutex);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_currentFrame == frame)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_currentFrame = frame;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	StyleAnimationTable::iterator sat, saEnd = m_styleAnimationTable.end();
Toshihiro Shimizu 890ddd
	for (sat = m_styleAnimationTable.begin();
Toshihiro Shimizu 890ddd
		 sat != saEnd; ++sat) {
Toshihiro Shimizu 890ddd
		StyleAnimation &animation = sat->second;
Toshihiro Shimizu 890ddd
		assert(!animation.empty());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// Retrieve the associated style to interpolate
Toshihiro Shimizu 890ddd
		int styleId = sat->first;
Toshihiro Shimizu 890ddd
		assert(0 <= styleId && styleId < getStyleCount());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TColorStyle *cs = getStyle(styleId);
Toshihiro Shimizu 890ddd
		assert(cs);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// Buid the keyframes interval containing frame
Toshihiro Shimizu 890ddd
		StyleAnimation::iterator j0, j1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		j1 = animation.lower_bound(frame); // j1 is the first element:  j1->first >= frame
Toshihiro Shimizu 890ddd
		if (j1 == animation.begin())
Toshihiro Shimizu 890ddd
			cs->copy(*j1->second);
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			j0 = j1, --j0;
Toshihiro Shimizu 890ddd
			assert(j0->first <= frame);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (j1 == animation.end())
Toshihiro Shimizu 890ddd
				cs->copy(*j0->second);
Toshihiro Shimizu 890ddd
			else {
Toshihiro Shimizu 890ddd
				assert(frame <= j1->first);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				cs->assignBlend(*j0->second, *j1->second,
Toshihiro Shimizu 890ddd
								(frame - j0->first) / double(j1->first - j0->first));
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TPalette::isKeyframe(int styleId, int frame) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	StyleAnimationTable::const_iterator it =
Toshihiro Shimizu 890ddd
		m_styleAnimationTable.find(styleId);
Toshihiro Shimizu 890ddd
	if (it == m_styleAnimationTable.end())
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	return it->second.count(frame) > 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TPalette::getKeyframeCount(int styleId) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	StyleAnimationTable::const_iterator it =
Toshihiro Shimizu 890ddd
		m_styleAnimationTable.find(styleId);
Toshihiro Shimizu 890ddd
	if (it == m_styleAnimationTable.end())
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	return int(it->second.size());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TPalette::getKeyframe(int styleId, int index) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	StyleAnimationTable::const_iterator it =
Toshihiro Shimizu 890ddd
		m_styleAnimationTable.find(styleId);
Toshihiro Shimizu 890ddd
	if (it == m_styleAnimationTable.end())
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	const StyleAnimation &animation = it->second;
Toshihiro Shimizu 890ddd
	if (index < 0 || index >= (int)animation.size())
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	StyleAnimation::const_iterator j = animation.begin();
Toshihiro Shimizu 890ddd
	std::advance(j, index);
Toshihiro Shimizu 890ddd
	return j->first;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::setKeyframe(int styleId, int frame)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(styleId >= 0 && styleId < getStyleCount());
Toshihiro Shimizu 890ddd
	assert(frame >= 0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	StyleAnimationTable::iterator sat =
Toshihiro Shimizu 890ddd
		m_styleAnimationTable.find(styleId);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (sat == m_styleAnimationTable.end())
Toshihiro Shimizu 890ddd
		sat = m_styleAnimationTable.insert(std::make_pair(styleId, StyleAnimation())).first;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	assert(sat != m_styleAnimationTable.end());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	StyleAnimation &animation = sat->second;
Toshihiro Shimizu 890ddd
	animation[frame] = getStyle(styleId)->clone();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::clearKeyframe(int styleId, int frame)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(0 <= styleId && styleId < getStyleCount());
Toshihiro Shimizu 890ddd
	assert(0 <= frame);
Toshihiro Shimizu 890ddd
	StyleAnimationTable::iterator it =
Toshihiro Shimizu 890ddd
		m_styleAnimationTable.find(styleId);
Toshihiro Shimizu 890ddd
	if (it == m_styleAnimationTable.end())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	StyleAnimation &animation = it->second;
Toshihiro Shimizu 890ddd
	StyleAnimation::iterator j = animation.find(frame);
Toshihiro Shimizu 890ddd
	if (j == animation.end())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	//j->second->release();
Toshihiro Shimizu 890ddd
	animation.erase(j);
Toshihiro Shimizu 890ddd
	if (animation.empty()) {
Toshihiro Shimizu 890ddd
		m_styleAnimationTable.erase(styleId);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TPalette::getShortcutValue(int key) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert('0' <= key && key <= '9');
Toshihiro Shimizu 890ddd
	std::map<int, int="">::const_iterator it;</int,>
Toshihiro Shimizu 890ddd
	it = m_shortcuts.find(key);
Toshihiro Shimizu 890ddd
	if (it == m_shortcuts.end())
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	int styleId = it->second;
Toshihiro Shimizu 890ddd
	return 0 <= styleId && styleId < getStyleCount()
Toshihiro Shimizu 890ddd
			   ? styleId
Toshihiro Shimizu 890ddd
			   : -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TPalette::getStyleShortcut(int styleId) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(0 <= styleId && styleId < getStyleCount());
Toshihiro Shimizu 890ddd
	std::map<int, int="">::const_iterator it;</int,>
Toshihiro Shimizu 890ddd
	for (it = m_shortcuts.begin();
Toshihiro Shimizu 890ddd
		 it != m_shortcuts.end(); ++it)
Toshihiro Shimizu 890ddd
		if (it->second == styleId)
Toshihiro Shimizu 890ddd
			return it->first;
Toshihiro Shimizu 890ddd
	return -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TPalette::setShortcutValue(int key, int styleId)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert('0' <= key && key <= '9');
Toshihiro Shimizu 890ddd
	assert(styleId == -1 ||
Toshihiro Shimizu 890ddd
		   0 <= styleId && styleId < getStyleCount());
Toshihiro Shimizu 890ddd
	if (styleId == -1)
Toshihiro Shimizu 890ddd
		m_shortcuts.erase(key);
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		std::map<int, int="">::iterator it;</int,>
Toshihiro Shimizu 890ddd
		for (it = m_shortcuts.begin();
Toshihiro Shimizu 890ddd
			 it != m_shortcuts.end(); ++it)
Toshihiro Shimizu 890ddd
			if (it->second == styleId) {
Toshihiro Shimizu 890ddd
				m_shortcuts.erase(it);
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		m_shortcuts[key] = styleId;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}