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);
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,
shun-iwasawa 98926d
                        bool isPath) {
shun-iwasawa 98926d
  if (isPath) return 0.0;
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)
shun-iwasawa 98926d
    , m_capStyle("Cap")
shun-iwasawa 98926d
    , m_joinStyle("Join")
shun-iwasawa 98926d
    , m_miterJoinLimit("Miter:", 0, 100, 4)
shun-iwasawa 98926d
    , m_rasterTrack(0)
shun-iwasawa 98926d
    , m_styleId(0)
shun-iwasawa 98926d
    , m_modifiedRegion()
shun-iwasawa 98926d
    , m_bluredBrush(0)
shun-iwasawa 98926d
    , m_active(false)
shun-iwasawa 98926d
    , m_enabled(false)
shun-iwasawa 98926d
    , m_isPrompting(false)
shun-iwasawa 98926d
    , m_firstTime(true)
shun-iwasawa 98926d
    , m_firstFrameRange(true)
shun-iwasawa 98926d
    , m_presetsLoaded(false)
shun-iwasawa 98926d
    , m_frameRange("Range:")
shun-iwasawa 98926d
    , m_snap("Snap", false)
shun-iwasawa 98926d
    , m_snapSensitivity("Sensitivity:")
shun-iwasawa 98926d
    , m_targetType(targetType)
