Rev 11 | Blame | Last modification | View Log | RSS feed
/*
* Copyright (C) 2020, 2021 by Andreas Theofilu <andreas@theosys.at>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "tpalette.h"
#include "terror.h"
#include "tconfig.h"
#include "treadxml.h"
using std::string;
using std::vector;
using std::map;
using std::pair;
TPalette::TPalette()
{
DECL_TRACER("TPalette::TPalette()");
}
TPalette::TPalette(const std::string& file)
{
DECL_TRACER("TPalette::TPalette(const std::string& file)");
initialize(file);
}
TPalette::~TPalette()
{
DECL_TRACER("TPalette::~TPalette()");
}
void TPalette::initialize(const std::string& file)
{
DECL_TRACER("TPalette::initialize(const std::string& file)");
makeFileName(TConfig::getProjectPath(), file);
string path;
if (isValidFile())
path = getFileName();
TReadXML reader(path);
if (TError::isError())
return;
reader.findElement("paletteData", "name");
if (!reader.success())
{
MSG_ERROR("Error reading palatte file " << file << "!");
TError::setError();
return;
}
string palName = reader.getAttribute("name");
if (havePalette(palName))
return;
mxml_node_t *node = reader.getFirstChild();
while (node)
{
PDATA_T pal;
string e = reader.getElementName(node);
if (e.compare("color") != 0)
{
node = reader.getNextChild();
pal.clear();
continue;
}
pal.index = atoi(reader.getAttributeFromNode(node, "index").c_str());
pal.name = reader.getAttributeFromNode(node, "name");
string color = reader.getTextFromNode(node);
if (color.at(0) == '#') // Do we have a valid color value?
{
string sCol = "0x" + color.substr(1);
pal.color = strtoul(sCol.c_str(), 0, 16);
}
if (mColors.find(pal.name) != mColors.end()) // Don't insert color if it's already in list
{
MSG_INFO("Ignoring color " << pal.name << " because it was read before!");
node = reader.getNextChild();
pal.clear();
continue;
}
// Insert color into list and get next child if there is one.
mColors.insert(pair<string, PDATA_T>(pal.name, pal));
pal.clear();
node = reader.getNextChild();
}
}
void TPalette::reset()
{
DECL_TRACER("TPalette::reset()");
mColors.clear();
mPaletteNames.clear();
}
PDATA_T TPalette::findColor(const std::string& name)
{
DECL_TRACER("TPalette::findColor(const std::string& name)");
map<string, PDATA_T>::iterator iter;
if ((iter = mColors.find(name)) == mColors.end())
return PDATA_T();
return iter->second;
}
PDATA_T TPalette::findColor(int pID)
{
DECL_TRACER("TPalette::findColor(int pID)");
map<string, PDATA_T>::iterator iter;
for (iter = mColors.begin(); iter != mColors.end(); iter++)
{
if (iter->second.index == pID)
return iter->second;
}
return PDATA_T();
}
bool TPalette::havePalette(const std::string& name)
{
DECL_TRACER("TPalette::havePalette(const std::string& name)");
vector<string>::iterator iter;
for (iter = mPaletteNames.begin(); iter != mPaletteNames.end(); iter++)
{
if (iter->compare(name) == 0)
return true;
}
return false;
}