2014-07-19 17:24:28 -07:00
|
|
|
/* camera_parser.cc
|
|
|
|
* vim: set tw=80:
|
|
|
|
* Eryn Wells <eryn@erynwells.me>
|
|
|
|
*/
|
|
|
|
|
2014-07-19 20:59:48 -07:00
|
|
|
#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),
|
2014-07-19 20:59:48 -07:00
|
|
|
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 = {
|
2014-07-19 20:59:48 -07:00
|
|
|
{"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
|
2014-07-19 20:59:48 -07:00
|
|
|
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) {
|
2014-07-20 12:37:04 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-07-19 20:59:48 -07:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-19 20:59:48 -07:00
|
|
|
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);
|
|
|
|
}
|
2014-07-19 21:12:45 -07:00
|
|
|
|
|
|
|
mSection = NoSection;
|
|
|
|
SetShouldExpectKey(true);
|
2014-07-19 20:59:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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 */
|