Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/txshpalettelevel.h"
Toshihiro Shimizu 890ddd
#include "toonz/txshleveltypes.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzscene.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
#include "tstream.h"
Toshihiro Shimizu 890ddd
#include "tfilepath_io.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
DEFINE_CLASS_CODE(TXshPaletteLevel, 52)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
PERSIST_IDENTIFIER(TXshPaletteLevel, "paletteLevel")
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// TXshPaletteLevel
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
TXshPaletteLevel::TXshPaletteLevel(std::wstring name)
Shinya Kitaoka 120a6e
    : TXshLevel(m_classCode, name), m_palette(0) {
Shinya Kitaoka 120a6e
  m_type = PLT_XSHLEVEL;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TXshPaletteLevel::~TXshPaletteLevel() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TPalette *TXshPaletteLevel::getPalette() const { return m_palette; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TXshPaletteLevel::setPalette(TPalette *palette) {
Shinya Kitaoka 120a6e
  if (m_palette != palette) {
Shinya Kitaoka 120a6e
    if (m_palette) m_palette->release();
Shinya Kitaoka 120a6e
    m_palette = palette;
Shinya Kitaoka 120a6e
    if (m_palette) m_palette->addRef();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TXshPaletteLevel::setPath(const TFilePath &path) { m_path = path; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TXshPaletteLevel::loadData(TIStream &is) {
Shinya Kitaoka 120a6e
  std::string tagName;
Shinya Kitaoka 120a6e
  while (is.matchTag(tagName)) {
Shinya Kitaoka 120a6e
    if (tagName == "name") {
Shinya Kitaoka 120a6e
      std::wstring name;
Shinya Kitaoka 120a6e
      is >> name;
Shinya Kitaoka 120a6e
      setName(name);
Shinya Kitaoka 120a6e
    } else if (tagName == "path") {
Shinya Kitaoka 120a6e
      is >> m_path;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      throw TException("TXshPaletteLevel, unknown tag: " + tagName);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    is.closeChild();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TXshPaletteLevel::saveData(TOStream &os) {
Shinya Kitaoka 120a6e
  os.child("path") << m_path;
Shinya Kitaoka 120a6e
  os.child("name") << getName();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TXshPaletteLevel::load() {
Shinya Kitaoka 120a6e
  TFilePath path = getScene()->decodeFilePath(m_path);
Shinya Kitaoka 120a6e
  if (TSystem::doesExistFileOrLevel(path)) {
Shinya Kitaoka 120a6e
    TFileStatus fs(path);
Shinya Kitaoka 120a6e
    TPersist *p = 0;
Shinya Kitaoka 120a6e
    TIStream is(path);
shun-iwasawa 995cf6
    TPalette *palette = nullptr;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (is && fs.doesExist()) {
shun-iwasawa 995cf6
      std::string tagName;
shun-iwasawa 995cf6
      if (is.matchTag(tagName) && tagName == "palette") {
shun-iwasawa 995cf6
        std::string gname;
shun-iwasawa 995cf6
        is.getTagParam("name", gname);
shun-iwasawa 995cf6
        palette = new TPalette();
shun-iwasawa 995cf6
        palette->loadData(is);
shun-iwasawa 995cf6
        palette->setGlobalName(::to_wstring(gname));
shun-iwasawa 995cf6
        is.matchEndTag();
shun-iwasawa 995cf6
        palette->setPaletteName(path.getWideName());
shun-iwasawa 995cf6
        setPalette(palette);
shun-iwasawa 995cf6
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    assert(m_palette);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TXshPaletteLevel::save() {
Shinya Kitaoka 120a6e
  TFilePath path = getScene()->decodeFilePath(m_path);
Shinya Kitaoka 120a6e
  if (TSystem::doesExistFileOrLevel(path) && m_palette) {
John Dancel 35a3a8
    TFileStatus fs(path);
John Dancel 35a3a8
    if (!fs.isWritable()) {
John Dancel 35a3a8
      throw TSystemException(
John Dancel 35a3a8
          path, "The palette cannot be saved: it is a read only palette.");
John Dancel 35a3a8
    }
Shinya Kitaoka 120a6e
    TOStream os(path);
Shinya Kitaoka 120a6e
    os << m_palette;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int TXshPaletteLevel::getFrameCount() const { return 0; }