shun-iwasawa 98926d
shun-iwasawa 98926d
shun-iwasawa 98926d
#include "toonzvectorbrushtool.h"
shun-iwasawa 98926d
shun-iwasawa 98926d
// TnzTools includes
shun-iwasawa 98926d
#include "tools/toolhandle.h"
shun-iwasawa 98926d
#include "tools/toolutils.h"
shun-iwasawa 98926d
#include "tools/tooloptions.h"
da847a
#include "tools/replicator.h"
shun-iwasawa 98926d
#include "bluredbrush.h"
shun-iwasawa 98926d
shun-iwasawa 98926d
// TnzQt includes
shun-iwasawa 98926d
#include "toonzqt/dvdialog.h"
shun-iwasawa 98926d
#include "toonzqt/imageutils.h"
shun-iwasawa 98926d
shun-iwasawa 98926d
// TnzLib includes
shun-iwasawa 98926d
#include "toonz/tobjecthandle.h"
shun-iwasawa 98926d
#include "toonz/txsheethandle.h"
shun-iwasawa 98926d
#include "toonz/txshlevelhandle.h"
shun-iwasawa 98926d
#include "toonz/tframehandle.h"
shun-iwasawa 98926d
#include "toonz/tcolumnhandle.h"
shun-iwasawa 98926d
#include "toonz/txsheet.h"
shun-iwasawa 98926d
#include "toonz/tstageobject.h"
shun-iwasawa 98926d
#include "toonz/tstageobjectspline.h"
shun-iwasawa 98926d
#include "toonz/rasterstrokegenerator.h"
shun-iwasawa 98926d
#include "toonz/ttileset.h"
shun-iwasawa 98926d
#include "toonz/txshsimplelevel.h"
shun-iwasawa 98926d
#include "toonz/toonzimageutils.h"
shun-iwasawa 98926d
#include "toonz/palettecontroller.h"
shun-iwasawa 98926d
#include "toonz/stage2.h"
shun-iwasawa 98926d
#include "toonz/preferences.h"
manongjohn 40a40e
#include "toonz/tonionskinmaskhandle.h"
shun-iwasawa 98926d
shun-iwasawa 98926d
// TnzCore includes
shun-iwasawa 98926d
#include "tstream.h"
shun-iwasawa 98926d
#include "tcolorstyles.h"
shun-iwasawa 98926d
#include "tvectorimage.h"
shun-iwasawa 98926d
#include "tenv.h"
shun-iwasawa 98926d
#include "tregion.h"
shun-iwasawa 98926d
#include "tinbetween.h"
shun-iwasawa 98926d
shun-iwasawa 98926d
#include "tgl.h"
shun-iwasawa 98926d
#include "trop.h"
shun-iwasawa 98926d
shun-iwasawa 98926d
// Qt includes
shun-iwasawa 98926d
#include <qpainter></qpainter>
shun-iwasawa 98926d
shun-iwasawa 98926d
using namespace ToolUtils;
shun-iwasawa 98926d
shun-iwasawa 98926d
TEnv::DoubleVar V_VectorBrushMinSize("InknpaintVectorBrushMinSize", 1);
shun-iwasawa 98926d
TEnv::DoubleVar V_VectorBrushMaxSize("InknpaintVectorBrushMaxSize", 5);
shun-iwasawa 98926d
TEnv::IntVar V_VectorCapStyle("InknpaintVectorCapStyle", 1);
shun-iwasawa 98926d
TEnv::IntVar V_VectorJoinStyle("InknpaintVectorJoinStyle", 1);
shun-iwasawa 98926d
TEnv::IntVar V_VectorMiterValue("InknpaintVectorMiterValue", 4);
shun-iwasawa 98926d
TEnv::DoubleVar V_BrushAccuracy("InknpaintBrushAccuracy", 20);
shun-iwasawa 98926d
TEnv::DoubleVar V_BrushSmooth("InknpaintBrushSmooth", 0);
shun-iwasawa 98926d
TEnv::IntVar V_BrushBreakSharpAngles("InknpaintBrushBreakSharpAngles", 0);
shun-iwasawa 98926d
TEnv::IntVar V_BrushPressureSensitivity("InknpaintBrushPressureSensitivity", 1);
shun-iwasawa 98926d
TEnv::IntVar V_VectorBrushFrameRange("VectorBrushFrameRange", 0);
shun-iwasawa 98926d
TEnv::IntVar V_VectorBrushSnap("VectorBrushSnap", 0);
shun-iwasawa 98926d
TEnv::IntVar V_VectorBrushSnapSensitivity("VectorBrushSnapSensitivity", 0);
fa009d
TEnv::IntVar V_VectorBrushAssistants("VectorBrushAssistants", 1);
manongjohn df5842
TEnv::StringVar V_VectorBrushPreset("VectorBrushPreset", "<custom>");</custom>
shun-iwasawa 98926d
shun-iwasawa 98926d
//-------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
#define ROUNDC_WSTR L"round_cap"
shun-iwasawa 98926d
#define BUTT_WSTR L"butt_cap"
shun-iwasawa 98926d
#define PROJECTING_WSTR L"projecting_cap"
shun-iwasawa 98926d
#define ROUNDJ_WSTR L"round_join"
shun-iwasawa 98926d
#define BEVEL_WSTR L"bevel_join"
shun-iwasawa 98926d
#define MITER_WSTR L"miter_join"
shun-iwasawa 98926d
#define CUSTOM_WSTR L"<custom>"</custom>
shun-iwasawa 98926d
shun-iwasawa 98926d
#define LINEAR_WSTR L"Linear"
shun-iwasawa 98926d
#define EASEIN_WSTR L"In"
shun-iwasawa 98926d
#define EASEOUT_WSTR L"Out"
shun-iwasawa 98926d
#define EASEINOUT_WSTR L"In&Out"
shun-iwasawa 98926d
shun-iwasawa 98926d
#define LOW_WSTR L"Low"
shun-iwasawa 98926d
#define MEDIUM_WSTR L"Med"
shun-iwasawa 98926d
#define HIGH_WSTR L"High"
shun-iwasawa 98926d
shun-iwasawa 98926d
const double SNAPPING_LOW    = 5.0;
shun-iwasawa 98926d
const double SNAPPING_MEDIUM = 25.0;
shun-iwasawa 98926d
const double SNAPPING_HIGH   = 100.0;
shun-iwasawa 98926d
//-------------------------------------------------------------------
shun-iwasawa 98926d
//
shun-iwasawa 98926d
// (Da mettere in libreria) : funzioni che spezzano una stroke
shun-iwasawa 98926d
// nei suoi punti angolosi. Lo facciamo specialmente per limitare
shun-iwasawa 98926d
// i problemi di fill.
shun-iwasawa 98926d
//
shun-iwasawa 98926d
//-------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
//
shun-iwasawa 98926d
// Split a stroke in n+1 parts, according to n parameter values
shun-iwasawa 98926d
// Input:
shun-iwasawa 98926d
//      stroke            = stroke to split
shun-iwasawa 98926d
//      parameterValues[] = vector of parameters where I want to split the
shun-iwasawa 98926d
//      stroke
shun-iwasawa 98926d
//                          assert: 0
shun-iwasawa 98926d
// Output:
shun-iwasawa 98926d
//      strokes[]         = the split strokes
shun-iwasawa 98926d
//
shun-iwasawa 98926d
// note: stroke is unchanged
shun-iwasawa 98926d
//
shun-iwasawa 98926d
shun-iwasawa 98926d
static void split(TStroke *stroke, const std::vector<double> ¶meterValues,</double>
shun-iwasawa 98926d
                  std::vector<tstroke *=""> &strokes) {</tstroke>
shun-iwasawa 98926d
  TThickPoint p2;
shun-iwasawa 98926d
  std::vector<tthickpoint> points;</tthickpoint>
shun-iwasawa 98926d
  TThickPoint lastPoint = stroke->getControlPoint(0);
shun-iwasawa 98926d
  int n                 = parameterValues.size();
shun-iwasawa 98926d
  int chunk;
shun-iwasawa 98926d
  double t;
shun-iwasawa 98926d
  int last_chunk = -1, startPoint = 0;
shun-iwasawa 98926d
  double lastLocT = 0;
shun-iwasawa 98926d
shun-iwasawa 98926d
  for (int i = 0; i < n; i++) {
shun-iwasawa 98926d
    points.push_back(lastPoint);  // Add first point of the stroke
shun-iwasawa 98926d
    double w =
shun-iwasawa 98926d
        parameterValues[i];  // Global parameter. along the stroke 0<=w<=1
shun-iwasawa 98926d
    stroke->getChunkAndT(w, chunk,
shun-iwasawa 98926d
                         t);  // t: local parameter in the chunk-th quadratic
shun-iwasawa 98926d
shun-iwasawa 98926d
    if (i == 0)
shun-iwasawa 98926d
      startPoint = 1;
shun-iwasawa 98926d
    else {
shun-iwasawa 98926d
      int indexAfterLastT =
shun-iwasawa 98926d
          stroke->getControlPointIndexAfterParameter(parameterValues[i - 1]);
shun-iwasawa 98926d
      startPoint = indexAfterLastT;
shun-iwasawa 98926d
      if ((indexAfterLastT & 1) && lastLocT != 1) startPoint++;
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
    int endPoint = 2 * chunk + 1;
shun-iwasawa 98926d
    if (lastLocT != 1 && i > 0) {
shun-iwasawa 98926d
      if (last_chunk != chunk || t == 1)
shun-iwasawa 98926d
        points.push_back(p2);  // If the last local t is not an extreme
shun-iwasawa 98926d
                               // add the point p2
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
shun-iwasawa 98926d
    for (int j = startPoint; j < endPoint; j++)
shun-iwasawa 98926d
      points.push_back(stroke->getControlPoint(j));
shun-iwasawa 98926d
shun-iwasawa 98926d
    TThickPoint p, A, B, C;
shun-iwasawa 98926d
    p       = stroke->getPoint(w);
shun-iwasawa 98926d
    C       = stroke->getControlPoint(2 * chunk + 2);
shun-iwasawa 98926d
    B       = stroke->getControlPoint(2 * chunk + 1);
shun-iwasawa 98926d
    A       = stroke->getControlPoint(2 * chunk);
shun-iwasawa 98926d
    p.thick = A.thick;
shun-iwasawa 98926d
shun-iwasawa 98926d
    if (last_chunk != chunk) {
shun-iwasawa 98926d
      TThickPoint p1 = (1 - t) * A + t * B;
shun-iwasawa 98926d
      points.push_back(p1);
shun-iwasawa 98926d
      p.thick = p1.thick;
shun-iwasawa 98926d
    } else {
shun-iwasawa 98926d
      if (t != 1) {
shun-iwasawa 98926d
        // If the i-th cut point belong to the same chunk of the (i-1)-th cut
shun-iwasawa 98926d
        // point.
shun-iwasawa 98926d
        double tInters  = lastLocT / t;
shun-iwasawa 98926d
        TThickPoint p11 = (1 - t) * A + t * B;
shun-iwasawa 98926d
        TThickPoint p1  = (1 - tInters) * p11 + tInters * p;
shun-iwasawa 98926d
        points.push_back(p1);
shun-iwasawa 98926d
        p.thick = p1.thick;
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
shun-iwasawa 98926d
    points.push_back(p);
shun-iwasawa 98926d
shun-iwasawa 98926d
    if (t != 1) p2 = (1 - t) * B + t * C;
shun-iwasawa 98926d
shun-iwasawa 98926d
    assert(points.size() & 1);
shun-iwasawa 98926d
shun-iwasawa 98926d
    // Add new stroke
shun-iwasawa 98926d
    TStroke *strokeAdd = new TStroke(points);
shun-iwasawa 98926d
    strokeAdd->setStyle(stroke->getStyle());
shun-iwasawa 98926d
    strokeAdd->outlineOptions() = stroke->outlineOptions();
shun-iwasawa 98926d
    strokes.push_back(strokeAdd);
shun-iwasawa 98926d
shun-iwasawa 98926d
    lastPoint  = p;
shun-iwasawa 98926d
    last_chunk = chunk;
shun-iwasawa 98926d
    lastLocT   = t;
shun-iwasawa 98926d
    points.clear();
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
  // Add end stroke
shun-iwasawa 98926d
  points.push_back(lastPoint);
shun-iwasawa 98926d
shun-iwasawa 98926d
  if (lastLocT != 1) points.push_back(p2);
shun-iwasawa 98926d
shun-iwasawa 98926d
  startPoint =
shun-iwasawa 98926d
      stroke->getControlPointIndexAfterParameter(parameterValues[n - 1]);
shun-iwasawa 98926d
  if ((stroke->getControlPointIndexAfterParameter(parameterValues[n - 1]) &
shun-iwasawa 98926d
       1) &&
shun-iwasawa 98926d
      lastLocT != 1)
shun-iwasawa 98926d
    startPoint++;
shun-iwasawa 98926d
  for (int j = startPoint; j < stroke->getControlPointCount(); j++)
shun-iwasawa 98926d
    points.push_back(stroke->getControlPoint(j));
shun-iwasawa 98926d
shun-iwasawa 98926d
  assert(points.size() & 1);
shun-iwasawa 98926d
  TStroke *strokeAdd = new TStroke(points);
shun-iwasawa 98926d
  strokeAdd->setStyle(stroke->getStyle());
shun-iwasawa 98926d
  strokeAdd->outlineOptions() = stroke->outlineOptions();
shun-iwasawa 98926d
  strokes.push_back(strokeAdd);
shun-iwasawa 98926d
  points.clear();
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
// Compute Parametric Curve Curvature
shun-iwasawa 98926d
// By Formula:
shun-iwasawa 98926d
// k(t)=(|p'(t) x p''(t)|)/Norm2(p')^3
shun-iwasawa 98926d
// p(t) is parametric curve
shun-iwasawa 98926d
// Input:
shun-iwasawa 98926d
//      dp  = First Derivate.
shun-iwasawa 98926d
//      ddp = Second Derivate
shun-iwasawa 98926d
// Output:
shun-iwasawa 98926d
//      return curvature value.
shun-iwasawa 98926d
//      Note: if the curve is a single point (that's dp=0) or it is a straight
shun-iwasawa 98926d
//      line (that's ddp=0) return 0
shun-iwasawa 98926d
shun-iwasawa 98926d
static double curvature(TPointD dp, TPointD ddp) {
shun-iwasawa 98926d
  if (dp == TPointD(0, 0))
shun-iwasawa 98926d
    return 0;
shun-iwasawa 98926d
  else
shun-iwasawa 98926d
    return fabs(cross(dp, ddp) / pow(norm2(dp), 1.5));
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
// Find the max curvature points of a stroke.
shun-iwasawa 98926d
// Input:
shun-iwasawa 98926d
//      stroke.
shun-iwasawa 98926d
//      angoloLim =  Value (radians) of the Corner between two tangent vector.
shun-iwasawa 98926d
//                   Up this value the two corner can be considered angular.
shun-iwasawa 98926d
//      curvMaxLim = Value of the max curvature.
shun-iwasawa 98926d
//                   Up this value the point can be considered a max curvature
shun-iwasawa 98926d
//                   point.
shun-iwasawa 98926d
// Output:
shun-iwasawa 98926d
//      parameterValues = vector of max curvature parameter points
shun-iwasawa 98926d
shun-iwasawa 98926d
static void findMaxCurvPoints(TStroke *stroke, const float &angoloLim,
shun-iwasawa 98926d
                              const float &curvMaxLim,
shun-iwasawa 98926d
                              std::vector<double> ¶meterValues) {</double>
shun-iwasawa 98926d
  TPointD tg1, tg2;  // Tangent vectors
shun-iwasawa 98926d
shun-iwasawa 98926d
  TPointD dp, ddp;  // First and Second derivate.
shun-iwasawa 98926d
shun-iwasawa 98926d
  parameterValues.clear();
shun-iwasawa 98926d
  int cpn = stroke ? stroke->getControlPointCount() : 0;
shun-iwasawa 98926d
  for (int j = 2; j < cpn; j += 2) {
shun-iwasawa 98926d
    TPointD p0 = stroke->getControlPoint(j - 2);
shun-iwasawa 98926d
    TPointD p1 = stroke->getControlPoint(j - 1);
shun-iwasawa 98926d
    TPointD p2 = stroke->getControlPoint(j);
shun-iwasawa 98926d
shun-iwasawa 98926d
    TPointD q = p1 - (p0 + p2) * 0.5;
shun-iwasawa 98926d
shun-iwasawa 98926d
    // Search corner point
shun-iwasawa 98926d
    if (j > 2) {
shun-iwasawa 98926d
      tg2 = -p0 + p2 + 2 * q;  // Tangent vector to this chunk at t=0
shun-iwasawa 98926d
      double prod_scal =
shun-iwasawa 98926d
          tg2 * tg1;  // Inner product between tangent vectors at t=0.
shun-iwasawa 98926d
      assert(tg1 != TPointD(0, 0) || tg2 != TPointD(0, 0));
shun-iwasawa 98926d
      // Compute corner between two tangent vectors
shun-iwasawa 98926d
      double angolo =
shun-iwasawa 98926d
          acos(prod_scal / (pow(norm2(tg2), 0.5) * pow(norm2(tg1), 0.5)));
shun-iwasawa 98926d
shun-iwasawa 98926d
      // Add corner point
shun-iwasawa 98926d
      if (angolo > angoloLim) {
shun-iwasawa 98926d
        double w = getWfromChunkAndT(stroke, (UINT)(0.5 * (j - 2)),
shun-iwasawa 98926d
                                     0);  //  transform lacal t to global t
shun-iwasawa 98926d
        parameterValues.push_back(w);
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
    tg1 = -p0 + p2 - 2 * q;  // Tangent vector to this chunk at t=1
shun-iwasawa 98926d
shun-iwasawa 98926d
    // End search corner point
shun-iwasawa 98926d
shun-iwasawa 98926d
    // Search max curvature point
shun-iwasawa 98926d
    // Value of t where the curvature function has got an extreme.
shun-iwasawa 98926d
    // (Point where first derivate is null)
shun-iwasawa 98926d
    double estremo_int = 0;
shun-iwasawa 98926d
    double t           = -1;
shun-iwasawa 98926d
    if (q != TPointD(0, 0)) {
shun-iwasawa 76d093
      t = 0.25 *
shun-iwasawa 76d093
          (2 * q.x * q.x + 2 * q.y * q.y - q.x * p0.x + q.x * p2.x -
shun-iwasawa 76d093
           q.y * p0.y + q.y * p2.y) /
shun-iwasawa 98926d
          (q.x * q.x + q.y * q.y);
shun-iwasawa 98926d
shun-iwasawa 98926d
      dp  = -p0 + p2 + 2 * q - 4 * t * q;  // First derivate of the curve
shun-iwasawa 98926d
      ddp = -4 * q;                        // Second derivate of the curve
shun-iwasawa 98926d
      estremo_int = curvature(dp, ddp);
shun-iwasawa 98926d
shun-iwasawa 98926d
      double h    = 0.01;
shun-iwasawa 98926d
      dp          = -p0 + p2 + 2 * q - 4 * (t + h) * q;
shun-iwasawa 98926d
      double c_dx = curvature(dp, ddp);
shun-iwasawa 98926d
      dp          = -p0 + p2 + 2 * q - 4 * (t - h) * q;
shun-iwasawa 98926d
      double c_sx = curvature(dp, ddp);
shun-iwasawa 98926d
      // Check the point is a max and not a minimum
shun-iwasawa 98926d
      if (estremo_int < c_dx && estremo_int < c_sx) {
shun-iwasawa 98926d
        estremo_int = 0;
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
    double curv_max = estremo_int;
shun-iwasawa 98926d
shun-iwasawa 98926d
    // Compute curvature at the extreme of interval [0,1]
shun-iwasawa 98926d
    // Compute curvature at t=0 (Left extreme)
shun-iwasawa 98926d
    dp                = -p0 + p2 + 2 * q;
shun-iwasawa 98926d
    double estremo_sx = curvature(dp, ddp);
shun-iwasawa 98926d
shun-iwasawa 98926d
    // Compute curvature at t=1 (Right extreme)
shun-iwasawa 98926d
    dp                = -p0 + p2 - 2 * q;
shun-iwasawa 98926d
    double estremo_dx = curvature(dp, ddp);
shun-iwasawa 98926d
shun-iwasawa 98926d
    // Compare curvature at the extreme of interval [0,1] with the internal
shun-iwasawa 98926d
    // value
shun-iwasawa 98926d
    double t_ext;
shun-iwasawa 98926d
    if (estremo_sx >= estremo_dx)
shun-iwasawa 98926d
      t_ext = 0;
shun-iwasawa 98926d
    else
shun-iwasawa 76d093
      t_ext = 1;
shun-iwasawa 98926d
    double maxEstremi = std::max(estremo_dx, estremo_sx);
shun-iwasawa 98926d
    if (maxEstremi > estremo_int) {
shun-iwasawa 98926d
      t        = t_ext;
shun-iwasawa 98926d
      curv_max = maxEstremi;
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
shun-iwasawa 98926d
    // Add max curvature point
shun-iwasawa 98926d
    if (t >= 0 && t <= 1 && curv_max > curvMaxLim) {
shun-iwasawa 98926d
      double w = getWfromChunkAndT(stroke, (UINT)(0.5 * (j - 2)),
shun-iwasawa 98926d
                                   t);  // transform local t to global t
shun-iwasawa 98926d
      parameterValues.push_back(w);
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
    // End search max curvature point
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
  // Delete duplicate of parameterValues
shun-iwasawa 98926d
  // Because some max cuvature point can coincide with the corner point
shun-iwasawa 98926d
  if ((int)parameterValues.size() > 1) {
shun-iwasawa 98926d
    std::sort(parameterValues.begin(), parameterValues.end());
shun-iwasawa 98926d
    parameterValues.erase(
shun-iwasawa 98926d
        std::unique(parameterValues.begin(), parameterValues.end()),
shun-iwasawa 98926d
        parameterValues.end());
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
static void addStroke(TTool::Application *application, const TVectorImageP &vi,
manongjohn 40a40e
                      TStroke *stroke, bool breakAngles, bool autoGroup,
manongjohn 40a40e
                      bool autoFill, bool frameCreated, bool levelCreated,
manongjohn 40a40e
                      TXshSimpleLevel *sLevel = NULL,
shun-iwasawa ddc79b
                      TFrameId fid            = TFrameId::NO_FRAME) {
shun-iwasawa 98926d
  QMutexLocker lock(vi->getMutex());
shun-iwasawa 98926d
shun-iwasawa 98926d
  if (application->getCurrentObject()->isSpline()) {
shun-iwasawa 98926d
    application->getCurrentXsheet()->notifyXsheetChanged();
shun-iwasawa 98926d
    return;
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  std::vector<double> corners;</double>
shun-iwasawa 98926d
  std::vector<tstroke *=""> strokes;</tstroke>
shun-iwasawa 98926d
shun-iwasawa 98926d
  const float angoloLim =
shun-iwasawa 98926d
      1;  // Value (radians) of the Corner between two tangent vector.
shun-iwasawa 98926d
          // Up this value the two corner can be considered angular.
shun-iwasawa 98926d
  const float curvMaxLim = 0.8;  // Value of the max curvature.
shun-iwasawa 98926d
  // Up this value the point can be considered a max curvature point.
shun-iwasawa 98926d
shun-iwasawa 98926d
  findMaxCurvPoints(stroke, angoloLim, curvMaxLim, corners);
shun-iwasawa 98926d
  TXshSimpleLevel *sl;
shun-iwasawa 98926d
  if (!sLevel) {
shun-iwasawa 98926d
    sl = application->getCurrentLevel()->getSimpleLevel();
shun-iwasawa 98926d
  } else {
shun-iwasawa 98926d
    sl = sLevel;
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
  TFrameId id = application->getCurrentTool()->getTool()->getCurrentFid();
shun-iwasawa 98926d
  if (id == TFrameId::NO_FRAME && fid != TFrameId::NO_FRAME) id = fid;
shun-iwasawa 98926d
  if (!corners.empty()) {
shun-iwasawa 98926d
    if (breakAngles)
shun-iwasawa 98926d
      split(stroke, corners, strokes);
shun-iwasawa 98926d
    else
shun-iwasawa 98926d
      strokes.push_back(new TStroke(*stroke));
shun-iwasawa 98926d
shun-iwasawa 98926d
    int n = strokes.size();
shun-iwasawa 98926d
shun-iwasawa 98926d
    TUndoManager::manager()->beginBlock();
shun-iwasawa 98926d
    for (int i = 0; i < n; i++) {
shun-iwasawa 98926d
      std::vector<tfilledregioninf> *fillInformation =</tfilledregioninf>
shun-iwasawa 98926d
          new std::vector<tfilledregioninf>;</tfilledregioninf>
shun-iwasawa 98926d
      ImageUtils::getFillingInformationOverlappingArea(vi, *fillInformation,
shun-iwasawa 98926d
                                                       stroke->getBBox());
shun-iwasawa 98926d
      TStroke *str = new TStroke(*strokes[i]);
shun-iwasawa 98926d
      vi->addStroke(str);
shun-iwasawa 98926d
      TUndoManager::manager()->add(new UndoPencil(str, fillInformation, sl, id,
manongjohn 40a40e
                                                  frameCreated, levelCreated,
manongjohn 40a40e
                                                  autoGroup, autoFill));
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
    TUndoManager::manager()->endBlock();
shun-iwasawa 98926d
  } else {
shun-iwasawa 98926d
    std::vector<tfilledregioninf> *fillInformation =</tfilledregioninf>
shun-iwasawa 98926d
        new std::vector<tfilledregioninf>;</tfilledregioninf>
shun-iwasawa 98926d
    ImageUtils::getFillingInformationOverlappingArea(vi, *fillInformation,
shun-iwasawa 98926d
                                                     stroke->getBBox());
shun-iwasawa 98926d
    TStroke *str = new TStroke(*stroke);
shun-iwasawa 98926d
    vi->addStroke(str);
shun-iwasawa 98926d
    TUndoManager::manager()->add(new UndoPencil(str, fillInformation, sl, id,
manongjohn 40a40e
                                                frameCreated, levelCreated,
manongjohn 40a40e
                                                autoGroup, autoFill));
manongjohn 40a40e
  }
manongjohn 40a40e
manongjohn 40a40e
  if (autoGroup && stroke->isSelfLoop()) {
manongjohn 40a40e
    int index = vi->getStrokeCount() - 1;
manongjohn 40a40e
    vi->group(index, 1);
manongjohn 40a40e
    if (autoFill) {
manongjohn 40a40e
      // to avoid filling other strokes, I enter into the new stroke group
manongjohn 40a40e
      int currentGroup = vi->exitGroup();
manongjohn 40a40e
      vi->enterGroup(index);
manongjohn 40a40e
      vi->selectFill(stroke->getBBox().enlarge(1, 1), 0, stroke->getStyle(),
manongjohn 40a40e
                     false, true, false);
manongjohn 40a40e
      if (currentGroup != -1)
manongjohn 40a40e
        vi->enterGroup(currentGroup);
manongjohn 40a40e
      else
manongjohn 40a40e
        vi->exitGroup();
manongjohn 40a40e
    }
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  // Update regions. It will call roundStroke() in
shun-iwasawa 98926d
  // TVectorImage::Imp::findIntersections().
shun-iwasawa 98926d
  // roundStroke() will slightly modify all the stroke positions.
shun-iwasawa 98926d
  // It is needed to update information for Fill Check.
shun-iwasawa 98926d
  vi->findRegions();
shun-iwasawa 98926d
shun-iwasawa 98926d
  for (int k = 0; k < (int)strokes.size(); k++) delete strokes[k];
shun-iwasawa 98926d
  strokes.clear();
shun-iwasawa 98926d
shun-iwasawa 98926d
  application->getCurrentTool()->getTool()->notifyImageChanged();
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//-------------------------------------------------------------------
shun-iwasawa 98926d
//
shun-iwasawa 98926d
// Gennaro: end
shun-iwasawa 98926d
//
shun-iwasawa 98926d
//-------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
//===================================================================
shun-iwasawa 98926d
//
shun-iwasawa 98926d
// Helper functions and classes
shun-iwasawa 98926d
//
shun-iwasawa 98926d
//-------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
namespace {
shun-iwasawa 98926d
shun-iwasawa 98926d
//-------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void addStrokeToImage(TTool::Application *application, const TVectorImageP &vi,
manongjohn 40a40e
                      TStroke *stroke, bool breakAngles, bool autoGroup,
manongjohn 40a40e
                      bool autoFill, bool frameCreated, bool levelCreated,
manongjohn 40a40e
                      TXshSimpleLevel *sLevel = NULL,
shun-iwasawa ddc79b
                      TFrameId id             = TFrameId::NO_FRAME) {
shun-iwasawa 98926d
  QMutexLocker lock(vi->getMutex());
manongjohn 40a40e
  addStroke(application, vi.getPointer(), stroke, breakAngles, autoGroup,
manongjohn 40a40e
            autoFill, frameCreated, levelCreated, sLevel, id);
shun-iwasawa 98926d
  // la notifica viene gia fatta da addStroke!
shun-iwasawa 98926d
  // getApplication()->getCurrentTool()->getTool()->notifyImageChanged();
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//---------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
enum DrawOrder { OverAll = 0, UnderAll, PaletteOrder };
shun-iwasawa 98926d
shun-iwasawa 98926d
void getAboveStyleIdSet(int styleId, TPaletteP palette,
shun-iwasawa 98926d
                        QSet<int> &aboveStyles) {</int>
shun-iwasawa 98926d
  if (!palette) return;
shun-iwasawa 98926d
  for (int p = 0; p < palette->getPageCount(); p++) {
shun-iwasawa 98926d
    TPalette::Page *page = palette->getPage(p);
shun-iwasawa 98926d
    for (int s = 0; s < page->getStyleCount(); s++) {
shun-iwasawa 98926d
      int tmpId = page->getStyleId(s);
shun-iwasawa 98926d
      if (tmpId == styleId) return;
shun-iwasawa 98926d
      if (tmpId != 0) aboveStyles.insert(tmpId);
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//=========================================================================================================
shun-iwasawa 98926d
shun-iwasawa 98926d
double computeThickness(double pressure, const TDoublePairProperty &property,
fa009d
                        bool enablePressure, bool isPath ) {
shun-iwasawa 98926d
  if (isPath) return 0.0;
fa009d
  if (!enablePressure) return property.getValue().second*0.5;
shun-iwasawa 76d093
  double t      = pressure * pressure * pressure;
shun-iwasawa 76d093
  double thick0 = property.getValue().first;
shun-iwasawa 76d093
  double thick1 = property.getValue().second;
shun-iwasawa 98926d
  if (thick1 < 0.0001) thick0 = thick1 = 0.0;
shun-iwasawa 98926d
  return (thick0 + (thick1 - thick0) * t) * 0.5;
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
}  // namespace
shun-iwasawa 98926d
shun-iwasawa 98926d
//===================================================================
shun-iwasawa 98926d
//
shun-iwasawa 98926d
// ToonzVectorBrushTool
shun-iwasawa 98926d
//
shun-iwasawa 98926d
//-----------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
ToonzVectorBrushTool::ToonzVectorBrushTool(std::string name, int targetType)
shun-iwasawa 98926d
    : TTool(name)
shun-iwasawa 76d093
    , m_thickness("Size", 0, 1000, 0, 5)
shun-iwasawa 98926d
    , m_accuracy("Accuracy:", 1, 100, 20)
shun-iwasawa 98926d
    , m_smooth("Smooth:", 0, 50, 0)
shun-iwasawa 98926d
    , m_preset("Preset:")
shun-iwasawa 98926d
    , m_breakAngles("Break", true)
shun-iwasawa 98926d
    , m_pressure("Pressure", true)
fa009d
    , m_snap("Snap", false)
fa009d
    , m_frameRange("Range:")
fa009d
    , m_snapSensitivity("Sensitivity:")
shun-iwasawa 98926d
    , m_capStyle("Cap")
shun-iwasawa 98926d
    , m_joinStyle("Join")
shun-iwasawa 98926d
    , m_miterJoinLimit("Miter:", 0, 100, 4)
fa009d
    , m_assistants("Assistants", true)
fa009d
    , m_styleId()
fa009d
    , m_minThick()
fa009d
    , m_maxThick()
fa009d
    , m_col()
fa009d
    , m_firstFrame()
fa009d
    , m_veryFirstFrame()
fa009d
    , m_veryFirstCol()
fa009d
    , m_targetType(targetType)
fa009d
    , m_pixelSize()
fa009d
    , m_minDistance2()
fa009d
    , m_snapped()
fa009d
    , m_snappedSelf()
fa009d
    , m_active()
shun-iwasawa 98926d
    , m_firstTime(true)
fa009d
    , m_isPath()
fa009d
    , m_presetsLoaded()
shun-iwasawa 98926d
    , m_firstFrameRange(true)
fa009d
    , m_propertyUpdating()
fa009d
{
shun-iwasawa 98926d
  bind(targetType);
shun-iwasawa 76d093
shun-iwasawa 76d093
  m_thickness.setNonLinearSlider();
shun-iwasawa 76d093
shun-iwasawa 98926d
  m_prop[0].bind(m_thickness);
shun-iwasawa 98926d
  m_prop[0].bind(m_accuracy);
shun-iwasawa 98926d
  m_prop[0].bind(m_smooth);
shun-iwasawa 98926d
  m_prop[0].bind(m_breakAngles);
shun-iwasawa 98926d
  m_prop[0].bind(m_pressure);
shun-iwasawa 98926d
shun-iwasawa 98926d
  m_prop[0].bind(m_frameRange);
shun-iwasawa 98926d
  m_frameRange.addValue(L"Off");
shun-iwasawa 98926d
  m_frameRange.addValue(LINEAR_WSTR);
shun-iwasawa 98926d
  m_frameRange.addValue(EASEIN_WSTR);
shun-iwasawa 98926d
  m_frameRange.addValue(EASEOUT_WSTR);
shun-iwasawa 98926d
  m_frameRange.addValue(EASEINOUT_WSTR);
shun-iwasawa 98926d
shun-iwasawa 98926d
  m_prop[0].bind(m_snap);
shun-iwasawa 98926d
shun-iwasawa 98926d
  m_prop[0].bind(m_snapSensitivity);
shun-iwasawa 98926d
  m_snapSensitivity.addValue(LOW_WSTR);
shun-iwasawa 98926d
  m_snapSensitivity.addValue(MEDIUM_WSTR);
shun-iwasawa 98926d
  m_snapSensitivity.addValue(HIGH_WSTR);
shun-iwasawa 98926d
fa009d
  m_prop[0].bind(m_assistants);
fa009d
shun-iwasawa 98926d
  m_prop[0].bind(m_preset);
shun-iwasawa 98926d
  m_preset.addValue(CUSTOM_WSTR);
shun-iwasawa 98926d
shun-iwasawa 98926d
  m_prop[1].bind(m_capStyle);
shun-iwasawa 98926d
  m_capStyle.addValue(BUTT_WSTR, QString::fromStdWString(BUTT_WSTR));
shun-iwasawa 98926d
  m_capStyle.addValue(ROUNDC_WSTR, QString::fromStdWString(ROUNDC_WSTR));
shun-iwasawa 98926d
  m_capStyle.addValue(PROJECTING_WSTR,
shun-iwasawa 98926d
                      QString::fromStdWString(PROJECTING_WSTR));
shun-iwasawa 98926d
shun-iwasawa 98926d
  m_prop[1].bind(m_joinStyle);
shun-iwasawa 98926d
  m_joinStyle.addValue(MITER_WSTR, QString::fromStdWString(MITER_WSTR));
shun-iwasawa 98926d
  m_joinStyle.addValue(ROUNDJ_WSTR, QString::fromStdWString(ROUNDJ_WSTR));
shun-iwasawa 98926d
  m_joinStyle.addValue(BEVEL_WSTR, QString::fromStdWString(BEVEL_WSTR));
shun-iwasawa 98926d
shun-iwasawa 98926d
  m_prop[1].bind(m_miterJoinLimit);
shun-iwasawa 98926d
shun-iwasawa 98926d
  m_breakAngles.setId("BreakSharpAngles");
shun-iwasawa 98926d
  m_frameRange.setId("FrameRange");
shun-iwasawa 98926d
  m_snap.setId("Snap");
shun-iwasawa 98926d
  m_snapSensitivity.setId("SnapSensitivity");
shun-iwasawa 98926d
  m_preset.setId("BrushPreset");
shun-iwasawa 98926d
  m_pressure.setId("PressureSensitivity");
shun-iwasawa 98926d
  m_capStyle.setId("Cap");
shun-iwasawa 98926d
  m_joinStyle.setId("Join");
shun-iwasawa 98926d
  m_miterJoinLimit.setId("Miter");
fa009d
  m_assistants.setId("Assistants");
fa009d
fa009d
  m_inputmanager.setHandler(this);
fa009d
  m_modifierLine               = new TModifierLine();
fa009d
  m_modifierTangents           = new TModifierTangents();
fa009d
  m_modifierAssistants         = new TModifierAssistants();
fa009d
  m_modifierSegmentation       = new TModifierSegmentation();
fa009d
  m_modifierSmoothSegmentation = new TModifierSegmentation(TPointD(1, 1), 3);
fa009d
  for(int i = 0; i < 3; ++i)
fa009d
    m_modifierSmooth[i]        = new TModifierSmooth();
fa009d
  m_modifierSimplify           = new TModifierSimplify();
fa009d
#ifndef NDEBUG
fa009d
  m_modifierTest = new TModifierTest();
fa009d
#endif
fa009d
fa009d
  m_inputmanager.addModifier(
fa009d
      TInputModifierP(m_modifierAssistants.getPointer()));
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//-------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
da847a
unsigned int ToonzVectorBrushTool::getToolHints() const {
da847a
  unsigned int h = TTool::getToolHints() & ~HintAssistantsAll;
da847a
  if (m_assistants.getValue()) {
da847a
    h |= HintReplicators;
da847a
    h |= HintReplicatorsPoints;
da847a
    h |= HintReplicatorsEnabled;
da847a
  }
da847a
  return h;
da847a
}
da847a
  
da847a
//-------------------------------------------------------------------------------------------------------
da847a
shun-iwasawa 98926d
ToolOptionsBox *ToonzVectorBrushTool::createOptionsBox() {
shun-iwasawa 98926d
  TPaletteHandle *currPalette =
shun-iwasawa 98926d
      TTool::getApplication()->getPaletteController()->getCurrentLevelPalette();
shun-iwasawa 98926d
  ToolHandle *currTool = TTool::getApplication()->getCurrentTool();
shun-iwasawa 98926d
  return new BrushToolOptionsBox(0, this, currPalette, currTool);
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//-------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::updateTranslation() {
shun-iwasawa 98926d
  m_thickness.setQStringName(tr("Size"));
shun-iwasawa 98926d
  m_accuracy.setQStringName(tr("Accuracy:"));
shun-iwasawa 98926d
  m_smooth.setQStringName(tr("Smooth:"));
shun-iwasawa 98926d
  m_preset.setQStringName(tr("Preset:"));
shun-iwasawa 98926d
  m_preset.setItemUIName(CUSTOM_WSTR, tr("<custom>"));</custom>
shun-iwasawa 98926d
  m_breakAngles.setQStringName(tr("Break"));
shun-iwasawa 98926d
  m_pressure.setQStringName(tr("Pressure"));
shun-iwasawa 98926d
  m_capStyle.setQStringName(tr("Cap"));
shun-iwasawa 98926d
  m_joinStyle.setQStringName(tr("Join"));
shun-iwasawa 98926d
  m_miterJoinLimit.setQStringName(tr("Miter:"));
shun-iwasawa 98926d
  m_frameRange.setQStringName(tr("Range:"));
shun-iwasawa 98926d
  m_snap.setQStringName(tr("Snap"));
shun-iwasawa 98926d
  m_snapSensitivity.setQStringName("");
fa009d
  m_assistants.setQStringName(tr("Assistants"));
shun-iwasawa 98926d
  m_frameRange.setItemUIName(L"Off", tr("Off"));
shun-iwasawa 98926d
  m_frameRange.setItemUIName(LINEAR_WSTR, tr("Linear"));
shun-iwasawa 98926d
  m_frameRange.setItemUIName(EASEIN_WSTR, tr("In"));
shun-iwasawa 98926d
  m_frameRange.setItemUIName(EASEOUT_WSTR, tr("Out"));
shun-iwasawa 98926d
  m_frameRange.setItemUIName(EASEINOUT_WSTR, tr("In&Out"));
shun-iwasawa 98926d
  m_snapSensitivity.setItemUIName(LOW_WSTR, tr("Low"));
shun-iwasawa 98926d
  m_snapSensitivity.setItemUIName(MEDIUM_WSTR, tr("Med"));
shun-iwasawa 98926d
  m_snapSensitivity.setItemUIName(HIGH_WSTR, tr("High"));
shun-iwasawa 98926d
  m_capStyle.setItemUIName(BUTT_WSTR, tr("Butt cap"));
shun-iwasawa 98926d
  m_capStyle.setItemUIName(ROUNDC_WSTR, tr("Round cap"));
shun-iwasawa 98926d
  m_capStyle.setItemUIName(PROJECTING_WSTR, tr("Projecting cap"));
shun-iwasawa 98926d
  m_joinStyle.setItemUIName(MITER_WSTR, tr("Miter join"));
shun-iwasawa 98926d
  m_joinStyle.setItemUIName(ROUNDJ_WSTR, tr("Round join"));
shun-iwasawa 98926d
  m_joinStyle.setItemUIName(BEVEL_WSTR, tr("Bevel join"));
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//---------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::onActivate() {
shun-iwasawa 98926d
  if (m_firstTime) {
shun-iwasawa 98926d
    m_firstTime = false;
shun-iwasawa 98926d
manongjohn df5842
    std::wstring wpreset =
manongjohn df5842
        QString::fromStdString(V_VectorBrushPreset.getValue()).toStdWString();
manongjohn df5842
    if (wpreset != CUSTOM_WSTR) {
manongjohn df5842
      initPresets();
manongjohn 8b8d41
      if (!m_preset.isValue(wpreset)) wpreset = CUSTOM_WSTR;
manongjohn df5842
      m_preset.setValue(wpreset);
manongjohn 8b8d41
      V_VectorBrushPreset = m_preset.getValueAsString();
manongjohn df5842
      loadPreset();
manongjohn df5842
    } else
manongjohn df5842
      loadLastBrush();
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
  resetFrameRange();
da847a
  updateModifiers();
shun-iwasawa 98926d
  // TODO:app->editImageOrSpline();
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//--------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::onDeactivate() {
shun-iwasawa 98926d
  /*---
Rozhuk Ivan 823a31
   * ドラッグ中にツールが切り替わった場合に備え、onDeactivateにもMouseReleaseと同じ処理を行う
shun-iwasawa 76d093
   * ---*/
Martin van Zijl 2420a8
Martin van Zijl 2420a8
  // End current stroke.
fa009d
  m_inputmanager.finishTracks();
shun-iwasawa 98926d
  resetFrameRange();
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//--------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
fa009d
void ToonzVectorBrushTool::inputMouseMove(
fa009d
  const TPointD &position, const TInputState &state )
fa009d
{
fa009d
  struct Locals {
fa009d
    ToonzVectorBrushTool *m_this;
manongjohn 40a40e
fa009d
    void setValue(TDoublePairProperty &prop,
fa009d
                  const TDoublePairProperty::Value &value) {
fa009d
      prop.setValue(value);
shun-iwasawa 98926d
fa009d
      m_this->onPropertyChanged(prop.getName());
fa009d
      TTool::getApplication()->getCurrentTool()->notifyToolChanged();
fa009d
    }
shun-iwasawa 98926d
fa009d
    void addMinMax(TDoublePairProperty &prop, double min, double max) {
fa009d
      if (min == 0.0 && max == 0.0) return;
fa009d
      const TDoublePairProperty::Range &range = prop.getRange();
shun-iwasawa 98926d
fa009d
      TDoublePairProperty::Value value = prop.getValue();
fa009d
      value.first += min;
fa009d
      value.second += max;
fa009d
      if (value.first > value.second) value.first = value.second;
fa009d
      value.first  = tcrop(value.first, range.first, range.second);
fa009d
      value.second = tcrop(value.second, range.first, range.second);
manongjohn 40a40e
fa009d
      setValue(prop, value);
shun-iwasawa 98926d
    }
justburner e250b7
fa009d
  } locals = {this};
justburner e250b7
fa009d
  TPointD halfThick(m_maxThick * 0.5, m_maxThick * 0.5);
fa009d
  TRectD invalidateRect(m_brushPos - halfThick, m_brushPos + halfThick);
shun-iwasawa 98926d
fa009d
  bool alt     = state.isKeyPressed(TInputState::Key::alt);
fa009d
  bool shift   = state.isKeyPressed(TInputState::Key::shift);
fa009d
  bool control = state.isKeyPressed(TInputState::Key::control);
fa009d
  
fa009d
  if ( alt && control && !shift
fa009d
    && Preferences::instance()->useCtrlAltToResizeBrushEnabled() )
fa009d
  {
fa009d
    // Resize the brush if CTRL+ALT is pressed and the preference is enabled.
fa009d
    const TPointD &diff = position - m_mousePos;
fa009d
    double max          = diff.x / 2;
fa009d
    double min          = diff.y / 2;
shun-iwasawa 98926d
fa009d
    locals.addMinMax(m_thickness, min, max);
shun-iwasawa 98926d
fa009d
    double radius = m_thickness.getValue().second * 0.5;
fa009d
    halfThick = TPointD(radius, radius);
fa009d
  } else {
fa009d
    m_brushPos = m_mousePos = position;
shun-iwasawa 98926d
  }
fa009d
  
shun-iwasawa 98926d
  invalidateRect += TRectD(m_brushPos - halfThick, m_brushPos + halfThick);
shun-iwasawa 98926d
fa009d
  if (m_minThick == 0 && m_maxThick == 0) {
fa009d
    m_minThick = m_thickness.getValue().first;
fa009d
    m_maxThick = m_thickness.getValue().second;
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
fa009d
  invalidate(invalidateRect.enlarge(2));
fa009d
}
shun-iwasawa 98926d
fa009d
//--------------------------------------------------------------------------------------------------
fa009d
fa009d
void ToonzVectorBrushTool::deleteStrokes(StrokeList &strokes) {
fa009d
  for(StrokeList::iterator i = strokes.begin(); i != strokes.end(); ++i)
fa009d
    delete *i;
fa009d
  strokes.clear();
shun-iwasawa 98926d
}
shun-iwasawa 98926d
fa009d
//--------------------------------------------------------------------------------------------------
fa009d
fa009d
void ToonzVectorBrushTool::copyStrokes(StrokeList &dst, const StrokeList &src) {
fa009d
  deleteStrokes(dst);
fa009d
  dst.reserve(src.size());
fa009d
  for(StrokeList::const_iterator i = src.begin(); i != src.end(); ++i)
fa009d
    dst.push_back(new TStroke(**i));
fa009d
}
shun-iwasawa 98926d
fa009d
//--------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
fa009d
void ToonzVectorBrushTool::inputSetBusy(bool busy) {
fa009d
  if (m_active == busy) return;
fa009d
  
fa009d
  if (busy) {
fa009d
    
fa009d
    // begin painting //////////////////////////
fa009d
    
fa009d
    m_styleId = 0;
fa009d
    m_tracks.clear();
fa009d
    
fa009d
    TTool::Application *app = TTool::getApplication();
fa009d
    if (!app)
fa009d
      return;
fa009d
fa009d
    m_isPath = app->getCurrentObject()->isSpline();
fa009d
    if (m_isPath) {
fa009d
      m_currentColor = TPixel32::Red;
fa009d
      m_active = true;
fa009d
      return;
shun-iwasawa 98926d
    }
fa009d
    
fa009d
    // todo: gestire autoenable
fa009d
    if ( app->getCurrentColumn()->getColumnIndex() < 0
fa009d
      && !app->getCurrentFrame()->isEditingLevel() )
fa009d
      return;
fa009d
    if (!getImage(true) || !touchImage())
fa009d
      return;
fa009d
    if (!app->getCurrentLevel()->getLevel())
fa009d
      return;
fa009d
fa009d
    // nel caso che il colore corrente sia un cleanup/studiopalette color
fa009d
    // oppure il colore di un colorfield
fa009d
    if (TColorStyle *cs = app->getCurrentLevelStyle()) {
fa009d
      TRasterStyleFx *rfx = cs->getRasterStyleFx();
fa009d
      if (!cs->isStrokeStyle() && (!rfx || !rfx->isInkStyle()))
fa009d
        return;
fa009d
      m_styleId = app->getCurrentLevelStyleIndex();
fa009d
      m_currentColor = cs->getAverageColor();
fa009d
      m_currentColor.m = 255;
fa009d
    } else {
fa009d
      m_styleId = 1;
fa009d
      m_currentColor = TPixel32::Black;
fa009d
    }
fa009d
    
fa009d
    m_active = true;
fa009d
    
fa009d
    return; // painting has begun
shun-iwasawa 98926d
  }
fa009d
  
fa009d
  
fa009d
  // end painting //////////////////////////
fa009d
  
fa009d
  m_active = false;
fa009d
  
fa009d
  // clear tracks automatically when return from this function
fa009d
  struct Cleanup {
fa009d
    ToonzVectorBrushTool &owner;
fa009d
    inline ~Cleanup() { owner.m_tracks.clear(); owner.invalidate(); }
fa009d
  } cleanup = {*this};
fa009d
fa009d
  // remove empty tracks
fa009d
  for(TrackList::iterator i = m_tracks.begin(); i != m_tracks.end(); )
fa009d
    if (i->isEmpty()) i = m_tracks.erase(i); else ++i;
fa009d
  
fa009d
  if (m_tracks.empty())
fa009d
    return;
fa009d
  
fa009d
  // make motion path (if need)
fa009d
    
shun-iwasawa 98926d
  if (m_isPath) {
fa009d
    double error = 20.0 * m_pixelSize;
shun-iwasawa 98926d
fa009d
    m_tracks.resize(1);
fa009d
    TStroke *stroke = m_tracks.front().makeStroke(error);
shun-iwasawa 98926d
shun-iwasawa 98926d
    TVectorImageP vi = getImage(true);
shun-iwasawa 98926d
shun-iwasawa 98926d
    if (!isJustCreatedSpline(vi.getPointer())) {
fa009d
      m_currentColor = TPixel32::Green;
fa009d
      invalidate();
fa009d
      int ret = DVGui::MsgBox(
fa009d
        QString("Are you sure you want to replace the motion path?"),
fa009d
        QObject::tr("Yes"), QObject::tr("No"), 0 );
fa009d
      if (ret != 1)
fa009d
        return; // 1 here means "Yes" button
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
shun-iwasawa 98926d
    QMutexLocker lock(vi->getMutex());
shun-iwasawa 98926d
fa009d
    TUndo *undo = new UndoPath(
fa009d
      getXsheet()->getStageObject(getObjectId())->getSpline() );
shun-iwasawa 98926d
fa009d
    while(vi->getStrokeCount() > 0) vi->deleteStroke(0);
shun-iwasawa 98926d
    vi->addStroke(stroke, false);
shun-iwasawa 98926d
shun-iwasawa 98926d
    notifyImageChanged();
shun-iwasawa 98926d
    TUndoManager::manager()->add(undo);
shun-iwasawa 98926d
fa009d
    return; // done with motion path
shun-iwasawa 98926d
  }
fa009d
  
fa009d
  // paint regular strokes
fa009d
  
shun-iwasawa 98926d
  TVectorImageP vi = getImage(true);
fa009d
  QMutexLocker lock(vi->getMutex());
fa009d
  TTool::Application *app = TTool::getApplication();
fa009d
  
fa009d
  // prepare strokes
fa009d
    
fa009d
  StrokeList strokes;
fa009d
  strokes.reserve(m_tracks.size());
fa009d
  for(TrackList::iterator i = m_tracks.begin(); i != m_tracks.end(); ++i) {
fa009d
    StrokeGenerator &track = *i;
fa009d
    
fa009d
    track.filterPoints();
fa009d
    double error = 30.0/(1 + 0.5 * m_accuracy.getValue())*m_pixelSize;
fa009d
    TStroke *stroke = track.makeStroke(error);
fa009d
    
fa009d
    stroke->setStyle(m_styleId);
fa009d
    
shun-iwasawa 98926d
    TStroke::OutlineOptions &options = stroke->outlineOptions();
fa009d
    options.m_capStyle   = m_capStyle.getIndex();
fa009d
    options.m_joinStyle  = m_joinStyle.getIndex();
fa009d
    options.m_miterUpper = m_miterJoinLimit.getValue();
fa009d
fa009d
    if ( stroke->getControlPointCount() == 3
fa009d
      && stroke->getControlPoint(0) != stroke->getControlPoint(2) )
fa009d
                                        // gli stroke con solo 1 chunk vengono
fa009d
                                        // fatti dal tape tool...e devono venir
fa009d
                                        // riconosciuti come speciali di
fa009d
                                        // autoclose proprio dal fatto che
fa009d
                                        // hanno 1 solo chunk.
fa009d
      stroke->insertControlPoints(0.5);
fa009d
    
fa009d
    if (!m_frameRange.getIndex() && track.getLoop())
fa009d
      stroke->setSelfLoop(true);
fa009d
    
fa009d
    strokes.push_back(stroke);
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
fa009d
  // add stroke to image
fa009d
  
shun-iwasawa 98926d
  if (m_frameRange.getIndex()) {
fa009d
    // frame range stroke
shun-iwasawa 98926d
    if (m_firstFrameId == -1) {
fa009d
      // remember strokes for first srame
fa009d
      copyStrokes(m_firstStrokes, strokes);
fa009d
      m_firstFrameId = getFrameId();
fa009d
      m_rangeTracks  = m_tracks;
fa009d
      
fa009d
      if (app) {
fa009d
        m_col        = app->getCurrentColumn()->getColumnIndex();
fa009d
        m_firstFrame = app->getCurrentFrame()->getFrame();
shun-iwasawa 98926d
      }
fa009d
      
shun-iwasawa 98926d
      if (m_firstFrameRange) {
shun-iwasawa 98926d
        m_veryFirstCol     = m_col;
shun-iwasawa 98926d
        m_veryFirstFrame   = m_firstFrame;
shun-iwasawa 98926d
        m_veryFirstFrameId = m_firstFrameId;
shun-iwasawa 98926d
      }
fa009d
    } else
fa009d
    if (m_firstFrameId == getFrameId()) {
fa009d
      // painted of first frame agein, so
fa009d
      // just replace the remembered strokes for first frame
fa009d
      copyStrokes(m_firstStrokes, strokes);
fa009d
      m_rangeTracks = m_tracks;
shun-iwasawa 98926d
    } else {
fa009d
      // paint frame range strokes
shun-iwasawa 98926d
      TFrameId currentId = getFrameId();
fa009d
      int curCol   = app ? app->getCurrentColumn()->getColumnIndex() : 0;
fa009d
      int curFrame = app ? app->getCurrentFrame()->getFrame()        : 0;
fa009d
      
fa009d
      if (size_t count = std::min(m_firstStrokes.size(), strokes.size())) {
fa009d
        TUndoManager::manager()->beginBlock();
fa009d
        for(size_t i = 0; i < count; ++i)
fa009d
          doFrameRangeStrokes(
fa009d
              m_firstFrameId, m_firstStrokes[i], getFrameId(), strokes[i],
fa009d
              m_frameRange.getIndex(), m_breakAngles.getValue(), false, false,
fa009d
              m_firstFrameRange );
fa009d
        TUndoManager::manager()->endBlock();
shun-iwasawa 98926d
      }
fa009d
      
fa009d
      if (m_inputmanager.state.isKeyPressed(TInputState::Key::control)) {
fa009d
        if (app && m_firstFrameId > currentId) {
fa009d
          if (app->getCurrentFrame()->isEditingScene()) {
fa009d
            app->getCurrentColumn()->setColumnIndex(curCol);
fa009d
            app->getCurrentFrame()->setFrame(curFrame);
fa009d
          } else {
fa009d
            app->getCurrentFrame()->setFid(currentId);
shun-iwasawa 98926d
          }
shun-iwasawa 98926d
        }
fa009d
        
shun-iwasawa 98926d
        resetFrameRange();
fa009d
        copyStrokes(m_firstStrokes, strokes);
fa009d
        m_rangeTracks     = m_tracks;
shun-iwasawa 98926d
        m_firstFrameId    = currentId;
shun-iwasawa 98926d
        m_firstFrameRange = false;
fa009d
      } else {
fa009d
        if (app) {
fa009d
          if (app->getCurrentFrame()->isEditingScene()) {
fa009d
            app->getCurrentColumn()->setColumnIndex(m_veryFirstCol);
fa009d
            app->getCurrentFrame()->setFrame(m_veryFirstFrame);
fa009d
          } else {
fa009d
            app->getCurrentFrame()->setFid(m_veryFirstFrameId);
fa009d
          }
fa009d
        }
shun-iwasawa 98926d
        resetFrameRange();
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
  } else {
fa009d
    // regular paint strokes
fa009d
    TUndoManager::manager()->beginBlock();
fa009d
    for(StrokeList::iterator i = strokes.begin(); i != strokes.end(); ++i) {
fa009d
      TStroke *stroke = *i;
fa009d
      addStrokeToImage(app, vi, stroke, m_breakAngles.getValue(),
fa009d
                      false, false, m_isFrameCreated, m_isLevelCreated);
fa009d
fa009d
      if ((Preferences::instance()->getGuidedDrawingType() == 1 ||
fa009d
          Preferences::instance()->getGuidedDrawingType() == 2) &&
fa009d
          Preferences::instance()->getGuidedAutoInbetween())
fa009d
      {
fa009d
        TFrameId fId = getFrameId();
fa009d
        doGuidedAutoInbetween(fId, vi, stroke, m_breakAngles.getValue(), false,
fa009d
                              false, false);
fa009d
        if (app->getCurrentFrame()->isEditingScene())
fa009d
          app->getCurrentFrame()->setFrame( app->getCurrentFrame()->getFrameIndex() );
fa009d
        else
fa009d
          app->getCurrentFrame()->setFid(fId);
fa009d
      }
shun-iwasawa 98926d
    }
fa009d
    TUndoManager::manager()->endBlock();
fa009d
  }
fa009d
  
fa009d
  deleteStrokes(strokes);
fa009d
}
manongjohn 40a40e
fa009d
//--------------------------------------------------------------------------------------------------
manongjohn 4fae98
fa009d
void ToonzVectorBrushTool::inputPaintTracks(const TTrackList &tracks) {
fa009d
  if (tracks.empty()) return;
manongjohn 40a40e
fa009d
  TRectD invalidateRect;
manongjohn 40a40e
fa009d
  size_t count = m_isPath ? 1 : tracks.size();
fa009d
  m_tracks.resize(count);
fa009d
  for(size_t i = 0; i < count; ++i) {
fa009d
    const TTrack &track = *tracks[i];
fa009d
    StrokeGenerator &gen = m_tracks[i];
fa009d
    
fa009d
    while(track.pointsRemoved) {
fa009d
      gen.pop();
fa009d
      --track.pointsRemoved;
fa009d
    }
fa009d
    
fa009d
    while(track.pointsAdded) {
fa009d
      const TTrackPoint &p = track.current();
fa009d
      double t = computeThickness(p.pressure, m_thickness, m_pressure.getValue(), m_isPath);
fa009d
      gen.add(TThickPoint(p.position, t), 0);
fa009d
      --track.pointsAdded;
fa009d
    }
fa009d
    
fa009d
    bool loop = m_snappedSelf
fa009d
            && track.fixedFinished()
fa009d
            && !track.empty()
fa009d
            && areAlmostEqual(track.front().position, track.back().position);
fa009d
    gen.setLoop(loop);
fa009d
    
fa009d
    invalidateRect += gen.getLastModifiedRegion();
fa009d
    if (!i) {
fa009d
      TPointD halfThick(m_maxThick * 0.5, m_maxThick * 0.5);
fa009d
      invalidateRect += TRectD(m_brushPos - halfThick, m_brushPos + halfThick);
fa009d
      m_brushPos = m_mousePos = track.current().position;
fa009d
      invalidateRect += TRectD(m_brushPos - halfThick, m_brushPos + halfThick);
fa009d
    }
fa009d
  }
fa009d
  
fa009d
  if (!invalidateRect.isEmpty()) {
fa009d
    if (m_isPath) {
fa009d
      if (getViewer()) getViewer()->GLInvalidateRect(invalidateRect);
fa009d
    } else {
fa009d
      invalidate(invalidateRect.enlarge(2));
fa009d
    }
fa009d
  }
fa009d
}
manongjohn 40a40e
fa009d
//--------------------------------------------------------------------------------------------------
fa009d
da847a
void ToonzVectorBrushTool::updateModifiers() {
fa009d
  m_pixelSize = getPixelSize();
fa009d
  int smoothRadius = (int)round(m_smooth.getValue());
fa009d
  m_modifierAssistants->magnetism = m_assistants.getValue() ? 1 : 0;
fa009d
  m_modifierSegmentation->setStep(TPointD(m_pixelSize, m_pixelSize));
fa009d
  m_modifierSmoothSegmentation->setStep(TPointD(2*m_pixelSize, 2*m_pixelSize));
fa009d
  m_modifierSimplify->step = 2*m_pixelSize;
fa009d
  m_inputmanager.drawPreview = false;
fa009d
da847a
  m_modifierReplicate.clear();
da847a
  if (m_assistants.getValue())
da847a
    TReplicator::scanReplicators(this, nullptr, &m_modifierReplicate, false, true, false, false, nullptr);
da847a
  
fa009d
  m_inputmanager.clearModifiers();
fa009d
  m_inputmanager.addModifier(TInputModifierP(m_modifierTangents.getPointer()));
fa009d
  if (smoothRadius > 0) {
fa009d
    m_inputmanager.addModifier(TInputModifierP(m_modifierSmoothSegmentation.getPointer()));
fa009d
    for(int i = 0; i < 3; ++i) {
fa009d
      m_modifierSmooth[i]->radius = smoothRadius;
fa009d
      m_inputmanager.addModifier(TInputModifierP(m_modifierSmooth[i].getPointer()));
manongjohn 40a40e
    }
shun-iwasawa 98926d
  }
fa009d
  m_inputmanager.addModifier(TInputModifierP(m_modifierAssistants.getPointer()));
fa009d
#ifndef NDEBUG
fa009d
  m_inputmanager.addModifier(TInputModifierP(m_modifierTest.getPointer()));
fa009d
#endif
da847a
  m_inputmanager.addModifiers(m_modifierReplicate);
fa009d
  m_inputmanager.addModifier(TInputModifierP(m_modifierSegmentation.getPointer()));
fa009d
  m_inputmanager.addModifier(TInputModifierP(m_modifierSimplify.getPointer()));
da847a
}
fa009d
da847a
//--------------------------------------------------------------------------------------------------
da847a
da847a
bool ToonzVectorBrushTool::preLeftButtonDown() {
da847a
  if (getViewer() && getViewer()->getGuidedStrokePickerMode()) return false;
da847a
  updateModifiers();
fa009d
  touchImage();
fa009d
  if (m_isFrameCreated) {
fa009d
    // When the xsheet frame is selected, whole viewer will be updated from
fa009d
    // SceneViewer::onXsheetChanged() on adding a new frame.
fa009d
    // We need to take care of a case when the level frame is selected.
fa009d
    if (m_application->getCurrentFrame()->isEditingLevel()) invalidate();
fa009d
  }
fa009d
  return true;
fa009d
}
fa009d
fa009d
//--------------------------------------------------------------------------------------------------
fa009d
fa009d
void ToonzVectorBrushTool::handleMouseEvent(MouseEventType type,
fa009d
                                          const TPointD &pos,
fa009d
                                          const TMouseEvent &e)
fa009d
{
fa009d
  TTimerTicks t = TToolTimer::ticks();
fa009d
  bool alt      = e.getModifiersMask() & TMouseEvent::ALT_KEY;
fa009d
  bool shift    = e.getModifiersMask() & TMouseEvent::SHIFT_KEY;
fa009d
  bool control  = e.getModifiersMask() & TMouseEvent::CTRL_KEY;
fa009d
fa009d
  if (shift && type == ME_DOWN && e.button() == Qt::LeftButton && !m_active) {
fa009d
    m_modifierAssistants->magnetism = 0;
fa009d
    m_inputmanager.clearModifiers();
fa009d
    m_inputmanager.addModifier(TInputModifierP(m_modifierLine.getPointer()));
fa009d
    m_inputmanager.addModifier(TInputModifierP(m_modifierAssistants.getPointer()));
da847a
    m_inputmanager.addModifiers(m_modifierReplicate);
fa009d
    m_inputmanager.addModifier(TInputModifierP(m_modifierSegmentation.getPointer()));
fa009d
    m_inputmanager.addModifier(TInputModifierP(m_modifierSimplify.getPointer()));
fa009d
    m_inputmanager.drawPreview = true;
fa009d
  }
fa009d
fa009d
  if (alt != m_inputmanager.state.isKeyPressed(TKey::alt))
fa009d
    m_inputmanager.keyEvent(alt, TKey::alt, t, nullptr);
fa009d
  if (shift != m_inputmanager.state.isKeyPressed(TKey::shift))
fa009d
    m_inputmanager.keyEvent(shift, TKey::shift, t, nullptr);
fa009d
  if (control != m_inputmanager.state.isKeyPressed(TKey::control))
fa009d
    m_inputmanager.keyEvent(control, TKey::control, t, nullptr);
fa009d
fa009d
  TPointD snappedPos = pos;
fa009d
  bool pickerMode = getViewer() && getViewer()->getGuidedStrokePickerMode();
fa009d
  bool snapInvert = alt && (!control || type == ME_MOVE || type == ME_DOWN);
fa009d
  bool snapEnabled = !pickerMode && (snapInvert != m_snap.getValue());
fa009d
  snap(pos, snapEnabled, m_active);
fa009d
  if (m_snapped)
fa009d
    snappedPos = m_snapPoint;
fa009d
  if (m_snappedSelf && type == ME_UP)
fa009d
    snappedPos = m_snapPointSelf;
fa009d
  
fa009d
  if (type == ME_MOVE) {
fa009d
    qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
fa009d
    THoverList hovers(1, snappedPos);
fa009d
    m_inputmanager.hoverEvent(hovers);
fa009d
  } else
fa009d
  if (pickerMode) {
fa009d
    if (type == ME_DOWN) getViewer()->doPickGuideStroke(pos);
fa009d
  } else {
fa009d
    int    deviceId    = e.isTablet() ? 1 : 0;
fa009d
    bool   hasPressure = e.isTablet();
fa009d
    double pressure    = hasPressure ? e.m_pressure : 1.0;
fa009d
    bool   final       = type == ME_UP;
fa009d
    m_inputmanager.trackEvent(
fa009d
      deviceId, 0, snappedPos, pressure, TPointD(), hasPressure, false, final, t);
fa009d
    m_inputmanager.processTracks();
fa009d
  }
fa009d
}
fa009d
fa009d
//--------------------------------------------------------------------------------------------------
fa009d
fa009d
void ToonzVectorBrushTool::leftButtonDown(const TPointD &pos,
fa009d
                                        const TMouseEvent &e) {
fa009d
  handleMouseEvent(ME_DOWN, pos, e);
fa009d
}
fa009d
void ToonzVectorBrushTool::leftButtonDrag(const TPointD &pos,
fa009d
                                        const TMouseEvent &e) {
fa009d
  handleMouseEvent(ME_DRAG, pos, e);
fa009d
}
fa009d
void ToonzVectorBrushTool::leftButtonUp(const TPointD &pos,
fa009d
                                      const TMouseEvent &e) {
fa009d
  handleMouseEvent(ME_UP, pos, e);
fa009d
}
fa009d
void ToonzVectorBrushTool::mouseMove(const TPointD &pos, const TMouseEvent &e) {
fa009d
  handleMouseEvent(ME_MOVE, pos, e);
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//--------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
bool ToonzVectorBrushTool::keyDown(QKeyEvent *event) {
fa009d
  if (event->key() == Qt::Key_Escape)
shun-iwasawa 98926d
    resetFrameRange();
shun-iwasawa 98926d
  return false;
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//--------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
manongjohn 40a40e
bool ToonzVectorBrushTool::doFrameRangeStrokes(
manongjohn 40a40e
    TFrameId firstFrameId, TStroke *firstStroke, TFrameId lastFrameId,
manongjohn 40a40e
    TStroke *lastStroke, int interpolationType, bool breakAngles,
manongjohn 40a40e
    bool autoGroup, bool autoFill, bool drawFirstStroke, bool drawLastStroke,
manongjohn 40a40e
    bool withUndo) {
shun-iwasawa 98926d
  TXshSimpleLevel *sl =
shun-iwasawa 98926d
      TTool::getApplication()->getCurrentLevel()->getLevel()->getSimpleLevel();
shun-iwasawa 98926d
  TStroke *first           = new TStroke();
shun-iwasawa 98926d
  TStroke *last            = new TStroke();
shun-iwasawa 98926d
  TVectorImageP firstImage = new TVectorImage();
shun-iwasawa 98926d
  TVectorImageP lastImage  = new TVectorImage();
shun-iwasawa 98926d
fa009d
  bool swapped = firstFrameId > lastFrameId;
fa009d
  if (swapped) {
shun-iwasawa 98926d
    std::swap(firstFrameId, lastFrameId);
fa009d
    *first = *lastStroke;
fa009d
    *last  = *firstStroke;
fa009d
  } else {
fa009d
    *first = *firstStroke;
fa009d
    *last  = *lastStroke;
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  firstImage->addStroke(first, false);
shun-iwasawa 98926d
  lastImage->addStroke(last, false);
shun-iwasawa 98926d
  assert(firstFrameId <= lastFrameId);
shun-iwasawa 98926d
shun-iwasawa 98926d
  std::vector<tframeid> allFids;</tframeid>
shun-iwasawa 98926d
  sl->getFids(allFids);
shun-iwasawa 98926d
  std::vector<tframeid>::iterator i0 = allFids.begin();</tframeid>
shun-iwasawa 98926d
  while (i0 != allFids.end() && *i0 < firstFrameId) i0++;
shun-iwasawa 98926d
  if (i0 == allFids.end()) return false;
shun-iwasawa 98926d
  std::vector<tframeid>::iterator i1 = i0;</tframeid>
shun-iwasawa 98926d
  while (i1 != allFids.end() && *i1 <= lastFrameId) i1++;
shun-iwasawa 98926d
  assert(i0 < i1);
shun-iwasawa 98926d
  std::vector<tframeid> fids(i0, i1);</tframeid>
shun-iwasawa 98926d
  int m = fids.size();
shun-iwasawa 98926d
  assert(m > 0);
shun-iwasawa 98926d
manongjohn 40a40e
  if (withUndo) TUndoManager::manager()->beginBlock();
shun-iwasawa 443318
  int row       = getApplication()->getCurrentFrame()->isEditingScene()
shun-iwasawa 443318
                      ? getApplication()->getCurrentFrame()->getFrameIndex()
shun-iwasawa 443318
                      : -1;
manongjohn 40a40e
  TFrameId cFid = getApplication()->getCurrentFrame()->getFid();
shun-iwasawa 98926d
  for (int i = 0; i < m; ++i) {
shun-iwasawa 98926d
    TFrameId fid = fids[i];
shun-iwasawa 98926d
    assert(firstFrameId <= fid && fid <= lastFrameId);
shun-iwasawa 98926d
shun-iwasawa 98926d
    // This is an attempt to divide the tween evenly
shun-iwasawa 98926d
    double t = m > 1 ? (double)i / (double)(m - 1) : 0.5;
shun-iwasawa 98926d
    double s = t;
manongjohn 40a40e
    switch (interpolationType) {
shun-iwasawa 98926d
    case 1:  // LINEAR_WSTR
shun-iwasawa 98926d
      break;
shun-iwasawa 98926d
    case 2:  // EASEIN_WSTR
shun-iwasawa 98926d
      s = t * (2 - t);
shun-iwasawa 98926d
      break;  // s'(1) = 0
manongjohn f7197e
    case 3:   // EASEOUT_WSTR
manongjohn f7197e
      s = t * t;
manongjohn f7197e
      break;  // s'(0) = 0
shun-iwasawa 98926d
    case 4:   // EASEINOUT_WSTR:
shun-iwasawa 98926d
      s = t * t * (3 - 2 * t);
shun-iwasawa 98926d
      break;  // s'(0) = s'(1) = 0
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
shun-iwasawa 98926d
    TTool::Application *app = TTool::getApplication();
manongjohn 40a40e
    if (app) app->getCurrentFrame()->setFid(fid);
shun-iwasawa 98926d
shun-iwasawa 98926d
    TVectorImageP img = sl->getFrame(fid, true);
shun-iwasawa 98926d
    if (t == 0) {
shun-iwasawa 98926d
      if (!swapped && !drawFirstStroke) {
shun-iwasawa 98926d
      } else
shun-iwasawa 98926d
        addStrokeToImage(getApplication(), img, firstImage->getStroke(0),
manongjohn 40a40e
                         breakAngles, autoGroup, autoFill, m_isFrameCreated,
shun-iwasawa 98926d
                         m_isLevelCreated, sl, fid);
shun-iwasawa 98926d
    } else if (t == 1) {
shun-iwasawa 98926d
      if (swapped && !drawFirstStroke) {
manongjohn 40a40e
      } else if (drawLastStroke)
shun-iwasawa 98926d
        addStrokeToImage(getApplication(), img, lastImage->getStroke(0),
manongjohn 40a40e
                         breakAngles, autoGroup, autoFill, m_isFrameCreated,
shun-iwasawa 98926d
                         m_isLevelCreated, sl, fid);
shun-iwasawa 98926d
    } else {
shun-iwasawa 98926d
      assert(firstImage->getStrokeCount() == 1);
shun-iwasawa 98926d
      assert(lastImage->getStrokeCount() == 1);
shun-iwasawa 98926d
      TVectorImageP vi = TInbetween(firstImage, lastImage).tween(s);
shun-iwasawa 98926d
      assert(vi->getStrokeCount() == 1);
manongjohn 40a40e
      addStrokeToImage(getApplication(), img, vi->getStroke(0), breakAngles,
manongjohn 40a40e
                       autoGroup, autoFill, m_isFrameCreated, m_isLevelCreated,
manongjohn 40a40e
                       sl, fid);
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
  }
manongjohn 40a40e
  if (row != -1)
manongjohn 40a40e
    getApplication()->getCurrentFrame()->setFrame(row);
manongjohn 40a40e
  else
manongjohn 40a40e
    getApplication()->getCurrentFrame()->setFid(cFid);
manongjohn 40a40e
manongjohn 40a40e
  if (withUndo) TUndoManager::manager()->endBlock();
shun-iwasawa 98926d
  notifyImageChanged();
shun-iwasawa 98926d
  return true;
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//--------------------------------------------------------------------------------------------------
manongjohn 40a40e
bool ToonzVectorBrushTool::doGuidedAutoInbetween(
manongjohn 40a40e
    TFrameId cFid, const TVectorImageP &cvi, TStroke *cStroke, bool breakAngles,
manongjohn 40a40e
    bool autoGroup, bool autoFill, bool drawStroke) {
manongjohn 40a40e
  TApplication *app = TTool::getApplication();
manongjohn 40a40e
manongjohn 40a40e
  if (cFid.isEmptyFrame() || cFid.isNoFrame() || !cvi || !cStroke) return false;
manongjohn 40a40e
manongjohn 40a40e
  TXshSimpleLevel *sl = app->getCurrentLevel()->getLevel()->getSimpleLevel();
manongjohn 40a40e
  if (!sl) return false;
manongjohn 40a40e
manongjohn 40a40e
  int osBack  = -1;
manongjohn 40a40e
  int osFront = -1;
manongjohn 40a40e
manongjohn 40a40e
  getViewer()->getGuidedFrameIdx(&osBack, &osFront);
manongjohn 40a40e
manongjohn 40a40e
  TFrameHandle *currentFrame = getApplication()->getCurrentFrame();
manongjohn 40a40e
  bool resultBack            = false;
manongjohn 40a40e
  bool resultFront           = false;
manongjohn 40a40e
  TFrameId oFid;
shun-iwasawa ddc79b
  int cStrokeIdx = cvi->getStrokeCount();
manongjohn 4fae98
  if (!drawStroke) cStrokeIdx--;
manongjohn 40a40e
manongjohn 40a40e
  TUndoManager::manager()->beginBlock();
manongjohn 40a40e
  if (osBack != -1) {
manongjohn 40a40e
    if (currentFrame->isEditingScene()) {
manongjohn 40a40e
      TXsheet *xsh = app->getCurrentXsheet()->getXsheet();
manongjohn 40a40e
      int col      = app->getCurrentColumn()->getColumnIndex();
manongjohn 40a40e
      if (xsh && col >= 0) {
shun-iwasawa ddc79b
        TXshCell cell = xsh->getCell(osBack, col);
manongjohn 40a40e
        if (!cell.isEmpty()) oFid = cell.getFrameId();
manongjohn 40a40e
      }
manongjohn 40a40e
    } else
manongjohn 40a40e
      oFid = sl->getFrameId(osBack);
manongjohn 40a40e
manongjohn 40a40e
    TVectorImageP fvi = sl->getFrame(oFid, false);
manongjohn 40a40e
    int fStrokeCount  = fvi ? fvi->getStrokeCount() : 0;
manongjohn 40a40e
manongjohn 40a40e
    int strokeIdx = getViewer()->getGuidedBackStroke() != -1
manongjohn 40a40e
                        ? getViewer()->getGuidedBackStroke()
manongjohn 40a40e
                        : cStrokeIdx;
manongjohn 40a40e
manongjohn 40a40e
    if (!oFid.isEmptyFrame() && oFid != cFid && fvi && fStrokeCount &&
manongjohn 40a40e
        strokeIdx < fStrokeCount) {
manongjohn 40a40e
      TStroke *fStroke = fvi->getStroke(strokeIdx);
manongjohn 40a40e
manongjohn 4fae98
      bool frameCreated = m_isFrameCreated;
manongjohn 4fae98
      m_isFrameCreated  = false;
manongjohn 87652d
      touchImage();
shun-iwasawa 443318
      resultBack = doFrameRangeStrokes(
manongjohn 40a40e
          oFid, fStroke, cFid, cStroke,
manongjohn 40a40e
          Preferences::instance()->getGuidedInterpolation(), breakAngles,
manongjohn 40a40e
          autoGroup, autoFill, false, drawStroke, false);
manongjohn 4fae98
      m_isFrameCreated = frameCreated;
manongjohn 40a40e
    }
manongjohn 40a40e
  }
manongjohn 40a40e
manongjohn 40a40e
  if (osFront != -1) {
manongjohn 40a40e
    bool drawFirstStroke = (osBack != -1 && resultBack) ? false : true;
manongjohn 40a40e
manongjohn 40a40e
    if (currentFrame->isEditingScene()) {
manongjohn 40a40e
      TXsheet *xsh = app->getCurrentXsheet()->getXsheet();
manongjohn 40a40e
      int col      = app->getCurrentColumn()->getColumnIndex();
manongjohn 40a40e
      if (xsh && col >= 0) {
shun-iwasawa ddc79b
        TXshCell cell = xsh->getCell(osFront, col);
manongjohn 40a40e
        if (!cell.isEmpty()) oFid = cell.getFrameId();
manongjohn 40a40e
      }
manongjohn 40a40e
    } else
manongjohn 40a40e
      oFid = sl->getFrameId(osFront);
manongjohn 40a40e
manongjohn 40a40e
    TVectorImageP fvi = sl->getFrame(oFid, false);
manongjohn 40a40e
    int fStrokeCount  = fvi ? fvi->getStrokeCount() : 0;
manongjohn 40a40e
manongjohn 40a40e
    int strokeIdx = getViewer()->getGuidedFrontStroke() != -1
manongjohn 40a40e
                        ? getViewer()->getGuidedFrontStroke()
manongjohn 40a40e
                        : cStrokeIdx;
manongjohn 40a40e
manongjohn 40a40e
    if (!oFid.isEmptyFrame() && oFid != cFid && fvi && fStrokeCount &&
manongjohn 40a40e
        strokeIdx < fStrokeCount) {
manongjohn 40a40e
      TStroke *fStroke = fvi->getStroke(strokeIdx);
manongjohn 40a40e
manongjohn 4fae98
      bool frameCreated = m_isFrameCreated;
manongjohn 4fae98
      m_isFrameCreated  = false;
manongjohn 87652d
      touchImage();
shun-iwasawa 443318
      resultFront = doFrameRangeStrokes(
manongjohn 40a40e
          cFid, cStroke, oFid, fStroke,
manongjohn 40a40e
          Preferences::instance()->getGuidedInterpolation(), breakAngles,
manongjohn 40a40e
          autoGroup, autoFill, drawFirstStroke, false, false);
manongjohn 4fae98
      m_isFrameCreated = frameCreated;
manongjohn 40a40e
    }
manongjohn 40a40e
  }
manongjohn 40a40e
  TUndoManager::manager()->endBlock();
manongjohn 40a40e
manongjohn 40a40e
  return resultBack || resultFront;
manongjohn 40a40e
}
manongjohn 40a40e
manongjohn 40a40e
//--------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
fa009d
void ToonzVectorBrushTool::snap(const TPointD &pos, bool snapEnabled, bool withSelfSnap) {
fa009d
  bool oldSnapped      = m_snapped;
fa009d
  bool oldSnappedSelf  = m_snappedSelf;
fa009d
  TPointD oldPoint     = m_snapPoint;
fa009d
  TPointD oldPointSelf = m_snapPointSelf;
fa009d
  
fa009d
  m_snapped = m_snappedSelf = false;
fa009d
  
fa009d
  if (snapEnabled) { // snapping is active
shun-iwasawa 98926d
    double minDistance2 = m_minDistance2;
fa009d
    
fa009d
    // 0 - strokes, 1 - guides, 2 - all
fa009d
    int target = Preferences::instance()->getVectorSnappingTarget();
fa009d
fa009d
    // snap to guides
fa009d
    if (target != 0) {
fa009d
      if (TToolViewer *viewer = getViewer()) {
fa009d
        // find nearest vertical guide
fa009d
        int cnt = viewer->getVGuideCount();
fa009d
        for(int i = 0; i < cnt; ++i) {
fa009d
          double guide = viewer->getVGuide(i);
fa009d
          double d2 = guide - pos.y;
fa009d
          d2 *= d2; // we work with square of the distance
fa009d
          if (d2 < minDistance2) {
fa009d
            m_snapped = true;
fa009d
            m_snapPoint.x = pos.x;
fa009d
            m_snapPoint.y = guide;
fa009d
            minDistance2 = d2;
fa009d
          }
fa009d
        }
shun-iwasawa 98926d
fa009d
        // find nearest horizontal guide
fa009d
        cnt = viewer->getHGuideCount();
fa009d
        for(int i = 0; i < cnt; ++i) {
fa009d
          double guide = viewer->getHGuide(i);
fa009d
          double d2 = guide - pos.x;
fa009d
          d2 *= d2; // we work with square of the distance
fa009d
          if (d2 < minDistance2) {
fa009d
            m_snapped = true;
fa009d
            m_snapPoint.x = guide;
fa009d
            m_snapPoint.y = pos.y;
fa009d
            minDistance2 = d2;
fa009d
          }
shun-iwasawa 98926d
        }
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
fa009d
    // snap to strokes
fa009d
    if (target != 1) {
fa009d
      if (TVectorImageP vi = getImage(false)) {
fa009d
        int count = vi->getStrokeCount();
fa009d
        for(int i = 0; i < count; ++i) {
fa009d
          double w, d2;
fa009d
          TStroke *stroke = vi->getStroke(i);
fa009d
          if (!stroke->getNearestW(pos, w, d2) || d2 >= minDistance2)
fa009d
            continue;
fa009d
          minDistance2 = d2;
fa009d
          w = w > 0.001 ? (w < 0.999 ? w : 1.0) : 0.0;
fa009d
          m_snapped = true;
fa009d
          m_snapPoint = stroke->getPoint(w);
shun-iwasawa 98926d
        }
shun-iwasawa 98926d
      }
fa009d
    
fa009d
      // finally snap to first point of track (self snap)
fa009d
      if (withSelfSnap && !m_tracks.empty() && !m_tracks.front().isEmpty()) {
fa009d
        TPointD p = m_tracks.front().getFirstPoint();
fa009d
        double d2 = tdistance2(pos, p);
fa009d
        if (d2 < minDistance2) {
fa009d
          m_snappedSelf = true;
fa009d
          m_snapPointSelf = p;
shun-iwasawa 98926d
        }
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
    }
fa009d
  } // snapping is active
fa009d
  
fa009d
  // invalidate rect
fa009d
  TRectD invalidateRect;
fa009d
  double radius = 8.0*m_pixelSize;
fa009d
  TPointD halfSize(radius, radius);
shun-iwasawa 98926d
fa009d
  if ( oldSnapped != m_snapped
fa009d
    || !areAlmostEqual(oldPoint, m_snapPoint) )
fa009d
  {
fa009d
    if (oldSnapped) invalidateRect += TRectD(oldPoint - halfSize, oldPoint + halfSize);
fa009d
    if (m_snapped)  invalidateRect += TRectD(m_snapPoint - halfSize, m_snapPoint + halfSize);
shun-iwasawa 98926d
  }
fa009d
  
fa009d
  if ( oldSnappedSelf != m_snappedSelf
fa009d
    || !areAlmostEqual(oldPointSelf, m_snapPointSelf) )
fa009d
  {
fa009d
    if (oldSnappedSelf) invalidateRect += TRectD(oldPointSelf - halfSize, oldPointSelf + halfSize);
fa009d
    if (m_snappedSelf)  invalidateRect += TRectD(m_snapPointSelf - halfSize, m_snapPointSelf + halfSize);
fa009d
  }
fa009d
  
fa009d
  if (!invalidateRect.isEmpty())
fa009d
    invalidate(invalidateRect);
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//-------------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::draw() {
fa009d
  m_pixelSize = getPixelSize();
fa009d
  m_inputmanager.draw();
fa009d
  
Rozhuk Ivan 823a31
  /*--ショートカットでのツール切り替え時に赤点が描かれるのを防止する--*/
shun-iwasawa 98926d
  if (m_minThick == 0 && m_maxThick == 0 &&
shun-iwasawa 98926d
      !Preferences::instance()->getShow0ThickLines())
shun-iwasawa 98926d
    return;
shun-iwasawa 98926d
fa009d
  // draw track
fa009d
  tglColor(m_currentColor);
fa009d
  for(TrackList::iterator i = m_tracks.begin(); i != m_tracks.end(); ++i)
fa009d
    i->drawAllFragments();
fa009d
  
fa009d
  // draw snapping
fa009d
  double snapMarkRadius = 6.0 * m_pixelSize;
fa009d
  if (m_snapped) {
fa009d
    tglColor(TPixelD(0.1, 0.9, 0.1));
fa009d
    tglDrawCircle(m_snapPoint, snapMarkRadius);
fa009d
  }
fa009d
  if (m_snappedSelf) {
fa009d
    tglColor(TPixelD(0.9, 0.9, 0.1));
fa009d
    tglDrawCircle(m_snapPointSelf, snapMarkRadius);
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  // frame range
fa009d
  for(TrackList::iterator i = m_rangeTracks.begin(); i != m_rangeTracks.end(); ++i) {
fa009d
    if (i->isEmpty()) continue;
fa009d
    TPointD offset1 = TPointD(5, 5);
fa009d
    TPointD offset2 = TPointD(-offset1.x, offset1.y);
fa009d
    TPointD point = i->getFirstPoint();
shun-iwasawa 98926d
    glColor3d(1.0, 0.0, 0.0);
fa009d
    i->drawAllFragments();
shun-iwasawa 98926d
    glColor3d(0.0, 0.6, 0.0);
fa009d
    tglDrawSegment(point - offset1, point + offset1);
fa009d
    tglDrawSegment(point - offset2, point + offset2);
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  if (getApplication()->getCurrentObject()->isSpline()) return;
shun-iwasawa 98926d
shun-iwasawa 98926d
  // If toggled off, don't draw brush outline
shun-iwasawa 98926d
  if (!Preferences::instance()->isCursorOutlineEnabled()) return;
shun-iwasawa 98926d
manongjohn 40a40e
  // Don't draw brush outline if picking guiding stroke
manongjohn 40a40e
  if (getViewer()->getGuidedStrokePickerMode()) return;
manongjohn 40a40e
shun-iwasawa 98926d
  // Draw the brush outline - change color when the Ink / Paint check is
shun-iwasawa 98926d
  // activated
shun-iwasawa 98926d
  if ((ToonzCheck::instance()->getChecks() & ToonzCheck::eInk) ||
shun-iwasawa 98926d
      (ToonzCheck::instance()->getChecks() & ToonzCheck::ePaint) ||
shun-iwasawa 98926d
      (ToonzCheck::instance()->getChecks() & ToonzCheck::eInk1))
shun-iwasawa 98926d
    glColor3d(0.5, 0.8, 0.8);
shun-iwasawa 98926d
  // normally draw in red
shun-iwasawa 98926d
  else
shun-iwasawa 98926d
    glColor3d(1.0, 0.0, 0.0);
shun-iwasawa 98926d
shun-iwasawa 98926d
  tglDrawCircle(m_brushPos, 0.5 * m_minThick);
shun-iwasawa 98926d
  tglDrawCircle(m_brushPos, 0.5 * m_maxThick);
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//--------------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::onEnter() {
shun-iwasawa 98926d
  m_minThick = m_thickness.getValue().first;
shun-iwasawa 98926d
  m_maxThick = m_thickness.getValue().second;
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//----------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::onLeave() {
shun-iwasawa 98926d
  m_minThick = 0;
shun-iwasawa 98926d
  m_maxThick = 0;
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//----------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
TPropertyGroup *ToonzVectorBrushTool::getProperties(int idx) {
shun-iwasawa 98926d
  if (!m_presetsLoaded) initPresets();
shun-iwasawa 98926d
  return &m_prop[idx];
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::resetFrameRange() {
fa009d
  m_rangeTracks.clear();
shun-iwasawa 98926d
  m_firstFrameId = -1;
fa009d
  deleteStrokes(m_firstStrokes);
shun-iwasawa 98926d
  m_firstFrameRange = true;
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
bool ToonzVectorBrushTool::onPropertyChanged(std::string propertyName) {
manongjohn 5a2268
  if (m_propertyUpdating) return true;
manongjohn 5a2268
shun-iwasawa 98926d
  // Set the following to true whenever a different piece of interface must
shun-iwasawa 98926d
  // be refreshed - done once at the end.
shun-iwasawa 98926d
  bool notifyTool = false;
shun-iwasawa 98926d
manongjohn df5842
  if (propertyName == m_preset.getName()) {
manongjohn df5842
    if (m_preset.getValue() != CUSTOM_WSTR)
manongjohn df5842
      loadPreset();
manongjohn df5842
    else  // Chose <custom>, go back to last saved brush settings</custom>
manongjohn df5842
      loadLastBrush();
manongjohn df5842
manongjohn df5842
    V_VectorBrushPreset = m_preset.getValueAsString();
manongjohn 5a2268
    m_propertyUpdating  = true;
manongjohn df5842
    getApplication()->getCurrentTool()->notifyToolChanged();
manongjohn 5a2268
    m_propertyUpdating = false;
manongjohn df5842
    return true;
manongjohn df5842
  }
manongjohn df5842
manongjohn 0dcb3a
  // Switch to <custom> only if it's a preset property change</custom>
manongjohn 0dcb3a
  if (m_preset.getValue() != CUSTOM_WSTR &&
manongjohn 0dcb3a
      (propertyName == m_thickness.getName() ||
manongjohn 0dcb3a
       propertyName == m_accuracy.getName() ||
manongjohn 0dcb3a
       propertyName == m_smooth.getName() ||
manongjohn 0dcb3a
       propertyName == m_breakAngles.getName() ||
manongjohn 0dcb3a
       propertyName == m_pressure.getName() ||
manongjohn 0dcb3a
       propertyName == m_capStyle.getName() ||
manongjohn 0dcb3a
       propertyName == m_joinStyle.getName() ||
manongjohn 0dcb3a
       propertyName == m_miterJoinLimit.getName())) {
manongjohn 0dcb3a
    m_preset.setValue(CUSTOM_WSTR);
manongjohn 0dcb3a
    V_VectorBrushPreset = m_preset.getValueAsString();
manongjohn 0dcb3a
    notifyTool          = true;
manongjohn 0dcb3a
  }
manongjohn 0dcb3a
manongjohn 0dcb3a
  // Properties tracked with preset. Update only on <custom></custom>
manongjohn 0dcb3a
  if (m_preset.getValue() == CUSTOM_WSTR) {
manongjohn 0dcb3a
    V_VectorBrushMinSize       = m_thickness.getValue().first;
manongjohn 0dcb3a
    V_VectorBrushMaxSize       = m_thickness.getValue().second;
manongjohn 0dcb3a
    V_BrushAccuracy            = m_accuracy.getValue();
manongjohn 0dcb3a
    V_BrushSmooth              = m_smooth.getValue();
manongjohn 0dcb3a
    V_BrushBreakSharpAngles    = m_breakAngles.getValue();
shun-iwasawa 98926d
    V_BrushPressureSensitivity = m_pressure.getValue();
manongjohn 0dcb3a
    V_VectorCapStyle           = m_capStyle.getIndex();
manongjohn 0dcb3a
    V_VectorJoinStyle          = m_joinStyle.getIndex();
manongjohn 0dcb3a
    V_VectorMiterValue         = m_miterJoinLimit.getValue();
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
manongjohn 0dcb3a
  // Properties not tracked with preset
manongjohn 0dcb3a
  int frameIndex               = m_frameRange.getIndex();
manongjohn 0dcb3a
  V_VectorBrushFrameRange      = frameIndex;
manongjohn 0dcb3a
  V_VectorBrushSnap            = m_snap.getValue();
manongjohn 0dcb3a
  int snapSensitivityIndex     = m_snapSensitivity.getIndex();
manongjohn 0dcb3a
  V_VectorBrushSnapSensitivity = snapSensitivityIndex;
fa009d
  V_VectorBrushAssistants      = m_assistants.getValue();
shun-iwasawa 98926d
manongjohn 0dcb3a
  // Recalculate/reset based on changed settings
manongjohn 0dcb3a
  m_minThick = m_thickness.getValue().first;
manongjohn 0dcb3a
  m_maxThick = m_thickness.getValue().second;
manongjohn 0dcb3a
manongjohn 0dcb3a
  if (frameIndex == 0) resetFrameRange();
manongjohn 0dcb3a
manongjohn 0dcb3a
  switch (snapSensitivityIndex) {
manongjohn 0dcb3a
  case 0:
manongjohn 0dcb3a
    m_minDistance2 = SNAPPING_LOW;
manongjohn 0dcb3a
    break;
manongjohn 0dcb3a
  case 1:
manongjohn 0dcb3a
    m_minDistance2 = SNAPPING_MEDIUM;
manongjohn 0dcb3a
    break;
manongjohn 0dcb3a
  case 2:
manongjohn 0dcb3a
    m_minDistance2 = SNAPPING_HIGH;
manongjohn 0dcb3a
    break;
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
manongjohn 0dcb3a
  if (propertyName == m_joinStyle.getName()) notifyTool = true;
manongjohn 0dcb3a
manongjohn 5a2268
  if (notifyTool) {
manongjohn 5a2268
    m_propertyUpdating = true;
manongjohn 5a2268
    getApplication()->getCurrentTool()->notifyToolChanged();
manongjohn 5a2268
    m_propertyUpdating = false;
manongjohn 5a2268
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  return true;
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::initPresets() {
shun-iwasawa 98926d
  if (!m_presetsLoaded) {
shun-iwasawa 98926d
    // If necessary, load the presets from file
shun-iwasawa 98926d
    m_presetsLoaded = true;
shun-iwasawa 98926d
    m_presetsManager.load(TEnv::getConfigDir() + "brush_vector.txt");
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  // Rebuild the presets property entries
shun-iwasawa 98926d
  const std::set<vectorbrushdata> &presets = m_presetsManager.presets();</vectorbrushdata>
shun-iwasawa 98926d
shun-iwasawa 98926d
  m_preset.deleteAllValues();
shun-iwasawa 98926d
  m_preset.addValue(CUSTOM_WSTR);
shun-iwasawa 98926d
  m_preset.setItemUIName(CUSTOM_WSTR, tr("<custom>"));</custom>
shun-iwasawa 98926d
shun-iwasawa 98926d
  std::set<vectorbrushdata>::const_iterator it, end = presets.end();</vectorbrushdata>
shun-iwasawa 98926d
  for (it = presets.begin(); it != end; ++it) m_preset.addValue(it->m_name);
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//----------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::loadPreset() {
shun-iwasawa 98926d
  const std::set<vectorbrushdata> &presets = m_presetsManager.presets();</vectorbrushdata>
shun-iwasawa 98926d
  std::set<vectorbrushdata>::const_iterator it;</vectorbrushdata>
shun-iwasawa 98926d
shun-iwasawa 98926d
  it = presets.find(VectorBrushData(m_preset.getValue()));
shun-iwasawa 98926d
  if (it == presets.end()) return;
shun-iwasawa 98926d
shun-iwasawa 98926d
  const VectorBrushData &preset = *it;
shun-iwasawa 98926d
shun-iwasawa 98926d
  try  // Don't bother with RangeErrors
shun-iwasawa 98926d
  {
shun-iwasawa 98926d
    m_thickness.setValue(
shun-iwasawa 98926d
        TDoublePairProperty::Value(preset.m_min, preset.m_max));
shun-iwasawa 98926d
    m_accuracy.setValue(preset.m_acc, true);
shun-iwasawa 98926d
    m_smooth.setValue(preset.m_smooth, true);
shun-iwasawa 98926d
    m_breakAngles.setValue(preset.m_breakAngles);
shun-iwasawa 98926d
    m_pressure.setValue(preset.m_pressure);
shun-iwasawa 98926d
    m_capStyle.setIndex(preset.m_cap);
shun-iwasawa 98926d
    m_joinStyle.setIndex(preset.m_join);
shun-iwasawa 98926d
    m_miterJoinLimit.setValue(preset.m_miter);
shun-iwasawa 98926d
manongjohn 0dcb3a
    // Recalculate based on updated presets
manongjohn 0dcb3a
    m_minThick = m_thickness.getValue().first;
manongjohn 0dcb3a
    m_maxThick = m_thickness.getValue().second;
shun-iwasawa 98926d
  } catch (...) {
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::addPreset(QString name) {
shun-iwasawa 98926d
  // Build the preset
shun-iwasawa 98926d
  VectorBrushData preset(name.toStdWString());
shun-iwasawa 98926d
shun-iwasawa 98926d
  preset.m_min = m_thickness.getValue().first;
shun-iwasawa 98926d
  preset.m_max = m_thickness.getValue().second;
shun-iwasawa 98926d
shun-iwasawa 98926d
  preset.m_acc         = m_accuracy.getValue();
shun-iwasawa 98926d
  preset.m_smooth      = m_smooth.getValue();
shun-iwasawa 98926d
  preset.m_breakAngles = m_breakAngles.getValue();
shun-iwasawa 98926d
  preset.m_pressure    = m_pressure.getValue();
shun-iwasawa 98926d
  preset.m_cap         = m_capStyle.getIndex();
shun-iwasawa 98926d
  preset.m_join        = m_joinStyle.getIndex();
shun-iwasawa 98926d
  preset.m_miter       = m_miterJoinLimit.getValue();
shun-iwasawa 98926d
shun-iwasawa 98926d
  // Pass the preset to the manager
shun-iwasawa 98926d
  m_presetsManager.addPreset(preset);
shun-iwasawa 98926d
shun-iwasawa 98926d
  // Reinitialize the associated preset enum
shun-iwasawa 98926d
  initPresets();
shun-iwasawa 98926d
shun-iwasawa 98926d
  // Set the value to the specified one
shun-iwasawa 98926d
  m_preset.setValue(preset.m_name);
manongjohn 8b8d41
  V_VectorBrushPreset = m_preset.getValueAsString();
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::removePreset() {
shun-iwasawa 98926d
  std::wstring name(m_preset.getValue());
shun-iwasawa 98926d
  if (name == CUSTOM_WSTR) return;
shun-iwasawa 98926d
shun-iwasawa 98926d
  m_presetsManager.removePreset(name);
shun-iwasawa 98926d
  initPresets();
shun-iwasawa 98926d
shun-iwasawa 98926d
  // No parameter change, and set the preset value to custom
shun-iwasawa 98926d
  m_preset.setValue(CUSTOM_WSTR);
manongjohn 8b8d41
  V_VectorBrushPreset = m_preset.getValueAsString();
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//------------------------------------------------------------------
manongjohn df5842
manongjohn df5842
void ToonzVectorBrushTool::loadLastBrush() {
manongjohn 0dcb3a
  // Properties tracked with preset
manongjohn df5842
  m_thickness.setValue(
manongjohn df5842
      TDoublePairProperty::Value(V_VectorBrushMinSize, V_VectorBrushMaxSize));
manongjohn df5842
  m_capStyle.setIndex(V_VectorCapStyle);
manongjohn df5842
  m_joinStyle.setIndex(V_VectorJoinStyle);
manongjohn df5842
  m_miterJoinLimit.setValue(V_VectorMiterValue);
manongjohn df5842
  m_breakAngles.setValue(V_BrushBreakSharpAngles ? 1 : 0);
manongjohn df5842
  m_accuracy.setValue(V_BrushAccuracy);
manongjohn df5842
  m_pressure.setValue(V_BrushPressureSensitivity ? 1 : 0);
manongjohn df5842
  m_smooth.setValue(V_BrushSmooth);
manongjohn df5842
manongjohn 0dcb3a
  // Properties not tracked with preset
manongjohn df5842
  m_frameRange.setIndex(V_VectorBrushFrameRange);
fa009d
  m_snap.setValue(V_VectorBrushSnap ? 1 : 0);
manongjohn df5842
  m_snapSensitivity.setIndex(V_VectorBrushSnapSensitivity);
fa009d
  m_assistants.setValue(V_VectorBrushAssistants ? 1 : 0);
manongjohn 0dcb3a
manongjohn 0dcb3a
  // Recalculate based on prior values
manongjohn 0dcb3a
  m_minThick = m_thickness.getValue().first;
manongjohn 0dcb3a
  m_maxThick = m_thickness.getValue().second;
manongjohn 0dcb3a
manongjohn df5842
  switch (V_VectorBrushSnapSensitivity) {
manongjohn df5842
  case 0:
manongjohn df5842
    m_minDistance2 = SNAPPING_LOW;
manongjohn df5842
    break;
manongjohn df5842
  case 1:
manongjohn df5842
    m_minDistance2 = SNAPPING_MEDIUM;
manongjohn df5842
    break;
manongjohn df5842
  case 2:
manongjohn df5842
    m_minDistance2 = SNAPPING_HIGH;
manongjohn df5842
    break;
manongjohn df5842
  }
manongjohn df5842
}
manongjohn df5842
manongjohn df5842
//------------------------------------------------------------------
Rozhuk Ivan 823a31
/*!	Brush、PaintBrush、EraserToolがPencilModeのときにTrueを返す
shun-iwasawa 76d093
 */
shun-iwasawa 98926d
bool ToonzVectorBrushTool::isPencilModeActive() { return false; }
shun-iwasawa 98926d
shun-iwasawa 98926d
//==========================================================================================================
shun-iwasawa 98926d
shun-iwasawa 98926d
// Tools instantiation
shun-iwasawa 98926d
shun-iwasawa 98926d
ToonzVectorBrushTool vectorPencil("T_Brush",
shun-iwasawa 98926d
                                  TTool::Vectors | TTool::EmptyTarget);
shun-iwasawa 98926d
shun-iwasawa 98926d
//*******************************************************************************
shun-iwasawa 98926d
//    Brush Data implementation
shun-iwasawa 98926d
//*******************************************************************************
shun-iwasawa 98926d
shun-iwasawa 98926d
VectorBrushData::VectorBrushData()
shun-iwasawa 98926d
    : m_name()
shun-iwasawa 98926d
    , m_min(0.0)
shun-iwasawa 98926d
    , m_max(0.0)
shun-iwasawa 98926d
    , m_acc(0.0)
shun-iwasawa 98926d
    , m_smooth(0.0)
shun-iwasawa 98926d
    , m_breakAngles(false)
shun-iwasawa 98926d
    , m_pressure(false)
shun-iwasawa 98926d
    , m_cap(0)
shun-iwasawa 98926d
    , m_join(0)
shun-iwasawa 98926d
    , m_miter(0) {}
shun-iwasawa 98926d
shun-iwasawa 98926d
//----------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
VectorBrushData::VectorBrushData(const std::wstring &name)
shun-iwasawa 98926d
    : m_name(name)
shun-iwasawa 98926d
    , m_min(0.0)
shun-iwasawa 98926d
    , m_max(0.0)
shun-iwasawa 98926d
    , m_acc(0.0)
shun-iwasawa 98926d
    , m_smooth(0.0)
shun-iwasawa 98926d
    , m_breakAngles(false)
shun-iwasawa 98926d
    , m_pressure(false)
shun-iwasawa 98926d
    , m_cap(0)
shun-iwasawa 98926d
    , m_join(0)
shun-iwasawa 98926d
    , m_miter(0) {}
shun-iwasawa 98926d
shun-iwasawa 98926d
//----------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void VectorBrushData::saveData(TOStream &os) {
shun-iwasawa 98926d
  os.openChild("Name");
shun-iwasawa 98926d
  os << m_name;
shun-iwasawa 98926d
  os.closeChild();
shun-iwasawa 98926d
  os.openChild("Thickness");
shun-iwasawa 98926d
  os << m_min << m_max;
shun-iwasawa 98926d
  os.closeChild();
shun-iwasawa 98926d
  os.openChild("Accuracy");
shun-iwasawa 98926d
  os << m_acc;
shun-iwasawa 98926d
  os.closeChild();
shun-iwasawa 98926d
  os.openChild("Smooth");
shun-iwasawa 98926d
  os << m_smooth;
shun-iwasawa 98926d
  os.closeChild();
shun-iwasawa 98926d
  os.openChild("Break_Sharp_Angles");
shun-iwasawa 98926d
  os << (int)m_breakAngles;
shun-iwasawa 98926d
  os.closeChild();
shun-iwasawa 98926d
  os.openChild("Pressure_Sensitivity");
shun-iwasawa 98926d
  os << (int)m_pressure;
shun-iwasawa 98926d
  os.closeChild();
shun-iwasawa 98926d
  os.openChild("Cap");
shun-iwasawa 98926d
  os << m_cap;
shun-iwasawa 98926d
  os.closeChild();
shun-iwasawa 98926d
  os.openChild("Join");
shun-iwasawa 98926d
  os << m_join;
shun-iwasawa 98926d
  os.closeChild();
shun-iwasawa 98926d
  os.openChild("Miter");
shun-iwasawa 98926d
  os << m_miter;
shun-iwasawa 98926d
  os.closeChild();
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//----------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void VectorBrushData::loadData(TIStream &is) {
shun-iwasawa 98926d
  std::string tagName;
shun-iwasawa 98926d
  int val;
shun-iwasawa 98926d
shun-iwasawa 98926d
  while (is.matchTag(tagName)) {
shun-iwasawa 98926d
    if (tagName == "Name")
shun-iwasawa 98926d
      is >> m_name, is.matchEndTag();
shun-iwasawa 98926d
    else if (tagName == "Thickness")
shun-iwasawa 98926d
      is >> m_min >> m_max, is.matchEndTag();
shun-iwasawa 98926d
    else if (tagName == "Accuracy")
shun-iwasawa 98926d
      is >> m_acc, is.matchEndTag();
shun-iwasawa 98926d
    else if (tagName == "Smooth")
shun-iwasawa 98926d
      is >> m_smooth, is.matchEndTag();
shun-iwasawa 98926d
    else if (tagName == "Break_Sharp_Angles")
shun-iwasawa 98926d
      is >> val, m_breakAngles = val, is.matchEndTag();
shun-iwasawa 98926d
    else if (tagName == "Pressure_Sensitivity")
shun-iwasawa 98926d
      is >> val, m_pressure = val, is.matchEndTag();
shun-iwasawa 98926d
    else if (tagName == "Cap")
shun-iwasawa 98926d
      is >> m_cap, is.matchEndTag();
shun-iwasawa 98926d
    else if (tagName == "Join")
shun-iwasawa 98926d
      is >> m_join, is.matchEndTag();
shun-iwasawa 98926d
    else if (tagName == "Miter")
shun-iwasawa 98926d
      is >> m_miter, is.matchEndTag();
shun-iwasawa 98926d
    else
shun-iwasawa 98926d
      is.skipCurrentTag();
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//----------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
PERSIST_IDENTIFIER(VectorBrushData, "VectorBrushData");
shun-iwasawa 98926d
shun-iwasawa 98926d
//*******************************************************************************
shun-iwasawa 98926d
//    Brush Preset Manager implementation
shun-iwasawa 98926d
//*******************************************************************************
shun-iwasawa 98926d
shun-iwasawa 98926d
void VectorBrushPresetManager::load(const TFilePath &fp) {
shun-iwasawa 98926d
  m_fp = fp;
shun-iwasawa 98926d
shun-iwasawa 98926d
  std::string tagName;
shun-iwasawa 98926d
  VectorBrushData data;
shun-iwasawa 98926d
shun-iwasawa 98926d
  TIStream is(m_fp);
shun-iwasawa 98926d
  try {
shun-iwasawa 98926d
    while (is.matchTag(tagName)) {
shun-iwasawa 98926d
      if (tagName == "version") {
shun-iwasawa 98926d
        VersionNumber version;
shun-iwasawa 98926d
        is >> version.first >> version.second;
shun-iwasawa 98926d
shun-iwasawa 98926d
        is.setVersion(version);
shun-iwasawa 98926d
        is.matchEndTag();
shun-iwasawa 98926d
      } else if (tagName == "brushes") {
shun-iwasawa 98926d
        while (is.matchTag(tagName)) {
shun-iwasawa 98926d
          if (tagName == "brush") {
shun-iwasawa 98926d
            is >> data, m_presets.insert(data);
shun-iwasawa 98926d
            is.matchEndTag();
shun-iwasawa 98926d
          } else
shun-iwasawa 98926d
            is.skipCurrentTag();
shun-iwasawa 98926d
        }
shun-iwasawa 98926d
shun-iwasawa 98926d
        is.matchEndTag();
shun-iwasawa 98926d
      } else
shun-iwasawa 98926d
        is.skipCurrentTag();
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
  } catch (...) {
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void VectorBrushPresetManager::save() {
shun-iwasawa 98926d
  TOStream os(m_fp);
shun-iwasawa 98926d
shun-iwasawa 98926d
  os.openChild("version");
shun-iwasawa 98926d
  os << 1 << 20;
shun-iwasawa 98926d
  os.closeChild();
shun-iwasawa 98926d
shun-iwasawa 98926d
  os.openChild("brushes");
shun-iwasawa 98926d
shun-iwasawa 98926d
  std::set<vectorbrushdata>::iterator it, end = m_presets.end();</vectorbrushdata>
shun-iwasawa 98926d
  for (it = m_presets.begin(); it != end; ++it) {
shun-iwasawa 98926d
    os.openChild("brush");
shun-iwasawa 98926d
    os << (TPersist &)*it;
shun-iwasawa 98926d
    os.closeChild();
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  os.closeChild();
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void VectorBrushPresetManager::addPreset(const VectorBrushData &data) {
shun-iwasawa 98926d
  m_presets.erase(data);  // Overwriting insertion
shun-iwasawa 98926d
  m_presets.insert(data);
shun-iwasawa 98926d
  save();
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void VectorBrushPresetManager::removePreset(const std::wstring &name) {
shun-iwasawa 98926d
  m_presets.erase(VectorBrushData(name));
shun-iwasawa 98926d
  save();
shun-iwasawa 98926d
}