Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
#include "toonzrasterbrushtool.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"
da847a
#include "tools/replicator.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/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"
Jeremy Bullock 7f2044
#include "toonz/preferences.h"
shun-iwasawa 98926d
#include "toonz/tpalettehandle.h"
shun-iwasawa 98926d
#include "toonz/mypaintbrushstyle.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"
Jeremy Bullock cd00fd
#include "tinbetween.h"
Jeremy Bullock cd00fd
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 RasterBrushMinSize("InknpaintRasterBrushMinSize", 1);
Toshihiro Shimizu 890ddd
TEnv::DoubleVar RasterBrushMaxSize("InknpaintRasterBrushMaxSize", 5);
walkerka 2e244a
TEnv::DoubleVar BrushSmooth("InknpaintBrushSmooth", 0);
shun-iwasawa 975eb1
TEnv::IntVar BrushDrawOrder("InknpaintBrushDrawOrder", 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);
shun-iwasawa 98926d
TEnv::DoubleVar RasterBrushModifierSize("RasterBrushModifierSize", 0);
manongjohn df5842
TEnv::StringVar RasterBrushPreset("RasterBrushPreset", "<custom>");</custom>
manongjohn f936c0
TEnv::IntVar BrushLockAlpha("InknpaintBrushLockAlpha", 0);
fa009d
TEnv::IntVar RasterBrushAssistants("RasterBrushAssistants", 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
#define CUSTOM_WSTR L"<custom>"</custom>
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)) {
shun-iwasawa 76d093
      t = 0.25 *
shun-iwasawa 76d093
          (2 * q.x * q.x + 2 * q.y * q.y - q.x * p0.x + q.x * p2.x -
shun-iwasawa 76d093
           q.y * p0.y + q.y * p2.y) /
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
shun-iwasawa 76d093
      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,
Jeremy Bullock 129805
                      bool levelCreated, TXshSimpleLevel *sLevel = NULL,
Jeremy Bullock 129805
                      TFrameId fid = TFrameId::NO_FRAME) {
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);
Jeremy Bullock 129805
  TXshSimpleLevel *sl;
Jeremy Bullock 129805
  if (!sLevel) {
Jeremy Bullock 129805
    sl = application->getCurrentLevel()->getSimpleLevel();
Jeremy Bullock 129805
  } else {
Jeremy Bullock 129805
    sl = sLevel;
Jeremy Bullock 129805
  }
Shinya Kitaoka 120a6e
  TFrameId id = application->getCurrentTool()->getTool()->getCurrentFid();
Jeremy Bullock 129805
  if (id == TFrameId::NO_FRAME && fid != TFrameId::NO_FRAME) id = fid;
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
shun-iwasawa 975eb1
enum DrawOrder { OverAll = 0, UnderAll, PaletteOrder };
shun-iwasawa 975eb1
shun-iwasawa 975eb1
void getAboveStyleIdSet(int styleId, TPaletteP palette,
shun-iwasawa 975eb1
                        QSet<int> &aboveStyles) {</int>
shun-iwasawa 975eb1
  if (!palette) return;
shun-iwasawa 975eb1
  for (int p = 0; p < palette->getPageCount(); p++) {
shun-iwasawa 975eb1
    TPalette::Page *page = palette->getPage(p);
shun-iwasawa 975eb1
    for (int s = 0; s < page->getStyleCount(); s++) {
shun-iwasawa 975eb1
      int tmpId = page->getStyleId(s);
shun-iwasawa 975eb1
      if (tmpId == styleId) return;
shun-iwasawa 975eb1
      if (tmpId != 0) aboveStyles.insert(tmpId);
shun-iwasawa 975eb1
    }
shun-iwasawa 975eb1
  }
shun-iwasawa 975eb1
}
shun-iwasawa 975eb1
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;
shun-iwasawa 975eb1
  bool m_isPaletteOrder;
Shinya Kitaoka 120a6e
  bool m_isPencil;
justburner e250b7
  bool m_isStraight;
manongjohn f936c0
  bool m_modifierLockAlpha;
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,
justburner e250b7
                  bool isLevelCreated, bool isPaletteOrder, bool lockAlpha,
justburner e250b7
                  bool isStraight = false)
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)
f278a5
      , m_isPaletteOrder(isPaletteOrder)
shun-iwasawa 975eb1
      , m_isPencil(isPencil)
justburner e250b7
      , m_isStraight(isStraight)
manongjohn f936c0
      , m_modifierLockAlpha(lockAlpha) {}
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();
f278a5
    RasterStrokeGenerator rasterTrack(
manongjohn f936c0
        ras, BRUSH, NONE, m_styleId, m_points[0], m_selective, 0,
manongjohn f936c0
        m_modifierLockAlpha, !m_isPencil, m_isPaletteOrder);
shun-iwasawa 975eb1
    if (m_isPaletteOrder) {
shun-iwasawa 975eb1
      QSet<int> aboveStyleIds;</int>
shun-iwasawa 975eb1
      getAboveStyleIdSet(m_styleId, image->getPalette(), aboveStyleIds);
f278a5
      rasterTrack.setAboveStyleIds(aboveStyleIds);
shun-iwasawa 975eb1
    }
f278a5
    rasterTrack.setPointsSequence(m_points);
f278a5
    rasterTrack.generateStroke(m_isPencil, m_isStraight);
Shinya Kitaoka 120a6e
    image->setSavebox(image->getSavebox() +
f278a5
                      rasterTrack.getBBox(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;
shun-iwasawa 975eb1
  DrawOrder m_drawOrder;
Shinya Kitaoka 120a6e
  int m_maxThick;
Shinya Kitaoka 120a6e
  double m_hardness;
justburner e250b7
  bool m_isStraight;
manongjohn 422d0a
  bool m_modifierLockAlpha;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  RasterBluredBrushUndo(TTileSetCM32 *tileSet,
Shinya Kitaoka 120a6e
                        const std::vector<tthickpoint> &points, int styleId,</tthickpoint>
manongjohn 422d0a
                        DrawOrder drawOrder, bool lockAlpha,
manongjohn 422d0a
                        TXshSimpleLevel *level, const TFrameId &frameId,
manongjohn 422d0a
                        int maxThick, double hardness, bool isFrameCreated,
justburner e250b7
                        bool isLevelCreated, bool isStraight = false)
Shinya Kitaoka 120a6e
      : TRasterUndo(tileSet, level, frameId, isFrameCreated, isLevelCreated, 0)
Shinya Kitaoka 120a6e
      , m_points(points)
Shinya Kitaoka 120a6e
      , m_styleId(styleId)
shun-iwasawa 975eb1
      , m_drawOrder(drawOrder)
Shinya Kitaoka 120a6e
      , m_maxThick(maxThick)
manongjohn 422d0a
      , m_hardness(hardness)
f278a5
      , m_isStraight(isStraight)
manongjohn 422d0a
      , m_modifierLockAlpha(lockAlpha) {}
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
shun-iwasawa 975eb1
    if (m_drawOrder == PaletteOrder) {
shun-iwasawa 975eb1
      QSet<int> aboveStyleIds;</int>
shun-iwasawa 975eb1
      getAboveStyleIdSet(m_styleId, image->getPalette(), aboveStyleIds);
shun-iwasawa 975eb1
      brush.setAboveStyleIds(aboveStyleIds);
shun-iwasawa 975eb1
    }
shun-iwasawa 975eb1
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);
manongjohn 422d0a
    brush.updateDrawing(ras, ras, bbox, m_styleId, (int)m_drawOrder,
manongjohn 422d0a
                        m_modifierLockAlpha);
justburner e250b7
    if (m_isStraight) {
justburner e250b7
      points.clear();
justburner e250b7
      points.push_back(m_points[0]);
justburner e250b7
      points.push_back(m_points[1]);
justburner e250b7
      points.push_back(m_points[2]);
justburner e250b7
      bbox = brush.getBoundFromPoints(points);
justburner e250b7
      brush.addArc(m_points[0], m_points[1], m_points[2], 1, 1);
justburner e250b7
      brush.updateDrawing(ras, backupRas, bbox, m_styleId, (int)m_drawOrder,
justburner e250b7
                          m_modifierLockAlpha);
justburner e250b7
    } else 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);
