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

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