charles/src/yaml/objectParser.cc

215 lines
4.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"
#include "objectBox.hh"
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,
const std::string& tag)
2014-07-19 14:45:37 -07:00
: ScalarMappingParser(scene, parsers),
mObject(nullptr),
2014-07-19 14:45:37 -07:00
mSection(NoSection)
{
if (tag == "!Object.Box") {
mType = Type::Box;
mObject.reset(new Box());
} else if (tag == "!Object.Sphere") {
mType = Type::Sphere;
mObject.reset(new Sphere());
} else {
assert(false);
}
GetScene().add_shape(mObject);
}
2014-07-17 20:14:52 -07:00
2014-07-19 14:45:37 -07:00
ObjectParser::~ObjectParser()
{
mObject.reset();
}
2014-07-19 14:45:37 -07:00
void
ObjectParser::HandleKeyEvent(const std::string& key)
{
static const std::map<std::string, Section> sCommonSections = {
{"color", ColorSection}
};
static const std::map<std::string, Section> sSphereSections = {
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
};
static const std::map<std::string, Section> sBoxSections = {
{"near", NearSection},
{"far", FarSection}
};
if (sCommonSections.count(key) > 0) {
mSection = sCommonSections.at(key);
return;
2014-07-19 14:45:37 -07:00
}
if (mType == Type::Box) {
if (sBoxSections.count(key) > 0) {
mSection = sBoxSections.at(key);
} else {
mSection = NoSection;
}
} else if (mType == Type::Sphere) {
if (sSphereSections.count(key) > 0) {
mSection = sSphereSections.at(key);
} else {
mSection = NoSection;
}
} else {
assert(false);
2014-07-19 14:45:37 -07:00
}
}
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;
case NearSection:
HandleNearEvent(event);
break;
case FarSection:
HandleFarEvent(event);
break;
2014-07-17 20:14:52 -07:00
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);
}
std::dynamic_pointer_cast<Sphere>(mObject)->set_radius(radius);
2014-07-17 20:14:52 -07:00
mSection = NoSection;
SetShouldExpectKey(true);
2014-07-17 20:14:52 -07:00
}
void
ObjectParser::HandleNearEvent(yaml_event_t& event)
{
if (event.type != YAML_SEQUENCE_START_EVENT) {
/* TODO: Clean this up. */
assert(event.type != YAML_SEQUENCE_START_EVENT);
return;
}
auto onDone = [this](Vector3 near) {
std::dynamic_pointer_cast<Box>(mObject)->SetNear(near);
mSection = NoSection;
SetShouldExpectKey(true);
};
GetParsers().push(new Vector3Parser(GetScene(), GetParsers(), onDone));
}
void
ObjectParser::HandleFarEvent(yaml_event_t& event)
{
if (event.type != YAML_SEQUENCE_START_EVENT) {
/* TODO: Clean this up. */
assert(event.type != YAML_SEQUENCE_START_EVENT);
return;
}
auto onDone = [this](Vector3 far) {
std::dynamic_pointer_cast<Box>(mObject)->SetFar(far);
mSection = NoSection;
SetShouldExpectKey(true);
};
GetParsers().push(new Vector3Parser(GetScene(), GetParsers(), onDone));
}
2014-07-17 20:14:52 -07:00
} /* namespace yaml */