bf1d82
bf1d82
#include <streambuf></streambuf>
bf1d82
bf1d82
#include <qstandardpaths></qstandardpaths>
bf1d82
bf1d82
#include "tfilepath_io.h"
bf1d82
#include "timage_io.h"
bf1d82
#include "trop.h"
bf1d82
#include "tsystem.h"
bf1d82
#include "tvectorimage.h"
bf1d82
#include "tpixelutils.h"
bf1d82
#include "toonz/toonzscene.h"
bf1d82
bf1d82
#include "toonz/mypaintbrushstyle.h"
bf1d82
bf1d82
bf1d82
//*************************************************************************************
bf1d82
//    TMyPaintBrushStyle  implementation
bf1d82
//*************************************************************************************
bf1d82
bf1d82
TMyPaintBrushStyle::TMyPaintBrushStyle()
bf1d82
  { }
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
TMyPaintBrushStyle::TMyPaintBrushStyle(const TFilePath &path) {
bf1d82
  loadBrush(path);
bf1d82
}
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
TMyPaintBrushStyle::TMyPaintBrushStyle(const TMyPaintBrushStyle &other):
bf1d82
  TColorStyle(other),
bf1d82
  m_path          (other.m_path),
bf1d82
  m_fullpath      (other.m_fullpath),
bf1d82
  m_brushOriginal (other.m_brushOriginal),
bf1d82
  m_brushModified (other.m_brushModified),
bf1d82
  m_preview       (other.m_preview),
bf1d82
  m_color         (other.m_color),
bf1d82
  m_baseValues    (other.m_baseValues)
bf1d82
  { }
