1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#include "MapPosition.h"
#include "ientity.h"
#include "icameraview.h"
#include "icommandsystem.h"
#include "itextstream.h"
#include "string/string.h"
#include "map/Map.h"
#include "gamelib.h"
namespace map
{
namespace
{
const char* const GKEY_MAP_POSROOT = "/mapFormat/mapPositionPosKey";
const char* const GKEY_MAP_ANGLEROOT = "/mapFormat/mapPositionAngleKey";
const char* const POSITION_KEY_FORMAT = "MapPosition{0:d}";
const char* const ANGLE_KEY_FORMAT = "MapAngle{0:d}";
}
MapPosition::MapPosition(unsigned int index) :
_index(index),
_position(0,0,0),
_angle(0,0,0)
{
// Construct the entity key names from the index
_posKey = game::current::getValue<std::string>(GKEY_MAP_POSROOT) + string::to_string(_index);
_angleKey = game::current::getValue<std::string>(GKEY_MAP_ANGLEROOT) + string::to_string(_index);
}
void MapPosition::loadFrom(Entity* entity)
{
// Sanity check
if (entity == nullptr) return;
const std::string savedPos = entity->getKeyValue(_posKey);
if (!savedPos.empty())
{
// Parse the vectors from std::string
_position = string::convert<Vector3>(savedPos);
_angle = string::convert<Vector3>(entity->getKeyValue(_angleKey));
}
}
void MapPosition::removeFrom(Entity* entity)
{
// Sanity check
if (entity != nullptr)
{
entity->setKeyValue(_posKey, "");
entity->setKeyValue(_angleKey, "");
}
}
void MapPosition::loadFrom(const scene::IMapRootNodePtr& root)
{
assert(root);
auto pos = root->getProperty(fmt::format(POSITION_KEY_FORMAT, _index));
if (!pos.empty())
{
// Parse the vectors from std::string
_position = string::convert<Vector3>(pos);
auto angle = root->getProperty(fmt::format(ANGLE_KEY_FORMAT, _index));
_angle = string::convert<Vector3>(angle);
}
}
void MapPosition::saveTo(const scene::IMapRootNodePtr& root)
{
assert(root);
if (!empty())
{
root->setProperty(fmt::format(POSITION_KEY_FORMAT, _index), string::to_string(_position));
root->setProperty(fmt::format(ANGLE_KEY_FORMAT, _index), string::to_string(_angle));
}
else
{
removeFrom(root);
}
}
void MapPosition::removeFrom(const scene::IMapRootNodePtr& root)
{
root->removeProperty(fmt::format(POSITION_KEY_FORMAT, _index));
root->removeProperty(fmt::format(ANGLE_KEY_FORMAT, _index));
}
void MapPosition::clear()
{
_position = Vector3(0,0,0);
_angle = Vector3(0,0,0);
}
bool MapPosition::empty() const
{
return _position == Vector3(0,0,0) && _angle == Vector3(0,0,0);
}
void MapPosition::store(const cmd::ArgumentList& args)
{
auto mapRoot = GlobalMapModule().getRoot();
if (!mapRoot)
{
rError() << "Cannot store map position, no map loaded." << std::endl;
return;
}
rMessage() << "Storing map position #" << _index << std::endl;
try
{
auto& cameraView = GlobalCameraManager().getActiveView();
_position = cameraView.getCameraOrigin();
_angle = cameraView.getCameraAngles();
saveTo(mapRoot);
// Tag the map as modified
GlobalMap().setModified(true);
}
catch (const std::runtime_error& ex)
{
rError() << "Exception saving camera position: " << ex.what() << std::endl;
}
}
void MapPosition::recall(const cmd::ArgumentList& args)
{
auto mapRoot = GlobalMapModule().getRoot();
if (!mapRoot)
{
rError() << "Cannot recall map position, no map loaded." << std::endl;
return;
}
// Refresh our local data from the properties
loadFrom(mapRoot);
if (!empty())
{
rMessage() << "Restoring map position #" << _index << std::endl;
// Focus the view with the default angle
GlobalCommandSystem().executeCommand("FocusViews", cmd::ArgumentList{ _position, _angle });
}
else
{
rMessage() << "Map position #" << _index << " has not been set" << std::endl;
}
}
} // namespace map
|