manongjohn 422d0a
      brush.updateDrawing(ras, backupRas, bbox, m_styleId, (int)m_drawOrder,
manongjohn 422d0a
                          m_modifierLockAlpha);
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);
manongjohn 422d0a
        brush.updateDrawing(ras, backupRas, bbox, m_styleId, (int)m_drawOrder,
manongjohn 422d0a
                            m_modifierLockAlpha);
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
shun-iwasawa 98926d
class MyPaintBrushUndo final : public TRasterUndo {
shun-iwasawa 98926d
  TPoint m_offset;
shun-iwasawa 98926d
  QString m_id;
shun-iwasawa 98926d
shun-iwasawa 98926d
public:
shun-iwasawa 98926d
  MyPaintBrushUndo(TTileSetCM32 *tileSet, TXshSimpleLevel *level,
shun-iwasawa 98926d
                   const TFrameId &frameId, bool isFrameCreated,
shun-iwasawa 98926d
                   bool isLevelCreated, const TRasterCM32P &ras,
shun-iwasawa 98926d
                   const TPoint &offset)
shun-iwasawa 98926d
      : TRasterUndo(tileSet, level, frameId, isFrameCreated, isLevelCreated, 0)
shun-iwasawa 98926d
      , m_offset(offset) {
shun-iwasawa 98926d
    static int counter = 0;
shun-iwasawa 98926d
    m_id = QString("MyPaintBrushUndo") + QString::number(counter++);
shun-iwasawa 98926d
    TImageCache::instance()->add(m_id.toStdString(),
shun-iwasawa 98926d
                                 TToonzImageP(ras, TRect(ras->getSize())));
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  ~MyPaintBrushUndo() { TImageCache::instance()->remove(m_id); }
shun-iwasawa 98926d
shun-iwasawa 98926d
  void redo() const override {
shun-iwasawa 98926d
    insertLevelAndFrameIfNeeded();
shun-iwasawa 98926d
shun-iwasawa 98926d
    TToonzImageP image = getImage();
shun-iwasawa 98926d
    TRasterCM32P ras   = image->getRaster();
shun-iwasawa 98926d
shun-iwasawa 98926d
    TImageP srcImg =
shun-iwasawa 98926d
        TImageCache::instance()->get(m_id.toStdString(), false)->cloneImage();
shun-iwasawa 98926d
    TToonzImageP tSrcImg = srcImg;
shun-iwasawa 98926d
    assert(tSrcImg);
shun-iwasawa 98926d
    ras->copy(tSrcImg->getRaster(), m_offset);
shun-iwasawa 98926d
    ToolUtils::updateSaveBox();
shun-iwasawa 98926d
    TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
shun-iwasawa 98926d
    notifyImageChanged();
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  int getSize() const override {
shun-iwasawa 98926d
    return sizeof(*this) + TRasterUndo::getSize();
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
shun-iwasawa 98926d
  QString getToolName() override { return QString("Brush Tool"); }
shun-iwasawa 98926d
  int getHistoryType() override { return HistoryType::BrushTool; }
shun-iwasawa 98926d
};
shun-iwasawa 98926d
shun-iwasawa 98926d
//=========================================================================================================
shun-iwasawa 98926d
shun-iwasawa 98926d
double computeThickness(double pressure, const TDoublePairProperty &property) {
shun-iwasawa 76d093
  double t      = pressure * pressure * pressure;
shun-iwasawa 76d093
  double thick0 = property.getValue().first;
shun-iwasawa 76d093
  double thick1 = property.getValue().second;
justburner e250b7
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
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
shun-iwasawa 28636d
static void Smooth(std::vector<tthickpoint> &points, const int radius,</tthickpoint>
shun-iwasawa 28636d
                   const int readIndex, const int level) {
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
shun-iwasawa 28636d
  int endSamples = 10;
shun-iwasawa 28636d
  int startId    = std::max(readIndex - endSamples * 3 - radius * level, 1);
shun-iwasawa 28636d
shun-iwasawa 28636d
  for (int i = startId; 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
shun-iwasawa 28636d
  auto result_itr = result.begin();
shun-iwasawa 28636d
  for (int i = startId; i < n - 1; ++i, ++result_itr) {
shun-iwasawa 28636d
    points[i].x     = (*result_itr).x;
shun-iwasawa 28636d
    points[i].y     = (*result_itr).y;
shun-iwasawa 28636d
    points[i].thick = (*result_itr).thick;
walkerka 22f456
  }
walkerka 22f456
walkerka 22f456
  if (points.size() >= 3) {
walkerka 22f456
    std::vector<tthickpoint> pts;</tthickpoint>
shun-iwasawa 28636d
    CatmullRomInterpolate(points[0], points[0], points[1], points[2],
shun-iwasawa 28636d
                          endSamples, pts);
pojienie ab83fc
    std::vector<tthickpoint>::iterator it = points.begin() + 1;</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],
shun-iwasawa 28636d
                          points[n - 1], endSamples, 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
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
//
shun-iwasawa 98926d
// ToonzRasterBrushTool
Toshihiro Shimizu 890ddd
//
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
ToonzRasterBrushTool::ToonzRasterBrushTool(std::string name, int targetType)
Shinya Kitaoka 120a6e
    : TTool(name)
shun-iwasawa 76d093
    , m_rasThickness("Size", 1, 1000, 1, 5)
walkerka bb91cd
    , m_smooth("Smooth:", 0, 50, 0)
Shinya Kitaoka 120a6e
    , m_hardness("Hardness:", 0, 100, 100)
Shinya Kitaoka 120a6e
    , m_preset("Preset:")
shun-iwasawa 975eb1
    , m_drawOrder("Draw Order:")
Shinya Kitaoka 120a6e
    , m_pencil("Pencil", false)
Shinya Kitaoka 120a6e
    , m_pressure("Pressure", true)
shun-iwasawa 98926d
    , m_modifierSize("ModifierSize", -3, 3, 0, true)
f278a5
    , m_modifierLockAlpha("Lock Alpha", false)
f278a5
    , m_assistants("Assistants", true)
f278a5
    , m_targetType(targetType)
Shinya Kitaoka 120a6e
    , m_enabled(false)
Shinya Kitaoka 120a6e
    , m_isPrompting(false)
Shinya Kitaoka 120a6e
    , m_firstTime(true)
Shinya Kitaoka 120a6e
    , m_presetsLoaded(false)
manongjohn f936c0
    , m_notifier(0)
f278a5
{
Shinya Kitaoka 120a6e
  bind(targetType);
Shinya Kitaoka 120a6e
shun-iwasawa 76d093
  m_rasThickness.setNonLinearSlider();
shun-iwasawa 76d093
shun-iwasawa 98926d
  m_prop[0].bind(m_rasThickness);
shun-iwasawa 98926d
  m_prop[0].bind(m_hardness);
shun-iwasawa 98926d
  m_prop[0].bind(m_smooth);
shun-iwasawa 98926d
  m_prop[0].bind(m_drawOrder);
shun-iwasawa 98926d
  m_prop[0].bind(m_modifierSize);
manongjohn f936c0
  m_prop[0].bind(m_modifierLockAlpha);
shun-iwasawa 98926d
  m_prop[0].bind(m_pencil);
f278a5
  m_prop[0].bind(m_assistants);
shun-iwasawa 98926d
  m_pencil.setId("PencilMode");
Shinya Kitaoka 120a6e
shun-iwasawa 98926d
  m_drawOrder.addValue(L"Over All");
shun-iwasawa 98926d
  m_drawOrder.addValue(L"Under All");
shun-iwasawa 98926d
  m_drawOrder.addValue(L"Palette Order");
shun-iwasawa 98926d
  m_drawOrder.setId("DrawOrder");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_prop[0].bind(m_pressure);
Jeremy Bullock cd00fd
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");
manongjohn f936c0
  m_modifierLockAlpha.setId("LockAlpha");
f278a5
f278a5
  m_inputmanager.setHandler(this);
f278a5
  m_modifierLine               = new TModifierLine();
f278a5
  m_modifierTangents           = new TModifierTangents();
f278a5
  m_modifierAssistants         = new TModifierAssistants();
f278a5
  m_modifierSegmentation       = new TModifierSegmentation();
f278a5
  m_modifierSmoothSegmentation = new TModifierSegmentation(TPointD(1, 1), 3);
f278a5
  for(int i = 0; i < 3; ++i)
f278a5
    m_modifierSmooth[i]        = new TModifierSmooth();
f278a5
#ifndef NDEBUG
fa009d
  m_modifierTest = new TModifierTest();
f278a5
#endif
f278a5
f278a5
  m_inputmanager.addModifier(
f278a5
      TInputModifierP(m_modifierAssistants.getPointer()));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
da847a
unsigned int ToonzRasterBrushTool::getToolHints() const {
da847a
  unsigned int h = TTool::getToolHints() & ~HintAssistantsAll;
da847a
  if (m_assistants.getValue()) {
da847a
    h |= HintReplicators;
da847a
    h |= HintReplicatorsPoints;
da847a
    h |= HintReplicatorsEnabled;
da847a
  }
da847a
  return h;
da847a
}
da847a
da847a
//-------------------------------------------------------------------------------------------------------
da847a
shun-iwasawa 98926d
ToolOptionsBox *ToonzRasterBrushTool::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
shun-iwasawa 98926d
void ToonzRasterBrushTool::drawLine(const TPointD &point, const TPointD ¢re,
shun-iwasawa 98926d
                                    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
shun-iwasawa 98926d
void ToonzRasterBrushTool::drawEmptyCircle(TPointD pos, int thick,
shun-iwasawa 98926d
                                           bool isLxEven, bool isLyEven,
shun-iwasawa 98926d
                                           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 {
f278a5
    pos.x = floor(pos.x) + 0.5;
f278a5
    pos.y = floor(pos.y) + 0.5;
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
manongjohn 603264
TPointD ToonzRasterBrushTool::getCenteredCursorPos(
manongjohn 603264
    const TPointD &originalCursorPos) {
manongjohn fe8e5c
  if (m_isMyPaintStyleSelected) return originalCursorPos;
manongjohn 603264
  TXshLevelHandle *levelHandle = m_application->getCurrentLevel();
manongjohn 603264
  TXshSimpleLevel *level = levelHandle ? levelHandle->getSimpleLevel() : 0;
manongjohn 603264
  TDimension resolution =
manongjohn 603264
      level ? level->getProperties()->getImageRes() : TDimension(0, 0);
manongjohn 603264
Martin van Zijl e9b900
  bool xEven = (resolution.lx % 2 == 0);
Martin van Zijl e9b900
  bool yEven = (resolution.ly % 2 == 0);
Martin van Zijl e9b900
Martin van Zijl e9b900
  TPointD centeredCursorPos = originalCursorPos;
Martin van Zijl e9b900
manongjohn 603264
  if (xEven) centeredCursorPos.x -= 0.5;
manongjohn 603264
  if (yEven) centeredCursorPos.y -= 0.5;
Martin van Zijl e9b900
Martin van Zijl e9b900
  return centeredCursorPos;
Martin van Zijl e9b900
}
Martin van Zijl e9b900
Martin van Zijl e9b900
//-------------------------------------------------------------------------------------------------------
Martin van Zijl e9b900
shun-iwasawa 98926d
void ToonzRasterBrushTool::updateTranslation() {
Shinya Kitaoka 120a6e
  m_rasThickness.setQStringName(tr("Size"));
Shinya Kitaoka 120a6e
  m_hardness.setQStringName(tr("Hardness:"));
walkerka bb91cd
  m_smooth.setQStringName(tr("Smooth:"));
shun-iwasawa 975eb1
  m_drawOrder.setQStringName(tr("Draw Order:"));
shun-iwasawa 98926d
  m_drawOrder.setItemUIName(L"Over All", tr("Over All"));
shun-iwasawa 98926d
  m_drawOrder.setItemUIName(L"Under All", tr("Under All"));
shun-iwasawa 98926d
  m_drawOrder.setItemUIName(L"Palette Order", tr("Palette Order"));
shun-iwasawa 98926d
  m_modifierSize.setQStringName(tr("Size"));
shun-iwasawa 98926d
Shinya Kitaoka 120a6e
  // m_filled.setQStringName(tr("Filled"));
Shinya Kitaoka 120a6e
  m_preset.setQStringName(tr("Preset:"));
shun-iwasawa df7bb0
  m_preset.setItemUIName(CUSTOM_WSTR, tr("<custom>"));</custom>
Shinya Kitaoka 120a6e
  m_pencil.setQStringName(tr("Pencil"));
Shinya Kitaoka 120a6e
  m_pressure.setQStringName(tr("Pressure"));
manongjohn f936c0
  m_modifierLockAlpha.setQStringName(tr("Lock Alpha"));
f278a5
  m_assistants.setQStringName(tr("Assistants"));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
void ToonzRasterBrushTool::onActivate() {
shun-iwasawa 98926d
  if (!m_notifier) m_notifier = new ToonzRasterBrushToolNotifier(this);
shun-iwasawa 98926d
Shinya Kitaoka 120a6e
  if (m_firstTime) {
Shinya Kitaoka 120a6e
    m_firstTime = false;
manongjohn df5842
manongjohn df5842
    std::wstring wpreset =
manongjohn df5842
        QString::fromStdString(RasterBrushPreset.getValue()).toStdWString();
manongjohn df5842
    if (wpreset != CUSTOM_WSTR) {
manongjohn df5842
      initPresets();
manongjohn 8b8d41
      if (!m_preset.isValue(wpreset)) wpreset = CUSTOM_WSTR;
manongjohn df5842
      m_preset.setValue(wpreset);
manongjohn 8b8d41
      RasterBrushPreset = m_preset.getValueAsString();
manongjohn df5842
      loadPreset();
manongjohn df5842
    } else
manongjohn df5842
      loadLastBrush();
Shinya Kitaoka 120a6e
  }
shun-iwasawa 98926d
  m_brushPad = ToolUtils::getBrushPad(m_rasThickness.getValue().second,
shun-iwasawa 98926d
                                      m_hardness.getValue() * 0.01);
shun-iwasawa 98926d
  setWorkAndBackupImages();
shun-iwasawa 98926d
da847a
  updateModifiers();
shun-iwasawa 98926d
  m_brushTimer.start();
Shinya Kitaoka 120a6e
  // TODO:app->editImageOrSpline();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
void ToonzRasterBrushTool::onDeactivate() {
f278a5
  m_inputmanager.finishTracks();
f278a5
  m_enabled   = false;
Shinya Kitaoka 120a6e
  m_workRas   = TRaster32P();
Shinya Kitaoka 120a6e
  m_backupRas = TRasterCM32P();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
bool ToonzRasterBrushTool::askRead(const TRect &rect) { return askWrite(rect); }
shun-iwasawa 98926d
shun-iwasawa 98926d
//--------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
bool ToonzRasterBrushTool::askWrite(const TRect &rect) {
shun-iwasawa 98926d
  if (rect.isEmpty()) return true;
f278a5
  m_painting.myPaint.strokeSegmentRect += rect;
shun-iwasawa 98926d
  updateWorkAndBackupRasters(rect);
f278a5
  m_painting.tileSaver->save(rect);
shun-iwasawa 98926d
  return true;
shun-iwasawa 98926d
}
shun-iwasawa 98926d
da847a
//---------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
da847a
void ToonzRasterBrushTool::updateModifiers() {
f278a5
  int smoothRadius = (int)round(m_smooth.getValue());
fa009d
  m_modifierAssistants->magnetism = m_assistants.getValue() ? 1 : 0;
fa009d
  m_inputmanager.drawPreview      = false; //!m_modifierAssistants->drawOnly;
f278a5
da847a
  m_modifierReplicate.clear();
da847a
  if (m_assistants.getValue())
da847a
    TReplicator::scanReplicators(this, nullptr, &m_modifierReplicate, false, true, false, false, nullptr);
da847a
  
f278a5
  m_inputmanager.clearModifiers();
f278a5
  m_inputmanager.addModifier(TInputModifierP(m_modifierTangents.getPointer()));
f278a5
  if (smoothRadius > 0) {
f278a5
    m_inputmanager.addModifier(TInputModifierP(m_modifierSmoothSegmentation.getPointer()));
f278a5
    for(int i = 0; i < 3; ++i) {
fa009d
      m_modifierSmooth[i]->radius = smoothRadius;
f278a5
      m_inputmanager.addModifier(TInputModifierP(m_modifierSmooth[i].getPointer()));
f278a5
    }
f278a5
  }
f278a5
  m_inputmanager.addModifier(TInputModifierP(m_modifierAssistants.getPointer()));
f278a5
#ifndef NDEBUG
f278a5
  m_inputmanager.addModifier(TInputModifierP(m_modifierTest.getPointer()));
f278a5
#endif
da847a
  m_inputmanager.addModifiers(m_modifierReplicate);
f278a5
  m_inputmanager.addModifier(TInputModifierP(m_modifierSegmentation.getPointer()));
da847a
}
f278a5
da847a
//--------------------------------------------------------------------------------------------------
da847a
da847a
bool ToonzRasterBrushTool::preLeftButtonDown() {
da847a
  updateModifiers();
Shinya Kitaoka 120a6e
  touchImage();
shun-iwasawa 85f3fb
  if (m_isFrameCreated) {
shun-iwasawa 85f3fb
    setWorkAndBackupImages();
shun-iwasawa 85f3fb
    // When the xsheet frame is selected, whole viewer will be updated from
shun-iwasawa 85f3fb
    // SceneViewer::onXsheetChanged() on adding a new frame.
shun-iwasawa 85f3fb
    // We need to take care of a case when the level frame is selected.
shun-iwasawa 85f3fb
    if (m_application->getCurrentFrame()->isEditingLevel()) invalidate();
shun-iwasawa 85f3fb
  }
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
f278a5
//---------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
f278a5
void ToonzRasterBrushTool::handleMouseEvent(MouseEventType type,
f278a5
                                          const TPointD &pos,
shun-iwasawa 98926d
                                          const TMouseEvent &e) {
f278a5
  TTimerTicks t = TToolTimer::ticks();
f278a5
  bool alt      = e.getModifiersMask() & TMouseEvent::ALT_KEY;
f278a5
  bool shift    = e.getModifiersMask() & TMouseEvent::SHIFT_KEY;
f278a5
  bool control  = e.getModifiersMask() & TMouseEvent::CTRL_KEY;
f278a5
f278a5
  if (shift && type == ME_DOWN && e.button() == Qt::LeftButton && !m_painting.active) {
fa009d
    m_modifierAssistants->magnetism = 0;
f278a5
    m_inputmanager.clearModifiers();
f278a5
    m_inputmanager.addModifier(TInputModifierP(m_modifierLine.getPointer()));
f278a5
    m_inputmanager.addModifier(TInputModifierP(m_modifierAssistants.getPointer()));
da847a
    m_inputmanager.addModifiers(m_modifierReplicate);
f278a5
    m_inputmanager.addModifier(TInputModifierP(m_modifierSegmentation.getPointer()));
f278a5
    m_inputmanager.drawPreview = true;
justburner e250b7
  }
justburner e250b7
f278a5
  if (alt != m_inputmanager.state.isKeyPressed(TKey::alt))
f278a5
    m_inputmanager.keyEvent(alt, TKey::alt, t, nullptr);
f278a5
  if (shift != m_inputmanager.state.isKeyPressed(TKey::shift))
f278a5
    m_inputmanager.keyEvent(shift, TKey::shift, t, nullptr);
f278a5
  if (control != m_inputmanager.state.isKeyPressed(TKey::control))
f278a5
    m_inputmanager.keyEvent(control, TKey::control, t, nullptr);
shun-iwasawa 98926d
f278a5
  if (type == ME_MOVE) {
f278a5
    THoverList hovers(1, pos);
f278a5
    m_inputmanager.hoverEvent(hovers);
f278a5
  } else {
fa009d
    int    deviceId    = e.isTablet() ? 1 : 0;
fa009d
    double defPressure = m_isMyPaintStyleSelected ? 0.5 : 1.0;
fa009d
    bool   hasPressure = e.isTablet();
fa009d
    double pressure    = hasPressure ? e.m_pressure : defPressure;
fa009d
    bool   final       = type == ME_UP;
fa009d
    m_inputmanager.trackEvent(
fa009d
      deviceId, 0, pos, pressure, TPointD(), hasPressure, false, final, t);
f278a5
    m_inputmanager.processTracks();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
f278a5
//---------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
f278a5
void ToonzRasterBrushTool::leftButtonDown(const TPointD &pos,
f278a5
                                        const TMouseEvent &e) {
f278a5
  handleMouseEvent(ME_DOWN, pos, e);
f278a5
}
shun-iwasawa 98926d
void ToonzRasterBrushTool::leftButtonDrag(const TPointD &pos,
f278a5
                                        const TMouseEvent &e) {
f278a5
  handleMouseEvent(ME_DRAG, pos, e);
f278a5
}
f278a5
void ToonzRasterBrushTool::leftButtonUp(const TPointD &pos,
f278a5
                                      const TMouseEvent &e) {
f278a5
  handleMouseEvent(ME_UP, pos, e);
f278a5
}
f278a5
void ToonzRasterBrushTool::mouseMove(const TPointD &pos, const TMouseEvent &e) {
f278a5
  handleMouseEvent(ME_MOVE, pos, e);
f278a5
}
Toshihiro Shimizu 890ddd
f278a5
//--------------------------------------------------------------------------------------------------
justburner 65e3a5
f278a5
void ToonzRasterBrushTool::inputSetBusy(bool busy) {
f278a5
  if (m_painting.active == busy)
justburner e250b7
    return;
f278a5
  
f278a5
  if (busy) {
f278a5
    // begin painting
f278a5
    
f278a5
    TTool::Application *app = TTool::getApplication();
f278a5
    if (!app) return;
justburner e250b7
f278a5
    int col   = app->getCurrentColumn()->getColumnIndex();
f278a5
    m_enabled = col >= 0 || app->getCurrentFrame()->isEditingLevel();
f278a5
    // todo: gestire autoenable
f278a5
    if (!m_enabled) return;
f278a5
f278a5
    m_painting.active = !!getImage(true);
f278a5
    if (!m_painting.active)
f278a5
      m_painting.active = !!touchImage();
f278a5
    if (!m_painting.active)
f278a5
      return;
f278a5
    
f278a5
    // nel caso che il colore corrente sia un cleanup/studiopalette color
f278a5
    // oppure il colore di un colorfield
f278a5
    updateCurrentStyle();
f278a5
    if (TColorStyle *cs = app->getCurrentLevelStyle()) {
f278a5
      m_painting.styleId = app->getCurrentLevelStyleIndex();
f278a5
      TRasterStyleFx *rfx = cs->getRasterStyleFx();
f278a5
      if (!cs->isStrokeStyle() && (!rfx || !rfx->isInkStyle()))
f278a5
          { m_painting.active = false; return; }
Jeremy Bullock 7f2044
    } else {
f278a5
      m_painting.styleId = 1;
Jeremy Bullock 7f2044
    }
f278a5
    
f278a5
    m_painting.frameId = getFrameId();
f278a5
f278a5
    TImageP img = getImage(true);
f278a5
    TToonzImageP ri(img);
f278a5
    TRasterCM32P ras = ri->getRaster();
f278a5
    if (!ras)
f278a5
      { m_painting.active = false; return; }
f278a5
      
f278a5
    m_painting.tileSet   = new TTileSetCM32(ras->getSize());
f278a5
    m_painting.tileSaver = new TTileSaverCM32(ras, m_painting.tileSet);
f278a5
    m_painting.affectedRect.empty();
f278a5
    setWorkAndBackupImages();
f278a5
    
f278a5
    if (m_isMyPaintStyleSelected) {
f278a5
      // init myPaint drawing
f278a5
      
f278a5
      m_painting.myPaint.isActive = true;
f278a5
      m_painting.myPaint.strokeSegmentRect.empty();
f278a5
      
f278a5
      m_workRas->lock();
f278a5
      
f278a5
      // prepare base brush
f278a5
      if ( TMyPaintBrushStyle *mypaintStyle = dynamic_cast<tmypaintbrushstyle *="">(app->getCurrentLevelStyle()) )</tmypaintbrushstyle>
f278a5
        m_painting.myPaint.baseBrush.fromBrush( mypaintStyle->getBrush() ); else
f278a5
          m_painting.myPaint.baseBrush.fromDefaults();
f278a5
      double modifierSize = m_modifierSize.getValue() * log(2.0);
f278a5
      float baseSize = m_painting.myPaint.baseBrush.getBaseValue( MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC );
f278a5
      m_painting.myPaint.baseBrush.setBaseValue( MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC, baseSize + modifierSize );
f278a5
    } else
f278a5
    if (m_hardness.getValue() == 100 || m_pencil.getValue()) {
f278a5
      // init pencil drawing
f278a5
      
f278a5
      m_painting.pencil.isActive = true;
f278a5
      m_painting.pencil.realPencil = m_pencil.getValue();
f278a5
    } else {
f278a5
      // init blured brush drawing (regular drawing)
f278a5
      
f278a5
      m_painting.blured.isActive = true;
Jeremy Bullock cd00fd
    }
f278a5
    
shun-iwasawa 98926d
  } else {
f278a5
    // finish painting
f278a5
    
f278a5
    if (m_painting.myPaint.isActive) {
f278a5
      // finish myPaint drawing
f278a5
      m_workRas->unlock();
Shinya Kitaoka 120a6e
    }
Jeremy Bullock cd00fd
f278a5
    delete m_painting.tileSaver;
f278a5
    m_painting.tileSaver = nullptr;
f278a5
f278a5
    // add undo record
f278a5
    TFrameId frameId = m_painting.frameId.isEmptyFrame() ? getCurrentFid() : m_painting.frameId;
f278a5
    if (m_painting.tileSet->getTileCount() > 0) {
f278a5
      TTool::Application *app   = TTool::getApplication();
f278a5
      TXshLevel *level          = app->getCurrentLevel()->getLevel();
f278a5
      TXshSimpleLevelP simLevel = level->getSimpleLevel();
f278a5
      TRasterCM32P ras          = TToonzImageP( getImage(true) )->getRaster();
f278a5
      TRasterCM32P subras       = ras->extract(m_painting.affectedRect)->clone();
f278a5
      TUndoManager::manager()->add(new MyPaintBrushUndo(
f278a5
          m_painting.tileSet, simLevel.getPointer(), frameId, m_isFrameCreated,
f278a5
          m_isLevelCreated, subras, m_painting.affectedRect.getP00()));
f278a5
      // MyPaintBrushUndo will delete tileSet by it self
f278a5
    } else {
f278a5
      // delete tileSet here because MyPaintBrushUndo will not do it
f278a5
      delete m_painting.tileSet;
Jeremy Bullock cd00fd
    }
f278a5
    m_painting.tileSet = nullptr;
Jeremy Bullock cd00fd
f278a5
    /*-- 作業中のフレームをリセット --*/
f278a5
    m_painting.frameId = TFrameId();
Jeremy Bullock cd00fd
walkerka 57fb3c
f278a5
    m_painting.myPaint.isActive = false;
f278a5
    m_painting.pencil.isActive = false;
f278a5
    m_painting.blured.isActive = false;
f278a5
    m_painting.active = false;
f278a5
    
f278a5
    /*-- FIdを指定して、描画中にフレームが動いても、
f278a5
      描画開始時のFidのサムネイルが更新されるようにする。--*/
f278a5
    notifyImageChanged(frameId);
f278a5
    ToolUtils::updateSaveBox();
walkerka 22f456
  }
walkerka 57fb3c
}
walkerka 57fb3c
f278a5
//--------------------------------------------------------------------------------------------------
Shinya Kitaoka 120a6e
f278a5
void ToonzRasterBrushTool::inputPaintTrackPoint(const TTrackPoint &point, const TTrack &track, bool firstTrack, bool preview) {
f278a5
  if (!m_painting.active || preview)
f278a5
    return;
f278a5
  
f278a5
  TImageP img = getImage(true);
f278a5
  TToonzImageP ri(img);
f278a5
  TRasterCM32P ras = ri->getRaster();
f278a5
  if (!ras)
f278a5
    return;
f278a5
  TPointD rasCenter = ras->getCenterD();
f278a5
  TPointD fixedPosition = getCenteredCursorPos(point.position);
f278a5
  
walkerka 22f456
f278a5
  TRectD invalidateRect;
f278a5
  bool firstPoint = track.size() == track.pointsAdded;
f278a5
  bool lastPoint  = track.pointsAdded == 1 && track.finished();
f278a5
  
f278a5
  // first point must be without handler, following points must be with handler
f278a5
  // other behaviour is possible bug and must be ignored
fa009d
  assert(firstPoint == !track.handler);
fa009d
  if (firstPoint != !track.handler)
f278a5
    return;
f278a5
  
fa009d
  double defPressure = m_painting.myPaint.isActive ? 0.5 : 1.0;
fa009d
  double pressure    = m_pressure.getValue() ? point.pressure : defPressure;
fa009d
  
f278a5
  if (m_painting.myPaint.isActive) {
f278a5
    // mypaint case
f278a5
    
f278a5
    // init brush
f278a5
    MyPaintStroke *handler;
f278a5
    if (firstPoint) {
f278a5
      handler = new MyPaintStroke(m_workRas, *this, m_painting.myPaint.baseBrush, false);
f278a5
      handler->brush.beginStroke();
fa009d
      track.handler = handler;
Jeremy Bullock 6c7e54
    }
fa009d
    handler = dynamic_cast<mypaintstroke*>(track.handler.getPointer());</mypaintstroke*>
f278a5
    if (!handler) return;
f278a5
    
f278a5
    // paint stroke
f278a5
    m_painting.myPaint.strokeSegmentRect.empty();
fa009d
    handler->brush.strokeTo( fixedPosition + rasCenter, pressure,
f278a5
                             point.tilt, point.time - track.previous().time );
f278a5
    if (lastPoint)
f278a5
      handler->brush.endStroke();
f278a5
    
f278a5
    // update affected area
f278a5
    TRect updateRect = m_painting.myPaint.strokeSegmentRect * ras->getBounds();
f278a5
    m_painting.affectedRect += updateRect;
f278a5
    if (!updateRect.isEmpty())
f278a5
      handler->brush.updateDrawing( ras, m_backupRas, m_painting.myPaint.strokeSegmentRect,
f278a5
                                    m_painting.styleId, m_modifierLockAlpha.getValue() );
f278a5
    
f278a5
    // determine invalidate rect
f278a5
    invalidateRect += convert(m_painting.myPaint.strokeSegmentRect) - rasCenter;
f278a5
  } else
f278a5
  if (m_painting.pencil.isActive) {
f278a5
    // pencil case
f278a5
    
f278a5
    // Pencilモードでなく、Hardness=100 の場合のブラシサイズを1段階下げる
fa009d
    double thickness = computeThickness(pressure, m_rasThickness)*2;
f278a5
    //if (!m_painting.pencil.realPencil && !m_modifierLine->getManager())
f278a5
    //  thickness -= 1.0;
f278a5
    TThickPoint thickPoint(fixedPosition + rasCenter, thickness);
f278a5
f278a5
    // init brush
f278a5
    PencilStroke *handler;
f278a5
    if (firstPoint) {
f278a5
      DrawOrder drawOrder = (DrawOrder)m_drawOrder.getIndex();
f278a5
      handler = new PencilStroke( ras, BRUSH, NONE, m_painting.styleId, thickPoint, drawOrder != OverAll, 0,
f278a5
                                  m_modifierLockAlpha.getValue(), !m_painting.pencil.realPencil,
f278a5
                                  drawOrder == PaletteOrder );
f278a5
      
f278a5
      // if the drawOrder mode = "Palette Order",
f278a5
      // get styleId list which is above the current style in the palette
f278a5
      if (drawOrder == PaletteOrder) {
f278a5
        QSet<int> aboveStyleIds;</int>
f278a5
        getAboveStyleIdSet(m_painting.styleId, ri->getPalette(), aboveStyleIds);
f278a5
        handler->brush.setAboveStyleIds(aboveStyleIds);
walkerka 22f456
      }
fa009d
      track.handler = handler;
shun-iwasawa cd7d6a
    }
fa009d
    handler = dynamic_cast<pencilstroke*>(track.handler.getPointer());</pencilstroke*>
f278a5
    if (!handler) return;
f278a5
f278a5
    // paint stroke
f278a5
    if (!firstPoint)
f278a5
      handler->brush.add(thickPoint);
f278a5
    
f278a5
    // update affected area
f278a5
    TRect strokeRect = handler->brush.getLastRect() * ras->getBounds();
f278a5
    m_painting.tileSaver->save( strokeRect );
f278a5
    m_painting.affectedRect += strokeRect;
f278a5
    handler->brush.generateLastPieceOfStroke( m_painting.pencil.realPencil );
f278a5
    
f278a5
    // determine invalidate rect
f278a5
    invalidateRect += convert(strokeRect) - rasCenter;
f278a5
  } else
f278a5
  if (m_painting.blured.isActive) {
f278a5
    // blured brush case (aka regular brush)
f278a5
f278a5
    double maxThick = m_rasThickness.getValue().second;
f278a5
    DrawOrder drawOrder = (DrawOrder)m_drawOrder.getIndex();
f278a5
    
f278a5
    // init brush
f278a5
    BluredStroke *handler;
f278a5
    if (firstPoint) {
f278a5
      handler = new BluredStroke(m_workRas, maxThick, m_brushPad, false);
f278a5
      // if the drawOrder mode = "Palette Order",
f278a5
      // get styleId list which is above the current style in the palette
f278a5
      if (drawOrder == PaletteOrder) {
f278a5
        QSet<int> aboveStyleIds;</int>
f278a5
        getAboveStyleIdSet(m_painting.styleId, ri->getPalette(), aboveStyleIds);
f278a5
        handler->brush.setAboveStyleIds(aboveStyleIds);
justburner e250b7
      }
fa009d
      track.handler = handler;
Shinya Kitaoka 120a6e
    }
fa009d
    handler = dynamic_cast<bluredstroke*>(track.handler.getPointer());</bluredstroke*>
f278a5
    if (!handler) return;
f278a5
f278a5
    // paint stroke
fa009d
    double radius = computeThickness(pressure, m_rasThickness);
f278a5
    TThickPoint thickPoint(fixedPosition + rasCenter, radius*2);
f278a5
    TRect strokeRect( tfloor(thickPoint.x - maxThick - 0.999),
f278a5
                      tfloor(thickPoint.y - maxThick - 0.999),
f278a5
                      tceil(thickPoint.x + maxThick + 0.999),
f278a5
                      tceil(thickPoint.y + maxThick + 0.999) );
f278a5
    strokeRect *= ras->getBounds();
f278a5
    m_painting.affectedRect += strokeRect;
f278a5
    updateWorkAndBackupRasters(m_painting.affectedRect);
f278a5
    m_painting.tileSaver->save(strokeRect);
f278a5
    handler->brush.addPoint(thickPoint, 1, !lastPoint);
f278a5
    handler->brush.updateDrawing( ras, m_backupRas, strokeRect,
f278a5
                                  m_painting.styleId, drawOrder,
f278a5
                                  m_modifierLockAlpha.getValue() );
f278a5
    
f278a5
    invalidateRect += convert(strokeRect) - rasCenter;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
f278a5
  // invalidate rect
f278a5
  if (firstTrack) {
f278a5
    // for the first track we will paint cursor circle
f278a5
    // here we will invalidate rects for it
f278a5
    double thickness = m_isMyPaintStyleSelected ? m_maxCursorThick : m_maxThick*0.5;
f278a5
    TPointD thickOffset(thickness + 1, thickness + 1);
f278a5
    invalidateRect += TRectD(m_brushPos    - thickOffset, m_brushPos    + thickOffset);
f278a5
    invalidateRect += TRectD(fixedPosition - thickOffset, fixedPosition + thickOffset);
f278a5
    m_mousePos = point.position;
f278a5
    m_brushPos = fixedPosition;
f278a5
  }
f278a5
 
f278a5
  if (!invalidateRect.isEmpty())
f278a5
    invalidate(invalidateRect.enlarge(2));
Toshihiro Shimizu 890ddd
}
f278a5
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
f278a5
// 明日はここをMyPaintのときにカーソルを消せるように修正する!!!!!!
f278a5
void ToonzRasterBrushTool::inputMouseMove(const TPointD &position, const TInputState &state) {
Shinya Kitaoka 120a6e
  struct Locals {
shun-iwasawa 98926d
    ToonzRasterBrushTool *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
f278a5
    void addMinMax(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
    }
Jeremy Bullock 01e454
  } locals = {this};
Shinya Kitaoka 120a6e
shun-iwasawa 98926d
  double thickness =
shun-iwasawa 98926d
      (m_isMyPaintStyleSelected) ? (double)(m_maxCursorThick + 1) : m_maxThick;
shun-iwasawa 98926d
  TPointD halfThick(thickness * 0.5, thickness * 0.5);
shun-iwasawa 97f86c
  TRectD invalidateRect(m_brushPos - halfThick, m_brushPos + halfThick);
shun-iwasawa 97f86c
f278a5
  if ( Preferences::instance()->useCtrlAltToResizeBrushEnabled()
f278a5
    && state.isKeyPressed(TKey::control)
f278a5
    && state.isKeyPressed(TKey::alt)
f278a5
    && !state.isKeyPressed(TKey::shift) )
f278a5
  {
Martin van Zijl a10e2f
    // Resize the brush if CTRL+ALT is pressed and the preference is enabled.
f278a5
    const TPointD &diff = position - m_mousePos;
Jeremy Bullock 01e454
    double max          = diff.x / 2;
Jeremy Bullock 01e454
    double min          = diff.y / 2;
Jeremy Bullock 01e454
f278a5
    locals.addMinMax(m_rasThickness, min, max);
shun-iwasawa 97f86c
shun-iwasawa 98926d
    double radius = m_rasThickness.getValue().second * 0.5;
shun-iwasawa 97f86c
    invalidateRect += TRectD(m_brushPos - TPointD(radius, radius),
shun-iwasawa 97f86c
                             m_brushPos + TPointD(radius, radius));
shun-iwasawa 97f86c
Jeremy Bullock 01e454
  } else {
f278a5
    m_mousePos = position;
f278a5
    m_brushPos = getCenteredCursorPos(position);
Jeremy Bullock cd00fd
f278a5
    invalidateRect += TRectD(position - halfThick, position + halfThick);
Shinya Kitaoka 120a6e
  }
shun-iwasawa 97f86c
shun-iwasawa 97f86c
  invalidate(invalidateRect.enlarge(2));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_minThick == 0 && m_maxThick == 0) {
shun-iwasawa 98926d
    m_minThick = m_rasThickness.getValue().first;
shun-iwasawa 98926d
    m_maxThick = m_rasThickness.getValue().second;
Jeremy Bullock cd00fd
  }
Jeremy Bullock cd00fd
}
Jeremy Bullock cd00fd
Jeremy Bullock cd00fd
//-------------------------------------------------------------------------------------------------------------
Jeremy Bullock cd00fd
shun-iwasawa 98926d
void ToonzRasterBrushTool::draw() {
f278a5
  m_inputmanager.draw();
justburner e250b7
Shinya Kitaoka 120a6e
  /*--ショートカットでのツール切り替え時に赤点が描かれるのを防止する--*/
Jeremy Bullock 7f2044
  if (m_minThick == 0 && m_maxThick == 0 &&
Jeremy Bullock 7f2044
      !Preferences::instance()->getShow0ThickLines())
Jeremy Bullock 7f2044
    return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TImageP img = getImage(false, 1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (getApplication()->getCurrentObject()->isSpline()) return;
Shinya Kitaoka 120a6e
manongjohn 75da26
  // If toggled off, don't draw brush outline
manongjohn 75da26
  if (!Preferences::instance()->isCursorOutlineEnabled()) return;
manongjohn 75da26
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
shun-iwasawa 98926d
  if (m_isMyPaintStyleSelected) {
shun-iwasawa 98926d
    tglDrawCircle(m_brushPos, (m_minCursorThick + 1) * 0.5);
shun-iwasawa 98926d
    tglDrawCircle(m_brushPos, (m_maxCursorThick + 1) * 0.5);
shun-iwasawa 98926d
    return;
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
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
    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());
shun-iwasawa 98926d
  } else {
shun-iwasawa 97f86c
    drawEmptyCircle(m_brushPos, tround(m_minThick), true, true,
shun-iwasawa 97f86c
                    m_pencil.getValue());
shun-iwasawa 97f86c
    drawEmptyCircle(m_brushPos, tround(m_maxThick), true, true,
shun-iwasawa 97f86c
                    m_pencil.getValue());
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
void ToonzRasterBrushTool::onEnter() {
shun-iwasawa 98926d
  m_minThick = m_rasThickness.getValue().first;
shun-iwasawa 98926d
  m_maxThick = m_rasThickness.getValue().second;
shun-iwasawa 98926d
  updateCurrentStyle();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
void ToonzRasterBrushTool::onLeave() {
shun-iwasawa 98926d
  m_minThick       = 0;
shun-iwasawa 98926d
  m_maxThick       = 0;
shun-iwasawa 98926d
  m_minCursorThick = 0;
shun-iwasawa 98926d
  m_maxCursorThick = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
TPropertyGroup *ToonzRasterBrushTool::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
shun-iwasawa 98926d
void ToonzRasterBrushTool::onImageChanged() {
shun-iwasawa 98926d
  if (!isEnabled()) return;
Shinya Kitaoka 120a6e
  setWorkAndBackupImages();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
void ToonzRasterBrushTool::setWorkAndBackupImages() {
Shinya Kitaoka 120a6e
  TToonzImageP ti = (TToonzImageP)getImage(false, 1);
Shinya Kitaoka 120a6e
  if (!ti) return;
Shinya Kitaoka 120a6e
  TRasterP ras   = ti->getRaster();
Shinya Kitaoka 120a6e
  TDimension dim = ras->getSize();
Shinya Kitaoka 120a6e
f278a5
  if (!m_workRas || m_workRas->getLx() < dim.lx || m_workRas->getLy() < dim.ly)
f278a5
    m_workRas = TRaster32P(dim);
f278a5
  if (!m_backupRas || m_backupRas->getLx() < dim.lx || m_backupRas->getLy() < dim.ly)
f278a5
    m_backupRas = TRasterCM32P(dim);
f278a5
f278a5
  m_workBackupRect.empty();
f278a5
}
f278a5
f278a5
//---------------------------------------------------------------------------------------------------
f278a5
f278a5
void ToonzRasterBrushTool::updateWorkAndBackupRasters(const TRect &rect) {
f278a5
  TToonzImageP ti = TImageP(getImage(false, 1));
f278a5
  if (!ti) return;
f278a5
  TRasterCM32P ras = ti->getRaster();
f278a5
f278a5
  // work and backup rect will additionaly enlarged to 1/8 of it size
f278a5
  // in each affected direction to predict future possible enlargements in this direction
f278a5
  const int denominator = 8;
f278a5
  TRect enlargedRect    = rect + m_workBackupRect;
f278a5
  int dx                = (enlargedRect.getLx() - 1) / denominator + 1;
f278a5
  int dy                = (enlargedRect.getLy() - 1) / denominator + 1;
f278a5
f278a5
  if (m_workBackupRect.isEmpty()) {
f278a5
    enlargedRect.x0 -= dx;
f278a5
    enlargedRect.y0 -= dy;
f278a5
    enlargedRect.x1 += dx;
f278a5
    enlargedRect.y1 += dy;
f278a5
f278a5
    enlargedRect *= ras->getBounds();
f278a5
    if (enlargedRect.isEmpty()) return;
f278a5
f278a5
    m_workRas->extract(enlargedRect)->copy(ras->extract(enlargedRect));
f278a5
    m_backupRas->extract(enlargedRect)->copy(ras->extract(enlargedRect));
Shinya Kitaoka 120a6e
  } else {
f278a5
    if (enlargedRect.x0 < m_workBackupRect.x0) enlargedRect.x0 -= dx;
f278a5
    if (enlargedRect.y0 < m_workBackupRect.y0) enlargedRect.y0 -= dy;
f278a5
    if (enlargedRect.x1 > m_workBackupRect.x1) enlargedRect.x1 += dx;
f278a5
    if (enlargedRect.y1 > m_workBackupRect.y1) enlargedRect.y1 += dy;
f278a5
f278a5
    enlargedRect *= ras->getBounds();
f278a5
    if (enlargedRect.isEmpty()) return;
f278a5
f278a5
    TRect lastRect     = m_workBackupRect * ras->getBounds();
f278a5
    QList<trect> rects = ToolUtils::splitRect(enlargedRect, lastRect);</trect>
f278a5
    for (int i = 0; i < rects.size(); i++) {
f278a5
      m_workRas->extract(rects[i])->copy(ras->extract(rects[i]));
f278a5
      m_backupRas->extract(rects[i])->copy(ras->extract(rects[i]));
f278a5
    }
Shinya Kitaoka 120a6e
  }
f278a5
f278a5
  m_workBackupRect = enlargedRect;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
bool ToonzRasterBrushTool::onPropertyChanged(std::string propertyName) {
manongjohn 5a2268
  if (m_propertyUpdating) return true;
manongjohn 5a2268
manongjohn df5842
  if (propertyName == m_preset.getName()) {
manongjohn df5842
    if (m_preset.getValue() != CUSTOM_WSTR)
manongjohn df5842
      loadPreset();
manongjohn df5842
    else  // Chose <custom>, go back to last saved brush settings</custom>
manongjohn df5842
      loadLastBrush();
manongjohn df5842
manongjohn 5a2268
    RasterBrushPreset  = m_preset.getValueAsString();
manongjohn 5a2268
    m_propertyUpdating = true;
manongjohn df5842
    getApplication()->getCurrentTool()->notifyToolChanged();
manongjohn 5a2268
    m_propertyUpdating = false;
manongjohn df5842
    return true;
manongjohn df5842
  }
Toshihiro Shimizu 890ddd
manongjohn 0dcb3a
  RasterBrushMinSize       = m_rasThickness.getValue().first;
manongjohn 0dcb3a
  RasterBrushMaxSize       = m_rasThickness.getValue().second;
manongjohn 0dcb3a
  BrushSmooth              = m_smooth.getValue();
manongjohn 0dcb3a
  BrushDrawOrder           = m_drawOrder.getIndex();
manongjohn 0dcb3a
  RasterBrushPencilMode    = m_pencil.getValue();
manongjohn 0dcb3a
  BrushPressureSensitivity = m_pressure.getValue();
manongjohn 0dcb3a
  RasterBrushHardness      = m_hardness.getValue();
manongjohn 0dcb3a
  RasterBrushModifierSize  = m_modifierSize.getValue();
manongjohn f936c0
  BrushLockAlpha           = m_modifierLockAlpha.getValue();
f278a5
  RasterBrushAssistants    = m_assistants.getValue();
manongjohn 0dcb3a
manongjohn 0dcb3a
  // Recalculate/reset based on changed settings
shun-iwasawa 98926d
  if (propertyName == m_rasThickness.getName()) {
shun-iwasawa 98926d
    m_minThick = m_rasThickness.getValue().first;
shun-iwasawa 98926d
    m_maxThick = m_rasThickness.getValue().second;
manongjohn 0dcb3a
  }
manongjohn 0dcb3a
shun-iwasawa 98926d
  if (propertyName == m_hardness.getName() ||
shun-iwasawa 98926d
      propertyName == m_rasThickness.getName()) {
shun-iwasawa 98926d
    m_brushPad = getBrushPad(m_rasThickness.getValue().second,
shun-iwasawa 98926d
                             m_hardness.getValue() * 0.01);
shun-iwasawa 98926d
    TRectD rect(m_mousePos - TPointD(m_maxThick + 2, m_maxThick + 2),
shun-iwasawa 98926d
                m_mousePos + TPointD(m_maxThick + 2, m_maxThick + 2));
shun-iwasawa 98926d
    invalidate(rect);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
manongjohn df5842
  if (m_preset.getValue() != CUSTOM_WSTR) {
Shinya Kitaoka 120a6e
    m_preset.setValue(CUSTOM_WSTR);
manongjohn 5a2268
    RasterBrushPreset  = m_preset.getValueAsString();
manongjohn 5a2268
    m_propertyUpdating = true;
manongjohn df5842
    getApplication()->getCurrentTool()->notifyToolChanged();
manongjohn 5a2268
    m_propertyUpdating = false;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
void ToonzRasterBrushTool::initPresets() {
Shinya Kitaoka 120a6e
  if (!m_presetsLoaded) {
Shinya Kitaoka 120a6e
    // If necessary, load the presets from file
Shinya Kitaoka 120a6e
    m_presetsLoaded = true;
shun-iwasawa 98926d
    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);
shun-iwasawa e87e08
  m_preset.setItemUIName(CUSTOM_WSTR, tr("<custom>"));</custom>
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
shun-iwasawa 98926d
void ToonzRasterBrushTool::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
  {
shun-iwasawa 98926d
    m_rasThickness.setValue(
shun-iwasawa 98926d
        TDoublePairProperty::Value(std::max(preset.m_min, 1.0), preset.m_max));
shun-iwasawa 98926d
    m_hardness.setValue(preset.m_hardness, true);
manongjohn 0dcb3a
    m_smooth.setValue(preset.m_smooth, true);
shun-iwasawa 98926d
    m_drawOrder.setIndex(preset.m_drawOrder);
shun-iwasawa 98926d
    m_pencil.setValue(preset.m_pencil);
shun-iwasawa 98926d
    m_pressure.setValue(preset.m_pressure);
shun-iwasawa 98926d
    m_modifierSize.setValue(preset.m_modifierSize);
manongjohn f936c0
    m_modifierLockAlpha.setValue(preset.m_modifierLockAlpha);
f278a5
    m_assistants.setValue(preset.m_assistants);
shun-iwasawa 98926d
manongjohn 0dcb3a
    // Recalculate based on updated presets
manongjohn 0dcb3a
    m_minThick = m_rasThickness.getValue().first;
manongjohn 0dcb3a
    m_maxThick = m_rasThickness.getValue().second;
manongjohn 0dcb3a
manongjohn 0dcb3a
    m_brushPad = ToolUtils::getBrushPad(preset.m_max, preset.m_hardness * 0.01);
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
void ToonzRasterBrushTool::addPreset(QString name) {
Shinya Kitaoka 120a6e
  // Build the preset
Shinya Kitaoka 120a6e
  BrushData preset(name.toStdWString());
Shinya Kitaoka 120a6e
shun-iwasawa 98926d
  preset.m_min = m_rasThickness.getValue().first;
shun-iwasawa 98926d
  preset.m_max = m_rasThickness.getValue().second;
Shinya Kitaoka 120a6e
manongjohn f936c0
  preset.m_smooth            = m_smooth.getValue();
manongjohn f936c0
  preset.m_hardness          = m_hardness.getValue();
manongjohn f936c0
  preset.m_drawOrder         = m_drawOrder.getIndex();
manongjohn f936c0
  preset.m_pencil            = m_pencil.getValue();
manongjohn f936c0
  preset.m_pressure          = m_pressure.getValue();
manongjohn f936c0
  preset.m_modifierSize      = m_modifierSize.getValue();
manongjohn f936c0
  preset.m_modifierLockAlpha = m_modifierLockAlpha.getValue();
f278a5
  preset.m_assistants        = m_assistants.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);
manongjohn 8b8d41
  RasterBrushPreset = m_preset.getValueAsString();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
void ToonzRasterBrushTool::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);
manongjohn 8b8d41
  RasterBrushPreset = m_preset.getValueAsString();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
manongjohn df5842
manongjohn df5842
void ToonzRasterBrushTool::loadLastBrush() {
manongjohn df5842
  m_rasThickness.setValue(
manongjohn df5842
      TDoublePairProperty::Value(RasterBrushMinSize, RasterBrushMaxSize));
manongjohn df5842
  m_drawOrder.setIndex(BrushDrawOrder);
manongjohn df5842
  m_pencil.setValue(RasterBrushPencilMode ? 1 : 0);
manongjohn df5842
  m_hardness.setValue(RasterBrushHardness);
manongjohn df5842
  m_pressure.setValue(BrushPressureSensitivity ? 1 : 0);
manongjohn df5842
  m_smooth.setValue(BrushSmooth);
manongjohn df5842
  m_modifierSize.setValue(RasterBrushModifierSize);
manongjohn f936c0
  m_modifierLockAlpha.setValue(BrushLockAlpha ? 1 : 0);
f278a5
  m_assistants.setValue(RasterBrushAssistants ? 1 : 0);
manongjohn 0dcb3a
manongjohn 0dcb3a
  // Recalculate based on prior values
manongjohn 0dcb3a
  m_minThick = m_rasThickness.getValue().first;
manongjohn 0dcb3a
  m_maxThick = m_rasThickness.getValue().second;
manongjohn 0dcb3a
manongjohn 0dcb3a
  m_brushPad = getBrushPad(m_rasThickness.getValue().second,
manongjohn 0dcb3a
                           m_hardness.getValue() * 0.01);
manongjohn df5842
}
manongjohn df5842
manongjohn df5842
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
/*!	Brush、PaintBrush、EraserToolがPencilModeのときにTrueを返す
shun-iwasawa 76d093
 */
shun-iwasawa 98926d
bool ToonzRasterBrushTool::isPencilModeActive() {
Shinya Kitaoka 120a6e
  return getTargetType() == TTool::ToonzImage && m_pencil.getValue();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
//------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzRasterBrushTool::onColorStyleChanged() {
f278a5
  m_inputmanager.finishTracks();
f278a5
  m_enabled = false;
shun-iwasawa 98926d
shun-iwasawa 98926d
  TTool::Application *app = getApplication();
shun-iwasawa 98926d
  TMyPaintBrushStyle *mpbs =
shun-iwasawa 98926d
      dynamic_cast<tmypaintbrushstyle *="">(app->getCurrentLevelStyle());</tmypaintbrushstyle>
shun-iwasawa 98926d
  m_isMyPaintStyleSelected = (mpbs) ? true : false;
shun-iwasawa 98926d
  getApplication()->getCurrentTool()->notifyToolChanged();
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
double ToonzRasterBrushTool::restartBrushTimer() {
shun-iwasawa 98926d
  double dtime = m_brushTimer.nsecsElapsed() * 1e-9;
shun-iwasawa 98926d
  m_brushTimer.restart();
shun-iwasawa 98926d
  return dtime;
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
void ToonzRasterBrushTool::updateCurrentStyle() {
shun-iwasawa 98926d
  if (m_isMyPaintStyleSelected) {
shun-iwasawa 98926d
    TTool::Application *app = TTool::getApplication();
shun-iwasawa 98926d
    TMyPaintBrushStyle *brushStyle =
shun-iwasawa 98926d
        dynamic_cast<tmypaintbrushstyle *="">(app->getCurrentLevelStyle());</tmypaintbrushstyle>
manongjohn b5dd39
    if (!brushStyle) {
manongjohn b5dd39
      // brush changed to normal abnormally. Complete color style change.
manongjohn b5dd39
      onColorStyleChanged();
manongjohn b5dd39
      return;
manongjohn b5dd39
    }
shun-iwasawa 98926d
    double radiusLog = brushStyle->getBrush().getBaseValue(
shun-iwasawa 98926d
                           MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC) +
shun-iwasawa 98926d
                       m_modifierSize.getValue() * log(2.0);
shun-iwasawa 98926d
    double radius    = exp(radiusLog);
shun-iwasawa 98926d
    m_minCursorThick = m_maxCursorThick = (int)std::round(2.0 * radius);
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
}
shun-iwasawa 98926d
//==========================================================================================================
shun-iwasawa 98926d
shun-iwasawa 98926d
ToonzRasterBrushToolNotifier::ToonzRasterBrushToolNotifier(
shun-iwasawa 98926d
    ToonzRasterBrushTool *tool)
shun-iwasawa 98926d
    : m_tool(tool) {
shun-iwasawa 98926d
  if (TTool::Application *app = m_tool->getApplication()) {
shun-iwasawa 98926d
    if (TPaletteHandle *paletteHandle = app->getCurrentPalette()) {
shun-iwasawa 98926d
      bool ret;
shun-iwasawa 98926d
      ret = connect(paletteHandle, SIGNAL(colorStyleChanged(bool)), this,
shun-iwasawa 98926d
                    SLOT(onColorStyleChanged()));
shun-iwasawa e93284
      ret = ret && connect(paletteHandle, SIGNAL(colorStyleSwitched()), this,
shun-iwasawa e93284
                           SLOT(onColorStyleChanged()));
shun-iwasawa e93284
      ret = ret && connect(paletteHandle, SIGNAL(paletteSwitched()), this,
shun-iwasawa e93284
                           SLOT(onColorStyleChanged()));
shun-iwasawa 98926d
      assert(ret);
shun-iwasawa 98926d
    }
shun-iwasawa 98926d
  }
shun-iwasawa 98926d
  onColorStyleChanged();
shun-iwasawa 98926d
}
shun-iwasawa 98926d
Toshihiro Shimizu 890ddd
//==========================================================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Tools instantiation
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
ToonzRasterBrushTool toonzPencil("T_Brush",
shun-iwasawa 98926d
                                 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)
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_pencil(false)
Shinya Kitaoka 120a6e
    , m_pressure(false)
9380d5
    , m_drawOrder(0)
f278a5
    , m_modifierSize(0.0)
572ed1
    , m_modifierOpacity(0.0)
572ed1
    , m_modifierEraser(0.0)
9380d5
    , m_modifierLockAlpha(0.0)
9380d5
    , m_assistants(false) {}
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)
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_pencil(false)
Shinya Kitaoka 120a6e
    , m_pressure(false)
9380d5
    , m_drawOrder(0)
bf1d82
    , m_modifierSize(0.0)
572ed1
    , m_modifierOpacity(0.0)
572ed1
    , m_modifierEraser(0.0)
9380d5
    , m_modifierLockAlpha(0.0)
9380d5
    , m_assistants(false) {}
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();
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();
shun-iwasawa 975eb1
  os.openChild("Draw_Order");
shun-iwasawa 975eb1
  os << m_drawOrder;
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("Pressure_Sensitivity");
Shinya Kitaoka 120a6e
  os << (int)m_pressure;
Shinya Kitaoka 120a6e
  os.closeChild();
bf1d82
  os.openChild("Modifier_Size");
bf1d82
  os << m_modifierSize;
bf1d82
  os.closeChild();
bf1d82
  os.openChild("Modifier_Opacity");
bf1d82
  os << m_modifierOpacity;
bf1d82
  os.closeChild();
572ed1
  os.openChild("Modifier_Eraser");
572ed1
  os << (int)m_modifierEraser;
572ed1
  os.closeChild();
572ed1
  os.openChild("Modifier_LockAlpha");
572ed1
  os << (int)m_modifierLockAlpha;
572ed1
  os.closeChild();
9380d5
  os.openChild("Assistants");
9380d5
  os << (int)m_assistants;
9380d5
  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();
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();
shun-iwasawa 975eb1
    else if (tagName == "Selective" ||
shun-iwasawa 975eb1
             tagName == "Draw_Order")  // "Selective" is left to keep backward
shun-iwasawa 975eb1
                                       // compatibility
shun-iwasawa 975eb1
      is >> m_drawOrder, 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 == "Pressure_Sensitivity")
Shinya Kitaoka 120a6e
      is >> val, m_pressure = val, is.matchEndTag();
bf1d82
    else if (tagName == "Modifier_Size")
bf1d82
      is >> m_modifierSize, is.matchEndTag();
bf1d82
    else if (tagName == "Modifier_Opacity")
bf1d82
      is >> m_modifierOpacity, is.matchEndTag();
572ed1
    else if (tagName == "Modifier_Eraser")
572ed1
      is >> val, m_modifierEraser = val, is.matchEndTag();
572ed1
    else if (tagName == "Modifier_LockAlpha")
572ed1
      is >> val, m_modifierLockAlpha = val, is.matchEndTag();
9380d5
    else if (tagName == "Assistants")
9380d5
      is >> val, m_assistants = val, 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
}