dl
dl_animation_info.h
浏览该文件的文档.
1
12#pragma once
13
14#include "dl_type.h"
15#include "io/dl_json.h"
16
17
18namespace dl
19{
20 constexpr unsigned ANIMATION_FTIME = 125;
21
24 {
25 struct Node
26 {
27 unsigned _beg = 0;
28 unsigned _skip = 0;
30 unsigned _dir = 1;
31 unsigned _count = 0;
32 };
33 std::string _type = ""; // sheet.动画在一张图上 multi.多张图组成的动画
34 std::string _format = "{}_{}";
35 unsigned _skip = 0;
37 unsigned _dir = 1;
38 unsigned _count = 0; // sheet.0表示sheet的所有精灵 multi.表示1张
39
40 std::unordered_map<std::string, Node, string_hash, std::equal_to<>> _allAni;
41 };
42
43 inline void to_json(json& j, const AnimationInfo& info)
44 {
45 j = json{
46 {"type", info._type},
47 {"format", info._format},
48 {"skip", info._skip},
49 {"ftime", info._ftime},
50 {"dir", info._dir},
51 {"count", info._count},
52 };
53 json j_ani = {};
54 for (auto& iter : info._allAni)
55 {
56 AnimationInfo::Node node = iter.second;
57 json j_node;
58 j_node["name"] = iter.first;
59 j_node["beg"] = node._beg;
60 j_node["skip"] = node._skip;
61 j_node["ftime"] = node._ftime;
62 j_node["dir"] = node._dir;
63 j_node["count"] = node._count;
64 j_ani.push_back(j_node);
65 }
66 j["ani"] = j_ani;
67 }
68 inline void from_json(const json& j, AnimationInfo& info)
69 {
71 Json::Get(j, "type", info._type);
72 Json::Get(j, "format", info._format);
73 Json::Get(j, "skip", info._skip);
74 Json::Get(j, "ftime", info._ftime);
75 Json::Get(j, "dir", info._dir);
76 Json::Get(j, "count", info._count);
77 json j_ani;
78 Json::Get(j, "ani", j_ani);
79 for (auto& iter : j_ani)
80 {
81 std::string name;
83 Json::Get(iter, "name", name);
84 Json::Get(iter, "beg", node._beg);
85 if (!Json::Get(iter, "skip", node._skip))
86 node._skip = info._skip;
87 if (!Json::Get(iter, "ftime", node._ftime))
88 node._ftime = info._ftime;
89 if (!Json::Get(iter, "dir", node._dir))
90 node._dir = info._dir;
91 if (!Json::Get(iter, "count", node._count))
92 node._count = info._count;
93 auto iter = info._allAni.find(name);
94 if (iter != info._allAni.end())
95 {
96 log_warn("子动画名重复,将覆盖:{}", name);
97 }
98 info._allAni[name] = node;
99 }
100 }
101}
json格式相关
nlohmann::json json
#define log_warn(...)
通用类型
bool Get(const json &j, const std::string &name, T &t)
从json返回值,不存在则返回false,打印日志
void to_json(json &j, const AnimationInfo &info)
constexpr unsigned ANIMATION_FTIME
void from_json(const json &j, AnimationInfo &info)
std::unordered_map< std::string, Node, string_hash, std::equal_to<> > _allAni
临时关闭json日志