2015-08-13 21:46:50 +00:00
|
|
|
#include "FloatNode.h"
|
|
|
|
|
|
|
|
#include "PackStream.h"
|
2015-08-16 21:01:56 +00:00
|
|
|
#include "FloatDiff.h"
|
|
|
|
#include "Logs.h"
|
2015-08-13 21:46:50 +00:00
|
|
|
#include <sstream>
|
2015-08-16 21:01:56 +00:00
|
|
|
#include <cassert>
|
2015-08-13 21:46:50 +00:00
|
|
|
|
|
|
|
FloatNode::FloatNode(DefinitionNode* parent, const std::string &name, double value):
|
2015-08-16 21:01:56 +00:00
|
|
|
DefinitionNode(parent, name, "float"), value(value)
|
2015-08-13 21:46:50 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string FloatNode::toString(int indent) const
|
|
|
|
{
|
|
|
|
std::ostringstream stream;
|
|
|
|
|
|
|
|
stream << DefinitionNode::toString(indent) << " " << value;
|
|
|
|
|
|
|
|
return stream.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatNode::save(PackStream *stream) const
|
|
|
|
{
|
|
|
|
stream->write(&value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatNode::load(PackStream *stream)
|
|
|
|
{
|
|
|
|
stream->read(&value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FloatNode::copy(DefinitionNode *destination) const
|
|
|
|
{
|
2015-08-16 21:01:56 +00:00
|
|
|
if (destination->getTypeName() == getTypeName())
|
|
|
|
{
|
|
|
|
((FloatNode *)destination)->value = value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Logs::error() << "Can't copy from " << getTypeName() << " to " << destination->getTypeName() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const FloatDiff *FloatNode::produceDiff(double new_value) const
|
|
|
|
{
|
|
|
|
return new FloatDiff(value, new_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FloatNode::applyDiff(const DefinitionDiff *diff, bool backward)
|
|
|
|
{
|
|
|
|
if (!DefinitionNode::applyDiff(diff, backward))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(diff->getTypeName() == "float");
|
|
|
|
const FloatDiff *float_diff = (const FloatDiff *)diff;
|
|
|
|
|
|
|
|
double previous = backward ? float_diff->getNewValue() : float_diff->getOldValue();
|
|
|
|
double next = backward ? float_diff->getOldValue() : float_diff->getNewValue();
|
2015-08-13 21:46:50 +00:00
|
|
|
|
2015-08-16 21:01:56 +00:00
|
|
|
if (value == previous)
|
|
|
|
{
|
|
|
|
value = next;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Logs::error() << "Can't apply float diff " << previous << " => " << next << " to " << getName() << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-13 21:46:50 +00:00
|
|
|
}
|