settings file and save/restore ui state
This commit is contained in:
105
src/serializer.h
105
src/serializer.h
@@ -70,7 +70,17 @@ public:
|
||||
{
|
||||
w.wi32((int)items.size());
|
||||
for (auto& i : items)
|
||||
{
|
||||
w.wstring_raw(i->type_key());
|
||||
i->write(w);
|
||||
}
|
||||
}
|
||||
template <typename T>
|
||||
std::shared_ptr<T> add()
|
||||
{
|
||||
auto ptr = std::make_shared<T>();
|
||||
items.emplace_back(ptr);
|
||||
return ptr;
|
||||
}
|
||||
};
|
||||
struct Double : public Type
|
||||
@@ -263,6 +273,81 @@ public:
|
||||
w.wflt(value.w);
|
||||
}
|
||||
};
|
||||
struct IVec2 : public Type
|
||||
{
|
||||
using native_type = glm::ivec2;
|
||||
glm::ivec2 value;
|
||||
IVec2() = default;
|
||||
IVec2(glm::ivec2 v) : value(v) { }
|
||||
virtual std::string type_key() const override { return "ive2"; }
|
||||
virtual std::string str(int indent, const std::string& prefix) const override
|
||||
{
|
||||
return std::string(indent, '-') + prefix + fmt::format("ivec2: {} {}", value.x, value.y);
|
||||
}
|
||||
virtual bool read(BinaryStreamReader& r) override
|
||||
{
|
||||
value.x = r.ri32();
|
||||
value.y = r.ri32();
|
||||
return true;
|
||||
}
|
||||
virtual void write(BinaryStreamWriter& w) const override
|
||||
{
|
||||
w.wi32(value.x);
|
||||
w.wi32(value.y);
|
||||
}
|
||||
};
|
||||
struct IVec3 : public Type
|
||||
{
|
||||
using native_type = glm::ivec3;
|
||||
glm::ivec3 value;
|
||||
IVec3() = default;
|
||||
IVec3(glm::ivec3 v) : value(v) { }
|
||||
virtual std::string type_key() const override { return "ive3"; }
|
||||
virtual std::string str(int indent, const std::string& prefix) const override
|
||||
{
|
||||
return std::string(indent, '-') + prefix + fmt::format("ivec3: {} {} {}", value.x, value.y, value.z);
|
||||
}
|
||||
virtual bool read(BinaryStreamReader& r) override
|
||||
{
|
||||
value.x = r.ri32();
|
||||
value.y = r.ri32();
|
||||
value.z = r.ri32();
|
||||
return true;
|
||||
}
|
||||
virtual void write(BinaryStreamWriter& w) const override
|
||||
{
|
||||
w.wu32(value.x);
|
||||
w.wu32(value.y);
|
||||
w.wu32(value.z);
|
||||
}
|
||||
};
|
||||
struct IVec4 : public Type
|
||||
{
|
||||
using native_type = glm::ivec4;
|
||||
glm::ivec4 value;
|
||||
IVec4() = default;
|
||||
IVec4(glm::ivec4 v) : value(v) { }
|
||||
virtual std::string type_key() const override { return "ive4"; }
|
||||
virtual std::string str(int indent, const std::string& prefix) const override
|
||||
{
|
||||
return std::string(indent, '-') + prefix + fmt::format("ivec4: {} {} {} {}", value.x, value.y, value.z, value.w);
|
||||
}
|
||||
virtual bool read(BinaryStreamReader& r) override
|
||||
{
|
||||
value.x = r.ri32();
|
||||
value.y = r.ri32();
|
||||
value.z = r.ri32();
|
||||
value.w = r.ri32();
|
||||
return true;
|
||||
}
|
||||
virtual void write(BinaryStreamWriter& w) const override
|
||||
{
|
||||
w.wi32(value.x);
|
||||
w.wi32(value.y);
|
||||
w.wi32(value.z);
|
||||
w.wi32(value.w);
|
||||
}
|
||||
};
|
||||
struct Enum : public Type
|
||||
{
|
||||
std::string type;
|
||||
@@ -352,7 +437,7 @@ public:
|
||||
struct Descriptor : public Type
|
||||
{
|
||||
std::wstring name;
|
||||
std::string class_id;
|
||||
std::string class_id = "desc";
|
||||
Type::Map props;
|
||||
Descriptor() = default;
|
||||
virtual std::string type_key() const override { return "Objc"; }
|
||||
@@ -372,19 +457,33 @@ public:
|
||||
{
|
||||
return has(key) ? std::dynamic_pointer_cast<T>(props.at(key)) : nullptr;
|
||||
}
|
||||
template<typename T> std::shared_ptr<T> set(const std::string& key, const T value)
|
||||
{
|
||||
if (auto ptr = get<T>(key))
|
||||
{
|
||||
*ptr = value;
|
||||
return ptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
ptr = std::make_shared<T>(value);
|
||||
props[key] = ptr;
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
template<typename T> auto value(const std::string& key) const
|
||||
{
|
||||
if (auto v = get<T>(key))
|
||||
return v->value;
|
||||
return decltype(T::value){};
|
||||
}
|
||||
template<typename T, typename D> auto value_or(const std::string& key, const D val) const
|
||||
template<typename T, typename D = decltype(T::value)> auto value_or(const std::string& key, const D val) const
|
||||
{
|
||||
if (auto v = get<T>(key))
|
||||
return v->value;
|
||||
return val;
|
||||
}
|
||||
template<typename T, typename D> void value(const std::string& key, D& dest) const
|
||||
template<typename T, typename D = decltype(T::value)> void value(const std::string& key, D& dest) const
|
||||
{
|
||||
if (auto v = get<T>(key))
|
||||
dest = static_cast<D>(v->value);
|
||||
|
||||
Reference in New Issue
Block a user