Skip to content

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

C

Add Trail Cycle to Style Editor Settings

Rodney191c6bf1c88b
diff --git a/toonz/sources/common/tvectorimage/tstroke.cpp b/toonz/sources/common/tvectorimage/tstroke.cpp
index 586b1c9e7..cd753ea5a 100644
--- a/toonz/sources/common/tvectorimage/tstroke.cpp
+++ b/toonz/sources/common/tvectorimage/tstroke.cpp
@@ -3776,13 +3776,19 @@ TStroke::OutlineOptions::OutlineOptions()
: m_capStyle(ROUND_CAP)
, m_joinStyle(ROUND_JOIN)
, m_miterLower(0.0)
- , m_miterUpper(4.0) {}
+ , m_miterUpper(4.0)
+ , m_patternFrameOffset(0)
+ , m_patternFrameStep(1) {}
//-----------------------------------------------------------------------------
TStroke::OutlineOptions::OutlineOptions(UCHAR capStyle, UCHAR joinStyle,
- double lower, double upper)
+ double lower, double upper,
+ int patternFrameOffset,
+ int patternFrameStep)
: m_capStyle(capStyle)
, m_joinStyle(joinStyle)
, m_miterLower(lower)
- , m_miterUpper(upper) {}
+ , m_miterUpper(upper)
+ , m_patternFrameOffset(patternFrameOffset)
+ , m_patternFrameStep(patternFrameStep) {}
diff --git a/toonz/sources/common/tvrender/tpalette.cpp b/toonz/sources/common/tvrender/tpalette.cpp
index c70f501df..0a42fd44b 100644
--- a/toonz/sources/common/tvrender/tpalette.cpp
+++ b/toonz/sources/common/tvrender/tpalette.cpp
@@ -649,13 +649,21 @@ void TPalette::saveData(TOStream &os) {
{
for (int i = 0; i < getStyleCount(); ++i) {
TColorStyleP style = m_styles[i].second;
- if (style->getPickedPosition().pos == TPoint())
- os.openChild("style");
- else {
- std::map<std::string, std::string> attr;
+ std::map<std::string, std::string> attr;
+ if (style->getPickedPosition().pos != TPoint())
attr["pickedpos"] = pointToString(style->getPickedPosition());
- os.openChild("style", attr);
- }
+ const auto trailMode = TrailStyles::getMode(style.getPointer());
+ if (trailMode != TrailCycle::Mode::Off)
+ attr["trailCycle"] = std::to_string(int(trailMode));
+ const int trailOffset = TrailStyles::getFrameOffset(style.getPointer());
+ if (trailOffset > 0)
+ attr["trailFrameOffset"] = std::to_string(trailOffset);
+ const double trailSize =
+ TrailStyles::getSizeMultiplier(style.getPointer());
+ if (trailSize != 1.0)
+ attr["trailSizeMultiplier"] =
+ QString::number(trailSize, 'g', 17).toStdString();
+ os.openChild("style", attr);
{
StyleWriter w(os, i);
style->save(w);
@@ -708,6 +716,16 @@ void TPalette::saveData(TOStream &os) {
attributes.clear();
attributes["frame"] = std::to_string(frame);
+ const auto trailMode = TrailStyles::getMode(cs);
+ if (trailMode != TrailCycle::Mode::Off)
+ attributes["trailCycle"] = std::to_string(int(trailMode));
+ const int trailOffset = TrailStyles::getFrameOffset(cs);
+ if (trailOffset > 0)
+ attributes["trailFrameOffset"] = std::to_string(trailOffset);
+ const double trailSize = TrailStyles::getSizeMultiplier(cs);
+ if (trailSize != 1.0)
+ attributes["trailSizeMultiplier"] =
+ QString::number(trailSize, 'g', 17).toStdString();
/*os.openChild("keycolor", attributes); // Up
to Toonz 7.0, animations saved os << cs->getMainColor(); // the main
@@ -771,6 +789,19 @@ void TPalette::loadData(TIStream &is) {
{
StyleReader r(is, version);
TColorStyle *cs = TColorStyle::load(r);
+ int trailMode = 0;
+ if (is.getTagParam("trailCycle", trailMode))
+ TrailStyles::setMode(cs, TrailCycle::modeFromValue(trailMode));
+ int trailOffset = 0;
+ if (is.getTagParam("trailFrameOffset", trailOffset))
+ TrailStyles::setFrameOffset(cs, trailOffset);
+ std::string trailSize;
+ if (is.getTagParam("trailSizeMultiplier", trailSize)) {
+ bool ok = false;
+ const double multiplier =
+ QString::fromStdString(trailSize).toDouble(&ok);
+ if (ok) TrailStyles::setSizeMultiplier(cs, multiplier);
+ }
std::string pickedPosStr;
if (is.getTagParam("pickedpos", pickedPosStr))
@@ -853,6 +884,19 @@ void TPalette::loadData(TIStream &is) {
StyleReader r(is, version);
cs = TColorStyle::load(r);
+ int trailMode = 0;
+ if (is.getTagParam("trailCycle", trailMode))
+ TrailStyles::setMode(cs, TrailCycle::modeFromValue(trailMode));
+ int trailOffset = 0;
+ if (is.getTagParam("trailFrameOffset", trailOffset))
+ TrailStyles::setFrameOffset(cs, trailOffset);
+ std::string trailSize;
+ if (is.getTagParam("trailSizeMultiplier", trailSize)) {
+ bool ok = false;
+ const double multiplier =
+ QString::fromStdString(trailSize).toDouble(&ok);
+ if (ok) TrailStyles::setSizeMultiplier(cs, multiplier);
+ }
} else
throw TException("palette, expected <keyframe> tag");
@@ -1062,6 +1106,10 @@ void TPalette::setFrame(int frame) {
TColorStyle *cs = getStyle(styleId);
assert(cs);
+ // Trail Cycle is a drawing default, not an animated appearance parameter.
+ const auto trailMode = TrailStyles::getMode(cs);
+ const int trailOffset = TrailStyles::getFrameOffset(cs);
+
// Build the keyframes interval containing frame
StyleAnimation::iterator j0, j1;
@@ -1082,6 +1130,8 @@ void TPalette::setFrame(int frame) {
(frame - j0->first) / double(j1->first - j0->first));
}
}
+ TrailStyles::setMode(cs, trailMode);
+ TrailStyles::setFrameOffset(cs, trailOffset);
}
}
diff --git a/toonz/sources/common/tvrender/tsimplecolorstyles.cpp b/toonz/sources/common/tvrender/tsimplecolorstyles.cpp
index 1b850c94c..a33f6db0b 100644
--- a/toonz/sources/common/tvrender/tsimplecolorstyles.cpp
+++ b/toonz/sources/common/tvrender/tsimplecolorstyles.cpp
@@ -1,5 +1,8 @@
#include <cstring>
+#include <limits>
+#include <QStringList>
+#include <QDir>
// TnzCore includes
#include "tsystem.h"
@@ -17,6 +20,8 @@
#include "tofflinegl.h"
#include "drawutil.h"
#include "trop.h"
+#include "tropcm.h"
+#include "ttoonzimage.h"
#include "tstencilcontrol.h"
#include "tpalette.h"
#include "tpixelutils.h"
@@ -27,6 +32,7 @@
#include "tregion.h"
#include "tsimplecolorstyles.h"
+#include "trailscale.h"
#ifndef _WIN32
#define CALLBACK
@@ -91,6 +97,28 @@ TRaster32P makeTexture(const TRaster32P &ras) {
//-----------------------------------------------------------------------------
+double maximumPatternThickness(const TStroke *stroke) {
+ double thickness = 0.0;
+ for (int i = 0; i < stroke->getControlPointCount(); ++i)
+ thickness = (std::max)(thickness, stroke->getControlPoint(i).thick);
+ return thickness;
+}
+
+int findPatternSourceFrameIndex(const TLevelP &level, int frame) {
+ if (!level || frame <= 0) return -1;
+ const TFrameId fid(frame);
+ int index = 0;
+ for (auto it = level->begin(); it != level->end(); ++it, ++index) {
+ if (it->first == fid) return it->second ? index : -1;
+ }
+ return -1;
+}
+
+TLevel::Iterator getPatternFrameIterator(const TLevelP &level,
+ const TStroke *stroke);
+void advancePatternFrameIterator(TLevel::Iterator &it, const TLevelP &level,
+ int step);
+
class OutlineBuilder {
private:
double width;
@@ -988,6 +1016,54 @@ TColorStyle *TRasterImagePatternStrokeStyle::clone() const {
//-----------------------------------------------------------------------------
+TColorStyle &TRasterImagePatternStrokeStyle::copy(const TColorStyle &other) {
+ const auto *trail =
+ dynamic_cast<const TRasterImagePatternStrokeStyle *>(&other);
+ assert(trail);
+ if (!trail) return *this;
+ TColorStyle::copy(other);
+ m_trailCycle = trail->m_trailCycle;
+ m_trailFrameOffset = trail->m_trailFrameOffset;
+ return *this;
+}
+
+//-----------------------------------------------------------------------------
+
+void TRasterImagePatternStrokeStyle::getParamRange(int index,
+ QStringList &items) const {
+ assert(index == TrailStyles::cycleParam);
+ items << QCoreApplication::translate("TRasterImagePatternStrokeStyle", "Off")
+ << QCoreApplication::translate("TRasterImagePatternStrokeStyle",
+ "Forward")
+ << QCoreApplication::translate("TRasterImagePatternStrokeStyle",
+ "Backward")
+ << QCoreApplication::translate("TRasterImagePatternStrokeStyle",
+ "Repeat");
+}
+
+//-----------------------------------------------------------------------------
+
+int TRasterImagePatternStrokeStyle::getParamValue(TColorStyle::int_tag,
+ int index) const {
+ assert(index == TrailStyles::cycleParam ||
+ index == TrailStyles::frameOffsetParam);
+ return index == TrailStyles::frameOffsetParam ? m_trailFrameOffset
+ : int(m_trailCycle);
+}
+
+//-----------------------------------------------------------------------------
+
+void TRasterImagePatternStrokeStyle::setParamValue(int index, int value) {
+ if (index == TrailStyles::cycleParam)
+ m_trailCycle = TrailCycle::modeFromValue(value);
+ else if (index == TrailStyles::frameOffsetParam)
+ setTrailFrameOffset(value);
+ else
+ setParamValue(index, double(value));
+}
+
+//-----------------------------------------------------------------------------
+
TColorStyle *TRasterImagePatternStrokeStyle::clone(
std::string brushIdName) const {
TRasterImagePatternStrokeStyle *style =
@@ -1049,13 +1125,41 @@ void TRasterImagePatternStrokeStyle::makeIcon(const TDimension &size) {
//-----------------------------------------------------------------------------
-int TRasterImagePatternStrokeStyle::getParamCount() const { return 2; }
+void TRasterImagePatternStrokeStyle::setTrailSizeMultiplier(double multiplier) {
+ multiplier = TrailScale::normalized(multiplier);
+ if (m_trailSizeMultiplier == multiplier) return;
+ m_trailSizeMultiplier = multiplier;
+ updateVersionNumber(); // Rebuild cached placements and update drawing
+ // bounds.
+ invalidateIcon();
+}
+
+//-----------------------------------------------------------------------------
+
+int TRasterImagePatternStrokeStyle::getTrailStartFrameIndex() const {
+ return findPatternSourceFrameIndex(m_level, m_trailFrameOffset);
+}
+
+//-----------------------------------------------------------------------------
+
+void TRasterImagePatternStrokeStyle::getParamRange(int index, int &min,
+ int &max) const {
+ assert(index == TrailStyles::frameOffsetParam);
+ min = 0;
+ max = (std::numeric_limits<int>::max)();
+}
+
+//-----------------------------------------------------------------------------
+
+int TRasterImagePatternStrokeStyle::getParamCount() const { return 5; }
//-----------------------------------------------------------------------------
TColorStyle::ParamType TRasterImagePatternStrokeStyle::getParamType(
int index) const {
assert(0 <= index && index < getParamCount());
+ if (index == TrailStyles::cycleParam) return TColorStyle::ENUM;
+ if (index == TrailStyles::frameOffsetParam) return TColorStyle::INT;
return TColorStyle::DOUBLE;
}
@@ -1063,6 +1167,15 @@ TColorStyle::ParamType TRasterImagePatternStrokeStyle::getParamType(
QString TRasterImagePatternStrokeStyle::getParamNames(int index) const {
assert(0 <= index && index < getParamCount());
+ if (index == TrailStyles::sizeMultiplierParam)
+ return QCoreApplication::translate("TRasterImagePatternStrokeStyle",
+ "Size Multiplier");
+ if (index == TrailStyles::cycleParam)
+ return QCoreApplication::translate("TRasterImagePatternStrokeStyle",
+ "Trail Cycle");
+ if (index == TrailStyles::frameOffsetParam)
+ return QCoreApplication::translate("TRasterImagePatternStrokeStyle",
+ "Frame Offset");
return (index == 0) ? QCoreApplication::translate(
"TRasterImagePatternStrokeStyle", "Distance")
: QCoreApplication::translate(
@@ -1074,6 +1187,11 @@ QString TRasterImagePatternStrokeStyle::getParamNames(int index) const {
void TRasterImagePatternStrokeStyle::getParamRange(int index, double &min,
double &max) const {
assert(0 <= index && index < getParamCount());
+ if (index == TrailStyles::sizeMultiplierParam) {
+ min = TrailScale::minimum;
+ max = TrailScale::maximum;
+ return;
+ }
if (index == 0) {
min = -50;
@@ -1089,6 +1207,7 @@ void TRasterImagePatternStrokeStyle::getParamRange(int index, double &min,
double TRasterImagePatternStrokeStyle::getParamValue(TColorStyle::double_tag,
int index) const {
assert(0 <= index && index < getParamCount());
+ if (index == TrailStyles::sizeMultiplierParam) return m_trailSizeMultiplier;
return (index == 0) ? m_space : m_rotation;
}
@@ -1096,6 +1215,10 @@ double TRasterImagePatternStrokeStyle::getParamValue(TColorStyle::double_tag,
void TRasterImagePatternStrokeStyle::setParamValue(int index, double value) {
assert(0 <= index && index < getParamCount());
+ if (index == TrailStyles::sizeMultiplierParam) {
+ setTrailSizeMultiplier(value);
+ return;
+ }
if (index == 0) {
if (m_space != value) {
@@ -1112,9 +1235,94 @@ void TRasterImagePatternStrokeStyle::setParamValue(int index, double value) {
//-----------------------------------------------------------------------------
-//
-// carico il pattern 'patternName' dalla directory dei custom styles
-//
+QString TrailStyles::sourceFilters() {
+ return QStringLiteral(
+ "*.pli *.tlv *.png *.tif *.tiff *.tga *.sgi *.rgb "
+ "*.pct *.pic *.pict *.exr *.bmp *.jpg *.jpeg *.nol");
+}
+
+//-----------------------------------------------------------------------------
+
+TFilePath TrailStyles::findSource(const TFilePath &root,
+ const std::string &name) {
+ // Keep the historical basename identity and ordering, but do not let a
+ // palette sidecar, backup, or directory shadow an actual image source.
+ TFilePathSet paths;
+ TSystem::readDirectory(paths, root, true, true);
+ const QStringList filters = sourceFilters().split(' ');
+ for (const TFilePath &path : paths) {
+ if (path.getName() == name &&
+ QDir::match(filters, QString::fromStdWString(path.getLevelNameW())))
+ return path;
+ }
+ return TFilePath();
+}
+
+//-----------------------------------------------------------------------------
+
+TRaster32P TrailStyles::rasterSource(const TImageP &image,
+ TPalette *levelPalette, int sourceFrame) {
+ if (TRasterImageP ri = image) {
+ TRasterP input = ri->getRaster();
+ if (!input) return TRaster32P();
+ if (TRaster32P rgba = input) return rgba; // Leave existing RGBA untouched.
+ if (TRasterGR16P gray = input) {
+ // The generic converter has no GR16 -> RGBA path.
+ TRaster32P rgba(input->getSize());
+ gray->lock();
+ rgba->lock();
+ for (int y = 0; y < gray->getLy(); ++y) {
+ const TPixelGR16 *source = gray->pixels(y);
+ TPixel32 *target = rgba->pixels(y);
+ for (int x = 0; x < gray->getLx(); ++x) {
+ const int value = (int(source[x].value) + 128) / 257;
+ target[x] = TPixel32(value, value, value, 255);
+ }
+ }
+ rgba->setLinear(input->isLinear());
+ rgba->unlock();
+ gray->unlock();
+ return rgba;
+ }
+ if (!TRaster64P(input) && !TRasterFP(input) && !TRasterGR8P(input))
+ return TRaster32P();
+ TRaster32P rgba(input->getSize());
+ TRop::convert(rgba, input);
+ return rgba;
+ }
+
+ TToonzImageP ti = image;
+ if (!ti || !ti->getRaster()) return TRaster32P();
+ TPaletteP palette = ti->getPalette() ? ti->getPalette() : levelPalette;
+ if (!palette || palette->getStyleCount() == 0) return TRaster32P();
+ if (sourceFrame >= 0 && palette->isAnimated()) {
+ palette = palette->clone();
+ palette->setFrame(sourceFrame); // Never scrub the caller's palette.
+ }
+
+ TRasterCM32P input = ti->getRaster();
+ const int count = palette->getStyleCount();
+ // A damaged/mismatched sidecar must not index past the conversion tables.
+ input->lock();
+ for (int y = 0; y < input->getLy(); ++y) {
+ const TPixelCM32 *row = input->pixels(y);
+ for (int x = 0; x < input->getLx(); ++x) {
+ if ((row[x].getTone() < TPixelCM32::getMaxTone() &&
+ row[x].getInk() >= count) ||
+ (row[x].getTone() > 0 && row[x].getPaint() >= count)) {
+ input->unlock();
+ return TRaster32P();
+ }
+ }
+ }
+ input->unlock();
+ TRaster32P rgba(input->getSize());
+ TRop::convert(rgba, input, palette);
+ return rgba;
+}
+
+//-----------------------------------------------------------------------------
+
void TRasterImagePatternStrokeStyle::loadLevel(const std::string &patternName) {
struct locals {
static TAffine getAffine(const TDimension &srcSize,
@@ -1131,6 +1339,7 @@ void TRasterImagePatternStrokeStyle::loadLevel(const std::string &patternName) {
// button l'eventuale livello
m_level = TLevelP();
+ m_toonzRasterSource = false;
// aggiorno il nome
m_name = patternName;
@@ -1138,23 +1347,15 @@ void TRasterImagePatternStrokeStyle::loadLevel(const std::string &patternName) {
// getRootDir() e' nulla se non si e' chiamata la setRoot(..)
assert(!getRootDir().isEmpty());
- // leggo tutti i livelli contenuti
- TFilePathSet fps;
- TSystem::readDirectory(fps, getRootDir());
-
- // prendo il primo livello il cui nome sia patternName
- // (puo' essere un pli, ma anche un png, ecc.)
- TFilePath fp;
- TFilePathSet::iterator fpIt;
- for (fpIt = fps.begin(); fpIt != fps.end(); ++fpIt) {
- if (fpIt->getName() == patternName) {
- fp = *fpIt;
- break;
- }
- }
+ const TFilePath fp = TrailStyles::findSource(getRootDir(), patternName);
// se non ho trovato nulla esco
if (fp == TFilePath() || !TSystem::doesExistFileOrLevel(fp)) return;
+ // The TLV reader synthesizes fallback colors when its TPL is missing. Do not
+ // bake those diagnostic colors into a Trail or silently guess a palette.
+ if (fp.getType() == "tlv" &&
+ !TFileStatus(fp.withNoFrame().withType("tpl")).doesExist())
+ return;
// Leggo i frames del livello e ne ricavo delle textures
// che memorizzo in m_level come TRasterImage
@@ -1165,14 +1366,15 @@ void TRasterImagePatternStrokeStyle::loadLevel(const std::string &patternName) {
for (frameIt = level->begin(); frameIt != level->end(); ++frameIt) {
TImageP img = lr->getFrameReader(frameIt->first)->load();
- if (TRasterImageP ri = img) {
- // se il frame e' raster...
- TRaster32P ras = ri->getRaster();
+ if (TRasterImageP(img) || TToonzImageP(img)) {
+ TRaster32P ras = TrailStyles::rasterSource(
+ img, level->getPalette(), frameIt->first.getNumber() - 1);
if (!ras) continue;
// aggiusta le dimensioni
ras = makeTexture(ras);
if (!ras) continue;
m_level->setFrame(frameIt->first, new TRasterImage(ras));
+ m_toonzRasterSource = bool(TToonzImageP(img));
} else if (TVectorImageP vi = img) {
// se il frame e' vettoriale
// lo rasterizzo creando una texture 256x256 (N.B. le dimensioni
@@ -1230,7 +1432,7 @@ void TRasterImagePatternStrokeStyle::computeTransformations(
double ang = rad2degree(atan(v)) + m_rotation;
int ly = std::max(1.0, images[index].ly);
- double sc = p.thick / ly;
+ double sc = TrailScale::apply(p.thick / ly, m_trailSizeMultiplier);
transformations.push_back(TTranslation(p) * TRotation(ang) * TScale(sc));
double ds = std::max(2.0, sc * images[index].lx * 2 + m_space);
s += ds;
@@ -1256,7 +1458,10 @@ void TRasterImagePatternStrokeStyle::drawStroke(
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ // TRop's indexed-color conversion is premultiplied. Apply its alpha once;
+ // retain the legacy blend mode for pre-existing full-color Trail sources.
+ glBlendFunc(m_toonzRasterSource ? GL_ONE : GL_SRC_ALPHA,
+ GL_ONE_MINUS_SRC_ALPHA);
GLuint texId;
glGenTextures(1, &texId);
@@ -1276,9 +1481,13 @@ void TRasterImagePatternStrokeStyle::drawStroke(
// visto che cambiare texture costa tempo il ciclo esterno e' sulle textures
// piuttosto che sulle trasformazioni
- TLevel::Iterator frameIt = m_level->begin();
- for (int i = 0; i < (int)size && frameIt != m_level->end(); ++i, ++frameIt) {
- TRasterImageP ri = frameIt->second;
+ TLevel::Iterator frameIt = getPatternFrameIterator(m_level, stroke);
+ const int frameStep = stroke->outlineOptions().m_patternFrameStep;
+ const int distinctFrameCount = (frameStep % frameCount == 0) ? 1 : frameCount;
+ for (int i = 0; i < (int)size && i < distinctFrameCount; ++i) {
+ TLevel::Iterator currentFrame = frameIt;
+ advancePatternFrameIterator(frameIt, m_level, frameStep);
+ TRasterImageP ri = currentFrame->second;
TRasterP ras;
if (ri) ras = ri->getRaster();
if (!ras) continue;
@@ -1295,7 +1504,7 @@ void TRasterImagePatternStrokeStyle::drawStroke(
texInfo.format, // pixel data type // oh, SO much
texImage->getRawData());
- for (int j = i; j < (int)size; j += frameCount) {
+ for (int j = i; j < (int)size; j += distinctFrameCount) {
TAffine aff = rd.m_aff * transformations[j];
glPushMatrix();
tglMultMatrix(aff);
@@ -1390,7 +1599,28 @@ void TRasterImagePatternStrokeStyle::getObsoleteTagIds(
TRectD TRasterImagePatternStrokeStyle::getStrokeBBox(
const TStroke *stroke) const {
TRectD rect = TColorStyle::getStrokeBBox(stroke);
- return rect.enlarge(std::max(rect.getLx() * 0.25, rect.getLy() * 0.25));
+ double padding = std::max(rect.getLx() * 0.25, rect.getLy() * 0.25);
+ if (!m_level || m_level->getFrameCount() == 0) return rect.enlarge(padding);
+
+ // Legacy raster placement uses the first valid frame's height for every
+ // stamp. The quad uses +/-width and +/-height, including transparent padding.
+ double referenceHeight = 0.0;
+ double sourceRadius = 0.0;
+ for (auto it = m_level->begin(); it != m_level->end(); ++it) {
+ TRasterImageP image = it->second;
+ if (!image || !image->getRaster()) continue;
+ const TDimension size = image->getRaster()->getSize();
+ if (referenceHeight == 0.0) referenceHeight = (std::max)(1, size.ly);
+ sourceRadius =
+ (std::max)(sourceRadius, TrailScale::radius(size.lx, size.ly, 1.0));
+ }
+ if (referenceHeight > 0.0) {
+ const double scale =
+ TrailScale::apply(maximumPatternThickness(stroke) / referenceHeight,
+ m_trailSizeMultiplier);
+ padding = (std::max)(padding, sourceRadius * scale);
+ }
+ return rect.enlarge(padding);
}
//*************************************************************************************
@@ -1416,6 +1646,54 @@ TColorStyle *TVectorImagePatternStrokeStyle::clone() const {
//-----------------------------------------------------------------------------
+TColorStyle &TVectorImagePatternStrokeStyle::copy(const TColorStyle &other) {
+ const auto *trail =
+ dynamic_cast<const TVectorImagePatternStrokeStyle *>(&other);
+ assert(trail);
+ if (!trail) return *this;
+ TColorStyle::copy(other);
+ m_trailCycle = trail->m_trailCycle;
+ m_trailFrameOffset = trail->m_trailFrameOffset;
+ return *this;
+}
+
+//-----------------------------------------------------------------------------
+
+void TVectorImagePatternStrokeStyle::getParamRange(int index,
+ QStringList &items) const {
+ assert(index == TrailStyles::cycleParam);
+ items << QCoreApplication::translate("TVectorImagePatternStrokeStyle", "Off")
+ << QCoreApplication::translate("TVectorImagePatternStrokeStyle",
+ "Forward")
+ << QCoreApplication::translate("TVectorImagePatternStrokeStyle",
+ "Backward")
+ << QCoreApplication::translate("TVectorImagePatternStrokeStyle",
+ "Repeat");
+}
+
+//-----------------------------------------------------------------------------
+
+int TVectorImagePatternStrokeStyle::getParamValue(TColorStyle::int_tag,
+ int index) const {
+ assert(index == TrailStyles::cycleParam ||
+ index == TrailStyles::frameOffsetParam);
+ return index == TrailStyles::frameOffsetParam ? m_trailFrameOffset
+ : int(m_trailCycle);
+}
+
+//-----------------------------------------------------------------------------
+
+void TVectorImagePatternStrokeStyle::setParamValue(int index, int value) {
+ if (index == TrailStyles::cycleParam)
+ m_trailCycle = TrailCycle::modeFromValue(value);
+ else if (index == TrailStyles::frameOffsetParam)
+ setTrailFrameOffset(value);
+ else
+ setParamValue(index, double(value));
+}
+
+//-----------------------------------------------------------------------------
+
TColorStyle *TVectorImagePatternStrokeStyle::clone(
std::string brushIdName) const {
TVectorImagePatternStrokeStyle *style =
@@ -1490,13 +1768,41 @@ void TVectorImagePatternStrokeStyle::makeIcon(const TDimension &size) {
//-----------------------------------------------------------------------------
-int TVectorImagePatternStrokeStyle::getParamCount() const { return 2; }
+void TVectorImagePatternStrokeStyle::setTrailSizeMultiplier(double multiplier) {
+ multiplier = TrailScale::normalized(multiplier);
+ if (m_trailSizeMultiplier == multiplier) return;
+ m_trailSizeMultiplier = multiplier;
+ updateVersionNumber(); // Rebuild cached placements and update drawing
+ // bounds.
+ invalidateIcon();
+}
+
+//-----------------------------------------------------------------------------
+
+int TVectorImagePatternStrokeStyle::getTrailStartFrameIndex() const {
+ return findPatternSourceFrameIndex(m_level, m_trailFrameOffset);
+}
+
+//-----------------------------------------------------------------------------
+
+void TVectorImagePatternStrokeStyle::getParamRange(int index, int &min,
+ int &max) const {
+ assert(index == TrailStyles::frameOffsetParam);
+ min = 0;
+ max = (std::numeric_limits<int>::max)();
+}
+
+//-----------------------------------------------------------------------------
+
+int TVectorImagePatternStrokeStyle::getParamCount() const { return 5; }
//-----------------------------------------------------------------------------
TColorStyle::ParamType TVectorImagePatternStrokeStyle::getParamType(
int index) const {
assert(0 <= index && index < getParamCount());
+ if (index == TrailStyles::cycleParam) return TColorStyle::ENUM;
+ if (index == TrailStyles::frameOffsetParam) return TColorStyle::INT;
return TColorStyle::DOUBLE;
}
@@ -1504,6 +1810,15 @@ TColorStyle::ParamType TVectorImagePatternStrokeStyle::getParamType(
QString TVectorImagePatternStrokeStyle::getParamNames(int index) const {
assert(0 <= index && index < getParamCount());
+ if (index == TrailStyles::sizeMultiplierParam)
+ return QCoreApplication::translate("TVectorImagePatternStrokeStyle",
+ "Size Multiplier");
+ if (index == TrailStyles::cycleParam)
+ return QCoreApplication::translate("TVectorImagePatternStrokeStyle",
+ "Trail Cycle");
+ if (index == TrailStyles::frameOffsetParam)
+ return QCoreApplication::translate("TVectorImagePatternStrokeStyle",
+ "Frame Offset");
return (index == 0) ? QCoreApplication::translate(
"TVectorImagePatternStrokeStyle", "Distance")
: QCoreApplication::translate(
@@ -1515,6 +1830,11 @@ QString TVectorImagePatternStrokeStyle::getParamNames(int index) const {
void TVectorImagePatternStrokeStyle::getParamRange(int index, double &min,
double &max) const {
assert(0 <= index && index < getParamCount());
+ if (index == TrailStyles::sizeMultiplierParam) {
+ min = TrailScale::minimum;
+ max = TrailScale::maximum;
+ return;
+ }
if (index == 0) {
min = -50;
@@ -1530,6 +1850,7 @@ void TVectorImagePatternStrokeStyle::getParamRange(int index, double &min,
double TVectorImagePatternStrokeStyle::getParamValue(TColorStyle::double_tag,
int index) const {
assert(0 <= index && index < getParamCount());
+ if (index == TrailStyles::sizeMultiplierParam) return m_trailSizeMultiplier;
return index == 0 ? m_space : m_rotation;
}
@@ -1537,6 +1858,10 @@ double TVectorImagePatternStrokeStyle::getParamValue(TColorStyle::double_tag,
void TVectorImagePatternStrokeStyle::setParamValue(int index, double value) {
assert(0 <= index && index < getParamCount());
+ if (index == TrailStyles::sizeMultiplierParam) {
+ setTrailSizeMultiplier(value);
+ return;
+ }
if (index == 0) {
if (m_space != value) {
@@ -1571,6 +1896,50 @@ void TVectorImagePatternStrokeStyle::loadLevel(const std::string &patternName) {
//--------------------------------------------------------------------------------------------------
+namespace {
+
+TLevel::Iterator getPatternFrameIterator(const TLevelP &level,
+ const TStroke *stroke) {
+ const int frameCount = level->getFrameCount();
+ assert(frameCount > 0);
+
+ int offset = stroke->outlineOptions().m_patternFrameOffset % frameCount;
+ if (offset < 0) offset += frameCount;
+
+ TLevel::Iterator it = level->begin();
+ while (offset-- > 0) {
+ if (++it == level->end()) it = level->begin();
+ }
+ return it;
+}
+
+// A step of zero holds the iterator still: the stroke shows one frame along
+// its whole length, which is how the brush's Repeat cycle mode freezes a
+// stamp.
+void advancePatternFrameIterator(TLevel::Iterator &it, const TLevelP &level,
+ int step) {
+ const int frameCount = level->getFrameCount();
+ assert(frameCount > 0);
+
+ step %= frameCount;
+ if (step < 0) step += frameCount;
+ if (step > frameCount / 2) step -= frameCount;
+
+ while (step > 0) {
+ if (++it == level->end()) it = level->begin();
+ --step;
+ }
+ while (step < 0) {
+ if (it == level->begin()) it = level->end();
+ --it;
+ ++step;
+ }
+}
+
+} // namespace
+
+//--------------------------------------------------------------------------------------------------
+
void TVectorImagePatternStrokeStyle::computeTransformations(
std::vector<TAffine> &transformations, const TStroke *stroke) const {
const int frameCount = m_level->getFrameCount();
@@ -1578,13 +1947,14 @@ void TVectorImagePatternStrokeStyle::computeTransformations(
transformations.clear();
const double length = stroke->getLength();
assert(m_level->begin() != m_level->end());
- TLevel::Iterator lit = m_level->begin();
+ TLevel::Iterator lit = getPatternFrameIterator(m_level, stroke);
+ const int frameStep = stroke->outlineOptions().m_patternFrameStep;
double s = 0;
while (s < length) {
TFrameId fid = lit->first;
TVectorImageP img = m_level->frame(fid);
- if (++lit == m_level->end()) lit = m_level->begin();
+ advancePatternFrameIterator(lit, m_level, frameStep);
assert(img);
if (img->getType() != TImage::VECTOR) return;
double t = stroke->getParameterAtLength(s);
@@ -1597,6 +1967,7 @@ void TVectorImagePatternStrokeStyle::computeTransformations(
if (ry * ry < 1e-5) ry = p.thick;
double sc = p.thick / ry;
if (sc < 0.0001) sc = 0.0001;
+ sc = TrailScale::apply(sc, m_trailSizeMultiplier);
TAffine aff =
TTranslation(p) * TRotation(ang) * TScale(sc) * TTranslation(-center);
transformations.push_back(aff);
@@ -1633,12 +2004,16 @@ void TVectorImagePatternStrokeStyle::drawStroke(
thickSum += stroke->getControlPoint(cp).thick;
count++;
}
- double averageThick = thickSum / (double)count;
+ double averageThick =
+ TrailScale::apply(thickSum / (double)count, m_trailSizeMultiplier);
glPushMatrix();
tglMultMatrix(rd.m_aff);
double pixelSize2 = tglGetPixelSize2();
glPopMatrix();
- if (averageThick * averageThick < 4 * pixelSize2) {
+ // A single stamp has no useful line-strip approximation: short carriers
+ // produce zero vertices in that path. Draw the source at its actual size.
+ if (transformations.size() > 1 &&
+ averageThick * averageThick < 4 * pixelSize2) {
CHECK_GL_ERROR
glPushMatrix();
@@ -1647,7 +2022,7 @@ void TVectorImagePatternStrokeStyle::drawStroke(
tglMultMatrix(rd.m_aff);
- TLevel::Iterator lit = m_level->begin();
+ TLevel::Iterator lit = getPatternFrameIterator(m_level, stroke);
TFrameId fid = lit->first;
TVectorImageP img = m_level->frame(fid);
@@ -1711,13 +2086,14 @@ void TVectorImagePatternStrokeStyle::drawStroke(
} else {
//--------------------------------------------
assert(m_level->begin() != m_level->end());
- TLevel::Iterator lit = m_level->begin();
+ TLevel::Iterator lit = getPatternFrameIterator(m_level, stroke);
+ const int frameStep = stroke->outlineOptions().m_patternFrameStep;
UINT i, size = transformations.size();
for (i = 0; i < size; i++) {
TFrameId fid = lit->first;
TVectorImageP img = m_level->frame(fid);
- if (++lit == m_level->end()) lit = m_level->begin();
+ advancePatternFrameIterator(lit, m_level, frameStep);
assert(img);
if (!img) continue;
if (img->getType() != TImage::VECTOR) return;
@@ -1830,7 +2206,25 @@ void TVectorImagePatternStrokeStyle::getObsoleteTagIds(
TRectD TVectorImagePatternStrokeStyle::getStrokeBBox(
const TStroke *stroke) const {
TRectD rect = TColorStyle::getStrokeBBox(stroke);
- return rect.enlarge(std::max(rect.getLx() * 0.25, rect.getLy() * 0.25));
+ double padding = std::max(rect.getLx() * 0.25, rect.getLy() * 0.25);
+ if (!m_level || m_level->getFrameCount() == 0) return rect.enlarge(padding);
+
+ const double thickness = maximumPatternThickness(stroke);
+ for (auto it = m_level->begin(); it != m_level->end(); ++it) {
+ TVectorImageP image = it->second;
+ if (!image) continue;
+ const TRectD source = image->getBBox();
+ if (source.isEmpty()) continue;
+ const double halfHeight = source.getLy() * 0.5;
+ // Match the renderer's nearly-flat source fallback and minimum scale.
+ double scale = halfHeight * halfHeight < 1e-5
+ ? 1.0
+ : (std::max)(0.0001, thickness / halfHeight);
+ scale = TrailScale::apply(scale, m_trailSizeMultiplier);
+ padding = (std::max)(
+ padding, TrailScale::radius(source.getLx() * 0.5, halfHeight, scale));
+ }
+ return rect.enlarge(padding);
}
//*************************************************************************************
diff --git a/toonz/sources/common/tvrender/tstrokeprop.cpp b/toonz/sources/common/tvrender/tstrokeprop.cpp
index 33efcc9b4..6c8475f96 100644
--- a/toonz/sources/common/tvrender/tstrokeprop.cpp
+++ b/toonz/sources/common/tvrender/tstrokeprop.cpp
@@ -113,7 +113,8 @@ void TRasterImagePatternStrokeProp::draw(
serbve che non sia const*/
{
if (rd.m_clippingRect != TRect() && !rd.m_is3dView &&
- !convert(rd.m_aff * m_stroke->getBBox()).overlaps(rd.m_clippingRect))
+ !convert(rd.m_aff * m_colorStyle->getStrokeBBox(m_stroke))
+ .overlaps(rd.m_clippingRect))
return;
if (m_strokeChanged ||
@@ -167,7 +168,8 @@ void TVectorImagePatternStrokeProp::draw(
serbve che non sia const*/
{
if (rd.m_clippingRect != TRect() && !rd.m_is3dView &&
- !convert(rd.m_aff * m_stroke->getBBox()).overlaps(rd.m_clippingRect))
+ !convert(rd.m_aff * m_colorStyle->getStrokeBBox(m_stroke))
+ .overlaps(rd.m_clippingRect))
return;
if (m_strokeChanged ||
diff --git a/toonz/sources/image/pli/pli_io.cpp b/toonz/sources/image/pli/pli_io.cpp
index 70b7eb537..1e718be41 100644
--- a/toonz/sources/image/pli/pli_io.cpp
+++ b/toonz/sources/image/pli/pli_io.cpp
@@ -1,3 +1,4 @@
+#include <algorithm>
#include <memory>
#include "tmachine.h"
@@ -539,7 +540,7 @@ ParsedPliImp::ParsedPliImp(const TFilePath &filename, bool readInfo)
TagElem *tagElem;
UCHAR maxThickness;
- // cerr<<m_filePath<<endl;
+ m_filePath = filename;
//#ifdef _WIN32
m_iChan.open(filename);
@@ -1430,6 +1431,9 @@ PliTag *ParsedPliImp::readStyleTag() {
/*=====================================================================*/
PliTag *ParsedPliImp::readOutlineOptionsTag() {
+ const TUINT32 baseLength = 2 + 2 * m_currDynamicTypeBytesNum;
+ if (m_tagLength < baseLength)
+ throw TImageException(m_filePath, "Truncated PLI stroke outline options");
TUINT32 bufOffs = 0;
TINT32 d;
@@ -1447,8 +1451,20 @@ PliTag *ParsedPliImp::readOutlineOptionsTag() {
readDynamicData(d, bufOffs);
miterUpper = scale * d;
- return new StrokeOutlineOptionsTag(
- TStroke::OutlineOptions(capStyle, joinStyle, miterLower, miterUpper));
+ TStroke::OutlineOptions options(capStyle, joinStyle, miterLower, miterUpper);
+
+ // PLI readers that predate this extension consume the original four fields
+ // and ignore the remaining bytes in this tag. Keep the extra data at the
+ // end for that compatibility.
+ if (m_tagLength >= baseLength + 2 * m_currDynamicTypeBytesNum) {
+ TINT32 frameOffset, frameStep;
+ readDynamicData(frameOffset, bufOffs);
+ readDynamicData(frameStep, bufOffs);
+ options.m_patternFrameOffset = frameOffset;
+ options.m_patternFrameStep = frameStep;
+ }
+
+ return new StrokeOutlineOptionsTag(options);
}
/*=====================================================================*/
@@ -2436,10 +2452,19 @@ TUINT32 ParsedPliImp::writeOutlineOptionsTag(StrokeOutlineOptionsTag *tag) {
TINT32 miterLower = scale * tag->m_options.m_miterLower;
TINT32 miterUpper = scale * tag->m_options.m_miterUpper;
+ TINT32 frameOffset = tag->m_options.m_patternFrameOffset;
+ TINT32 frameStep = tag->m_options.m_patternFrameStep;
+ const bool hasPatternFrameData = frameOffset != 0 || frameStep != 1;
- setDynamicTypeBytesNum(scale * miterLower, scale * miterUpper);
+ int minValue = std::min(miterLower, miterUpper);
+ int maxValue = std::max(miterLower, miterUpper);
+ if (hasPatternFrameData) {
+ minValue = std::min({minValue, (int)frameOffset, (int)frameStep});
+ maxValue = std::max({maxValue, (int)frameOffset, (int)frameStep});
+ }
+ setDynamicTypeBytesNum(minValue, maxValue);
- int tagLength = 2 + 2 * m_currDynamicTypeBytesNum;
+ int tagLength = 2 + (hasPatternFrameData ? 4 : 2) * m_currDynamicTypeBytesNum;
int offset =
(int)writeTagHeader((UCHAR)PliTag::OUTLINE_OPTIONS_GOBJ, tagLength);
@@ -2447,6 +2472,10 @@ TUINT32 ParsedPliImp::writeOutlineOptionsTag(StrokeOutlineOptionsTag *tag) {
*m_oChan << (UCHAR)tag->m_options.m_joinStyle;
writeDynamicData(miterLower);
writeDynamicData(miterUpper);
+ if (hasPatternFrameData) {
+ writeDynamicData(frameOffset);
+ writeDynamicData(frameStep);
+ }
return offset;
}
diff --git a/toonz/sources/image/pli/tags.cpp b/toonz/sources/image/pli/tags.cpp
index f9fd5f4ce..14f1a4364 100644
--- a/toonz/sources/image/pli/tags.cpp
+++ b/toonz/sources/image/pli/tags.cpp
@@ -6,6 +6,9 @@
#include "pli_io.h"
+#include <cmath>
+#include <limits>
+
typedef TVectorImage::IntersectionBranch IntersectionBranch;
/*=====================================================================*/
@@ -310,7 +313,18 @@ StrokeOutlineOptionsTag::StrokeOutlineOptionsTag()
StrokeOutlineOptionsTag::StrokeOutlineOptionsTag(
const TStroke::OutlineOptions &options)
- : PliObjectTag(OUTLINE_OPTIONS_GOBJ), m_options(options) {}
+ : PliObjectTag(OUTLINE_OPTIONS_GOBJ), m_options(options) {
+ const double maxMiter = std::numeric_limits<TINT32>::max() / 1000.0;
+ if (!std::isfinite(options.m_miterLower) ||
+ !std::isfinite(options.m_miterUpper) ||
+ std::abs(options.m_miterLower) > maxMiter ||
+ std::abs(options.m_miterUpper) > maxMiter ||
+ options.m_patternFrameOffset == std::numeric_limits<TINT32>::min() ||
+ options.m_patternFrameStep == std::numeric_limits<TINT32>::min())
+ throw TException(
+ "Cannot save PLI outline options: value exceeds the signed-magnitude "
+ "format");
+}
/*=====================================================================*/
diff --git a/toonz/sources/image/pli/tiio_pli.cpp b/toonz/sources/image/pli/tiio_pli.cpp
index 44fcd4352..aeb3d9ade 100644
--- a/toonz/sources/image/pli/tiio_pli.cpp
+++ b/toonz/sources/image/pli/tiio_pli.cpp
@@ -1,6 +1,7 @@
#include "tiio_pli.h"
+#include <cmath>
//#include "tstrokeoutline.h"
#include "tsystem.h"
#include "pli_io.h"
@@ -103,9 +104,60 @@ public:
return *this;
}
+ void readTrailCycle(TColorStyle *style) {
+ if (!TrailStyles::isTrail(style)) return;
+ // Extensions are independently optional and confined to this typed
+ // style record. Unknown/truncated suffixes are left alone.
+ while (m_count + 1 < int(m_stream->size())) {
+ const TStyleParam &marker = (*m_stream)[m_count];
+ const TStyleParam &value = (*m_stream)[m_count + 1];
+ if (marker.m_type != TStyleParam::SP_STRING) return;
+ const bool cycle = marker.m_string == "trail-cycle-v1";
+ const bool offset = marker.m_string == "trail-frame-offset-v1";
+ const bool size = marker.m_string == "trail-size-multiplier-v1";
+ if (!cycle && !offset && !size) return;
+ if (cycle) {
+ if (value.m_type != TStyleParam::SP_INT) return;
+ const double number = value.m_numericVal;
+ if (std::isfinite(number) && number >= 0 && number <= 3 &&
+ number == int(number))
+ TrailStyles::setMode(style, TrailCycle::modeFromValue(int(number)));
+ } else if (offset) {
+ if (value.m_type != TStyleParam::SP_STRING) return;
+ // PLI numeric style parameters have only a 16-bit integer part.
+ // A tagged decimal string preserves the full source-frame ID range.
+ bool ok = false;
+ const int frame = QString::fromStdString(value.m_string).toInt(&ok);
+ if (ok && frame >= 0) TrailStyles::setFrameOffset(style, frame);
+ } else {
+ if (value.m_type != TStyleParam::SP_STRING) return;
+ bool ok = false;
+ const double multiplier =
+ QString::fromStdString(value.m_string).toDouble(&ok);
+ if (ok) TrailStyles::setSizeMultiplier(style, multiplier);
+ }
+ m_count += 2;
+ }
+ }
+
VersionNumber versionNumber() const override { return m_version; }
};
+void saveStyleWithTrailCycle(const TColorStyle *style,
+ PliOutputStream &stream) {
+ style->save(stream);
+ const auto mode = TrailStyles::getMode(style);
+ if (mode != TrailCycle::Mode::Off)
+ stream << std::string("trail-cycle-v1") << int(mode);
+ const int offset = TrailStyles::getFrameOffset(style);
+ if (offset > 0)
+ stream << std::string("trail-frame-offset-v1") << std::to_string(offset);
+ const double multiplier = TrailStyles::getSizeMultiplier(style);
+ if (multiplier != 1.0)
+ stream << std::string("trail-size-multiplier-v1")
+ << QString::number(multiplier, 'g', 17).toStdString();
+}
+
//---------------------------------------------------------------------------
TPixel32 getColor(const TStroke *stroke) {
@@ -185,7 +237,7 @@ for (int pageIndex = 0; pageIndex < vPalette->getPageCount(); pageIndex++) {
TColorStyle *style = vPalette->getStyle(styleId);
std::vector<TStyleParam> stream;
PliOutputStream chan(&stream);
- style->save(chan); // viene riempito lo stream;
+ saveStyleWithTrailCycle(style, chan);
assert(pageIndex >= 0 && pageIndex <= 65535);
StyleTag *styleTag = new StyleTag(
@@ -219,7 +271,7 @@ for (int pageIndex = 0; pageIndex < vPalette->getPageCount(); pageIndex++) {
// TColorStyle*style = tempVecImg->getPalette()->getStyle(styleId);
std::vector<TStyleParam> stream;
PliOutputStream chan(&stream);
- style->save(chan); // viene riempito lo stream;
+ saveStyleWithTrailCycle(style, chan);
assert(pageIndex >= 0 && pageIndex <= 65535);
StyleTag *styleTag =
@@ -484,11 +536,12 @@ static void putStroke(TStroke *stroke, int &currStyleId,
tags.push_back(colorTag.release());
}
- // If the outline options are non-standard (not round), add the outline infos
+ // If the outline options are non-standard, add the outline infos.
TStroke::OutlineOptions &options = stroke->outlineOptions();
if (options.m_capStyle != TStroke::OutlineOptions::ROUND_CAP ||
options.m_joinStyle != TStroke::OutlineOptions::ROUND_JOIN ||
- options.m_miterLower != 0.0 || options.m_miterUpper != 4.0) {
+ options.m_miterLower != 0.0 || options.m_miterUpper != 4.0 ||
+ options.m_patternFrameOffset != 0 || options.m_patternFrameStep != 1) {
StrokeOutlineOptionsTag *outlineOptionsTag =
new StrokeOutlineOptionsTag(options);
tags.push_back((PliObjectTag *)outlineOptionsTag);
@@ -764,12 +817,17 @@ TPalette *readPalette(GroupTag *paletteTag, int majorVersion,
PliInputStream chan(&params, majorVersion, minorVersion);
TColorStyle *style = TColorStyle::load(chan); // leggo params
+ chan.readTrailCycle(style);
assert(id > 0);
if (id < palette->getStyleCount()) {
if (frame > -1) {
TColorStyle *oldStyle = palette->getStyle(id);
+ const auto trailMode = TrailStyles::getMode(oldStyle);
+ const int trailOffset = TrailStyles::getFrameOffset(oldStyle);
oldStyle->copy(*style);
palette->setKeyframe(id, frame);
+ TrailStyles::setMode(oldStyle, trailMode);
+ TrailStyles::setFrameOffset(oldStyle, trailOffset);
} else
palette->setStyle(id, style);
} else {
diff --git a/toonz/sources/include/toonzqt/styleeditor.h b/toonz/sources/include/toonzqt/styleeditor.h
index 08e57dc33..696c563fe 100644
--- a/toonz/sources/include/toonzqt/styleeditor.h
+++ b/toonz/sources/include/toonzqt/styleeditor.h
@@ -639,15 +639,7 @@ signals:
class CustomStyleChooserPage final : public StyleChooserPage {
public:
- CustomStyleChooserPage(StyleEditor *styleEditor, QWidget *parent = 0)
- : StyleChooserPage(styleEditor, parent) {
- static const QString filters(
- "*.pli *.tif *.png *.tga *.tiff *.sgi *.rgb *.pct *.pic *.exr");
- static CustomStyleManager theManager(
- "RasterImagePatternStrokeStyle:", "VectorImagePatternStrokeStyle:",
- TFilePath("custom styles"), filters, m_chipSize);
- m_manager = &theManager;
- }
+ CustomStyleChooserPage(StyleEditor *styleEditor, QWidget *parent = 0);
void showEvent(QShowEvent *) override {
connect(m_manager, SIGNAL(patternAdded()), this, SLOT(patternAdded()));
diff --git a/toonz/sources/include/trailcycle.h b/toonz/sources/include/trailcycle.h
new file mode 100644
index 000000000..38412d79b
--- /dev/null
+++ b/toonz/sources/include/trailcycle.h
@@ -0,0 +1,14 @@
+#pragma once
+
+namespace TrailCycle {
+
+// Persisted numeric values. This is a style's drawing default, not a cursor.
+enum class Mode { Off = 0, Forward = 1, Backward = 2, Repeat = 3 };
+
+inline Mode modeFromValue(int value) {
+ return value >= int(Mode::Off) && value <= int(Mode::Repeat)
+ ? static_cast<Mode>(value)
+ : Mode::Off;
+}
+
+} // namespace TrailCycle
diff --git a/toonz/sources/include/trailscale.h b/toonz/sources/include/trailscale.h
new file mode 100644
index 000000000..524206a3f
--- /dev/null
+++ b/toonz/sources/include/trailscale.h
@@ -0,0 +1,30 @@
+#pragma once
+
+#ifndef TRAILSCALE_H
+#define TRAILSCALE_H
+
+#include <algorithm>
+#include <cmath>
+
+namespace TrailScale {
+constexpr double minimum = 0.25;
+constexpr double maximum = 10.0;
+constexpr double normal = 1.0;
+
+// Reject nonfinite metadata before it reaches transforms or drawing bounds.
+inline double normalized(double value) {
+ return std::isfinite(value) ? (std::max)(minimum, (std::min)(maximum, value))
+ : normal;
+}
+
+inline double apply(double scale, double multiplier) {
+ return scale * normalized(multiplier);
+}
+
+// Circumscribed radius of a centered source rectangle, valid at any Rotation.
+inline double radius(double halfWidth, double halfHeight, double scale) {
+ return std::hypot(halfWidth, halfHeight) * std::abs(scale);
+}
+} // namespace TrailScale
+
+#endif // TRAILSCALE_H
diff --git a/toonz/sources/include/tsimplecolorstyles.h b/toonz/sources/include/tsimplecolorstyles.h
index d3b7f6d94..2eb428d10 100644
--- a/toonz/sources/include/tsimplecolorstyles.h
+++ b/toonz/sources/include/tsimplecolorstyles.h
@@ -8,6 +8,7 @@
#include "tlevel.h"
#include "traster.h"
#include "tstrokeoutline.h"
+#include "trailcycle.h"
// Qt includes
#include <QCoreApplication>
@@ -250,6 +251,10 @@ protected:
TLevelP m_level;
std::string m_name;
double m_space, m_rotation;
+ TrailCycle::Mode m_trailCycle = TrailCycle::Mode::Off;
+ int m_trailFrameOffset = 0;
+ double m_trailSizeMultiplier = 1.0;
+ bool m_toonzRasterSource = false;
public:
TRasterImagePatternStrokeStyle();
@@ -258,7 +263,7 @@ public:
bool isRegionStyle() const override { return false; }
bool isStrokeStyle() const override { return true; }
- int getLevelFrameCount() { return m_level->getFrameCount(); }
+ int getLevelFrameCount() const { return m_level->getFrameCount(); }
void computeTransformations(std::vector<TAffine> &positions,
const TStroke *stroke) const;
@@ -269,6 +274,7 @@ public:
void invalidate(){};
TColorStyle *clone() const override;
+ TColorStyle &copy(const TColorStyle &other) override;
TColorStyle *clone(std::string brushIdName) const override;
QString getDescription() const override;
@@ -293,13 +299,34 @@ public:
m_rootDir = path + "custom styles";
}
+ TrailCycle::Mode getTrailCycleMode() const { return m_trailCycle; }
+ void setTrailCycleMode(TrailCycle::Mode mode) {
+ m_trailCycle = TrailCycle::modeFromValue(int(mode));
+ }
+
+ // 0 keeps the existing cycle; positive values select an exact source frame.
+ int getTrailFrameOffset() const { return m_trailFrameOffset; }
+ void setTrailFrameOffset(int frame) {
+ m_trailFrameOffset = frame > 0 ? frame : 0;
+ }
+ // Convert the requested drawing number to the renderer's zero-based index.
+ // Missing drawings (including gaps and letter-only variants) return -1.
+ int getTrailStartFrameIndex() const;
+
+ double getTrailSizeMultiplier() const { return m_trailSizeMultiplier; }
+ void setTrailSizeMultiplier(double multiplier);
+
int getParamCount() const override;
TColorStyle::ParamType getParamType(int index) const override;
+ void getParamRange(int index, int &min, int &max) const override;
QString getParamNames(int index) const override;
void getParamRange(int index, double &min, double &max) const override;
double getParamValue(TColorStyle::double_tag, int index) const override;
void setParamValue(int index, double value) override;
+ void getParamRange(int index, QStringList &items) const override;
+ int getParamValue(TColorStyle::int_tag, int index) const override;
+ void setParamValue(int index, int value) override;
TRectD getStrokeBBox(const TStroke *stroke) const override;
@@ -328,6 +355,9 @@ protected:
TLevelP m_level;
std::string m_name;
double m_space, m_rotation;
+ TrailCycle::Mode m_trailCycle = TrailCycle::Mode::Off;
+ int m_trailFrameOffset = 0;
+ double m_trailSizeMultiplier = 1.0;
public:
TVectorImagePatternStrokeStyle();
@@ -336,7 +366,7 @@ public:
bool isRegionStyle() const override { return false; }
bool isStrokeStyle() const override { return true; }
- int getLevelFrameCount() { return m_level->getFrameCount(); }
+ int getLevelFrameCount() const { return m_level->getFrameCount(); }
void computeTransformations(std::vector<TAffine> &positions,
const TStroke *stroke) const;
@@ -347,6 +377,7 @@ public:
void invalidate(){};
TColorStyle *clone() const override;
+ TColorStyle &copy(const TColorStyle &other) override;
TColorStyle *clone(std::string brushIdName) const override;
QString getDescription() const override;
@@ -371,13 +402,34 @@ public:
m_rootDir = path + "custom styles";
}
+ TrailCycle::Mode getTrailCycleMode() const { return m_trailCycle; }
+ void setTrailCycleMode(TrailCycle::Mode mode) {
+ m_trailCycle = TrailCycle::modeFromValue(int(mode));
+ }
+
+ // 0 keeps the existing cycle; positive values select an exact source frame.
+ int getTrailFrameOffset() const { return m_trailFrameOffset; }
+ void setTrailFrameOffset(int frame) {
+ m_trailFrameOffset = frame > 0 ? frame : 0;
+ }
+ // Convert the requested drawing number to the renderer's zero-based index.
+ // Missing drawings (including gaps and letter-only variants) return -1.
+ int getTrailStartFrameIndex() const;
+
+ double getTrailSizeMultiplier() const { return m_trailSizeMultiplier; }
+ void setTrailSizeMultiplier(double multiplier);
+
int getParamCount() const override;
TColorStyle::ParamType getParamType(int index) const override;
+ void getParamRange(int index, int &min, int &max) const override;
QString getParamNames(int index) const override;
void getParamRange(int index, double &min, double &max) const override;
double getParamValue(TColorStyle::double_tag, int index) const override;
void setParamValue(int index, double value) override;
+ void getParamRange(int index, QStringList &items) const override;
+ int getParamValue(TColorStyle::int_tag, int index) const override;
+ void setParamValue(int index, int value) override;
static void clearGlDisplayLists();
@@ -397,4 +449,86 @@ private:
const TVectorImagePatternStrokeStyle &);
};
+// Shared access for the Settings page, palette persistence and the Brush.
+namespace TrailStyles {
+
+// Used by both the Trail chooser and its source loader. Raster conversion keeps
+// the full canvas and returns premultiplied pixels for Toonz Raster images.
+DVAPI QString sourceFilters();
+DVAPI TFilePath findSource(const TFilePath &root, const std::string &name);
+DVAPI TRaster32P rasterSource(const TImageP &image, TPalette *levelPalette,
+ int sourceFrame = -1);
+
+constexpr int cycleParam = 2;
+constexpr int frameOffsetParam = 3;
+constexpr int sizeMultiplierParam = 4;
+
+inline bool isTrail(const TColorStyle *style) {
+ return dynamic_cast<const TRasterImagePatternStrokeStyle *>(style) ||
+ dynamic_cast<const TVectorImagePatternStrokeStyle *>(style);
+}
+
+inline TrailCycle::Mode getMode(const TColorStyle *style) {
+ if (auto *trail = dynamic_cast<const TRasterImagePatternStrokeStyle *>(style))
+ return trail->getTrailCycleMode();
+ if (auto *trail = dynamic_cast<const TVectorImagePatternStrokeStyle *>(style))
+ return trail->getTrailCycleMode();
+ return TrailCycle::Mode::Off;
+}
+
+inline void setMode(TColorStyle *style, TrailCycle::Mode mode) {
+ if (auto *trail = dynamic_cast<TRasterImagePatternStrokeStyle *>(style))
+ trail->setTrailCycleMode(mode);
+ else if (auto *trail = dynamic_cast<TVectorImagePatternStrokeStyle *>(style))
+ trail->setTrailCycleMode(mode);
+}
+
+inline int getFrameOffset(const TColorStyle *style) {
+ if (auto *trail = dynamic_cast<const TRasterImagePatternStrokeStyle *>(style))
+ return trail->getTrailFrameOffset();
+ if (auto *trail = dynamic_cast<const TVectorImagePatternStrokeStyle *>(style))
+ return trail->getTrailFrameOffset();
+ return 0;
+}
+
+inline void setFrameOffset(TColorStyle *style, int frame) {
+ if (auto *trail = dynamic_cast<TRasterImagePatternStrokeStyle *>(style))
+ trail->setTrailFrameOffset(frame);
+ else if (auto *trail = dynamic_cast<TVectorImagePatternStrokeStyle *>(style))
+ trail->setTrailFrameOffset(frame);
+}
+
+inline double getSizeMultiplier(const TColorStyle *style) {
+ if (auto *trail = dynamic_cast<const TRasterImagePatternStrokeStyle *>(style))
+ return trail->getTrailSizeMultiplier();
+ if (auto *trail = dynamic_cast<const TVectorImagePatternStrokeStyle *>(style))
+ return trail->getTrailSizeMultiplier();
+ return 1.0;
+}
+
+inline void setSizeMultiplier(TColorStyle *style, double multiplier) {
+ if (auto *trail = dynamic_cast<TRasterImagePatternStrokeStyle *>(style))
+ trail->setTrailSizeMultiplier(multiplier);
+ else if (auto *trail = dynamic_cast<TVectorImagePatternStrokeStyle *>(style))
+ trail->setTrailSizeMultiplier(multiplier);
+}
+
+inline int startFrameIndex(const TColorStyle *style) {
+ if (auto *trail = dynamic_cast<const TRasterImagePatternStrokeStyle *>(style))
+ return trail->getTrailStartFrameIndex();
+ if (auto *trail = dynamic_cast<const TVectorImagePatternStrokeStyle *>(style))
+ return trail->getTrailStartFrameIndex();
+ return -1;
+}
+
+inline int frameCount(const TColorStyle *style) {
+ if (auto *trail = dynamic_cast<const TRasterImagePatternStrokeStyle *>(style))
+ return trail->getLevelFrameCount();
+ if (auto *trail = dynamic_cast<const TVectorImagePatternStrokeStyle *>(style))
+ return trail->getLevelFrameCount();
+ return 0;
+}
+
+} // namespace TrailStyles
+
#endif // TSIMPLECOLORSTYLES_H
diff --git a/toonz/sources/include/tstroke.h b/toonz/sources/include/tstroke.h
index 9e9bed8a9..8a2f24a64 100644
--- a/toonz/sources/include/tstroke.h
+++ b/toonz/sources/include/tstroke.h
@@ -441,9 +441,16 @@ struct DVAPI TStroke::OutlineOptions {
double m_miterLower;
double m_miterUpper;
+ // Pattern stroke styles use these values to select the first frame and
+ // direction of their source level. Keeping them on the stroke makes a
+ // pattern's appearance independent from the order in which strokes render.
+ int m_patternFrameOffset;
+ int m_patternFrameStep;
+
public:
OutlineOptions();
- OutlineOptions(UCHAR capStyle, UCHAR JoinStyle, double lower, double upper);
+ OutlineOptions(UCHAR capStyle, UCHAR JoinStyle, double lower, double upper,
+ int patternFrameOffset = 0, int patternFrameStep = 1);
};
//***************************************************************************
diff --git a/toonz/sources/tnztools/toonzvectorbrushtool.cpp b/toonz/sources/tnztools/toonzvectorbrushtool.cpp
index d8c6d5334..6712ebba1 100644
--- a/toonz/sources/tnztools/toonzvectorbrushtool.cpp
+++ b/toonz/sources/tnztools/toonzvectorbrushtool.cpp
@@ -1,6 +1,7 @@
#include "toonzvectorbrushtool.h"
+#include "trailstamp.h"
// TnzTools includes
#include "tools/toolhandle.h"
@@ -1026,7 +1027,10 @@ void ToonzVectorBrushTool::inputSetBusy(bool busy) {
m_styleId = 0;
m_bypassAutoClose = m_inputmanager.state.isKeyPressed(TKey::shift);
m_tracks.clear();
-
+ m_trailSelection = TrailCycle::Selection();
+ m_trailCanStamp = false;
+ m_trailGesturePalette = TPaletteP();
+
TTool::Application *app = TTool::getApplication();
if (!app)
return;
@@ -1056,17 +1060,36 @@ void ToonzVectorBrushTool::inputSetBusy(bool busy) {
m_styleId = app->getCurrentLevelStyleIndex();
m_currentColor = cs->getAverageColor();
m_currentColor.m = 255;
+
+ int trailFrameCount = 0;
+ if (TVectorImagePatternStrokeStyle *trailStyle =
+ dynamic_cast<TVectorImagePatternStrokeStyle *>(cs))
+ trailFrameCount = trailStyle->getLevelFrameCount();
+ else if (TRasterImagePatternStrokeStyle *trailStyle =
+ dynamic_cast<TRasterImagePatternStrokeStyle *>(cs))
+ trailFrameCount = trailStyle->getLevelFrameCount();
+ m_trailCanStamp = trailFrameCount > 0;
+ TVectorImageP trailImage = getImage(true);
+ if (!trailImage || !trailImage->getPalette()) return;
+ m_trailGesturePalette = trailImage->getPalette();
+ const TrailCycle::StyleKey key{m_trailGesturePalette.getPointer(),
+ m_styleId, cs->getBrushIdName()};
+ const auto mode = m_frameRange.getIndex() ? TrailCycle::Mode::Off
+ : TrailStyles::getMode(cs);
+ const int startOffset =
+ m_frameRange.getIndex() ? -1 : TrailStyles::startFrameIndex(cs);
+ m_trailSelection =
+ m_trailState.begin(mode, key, trailFrameCount, startOffset);
} else {
- m_styleId = 1;
+ m_styleId = 1;
m_currentColor = TPixel32::Black;
}
-
+
m_active = true;
-
- return; // painting has begun
+
+ return; // painting has begun
}
-
-
+
// end painting //////////////////////////
m_active = false;
@@ -1074,7 +1097,13 @@ void ToonzVectorBrushTool::inputSetBusy(bool busy) {
// clear tracks automatically when return from this function
struct Cleanup {
ToonzVectorBrushTool &owner;
- inline ~Cleanup() { owner.m_tracks.clear(); owner.invalidate(); }
+ inline ~Cleanup() {
+ owner.m_tracks.clear();
+ owner.m_trailSelection = TrailCycle::Selection();
+ owner.m_trailCanStamp = false;
+ owner.m_trailGesturePalette = TPaletteP();
+ owner.invalidate();
+ }
} cleanup = {*this};
// remove empty tracks
@@ -1136,13 +1165,34 @@ void ToonzVectorBrushTool::inputSetBusy(bool busy) {
track.filterPoints();
double error = 30.0/(1 + 0.5 * m_accuracy.getValue())*m_pixelSize;
TStroke *stroke = track.makeStroke(error);
-
+ bool isClickStamp = false;
+
+ // Promote only newly drawn point-like Trails, not ordinary brush dots,
+ // motion paths, Frame Range endpoints, or any nonzero-length drag.
+ if (m_trailCanStamp && !m_frameRange.getIndex() &&
+ stroke->getLength() == 0.0) {
+ std::vector<TThickPoint> points;
+ stroke->getControlPoints(points);
+ std::array<TThickPoint, 5> carrier;
+ if (TrailStamp::makeClickCarrier(points, carrier)) {
+ stroke->reshape(carrier.data(), int(carrier.size()));
+ // Snap can flag a stationary click as self-snapped. The carrier must
+ // remain open; closing it would remove its directional extent.
+ track.setLoop(false);
+ isClickStamp = true;
+ }
+ }
+
stroke->setStyle(m_styleId);
TStroke::OutlineOptions &options = stroke->outlineOptions();
options.m_capStyle = m_capStyle.getIndex();
options.m_joinStyle = m_joinStyle.getIndex();
options.m_miterUpper = m_miterJoinLimit.getValue();
+ if (m_trailSelection.active) {
+ options.m_patternFrameOffset = m_trailSelection.offset;
+ options.m_patternFrameStep = m_trailSelection.step;
+ }
if ( stroke->getControlPointCount() == 3
&& stroke->getControlPoint(0) != stroke->getControlPoint(2) )
@@ -1153,9 +1203,12 @@ void ToonzVectorBrushTool::inputSetBusy(bool busy) {
// hanno 1 solo chunk.
stroke->insertControlPoints(0.5);
- if ((!m_frameRange.getIndex() || autoCloseEnabled()) && track.getLoop())
- stroke->setSelfLoop(true);
- if (autoCloseEnabled()) closeOpenStroke(stroke);
+ // Keep the single-click carrier open even when Auto Close is enabled.
+ if (!isClickStamp) {
+ if ((!m_frameRange.getIndex() || autoCloseEnabled()) && track.getLoop())
+ stroke->setSelfLoop(true);
+ if (autoCloseEnabled()) closeOpenStroke(stroke);
+ }
strokes.push_back(stroke);
}
@@ -1233,12 +1286,15 @@ void ToonzVectorBrushTool::inputSetBusy(bool busy) {
} else {
// regular paint strokes
TUndoManager::manager()->beginBlock();
+ bool strokeCommitted = false;
for(StrokeList::iterator i = strokes.begin(); i != strokes.end(); ++i) {
TStroke *stroke = *i;
+ const int previousCount = vi->getStrokeCount();
addStrokeToImage(app, vi, stroke, (DrawOrder)m_drawOrder.getIndex(),
m_breakAngles.getValue(), m_autoGroup.getValue(),
m_autoFill.getValue() && !m_bypassAutoClose,
m_isFrameCreated, m_isLevelCreated);
+ strokeCommitted = strokeCommitted || vi->getStrokeCount() > previousCount;
if ((Preferences::instance()->getGuidedDrawingType() == 1 ||
Preferences::instance()->getGuidedDrawingType() == 2) &&
@@ -1255,6 +1311,11 @@ void ToonzVectorBrushTool::inputSetBusy(bool busy) {
}
}
TUndoManager::manager()->endBlock();
+
+ if (m_trailSelection.updatesCursor && strokeCommitted) {
+ m_trailPalette = m_trailGesturePalette;
+ m_trailState.commit(m_trailSelection, true);
+ }
}
deleteStrokes(strokes);
diff --git a/toonz/sources/tnztools/toonzvectorbrushtool.h b/toonz/sources/tnztools/toonzvectorbrushtool.h
index 3700876ca..c0d970e76 100644
--- a/toonz/sources/tnztools/toonzvectorbrushtool.h
+++ b/toonz/sources/tnztools/toonzvectorbrushtool.h
@@ -8,6 +8,8 @@
#include <trasterimage.h>
#include <ttoonzimage.h>
#include <tstroke.h>
+#include <tpalette.h>
+#include "trailcyclestate.h"
#include <toonz/strokegenerator.h>
#include "toonz/preferences.h"
#include <tools/tool.h>
@@ -251,6 +253,11 @@ protected:
TFrameId m_firstFrameId, m_veryFirstFrameId;
TPixel32 m_currentColor;
int m_styleId;
+ TrailCycle::State m_trailState;
+ TrailCycle::Selection m_trailSelection;
+ bool m_trailCanStamp = false;
+ TPaletteP m_trailPalette;
+ TPaletteP m_trailGesturePalette;
double m_minThick, m_maxThick;
// for snapping and framerange
diff --git a/toonz/sources/tnztools/trailcyclestate.h b/toonz/sources/tnztools/trailcyclestate.h
new file mode 100644
index 000000000..73f3b31f6
--- /dev/null
+++ b/toonz/sources/tnztools/trailcyclestate.h
@@ -0,0 +1,77 @@
+#pragma once
+
+#include "trailcycle.h"
+
+#include <string>
+#include <tuple>
+
+namespace TrailCycle {
+
+struct StyleKey {
+ // The tool owns the palette lifetime.
+ const void *palette = nullptr;
+ int styleId = -1;
+ std::string resource;
+
+ bool operator==(const StyleKey &other) const {
+ return std::tie(palette, styleId, resource) ==
+ std::tie(other.palette, other.styleId, other.resource);
+ }
+};
+
+struct Selection {
+ StyleKey style;
+ int frameCount = 0;
+ int offset = 0;
+ int step = 1;
+ bool active = false;
+ // Off and single-frame selections leave the shared cursor unchanged.
+ bool updatesCursor = false;
+ int startOffset = -1;
+};
+
+class State {
+public:
+ // startOffset is a source index; -1 preserves the current cycle.
+ Selection begin(Mode mode, const StyleKey &style, int frameCount,
+ int startOffset = -1) const {
+ Selection selection;
+ const bool explicitStart = startOffset >= 0 && startOffset < frameCount;
+ if (frameCount < 1 ||
+ ((mode == Mode::Off || frameCount < 2) && !explicitStart))
+ return selection;
+ selection.style = style;
+ selection.frameCount = frameCount;
+ selection.active = true;
+ selection.updatesCursor = mode != Mode::Off && frameCount > 1;
+ if (mode == Mode::Backward)
+ selection.step = -1;
+ else if (mode == Mode::Repeat)
+ selection.step = 0;
+ const bool sameCycle = m_last.active && m_last.style == style &&
+ m_last.frameCount == frameCount;
+ selection.startOffset =
+ explicitStart ? startOffset : (sameCycle ? m_last.startOffset : -1);
+ if (!sameCycle || mode == Mode::Off ||
+ (explicitStart &&
+ (mode == Mode::Repeat || startOffset != m_last.startOffset))) {
+ selection.offset = explicitStart
+ ? startOffset
+ : (selection.step < 0 ? frameCount - 1 : 0);
+ } else {
+ selection.offset = (m_last.offset + selection.step) % frameCount;
+ if (selection.offset < 0) selection.offset += frameCount;
+ }
+ return selection;
+ }
+
+ void commit(const Selection &selection, bool strokeCommitted) {
+ if (selection.active && selection.updatesCursor && strokeCommitted)
+ m_last = selection;
+ }
+
+private:
+ Selection m_last;
+};
+
+} // namespace TrailCycle
diff --git a/toonz/sources/tnztools/trailstamp.h b/toonz/sources/tnztools/trailstamp.h
new file mode 100644
index 000000000..188a23b18
--- /dev/null
+++ b/toonz/sources/tnztools/trailstamp.h
@@ -0,0 +1,33 @@
+#pragma once
+
+#include "tgeometry.h"
+
+#include <array>
+#include <cmath>
+#include <vector>
+
+namespace TrailStamp {
+
+// Two straight quadratics preserve the click anchor and style rotation.
+// One unit survives PLI quantization; minimum stamp spacing is two.
+inline bool makeClickCarrier(const std::vector<TThickPoint> &points,
+ std::array<TThickPoint, 5> &carrier) {
+ if (points.empty()) return false;
+ const TThickPoint &first = points.front();
+ double thickness = 0.0;
+ for (const TThickPoint &point : points) {
+ // Reject loops and drags, however small.
+ if (!std::isfinite(point.x) || !std::isfinite(point.y) ||
+ !std::isfinite(point.thick) || point.x != first.x || point.y != first.y)
+ return false;
+ // Keep peak tap pressure, not the zero-pressure release.
+ thickness = std::max(thickness, point.thick);
+ }
+ if (thickness <= 0.0 || first.x + 0.25 == first.x) return false;
+
+ for (size_t i = 0; i < carrier.size(); ++i)
+ carrier[i] = TThickPoint(first.x + 0.25 * i, first.y, thickness);
+ return true;
+}
+
+} // namespace TrailStamp
diff --git a/toonz/sources/toonzlib/stylemanager.cpp b/toonz/sources/toonzlib/stylemanager.cpp
index c0520c8fb..30429b45b 100644
--- a/toonz/sources/toonzlib/stylemanager.cpp
+++ b/toonz/sources/toonzlib/stylemanager.cpp
@@ -10,6 +10,7 @@
#include "tsystem.h"
#include "tvectorgl.h"
#include "tcolorstyles.h"
+#include "tsimplecolorstyles.h"
// TnzCore includes
#include "tfiletype.h"
@@ -59,6 +60,7 @@ QImage rasterToQImage(const TRasterP &ras, bool premultiplied = true,
bool mirrored = true) {
if (TRaster32P ras32 = ras) {
QImage image(ras->getRawData(), ras->getLx(), ras->getLy(),
+ ras->getWrap() * sizeof(TPixel32),
premultiplied ? QImage::Format_ARGB32_Premultiplied
: QImage::Format_ARGB32);
if (mirrored) return image.mirrored();
@@ -337,7 +339,9 @@ void CustomStyleManager::loadItems() {
QDir patternDir(
QString::fromStdWString((rootFP + m_stylesFolder).getWideString()));
- patternDir.setNameFilters(m_filters.split(' '));
+ // Apply QDir's case-insensitive wildcard matching below. TSystem's Windows
+ // directory reader otherwise treats lowercase name filters case-sensitively.
+ patternDir.setFilter(QDir::Files);
// Read the said folder
TFilePathSet fps;
@@ -347,6 +351,15 @@ void CustomStyleManager::loadItems() {
return;
}
+ const QStringList filters = m_filters.split(' ', Qt::SkipEmptyParts);
+ for (auto it = fps.begin(); it != fps.end();) {
+ if (!filters.isEmpty() &&
+ !QDir::match(filters, QString::fromStdWString(it->getLevelNameW())))
+ it = fps.erase(it);
+ else
+ ++it;
+ }
+
// Delete patterns no longer in the folder
TFilePathSet newFps;
TFilePathSet::iterator it;
@@ -375,6 +388,9 @@ QImage CustomStyleManager::makeIcon(
const TFilePath &path, const QSize &qChipSize,
std::shared_ptr<QOffscreenSurface> offsurf) {
try {
+ if (path.getType() == "tlv" &&
+ !TFileStatus(path.withNoFrame().withType("tpl")).doesExist())
+ return QImage();
// Fetch the level
TLevelReaderP lr(path);
TLevelP level = lr->loadInfo();
@@ -389,7 +405,8 @@ QImage CustomStyleManager::makeIcon(
TDimension chipSize(qChipSize.width(), qChipSize.height());
TVectorImageP vimg = img;
- TRasterImageP rimg = img;
+ TRaster32P source = TrailStyles::rasterSource(
+ img, level->getPalette(), frameIt->first.getNumber() - 1);
TRaster32P ras;
@@ -457,14 +474,14 @@ QImage CustomStyleManager::makeIcon(
fb.release();
glContext->deleteLater();
#endif
- } else if (rimg) {
- TDimension size = rimg->getRaster()->getSize();
+ } else if (source) {
+ TDimension size = source->getSize();
if (size == chipSize)
- ras = rimg->getRaster()->clone(); // Yep, this may be necessary
+ ras = source->clone(); // The QImage must outlive the decoded frame.
else {
TRaster32P rout(chipSize);
- TRop::resample(rout, rimg->getRaster(),
+ TRop::resample(rout, source,
TScale((double)chipSize.lx / size.lx,
(double)chipSize.ly / size.ly));
@@ -477,7 +494,7 @@ QImage CustomStyleManager::makeIcon(
image = rasterToQImage(ras);
#endif
} else
- assert(!"unsupported type for custom styles!");
+ return QImage(); // Unsupported or invalid source: no debug assertion.
#ifdef LINUX
// image = QImage(chipSize.lx, chipSize.ly, QImage::Format_RGB32);
diff --git a/toonz/sources/toonzqt/styleeditor.cpp b/toonz/sources/toonzqt/styleeditor.cpp
index 36d82e47c..8c41b9fe9 100644
--- a/toonz/sources/toonzqt/styleeditor.cpp
+++ b/toonz/sources/toonzqt/styleeditor.cpp
@@ -2154,6 +2154,17 @@ void StyleChooserPage::setRootPath(const TFilePath &rootPath) {
// CustomStyleChooser implementation
//*****************************************************************************
+CustomStyleChooserPage::CustomStyleChooserPage(StyleEditor *styleEditor,
+ QWidget *parent)
+ : StyleChooserPage(styleEditor, parent) {
+ static CustomStyleManager theManager(
+ "RasterImagePatternStrokeStyle:", "VectorImagePatternStrokeStyle:",
+ TFilePath("custom styles"), TrailStyles::sourceFilters(), m_chipSize);
+ m_manager = &theManager;
+}
+
+//-----------------------------------------------------------------------------
+
int CustomStyleChooserPage::drawChip(QPainter &p, QRect rect, int index) {
assert(0 <= index && index < getChipCount());
auto &data = m_manager->getData(index);
@@ -2632,6 +2643,23 @@ void SettingsPage::setStyle(const TColorStyleP &editedStyle) {
m_editedStyle->getParamRange(p, min, max);
intField->setRange(min, max);
+ if (p == TrailStyles::frameOffsetParam &&
+ TrailStyles::isTrail(m_editedStyle.getPointer())) {
+ intField->setObjectName("trailFrameOffset");
+ // Drawing IDs can be sparse or start well above the frame count.
+ // Keep direct numeric entry; a count-limited slider would be
+ // misleading.
+ intField->enableSlider(false);
+ intField->enableRoller(true);
+ const QString help =
+ tr("Source drawing number: 0 keeps the automatic start or "
+ "current cycle. "
+ "A positive number starts at that drawing; Repeat holds it. "
+ "Nonexistent drawing numbers are ignored, not wrapped. "
+ "Affects new strokes only; Frame Range ignores this setting.");
+ intField->setToolTip(help);
+ label->setToolTip(help);
+ }
ret = QObject::connect(intField, SIGNAL(valueChanged(bool)), this,
SLOT(onValueChanged(bool))) &&
@@ -2642,6 +2670,9 @@ void SettingsPage::setStyle(const TColorStyleP &editedStyle) {
case TColorStyle::ENUM: {
QComboBox *comboBox = new QComboBox;
+ if (p == TrailStyles::cycleParam &&
+ TrailStyles::isTrail(m_editedStyle.getPointer()))
+ comboBox->setObjectName("trailCycleMode");
m_paramsLayout->addWidget(comboBox, p, 1);
QStringList items;
@@ -2770,6 +2801,27 @@ void SettingsPage::updateValues() {
comboBox->setCurrentIndex(
m_editedStyle->getParamValue(TColorStyle::int_tag(), p));
+ if (p == TrailStyles::cycleParam &&
+ TrailStyles::isTrail(m_editedStyle.getPointer())) {
+ const bool available =
+ TrailStyles::frameCount(m_editedStyle.getPointer()) > 1;
+ const QString help =
+ available
+ ? tr("Selects source frames for new Brush strokes. Existing "
+ "strokes keep "
+ "their recorded sequence. Frame Range drawing ignores "
+ "this setting. "
+ "Off starts each stroke normally; Repeat holds one source "
+ "frame.")
+ : tr("Trail Cycle requires a loaded multi-frame Trail source. "
+ "The saved mode is retained until the source is "
+ "available.");
+ comboBox->setEnabled(available);
+ comboBox->setToolTip(help);
+ QWidget *label = m_paramsLayout->itemAtPosition(p, 0)->widget();
+ label->setEnabled(available);
+ label->setToolTip(help);
+ }
break;
}