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"
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);
0d5555
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,
c9521d
                        bool enablePressure, bool isPath ) {
shun-iwasawa 98926d
  if (isPath) return 0.0;
c9521d
  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)
c9521d
    , m_snap("Snap", false)
c9521d
    , m_frameRange("Range:")
c9521d
    , m_snapSensitivity("Sensitivity:")
shun-iwasawa 98926d
    , m_capStyle("Cap")
shun-iwasawa 98926d
    , m_joinStyle("Join")
shun-iwasawa 98926d
    , m_miterJoinLimit("Miter:", 0, 100, 4)
0d5555
    , m_assistants("Assistants", true)
b8dbde
    , m_styleId()
b8dbde
    , m_minThick()
b8dbde
    , m_maxThick()
b8dbde
    , m_col()
b8dbde
    , m_firstFrame()
b8dbde
    , m_veryFirstFrame()
b8dbde
    , m_veryFirstCol()
c9521d
    , m_targetType(targetType)
b8dbde
    , m_pixelSize()
b8dbde
    , m_minDistance2()
398cd8
    , m_snapped()
398cd8
    , m_snappedSelf()
b8dbde
    , m_active()
shun-iwasawa 98926d
    , m_firstTime(true)
b8dbde
    , m_isPath()
b8dbde
    , m_presetsLoaded()
c9521d
    , m_firstFrameRange(true)
b8dbde
    , m_propertyUpdating()
