Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "setsaveboxtool.h"
Toshihiro Shimizu 890ddd
#include "tgl.h"
Toshihiro Shimizu 890ddd
#include "tundo.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzimageutils.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshlevelhandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/txsheethandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/ttileset.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshsimplelevel.h"
Toshihiro Shimizu 890ddd
#include "toonz/preferences.h"
Toshihiro Shimizu 890ddd
#include "tools/toolutils.h"
Toshihiro Shimizu 890ddd
#include "tools/cursors.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace ToolUtils;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class SetSaveboxUndo : public TRasterUndo
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRect m_modifiedSavebox;
Toshihiro Shimizu 890ddd
	TRect m_originalSavebox;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	SetSaveboxUndo(TTileSetCM32 *tileSet,
Toshihiro Shimizu 890ddd
				   const TRect &modifiedSavebox,
Toshihiro Shimizu 890ddd
				   const TRect &originalSavebox,
Toshihiro Shimizu 890ddd
				   TXshSimpleLevel *level, const TFrameId &frameId)
Toshihiro Shimizu 890ddd
		: TRasterUndo(tileSet, level, frameId, false, false, 0), m_modifiedSavebox(modifiedSavebox), m_originalSavebox(originalSavebox)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void redo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TToonzImageP ti = getImage();
Toshihiro Shimizu 890ddd
		if (!ti)
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
Toshihiro Shimizu 890ddd
		ti->getRaster()->clearOutside(m_modifiedSavebox);
Toshihiro Shimizu 890ddd
		ti->setSavebox(m_modifiedSavebox);
Toshihiro Shimizu 890ddd
		notifyImageChanged();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void undo() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TRasterUndo::undo();
Toshihiro Shimizu 890ddd
		TToonzImageP ti = getImage();
Toshihiro Shimizu 890ddd
		if (!ti)
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		ti->setSavebox(m_originalSavebox);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int getSize() const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return TRasterUndo::getSize() + sizeof(this) + 100;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~SetSaveboxUndo()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QString getHistoryString()
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return QObject::tr("Set Save Box : (X%1,Y%2,W%3,H%4)->(X%5,Y%6,W%7,H%8)")
Toshihiro Shimizu 890ddd
			.arg(QString::number(m_originalSavebox.x0))
Toshihiro Shimizu 890ddd
			.arg(QString::number(m_originalSavebox.y0))
Toshihiro Shimizu 890ddd
			.arg(QString::number(m_originalSavebox.getLx()))
Toshihiro Shimizu 890ddd
			.arg(QString::number(m_originalSavebox.getLy()))
Toshihiro Shimizu 890ddd
			.arg(QString::number(m_modifiedSavebox.x0))
Toshihiro Shimizu 890ddd
			.arg(QString::number(m_modifiedSavebox.y0))
Toshihiro Shimizu 890ddd
			.arg(QString::number(m_modifiedSavebox.getLx()))
Toshihiro Shimizu 890ddd
			.arg(QString::number(m_modifiedSavebox.getLy()));
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
// SetSaveboxTool
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
SetSaveboxTool::SetSaveboxTool(TTool *tool)
Toshihiro Shimizu 890ddd
	: m_tool(tool), m_pos(), m_modifiedRect()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TToonzImage *SetSaveboxTool::getImage()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TImageP image = m_tool->getImage(true);
Toshihiro Shimizu 890ddd
	if (!image)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	TToonzImageP ti = TToonzImageP(image);
Toshihiro Shimizu 890ddd
	if (!ti)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	return ti.getPointer();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int SetSaveboxTool::getDragType(const TPointD &pos)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TToonzImageP image = getImage();
Toshihiro Shimizu 890ddd
	if (!image)
Toshihiro Shimizu 890ddd
		return eNone;
Toshihiro Shimizu 890ddd
	TRectD bbox = ToonzImageUtils::convertRasterToWorld(convert(image->getBBox()), image);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int ret = 0;
Toshihiro Shimizu 890ddd
	int dx = tmin(fabs(bbox.x0 - pos.x), fabs(bbox.x1 - pos.x));
Toshihiro Shimizu 890ddd
	int dy = tmin(fabs(bbox.y0 - pos.y), fabs(bbox.y1 - pos.y));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double maxDist = 5 * m_tool->getPixelSize();
