[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[824] | 2 | // Copyright (C) 1999-2018 Maciej Komosinski and Szymon Ulatowski. |
---|
[286] | 3 | // See LICENSE.txt for details. |
---|
[138] | 4 | |
---|
| 5 | #include "mutableparam.h" |
---|
| 6 | #include <frams/util/extvalue.h> |
---|
| 7 | |
---|
[832] | 8 | #define PROPERTY_WARNING "Using most _property functions is restricted for internal purposes. Use \"property:\" or \"state:\" definitions in your script files to change object properties." |
---|
| 9 | |
---|
[138] | 10 | #define FIELDSTRUCT MutableParam |
---|
[792] | 11 | ParamEntry MutableParam::pe_tab[] = |
---|
[832] | 12 | { |
---|
| 13 | { "_propertyClear", 0, PARAM_DONTSAVE | PARAM_USERHIDDEN, "Remove all properties", "p()", PROCEDURE(p_clear), PROPERTY_WARNING }, |
---|
| 14 | { "_propertyAdd", 0, PARAM_DONTSAVE | PARAM_USERHIDDEN, "Add property (id,type,name,help)", "p(s id,s type description,s name,d flags,s help text)", PROCEDURE(p_addprop), PROPERTY_WARNING }, |
---|
| 15 | { "_propertyRemove", 0, PARAM_DONTSAVE | PARAM_USERHIDDEN, "Remove property", "p(d index)", PROCEDURE(p_remprop), PROPERTY_WARNING }, |
---|
| 16 | { "_propertyChange", 0, PARAM_DONTSAVE | PARAM_USERHIDDEN, "Change property", "p(s id,s type description,s name,d flags,s help text)", PROCEDURE(p_changeprop), PROPERTY_WARNING }, |
---|
| 17 | { "_propertyAddGroup", 0, PARAM_DONTSAVE | PARAM_USERHIDDEN, "Add property group", "p(s name)", PROCEDURE(p_addgroup), PROPERTY_WARNING }, |
---|
| 18 | { "_propertyRemoveGroup", 0, PARAM_DONTSAVE | PARAM_USERHIDDEN, "Remove property group", "p(d index)", PROCEDURE(p_remgroup), PROPERTY_WARNING }, |
---|
| 19 | { "_property_changed_index", 0, PARAM_DONTSAVE | PARAM_USERHIDDEN | PARAM_READONLY, "Last changed property index", "d", FIELD(changed), }, |
---|
| 20 | { "_property_changed_id", 0, PARAM_DONTSAVE | PARAM_USERHIDDEN | PARAM_READONLY, "Last changed property id", "s", GETONLY(changedname), }, |
---|
[138] | 21 | }; |
---|
| 22 | #undef FIELDSTRUCT |
---|
[832] | 23 | const int MutableParam::staticprops = ARRAY_LENGTH(pe_tab); |
---|
[138] | 24 | |
---|
[792] | 25 | MutableParam::MutableParam(const char*n, const char*g, int gr0) |
---|
| 26 | :SimpleAbstractParam(this, n), persistgroup0(gr0), grprefix(g) |
---|
[138] | 27 | { |
---|
[792] | 28 | if (persistgroup0) |
---|
| 29 | addGroup(grprefix, 1); |
---|
[138] | 30 | } |
---|
| 31 | |
---|
[792] | 32 | int MutableParam::findGroup(const SString name, int ignoreprefix) |
---|
[138] | 33 | { |
---|
[792] | 34 | int skipprefix = grprefix.len() ? grprefix.len() + 2 : 0; |
---|
| 35 | for (int i = 0; i < groups.size(); i++) |
---|
[138] | 36 | { |
---|
[792] | 37 | if (ignoreprefix) |
---|
[138] | 38 | { |
---|
[792] | 39 | const char *noprefix = groupname(i).c_str(); |
---|
| 40 | if ((int)strlen(noprefix) < skipprefix) continue; |
---|
| 41 | noprefix += skipprefix; |
---|
| 42 | if (!strcmp(noprefix, name.c_str())) return i; |
---|
[138] | 43 | } |
---|
| 44 | else |
---|
[792] | 45 | if (groupname(i) == name) return i; |
---|
[138] | 46 | } |
---|
[792] | 47 | return -1; |
---|
[138] | 48 | } |
---|
| 49 | |
---|
[792] | 50 | int MutableParam::addGroup(const SString& gname, int noprefix) |
---|
[138] | 51 | { |
---|
[792] | 52 | SString tmp; |
---|
| 53 | if (noprefix) |
---|
| 54 | tmp = gname; |
---|
| 55 | else |
---|
[138] | 56 | { |
---|
[792] | 57 | tmp = grprefix; |
---|
| 58 | if (tmp.len()) tmp += ": "; |
---|
| 59 | tmp += gname; |
---|
[138] | 60 | } |
---|
[792] | 61 | groups += new SString(tmp); |
---|
| 62 | int position = groups.size() - 1; |
---|
| 63 | ongroupadd.action(position); |
---|
| 64 | return position; |
---|
[138] | 65 | } |
---|
| 66 | |
---|
| 67 | void MutableParam::removeGroup(int g) |
---|
| 68 | { |
---|
[792] | 69 | if ((g < 0) || (g >= MutableParam::getGroupCount())) return; |
---|
| 70 | ParamEntry *e; |
---|
| 71 | for (int i = MutableParam::getPropCount() - 1; i >= staticprops; i--) |
---|
[138] | 72 | { |
---|
[792] | 73 | e = entry(i); |
---|
| 74 | if (g == e->group) |
---|
| 75 | removeProperty(i); |
---|
[138] | 76 | } |
---|
[792] | 77 | SString *s = (SString *)groups(g); |
---|
| 78 | if (s) delete s; |
---|
| 79 | groups -= g; |
---|
| 80 | ongroupdelete.action(g); |
---|
[138] | 81 | } |
---|
| 82 | |
---|
[792] | 83 | int MutableParam::grmember(int g, int a) |
---|
[138] | 84 | { |
---|
[792] | 85 | if (g == 0) |
---|
[138] | 86 | { |
---|
[792] | 87 | if (getGroupCount() < 2) |
---|
| 88 | return (a < getPropCount()) ? a : -9999; |
---|
| 89 | if (a < staticprops) return a; |
---|
| 90 | a -= staticprops; |
---|
[138] | 91 | } |
---|
[792] | 92 | ParamEntry *e; |
---|
| 93 | for (int i = staticprops; e = entry(i); i++) |
---|
| 94 | if (g == e->group) |
---|
| 95 | if (!a--) return i; |
---|
| 96 | return -9999; |
---|
[138] | 97 | } |
---|
| 98 | |
---|
[792] | 99 | int MutableParam::addProperty(ParamEntry *pe, int position) |
---|
[138] | 100 | { |
---|
[792] | 101 | DB(printf("MutableParam::add(%s)\n", pe->id)); |
---|
| 102 | if (position < 0) |
---|
| 103 | position = entries.size() + staticprops; |
---|
| 104 | entries.insert(position - staticprops, pe); |
---|
[138] | 105 | |
---|
[792] | 106 | if (pe->offset) |
---|
| 107 | pe->flags &= ~MUTPARAM_ALLOCDATA; |
---|
| 108 | else |
---|
[138] | 109 | { |
---|
[792] | 110 | pe->flags |= MUTPARAM_ALLOCDATA; |
---|
| 111 | void *d = 0; |
---|
| 112 | switch (pe->type[0]) |
---|
[138] | 113 | { |
---|
[792] | 114 | case 'd': d = new paInt(); *((paInt*)d) = 0; break; |
---|
| 115 | case 'f': d = new double(); *((double*)d) = 0; break; |
---|
| 116 | case 's': d = new SString(); break; |
---|
| 117 | case 'x': d = new ExtValue(); break; |
---|
| 118 | case 'o': d = new ExtObject(); break; |
---|
[138] | 119 | } |
---|
[792] | 120 | pe->offset = (intptr_t)d; |
---|
[138] | 121 | } |
---|
[792] | 122 | onadd.action(position); |
---|
| 123 | return position; |
---|
[138] | 124 | } |
---|
| 125 | |
---|
| 126 | ParamEntry * MutableParam::removeProperty(ParamEntry *pe) |
---|
| 127 | { |
---|
[792] | 128 | int index = entries.find(pe); |
---|
| 129 | if (index >= 0) return removeProperty(index); else return pe; |
---|
[138] | 130 | } |
---|
| 131 | |
---|
| 132 | ParamEntry * MutableParam::removeProperty(int i) |
---|
| 133 | { |
---|
[792] | 134 | ParamEntry *pe = (ParamEntry *)entries(i - staticprops); |
---|
| 135 | DB(printf("MutableParam::remove(%d)\n", i)); |
---|
| 136 | void *d = (void*)pe->offset; |
---|
| 137 | if (d && (pe->flags & MUTPARAM_ALLOCDATA)) |
---|
| 138 | switch (pe->type[0]) |
---|
[824] | 139 | { |
---|
[247] | 140 | case 'd': delete (paInt*)d; break; |
---|
[138] | 141 | case 'f': delete (double*)d; break; |
---|
| 142 | case 's': delete (SString*)d; break; |
---|
| 143 | case 'x': delete (ExtValue*)d; break; |
---|
| 144 | case 'o': delete (ExtObject*)d; break; |
---|
[824] | 145 | } |
---|
[792] | 146 | entries -= i - staticprops; |
---|
| 147 | if (pe->flags & MUTPARAM_ALLOCENTRY) |
---|
[138] | 148 | { |
---|
[792] | 149 | if (pe->name) free((void*)pe->name); |
---|
| 150 | if (pe->id) free((void*)pe->id); |
---|
| 151 | if (pe->help) free((void*)pe->help); |
---|
| 152 | if (pe->type) free((void*)pe->type); |
---|
| 153 | delete pe; |
---|
[138] | 154 | } |
---|
[792] | 155 | ondelete.action(i); |
---|
| 156 | return pe; |
---|
[138] | 157 | } |
---|
| 158 | |
---|
| 159 | void MutableParam::clear(int everything) |
---|
| 160 | { |
---|
[792] | 161 | DB(printf("MutableParam::clear\n")); |
---|
| 162 | for (int i = entries.size() - 1; i >= 0; i--) |
---|
| 163 | removeProperty(i + staticprops); |
---|
| 164 | int lastgroup = (everything || (persistgroup0 == 0)) ? 0 : 1; |
---|
| 165 | for (int i = groups.size() - 1; i >= lastgroup; i--) |
---|
| 166 | removeGroup(i); |
---|
[138] | 167 | } |
---|
| 168 | |
---|
[792] | 169 | void MutableParam::p_clear(ExtValue *args, ExtValue *ret) |
---|
| 170 | { |
---|
| 171 | clear(); |
---|
| 172 | } |
---|
[138] | 173 | |
---|
[792] | 174 | int MutableParam::addProperty(void* data, const char* id, const char* type, const char* name, const char* help, int flags, int group, int position) |
---|
[138] | 175 | { |
---|
[792] | 176 | if ((!id) && (!type)) return -1; |
---|
| 177 | if (!isValidTypeDescription(type)) return -1; |
---|
| 178 | ParamEntry *pe = new ParamEntry(); |
---|
| 179 | pe->fun1 = 0; pe->fun2 = 0; |
---|
| 180 | pe->group = (paInt)group; |
---|
| 181 | pe->flags = (paInt)(flags | MUTPARAM_ALLOCENTRY); |
---|
| 182 | pe->offset = (intptr_t)data; |
---|
| 183 | pe->id = strdup(id); |
---|
| 184 | pe->type = strdup(type); |
---|
| 185 | pe->name = name ? strdup(name) : 0; |
---|
| 186 | pe->help = help ? strdup(help) : 0; |
---|
| 187 | return addProperty(pe, position); |
---|
[138] | 188 | } |
---|
| 189 | |
---|
[824] | 190 | static void changeString(const char* (&s), const char* newstr) |
---|
| 191 | { |
---|
| 192 | if ((newstr != NULL) && (newstr[0] == 0)) newstr = NULL; |
---|
| 193 | if ((s == NULL) && (newstr == NULL)) return; |
---|
| 194 | if ((s != NULL) && (newstr != NULL) && (strcmp(s, newstr) == 0)) return; |
---|
| 195 | if (s != NULL) { free((void*)s); s = NULL; } |
---|
| 196 | if (newstr != NULL) s = strdup(newstr); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | bool MutableParam::changeProperty(int i, const char* id, const char* type, const char* name, const char* help, int flags, int group) |
---|
| 200 | { |
---|
| 201 | ParamEntry *pe = entry(i); |
---|
| 202 | if ((!id) && (!type)) return false; |
---|
| 203 | if (!isValidTypeDescription(type)) return false; |
---|
| 204 | pe->group = (paInt)group; |
---|
| 205 | pe->flags = (pe->flags & (MUTPARAM_ALLOCENTRY | MUTPARAM_ALLOCDATA)) | (flags & ~(MUTPARAM_ALLOCENTRY | MUTPARAM_ALLOCDATA)); |
---|
| 206 | changeString(pe->id, id); |
---|
| 207 | changeString(pe->name, name); |
---|
| 208 | changeString(pe->type, type); |
---|
| 209 | changeString(pe->help, help); |
---|
| 210 | onchange.action(i); |
---|
| 211 | return true; |
---|
| 212 | } |
---|
| 213 | |
---|
[792] | 214 | void MutableParam::p_addprop(ExtValue *args, ExtValue *ret) |
---|
[138] | 215 | { |
---|
[832] | 216 | int i = addProperty(0, args[4].getString().c_str(), args[3].getString().c_str(), args[2].getString().c_str(), args[0].getString().c_str(), args[1].getInt()); |
---|
[792] | 217 | ret->setInt(i); |
---|
[138] | 218 | } |
---|
| 219 | |
---|
[824] | 220 | void MutableParam::p_changeprop(ExtValue *args, ExtValue *ret) |
---|
| 221 | { |
---|
| 222 | int i = findId(args[4].getString().c_str()); |
---|
| 223 | if (i >= staticprops) |
---|
| 224 | { |
---|
| 225 | changeProperty(i, args[4].getString().c_str(), args[3].getString().c_str(), args[2].getString().c_str(), args[0].getString().c_str(), args[1].getInt(), entry(i)->group); |
---|
| 226 | ret->setInt(i); |
---|
| 227 | } |
---|
| 228 | else |
---|
| 229 | ret->setEmpty(); |
---|
| 230 | } |
---|
| 231 | |
---|
[792] | 232 | void MutableParam::p_remprop(ExtValue *args, ExtValue *ret) |
---|
[138] | 233 | { |
---|
[792] | 234 | removeProperty(args[0].getInt()); |
---|
[138] | 235 | } |
---|
| 236 | |
---|
[792] | 237 | void MutableParam::p_addgroup(ExtValue *args, ExtValue *ret) |
---|
[138] | 238 | { |
---|
[792] | 239 | int i = addGroup(args[0].getString()); |
---|
| 240 | ret->setInt(i); |
---|
[138] | 241 | } |
---|
| 242 | |
---|
[792] | 243 | void MutableParam::p_remgroup(ExtValue *args, ExtValue *ret) |
---|
[138] | 244 | { |
---|
[792] | 245 | removeGroup(args[0].getInt()); |
---|
[138] | 246 | } |
---|
| 247 | |
---|
| 248 | void MutableParam::notify(int id) |
---|
| 249 | { |
---|
[792] | 250 | changed = id; |
---|
| 251 | onactivate.action(id); |
---|
[138] | 252 | } |
---|
| 253 | |
---|
[792] | 254 | int MutableParam::setInt(int i, paInt v) |
---|
[138] | 255 | { |
---|
[792] | 256 | int ret = SimpleAbstractParam::setInt(i, v); |
---|
| 257 | if (ret & PSET_CHANGED) notify(i); |
---|
| 258 | return ret; |
---|
[138] | 259 | } |
---|
| 260 | |
---|
[792] | 261 | int MutableParam::setDouble(int i, double v) |
---|
[138] | 262 | { |
---|
[792] | 263 | int ret = SimpleAbstractParam::setDouble(i, v); |
---|
| 264 | if (ret & PSET_CHANGED) notify(i); |
---|
| 265 | return ret; |
---|
[138] | 266 | } |
---|
| 267 | |
---|
[792] | 268 | int MutableParam::setString(int i, const SString &v) |
---|
[138] | 269 | { |
---|
[792] | 270 | int ret = SimpleAbstractParam::setString(i, v); |
---|
| 271 | if (ret & PSET_CHANGED) notify(i); |
---|
| 272 | return ret; |
---|
[138] | 273 | } |
---|
| 274 | |
---|
[792] | 275 | int MutableParam::setObject(int i, const ExtObject &v) |
---|
[138] | 276 | { |
---|
[792] | 277 | int ret = SimpleAbstractParam::setObject(i, v); |
---|
| 278 | if (ret & PSET_CHANGED) notify(i); |
---|
| 279 | return ret; |
---|
[138] | 280 | } |
---|
| 281 | |
---|
[792] | 282 | int MutableParam::setExtValue(int i, const ExtValue &v) |
---|
[138] | 283 | { |
---|
[792] | 284 | int ret = SimpleAbstractParam::setExtValue(i, v); |
---|
| 285 | if (ret & PSET_CHANGED) notify(i); |
---|
| 286 | return ret; |
---|
[138] | 287 | } |
---|
| 288 | |
---|
[792] | 289 | void MutableParam::call(int i, ExtValue* args, ExtValue *ret) |
---|
[138] | 290 | { |
---|
[792] | 291 | if (i < staticprops) return SimpleAbstractParam::call(i, args, ret); |
---|
| 292 | notify(i); |
---|
[138] | 293 | } |
---|
| 294 | |
---|
| 295 | /////////////////// |
---|
| 296 | |
---|
| 297 | void ParamSaver::clear() |
---|
| 298 | { |
---|
[792] | 299 | for (int i = 0; i < store.size(); i += 2) |
---|
[138] | 300 | { |
---|
[792] | 301 | SString *n = (SString*)store(i); |
---|
| 302 | ExtValue *v = (ExtValue*)store(i + 1); |
---|
| 303 | delete n; |
---|
| 304 | delete v; |
---|
| 305 | } |
---|
| 306 | store.clear(); |
---|
[138] | 307 | } |
---|
| 308 | |
---|
| 309 | void ParamSaver::loadFrom(ParamInterface& p) |
---|
| 310 | { |
---|
[792] | 311 | int N = p.getPropCount(); |
---|
| 312 | for (int i = 0; i < N; i++) |
---|
[138] | 313 | { |
---|
[792] | 314 | if (shouldLoad(p, i)) |
---|
[138] | 315 | { |
---|
[792] | 316 | ExtValue v; |
---|
| 317 | p.get(i, v); |
---|
| 318 | store += new SString(p.id(i)); |
---|
| 319 | store += new ExtValue(v); |
---|
[138] | 320 | } |
---|
| 321 | } |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | void ParamSaver::saveTo(MutableParam& p) |
---|
| 325 | { |
---|
[792] | 326 | for (int i = 0; i < store.size(); i += 2) |
---|
[138] | 327 | { |
---|
[792] | 328 | SString *n = (SString*)store(i); |
---|
| 329 | int prop = p.findId(n->c_str()); |
---|
| 330 | if (prop < 0) |
---|
| 331 | prop = p.addProperty(0, n->c_str(), "x", 0, 0, 0, 0, -1); |
---|
| 332 | p.setExtValue(prop, *(ExtValue*)store(i + 1)); |
---|
[138] | 333 | } |
---|
| 334 | } |
---|