tahoma2d/toonz/sources/tnztools/plastictool_animate.cpp

248 lines
7.6 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
// TnzCore includes
#include "tundo.h"
// TnzLib includes
#include "toonz/tobjecthandle.h"
#include "plastictool.h"
using namespace PlasticToolLocals;
//****************************************************************************************
// Undo definitions
//****************************************************************************************
2016-06-15 18:43:10 +12:00
namespace {
2016-03-19 06:57:51 +13:00
class AnimateValuesUndo final : public TUndo {
2016-06-15 18:43:10 +12:00
int m_row, m_col; //!< Xsheet coordinates
int m_v; //!< Moved vertex
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
SkDKey m_oldValues, m_newValues; //!< Keyframe values
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
AnimateValuesUndo(int v) : m_row(::row()), m_col(::column()), m_v(v) {}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Again, not accurate. We should get in the details of SkDF... So, let's say
// around 10 kB - max 10k instances in the standard undos pool.
2016-06-19 20:06:29 +12:00
int getSize() const override { return 10 << 10; }
2016-03-19 06:57:51 +13:00
2016-06-19 20:06:29 +12:00
void redo() const override {
2016-06-15 18:43:10 +12:00
PlasticTool::TemporaryActivation tempActivate(m_row, m_col);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_v >= 0) l_plasticTool.setSkeletonSelection(m_v);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
l_suspendParamsObservation =
true; // Coalesce params change notifications into one
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
l_plasticTool.deformation()->deleteKeyframe(m_row - 1);
l_plasticTool.deformation()->setKeyframe(m_newValues);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
l_suspendParamsObservation = false;
l_plasticTool.onChange();
}
2016-03-19 06:57:51 +13:00
2016-06-19 20:06:29 +12:00
void undo() const override {
2016-06-15 18:43:10 +12:00
PlasticTool::TemporaryActivation tempActivate(m_row, m_col);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_v >= 0) l_plasticTool.setSkeletonSelection(m_v);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
l_suspendParamsObservation =
true; // Coalesce params change notifications into one
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
l_plasticTool.deformation()->deleteKeyframe(
m_row - 1); // Yep. Typical frame/row shift... xD
l_plasticTool.deformation()->setKeyframe(m_oldValues);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
l_suspendParamsObservation = false;
l_plasticTool.onChange();
}
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
//****************************************************************************************
// PlasticTool functions
//****************************************************************************************
2016-06-15 18:43:10 +12:00
void PlasticTool::mouseMove_animate(const TPointD &pos, const TMouseEvent &me) {
// Track mouse position
m_pos = pos; // Needs to be done now - ensures m_pos is valid
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_svHigh = m_seHigh = -1; // Reset highlighted primitives
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_sd) {
double d, highlightRadius = getPixelSize() * HIGHLIGHT_DISTANCE;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Look for nearest vertex
int v = deformedSkeleton().closestVertex(pos, &d);
if (v >= 0 && d < highlightRadius) m_svHigh = v;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
invalidate();
}
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::leftButtonDown_animate(const TPointD &pos,
const TMouseEvent &me) {
// Track mouse position
m_pressedPos = m_pos = pos;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
setSkeletonSelection(m_svHigh);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_svSel.hasSingleObject()) {
// Store original vertex position and keyframe values
m_pressedVxsPos =
std::vector<TPointD>(1, deformedSkeleton().vertex(m_svSel).P());
m_sd->getKeyframeAt(frame(), m_pressedSkDF);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
invalidate();
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::leftButtonDrag_animate(const TPointD &pos,
const TMouseEvent &me) {
// Track mouse position
m_pos = pos;
if (m_sd && m_svSel.hasSingleObject() &&
m_svSel > 0) // Avoid move if vertex is root
{
l_suspendParamsObservation = true; // Automatic params notification happen
// twice (1 x param) - dealing with it manually
double frame = ::frame();
// First, retrieve selected vertex's deformation
SkVD *vd = m_sd->vertexDeformation(::skeletonId(), m_svSel);
assert(vd);
// Move selected branch
if (m_keepDistance.getValue()) {
::setKeyframe(vd->m_params[SkVD::ANGLE],
frame); // Set a keyframe for it. It must be done
// to set the correct function interpolation
// type and other stuff.
m_sd->updateAngle(*skeleton(), deformedSkeleton(), frame, m_svSel, pos);
} else {
::setKeyframe(vd->m_params[SkVD::ANGLE],
frame); // Same here. NOTE: Not setting a frame on
::setKeyframe(vd->m_params[SkVD::DISTANCE],
frame); // vd directly due to SkVD::SO
m_sd->updatePosition(*skeleton(), deformedSkeleton(), frame, m_svSel,
pos);
}
l_suspendParamsObservation = false;
// onChange(); // Due to
// a nasty Function Editor dependency,
// it's better to call the following directly
m_deformedSkeleton.invalidate();
invalidate();
}
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::leftButtonUp_animate(const TPointD &pos,
const TMouseEvent &me) {
// Track mouse position
m_pos = pos;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (m_svSel.hasSingleObject() && m_dragged) {
// Set a keyframe to each skeleton vertex, if that was requested
if (m_globalKey.getValue())
::setKeyframe(m_sd, ::frame()); // Already invokes keyframes rebuild
else
stageObject()->updateKeyframes(); // Otherwise, must be explicit
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Add a corresponding undo
AnimateValuesUndo *undo = new AnimateValuesUndo(m_svSel);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
undo->m_oldValues = m_pressedSkDF;
m_sd->getKeyframeAt(frame(), undo->m_newValues);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TUndoManager::manager()->add(undo);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// This is needed to refresh the xsheet (there may be new keyframes)
TTool::getApplication()->getCurrentObject()->notifyObjectIdChanged(false);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// In case one of the vertices is attached (as a hook) to an external
// position,
// we need to update the whole skeleton according to the updated vertex.
updateMatrix();
invalidate();
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::addContextMenuActions_animate(QMenu *menu) {
bool ret = true;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!m_svSel.isEmpty()) {
QAction *setKey = menu->addAction(tr("Set Key"));
ret = ret && connect(setKey, SIGNAL(triggered()), &l_plasticTool,
SLOT(setKey_undo()));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
QAction *setRestKey = menu->addAction(tr("Set Rest Key"));
ret = ret && connect(setRestKey, SIGNAL(triggered()), &l_plasticTool,
SLOT(setRestKey_undo()));
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
QAction *setGlobalKey = menu->addAction(tr("Set Global Key"));
ret = ret && connect(setGlobalKey, SIGNAL(triggered()), &l_plasticTool,
SLOT(setGlobalKey_undo()));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
QAction *setGlobalRestKey = menu->addAction(tr("Set Global Rest Key"));
ret = ret && connect(setGlobalRestKey, SIGNAL(triggered()), &l_plasticTool,
SLOT(setGlobalRestKey_undo()));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
menu->addSeparator();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
assert(ret);
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::keyFunc_undo(void (PlasticTool::*keyFunc)()) {
assert(m_svSel.objects().size() <= 1);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
double frame = ::frame();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
AnimateValuesUndo *undo = new AnimateValuesUndo(m_svSel);
m_sd->getKeyframeAt(frame, undo->m_oldValues);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
(this->*keyFunc)();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_sd->getKeyframeAt(frame, undo->m_newValues);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TUndoManager::manager()->add(undo);
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void PlasticTool::draw_animate() {
double pixelSize = getPixelSize();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
PlasticSkeleton &deformedSkeleton = this->deformedSkeleton();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Draw deformed skeleton
if (m_sd) {
drawOnionSkinSkeletons_animate(pixelSize);
drawSkeleton(deformedSkeleton, pixelSize);
drawSelections(m_sd, deformedSkeleton, pixelSize);
drawAngleLimits(m_sd, m_skelId, m_svSel, pixelSize);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
drawHighlights(m_sd, &deformedSkeleton, pixelSize);
2016-03-19 06:57:51 +13:00
}