Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "brushtool.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzTools includes
Toshihiro Shimizu 890ddd
#include "tools/toolhandle.h"
Toshihiro Shimizu 890ddd
#include "tools/toolutils.h"
Toshihiro Shimizu 890ddd
#include "tools/tooloptions.h"
Toshihiro Shimizu 890ddd
#include "bluredbrush.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzQt includes
Toshihiro Shimizu 890ddd
#include "toonzqt/dvdialog.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/imageutils.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzLib includes
Toshihiro Shimizu 890ddd
#include "toonz/tobjecthandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/txsheethandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshlevelhandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/tframehandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/tcolumnhandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/txsheet.h"
Toshihiro Shimizu 890ddd
#include "toonz/tstageobject.h"
Toshihiro Shimizu 890ddd
#include "toonz/tstageobjectspline.h"
Toshihiro Shimizu 890ddd
#include "toonz/rasterstrokegenerator.h"
Toshihiro Shimizu 890ddd
#include "toonz/ttileset.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshsimplelevel.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzimageutils.h"
Toshihiro Shimizu 890ddd
#include "toonz/palettecontroller.h"
Toshihiro Shimizu 890ddd
#include "toonz/stage2.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tstream.h"
Toshihiro Shimizu 890ddd
#include "tcolorstyles.h"
Toshihiro Shimizu 890ddd
#include "tvectorimage.h"
Toshihiro Shimizu 890ddd
#include "tenv.h"
Toshihiro Shimizu 890ddd
#include "tregion.h"
Toshihiro Shimizu 890ddd
#include "tstroke.h"
Toshihiro Shimizu 890ddd
#include "tgl.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Qt includes
Toshihiro Shimizu 890ddd
#include <qpainter></qpainter>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace ToolUtils;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TEnv::DoubleVar VectorBrushMinSize("InknpaintVectorBrushMinSize", 1);
Toshihiro Shimizu 890ddd
TEnv::DoubleVar VectorBrushMaxSize("InknpaintVectorBrushMaxSize", 5);
Jeremy Bullock 9ec2d1
TEnv::IntVar VectorCapStyle("InknpaintVectorCapStyle", 1);
Jeremy Bullock f7993a
TEnv::IntVar VectorJoinStyle("InknpaintVectorJoinStyle", 1);
Toshihiro Shimizu 890ddd
TEnv::IntVar VectorMiterValue("InknpaintVectorMiterValue", 4);
Toshihiro Shimizu 890ddd
TEnv::DoubleVar RasterBrushMinSize("InknpaintRasterBrushMinSize", 1);
Toshihiro Shimizu 890ddd
TEnv::DoubleVar RasterBrushMaxSize("InknpaintRasterBrushMaxSize", 5);
Toshihiro Shimizu 890ddd
TEnv::DoubleVar BrushAccuracy("InknpaintBrushAccuracy", 20);
walkerka 2e244a
TEnv::DoubleVar BrushSmooth("InknpaintBrushSmooth", 0);
Toshihiro Shimizu 890ddd
TEnv::IntVar BrushSelective("InknpaintBrushSelective", 0);
Toshihiro Shimizu 890ddd
TEnv::IntVar BrushBreakSharpAngles("InknpaintBrushBreakSharpAngles", 0);
Toshihiro Shimizu 890ddd
TEnv::IntVar RasterBrushPencilMode("InknpaintRasterBrushPencilMode", 0);
Campbell Barton f49389
TEnv::IntVar BrushPressureSensitivity("InknpaintBrushPressureSensitivity", 1);
Toshihiro Shimizu 890ddd
TEnv::DoubleVar RasterBrushHardness("RasterBrushHardness", 100);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define ROUNDC_WSTR L"round_cap"
Toshihiro Shimizu 890ddd
#define BUTT_WSTR L"butt_cap"
Toshihiro Shimizu 890ddd
#define PROJECTING_WSTR L"projecting_cap"
Toshihiro Shimizu 890ddd
#define ROUNDJ_WSTR L"round_join"
Toshihiro Shimizu 890ddd
#define BEVEL_WSTR L"bevel_join"
Toshihiro Shimizu 890ddd
#define MITER_WSTR L"miter_join"
Toshihiro Shimizu 890ddd
#define CUSTOM_WSTR L"<custom>"</custom>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// (Da mettere in libreria) : funzioni che spezzano una stroke
Toshihiro Shimizu 890ddd
// nei suoi punti angolosi. Lo facciamo specialmente per limitare
Toshihiro Shimizu 890ddd
// i problemi di fill.
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// Split a stroke in n+1 parts, according to n parameter values
Toshihiro Shimizu 890ddd
// Input:
Toshihiro Shimizu 890ddd
//      stroke            = stroke to split
Shinya Kitaoka 120a6e
//      parameterValues[] = vector of parameters where I want to split the
Shinya Kitaoka 120a6e
//      stroke
Toshihiro Shimizu 890ddd
//                          assert: 0
Toshihiro Shimizu 890ddd
// Output:
Toshihiro Shimizu 890ddd
//      strokes[]         = the split strokes
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// note: stroke is unchanged
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
Campbell Barton 8c6c57
static void split(TStroke *stroke, const std::vector<double> ¶meterValues,</double>
Campbell Barton 8c6c57
                  std::vector<tstroke *=""> &strokes) {</tstroke>
Shinya Kitaoka 120a6e
  TThickPoint p2;
Shinya Kitaoka 120a6e
  std::vector<tthickpoint> points;</tthickpoint>
Shinya Kitaoka 120a6e
  TThickPoint lastPoint = stroke->getControlPoint(0);
Shinya Kitaoka 120a6e
  int n                 = parameterValues.size();
Shinya Kitaoka 120a6e
  int chunk;
Shinya Kitaoka 120a6e
  double t;
Shinya Kitaoka 120a6e
  int last_chunk = -1, startPoint = 0;
Shinya Kitaoka 120a6e
  double lastLocT = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int i = 0; i < n; i++) {
Shinya Kitaoka 120a6e
    points.push_back(lastPoint);  // Add first point of the stroke
Shinya Kitaoka 120a6e
    double w =
Shinya Kitaoka 120a6e
        parameterValues[i];  // Global parameter. along the stroke 0<=w<=1
Shinya Kitaoka 120a6e
    stroke->getChunkAndT(w, chunk,
Shinya Kitaoka 120a6e
                         t);  // t: local parameter in the chunk-th quadratic
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (i == 0)
Shinya Kitaoka 120a6e
      startPoint = 1;
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      int indexAfterLastT =
Shinya Kitaoka 120a6e
          stroke->getControlPointIndexAfterParameter(parameterValues[i - 1]);
Shinya Kitaoka 120a6e
      startPoint = indexAfterLastT;
Shinya Kitaoka 120a6e
      if ((indexAfterLastT & 1) && lastLocT != 1) startPoint++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    int endPoint = 2 * chunk + 1;
Shinya Kitaoka 120a6e
    if (lastLocT != 1 && i > 0) {
Shinya Kitaoka 120a6e
      if (last_chunk != chunk || t == 1)
Shinya Kitaoka 120a6e
        points.push_back(p2);  // If the last local t is not an extreme
Shinya Kitaoka 120a6e
                               // add the point p2
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    for (int j = startPoint; j < endPoint; j++)
Shinya Kitaoka 120a6e
      points.push_back(stroke->getControlPoint(j));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TThickPoint p, A, B, C;
Shinya Kitaoka 120a6e
    p       = stroke->getPoint(w);
Shinya Kitaoka 120a6e
    C       = stroke->getControlPoint(2 * chunk + 2);
Shinya Kitaoka 120a6e
    B       = stroke->getControlPoint(2 * chunk + 1);
Shinya Kitaoka 120a6e
    A       = stroke->getControlPoint(2 * chunk);
Shinya Kitaoka 120a6e
    p.thick = A.thick;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (last_chunk != chunk) {
Shinya Kitaoka 120a6e
      TThickPoint p1 = (1 - t) * A + t * B;
Shinya Kitaoka 120a6e
      points.push_back(p1);
Shinya Kitaoka 120a6e
      p.thick = p1.thick;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      if (t != 1) {
Shinya Kitaoka 120a6e
        // If the i-th cut point belong to the same chunk of the (i-1)-th cut
Shinya Kitaoka 120a6e
        // point.
Shinya Kitaoka 120a6e
        double tInters  = lastLocT / t;
Shinya Kitaoka 120a6e
        TThickPoint p11 = (1 - t) * A + t * B;
Shinya Kitaoka 120a6e
        TThickPoint p1  = (1 - tInters) * p11 + tInters * p;
Shinya Kitaoka 120a6e
        points.push_back(p1);
Shinya Kitaoka 120a6e
        p.thick = p1.thick;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    points.push_back(p);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (t != 1) p2 = (1 - t) * B + t * C;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    assert(points.size() & 1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // Add new stroke
Shinya Kitaoka 120a6e
    TStroke *strokeAdd = new TStroke(points);
Shinya Kitaoka 120a6e
    strokeAdd->setStyle(stroke->getStyle());
Shinya Kitaoka 120a6e
    strokeAdd->outlineOptions() = stroke->outlineOptions();
Shinya Kitaoka 120a6e
    strokes.push_back(strokeAdd);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    lastPoint  = p;
Shinya Kitaoka 120a6e
    last_chunk = chunk;
Shinya Kitaoka 120a6e
    lastLocT   = t;
Shinya Kitaoka 120a6e
    points.clear();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  // Add end stroke
Shinya Kitaoka 120a6e
  points.push_back(lastPoint);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (lastLocT != 1) points.push_back(p2);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  startPoint =
Shinya Kitaoka 120a6e
      stroke->getControlPointIndexAfterParameter(parameterValues[n - 1]);
Shinya Kitaoka 120a6e
  if ((stroke->getControlPointIndexAfterParameter(parameterValues[n - 1]) &
Shinya Kitaoka 120a6e
       1) &&
Shinya Kitaoka 120a6e
      lastLocT != 1)
Shinya Kitaoka 120a6e
    startPoint++;
Shinya Kitaoka 120a6e
  for (int j = startPoint; j < stroke->getControlPointCount(); j++)
Shinya Kitaoka 120a6e
    points.push_back(stroke->getControlPoint(j));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  assert(points.size() & 1);
Shinya Kitaoka 120a6e
  TStroke *strokeAdd = new TStroke(points);
Shinya Kitaoka 120a6e
  strokeAdd->setStyle(stroke->getStyle());
Shinya Kitaoka 120a6e
  strokeAdd->outlineOptions() = stroke->outlineOptions();
Shinya Kitaoka 120a6e
  strokes.push_back(strokeAdd);
Shinya Kitaoka 120a6e
  points.clear();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// Compute Parametric Curve Curvature
Shinya Kitaoka 120a6e
// By Formula:
Toshihiro Shimizu 890ddd
// k(t)=(|p'(t) x p''(t)|)/Norm2(p')^3
Toshihiro Shimizu 890ddd
// p(t) is parametric curve
Toshihiro Shimizu 890ddd
// Input:
Toshihiro Shimizu 890ddd
//      dp  = First Derivate.
Toshihiro Shimizu 890ddd
//      ddp = Second Derivate
Toshihiro Shimizu 890ddd
// Output:
Toshihiro Shimizu 890ddd
//      return curvature value.
Shinya Kitaoka 120a6e
//      Note: if the curve is a single point (that's dp=0) or it is a straight
Shinya Kitaoka 120a6e
//      line (that's ddp=0) return 0
Shinya Kitaoka 120a6e
Campbell Barton 8c6c57
static double curvature(TPointD dp, TPointD ddp) {
Shinya Kitaoka 120a6e
  if (dp == TPointD(0, 0))
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return fabs(cross(dp, ddp) / pow(norm2(dp), 1.5));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Find the max curvature points of a stroke.
Toshihiro Shimizu 890ddd
// Input:
Toshihiro Shimizu 890ddd
//      stroke.
Toshihiro Shimizu 890ddd
//      angoloLim =  Value (radians) of the Corner between two tangent vector.
Toshihiro Shimizu 890ddd
//                   Up this value the two corner can be considered angular.
Toshihiro Shimizu 890ddd
//      curvMaxLim = Value of the max curvature.
Shinya Kitaoka 120a6e
//                   Up this value the point can be considered a max curvature
Shinya Kitaoka 120a6e
//                   point.
Toshihiro Shimizu 890ddd
// Output:
Toshihiro Shimizu 890ddd
//      parameterValues = vector of max curvature parameter points
Toshihiro Shimizu 890ddd
Campbell Barton 8c6c57
static void findMaxCurvPoints(TStroke *stroke, const float &angoloLim,
Campbell Barton 8c6c57
                              const float &curvMaxLim,
Campbell Barton 8c6c57
                              std::vector<double> ¶meterValues) {</double>
Shinya Kitaoka 120a6e
  TPointD tg1, tg2;  // Tangent vectors
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TPointD dp, ddp;  // First and Second derivate.
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  parameterValues.clear();
Shinya Kitaoka 120a6e
  int cpn = stroke ? stroke->getControlPointCount() : 0;
Shinya Kitaoka 120a6e
  for (int j = 2; j < cpn; j += 2) {
Shinya Kitaoka 120a6e
    TPointD p0 = stroke->getControlPoint(j - 2);
Shinya Kitaoka 120a6e
    TPointD p1 = stroke->getControlPoint(j - 1);
Shinya Kitaoka 120a6e
    TPointD p2 = stroke->getControlPoint(j);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TPointD q = p1 - (p0 + p2) * 0.5;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // Search corner point
Shinya Kitaoka 120a6e
    if (j > 2) {
Shinya Kitaoka 120a6e
      tg2 = -p0 + p2 + 2 * q;  // Tangent vector to this chunk at t=0
Shinya Kitaoka 120a6e
      double prod_scal =
Shinya Kitaoka 120a6e
          tg2 * tg1;  // Inner product between tangent vectors at t=0.
Shinya Kitaoka 120a6e
      assert(tg1 != TPointD(0, 0) || tg2 != TPointD(0, 0));
Shinya Kitaoka 120a6e
      // Compute corner between two tangent vectors
Shinya Kitaoka 120a6e
      double angolo =
Shinya Kitaoka 120a6e
          acos(prod_scal / (pow(norm2(tg2), 0.5) * pow(norm2(tg1), 0.5)));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      // Add corner point
Shinya Kitaoka 120a6e
      if (angolo > angoloLim) {
Shinya Kitaoka 120a6e
        double w = getWfromChunkAndT(stroke, (UINT)(0.5 * (j - 2)),
Shinya Kitaoka 120a6e
                                     0);  //  transform lacal t to global t
Shinya Kitaoka 120a6e
        parameterValues.push_back(w);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    tg1 = -p0 + p2 - 2 * q;  // Tangent vector to this chunk at t=1
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // End search corner point
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // Search max curvature point
Shinya Kitaoka 120a6e
    // Value of t where the curvature function has got an extreme.
Shinya Kitaoka 120a6e
    // (Point where first derivate is null)
Shinya Kitaoka 120a6e
    double estremo_int = 0;
Shinya Kitaoka 120a6e
    double t           = -1;
Shinya Kitaoka 120a6e
    if (q != TPointD(0, 0)) {
Shinya Kitaoka 120a6e
      t = 0.25 * (2 * q.x * q.x + 2 * q.y * q.y - q.x * p0.x + q.x * p2.x -
Shinya Kitaoka 120a6e
                  q.y * p0.y + q.y * p2.y) /
Shinya Kitaoka 120a6e
          (q.x * q.x + q.y * q.y);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      dp  = -p0 + p2 + 2 * q - 4 * t * q;  // First derivate of the curve
Shinya Kitaoka 120a6e
      ddp = -4 * q;                        // Second derivate of the curve
Shinya Kitaoka 120a6e
      estremo_int = curvature(dp, ddp);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      double h    = 0.01;
Shinya Kitaoka 120a6e
      dp          = -p0 + p2 + 2 * q - 4 * (t + h) * q;
Shinya Kitaoka 120a6e
      double c_dx = curvature(dp, ddp);
Shinya Kitaoka 120a6e
      dp          = -p0 + p2 + 2 * q - 4 * (t - h) * q;
Shinya Kitaoka 120a6e
      double c_sx = curvature(dp, ddp);
Shinya Kitaoka 120a6e
      // Check the point is a max and not a minimum
Shinya Kitaoka 120a6e
      if (estremo_int < c_dx && estremo_int < c_sx) {
Shinya Kitaoka 120a6e
        estremo_int = 0;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    double curv_max = estremo_int;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // Compute curvature at the extreme of interval [0,1]
Shinya Kitaoka 120a6e
    // Compute curvature at t=0 (Left extreme)
Shinya Kitaoka 120a6e
    dp                = -p0 + p2 + 2 * q;
Shinya Kitaoka 120a6e
    double estremo_sx = curvature(dp, ddp);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // Compute curvature at t=1 (Right extreme)
Shinya Kitaoka 120a6e
    dp                = -p0 + p2 - 2 * q;
Shinya Kitaoka 120a6e
    double estremo_dx = curvature(dp, ddp);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // Compare curvature at the extreme of interval [0,1] with the internal
Shinya Kitaoka 120a6e
    // value
Shinya Kitaoka 120a6e
    double t_ext;
Shinya Kitaoka 120a6e
    if (estremo_sx >= estremo_dx)
Shinya Kitaoka 120a6e
      t_ext = 0;
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      t_ext           = 1;
Shinya Kitaoka 120a6e
    double maxEstremi = std::max(estremo_dx, estremo_sx);
Shinya Kitaoka 120a6e
    if (maxEstremi > estremo_int) {
Shinya Kitaoka 120a6e
      t        = t_ext;
Shinya Kitaoka 120a6e
      curv_max = maxEstremi;
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    // Add max curvature point
Shinya Kitaoka 120a6e
    if (t >= 0 && t <= 1 && curv_max > curvMaxLim) {
Shinya Kitaoka 120a6e
      double w = getWfromChunkAndT(stroke, (UINT)(0.5 * (j - 2)),
Shinya Kitaoka 120a6e
                                   t);  // transform local t to global t
Shinya Kitaoka 120a6e
      parameterValues.push_back(w);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    // End search max curvature point
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  // Delete duplicate of parameterValues
Shinya Kitaoka 120a6e
  // Because some max cuvature point can coincide with the corner point
Shinya Kitaoka 120a6e
  if ((int)parameterValues.size() > 1) {
Shinya Kitaoka 120a6e
    std::sort(parameterValues.begin(), parameterValues.end());
Shinya Kitaoka 120a6e
    parameterValues.erase(
Shinya Kitaoka 120a6e
        std::unique(parameterValues.begin(), parameterValues.end()),
Shinya Kitaoka 120a6e
        parameterValues.end());
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Campbell Barton 8c6c57
static void addStroke(TTool::Application *application, const TVectorImageP &vi,
Campbell Barton 8c6c57
                      TStroke *stroke, bool breakAngles, bool frameCreated,
Campbell Barton 8c6c57
                      bool levelCreated) {
Shinya Kitaoka 120a6e
  QMutexLocker lock(vi->getMutex());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (application->getCurrentObject()->isSpline()) {
Shinya Kitaoka 120a6e
    application->getCurrentXsheet()->notifyXsheetChanged();
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::vector<double> corners;</double>
Shinya Kitaoka 120a6e
  std::vector<tstroke *=""> strokes;</tstroke>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  const float angoloLim =
Shinya Kitaoka 120a6e
      1;  // Value (radians) of the Corner between two tangent vector.
Shinya Kitaoka 120a6e
          // Up this value the two corner can be considered angular.
Shinya Kitaoka 120a6e
  const float curvMaxLim = 0.8;  // Value of the max curvature.
Shinya Kitaoka 120a6e
  // Up this value the point can be considered a max curvature point.
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  findMaxCurvPoints(stroke, angoloLim, curvMaxLim, corners);
Shinya Kitaoka 120a6e
  TXshSimpleLevel *sl = application->getCurrentLevel()->getSimpleLevel();
Shinya Kitaoka 120a6e
  TFrameId id = application->getCurrentTool()->getTool()->getCurrentFid();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!corners.empty()) {
Shinya Kitaoka 120a6e
    if (breakAngles)
Shinya Kitaoka 120a6e
      split(stroke, corners, strokes);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      strokes.push_back(new TStroke(*stroke));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    int n = strokes.size();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TUndoManager::manager()->beginBlock();
Shinya Kitaoka 120a6e
    for (int i = 0; i < n; i++) {
Shinya Kitaoka 120a6e
      std::vector<tfilledregioninf> *fillInformation =</tfilledregioninf>
Shinya Kitaoka 120a6e
          new std::vector<tfilledregioninf>;</tfilledregioninf>
Shinya Kitaoka 120a6e
      ImageUtils::getFillingInformationOverlappingArea(vi, *fillInformation,
Shinya Kitaoka 120a6e
                                                       stroke->getBBox());
Shinya Kitaoka 120a6e
      TStroke *str = new TStroke(*strokes[i]);
Shinya Kitaoka 120a6e
      vi->addStroke(str);
Shinya Kitaoka 120a6e
      TUndoManager::manager()->add(new UndoPencil(str, fillInformation, sl, id,
Shinya Kitaoka 120a6e
                                                  frameCreated, levelCreated));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    TUndoManager::manager()->endBlock();
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    std::vector<tfilledregioninf> *fillInformation =</tfilledregioninf>
Shinya Kitaoka 120a6e
        new std::vector<tfilledregioninf>;</tfilledregioninf>
Shinya Kitaoka 120a6e
    ImageUtils::getFillingInformationOverlappingArea(vi, *fillInformation,
Shinya Kitaoka 120a6e
                                                     stroke->getBBox());
Shinya Kitaoka 120a6e
    TStroke *str = new TStroke(*stroke);
Shinya Kitaoka 120a6e
    vi->addStroke(str);
Shinya Kitaoka 120a6e
    TUndoManager::manager()->add(new UndoPencil(str, fillInformation, sl, id,
Shinya Kitaoka 120a6e
                                                frameCreated, levelCreated));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
shun_iwasawa 597034
  // Update regions. It will call roundStroke() in
shun_iwasawa 597034
  // TVectorImage::Imp::findIntersections().
shun_iwasawa 597034
  // roundStroke() will slightly modify all the stroke positions.
shun_iwasawa 597034
  // It is needed to update information for Fill Check.
shun_iwasawa 597034
  vi->findRegions();
shun_iwasawa 597034
Shinya Kitaoka 120a6e
  for (int k = 0; k < (int)strokes.size(); k++) delete strokes[k];
Shinya Kitaoka 120a6e
  strokes.clear();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  application->getCurrentTool()->getTool()->notifyImageChanged();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// Gennaro: end
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// Helper functions and classes
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void addStrokeToImage(TTool::Application *application, const TVectorImageP &vi,
Shinya Kitaoka 120a6e
                      TStroke *stroke, bool breakAngles, bool frameCreated,
Shinya Kitaoka 120a6e
                      bool levelCreated) {
Shinya Kitaoka 120a6e
  QMutexLocker lock(vi->getMutex());
Shinya Kitaoka 120a6e
  addStroke(application, vi.getPointer(), stroke, breakAngles, frameCreated,
Shinya Kitaoka 120a6e
            levelCreated);
Shinya Kitaoka 120a6e
  // la notifica viene gia fatta da addStroke!
Shinya Kitaoka 120a6e
  // getApplication()->getCurrentTool()->getTool()->notifyImageChanged();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class RasterBrushUndo final : public TRasterUndo {
Shinya Kitaoka 120a6e
  std::vector<tthickpoint> m_points;</tthickpoint>
Shinya Kitaoka 120a6e
  int m_styleId;
Shinya Kitaoka 120a6e
  bool m_selective;
Shinya Kitaoka 120a6e
  bool m_isPencil;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  RasterBrushUndo(TTileSetCM32 *tileSet, const std::vector<tthickpoint> &points,</tthickpoint>
Shinya Kitaoka 120a6e
                  int styleId, bool selective, TXshSimpleLevel *level,
Shinya Kitaoka 120a6e
                  const TFrameId &frameId, bool isPencil, bool isFrameCreated,
Shinya Kitaoka 120a6e
                  bool isLevelCreated)
Shinya Kitaoka 120a6e
      : TRasterUndo(tileSet, level, frameId, isFrameCreated, isLevelCreated, 0)
Shinya Kitaoka 120a6e
      , m_points(points)
Shinya Kitaoka 120a6e
      , m_styleId(styleId)
Shinya Kitaoka 120a6e
      , m_selective(selective)
Shinya Kitaoka 120a6e
      , m_isPencil(isPencil) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void redo() const override {
Shinya Kitaoka 120a6e
    insertLevelAndFrameIfNeeded();
Shinya Kitaoka 120a6e
    TToonzImageP image = getImage();
Shinya Kitaoka 120a6e
    TRasterCM32P ras   = image->getRaster();
Shinya Kitaoka 120a6e
    RasterStrokeGenerator m_rasterTrack(
Shinya Kitaoka 120a6e
        ras, BRUSH, NONE, m_styleId, m_points[0], m_selective, 0, !m_isPencil);
Shinya Kitaoka 120a6e
    m_rasterTrack.setPointsSequence(m_points);
Shinya Kitaoka 120a6e
    m_rasterTrack.generateStroke(m_isPencil);
Shinya Kitaoka 120a6e
    image->setSavebox(image->getSavebox() +
Shinya Kitaoka 120a6e
                      m_rasterTrack.getBBox(m_rasterTrack.getPointsSequence()));
Shinya Kitaoka 120a6e
    ToolUtils::updateSaveBox();
Shinya Kitaoka 120a6e
    TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
Shinya Kitaoka 120a6e
    notifyImageChanged();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 38fd86
  int getSize() const override {
Shinya Kitaoka 38fd86
    return sizeof(*this) + TRasterUndo::getSize();
Shinya Kitaoka 38fd86
  }
Shinya Kitaoka 473e70
  QString getToolName() override { return QString("Brush Tool"); }
Shinya Kitaoka 473e70
  int getHistoryType() override { return HistoryType::BrushTool; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class RasterBluredBrushUndo final : public TRasterUndo {
Shinya Kitaoka 120a6e
  std::vector<tthickpoint> m_points;</tthickpoint>
Shinya Kitaoka 120a6e
  int m_styleId;
Shinya Kitaoka 120a6e
  bool m_selective;
Shinya Kitaoka 120a6e
  int m_maxThick;
Shinya Kitaoka 120a6e
  double m_hardness;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  RasterBluredBrushUndo(TTileSetCM32 *tileSet,
Shinya Kitaoka 120a6e
                        const std::vector<tthickpoint> &points, int styleId,</tthickpoint>
Shinya Kitaoka 120a6e
                        bool selective, TXshSimpleLevel *level,
Shinya Kitaoka 120a6e
                        const TFrameId &frameId, int maxThick, double hardness,
Shinya Kitaoka 120a6e
                        bool isFrameCreated, bool isLevelCreated)
Shinya Kitaoka 120a6e
      : TRasterUndo(tileSet, level, frameId, isFrameCreated, isLevelCreated, 0)
Shinya Kitaoka 120a6e
      , m_points(points)
Shinya Kitaoka 120a6e
      , m_styleId(styleId)
Shinya Kitaoka 120a6e
      , m_selective(selective)
Shinya Kitaoka 120a6e
      , m_maxThick(maxThick)
Shinya Kitaoka 120a6e
      , m_hardness(hardness) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void redo() const override {
Shinya Kitaoka 120a6e
    if (m_points.size() == 0) return;
Shinya Kitaoka 120a6e
    insertLevelAndFrameIfNeeded();
Shinya Kitaoka 120a6e
    TToonzImageP image     = getImage();
Shinya Kitaoka 120a6e
    TRasterCM32P ras       = image->getRaster();
Shinya Kitaoka 120a6e
    TRasterCM32P backupRas = ras->clone();
Shinya Kitaoka 120a6e
    TRaster32P workRaster(ras->getSize());
Shinya Kitaoka 120a6e
    QRadialGradient brushPad = ToolUtils::getBrushPad(m_maxThick, m_hardness);
Shinya Kitaoka 120a6e
    workRaster->clear();
Shinya Kitaoka 120a6e
    BluredBrush brush(workRaster, m_maxThick, brushPad, false);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    std::vector<tthickpoint> points;</tthickpoint>
Shinya Kitaoka 120a6e
    points.push_back(m_points[0]);
Shinya Kitaoka 120a6e
    TRect bbox = brush.getBoundFromPoints(points);
Shinya Kitaoka 120a6e
    brush.addPoint(m_points[0], 1);
Shinya Kitaoka 120a6e
    brush.updateDrawing(ras, ras, bbox, m_styleId, m_selective);
Shinya Kitaoka 120a6e
    if (m_points.size() > 1) {
Shinya Kitaoka 120a6e
      points.clear();
Shinya Kitaoka 120a6e
      points.push_back(m_points[0]);
Shinya Kitaoka 120a6e
      points.push_back(m_points[1]);
Shinya Kitaoka 120a6e
      bbox = brush.getBoundFromPoints(points);
Shinya Kitaoka 120a6e
      brush.addArc(m_points[0], (m_points[1] + m_points[0]) * 0.5, m_points[1],
Shinya Kitaoka 120a6e
                   1, 1);
Shinya Kitaoka 120a6e
      brush.updateDrawing(ras, backupRas, bbox, m_styleId, m_selective);
Shinya Kitaoka 120a6e
      int i;
Shinya Kitaoka 120a6e
      for (i = 1; i + 2 < (int)m_points.size(); i = i + 2) {
Shinya Kitaoka 120a6e
        points.clear();
Shinya Kitaoka 120a6e
        points.push_back(m_points[i]);
Shinya Kitaoka 120a6e
        points.push_back(m_points[i + 1]);
Shinya Kitaoka 120a6e
        points.push_back(m_points[i + 2]);
Shinya Kitaoka 120a6e
        bbox = brush.getBoundFromPoints(points);
Shinya Kitaoka 120a6e
        brush.addArc(m_points[i], m_points[i + 1], m_points[i + 2], 1, 1);
Shinya Kitaoka 120a6e
        brush.updateDrawing(ras, backupRas, bbox, m_styleId, m_selective);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    ToolUtils::updateSaveBox();
Shinya Kitaoka 120a6e
    TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
Shinya Kitaoka 120a6e
    notifyImageChanged();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 38fd86
  int getSize() const override {
Shinya Kitaoka 38fd86
    return sizeof(*this) + TRasterUndo::getSize();
Shinya Kitaoka 38fd86
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  QString getToolName() override { return QString("Brush Tool"); }
Shinya Kitaoka 473e70
  int getHistoryType() override { return HistoryType::BrushTool; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=========================================================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double computeThickness(int pressure, const TDoublePairProperty &property,
Shinya Kitaoka 120a6e
                        bool isPath) {
Shinya Kitaoka 120a6e
  if (isPath) return 0.0;
Shinya Kitaoka 120a6e
  double p                    = pressure / 255.0;
Shinya Kitaoka 120a6e
  double t                    = p * p * p;
Shinya Kitaoka 120a6e
  double thick0               = property.getValue().first;
Shinya Kitaoka 120a6e
  double thick1               = property.getValue().second;
Shinya Kitaoka 120a6e
  if (thick1 < 0.0001) thick0 = thick1 = 0.0;
Shinya Kitaoka 120a6e
  return (thick0 + (thick1 - thick0) * t) * 0.5;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int computeThickness(int pressure, const TIntPairProperty &property,
Shinya Kitaoka 120a6e
                     bool isPath) {
Shinya Kitaoka 120a6e
  if (isPath) return 0.0;
Shinya Kitaoka 120a6e
  double p   = pressure / 255.0;
Shinya Kitaoka 120a6e
  double t   = p * p * p;
Shinya Kitaoka 120a6e
  int thick0 = property.getValue().first;
Shinya Kitaoka 120a6e
  int thick1 = property.getValue().second;
Shinya Kitaoka 120a6e
  return tround(thick0 + (thick1 - thick0) * t);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
walkerka 57fb3c
//--------------------------------------------------------------------------------------------------
walkerka 57fb3c
walkerka 22f456
static void CatmullRomInterpolate(const TThickPoint &P0, const TThickPoint &P1,
walkerka 22f456
                                  const TThickPoint &P2, const TThickPoint &P3,
walkerka 22f456
                                  int samples,
walkerka 22f456
                                  std::vector<tthickpoint> &points) {</tthickpoint>
walkerka 22f456
  double x0 = P1.x;
walkerka 22f456
  double x1 = (-P0.x + P2.x) * 0.5f;
walkerka 22f456
  double x2 = P0.x - 2.5f * P1.x + 2.0f * P2.x - 0.5f * P3.x;
walkerka 22f456
  double x3 = -0.5f * P0.x + 1.5f * P1.x - 1.5f * P2.x + 0.5f * P3.x;
walkerka 22f456
walkerka 22f456
  double y0 = P1.y;
walkerka 22f456
  double y1 = (-P0.y + P2.y) * 0.5f;
walkerka 22f456
  double y2 = P0.y - 2.5f * P1.y + 2.0f * P2.y - 0.5f * P3.y;
walkerka 22f456
  double y3 = -0.5f * P0.y + 1.5f * P1.y - 1.5f * P2.y + 0.5f * P3.y;
walkerka 22f456
walkerka 22f456
  double z0 = P1.thick;
walkerka 22f456
  double z1 = (-P0.thick + P2.thick) * 0.5f;
walkerka 22f456
  double z2 = P0.thick - 2.5f * P1.thick + 2.0f * P2.thick - 0.5f * P3.thick;
walkerka 22f456
  double z3 =
walkerka 22f456
      -0.5f * P0.thick + 1.5f * P1.thick - 1.5f * P2.thick + 0.5f * P3.thick;
walkerka 22f456
walkerka 22f456
  for (int i = 1; i <= samples; ++i) {
walkerka 22f456
    double t  = i / (double)(samples + 1);
walkerka 22f456
    double t2 = t * t;
walkerka 22f456
    double t3 = t2 * t;
walkerka 22f456
    TThickPoint p;
walkerka 22f456
    p.x     = x0 + x1 * t + x2 * t2 + x3 * t3;
walkerka 22f456
    p.y     = y0 + y1 * t + y2 * t2 + y3 * t3;
walkerka 22f456
    p.thick = z0 + z1 * t + z2 * t2 + z3 * t3;
walkerka 22f456
    points.push_back(p);
walkerka 22f456
  }
walkerka 57fb3c
}
walkerka 57fb3c
walkerka 57fb3c
//--------------------------------------------------------------------------------------------------
walkerka 57fb3c
walkerka 22f456
static void Smooth(std::vector<tthickpoint> &points, int radius) {</tthickpoint>
walkerka 22f456
  int n = (int)points.size();
walkerka 22f456
  if (radius < 1 || n < 3) {
walkerka 22f456
    return;
walkerka 22f456
  }
walkerka 57fb3c
walkerka 22f456
  std::vector<tthickpoint> result;</tthickpoint>
walkerka 57fb3c
walkerka 22f456
  float d = 1.0f / (radius * 2 + 1);
walkerka 57fb3c
walkerka 22f456
  for (int i = 1; i < n - 1; ++i) {
walkerka 22f456
    int lower = i - radius;
walkerka 22f456
    int upper = i + radius;
walkerka 57fb3c
walkerka 22f456
    TThickPoint total;
walkerka 22f456
    total.x     = 0;
walkerka 22f456
    total.y     = 0;
walkerka 22f456
    total.thick = 0;
walkerka 57fb3c
walkerka 22f456
    for (int j = lower; j <= upper; ++j) {
walkerka 22f456
      int idx = j;
walkerka 22f456
      if (idx < 0) {
walkerka 22f456
        idx = 0;
walkerka 22f456
      } else if (idx >= n) {
walkerka 22f456
        idx = n - 1;
walkerka 22f456
      }
walkerka 22f456
      total.x += points[idx].x;
walkerka 22f456
      total.y += points[idx].y;
walkerka 22f456
      total.thick += points[idx].thick;
walkerka 57fb3c
    }
walkerka 57fb3c
walkerka 22f456
    total.x *= d;
walkerka 22f456
    total.y *= d;
walkerka 22f456
    total.thick *= d;
walkerka 22f456
    result.push_back(total);
walkerka 22f456
  }
walkerka 22f456
walkerka 22f456
  for (int i = 1; i < n - 1; ++i) {
walkerka 22f456
    points[i].x     = result[i - 1].x;
walkerka 22f456
    points[i].y     = result[i - 1].y;
walkerka 22f456
    points[i].thick = result[i - 1].thick;
walkerka 22f456
  }
walkerka 22f456
walkerka 22f456
  if (points.size() >= 3) {
walkerka 22f456
    std::vector<tthickpoint> pts;</tthickpoint>
walkerka 22f456
    CatmullRomInterpolate(points[0], points[0], points[1], points[2], 10, pts);
walkerka 22f456
    std::vector<tthickpoint>::iterator it = points.begin();</tthickpoint>
walkerka 22f456
    points.insert(it, pts.begin(), pts.end());
walkerka 22f456
walkerka 22f456
    pts.clear();
walkerka 22f456
    CatmullRomInterpolate(points[n - 3], points[n - 2], points[n - 1],
walkerka 22f456
                          points[n - 1], 10, pts);
walkerka 22f456
    it = points.begin();
walkerka 22f456
    it += n - 1;
walkerka 22f456
    points.insert(it, pts.begin(), pts.end());
walkerka 22f456
  }
walkerka 57fb3c
}
walkerka 57fb3c
walkerka 57fb3c
//--------------------------------------------------------------------------------------------------
walkerka 57fb3c
walkerka 22f456
void SmoothStroke::beginStroke(int smooth) {
walkerka 22f456
  m_smooth      = smooth;
walkerka 22f456
  m_outputIndex = 0;
walkerka 22f456
  m_readIndex   = -1;
walkerka 22f456
  m_rawPoints.clear();
walkerka 22f456
  m_outputPoints.clear();
walkerka 57fb3c
}
walkerka 57fb3c
walkerka 57fb3c
//--------------------------------------------------------------------------------------------------
walkerka 57fb3c
walkerka 22f456
void SmoothStroke::addPoint(const TThickPoint &point) {
walkerka 22f456
  if (m_rawPoints.size() > 0 && m_rawPoints.back().x == point.x &&
walkerka 22f456
      m_rawPoints.back().y == point.y) {
walkerka 22f456
    return;
walkerka 22f456
  }
walkerka 22f456
  m_rawPoints.push_back(point);
walkerka 22f456
  generatePoints();
walkerka 57fb3c
}
walkerka 57fb3c
walkerka 57fb3c
//--------------------------------------------------------------------------------------------------
walkerka 57fb3c
walkerka 22f456
void SmoothStroke::endStroke() {
walkerka 22f456
  generatePoints();
walkerka 22f456
  // force enable the output all segments
walkerka 22f456
  m_outputIndex = m_outputPoints.size() - 1;
walkerka 57fb3c
}
walkerka 57fb3c
walkerka 57fb3c
//--------------------------------------------------------------------------------------------------
walkerka 57fb3c
walkerka 22f456
void SmoothStroke::getSmoothPoints(std::vector<tthickpoint> &smoothPoints) {</tthickpoint>
walkerka 22f456
  int n = m_outputPoints.size();
walkerka 22f456
  for (int i = m_readIndex + 1; i <= m_outputIndex && i < n; ++i) {
walkerka 22f456
    smoothPoints.push_back(m_outputPoints[i]);
walkerka 22f456
  }
walkerka 22f456
  m_readIndex = m_outputIndex;
walkerka 57fb3c
}
walkerka 57fb3c
walkerka 57fb3c
//--------------------------------------------------------------------------------------------------
walkerka 57fb3c
walkerka 22f456
void SmoothStroke::generatePoints() {
walkerka 22f456
  int n = (int)m_rawPoints.size();
walkerka 22f456
  if (n == 0) {
walkerka 22f456
    return;
walkerka 22f456
  }
walkerka 22f456
  std::vector<tthickpoint> smoothedPoints;</tthickpoint>
walkerka 22f456
  // Add more stroke samples before applying the smoothing
walkerka 22f456
  // This is because the raw inputs points are too few to support smooth result,
walkerka 22f456
  // especially on stroke ends
walkerka 22f456
  smoothedPoints.push_back(m_rawPoints.front());
walkerka 22f456
  for (int i = 1; i < n; ++i) {
walkerka 22f456
    const TThickPoint &p1 = m_rawPoints[i - 1];
walkerka 22f456
    const TThickPoint &p2 = m_rawPoints[i];
walkerka 22f456
    const TThickPoint &p0 = i - 2 >= 0 ? m_rawPoints[i - 2] : p1;
walkerka 22f456
    const TThickPoint &p3 = i + 1 < n ? m_rawPoints[i + 1] : p2;
walkerka 22f456
walkerka 22f456
    int samples = 8;
walkerka 22f456
    CatmullRomInterpolate(p0, p1, p2, p3, samples, smoothedPoints);
walkerka 22f456
    smoothedPoints.push_back(p2);
walkerka 22f456
  }
walkerka 22f456
  // Apply the 1D box filter
walkerka 22f456
  // Multiple passes result in better quality and fix the stroke ends break
walkerka 22f456
  // issue
walkerka 22f456
  for (int i = 0; i < 3; ++i) {
walkerka 22f456
    Smooth(smoothedPoints, m_smooth);
walkerka 22f456
  }
walkerka 22f456
  // Compare the new smoothed stroke with old one
walkerka 22f456
  // Enable the output for unchanged parts
walkerka 22f456
  int outputNum = (int)m_outputPoints.size();
walkerka 22f456
  for (int i = m_outputIndex; i < outputNum; ++i) {
walkerka 22f456
    if (m_outputPoints[i] != smoothedPoints[i]) {
walkerka 22f456
      break;
walkerka 57fb3c
    }
walkerka 22f456
    ++m_outputIndex;
walkerka 22f456
  }
walkerka 22f456
  m_outputPoints = smoothedPoints;
walkerka 57fb3c
}
walkerka 57fb3c
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
// BrushTool
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
BrushTool::BrushTool(std::string name, int targetType)
Shinya Kitaoka 120a6e
    : TTool(name)
Shinya Kitaoka 120a6e
    , m_thickness("Size", 0, 100, 0, 5)
Shinya Kitaoka 120a6e
    , m_rasThickness("Size", 1, 100, 1, 5)
Shinya Kitaoka 120a6e
    , m_accuracy("Accuracy:", 1, 100, 20)
walkerka bb91cd
    , m_smooth("Smooth:", 0, 50, 0)
Shinya Kitaoka 120a6e
    , m_hardness("Hardness:", 0, 100, 100)
Shinya Kitaoka 120a6e
    , m_preset("Preset:")
Shinya Kitaoka 120a6e
    , m_selective("Selective", false)
Shinya Kitaoka 120a6e
    , m_breakAngles("Break", true)
Shinya Kitaoka 120a6e
    , m_pencil("Pencil", false)
Shinya Kitaoka 120a6e
    , m_pressure("Pressure", true)
Shinya Kitaoka 120a6e
    , m_capStyle("Cap")
Shinya Kitaoka 120a6e
    , m_joinStyle("Join")
Shinya Kitaoka 120a6e
    , m_miterJoinLimit("Miter:", 0, 100, 4)
Shinya Kitaoka 120a6e
    , m_rasterTrack(0)
Shinya Kitaoka 120a6e
    , m_styleId(0)
Shinya Kitaoka 120a6e
    , m_modifiedRegion()
Shinya Kitaoka 120a6e
    , m_bluredBrush(0)
Shinya Kitaoka 120a6e
    , m_active(false)
Shinya Kitaoka 120a6e
    , m_enabled(false)
Shinya Kitaoka 120a6e
    , m_isPrompting(false)
Shinya Kitaoka 120a6e
    , m_firstTime(true)
Shinya Kitaoka 120a6e
    , m_presetsLoaded(false)
Shinya Kitaoka 120a6e
    , m_workingFrameId(TFrameId()) {
Shinya Kitaoka 120a6e
  bind(targetType);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (targetType & TTool::Vectors) {
Shinya Kitaoka 120a6e
    m_prop[0].bind(m_thickness);
Shinya Kitaoka 120a6e
    m_prop[0].bind(m_accuracy);
walkerka bb91cd
    m_prop[0].bind(m_smooth);
Shinya Kitaoka 120a6e
    m_prop[0].bind(m_breakAngles);
Shinya Kitaoka 120a6e
    m_breakAngles.setId("BreakSharpAngles");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (targetType & TTool::ToonzImage) {
Shinya Kitaoka 120a6e
    m_prop[0].bind(m_rasThickness);
Shinya Kitaoka 120a6e
    m_prop[0].bind(m_hardness);
walkerka bb91cd
    m_prop[0].bind(m_smooth);
Shinya Kitaoka 120a6e
    m_prop[0].bind(m_selective);
Shinya Kitaoka 120a6e
    m_prop[0].bind(m_pencil);
Shinya Kitaoka 120a6e
    m_pencil.setId("PencilMode");
Shinya Kitaoka 120a6e
    m_selective.setId("Selective");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_prop[0].bind(m_pressure);
Shinya Kitaoka 120a6e
  m_prop[0].bind(m_preset);
Shinya Kitaoka 120a6e
  m_preset.setId("BrushPreset");
Shinya Kitaoka 120a6e
  m_preset.addValue(CUSTOM_WSTR);
Campbell Barton f49389
  m_pressure.setId("PressureSensitivity");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_capStyle.addValue(BUTT_WSTR);
Shinya Kitaoka 120a6e
  m_capStyle.addValue(ROUNDC_WSTR);
Shinya Kitaoka 120a6e
  m_capStyle.addValue(PROJECTING_WSTR);
Shinya Kitaoka 120a6e
  m_capStyle.setId("Cap");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_joinStyle.addValue(MITER_WSTR);
Shinya Kitaoka 120a6e
  m_joinStyle.addValue(ROUNDJ_WSTR);
Shinya Kitaoka 120a6e
  m_joinStyle.addValue(BEVEL_WSTR);
Shinya Kitaoka 120a6e
  m_joinStyle.setId("Join");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_miterJoinLimit.setId("Miter");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (targetType & TTool::Vectors) {
Shinya Kitaoka 120a6e
    m_prop[1].bind(m_capStyle);
Shinya Kitaoka 120a6e
    m_prop[1].bind(m_joinStyle);
Shinya Kitaoka 120a6e
    m_prop[1].bind(m_miterJoinLimit);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ToolOptionsBox *BrushTool::createOptionsBox() {
Shinya Kitaoka 120a6e
  TPaletteHandle *currPalette =
Shinya Kitaoka 120a6e
      TTool::getApplication()->getPaletteController()->getCurrentLevelPalette();
Shinya Kitaoka 120a6e
  ToolHandle *currTool = TTool::getApplication()->getCurrentTool();
Shinya Kitaoka 120a6e
  return new BrushToolOptionsBox(0, this, currPalette, currTool);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::drawLine(const TPointD &point, const TPointD ¢re,
Shinya Kitaoka 120a6e
                         bool horizontal, bool isDecimal) {
Shinya Kitaoka 120a6e
  if (!isDecimal) {
Shinya Kitaoka 120a6e
    if (horizontal) {
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.x - 1.5, point.y + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.x - 0.5, point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.y - 0.5, -point.x + 1.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.y - 0.5, -point.x + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.x + 0.5, -point.y + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.x - 0.5, -point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.y - 0.5, point.x - 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.y - 0.5, point.x + 0.5) + centre);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.y - 0.5, point.x + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.y - 0.5, point.x - 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.x - 0.5, -point.y + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.x - 1.5, -point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.y - 0.5, -point.x + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.y - 0.5, -point.x + 1.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.x - 0.5, point.y + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.x + 0.5, point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.x - 1.5, point.y + 1.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.x - 1.5, point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.x - 1.5, point.y + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.x - 0.5, point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.y + 0.5, -point.x + 1.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.y - 0.5, -point.x + 1.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.y - 0.5, -point.x + 1.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.y - 0.5, -point.x + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.x + 0.5, -point.y - 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.x + 0.5, -point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.x + 0.5, -point.y + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.x - 0.5, -point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.y - 1.5, point.x - 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.y - 0.5, point.x - 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.y - 0.5, point.x - 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.y - 0.5, point.x + 0.5) + centre);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.y + 0.5, point.x - 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.y - 0.5, point.x - 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.y - 0.5, point.x - 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.y - 0.5, point.x + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.x - 1.5, -point.y - 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.x - 1.5, -point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.x - 1.5, -point.y + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.x - 0.5, -point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.y - 1.5, -point.x + 1.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.y - 0.5, -point.x + 1.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.y - 0.5, -point.x + 1.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.y - 0.5, -point.x + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.x + 0.5, point.y + 1.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.x + 0.5, point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.x + 0.5, point.y + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.x - 0.5, point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    if (horizontal) {
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.x - 0.5, point.y + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.x + 0.5, point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.y + 0.5, point.x - 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.y + 0.5, point.x + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.y + 0.5, -point.x + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.y + 0.5, -point.x - 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.x + 0.5, -point.y - 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.x - 0.5, -point.y - 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.x - 0.5, -point.y - 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.x + 0.5, -point.y - 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.y - 0.5, -point.x + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.y - 0.5, -point.x - 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.y - 0.5, point.x - 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.y - 0.5, point.x + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.x + 0.5, point.y + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.x - 0.5, point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.x - 0.5, point.y + 1.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.x - 0.5, point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.x - 0.5, point.y + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.x + 0.5, point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.y + 1.5, point.x - 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.y + 0.5, point.x - 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.y + 0.5, point.x - 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.y + 0.5, point.x + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.y + 1.5, -point.x + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.y + 0.5, -point.x + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.y + 0.5, -point.x + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.y + 0.5, -point.x - 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.x - 0.5, -point.y - 1.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.x - 0.5, -point.y - 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(point.x - 0.5, -point.y - 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(point.x + 0.5, -point.y - 0.5) + centre);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.x + 0.5, -point.y - 1.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.x + 0.5, -point.y - 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.x + 0.5, -point.y - 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.x - 0.5, -point.y - 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.y - 1.5, -point.x + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.y - 0.5, -point.x + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.y - 0.5, -point.x + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.y - 0.5, -point.x - 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.y - 1.5, point.x - 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.y - 0.5, point.x - 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.y - 0.5, point.x - 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.y - 0.5, point.x + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.x + 0.5, point.y + 1.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.x + 0.5, point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
      tglDrawSegment(TPointD(-point.x + 0.5, point.y + 0.5) + centre,
Shinya Kitaoka 120a6e
                     TPointD(-point.x - 0.5, point.y + 0.5) + centre);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::drawEmptyCircle(TPointD pos, int thick, bool isLxEven,
Shinya Kitaoka 120a6e
                                bool isLyEven, bool isPencil) {
Shinya Kitaoka 120a6e
  if (isLxEven) pos.x += 0.5;
Shinya Kitaoka 120a6e
  if (isLyEven) pos.y += 0.5;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!isPencil)
Shinya Kitaoka 120a6e
    tglDrawCircle(pos, (thick + 1) * 0.5);
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    int x = 0, y = tround((thick * 0.5) - 0.5);
Shinya Kitaoka 120a6e
    int d           = 3 - 2 * (int)(thick * 0.5);
Shinya Kitaoka 120a6e
    bool horizontal = true, isDecimal = thick % 2 != 0;
Shinya Kitaoka 120a6e
    drawLine(TPointD(x, y), pos, horizontal, isDecimal);
Shinya Kitaoka 120a6e
    while (y > x) {
Shinya Kitaoka 120a6e
      if (d < 0) {
Shinya Kitaoka 120a6e
        d          = d + 4 * x + 6;
Shinya Kitaoka 120a6e
        horizontal = true;
Shinya Kitaoka 120a6e
      } else {
Shinya Kitaoka 120a6e
        d          = d + 4 * (x - y) + 10;
Shinya Kitaoka 120a6e
        horizontal = false;
Shinya Kitaoka 120a6e
        y--;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      x++;
Shinya Kitaoka 120a6e
      drawLine(TPointD(x, y), pos, horizontal, isDecimal);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::updateTranslation() {
Shinya Kitaoka 120a6e
  m_thickness.setQStringName(tr("Size"));
Shinya Kitaoka 120a6e
  m_rasThickness.setQStringName(tr("Size"));
Shinya Kitaoka 120a6e
  m_hardness.setQStringName(tr("Hardness:"));
Shinya Kitaoka 120a6e
  m_accuracy.setQStringName(tr("Accuracy:"));
walkerka bb91cd
  m_smooth.setQStringName(tr("Smooth:"));
Shinya Kitaoka 120a6e
  m_selective.setQStringName(tr("Selective"));
Shinya Kitaoka 120a6e
  // m_filled.setQStringName(tr("Filled"));
Shinya Kitaoka 120a6e
  m_preset.setQStringName(tr("Preset:"));
Shinya Kitaoka 120a6e
  m_breakAngles.setQStringName(tr("Break"));
Shinya Kitaoka 120a6e
  m_pencil.setQStringName(tr("Pencil"));
Shinya Kitaoka 120a6e
  m_pressure.setQStringName(tr("Pressure"));
Shinya Kitaoka 120a6e
  m_capStyle.setQStringName(tr("Cap"));
Shinya Kitaoka 120a6e
  m_joinStyle.setQStringName(tr("Join"));
Shinya Kitaoka 120a6e
  m_miterJoinLimit.setQStringName(tr("Miter:"));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::updateWorkAndBackupRasters(const TRect &rect) {
Shinya Kitaoka 120a6e
  TToonzImageP ti = TImageP(getImage(false, 1));
Shinya Kitaoka 120a6e
  if (!ti) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRasterCM32P ras = ti->getRaster();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRect _rect     = rect * ras->getBounds();
Shinya Kitaoka 120a6e
  TRect _lastRect = m_lastRect * ras->getBounds();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (_rect.isEmpty()) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (m_lastRect.isEmpty()) {
Shinya Kitaoka 120a6e
    m_workRas->extract(_rect)->clear();
Shinya Kitaoka 120a6e
    m_backupRas->extract(_rect)->copy(ras->extract(_rect));
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  QList<trect> rects = ToolUtils::splitRect(_rect, _lastRect);</trect>
Shinya Kitaoka 120a6e
  for (int i = 0; i < rects.size(); i++) {
Shinya Kitaoka 120a6e
    m_workRas->extract(rects[i])->clear();
Shinya Kitaoka 120a6e
    m_backupRas->extract(rects[i])->copy(ras->extract(rects[i]));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::onActivate() {
Shinya Kitaoka 120a6e
  if (m_firstTime) {
Shinya Kitaoka 120a6e
    m_thickness.setValue(
Shinya Kitaoka 120a6e
        TDoublePairProperty::Value(VectorBrushMinSize, VectorBrushMaxSize));
Shinya Kitaoka 120a6e
    m_rasThickness.setValue(
Shinya Kitaoka 120a6e
        TDoublePairProperty::Value(RasterBrushMinSize, RasterBrushMaxSize));
Shinya Kitaoka 120a6e
    m_capStyle.setIndex(VectorCapStyle);
Shinya Kitaoka 120a6e
    m_joinStyle.setIndex(VectorJoinStyle);
Shinya Kitaoka 120a6e
    m_miterJoinLimit.setValue(VectorMiterValue);
Shinya Kitaoka 120a6e
    m_selective.setValue(BrushSelective ? 1 : 0);
Shinya Kitaoka 120a6e
    m_breakAngles.setValue(BrushBreakSharpAngles ? 1 : 0);
Shinya Kitaoka 120a6e
    m_pencil.setValue(RasterBrushPencilMode ? 1 : 0);
Campbell Barton f49389
    m_pressure.setValue(BrushPressureSensitivity ? 1 : 0);
Shinya Kitaoka 120a6e
    m_firstTime = false;
Shinya Kitaoka 120a6e
    m_accuracy.setValue(BrushAccuracy);
walkerka bb91cd
    m_smooth.setValue(BrushSmooth);
Shinya Kitaoka 120a6e
    m_hardness.setValue(RasterBrushHardness);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (m_targetType & TTool::ToonzImage) {
Shinya Kitaoka 120a6e
    m_brushPad = ToolUtils::getBrushPad(m_rasThickness.getValue().second,
Shinya Kitaoka 120a6e
                                        m_hardness.getValue() * 0.01);
Shinya Kitaoka 120a6e
    setWorkAndBackupImages();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  // TODO:app->editImageOrSpline();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::onDeactivate() {
Shinya Kitaoka 120a6e
  /*---
Shinya Kitaoka 120a6e
   * ドラッグ中にツールが切り替わった場合に備え、onDeactivateにもMouseReleaseと同じ処理を行う
Shinya Kitaoka 120a6e
   * ---*/
Shinya Kitaoka 120a6e
  if (m_tileSaver && !m_isPath) {
Shinya Kitaoka 120a6e
    bool isValid = m_enabled && m_active;
Shinya Kitaoka 120a6e
    m_enabled    = false;
Shinya Kitaoka 120a6e
    if (isValid) {
Shinya Kitaoka 120a6e
      TImageP image = getImage(true);
Shinya Kitaoka 120a6e
      if (TToonzImageP ti = image)
Shinya Kitaoka 120a6e
        finishRasterBrush(m_mousePos,
Shinya Kitaoka 120a6e
                          1); /*-- 最後のストロークの筆圧は1とする --*/
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  m_workRas   = TRaster32P();
Shinya Kitaoka 120a6e
  m_backupRas = TRasterCM32P();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool BrushTool::preLeftButtonDown() {
Shinya Kitaoka 120a6e
  touchImage();
Shinya Kitaoka 120a6e
  if (m_isFrameCreated) setWorkAndBackupImages();
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::leftButtonDown(const TPointD &pos, const TMouseEvent &e) {
Shinya Kitaoka 120a6e
  TTool::Application *app = TTool::getApplication();
Shinya Kitaoka 120a6e
  if (!app) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int col   = app->getCurrentColumn()->getColumnIndex();
Shinya Kitaoka 120a6e
  m_isPath  = app->getCurrentObject()->isSpline();
Shinya Kitaoka 120a6e
  m_enabled = col >= 0 || m_isPath;
Shinya Kitaoka 120a6e
  // todo: gestire autoenable
Shinya Kitaoka 120a6e
  if (!m_enabled) return;
Shinya Kitaoka 120a6e
  if (!m_isPath) {
Shinya Kitaoka 120a6e
    m_currentColor = TPixel32::Black;
Shinya Kitaoka 120a6e
    m_active       = !!getImage(true);
Shinya Kitaoka 120a6e
    if (!m_active) {
Shinya Kitaoka 120a6e
      m_active = !!touchImage();
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (!m_active) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (m_active) {
Shinya Kitaoka 120a6e
      // nel caso che il colore corrente sia un cleanup/studiopalette color
Shinya Kitaoka 120a6e
      // oppure il colore di un colorfield
Shinya Kitaoka 120a6e
      m_styleId       = app->getCurrentLevelStyleIndex();
Shinya Kitaoka 120a6e
      TColorStyle *cs = app->getCurrentLevelStyle();
Shinya Kitaoka 120a6e
      if (cs) {
Shinya Kitaoka 120a6e
        TRasterStyleFx *rfx = cs ? cs->getRasterStyleFx() : 0;
Shinya Kitaoka 120a6e
        m_active =
Shinya Kitaoka 120a6e
            cs != 0 && (cs->isStrokeStyle() || (rfx && rfx->isInkStyle()));
Shinya Kitaoka 120a6e
        m_currentColor   = cs->getAverageColor();
Shinya Kitaoka 120a6e
        m_currentColor.m = 255;
Shinya Kitaoka 120a6e
      } else {
Shinya Kitaoka 120a6e
        m_styleId      = 1;
Shinya Kitaoka 120a6e
        m_currentColor = TPixel32::Black;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    m_currentColor = TPixel32::Red;
Shinya Kitaoka 120a6e
    m_active       = true;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  // assert(0<=m_styleId && m_styleId<2);
Shinya Kitaoka 120a6e
  TImageP img = getImage(true);
Shinya Kitaoka 120a6e
  TToonzImageP ri(img);
Shinya Kitaoka 120a6e
  if (ri) {
Shinya Kitaoka 120a6e
    TRasterCM32P ras = ri->getRaster();
Shinya Kitaoka 120a6e
    if (ras) {
Shinya Kitaoka 120a6e
      TPointD rasCenter = ras->getCenterD();
Shinya Kitaoka 120a6e
      m_tileSet         = new TTileSetCM32(ras->getSize());
Shinya Kitaoka 120a6e
      m_tileSaver       = new TTileSaverCM32(ras, m_tileSet);
Shinya Kitaoka 120a6e
      double maxThick   = m_rasThickness.getValue().second;
Shinya Kitaoka 120a6e
      double thickness =
Shinya Kitaoka 120a6e
          (m_pressure.getValue() || m_isPath)
Shinya Kitaoka 120a6e
              ? computeThickness(e.m_pressure, m_rasThickness, m_isPath) * 2
Shinya Kitaoka 120a6e
              : maxThick;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      /*--- ストロークの最初にMaxサイズの円が描かれてしまう不具合を防止する
Shinya Kitaoka 120a6e
       * ---*/
Shinya Kitaoka 120a6e
      if (m_pressure.getValue() && e.m_pressure == 255)
Shinya Kitaoka 120a6e
        thickness = m_rasThickness.getValue().first;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      TPointD halfThick(maxThick * 0.5, maxThick * 0.5);
Shinya Kitaoka 120a6e
      TRectD invalidateRect(pos - halfThick, pos + halfThick);
Shinya Kitaoka 120a6e
      if (m_hardness.getValue() == 100 || m_pencil.getValue()) {
Shinya Kitaoka 120a6e
        /*-- Pencilモードでなく、Hardness=100 の場合のブラシサイズを1段階下げる
Shinya Kitaoka 120a6e
         * --*/
Shinya Kitaoka 120a6e
        if (!m_pencil.getValue()) thickness -= 1.0;
walkerka 22f456
walkerka 22f456
        TThickPoint thickPoint(pos + convert(ras->getCenter()), thickness);
walkerka 22f456
        m_rasterTrack = new RasterStrokeGenerator(
walkerka 22f456
            ras, BRUSH, NONE, m_styleId, thickPoint, m_selective.getValue(), 0,
walkerka 22f456
            !m_pencil.getValue());
walkerka 22f456
        m_tileSaver->save(m_rasterTrack->getLastRect());
walkerka 22f456
        m_rasterTrack->generateLastPieceOfStroke(m_pencil.getValue());
walkerka 22f456
walkerka 22f456
        m_smoothStroke.beginStroke(m_smooth.getValue());
walkerka 22f456
        m_smoothStroke.addPoint(thickPoint);
walkerka 22f456
        std::vector<tthickpoint> pts;</tthickpoint>
walkerka 22f456
        m_smoothStroke.getSmoothPoints(
walkerka 22f456
            pts);  // skip first point because it has been outputted
walkerka 22f456
      } else {
walkerka 22f456
        m_points.clear();
walkerka 22f456
        TThickPoint point(pos + rasCenter, thickness);
walkerka 22f456
        m_points.push_back(point);
walkerka 22f456
        m_bluredBrush = new BluredBrush(m_workRas, maxThick, m_brushPad, false);
walkerka 22f456
walkerka 22f456
        m_strokeRect = m_bluredBrush->getBoundFromPoints(m_points);
walkerka 22f456
        updateWorkAndBackupRasters(m_strokeRect);
walkerka 22f456
        m_tileSaver->save(m_strokeRect);
walkerka 22f456
        m_bluredBrush->addPoint(point, 1);
walkerka 22f456
        m_bluredBrush->updateDrawing(ri->getRaster(), m_backupRas, m_strokeRect,
walkerka 22f456
                                     m_styleId, m_selective.getValue());
walkerka 22f456
        m_lastRect = m_strokeRect;
walkerka 22f456
walkerka 22f456
        m_smoothStroke.beginStroke(m_smooth.getValue());
walkerka 22f456
        m_smoothStroke.addPoint(point);
walkerka 22f456
        std::vector<tthickpoint> pts;</tthickpoint>
walkerka 22f456
        m_smoothStroke.getSmoothPoints(
walkerka 22f456
            pts);  // skip first point because it has been outputted
walkerka 22f456
      }
walkerka 22f456
      /*-- 作業中のFidを登録 --*/
walkerka 22f456
      m_workingFrameId = getFrameId();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      invalidate(invalidateRect);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    m_track.clear();
Shinya Kitaoka 120a6e
    double thickness =
Shinya Kitaoka 120a6e
        (m_pressure.getValue() || m_isPath)
Shinya Kitaoka 120a6e
            ? computeThickness(e.m_pressure, m_thickness, m_isPath)
Shinya Kitaoka 120a6e
            : m_thickness.getValue().second * 0.5;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    /*--- ストロークの最初にMaxサイズの円が描かれてしまう不具合を防止する ---*/
Shinya Kitaoka 120a6e
    if (m_pressure.getValue() && e.m_pressure == 255)
Shinya Kitaoka 120a6e
      thickness = m_rasThickness.getValue().first;
Shinya Kitaoka 120a6e
walkerka bb91cd
    m_smoothStroke.beginStroke(m_smooth.getValue());
walkerka bb91cd
    addTrackPoint(TThickPoint(pos, thickness), getPixelSize() * getPixelSize());
walkerka 22f456
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::leftButtonDrag(const TPointD &pos, const TMouseEvent &e) {
Shinya Kitaoka 120a6e
  m_brushPos = m_mousePos = pos;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!m_enabled || !m_active) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool isAdded;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (TToonzImageP ti = TImageP(getImage(true))) {
Shinya Kitaoka 120a6e
    TPointD rasCenter = ti->getRaster()->getCenterD();
Shinya Kitaoka 120a6e
    int maxThickness  = m_rasThickness.getValue().second;
Shinya Kitaoka 120a6e
    double thickness =
Shinya Kitaoka 120a6e
        (m_pressure.getValue() || m_isPath)
Shinya Kitaoka 120a6e
            ? computeThickness(e.m_pressure, m_rasThickness, m_isPath) * 2
Shinya Kitaoka 120a6e
            : maxThickness;
Shinya Kitaoka 120a6e
    TRectD invalidateRect;
Shinya Kitaoka 120a6e
    if (m_rasterTrack &&
Shinya Kitaoka 120a6e
        (m_hardness.getValue() == 100 || m_pencil.getValue())) {
Shinya Kitaoka 120a6e
      /*-- Pencilモードでなく、Hardness=100 の場合のブラシサイズを1段階下げる
Shinya Kitaoka 120a6e
       * --*/
Shinya Kitaoka 120a6e
      if (!m_pencil.getValue()) thickness -= 1.0;
walkerka 22f456
walkerka 22f456
      TThickPoint thickPoint(pos + rasCenter, thickness);
walkerka 22f456
      m_smoothStroke.addPoint(thickPoint);
walkerka 22f456
      std::vector<tthickpoint> pts;</tthickpoint>
walkerka 22f456
      m_smoothStroke.getSmoothPoints(pts);
walkerka 22f456
      for (size_t i = 0; i < pts.size(); ++i) {
walkerka 22f456
        const TThickPoint &thickPoint = pts[i];
walkerka 22f456
        isAdded                       = m_rasterTrack->add(thickPoint);
walkerka 22f456
        if (isAdded) {
walkerka 22f456
          m_tileSaver->save(m_rasterTrack->getLastRect());
walkerka 22f456
          m_rasterTrack->generateLastPieceOfStroke(m_pencil.getValue());
walkerka 22f456
          std::vector<tthickpoint> brushPoints =</tthickpoint>
walkerka 22f456
              m_rasterTrack->getPointsSequence();
walkerka 22f456
          int m = (int)brushPoints.size();
walkerka 22f456
          std::vector<tthickpoint> points;</tthickpoint>
walkerka 22f456
          if (m == 3) {
walkerka 22f456
            points.push_back(brushPoints[0]);
walkerka 22f456
            points.push_back(brushPoints[1]);
walkerka 22f456
          } else {
walkerka 22f456
            points.push_back(brushPoints[m - 4]);
walkerka 22f456
            points.push_back(brushPoints[m - 3]);
walkerka 22f456
            points.push_back(brushPoints[m - 2]);
walkerka 22f456
          }
walkerka 22f456
          if (i == 0) {
walkerka 22f456
            invalidateRect =
walkerka 22f456
                ToolUtils::getBounds(points, maxThickness) - rasCenter;
walkerka 22f456
          } else {
walkerka 22f456
            invalidateRect +=
walkerka 22f456
                ToolUtils::getBounds(points, maxThickness) - rasCenter;
walkerka 22f456
          }
Shinya Kitaoka 120a6e
        }
walkerka 22f456
      }
Shinya Kitaoka 120a6e
    } else {
walkerka 22f456
      // antialiased brush
walkerka 22f456
      assert(m_workRas.getPointer() && m_backupRas.getPointer());
walkerka 22f456
      TThickPoint thickPoint(pos + rasCenter, thickness);
walkerka 22f456
      m_smoothStroke.addPoint(thickPoint);
walkerka 22f456
      std::vector<tthickpoint> pts;</tthickpoint>
walkerka 22f456
      m_smoothStroke.getSmoothPoints(pts);
walkerka 22f456
      bool rectUpdated = false;
walkerka 22f456
      for (size_t i = 0; i < pts.size(); ++i) {
walkerka 22f456
        TThickPoint old = m_points.back();
walkerka 22f456
        if (norm2(pos - old) < 4) continue;
walkerka 22f456
walkerka 22f456
        const TThickPoint &point = pts[i];
walkerka 22f456
        TThickPoint mid((old + point) * 0.5, (point.thick + old.thick) * 0.5);
walkerka 22f456
        m_points.push_back(mid);
walkerka 22f456
        m_points.push_back(point);
walkerka 22f456
walkerka 22f456
        TRect bbox;
walkerka 22f456
        int m = (int)m_points.size();
walkerka 22f456
        std::vector<tthickpoint> points;</tthickpoint>
walkerka 22f456
        if (m == 3) {
walkerka 22f456
          // ho appena cominciato. devo disegnare un segmento
walkerka 22f456
          TThickPoint pa = m_points.front();
walkerka 22f456
          points.push_back(pa);
walkerka 22f456
          points.push_back(mid);
walkerka 22f456
          bbox = m_bluredBrush->getBoundFromPoints(points);
walkerka 22f456
          updateWorkAndBackupRasters(bbox + m_lastRect);
walkerka 22f456
          m_tileSaver->save(bbox);
walkerka 22f456
          m_bluredBrush->addArc(pa, (mid + pa) * 0.5, mid, 1, 1);
walkerka 22f456
          m_lastRect += bbox;
walkerka 22f456
        } else {
walkerka 22f456
          points.push_back(m_points[m - 4]);
walkerka 22f456
          points.push_back(old);
walkerka 22f456
          points.push_back(mid);
walkerka 22f456
          bbox = m_bluredBrush->getBoundFromPoints(points);
walkerka 22f456
          updateWorkAndBackupRasters(bbox + m_lastRect);
walkerka 22f456
          m_tileSaver->save(bbox);
walkerka 22f456
          m_bluredBrush->addArc(m_points[m - 4], old, mid, 1, 1);
walkerka 22f456
          m_lastRect += bbox;
walkerka 22f456
        }
walkerka 22f456
        if (!rectUpdated) {
walkerka 22f456
          invalidateRect =
walkerka 22f456
              ToolUtils::getBounds(points, maxThickness) - rasCenter;
walkerka 22f456
          rectUpdated = true;
walkerka 22f456
        } else {
walkerka 22f456
          invalidateRect +=
walkerka 22f456
              ToolUtils::getBounds(points, maxThickness) - rasCenter;
walkerka 22f456
        }
walkerka 22f456
walkerka 22f456
        m_bluredBrush->updateDrawing(ti->getRaster(), m_backupRas, bbox,
walkerka 22f456
                                     m_styleId, m_selective.getValue());
walkerka 22f456
        m_strokeRect += bbox;
walkerka 22f456
      }
walkerka 22f456
    }
walkerka 22f456
    invalidate(invalidateRect.enlarge(2));
walkerka 22f456
  } else {
walkerka 22f456
    double thickness =
walkerka 22f456
        (m_pressure.getValue() || m_isPath)
walkerka 22f456
            ? computeThickness(e.m_pressure, m_thickness, m_isPath)
walkerka 22f456
            : m_thickness.getValue().second * 0.5;
walkerka 22f456
    addTrackPoint(TThickPoint(pos, thickness), getPixelSize() * getPixelSize());
Shinya Kitaoka 120a6e
    invalidate();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::leftButtonUp(const TPointD &pos, const TMouseEvent &e) {
Shinya Kitaoka 120a6e
  bool isValid = m_enabled && m_active;
Shinya Kitaoka 120a6e
  m_enabled    = false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!isValid) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (m_isPath) {
Shinya Kitaoka 120a6e
    double error = 20.0 * getPixelSize();
walkerka 22f456
    flushTrackPoint();
Shinya Kitaoka 120a6e
    TStroke *stroke = m_track.makeStroke(error);
Shinya Kitaoka 120a6e
    int points      = stroke->getControlPointCount();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    if (TVectorImageP vi = getImage(true)) {
Shinya Kitaoka 120a6e
      struct Cleanup {
Shinya Kitaoka 120a6e
        BrushTool *m_this;
Shinya Kitaoka 120a6e
        ~Cleanup() { m_this->m_track.clear(), m_this->invalidate(); }
Shinya Kitaoka 120a6e
      } cleanup = {this};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      if (!isJustCreatedSpline(vi.getPointer())) {
Shinya Kitaoka 120a6e
        m_isPrompting = true;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
        QString question("Are you sure you want to replace the motion path?");
Shinya Kitaoka 120a6e
        int ret =
Shinya Kitaoka 120a6e
            DVGui::MsgBox(question, QObject::tr("Yes"), QObject::tr("No"), 0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
        m_isPrompting = false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
        if (ret == 2 || ret == 0) return;
Shinya Kitaoka 120a6e
      }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      QMutexLocker lock(vi->getMutex());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      TUndo *undo =
Shinya Kitaoka 120a6e
          new UndoPath(getXsheet()->getStageObject(getObjectId())->getSpline());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      while (vi->getStrokeCount() > 0) vi->deleteStroke(0);
Shinya Kitaoka 120a6e
      vi->addStroke(stroke, false);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      notifyImageChanged();
Shinya Kitaoka 120a6e
      TUndoManager::manager()->add(undo);
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TImageP image = getImage(true);
Shinya Kitaoka 120a6e
  if (TVectorImageP vi = image) {
Shinya Kitaoka 120a6e
    if (m_track.isEmpty()) {
Shinya Kitaoka 120a6e
      m_styleId = 0;
Shinya Kitaoka 120a6e
      m_track.clear();
Shinya Kitaoka 120a6e
      return;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    m_track.filterPoints();
Shinya Kitaoka 120a6e
    double error = 30.0 / (1 + 0.5 * m_accuracy.getValue());
Shinya Kitaoka 120a6e
    error *= getPixelSize();
Toshihiro Shimizu 890ddd
walkerka bb91cd
    flushTrackPoint();
Shinya Kitaoka 120a6e
    TStroke *stroke = m_track.makeStroke(error);
Shinya Kitaoka 120a6e
    stroke->setStyle(m_styleId);
Shinya Kitaoka 120a6e
    {
Shinya Kitaoka 120a6e
      TStroke::OutlineOptions &options = stroke->outlineOptions();
Shinya Kitaoka 120a6e
      options.m_capStyle               = m_capStyle.getIndex();
Shinya Kitaoka 120a6e
      options.m_joinStyle              = m_joinStyle.getIndex();
Shinya Kitaoka 120a6e
      options.m_miterUpper             = m_miterJoinLimit.getValue();
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    m_styleId = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    QMutexLocker lock(vi->getMutex());
Shinya Kitaoka 120a6e
    if (stroke->getControlPointCount() == 3 &&
Shinya Kitaoka 120a6e
        stroke->getControlPoint(0) !=
Shinya Kitaoka 120a6e
            stroke->getControlPoint(2))  // gli stroke con solo 1 chunk vengono
Shinya Kitaoka 120a6e
                                         // fatti dal tape tool...e devono venir
Shinya Kitaoka 120a6e
                                         // riconosciuti come speciali di
Shinya Kitaoka 120a6e
                                         // autoclose proprio dal fatto che
Shinya Kitaoka 120a6e
                                         // hanno 1 solo chunk.
Shinya Kitaoka 120a6e
      stroke->insertControlPoints(0.5);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    addStrokeToImage(getApplication(), vi, stroke, m_breakAngles.getValue(),
Shinya Kitaoka 120a6e
                     m_isFrameCreated, m_isLevelCreated);
Shinya Kitaoka 120a6e
    TRectD bbox = stroke->getBBox().enlarge(2) + m_track.getModifiedRegion();
Shinya Kitaoka 120a6e
    invalidate();
Shinya Kitaoka 120a6e
    assert(stroke);
Shinya Kitaoka 120a6e
    m_track.clear();
Shinya Kitaoka 120a6e
  } else if (TToonzImageP ti = image) {
Shinya Kitaoka 120a6e
    finishRasterBrush(pos, e.m_pressure);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
walkerka 57fb3c
//--------------------------------------------------------------------------------------------------
walkerka 57fb3c
walkerka 22f456
void BrushTool::addTrackPoint(const TThickPoint &point, double pixelSize2) {
walkerka 22f456
  m_smoothStroke.addPoint(point);
walkerka 22f456
  std::vector<tthickpoint> pts;</tthickpoint>
walkerka 22f456
  m_smoothStroke.getSmoothPoints(pts);
walkerka 22f456
  for (size_t i = 0; i < pts.size(); ++i) {
walkerka 22f456
    m_track.add(pts[i], pixelSize2);
walkerka 22f456
  }
walkerka 57fb3c
}
walkerka 57fb3c
walkerka 57fb3c
//--------------------------------------------------------------------------------------------------
walkerka 57fb3c
walkerka 22f456
void BrushTool::flushTrackPoint() {
walkerka 22f456
  m_smoothStroke.endStroke();
walkerka 22f456
  std::vector<tthickpoint> pts;</tthickpoint>
walkerka 22f456
  m_smoothStroke.getSmoothPoints(pts);
walkerka 22f456
  double pixelSize2 = getPixelSize() * getPixelSize();
walkerka 22f456
  for (size_t i = 0; i < pts.size(); ++i) {
walkerka 22f456
    m_track.add(pts[i], pixelSize2);
walkerka 22f456
  }
walkerka 57fb3c
}
walkerka 57fb3c
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------------------
Shinya Kitaoka 120a6e
/*!
Shinya Kitaoka 120a6e
 * ドラッグ中にツールが切り替わった場合に備え、onDeactivate時とMouseRelease時にと同じ終了処理を行う
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
void BrushTool::finishRasterBrush(const TPointD &pos, int pressureVal) {
Shinya Kitaoka 120a6e
  TImageP image   = getImage(true);
Shinya Kitaoka 120a6e
  TToonzImageP ti = image;
Shinya Kitaoka 120a6e
  if (!ti) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TPointD rasCenter         = ti->getRaster()->getCenterD();
Shinya Kitaoka 120a6e
  TTool::Application *app   = TTool::getApplication();
Shinya Kitaoka 120a6e
  TXshLevel *level          = app->getCurrentLevel()->getLevel();
Shinya Kitaoka 120a6e
  TXshSimpleLevelP simLevel = level->getSimpleLevel();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*--
Shinya Kitaoka 120a6e
   * 描画中にカレントフレームが変わっても、描画開始時のFidに対してUndoを記録する
Shinya Kitaoka 120a6e
   * --*/
Shinya Kitaoka 120a6e
  TFrameId frameId =
Shinya Kitaoka 120a6e
      m_workingFrameId.isEmptyFrame() ? getCurrentFid() : m_workingFrameId;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_rasterTrack && (m_hardness.getValue() == 100 || m_pencil.getValue())) {
Shinya Kitaoka 120a6e
    double thickness =
Shinya Kitaoka 120a6e
        m_pressure.getValue()
Shinya Kitaoka 120a6e
            ? computeThickness(pressureVal, m_rasThickness, m_isPath)
Shinya Kitaoka 120a6e
            : m_rasThickness.getValue().second;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    /*--- ストロークの最初にMaxサイズの円が描かれてしまう不具合を防止する ---*/
Shinya Kitaoka 120a6e
    if (m_pressure.getValue() && pressureVal == 255)
Shinya Kitaoka 120a6e
      thickness = m_rasThickness.getValue().first;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    /*-- Pencilモードでなく、Hardness=100 の場合のブラシサイズを1段階下げる --*/
Shinya Kitaoka 120a6e
    if (!m_pencil.getValue()) thickness -= 1.0;
walkerka 22f456
walkerka 22f456
    TRectD invalidateRect;
walkerka 22f456
    TThickPoint thickPoint(pos + rasCenter, thickness);
walkerka 22f456
    m_smoothStroke.addPoint(thickPoint);
walkerka 22f456
    m_smoothStroke.endStroke();
walkerka 22f456
    std::vector<tthickpoint> pts;</tthickpoint>
walkerka 22f456
    m_smoothStroke.getSmoothPoints(pts);
walkerka 22f456
    for (size_t i = 0; i < pts.size(); ++i) {
walkerka 22f456
      const TThickPoint &thickPoint = pts[i];
walkerka 22f456
      bool isAdded                  = m_rasterTrack->add(thickPoint);
walkerka 22f456
      if (isAdded) {
walkerka 22f456
        m_tileSaver->save(m_rasterTrack->getLastRect());
walkerka 22f456
        m_rasterTrack->generateLastPieceOfStroke(m_pencil.getValue(), true);
walkerka 22f456
walkerka 22f456
        std::vector<tthickpoint> brushPoints =</tthickpoint>
walkerka 22f456
            m_rasterTrack->getPointsSequence();
walkerka 22f456
        int m = (int)brushPoints.size();
walkerka 22f456
        std::vector<tthickpoint> points;</tthickpoint>
walkerka 22f456
        if (m == 3) {
walkerka 22f456
          points.push_back(brushPoints[0]);
walkerka 22f456
          points.push_back(brushPoints[1]);
walkerka 22f456
        } else {
walkerka 22f456
          points.push_back(brushPoints[m - 4]);
walkerka 22f456
          points.push_back(brushPoints[m - 3]);
walkerka 22f456
          points.push_back(brushPoints[m - 2]);
walkerka 611fd3
        }
walkerka 22f456
        int maxThickness = m_rasThickness.getValue().second;
walkerka 22f456
        if (i == 0) {
walkerka 22f456
          invalidateRect =
walkerka 22f456
              ToolUtils::getBounds(points, maxThickness) - rasCenter;
walkerka 22f456
        } else {
walkerka 22f456
          invalidateRect +=
walkerka 22f456
              ToolUtils::getBounds(points, maxThickness) - rasCenter;
walkerka bb91cd
        }
walkerka 22f456
      }
walkerka 22f456
    }
walkerka 22f456
    invalidate(invalidateRect.enlarge(2));
walkerka 22f456
walkerka 22f456
    if (m_tileSet->getTileCount() > 0) {
walkerka 22f456
      TUndoManager::manager()->add(new RasterBrushUndo(
walkerka 22f456
          m_tileSet, m_rasterTrack->getPointsSequence(),
walkerka 22f456
          m_rasterTrack->getStyleId(), m_rasterTrack->isSelective(),
walkerka 22f456
          simLevel.getPointer(), frameId, m_pencil.getValue(), m_isFrameCreated,
walkerka 22f456
          m_isLevelCreated));
walkerka 22f456
    }
walkerka 22f456
    delete m_rasterTrack;
walkerka 22f456
    m_rasterTrack = 0;
walkerka 22f456
  } else {
walkerka 22f456
    if (m_points.size() != 1) {
walkerka 22f456
      double maxThickness = m_rasThickness.getValue().second;
walkerka 22f456
      double thickness =
walkerka 22f456
          (m_pressure.getValue() || m_isPath)
walkerka 22f456
              ? computeThickness(pressureVal, m_rasThickness, m_isPath)
walkerka 22f456
              : maxThickness;
walkerka 22f456
      TPointD rasCenter = ti->getRaster()->getCenterD();
walkerka 22f456
      TRectD invalidateRect;
walkerka 22f456
      bool rectUpdated = false;
walkerka 22f456
      TThickPoint thickPoint(pos + rasCenter, thickness);
walkerka 22f456
      m_smoothStroke.addPoint(thickPoint);
walkerka 22f456
      m_smoothStroke.endStroke();
walkerka 22f456
      std::vector<tthickpoint> pts;</tthickpoint>
walkerka 22f456
      m_smoothStroke.getSmoothPoints(pts);
walkerka 22f456
      for (size_t i = 0; i < pts.size() - 1; ++i) {
walkerka 22f456
        TThickPoint old = m_points.back();
walkerka 22f456
        if (norm2(pos - old) < 4) continue;
walkerka 22f456
walkerka 22f456
        const TThickPoint &point = pts[i];
walkerka 22f456
        TThickPoint mid((old + point) * 0.5, (point.thick + old.thick) * 0.5);
walkerka 22f456
        m_points.push_back(mid);
walkerka 22f456
        m_points.push_back(point);
walkerka 22f456
walkerka 22f456
        TRect bbox;
walkerka 22f456
        int m = (int)m_points.size();
walkerka 22f456
        std::vector<tthickpoint> points;</tthickpoint>
walkerka 22f456
        if (m == 3) {
walkerka 22f456
          // ho appena cominciato. devo disegnare un segmento
walkerka 22f456
          TThickPoint pa = m_points.front();
walkerka 22f456
          points.push_back(pa);
walkerka 22f456
          points.push_back(mid);
walkerka 22f456
          bbox = m_bluredBrush->getBoundFromPoints(points);
walkerka 22f456
          updateWorkAndBackupRasters(bbox + m_lastRect);
walkerka 22f456
          m_tileSaver->save(bbox);
walkerka 22f456
          m_bluredBrush->addArc(pa, (mid + pa) * 0.5, mid, 1, 1);
walkerka 22f456
          m_lastRect += bbox;
walkerka 22f456
        } else {
walkerka 22f456
          points.push_back(m_points[m - 4]);
walkerka 22f456
          points.push_back(old);
walkerka 22f456
          points.push_back(mid);
walkerka 22f456
          bbox = m_bluredBrush->getBoundFromPoints(points);
walkerka 22f456
          updateWorkAndBackupRasters(bbox + m_lastRect);
walkerka 22f456
          m_tileSaver->save(bbox);
walkerka 22f456
          m_bluredBrush->addArc(m_points[m - 4], old, mid, 1, 1);
walkerka 22f456
          m_lastRect += bbox;
walkerka 22f456
        }
walkerka 22f456
        if (!rectUpdated) {
walkerka 22f456
          invalidateRect =
walkerka 22f456
              ToolUtils::getBounds(points, maxThickness) - rasCenter;
walkerka 22f456
          rectUpdated = true;
walkerka 22f456
        } else {
walkerka 22f456
          invalidateRect +=
walkerka 22f456
              ToolUtils::getBounds(points, maxThickness) - rasCenter;
walkerka bb91cd
        }
walkerka 22f456
walkerka 22f456
        m_bluredBrush->updateDrawing(ti->getRaster(), m_backupRas, bbox,
walkerka 22f456
                                     m_styleId, m_selective.getValue());
walkerka 22f456
        m_strokeRect += bbox;
walkerka 22f456
      }
walkerka 22f456
      if (pts.size() > 0) {
walkerka 22f456
        TThickPoint point = pts.back();
walkerka 22f456
        m_points.push_back(point);
walkerka 22f456
        int m = m_points.size();
walkerka 22f456
        std::vector<tthickpoint> points;</tthickpoint>
walkerka 22f456
        points.push_back(m_points[m - 3]);
walkerka 22f456
        points.push_back(m_points[m - 2]);
walkerka 22f456
        points.push_back(m_points[m - 1]);
walkerka 22f456
        TRect bbox = m_bluredBrush->getBoundFromPoints(points);
walkerka 22f456
        updateWorkAndBackupRasters(bbox);
walkerka 22f456
        m_tileSaver->save(bbox);
walkerka 22f456
        m_bluredBrush->addArc(points[0], points[1], points[2], 1, 1);
walkerka 22f456
        m_bluredBrush->updateDrawing(ti->getRaster(), m_backupRas, bbox,
walkerka 22f456
                                     m_styleId, m_selective.getValue());
walkerka 22f456
walkerka 22f456
        if (!rectUpdated) {
walkerka 22f456
          invalidateRect =
walkerka 22f456
              ToolUtils::getBounds(points, maxThickness) - rasCenter;
walkerka 22f456
        } else {
walkerka 22f456
          invalidateRect +=
walkerka 22f456
              ToolUtils::getBounds(points, maxThickness) - rasCenter;
walkerka 22f456
        }
walkerka 22f456
        m_lastRect += bbox;
walkerka 22f456
        m_strokeRect += bbox;
walkerka 22f456
      }
walkerka 22f456
      invalidate(invalidateRect.enlarge(2));
walkerka 22f456
      m_lastRect.empty();
walkerka 22f456
    }
walkerka 22f456
    delete m_bluredBrush;
walkerka 22f456
    m_bluredBrush = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (m_tileSet->getTileCount() > 0) {
Shinya Kitaoka 120a6e
      TUndoManager::manager()->add(new RasterBluredBrushUndo(
Shinya Kitaoka 120a6e
          m_tileSet, m_points, m_styleId, m_selective.getValue(),
Shinya Kitaoka 120a6e
          simLevel.getPointer(), frameId, m_rasThickness.getValue().second,
Shinya Kitaoka 120a6e
          m_hardness.getValue() * 0.01, m_isFrameCreated, m_isLevelCreated));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  delete m_tileSaver;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_tileSaver = 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /*-- FIdを指定して、描画中にフレームが動いても、
Shinya Kitaoka 120a6e
    描画開始時のFidのサムネイルが更新されるようにする。--*/
Shinya Kitaoka 120a6e
  notifyImageChanged(frameId);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_strokeRect.empty();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  ToolUtils::updateSaveBox();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /*-- 作業中のフレームをリセット --*/
Shinya Kitaoka 120a6e
  m_workingFrameId = TFrameId();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::mouseMove(const TPointD &pos, const TMouseEvent &e) {
Shinya Kitaoka 120a6e
  qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  struct Locals {
Shinya Kitaoka 120a6e
    BrushTool *m_this;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    void setValue(TDoublePairProperty &prop,
Shinya Kitaoka 120a6e
                  const TDoublePairProperty::Value &value) {
Shinya Kitaoka 120a6e
      prop.setValue(value);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      m_this->onPropertyChanged(prop.getName());
Shinya Kitaoka 120a6e
      TTool::getApplication()->getCurrentTool()->notifyToolChanged();
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    void addMinMax(TDoublePairProperty &prop, double add) {
Shinya Kitaoka 120a6e
      if (add == 0.0) return;
Shinya Kitaoka 120a6e
      const TDoublePairProperty::Range &range = prop.getRange();
Shinya Kitaoka d4642c
Shinya Kitaoka 120a6e
      TDoublePairProperty::Value value = prop.getValue();
Shinya Kitaoka 120a6e
      value.first  = tcrop(value.first + add, range.first, range.second);
Shinya Kitaoka 120a6e
      value.second = tcrop(value.second + add, range.first, range.second);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      setValue(prop, value);
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
Jeremy Bullock 01e454
    void addMinMaxSeparate(TDoublePairProperty &prop, double min, double max) {
Jeremy Bullock 01e454
      if (min == 0.0 && max == 0.0) return;
Jeremy Bullock 01e454
      const TDoublePairProperty::Range &range = prop.getRange();
Shinya Kitaoka 120a6e
Jeremy Bullock 01e454
      TDoublePairProperty::Value value = prop.getValue();
Jeremy Bullock 01e454
      value.first += min;
Jeremy Bullock 01e454
      value.second += max;
Jeremy Bullock 01e454
      if (value.first > value.second) value.first = value.second;
Jeremy Bullock 01e454
      value.first  = tcrop(value.first, range.first, range.second);
Jeremy Bullock 01e454
      value.second = tcrop(value.second, range.first, range.second);
Shinya Kitaoka 120a6e
Jeremy Bullock 01e454
      setValue(prop, value);
Jeremy Bullock 01e454
    }
Shinya Kitaoka 120a6e
Jeremy Bullock 01e454
  } locals = {this};
Shinya Kitaoka 120a6e
Jeremy Bullock 01e454
  // if (e.isAltPressed() && !e.isCtrlPressed()) {
Jeremy Bullock 01e454
  // const TPointD &diff = pos - m_mousePos;
Jeremy Bullock 01e454
  // double add = (fabs(diff.x) > fabs(diff.y)) ? diff.x : diff.y;
Jeremy Bullock 01e454
Jeremy Bullock 01e454
  // locals.addMinMax(
Jeremy Bullock 01e454
  //  TToonzImageP(getImage(false, 1)) ? m_rasThickness : m_thickness, add);
Jeremy Bullock 01e454
  //} else
Jeremy Bullock 01e454
  if (e.isCtrlPressed() && e.isAltPressed()) {
Jeremy Bullock 01e454
    const TPointD &diff = pos - m_mousePos;
Jeremy Bullock 01e454
    double max          = diff.x / 2;
Jeremy Bullock 01e454
    double min          = diff.y / 2;
Jeremy Bullock 01e454
Jeremy Bullock 01e454
    locals.addMinMaxSeparate(
Jeremy Bullock 01e454
        (m_targetType & TTool::ToonzImage) ? m_rasThickness : m_thickness, min,
Jeremy Bullock 01e454
        max);
Jeremy Bullock 01e454
  } else {
Shinya Kitaoka 120a6e
    m_brushPos = pos;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  m_mousePos = pos;
Shinya Kitaoka 120a6e
  invalidate();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_minThick == 0 && m_maxThick == 0) {
Shinya Kitaoka 120a6e
    if (m_targetType & TTool::ToonzImage) {
Shinya Kitaoka 120a6e
      m_minThick = m_rasThickness.getValue().first;
Shinya Kitaoka 120a6e
      m_maxThick = m_rasThickness.getValue().second;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      m_minThick = m_thickness.getValue().first;
Shinya Kitaoka 120a6e
      m_maxThick = m_thickness.getValue().second;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::draw() {
Shinya Kitaoka 120a6e
  /*--ショートカットでのツール切り替え時に赤点が描かれるのを防止する--*/
Shinya Kitaoka 120a6e
  if (m_minThick == 0 && m_maxThick == 0) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TImageP img = getImage(false, 1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Draw track
Shinya Kitaoka 120a6e
  tglColor(m_isPrompting ? TPixel32::Green : m_currentColor);
Shinya Kitaoka 120a6e
  m_track.drawAllFragments();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (getApplication()->getCurrentObject()->isSpline()) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Draw the brush outline - change color when the Ink / Paint check is
Shinya Kitaoka 120a6e
  // activated
Shinya Kitaoka 120a6e
  if ((ToonzCheck::instance()->getChecks() & ToonzCheck::eInk) ||
Shinya Kitaoka 120a6e
      (ToonzCheck::instance()->getChecks() & ToonzCheck::ePaint) ||
Shinya Kitaoka 120a6e
      (ToonzCheck::instance()->getChecks() & ToonzCheck::eInk1))
Shinya Kitaoka 120a6e
    glColor3d(0.5, 0.8, 0.8);
Shinya Kitaoka 120a6e
  // normally draw in red
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    glColor3d(1.0, 0.0, 0.0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (TToonzImageP ti = img) {
Shinya Kitaoka 120a6e
    TRasterP ras = ti->getRaster();
Shinya Kitaoka 120a6e
    int lx       = ras->getLx();
Shinya Kitaoka 120a6e
    int ly       = ras->getLy();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    drawEmptyCircle(m_brushPos, tround(m_minThick), lx % 2 == 0, ly % 2 == 0,
Shinya Kitaoka 120a6e
                    m_pencil.getValue());
Shinya Kitaoka 120a6e
    drawEmptyCircle(m_brushPos, tround(m_maxThick), lx % 2 == 0, ly % 2 == 0,
Shinya Kitaoka 120a6e
                    m_pencil.getValue());
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    tglDrawCircle(m_brushPos, 0.5 * m_minThick);
Shinya Kitaoka 120a6e
    tglDrawCircle(m_brushPos, 0.5 * m_maxThick);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::onEnter() {
Shinya Kitaoka 120a6e
  TImageP img = getImage(false);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (TToonzImageP(img)) {
Shinya Kitaoka 120a6e
    m_minThick = m_rasThickness.getValue().first;
Shinya Kitaoka 120a6e
    m_maxThick = m_rasThickness.getValue().second;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    m_minThick = m_thickness.getValue().first;
Shinya Kitaoka 120a6e
    m_maxThick = m_thickness.getValue().second;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Application *app = getApplication();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_styleId       = app->getCurrentLevelStyleIndex();
Shinya Kitaoka 120a6e
  TColorStyle *cs = app->getCurrentLevelStyle();
Shinya Kitaoka 120a6e
  if (cs) {
Shinya Kitaoka 120a6e
    TRasterStyleFx *rfx = cs->getRasterStyleFx();
Shinya Kitaoka 120a6e
    m_active            = cs->isStrokeStyle() || (rfx && rfx->isInkStyle());
Shinya Kitaoka 120a6e
    m_currentColor      = cs->getAverageColor();
Shinya Kitaoka 120a6e
    m_currentColor.m    = 255;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    m_currentColor = TPixel32::Black;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  m_active = img;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::onLeave() {
Shinya Kitaoka 120a6e
  m_minThick = 0;
Shinya Kitaoka 120a6e
  m_maxThick = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPropertyGroup *BrushTool::getProperties(int idx) {
Shinya Kitaoka 120a6e
  if (!m_presetsLoaded) initPresets();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return &m_prop[idx];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::onImageChanged() {
Shinya Kitaoka 120a6e
  TToonzImageP ti = (TToonzImageP)getImage(false, 1);
Shinya Kitaoka 120a6e
  if (!ti || !isEnabled()) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  setWorkAndBackupImages();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::setWorkAndBackupImages() {
Shinya Kitaoka 120a6e
  TToonzImageP ti = (TToonzImageP)getImage(false, 1);
Shinya Kitaoka 120a6e
  if (!ti) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRasterP ras   = ti->getRaster();
Shinya Kitaoka 120a6e
  TDimension dim = ras->getSize();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double hardness = m_hardness.getValue() * 0.01;
Shinya Kitaoka 120a6e
  if (hardness == 1.0 && ras->getPixelSize() == 4) {
Shinya Kitaoka 120a6e
    m_workRas   = TRaster32P();
Shinya Kitaoka 120a6e
    m_backupRas = TRasterCM32P();
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    if (!m_workRas || m_workRas->getLx() > dim.lx ||
Shinya Kitaoka 120a6e
        m_workRas->getLy() > dim.ly)
Shinya Kitaoka 120a6e
      m_workRas = TRaster32P(dim);
Shinya Kitaoka 120a6e
    if (!m_backupRas || m_backupRas->getLx() > dim.lx ||
Shinya Kitaoka 120a6e
        m_backupRas->getLy() > dim.ly)
Shinya Kitaoka 120a6e
      m_backupRas = TRasterCM32P(dim);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_strokeRect.empty();
Shinya Kitaoka 120a6e
    m_lastRect.empty();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool BrushTool::onPropertyChanged(std::string propertyName) {
Shinya Kitaoka 120a6e
  // Set the following to true whenever a different piece of interface must
Shinya Kitaoka 120a6e
  // be refreshed - done once at the end.
Shinya Kitaoka 120a6e
  bool notifyTool = false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /*--- 変更されたPropertyに合わせて処理を分ける ---*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /*--- m_thicknessとm_rasThicknessは同じName(="Size:")なので、
Shinya Kitaoka 120a6e
          扱っている画像がラスタかどうかで区別する---*/
Shinya Kitaoka 120a6e
  if (propertyName == m_thickness.getName()) {
Shinya Kitaoka 120a6e
    TImageP img = getImage(false);
Shinya Kitaoka 120a6e
    if (TToonzImageP(img))  // raster
Shinya Kitaoka 120a6e
    {
Shinya Kitaoka 120a6e
      RasterBrushMinSize = m_rasThickness.getValue().first;
Shinya Kitaoka 120a6e
      RasterBrushMaxSize = m_rasThickness.getValue().second;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      m_minThick = m_rasThickness.getValue().first;
Shinya Kitaoka 120a6e
      m_maxThick = m_rasThickness.getValue().second;
Shinya Kitaoka 120a6e
    } else  // vector
Shinya Kitaoka 120a6e
    {
Shinya Kitaoka 120a6e
      VectorBrushMinSize = m_thickness.getValue().first;
Shinya Kitaoka 120a6e
      VectorBrushMaxSize = m_thickness.getValue().second;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      m_minThick = m_thickness.getValue().first;
Shinya Kitaoka 120a6e
      m_maxThick = m_thickness.getValue().second;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else if (propertyName == m_accuracy.getName()) {
Shinya Kitaoka 120a6e
    BrushAccuracy = m_accuracy.getValue();
walkerka bb91cd
  } else if (propertyName == m_smooth.getName()) {
walkerka bb91cd
    BrushSmooth = m_smooth.getValue();
Shinya Kitaoka 120a6e
  } else if (propertyName == m_preset.getName()) {
Shinya Kitaoka 120a6e
    loadPreset();
Shinya Kitaoka 120a6e
    notifyTool = true;
Shinya Kitaoka 120a6e
  } else if (propertyName == m_selective.getName()) {
Shinya Kitaoka 120a6e
    BrushSelective = m_selective.getValue();
Shinya Kitaoka 120a6e
  } else if (propertyName == m_breakAngles.getName()) {
Shinya Kitaoka 120a6e
    BrushBreakSharpAngles = m_breakAngles.getValue();
Shinya Kitaoka 120a6e
  } else if (propertyName == m_pencil.getName()) {
Shinya Kitaoka 120a6e
    RasterBrushPencilMode = m_pencil.getValue();
Shinya Kitaoka 120a6e
  } else if (propertyName == m_pressure.getName()) {
Campbell Barton f49389
    BrushPressureSensitivity = m_pressure.getValue();
Shinya Kitaoka 120a6e
  } else if (propertyName == m_capStyle.getName()) {
Shinya Kitaoka 120a6e
    VectorCapStyle = m_capStyle.getIndex();
Shinya Kitaoka 120a6e
  } else if (propertyName == m_joinStyle.getName()) {
Shinya Kitaoka 120a6e
    VectorJoinStyle = m_joinStyle.getIndex();
Shinya Kitaoka 120a6e
  } else if (propertyName == m_miterJoinLimit.getName()) {
Shinya Kitaoka 120a6e
    VectorMiterValue = m_miterJoinLimit.getValue();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_targetType & TTool::Vectors) {
Shinya Kitaoka 120a6e
    if (propertyName == m_joinStyle.getName()) notifyTool = true;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (m_targetType & TTool::ToonzImage) {
Shinya Kitaoka 120a6e
    if (propertyName == m_hardness.getName()) setWorkAndBackupImages();
Shinya Kitaoka 120a6e
    if (propertyName == m_hardness.getName() ||
Shinya Kitaoka 120a6e
        propertyName == m_thickness.getName()) {
Shinya Kitaoka 120a6e
      m_brushPad = getBrushPad(m_rasThickness.getValue().second,
Shinya Kitaoka 120a6e
                               m_hardness.getValue() * 0.01);
Shinya Kitaoka 120a6e
      TRectD rect(m_mousePos - TPointD(m_maxThick + 2, m_maxThick + 2),
Shinya Kitaoka 120a6e
                  m_mousePos + TPointD(m_maxThick + 2, m_maxThick + 2));
Shinya Kitaoka 120a6e
      invalidate(rect);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (propertyName != m_preset.getName() &&
Shinya Kitaoka 120a6e
      m_preset.getValue() != CUSTOM_WSTR) {
Shinya Kitaoka 120a6e
    m_preset.setValue(CUSTOM_WSTR);
Shinya Kitaoka 120a6e
    notifyTool = true;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (notifyTool) getApplication()->getCurrentTool()->notifyToolChanged();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::initPresets() {
Shinya Kitaoka 120a6e
  if (!m_presetsLoaded) {
Shinya Kitaoka 120a6e
    // If necessary, load the presets from file
Shinya Kitaoka 120a6e
    m_presetsLoaded = true;
Shinya Kitaoka 120a6e
    if (getTargetType() & TTool::Vectors)
Shinya Kitaoka 120a6e
      m_presetsManager.load(TEnv::getConfigDir() + "brush_vector.txt");
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      m_presetsManager.load(TEnv::getConfigDir() + "brush_toonzraster.txt");
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Rebuild the presets property entries
Shinya Kitaoka 120a6e
  const std::set<brushdata> &presets = m_presetsManager.presets();</brushdata>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_preset.deleteAllValues();
Shinya Kitaoka 120a6e
  m_preset.addValue(CUSTOM_WSTR);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::set<brushdata>::const_iterator it, end = presets.end();</brushdata>
Shinya Kitaoka 120a6e
  for (it = presets.begin(); it != end; ++it) m_preset.addValue(it->m_name);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::loadPreset() {
Shinya Kitaoka 120a6e
  const std::set<brushdata> &presets = m_presetsManager.presets();</brushdata>
Shinya Kitaoka 120a6e
  std::set<brushdata>::const_iterator it;</brushdata>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  it = presets.find(BrushData(m_preset.getValue()));
Shinya Kitaoka 120a6e
  if (it == presets.end()) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  const BrushData &preset = *it;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  try  // Don't bother with RangeErrors
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    if (getTargetType() & TTool::Vectors) {
Shinya Kitaoka 120a6e
      m_thickness.setValue(
Shinya Kitaoka 120a6e
          TDoublePairProperty::Value(preset.m_min, preset.m_max));
Shinya Kitaoka 120a6e
      m_accuracy.setValue(preset.m_acc, true);
walkerka bb91cd
      m_smooth.setValue(preset.m_smooth, true);
Shinya Kitaoka 120a6e
      m_breakAngles.setValue(preset.m_breakAngles);
Shinya Kitaoka 120a6e
      m_pressure.setValue(preset.m_pressure);
Shinya Kitaoka 120a6e
      m_capStyle.setIndex(preset.m_cap);
Shinya Kitaoka 120a6e
      m_joinStyle.setIndex(preset.m_join);
Shinya Kitaoka 120a6e
      m_miterJoinLimit.setValue(preset.m_miter);
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      m_rasThickness.setValue(TDoublePairProperty::Value(
Shinya Kitaoka 120a6e
          std::max(preset.m_min, 1.0), preset.m_max));
Shinya Kitaoka 120a6e
      m_brushPad =
Shinya Kitaoka 120a6e
          ToolUtils::getBrushPad(preset.m_max, preset.m_hardness * 0.01);
walkerka bb91cd
      m_smooth.setValue(preset.m_smooth, true);
Shinya Kitaoka 120a6e
      m_hardness.setValue(preset.m_hardness, true);
Shinya Kitaoka 120a6e
      m_selective.setValue(preset.m_selective);
Shinya Kitaoka 120a6e
      m_pencil.setValue(preset.m_pencil);
Shinya Kitaoka 120a6e
      m_pressure.setValue(preset.m_pressure);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::addPreset(QString name) {
Shinya Kitaoka 120a6e
  // Build the preset
Shinya Kitaoka 120a6e
  BrushData preset(name.toStdWString());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (getTargetType() & TTool::Vectors) {
Shinya Kitaoka 120a6e
    preset.m_min = m_thickness.getValue().first;
Shinya Kitaoka 120a6e
    preset.m_max = m_thickness.getValue().second;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    preset.m_min = m_rasThickness.getValue().first;
Shinya Kitaoka 120a6e
    preset.m_max = m_rasThickness.getValue().second;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  preset.m_acc         = m_accuracy.getValue();
walkerka 22f456
  preset.m_smooth      = m_smooth.getValue();
Shinya Kitaoka 120a6e
  preset.m_hardness    = m_hardness.getValue();
Shinya Kitaoka 120a6e
  preset.m_selective   = m_selective.getValue();
Shinya Kitaoka 120a6e
  preset.m_pencil      = m_pencil.getValue();
Shinya Kitaoka 120a6e
  preset.m_breakAngles = m_breakAngles.getValue();
Shinya Kitaoka 120a6e
  preset.m_pressure    = m_pressure.getValue();
Shinya Kitaoka 120a6e
  preset.m_cap         = m_capStyle.getIndex();
Shinya Kitaoka 120a6e
  preset.m_join        = m_joinStyle.getIndex();
Shinya Kitaoka 120a6e
  preset.m_miter       = m_miterJoinLimit.getValue();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Pass the preset to the manager
Shinya Kitaoka 120a6e
  m_presetsManager.addPreset(preset);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Reinitialize the associated preset enum
Shinya Kitaoka 120a6e
  initPresets();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Set the value to the specified one
Shinya Kitaoka 120a6e
  m_preset.setValue(preset.m_name);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushTool::removePreset() {
Shinya Kitaoka 120a6e
  std::wstring name(m_preset.getValue());
Shinya Kitaoka 120a6e
  if (name == CUSTOM_WSTR) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_presetsManager.removePreset(name);
Shinya Kitaoka 120a6e
  initPresets();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // No parameter change, and set the preset value to custom
Shinya Kitaoka 120a6e
  m_preset.setValue(CUSTOM_WSTR);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*!	Brush、PaintBrush、EraserToolがPencilModeのときにTrueを返す
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
bool BrushTool::isPencilModeActive() {
Shinya Kitaoka 120a6e
  return getTargetType() == TTool::ToonzImage && m_pencil.getValue();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==========================================================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Tools instantiation
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
BrushTool vectorPencil("T_Brush", TTool::Vectors | TTool::EmptyTarget);
Toshihiro Shimizu 890ddd
BrushTool toonzPencil("T_Brush", TTool::ToonzImage | TTool::EmptyTarget);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*******************************************************************************
Toshihiro Shimizu 890ddd
//    Brush Data implementation
Toshihiro Shimizu 890ddd
//*******************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
BrushData::BrushData()
Shinya Kitaoka 120a6e
    : m_name()
Shinya Kitaoka 120a6e
    , m_min(0.0)
Shinya Kitaoka 120a6e
    , m_max(0.0)
Shinya Kitaoka 120a6e
    , m_acc(0.0)
walkerka bb91cd
    , m_smooth(0.0)
Shinya Kitaoka 120a6e
    , m_hardness(0.0)
Shinya Kitaoka 120a6e
    , m_opacityMin(0.0)
Shinya Kitaoka 120a6e
    , m_opacityMax(0.0)
Shinya Kitaoka 120a6e
    , m_selective(false)
Shinya Kitaoka 120a6e
    , m_pencil(false)
Shinya Kitaoka 120a6e
    , m_breakAngles(false)
Shinya Kitaoka 120a6e
    , m_pressure(false)
Shinya Kitaoka 120a6e
    , m_cap(0)
Shinya Kitaoka 120a6e
    , m_join(0)
Shinya Kitaoka 120a6e
    , m_miter(0) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
BrushData::BrushData(const std::wstring &name)
Shinya Kitaoka 120a6e
    : m_name(name)
Shinya Kitaoka 120a6e
    , m_min(0.0)
Shinya Kitaoka 120a6e
    , m_max(0.0)
Shinya Kitaoka 120a6e
    , m_acc(0.0)
walkerka bb91cd
    , m_smooth(0.0)
Shinya Kitaoka 120a6e
    , m_hardness(0.0)
Shinya Kitaoka 120a6e
    , m_opacityMin(0.0)
Shinya Kitaoka 120a6e
    , m_opacityMax(0.0)
Shinya Kitaoka 120a6e
    , m_selective(false)
Shinya Kitaoka 120a6e
    , m_pencil(false)
Shinya Kitaoka 120a6e
    , m_breakAngles(false)
Shinya Kitaoka 120a6e
    , m_pressure(false)
Shinya Kitaoka 120a6e
    , m_cap(0)
Shinya Kitaoka 120a6e
    , m_join(0)
Shinya Kitaoka 120a6e
    , m_miter(0) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushData::saveData(TOStream &os) {
Shinya Kitaoka 120a6e
  os.openChild("Name");
Shinya Kitaoka 120a6e
  os << m_name;
Shinya Kitaoka 120a6e
  os.closeChild();
Shinya Kitaoka 120a6e
  os.openChild("Thickness");
Shinya Kitaoka 120a6e
  os << m_min << m_max;
Shinya Kitaoka 120a6e
  os.closeChild();
Shinya Kitaoka 120a6e
  os.openChild("Accuracy");
Shinya Kitaoka 120a6e
  os << m_acc;
Shinya Kitaoka 120a6e
  os.closeChild();
walkerka bb91cd
  os.openChild("Smooth");
walkerka bb91cd
  os << m_smooth;
walkerka bb91cd
  os.closeChild();
Shinya Kitaoka 120a6e
  os.openChild("Hardness");
Shinya Kitaoka 120a6e
  os << m_hardness;
Shinya Kitaoka 120a6e
  os.closeChild();
Shinya Kitaoka 120a6e
  os.openChild("Opacity");
Shinya Kitaoka 120a6e
  os << m_opacityMin << m_opacityMax;
Shinya Kitaoka 120a6e
  os.closeChild();
Shinya Kitaoka 120a6e
  os.openChild("Selective");
Shinya Kitaoka 120a6e
  os << (int)m_selective;
Shinya Kitaoka 120a6e
  os.closeChild();
Shinya Kitaoka 120a6e
  os.openChild("Pencil");
Shinya Kitaoka 120a6e
  os << (int)m_pencil;
Shinya Kitaoka 120a6e
  os.closeChild();
Shinya Kitaoka 120a6e
  os.openChild("Break_Sharp_Angles");
Shinya Kitaoka 120a6e
  os << (int)m_breakAngles;
Shinya Kitaoka 120a6e
  os.closeChild();
Shinya Kitaoka 120a6e
  os.openChild("Pressure_Sensitivity");
Shinya Kitaoka 120a6e
  os << (int)m_pressure;
Shinya Kitaoka 120a6e
  os.closeChild();
Shinya Kitaoka 120a6e
  os.openChild("Cap");
Shinya Kitaoka 120a6e
  os << m_cap;
Shinya Kitaoka 120a6e
  os.closeChild();
Shinya Kitaoka 120a6e
  os.openChild("Join");
Shinya Kitaoka 120a6e
  os << m_join;
Shinya Kitaoka 120a6e
  os.closeChild();
Shinya Kitaoka 120a6e
  os.openChild("Miter");
Shinya Kitaoka 120a6e
  os << m_miter;
Shinya Kitaoka 120a6e
  os.closeChild();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushData::loadData(TIStream &is) {
Shinya Kitaoka 120a6e
  std::string tagName;
Shinya Kitaoka 120a6e
  int val;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  while (is.matchTag(tagName)) {
Shinya Kitaoka 120a6e
    if (tagName == "Name")
Shinya Kitaoka 120a6e
      is >> m_name, is.matchEndTag();
Shinya Kitaoka 120a6e
    else if (tagName == "Thickness")
Shinya Kitaoka 120a6e
      is >> m_min >> m_max, is.matchEndTag();
Shinya Kitaoka 120a6e
    else if (tagName == "Accuracy")
Shinya Kitaoka 120a6e
      is >> m_acc, is.matchEndTag();
walkerka bb91cd
    else if (tagName == "Smooth")
walkerka bb91cd
      is >> m_smooth, is.matchEndTag();
Shinya Kitaoka 120a6e
    else if (tagName == "Hardness")
Shinya Kitaoka 120a6e
      is >> m_hardness, is.matchEndTag();
Shinya Kitaoka 120a6e
    else if (tagName == "Opacity")
Shinya Kitaoka 120a6e
      is >> m_opacityMin >> m_opacityMax, is.matchEndTag();
Shinya Kitaoka 120a6e
    else if (tagName == "Selective")
Shinya Kitaoka 120a6e
      is >> val, m_selective = val, is.matchEndTag();
Shinya Kitaoka 120a6e
    else if (tagName == "Pencil")
Shinya Kitaoka 120a6e
      is >> val, m_pencil = val, is.matchEndTag();
Shinya Kitaoka 120a6e
    else if (tagName == "Break_Sharp_Angles")
Shinya Kitaoka 120a6e
      is >> val, m_breakAngles = val, is.matchEndTag();
Shinya Kitaoka 120a6e
    else if (tagName == "Pressure_Sensitivity")
Shinya Kitaoka 120a6e
      is >> val, m_pressure = val, is.matchEndTag();
Shinya Kitaoka 120a6e
    else if (tagName == "Cap")
Shinya Kitaoka 120a6e
      is >> m_cap, is.matchEndTag();
Shinya Kitaoka 120a6e
    else if (tagName == "Join")
Shinya Kitaoka 120a6e
      is >> m_join, is.matchEndTag();
Shinya Kitaoka 120a6e
    else if (tagName == "Miter")
Shinya Kitaoka 120a6e
      is >> m_miter, is.matchEndTag();
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      is.skipCurrentTag();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
PERSIST_IDENTIFIER(BrushData, "BrushData");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*******************************************************************************
Toshihiro Shimizu 890ddd
//    Brush Preset Manager implementation
Toshihiro Shimizu 890ddd
//*******************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushPresetManager::load(const TFilePath &fp) {
Shinya Kitaoka 120a6e
  m_fp = fp;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::string tagName;
Shinya Kitaoka 120a6e
  BrushData data;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TIStream is(m_fp);
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    while (is.matchTag(tagName)) {
Shinya Kitaoka 120a6e
      if (tagName == "version") {
Shinya Kitaoka 120a6e
        VersionNumber version;
Shinya Kitaoka 120a6e
        is >> version.first >> version.second;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        is.setVersion(version);
Shinya Kitaoka 120a6e
        is.matchEndTag();
Shinya Kitaoka 120a6e
      } else if (tagName == "brushes") {
Shinya Kitaoka 120a6e
        while (is.matchTag(tagName)) {
Shinya Kitaoka 120a6e
          if (tagName == "brush") {
Shinya Kitaoka 120a6e
            is >> data, m_presets.insert(data);
Shinya Kitaoka 120a6e
            is.matchEndTag();
Shinya Kitaoka 120a6e
          } else
Shinya Kitaoka 120a6e
            is.skipCurrentTag();
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        is.matchEndTag();
Shinya Kitaoka 120a6e
      } else
Shinya Kitaoka 120a6e
        is.skipCurrentTag();
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushPresetManager::save() {
Shinya Kitaoka 120a6e
  TOStream os(m_fp);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  os.openChild("version");
Shinya Kitaoka 120a6e
  os << 1 << 19;
Shinya Kitaoka 120a6e
  os.closeChild();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  os.openChild("brushes");
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::set<brushdata>::iterator it, end = m_presets.end();</brushdata>
Shinya Kitaoka 120a6e
  for (it = m_presets.begin(); it != end; ++it) {
Shinya Kitaoka 120a6e
    os.openChild("brush");
Shinya Kitaoka 120a6e
    os << (TPersist &)*it;
Shinya Kitaoka 120a6e
    os.closeChild();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  os.closeChild();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushPresetManager::addPreset(const BrushData &data) {
Shinya Kitaoka 120a6e
  m_presets.erase(data);  // Overwriting insertion
Shinya Kitaoka 120a6e
  m_presets.insert(data);
Shinya Kitaoka 120a6e
  save();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BrushPresetManager::removePreset(const std::wstring &name) {
Shinya Kitaoka 120a6e
  m_presets.erase(BrushData(name));
Shinya Kitaoka 120a6e
  save();
Toshihiro Shimizu 890ddd
}