ObjectParser using ScalarMappingParser

This commit is contained in:
Eryn Wells 2014-07-19 14:45:37 -07:00
parent 2f91e12296
commit 573f53b67c
2 changed files with 68 additions and 38 deletions

View file

@ -10,9 +10,9 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "yaml/object_parser.hh" #include "material.h"
#include "object_sphere.h" #include "object_sphere.h"
#include "yaml/objectParser.hh"
#include "yaml/vector_parser.hh" #include "yaml/vector_parser.hh"
@ -20,20 +20,46 @@ namespace yaml {
ObjectParser::ObjectParser(Scene& scene, ObjectParser::ObjectParser(Scene& scene,
ParserStack& parsers) ParserStack& parsers)
: Parser(scene, parsers) : ScalarMappingParser(scene, parsers),
mSection(NoSection)
{ }
ObjectParser::~ObjectParser()
{ } { }
void void
ObjectParser::HandleEvent(yaml_event_t& event) ObjectParser::HandleKeyEvent(const std::string& key)
{
static const std::map<std::string, Section> sSections = {
{"origin", OriginSection},
{"color", ColorSection}
};
printf("%s: got key = %s", __PRETTY_FUNCTION__, key.c_str());
if (sSections.count(key) > 0) {
mSection = sSections.at(key);
}
else {
mSection = NoSection;
}
}
void
ObjectParser::HandleValueEvent(yaml_event_t& event)
{ {
switch (mSection) { switch (mSection) {
case NoSection: case ColorSection:
HandleTopLevelEvent(event); HandleColorEvent(event);
break; break;
case OriginSection: case OriginSection:
HandleOriginEvent(event);
break; break;
case RadiusSection: case RadiusSection:
HandleRadiusEvent(event);
break; break;
default: default:
assert(false); assert(false);
@ -43,32 +69,27 @@ ObjectParser::HandleEvent(yaml_event_t& event)
void void
ObjectParser::HandleTopLevelEvent(yaml_event_t& event) ObjectParser::HandleColorEvent(yaml_event_t& event)
{ {
static const std::string ORIGIN = "origin"; if (event.type != YAML_SEQUENCE_START_EVENT) {
static const std::string RADIUS = "radius"; assert(event.type != YAML_SEQUENCE_START_EVENT);
if (event.type == YAML_MAPPING_END_EVENT) {
SetDone(true);
return; return;
} }
if (event.type != YAML_SCALAR_EVENT) { auto onDone = [this](std::vector<double> color) {
/* TODO: Clean this up. */ if (color.size() < 3) {
assert(false); assert(color.size() < 3);
} }
if (!mObject->get_material()) {
mObject->set_material(new Material());
}
mObject->get_material()->set_diffuse_color(Color(color[0], color[1], color[2]));
std::string value = (char *)event.data.scalar.value; mSection = NoSection;
if (value == ORIGIN) { SetShouldExpectKey(true);
mSection = OriginSection; };
}
else if (value == RADIUS) { GetParsers().push(new VectorParser<double>(GetScene(), GetParsers(), onDone));
mSection = RadiusSection;
}
else {
/* TODO: Clean this up. */
assert(false);
}
} }
@ -85,7 +106,9 @@ ObjectParser::HandleOriginEvent(yaml_event_t& event)
assert(origin.size() < 3); assert(origin.size() < 3);
} }
mObject->set_origin(Vector3(origin[0], origin[1], origin[2])); mObject->set_origin(Vector3(origin[0], origin[1], origin[2]));
mSection = NoSection; mSection = NoSection;
SetShouldExpectKey(true);
}; };
GetParsers().push(new VectorParser<double>(GetScene(), GetParsers(), onDone)); GetParsers().push(new VectorParser<double>(GetScene(), GetParsers(), onDone));

View file

@ -10,6 +10,7 @@
#define __YAML_OBJECTPARSER_HH__ #define __YAML_OBJECTPARSER_HH__
#include "yaml/parsers.hh" #include "yaml/parsers.hh"
#include "yaml/scalarMappingParser.hh"
class Sphere; class Sphere;
@ -18,23 +19,29 @@ class Sphere;
namespace yaml { namespace yaml {
struct ObjectParser struct ObjectParser
: public Parser : public ScalarMappingParser
{ {
ObjectParser(Scene& scene, ParserStack& parsers); ObjectParser(Scene& scene, ParserStack& parsers);
~ObjectParser();
void HandleEvent(yaml_event_t& event); protected:
void HandleTopLevelEvent(yaml_event_t& event); void HandleKeyEvent(const std::string& key);
void HandleValueEvent(yaml_event_t& event);
private:
enum Section {
NoSection,
ColorSection,
OriginSection,
RadiusSection
};
void HandleColorEvent(yaml_event_t& event);
void HandleOriginEvent(yaml_event_t& event); void HandleOriginEvent(yaml_event_t& event);
void HandleRadiusEvent(yaml_event_t& event); void HandleRadiusEvent(yaml_event_t& event);
private:
Sphere* mObject; Sphere* mObject;
Section mSection;
enum {
NoSection,
OriginSection,
RadiusSection
} mSection;
}; };
} /* namespace yaml */ } /* namespace yaml */