source: cpp/frams/param/mutableparam.cpp @ 1097

Last change on this file since 1097 was 973, checked in by Maciej Komosinski, 4 years ago

Increased SString and std::string compatibility: introduced length(), size(), and capacity(), and removed legacy methods that have std::string equivalents

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