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

Last change on this file since 836 was 832, checked in by Maciej Komosinski, 5 years ago

Renamed MutableParam? property manipulation functions to avoid potential name clash in scripts; added warning for docs to discourage their use; automatically calculated the correct static member count from the paramtab array

  • Property svn:eol-style set to native
File size: 9.2 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2018  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        { "_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), },
21};
22#undef FIELDSTRUCT
23const int MutableParam::staticprops = ARRAY_LENGTH(pe_tab);
24
25MutableParam::MutableParam(const char*n, const char*g, int gr0)
26        :SimpleAbstractParam(this, n), persistgroup0(gr0), grprefix(g)
27{
28        if (persistgroup0)
29                addGroup(grprefix, 1);
30}
31
32int MutableParam::findGroup(const SString name, int ignoreprefix)
33{
34        int skipprefix = grprefix.len() ? grprefix.len() + 2 : 0;
35        for (int i = 0; i < groups.size(); i++)
36        {
37                if (ignoreprefix)
38                {
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;
43                }
44                else
45                        if (groupname(i) == name) return i;
46        }
47        return -1;
48}
49
50int MutableParam::addGroup(const SString& gname, int noprefix)
51{
52        SString tmp;
53        if (noprefix)
54                tmp = gname;
55        else
56        {
57                tmp = grprefix;
58                if (tmp.len()) tmp += ": ";
59                tmp += gname;
60        }
61        groups += new SString(tmp);
62        int position = groups.size() - 1;
63        ongroupadd.action(position);
64        return position;
65}
66
67void MutableParam::removeGroup(int g)
68{
69        if ((g < 0) || (g >= MutableParam::getGroupCount())) return;
70        ParamEntry *e;
71        for (int i = MutableParam::getPropCount() - 1; i >= staticprops; i--)
72        {
73                e = entry(i);
74                if (g == e->group)
75                        removeProperty(i);
76        }
77        SString *s = (SString *)groups(g);
78        if (s) delete s;
79        groups -= g;
80        ongroupdelete.action(g);
81}
82
83int MutableParam::grmember(int g, int a)
84{
85        if (g == 0)
86        {
87                if (getGroupCount() < 2)
88                        return (a < getPropCount()) ? a : -9999;
89                if (a < staticprops) return a;
90                a -= staticprops;
91        }
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;
97}
98
99int MutableParam::addProperty(ParamEntry *pe, int position)
100{
101        DB(printf("MutableParam::add(%s)\n", pe->id));
102        if (position < 0)
103                position = entries.size() + staticprops;
104        entries.insert(position - staticprops, pe);
105
106        if (pe->offset)
107                pe->flags &= ~MUTPARAM_ALLOCDATA;
108        else
109        {
110                pe->flags |= MUTPARAM_ALLOCDATA;
111                void *d = 0;
112                switch (pe->type[0])
113                {
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;
119                }
120                pe->offset = (intptr_t)d;
121        }
122        onadd.action(position);
123        return position;
124}
125
126ParamEntry * MutableParam::removeProperty(ParamEntry *pe)
127{
128        int index = entries.find(pe);
129        if (index >= 0) return removeProperty(index); else return pe;
130}
131
132ParamEntry * MutableParam::removeProperty(int i)
133{
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])
139                {
140                case 'd': delete (paInt*)d; break;
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;
145                }
146        entries -= i - staticprops;
147        if (pe->flags & MUTPARAM_ALLOCENTRY)
148        {
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;
154        }
155        ondelete.action(i);
156        return pe;
157}
158
159void MutableParam::clear(int everything)
160{
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);
167}
168
169void MutableParam::p_clear(ExtValue *args, ExtValue *ret)
170{
171        clear();
172}
173
174int MutableParam::addProperty(void* data, const char* id, const char* type, const char* name, const char* help, int flags, int group, int position)
175{
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);
188}
189
190static 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
199bool 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
214void MutableParam::p_addprop(ExtValue *args, ExtValue *ret)
215{
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());
217        ret->setInt(i);
218}
219
220void 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
232void MutableParam::p_remprop(ExtValue *args, ExtValue *ret)
233{
234        removeProperty(args[0].getInt());
235}
236
237void MutableParam::p_addgroup(ExtValue *args, ExtValue *ret)
238{
239        int i = addGroup(args[0].getString());
240        ret->setInt(i);
241}
242
243void MutableParam::p_remgroup(ExtValue *args, ExtValue *ret)
244{
245        removeGroup(args[0].getInt());
246}
247
248void MutableParam::notify(int id)
249{
250        changed = id;
251        onactivate.action(id);
252}
253
254int MutableParam::setInt(int i, paInt v)
255{
256        int ret = SimpleAbstractParam::setInt(i, v);
257        if (ret & PSET_CHANGED) notify(i);
258        return ret;
259}
260
261int MutableParam::setDouble(int i, double v)
262{
263        int ret = SimpleAbstractParam::setDouble(i, v);
264        if (ret & PSET_CHANGED) notify(i);
265        return ret;
266}
267
268int MutableParam::setString(int i, const SString &v)
269{
270        int ret = SimpleAbstractParam::setString(i, v);
271        if (ret & PSET_CHANGED) notify(i);
272        return ret;
273}
274
275int MutableParam::setObject(int i, const ExtObject &v)
276{
277        int ret = SimpleAbstractParam::setObject(i, v);
278        if (ret & PSET_CHANGED) notify(i);
279        return ret;
280}
281
282int MutableParam::setExtValue(int i, const ExtValue &v)
283{
284        int ret = SimpleAbstractParam::setExtValue(i, v);
285        if (ret & PSET_CHANGED) notify(i);
286        return ret;
287}
288
289void MutableParam::call(int i, ExtValue* args, ExtValue *ret)
290{
291        if (i < staticprops) return SimpleAbstractParam::call(i, args, ret);
292        notify(i);
293}
294
295///////////////////
296
297void ParamSaver::clear()
298{
299        for (int i = 0; i < store.size(); i += 2)
300        {
301                SString *n = (SString*)store(i);
302                ExtValue *v = (ExtValue*)store(i + 1);
303                delete n;
304                delete v;
305        }
306        store.clear();
307}
308
309void ParamSaver::loadFrom(ParamInterface& p)
310{
311        int N = p.getPropCount();
312        for (int i = 0; i < N; i++)
313        {
314                if (shouldLoad(p, i))
315                {
316                        ExtValue v;
317                        p.get(i, v);
318                        store += new SString(p.id(i));
319                        store += new ExtValue(v);
320                }
321        }
322}
323
324void ParamSaver::saveTo(MutableParam& p)
325{
326        for (int i = 0; i < store.size(); i += 2)
327        {
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));
333        }
334}
Note: See TracBrowser for help on using the repository browser.