c9521d
{
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
0d5555
  m_prop[0].bind(m_assistants);
0d5555
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");
0d5555
  m_assistants.setId("Assistants");
ebedef
ebedef
  m_inputmanager.setHandler(this);
ebedef
  m_modifierLine               = new TModifierLine();
ebedef
  m_modifierTangents           = new TModifierTangents();
ebedef
  m_modifierAssistants         = new TModifierAssistants();
ebedef
  m_modifierSegmentation       = new TModifierSegmentation();
ebedef
  m_modifierSmoothSegmentation = new TModifierSegmentation(TPointD(1, 1), 3);
ebedef
  for(int i = 0; i < 3; ++i)
ebedef
    m_modifierSmooth[i]        = new TModifierSmooth();
ebedef
  m_modifierSimplify           = new TModifierSimplify();
ebedef
#ifndef NDEBUG
bdb1e2
  m_modifierTest = new TModifierTest();
ebedef
#endif
ebedef
ebedef
  m_inputmanager.addModifier(
ebedef
      TInputModifierP(m_modifierAssistants.getPointer()));
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//-------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
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("");
0d5555
  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();
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.
b8dbde
  m_inputmanager.finishTracks();
shun-iwasawa 98926d
  resetFrameRange();
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//--------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
ebedef
void ToonzVectorBrushTool::inputMouseMove(
ebedef
  const TPointD &position, const TInputState &state )
ebedef
{
ebedef
  struct Locals {
ebedef
    ToonzVectorBrushTool *m_this;
manongjohn 40a40e
ebedef
    void setValue(TDoublePairProperty &prop,
ebedef
                  const TDoublePairProperty::Value &value) {
ebedef
      prop.setValue(value);
shun-iwasawa 98926d
ebedef
      m_this->onPropertyChanged(prop.getName());
ebedef
      TTool::getApplication()->getCurrentTool()->notifyToolChanged();
ebedef
    }
shun-iwasawa 98926d
ebedef
    void addMinMax(TDoublePairProperty &prop, double min, double max) {
ebedef
      if (min == 0.0 && max == 0.0) return;
ebedef
      const TDoublePairProperty::Range &range = prop.getRange();
ebedef
ebedef
      TDoublePairProperty::Value value = prop.getValue();
ebedef
      value.first += min;
ebedef
      value.second += max;
ebedef
      if (value.first > value.second) value.first = value.second;
ebedef
      value.first  = tcrop(value.first, range.first, range.second);
ebedef
      value.second = tcrop(value.second, range.first, range.second);
ebedef
ebedef
      setValue(prop, value);
ebedef
    }
ebedef
ebedef
  } locals = {this};
ebedef
ebedef
  TPointD halfThick(m_maxThick * 0.5, m_maxThick * 0.5);
ebedef
  TRectD invalidateRect(m_brushPos - halfThick, m_brushPos + halfThick);
ebedef
ebedef
  bool alt     = state.isKeyPressed(TInputState::Key::alt);
ebedef
  bool shift   = state.isKeyPressed(TInputState::Key::shift);
ebedef
  bool control = state.isKeyPressed(TInputState::Key::control);
c9521d
  
ebedef
  if ( alt && control && !shift
ebedef
    && Preferences::instance()->useCtrlAltToResizeBrushEnabled() )
ebedef
  {
ebedef
    // Resize the brush if CTRL+ALT is pressed and the preference is enabled.
ebedef
    const TPointD &diff = position - m_mousePos;
ebedef
    double max          = diff.x / 2;
ebedef
    double min          = diff.y / 2;
shun-iwasawa 98926d
ebedef
    locals.addMinMax(m_thickness, min, max);
manongjohn 40a40e
ebedef
    double radius = m_thickness.getValue().second * 0.5;
ebedef
    halfThick = TPointD(radius, radius);
ebedef
  } else {
ebedef
    m_brushPos = m_mousePos = position;
ebedef
  }
c9521d
  
ebedef
  invalidateRect += TRectD(m_brushPos - halfThick, m_brushPos + halfThick);
ebedef
ebedef
  if (m_minThick == 0 && m_maxThick == 0) {
ebedef
    m_minThick = m_thickness.getValue().first;
ebedef
    m_maxThick = m_thickness.getValue().second;
ebedef
  }
ebedef
ebedef
  invalidate(invalidateRect.enlarge(2));
ebedef
}
ebedef
ebedef
//--------------------------------------------------------------------------------------------------
ebedef
d04010
void ToonzVectorBrushTool::deleteStrokes(StrokeList &strokes) {
d04010
  for(StrokeList::iterator i = strokes.begin(); i != strokes.end(); ++i)
d04010
    delete *i;
d04010
  strokes.clear();
d04010
}
d04010
d04010
//--------------------------------------------------------------------------------------------------
d04010
d04010
void ToonzVectorBrushTool::copyStrokes(StrokeList &dst, const StrokeList &src) {
d04010
  deleteStrokes(dst);
d04010
  dst.reserve(src.size());
d04010
  for(StrokeList::const_iterator i = src.begin(); i != src.end(); ++i)
d04010
    dst.push_back(new TStroke(**i));
d04010
}
d04010
d04010
//--------------------------------------------------------------------------------------------------
d04010
ebedef
void ToonzVectorBrushTool::inputSetBusy(bool busy) {
ebedef
  if (m_active == busy) return;
c9521d
  
ebedef
  if (busy) {
ebedef
    
ebedef
    // begin painting //////////////////////////
ebedef
    
d04010
    m_styleId = 0;
d04010
    m_tracks.clear();
d04010
    
ebedef
    TTool::Application *app = TTool::getApplication();
ebedef
    if (!app)
ebedef
      return;
ebedef
ebedef
    m_isPath = app->getCurrentObject()->isSpline();
ebedef
    if (m_isPath) {
ebedef
      m_currentColor = TPixel32::Red;
ebedef
      m_active = true;
ebedef
      return;
ebedef
    }
ebedef
    
ebedef
    // todo: gestire autoenable
ebedef
    if ( app->getCurrentColumn()->getColumnIndex() < 0
ebedef
      && !app->getCurrentFrame()->isEditingLevel() )
ebedef
      return;
c9521d
    if (!getImage(true) || !touchImage())
c9521d
      return;
c9521d
    if (!app->getCurrentLevel()->getLevel())
c9521d
      return;
justburner e250b7
c9521d
    // nel caso che il colore corrente sia un cleanup/studiopalette color
c9521d
    // oppure il colore di un colorfield
c9521d
    if (TColorStyle *cs = app->getCurrentLevelStyle()) {
c9521d
      TRasterStyleFx *rfx = cs->getRasterStyleFx();
c9521d
      if (!cs->isStrokeStyle() && (!rfx || !rfx->isInkStyle()))
c9521d
        return;
c9521d
      m_styleId = app->getCurrentLevelStyleIndex();
c9521d
      m_currentColor = cs->getAverageColor();
c9521d
      m_currentColor.m = 255;
c9521d
    } else {
c9521d
      m_styleId = 1;
c9521d
      m_currentColor = TPixel32::Black;
c9521d
    }
ebedef
    
ebedef
    m_active = true;
ebedef
    
ebedef
    return; // painting has begun
justburner e250b7
  }
c9521d
  
c9521d
  
ebedef
  // end painting //////////////////////////
ebedef
  
c9521d
  m_active = false;
c9521d
  
ebedef
  // clear tracks automatically when return from this function
ebedef
  struct Cleanup {
ebedef
    ToonzVectorBrushTool &owner;
d04010
    inline ~Cleanup() { owner.m_tracks.clear(); owner.invalidate(); }
ebedef
  } cleanup = {*this};
d04010
d04010
  // remove empty tracks
d04010
  for(TrackList::iterator i = m_tracks.begin(); i != m_tracks.end(); )
d04010
    if (i->isEmpty()) i = m_tracks.erase(i); else ++i;
d04010
  
d04010
  if (m_tracks.empty())
d04010
    return;
ebedef
  
ebedef
  // make motion path (if need)
ebedef
    
shun-iwasawa 98926d
  if (m_isPath) {
c9521d
    double error = 20.0 * m_pixelSize;
shun-iwasawa 98926d
d04010
    m_tracks.resize(1);
d04010
    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())) {
d04010
      m_currentColor = TPixel32::Green;
695ef9
      invalidate();
c9521d
      int ret = DVGui::MsgBox(
c9521d
        QString("Are you sure you want to replace the motion path?"),
c9521d
        QObject::tr("Yes"), QObject::tr("No"), 0 );
c9521d
      if (ret != 1)
c9521d
        return; // 1 here means "Yes" button
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
shun-iwasawa 98926d
    QMutexLocker lock(vi->getMutex());
shun-iwasawa 98926d
ebedef
    TUndo *undo = new UndoPath(
ebedef
      getXsheet()->getStageObject(getObjectId())->getSpline() );
shun-iwasawa 98926d
c9521d
    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
ebedef
    return; // done with motion path
shun-iwasawa 98926d
  }
ebedef
  
ebedef
  // paint regular strokes
c9521d
  
ebedef
  TVectorImageP vi = getImage(true);
ebedef
  QMutexLocker lock(vi->getMutex());
c9521d
  TTool::Application *app = TTool::getApplication();
c9521d
  
d04010
  // prepare strokes
d04010
    
d04010
  StrokeList strokes;
d04010
  strokes.reserve(m_tracks.size());
d04010
  for(TrackList::iterator i = m_tracks.begin(); i != m_tracks.end(); ++i) {
d04010
    StrokeGenerator &track = *i;
d04010
    
d04010
    track.filterPoints();
d04010
    double error = 30.0/(1 + 0.5 * m_accuracy.getValue())*m_pixelSize;
d04010
    TStroke *stroke = track.makeStroke(error);
d04010
    
d04010
    stroke->setStyle(m_styleId);
d04010
    
d04010
    TStroke::OutlineOptions &options = stroke->outlineOptions();
d04010
    options.m_capStyle   = m_capStyle.getIndex();
d04010
    options.m_joinStyle  = m_joinStyle.getIndex();
d04010
    options.m_miterUpper = m_miterJoinLimit.getValue();
d04010
d04010
    if ( stroke->getControlPointCount() == 3
d04010
      && stroke->getControlPoint(0) != stroke->getControlPoint(2) )
d04010
                                        // gli stroke con solo 1 chunk vengono
d04010
                                        // fatti dal tape tool...e devono venir
d04010
                                        // riconosciuti come speciali di
d04010
                                        // autoclose proprio dal fatto che
d04010
                                        // hanno 1 solo chunk.
d04010
      stroke->insertControlPoints(0.5);
d04010
    
d04010
    if (!m_frameRange.getIndex() && track.getLoop())
d04010
      stroke->setSelfLoop(true);
d04010
    
d04010
    strokes.push_back(stroke);
d04010
  }
d04010
ebedef
  // add stroke to image
ebedef
  
shun-iwasawa 98926d
  if (m_frameRange.getIndex()) {
d04010
    // frame range stroke
shun-iwasawa 98926d
    if (m_firstFrameId == -1) {
d04010
      // remember strokes for first srame
d04010
      copyStrokes(m_firstStrokes, strokes);
c9521d
      m_firstFrameId = getFrameId();
d04010
      m_rangeTracks  = m_tracks;
c9521d
      
c9521d
      if (app) {
c9521d
        m_col        = app->getCurrentColumn()->getColumnIndex();
c9521d
        m_firstFrame = app->getCurrentFrame()->getFrame();
c9521d
      }
c9521d
      
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
      }
c9521d
    } else
c9521d
    if (m_firstFrameId == getFrameId()) {
d04010
      // painted of first frame agein, so
d04010
      // just replace the remembered strokes for first frame
d04010
      copyStrokes(m_firstStrokes, strokes);
d04010
      m_rangeTracks = m_tracks;
shun-iwasawa 98926d
    } else {
d04010
      // paint frame range strokes
shun-iwasawa 98926d
      TFrameId currentId = getFrameId();
c9521d
      int curCol   = app ? app->getCurrentColumn()->getColumnIndex() : 0;
c9521d
      int curFrame = app ? app->getCurrentFrame()->getFrame()        : 0;
c9521d
      
d04010
      if (size_t count = std::min(m_firstStrokes.size(), strokes.size())) {
d04010
        TUndoManager::manager()->beginBlock();
d04010
        for(size_t i = 0; i < count; ++i)
d04010
          doFrameRangeStrokes(
d04010
              m_firstFrameId, m_firstStrokes[i], getFrameId(), strokes[i],
d04010
              m_frameRange.getIndex(), m_breakAngles.getValue(), false, false,
d04010
              m_firstFrameRange );
d04010
        TUndoManager::manager()->endBlock();
d04010
      }
c9521d
      
ebedef
      if (m_inputmanager.state.isKeyPressed(TInputState::Key::control)) {
c9521d
        if (app && m_firstFrameId > currentId) {
c9521d
          if (app->getCurrentFrame()->isEditingScene()) {
c9521d
            app->getCurrentColumn()->setColumnIndex(curCol);
c9521d
            app->getCurrentFrame()->setFrame(curFrame);
c9521d
          } else {
c9521d
            app->getCurrentFrame()->setFid(currentId);
shun-iwasawa 98926d
          }
shun-iwasawa 98926d
        }
c9521d
        
shun-iwasawa 98926d
        resetFrameRange();
d04010
        copyStrokes(m_firstStrokes, strokes);
d04010
        m_rangeTracks     = m_tracks;
shun-iwasawa 98926d
        m_firstFrameId    = currentId;
shun-iwasawa 98926d
        m_firstFrameRange = false;
c9521d
      } else {
c9521d
        if (app) {
c9521d
          if (app->getCurrentFrame()->isEditingScene()) {
c9521d
            app->getCurrentColumn()->setColumnIndex(m_veryFirstCol);
c9521d
            app->getCurrentFrame()->setFrame(m_veryFirstFrame);
c9521d
          } else {
c9521d
            app->getCurrentFrame()->setFid(m_veryFirstFrameId);
c9521d
          }
c9521d
        }
shun-iwasawa 98926d
        resetFrameRange();
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
  } else {
d04010
    // regular paint strokes
d04010
    TUndoManager::manager()->beginBlock();
d04010
    for(StrokeList::iterator i = strokes.begin(); i != strokes.end(); ++i) {
d04010
      TStroke *stroke = *i;
d04010
      addStrokeToImage(app, vi, stroke, m_breakAngles.getValue(),
d04010
                      false, false, m_isFrameCreated, m_isLevelCreated);
d04010
d04010
      if ((Preferences::instance()->getGuidedDrawingType() == 1 ||
d04010
          Preferences::instance()->getGuidedDrawingType() == 2) &&
d04010
          Preferences::instance()->getGuidedAutoInbetween())
d04010
      {
d04010
        TFrameId fId = getFrameId();
d04010
        doGuidedAutoInbetween(fId, vi, stroke, m_breakAngles.getValue(), false,
d04010
                              false, false);
d04010
        if (app->getCurrentFrame()->isEditingScene())
d04010
          app->getCurrentFrame()->setFrame( app->getCurrentFrame()->getFrameIndex() );
d04010
        else
d04010
          app->getCurrentFrame()->setFid(fId);
d04010
      }
manongjohn 40a40e
    }
d04010
    TUndoManager::manager()->endBlock();
shun-iwasawa 98926d
  }
c9521d
  
d04010
  deleteStrokes(strokes);
ebedef
}
ebedef
ebedef
//--------------------------------------------------------------------------------------------------
ebedef
ebedef
void ToonzVectorBrushTool::inputPaintTracks(const TTrackList &tracks) {
bdb1e2
  if (tracks.empty()) return;
ebedef
ebedef
  TRectD invalidateRect;
ebedef
d04010
  size_t count = m_isPath ? 1 : tracks.size();
d04010
  m_tracks.resize(count);
d04010
  for(size_t i = 0; i < count; ++i) {
d04010
    const TTrack &track = *tracks[i];
d04010
    StrokeGenerator &gen = m_tracks[i];
d04010
    
d04010
    while(track.pointsRemoved) {
d04010
      gen.pop();
d04010
      --track.pointsRemoved;
d04010
    }
d04010
    
d04010
    while(track.pointsAdded) {
d04010
      const TTrackPoint &p = track.current();
d04010
      double t = computeThickness(p.pressure, m_thickness, m_pressure.getValue(), m_isPath);
d04010
      gen.add(TThickPoint(p.position, t), 0);
d04010
      --track.pointsAdded;
d04010
    }
d04010
    
d04010
    bool loop = m_snappedSelf
d04010
            && track.fixedFinished()
d04010
            && !track.empty()
d04010
            && areAlmostEqual(track.front().position, track.back().position);
d04010
    gen.setLoop(loop);
d04010
    
d04010
    invalidateRect += gen.getLastModifiedRegion();
d04010
    if (!i) {
d04010
      TPointD halfThick(m_maxThick * 0.5, m_maxThick * 0.5);
d04010
      invalidateRect += TRectD(m_brushPos - halfThick, m_brushPos + halfThick);
d04010
      m_brushPos = m_mousePos = track.current().position;
d04010
      invalidateRect += TRectD(m_brushPos - halfThick, m_brushPos + halfThick);
d04010
    }
d04010
  }
ebedef
  
695ef9
  if (!invalidateRect.isEmpty()) {
695ef9
    if (m_isPath) {
695ef9
      if (getViewer()) getViewer()->GLInvalidateRect(invalidateRect);
695ef9
    } else {
695ef9
      invalidate(invalidateRect.enlarge(2));
695ef9
    }
695ef9
  }
ebedef
}
ebedef
ebedef
//--------------------------------------------------------------------------------------------------
ebedef
ebedef
bool ToonzVectorBrushTool::preLeftButtonDown() {
ebedef
  if (getViewer() && getViewer()->getGuidedStrokePickerMode()) return false;
ebedef
bdb1e2
  m_pixelSize = getPixelSize();
ebedef
  int smoothRadius = (int)round(m_smooth.getValue());
bdb1e2
  m_modifierAssistants->magnetism = m_assistants.getValue() ? 1 : 0;
ebedef
  m_modifierSegmentation->setStep(TPointD(m_pixelSize, m_pixelSize));
ebedef
  m_modifierSmoothSegmentation->setStep(TPointD(2*m_pixelSize, 2*m_pixelSize));
bdb1e2
  m_modifierSimplify->step = 2*m_pixelSize;
ebedef
  m_inputmanager.drawPreview = false;
ebedef
ebedef
  m_inputmanager.clearModifiers();
ebedef
  m_inputmanager.addModifier(TInputModifierP(m_modifierTangents.getPointer()));
ebedef
  if (smoothRadius > 0) {
ebedef
    m_inputmanager.addModifier(TInputModifierP(m_modifierSmoothSegmentation.getPointer()));
ebedef
    for(int i = 0; i < 3; ++i) {
bdb1e2
      m_modifierSmooth[i]->radius = smoothRadius;
ebedef
      m_inputmanager.addModifier(TInputModifierP(m_modifierSmooth[i].getPointer()));
ebedef
    }
ebedef
  }
ebedef
  m_inputmanager.addModifier(TInputModifierP(m_modifierAssistants.getPointer()));
ebedef
#ifndef NDEBUG
ebedef
  m_inputmanager.addModifier(TInputModifierP(m_modifierTest.getPointer()));
ebedef
#endif
ebedef
  m_inputmanager.addModifier(TInputModifierP(m_modifierSegmentation.getPointer()));
ebedef
  m_inputmanager.addModifier(TInputModifierP(m_modifierSimplify.getPointer()));
ebedef
ebedef
  touchImage();
ebedef
  if (m_isFrameCreated) {
ebedef
    // When the xsheet frame is selected, whole viewer will be updated from
ebedef
    // SceneViewer::onXsheetChanged() on adding a new frame.
ebedef
    // We need to take care of a case when the level frame is selected.
ebedef
    if (m_application->getCurrentFrame()->isEditingLevel()) invalidate();
ebedef
  }
ebedef
  return true;
ebedef
}
ebedef
ebedef
//--------------------------------------------------------------------------------------------------
ebedef
ebedef
void ToonzVectorBrushTool::handleMouseEvent(MouseEventType type,
ebedef
                                          const TPointD &pos,
ebedef
                                          const TMouseEvent &e)
ebedef
{
ebedef
  TTimerTicks t = TToolTimer::ticks();
ebedef
  bool alt      = e.getModifiersMask() & TMouseEvent::ALT_KEY;
ebedef
  bool shift    = e.getModifiersMask() & TMouseEvent::SHIFT_KEY;
ebedef
  bool control  = e.getModifiersMask() & TMouseEvent::CTRL_KEY;
ebedef
ebedef
  if (shift && type == ME_DOWN && e.button() == Qt::LeftButton && !m_active) {
bdb1e2
    m_modifierAssistants->magnetism = 0;
ebedef
    m_inputmanager.clearModifiers();
ebedef
    m_inputmanager.addModifier(TInputModifierP(m_modifierLine.getPointer()));
ebedef
    m_inputmanager.addModifier(TInputModifierP(m_modifierAssistants.getPointer()));
ebedef
    m_inputmanager.addModifier(TInputModifierP(m_modifierSegmentation.getPointer()));
ebedef
    m_inputmanager.addModifier(TInputModifierP(m_modifierSimplify.getPointer()));
ebedef
    m_inputmanager.drawPreview = true;
ebedef
  }
ebedef
ebedef
  if (alt != m_inputmanager.state.isKeyPressed(TKey::alt))
ebedef
    m_inputmanager.keyEvent(alt, TKey::alt, t, nullptr);
ebedef
  if (shift != m_inputmanager.state.isKeyPressed(TKey::shift))
ebedef
    m_inputmanager.keyEvent(shift, TKey::shift, t, nullptr);
ebedef
  if (control != m_inputmanager.state.isKeyPressed(TKey::control))
ebedef
    m_inputmanager.keyEvent(control, TKey::control, t, nullptr);
ebedef
398cd8
  TPointD snappedPos = pos;
398cd8
  bool pickerMode = getViewer() && getViewer()->getGuidedStrokePickerMode();
5bd325
  bool snapInvert = alt && (!control || type == ME_MOVE || type == ME_DOWN);
5bd325
  bool snapEnabled = !pickerMode && (snapInvert != m_snap.getValue());
398cd8
  snap(pos, snapEnabled, m_active);
398cd8
  if (m_snapped)
398cd8
    snappedPos = m_snapPoint;
398cd8
  if (m_snappedSelf && type == ME_UP)
398cd8
    snappedPos = m_snapPointSelf;
398cd8
  
ebedef
  if (type == ME_MOVE) {
ebedef
    qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
398cd8
    THoverList hovers(1, snappedPos);
ebedef
    m_inputmanager.hoverEvent(hovers);
ebedef
  } else
398cd8
  if (pickerMode) {
d04010
    if (type == ME_DOWN) getViewer()->doPickGuideStroke(pos);
ebedef
  } else {
a203c4
    int    deviceId    = e.isTablet() ? 1 : 0;
a203c4
    bool   hasPressure = e.isTablet();
a203c4
    double pressure    = hasPressure ? e.m_pressure : 1.0;
a203c4
    bool   final       = type == ME_UP;
a203c4
    m_inputmanager.trackEvent(
a203c4
      deviceId, 0, snappedPos, pressure, TPointD(), hasPressure, false, final, t);
ebedef
    m_inputmanager.processTracks();
ebedef
  }
ebedef
}
ebedef
ebedef
//--------------------------------------------------------------------------------------------------
ebedef
ebedef
void ToonzVectorBrushTool::leftButtonDown(const TPointD &pos,
ebedef
                                        const TMouseEvent &e) {
ebedef
  handleMouseEvent(ME_DOWN, pos, e);
ebedef
}
ebedef
void ToonzVectorBrushTool::leftButtonDrag(const TPointD &pos,
ebedef
                                        const TMouseEvent &e) {
ebedef
  handleMouseEvent(ME_DRAG, pos, e);
ebedef
}
ebedef
void ToonzVectorBrushTool::leftButtonUp(const TPointD &pos,
ebedef
                                      const TMouseEvent &e) {
ebedef
  handleMouseEvent(ME_UP, pos, e);
ebedef
}
ebedef
void ToonzVectorBrushTool::mouseMove(const TPointD &pos, const TMouseEvent &e) {
ebedef
  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) {
ebedef
  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
ebedef
  bool swapped = firstFrameId > lastFrameId;
ebedef
  if (swapped) {
shun-iwasawa 98926d
    std::swap(firstFrameId, lastFrameId);
ebedef
    *first = *lastStroke;
ebedef
    *last  = *firstStroke;
ebedef
  } else {
ebedef
    *first = *firstStroke;
ebedef
    *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
398cd8
void ToonzVectorBrushTool::snap(const TPointD &pos, bool snapEnabled, bool withSelfSnap) {
398cd8
  bool oldSnapped      = m_snapped;
398cd8
  bool oldSnappedSelf  = m_snappedSelf;
398cd8
  TPointD oldPoint     = m_snapPoint;
398cd8
  TPointD oldPointSelf = m_snapPointSelf;
f8139f
  
398cd8
  m_snapped = m_snappedSelf = false;
f8139f
  
398cd8
  if (snapEnabled) { // snapping is active
398cd8
    double minDistance2 = m_minDistance2;
f8139f
    
398cd8
    // 0 - strokes, 1 - guides, 2 - all
398cd8
    int target = Preferences::instance()->getVectorSnappingTarget();
398cd8
398cd8
    // snap to guides
398cd8
    if (target != 0) {
398cd8
      if (TToolViewer *viewer = getViewer()) {
398cd8
        // find nearest vertical guide
398cd8
        int cnt = viewer->getVGuideCount();
398cd8
        for(int i = 0; i < cnt; ++i) {
398cd8
          double guide = viewer->getVGuide(i);
398cd8
          double d2 = guide - pos.y;
398cd8
          d2 *= d2; // we work with square of the distance
398cd8
          if (d2 < minDistance2) {
398cd8
            m_snapped = true;
398cd8
            m_snapPoint.x = pos.x;
398cd8
            m_snapPoint.y = guide;
398cd8
            minDistance2 = d2;
398cd8
          }
398cd8
        }
shun-iwasawa 98926d
398cd8
        // find nearest horizontal guide
398cd8
        cnt = viewer->getHGuideCount();
398cd8
        for(int i = 0; i < cnt; ++i) {
398cd8
          double guide = viewer->getHGuide(i);
398cd8
          double d2 = guide - pos.x;
398cd8
          d2 *= d2; // we work with square of the distance
398cd8
          if (d2 < minDistance2) {
398cd8
            m_snapped = true;
398cd8
            m_snapPoint.x = guide;
398cd8
            m_snapPoint.y = pos.y;
398cd8
            minDistance2 = d2;
398cd8
          }
398cd8
        }
398cd8
      }
398cd8
    }
f8139f
398cd8
    // snap to strokes
398cd8
    if (target != 1) {
398cd8
      if (TVectorImageP vi = getImage(false)) {
398cd8
        int count = vi->getStrokeCount();
398cd8
        for(int i = 0; i < count; ++i) {
398cd8
          double w, d2;
398cd8
          TStroke *stroke = vi->getStroke(i);
398cd8
          if (!stroke->getNearestW(pos, w, d2) || d2 >= minDistance2)
398cd8
            continue;
398cd8
          minDistance2 = d2;
398cd8
          w = w > 0.001 ? (w < 0.999 ? w : 1.0) : 0.0;
398cd8
          m_snapped = true;
398cd8
          m_snapPoint = stroke->getPoint(w);
398cd8
        }
398cd8
      }
398cd8
    
398cd8
      // finally snap to first point of track (self snap)
d04010
      if (withSelfSnap && !m_tracks.empty() && !m_tracks.front().isEmpty()) {
d04010
        TPointD p = m_tracks.front().getFirstPoint();
398cd8
        double d2 = tdistance2(pos, p);
b8dbde
        if (d2 < minDistance2) {
398cd8
          m_snappedSelf = true;
398cd8
          m_snapPointSelf = p;
398cd8
        }
398cd8
      }
398cd8
    }
398cd8
  } // snapping is active
f8139f
  
398cd8
  // invalidate rect
398cd8
  TRectD invalidateRect;
398cd8
  double radius = 8.0*m_pixelSize;
398cd8
  TPointD halfSize(radius, radius);
398cd8
398cd8
  if ( oldSnapped != m_snapped
398cd8
    || !areAlmostEqual(oldPoint, m_snapPoint) )
398cd8
  {
398cd8
    if (oldSnapped) invalidateRect += TRectD(oldPoint - halfSize, oldPoint + halfSize);
398cd8
    if (m_snapped)  invalidateRect += TRectD(m_snapPoint - halfSize, m_snapPoint + halfSize);
f8139f
  }
f8139f
  
398cd8
  if ( oldSnappedSelf != m_snappedSelf
398cd8
    || !areAlmostEqual(oldPointSelf, m_snapPointSelf) )
398cd8
  {
398cd8
    if (oldSnappedSelf) invalidateRect += TRectD(oldPointSelf - halfSize, oldPointSelf + halfSize);
398cd8
    if (m_snappedSelf)  invalidateRect += TRectD(m_snapPointSelf - halfSize, m_snapPointSelf + halfSize);
shun-iwasawa 98926d
  }
398cd8
  
398cd8
  if (!invalidateRect.isEmpty())
398cd8
    invalidate(invalidateRect);
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//-------------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::draw() {
398cd8
  m_pixelSize = getPixelSize();
ebedef
  m_inputmanager.draw();
ebedef
  
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
398cd8
  // draw track
d04010
  tglColor(m_currentColor);
d04010
  for(TrackList::iterator i = m_tracks.begin(); i != m_tracks.end(); ++i)
d04010
    i->drawAllFragments();
c9521d
  
398cd8
  // draw snapping
398cd8
  double snapMarkRadius = 6.0 * m_pixelSize;
398cd8
  if (m_snapped) {
398cd8
    tglColor(TPixelD(0.1, 0.9, 0.1));
398cd8
    tglDrawCircle(m_snapPoint, snapMarkRadius);
398cd8
  }
398cd8
  if (m_snappedSelf) {
398cd8
    tglColor(TPixelD(0.9, 0.9, 0.1));
398cd8
    tglDrawCircle(m_snapPointSelf, snapMarkRadius);
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  // frame range
d04010
  for(TrackList::iterator i = m_rangeTracks.begin(); i != m_rangeTracks.end(); ++i) {
d04010
    if (i->isEmpty()) continue;
b8dbde
    TPointD offset1 = TPointD(5, 5);
b8dbde
    TPointD offset2 = TPointD(-offset1.x, offset1.y);
d04010
    TPointD point = i->getFirstPoint();
d04010
    glColor3d(1.0, 0.0, 0.0);
d04010
    i->drawAllFragments();
d04010
    glColor3d(0.0, 0.6, 0.0);
b8dbde
    tglDrawSegment(point - offset1, point + offset1);
b8dbde
    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() {
d04010
  m_rangeTracks.clear();
shun-iwasawa 98926d
  m_firstFrameId = -1;
d04010
  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;
0d5555
  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);
0d5555
  m_snap.setValue(V_VectorBrushSnap ? 1 : 0);
manongjohn df5842
  m_snapSensitivity.setIndex(V_VectorBrushSnapSensitivity);
0d5555
  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
}