charles/src/yaml/cameraParser.cc

187 lines
4.2 KiB
C++
Raw Normal View History

2014-07-19 17:24:28 -07:00
/* camera_parser.cc
* vim: set tw=80:
* Eryn Wells <eryn@erynwells.me>
*/
#include <cassert>
#include "camera.h"
#include "yaml/cameraParser.hh"
#include "yaml/vectorParser.hh"
2014-07-19 17:24:28 -07:00
namespace yaml {
CameraParser::CameraParser(Scene& scene,
ParserStack& parsers)
: ScalarMappingParser(scene, parsers),
mCamera(new PerspectiveCamera()),
mSection(NoSection),
mType(TypePerspective)
2014-07-19 17:24:28 -07:00
{
GetScene().SetCamera(mCamera);
}
CameraParser::~CameraParser()
{ }
void
CameraParser::HandleKeyEvent(const std::string& key)
{
static const std::map<std::string, Section> sSections = {
{"direction", DirectionSection},
{"origin", OriginSection},
{"right", RightSection},
{"type", TypeSection},
{"up", UpSection},
2014-07-19 17:24:28 -07:00
};
if (sSections.count(key) > 0) {
mSection = sSections.at(key);
}
else {
mSection = NoSection;
}
}
void
CameraParser::HandleValueEvent(yaml_event_t& event)
2014-07-19 17:24:28 -07:00
{
switch (mSection) {
case DirectionSection:
HandleDirectionEvent(event);
break;
case OriginSection:
HandleOriginEvent(event);
break;
case RightSection:
HandleRightEvent(event);
break;
case TypeSection:
HandleTypeEvent(event);
break;
case UpSection:
HandleUpEvent(event);
break;
default:
assert(false);
break;
}
}
void
CameraParser::HandleDirectionEvent(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 origin) {
mCamera->set_direction(origin);
mSection = NoSection;
SetShouldExpectKey(true);
};
GetParsers().push(new Vector3Parser(GetScene(), GetParsers(), onDone));
}
void
CameraParser::HandleOriginEvent(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 origin) {
mCamera->SetOrigin(origin);
2014-07-19 17:24:28 -07:00
mSection = NoSection;
SetShouldExpectKey(true);
};
GetParsers().push(new Vector3Parser(GetScene(), GetParsers(), onDone));
}
void
CameraParser::HandleRightEvent(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 right) {
mCamera->SetRight(right);
2014-07-19 17:24:28 -07:00
mSection = NoSection;
SetShouldExpectKey(true);
};
GetParsers().push(new Vector3Parser(GetScene(), GetParsers(), onDone));
}
void
CameraParser::HandleTypeEvent(yaml_event_t& event)
{
if (event.type != YAML_SCALAR_EVENT) {
assert(event.type != YAML_SCALAR_EVENT);
return;
}
std::string value = std::string((char*)event.data.scalar.value,
event.data.scalar.length);
if (value == "perspective") {
if (mType == TypeOrthographic) {
Camera *newCamera = new PerspectiveCamera(*mCamera);
delete mCamera;
mCamera = newCamera;
GetScene().SetCamera(newCamera);
}
}
else if (value == "orthographic") {
if (mType == TypePerspective) {
Camera *newCamera = new OrthographicCamera(*mCamera);
delete mCamera;
mCamera = newCamera;
GetScene().SetCamera(newCamera);
}
}
else {
assert(false);
}
mSection = NoSection;
SetShouldExpectKey(true);
}
2014-07-19 17:24:28 -07:00
void
CameraParser::HandleUpEvent(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 origin) {
mCamera->SetUp(origin);
mSection = NoSection;
SetShouldExpectKey(true);
};
GetParsers().push(new Vector3Parser(GetScene(), GetParsers(), onDone));
}
} /* namespace yaml */