tahoma2d/toonz/sources/tnztools/rastertapetool.cpp

1022 lines
35 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include "tools/tool.h"
#include "tundo.h"
#include "tproperty.h"
#include "tools/cursors.h"
#include "toonz/autoclose.h"
#include "ttoonzimage.h"
#include "toonz/toonzimageutils.h"
#include "tenv.h"
#include "tools/toolutils.h"
#include "toonz/txshsimplelevel.h"
#include "toonz/ttileset.h"
#include "toonz/levelproperties.h"
#include "toonz/stage2.h"
#include "tvectorimage.h"
#include "tstroke.h"
#include "drawutil.h"
#include "tinbetween.h"
2023-02-04 15:05:42 +13:00
#include "symmetrytool.h"
#include "vectorbrush.h"
#include "symmetrystroke.h"
2016-03-19 06:57:51 +13:00
#include "toonz/txsheethandle.h"
#include "toonz/tframehandle.h"
#include "toonz/tcolumnhandle.h"
#include "toonz/tpalettehandle.h"
#include "toonz/txshlevelhandle.h"
#include "tools/toolhandle.h"
#include "toonz/tonionskinmaskhandle.h"
2016-03-19 06:57:51 +13:00
// For Qt translation support
#include <QCoreApplication>
using namespace ToolUtils;
TEnv::StringVar AutocloseVectorType("InknpaintAutocloseVectorType", "Normal");
2020-10-02 17:30:20 +13:00
TEnv::DoubleVar AutocloseDistance("InknpaintAutocloseDistance", 20.0);
2016-03-19 06:57:51 +13:00
TEnv::DoubleVar AutocloseAngle("InknpaintAutocloseAngle", 60.0);
TEnv::IntVar AutocloseRange("InknpaintAutocloseRange", 0);
2020-10-02 17:30:20 +13:00
TEnv::IntVar AutocloseOpacity("InknpaintAutocloseOpacity", 180);
2016-03-19 06:57:51 +13:00
#define NORMAL_CLOSE L"Normal"
#define RECT_CLOSE L"Rectangular"
#define FREEHAND_CLOSE L"Freehand"
#define POLYLINE_CLOSE L"Polyline"
2016-06-15 18:43:10 +12:00
namespace {
2016-03-19 06:57:51 +13:00
//============================================================
2016-06-15 18:43:10 +12:00
class AutocloseParameters {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
int m_closingDistance, m_inkIndex, m_opacity;
double m_spotAngle;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
AutocloseParameters()
: m_closingDistance(0), m_inkIndex(0), m_spotAngle(0), m_opacity(1) {}
2016-03-19 06:57:51 +13:00
};
//============================================================
class RasterAutocloseUndo final : public TRasterUndo {
2016-06-15 18:43:10 +12:00
AutocloseParameters m_params;
std::vector<TAutocloser::Segment> m_segments;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
RasterAutocloseUndo(TTileSetCM32 *tileSet, const AutocloseParameters &params,
const std::vector<TAutocloser::Segment> &segments,
TXshSimpleLevel *level, const TFrameId &frameId)
: TRasterUndo(tileSet, level, frameId, false, false, 0)
, m_segments(segments)
, m_params(params) {}
//-------------------------------------------------------------------
2016-06-19 20:06:29 +12:00
void redo() const override {
2016-06-15 18:43:10 +12:00
TToonzImageP image = getImage();
if (!image) return;
TAutocloser ac(image->getRaster(), m_params.m_closingDistance,
m_params.m_spotAngle, m_params.m_inkIndex,
m_params.m_opacity);
ac.draw(m_segments);
ToolUtils::updateSaveBox();
/*-- Viewerを更新させるため --*/
TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
notifyImageChanged();
}
//-------------------------------------------------------------------
2016-06-20 14:23:05 +12:00
int getSize() const override {
return sizeof(*this) + TRasterUndo::getSize();
}
2016-06-15 18:43:10 +12:00
2016-06-19 20:06:29 +12:00
QString getToolName() override { return QString("Autoclose Tool"); }
int getHistoryType() override { return HistoryType::AutocloseTool; }
2016-03-19 06:57:51 +13:00
};
2016-06-15 18:43:10 +12:00
} // namespace
2016-03-19 06:57:51 +13:00
//============================================================
class RasterTapeTool final : public TTool {
2016-06-15 18:43:10 +12:00
Q_DECLARE_TR_FUNCTIONS(RasterTapeTool)
bool m_selecting;
TRectD m_selectingRect;
TRectD m_firstRect;
TPointD m_firstPoint;
bool m_firstFrameSelected;
TXshSimpleLevelP m_level;
// TBoolProperty m_isRect;
TEnumProperty m_closeType;
TDoubleProperty m_distance;
TDoubleProperty m_angle;
TStyleIndexProperty m_inkIndex;
TIntProperty m_opacity;
TPropertyGroup m_prop;
TBoolProperty m_multi;
TFrameId m_firstFrameId, m_veryFirstFrameId;
bool m_isXsheetCell;
std::pair<int, int> m_currCell;
// Aggiunte per disegnare il lazzo a la polyline
2023-02-04 15:05:42 +13:00
VectorBrush m_track;
2016-06-15 18:43:10 +12:00
TPointD m_firstPos;
TPointD m_mousePosition;
double m_thick;
TStroke *m_stroke;
2023-02-04 15:05:42 +13:00
std::vector<TStroke *>m_firstStrokes;
SymmetryStroke m_polyline;
2016-06-15 18:43:10 +12:00
bool m_firstTime;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
RasterTapeTool()
: TTool("T_Tape")
, m_closeType("Type:") // W_ToolOptions_CloseType
, m_distance("Distance:", 1, 100, 10) // W_ToolOptions_Distance
, m_angle("Angle:", 1, 180, 60) // W_ToolOptions_Angle
, m_inkIndex("Style Index:", L"current") // W_ToolOptions_InkIndex
, m_opacity("Opacity:", 1, 255, 255)
, m_multi("Frame Range", false) // W_ToolOptions_FrameRange
, m_selecting(false)
, m_selectingRect()
, m_firstRect()
, m_level(0)
, m_firstFrameSelected(false)
, m_isXsheetCell(false)
, m_currCell(-1, -1)
, m_firstPos()
, m_mousePosition()
, m_thick(0.5)
, m_stroke(0)
, m_firstTime(true) {
bind(TTool::ToonzImage);
m_prop.bind(m_closeType);
m_closeType.addValue(NORMAL_CLOSE);
m_closeType.addValue(RECT_CLOSE);
m_closeType.addValue(FREEHAND_CLOSE);
m_closeType.addValue(POLYLINE_CLOSE);
m_prop.bind(m_multi);
m_prop.bind(m_distance);
m_prop.bind(m_angle);
m_prop.bind(m_inkIndex);
m_prop.bind(m_opacity);
m_multi.setId("FrameRange");
m_closeType.setId("Type");
}
//------------------------------------------------------------
2016-06-19 20:06:29 +12:00
ToolType getToolType() const override { return TTool::LevelWriteTool; }
2016-06-15 18:43:10 +12:00
//------------------------------------------------------------
2016-06-19 20:06:29 +12:00
void updateTranslation() override {
2016-06-15 18:43:10 +12:00
m_closeType.setQStringName(tr("Type:"));
m_closeType.setItemUIName(NORMAL_CLOSE, tr("Normal"));
m_closeType.setItemUIName(RECT_CLOSE, tr("Rectangular"));
m_closeType.setItemUIName(FREEHAND_CLOSE, tr("Freehand"));
m_closeType.setItemUIName(POLYLINE_CLOSE, tr("Polyline"));
2016-06-15 18:43:10 +12:00
m_distance.setQStringName(tr("Distance:"));
m_inkIndex.setQStringName(tr("Style Index:"));
m_inkIndex.setValue(tr("current").toStdWString());
2016-06-15 18:43:10 +12:00
m_opacity.setQStringName(tr("Opacity:"));
m_multi.setQStringName(tr("Frame Range"));
m_angle.setQStringName(tr("Angle:"));
}
//------------------------------------------------------------
2016-06-19 20:06:29 +12:00
void leftButtonDrag(const TPointD &pos, const TMouseEvent &e) override {
2016-06-15 18:43:10 +12:00
if (m_closeType.getValue() == RECT_CLOSE) {
if (!m_selecting) return;
m_selectingRect.x1 = pos.x;
m_selectingRect.y1 = pos.y;
2023-02-04 15:05:42 +13:00
if (m_polyline.size() > 1 && m_polyline.hasSymmetryBrushes()) {
m_polyline.clear();
m_polyline.setRectangle(
TPointD(m_selectingRect.x0, m_selectingRect.y0),
TPointD(m_selectingRect.x1, m_selectingRect.y1));
}
2016-06-15 18:43:10 +12:00
invalidate();
} else if (m_closeType.getValue() == FREEHAND_CLOSE) {
freehandDrag(pos);
2017-11-14 16:38:01 +13:00
invalidate();
2016-06-15 18:43:10 +12:00
}
}
//------------------------------------------------------------
/*-- AutoClose Returns true if executed, false otherwise --*/
2023-02-04 15:05:42 +13:00
bool applyAutoclose(const TToonzImageP &ti, TFrameId id, const TRectD &selRect = TRectD(),
2016-06-15 18:43:10 +12:00
TStroke *stroke = 0) {
if (!ti) return false;
// inizializzo gli AutocloseParameters
AutocloseParameters params;
params.m_closingDistance = (int)(m_distance.getValue());
params.m_spotAngle = (int)(m_angle.getValue());
params.m_opacity = m_opacity.getValue();
std::string inkString = ::to_string(m_inkIndex.getValue());
int inkIndex =
TTool::getApplication()
->getCurrentLevelStyleIndex(); // TApp::instance()->getCurrentPalette()->getStyleIndex();
if (isInt(inkString)) inkIndex = std::stoi(inkString);
params.m_inkIndex = inkIndex;
TPoint delta;
TRasterCM32P ras, raux = ti->getRaster();
if (m_closeType.getValue() == RECT_CLOSE && raux && !selRect.isEmpty()) {
TRectD selArea = selRect;
if (selRect.x0 > selRect.x1) {
selArea.x1 = selRect.x0;
selArea.x0 = selRect.x1;
}
if (selRect.y0 > selRect.y1) {
selArea.y1 = selRect.y0;
selArea.y0 = selRect.y1;
}
TRect myRect(ToonzImageUtils::convertWorldToRaster(selArea, ti));
ras = raux->extract(myRect);
delta = myRect.getP00();
} else if ((m_closeType.getValue() == FREEHAND_CLOSE ||
2023-02-04 15:05:42 +13:00
m_closeType.getValue() == POLYLINE_CLOSE ||
m_closeType.getValue() == RECT_CLOSE) &&
2016-06-15 18:43:10 +12:00
stroke) {
TRectD selArea = stroke->getBBox();
TRect myRect(ToonzImageUtils::convertWorldToRaster(selArea, ti));
ras = raux->extract(myRect);
delta = myRect.getP00();
} else
ras = raux;
if (!ras) return false;
TAutocloser ac(ras, params.m_closingDistance, params.m_spotAngle,
params.m_inkIndex, params.m_opacity);
std::vector<TAutocloser::Segment> segments;
ac.compute(segments);
if ((m_closeType.getValue() == FREEHAND_CLOSE ||
m_closeType.getValue() == POLYLINE_CLOSE) &&
stroke)
checkSegments(segments, stroke, raux, delta);
std::vector<TAutocloser::Segment> segments2(segments);
/*-- segmentが取得できなければfalseを返す --*/
if (segments2.empty()) return false;
int i;
if (delta != TPoint(0, 0))
for (i = 0; i < (int)segments2.size(); i++) {
segments2[i].first += delta;
segments2[i].second += delta;
}
TTileSetCM32 *tileSet = new TTileSetCM32(raux->getSize());
for (i = 0; i < (int)segments2.size(); i++) {
TRect bbox(segments2[i].first, segments2[i].second);
bbox = bbox.enlarge(2);
tileSet->add(raux, bbox);
}
TXshSimpleLevel *sl =
TTool::getApplication()->getCurrentLevel()->getSimpleLevel();
TUndoManager::manager()->add(
new RasterAutocloseUndo(tileSet, params, segments2, sl, id));
ac.draw(segments);
ToolUtils::updateSaveBox();
return true;
}
//------------------------------------------------------------
inline TRectD interpolateRect(const TRectD &r1, const TRectD &r2, double t) {
return TRectD(r1.x0 + (r2.x0 - r1.x0) * t, r1.y0 + (r2.y0 - r1.y0) * t,
r1.x1 + (r2.x1 - r1.x1) * t, r1.y1 + (r2.y1 - r1.y1) * t);
}
//============================================================
2023-02-04 15:05:42 +13:00
void multiApplyAutoclose(
TFrameId firstFid, TFrameId lastFid, std::wstring closeType,
TRectD firstRect, TRectD lastRect,
std::vector<TStroke *> firstStrokes = std::vector<TStroke *>(),
std::vector<TStroke *> lastStrokes = std::vector<TStroke *>()) {
2016-06-15 18:43:10 +12:00
bool backward = false;
if (firstFid > lastFid) {
std::swap(firstFid, lastFid);
2016-06-15 18:43:10 +12:00
backward = true;
}
assert(firstFid <= lastFid);
std::vector<TFrameId> allFids;
m_level->getFids(allFids);
std::vector<TFrameId>::iterator i0 = allFids.begin();
while (i0 != allFids.end() && *i0 < firstFid) i0++;
if (i0 == allFids.end()) return;
std::vector<TFrameId>::iterator i1 = i0;
while (i1 != allFids.end() && *i1 <= lastFid) i1++;
assert(i0 < i1);
std::vector<TFrameId> fids(i0, i1);
int m = fids.size();
assert(m > 0);
TVectorImageP firstImage;
TVectorImageP lastImage;
2023-02-04 15:05:42 +13:00
if ((closeType == FREEHAND_CLOSE || closeType == POLYLINE_CLOSE) &&
firstStrokes.size() && lastStrokes.size()) {
2016-06-15 18:43:10 +12:00
firstImage = new TVectorImage();
lastImage = new TVectorImage();
2023-02-04 15:05:42 +13:00
for (int i = 0; i < firstStrokes.size(); i++)
firstImage->addStroke(firstStrokes[i]);
for (int i = 0; i < lastStrokes.size(); i++)
lastImage->addStroke(lastStrokes[i]);
2016-06-15 18:43:10 +12:00
}
TUndoManager::manager()->beginBlock();
for (int i = 0; i < m; ++i) {
TFrameId fid = fids[i];
TToonzImageP img = (TToonzImageP)m_level->getFrame(fid, true);
if (!img) continue;
double t = m > 1 ? (double)i / (double)(m - 1) : 0.5;
2023-02-04 15:05:42 +13:00
if (closeType == RECT_CLOSE)
applyAutoclose(img, fid, interpolateRect(firstRect, lastRect, t));
else if ((closeType == FREEHAND_CLOSE || closeType == POLYLINE_CLOSE) &&
firstStrokes.size() && lastStrokes.size())
doClose(t, fid, img, firstImage, lastImage);
2016-06-15 18:43:10 +12:00
m_level->getProperties()->setDirtyFlag(true);
}
TUndoManager::manager()->endBlock();
TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
// TNotifier::instance()->notify(TLevelChange());
// TNotifier::instance()->notify(TStageChange());
}
//----------------------------------------------------------------------
void multiApplyAutoclose(TFrameId firstFrameId, TFrameId lastFrameId) {
int r0 = firstFrameId.getNumber();
int r1 = lastFrameId.getNumber();
if (r0 > r1) {
std::swap(r0, r1);
std::swap(firstFrameId, lastFrameId);
2016-06-15 18:43:10 +12:00
}
if ((r1 - r0) < 2) return;
TUndoManager::manager()->beginBlock();
for (int i = r0; i <= r1; ++i) {
TFrameId fid(i);
TImageP img = m_level->getFrame(fid, true);
2023-02-04 15:05:42 +13:00
applyAutoclose(img, fid);
2016-06-15 18:43:10 +12:00
}
TUndoManager::manager()->endBlock();
TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
// TNotifier::instance()->notify(TLevelChange());
// TNotifier::instance()->notify(TStageChange());
}
//----------------------------------------------------------------------
2016-06-19 20:06:29 +12:00
void leftButtonUp(const TPointD &pos, const TMouseEvent &e) override {
2016-06-15 18:43:10 +12:00
TToonzImageP ti = TToonzImageP(getImage(true));
if (!ti) return;
/*-- Rectの座標の向きを揃える --*/
if (m_selectingRect.x0 > m_selectingRect.x1)
std::swap(m_selectingRect.x1, m_selectingRect.x0);
2016-06-15 18:43:10 +12:00
if (m_selectingRect.y0 > m_selectingRect.y1)
std::swap(m_selectingRect.y1, m_selectingRect.y0);
2016-06-15 18:43:10 +12:00
TTool::Application *app = TTool::getApplication();
m_selecting = false;
TRasterCM32P ras;
if (m_closeType.getValue() == RECT_CLOSE) {
2023-02-04 15:05:42 +13:00
if (m_polyline.size() > 1 && m_polyline.hasSymmetryBrushes()) {
// We'll use polyline
m_polyline.clear();
m_polyline.setRectangle(
TPointD(m_selectingRect.x0, m_selectingRect.y0),
TPointD(m_selectingRect.x1, m_selectingRect.y1));
}
2016-06-15 18:43:10 +12:00
if (m_multi.getValue()) {
if (m_firstFrameSelected) {
2023-02-04 15:05:42 +13:00
if (m_polyline.size() > 1 && m_polyline.hasSymmetryBrushes()) {
// We'll use polyline
TFrameId tmp = getCurrentFid();
std::vector<TStroke *> lastStrokes;
for (int i = 0; i < m_polyline.getBrushCount(); i++)
lastStrokes.push_back(m_polyline.makeRectangleStroke(i));
multiAutocloseRegion(lastStrokes, POLYLINE_CLOSE, e);
} else {
multiApplyAutoclose(m_firstFrameId, getFrameId(), RECT_CLOSE,
m_firstRect, m_selectingRect);
}
2016-06-15 18:43:10 +12:00
invalidate(m_selectingRect.enlarge(2));
if (e.isShiftPressed()) {
m_firstRect = m_selectingRect;
m_firstFrameId = getFrameId();
2023-02-04 15:05:42 +13:00
m_firstStrokes.clear();
if (m_polyline.size() > 1) {
for (int i = 0; i < m_polyline.getBrushCount(); i++)
m_firstStrokes.push_back(m_polyline.makeRectangleStroke(i));
}
2016-06-15 18:43:10 +12:00
} else {
if (m_isXsheetCell) {
app->getCurrentColumn()->setColumnIndex(m_currCell.first);
app->getCurrentFrame()->setFrame(m_currCell.second);
} else
app->getCurrentFrame()->setFid(m_veryFirstFrameId);
resetMulti();
2023-02-04 15:05:42 +13:00
m_polyline.reset();
2016-06-15 18:43:10 +12:00
}
} else {
2023-02-04 15:05:42 +13:00
m_firstStrokes.clear();
if (m_polyline.size() > 1) {
for (int i = 0; i < m_polyline.getBrushCount(); i++)
m_firstStrokes.push_back(m_polyline.makeRectangleStroke(i));
}
2016-06-15 18:43:10 +12:00
m_isXsheetCell = app->getCurrentFrame()->isEditingScene();
// if (m_isXsheetCell)
m_currCell = std::pair<int, int>(getColumnIndex(), this->getFrame());
}
return;
2023-02-04 15:05:42 +13:00
} else if (m_polyline.size() > 1 && m_polyline.hasSymmetryBrushes()) {
TUndoManager::manager()->beginBlock();
2016-06-15 18:43:10 +12:00
2023-02-04 15:05:42 +13:00
TStroke *stroke = m_polyline.makeRectangleStroke();
TFrameId fid = getFrameId();
applyAutoclose(ti, fid, TRectD(), stroke);
for (int i = 1; i < m_polyline.getBrushCount(); i++) {
TStroke *symmStroke = m_polyline.makeRectangleStroke(i);
applyAutoclose(ti, fid, TRectD(), symmStroke);
2016-06-15 18:43:10 +12:00
}
2023-02-04 15:05:42 +13:00
TUndoManager::manager()->endBlock();
2016-06-15 18:43:10 +12:00
invalidate();
2023-02-04 15:05:42 +13:00
} else {
/*-- AutoCloseが実行されたか判定する --*/
TFrameId fid = getFrameId();
if (!applyAutoclose(ti, fid, m_selectingRect)) {
if (m_stroke) {
delete m_stroke;
m_stroke = 0;
}
invalidate();
return;
}
2016-06-15 18:43:10 +12:00
}
2023-02-04 15:05:42 +13:00
m_selectingRect.empty();
m_polyline.reset();
2016-06-15 18:43:10 +12:00
invalidate();
notifyImageChanged();
} else if (m_closeType.getValue() == FREEHAND_CLOSE) {
closeFreehand(pos);
2023-02-04 15:05:42 +13:00
double error = (30.0 / 11) * sqrt(getPixelSize() * getPixelSize());
if (m_multi.getValue()) {
TFrameId tmp = getFrameId();
if (m_firstStrokes.size() && m_stroke) {
std::vector<TStroke *> lastStrokes;
for (int i = 0; i < m_track.getBrushCount(); i++)
lastStrokes.push_back(m_track.makeStroke(error, i));
multiAutocloseRegion(lastStrokes, m_closeType.getValue(), e);
} else {
m_firstStrokes.clear();
multiAutocloseRegion(m_firstStrokes, m_closeType.getValue(), e);
}
} else {
if (m_track.hasSymmetryBrushes()) TUndoManager::manager()->beginBlock();
TFrameId fid = getFrameId();
applyAutoclose(ti, fid, TRectD(), m_stroke);
if (m_track.hasSymmetryBrushes()) {
std::vector<TStroke *> symmStrokes =
m_track.makeSymmetryStrokes(error);
for (int i = 0; i < symmStrokes.size(); i++) {
applyAutoclose(ti, fid, TRectD(), symmStrokes[i]);
}
TUndoManager::manager()->endBlock();
}
}
m_track.reset();
2016-06-15 18:43:10 +12:00
invalidate();
}
if (m_stroke) {
delete m_stroke;
m_stroke = 0;
}
}
//------------------------------------------------------------
2016-06-19 20:06:29 +12:00
void draw() override {
2016-06-15 18:43:10 +12:00
double pixelSize2 = getPixelSize() * getPixelSize();
m_thick = sqrt(pixelSize2) / 2.0;
2023-02-04 15:05:42 +13:00
// TPixel color = ToonzCheck::instance()->getChecks() & ToonzCheck::eBlackBg
// ? TPixel32::White
// : TPixel32::Black;
TPixel color = TPixel32::Red;
2016-06-15 18:43:10 +12:00
if (m_closeType.getValue() == RECT_CLOSE) {
2023-02-04 15:05:42 +13:00
if (m_multi.getValue() && m_firstFrameSelected) {
if (m_firstStrokes.size()) {
tglColor(color);
for (int i = 0; i < m_firstStrokes.size(); i++)
drawStrokeCenterline(*m_firstStrokes[i], 1);
} else
drawRect(m_firstRect, color, 0x3F33, true);
}
if (m_selecting || (m_multi.getValue() && !m_firstFrameSelected)) {
if (m_polyline.size() > 1) {
glPushMatrix();
m_polyline.drawRectangle(color);
glPopMatrix();
} else
drawRect(m_selectingRect, color, 0xFFFF, true);
}
2016-06-15 18:43:10 +12:00
}
if ((m_closeType.getValue() == FREEHAND_CLOSE ||
m_closeType.getValue() == POLYLINE_CLOSE) &&
2023-02-04 15:05:42 +13:00
m_multi.getValue()) {
2016-06-15 18:43:10 +12:00
tglColor(color);
2023-02-04 15:05:42 +13:00
for (int i = 0; i < m_firstStrokes.size(); i++)
drawStrokeCenterline(*m_firstStrokes[i], 1);
2016-06-15 18:43:10 +12:00
}
if (m_closeType.getValue() == POLYLINE_CLOSE && !m_polyline.empty()) {
2023-02-04 15:05:42 +13:00
m_polyline.drawPolyline(m_mousePosition, color);
2017-11-14 16:38:01 +13:00
} else if (m_closeType.getValue() == FREEHAND_CLOSE && !m_track.isEmpty()) {
tglColor(color);
2023-02-04 15:05:42 +13:00
glPushMatrix();
2017-11-14 16:38:01 +13:00
m_track.drawAllFragments();
2023-02-04 15:05:42 +13:00
glPopMatrix();
} else if (m_multi.getValue() && m_firstFrameSelected) {
2016-06-15 18:43:10 +12:00
drawCross(m_firstPoint, 5);
2023-02-04 15:05:42 +13:00
SymmetryTool *symmetryTool = dynamic_cast<SymmetryTool *>(
TTool::getTool("T_Symmetry", TTool::RasterImage));
if (symmetryTool && symmetryTool->isGuideEnabled()) {
TImageP image = getImage(false);
TToonzImageP ti = image;
TPointD dpiScale = getViewer()->getDpiScale();
2023-02-04 18:35:30 +13:00
TRasterCM32P ras = ti ? ti->getRaster() : TRasterCM32P();
2023-02-04 15:05:42 +13:00
TPointD rasCenter = ti ? ras->getCenterD() : TPointD(0, 0);
TPointD fillPt = m_firstPoint + rasCenter;
std::vector<TPointD> symmPts =
symmetryTool->getSymmetryPoints(fillPt, rasCenter, dpiScale);
for (int i = 1; i < symmPts.size(); i++) {
drawCross(symmPts[i] - rasCenter, 5);
}
}
}
2016-06-15 18:43:10 +12:00
}
//------------------------------------------------------------
2016-06-19 20:06:29 +12:00
bool onPropertyChanged(std::string propertyName) override {
2016-06-15 18:43:10 +12:00
if (propertyName == m_closeType.getName()) {
AutocloseVectorType = ::to_string(m_closeType.getValue());
resetMulti();
}
else if (propertyName == m_distance.getName()) {
AutocloseDistance = m_distance.getValue();
TTool::Application *app = TTool::getApplication();
// This is a hack to get the viewer to update with the distance.
app->getCurrentOnionSkin()->notifyOnionSkinMaskChanged();
}
2016-06-15 18:43:10 +12:00
else if (propertyName == m_angle.getName())
AutocloseAngle = m_angle.getValue();
else if (propertyName == m_inkIndex.getName()) {
}
else if (propertyName == m_opacity.getName())
AutocloseOpacity = m_opacity.getValue();
else if (propertyName == m_multi.getName()) {
AutocloseRange = (int)((m_multi.getValue()));
resetMulti();
}
if (ToonzCheck::instance()->getChecks() & ToonzCheck::eAutoclose)
notifyImageChanged();
return true;
}
//----------------------------------------------------------------------
void resetMulti() {
m_firstFrameSelected = false;
m_firstRect.empty();
m_firstPoint = TPointD();
m_selectingRect.empty();
TTool::Application *app = TTool::getApplication();
m_level = app->getCurrentLevel()->getLevel()
? app->getCurrentLevel()->getSimpleLevel()
: 0;
m_firstFrameId = m_veryFirstFrameId = getFrameId();
2023-02-04 15:05:42 +13:00
m_firstStrokes.clear();
2016-06-15 18:43:10 +12:00
}
//----------------------------------------------------------------------
2016-06-19 20:06:29 +12:00
void onImageChanged() override {
2016-06-15 18:43:10 +12:00
if (!m_multi.getValue()) return;
TTool::Application *app = TTool::getApplication();
TXshSimpleLevel *xshl = 0;
if (app->getCurrentLevel()->getLevel())
xshl = app->getCurrentLevel()->getSimpleLevel();
if (!xshl || m_level.getPointer() != xshl ||
(m_closeType.getValue() == RECT_CLOSE && m_selectingRect.isEmpty()) ||
((m_closeType.getValue() == FREEHAND_CLOSE ||
m_closeType.getValue() == POLYLINE_CLOSE) &&
2023-02-04 15:05:42 +13:00
!m_firstStrokes.size()))
2016-06-15 18:43:10 +12:00
resetMulti();
else if (m_firstFrameId == getFrameId())
m_firstFrameSelected = false; // nel caso sono passato allo stato 1 e
// torno all'immagine iniziale, torno allo
// stato iniziale
2016-06-20 14:23:05 +12:00
else { // cambio stato.
2016-06-15 18:43:10 +12:00
m_firstFrameSelected = true;
if (m_closeType.getValue() == RECT_CLOSE) {
assert(!m_selectingRect.isEmpty());
m_firstRect = m_selectingRect;
}
}
}
//----------------------------------------------------------------------
2016-06-19 20:06:29 +12:00
void leftButtonDown(const TPointD &pos, const TMouseEvent &) override {
2016-06-15 18:43:10 +12:00
TToonzImageP ti = TToonzImageP(getImage(true));
if (!ti) return;
2023-02-04 15:05:42 +13:00
SymmetryTool *symmetryTool = dynamic_cast<SymmetryTool *>(
TTool::getTool("T_Symmetry", TTool::RasterImage));
TPointD dpiScale = getViewer()->getDpiScale();
SymmetryObject symmObj = symmetryTool->getSymmetryObject();
2016-06-15 18:43:10 +12:00
if (m_closeType.getValue() == RECT_CLOSE) {
m_selecting = true;
m_selectingRect.x0 = pos.x;
m_selectingRect.y0 = pos.y;
m_selectingRect.x1 = pos.x + 1;
m_selectingRect.y1 = pos.y + 1;
2023-02-04 15:05:42 +13:00
if (symmetryTool && symmetryTool->isGuideEnabled()) {
// We'll use polyline
m_polyline.reset();
m_polyline.addSymmetryBrushes(symmObj.getLines(), symmObj.getRotation(),
symmObj.getCenterPoint(),
symmObj.isUsingLineSymmetry(), dpiScale);
m_polyline.setRectangle(
TPointD(m_selectingRect.x0, m_selectingRect.y0),
TPointD(m_selectingRect.x1, m_selectingRect.y1));
}
2016-06-15 18:43:10 +12:00
return;
} else if (m_closeType.getValue() == FREEHAND_CLOSE) {
startFreehand(pos);
return;
} else if (m_closeType.getValue() == POLYLINE_CLOSE) {
2023-02-04 15:05:42 +13:00
if (symmetryTool && symmetryTool->isGuideEnabled() &&
!m_polyline.hasSymmetryBrushes()) {
m_polyline.addSymmetryBrushes(symmObj.getLines(), symmObj.getRotation(),
symmObj.getCenterPoint(),
symmObj.isUsingLineSymmetry(), dpiScale);
}
2016-06-15 18:43:10 +12:00
addPointPolyline(pos);
return;
} else if (m_closeType.getValue() == NORMAL_CLOSE) {
if (m_multi.getValue()) {
TTool::Application *app = TTool::getApplication();
if (m_firstFrameSelected) {
multiApplyAutoclose(m_firstFrameId, getFrameId());
invalidate();
if (m_isXsheetCell) {
app->getCurrentColumn()->setColumnIndex(m_currCell.first);
app->getCurrentFrame()->setFrame(m_currCell.second);
} else
app->getCurrentFrame()->setFid(m_veryFirstFrameId);
resetMulti();
} else {
m_isXsheetCell = app->getCurrentFrame()->isEditingScene();
// if (m_isXsheetCell)
m_currCell = std::pair<int, int>(getColumnIndex(), getFrame());
m_firstFrameSelected = true;
m_firstPoint = pos;
}
invalidate();
return;
}
m_selecting = false;
2023-02-04 15:05:42 +13:00
TFrameId fid = getFrameId();
applyAutoclose(ti, fid);
2016-06-15 18:43:10 +12:00
invalidate();
notifyImageChanged();
}
}
//----------------------------------------------------------------------
2016-06-20 14:23:05 +12:00
void leftButtonDoubleClick(const TPointD &pos,
const TMouseEvent &e) override {
2016-06-15 18:43:10 +12:00
TToonzImageP ti = TToonzImageP(getImage(true));
if (m_closeType.getValue() == POLYLINE_CLOSE && ti) {
closePolyline(pos);
2023-02-04 15:05:42 +13:00
m_stroke = m_polyline.makePolylineStroke();
2016-06-15 18:43:10 +12:00
assert(m_stroke->getPoint(0) == m_stroke->getPoint(1));
2023-02-04 15:05:42 +13:00
if (m_multi.getValue()) {
if (m_firstStrokes.size()) {
std::vector<TStroke *> lastStrokes;
for (int i = 0; i < m_polyline.getBrushCount(); i++)
lastStrokes.push_back(m_polyline.makePolylineStroke(i));
multiAutocloseRegion(lastStrokes, m_closeType.getValue(), e);
} else {
m_firstStrokes.clear();
multiAutocloseRegion(m_firstStrokes, m_closeType.getValue(), e);
}
} else {
if (m_polyline.hasSymmetryBrushes())
TUndoManager::manager()->beginBlock();
TFrameId fid = getFrameId();
applyAutoclose(ti, fid, TRectD(), m_stroke);
if (m_polyline.hasSymmetryBrushes()) {
for (int i = 1; i < m_polyline.getBrushCount(); i++) {
TStroke *symmStroke = m_polyline.makePolylineStroke(i);
applyAutoclose(ti, fid, TRectD(), symmStroke);
}
TUndoManager::manager()->endBlock();
}
}
2016-06-15 18:43:10 +12:00
invalidate();
2023-02-04 15:05:42 +13:00
m_polyline.reset();
2016-06-15 18:43:10 +12:00
}
if (m_stroke) {
delete m_stroke;
m_stroke = 0;
}
}
//----------------------------------------------------------------------
2016-06-19 20:06:29 +12:00
void mouseMove(const TPointD &pos, const TMouseEvent &e) override {
2016-06-15 18:43:10 +12:00
if (m_closeType.getValue() == POLYLINE_CLOSE) {
m_mousePosition = pos;
invalidate();
}
}
//----------------------------------------------------------------------
2016-06-19 20:06:29 +12:00
void onEnter() override {
2016-06-15 18:43:10 +12:00
// getApplication()->editImage();
}
//----------------------------------------------------------------------
2016-06-19 20:06:29 +12:00
TPropertyGroup *getProperties(int targetType) override { return &m_prop; }
2016-06-15 18:43:10 +12:00
//----------------------------------------------------------------------
2016-06-19 20:06:29 +12:00
void onActivate() override {
2016-06-15 18:43:10 +12:00
if (m_firstTime) {
m_closeType.setValue(::to_wstring(AutocloseVectorType.getValue()));
m_distance.setValue(AutocloseDistance);
m_angle.setValue(AutocloseAngle);
m_opacity.setValue(AutocloseOpacity);
m_multi.setValue(AutocloseRange ? 1 : 0);
m_firstTime = false;
}
// getApplication()->editImage();
resetMulti();
}
//----------------------------------------------------------------------
2016-06-19 20:06:29 +12:00
void onDeactivate() override {}
2016-06-15 18:43:10 +12:00
//----------------------------------------------------------------------
2016-06-19 20:06:29 +12:00
int getCursorId() const override {
2017-10-26 19:17:19 +13:00
int ret = ToolCursor::TapeCursor;
if (m_closeType.getValue() == FREEHAND_CLOSE)
ret = ret | ToolCursor::Ex_FreeHand;
else if (m_closeType.getValue() == POLYLINE_CLOSE)
ret = ret | ToolCursor::Ex_PolyLine;
else if (m_closeType.getValue() == RECT_CLOSE)
ret = ret | ToolCursor::Ex_Rectangle;
2016-06-15 18:43:10 +12:00
if (ToonzCheck::instance()->getChecks() & ToonzCheck::eBlackBg)
2017-10-26 19:17:19 +13:00
ret = ret | ToolCursor::Ex_Negate;
return ret;
2016-06-15 18:43:10 +12:00
}
//----------------------------------------------------------------------
//! Viene aggiunto \b pos a \b m_track e disegnato il primo pezzetto del
//! lazzo. Viene inizializzato \b m_firstPos
void startFreehand(const TPointD &pos) {
2023-02-04 15:05:42 +13:00
m_track.reset();
SymmetryTool *symmetryTool = dynamic_cast<SymmetryTool *>(
TTool::getTool("T_Symmetry", TTool::RasterImage));
TPointD dpiScale = getViewer()->getDpiScale();
SymmetryObject symmObj = symmetryTool->getSymmetryObject();
if (symmetryTool && symmetryTool->isGuideEnabled()) {
m_track.addSymmetryBrushes(symmObj.getLines(), symmObj.getRotation(),
symmObj.getCenterPoint(),
symmObj.isUsingLineSymmetry(), dpiScale);
}
2016-06-15 18:43:10 +12:00
m_firstPos = pos;
double pixelSize2 = getPixelSize() * getPixelSize();
m_track.add(TThickPoint(pos, m_thick), pixelSize2);
}
//------------------------------------------------------------------
//! Viene aggiunto \b pos a \b m_track e disegnato un altro pezzetto del
//! lazzo.
void freehandDrag(const TPointD &pos) {
double pixelSize2 = getPixelSize() * getPixelSize();
m_track.add(TThickPoint(pos, m_thick), pixelSize2);
}
//------------------------------------------------------------------
//! Viene chiuso il lazzo (si aggiunge l'ultimo punto ad m_track) e viene
//! creato lo stroke rappresentante il lazzo.
void closeFreehand(const TPointD &pos) {
if (m_track.isEmpty()) return;
double pixelSize2 = getPixelSize() * getPixelSize();
m_track.add(TThickPoint(m_firstPos, m_thick), pixelSize2);
m_track.filterPoints();
double error = (30.0 / 11) * sqrt(pixelSize2);
m_stroke = m_track.makeStroke(error);
m_stroke->setStyle(1);
}
//------------------------------------------------------------------
//! Viene aggiunto un punto al vettore m_polyline.
void addPointPolyline(const TPointD &pos) {
m_firstPos = pos;
m_polyline.push_back(pos);
}
//------------------------------------------------------------------
//! Agginge l'ultimo pos a \b m_polyline e chiude la spezzata (aggiunge \b
//! m_polyline.front() alla fine del vettore)
void closePolyline(const TPointD &pos) {
if (m_polyline.size() <= 1) return;
if (m_polyline.back() != pos) m_polyline.push_back(pos);
if (m_polyline.back() != m_polyline.front())
m_polyline.push_back(m_polyline.front());
invalidate();
}
//-------------------------------------------------------------------
//! Elimina i segmenti che non sono contenuti all'interno dello stroke!!!
void checkSegments(std::vector<TAutocloser::Segment> &segments,
TStroke *stroke, const TRasterCM32P &ras,
const TPoint &delta) {
TVectorImage vi;
TStroke *app = new TStroke();
*app = *stroke;
app->transform(TTranslation(convert(ras->getCenter())));
vi.addStroke(app);
vi.findRegions();
std::vector<TAutocloser::Segment>::iterator it = segments.begin();
for (; it < segments.end(); it++) {
if (it == segments.end()) break;
int i;
bool isContained = false;
for (i = 0; i < (int)vi.getRegionCount(); i++) {
TRegion *reg = vi.getRegion(i);
if (reg->contains(convert(it->first + delta)) &&
reg->contains(convert(it->second + delta))) {
isContained = true;
break;
}
}
if (!isContained) {
it = segments.erase(it);
if (it != segments.end() && it != segments.begin())
it--;
else if (it == segments.end())
break;
}
}
}
//-------------------------------------------------------------------
2023-02-04 15:05:42 +13:00
void multiAutocloseRegion(std::vector<TStroke *> laststrokes,
const std::wstring eraseType,
const TMouseEvent &e) {
2016-06-15 18:43:10 +12:00
TTool::Application *app = TTool::getApplication();
2023-02-04 15:05:42 +13:00
if (m_firstStrokes.size()) {
multiApplyAutoclose(m_firstFrameId, getFrameId(), eraseType, TRectD(),
TRectD(), m_firstStrokes, laststrokes);
2016-06-15 18:43:10 +12:00
invalidate();
if (e.isShiftPressed()) {
2023-02-04 15:05:42 +13:00
m_firstStrokes.clear();
if (eraseType == POLYLINE_CLOSE) {
if (m_polyline.size() > 1) {
for (int i = 0; i < m_polyline.getBrushCount(); i++)
m_firstStrokes.push_back(m_polyline.makeRectangleStroke(i));
}
} else {
double error = (30.0 / 11) * sqrt(getPixelSize() * getPixelSize());
for (int i = 0; i < m_track.getBrushCount(); i++)
m_firstStrokes.push_back(m_track.makeStroke(error, i));
}
2016-06-15 18:43:10 +12:00
m_firstFrameId = getFrameId();
} else {
if (m_isXsheetCell) {
app->getCurrentColumn()->setColumnIndex(m_currCell.first);
app->getCurrentFrame()->setFrame(m_currCell.second);
} else
app->getCurrentFrame()->setFid(m_veryFirstFrameId);
resetMulti();
}
} else {
m_isXsheetCell = app->getCurrentFrame()->isEditingScene();
// if (m_isXsheetCell)
m_currCell = std::pair<int, int>(getColumnIndex(), getFrame());
2023-02-04 15:05:42 +13:00
m_firstStrokes.clear();
if (eraseType == POLYLINE_CLOSE) {
if (m_polyline.size() > 1) {
for (int i = 0; i < m_polyline.getBrushCount(); i++)
m_firstStrokes.push_back(m_polyline.makeRectangleStroke(i));
}
} else {
double error = (30.0 / 11) * sqrt(getPixelSize() * getPixelSize());
for (int i = 0; i < m_track.getBrushCount(); i++)
m_firstStrokes.push_back(m_track.makeStroke(error, i));
}
2016-06-15 18:43:10 +12:00
}
return;
}
//------------------------------------------------------------------------
2023-02-04 15:05:42 +13:00
void doClose(double t, TFrameId fid, const TImageP &img, const TVectorImageP &firstImage,
2016-06-15 18:43:10 +12:00
const TVectorImageP &lastImage) {
if (t == 0)
2023-02-04 15:05:42 +13:00
for (int i = 0; i < firstImage->getStrokeCount(); i++)
applyAutoclose(img, fid, TRectD(), firstImage->getStroke(i));
2016-06-15 18:43:10 +12:00
else if (t == 1)
2023-02-04 15:05:42 +13:00
for (int i = 0; i < lastImage->getStrokeCount(); i++)
applyAutoclose(img, fid, TRectD(), lastImage->getStroke(i));
2016-06-15 18:43:10 +12:00
else {
2023-02-04 15:05:42 +13:00
// assert(firstImage->getStrokeCount() == 1);
// assert(lastImage->getStrokeCount() == 1);
2016-06-15 18:43:10 +12:00
TVectorImageP vi = TInbetween(firstImage, lastImage).tween(t);
2023-02-04 15:05:42 +13:00
// assert(vi->getStrokeCount() == 1);
for (int i = 0; i < vi->getStrokeCount(); i++)
applyAutoclose(img, fid, TRectD(), vi->getStroke(i));
2016-06-15 18:43:10 +12:00
}
}
//-------------------------------------------------------------------
2016-03-19 06:57:51 +13:00
} rasterTapeTool;