tahoma2d/toonz/sources/tnztools/rasterselectiontool.cpp

1048 lines
36 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include "rasterselectiontool.h"
#include "vectorselectiontool.h"
#include "drawutil.h"
#include "tenv.h"
#include "tools/toolhandle.h"
#include "toonz/tdistort.h"
#include "toonz/glrasterpainter.h"
#include "toonz/toonzimageutils.h"
#include "toonzqt/tselectionhandle.h"
#include "toonzqt/imageutils.h"
#include "toonz/txshlevelhandle.h"
2016-03-19 06:57:51 +13:00
using namespace ToolUtils;
using namespace DragSelectionTool;
//=============================================================================
// RasterFreeDeformer
//-----------------------------------------------------------------------------
RasterFreeDeformer::RasterFreeDeformer(TRasterP ras)
2016-06-15 18:43:10 +12:00
: FreeDeformer(), m_ras(ras), m_newRas(), m_noAntialiasing(false) {
TRect r = ras->getBounds();
m_originalP00 = convert(r.getP00());
m_originalP11 = convert(r.getP11());
m_newPoints.push_back(m_originalP00);
m_newPoints.push_back(convert(r.getP10()));
m_newPoints.push_back(m_originalP11);
m_newPoints.push_back(convert(r.getP01()));
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
RasterFreeDeformer::~RasterFreeDeformer() {}
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterFreeDeformer::setPoint(int index, const TPointD &p) {
m_newPoints[index] = p;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterFreeDeformer::setPoints(const TPointD &p0, const TPointD &p1,
const TPointD &p2, const TPointD &p3) {
m_newPoints[0] = p0;
m_newPoints[1] = p1;
m_newPoints[2] = p2;
m_newPoints[3] = p3;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterFreeDeformer::deformImage() {
TPointD p00 = TPointD();
TPointD p10 = m_newPoints[1] - m_newPoints[0];
TPointD p11 = m_newPoints[2] - m_newPoints[0];
TPointD p01 = m_newPoints[3] - m_newPoints[0];
double x0 = std::min({p00.x, p10.x, p11.x, p01.x});
double y0 = std::min({p00.y, p10.y, p11.y, p01.y});
double x1 = std::max({p00.x, p10.x, p11.x, p01.x});
double y1 = std::max({p00.y, p10.y, p11.y, p01.y});
TRectD sourceRect(TPointD(), TPointD(m_ras->getLx(), m_ras->getLy()));
BilinearDistorterBase dist(sourceRect.getP00(), sourceRect.getP10(),
sourceRect.getP01(), sourceRect.getP11(), p00, p10,
p01, p11);
TRect destRect(tfloor(x0), tfloor(y0), tceil(x1) - 1, tceil(y1) - 1);
if (TRasterCM32P ras = (TRasterCM32P)m_ras)
m_newRas = TRasterCM32P(destRect.getLx(), destRect.getLy());
else if (TRaster32P ras = (TRaster32P)m_ras)
m_newRas = TRaster32P(destRect.getLx(), destRect.getLy());
TRasterP newRas(m_newRas); // Someway, conversion from TRasterCM32P to
// TRasterP is not automatic
distort(newRas, m_ras, dist, destRect.getP00(),
m_noAntialiasing ? TRop::ClosestPixel : TRop::Bilinear);
2016-03-19 06:57:51 +13:00
}
//=============================================================================
// UndoRasterDeform
//-----------------------------------------------------------------------------
DragSelectionTool::UndoRasterDeform::UndoRasterDeform(RasterSelectionTool *tool)
2016-06-15 18:43:10 +12:00
: TUndo()
, m_tool(tool)
, m_oldBBox(tool->getBBox())
, m_newBBox()
, m_oldCenter(tool->getCenter())
, m_newCenter()
, m_dim() {
RasterSelection *selection = (RasterSelection *)tool->getSelection();
m_oldStrokes = selection->getStrokes();
m_oldFloatingImageId =
"UndoRasterDeform_old_floating_" + std::to_string(m_id++);
TRasterP floatingRas = selection->getFloatingSelection();
TImageP floatingImage;
if (TRasterCM32P toonzRas = (TRasterCM32P)(floatingRas)) {
floatingImage = TToonzImageP(toonzRas, toonzRas->getBounds());
m_dim = toonzRas->getSize();
m_pixelSize = toonzRas->getPixelSize();
}
if (TRaster32P fullColorRas = (TRaster32P)(floatingRas)) {
floatingImage = TRasterImageP(fullColorRas);
m_dim = fullColorRas->getSize();
m_pixelSize = fullColorRas->getPixelSize();
}
if (TRasterGR8P grRas = (TRasterGR8P)(floatingRas)) {
floatingImage = TRasterImageP(grRas);
m_dim = grRas->getSize();
m_pixelSize = grRas->getPixelSize();
}
TImageCache::instance()->add(m_oldFloatingImageId, floatingImage, false);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
DragSelectionTool::UndoRasterDeform::~UndoRasterDeform() {
if (TImageCache::instance()->isCached(m_oldFloatingImageId))
TImageCache::instance()->remove(m_oldFloatingImageId);
if (TImageCache::instance()->isCached(m_newFloatingImageId))
TImageCache::instance()->remove(m_newFloatingImageId);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::UndoRasterDeform::registerRasterDeformation() {
RasterSelection *selection = (RasterSelection *)m_tool->getSelection();
m_newStrokes = selection->getStrokes();
m_newFloatingImageId =
"UndoRasterDeform_new_floating_" + std::to_string(m_id);
TRasterP floatingRas = selection->getFloatingSelection();
TImageP floatingImage;
if (TRasterCM32P toonzRas = (TRasterCM32P)(floatingRas))
floatingImage = TToonzImageP(toonzRas, toonzRas->getBounds());
if (TRaster32P fullColorRas = (TRaster32P)(floatingRas))
floatingImage = TRasterImageP(fullColorRas);
if (TRasterGR8P grRas = (TRasterGR8P)(floatingRas))
floatingImage = TRasterImageP(grRas);
TImageCache::instance()->add(m_newFloatingImageId, floatingImage, false);
m_newBBox = m_tool->getBBox();
m_newCenter = m_tool->getCenter();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::UndoRasterDeform::undo() const {
RasterSelection *selection = (RasterSelection *)m_tool->getSelection();
if (!selection->isFloating()) return;
TImageP img = TImageCache::instance()->get(m_oldFloatingImageId, false);
TRasterP ras;
if (TToonzImageP ti = (TToonzImageP)(img)) ras = ti->getRaster();
if (TRasterImageP ri = (TRasterImageP)(img)) ras = ri->getRaster();
selection->setFloatingSeletion(ras);
selection->setStrokes(m_oldStrokes);
m_tool->setBBox(m_oldBBox);
m_tool->setCenter(m_oldCenter);
m_tool->invalidate();
m_tool->decreaseTransformationCount();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::UndoRasterDeform::redo() const {
RasterSelection *selection = (RasterSelection *)m_tool->getSelection();
if (!selection->isFloating()) return;
TToonzImageP img = TImageCache::instance()->get(m_newFloatingImageId, false);
TRasterP ras;
if (TToonzImageP ti = (TToonzImageP)(img)) ras = ti->getRaster();
if (TRasterImageP ri = (TRasterImageP)(img)) ras = ri->getRaster();
selection->setStrokes(m_newStrokes);
m_tool->setBBox(m_newBBox);
m_tool->setCenter(m_newCenter);
m_tool->invalidate();
m_tool->increaseTransformationCount();
}
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
int DragSelectionTool::UndoRasterDeform::getSize() const {
return sizeof(*this) + (m_dim.lx * m_dim.ly * m_pixelSize);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
int DragSelectionTool::UndoRasterDeform::m_id = 0;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
//=============================================================================
// UndoRasterTransform
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
DragSelectionTool::UndoRasterTransform::UndoRasterTransform(
RasterSelectionTool *tool)
: m_tool(tool) {
m_oldDeformValues = m_tool->m_deformValues;
RasterSelection *selection =
dynamic_cast<RasterSelection *>(tool->getSelection());
m_oldTransform = selection->getTransformation();
m_oldCenter = tool->getCenter();
m_oldBbox = tool->getBBox();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::UndoRasterTransform::setChangedValues() {
m_newDeformValues = m_tool->m_deformValues;
RasterSelection *selection =
dynamic_cast<RasterSelection *>(m_tool->getSelection());
m_newTransform = selection->getTransformation();
m_newCenter = m_tool->getCenter();
m_newBbox = m_tool->getBBox();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::UndoRasterTransform::undo() const {
m_tool->transformFloatingSelection(m_oldTransform, m_oldCenter, m_oldBbox);
m_tool->m_deformValues = m_oldDeformValues;
m_tool->decreaseTransformationCount();
TTool::getApplication()->getCurrentTool()->notifyToolChanged();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::UndoRasterTransform::redo() const {
m_tool->transformFloatingSelection(m_newTransform, m_newCenter, m_newBbox);
m_tool->m_deformValues = m_newDeformValues;
m_tool->increaseTransformationCount();
TTool::getApplication()->getCurrentTool()->notifyToolChanged();
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
//=============================================================================
// RasterDeformTool
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
DragSelectionTool::RasterDeformTool::RasterDeformTool(RasterSelectionTool *tool,
bool freeDeformer)
: DeformTool(tool)
, m_transformUndo(0)
, m_deformUndo(0)
, m_isFreeDeformer(freeDeformer) {
if (!m_isFreeDeformer) m_transformUndo = new UndoRasterTransform(tool);
}
//-----------------------------------------------------------------------------
2021-04-21 20:42:56 +12:00
void DragSelectionTool::RasterDeformTool::applyTransform(FourPoints bbox,
bool onFastDragging) {
2016-06-15 18:43:10 +12:00
RasterSelectionTool *tool = (RasterSelectionTool *)getTool();
tool->setNewFreeDeformer();
if (!m_deformUndo) m_deformUndo = new UndoRasterDeform(tool);
RasterSelection *selection =
dynamic_cast<RasterSelection *>(tool->getSelection());
assert(selection);
FourPoints realBbox = bbox * selection->getTransformation().inv();
RasterFreeDeformer *freeDeformer =
(RasterFreeDeformer *)tool->getFreeDeformer();
2017-09-14 15:12:24 +12:00
if (!freeDeformer) return;
2021-04-21 20:42:56 +12:00
freeDeformer->setNoAntialiasing(tool->getNoAntialiasingValue() |
onFastDragging);
2016-06-15 18:43:10 +12:00
freeDeformer->setPoints(realBbox.getP00(), realBbox.getP10(),
realBbox.getP11(), realBbox.getP01());
freeDeformer->deformImage();
selection->setFloatingSeletion(freeDeformer->getImage());
VectorFreeDeformer *vectorFreeDeformer = tool->getSelectionFreeDeformer();
if (vectorFreeDeformer) {
vectorFreeDeformer->setPoints(realBbox.getP00(), realBbox.getP10(),
realBbox.getP11(), realBbox.getP01());
vectorFreeDeformer->deformImage();
TVectorImage *vi = vectorFreeDeformer->getDeformedImage();
std::vector<TStroke> newStrokes;
int i;
for (i = 0; i < (int)vi->getStrokeCount(); i++)
newStrokes.push_back(*(vi->getStroke(i)));
selection->setStrokes(newStrokes);
}
tool->m_deformValues.m_isSelectionModified = true;
if (!m_isDragging) tool->notifyImageChanged();
}
//-----------------------------------------------------------------------------
void DragSelectionTool::RasterDeformTool::applyTransform(TAffine aff,
bool modifyCenter) {
m_transform *= aff;
RasterSelectionTool *tool = dynamic_cast<RasterSelectionTool *>(getTool());
RasterSelection *rasterSelection =
dynamic_cast<RasterSelection *>(getTool()->getSelection());
rasterSelection->transform(aff);
tool->setBBox(tool->getBBox() * aff);
if (modifyCenter) tool->setCenter(aff * tool->getCenter());
if (!m_isDragging && !rasterSelection->isFloating())
rasterSelection->makeFloating();
else if (!m_isDragging)
tool->notifyImageChanged();
}
//-----------------------------------------------------------------------------
void DragSelectionTool::RasterDeformTool::addTransformUndo() {
RasterSelection *rasterSelection =
dynamic_cast<RasterSelection *>(getTool()->getSelection());
if (!rasterSelection || !rasterSelection->isFloating()) return;
RasterSelectionTool *tool = dynamic_cast<RasterSelectionTool *>(getTool());
assert(tool);
if (!m_isFreeDeformer) {
if (!m_transformUndo) return;
m_transformUndo->setChangedValues();
m_transform = TAffine();
TUndoManager::manager()->add(m_transformUndo);
} else {
if (!m_deformUndo) return;
m_deformUndo->registerRasterDeformation();
TUndoManager::manager()->add(m_deformUndo);
}
tool->increaseTransformationCount();
2016-03-19 06:57:51 +13:00
}
//=============================================================================
// RasterRotationTool
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
DragSelectionTool::RasterRotationTool::RasterRotationTool(
RasterSelectionTool *tool)
: RasterDeformTool(tool, false) {
m_rotation = new Rotation(this);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::RasterRotationTool::transform(TAffine aff,
double angle) {
applyTransform(aff, false);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::RasterRotationTool::leftButtonDrag(
const TPointD &pos, const TMouseEvent &e) {
m_rotation->leftButtonDrag(pos, e);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::RasterRotationTool::draw() { m_rotation->draw(); }
2016-03-19 06:57:51 +13:00
//=============================================================================
// RasterFreeDeformTool
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
DragSelectionTool::RasterFreeDeformTool::RasterFreeDeformTool(
RasterSelectionTool *tool)
: RasterDeformTool(tool, true) {
m_freeDeform = new FreeDeform(this);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::RasterFreeDeformTool::leftButtonDrag(
const TPointD &pos, const TMouseEvent &e) {
m_freeDeform->leftButtonDrag(pos, e);
2016-03-19 06:57:51 +13:00
}
2021-04-21 20:42:56 +12:00
//-----------------------------------------------------------------------------
void DragSelectionTool::RasterFreeDeformTool::leftButtonUp(
const TPointD &pos, const TMouseEvent &e) {
m_freeDeform->leftButtonUp();
RasterDeformTool::leftButtonUp(pos, e);
}
2016-03-19 06:57:51 +13:00
//=============================================================================
// RasterMoveSelectionTool
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
DragSelectionTool::RasterMoveSelectionTool::RasterMoveSelectionTool(
RasterSelectionTool *tool)
: RasterDeformTool(tool, false) {
m_moveSelection = new MoveSelection(this);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::RasterMoveSelectionTool::transform(TAffine aff) {
applyTransform(aff, true);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::RasterMoveSelectionTool::leftButtonDown(
const TPointD &pos, const TMouseEvent &e) {
m_moveSelection->leftButtonDown(pos, e);
RasterDeformTool::leftButtonDown(pos, e);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::RasterMoveSelectionTool::leftButtonDrag(
const TPointD &pos, const TMouseEvent &e) {
m_moveSelection->leftButtonDrag(pos, e);
2016-03-19 06:57:51 +13:00
}
//=============================================================================
// RasterScaleTool
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
DragSelectionTool::RasterScaleTool::RasterScaleTool(RasterSelectionTool *tool,
ScaleType type)
2016-06-15 18:43:10 +12:00
: RasterDeformTool(tool, true) {
m_scale = new Scale(this, type);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2021-04-21 20:42:56 +12:00
TPointD DragSelectionTool::RasterScaleTool::transform(int index, TPointD newPos,
bool onFastDragging) {
2016-06-15 18:43:10 +12:00
SelectionTool *tool = getTool();
TPointD scaleValue = tool->m_deformValues.m_scaleValue;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::vector<FourPoints> startBboxs = m_scale->getStartBboxs();
FourPoints bbox =
m_scale->bboxScaleInCenter(index, startBboxs[0], newPos, scaleValue,
m_scale->getStartCenter(), true);
if (bbox == startBboxs[0]) return scaleValue;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Se non ho scalato rispetto al centro calcolo la posizione del nuovo centro
if (!m_scale->scaleInCenter())
tool->setCenter(m_scale->getNewCenter(index, startBboxs[0], scaleValue));
2016-03-19 06:57:51 +13:00
2021-04-21 20:42:56 +12:00
applyTransform(bbox, onFastDragging);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
tool->setBBox(bbox);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return scaleValue;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::RasterScaleTool::leftButtonDown(const TPointD &pos,
const TMouseEvent &e) {
m_scale->leftButtonDown(pos, e);
RasterDeformTool::leftButtonDown(pos, e);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void DragSelectionTool::RasterScaleTool::leftButtonDrag(const TPointD &pos,
const TMouseEvent &e) {
m_scale->leftButtonDrag(pos, e);
2016-03-19 06:57:51 +13:00
}
2021-04-21 20:42:56 +12:00
//-----------------------------------------------------------------------------
void DragSelectionTool::RasterScaleTool::leftButtonUp(const TPointD &pos,
const TMouseEvent &e) {
m_scale->leftButtonUp();
RasterDeformTool::leftButtonUp(pos, e);
}
2016-03-19 06:57:51 +13:00
TEnv::IntVar ModifySavebox("ModifySavebox", 0);
TEnv::IntVar NoAntialiasing("NoAntialiasing", 0);
//=============================================================================
// RasterSelectionTool
//-----------------------------------------------------------------------------
RasterSelectionTool::RasterSelectionTool(int targetType)
2016-06-15 18:43:10 +12:00
: SelectionTool(targetType)
, m_transformationCount(0)
, m_selectionFreeDeformer(0)
, m_noAntialiasing("No Antialiasing", false)
, m_modifySavebox("Modify Savebox", false)
, m_setSaveboxTool(0) {
m_prop.bind(m_noAntialiasing);
m_rasterSelection.setView(this);
if (m_targetType & ToonzImage) {
m_setSaveboxTool = new SetSaveboxTool(this);
m_modifySavebox.setId("ModifySavebox");
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterSelectionTool::setBBox(const DragSelectionTool::FourPoints &points,
int index) {
if (m_bboxs.empty()) return;
assert((int)m_bboxs.size() > index);
m_bboxs[index] = points;
TAffine aff = m_rasterSelection.getTransformation();
DragSelectionTool::FourPoints p = points * aff.inv();
m_rasterSelection.setSelectionBbox(p.getBox());
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterSelectionTool::setNewFreeDeformer() {
if (!m_freeDeformers.empty() || isSelectionEmpty()) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TImageP image = (TImageP)getImage(true);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TToonzImageP ti = (TToonzImageP)image;
TRasterImageP ri = (TRasterImageP)image;
if (!ti && !ri) return;
2016-03-19 06:57:51 +13:00
if (!m_rasterSelection.isEditable()) return;
2016-06-15 18:43:10 +12:00
if (!isFloating()) m_rasterSelection.makeFloating();
m_freeDeformers.push_back(
new RasterFreeDeformer(m_rasterSelection.getFloatingSelection()));
std::vector<TStroke> strokes = m_rasterSelection.getOriginalStrokes();
if (!strokes.empty()) {
TVectorImage *vi = new TVectorImage();
std::set<int> indices;
// Devo deformare anche gli strokes della selezione!!!
int i;
for (i = 0; i < (int)strokes.size(); i++) {
vi->addStroke(new TStroke(strokes[i]));
indices.insert(i);
}
m_selectionFreeDeformer = new VectorFreeDeformer(vi, indices);
m_selectionFreeDeformer->setPreserveThickness(true);
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
VectorFreeDeformer *RasterSelectionTool::getSelectionFreeDeformer() const {
return m_selectionFreeDeformer;
}
//-----------------------------------------------------------------------------
bool RasterSelectionTool::isFloating() const {
return m_rasterSelection.isFloating();
}
//-----------------------------------------------------------------------------
void RasterSelectionTool::modifySelectionOnClick(TImageP image,
const TPointD &pos,
const TMouseEvent &e) {
const TXshCell &imageCell = TTool::getImageCell();
TToonzImageP ti = (TToonzImageP)image;
TRasterImageP ri = (TRasterImageP)image;
if (!ti && !ri) return;
m_rasterSelection.makeCurrent();
updateAction(pos, e);
m_firstPos = m_curPos = pos;
if (!m_rasterSelection.isEmpty() && !m_rasterSelection.isFloating() &&
e.isShiftPressed() && !m_rasterSelection.isTransformed()) {
m_selectingRect.empty();
m_transformationCount = 0;
m_selecting = true;
} else if (!m_rasterSelection.isEmpty()) {
m_selectingRect.empty();
m_selecting = false;
if (m_what == Outside && m_rasterSelection.isFloating()) {
m_rasterSelection.pasteFloatingSelection();
} else if (m_what == Outside && !m_rasterSelection.isFloating()) {
m_rasterSelection.setCurrentImage(image, imageCell);
m_rasterSelection.selectNone();
m_bboxs.clear();
m_selectingRect.empty();
m_selecting = true;
} else {
if (!m_rasterSelection.isFloating() &&
(m_what == Inside || m_what == ROTATION || m_what == SCALE ||
m_what == SCALE_X || m_what == SCALE_Y)) {
m_rasterSelection.makeFloating();
m_transformationCount = 0;
m_rasterSelection.setTransformationCount(0);
}
}
} else {
if (m_what == Outside) {
m_rasterSelection.setCurrentImage(image, imageCell);
m_rasterSelection.selectNone();
m_bboxs.clear();
m_selectingRect.empty();
m_selecting = true;
}
}
TTool::getApplication()
->getCurrentTool()
->notifyToolChanged(); // Refreshes toolbar values
2016-06-15 18:43:10 +12:00
invalidate();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterSelectionTool::leftButtonDown(const TPointD &pos,
const TMouseEvent &e) {
if (m_setSaveboxTool && m_modifySavebox.getValue()) {
m_setSaveboxTool->leftButtonDown(pos);
return;
}
SelectionTool::leftButtonDown(pos, e);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterSelectionTool::mouseMove(const TPointD &pos, const TMouseEvent &e) {
if (m_setSaveboxTool && m_modifySavebox.getValue()) {
if (!m_leftButtonMousePressed)
m_cursorId = m_setSaveboxTool->getCursorId(pos);
return;
}
SelectionTool::mouseMove(pos, e);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterSelectionTool::leftButtonDrag(const TPointD &pos,
const TMouseEvent &e) {
if (m_setSaveboxTool && m_modifySavebox.getValue()) {
m_setSaveboxTool->leftButtonDrag(pos);
invalidate();
return;
}
2021-04-21 20:42:56 +12:00
double pixelSize = getPixelSize();
2016-06-15 18:43:10 +12:00
if (m_dragTool) {
2021-04-21 20:42:56 +12:00
// Even in Windows version deformation is processed at interval of 20msec.
// (See SceneViewer::tabletEvent)
if (e.isHighFrequent()) return;
if (!m_rasterSelection.isEditable()) return;
2016-06-15 18:43:10 +12:00
m_dragTool->leftButtonDrag(pos, e);
invalidate();
return;
}
TImageP image = getImage(true);
TToonzImageP ti = (TToonzImageP)image;
TRasterImageP ri = (TRasterImageP)image;
if (!ti && !ri) return;
if (m_selecting) {
if (m_strokeSelectionType.getValue() == RECT_SELECTION) {
TDimension imageSize;
if (ti)
imageSize = ti->getSize();
else if (ri)
imageSize = ri->getRaster()->getSize();
TPointD p(imageSize.lx % 2 ? 0.5 : 0.0, imageSize.ly % 2 ? 0.5 : 0.0);
TRectD rectD(tround(std::min(m_firstPos.x, pos.x) - p.x) + p.x,
tround(std::min(m_firstPos.y, pos.y) - p.y) + p.y,
tround(std::max(m_firstPos.x, pos.x) - p.x) + p.x,
tround(std::max(m_firstPos.y, pos.y) - p.y) + p.y);
m_selectingRect = rectD;
m_bboxs.clear();
TTool::getApplication()
->getCurrentTool()
->notifyToolChanged(); // Refreshes toolbar values
2016-06-15 18:43:10 +12:00
invalidate();
2017-11-14 16:38:01 +13:00
} else if (m_strokeSelectionType.getValue() == FREEHAND_SELECTION) {
2016-06-15 18:43:10 +12:00
freehandDrag(pos);
2017-11-14 16:38:01 +13:00
invalidate();
}
2016-06-15 18:43:10 +12:00
return;
}
TTool::Application *app = TTool::getApplication();
if (!app || m_justSelected || !m_selecting ||
tdistance2(pos, m_curPos) < 9.0 * pixelSize * pixelSize)
return;
m_curPos = pos;
2017-11-14 16:38:01 +13:00
if (m_strokeSelectionType.getValue() == FREEHAND_SELECTION) {
2016-06-15 18:43:10 +12:00
freehandDrag(pos);
2017-11-14 16:38:01 +13:00
invalidate();
} else if (m_strokeSelectionType.getValue() == RECT_SELECTION) {
2016-06-15 18:43:10 +12:00
bool selectOverlappingStroke = (m_firstPos.x > pos.x);
TRectD rect(m_firstPos, pos);
m_selectingRect = rect;
TTool::getApplication()
->getCurrentTool()
->notifyToolChanged(); // Refreshes toolbar values
2016-06-15 18:43:10 +12:00
invalidate();
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterSelectionTool::leftButtonUp(const TPointD &pos,
const TMouseEvent &e) {
if (m_setSaveboxTool && m_modifySavebox.getValue()) {
m_setSaveboxTool->leftButtonUp(pos);
invalidate();
return;
}
m_leftButtonMousePressed = false;
m_shiftPressed = false;
if (m_dragTool) {
if (!m_rasterSelection.isEditable()) {
delete m_dragTool;
m_dragTool = 0;
return;
}
2016-06-15 18:43:10 +12:00
m_dragTool->leftButtonUp(pos, e);
delete m_dragTool;
m_dragTool = 0;
invalidate();
notifyImageChanged();
return;
}
if (!m_selecting) return;
// Se stavo modificando la selezione la completo
TImageP image = getImage(true);
TToonzImageP ti = (TToonzImageP)image;
TRasterImageP ri = (TRasterImageP)image;
if (ti || ri) {
if (m_strokeSelectionType.getValue() == RECT_SELECTION) {
m_bboxs.push_back(m_selectingRect);
m_rasterSelection.select(
TRectD(m_selectingRect.getP00(), m_selectingRect.getP11()));
m_rasterSelection.setFrameId(getCurrentFid());
m_selectingRect.empty();
} else if (m_strokeSelectionType.getValue() == FREEHAND_SELECTION) {
closeFreehand(pos);
if (m_stroke->getControlPointCount() > 5) {
m_rasterSelection.select(*m_stroke);
m_rasterSelection.setFrameId(getCurrentFid());
m_rasterSelection.makeCurrent();
}
2017-11-14 16:38:01 +13:00
m_track.clear();
2016-06-15 18:43:10 +12:00
}
}
m_selecting = false;
m_justSelected = false;
invalidate();
}
//-----------------------------------------------------------------------------
void RasterSelectionTool::leftButtonDoubleClick(const TPointD &pos,
const TMouseEvent &e) {
TImageP image = getImage(true);
TToonzImageP ti = (TToonzImageP)image;
TRasterImageP ri = (TRasterImageP)image;
if (!ti && !ri) return;
if (m_strokeSelectionType.getValue() == POLYLINE_SELECTION &&
!m_polyline.empty()) {
closePolyline(pos);
if (m_stroke) {
m_rasterSelection.select(*m_stroke);
m_rasterSelection.setFrameId(getCurrentFid());
m_rasterSelection.makeCurrent();
}
m_selecting = false;
return;
}
TTool::getApplication()
->getCurrentTool()
->notifyToolChanged(); // Refreshes toolbar values
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
/*-- Paste後のフローティング状態の画像の描画 --*/
2016-06-15 18:43:10 +12:00
void RasterSelectionTool::drawFloatingSelection() {
double pixelSize =
TTool::getApplication()->getCurrentTool()->getTool()->getPixelSize();
TAffine aff = m_rasterSelection.getTransformation();
glPushMatrix();
tglMultMatrix(aff);
// draw m_floatingSelection
if (isFloating()) {
TRasterP floatingSelection = m_rasterSelection.getFloatingSelection();
TImageP app;
if (TRasterCM32P toonzRas = (TRasterCM32P)(floatingSelection))
app = TToonzImageP(toonzRas, toonzRas->getBounds());
if (TRaster32P fullColorRas = (TRaster32P)(floatingSelection))
app = TRasterImageP(fullColorRas);
if (TRasterGR8P grRas = (TRasterGR8P)(floatingSelection))
app = TRasterImageP(grRas);
app->setPalette(m_rasterSelection.getCurrentImage()->getPalette());
FourPoints points = getBBox() * aff.inv();
TRectD bbox = points.getBox();
TPointD center((bbox.getP00() + bbox.getP11()) * 0.5);
if (TToonzImageP ti = (TToonzImageP)app)
GLRasterPainter::drawRaster(TTranslation(center), ti, false);
if (TRasterImageP ri = (TRasterImageP)app)
GLRasterPainter::drawRaster(TTranslation(center), ri, true);
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
std::vector<TStroke> strokes = m_rasterSelection.getStrokes();
int i;
for (i = 0; i < (int)strokes.size(); i++) {
TStroke stroke = strokes[i];
glEnable(GL_LINE_STIPPLE);
glLineStipple(1, 0xF0F0);
tglColor(TPixel32::Black);
drawStrokeCenterline(stroke, pixelSize);
glDisable(GL_LINE_STIPPLE);
}
glPopMatrix();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterSelectionTool::draw() {
TImageP image = getImage(false);
TToonzImageP ti = (TToonzImageP)image;
TRasterImageP ri = (TRasterImageP)image;
if (!ti && !ri) return;
if (m_setSaveboxTool && m_modifySavebox.getValue()) {
m_setSaveboxTool->draw();
return;
}
glPushMatrix();
/*-- フローティング画像の描画 --*/
drawFloatingSelection();
if (m_strokeSelectionType.getValue() == POLYLINE_SELECTION &&
!m_rasterSelection.isFloating())
drawPolylineSelection();
2017-11-14 16:38:01 +13:00
else if (m_strokeSelectionType.getValue() == FREEHAND_SELECTION &&
!m_rasterSelection.isFloating())
drawFreehandSelection();
2016-06-15 18:43:10 +12:00
if (m_rasterSelection.isEmpty()) m_bboxs.clear();
/*-- 選択範囲の変形ハンドルの描画 --*/
if (getBBoxsCount() > 0) drawCommandHandle(image.getPointer());
/*-- 選択範囲の四角形の描画 --*/
if (m_selecting && !m_selectingRect.isEmpty())
drawRectSelection(image.getPointer());
/*-- バウンディングボックスの描画 --*/
/*
if(ti)
2016-03-19 06:57:51 +13:00
{
2016-06-15 18:43:10 +12:00
TRectD saveBox = ToonzImageUtils::convertRasterToWorld(ti->getSavebox(), ti);
drawRect(saveBox.enlarge(0.5)*ti->getSubsampling(), TPixel32::Black, 0x5555,
true);
}
*/
glPopMatrix();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TSelection *RasterSelectionTool::getSelection() { return &m_rasterSelection; }
//-----------------------------------------------------------------------------
bool RasterSelectionTool::isSelectionEmpty() {
TImageP image = getImage(false);
TToonzImageP ti = (TToonzImageP)image;
TRasterImageP ri = (TRasterImageP)image;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!ti && !ri) return true;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return m_rasterSelection.isEmpty();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterSelectionTool::computeBBox() {
TImageP image = getImage(false);
TToonzImageP ti = (TToonzImageP)image;
TRasterImageP ri = (TRasterImageP)image;
if (!ti && !ri) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_deformValues.reset();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_bboxs.clear();
m_centers.clear();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
{
std::vector<TStroke> strokes = m_rasterSelection.getStrokes();
TRectD strokesRect = m_rasterSelection.getStrokesBound(strokes);
DragSelectionTool::FourPoints p;
p = strokesRect;
p = p * m_rasterSelection.getTransformation();
m_bboxs.push_back(p);
m_centers.push_back((p.getP00() + p.getP11()) * 0.5);
m_rasterSelection.setSelectionBbox(strokesRect);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!m_freeDeformers.empty()) clearPointerContainer(m_freeDeformers);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_selectionFreeDeformer) {
delete m_selectionFreeDeformer;
m_selectionFreeDeformer = 0;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TTool::getApplication()->getCurrentTool()->notifyToolChanged();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterSelectionTool::doOnActivate() {
const TXshCell &imageCell = TTool::getImageCell();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TImageP image =
imageCell.getImage(false, 1); // => See the onImageChanged() warning !
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TToonzImageP ti = (TToonzImageP)image;
TRasterImageP ri = (TRasterImageP)image;
if (!ti && !ri) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_rasterSelection.makeCurrent();
m_rasterSelection.setCurrentImage(image, imageCell);
m_rasterSelection.selectNone();
m_noAntialiasing.setValue(NoAntialiasing);
m_rasterSelection.setNoAntialiasing(m_noAntialiasing.getValue());
TTool::getApplication()
->getCurrentTool()
->notifyToolChanged(); // Refreshes toolbar values
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterSelectionTool::doOnDeactivate() {
TTool::getApplication()->getCurrentSelection()->setSelection(0);
m_rasterSelection.setCurrentImage(0, TXshCell());
m_rasterSelection.selectNone();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterSelectionTool::onImageChanged() {
// ATTENTION: using getImage(false, 1) *works* here, but it's trickier than
// you could
// expect. It works because:
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// 1. Invoking getImage(true) after getImage(false, 1) will return the same
// image
// 2. The cached fullsampled image may be in the 'not modified' ImageManager
// state.
// Users could then alter the required image level subsampling - but doing
// so will
// immediately redirect here through 'onXshLevelChanged()' (thus resetting
// the subs back to 1).
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TImageP image = getImage(false, 1);
TToonzImageP ti = image;
TRasterImageP ri = image;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if ((!ti && !ri) || image != m_rasterSelection.getCurrentImage())
m_rasterSelection.selectNone();
TTool::getApplication()
->getCurrentTool()
->notifyToolChanged(); // Refreshes toolbar values
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterSelectionTool::transformFloatingSelection(
const TAffine &affine, const TPointD &center,
const DragSelectionTool::FourPoints &points) {
m_rasterSelection.setTransformation(affine);
if (isFloating()) {
setBBox(points);
setCenter(center);
}
invalidate();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterSelectionTool::increaseTransformationCount() {
if (m_rasterSelection.getTransformationCount() != m_transformationCount)
m_transformationCount = 0;
m_transformationCount++;
m_rasterSelection.setTransformationCount(m_transformationCount);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterSelectionTool::decreaseTransformationCount() {
m_transformationCount--;
m_rasterSelection.setTransformationCount(m_transformationCount);
if (m_rasterSelection.getTransformationCount() == 0)
m_rasterSelection.pasteFloatingSelection();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterSelectionTool::onActivate() {
if (m_firstTime) {
if (m_targetType & ToonzImage)
m_modifySavebox.setValue(ModifySavebox ? 1 : 0);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
SelectionTool::onActivate();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool RasterSelectionTool::onPropertyChanged(std::string propertyName) {
if (!m_rasterSelection.isEditable()) return false;
2018-06-26 17:47:11 +12:00
if (SelectionTool::onPropertyChanged(propertyName)) return true;
2016-06-15 18:43:10 +12:00
if (m_targetType & ToonzImage) {
ModifySavebox = (int)(m_modifySavebox.getValue());
invalidate();
}
if (propertyName == m_noAntialiasing.getName()) {
NoAntialiasing = m_noAntialiasing.getValue() ? 1 : 0;
m_rasterSelection.setNoAntialiasing(m_noAntialiasing.getValue());
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
return true;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RasterSelectionTool::updateTranslation() {
if (m_targetType & ToonzImage)
m_modifySavebox.setQStringName(tr("Modify Savebox"));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_noAntialiasing.setQStringName(tr("No Antialiasing"));
SelectionTool::updateTranslation();
2016-03-19 06:57:51 +13:00
}
//=============================================================================
RasterSelectionTool toonzRasterSelectionTool(TTool::ToonzImage);
RasterSelectionTool fullColorRasterSelectionTool(TTool::RasterImage);