Debian Codegraph @

plugins/dm.editing/SpawnargReplacer.h

raw
1 #pragma once
2
3 #include "inode.h"
4 #include "entitylib.h"
5
6 class SpawnargReplacer :
7 public scene::NodeVisitor
8 {
9 std::string _oldVal;
10 std::string _newVal;
11
12 std::size_t _modelCount;
13 std::size_t _otherCount;
14 std::size_t _eclassCount;
15
16 // A list of keys to be replaced
17 typedef std::vector<std::string> KeyList;
18 typedef std::map<scene::INodePtr, KeyList> EntityKeyMap;
19 EntityKeyMap _entityMap;
20
21 KeyList _curKeys;
22
23 public:
24 SpawnargReplacer(const std::string& oldVal, const std::string& newVal) :
25 _oldVal(oldVal),
26 _newVal(newVal),
27 _modelCount(0),
28 _otherCount(0),
29 _eclassCount(0)
30 {}
31
32 bool pre(const scene::INodePtr& node)
33 {
34 Entity* ent = Node_getEntity(node);
35
36 if (ent != NULL)
37 {
38 _curKeys.clear();
39
40 // Traverse the entity's spawnargs to check for keys to be replaced
41 ent->forEachKeyValue([&](const std::string& key, const std::string& value)
42 {
43 if (value == _oldVal)
44 {
45 // Matching value, remember this key
46 _curKeys.push_back(key);
47 }
48 });
49
50 // Save the result of the spawnarg search
51 if (!_curKeys.empty())
52 {
53 _entityMap[node] = _curKeys;
54 }
55
56 _curKeys.clear();
57 }
58
59 return false;
60 }
61
62 void processEntities()
63 {
64 for (EntityKeyMap::const_iterator e = _entityMap.begin();
65 e != _entityMap.end(); ++e)
66 {
67 const scene::INodePtr& ent = e->first;
68 const KeyList& keys = e->second;
69
70 for (KeyList::const_iterator i = keys.begin();
71 i != keys.end(); ++i)
72 {
73 // We have a match, check which key is affected
74 if (*i == "classname")
75 {
76 // Classname change
77 changeEntityClassname(ent, _newVal);
78 _eclassCount++;
79 }
80 else
81 {
82 Entity* entity = Node_getEntity(ent);
83 assert(entity != NULL);
84
85 entity->setKeyValue(*i, _newVal);
86
87 if (*i == "model")
88 {
89 _modelCount++;
90 }
91 else
92 {
93 _otherCount++;
94 }
95 }
96 }
97 }
98
99 _entityMap.clear();
100 }
101
102 std::size_t getModelCount() const
103 {
104 return _modelCount;
105 }
106
107 std::size_t getOtherCount() const
108 {
109 return _otherCount;
110 }
111
112 std::size_t getEclassCount() const
113 {
114 return _eclassCount;
115 }
116 };