shun-iwasawa 98926d
    , m_workingFrameId(TFrameId()) {
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
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");
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("");
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.
Martin van Zijl 2420a8
  if (m_active && m_enabled) {
manongjohn df5842
    leftButtonUp(m_lastDragPos, m_lastDragEvent);
Martin van Zijl 2420a8
  }
Martin van Zijl 2420a8
shun-iwasawa 98926d
  if (m_tileSaver && !m_isPath) {
shun-iwasawa 98926d
    m_enabled = false;
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
  m_workRas   = TRaster32P();
shun-iwasawa 98926d
  m_backupRas = TRasterCM32P();
shun-iwasawa 98926d
  resetFrameRange();
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//--------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
bool ToonzVectorBrushTool::preLeftButtonDown() {
manongjohn 40a40e
  if (getViewer() && getViewer()->getGuidedStrokePickerMode()) return false;
manongjohn 40a40e
shun-iwasawa 98926d
  touchImage();
shun-iwasawa 98926d
  if (m_isFrameCreated) {
shun-iwasawa 98926d
    // When the xsheet frame is selected, whole viewer will be updated from
shun-iwasawa 98926d
    // SceneViewer::onXsheetChanged() on adding a new frame.
shun-iwasawa 98926d
    // We need to take care of a case when the level frame is selected.
shun-iwasawa 98926d
    if (m_application->getCurrentFrame()->isEditingLevel()) invalidate();
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::leftButtonDown(const TPointD &pos,
shun-iwasawa 98926d
                                          const TMouseEvent &e) {
shun-iwasawa 98926d
  TTool::Application *app = TTool::getApplication();
shun-iwasawa 98926d
  if (!app) return;
shun-iwasawa 98926d
manongjohn 40a40e
  if (getViewer() && getViewer()->getGuidedStrokePickerMode()) {
manongjohn 40a40e
    getViewer()->doPickGuideStroke(pos);
manongjohn 40a40e
    return;
manongjohn 40a40e
  }
manongjohn 40a40e
shun-iwasawa 98926d
  int col   = app->getCurrentColumn()->getColumnIndex();
shun-iwasawa 98926d
  m_isPath  = app->getCurrentObject()->isSpline();
manongjohn 9bce81
  m_enabled = col >= 0 || m_isPath || app->getCurrentFrame()->isEditingLevel();
shun-iwasawa 98926d
  // todo: gestire autoenable
shun-iwasawa 98926d
  if (!m_enabled) return;
shun-iwasawa 98926d
  if (!m_isPath) {
shun-iwasawa 98926d
    m_currentColor = TPixel32::Black;
shun-iwasawa 98926d
    m_active       = !!getImage(true);
shun-iwasawa 98926d
    if (!m_active) {
shun-iwasawa 98926d
      m_active = !!touchImage();
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
    if (!m_active) return;
shun-iwasawa 98926d
shun-iwasawa 98926d
    if (m_active) {
shun-iwasawa 98926d
      // nel caso che il colore corrente sia un cleanup/studiopalette color
shun-iwasawa 98926d
      // oppure il colore di un colorfield
shun-iwasawa 98926d
      m_styleId       = app->getCurrentLevelStyleIndex();
shun-iwasawa 98926d
      TColorStyle *cs = app->getCurrentLevelStyle();
shun-iwasawa 98926d
      if (cs) {
shun-iwasawa 98926d
        TRasterStyleFx *rfx = cs ? cs->getRasterStyleFx() : 0;
shun-iwasawa 98926d
        m_active =
shun-iwasawa 98926d
            cs != 0 && (cs->isStrokeStyle() || (rfx && rfx->isInkStyle()));
shun-iwasawa 98926d
        m_currentColor   = cs->getAverageColor();
shun-iwasawa 98926d
        m_currentColor.m = 255;
shun-iwasawa 98926d
      } else {
shun-iwasawa 98926d
        m_styleId      = 1;
shun-iwasawa 98926d
        m_currentColor = TPixel32::Black;
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
  } else {
shun-iwasawa 98926d
    m_currentColor = TPixel32::Red;
shun-iwasawa 98926d
    m_active       = true;
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
  // assert(0<=m_styleId && m_styleId<2);
shun-iwasawa 98926d
  m_track.clear();
shun-iwasawa 98926d
  double thickness = (m_pressure.getValue() || m_isPath)
shun-iwasawa 98926d
                         ? computeThickness(e.m_pressure, m_thickness, m_isPath)
shun-iwasawa 98926d
                         : m_thickness.getValue().second * 0.5;
shun-iwasawa 98926d
Rozhuk Ivan 823a31
  /*--- ストロークの最初にMaxサイズの円が描かれてしまう不具合を防止する ---*/
shun-iwasawa 98926d
  if (m_pressure.getValue() && e.m_pressure == 1.0)
shun-iwasawa 76d093
    thickness = m_thickness.getValue().first * 0.5;
shun-iwasawa 98926d
  m_currThickness = thickness;
shun-iwasawa 98926d
  m_smoothStroke.beginStroke(m_smooth.getValue());
shun-iwasawa 98926d
shun-iwasawa 98926d
  if (m_foundFirstSnap) {
shun-iwasawa 98926d
    addTrackPoint(TThickPoint(m_firstSnapPoint, thickness),
shun-iwasawa 98926d
                  getPixelSize() * getPixelSize());
shun-iwasawa 98926d
  } else
shun-iwasawa 98926d
    addTrackPoint(TThickPoint(pos, thickness), getPixelSize() * getPixelSize());
shun-iwasawa 98926d
  TRectD invalidateRect = m_track.getLastModifiedRegion();
shun-iwasawa 98926d
  invalidate(invalidateRect.enlarge(2));
shun-iwasawa 98926d
shun-iwasawa 98926d
  // updating m_brushPos is needed to refresh viewer properly
shun-iwasawa 98926d
  m_brushPos = m_mousePos = pos;
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//-------------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::leftButtonDrag(const TPointD &pos,
shun-iwasawa 98926d
                                          const TMouseEvent &e) {
shun-iwasawa 98926d
  if (!m_enabled || !m_active) {
shun-iwasawa 98926d
    m_brushPos = m_mousePos = pos;
shun-iwasawa 98926d
    return;
shun-iwasawa 98926d
  }
Martin van Zijl 2420a8
manongjohn df5842
  m_lastDragPos   = pos;
Martin van Zijl 2420a8
  m_lastDragEvent = e;
Martin van Zijl 2420a8
shun-iwasawa 98926d
  double thickness = (m_pressure.getValue() || m_isPath)
shun-iwasawa 98926d
                         ? computeThickness(e.m_pressure, m_thickness, m_isPath)
shun-iwasawa 98926d
                         : m_thickness.getValue().second * 0.5;
shun-iwasawa 98926d
shun-iwasawa 98926d
  TRectD invalidateRect;
shun-iwasawa 98926d
  TPointD halfThick(m_maxThick * 0.5, m_maxThick * 0.5);
shun-iwasawa 98926d
  TPointD snapThick(6.0 * m_pixelSize, 6.0 * m_pixelSize);
shun-iwasawa 98926d
shun-iwasawa 98926d
  // In order to clear the previous brush tip
shun-iwasawa 98926d
  invalidateRect += TRectD(m_brushPos - halfThick, m_brushPos + halfThick);
shun-iwasawa 98926d
shun-iwasawa 98926d
  // In order to clear the previous snap indicator
shun-iwasawa 98926d
  if (m_foundLastSnap)
shun-iwasawa 98926d
    invalidateRect +=
shun-iwasawa 98926d
        TRectD(m_lastSnapPoint - snapThick, m_lastSnapPoint + snapThick);
shun-iwasawa 98926d
shun-iwasawa 98926d
  m_currThickness = thickness;
shun-iwasawa 98926d
shun-iwasawa 98926d
  m_mousePos       = pos;
shun-iwasawa 98926d
  m_lastSnapPoint  = pos;
shun-iwasawa 98926d
  m_foundLastSnap  = false;
shun-iwasawa 98926d
  m_foundFirstSnap = false;
shun-iwasawa 98926d
  m_snapSelf       = false;
shun-iwasawa 98926d
  m_altPressed     = e.isAltPressed() && !e.isCtrlPressed();
shun-iwasawa 98926d
shun-iwasawa 98926d
  checkStrokeSnapping(false, m_altPressed);
shun-iwasawa 98926d
  checkGuideSnapping(false, m_altPressed);
shun-iwasawa 98926d
  m_brushPos = m_lastSnapPoint;
shun-iwasawa 98926d
shun-iwasawa 98926d
  if (m_foundLastSnap)
shun-iwasawa 98926d
    invalidateRect +=
shun-iwasawa 98926d
        TRectD(m_lastSnapPoint - snapThick, m_lastSnapPoint + snapThick);
shun-iwasawa 98926d
shun-iwasawa 98926d
  if (e.isShiftPressed()) {
shun-iwasawa 98926d
    m_smoothStroke.clearPoints();
shun-iwasawa 98926d
    m_track.add(TThickPoint(m_brushPos, thickness),
shun-iwasawa 98926d
                getPixelSize() * getPixelSize());
shun-iwasawa 98926d
    m_track.removeMiddlePoints();
shun-iwasawa 98926d
    invalidateRect += m_track.getModifiedRegion();
shun-iwasawa 98926d
  } else if (m_dragDraw) {
shun-iwasawa 98926d
    addTrackPoint(TThickPoint(pos, thickness), getPixelSize() * getPixelSize());
shun-iwasawa 98926d
    invalidateRect += m_track.getLastModifiedRegion();
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  // In order to draw the current brush tip
shun-iwasawa 98926d
  invalidateRect += TRectD(m_brushPos - halfThick, m_brushPos + halfThick);
shun-iwasawa 98926d
shun-iwasawa 98926d
  if (!invalidateRect.isEmpty()) {
luz paz 6454c4
    // for motion path, call the invalidate function directly to ignore dpi of
shun-iwasawa 98926d
    // the current level
shun-iwasawa 98926d
    if (m_isPath)
shun-iwasawa 98926d
      m_viewer->GLInvalidateRect(invalidateRect.enlarge(2));
shun-iwasawa 98926d
    else
shun-iwasawa 98926d
      invalidate(invalidateRect.enlarge(2));
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//---------------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::leftButtonUp(const TPointD &pos,
shun-iwasawa 98926d
                                        const TMouseEvent &e) {
shun-iwasawa 98926d
  bool isValid = m_enabled && m_active;
shun-iwasawa 98926d
  m_enabled    = false;
shun-iwasawa 98926d
shun-iwasawa 98926d
  if (!isValid) {
shun-iwasawa 98926d
    // in case the current frame is moved to empty cell while dragging
shun-iwasawa 98926d
    if (!m_track.isEmpty()) {
shun-iwasawa 98926d
      m_track.clear();
shun-iwasawa 98926d
      invalidate();
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
    return;
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  if (m_isPath) {
shun-iwasawa 98926d
    double error = 20.0 * getPixelSize();
shun-iwasawa 98926d
shun-iwasawa 98926d
    TStroke *stroke;
shun-iwasawa 98926d
    if (e.isShiftPressed()) {
shun-iwasawa 98926d
      m_track.removeMiddlePoints();
shun-iwasawa 98926d
      stroke = m_track.makeStroke(0);
shun-iwasawa 98926d
    } else {
shun-iwasawa 98926d
      flushTrackPoint();
shun-iwasawa 98926d
      stroke = m_track.makeStroke(error);
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
    int points = stroke->getControlPointCount();
shun-iwasawa 98926d
shun-iwasawa 98926d
    TVectorImageP vi = getImage(true);
shun-iwasawa 98926d
    struct Cleanup {
shun-iwasawa 98926d
      ToonzVectorBrushTool *m_this;
shun-iwasawa 98926d
      ~Cleanup() { m_this->m_track.clear(), m_this->invalidate(); }
shun-iwasawa 98926d
    } cleanup = {this};
shun-iwasawa 98926d
shun-iwasawa 98926d
    if (!isJustCreatedSpline(vi.getPointer())) {
shun-iwasawa 98926d
      m_isPrompting = true;
shun-iwasawa 98926d
shun-iwasawa 98926d
      QString question("Are you sure you want to replace the motion path?");
shun-iwasawa 98926d
      int ret =
shun-iwasawa 98926d
          DVGui::MsgBox(question, QObject::tr("Yes"), QObject::tr("No"), 0);
shun-iwasawa 98926d
shun-iwasawa 98926d
      m_isPrompting = false;
shun-iwasawa 98926d
shun-iwasawa 98926d
      if (ret == 2 || ret == 0) return;
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
shun-iwasawa 98926d
    QMutexLocker lock(vi->getMutex());
shun-iwasawa 98926d
shun-iwasawa 98926d
    TUndo *undo =
shun-iwasawa 98926d
        new UndoPath(getXsheet()->getStageObject(getObjectId())->getSpline());
shun-iwasawa 98926d
shun-iwasawa 98926d
    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
shun-iwasawa 98926d
    return;
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  TVectorImageP vi = getImage(true);
shun-iwasawa 98926d
  if (m_track.isEmpty()) {
shun-iwasawa 98926d
    m_styleId = 0;
shun-iwasawa 98926d
    m_track.clear();
shun-iwasawa 98926d
    return;
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  if (vi && (m_snap.getValue() != m_altPressed) && m_foundLastSnap) {
shun-iwasawa 98926d
    addTrackPoint(TThickPoint(m_lastSnapPoint, m_currThickness),
shun-iwasawa 98926d
                  getPixelSize() * getPixelSize());
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
  m_strokeIndex1   = -1;
shun-iwasawa 98926d
  m_strokeIndex2   = -1;
shun-iwasawa 98926d
  m_w1             = -1;
shun-iwasawa 98926d
  m_w2             = -2;
shun-iwasawa 98926d
  m_foundFirstSnap = false;
shun-iwasawa 98926d
  m_foundLastSnap  = false;
shun-iwasawa 98926d
shun-iwasawa 98926d
  m_track.filterPoints();
shun-iwasawa 98926d
  double error = 30.0 / (1 + 0.5 * m_accuracy.getValue());
shun-iwasawa 98926d
  error *= getPixelSize();
shun-iwasawa 98926d
shun-iwasawa 98926d
  TStroke *stroke;
shun-iwasawa 98926d
  if (e.isShiftPressed()) {
shun-iwasawa 98926d
    m_track.removeMiddlePoints();
shun-iwasawa 98926d
    stroke = m_track.makeStroke(0);
shun-iwasawa 98926d
  } else {
shun-iwasawa 98926d
    flushTrackPoint();
shun-iwasawa 98926d
    stroke = m_track.makeStroke(error);
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
  stroke->setStyle(m_styleId);
shun-iwasawa 98926d
  {
shun-iwasawa 98926d
    TStroke::OutlineOptions &options = stroke->outlineOptions();
shun-iwasawa 98926d
    options.m_capStyle               = m_capStyle.getIndex();
shun-iwasawa 98926d
    options.m_joinStyle              = m_joinStyle.getIndex();
shun-iwasawa 98926d
    options.m_miterUpper             = m_miterJoinLimit.getValue();
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
  m_styleId = 0;
shun-iwasawa 98926d
shun-iwasawa 98926d
  QMutexLocker lock(vi->getMutex());
shun-iwasawa 98926d
  if (stroke->getControlPointCount() == 3 &&
shun-iwasawa 98926d
      stroke->getControlPoint(0) !=
shun-iwasawa 98926d
          stroke->getControlPoint(2))  // gli stroke con solo 1 chunk vengono
shun-iwasawa 98926d
                                       // fatti dal tape tool...e devono venir
shun-iwasawa 98926d
                                       // riconosciuti come speciali di
shun-iwasawa 98926d
                                       // autoclose proprio dal fatto che
shun-iwasawa 98926d
                                       // hanno 1 solo chunk.
shun-iwasawa 98926d
    stroke->insertControlPoints(0.5);
shun-iwasawa 98926d
  if (m_frameRange.getIndex()) {
shun-iwasawa 98926d
    if (m_firstFrameId == -1) {
shun-iwasawa 98926d
      m_firstStroke                   = new TStroke(*stroke);
shun-iwasawa 98926d
      m_firstFrameId                  = getFrameId();
shun-iwasawa 98926d
      TTool::Application *application = TTool::getApplication();
shun-iwasawa 98926d
      if (application) {
shun-iwasawa 98926d
        m_col        = application->getCurrentColumn()->getColumnIndex();
shun-iwasawa 98926d
        m_firstFrame = application->getCurrentFrame()->getFrame();
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
      m_rangeTrack = m_track;
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
      }
shun-iwasawa 98926d
    } else if (m_firstFrameId == getFrameId()) {
shun-iwasawa 98926d
      if (m_firstStroke) {
shun-iwasawa 98926d
        delete m_firstStroke;
shun-iwasawa 98926d
        m_firstStroke = 0;
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
      m_firstStroke = new TStroke(*stroke);
shun-iwasawa 98926d
      m_rangeTrack  = m_track;
shun-iwasawa 98926d
    } else {
shun-iwasawa 98926d
      TFrameId currentId = getFrameId();
shun-iwasawa 98926d
      int curCol = 0, curFrame = 0;
shun-iwasawa 98926d
      TTool::Application *application = TTool::getApplication();
shun-iwasawa 98926d
      if (application) {
shun-iwasawa 98926d
        curCol   = application->getCurrentColumn()->getColumnIndex();
shun-iwasawa 98926d
        curFrame = application->getCurrentFrame()->getFrame();
shun-iwasawa 98926d
      }
manongjohn 40a40e
      bool success = doFrameRangeStrokes(
manongjohn 40a40e
          m_firstFrameId, m_firstStroke, getFrameId(), stroke,
manongjohn 40a40e
          m_frameRange.getIndex(), m_breakAngles.getValue(), false, false,
manongjohn 40a40e
          m_firstFrameRange);
shun-iwasawa 98926d
      if (e.isCtrlPressed()) {
shun-iwasawa 98926d
        if (application) {
shun-iwasawa 98926d
          if (m_firstFrameId > currentId) {
shun-iwasawa 98926d
            if (application->getCurrentFrame()->isEditingScene()) {
shun-iwasawa 98926d
              application->getCurrentColumn()->setColumnIndex(curCol);
shun-iwasawa 98926d
              application->getCurrentFrame()->setFrame(curFrame);
shun-iwasawa 98926d
            } else
shun-iwasawa 98926d
              application->getCurrentFrame()->setFid(currentId);
shun-iwasawa 98926d
          }
shun-iwasawa 98926d
        }
shun-iwasawa 98926d
        resetFrameRange();
shun-iwasawa 98926d
        m_firstStroke     = new TStroke(*stroke);
shun-iwasawa 98926d
        m_rangeTrack      = m_track;
shun-iwasawa 98926d
        m_firstFrameId    = currentId;
shun-iwasawa 98926d
        m_firstFrameRange = false;
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
shun-iwasawa 98926d
      if (application && !e.isCtrlPressed()) {
shun-iwasawa 98926d
        if (application->getCurrentFrame()->isEditingScene()) {
shun-iwasawa 98926d
          application->getCurrentColumn()->setColumnIndex(m_veryFirstCol);
shun-iwasawa 98926d
          application->getCurrentFrame()->setFrame(m_veryFirstFrame);
shun-iwasawa 98926d
        } else
shun-iwasawa 98926d
          application->getCurrentFrame()->setFid(m_veryFirstFrameId);
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
shun-iwasawa 98926d
      if (!e.isCtrlPressed()) {
shun-iwasawa 98926d
        resetFrameRange();
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
    invalidate();
shun-iwasawa 98926d
  } else {
shun-iwasawa 98926d
    if (m_snapSelf) {
shun-iwasawa 98926d
      stroke->setSelfLoop(true);
shun-iwasawa 98926d
      m_snapSelf = false;
shun-iwasawa 98926d
    }
manongjohn 40a40e
manongjohn 4fae98
    addStrokeToImage(getApplication(), vi, stroke, m_breakAngles.getValue(),
manongjohn 4fae98
                     false, false, m_isFrameCreated, m_isLevelCreated);
manongjohn 4fae98
    TRectD bbox = stroke->getBBox().enlarge(2) + m_track.getModifiedRegion();
manongjohn 4fae98
manongjohn 4fae98
    invalidate();  // should use bbox?
manongjohn 40a40e
manongjohn 233fcf
    if ((Preferences::instance()->getGuidedDrawingType() == 1 ||
manongjohn 233fcf
         Preferences::instance()->getGuidedDrawingType() == 2) &&
manongjohn 40a40e
        Preferences::instance()->getGuidedAutoInbetween()) {
manongjohn 40a40e
      int fidx     = getApplication()->getCurrentFrame()->getFrameIndex();
manongjohn 40a40e
      TFrameId fId = getFrameId();
manongjohn 40a40e
manongjohn 4fae98
      doGuidedAutoInbetween(fId, vi, stroke, m_breakAngles.getValue(), false,
manongjohn 4fae98
                            false, false);
manongjohn 40a40e
manongjohn 40a40e
      if (getApplication()->getCurrentFrame()->isEditingScene())
manongjohn 40a40e
        getApplication()->getCurrentFrame()->setFrame(fidx);
manongjohn 40a40e
      else
manongjohn 40a40e
        getApplication()->getCurrentFrame()->setFid(fId);
manongjohn 40a40e
    }
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
  assert(stroke);
shun-iwasawa 98926d
  m_track.clear();
shun-iwasawa 98926d
  m_altPressed = false;
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//--------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
bool ToonzVectorBrushTool::keyDown(QKeyEvent *event) {
shun-iwasawa 98926d
  if (event->key() == Qt::Key_Escape) {
shun-iwasawa 98926d
    resetFrameRange();
shun-iwasawa 98926d
  }
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
shun-iwasawa 98926d
  *first       = *firstStroke;
shun-iwasawa 98926d
  *last        = *lastStroke;
shun-iwasawa 98926d
  bool swapped = false;
shun-iwasawa 98926d
  if (firstFrameId > lastFrameId) {
shun-iwasawa 98926d
    std::swap(firstFrameId, lastFrameId);
shun-iwasawa 98926d
    *first  = *lastStroke;
shun-iwasawa 98926d
    *last   = *firstStroke;
shun-iwasawa 98926d
    swapped = true;
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();
manongjohn 40a40e
  int row = getApplication()->getCurrentFrame()->isEditingScene()
manongjohn 40a40e
                ? getApplication()->getCurrentFrame()->getFrameIndex()
manongjohn 40a40e
                : -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();
manongjohn 4fae98
      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();
manongjohn 4fae98
      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
shun-iwasawa 98926d
void ToonzVectorBrushTool::addTrackPoint(const TThickPoint &point,
shun-iwasawa 98926d
                                         double pixelSize2) {
shun-iwasawa 98926d
  m_smoothStroke.addPoint(point);
shun-iwasawa 98926d
  std::vector<tthickpoint> pts;</tthickpoint>
shun-iwasawa 98926d
  m_smoothStroke.getSmoothPoints(pts);
shun-iwasawa 98926d
  for (size_t i = 0; i < pts.size(); ++i) {
shun-iwasawa 98926d
    m_track.add(pts[i], pixelSize2);
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//--------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::flushTrackPoint() {
shun-iwasawa 98926d
  m_smoothStroke.endStroke();
shun-iwasawa 98926d
  std::vector<tthickpoint> pts;</tthickpoint>
shun-iwasawa 98926d
  m_smoothStroke.getSmoothPoints(pts);
shun-iwasawa 98926d
  double pixelSize2 = getPixelSize() * getPixelSize();
shun-iwasawa 98926d
  for (size_t i = 0; i < pts.size(); ++i) {
shun-iwasawa 98926d
    m_track.add(pts[i], pixelSize2);
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//---------------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::mouseMove(const TPointD &pos, const TMouseEvent &e) {
shun-iwasawa 98926d
  qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
shun-iwasawa 98926d
shun-iwasawa 98926d
  struct Locals {
shun-iwasawa 98926d
    ToonzVectorBrushTool *m_this;
shun-iwasawa 98926d
shun-iwasawa 98926d
    void setValue(TDoublePairProperty &prop,
shun-iwasawa 98926d
                  const TDoublePairProperty::Value &value) {
shun-iwasawa 98926d
      prop.setValue(value);
shun-iwasawa 98926d
shun-iwasawa 98926d
      m_this->onPropertyChanged(prop.getName());
shun-iwasawa 98926d
      TTool::getApplication()->getCurrentTool()->notifyToolChanged();
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
shun-iwasawa 98926d
    void addMinMax(TDoublePairProperty &prop, double add) {
shun-iwasawa 98926d
      if (add == 0.0) return;
shun-iwasawa 98926d
      const TDoublePairProperty::Range &range = prop.getRange();
shun-iwasawa 98926d
shun-iwasawa 98926d
      TDoublePairProperty::Value value = prop.getValue();
shun-iwasawa 98926d
      value.first  = tcrop(value.first + add, range.first, range.second);
shun-iwasawa 98926d
      value.second = tcrop(value.second + add, range.first, range.second);
shun-iwasawa 98926d
shun-iwasawa 98926d
      setValue(prop, value);
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
shun-iwasawa 98926d
    void addMinMaxSeparate(TDoublePairProperty &prop, double min, double max) {
shun-iwasawa 98926d
      if (min == 0.0 && max == 0.0) return;
shun-iwasawa 98926d
      const TDoublePairProperty::Range &range = prop.getRange();
shun-iwasawa 98926d
shun-iwasawa 98926d
      TDoublePairProperty::Value value = prop.getValue();
shun-iwasawa 98926d
      value.first += min;
shun-iwasawa 98926d
      value.second += max;
shun-iwasawa 98926d
      if (value.first > value.second) value.first = value.second;
shun-iwasawa 98926d
      value.first  = tcrop(value.first, range.first, range.second);
shun-iwasawa 98926d
      value.second = tcrop(value.second, range.first, range.second);
shun-iwasawa 98926d
shun-iwasawa 98926d
      setValue(prop, value);
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
shun-iwasawa 98926d
  } locals = {this};
shun-iwasawa 98926d
shun-iwasawa 98926d
  TPointD halfThick(m_maxThick * 0.5, m_maxThick * 0.5);
shun-iwasawa 98926d
  TRectD invalidateRect(m_brushPos - halfThick, m_brushPos + halfThick);
shun-iwasawa 98926d
Martin van Zijl a10e2f
  if (e.isCtrlPressed() && e.isAltPressed() && !e.isShiftPressed() &&
Martin van Zijl a10e2f
      Preferences::instance()->useCtrlAltToResizeBrushEnabled()) {
Martin van Zijl a10e2f
    // Resize the brush if CTRL+ALT is pressed and the preference is enabled.
shun-iwasawa 98926d
    const TPointD &diff = pos - m_mousePos;
shun-iwasawa 98926d
    double max          = diff.x / 2;
shun-iwasawa 98926d
    double min          = diff.y / 2;
shun-iwasawa 98926d
shun-iwasawa 98926d
    locals.addMinMaxSeparate(m_thickness, min, max);
shun-iwasawa 98926d
shun-iwasawa 98926d
    double radius = m_thickness.getValue().second * 0.5;
shun-iwasawa 98926d
    invalidateRect += TRectD(m_brushPos - TPointD(radius, radius),
shun-iwasawa 98926d
                             m_brushPos + TPointD(radius, radius));
shun-iwasawa 98926d
shun-iwasawa 98926d
  } else {
shun-iwasawa 98926d
    m_mousePos = pos;
shun-iwasawa 98926d
    m_brushPos = pos;
shun-iwasawa 98926d
shun-iwasawa 98926d
    TPointD snapThick(6.0 * m_pixelSize, 6.0 * m_pixelSize);
shun-iwasawa 98926d
    // In order to clear the previous snap indicator
shun-iwasawa 98926d
    if (m_foundFirstSnap)
shun-iwasawa 98926d
      invalidateRect +=
shun-iwasawa 98926d
          TRectD(m_firstSnapPoint - snapThick, m_firstSnapPoint + snapThick);
shun-iwasawa 98926d
shun-iwasawa 98926d
    m_firstSnapPoint = pos;
shun-iwasawa 98926d
    m_foundFirstSnap = false;
shun-iwasawa 98926d
    m_altPressed     = e.isAltPressed() && !e.isCtrlPressed();
shun-iwasawa 98926d
    checkStrokeSnapping(true, m_altPressed);
shun-iwasawa 98926d
    checkGuideSnapping(true, m_altPressed);
shun-iwasawa 98926d
    m_brushPos = m_firstSnapPoint;
shun-iwasawa 98926d
    // In order to draw the snap indicator
shun-iwasawa 98926d
    if (m_foundFirstSnap)
shun-iwasawa 98926d
      invalidateRect +=
shun-iwasawa 98926d
          TRectD(m_firstSnapPoint - snapThick, m_firstSnapPoint + snapThick);
shun-iwasawa 98926d
shun-iwasawa 98926d
    invalidateRect += TRectD(pos - halfThick, pos + halfThick);
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  invalidate(invalidateRect.enlarge(2));
shun-iwasawa 98926d
shun-iwasawa 98926d
  if (m_minThick == 0 && m_maxThick == 0) {
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
shun-iwasawa 98926d
void ToonzVectorBrushTool::checkStrokeSnapping(bool beforeMousePress,
shun-iwasawa 98926d
                                               bool invertCheck) {
shun-iwasawa 98926d
  if (Preferences::instance()->getVectorSnappingTarget() == 1) return;
shun-iwasawa 98926d
shun-iwasawa 98926d
  TVectorImageP vi(getImage(false));
shun-iwasawa 76d093
  bool checkSnap = m_snap.getValue();
shun-iwasawa 98926d
  if (invertCheck) checkSnap = !checkSnap;
Martin van Zijl dbf19d
  m_dragDraw = true;
shun-iwasawa 98926d
  if (vi && checkSnap) {
shun-iwasawa 98926d
    double minDistance2 = m_minDistance2;
shun-iwasawa 98926d
    if (beforeMousePress)
shun-iwasawa 98926d
      m_strokeIndex1 = -1;
shun-iwasawa 98926d
    else
shun-iwasawa 98926d
      m_strokeIndex2 = -1;
shun-iwasawa 98926d
    int i, strokeNumber = vi->getStrokeCount();
shun-iwasawa 98926d
    TStroke *stroke;
shun-iwasawa 98926d
    double distance2, outW;
shun-iwasawa 98926d
    bool snapFound = false;
shun-iwasawa 98926d
    TThickPoint point1;
shun-iwasawa 98926d
shun-iwasawa 98926d
    for (i = 0; i < strokeNumber; i++) {
shun-iwasawa 98926d
      stroke = vi->getStroke(i);
shun-iwasawa 98926d
      if (stroke->getNearestW(m_mousePos, outW, distance2) &&
shun-iwasawa 98926d
          distance2 < minDistance2) {
shun-iwasawa 98926d
        minDistance2                      = distance2;
shun-iwasawa 98926d
        beforeMousePress ? m_strokeIndex1 = i : m_strokeIndex2 = i;
shun-iwasawa 98926d
        if (areAlmostEqual(outW, 0.0, 1e-3))
shun-iwasawa 98926d
          beforeMousePress ? m_w1 = 0.0 : m_w2 = 0.0;
shun-iwasawa 98926d
        else if (areAlmostEqual(outW, 1.0, 1e-3))
shun-iwasawa 98926d
          beforeMousePress ? m_w1 = 1.0 : m_w2 = 1.0;
shun-iwasawa 98926d
        else
shun-iwasawa 98926d
          beforeMousePress ? m_w1 = outW : m_w2 = outW;
shun-iwasawa 98926d
shun-iwasawa 98926d
        beforeMousePress ? point1 = stroke->getPoint(m_w1)
shun-iwasawa 98926d
                         : point1 = stroke->getPoint(m_w2);
shun-iwasawa 98926d
        snapFound                 = true;
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
    // compare to first point of current stroke
shun-iwasawa 98926d
    if (beforeMousePress && snapFound) {
shun-iwasawa 98926d
      m_firstSnapPoint = TPointD(point1.x, point1.y);
shun-iwasawa 98926d
      m_foundFirstSnap = true;
shun-iwasawa 98926d
    } else if (!beforeMousePress) {
shun-iwasawa 98926d
      if (!snapFound) {
shun-iwasawa 98926d
        TPointD tempPoint        = m_track.getFirstPoint();
shun-iwasawa 98926d
        double distanceFromStart = tdistance2(m_mousePos, tempPoint);
shun-iwasawa 98926d
shun-iwasawa 98926d
        if (distanceFromStart < m_minDistance2) {
shun-iwasawa 98926d
          point1     = tempPoint;
shun-iwasawa 98926d
          distance2  = distanceFromStart;
shun-iwasawa 98926d
          snapFound  = true;
shun-iwasawa 98926d
          m_snapSelf = true;
shun-iwasawa 98926d
        }
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
      if (snapFound) {
shun-iwasawa 76d093
        m_lastSnapPoint = TPointD(point1.x, point1.y);
shun-iwasawa 76d093
        m_foundLastSnap = true;
shun-iwasawa 98926d
        if (distance2 < 2.0) m_dragDraw = false;
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//-------------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::checkGuideSnapping(bool beforeMousePress,
shun-iwasawa 98926d
                                              bool invertCheck) {
shun-iwasawa 98926d
  if (Preferences::instance()->getVectorSnappingTarget() == 0) return;
shun-iwasawa 98926d
  bool foundSnap;
shun-iwasawa 98926d
  TPointD snapPoint;
shun-iwasawa 98926d
  beforeMousePress ? foundSnap = m_foundFirstSnap : foundSnap = m_foundLastSnap;
shun-iwasawa 98926d
  beforeMousePress ? snapPoint = m_firstSnapPoint : snapPoint = m_lastSnapPoint;
shun-iwasawa 98926d
shun-iwasawa 76d093
  bool checkSnap = m_snap.getValue();
shun-iwasawa 98926d
  if (invertCheck) checkSnap = !checkSnap;
shun-iwasawa 98926d
shun-iwasawa 98926d
  if (checkSnap) {
shun-iwasawa 98926d
    // check guide snapping
shun-iwasawa 98926d
    int vGuideCount = 0, hGuideCount = 0;
shun-iwasawa 98926d
    double guideDistance  = sqrt(m_minDistance2);
shun-iwasawa 98926d
    TTool::Viewer *viewer = getViewer();
shun-iwasawa 98926d
    if (viewer) {
shun-iwasawa 98926d
      vGuideCount = viewer->getVGuideCount();
shun-iwasawa 98926d
      hGuideCount = viewer->getHGuideCount();
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
    double distanceToVGuide = -1.0, distanceToHGuide = -1.0;
shun-iwasawa 98926d
    double vGuide, hGuide;
shun-iwasawa 98926d
    bool useGuides = false;
shun-iwasawa 98926d
    if (vGuideCount) {
shun-iwasawa 98926d
      for (int j = 0; j < vGuideCount; j++) {
shun-iwasawa 98926d
        double guide        = viewer->getVGuide(j);
Rozhuk Ivan 823a31
        double tempDistance = std::abs(guide - m_mousePos.y);
shun-iwasawa 98926d
        if (tempDistance < guideDistance &&
shun-iwasawa 98926d
            (distanceToVGuide < 0 || tempDistance < distanceToVGuide)) {
shun-iwasawa 98926d
          distanceToVGuide = tempDistance;
shun-iwasawa 98926d
          vGuide           = guide;
shun-iwasawa 98926d
          useGuides        = true;
shun-iwasawa 98926d
        }
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
    if (hGuideCount) {
shun-iwasawa 98926d
      for (int j = 0; j < hGuideCount; j++) {
shun-iwasawa 98926d
        double guide        = viewer->getHGuide(j);
Rozhuk Ivan 823a31
        double tempDistance = std::abs(guide - m_mousePos.x);
shun-iwasawa 98926d
        if (tempDistance < guideDistance &&
shun-iwasawa 98926d
            (distanceToHGuide < 0 || tempDistance < distanceToHGuide)) {
shun-iwasawa 98926d
          distanceToHGuide = tempDistance;
shun-iwasawa 98926d
          hGuide           = guide;
shun-iwasawa 98926d
          useGuides        = true;
shun-iwasawa 98926d
        }
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
    if (useGuides && foundSnap) {
Rozhuk Ivan 823a31
      double currYDistance = std::abs(snapPoint.y - m_mousePos.y);
Rozhuk Ivan 823a31
      double currXDistance = std::abs(snapPoint.x - m_mousePos.x);
shun-iwasawa 98926d
      double hypotenuse =
shun-iwasawa 98926d
          sqrt(pow(currYDistance, 2.0) + pow(currXDistance, 2.0));
shun-iwasawa 98926d
      if ((distanceToVGuide >= 0 && distanceToVGuide < hypotenuse) ||
shun-iwasawa 98926d
          (distanceToHGuide >= 0 && distanceToHGuide < hypotenuse)) {
shun-iwasawa 98926d
        useGuides  = true;
shun-iwasawa 98926d
        m_snapSelf = false;
shun-iwasawa 98926d
      } else
shun-iwasawa 98926d
        useGuides = false;
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
    if (useGuides) {
shun-iwasawa 98926d
      assert(distanceToHGuide >= 0 || distanceToVGuide >= 0);
shun-iwasawa 98926d
      if (distanceToHGuide < 0 ||
shun-iwasawa 98926d
          (distanceToVGuide <= distanceToHGuide && distanceToVGuide >= 0)) {
shun-iwasawa 98926d
        snapPoint.y = vGuide;
shun-iwasawa 98926d
        snapPoint.x = m_mousePos.x;
shun-iwasawa 98926d
shun-iwasawa 98926d
      } else {
shun-iwasawa 98926d
        snapPoint.y = m_mousePos.y;
shun-iwasawa 98926d
        snapPoint.x = hGuide;
shun-iwasawa 98926d
      }
shun-iwasawa 98926d
      beforeMousePress ? m_foundFirstSnap = true : m_foundLastSnap = true;
shun-iwasawa 76d093
      beforeMousePress ? m_firstSnapPoint                          = snapPoint
shun-iwasawa 76d093
                       : m_lastSnapPoint                           = snapPoint;
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//-------------------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzVectorBrushTool::draw() {
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
shun-iwasawa 98926d
  TImageP img = getImage(false, 1);
shun-iwasawa 98926d
shun-iwasawa 98926d
  // Draw track
shun-iwasawa 98926d
  tglColor(m_isPrompting ? TPixel32::Green : m_currentColor);
shun-iwasawa 98926d
  m_track.drawAllFragments();
shun-iwasawa 98926d
shun-iwasawa 98926d
  // snapping
shun-iwasawa 98926d
  TVectorImageP vi = img;
shun-iwasawa 98926d
  if (m_snap.getValue() != m_altPressed) {
shun-iwasawa 98926d
    m_pixelSize  = getPixelSize();
shun-iwasawa 98926d
    double thick = 6.0 * m_pixelSize;
shun-iwasawa 98926d
    if (m_foundFirstSnap) {
shun-iwasawa 98926d
      tglColor(TPixelD(0.1, 0.9, 0.1));
shun-iwasawa 98926d
      tglDrawCircle(m_firstSnapPoint, thick);
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
shun-iwasawa 98926d
    TThickPoint point2;
shun-iwasawa 98926d
shun-iwasawa 98926d
    if (m_foundLastSnap) {
shun-iwasawa 98926d
      tglColor(TPixelD(0.1, 0.9, 0.1));
shun-iwasawa 98926d
      tglDrawCircle(m_lastSnapPoint, thick);
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  // frame range
shun-iwasawa 98926d
  if (m_firstStroke) {
shun-iwasawa 98926d
    glColor3d(1.0, 0.0, 0.0);
shun-iwasawa 98926d
    m_rangeTrack.drawAllFragments();
shun-iwasawa 98926d
    glColor3d(0.0, 0.6, 0.0);
shun-iwasawa 98926d
    TPointD firstPoint        = m_rangeTrack.getFirstPoint();
shun-iwasawa 98926d
    TPointD topLeftCorner     = TPointD(firstPoint.x - 5, firstPoint.y - 5);
shun-iwasawa 98926d
    TPointD topRightCorner    = TPointD(firstPoint.x + 5, firstPoint.y - 5);
shun-iwasawa 98926d
    TPointD bottomLeftCorner  = TPointD(firstPoint.x - 5, firstPoint.y + 5);
shun-iwasawa 98926d
    TPointD bottomRightCorner = TPointD(firstPoint.x + 5, firstPoint.y + 5);
shun-iwasawa 98926d
    tglDrawSegment(topLeftCorner, bottomRightCorner);
shun-iwasawa 98926d
    tglDrawSegment(topRightCorner, bottomLeftCorner);
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
  TImageP img = getImage(false);
shun-iwasawa 98926d
shun-iwasawa 98926d
  m_minThick = m_thickness.getValue().first;
shun-iwasawa 98926d
  m_maxThick = m_thickness.getValue().second;
shun-iwasawa 98926d
shun-iwasawa 98926d
  Application *app = getApplication();
shun-iwasawa 98926d
shun-iwasawa 98926d
  m_styleId       = app->getCurrentLevelStyleIndex();
shun-iwasawa 98926d
  TColorStyle *cs = app->getCurrentLevelStyle();
shun-iwasawa 98926d
  if (cs) {
shun-iwasawa 98926d
    TRasterStyleFx *rfx = cs->getRasterStyleFx();
shun-iwasawa 98926d
    m_active            = cs->isStrokeStyle() || (rfx && rfx->isInkStyle());
shun-iwasawa 98926d
    m_currentColor      = cs->getAverageColor();
shun-iwasawa 98926d
    m_currentColor.m    = 255;
shun-iwasawa 98926d
  } else {
shun-iwasawa 98926d
    m_currentColor = TPixel32::Black;
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
  m_active = img;
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
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() {
shun-iwasawa 98926d
  m_rangeTrack.clear();
shun-iwasawa 98926d
  m_firstFrameId = -1;
shun-iwasawa 98926d
  if (m_firstStroke) {
shun-iwasawa 98926d
    delete m_firstStroke;
shun-iwasawa 98926d
    m_firstStroke = 0;
shun-iwasawa 98926d
  }
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;
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);
manongjohn df5842
  m_snap.setValue(V_VectorBrushSnap);
manongjohn df5842
  m_snapSensitivity.setIndex(V_VectorBrushSnapSensitivity);
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
}