Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
#ifndef TOONZRASTERBRUSHTOOL_H
shun-iwasawa 98926d
#define TOONZRASTERBRUSHTOOL_H
Toshihiro Shimizu 890ddd
458c62
#include <tgeometry.h></tgeometry.h>
458c62
#include <tproperty.h></tproperty.h>
458c62
#include <trasterimage.h></trasterimage.h>
458c62
#include <ttoonzimage.h></ttoonzimage.h>
458c62
#include <tstroke.h></tstroke.h>
458c62
#include <toonz strokegenerator.h=""></toonz>
458c62
#include <toonz rasterstrokegenerator.h=""></toonz>
458c62
458c62
#include <tools tool.h=""></tools>
458c62
#include <tools cursors.h=""></tools>
458c62
458c62
#include <tools inputmanager.h=""></tools>
458c62
#include <tools modifierline.h="" modifiers=""></tools>
458c62
#include <tools modifiers="" modifiertangents.h=""></tools>
458c62
#include <tools modifierassistants.h="" modifiers=""></tools>
458c62
#include <tools modifiers="" modifiersegmentation.h=""></tools>
458c62
#ifndef NDEBUG
458c62
#include <tools modifiers="" modifiertest.h=""></tools>
458c62
#endif
458c62
458c62
#include "bluredbrush.h"
shun-iwasawa 98926d
#include "mypainttoonzbrush.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qcoreapplication></qcoreapplication>
Toshihiro Shimizu 890ddd
#include <qradialgradient></qradialgradient>
shun-iwasawa 98926d
#include <qelapsedtimer></qelapsedtimer>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//  Forward declarations
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TTileSetCM32;
Toshihiro Shimizu 890ddd
class TTileSaverCM32;
Toshihiro Shimizu 890ddd
class RasterStrokeGenerator;
Toshihiro Shimizu 890ddd
class BluredBrush;
shun-iwasawa 98926d
class ToonzRasterBrushToolNotifier;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//************************************************************************
shun-iwasawa 98926d
//  Toonz Raster Brush Data declaration
Toshihiro Shimizu 890ddd
//************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
struct BrushData final : public TPersist {
Shinya Kitaoka 120a6e
  PERSIST_DECLARATION(BrushData)
Jeremy Bullock e84aac
  // frameRange, snapSensitivity and snap are not included
shun-iwasawa 7f1e30
  // Those options are not really a part of the brush settings,
shun-iwasawa 7f1e30
  // just the overall tool.
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::wstring m_name;
shun-iwasawa 98926d
  double m_min, m_max, m_smooth, m_hardness, m_opacityMin, m_opacityMax;
shun-iwasawa 98926d
  bool m_pencil, m_pressure;
shun-iwasawa 98926d
  int m_drawOrder;
bf1d82
  double m_modifierSize, m_modifierOpacity;
572ed1
  bool m_modifierEraser, m_modifierLockAlpha;
9380d5
  bool m_assistants;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  BrushData();
Shinya Kitaoka 120a6e
  BrushData(const std::wstring &name);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  bool operator<(const BrushData &other) const { return m_name < other.m_name; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void saveData(TOStream &os) override;
Shinya Kitaoka 473e70
  void loadData(TIStream &is) override;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//************************************************************************
shun-iwasawa 98926d
//   Toonz Raster Brush Preset Manager declaration
Toshihiro Shimizu 890ddd
//************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class BrushPresetManager {
Shinya Kitaoka 120a6e
  TFilePath m_fp;                 //!< Presets file path
Shinya Kitaoka 120a6e
  std::set<brushdata> m_presets;  //!< Current presets container</brushdata>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  BrushPresetManager() {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void load(const TFilePath &fp);
Shinya Kitaoka 120a6e
  void save();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  const TFilePath &path() { return m_fp; };
Shinya Kitaoka 120a6e
  const std::set<brushdata> &presets() const { return m_presets; }</brushdata>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void addPreset(const BrushData &data);
Shinya Kitaoka 120a6e
  void removePreset(const std::wstring &name);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//************************************************************************
walkerka 57fb3c
//    Smooth Stroke declaration
walkerka 57fb3c
//    Brush stroke smoothing buffer.
walkerka 57fb3c
//************************************************************************
walkerka 22f456
class SmoothStroke {
walkerka 57fb3c
public:
walkerka 22f456
  SmoothStroke() {}
walkerka 22f456
  ~SmoothStroke() {}
walkerka 22f456
walkerka 22f456
  // begin stroke
walkerka 22f456
  // smooth is smooth strength, from 0 to 100
walkerka 22f456
  void beginStroke(int smooth);
walkerka 22f456
  // add stroke point
walkerka 22f456
  void addPoint(const TThickPoint &point);
walkerka 22f456
  // end stroke
walkerka 22f456
  void endStroke();
walkerka 22f456
  // Get generated stroke points which has been smoothed.
walkerka 22f456
  // Both addPoint() and endStroke() generate new smoothed points.
walkerka 22f456
  // This method will removed generated points
walkerka 22f456
  void getSmoothPoints(std::vector<tthickpoint> &smoothPoints);</tthickpoint>
Jeremy Bullock 7f2044
  // Remove all points - used for straight lines
Jeremy Bullock 7f2044
  void clearPoints();
walkerka 57fb3c
walkerka 57fb3c
private:
walkerka 22f456
  void generatePoints();
walkerka 57fb3c
walkerka 57fb3c
private:
walkerka 22f456
  int m_smooth;
walkerka 22f456
  int m_outputIndex;
walkerka 22f456
  int m_readIndex;
walkerka 22f456
  std::vector<tthickpoint> m_rawPoints;</tthickpoint>
walkerka 22f456
  std::vector<tthickpoint> m_outputPoints;</tthickpoint>
shun-iwasawa 28636d
shun-iwasawa 28636d
  int m_resampledIndex;
shun-iwasawa 28636d
  std::vector<tthickpoint> m_resampledPoints;</tthickpoint>
walkerka 57fb3c
};
walkerka 57fb3c
//************************************************************************
shun-iwasawa 98926d
//   Toonz Raster Brush Tool declaration
Toshihiro Shimizu 890ddd
//************************************************************************
Toshihiro Shimizu 890ddd
458c62
class ToonzRasterBrushTool final : public TTool,
458c62
                                   public RasterController,
458c62
                                   public TInputHandler {
shun-iwasawa 98926d
  Q_DECLARE_TR_FUNCTIONS(ToonzRasterBrushTool)
shun-iwasawa 98926d
shun-iwasawa 98926d
  void updateCurrentStyle();
shun-iwasawa 98926d
  double restartBrushTimer();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
shun-iwasawa 98926d
  ToonzRasterBrushTool(std::string name, int targetType);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  ToolType getToolType() const override { return TTool::LevelWriteTool; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  ToolOptionsBox *createOptionsBox() override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void updateTranslation() override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void onActivate() override;
Shinya Kitaoka 473e70
  void onDeactivate() override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  bool preLeftButtonDown() override;
Shinya Kitaoka 473e70
  void leftButtonDown(const TPointD &pos, const TMouseEvent &e) override;
Shinya Kitaoka 473e70
  void leftButtonDrag(const TPointD &pos, const TMouseEvent &e) override;
Shinya Kitaoka 473e70
  void leftButtonUp(const TPointD &pos, const TMouseEvent &e) override;
Shinya Kitaoka 473e70
  void mouseMove(const TPointD &pos, const TMouseEvent &e) override;
Toshihiro Shimizu 890ddd
458c62
  void inputMouseMove(const TPointD &position,
458c62
                      const TInputState &state) override;
458c62
  void inputSetBusy(bool busy) override;
458c62
  void inputPaintTrackPoint(const TTrackPoint &point, const TTrack &track,
458c62
                            bool firstTrack) override;
458c62
  void inputInvalidateRect(const TRectD &bounds) override { invalidate(bounds); }
458c62
  TTool *inputGetTool() override { return this; };
458c62
Shinya Kitaoka 473e70
  void draw() override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void onEnter() override;
Shinya Kitaoka 473e70
  void onLeave() override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  int getCursorId() const override { return ToolCursor::PenCursor; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  TPropertyGroup *getProperties(int targetType) override;
Shinya Kitaoka 473e70
  bool onPropertyChanged(std::string propertyName) override;
Shinya Kitaoka 473e70
  void onImageChanged() override;
Shinya Kitaoka 120a6e
  void setWorkAndBackupImages();
Shinya Kitaoka 120a6e
  void updateWorkAndBackupRasters(const TRect &rect);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  void initPresets();
Shinya Kitaoka 120a6e
  void loadPreset();
Shinya Kitaoka 120a6e
  void addPreset(QString name);
Shinya Kitaoka 120a6e
  void removePreset();
Toshihiro Shimizu 890ddd
manongjohn df5842
  void loadLastBrush();
manongjohn df5842
Shinya Kitaoka 120a6e
  // return true if the pencil mode is active in the Brush / PaintBrush / Eraser
Shinya Kitaoka 120a6e
  // Tools.
Shinya Kitaoka 473e70
  bool isPencilModeActive() override;
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
  void onColorStyleChanged();
shun-iwasawa 98926d
  bool askRead(const TRect &rect) override;
shun-iwasawa 98926d
  bool askWrite(const TRect &rect) override;
shun-iwasawa 98926d
  bool isMyPaintStyleSelected() { return m_isMyPaintStyleSelected; }
walkerka 57fb3c
458c62
private:
458c62
  enum MouseEventType { ME_DOWN, ME_DRAG, ME_UP, ME_MOVE };
458c62
  void handleMouseEvent(MouseEventType type, const TPointD &pos,
458c62
                        const TMouseEvent &e);
458c62
Toshihiro Shimizu 890ddd
protected:
458c62
  TInputManager m_inputmanager;
458c62
#ifndef NDEBUG
458c62
  TSmartPointerT<tmodifiertest> m_modifierTest;</tmodifiertest>
458c62
#endif
458c62
  TSmartPointerT<tmodifierline> m_modifierLine;</tmodifierline>
458c62
  TSmartPointerT<tmodifiertangents> m_modifierTangents;</tmodifiertangents>
458c62
  TSmartPointerT<tmodifierassistants> m_modifierAssistants;</tmodifierassistants>
458c62
  TSmartPointerT<tmodifiersegmentation> m_modifierSegmentation;</tmodifiersegmentation>
458c62
458c62
  class MyPaintStroke: public TTrackToolHandler {
458c62
  public:
458c62
    MyPaintToonzBrush brush;
458c62
    
458c62
    inline MyPaintStroke(
458c62
      const TRaster32P &ras,
458c62
      RasterController &controller,
458c62
      const mypaint::Brush &brush,
458c62
      bool interpolation = false
458c62
    ): 
458c62
      brush(ras, controller, brush, interpolation)
458c62
    { }
458c62
  };
458c62
  
458c62
  class PencilStroke: public TTrackToolHandler {
458c62
  public:
458c62
    RasterStrokeGenerator brush;
458c62
    
458c62
    inline PencilStroke( const TRasterCM32P &raster, Tasks task,
458c62
                         ColorType colorType, int styleId, const TThickPoint &p,
458c62
                         bool selective, int selectedStyle, bool lockAlpha,
458c62
                         bool keepAntialias, bool isPaletteOrder = false
458c62
    ):
458c62
      brush(raster, task, colorType, styleId, p, selective, selectedStyle, lockAlpha, keepAntialias, isPaletteOrder)
458c62
    { }
458c62
  };
458c62
458c62
  class BluredStroke: public TTrackToolHandler {
458c62
  public:
458c62
    BluredBrush brush;
458c62
    
458c62
    inline BluredStroke( const TRaster32P &ras, int size,
458c62
                         const QRadialGradient &gradient, bool doDynamicOpacity
458c62
    ):
458c62
      brush(ras, size, gradient, doDynamicOpacity)
458c62
    { }
458c62
  };
8ca100
  
8ca100
  struct Painting {
526ddd
    // initial painting input
a84cc5
    bool active = false;
a84cc5
    int styleId = 0;
458c62
    bool smooth = false;
458c62
    // 作業中のFrameIdをクリック時に保存し、マウスリリース時(Undoの登録時)に別のフレームに 移動していたときの不具合を修正する。
458c62
    TFrameId frameId;
a84cc5
    
526ddd
    // common variables
526ddd
    TTileSetCM32 *tileSet = nullptr;
526ddd
    TTileSaverCM32 *tileSaver = nullptr;
458c62
    TRect affectedRect;
458c62
    
fd3b3f
    SmoothStroke smoothStroke;
526ddd
    
ae7f5f
    struct Pencil {
458c62
      bool isActive = false;
9c4ead
      bool realPencil = false;
ae7f5f
    } pencil;
ae7f5f
    
67082d
    struct Blured {
458c62
      bool isActive = false;
67082d
    } blured;
67082d
    
93e77b
    struct MyPaint {
458c62
      bool isActive = false;
458c62
      mypaint::Brush baseBrush;
6f40dc
      TRect strokeSegmentRect;
93e77b
    } myPaint;
93e77b
    
526ddd
    // straight variables
93e77b
    struct Straight {
93e77b
      bool isStraight = false;
93e77b
      TPointD firstPoint;
93e77b
      TPointD lastPoint;
93e77b
      double maxPressure = -1.0;
93e77b
    } straight;
8ca100
  } m_painting;
8ca100
  
Shinya Kitaoka 120a6e
  TPropertyGroup m_prop[2];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TDoublePairProperty m_rasThickness;
walkerka bb91cd
  TDoubleProperty m_smooth;
Shinya Kitaoka 120a6e
  TDoubleProperty m_hardness;
Shinya Kitaoka 120a6e
  TEnumProperty m_preset;
shun-iwasawa 975eb1
  TEnumProperty m_drawOrder;
Shinya Kitaoka 120a6e
  TBoolProperty m_pencil;
Shinya Kitaoka 120a6e
  TBoolProperty m_pressure;
shun-iwasawa 98926d
  TDoubleProperty m_modifierSize;
manongjohn f936c0
  TBoolProperty m_modifierLockAlpha;
shun-iwasawa 98926d
Shinya Kitaoka 120a6e
  double m_minThick, m_maxThick;
Shinya Kitaoka 120a6e
shun-iwasawa 98926d
  int m_targetType;
Shinya Kitaoka 120a6e
  TPointD m_dpiScale,
Shinya Kitaoka 120a6e
      m_mousePos,  //!< Current mouse position, in world coordinates.
shun-iwasawa 98926d
      m_brushPos;  //!< World position the brush will be painted at.
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  QRadialGradient m_brushPad;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRasterCM32P m_backupRas;
Shinya Kitaoka 120a6e
  TRaster32P m_workRas;
458c62
  TRect m_workBackupRect;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  BrushPresetManager
Shinya Kitaoka 120a6e
      m_presetsManager;  //!< Manager for presets of this tool instance
Shinya Kitaoka 120a6e
8ca100
  bool m_enabled,
Shinya Kitaoka 120a6e
      m_isPrompting,  //!< Whether the tool is prompting for spline
walkerka 22f456
                      //! substitution.
shun-iwasawa 98926d
      m_firstTime, m_presetsLoaded;
Shinya Kitaoka 120a6e
shun-iwasawa 98926d
  ToonzRasterBrushToolNotifier *m_notifier;
shun-iwasawa 98926d
  bool m_isMyPaintStyleSelected    = false;
shun-iwasawa 98926d
  QElapsedTimer m_brushTimer;
shun-iwasawa 98926d
  int m_minCursorThick, m_maxCursorThick;
shun-iwasawa 98926d
manongjohn 5a2268
  bool m_propertyUpdating = false;
manongjohn 5a2268
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 120a6e
  static void drawLine(const TPointD &point, const TPointD ¢re,
Shinya Kitaoka 120a6e
                       bool horizontal, bool isDecimal);
Shinya Kitaoka 120a6e
  static void drawEmptyCircle(TPointD point, int thick, bool isLxEven,
Shinya Kitaoka 120a6e
                              bool isLyEven, bool isPencil);
Martin van Zijl e9b900
manongjohn fe8e5c
  TPointD getCenteredCursorPos(const TPointD &originalCursorPos);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
shun-iwasawa 98926d
//------------------------------------------------------------
shun-iwasawa 98926d
shun-iwasawa 98926d
class ToonzRasterBrushToolNotifier final : public QObject {
shun-iwasawa 98926d
  Q_OBJECT
shun-iwasawa 98926d
shun-iwasawa 98926d
  ToonzRasterBrushTool *m_tool;
shun-iwasawa 98926d
shun-iwasawa 98926d
public:
shun-iwasawa 98926d
  ToonzRasterBrushToolNotifier(ToonzRasterBrushTool *tool);
shun-iwasawa 98926d
shun-iwasawa 98926d
protected slots:
shun-iwasawa 98926d
  // void onCanvasSizeChanged() { m_tool->onCanvasSizeChanged(); }
shun-iwasawa 98926d
  void onColorStyleChanged() { m_tool->onColorStyleChanged(); }
shun-iwasawa 98926d
};
shun-iwasawa 98926d
shun-iwasawa 98926d
#endif  // TOONZRASTERBRUSHTOOL_H