Toshihiro Shimizu 890ddd
	if (dx > maxDist && dy > maxDist)
Toshihiro Shimizu 890ddd
		return (bbox.contains(pos)) ? eMoveRect : eNone;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (dx <= maxDist && pos.y >= bbox.y0 - maxDist && pos.y <= bbox.y1 + maxDist) {
Toshihiro Shimizu 890ddd
		if (areAlmostEqual(dx, fabs(bbox.x0 - pos.x), maxDist))
Toshihiro Shimizu 890ddd
			ret = eMoveLeft;
Toshihiro Shimizu 890ddd
		else if (areAlmostEqual(dx, fabs(bbox.x1 - pos.x), maxDist))
Toshihiro Shimizu 890ddd
			ret = eMoveRight;
Toshihiro Shimizu 890ddd
	} else if (dy <= maxDist && pos.x >= bbox.x0 - maxDist && pos.x <= bbox.x1 + maxDist) {
Toshihiro Shimizu 890ddd
		if (areAlmostEqual(dy, fabs(bbox.y0 - pos.y), maxDist))
Toshihiro Shimizu 890ddd
			return eMoveDown;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			return eMoveUp;
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		return eNone;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (dy > maxDist)
Toshihiro Shimizu 890ddd
		return ret;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return (areAlmostEqual(dy, fabs(bbox.y0 - pos.y), maxDist)) ? ret | eMoveDown : ret | eMoveUp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int SetSaveboxTool::getCursorId(const TPointD &pos)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int dragType = getDragType(pos);
Toshihiro Shimizu 890ddd
	switch (dragType) {
Toshihiro Shimizu 890ddd
	case eMoveRect:
Toshihiro Shimizu 890ddd
		return ToolCursor::MoveCursor;
Toshihiro Shimizu 890ddd
		CASE eMoveLeft : __OR eMoveRight : return ToolCursor::ScaleHCursor;
Toshihiro Shimizu 890ddd
		CASE eMoveDown : __OR eMoveUp : return ToolCursor::ScaleVCursor;
Toshihiro Shimizu 890ddd
		CASE eMoveLeft | eMoveUp : __OR eMoveRight | eMoveDown : return ToolCursor::ScaleInvCursor;
Toshihiro Shimizu 890ddd
		CASE eMoveLeft | eMoveDown : __OR eMoveRight | eMoveUp : return ToolCursor::ScaleCursor;
Toshihiro Shimizu 890ddd
	DEFAULT:
Toshihiro Shimizu 890ddd
		return ToolCursor::StrokeSelectCursor;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return ToolCursor::StrokeSelectCursor;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SetSaveboxTool::leftButtonDown(const TPointD &pos)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TToonzImageP image = getImage();
Toshihiro Shimizu 890ddd
	if (!image)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	m_modifiedRect = ToonzImageUtils::convertRasterToWorld(convert(image->getBBox()), image);
Toshihiro Shimizu 890ddd
	m_pos = pos;
Toshihiro Shimizu 890ddd
	m_movementType = (SetSaveboxTool::Moviment)getDragType(pos);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SetSaveboxTool::leftButtonDrag(const TPointD &pos)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TToonzImageP image = getImage();
Toshihiro Shimizu 890ddd
	if (!image)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_movementType == eNone)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (m_movementType == eMoveRect)
Toshihiro Shimizu 890ddd
		m_modifiedRect = m_modifiedRect + (pos - m_pos);
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		if (m_movementType & eMoveLeft)
Toshihiro Shimizu 890ddd
			m_modifiedRect.x0 += pos.x - m_pos.x;
Toshihiro Shimizu 890ddd
		if (m_movementType & eMoveRight)
Toshihiro Shimizu 890ddd
			m_modifiedRect.x1 += pos.x - m_pos.x;
Toshihiro Shimizu 890ddd
		if (m_movementType & eMoveDown)
Toshihiro Shimizu 890ddd
			m_modifiedRect.y0 += pos.y - m_pos.y;
Toshihiro Shimizu 890ddd
		if (m_movementType & eMoveUp)
Toshihiro Shimizu 890ddd
			m_modifiedRect.y1 += pos.y - m_pos.y;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	m_pos = pos;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SetSaveboxTool::leftButtonUp(const TPointD &pos)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TToonzImageP image = getImage();
Toshihiro Shimizu 890ddd
	if (!image)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	m_pos = TPointD();
Toshihiro Shimizu 890ddd
	TRectD originalSavebox = image->getBBox();
Toshihiro Shimizu 890ddd
	TRect savebox = ToonzImageUtils::convertWorldToRaster(m_modifiedRect, image);
Toshihiro Shimizu 890ddd
	if (savebox.isEmpty()) {
Toshihiro Shimizu 890ddd
		m_modifiedRect = TRectD();
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TRasterCM32P ras = image->getRaster();
Toshihiro Shimizu 890ddd
	TTileSetCM32 *tileSet = new TTileSetCM32(ras->getSize());
Toshihiro Shimizu 890ddd
	tileSet->add(ras, image->getSavebox());
Toshihiro Shimizu 890ddd
	/*-- 以下を有効にすると、SaveBoxの外の絵のデータが失われる --*/
Toshihiro Shimizu 890ddd
	//ras->clearOutside(savebox);
Toshihiro Shimizu 890ddd
	//if(Preferences::instance()->isMinimizeSaveboxAfterEditing())
Toshihiro Shimizu 890ddd
	//	TRop::computeBBox(ras, savebox);
Toshihiro Shimizu 890ddd
	image->setSavebox(savebox);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TXshSimpleLevel *level = m_tool->getApplication()->getCurrentLevel()->getSimpleLevel();
Toshihiro Shimizu 890ddd
	TUndoManager::manager()->add(new SetSaveboxUndo(tileSet, savebox, convert(originalSavebox), level, m_tool->getCurrentFid()));
Toshihiro Shimizu 890ddd
	m_modifiedRect = TRectD();
Toshihiro Shimizu 890ddd
	m_tool->notifyImageChanged();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void SetSaveboxTool::draw()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TToonzImageP image = getImage();
Toshihiro Shimizu 890ddd
	if (!image)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRectD bbox;
Toshihiro Shimizu 890ddd
	if (m_modifiedRect == TRectD())
Toshihiro Shimizu 890ddd
		bbox = ToonzImageUtils::convertRasterToWorld(convert(image->getBBox()), image);
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		bbox = m_modifiedRect;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	drawRect(bbox * image->getSubsampling(), TPixel32::Black, 0x5555, true);
Toshihiro Shimizu 890ddd
	tglColor(TPixel32(90, 90, 90));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double pixelSize = m_tool->getPixelSize();
Toshihiro Shimizu 890ddd
	TPointD p00 = bbox.getP00();
Toshihiro Shimizu 890ddd
	TPointD p11 = bbox.getP11();
Toshihiro Shimizu 890ddd
	TPointD p01 = bbox.getP01();
Toshihiro Shimizu 890ddd
	TPointD p10 = bbox.getP10();
Toshihiro Shimizu 890ddd
	TPointD p11_01 = (bbox.getP11() + bbox.getP01()) * 0.5;
Toshihiro Shimizu 890ddd
	TPointD p11_10 = (bbox.getP11() + bbox.getP10()) * 0.5;
Toshihiro Shimizu 890ddd
	TPointD p00_01 = (bbox.getP00() + bbox.getP01()) * 0.5;
Toshihiro Shimizu 890ddd
	TPointD p00_10 = (bbox.getP00() + bbox.getP10()) * 0.5;
Toshihiro Shimizu 890ddd
	TPointD size(pixelSize * 4, pixelSize * 4);
Toshihiro Shimizu 890ddd
	tglDrawRect(TRectD(p00 - size, p00 + size));
Toshihiro Shimizu 890ddd
	tglDrawRect(TRectD(p11 - size, p11 + size));
Toshihiro Shimizu 890ddd
	tglDrawRect(TRectD(p01 - size, p01 + size));
Toshihiro Shimizu 890ddd
	tglDrawRect(TRectD(p10 - size, p10 + size));
Toshihiro Shimizu 890ddd
	tglDrawRect(TRectD(p11_01 - size, p11_01 + size));
Toshihiro Shimizu 890ddd
	tglDrawRect(TRectD(p11_10 - size, p11_10 + size));
Toshihiro Shimizu 890ddd
	tglDrawRect(TRectD(p00_01 - size, p00_01 + size));
Toshihiro Shimizu 890ddd
	tglDrawRect(TRectD(p00_10 - size, p00_10 + size));
Toshihiro Shimizu 890ddd
}