bf1d82
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
TMyPaintBrushStyle::~TMyPaintBrushStyle()
bf1d82
  { }
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
TColorStyle& TMyPaintBrushStyle::copy(const TColorStyle &other) {
bf1d82
  const TMyPaintBrushStyle *otherBrushStyle =
bf1d82
    dynamic_cast<const tmypaintbrushstyle*="">(&other);</const>
bf1d82
  if (otherBrushStyle) {
bf1d82
    m_path          = otherBrushStyle->m_path;
bf1d82
    m_fullpath      = otherBrushStyle->m_fullpath;
bf1d82
    m_brushOriginal = otherBrushStyle->m_brushOriginal;
bf1d82
    m_brushModified = otherBrushStyle->m_brushModified;
bf1d82
    m_preview       = otherBrushStyle->m_preview;
bf1d82
    m_baseValues    = otherBrushStyle->m_baseValues;
bf1d82
  }
bf1d82
  assignBlend(other, other, 0.0);
bf1d82
  return *this;
bf1d82
}
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
QString TMyPaintBrushStyle::getDescription() const
bf1d82
  { return "MyPaintBrushStyle"; }
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
std::string TMyPaintBrushStyle::getBrushType()
bf1d82
  { return "myb"; }
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
TFilePathSet TMyPaintBrushStyle::getBrushesDirs() {
bf1d82
  TFilePathSet paths;
bf1d82
  paths.push_back(m_libraryDir + "mypaint brushes");
bf1d82
  QStringList genericLocations = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
bf1d82
  for(QStringList::iterator i = genericLocations.begin(); i != genericLocations.end(); ++i)
bf1d82
    paths.push_back(TFilePath(*i) + "mypaint" + "brushes");
bf1d82
  return paths;
bf1d82
}
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
TFilePath TMyPaintBrushStyle::decodePath(const TFilePath &path) const {
bf1d82
  if (path.isAbsolute())
bf1d82
    return path;
bf1d82
bf1d82
  if (m_currentScene) {
bf1d82
    TFilePath p = m_currentScene->decodeFilePath(path);
bf1d82
    TFileStatus fs(p);
bf1d82
    if (fs.doesExist() && !fs.isDirectory())
bf1d82
      return p;
bf1d82
  }
bf1d82
bf1d82
  TFilePathSet paths = getBrushesDirs();
bf1d82
  for(TFilePathSet::iterator i = paths.begin(); i != paths.end(); ++i) {
bf1d82
    TFilePath p = *i + path;
bf1d82
    TFileStatus fs(p);
bf1d82
    if (fs.doesExist() && !fs.isDirectory())
bf1d82
      return p;
bf1d82
  }
bf1d82
bf1d82
  return path;
bf1d82
}
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
void TMyPaintBrushStyle::loadBrush(const TFilePath &path) {
bf1d82
  m_path = path;
bf1d82
  m_fullpath = decodePath(path);
bf1d82
  m_brushOriginal.fromDefaults();
bf1d82
bf1d82
  Tifstream is(m_fullpath);
bf1d82
  if (is) {
bf1d82
    std::string str;
bf1d82
    str.assign( std::istreambuf_iterator<char>(is), std::istreambuf_iterator<char>() );</char></char>
bf1d82
    m_brushOriginal.fromString(str);
bf1d82
  }
bf1d82
bf1d82
  m_brushModified = m_brushOriginal;
bf1d82
  std::map<mypaintbrushsetting, float=""> baseValuesCopy;</mypaintbrushsetting,>
bf1d82
  baseValuesCopy.swap(m_baseValues);
bf1d82
  for(std::map<mypaintbrushsetting, float="">::const_iterator i = baseValuesCopy.begin(); i != baseValuesCopy.end(); ++i)</mypaintbrushsetting,>
bf1d82
    setBaseValue(i->first, i->second);
bf1d82
bf1d82
  TFilePath preview_path = m_fullpath.getParentDir() + (m_fullpath.getWideName() + L"_prev.png");
bf1d82
  TImageReader::load(preview_path, m_preview);
bf1d82
bf1d82
  invalidateIcon();
bf1d82
}
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
void TMyPaintBrushStyle::setBaseValue(MyPaintBrushSetting id, bool enable, float value) {
bf1d82
  float def = m_brushOriginal.getBaseValue(id);
bf1d82
  if (enable && fabsf(value - def) > 0.01) {
bf1d82
    m_baseValues[id] = value;
bf1d82
    m_brushModified.setBaseValue(id, value);
bf1d82
  } else {
bf1d82
    m_baseValues.erase(id);
bf1d82
    m_brushModified.setBaseValue(id, def);
bf1d82
  }
bf1d82
}
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
void TMyPaintBrushStyle::resetBaseValues() {
bf1d82
  for(int i = 0; i < MYPAINT_BRUSH_SETTINGS_COUNT; ++i)
bf1d82
    setBaseValueEnabled((MyPaintBrushSetting)i, false);
bf1d82
}
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
void TMyPaintBrushStyle::makeIcon(const TDimension &d) {
bf1d82
  TFilePath path = m_fullpath.getParentDir() + (m_fullpath.getWideName() + L"_prev.png");
bf1d82
  if (!m_preview) {
bf1d82
    m_icon = TRaster32P(d);
bf1d82
    m_icon->fill(TPixel32::Red);
bf1d82
  } else
bf1d82
  if (m_preview->getSize() == d) {
bf1d82
    m_icon = m_preview;
bf1d82
  } else {
bf1d82
    m_icon = TRaster32P(d);
bf1d82
    double sx = (double)d.lx/(double)m_preview->getLx();
bf1d82
    double sy = (double)d.ly/(double)m_preview->getLy();
bf1d82
    TRop::resample(m_icon, m_preview, TScale(sx, sy));
bf1d82
  }
bf1d82
bf1d82
  // paint color marker
bf1d82
  if (d.lx > 0 && d.ly > 0) {
bf1d82
    int size = std::min( 1 + std::min(d.lx, d.ly)*2/3,
bf1d82
                         1 + std::max(d.lx, d.ly)/2 );
bf1d82
    TPixel32 color = getMainColor();
bf1d82
    for(int y = 0; y < size; ++y) {
bf1d82
      TPixel32 *p = m_icon->pixels(d.ly - y - 1);
bf1d82
      TPixel32 *endp = p + size - y - 1;
bf1d82
      for( ;p != endp; ++p)
bf1d82
        *p = color;
bf1d82
      *p = blend(*p, color, 0.5);
bf1d82
    }
bf1d82
  }
bf1d82
}
bf1d82
bf1d82
//------------------------------------------------------------
bf1d82
bf1d82
void TMyPaintBrushStyle::loadData(TInputStreamInterface &is) {
bf1d82
  std::string path;
bf1d82
  is >> path;
bf1d82
  is >> m_color;
bf1d82
  loadBrush(TFilePath(path));
bf1d82
bf1d82
  int baseSettingsCount = 0;
bf1d82
  is >> baseSettingsCount;
bf1d82
  for(int i = 0; i < baseSettingsCount; ++i) {
bf1d82
    std::string key;
bf1d82
    double value = 0.0;
bf1d82
    int inputsCount = 0;
bf1d82
    is >> key;
bf1d82
    is >> value;
bf1d82
    const mypaint::Setting *setting = mypaint::Setting::findByKey(key);
bf1d82
    if (setting) setBaseValue(setting->id, value);
bf1d82
  }
bf1d82
}
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
void TMyPaintBrushStyle::saveData(TOutputStreamInterface &os) const {
bf1d82
  std::wstring wstr = m_path.getWideString();
bf1d82
  std::string str;
bf1d82
  str.assign(wstr.begin(), wstr.end());
bf1d82
  os << str;
bf1d82
  os << m_color;
bf1d82
bf1d82
  os << (int)m_baseValues.size();
bf1d82
  for(std::map<mypaintbrushsetting, float="">::const_iterator i = m_baseValues.begin(); i != m_baseValues.end(); ++i) {</mypaintbrushsetting,>
bf1d82
    os << mypaint::Setting::byId(i->first).key;
bf1d82
    os << (double)i->second;
bf1d82
  }
bf1d82
}
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
int TMyPaintBrushStyle::getParamCount() const
bf1d82
  { return MYPAINT_BRUSH_SETTINGS_COUNT; }
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
QString TMyPaintBrushStyle::getParamNames(int index) const
bf1d82
  { return QString::fromUtf8(mypaint::Setting::byId((MyPaintBrushSetting)index).name.c_str()); }
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
TColorStyle::ParamType TMyPaintBrushStyle::getParamType(int index) const
bf1d82
  { return DOUBLE; }
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
bool TMyPaintBrushStyle::hasParamDefault(int index) const
bf1d82
  { return true; }
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
void TMyPaintBrushStyle::setParamDefault(int index)
bf1d82
  { setBaseValueEnabled((MyPaintBrushSetting)index, false); }
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
bool TMyPaintBrushStyle::isParamDefault(int index) const
bf1d82
  { return getBaseValueEnabled((MyPaintBrushSetting)index); }
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
void TMyPaintBrushStyle::getParamRange(int index, double &min, double &max) const {
bf1d82
  const mypaint::Setting &setting = mypaint::Setting::byId((MyPaintBrushSetting)index);
bf1d82
  min = setting.min;
bf1d82
  max = setting.max;
bf1d82
}
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
void TMyPaintBrushStyle::setParamValue(int index, double value)
bf1d82
  { setBaseValue((MyPaintBrushSetting)index, value); }
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
double TMyPaintBrushStyle::getParamValue(double_tag, int index) const
bf1d82
  { return getBaseValue((MyPaintBrushSetting)index); }
bf1d82
bf1d82
//-----------------------------------------------------------------------------
bf1d82
bf1d82
namespace {
bf1d82
  TColorStyle::Declaration mypaintBrushStyle(new TMyPaintBrushStyle());
bf1d82
}