载入中...
搜索中...
未找到
AnimClipRegistry.cpp
浏览该文件的文档.
2
5
7#include "common/Capability.h"
8
9#include <algorithm>
10#include <cctype>
11#include <map>
12#include <utility>
13
14namespace eve::animation {
15namespace {
16
17using EntryMap = std::map<std::string, std::vector<AnimClip*>>;
18
19EntryMap& entries() {
20 static EntryMap map;
21 return map;
22}
23
24bool isEvaPath(const std::string& path) {
25 if (path.size() < 5) return false;
26 std::string ext = path.substr(path.size() - 4);
27 for (char& c : ext) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
28 return ext == ".eva";
29}
30
32class AnimClipReloader : public eve::caps::IAssetReloader {
33public:
34 const char* reloadKind() const override { return "animclip"; }
35
36 bool handlesPath(const std::string& normPath) const override {
37 return isEvaPath(AnimClipRegistry::normalizePath(normPath));
38 }
39
40 bool reload(const std::string& normPath) override { return AnimClipRegistry::reloadPath(normPath) > 0; }
41};
42
43struct Register {
44 Register() {
45 static AnimClipReloader reloader;
47 }
48} g_register;
49
50} // namespace
51
52std::string AnimClipRegistry::normalizePath(const std::string& path) {
53 std::string out = path;
54 for (char& c : out)
55 if (c == '\\') c = '/';
56 return out;
57}
58
59void AnimClipRegistry::registerPath(const std::string& path, AnimClip* clip) {
60 if (!clip) return;
61 std::vector<AnimClip*>& list = entries()[normalizePath(path)];
62 if (std::find(list.begin(), list.end(), clip) == list.end()) list.push_back(clip);
63}
64
66 if (!clip) return;
67 for (auto it = entries().begin(); it != entries().end();) {
68 std::vector<AnimClip*>& list = it->second;
69 list.erase(std::remove(list.begin(), list.end(), clip), list.end());
70 if (list.empty())
71 it = entries().erase(it);
72 else
73 ++it;
74 }
75}
76
77std::vector<AnimClip*> AnimClipRegistry::findByPath(const std::string& path) {
78 const auto it = entries().find(normalizePath(path));
79 return it == entries().end() ? std::vector<AnimClip*>() : it->second;
80}
81
82bool AnimClipRegistry::hasPath(const std::string& path) { return entries().count(normalizePath(path)) > 0; }
83
84int AnimClipRegistry::reloadPath(const std::string& path) {
85 const std::string norm = normalizePath(path);
86 const auto it = entries().find(norm);
87 if (it == entries().end() || it->second.empty()) return 0;
88
89 AnimSkeleton* skeleton = nullptr;
90 AnimClip* fresh = nullptr;
91 try {
92 AnimImporter::importEvaFile(norm, &skeleton, &fresh);
93 } catch (...) {
94 delete skeleton;
95 delete fresh;
96 return 0;
97 }
98 if (!fresh) {
99 delete skeleton;
100 return 0;
101 }
102 for (AnimClip* clip : it->second) clip->adopt(*fresh);
103 delete fresh;
104 delete skeleton;
105 return static_cast<int>(it->second.size());
106}
107
109 int n = 0;
110 for (const auto& kv : entries()) n += static_cast<int>(kv.second.size());
111 return n;
112}
113
114void AnimClipRegistry::clear() { entries().clear(); }
115
116} // namespace eve::animation
glm::vec3 n
Definition Grass.cpp:64
uint32_t c
static std::string normalizePath(const std::string &path)
Normalize a path to the registry key form.
static void unregister(AnimClip *clip)
Forget a clip everywhere (called from ~AnimClip).
static void registerPath(const std::string &path, AnimClip *clip)
Track a clip under a source path (keys are normalized).
static int reloadPath(const std::string &path)
Re-import the source and adopt into every registered clip.
static int count()
Total registered clips (tests).
static bool hasPath(const std::string &path)
Whether any clip is registered under this path.
static void clear()
Drop all entries (tests).
static std::vector< AnimClip * > findByPath(const std::string &path)
Clips registered under a path; may be empty.
Keyframed skeletal animation clip (local TRS tracks per bone). Script type: AnimClip.
Definition AnimClip.h:17
static void importEvaFile(const std::string &path, AnimSkeleton **skeletonOut, AnimClip **clipOut)
Load from .eva file path.
3D bone hierarchy + bind-pose local TRS for skeletal animation. Independent of ik::Skeleton3D (FABRIK...
I * query()
Definition Capability.h:77