Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/columnfan.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tstream.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// STD includss
Toshihiro Shimizu 890ddd
#include <assert.h></assert.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Jaroslav 203cc8
// ColumnFan
Toshihiro Shimizu 890ddd
Jaroslav 203cc8
ColumnFan::ColumnFan() : m_firstFreePos(0), m_unfolded(74), m_folded(9) {}
Toshihiro Shimizu 890ddd
Jaroslav 203cc8
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Jaroslav 203cc8
void ColumnFan::setDimension(int unfolded) {
Jaroslav 203cc8
  m_unfolded = unfolded;
Jaroslav 203cc8
  // folded always 9
Jaroslav 203cc8
  update();
Jaroslav 203cc8
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ColumnFan::update() {
Jaroslav 203cc8
  int lastPos     = -m_unfolded;
Shinya Kitaoka 120a6e
  bool lastActive = true;
Shinya Kitaoka 120a6e
  int m           = m_columns.size();
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < m; i++) {
Shinya Kitaoka 120a6e
    bool active = m_columns[i].m_active;
Shinya Kitaoka 120a6e
    if (lastActive)
Jaroslav 203cc8
      lastPos += m_unfolded;
Shinya Kitaoka 120a6e
    else if (active)
Jaroslav 203cc8
      lastPos += m_folded;
Shinya Kitaoka 120a6e
    m_columns[i].m_pos = lastPos;
Shinya Kitaoka 120a6e
    lastActive         = active;
Shinya Kitaoka 120a6e
  }
Jaroslav 203cc8
  m_firstFreePos = lastPos + (lastActive ? m_unfolded : m_folded);
Shinya Kitaoka 120a6e
  m_table.clear();
Shinya Kitaoka 120a6e
  for (i = 0; i < m; i++)
Shinya Kitaoka 120a6e
    if (m_columns[i].m_active)
Jaroslav 203cc8
      m_table[m_columns[i].m_pos + m_unfolded - 1] = i;
Shinya Kitaoka 120a6e
    else if (i + 1 < m && m_columns[i + 1].m_active)
Shinya Kitaoka 120a6e
      m_table[m_columns[i + 1].m_pos - 1] = i;
Shinya Kitaoka 120a6e
    else if (i + 1 == m)
Shinya Kitaoka 120a6e
      m_table[m_firstFreePos - 1] = i;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Jaroslav 203cc8
int ColumnFan::layerAxisToCol(int coord) const {
Jaroslav 203cc8
  if (coord < m_firstFreePos) {
Jaroslav 203cc8
    std::map<int, int="">::const_iterator it = m_table.lower_bound(coord);</int,>
Shinya Kitaoka 120a6e
    if (it == m_table.end()) return -3;
Shinya Kitaoka 120a6e
    assert(it != m_table.end());
Shinya Kitaoka 120a6e
    return it->second;
Shinya Kitaoka 120a6e
  } else
Jaroslav 203cc8
    return m_columns.size() + (coord - m_firstFreePos) / m_unfolded;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Jaroslav 203cc8
int ColumnFan::colToLayerAxis(int col) const {
Shinya Kitaoka 120a6e
  int m = m_columns.size();
Shinya Kitaoka 120a6e
  if (col >= 0 && col < m)
Shinya Kitaoka 120a6e
    return m_columns[col].m_pos;
Shinya Kitaoka 120a6e
  else
Jaroslav 203cc8
    return m_firstFreePos + (col - m) * m_unfolded;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ColumnFan::activate(int col) {
Shinya Kitaoka 120a6e
  int m = m_columns.size();
Shinya Kitaoka 120a6e
  if (col < m) {
Shinya Kitaoka 120a6e
    m_columns[col].m_active = true;
Shinya Kitaoka 120a6e
    int i;
Shinya Kitaoka 120a6e
    for (i = m - 1; i >= 0 && m_columns[i].m_active; i--) {
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    i++;
Shinya Kitaoka 120a6e
    if (i < m) {
Shinya Kitaoka 120a6e
      m = i;
Shinya Kitaoka 120a6e
      m_columns.erase(m_columns.begin() + i, m_columns.end());
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ColumnFan::deactivate(int col) {
Shinya Kitaoka 120a6e
  while ((int)m_columns.size() <= col) m_columns.push_back(Column());
Shinya Kitaoka 120a6e
  m_columns[col].m_active = false;
Shinya Kitaoka 120a6e
  update();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool ColumnFan::isActive(int col) const {
Shinya Kitaoka 120a6e
  return 0 <= col && col < (int)m_columns.size() ? m_columns[col].m_active
Shinya Kitaoka 120a6e
                                                 : true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool ColumnFan::isEmpty() const { return m_columns.empty(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Jaroslav 203cc8
void ColumnFan::copyFoldedStateFrom(const ColumnFan &from) {
Jaroslav 203cc8
  for (int i = 0, n = (int)from.m_columns.size(); i < n; i++)
Jaroslav 203cc8
    if (!from.isActive(i)) deactivate(i);
Jaroslav 203cc8
}
Jaroslav 203cc8
Jaroslav 203cc8
//-----------------------------------------------------------------------------
Jaroslav 203cc8
Jaroslav 203cc8
void ColumnFan::saveData(
Jaroslav 203cc8
    TOStream &os) {  // only saves indices of folded columns
Shinya Kitaoka 120a6e
  int index, n = (int)m_columns.size();
Shinya Kitaoka 120a6e
  for (index = 0; index < n;) {
Shinya Kitaoka 120a6e
    while (index < n && m_columns[index].m_active) index++;
Shinya Kitaoka 120a6e
    if (index < n) {
Shinya Kitaoka 120a6e
      int firstIndex = index;
Shinya Kitaoka 120a6e
      os << index;
Shinya Kitaoka 120a6e
      index++;
Shinya Kitaoka 120a6e
      while (index < n && !m_columns[index].m_active) index++;
Shinya Kitaoka 120a6e
      os << index - firstIndex;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ColumnFan::loadData(TIStream &is) {
Shinya Kitaoka 120a6e
  m_columns.clear();
Shinya Kitaoka 120a6e
  m_table.clear();
Shinya Kitaoka 120a6e
  m_firstFreePos = 0;
Shinya Kitaoka 120a6e
  while (!is.eos()) {
Shinya Kitaoka 120a6e
    int index = 0, count = 0;
Shinya Kitaoka 120a6e
    is >> index >> count;
Shinya Kitaoka 120a6e
    int j;
Shinya Kitaoka 120a6e
    for (j = 0; j < count; j++) deactivate(index + j);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}