Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_level.h"
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_files.h"
Toshihiro Shimizu 890ddd
#include <qscriptengine></qscriptengine>
Toshihiro Shimizu 890ddd
#include "timage_io.h"
Toshihiro Shimizu 890ddd
#include "tlevel_io.h"
Toshihiro Shimizu 890ddd
#include "tlevel.h"
Toshihiro Shimizu 890ddd
#include "toonz/tcenterlinevectorizer.h"
Toshihiro Shimizu 890ddd
#include "toonz/tcamera.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
Toshihiro Shimizu 890ddd
#include "ttoonzimage.h"
Toshihiro Shimizu 890ddd
#include "tvectorimage.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
#include "tofflinegl.h"
Toshihiro Shimizu 890ddd
#include "tvectorrenderdata.h"
Toshihiro Shimizu 890ddd
#include "tgeometry.h"
Toshihiro Shimizu 890ddd
#include "toonz/stage.h"
Toshihiro Shimizu 890ddd
#include "toonz/tcamera.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshleveltypes.h"
Toshihiro Shimizu 890ddd
#include "toonz/levelproperties.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzscene.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshsimplelevel.h"
Toshihiro Shimizu 890ddd
#include "toonz/levelset.h"
Toshihiro Shimizu 890ddd
#include "tfiletype.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include <qregexp></qregexp>
Toshihiro Shimizu 890ddd
#include <qcolor></qcolor>
Toshihiro Shimizu 890ddd
#include "tfiletype.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace TScriptBinding
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Level::Level()
Toshihiro Shimizu 890ddd
	: m_sl(0), m_type(NO_XSHLEVEL), m_scene(new ToonzScene()), m_sceneOwner(true)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Level::Level(TXshSimpleLevel *sl)
Toshihiro Shimizu 890ddd
	: m_sl(sl), m_type(sl->getType()), m_scene(sl->getScene()), m_sceneOwner(false)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	sl->addRef();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Level::~Level()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_sceneOwner)
Toshihiro Shimizu 890ddd
		delete m_scene;
Toshihiro Shimizu 890ddd
	if (m_sl)
Toshihiro Shimizu 890ddd
		m_sl->release();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue Level::ctor(QScriptContext *context, QScriptEngine *engine)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Level *level = new Level();
Toshihiro Shimizu 890ddd
	QScriptValue obj = engine->newQObject(level, QScriptEngine::AutoOwnership);
