tahoma2d/toonz/sources/toonzlib/columnfan.cpp

176 lines
4.8 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include "toonz/columnfan.h"
2019-03-23 02:09:34 +13:00
#include "toonz/preferences.h"
2016-03-19 06:57:51 +13:00
// TnzCore includes
#include "tstream.h"
// STD includss
#include <assert.h>
//=============================================================================
// ColumnFan
2016-03-19 06:57:51 +13:00
2019-03-23 02:09:34 +13:00
ColumnFan::ColumnFan()
: m_firstFreePos(0), m_unfolded(74), m_folded(9), m_cameraActive(true) {}
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-03-19 06:57:51 +13:00
void ColumnFan::setDimension(int unfolded) {
m_unfolded = unfolded;
// folded always 9
update();
}
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void ColumnFan::update() {
int lastPos = -m_unfolded;
2016-06-15 18:43:10 +12:00
bool lastActive = true;
int m = m_columns.size();
int i;
for (i = 0; i < m; i++) {
bool active = m_columns[i].m_active;
if (lastActive)
lastPos += m_unfolded;
2016-06-15 18:43:10 +12:00
else if (active)
lastPos += m_folded;
2016-06-15 18:43:10 +12:00
m_columns[i].m_pos = lastPos;
lastActive = active;
}
m_firstFreePos = lastPos + (lastActive ? m_unfolded : m_folded);
2016-06-15 18:43:10 +12:00
m_table.clear();
for (i = 0; i < m; i++)
if (m_columns[i].m_active)
m_table[m_columns[i].m_pos + m_unfolded - 1] = i;
2016-06-15 18:43:10 +12:00
else if (i + 1 < m && m_columns[i + 1].m_active)
m_table[m_columns[i + 1].m_pos - 1] = i;
else if (i + 1 == m)
m_table[m_firstFreePos - 1] = i;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
int ColumnFan::layerAxisToCol(int coord) const {
if (Preferences::instance()->isXsheetCameraColumnVisible()) {
2019-03-23 02:09:34 +13:00
int firstCol =
m_cameraActive
? m_unfolded
: ((m_columns.size() > 0 && !m_columns[0].m_active) ? 0 : m_folded);
if (coord < firstCol) return -1;
coord -= firstCol;
}
if (coord < m_firstFreePos) {
std::map<int, int>::const_iterator it = m_table.lower_bound(coord);
2016-06-15 18:43:10 +12:00
if (it == m_table.end()) return -3;
assert(it != m_table.end());
return it->second;
} else
return m_columns.size() + (coord - m_firstFreePos) / m_unfolded;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
int ColumnFan::colToLayerAxis(int col) const {
2019-03-23 02:09:34 +13:00
int m = m_columns.size();
int firstCol = 0;
if (Preferences::instance()->isXsheetCameraColumnVisible()) {
2019-03-23 02:09:34 +13:00
if (col < -1) return -m_unfolded;
if (col < 0) return 0;
firstCol =
m_cameraActive
? m_unfolded
: ((m_columns.size() > 0 && !m_columns[0].m_active) ? 0 : m_folded);
}
2016-06-15 18:43:10 +12:00
if (col >= 0 && col < m)
2019-03-23 02:09:34 +13:00
return firstCol + m_columns[col].m_pos;
2016-06-15 18:43:10 +12:00
else
2019-03-23 02:09:34 +13:00
return firstCol + m_firstFreePos + (col - m) * m_unfolded;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void ColumnFan::activate(int col) {
int m = m_columns.size();
2019-03-23 02:09:34 +13:00
if (col < 0) {
m_cameraActive = true;
return;
}
2016-06-15 18:43:10 +12:00
if (col < m) {
m_columns[col].m_active = true;
int i;
for (i = m - 1; i >= 0 && m_columns[i].m_active; i--) {
}
i++;
if (i < m) {
m = i;
m_columns.erase(m_columns.begin() + i, m_columns.end());
}
}
update();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void ColumnFan::deactivate(int col) {
2019-03-23 02:09:34 +13:00
if (col < 0) {
m_cameraActive = false;
return;
}
2016-06-15 18:43:10 +12:00
while ((int)m_columns.size() <= col) m_columns.push_back(Column());
m_columns[col].m_active = false;
update();
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool ColumnFan::isActive(int col) const {
2019-03-23 02:09:34 +13:00
return 0 <= col && col < (int)m_columns.size()
? m_columns[col].m_active
: col < 0 ? m_cameraActive : true;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool ColumnFan::isEmpty() const { return m_columns.empty(); }
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
void ColumnFan::copyFoldedStateFrom(const ColumnFan &from) {
2019-03-23 02:09:34 +13:00
m_cameraActive = from.m_cameraActive;
for (int i = 0, n = (int)from.m_columns.size(); i < n; i++)
if (!from.isActive(i)) deactivate(i);
}
//-----------------------------------------------------------------------------
void ColumnFan::saveData(
TOStream &os) { // only saves indices of folded columns
2016-06-15 18:43:10 +12:00
int index, n = (int)m_columns.size();
for (index = 0; index < n;) {
while (index < n && m_columns[index].m_active) index++;
if (index < n) {
int firstIndex = index;
os << index;
index++;
while (index < n && !m_columns[index].m_active) index++;
os << index - firstIndex;
}
}
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void ColumnFan::loadData(TIStream &is) {
m_columns.clear();
m_table.clear();
m_firstFreePos = 0;
while (!is.eos()) {
int index = 0, count = 0;
is >> index >> count;
int j;
for (j = 0; j < count; j++) deactivate(index + j);
}
2016-03-19 06:57:51 +13:00
}