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"
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);
0d5555
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)
7d1790
      , 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();
7d1790
    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);
7d1790
      rasterTrack.setAboveStyleIds(aboveStyleIds);
shun-iwasawa 975eb1
    }
7d1790
    rasterTrack.setPointsSequence(m_points);
7d1790
    rasterTrack.generateStroke(m_isPencil, m_isStraight);
Shinya Kitaoka 120a6e
    image->setSavebox(image->getSavebox() +
7d1790
                      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)
7d1790
      , 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)
7d1790
    , m_modifierLockAlpha("Lock Alpha", false)
7d1790
    , m_assistants("Assistants", true)
7d1790
    , 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)
7d1790
{
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);
7d1790
  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");
7d1790
7d1790
  m_inputmanager.setHandler(this);
7d1790
  m_modifierLine               = new TModifierLine();
7d1790
  m_modifierTangents           = new TModifierTangents();
7d1790
  m_modifierAssistants         = new TModifierAssistants();
7d1790
  m_modifierSegmentation       = new TModifierSegmentation();
7d1790
  m_modifierSmoothSegmentation = new TModifierSegmentation(TPointD(1, 1), 3);
7d1790
  for(int i = 0; i < 3; ++i)
7d1790
    m_modifierSmooth[i]        = new TModifierSmooth();
7d1790
#ifndef NDEBUG
7d1790
  m_modifierTest = new TModifierTest(5, 40);
