diff --git a/src/yaml/objectParser.cc b/src/yaml/objectParser.cc index f911e56..6ba5d52 100644 --- a/src/yaml/objectParser.cc +++ b/src/yaml/objectParser.cc @@ -10,9 +10,9 @@ #include #include -#include "yaml/object_parser.hh" - +#include "material.h" #include "object_sphere.h" +#include "yaml/objectParser.hh" #include "yaml/vector_parser.hh" @@ -20,20 +20,46 @@ namespace yaml { ObjectParser::ObjectParser(Scene& scene, ParserStack& parsers) - : Parser(scene, parsers) + : ScalarMappingParser(scene, parsers), + mSection(NoSection) +{ } + + +ObjectParser::~ObjectParser() { } void -ObjectParser::HandleEvent(yaml_event_t& event) +ObjectParser::HandleKeyEvent(const std::string& key) +{ + static const std::map 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) { - case NoSection: - HandleTopLevelEvent(event); + case ColorSection: + HandleColorEvent(event); break; case OriginSection: + HandleOriginEvent(event); break; case RadiusSection: + HandleRadiusEvent(event); break; default: assert(false); @@ -43,32 +69,27 @@ ObjectParser::HandleEvent(yaml_event_t& event) void -ObjectParser::HandleTopLevelEvent(yaml_event_t& event) +ObjectParser::HandleColorEvent(yaml_event_t& event) { - static const std::string ORIGIN = "origin"; - static const std::string RADIUS = "radius"; - - if (event.type == YAML_MAPPING_END_EVENT) { - SetDone(true); + if (event.type != YAML_SEQUENCE_START_EVENT) { + assert(event.type != YAML_SEQUENCE_START_EVENT); return; } - if (event.type != YAML_SCALAR_EVENT) { - /* TODO: Clean this up. */ - assert(false); - } + auto onDone = [this](std::vector color) { + if (color.size() < 3) { + 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; - if (value == ORIGIN) { - mSection = OriginSection; - } - else if (value == RADIUS) { - mSection = RadiusSection; - } - else { - /* TODO: Clean this up. */ - assert(false); - } + mSection = NoSection; + SetShouldExpectKey(true); + }; + + GetParsers().push(new VectorParser(GetScene(), GetParsers(), onDone)); } @@ -85,9 +106,11 @@ ObjectParser::HandleOriginEvent(yaml_event_t& event) assert(origin.size() < 3); } mObject->set_origin(Vector3(origin[0], origin[1], origin[2])); + mSection = NoSection; + SetShouldExpectKey(true); }; - + GetParsers().push(new VectorParser(GetScene(), GetParsers(), onDone)); } diff --git a/src/yaml/objectParser.hh b/src/yaml/objectParser.hh index 19e2c81..b7be8bd 100644 --- a/src/yaml/objectParser.hh +++ b/src/yaml/objectParser.hh @@ -10,6 +10,7 @@ #define __YAML_OBJECTPARSER_HH__ #include "yaml/parsers.hh" +#include "yaml/scalarMappingParser.hh" class Sphere; @@ -18,23 +19,29 @@ class Sphere; namespace yaml { struct ObjectParser - : public Parser + : public ScalarMappingParser { ObjectParser(Scene& scene, ParserStack& parsers); + ~ObjectParser(); - void HandleEvent(yaml_event_t& event); - void HandleTopLevelEvent(yaml_event_t& event); +protected: + 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 HandleRadiusEvent(yaml_event_t& event); -private: Sphere* mObject; - - enum { - NoSection, - OriginSection, - RadiusSection - } mSection; + Section mSection; }; } /* namespace yaml */