tahoma2d/toonz/sources/toonzlib/skeleton.cpp

156 lines
4.6 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include "toonz/skeleton.h"
#include "toonz/tstageobject.h"
#include "toonz/tpinnedrangeset.h"
#include "toonz/tstageobjectkeyframe.h"
#include "toonz/txsheet.h"
#include "toonz/stage.h"
2016-06-15 18:43:10 +12:00
namespace {
TStageObjectId getAncestor(TXsheet *xsh, TStageObjectId id) {
assert(id.isColumn());
TStageObjectId parentId;
while (parentId = xsh->getStageObjectParent(id), parentId.isColumn())
id = parentId;
return id;
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
int Skeleton::Bone::getColumnIndex() const {
return m_stageObject->getId().getIndex();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void Skeleton::Bone::setParent(Bone *parent) {
if (m_parent != parent) {
m_parent = parent;
parent->m_children.push_back(this);
}
2016-03-19 06:57:51 +13:00
}
//=============================================================================
2016-06-15 18:43:10 +12:00
Skeleton::Skeleton() : m_rootBone(0) {}
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
Skeleton::~Skeleton() { clearPointerContainer(m_bones); }
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void Skeleton::clear() {
clearPointerContainer(m_bones);
m_rootBone = 0;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void Skeleton::build(TXsheet *xsh, int row, int col,
const std::set<int> &tempPinnedColumns) {
// clear
m_rootBone = 0;
clearPointerContainer(m_bones);
// antenato (colonna) della colonna corrente
TStageObjectId ancestorId = getAncestor(xsh, TStageObjectId::ColumnId(col));
// costruisco le "ossa"
std::map<TStageObjectId, Bone *> boneTable;
int columnCount = xsh->getColumnCount();
int pinnedCount = 0;
for (int i = 0; i < columnCount; i++) {
TStageObjectId columnId(TStageObjectId::ColumnId(i));
if (getAncestor(xsh, columnId) != ancestorId) continue;
TAffine aff = xsh->getPlacement(columnId, row);
TPointD center = Stage::inch * xsh->getCenter(columnId, row);
TPointD p = aff * center;
TStageObject *obj = xsh->getStageObject(columnId);
Bone *bone = new Bone(obj, p);
boneTable[columnId] = bone;
m_bones.push_back(bone);
if (columnId == ancestorId) m_rootBone = bone;
if (obj->getPinnedRangeSet()->isPinned(row)) {
pinnedCount++;
bone->setPinnedStatus(Bone::PINNED);
} else if (tempPinnedColumns.count(i) > 0)
bone->setPinnedStatus(Bone::TEMP_PINNED);
}
// if no bone is pinned then the root is considered pinned
if (pinnedCount == 0 && m_rootBone) m_rootBone->setPinnedStatus(Bone::PINNED);
// The skeleton could possibly be empty
if (boneTable.empty()) {
assert(!m_rootBone);
return;
}
// assign parents
std::map<TStageObjectId, Bone *>::iterator it, sit;
for (it = boneTable.begin(); it != boneTable.end(); ++it) {
sit = boneTable.find(xsh->getStageObjectParent(it->first));
if (sit != boneTable.end()) it->second->setParent(sit->second);
}
// select the "active chain", i.e. the starting bone (columnIndex=col) and the
// ancestors
it = boneTable.find(TStageObjectId::ColumnId(col));
if (it != boneTable.end()) {
Bone *bone = it->second;
while (bone) {
bone->select();
bone = bone->getParent();
}
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
Skeleton::Bone *Skeleton::getBone(int index) const {
assert(0 <= index && index < (int)m_bones.size());
return m_bones[index];
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
Skeleton::Bone *Skeleton::getBoneByColumnIndex(int columnIndex) const {
for (int i = 0; i < (int)m_bones.size(); i++)
if (m_bones[i]->getColumnIndex() == columnIndex) return m_bones[i];
return 0;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool Skeleton::isIKEnabled() const {
return m_rootBone &&
m_rootBone->getStageObject()->getStatus() == TStageObject::IK;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool Skeleton::hasPinnedRanges() const {
for (int i = 0; i < getBoneCount(); i++) {
TStageObject *obj = getBone(i)->getStageObject();
if (obj->getPinnedRangeSet()->getRangeCount() > 0) return true;
}
return false;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void Skeleton::clearAllPinnedRanges() {
for (int i = 0; i < getBoneCount(); i++) {
TStageObject *obj = getBone(i)->getStageObject();
obj->getPinnedRangeSet()->removeAllRanges();
obj->invalidate();
}
2016-03-19 06:57:51 +13:00
}