Toshihiro Shimizu 890ddd
	if (context->argumentCount() == 1) {
Toshihiro Shimizu 890ddd
		return obj.property("load").call(obj, context->argumentsObject());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return obj;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue Level::toString()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString info = "(";
Toshihiro Shimizu 890ddd
	QString comma = "";
Toshihiro Shimizu 890ddd
	if (getName() != "") {
Toshihiro Shimizu 890ddd
		info.append(comma).append(getName());
Toshihiro Shimizu 890ddd
		comma = ", ";
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	info.append(comma).append(tr("%1 frames").arg(getFrameCount()));
Toshihiro Shimizu 890ddd
	info.append(")");
Toshihiro Shimizu 890ddd
	if (m_type == PLI_XSHLEVEL)
Toshihiro Shimizu 890ddd
		return QString("Vector level %1").arg(info);
Toshihiro Shimizu 890ddd
	else if (m_type == TZP_XSHLEVEL)
Toshihiro Shimizu 890ddd
		return QString("Toonz level %1").arg(info);
Toshihiro Shimizu 890ddd
	else if (m_type == NO_XSHLEVEL)
Toshihiro Shimizu 890ddd
		return QString("Empty level");
Toshihiro Shimizu 890ddd
	else if (m_type == OVL_XSHLEVEL)
Toshihiro Shimizu 890ddd
		return QString("Raster level %1").arg(info);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return QString("Level %1").arg(info);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString Level::getType() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_type == NO_XSHLEVEL)
Toshihiro Shimizu 890ddd
		return "Empty";
Toshihiro Shimizu 890ddd
	else if (m_type == PLI_XSHLEVEL)
Toshihiro Shimizu 890ddd
		return "Vector";
Toshihiro Shimizu 890ddd
	else if (m_type == TZP_XSHLEVEL)
Toshihiro Shimizu 890ddd
		return "ToonzRaster";
Toshihiro Shimizu 890ddd
	else if (m_type == OVL_XSHLEVEL)
Toshihiro Shimizu 890ddd
		return "Raster";
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return "Unknown";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int Level::getFrameCount() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_sl ? m_sl->getFrameCount() : 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString Level::getName() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_sl ? QString::fromStdWString(m_sl->getName()) : "";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Level::setName(const QString &name)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_sl)
Toshihiro Shimizu 890ddd
		m_sl->setName(name.toStdWString());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue Level::getPath() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_sl) {
Toshihiro Shimizu 890ddd
		FilePath *result = new FilePath(m_sl->getPath());
Toshihiro Shimizu 890ddd
		return result->create<filepath>(engine());</filepath>
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		return QScriptValue();
Toshihiro Shimizu 890ddd
	return m_sl ? QString::fromStdWString(m_sl->getName()) : "";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Level::setPath(const QScriptValue &pathArg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TFilePath fp;
Toshihiro Shimizu 890ddd
	FilePath *filePath = qscriptvalue_cast<filepath *="">(pathArg);</filepath>
Toshihiro Shimizu 890ddd
	if (filePath)
Toshihiro Shimizu 890ddd
		fp = filePath->getToonzFilePath();
Toshihiro Shimizu 890ddd
	else if (pathArg.isString())
Toshihiro Shimizu 890ddd
		fp = TFilePath(pathArg.toString().toStdString());
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		context()->throwError(tr("Bad argument (%1). It should be FilePath or string").arg(pathArg.toString()));
Toshihiro Shimizu 890ddd
	if (m_sl) {
Toshihiro Shimizu 890ddd
		m_sl->setPath(fp);
Toshihiro Shimizu 890ddd
		try {
Toshihiro Shimizu 890ddd
			m_sl->load();
Toshihiro Shimizu 890ddd
		} catch (...) {
Toshihiro Shimizu 890ddd
			context()->throwError(tr("Exception loading level (%1)").arg(QString::fromStdWString(fp.getWideString())));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue Level::load(const QScriptValue &fpArg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_sl) {
Toshihiro Shimizu 890ddd
		m_scene->getLevelSet()->removeLevel(m_sl, true);
Toshihiro Shimizu 890ddd
		m_sl->release();
Toshihiro Shimizu 890ddd
		m_sl = 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// get the path
Toshihiro Shimizu 890ddd
	TFilePath fp;
Toshihiro Shimizu 890ddd
	QScriptValue err = checkFilePath(context(), fpArg, fp);
Toshihiro Shimizu 890ddd
	if (err.isError())
Toshihiro Shimizu 890ddd
		return err;
Toshihiro Shimizu 890ddd
	QString fpStr = fpArg.toString();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		if (!TSystem::doesExistFileOrLevel(fp)) {
Toshihiro Shimizu 890ddd
			return context()->throwError(tr("File %1 doesn't exist").arg(fpStr));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		TFileType::Type fileType = TFileType::getInfo(fp);
Toshihiro Shimizu 890ddd
		if (TFileType::isVector(fileType))
Toshihiro Shimizu 890ddd
			m_type = PLI_XSHLEVEL;
Toshihiro Shimizu 890ddd
		else if (0 != (fileType & TFileType::CMAPPED_IMAGE))
Toshihiro Shimizu 890ddd
			m_type = TZP_XSHLEVEL;
Toshihiro Shimizu 890ddd
		else if (0 != (fileType & TFileType::RASTER_IMAGE))
Toshihiro Shimizu 890ddd
			m_type = OVL_XSHLEVEL;
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			return context()->throwError(tr("File %1 is unsupported").arg(fpStr));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		TXshLevel *xl = m_scene->loadLevel(fp);
Toshihiro Shimizu 890ddd
		if (xl) {
Toshihiro Shimizu 890ddd
			m_sl = xl->getSimpleLevel();
Toshihiro Shimizu 890ddd
			m_sl->addRef();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		return context()->thisObject();
Toshihiro Shimizu 890ddd
	} catch (...) {
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("Exception reading %1").arg(fpStr));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue Level::save(const QScriptValue &fpArg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (getFrameCount() == 0) {
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("Can't save an empty level"));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// get the path
Toshihiro Shimizu 890ddd
	TFilePath fp;
Toshihiro Shimizu 890ddd
	QScriptValue err = checkFilePath(context(), fpArg, fp);
Toshihiro Shimizu 890ddd
	if (err.isError())
Toshihiro Shimizu 890ddd
		return err;
Toshihiro Shimizu 890ddd
	QString fpStr = fpArg.toString();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// handle conversion (if it is needed and possible)
Toshihiro Shimizu 890ddd
	TFileType::Type fileType = TFileType::getInfo(fp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool isCompatible = false;
Toshihiro Shimizu 890ddd
	if (TFileType::isFullColor(fileType)) {
Toshihiro Shimizu 890ddd
		if (m_sl->getType() == OVL_XSHLEVEL)
Toshihiro Shimizu 890ddd
			isCompatible = true;
Toshihiro Shimizu 890ddd
	} else if (TFileType::isVector(fileType)) {
Toshihiro Shimizu 890ddd
		if (m_sl->getType() == PLI_XSHLEVEL)
Toshihiro Shimizu 890ddd
			isCompatible = true;
Toshihiro Shimizu 890ddd
	} else if (fileType & TFileType::CMAPPED_IMAGE) {
Toshihiro Shimizu 890ddd
		if (m_sl->getType() == TZP_XSHLEVEL)
Toshihiro Shimizu 890ddd
			isCompatible = true;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("Unrecognized file type :").arg(fpStr));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (!isCompatible) {
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("Can't save a %1 level to this file type : %2").arg(getType()).arg(fpStr));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
		m_sl->save(fp);
Toshihiro Shimizu 890ddd
	} catch (TSystemException se) {
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("Exception writing %1").arg(QString::fromStdWString(se.getMessage())));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return context()->thisObject();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFrameId Level::getFid(const QScriptValue &arg, QString &err)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (arg.isNumber() || arg.isString()) {
Toshihiro Shimizu 890ddd
		QString s = arg.toString();
Toshihiro Shimizu 890ddd
		QRegExp re("(\\d+)(\\w?)");
Toshihiro Shimizu 890ddd
		if (re.exactMatch(s)) {
Toshihiro Shimizu 890ddd
			int d = re.cap(1).toInt();
Toshihiro Shimizu 890ddd
			QString c = re.cap(2);
Toshihiro Shimizu 890ddd
			TFrameId fid;
Toshihiro Shimizu 890ddd
			if (c.length() == 1)
Toshihiro Shimizu 890ddd
#if QT_VERSION >= 0x050500
Toshihiro Shimizu 890ddd
				fid = TFrameId(d, c[0].unicode());
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
				fid = TFrameId(d, c[0].toAscii());
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				fid = TFrameId(d);
Toshihiro Shimizu 890ddd
			err = "";
Toshihiro Shimizu 890ddd
			return fid;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	err = QObject::tr("Argument '%1' does not look like a FrameId").arg(arg.toString());
Toshihiro Shimizu 890ddd
	return TFrameId();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageP Level::getImg(const TFrameId &fid)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_sl)
Toshihiro Shimizu 890ddd
		return m_sl->getFrame(fid, false);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return TImageP();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue Level::getFrame(const QScriptValue &fidArg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (getFrameCount() == 0)
Toshihiro Shimizu 890ddd
		return context()->throwError("An empty level has no frames");
Toshihiro Shimizu 890ddd
	QString err;
Toshihiro Shimizu 890ddd
	TFrameId fid = getFid(fidArg, err);
Toshihiro Shimizu 890ddd
	if (err != "")
Toshihiro Shimizu 890ddd
		return context()->throwError(err);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TImageP content = m_sl->getFrame(fid, false);
Toshihiro Shimizu 890ddd
	if (content) {
Toshihiro Shimizu 890ddd
		Image *img = new Image(content.getPointer());
Toshihiro Shimizu 890ddd
		return create(img);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		return QScriptValue();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue Level::getFrameByIndex(const QScriptValue &indexArg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (getFrameCount() == 0)
Toshihiro Shimizu 890ddd
		return context()->throwError("An empty level has no frames");
Toshihiro Shimizu 890ddd
	if (!indexArg.isNumber()) {
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("frame index (%1) must be a number").arg(indexArg.toString()));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	int index = indexArg.toInteger();
Toshihiro Shimizu 890ddd
	if (index < 0 || index >= getFrameCount()) {
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("frame index (%1) is out of range (0-%2)").arg(index).arg(getFrameCount() - 1));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TFrameId fid = m_sl->index2fid(index);
Toshihiro Shimizu 890ddd
	TImageP content = m_sl->getFrame(fid, false);
Toshihiro Shimizu 890ddd
	if (content) {
Toshihiro Shimizu 890ddd
		Image *img = new Image(content.getPointer());
Toshihiro Shimizu 890ddd
		return create(img);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		return QScriptValue();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TODO: chiamare setFrame(const TFrameId &fid, const TImageP &img)
Toshihiro Shimizu 890ddd
QScriptValue Level::setFrame(const QScriptValue &fidArg, const QScriptValue &imageArg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString err;
Toshihiro Shimizu 890ddd
	TFrameId fid = getFid(fidArg, err);
Toshihiro Shimizu 890ddd
	if (err != "")
Toshihiro Shimizu 890ddd
		return context()->throwError(err);
Toshihiro Shimizu 890ddd
	Image *img = qscriptvalue_cast(imageArg);
Toshihiro Shimizu 890ddd
	if (!img) {
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("second argument (%1) is not an image").arg(imageArg.toString()));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString imgType = img->getType();
Toshihiro Shimizu 890ddd
	int levelType = NO_XSHLEVEL;
Toshihiro Shimizu 890ddd
	if (imgType == "ToonzRaster")
Toshihiro Shimizu 890ddd
		levelType = TZP_XSHLEVEL;
Toshihiro Shimizu 890ddd
	else if (imgType == "Raster")
Toshihiro Shimizu 890ddd
		levelType = OVL_XSHLEVEL;
Toshihiro Shimizu 890ddd
	else if (imgType == "Vector")
Toshihiro Shimizu 890ddd
		levelType = PLI_XSHLEVEL;
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("can not insert a %1 image into a level").arg(imgType));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_type == NO_XSHLEVEL) {
Toshihiro Shimizu 890ddd
		m_type = levelType;
Toshihiro Shimizu 890ddd
		TXshLevel *xl = m_scene->createNewLevel(levelType);
Toshihiro Shimizu 890ddd
		m_sl = xl->getSimpleLevel();
Toshihiro Shimizu 890ddd
		m_sl->addRef();
Toshihiro Shimizu 890ddd
		m_sl->setPalette(img->getImg()->getPalette());
Toshihiro Shimizu 890ddd
		if (levelType != PLI_XSHLEVEL) {
Toshihiro Shimizu 890ddd
			LevelProperties *lprop = m_sl->getProperties();
Toshihiro Shimizu 890ddd
			lprop->setDpiPolicy(LevelProperties::DP_ImageDpi);
Toshihiro Shimizu 890ddd
			int xres = img->getWidth();
Toshihiro Shimizu 890ddd
			int yres = img->getHeight();
Toshihiro Shimizu 890ddd
			double dpi = img->getDpi();
Toshihiro Shimizu 890ddd
			lprop->setDpi(dpi);
Toshihiro Shimizu 890ddd
			lprop->setImageDpi(TPointD(dpi, dpi));
Toshihiro Shimizu 890ddd
			lprop->setImageRes(TDimension(xres, yres));
Toshihiro Shimizu 890ddd
			// lprop->setHasAlpha(true);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else if (m_type != levelType) {
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("can not insert a %1 image to a %2 level").arg(imgType).arg(getType()));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (m_sl->getFrameCount() == 0)
Toshihiro Shimizu 890ddd
		m_sl->setPalette(img->getImg()->getPalette());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_sl->setFrame(fid, img->getImg());
Toshihiro Shimizu 890ddd
	m_sl->setDirtyFlag(true);
Toshihiro Shimizu 890ddd
	return context()->thisObject();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue Level::getFrameIds()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QList<tframeid> fids;</tframeid>
Toshihiro Shimizu 890ddd
	getFrameIds(fids);
Toshihiro Shimizu 890ddd
	QScriptValue result = engine()->newArray();
Toshihiro Shimizu 890ddd
	quint32 index = 0;
Toshihiro Shimizu 890ddd
	foreach (TFrameId fid, fids) {
Toshihiro Shimizu 890ddd
		QString fidStr = QString::fromStdString(fid.expand());
Toshihiro Shimizu 890ddd
		result.setProperty(index++, fidStr);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return result;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Level::getFrameIds(QList<tframeid> &result)</tframeid>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (getFrameCount() > 0) {
Toshihiro Shimizu 890ddd
		std::vector<tframeid> fids;</tframeid>
Toshihiro Shimizu 890ddd
		m_sl->getFids(fids);
Toshihiro Shimizu 890ddd
		for (std::vector<tframeid>::iterator it = fids.begin(); it != fids.end(); ++it) {</tframeid>
Toshihiro Shimizu 890ddd
			result.append(*it);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int Level::setFrame(const TFrameId &fid, const TImageP &img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TImage::Type imgType = img->getType();
Toshihiro Shimizu 890ddd
	int levelType = NO_XSHLEVEL;
Toshihiro Shimizu 890ddd
	if (imgType == TImage::TOONZ_RASTER)
Toshihiro Shimizu 890ddd
		levelType = TZP_XSHLEVEL;
Toshihiro Shimizu 890ddd
	else if (imgType == TImage::RASTER)
Toshihiro Shimizu 890ddd
		levelType = OVL_XSHLEVEL;
Toshihiro Shimizu 890ddd
	else if (imgType == TImage::VECTOR)
Toshihiro Shimizu 890ddd
		levelType = PLI_XSHLEVEL;
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		return -1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_type == NO_XSHLEVEL) {
Toshihiro Shimizu 890ddd
		m_type = levelType;
Toshihiro Shimizu 890ddd
		TXshLevel *xl = m_scene->createNewLevel(levelType);
Toshihiro Shimizu 890ddd
		m_sl = xl->getSimpleLevel();
Toshihiro Shimizu 890ddd
		m_sl->addRef();
Toshihiro Shimizu 890ddd
		m_sl->setPalette(img->getPalette());
Toshihiro Shimizu 890ddd
		if (levelType != PLI_XSHLEVEL) {
Toshihiro Shimizu 890ddd
			LevelProperties *lprop = m_sl->getProperties();
Toshihiro Shimizu 890ddd
			lprop->setDpiPolicy(LevelProperties::DP_ImageDpi);
Toshihiro Shimizu 890ddd
			int xres = 0, yres = 0;
Toshihiro Shimizu 890ddd
			double dpix = 0, dpiy = 0;
Toshihiro Shimizu 890ddd
			if (TRasterImageP ri = img) {
Toshihiro Shimizu 890ddd
				if (ri->getRaster()) {
Toshihiro Shimizu 890ddd
					TDimension size = ri->getRaster()->getSize();
Toshihiro Shimizu 890ddd
					xres = size.lx;
Toshihiro Shimizu 890ddd
					yres = size.ly;
Toshihiro Shimizu 890ddd
					ri->getDpi(dpix, dpiy);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			} else if (TToonzImageP ti = img) {
Toshihiro Shimizu 890ddd
				if (ti->getRaster()) {
Toshihiro Shimizu 890ddd
					TDimension size = ri->getRaster()->getSize();
Toshihiro Shimizu 890ddd
					xres = size.lx;
Toshihiro Shimizu 890ddd
					yres = size.ly;
Toshihiro Shimizu 890ddd
					ri->getDpi(dpix, dpiy);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			lprop->setDpi(dpix);
Toshihiro Shimizu 890ddd
			lprop->setImageDpi(TPointD(dpix, dpiy));
Toshihiro Shimizu 890ddd
			lprop->setImageRes(TDimension(xres, yres));
Toshihiro Shimizu 890ddd
			// lprop->setHasAlpha(true);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else if (m_type != levelType) {
Toshihiro Shimizu 890ddd
		return -2;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (m_sl->getFrameCount() == 0)
Toshihiro Shimizu 890ddd
		m_sl->setPalette(img->getPalette());
Toshihiro Shimizu 890ddd
	m_sl->setFrame(fid, img);
Toshihiro Shimizu 890ddd
	m_sl->setDirtyFlag(true);
Toshihiro Shimizu 890ddd
	return 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace TScriptBinding