Skip to content

Commit

Permalink
feat(serialization): remove overloads in bindings.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Dacops committed Feb 8, 2024
1 parent f1c3036 commit d30bde9
Showing 1 changed file with 0 additions and 93 deletions.
93 changes: 0 additions & 93 deletions engine/src/cubos/engine/input/bindings.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#include <cubos/core/data/old/deserializer.hpp>
#include <cubos/core/data/old/serializer.hpp>
#include <cubos/core/reflection/type.hpp>

#include <cubos/engine/input/bindings.hpp>

using cubos::core::io::Key;
Expand Down Expand Up @@ -36,93 +33,3 @@ std::unordered_map<std::string, InputAxis>& InputBindings::axes()
{
return mAxes;
}

template <>
void cubos::core::data::old::serialize<InputBindings>(Serializer& ser, const InputBindings& obj, const char* name)
{
ser.beginObject(name);
ser.beginDictionary(obj.actions().size(), "actions");
for (const auto& [actionName, action] : obj.actions())
{
ser.write(actionName, nullptr);
ser.beginObject(nullptr);
ser.write(action.keys(), "keys");
ser.write(action.gamepadButtons(), "gamepad");
ser.write(action.mouseButtons(), "mouse");
ser.endObject();
}
ser.endDictionary();
ser.beginDictionary(obj.axes().size(), "axes");
for (const auto& [axisName, axis] : obj.axes())
{
ser.write(axisName, nullptr);
ser.beginObject(nullptr);
ser.write(axis.positive(), "pos");
ser.write(axis.negative(), "neg");
ser.write(axis.gamepadAxes(), "gamepad");
ser.endObject();
}
ser.endDictionary();
ser.endObject();
}

template <>
void cubos::core::data::old::deserialize<InputBindings>(Deserializer& des, InputBindings& obj)
{
des.beginObject();

std::size_t actionsSz = des.beginDictionary();
for (std::size_t i = 0; i < actionsSz; ++i)
{
std::string action;
des.read(action);
des.beginObject();
des.read(obj.actions()[action].keys());
des.read(obj.actions()[action].gamepadButtons());
des.read(obj.actions()[action].mouseButtons());
des.endObject();
}
des.endDictionary();

std::size_t axesSz = des.beginDictionary();
for (std::size_t i = 0; i < axesSz; ++i)
{
std::string axis;
des.read(axis);
des.beginObject();
des.read(obj.axes()[axis].positive());
des.read(obj.axes()[axis].negative());
des.read(obj.axes()[axis].gamepadAxes());
des.endObject();
}
des.endDictionary();

des.endObject();
}

// This is a specialization of the serialize and deserialize functions for std::pair<Key, Modifiers>.
// Overloading these functions allows for human-readable serialization of the keybindings.

template <>
void cubos::core::data::old::serialize<Key, Modifiers>(Serializer& ser, const std::pair<Key, Modifiers>& obj,
const char* name)
{
ser.write(modifiersToString(obj.second) + keyToString(obj.first), name);
}

template <>
void cubos::core::data::old::deserialize<Key, Modifiers>(Deserializer& des, std::pair<Key, Modifiers>& obj)
{
std::string str;
des.readString(str);

std::size_t split = str.find_last_of('-');
if (split == std::string::npos)
{
obj = {stringToKey(str), Modifiers::None};
}
else
{
obj = {stringToKey(str.substr(split + 1)), stringToModifiers(str.substr(0, split + 1))};
}
}

0 comments on commit d30bde9

Please sign in to comment.