Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "fullcolorbrushtool.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzTools includes
Toshihiro Shimizu 890ddd
#include "tools/tool.h"
Toshihiro Shimizu 890ddd
#include "tools/cursors.h"
Toshihiro Shimizu 890ddd
#include "tools/toolutils.h"
Toshihiro Shimizu 890ddd
#include "tools/toolhandle.h"
Toshihiro Shimizu 890ddd
#include "tools/tooloptions.h"
Toshihiro Shimizu 890ddd
bf1d82
#include "mypainttoonzbrush.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzQt includes
Toshihiro Shimizu 890ddd
#include "toonzqt/dvdialog.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzLib includes
Toshihiro Shimizu 890ddd
#include "toonz/tpalettehandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/txsheethandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshlevelhandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/tobjecthandle.h"
Toshihiro Shimizu 890ddd
#include "toonz/ttileset.h"
Toshihiro Shimizu 890ddd
#include "toonz/ttilesaver.h"
Toshihiro Shimizu 890ddd
#include "toonz/strokegenerator.h"
Toshihiro Shimizu 890ddd
#include "toonz/tstageobject.h"
Toshihiro Shimizu 890ddd
#include "toonz/palettecontroller.h"
bf1d82
#include "toonz/mypaintbrushstyle.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tgl.h"
Toshihiro Shimizu 890ddd
#include "tproperty.h"
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
Toshihiro Shimizu 890ddd
#include "tenv.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
#include "tstream.h"
Toshihiro Shimizu 890ddd
#include "tstroke.h"
Toshihiro Shimizu 890ddd
#include "timagecache.h"
bf1d82
#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Qt includes
Shinya Kitaoka 120a6e
#include <qcoreapplication>  // Qt translation support</qcoreapplication>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TEnv::IntVar FullcolorBrushMinSize("FullcolorBrushMinSize", 1);
Toshihiro Shimizu 890ddd
TEnv::IntVar FullcolorBrushMaxSize("FullcolorBrushMaxSize", 5);
Campbell Barton f49389
TEnv::IntVar FullcolorPressureSensitivity("FullcolorPressureSensitivity", 1);
Toshihiro Shimizu 890ddd
TEnv::DoubleVar FullcolorBrushHardness("FullcolorBrushHardness", 100);
Toshihiro Shimizu 890ddd
TEnv::DoubleVar FullcolorMinOpacity("FullcolorMinOpacity", 100);
Toshihiro Shimizu 890ddd
TEnv::DoubleVar FullcolorMaxOpacity("FullcolorMaxOpacity", 100);
bf1d82
TEnv::DoubleVar FullcolorModifierSize("FullcolorModifierSize", 0);
bf1d82
TEnv::DoubleVar FullcolorModifierOpacity("FullcolorModifierOpacity", 100);
572ed1
TEnv::IntVar FullcolorModifierEraser("FullcolorModifierEraser", 0);
572ed1
TEnv::IntVar FullcolorModifierLockAlpha("FullcolorModifierLockAlpha", 0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define CUSTOM_WSTR L"<custom>"</custom>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Shinya Kitaoka 120a6e
Shinya Kitaoka d1f6c4
class FullColorBrushUndo final : public ToolUtils::TFullColorRasterUndo {
Shinya Kitaoka 120a6e
  TPoint m_offset;
Shinya Kitaoka 120a6e
  QString m_id;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  FullColorBrushUndo(TTileSetFullColor *tileSet, TXshSimpleLevel *level,
Shinya Kitaoka 120a6e
                     const TFrameId &frameId, bool isFrameCreated,
Shinya Kitaoka 120a6e
                     const TRasterP &ras, const TPoint &offset)
Shinya Kitaoka 120a6e
      : ToolUtils::TFullColorRasterUndo(tileSet, level, frameId, isFrameCreated,
Shinya Kitaoka 120a6e
                                        false, 0)
Shinya Kitaoka 120a6e
      , m_offset(offset) {
Shinya Kitaoka 120a6e
    static int counter = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_id = QString("FullColorBrushUndo") + QString::number(counter++);
Shinya Kitaoka 120a6e
    TImageCache::instance()->add(m_id.toStdString(), TRasterImageP(ras));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ~FullColorBrushUndo() { TImageCache::instance()->remove(m_id); }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void redo() const override {
Shinya Kitaoka 120a6e
    insertLevelAndFrameIfNeeded();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRasterImageP image = getImage();
Shinya Kitaoka 120a6e
    TRasterP ras        = image->getRaster();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRasterImageP srcImg =
Shinya Kitaoka 120a6e
        TImageCache::instance()->get(m_id.toStdString(), false);
Shinya Kitaoka 120a6e
    ras->copy(srcImg->getRaster(), m_offset);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TTool::getApplication()->getCurrentXsheet()->notifyXsheetChanged();
Shinya Kitaoka 120a6e
    notifyImageChanged();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  int getSize() const override {
Shinya Kitaoka 120a6e
    return sizeof(*this) + ToolUtils::TFullColorRasterUndo::getSize();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  QString getToolName() override { return QString("Raster Brush Tool"); }
Shinya Kitaoka 473e70
  int getHistoryType() override { return HistoryType::BrushTool; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//************************************************************************
Toshihiro Shimizu 890ddd
//    FullColor Brush Tool implementation
Toshihiro Shimizu 890ddd
//************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
FullColorBrushTool::FullColorBrushTool(std::string name)
Shinya Kitaoka 120a6e
    : TTool(name)
Jeremy Bullock 01e454
    , m_thickness("Size", 1, 100, 1, 5, false)
Jeremy Bullock 01e454
    , m_pressure("Pressure", true)
Jeremy Bullock 01e454
    , m_opacity("Opacity", 0, 100, 100, 100, true)
Shinya Kitaoka 120a6e
    , m_hardness("Hardness:", 0, 100, 100)
bf1d82
    , m_modifierSize("ModifierSize", -3, 3, 0, true)
bf1d82
    , m_modifierOpacity("ModifierOpacity", 0, 100, 100, true)
572ed1
    , m_modifierEraser("ModifierEraser", false)
572ed1
    , m_modifierLockAlpha("ModifierLockAlpha", false)
Shinya Kitaoka 120a6e
    , m_preset("Preset:")
bf1d82
    , m_minCursorThick(0)
bf1d82
    , m_maxCursorThick(0)
bf1d82
    , m_enabledPressure(false)
bf1d82
    , m_toonz_brush(0)
Shinya Kitaoka 120a6e
    , m_tileSet(0)
Shinya Kitaoka 120a6e
    , m_tileSaver(0)
Shinya Kitaoka 120a6e
    , m_notifier(0)
Shinya Kitaoka 120a6e
    , m_presetsLoaded(false)
Shinya Kitaoka 120a6e
    , m_firstTime(true) {
Shinya Kitaoka 120a6e
  bind(TTool::RasterImage | TTool::EmptyTarget);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_prop.bind(m_thickness);
Shinya Kitaoka 120a6e
  m_prop.bind(m_hardness);
Shinya Kitaoka 120a6e
  m_prop.bind(m_opacity);
bf1d82
  m_prop.bind(m_modifierSize);
bf1d82
  m_prop.bind(m_modifierOpacity);
572ed1
  m_prop.bind(m_modifierEraser);
572ed1
  m_prop.bind(m_modifierLockAlpha);
Shinya Kitaoka 120a6e
  m_prop.bind(m_pressure);
Shinya Kitaoka 120a6e
  m_prop.bind(m_preset);
bf1d82
Shinya Kitaoka 120a6e
  m_preset.setId("BrushPreset");
bf1d82
bf1d82
  m_brushTimer.start();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ToolOptionsBox *FullColorBrushTool::createOptionsBox() {
Shinya Kitaoka 120a6e
  TPaletteHandle *currPalette =
Shinya Kitaoka 120a6e
      TTool::getApplication()->getPaletteController()->getCurrentLevelPalette();
Shinya Kitaoka 120a6e
  ToolHandle *currTool = TTool::getApplication()->getCurrentTool();
Shinya Kitaoka 120a6e
  return new BrushToolOptionsBox(0, this, currPalette, currTool);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorBrushTool::onCanvasSizeChanged() {
Shinya Kitaoka 120a6e
  onDeactivate();
Shinya Kitaoka 120a6e
  setWorkAndBackupImages();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
bf1d82
void FullColorBrushTool::onColorStyleChanged() {
bf1d82
  getApplication()->getCurrentTool()->notifyToolChanged();
bf1d82
}
bf1d82
bf1d82
//---------------------------------------------------------------------------------------------------
bf1d82
Shinya Kitaoka 120a6e
void FullColorBrushTool::updateTranslation() {
Jeremy Bullock 01e454
  m_thickness.setQStringName(tr("Size"));
Jeremy Bullock 01e454
  m_pressure.setQStringName(tr("Pressure"));
Jeremy Bullock 01e454
  m_opacity.setQStringName(tr("Opacity"));
Shinya Kitaoka 120a6e
  m_hardness.setQStringName(tr("Hardness:"));
Shinya Kitaoka 120a6e
  m_preset.setQStringName(tr("Preset:"));
bf1d82
  m_modifierSize.setQStringName(tr("Size"));
bf1d82
  m_modifierOpacity.setQStringName(tr("Opacity"));
572ed1
  m_modifierEraser.setQStringName(tr("Eraser"));
572ed1
  m_modifierLockAlpha.setQStringName(tr("Lock Alpha"));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorBrushTool::onActivate() {
Shinya Kitaoka 120a6e
  if (!m_notifier) m_notifier = new FullColorBrushToolNotifier(this);
Shinya Kitaoka 120a6e
bf1d82
  updateCurrentStyle();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_firstTime) {
Shinya Kitaoka 120a6e
    m_firstTime = false;
Shinya Kitaoka 120a6e
    m_thickness.setValue(
Shinya Kitaoka 120a6e
        TIntPairProperty::Value(FullcolorBrushMinSize, FullcolorBrushMaxSize));
Campbell Barton f49389
    m_pressure.setValue(FullcolorPressureSensitivity ? 1 : 0);
Shinya Kitaoka 120a6e
    m_opacity.setValue(
Shinya Kitaoka 120a6e
        TDoublePairProperty::Value(FullcolorMinOpacity, FullcolorMaxOpacity));
Shinya Kitaoka 120a6e
    m_hardness.setValue(FullcolorBrushHardness);
bf1d82
    m_modifierSize.setValue(FullcolorModifierSize);
bf1d82
    m_modifierOpacity.setValue(FullcolorModifierOpacity);
572ed1
    m_modifierEraser.setValue(FullcolorModifierEraser ? true : false);
572ed1
    m_modifierLockAlpha.setValue(FullcolorModifierLockAlpha ? true : false);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  setWorkAndBackupImages();
bf1d82
  onColorStyleChanged();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorBrushTool::onDeactivate() {
Jeremy Bullock 5af997
  if (m_mousePressed) leftButtonUp(m_mousePos, m_mouseEvent);
Shinya Kitaoka 120a6e
  m_workRaster = TRaster32P();
Shinya Kitaoka 120a6e
  m_backUpRas  = TRasterP();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorBrushTool::updateWorkAndBackupRasters(const TRect &rect) {
bf1d82
  if (rect.isEmpty()) return;
bf1d82
Shinya Kitaoka 120a6e
  TRasterImageP ri = TImageP(getImage(false, 1));
Shinya Kitaoka 120a6e
  if (!ri) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRasterP ras = ri->getRaster();
Toshihiro Shimizu 890ddd
bf1d82
  const int denominator = 8;
bf1d82
  TRect enlargedRect = rect + m_lastRect;
bf1d82
  int dx = (enlargedRect.getLx()-1)/denominator+1;
bf1d82
  int dy = (enlargedRect.getLy()-1)/denominator+1;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (m_lastRect.isEmpty()) {
bf1d82
    enlargedRect.x0 -= dx;
bf1d82
    enlargedRect.y0 -= dy;
bf1d82
    enlargedRect.x1 += dx;
bf1d82
    enlargedRect.y1 += dy;
bf1d82
bf1d82
    TRect _rect = enlargedRect*ras->getBounds();
bf1d82
    if (_rect.isEmpty()) return;
bf1d82
bf1d82
    m_workRaster->extract(_rect)->copy(ras->extract(_rect));
Shinya Kitaoka 120a6e
    m_backUpRas->extract(_rect)->copy(ras->extract(_rect));
bf1d82
  } else {
bf1d82
    if (enlargedRect.x0 < m_lastRect.x0) enlargedRect.x0 -= dx;
bf1d82
    if (enlargedRect.y0 < m_lastRect.y0) enlargedRect.y0 -= dy;
bf1d82
    if (enlargedRect.x1 > m_lastRect.x1) enlargedRect.x1 += dx;
bf1d82
    if (enlargedRect.y1 > m_lastRect.y1) enlargedRect.y1 += dy;
bf1d82
bf1d82
    TRect _rect = enlargedRect*ras->getBounds();
bf1d82
    if (_rect.isEmpty()) return;
bf1d82
bf1d82
    TRect _lastRect = m_lastRect * ras->getBounds();
bf1d82
    QList<trect> rects = ToolUtils::splitRect(_rect, _lastRect);</trect>
bf1d82
    for (int i = 0; i < rects.size(); i++) {
bf1d82
      m_workRaster->extract(rects[i])->copy(ras->extract(rects[i]));
bf1d82
      m_backUpRas->extract(rects[i])->copy(ras->extract(rects[i]));
bf1d82
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
bf1d82
  m_lastRect = enlargedRect;
bf1d82
}
bf1d82
bf1d82
//--------------------------------------------------------------------------------------------------
bf1d82
bf1d82
bool FullColorBrushTool::askRead(const TRect &rect) {
bf1d82
  return askWrite(rect);
bf1d82
}
bf1d82
bf1d82
//--------------------------------------------------------------------------------------------------
bf1d82
bf1d82
bool FullColorBrushTool::askWrite(const TRect &rect) {
bf1d82
  if (rect.isEmpty()) return true;
bf1d82
  m_strokeRect += rect;
bf1d82
  m_strokeSegmentRect += rect;
bf1d82
  updateWorkAndBackupRasters(rect);
bf1d82
  m_tileSaver->save(rect);
bf1d82
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool FullColorBrushTool::preLeftButtonDown() {
Shinya Kitaoka 120a6e
  touchImage();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (m_isFrameCreated) setWorkAndBackupImages();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorBrushTool::leftButtonDown(const TPointD &pos,
Shinya Kitaoka 120a6e
                                        const TMouseEvent &e) {
bf1d82
  TPointD previousBrushPos = m_brushPos;
Shinya Kitaoka 120a6e
  m_brushPos = m_mousePos = pos;
Jeremy Bullock 5af997
  m_mousePressed          = true;
Jeremy Bullock 5af997
  m_mouseEvent            = e;
Jeremy Bullock 5af997
  Viewer *viewer          = getViewer();
Shinya Kitaoka 120a6e
  if (!viewer) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRasterImageP ri = (TRasterImageP)getImage(true);
Shinya Kitaoka 120a6e
  if (!ri) ri      = (TRasterImageP)touchImage();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!ri) return;
Shinya Kitaoka 120a6e
shun_iwasawa e3df22
  /* update color here since the current style might be switched with numpad
shun_iwasawa e3df22
   * shortcut keys */
bf1d82
  updateCurrentStyle();
shun_iwasawa e3df22
bf1d82
  TRasterP ras = ri->getRaster();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!(m_workRaster && m_backUpRas)) setWorkAndBackupImages();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_workRaster->lock();
Shinya Kitaoka 120a6e
bf1d82
  TPointD rasCenter = ras->getCenterD();
bf1d82
  TPointD point(pos + rasCenter);
bf1d82
  double pressure = m_enabledPressure && e.isTablet() ? e.m_pressure/255.0 : 0.5;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_tileSet       = new TTileSetFullColor(ras->getSize());
Shinya Kitaoka 120a6e
  m_tileSaver     = new TTileSaverFullColor(ras, m_tileSet);
bf1d82
bf1d82
  mypaint::Brush mypaintBrush;
bf1d82
  applyToonzBrushSettings(mypaintBrush);
bf1d82
  m_toonz_brush = new MyPaintToonzBrush(m_workRaster, *this, mypaintBrush);
bf1d82
bf1d82
  m_strokeRect.empty();
bf1d82
  m_strokeSegmentRect.empty();
bf1d82
  m_toonz_brush->beginStroke();
bf1d82
  m_toonz_brush->strokeTo(point, pressure, restartBrushTimer());
bf1d82
  TRect updateRect = m_strokeSegmentRect*ras->getBounds();
bf1d82
  if (!updateRect.isEmpty())
bf1d82
    ras->extract(updateRect)->copy(m_workRaster->extract(updateRect));
bf1d82
bf1d82
  TPointD thickOffset(m_maxCursorThick*0.5, m_maxCursorThick*0.5);
bf1d82
  TRectD invalidateRect = convert(m_strokeSegmentRect) - rasCenter;
bf1d82
  invalidateRect += TRectD(m_brushPos - thickOffset, m_brushPos + thickOffset);
bf1d82
  invalidateRect += TRectD(previousBrushPos - thickOffset, previousBrushPos + thickOffset);
bf1d82
  invalidate(invalidateRect.enlarge(2.0));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorBrushTool::leftButtonDrag(const TPointD &pos,
Shinya Kitaoka 120a6e
                                        const TMouseEvent &e) {
bf1d82
  TPointD previousBrushPos = m_brushPos;
Shinya Kitaoka 120a6e
  m_brushPos = m_mousePos = pos;
Jeremy Bullock 5af997
  m_mouseEvent            = e;
Jeremy Bullock 5af997
  TRasterImageP ri        = (TRasterImageP)getImage(true);
Shinya Kitaoka 120a6e
  if (!ri) return;
Shinya Kitaoka 120a6e
bf1d82
  TRasterP ras = ri->getRaster();
bf1d82
  TPointD rasCenter = ras->getCenterD();
bf1d82
  TPointD point(pos + rasCenter);
bf1d82
  double pressure = m_enabledPressure && e.isTablet() ? e.m_pressure/255.0 : 0.5;
bf1d82
bf1d82
  m_strokeSegmentRect.empty();
bf1d82
  m_toonz_brush->strokeTo(point, pressure, restartBrushTimer());
bf1d82
  TRect updateRect = m_strokeSegmentRect*ras->getBounds();
bf1d82
  if (!updateRect.isEmpty())
bf1d82
    ras->extract(updateRect)->copy(m_workRaster->extract(updateRect));
bf1d82
bf1d82
  TPointD thickOffset(m_maxCursorThick*0.5, m_maxCursorThick*0.5);
bf1d82
  TRectD invalidateRect = convert(m_strokeSegmentRect) - rasCenter;
bf1d82
  invalidateRect += TRectD(m_brushPos - thickOffset, m_brushPos + thickOffset);
bf1d82
  invalidateRect += TRectD(previousBrushPos - thickOffset, previousBrushPos + thickOffset);
bf1d82
  invalidate(invalidateRect.enlarge(2.0));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorBrushTool::leftButtonUp(const TPointD &pos,
Shinya Kitaoka 120a6e
                                      const TMouseEvent &e) {
bf1d82
  TPointD previousBrushPos = m_brushPos;
Shinya Kitaoka 120a6e
  m_brushPos = m_mousePos = pos;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRasterImageP ri = (TRasterImageP)getImage(true);
Shinya Kitaoka 120a6e
  if (!ri) return;
Shinya Kitaoka 120a6e
bf1d82
  TRasterP ras = ri->getRaster();
bf1d82
  TPointD rasCenter = ras->getCenterD();
bf1d82
  TPointD point(pos + rasCenter);
bf1d82
  double pressure = m_enabledPressure && e.isTablet() ? e.m_pressure/255.0 : 0.5;
bf1d82
bf1d82
  m_strokeSegmentRect.empty();
bf1d82
  m_toonz_brush->strokeTo(point, pressure, restartBrushTimer());
bf1d82
  m_toonz_brush->endStroke();
bf1d82
  TRect updateRect = m_strokeSegmentRect*ras->getBounds();
bf1d82
  if (!updateRect.isEmpty())
bf1d82
    ras->extract(updateRect)->copy(m_workRaster->extract(updateRect));
bf1d82
bf1d82
  TPointD thickOffset(m_maxCursorThick*0.5, m_maxCursorThick*0.5);
bf1d82
  TRectD invalidateRect = convert(m_strokeSegmentRect) - rasCenter;
bf1d82
  invalidateRect += TRectD(m_brushPos - thickOffset, m_brushPos + thickOffset);
bf1d82
  invalidateRect += TRectD(previousBrushPos - thickOffset, previousBrushPos + thickOffset);
bf1d82
  invalidate(invalidateRect.enlarge(2.0));
bf1d82
bf1d82
  if (m_toonz_brush) {
bf1d82
    delete m_toonz_brush;
bf1d82
    m_toonz_brush = 0;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
bf1d82
  m_lastRect.empty();
Shinya Kitaoka 120a6e
  m_workRaster->unlock();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_tileSet->getTileCount() > 0) {
Shinya Kitaoka 120a6e
    delete m_tileSaver;
Shinya Kitaoka 120a6e
    TTool::Application *app   = TTool::getApplication();
Shinya Kitaoka 120a6e
    TXshLevel *level          = app->getCurrentLevel()->getLevel();
Shinya Kitaoka 120a6e
    TXshSimpleLevelP simLevel = level->getSimpleLevel();
Shinya Kitaoka 120a6e
    TFrameId frameId          = getCurrentFid();
bf1d82
    TRasterP subras           = ras->extract(m_strokeRect)->clone();
Shinya Kitaoka 120a6e
    TUndoManager::manager()->add(
Shinya Kitaoka 120a6e
        new FullColorBrushUndo(m_tileSet, simLevel.getPointer(), frameId,
bf1d82
                               m_isFrameCreated, subras, m_strokeRect.getP00()));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  notifyImageChanged();
Shinya Kitaoka 120a6e
  m_strokeRect.empty();
Jeremy Bullock 5af997
  m_mousePressed = false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorBrushTool::mouseMove(const TPointD &pos, const TMouseEvent &e) {
Shinya Kitaoka 120a6e
  struct Locals {
Shinya Kitaoka 120a6e
    FullColorBrushTool *m_this;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    void setValue(TIntPairProperty &prop,
Shinya Kitaoka 120a6e
                  const TIntPairProperty::Value &value) {
Shinya Kitaoka 120a6e
      prop.setValue(value);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      m_this->onPropertyChanged(prop.getName());
Shinya Kitaoka 120a6e
      TTool::getApplication()->getCurrentTool()->notifyToolChanged();
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    void addMinMax(TIntPairProperty &prop, double add) {
Shinya Kitaoka 120a6e
      const TIntPairProperty::Range &range = prop.getRange();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      TIntPairProperty::Value value = prop.getValue();
Shinya Kitaoka 120a6e
      value.second =
Shinya Kitaoka 120a6e
          tcrop<double>(value.second + add, range.first, range.second);</double>
Shinya Kitaoka 120a6e
      value.first = tcrop<double>(value.first + add, range.first, range.second);</double>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      setValue(prop, value);
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
Jeremy Bullock 01e454
    void addMinMaxSeparate(TIntPairProperty &prop, double min, double max) {
Jeremy Bullock 01e454
      if (min == 0.0 && max == 0.0) return;
Jeremy Bullock 01e454
      const TIntPairProperty::Range &range = prop.getRange();
Toshihiro Shimizu 890ddd
Jeremy Bullock 01e454
      TIntPairProperty::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<double>(value.first, range.first, range.second);</double>
Jeremy Bullock 01e454
      value.second = tcrop<double>(value.second, range.first, range.second);</double>
Jeremy Bullock 01e454
Jeremy Bullock 01e454
      setValue(prop, value);
Jeremy Bullock 01e454
    }
Toshihiro Shimizu 890ddd
Jeremy Bullock 01e454
  } locals = {this};
Shinya Kitaoka d4642c
Jeremy Bullock 01e454
  // if (e.isAltPressed() && !e.isCtrlPressed()) {
Jeremy Bullock 01e454
  // const TPointD &diff = pos - m_mousePos;
Jeremy Bullock 01e454
  // double add = (fabs(diff.x) > fabs(diff.y)) ? diff.x : diff.y;
Toshihiro Shimizu 890ddd
Jeremy Bullock 01e454
  // locals.addMinMax(m_thickness, int(add));
Jeremy Bullock 01e454
  //} else
Jeremy Bullock 01e454
  if (e.isCtrlPressed() && e.isAltPressed()) {
Jeremy Bullock 01e454
    const TPointD &diff = pos - m_mousePos;
Jeremy Bullock 01e454
    double max          = diff.x / 2;
Jeremy Bullock 01e454
    double min          = diff.y / 2;
Jeremy Bullock 01e454
Jeremy Bullock 01e454
    locals.addMinMaxSeparate(m_thickness, int(min), int(max));
Jeremy Bullock 01e454
  } else {
Shinya Kitaoka 120a6e
    m_brushPos = pos;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_mousePos = pos;
Shinya Kitaoka 120a6e
  invalidate();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorBrushTool::draw() {
Shinya Kitaoka 120a6e
  if (TRasterImageP ri = TRasterImageP(getImage(false))) {
Shinya Kitaoka 120a6e
    TRasterP ras = ri->getRaster();
Toshihiro Shimizu 890ddd
bf1d82
    double alpha = 1.0;
bf1d82
    double alphaRadius = 3.0;
bf1d82
    double pixelSize = sqrt(tglGetPixelSize2());
bf1d82
bf1d82
    // circles with lesser radius looks more bold
bf1d82
    // to avoid these effect we'll reduce alpha for small radiuses
bf1d82
    double minX = m_minCursorThick/(alphaRadius*pixelSize);
bf1d82
    double maxX = m_maxCursorThick/(alphaRadius*pixelSize);
bf1d82
    double minAlpha = alpha*(1.0 - 1.0/(1.0 + minX));
bf1d82
    double maxAlpha = alpha*(1.0 - 1.0/(1.0 + maxX));
bf1d82
bf1d82
    glPushAttrib(GL_ALL_ATTRIB_BITS);
bf1d82
    tglEnableBlending();
bf1d82
    tglEnableLineSmooth(true, 0.5);
bf1d82
bf1d82
    if (m_minCursorThick < m_maxCursorThick - pixelSize) {
bf1d82
      glColor4d(1.0, 1.0, 1.0, minAlpha);
bf1d82
      tglDrawCircle(m_brushPos, (m_minCursorThick + 1)*0.5 - pixelSize);
bf1d82
      glColor4d(0.0, 0.0, 0.0, minAlpha);
bf1d82
      tglDrawCircle(m_brushPos, (m_minCursorThick + 1)*0.5);
bf1d82
    }
bf1d82
bf1d82
    glColor4d(1.0, 1.0, 1.0, maxAlpha);
bf1d82
    tglDrawCircle(m_brushPos, (m_maxCursorThick + 1)*0.5 - pixelSize);
bf1d82
    glColor4d(0.0, 0.0, 0.0, maxAlpha);
bf1d82
    tglDrawCircle(m_brushPos, (m_maxCursorThick + 1)*0.5);
Toshihiro Shimizu 890ddd
bf1d82
    glPopAttrib();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorBrushTool::onEnter() {
bf1d82
  updateCurrentStyle();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorBrushTool::onLeave() {
bf1d82
  m_minCursorThick = 0;
bf1d82
  m_maxCursorThick = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPropertyGroup *FullColorBrushTool::getProperties(int targetType) {
Shinya Kitaoka 120a6e
  if (!m_presetsLoaded) initPresets();
Shinya Kitaoka 120a6e
  return &m_prop;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorBrushTool::onImageChanged() { setWorkAndBackupImages(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorBrushTool::setWorkAndBackupImages() {
Shinya Kitaoka 120a6e
  TRasterImageP ri = (TRasterImageP)getImage(false, 1);
Shinya Kitaoka 120a6e
  if (!ri) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRasterP ras   = ri->getRaster();
Shinya Kitaoka 120a6e
  TDimension dim = ras->getSize();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!m_workRaster || m_workRaster->getLx() > dim.lx ||
Shinya Kitaoka 120a6e
      m_workRaster->getLy() > dim.ly)
Shinya Kitaoka 120a6e
    m_workRaster = TRaster32P(dim);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (!m_backUpRas || m_backUpRas->getLx() > dim.lx ||
Shinya Kitaoka 120a6e
      m_backUpRas->getLy() > dim.ly ||
Shinya Kitaoka 120a6e
      m_backUpRas->getPixelSize() != ras->getPixelSize())
Shinya Kitaoka 120a6e
    m_backUpRas = ras->create(dim.lx, dim.ly);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_strokeRect.empty();
Shinya Kitaoka 120a6e
  m_lastRect.empty();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool FullColorBrushTool::onPropertyChanged(std::string propertyName) {
bf1d82
  FullcolorBrushMinSize        = m_thickness.getValue().first;
bf1d82
  FullcolorBrushMaxSize        = m_thickness.getValue().second;
Campbell Barton f49389
  FullcolorPressureSensitivity = m_pressure.getValue();
Shinya Kitaoka 120a6e
  FullcolorBrushHardness       = m_hardness.getValue();
Shinya Kitaoka 120a6e
  FullcolorMinOpacity          = m_opacity.getValue().first;
Shinya Kitaoka 120a6e
  FullcolorMaxOpacity          = m_opacity.getValue().second;
bf1d82
  FullcolorModifierSize        = m_modifierSize.getValue();
bf1d82
  FullcolorModifierOpacity     = m_modifierOpacity.getValue();
572ed1
  FullcolorModifierEraser      = m_modifierEraser.getValue() ? 1 : 0;
572ed1
  FullcolorModifierLockAlpha   = m_modifierLockAlpha.getValue() ? 1 : 0;
bf1d82
bf1d82
  updateCurrentStyle();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (propertyName == "Preset:") {
Shinya Kitaoka 120a6e
    loadPreset();
Shinya Kitaoka 120a6e
    getApplication()->getCurrentTool()->notifyToolChanged();
Shinya Kitaoka 120a6e
    return true;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_preset.getValue() != CUSTOM_WSTR) {
Shinya Kitaoka 120a6e
    m_preset.setValue(CUSTOM_WSTR);
Shinya Kitaoka 120a6e
    getApplication()->getCurrentTool()->notifyToolChanged();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorBrushTool::initPresets() {
Shinya Kitaoka 120a6e
  if (!m_presetsLoaded) {
Shinya Kitaoka 120a6e
    // If necessary, load the presets from file
Shinya Kitaoka 120a6e
    m_presetsLoaded = true;
Shinya Kitaoka 120a6e
    m_presetsManager.load(TEnv::getConfigDir() + "brush_raster.txt");
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Rebuild the presets property entries
Shinya Kitaoka 120a6e
  const std::set<brushdata> &presets = m_presetsManager.presets();</brushdata>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_preset.deleteAllValues();
Shinya Kitaoka 120a6e
  m_preset.addValue(CUSTOM_WSTR);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::set<brushdata>::const_iterator it, end = presets.end();</brushdata>
Shinya Kitaoka 120a6e
  for (it = presets.begin(); it != end; ++it) m_preset.addValue(it->m_name);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorBrushTool::loadPreset() {
Shinya Kitaoka 120a6e
  const std::set<brushdata> &presets = m_presetsManager.presets();</brushdata>
Shinya Kitaoka 120a6e
  std::set<brushdata>::const_iterator it;</brushdata>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  it = presets.find(BrushData(m_preset.getValue()));
Shinya Kitaoka 120a6e
  if (it == presets.end()) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  const BrushData &preset = *it;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  try  // Don't bother with RangeErrors
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    m_thickness.setValue(
Shinya Kitaoka 120a6e
        TIntPairProperty::Value(std::max((int)preset.m_min, 1), preset.m_max));
Shinya Kitaoka 120a6e
    m_hardness.setValue(preset.m_hardness, true);
Shinya Kitaoka 120a6e
    m_opacity.setValue(
Shinya Kitaoka 120a6e
        TDoublePairProperty::Value(preset.m_opacityMin, preset.m_opacityMax));
Shinya Kitaoka 120a6e
    m_pressure.setValue(preset.m_pressure);
bf1d82
    m_modifierSize.setValue(preset.m_modifierSize);
bf1d82
    m_modifierOpacity.setValue(preset.m_modifierOpacity);
572ed1
    m_modifierEraser.setValue(preset.m_modifierEraser);
572ed1
    m_modifierLockAlpha.setValue(preset.m_modifierLockAlpha);
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorBrushTool::addPreset(QString name) {
Shinya Kitaoka 120a6e
  // Build the preset
Shinya Kitaoka 120a6e
  BrushData preset(name.toStdWString());
Toshihiro Shimizu 890ddd
572ed1
  preset.m_min               = m_thickness.getValue().first;
572ed1
  preset.m_max               = m_thickness.getValue().second;
572ed1
  preset.m_hardness          = m_hardness.getValue();
572ed1
  preset.m_opacityMin        = m_opacity.getValue().first;
572ed1
  preset.m_opacityMax        = m_opacity.getValue().second;
572ed1
  preset.m_pressure          = m_pressure.getValue();
572ed1
  preset.m_modifierSize      = m_modifierSize.getValue();
572ed1
  preset.m_modifierOpacity   = m_modifierOpacity.getValue();
572ed1
  preset.m_modifierEraser    = m_modifierEraser.getValue();
572ed1
  preset.m_modifierLockAlpha = m_modifierLockAlpha.getValue();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Pass the preset to the manager
Shinya Kitaoka 120a6e
  m_presetsManager.addPreset(preset);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Reinitialize the associated preset enum
Shinya Kitaoka 120a6e
  initPresets();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Set the value to the specified one
Shinya Kitaoka 120a6e
  m_preset.setValue(preset.m_name);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorBrushTool::removePreset() {
Shinya Kitaoka 120a6e
  std::wstring name(m_preset.getValue());
Shinya Kitaoka 120a6e
  if (name == CUSTOM_WSTR) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_presetsManager.removePreset(name);
Shinya Kitaoka 120a6e
  initPresets();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // No parameter change, and set the preset value to custom
Shinya Kitaoka 120a6e
  m_preset.setValue(CUSTOM_WSTR);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
shun_iwasawa e3df22
//------------------------------------------------------------------
shun_iwasawa e3df22
bf1d82
void FullColorBrushTool::updateCurrentStyle() {
bf1d82
  m_currentColor = TPixel32::Black;
bf1d82
  if (TTool::Application *app = getApplication()) {
bf1d82
    if (app->getCurrentObject()->isSpline()) {
bf1d82
      m_currentColor = TPixel32::Red;
bf1d82
    } else
bf1d82
    if (TPalette *plt = app->getCurrentPalette()->getPalette()) {
bf1d82
      int style               = app->getCurrentLevelStyleIndex();
bf1d82
      TColorStyle *colorStyle = plt->getStyle(style);
bf1d82
      m_currentColor          = colorStyle->getMainColor();
bf1d82
    }
shun_iwasawa e3df22
  }
shun_iwasawa e3df22
bf1d82
  int prevMinCursorThick = m_minCursorThick;
bf1d82
  int prevMaxCursorThick = m_maxCursorThick;
bf1d82
bf1d82
  m_enabledPressure = m_pressure.getValue();
bf1d82
  if (TMyPaintBrushStyle *brushStyle = getBrushStyle()) {
bf1d82
    double radiusLog = brushStyle->getBrush().getBaseValue(MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC)
bf1d82
                     + m_modifierSize.getValue()*log(2.0);
bf1d82
    double radius = exp(radiusLog);
bf1d82
    m_minCursorThick = m_maxCursorThick = (int)round(2.0*radius);
bf1d82
  } else {
bf1d82
    m_minCursorThick = std::max(m_thickness.getValue().first, 1);
bf1d82
    m_maxCursorThick = std::max(m_thickness.getValue().second, m_minCursorThick);
bf1d82
    if (!m_enabledPressure) {
bf1d82
      double minRadiusLog = log(0.5*m_minCursorThick);
bf1d82
      double maxRadiusLog = log(0.5*m_maxCursorThick);
bf1d82
      double avgRadiusLog = 0.5*(minRadiusLog + maxRadiusLog);
bf1d82
      double avgRadius = exp(avgRadiusLog);
bf1d82
      m_minCursorThick = m_maxCursorThick = (int)round(2.0*avgRadius);
bf1d82
    }
bf1d82
  }
bf1d82
bf1d82
  if (m_minCursorThick != prevMinCursorThick || m_maxCursorThick != prevMaxCursorThick) {
bf1d82
    TRectD rect(m_brushPos - TPointD(m_maxCursorThick + 2, m_maxCursorThick + 2),
bf1d82
                m_brushPos + TPointD(m_maxCursorThick + 2, m_maxCursorThick + 2));
bf1d82
    invalidate(rect);
bf1d82
  }
bf1d82
}
bf1d82
bf1d82
//------------------------------------------------------------------
bf1d82
bf1d82
double FullColorBrushTool::restartBrushTimer() {
bf1d82
  double dtime = m_brushTimer.nsecsElapsed()*1e-9;
bf1d82
  m_brushTimer.restart();
bf1d82
  return dtime;
bf1d82
}
bf1d82
bf1d82
//------------------------------------------------------------------
bf1d82
bf1d82
TMyPaintBrushStyle* FullColorBrushTool::getBrushStyle() {
bf1d82
  if (TTool::Application *app = getApplication())
bf1d82
      return dynamic_cast<tmypaintbrushstyle*>( app->getCurrentLevelStyle() );</tmypaintbrushstyle*>
bf1d82
  return 0;
shun_iwasawa e3df22
}
shun_iwasawa e3df22
bf1d82
//------------------------------------------------------------------
bf1d82
bf1d82
void FullColorBrushTool::applyClassicToonzBrushSettings(mypaint::Brush &mypaintBrush) {
bf1d82
  const double precision = 1e-5;
bf1d82
  const double hardnessOpacity = 0.1;
bf1d82
bf1d82
  double minThickness = 0.5*m_thickness.getValue().first;
bf1d82
  double maxThickness = 0.5*m_thickness.getValue().second;
bf1d82
  double minOpacity   = 0.01*m_opacity.getValue().first;
bf1d82
  double maxOpacity   = 0.01*m_opacity.getValue().second;
bf1d82
  double hardness     = 0.01*m_hardness.getValue();
bf1d82
bf1d82
  TPixelD color  = PixelConverter<tpixeld>::from(m_currentColor);</tpixeld>
bf1d82
  double  colorH = 0.0;
bf1d82
  double  colorS = 0.0;
bf1d82
  double  colorV = 0.0;
bf1d82
  RGB2HSV(color.r, color.g, color.b, &colorH, &colorS, &colorV);
bf1d82
bf1d82
  // avoid log(0)
bf1d82
  if (minThickness < precision)
bf1d82
    minThickness = precision;
bf1d82
  if (maxThickness < precision)
bf1d82
    maxThickness = precision;
bf1d82
bf1d82
  // tune hardness opacity for better visual softness
bf1d82
  hardness *= hardness;
bf1d82
  double opacityAmplifier = 1.0 - hardnessOpacity + hardness*hardnessOpacity;
bf1d82
  minOpacity *= opacityAmplifier;
bf1d82
  maxOpacity *= opacityAmplifier;
bf1d82
bf1d82
  // reset
bf1d82
  mypaintBrush.fromDefaults();
bf1d82
  mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_OPAQUE_MULTIPLY, 1.0);
bf1d82
  mypaintBrush.setMappingN(
bf1d82
      MYPAINT_BRUSH_SETTING_OPAQUE_MULTIPLY,
bf1d82
      MYPAINT_BRUSH_INPUT_PRESSURE,
bf1d82
      0 );
bf1d82
bf1d82
  mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_HARDNESS, 0.5*hardness + 0.5);
bf1d82
  mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_COLOR_H,  colorH/360.0);
bf1d82
  mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_COLOR_S,  colorS);
bf1d82
  mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_COLOR_V,  colorV);
bf1d82
  mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_DABS_PER_ACTUAL_RADIUS, 5.0 + hardness*10.0);
bf1d82
  mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_DABS_PER_BASIC_RADIUS, 0.0);
bf1d82
  mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_DABS_PER_SECOND, 0.0);
bf1d82
bf1d82
  // thickness may be dynamic
bf1d82
  if (minThickness + precision >= maxThickness) {
bf1d82
    mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC, log(maxThickness));
bf1d82
    mypaintBrush.setMappingN(
bf1d82
        MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC,
bf1d82
        MYPAINT_BRUSH_INPUT_PRESSURE,
bf1d82
        0 );
bf1d82
  } else {
bf1d82
    double minThicknessLog = log(minThickness);
bf1d82
    double maxThicknessLog = log(maxThickness);
bf1d82
    double baseThicknessLog = 0.5*(minThicknessLog + maxThicknessLog);
bf1d82
    mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC, baseThicknessLog);
bf1d82
    mypaintBrush.setMappingN(
bf1d82
        MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC,
bf1d82
        MYPAINT_BRUSH_INPUT_PRESSURE,
bf1d82
        2 );
bf1d82
    mypaintBrush.setMappingPoint(
bf1d82
        MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC,
bf1d82
        MYPAINT_BRUSH_INPUT_PRESSURE,
bf1d82
        0, 0.0, minThicknessLog - baseThicknessLog);
bf1d82
    mypaintBrush.setMappingPoint(
bf1d82
        MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC,
bf1d82
        MYPAINT_BRUSH_INPUT_PRESSURE,
bf1d82
        1, 1.0, maxThicknessLog - baseThicknessLog);
bf1d82
  }
bf1d82
bf1d82
  // opacity may be dynamic
bf1d82
  if (minOpacity + precision >= maxOpacity) {
bf1d82
    mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_OPAQUE, maxOpacity);
bf1d82
    mypaintBrush.setMappingN(
bf1d82
        MYPAINT_BRUSH_SETTING_OPAQUE,
bf1d82
        MYPAINT_BRUSH_INPUT_PRESSURE,
bf1d82
        0 );
bf1d82
  } else {
bf1d82
    mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_OPAQUE, minOpacity);
bf1d82
    mypaintBrush.setMappingN(
bf1d82
        MYPAINT_BRUSH_SETTING_OPAQUE,
bf1d82
        MYPAINT_BRUSH_INPUT_PRESSURE,
bf1d82
        2 );
bf1d82
    mypaintBrush.setMappingPoint(
bf1d82
        MYPAINT_BRUSH_SETTING_OPAQUE,
bf1d82
        MYPAINT_BRUSH_INPUT_PRESSURE,
bf1d82
        0, 0.0, 0.0);
bf1d82
    mypaintBrush.setMappingPoint(
bf1d82
        MYPAINT_BRUSH_SETTING_OPAQUE,
bf1d82
        MYPAINT_BRUSH_INPUT_PRESSURE,
bf1d82
        1, 1.0, maxOpacity - minOpacity);
bf1d82
  }
bf1d82
}
bf1d82
bf1d82
void FullColorBrushTool::applyToonzBrushSettings(mypaint::Brush &mypaintBrush) {
bf1d82
  TMyPaintBrushStyle *mypaintStyle = getBrushStyle();
bf1d82
bf1d82
  if (mypaintStyle) {
bf1d82
    const double precision = 1e-5;
bf1d82
572ed1
    double modifierSize      = m_modifierSize.getValue()*log(2.0);
572ed1
    double modifierOpacity   = 0.01*m_modifierOpacity.getValue();
572ed1
    bool   modifierEraser    = m_modifierEraser.getValue();
572ed1
    bool   modifierLockAlpha = m_modifierLockAlpha.getValue();
bf1d82
bf1d82
    TPixelD color  = PixelConverter<tpixeld>::from(m_currentColor);</tpixeld>
bf1d82
    double  colorH = 0.0;
bf1d82
    double  colorS = 0.0;
bf1d82
    double  colorV = 0.0;
bf1d82
    RGB2HSV(color.r, color.g, color.b, &colorH, &colorS, &colorV);
bf1d82
bf1d82
    mypaintBrush.fromBrush(mypaintStyle->getBrush());
bf1d82
bf1d82
    float baseSize    = mypaintBrush.getBaseValue(MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC);
bf1d82
    float baseOpacity = mypaintBrush.getBaseValue(MYPAINT_BRUSH_SETTING_OPAQUE);
bf1d82
bf1d82
    mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_RADIUS_LOGARITHMIC, baseSize + modifierSize);
bf1d82
    mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_OPAQUE, baseOpacity*modifierOpacity);
bf1d82
    mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_COLOR_H,  colorH/360.0);
bf1d82
    mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_COLOR_S,  colorS);
bf1d82
    mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_COLOR_V,  colorV);
572ed1
572ed1
    if (modifierEraser) {
572ed1
      mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_ERASER, 1.0);
572ed1
      mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_LOCK_ALPHA, 0.0);
572ed1
    } else if (modifierLockAlpha) {
572ed1
      // lock-alpha already disables eraser
572ed1
      mypaintBrush.setBaseValue(MYPAINT_BRUSH_SETTING_LOCK_ALPHA, 1.0);
572ed1
    }
bf1d82
  } else {
bf1d82
    applyClassicToonzBrushSettings(mypaintBrush);
bf1d82
  }
bf1d82
}
bf1d82
bf1d82
Toshihiro Shimizu 890ddd
//==========================================================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FullColorBrushToolNotifier::FullColorBrushToolNotifier(FullColorBrushTool *tool)
Shinya Kitaoka 120a6e
    : m_tool(tool) {
bf1d82
  if (TTool::Application *app = m_tool->getApplication()) {
bf1d82
    if (TXshLevelHandle *levelHandle = app->getCurrentLevel()) {
bf1d82
      bool ret = connect(levelHandle, SIGNAL(xshCanvasSizeChanged()), this,
bf1d82
                         SLOT(onCanvasSizeChanged()));
bf1d82
      assert(ret);
bf1d82
    }
bf1d82
    if (TPaletteHandle *paletteHandle = app->getCurrentPalette()) {
bf1d82
      bool ret;
bf1d82
      ret = connect(paletteHandle, SIGNAL(colorStyleChanged()), this,
bf1d82
                    SLOT(onColorStyleChanged()));
bf1d82
      assert(ret);
bf1d82
      ret = connect(paletteHandle, SIGNAL(colorStyleSwitched()), this,
bf1d82
                    SLOT(onColorStyleChanged()));
bf1d82
      assert(ret);
bf1d82
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==========================================================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FullColorBrushTool fullColorPencil("T_Brush");