charles/src/yaml/objectParser.cc

136 lines
2.8 KiB
C++
Raw Normal View History

2014-07-17 20:14:52 -07:00
/* object_parser.cc
* vim: set tw=80:
* Eryn Wells <eryn@erynwells.me>
*/
/**
* Implementation of ObjectParser.
*/
#include <cassert>
#include <string>
#include <vector>
2014-07-19 14:45:37 -07:00
#include "material.h"
#include "object.h"
2014-07-17 20:14:52 -07:00
#include "object_sphere.h"
2014-07-19 14:45:37 -07:00
#include "yaml/objectParser.hh"
#include "yaml/vectorParser.hh"
2014-07-17 20:14:52 -07:00
using namespace charles;
2014-07-17 20:14:52 -07:00
namespace yaml {
ObjectParser::ObjectParser(Scene& scene,
ParserStack& parsers)
2014-07-19 14:45:37 -07:00
: ScalarMappingParser(scene, parsers),
mObject(new Sphere()),
2014-07-19 14:45:37 -07:00
mSection(NoSection)
{
GetScene().add_shape(mObject);
}
2014-07-17 20:14:52 -07:00
2014-07-19 14:45:37 -07:00
ObjectParser::~ObjectParser()
{ }
void
ObjectParser::HandleKeyEvent(const std::string& key)
{
static const std::map<std::string, Section> sSections = {
2014-07-19 15:42:31 -07:00
{"color", ColorSection},
2014-07-19 14:45:37 -07:00
{"origin", OriginSection},
2014-07-19 15:42:31 -07:00
{"radius", RadiusSection}
2014-07-19 14:45:37 -07:00
};
if (sSections.count(key) > 0) {
mSection = sSections.at(key);
}
else {
mSection = NoSection;
}
}
2014-07-17 20:14:52 -07:00
void
2014-07-19 14:45:37 -07:00
ObjectParser::HandleValueEvent(yaml_event_t& event)
2014-07-17 20:14:52 -07:00
{
switch (mSection) {
2014-07-19 14:45:37 -07:00
case ColorSection:
HandleColorEvent(event);
2014-07-17 20:14:52 -07:00
break;
case OriginSection:
2014-07-19 14:45:37 -07:00
HandleOriginEvent(event);
2014-07-17 20:14:52 -07:00
break;
case RadiusSection:
2014-07-19 14:45:37 -07:00
HandleRadiusEvent(event);
2014-07-17 20:14:52 -07:00
break;
default:
assert(false);
break;
}
}
void
2014-07-19 14:45:37 -07:00
ObjectParser::HandleColorEvent(yaml_event_t& event)
2014-07-17 20:14:52 -07:00
{
2014-07-19 14:45:37 -07:00
if (event.type != YAML_SEQUENCE_START_EVENT) {
/* TODO: Clean this up. */
2014-07-19 14:45:37 -07:00
assert(event.type != YAML_SEQUENCE_START_EVENT);
2014-07-17 20:14:52 -07:00
return;
}
auto onDone = [this](Color color) {
mObject->GetMaterial().SetDiffuseColor(color);
2014-07-19 14:45:37 -07:00
mSection = NoSection;
SetShouldExpectKey(true);
};
GetParsers().push(new ColorParser(GetScene(), GetParsers(), onDone));
2014-07-17 20:14:52 -07:00
}
void
ObjectParser::HandleOriginEvent(yaml_event_t& event)
{
if (event.type != YAML_SEQUENCE_START_EVENT) {
/* TODO: Clean this up. */
assert(event.type != YAML_SEQUENCE_START_EVENT);
return;
2014-07-17 20:14:52 -07:00
}
auto onDone = [this](Vector3 origin) {
mObject->SetOrigin(origin);
2014-07-17 20:14:52 -07:00
mSection = NoSection;
2014-07-19 14:45:37 -07:00
SetShouldExpectKey(true);
2014-07-17 20:14:52 -07:00
};
2014-07-19 14:45:37 -07:00
GetParsers().push(new Vector3Parser(GetScene(), GetParsers(), onDone));
2014-07-17 20:14:52 -07:00
}
void
ObjectParser::HandleRadiusEvent(yaml_event_t& event)
{
if (event.type != YAML_SCALAR_EVENT) {
/* TODO: Clean this up. */
assert(false);
}
double radius;
std::string scalar((char *)event.data.scalar.value,
event.data.scalar.length);
if (!ParseScalar<double>(scalar, radius)) {
2014-07-17 20:14:52 -07:00
/* TODO: Clean this up. */
assert(false);
}
mObject->set_radius(radius);
mSection = NoSection;
SetShouldExpectKey(true);
2014-07-17 20:14:52 -07:00
}
} /* namespace yaml */