载入中...
搜索中...
未找到
EditorInspector.cpp
浏览该文件的文档.
2
3#include "common/Exception.h"
4
5namespace eve::editor {
6
8 fields_.clear();
9 pollCursor_ = 0;
10}
11
12int EditorInspector::findIndex(const std::string &id) const {
13 for (int i = 0; i < static_cast<int>(fields_.size()); ++i) {
14 if (fields_[i].id == id) return i;
15 }
16 return -1;
17}
18
19EditorInspector::Field *EditorInspector::find(const std::string &id) {
20 int i = findIndex(id);
21 return i < 0 ? nullptr : &fields_[i];
22}
23
24const EditorInspector::Field *EditorInspector::find(const std::string &id) const {
25 int i = findIndex(id);
26 return i < 0 ? nullptr : &fields_[i];
27}
28
29void EditorInspector::addFloat(const std::string &id, const std::string &label, float value,
30 float minV, float maxV, float step) {
31 if (id.empty() || findIndex(id) >= 0) throw Exception("EditorInspector::addFloat: bad id");
32 Field f;
33 f.kind = "float";
34 f.id = id;
35 f.label = label;
36 f.f0 = value;
37 f.minV = minV;
38 f.maxV = maxV;
39 f.step = step;
40 fields_.push_back(f);
41}
42
43void EditorInspector::addFloat3(const std::string &id, const std::string &label, float x, float y,
44 float z) {
45 if (id.empty() || findIndex(id) >= 0) throw Exception("EditorInspector::addFloat3: bad id");
46 Field f;
47 f.kind = "float3";
48 f.id = id;
49 f.label = label;
50 f.f0 = x;
51 f.f1 = y;
52 f.f2 = z;
53 fields_.push_back(f);
54}
55
56void EditorInspector::addBool(const std::string &id, const std::string &label, bool value) {
57 if (id.empty() || findIndex(id) >= 0) throw Exception("EditorInspector::addBool: bad id");
58 Field f;
59 f.kind = "bool";
60 f.id = id;
61 f.label = label;
62 f.b = value;
63 fields_.push_back(f);
64}
65
66void EditorInspector::addString(const std::string &id, const std::string &label,
67 const std::string &value) {
68 if (id.empty() || findIndex(id) >= 0) throw Exception("EditorInspector::addString: bad id");
69 Field f;
70 f.kind = "string";
71 f.id = id;
72 f.label = label;
73 f.s = value;
74 fields_.push_back(f);
75}
76
77void EditorInspector::addChoice(const std::string &id, const std::string &label,
78 const std::string &choicesCsv, const std::string &selected) {
79 if (id.empty() || findIndex(id) >= 0) throw Exception("EditorInspector::addChoice: bad id");
80 Field f;
81 f.kind = "choice";
82 f.id = id;
83 f.label = label;
84 f.choices = choicesCsv;
85 f.s = selected;
86 fields_.push_back(f);
87}
88
89std::string EditorInspector::getFieldKind(int index) const {
90 if (index < 0 || index >= static_cast<int>(fields_.size()))
91 throw Exception("EditorInspector::getFieldKind: bad index");
92 return fields_[index].kind;
93}
94std::string EditorInspector::getFieldId(int index) const {
95 if (index < 0 || index >= static_cast<int>(fields_.size()))
96 throw Exception("EditorInspector::getFieldId: bad index");
97 return fields_[index].id;
98}
99std::string EditorInspector::getFieldLabel(int index) const {
100 if (index < 0 || index >= static_cast<int>(fields_.size()))
101 throw Exception("EditorInspector::getFieldLabel: bad index");
102 return fields_[index].label;
103}
104
105float EditorInspector::getFloat(const std::string &id) const {
106 const Field *f = find(id);
107 if (!f || f->kind != "float") throw Exception("EditorInspector::getFloat: missing float field");
108 return f->f0;
109}
110
111void EditorInspector::setFloat(const std::string &id, float value) {
112 Field *f = find(id);
113 if (!f || f->kind != "float") throw Exception("EditorInspector::setFloat: missing float field");
114 if (f->f0 != value) {
115 f->f0 = value;
116 f->dirty = true;
117 }
118}
119
120float EditorInspector::getFloatMin(const std::string &id) const {
121 const Field *f = find(id);
122 if (!f || f->kind != "float") throw Exception("EditorInspector::getFloatMin: missing");
123 return f->minV;
124}
125float EditorInspector::getFloatMax(const std::string &id) const {
126 const Field *f = find(id);
127 if (!f || f->kind != "float") throw Exception("EditorInspector::getFloatMax: missing");
128 return f->maxV;
129}
130float EditorInspector::getFloatStep(const std::string &id) const {
131 const Field *f = find(id);
132 if (!f || f->kind != "float") throw Exception("EditorInspector::getFloatStep: missing");
133 return f->step;
134}
135
136float EditorInspector::getFloat3X(const std::string &id) const {
137 const Field *f = find(id);
138 if (!f || f->kind != "float3") throw Exception("EditorInspector::getFloat3X: missing");
139 return f->f0;
140}
141float EditorInspector::getFloat3Y(const std::string &id) const {
142 const Field *f = find(id);
143 if (!f || f->kind != "float3") throw Exception("EditorInspector::getFloat3Y: missing");
144 return f->f1;
145}
146float EditorInspector::getFloat3Z(const std::string &id) const {
147 const Field *f = find(id);
148 if (!f || f->kind != "float3") throw Exception("EditorInspector::getFloat3Z: missing");
149 return f->f2;
150}
151
152void EditorInspector::setFloat3(const std::string &id, float x, float y, float z) {
153 Field *f = find(id);
154 if (!f || f->kind != "float3") throw Exception("EditorInspector::setFloat3: missing");
155 if (f->f0 != x || f->f1 != y || f->f2 != z) {
156 f->f0 = x;
157 f->f1 = y;
158 f->f2 = z;
159 f->dirty = true;
160 }
161}
162
163bool EditorInspector::getBool(const std::string &id) const {
164 const Field *f = find(id);
165 if (!f || f->kind != "bool") throw Exception("EditorInspector::getBool: missing");
166 return f->b;
167}
168
169void EditorInspector::setBool(const std::string &id, bool value) {
170 Field *f = find(id);
171 if (!f || f->kind != "bool") throw Exception("EditorInspector::setBool: missing");
172 if (f->b != value) {
173 f->b = value;
174 f->dirty = true;
175 }
176}
177
178std::string EditorInspector::getString(const std::string &id) const {
179 const Field *f = find(id);
180 if (!f || f->kind != "string") throw Exception("EditorInspector::getString: missing");
181 return f->s;
182}
183
184void EditorInspector::setString(const std::string &id, const std::string &value) {
185 Field *f = find(id);
186 if (!f || f->kind != "string") throw Exception("EditorInspector::setString: missing");
187 if (f->s != value) {
188 f->s = value;
189 f->dirty = true;
190 }
191}
192
193std::string EditorInspector::getChoice(const std::string &id) const {
194 const Field *f = find(id);
195 if (!f || f->kind != "choice") throw Exception("EditorInspector::getChoice: missing");
196 return f->s;
197}
198
199void EditorInspector::setChoice(const std::string &id, const std::string &value) {
200 Field *f = find(id);
201 if (!f || f->kind != "choice") throw Exception("EditorInspector::setChoice: missing");
202 if (f->s != value) {
203 f->s = value;
204 f->dirty = true;
205 }
206}
207
208std::string EditorInspector::getChoicesCsv(const std::string &id) const {
209 const Field *f = find(id);
210 if (!f || f->kind != "choice") throw Exception("EditorInspector::getChoicesCsv: missing");
211 return f->choices;
212}
213
214bool EditorInspector::isDirty(const std::string &id) const {
215 const Field *f = find(id);
216 return f && f->dirty;
217}
218
219void EditorInspector::clearDirty(const std::string &id) {
220 Field *f = find(id);
221 if (f) f->dirty = false;
222}
223
225 for (auto &f : fields_) f.dirty = false;
226 pollCursor_ = 0;
227}
228
230 const int n = static_cast<int>(fields_.size());
231 if (n == 0) return "";
232 for (int k = 0; k < n; ++k) {
233 int i = (pollCursor_ + k) % n;
234 if (fields_[i].dirty) {
235 fields_[i].dirty = false;
236 pollCursor_ = (i + 1) % n;
237 return fields_[i].id;
238 }
239 }
240 return "";
241}
242
243} // namespace eve::editor
std::string value
std::string id
int y
Definition Grass.cpp:135
int z
Definition Grass.cpp:135
int x
Definition Grass.cpp:135
glm::vec3 n
Definition Grass.cpp:64
bool dirty
float f
float step
Definition TreeMesh.cpp:196
void addFloat(const std::string &id, const std::string &label, float value, float minV, float maxV, float step)
std::string getFieldKind(int index) const
bool isDirty(const std::string &id) const
void setFloat3(const std::string &id, float x, float y, float z)
float getFloatStep(const std::string &id) const
std::string getFieldLabel(int index) const
float getFloat3X(const std::string &id) const
void setChoice(const std::string &id, const std::string &value)
std::string getString(const std::string &id) const
void clearDirty(const std::string &id)
std::string pollChangedId()
Returns next dirty field id, or "" if none.
void setFloat(const std::string &id, float value)
std::string getChoice(const std::string &id) const
std::string getChoicesCsv(const std::string &id) const
void addString(const std::string &id, const std::string &label, const std::string &value)
float getFloat(const std::string &id) const
float getFloat3Z(const std::string &id) const
std::string getFieldId(int index) const
void setString(const std::string &id, const std::string &value)
void setBool(const std::string &id, bool value)
void addBool(const std::string &id, const std::string &label, bool value)
float getFloatMax(const std::string &id) const
float getFloatMin(const std::string &id) const
void addFloat3(const std::string &id, const std::string &label, float x, float y, float z)
bool getBool(const std::string &id) const
void addChoice(const std::string &id, const std::string &label, const std::string &choicesCsv, const std::string &selected)
float getFloat3Y(const std::string &id) const