7d1790
#endif
7d1790
7d1790
  m_inputmanager.addModifier(
7d1790
      TInputModifierP(m_modifierAssistants.getPointer()));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
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 {
7d1790
    pos.x = floor(pos.x) + 0.5;
7d1790
    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"));
7d1790
  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
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() {
7d1790
  m_inputmanager.finishTracks();
7d1790
  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;
7d1790
  m_painting.myPaint.strokeSegmentRect += rect;
shun-iwasawa 98926d
  updateWorkAndBackupRasters(rect);
7d1790
  m_painting.tileSaver->save(rect);
shun-iwasawa 98926d
  return true;
shun-iwasawa 98926d
}
shun-iwasawa 98926d
shun-iwasawa 98926d
//--------------------------------------------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
bool ToonzRasterBrushTool::preLeftButtonDown() {
7d1790
  int smoothRadius = (int)round(m_smooth.getValue());
0d5555
  m_modifierAssistants->drawOnly = !m_assistants.getValue();
7d1790
  m_inputmanager.drawPreview     = false; //!m_modifierAssistants->drawOnly;
7d1790
7d1790
  m_inputmanager.clearModifiers();
7d1790
  m_inputmanager.addModifier(TInputModifierP(m_modifierTangents.getPointer()));
7d1790
  if (smoothRadius > 0) {
7d1790
    m_inputmanager.addModifier(TInputModifierP(m_modifierSmoothSegmentation.getPointer()));
7d1790
    for(int i = 0; i < 3; ++i) {
7d1790
      m_modifierSmooth[i]->setRadius(smoothRadius);
7d1790
      m_inputmanager.addModifier(TInputModifierP(m_modifierSmooth[i].getPointer()));
7d1790
    }
7d1790
  }
7d1790
  m_inputmanager.addModifier(TInputModifierP(m_modifierAssistants.getPointer()));
7d1790
#ifndef NDEBUG
7d1790
  m_inputmanager.addModifier(TInputModifierP(m_modifierTest.getPointer()));
7d1790
#endif
7d1790
  m_inputmanager.addModifier(TInputModifierP(m_modifierSegmentation.getPointer()));
7d1790
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
7d1790
//---------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
7d1790
void ToonzRasterBrushTool::handleMouseEvent(MouseEventType type,
7d1790
                                          const TPointD &pos,
shun-iwasawa 98926d
                                          const TMouseEvent &e) {
7d1790
  TTimerTicks t = TToolTimer::ticks();
7d1790
  bool alt      = e.getModifiersMask() & TMouseEvent::ALT_KEY;
7d1790
  bool shift    = e.getModifiersMask() & TMouseEvent::SHIFT_KEY;
7d1790
  bool control  = e.getModifiersMask() & TMouseEvent::CTRL_KEY;
7d1790
7d1790
  if (shift && type == ME_DOWN && e.button() == Qt::LeftButton && !m_painting.active) {
7d1790
    m_modifierAssistants->drawOnly = true;
7d1790
    m_inputmanager.clearModifiers();
7d1790
    m_inputmanager.addModifier(TInputModifierP(m_modifierLine.getPointer()));
7d1790
    m_inputmanager.addModifier(TInputModifierP(m_modifierAssistants.getPointer()));
7d1790
    m_inputmanager.addModifier(TInputModifierP(m_modifierSegmentation.getPointer()));
7d1790
    m_inputmanager.drawPreview = true;
justburner e250b7
  }
justburner e250b7
7d1790
  if (alt != m_inputmanager.state.isKeyPressed(TKey::alt))
7d1790
    m_inputmanager.keyEvent(alt, TKey::alt, t, nullptr);
7d1790
  if (shift != m_inputmanager.state.isKeyPressed(TKey::shift))
7d1790
    m_inputmanager.keyEvent(shift, TKey::shift, t, nullptr);
7d1790
  if (control != m_inputmanager.state.isKeyPressed(TKey::control))
7d1790
    m_inputmanager.keyEvent(control, TKey::control, t, nullptr);
shun-iwasawa 98926d
7d1790
  if (type == ME_MOVE) {
7d1790
    THoverList hovers(1, pos);
7d1790
    m_inputmanager.hoverEvent(hovers);
7d1790
  } else {
7d1790
    m_inputmanager.trackEvent(e.isTablet(), 0, pos,
7d1790
                              e.isTablet() ? &e.m_pressure : nullptr, nullptr,
7d1790
                              type == ME_UP, t);
7d1790
    m_inputmanager.processTracks();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
7d1790
//---------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
7d1790
void ToonzRasterBrushTool::leftButtonDown(const TPointD &pos,
7d1790
                                        const TMouseEvent &e) {
7d1790
  handleMouseEvent(ME_DOWN, pos, e);
7d1790
}
shun-iwasawa 98926d
void ToonzRasterBrushTool::leftButtonDrag(const TPointD &pos,
7d1790
                                        const TMouseEvent &e) {
7d1790
  handleMouseEvent(ME_DRAG, pos, e);
7d1790
}
7d1790
void ToonzRasterBrushTool::leftButtonUp(const TPointD &pos,
7d1790
                                      const TMouseEvent &e) {
7d1790
  handleMouseEvent(ME_UP, pos, e);
7d1790
}
7d1790
void ToonzRasterBrushTool::mouseMove(const TPointD &pos, const TMouseEvent &e) {
7d1790
  handleMouseEvent(ME_MOVE, pos, e);
7d1790
}
Toshihiro Shimizu 890ddd
7d1790
//--------------------------------------------------------------------------------------------------
justburner 65e3a5
7d1790
void ToonzRasterBrushTool::inputSetBusy(bool busy) {
7d1790
  if (m_painting.active == busy)
justburner e250b7
    return;
7d1790
  
7d1790
  if (busy) {
7d1790
    // begin painting
7d1790
    
7d1790
    TTool::Application *app = TTool::getApplication();
7d1790
    if (!app) return;
justburner e250b7
7d1790
    int col   = app->getCurrentColumn()->getColumnIndex();
7d1790
    m_enabled = col >= 0 || app->getCurrentFrame()->isEditingLevel();
7d1790
    // todo: gestire autoenable
7d1790
    if (!m_enabled) return;
7d1790
7d1790
    m_painting.active = !!getImage(true);
7d1790
    if (!m_painting.active)
7d1790
      m_painting.active = !!touchImage();
7d1790
    if (!m_painting.active)
7d1790
      return;
7d1790
    
7d1790
    // nel caso che il colore corrente sia un cleanup/studiopalette color
7d1790
    // oppure il colore di un colorfield
7d1790
    updateCurrentStyle();
7d1790
    if (TColorStyle *cs = app->getCurrentLevelStyle()) {
7d1790
      m_painting.styleId = app->getCurrentLevelStyleIndex();
7d1790
      TRasterStyleFx *rfx = cs->getRasterStyleFx();
7d1790
      if (!cs->isStrokeStyle() && (!rfx || !rfx->isInkStyle()))
7d1790
          { m_painting.active = false; return; }
Jeremy Bullock 7f2044
    } else {
7d1790
      m_painting.styleId = 1;
Jeremy Bullock 7f2044
    }
7d1790
    
7d1790
    m_painting.frameId = getFrameId();
7d1790
7d1790
    TImageP img = getImage(true);
7d1790
    TToonzImageP ri(img);
7d1790
    TRasterCM32P ras = ri->getRaster();
7d1790
    if (!ras)
7d1790
      { m_painting.active = false; return; }
7d1790
      
7d1790
    m_painting.tileSet   = new TTileSetCM32(ras->getSize());
7d1790
    m_painting.tileSaver = new TTileSaverCM32(ras, m_painting.tileSet);
7d1790
    m_painting.affectedRect.empty();
7d1790
    setWorkAndBackupImages();
7d1790
    
7d1790
    if (m_isMyPaintStyleSelected) {
7d1790
      // init myPaint drawing
7d1790
      
7d1790
      m_painting.myPaint.isActive = true;
7d1790
      m_painting.myPaint.strokeSegmentRect.empty();
7d1790
      
7d1790
      m_workRas->lock();
7d1790
      
7d1790
      // prepare base brush
7d1790
      if ( TMyPaintBrushStyle *mypaintStyle = dynamic_cast<tmypaintbrushstyle *="">(app->getCurrentLevelStyle()) )</tmypaintbrushstyle>
7d1790
        m_painting.myPaint.baseBrush.fromBrush( mypaintStyle->getBrush() ); else
7d1790
          m_painting.myPaint.baseBrush.fromDefaults();
7d1790
      double modifierSize = m_modifierSize.getValue() * log(2.0);
7d1790
      float baseSize = m_painting.myPaint.baseBrush.getBaseValue( MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC );
7d1790
      m_painting.myPaint.baseBrush.setBaseValue( MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC, baseSize + modifierSize );
7d1790
    } else
7d1790
    if (m_hardness.getValue() == 100 || m_pencil.getValue()) {
7d1790
      // init pencil drawing
7d1790
      
7d1790
      m_painting.pencil.isActive = true;
7d1790
      m_painting.pencil.realPencil = m_pencil.getValue();
7d1790
    } else {
7d1790
      // init blured brush drawing (regular drawing)
7d1790
      
7d1790
      m_painting.blured.isActive = true;
Jeremy Bullock cd00fd
    }
7d1790
    
shun-iwasawa 98926d
  } else {
7d1790
    // finish painting
7d1790
    
7d1790
    if (m_painting.myPaint.isActive) {
7d1790
      // finish myPaint drawing
7d1790
      m_workRas->unlock();
Shinya Kitaoka 120a6e
    }
Jeremy Bullock cd00fd
7d1790
    delete m_painting.tileSaver;
7d1790
    m_painting.tileSaver = nullptr;
7d1790
7d1790
    // add undo record
7d1790
    TFrameId frameId = m_painting.frameId.isEmptyFrame() ? getCurrentFid() : m_painting.frameId;
7d1790
    if (m_painting.tileSet->getTileCount() > 0) {
7d1790
      TTool::Application *app   = TTool::getApplication();
7d1790
      TXshLevel *level          = app->getCurrentLevel()->getLevel();
7d1790
      TXshSimpleLevelP simLevel = level->getSimpleLevel();
7d1790
      TRasterCM32P ras          = TToonzImageP( getImage(true) )->getRaster();
7d1790
      TRasterCM32P subras       = ras->extract(m_painting.affectedRect)->clone();
7d1790
      TUndoManager::manager()->add(new MyPaintBrushUndo(
7d1790
          m_painting.tileSet, simLevel.getPointer(), frameId, m_isFrameCreated,
7d1790
          m_isLevelCreated, subras, m_painting.affectedRect.getP00()));
7d1790
      // MyPaintBrushUndo will delete tileSet by it self
7d1790
    } else {
7d1790
      // delete tileSet here because MyPaintBrushUndo will not do it
7d1790
      delete m_painting.tileSet;
Jeremy Bullock cd00fd
    }
7d1790
    m_painting.tileSet = nullptr;
Jeremy Bullock cd00fd
7d1790
    /*-- 作業中のフレームをリセット --*/
7d1790
    m_painting.frameId = TFrameId();
Jeremy Bullock cd00fd
walkerka 57fb3c
7d1790
    m_painting.myPaint.isActive = false;
7d1790
    m_painting.pencil.isActive = false;
7d1790
    m_painting.blured.isActive = false;
7d1790
    m_painting.active = false;
7d1790
    
7d1790
    /*-- FIdを指定して、描画中にフレームが動いても、
7d1790
      描画開始時のFidのサムネイルが更新されるようにする。--*/
7d1790
    notifyImageChanged(frameId);
7d1790
    ToolUtils::updateSaveBox();
walkerka 22f456
  }
walkerka 57fb3c
}
walkerka 57fb3c
7d1790
//--------------------------------------------------------------------------------------------------
Shinya Kitaoka 120a6e
7d1790
void ToonzRasterBrushTool::inputPaintTrackPoint(const TTrackPoint &point, const TTrack &track, bool firstTrack, bool preview) {
7d1790
  if (!m_painting.active || preview)
7d1790
    return;
7d1790
  
7d1790
  TImageP img = getImage(true);
7d1790
  TToonzImageP ri(img);
7d1790
  TRasterCM32P ras = ri->getRaster();
7d1790
  if (!ras)
7d1790
    return;
7d1790
  TPointD rasCenter = ras->getCenterD();
7d1790
  TPointD fixedPosition = getCenteredCursorPos(point.position);
7d1790
  
walkerka 22f456
7d1790
  TRectD invalidateRect;
7d1790
  bool firstPoint = track.size() == track.pointsAdded;
7d1790
  bool lastPoint  = track.pointsAdded == 1 && track.finished();
7d1790
  
7d1790
  // first point must be without handler, following points must be with handler
7d1790
  // other behaviour is possible bug and must be ignored
7d1790
  assert(firstPoint == !track.toolHandler);
7d1790
  if (firstPoint != !track.toolHandler)
7d1790
    return;
7d1790
  
7d1790
  if (m_painting.myPaint.isActive) {
7d1790
    // mypaint case
7d1790
    
7d1790
    // init brush
7d1790
    MyPaintStroke *handler;
7d1790
    if (firstPoint) {
7d1790
      handler = new MyPaintStroke(m_workRas, *this, m_painting.myPaint.baseBrush, false);
7d1790
      handler->brush.beginStroke();
7d1790
      track.toolHandler = handler;
Jeremy Bullock 6c7e54
    }
7d1790
    handler = dynamic_cast<mypaintstroke*>(track.toolHandler.getPointer());</mypaintstroke*>
7d1790
    if (!handler) return;
7d1790
    
7d1790
    // paint stroke
7d1790
    m_painting.myPaint.strokeSegmentRect.empty();
7d1790
    handler->brush.strokeTo( fixedPosition + rasCenter, point.pressure,
7d1790
                             point.tilt, point.time - track.previous().time );
7d1790
    if (lastPoint)
7d1790
      handler->brush.endStroke();
7d1790
    
7d1790
    // update affected area
7d1790
    TRect updateRect = m_painting.myPaint.strokeSegmentRect * ras->getBounds();
7d1790
    m_painting.affectedRect += updateRect;
7d1790
    if (!updateRect.isEmpty())
7d1790
      handler->brush.updateDrawing( ras, m_backupRas, m_painting.myPaint.strokeSegmentRect,
7d1790
                                    m_painting.styleId, m_modifierLockAlpha.getValue() );
7d1790
    
7d1790
    // determine invalidate rect
7d1790
    invalidateRect += convert(m_painting.myPaint.strokeSegmentRect) - rasCenter;
7d1790
  } else
7d1790
  if (m_painting.pencil.isActive) {
7d1790
    // pencil case
7d1790
    
7d1790
    // Pencilモードでなく、Hardness=100 の場合のブラシサイズを1段階下げる
7d1790
    double thickness = computeThickness(point.pressure, m_rasThickness)*2;
7d1790
    //if (!m_painting.pencil.realPencil && !m_modifierLine->getManager())
7d1790
    //  thickness -= 1.0;
7d1790
    TThickPoint thickPoint(fixedPosition + rasCenter, thickness);
7d1790
7d1790
    // init brush
7d1790
    PencilStroke *handler;
7d1790
    if (firstPoint) {
7d1790
      DrawOrder drawOrder = (DrawOrder)m_drawOrder.getIndex();
7d1790
      handler = new PencilStroke( ras, BRUSH, NONE, m_painting.styleId, thickPoint, drawOrder != OverAll, 0,
7d1790
                                  m_modifierLockAlpha.getValue(), !m_painting.pencil.realPencil,
7d1790
                                  drawOrder == PaletteOrder );
7d1790
      
7d1790
      // if the drawOrder mode = "Palette Order",
7d1790
      // get styleId list which is above the current style in the palette
7d1790
      if (drawOrder == PaletteOrder) {
7d1790
        QSet<int> aboveStyleIds;</int>
7d1790
        getAboveStyleIdSet(m_painting.styleId, ri->getPalette(), aboveStyleIds);
7d1790
        handler->brush.setAboveStyleIds(aboveStyleIds);
walkerka 22f456
      }
7d1790
      track.toolHandler = handler;
shun-iwasawa cd7d6a
    }
7d1790
    handler = dynamic_cast<pencilstroke*>(track.toolHandler.getPointer());</pencilstroke*>
7d1790
    if (!handler) return;
7d1790
7d1790
    // paint stroke
7d1790
    if (!firstPoint)
7d1790
      handler->brush.add(thickPoint);
7d1790
    
7d1790
    // update affected area
7d1790
    TRect strokeRect = handler->brush.getLastRect() * ras->getBounds();
7d1790
    m_painting.tileSaver->save( strokeRect );
7d1790
    m_painting.affectedRect += strokeRect;
7d1790
    handler->brush.generateLastPieceOfStroke( m_painting.pencil.realPencil );
7d1790
    
7d1790
    // determine invalidate rect
7d1790
    invalidateRect += convert(strokeRect) - rasCenter;
7d1790
  } else
7d1790
  if (m_painting.blured.isActive) {
7d1790
    // blured brush case (aka regular brush)
7d1790
7d1790
    double maxThick = m_rasThickness.getValue().second;
7d1790
    DrawOrder drawOrder = (DrawOrder)m_drawOrder.getIndex();
7d1790
    
7d1790
    // init brush
7d1790
    BluredStroke *handler;
7d1790
    if (firstPoint) {
7d1790
      handler = new BluredStroke(m_workRas, maxThick, m_brushPad, false);
7d1790
      // if the drawOrder mode = "Palette Order",
7d1790
      // get styleId list which is above the current style in the palette
7d1790
      if (drawOrder == PaletteOrder) {
7d1790
        QSet<int> aboveStyleIds;</int>
7d1790
        getAboveStyleIdSet(m_painting.styleId, ri->getPalette(), aboveStyleIds);
7d1790
        handler->brush.setAboveStyleIds(aboveStyleIds);
justburner e250b7
      }
7d1790
      track.toolHandler = handler;
Shinya Kitaoka 120a6e
    }
7d1790
    handler = dynamic_cast<bluredstroke*>(track.toolHandler.getPointer());</bluredstroke*>
7d1790
    if (!handler) return;
7d1790
7d1790
    // paint stroke
7d1790
    double radius = computeThickness(point.pressure, m_rasThickness);
7d1790
    TThickPoint thickPoint(fixedPosition + rasCenter, radius*2);
7d1790
    TRect strokeRect( tfloor(thickPoint.x - maxThick - 0.999),
7d1790
                      tfloor(thickPoint.y - maxThick - 0.999),
7d1790
                      tceil(thickPoint.x + maxThick + 0.999),
7d1790
                      tceil(thickPoint.y + maxThick + 0.999) );
7d1790
    strokeRect *= ras->getBounds();
7d1790
    m_painting.affectedRect += strokeRect;
7d1790
    updateWorkAndBackupRasters(m_painting.affectedRect);
7d1790
    m_painting.tileSaver->save(strokeRect);
7d1790
    handler->brush.addPoint(thickPoint, 1, !lastPoint);
7d1790
    handler->brush.updateDrawing( ras, m_backupRas, strokeRect,
7d1790
                                  m_painting.styleId, drawOrder,
7d1790
                                  m_modifierLockAlpha.getValue() );
7d1790
    
7d1790
    invalidateRect += convert(strokeRect) - rasCenter;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
7d1790
  // invalidate rect
7d1790
  if (firstTrack) {
7d1790
    // for the first track we will paint cursor circle
7d1790
    // here we will invalidate rects for it
7d1790
    double thickness = m_isMyPaintStyleSelected ? m_maxCursorThick : m_maxThick*0.5;
7d1790
    TPointD thickOffset(thickness + 1, thickness + 1);
7d1790
    invalidateRect += TRectD(m_brushPos    - thickOffset, m_brushPos    + thickOffset);
7d1790
    invalidateRect += TRectD(fixedPosition - thickOffset, fixedPosition + thickOffset);
7d1790
    m_mousePos = point.position;
7d1790
    m_brushPos = fixedPosition;
7d1790
  }
7d1790
 
7d1790
  if (!invalidateRect.isEmpty())
7d1790
    invalidate(invalidateRect.enlarge(2));
Toshihiro Shimizu 890ddd
}
7d1790
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
7d1790
// 明日はここをMyPaintのときにカーソルを消せるように修正する!!!!!!
7d1790
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
7d1790
    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
7d1790
  if ( Preferences::instance()->useCtrlAltToResizeBrushEnabled()
7d1790
    && state.isKeyPressed(TKey::control)
7d1790
    && state.isKeyPressed(TKey::alt)
7d1790
    && !state.isKeyPressed(TKey::shift) )
7d1790
  {
Martin van Zijl a10e2f
    // Resize the brush if CTRL+ALT is pressed and the preference is enabled.
7d1790
    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
7d1790
    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 {
7d1790
    m_mousePos = position;
7d1790
    m_brushPos = getCenteredCursorPos(position);
Jeremy Bullock cd00fd
7d1790
    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() {
7d1790
  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
7d1790
  if (!m_workRas || m_workRas->getLx() < dim.lx || m_workRas->getLy() < dim.ly)
7d1790
    m_workRas = TRaster32P(dim);
7d1790
  if (!m_backupRas || m_backupRas->getLx() < dim.lx || m_backupRas->getLy() < dim.ly)
7d1790
    m_backupRas = TRasterCM32P(dim);
7d1790
7d1790
  m_workBackupRect.empty();
7d1790
}
7d1790
7d1790
//---------------------------------------------------------------------------------------------------
7d1790
7d1790
void ToonzRasterBrushTool::updateWorkAndBackupRasters(const TRect &rect) {
7d1790
  TToonzImageP ti = TImageP(getImage(false, 1));
7d1790
  if (!ti) return;
7d1790
  TRasterCM32P ras = ti->getRaster();
7d1790
7d1790
  // work and backup rect will additionaly enlarged to 1/8 of it size
7d1790
  // in each affected direction to predict future possible enlargements in this direction
7d1790
  const int denominator = 8;
7d1790
  TRect enlargedRect    = rect + m_workBackupRect;
7d1790
  int dx                = (enlargedRect.getLx() - 1) / denominator + 1;
7d1790
  int dy                = (enlargedRect.getLy() - 1) / denominator + 1;
7d1790
7d1790
  if (m_workBackupRect.isEmpty()) {
7d1790
    enlargedRect.x0 -= dx;
7d1790
    enlargedRect.y0 -= dy;
7d1790
    enlargedRect.x1 += dx;
7d1790
    enlargedRect.y1 += dy;
7d1790
7d1790
    enlargedRect *= ras->getBounds();
7d1790
    if (enlargedRect.isEmpty()) return;
7d1790
7d1790
    m_workRas->extract(enlargedRect)->copy(ras->extract(enlargedRect));
7d1790
    m_backupRas->extract(enlargedRect)->copy(ras->extract(enlargedRect));
Shinya Kitaoka 120a6e
  } else {
7d1790
    if (enlargedRect.x0 < m_workBackupRect.x0) enlargedRect.x0 -= dx;
7d1790
    if (enlargedRect.y0 < m_workBackupRect.y0) enlargedRect.y0 -= dy;
7d1790
    if (enlargedRect.x1 > m_workBackupRect.x1) enlargedRect.x1 += dx;
7d1790
    if (enlargedRect.y1 > m_workBackupRect.y1) enlargedRect.y1 += dy;
7d1790
7d1790
    enlargedRect *= ras->getBounds();
7d1790
    if (enlargedRect.isEmpty()) return;
7d1790
7d1790
    TRect lastRect     = m_workBackupRect * ras->getBounds();
7d1790
    QList<trect> rects = ToolUtils::splitRect(enlargedRect, lastRect);</trect>
7d1790
    for (int i = 0; i < rects.size(); i++) {
7d1790
      m_workRas->extract(rects[i])->copy(ras->extract(rects[i]));
7d1790
      m_backupRas->extract(rects[i])->copy(ras->extract(rects[i]));
7d1790
    }
Shinya Kitaoka 120a6e
  }
7d1790
7d1790
  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();
7d1790
  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);
7d1790
    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();
7d1790
  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);
7d1790
  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() {
7d1790
  m_inputmanager.finishTracks();
7d1790
  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)
7d1790
    , 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
}