tahoma2d/toonz/sources/toonzlib/columnfan.cpp

234 lines
6.2 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()
2019-09-26 14:03:35 +12:00
: m_firstFreePos(0)
, m_unfolded(74)
, m_folded(9)
, m_cameraActive(true)
, m_cameraColumnDim(22) {}
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-03-19 06:57:51 +13:00
2019-09-26 14:03:35 +12:00
void ColumnFan::setDimensions(int unfolded, int cameraColumn) {
m_unfolded = unfolded;
m_cameraColumnDim = cameraColumn;
// 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
2019-09-26 14:03:35 +12:00
? m_cameraColumnDim
2019-03-23 02:09:34 +13:00
: ((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-09-26 14:03:35 +12:00
if (col < -1) return -m_cameraColumnDim;
2019-03-23 02:09:34 +13:00
if (col < 0) return 0;
firstCol =
m_cameraActive
2019-09-26 14:03:35 +12:00
? m_cameraColumnDim
2019-03-23 02:09:34 +13:00
: ((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
}
2019-11-15 22:36:29 +13:00
//-----------------------------------------------------------------------------
void ColumnFan::rollLeftFoldedState(int index, int count) {
assert(index >= 0);
int columnCount = m_columns.size();
if (columnCount <= index) return;
if (index + count - 1 > columnCount) count = columnCount - index + 1;
if (count < 2) return;
int i = index, j = index + count - 1;
bool tmp = isActive(i);
for (int k = i; k < j; ++k) {
if (isActive(k) && !isActive(k + 1))
deactivate(k);
else if (!isActive(k) && isActive(k + 1))
activate(k);
}
if (isActive(j) && !tmp)
deactivate(j);
else if (!isActive(j) && tmp)
activate(j);
update();
}
//-----------------------------------------------------------------------------
void ColumnFan::rollRightFoldedState(int index, int count) {
assert(index >= 0);
int columnCount = m_columns.size();
if (columnCount <= index) return;
if (index + count - 1 > columnCount) count = columnCount - index + 1;
if (count < 2) return;
int i = index, j = index + count - 1;
bool tmp = isActive(j);
for (int k = j; k > i; --k) {
if (isActive(k) && !isActive(k - 1))
deactivate(k);
else if (!isActive(k) && isActive(k - 1))
activate(k);
}
if (isActive(i) && !tmp)
deactivate(i);
else if (!isActive(i) && tmp)
activate(i);
update();
}