tahoma2d/toonz/sources/common/tparam/tspectrumparam.cpp

537 lines
15 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
// TnzCore includes
#include "tundo.h"
#include "tconvert.h"
#include "tstream.h"
// TnzBase includes
#include "tdoubleparam.h"
// STD includes
#include <set>
#include "tspectrumparam.h"
typedef std::pair<TDoubleParamP, TPixelParamP> ColorKeyParam;
2016-03-19 06:57:51 +13:00
//=========================================================
2016-06-15 18:43:10 +12:00
class TSpectrumParamImp {
TSpectrumParam *m_sp;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::vector<ColorKeyParam> m_keys;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
bool m_draggingEnabled;
bool m_notificationEnabled;
bool m_isMatteEnabled;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::set<TParamObserver *> m_observers;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
TSpectrumParamImp(TSpectrumParam *sp)
: m_sp(sp)
, m_keys()
, m_draggingEnabled(false)
, m_notificationEnabled(true)
, m_isMatteEnabled(true) {}
TSpectrumParamImp(const TSpectrumParamImp &s) { copy(s); }
void copy(const TSpectrumParamImp &src) {
m_keys.clear();
std::vector<ColorKeyParam>::const_iterator it = src.m_keys.begin();
for (; it != src.m_keys.end(); ++it) {
TDoubleParamP s(it->first->clone());
TPixelParamP c(it->second->clone());
m_keys.push_back(std::make_pair(s, c));
}
}
void addKey(const ColorKeyParam &colorKey) {
/*
m_sp->addParam(colorKey.first);
m_sp->addParam(colorKey.second);
2016-03-19 06:57:51 +13:00
*/
2016-06-15 18:43:10 +12:00
m_keys.push_back(colorKey);
}
void insertKey(int index, ColorKeyParam &colorKey) {
/*
m_sp->addParam(colorKey.first);
m_sp->addParam(colorKey.second);
2016-03-19 06:57:51 +13:00
*/
2016-06-15 18:43:10 +12:00
std::vector<ColorKeyParam>::iterator it = m_keys.begin() + index;
m_keys.insert(it, colorKey);
}
void eraseKey(int index) {
std::vector<ColorKeyParam>::iterator colorKeyIt = m_keys.begin() + index;
/*
m_sp->removeParam((*colorKeyIt).first);
m_sp->removeParam((*colorKeyIt).second);
2016-03-19 06:57:51 +13:00
*/
2016-06-15 18:43:10 +12:00
m_keys.erase(colorKeyIt);
}
int getKeyCount() const { return m_keys.size(); }
ColorKeyParam getKey(int index) const { return m_keys[index]; }
void clearKeys() { m_keys.clear(); }
void notify(const TParamChange &change) {
for (std::set<TParamObserver *>::iterator it = m_observers.begin();
it != m_observers.end(); ++it)
(*it)->onChange(change);
}
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
TSpectrumParamImp &operator=(const TSpectrumParamImp &); // not implemented
2016-03-19 06:57:51 +13:00
};
//=========================================================
PERSIST_IDENTIFIER(TSpectrumParam, "spectrumParam")
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
TSpectrumParam::TSpectrumParam()
: m_imp(new TSpectrumParamImp(this)) // brutto...
2016-03-19 06:57:51 +13:00
{
2016-06-15 18:43:10 +12:00
ColorKeyParam ck1(TDoubleParamP(0.0), TPixelParamP(TPixel32::Black));
ColorKeyParam ck2(TDoubleParamP(1.0), TPixelParamP(TPixel32::White));
m_imp->addKey(ck1);
m_imp->addKey(ck2);
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
TSpectrumParam::TSpectrumParam(const TSpectrumParam &src)
2016-06-15 18:43:10 +12:00
: TParam(src.getName()), m_imp(new TSpectrumParamImp(*src.m_imp)) {}
2016-03-19 06:57:51 +13:00
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSpectrumParam::addObserver(TParamObserver *obs) {
m_imp->m_observers.insert(obs);
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSpectrumParam::removeObserver(TParamObserver *obs) {
m_imp->m_observers.erase(obs);
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
TSpectrumParam::TSpectrumParam(int keyCount, TSpectrum::ColorKey keys[])
2016-06-15 18:43:10 +12:00
: m_imp(new TSpectrumParamImp(this)) {
for (int i = 0; i < keyCount; i++) {
double v = keys[i].first;
TPixel32 pix = keys[i].second;
TDoubleParamP dp(v);
TPixelParamP pp(pix);
pp->enableMatte(m_imp->m_isMatteEnabled);
ColorKeyParam ck(dp, pp);
m_imp->addKey(ck);
}
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSpectrumParam::copy(TParam *src) {
TSpectrumParam *p = dynamic_cast<TSpectrumParam *>(src);
if (!p) throw TException("invalid source for copy");
setName(src->getName());
m_imp->copy(*(p->m_imp));
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
TSpectrumParam::~TSpectrumParam() {}
2016-03-19 06:57:51 +13:00
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
TSpectrum TSpectrumParam::getValue(double frame) const {
assert(m_imp);
std::vector<TSpectrum::ColorKey> keys;
int keyCount = m_imp->getKeyCount();
for (int i = 0; i < keyCount; i++) {
ColorKeyParam paramKey = m_imp->getKey(i);
TSpectrum::ColorKey key(paramKey.first->getValue(frame),
paramKey.second->getValue(frame));
keys.push_back(key);
}
return TSpectrum(keys.size(), &keys[0]);
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
TSpectrum64 TSpectrumParam::getValue64(double frame) const {
assert(m_imp);
std::vector<TSpectrum64::ColorKey> keys;
int keyCount = m_imp->getKeyCount();
for (int i = 0; i < keyCount; i++) {
ColorKeyParam paramKey = m_imp->getKey(i);
TSpectrum64::ColorKey key(paramKey.first->getValue(frame),
toPixel64(paramKey.second->getValue(frame)));
keys.push_back(key);
}
return TSpectrum64(keys.size(), &keys[0]);
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSpectrumParam::setValue(double frame, const TSpectrum &spectrum,
bool undoing) {
assert(getKeyCount() == spectrum.getKeyCount());
int keyCount = getKeyCount();
for (int i = 0; i < keyCount; i++) {
TSpectrum::Key key = spectrum.getKey(i);
setValue(frame, i, key.first, key.second, undoing);
}
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
TDoubleParamP TSpectrumParam::getPosition(int index) const {
assert(index <= m_imp->getKeyCount());
return m_imp->getKey(index).first;
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
TPixelParamP TSpectrumParam::getColor(int index) const {
assert(index <= m_imp->getKeyCount());
return m_imp->getKey(index).second;
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
int TSpectrumParam::getKeyCount() const {
assert(m_imp);
return m_imp->getKeyCount();
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSpectrumParam::setValue(double frame, int index, double s,
const TPixel32 &color, bool undoing) {
assert(m_imp);
int keyCount = m_imp->getKeyCount();
if (index < 0 || index >= keyCount)
throw TException("TSpectrumParam::setValue. Index out of range");
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
ColorKeyParam key = m_imp->getKey(index);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// beginParameterChange();
key.first->setValue(frame, s);
key.second->setValue(frame, color);
// endParameterChange();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_imp->notify(TParamChange(this, TParamChange::m_minFrame,
TParamChange::m_maxFrame, true,
m_imp->m_draggingEnabled, false));
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSpectrumParam::setDefaultValue(const TSpectrum &value) {
assert(value.getKeyCount() == getKeyCount());
for (int i = 0; i < getKeyCount(); i++) {
ColorKeyParam dstKeyParam = m_imp->getKey(i);
TSpectrum::Key srcKey = value.getKey(i);
dstKeyParam.first->setDefaultValue(srcKey.first);
dstKeyParam.second->setDefaultValue(srcKey.second);
}
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSpectrumParam::insertKey(int index, double s, const TPixel32 &color) {
assert(m_imp);
int keyCount = m_imp->getKeyCount();
if (index < 0)
index = 0;
else if (index >= keyCount)
index = keyCount;
TDoubleParamP dp(s);
TPixelParamP pp(color);
pp->enableMatte(m_imp->m_isMatteEnabled);
ColorKeyParam ck(dp, pp);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_imp->insertKey(index, ck);
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSpectrumParam::addKey(double s, const TPixel32 &color) {
/*
assert(m_imp);
insertKey(m_imp->getKeyCount(), s,color);
2016-03-19 06:57:51 +13:00
*/
2016-06-15 18:43:10 +12:00
int index = m_imp->getKeyCount();
assert(m_imp);
int keyCount = m_imp->getKeyCount();
if (index < 0)
index = 0;
else if (index >= keyCount)
index = keyCount;
TDoubleParamP dp(s);
TPixelParamP pp(color);
pp->enableMatte(m_imp->m_isMatteEnabled);
ColorKeyParam ck(dp, pp);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_imp->insertKey(index, ck);
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSpectrumParam::removeKey(int index) {
assert(m_imp);
int keyCount = m_imp->getKeyCount();
if (index < 0 || index >= keyCount)
throw TException("TSpectrumParam::removeKey. Index out of range");
m_imp->eraseKey(index);
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool TSpectrumParam::isKeyframe(double frame) const {
int keyCount = m_imp->getKeyCount();
for (int i = 0; i < keyCount; i++) {
ColorKeyParam currentKey = m_imp->getKey(i);
if (currentKey.first->isKeyframe(frame)) return true;
if (currentKey.second->isKeyframe(frame)) return true;
}
return false;
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSpectrumParam::deleteKeyframe(double frame) {
int keyCount = m_imp->getKeyCount();
for (int i = 0; i < keyCount; i++) {
ColorKeyParam currentKey = m_imp->getKey(i);
currentKey.first->deleteKeyframe(frame);
currentKey.second->deleteKeyframe(frame);
}
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSpectrumParam::clearKeyframes() {
assert(m_imp);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int k, keyCount = m_imp->getKeyCount();
for (k = 0; k < keyCount; ++k) {
const ColorKeyParam &key = m_imp->getKey(k);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
key.first->clearKeyframes();
key.second->clearKeyframes();
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_imp->notify(TParamChange(this, TParamChange::m_minFrame,
TParamChange::m_maxFrame, true,
m_imp->m_draggingEnabled, false));
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSpectrumParam::assignKeyframe(double frame, const TParamP &src,
double srcFrame, bool changedOnly) {
TSpectrumParamP spectrum = src;
if (!spectrum) return;
int keyCount = m_imp->getKeyCount();
if (keyCount != spectrum->m_imp->getKeyCount()) return;
for (int i = 0; i < keyCount; i++) {
ColorKeyParam dstKey = m_imp->getKey(i);
ColorKeyParam srcKey = spectrum->m_imp->getKey(i);
dstKey.first->setValue(frame, srcKey.first->getValue(srcFrame));
dstKey.second->setValue(frame, srcKey.second->getValue(srcFrame));
}
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSpectrumParam::loadData(TIStream &is) {
assert(m_imp);
m_imp->clearKeys();
std::string tagName;
is.openChild(tagName);
assert(tagName == "spectrum");
while (!is.eos()) {
TDoubleParamP pos(0.0);
TPixelParamP color(TPixel32::Black);
is.openChild(tagName);
pos->loadData(is);
is.closeChild();
is.openChild(tagName);
color->loadData(is);
is.closeChild();
ColorKeyParam ck(pos, color);
m_imp->addKey(ck);
}
is.closeChild();
}
//---------------------------------------------------------
void TSpectrumParam::saveData(TOStream &os) {
assert(m_imp);
int keyCount = m_imp->getKeyCount();
os.openChild("spectrum");
for (int i = 0; i < keyCount; i++) {
ColorKeyParam key = m_imp->getKey(i);
os.openChild("s_value");
key.first->saveData(os);
os.closeChild();
os.openChild("col_value");
key.second->saveData(os);
os.closeChild();
}
os.closeChild();
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSpectrumParam::enableDragging(bool on) { m_imp->m_draggingEnabled = on; }
2016-03-19 06:57:51 +13:00
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSpectrumParam::enableNotification(bool on) {
m_imp->m_notificationEnabled = on;
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool TSpectrumParam::isNotificationEnabled() const {
return m_imp->m_notificationEnabled;
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
namespace {
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
inline std::string to_string(const TPixel32 &color) {
std::string alias = "(";
alias += std::to_string(color.r) + ",";
alias += std::to_string(color.g) + ",";
alias += std::to_string(color.b) + ",";
alias += std::to_string(color.m);
alias += ")";
return alias;
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
inline std::string toString(const TSpectrum::ColorKey &key, int precision) {
std::string alias = "(";
alias += ::to_string(key.first, precision) + ",";
alias += to_string(key.second);
alias += ")";
return alias;
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
} // namespace
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
std::string TSpectrumParam::getValueAlias(double frame, int precision) {
std::vector<TSpectrum::ColorKey> keys;
int keyCount = m_imp->getKeyCount();
for (int i = 0; i < keyCount; i++) {
ColorKeyParam paramKey = m_imp->getKey(i);
TSpectrum::ColorKey key(paramKey.first->getValue(frame),
paramKey.second->getValue(frame));
keys.push_back(key);
}
std::string alias = "(";
if (!keys.empty()) {
std::vector<TSpectrum::ColorKey>::iterator it = keys.begin();
std::vector<TSpectrum::ColorKey>::iterator end = keys.begin();
std::advance(end, keys.size() - 1);
for (; it != end; ++it) {
alias += toString(*it, precision);
alias += ",";
}
alias += toString(*it, precision);
}
alias += ")";
return alias;
2016-03-19 06:57:51 +13:00
}
//=========================================================
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSpectrumParam::enableMatte(bool on) { m_imp->m_isMatteEnabled = on; }
2016-03-19 06:57:51 +13:00
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool TSpectrumParam::isMatteEnabled() const { return m_imp->m_isMatteEnabled; }
2016-03-19 06:57:51 +13:00
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool TSpectrumParam::hasKeyframes() const {
int keyCount = m_imp->getKeyCount();
for (int i = 0; i < keyCount; i++) {
ColorKeyParam currentKey = m_imp->getKey(i);
if (currentKey.first->hasKeyframes() || currentKey.second->hasKeyframes())
return true;
}
return false;
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TSpectrumParam::getKeyframes(std::set<double> &frames) const {
int keyCount = m_imp->getKeyCount();
for (int i = 0; i < keyCount; i++) {
ColorKeyParam currentKey = m_imp->getKey(i);
currentKey.first->getKeyframes(frames);
currentKey.second->getKeyframes(frames);
}
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
int TSpectrumParam::getNextKeyframe(double frame) const {
std::set<double> frames;
getKeyframes(frames);
std::set<double>::iterator it = frames.upper_bound(frame);
if (it == frames.end())
return -1;
else
return std::distance(frames.begin(), it);
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
int TSpectrumParam::getPrevKeyframe(double frame) const {
std::set<double> frames;
getKeyframes(frames);
std::set<double>::iterator it = frames.lower_bound(frame);
if (it == frames.begin())
return -1;
else {
--it;
return std::distance(frames.begin(), it);
}
2016-03-19 06:57:51 +13:00
}
//---------------------------------------------------------
2016-06-15 18:43:10 +12:00
double TSpectrumParam::keyframeIndexToFrame(int index) const {
std::set<double> frames;
getKeyframes(frames);
assert(0 <= index && index < (int)frames.size());
std::set<double>::const_iterator it = frames.begin();
std::advance(it, index);
return *it;
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
TIStream &operator>>(TIStream &in, TSpectrumParamP &p) {
TPersist *tmp;
in >> tmp;
p = TSpectrumParamP(dynamic_cast<TSpectrumParam *>(tmp));
return in;
2016-03-19 06:57:51 +13:00
}