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
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const int colWidth = 74;
Shinya Kitaoka 120a6e
const int colSkip  = 9;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// ColumnFan
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ColumnFan::ColumnFan() : m_firstFreePos(0) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ColumnFan::update() {
Shinya Kitaoka 120a6e
  int lastPos     = -colWidth;
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)
Shinya Kitaoka 120a6e
      lastPos += colWidth;
Shinya Kitaoka 120a6e
    else if (active)
Shinya Kitaoka 120a6e
      lastPos += colSkip;
Shinya Kitaoka 120a6e
    m_columns[i].m_pos = lastPos;
Shinya Kitaoka 120a6e
    lastActive         = active;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  m_firstFreePos = lastPos + (lastActive ? colWidth : colSkip);
Shinya Kitaoka 120a6e
  m_table.clear();
Shinya Kitaoka 120a6e
  for (i = 0; i < m; i++)
Shinya Kitaoka 120a6e
    if (m_columns[i].m_active)
Shinya Kitaoka 120a6e
      m_table[m_columns[i].m_pos + colWidth - 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
Shinya Kitaoka 120a6e
int ColumnFan::xToCol(int x) const {
Shinya Kitaoka 120a6e
  if (x < m_firstFreePos) {
Shinya Kitaoka 120a6e
    std::map<int, int="">::const_iterator it = m_table.lower_bound(x);</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
Shinya Kitaoka 120a6e
    return m_columns.size() + (x - m_firstFreePos) / colWidth;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int ColumnFan::colToX(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
Shinya Kitaoka 120a6e
    return m_firstFreePos + (col - m) * colWidth;
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
Shinya Kitaoka 120a6e
void ColumnFan::saveData(TOStream &os) {
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
}