Skip to content

OpenToonz - An open-source full-featured 2D animation creation software

C

Implementation of Vector Brush Draw Order

Abel Youb2cb06e19e61
diff --git a/toonz/sources/common/tvectorimage/tvectorimage.cpp b/toonz/sources/common/tvectorimage/tvectorimage.cpp
index 75c99042c..ab8da1299 100644
--- a/toonz/sources/common/tvectorimage/tvectorimage.cpp
+++ b/toonz/sources/common/tvectorimage/tvectorimage.cpp
@@ -17,6 +17,7 @@
#include "tcomputeregions.h"
#include <memory>
+#include <QSet>
//=============================================================================
typedef TVectorImage::IntersectionBranch IntersectionBranch;
@@ -147,7 +148,7 @@ int TVectorImage::addStrokeToGroup(TStroke *stroke, int strokeIndex) {
//-----------------------------------------------------------------------------
-int TVectorImage::addStroke(TStroke *stroke, bool discardPoints) {
+int TVectorImage::addStroke(TStroke *stroke, bool discardPoints, bool underAll) {
if (discardPoints) {
TRectD bBox = stroke->getBBox();
if (bBox.x0 == bBox.x1 && bBox.y0 == bBox.y1) // empty stroke: discard
@@ -158,9 +159,19 @@ int TVectorImage::addStroke(TStroke *stroke, bool discardPoints) {
int i;
for (i = m_imp->m_strokes.size() - 1; i >= 0; i--)
if (m_imp->m_insideGroup.isParentOf(m_imp->m_strokes[i]->m_groupId)) {
- m_imp->insertStrokeAt(
- new VIStroke(stroke, m_imp->m_strokes[i]->m_groupId), i + 1);
- return i + 1;
+ if (underAll) {
+ int j = 0;
+ for (j = i - 1; j >= 0 && m_imp->m_insideGroup.isParentOf(
+ m_imp->m_strokes[j]->m_groupId);
+ j--);
+ m_imp->insertStrokeAt(
+ new VIStroke(stroke, m_imp->m_strokes[j + 1]->m_groupId), j + 1);
+ return j + 1;
+ } else {
+ m_imp->insertStrokeAt(
+ new VIStroke(stroke, m_imp->m_strokes[i]->m_groupId), i + 1);
+ return i + 1;
+ }
}
}
@@ -170,10 +181,59 @@ int TVectorImage::addStroke(TStroke *stroke, bool discardPoints) {
gid = TGroupId(this, true);
else
gid = m_imp->m_strokes.back()->m_groupId;
+
+ if (underAll) {
+ m_imp->insertStrokeAt(new VIStroke(stroke, gid), 0);
+ m_imp->m_areValidRegions = false;
+ return 0;
+ } else {
+ m_imp->m_areValidRegions = false;
+ m_imp->m_strokes.push_back(new VIStroke(stroke, gid));
+ return m_imp->m_strokes.size() - 1;
+ }
+}
+
+int TVectorImage::addStrokeBelow(TStroke *stroke, const QSet<int> &aboveStyles,
+ bool discardPoints) {
+ if (aboveStyles.isEmpty())
+ return addStroke(stroke, discardPoints);
+ if (discardPoints) {
+ TRectD bBox = stroke->getBBox();
+ if (bBox.x0 == bBox.x1 && bBox.y0 == bBox.y1) // empty stroke: discard
+ return -1;
+ }
+
+ if (m_imp->m_insideGroup != TGroupId()) {
+ int i;
+ for (i = m_imp->m_strokes.size() - 1; i >= 0; i--)
+ if (m_imp->m_insideGroup.isParentOf(m_imp->m_strokes[i]->m_groupId)) {
+ int j = 0;
+ for (j = i - 1;
+ j >= 0 &&
+ m_imp->m_insideGroup.isParentOf(m_imp->m_strokes[j]->m_groupId) &&
+ aboveStyles.contains(m_imp->m_strokes[j]->m_s->getStyle());
+ j--);
+ m_imp->insertStrokeAt(
+ new VIStroke(stroke, m_imp->m_strokes[j + 1]->m_groupId), j + 1);
+ return j + 1;
+ }
+ }
- m_imp->m_strokes.push_back(new VIStroke(stroke, gid));
+ TGroupId gid;
+ if (m_imp->m_strokes.empty() ||
+ m_imp->m_strokes.back()->m_groupId.isGrouped() != 0)
+ gid = TGroupId(this, true);
+ else
+ gid = m_imp->m_strokes.back()->m_groupId;
+
+ int i;
+ for (i = m_imp->m_strokes.size() - 1;
+ i >= 0 && !isStrokeGrouped(i) &&
+ aboveStyles.contains(m_imp->m_strokes[i]->m_s->getStyle());
+ i--);
+ m_imp->insertStrokeAt(new VIStroke(stroke, gid), i + 1);
m_imp->m_areValidRegions = false;
- return m_imp->m_strokes.size() - 1;
+ return i + 1;
}
//-----------------------------------------------------------------------------
diff --git a/toonz/sources/include/tvectorimage.h b/toonz/sources/include/tvectorimage.h
index 53416e506..dd0215315 100644
--- a/toonz/sources/include/tvectorimage.h
+++ b/toonz/sources/include/tvectorimage.h
@@ -225,7 +225,8 @@ get the stroke nearest at point
//! Add a stroke at the end of the vector; returns position of the stroke (or
//! -1 if not added)
- int addStroke(TStroke *, bool discardPoints = true);
+ int addStroke(TStroke *, bool discardPoints = true, bool underAll = false);
+ int addStrokeBelow(TStroke *, const QSet<int> &aboveStyles, bool discardPoints = true);
int addStrokeToGroup(TStroke *stroke, int strokeIndex);
//! Replace the stroke at index \b index with \b newStroke
void replaceStroke(int index, TStroke *newStroke);
diff --git a/toonz/sources/tnztools/toonzvectorbrushtool.cpp b/toonz/sources/tnztools/toonzvectorbrushtool.cpp
index 34d41438b..5041e568a 100644
--- a/toonz/sources/tnztools/toonzvectorbrushtool.cpp
+++ b/toonz/sources/tnztools/toonzvectorbrushtool.cpp
@@ -54,7 +54,7 @@ TEnv::IntVar V_VectorJoinStyle("InknpaintVectorJoinStyle", 1);
TEnv::IntVar V_VectorMiterValue("InknpaintVectorMiterValue", 4);
TEnv::DoubleVar V_BrushAccuracy("InknpaintBrushAccuracy", 20);
TEnv::DoubleVar V_BrushSmooth("InknpaintBrushSmooth", 0);
-TEnv::IntVar V_BrushDrawOrder("InknpaintBrushDrawOrder", 0);
+TEnv::IntVar V_BrushDrawOrder("InknpaintVectorDrawOrder", 0);
TEnv::IntVar V_BrushBreakSharpAngles("InknpaintBrushBreakSharpAngles", 0);
TEnv::IntVar V_BrushPressureSensitivity("InknpaintBrushPressureSensitivity", 1);
TEnv::IntVar V_VectorBrushFrameRange("VectorBrushFrameRange", 0);
@@ -338,8 +338,26 @@ static void findMaxCurvPoints(TStroke *stroke, const float &angoloLim,
}
}
+namespace {
+
+enum DrawOrder { OverAll = 0, UnderAll, PaletteOrder };
+
+void getAboveStyleIdSet(int styleId, TPaletteP palette,
+ QSet<int> &aboveStyles) {
+ if (!palette) return;
+ for (int p = 0; p < palette->getPageCount(); p++) {
+ TPalette::Page *page = palette->getPage(p);
+ for (int s = 0; s < page->getStyleCount(); s++) {
+ int tmpId = page->getStyleId(s);
+ if (tmpId == styleId) return;
+ if (tmpId != 0) aboveStyles.insert(tmpId);
+ }
+ }
+}
+} // namespace
+
static void addStroke(TTool::Application *application, const TVectorImageP &vi,
- TStroke *stroke, bool breakAngles, bool autoGroup,
+ TStroke *stroke, DrawOrder drawOrder, bool breakAngles, bool autoGroup,
bool autoFill, bool frameCreated, bool levelCreated,
TXshSimpleLevel *sLevel = NULL,
TFrameId fid = TFrameId::NO_FRAME) {
@@ -383,8 +401,20 @@ static void addStroke(TTool::Application *application, const TVectorImageP &vi,
ImageUtils::getFillingInformationOverlappingArea(vi, *fillInformation,
stroke->getBBox());
TStroke *str = new TStroke(*strokes[i]);
- vi->addStroke(str);
- vi->moveStrokes(vi->getStrokeCount() - 1,1,0);
+ switch (drawOrder) {
+ default:
+ case OverAll:
+ vi->addStroke(str, true, false);
+ break;
+ case UnderAll:
+ vi->addStroke(str, true, false);
+ break;
+ case PaletteOrder:
+ QSet<int> aboveStyles;
+ getAboveStyleIdSet(str->getStyle(), vi->getPalette(), aboveStyles);
+ vi->addStrokeBelow(str, aboveStyles);
+ break;
+ }
TUndoManager::manager()->add(new UndoPencil(str, fillInformation, sl, id,
frameCreated, levelCreated,
autoGroup, autoFill));
@@ -396,8 +426,20 @@ static void addStroke(TTool::Application *application, const TVectorImageP &vi,
ImageUtils::getFillingInformationOverlappingArea(vi, *fillInformation,
stroke->getBBox());
TStroke *str = new TStroke(*stroke);
- vi->addStroke(str);
- vi->moveStrokes(vi->getStrokeCount() - 1, 1, 0);
+ switch (drawOrder) {
+ default:
+ case OverAll:
+ vi->addStroke(str, true, false);
+ break;
+ case UnderAll:
+ vi->addStroke(str, true, true);
+ break;
+ case PaletteOrder:
+ QSet<int> aboveStyles;
+ getAboveStyleIdSet(str->getStyle(), vi->getPalette(), aboveStyles);
+ vi->addStrokeBelow(str, aboveStyles);
+ break;
+ }
TUndoManager::manager()->add(new UndoPencil(str, fillInformation, sl, id,
frameCreated, levelCreated,
@@ -449,34 +491,17 @@ namespace {
//-------------------------------------------------------------------
void addStrokeToImage(TTool::Application *application, const TVectorImageP &vi,
- TStroke *stroke, bool breakAngles, bool autoGroup,
- bool autoFill, bool frameCreated, bool levelCreated,
- TXshSimpleLevel *sLevel = NULL,
- TFrameId id = TFrameId::NO_FRAME) {
+ TStroke *stroke, DrawOrder drawOrder, bool breakAngles,
+ bool autoGroup, bool autoFill, bool frameCreated,
+ bool levelCreated, TXshSimpleLevel *sLevel = NULL,
+ TFrameId id = TFrameId::NO_FRAME) {
QMutexLocker lock(vi->getMutex());
- addStroke(application, vi.getPointer(), stroke, breakAngles, autoGroup,
- autoFill, frameCreated, levelCreated, sLevel, id);
+ addStroke(application, vi.getPointer(), stroke, drawOrder, breakAngles,
+ autoGroup, autoFill, frameCreated, levelCreated, sLevel, id);
// la notifica viene gia fatta da addStroke!
// getApplication()->getCurrentTool()->getTool()->notifyImageChanged();
}
-//---------------------------------------------------------------------------------------------------------
-
-enum DrawOrder { OverAll = 0, UnderAll, PaletteOrder };
-
-void getAboveStyleIdSet(int styleId, TPaletteP palette,
- QSet<int> &aboveStyles) {
- if (!palette) return;
- for (int p = 0; p < palette->getPageCount(); p++) {
- TPalette::Page *page = palette->getPage(p);
- for (int s = 0; s < page->getStyleCount(); s++) {
- int tmpId = page->getStyleId(s);
- if (tmpId == styleId) return;
- if (tmpId != 0) aboveStyles.insert(tmpId);
- }
- }
-}
-
//=========================================================================================================
double computeThickness(double pressure, const TDoublePairProperty &property,
@@ -999,7 +1024,8 @@ void ToonzVectorBrushTool::inputSetBusy(bool busy) {
TUndoManager::manager()->beginBlock();
for(StrokeList::iterator i = strokes.begin(); i != strokes.end(); ++i) {
TStroke *stroke = *i;
- addStrokeToImage(app, vi, stroke, m_breakAngles.getValue(),
+ addStrokeToImage(app, vi, stroke, (DrawOrder)m_drawOrder.getIndex(),
+ m_breakAngles.getValue(),
false, false, m_isFrameCreated, m_isLevelCreated);
if ((Preferences::instance()->getGuidedDrawingType() == 1 ||
@@ -1275,12 +1301,14 @@ bool ToonzVectorBrushTool::doFrameRangeStrokes(
if (!swapped && !drawFirstStroke) {
} else
addStrokeToImage(getApplication(), img, firstImage->getStroke(0),
+ (DrawOrder)m_drawOrder.getIndex(),
breakAngles, autoGroup, autoFill, m_isFrameCreated,
m_isLevelCreated, sl, fid);
} else if (t == 1) {
if (swapped && !drawFirstStroke) {
} else if (drawLastStroke)
addStrokeToImage(getApplication(), img, lastImage->getStroke(0),
+ (DrawOrder)m_drawOrder.getIndex(),
breakAngles, autoGroup, autoFill, m_isFrameCreated,
m_isLevelCreated, sl, fid);
} else {
@@ -1288,7 +1316,8 @@ bool ToonzVectorBrushTool::doFrameRangeStrokes(
assert(lastImage->getStrokeCount() == 1);
TVectorImageP vi = TInbetween(firstImage, lastImage).tween(s);
assert(vi->getStrokeCount() == 1);
- addStrokeToImage(getApplication(), img, vi->getStroke(0), breakAngles,
+ addStrokeToImage(getApplication(), img, vi->getStroke(0),
+ (DrawOrder)m_drawOrder.getIndex(), breakAngles,
autoGroup, autoFill, m_isFrameCreated, m_isLevelCreated,
sl, fid);
}