source: cpp/frams/param/mutparamlist.cpp @ 792

Last change on this file since 792 was 792, checked in by Maciej Komosinski, 6 years ago

Code formatting

  • Property svn:eol-style set to native
File size: 7.9 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
3// See LICENSE.txt for details.
4
5#include "mutparamlist.h"
6#include <frams/util/extvalue.h>
7
8struct ParamInfo
9{
10        ParamInterface *pi;
11        MutableParamInterface *mpi;
12        CallbackNode *anode, *dnode, *ganode, *gdnode, *cnode, *gcnode, *panode;
13        int firstprop, firstgroup;
14        int propcount, groupcount;
15};
16
17ParamInfo* MutableParamList::getParamInfo(int i)
18{
19        return (ParamInfo*)list(i);
20}
21
22void MutableParamList::addPI(int pos, ParamInfo *pi)
23{
24        int propcount = pi->propcount;
25        int groupcount = pi->groupcount;
26        if (pos == 0)
27        {
28                pi->firstprop = 0;
29                pi->firstgroup = 0;
30        }
31        else
32        {
33                ParamInfo *prev_pi = getParamInfo(pos - 1);
34                pi->firstprop = prev_pi->firstprop + prev_pi->propcount;
35                pi->firstgroup = prev_pi->firstgroup + prev_pi->groupcount;
36        }
37        list.insert(pos, pi);
38        pi->propcount = 0;
39        pi->groupcount = 0;
40        for (int i = 0; i < groupcount; i++)
41        {
42                pi->groupcount = i + 1;
43                adjustPI(pos + 1, 0, 1);
44                ongroupadd.action(pi->firstgroup + i);
45        }
46        for (int i = 0; i < propcount; i++)
47        {
48                pi->propcount = i + 1;
49                adjustPI(pos + 1, 1, 0);
50                onadd.action(pi->firstprop + i);
51        }
52        if (pi->mpi)
53        {
54                pi->anode = pi->mpi->onadd.add(STATRICKCALLBACK(this, &MutableParamList::onPropAdd, pi));
55                pi->ganode = pi->mpi->ongroupadd.add(STATRICKCALLBACK(this, &MutableParamList::onGroupAdd, pi));
56                pi->dnode = pi->mpi->ondelete.add(STATRICKCALLBACK(this, &MutableParamList::onPropDelete, pi));
57                pi->gdnode = pi->mpi->ongroupdelete.add(STATRICKCALLBACK(this, &MutableParamList::onGroupDelete, pi));
58                pi->cnode = pi->mpi->onchange.add(STATRICKCALLBACK(this, &MutableParamList::onPropChange, pi));
59                pi->gcnode = pi->mpi->ongroupchange.add(STATRICKCALLBACK(this, &MutableParamList::onGroupChange, pi));
60                pi->panode = pi->mpi->onactivate.add(STATRICKCALLBACK(this, &MutableParamList::onPropActivate, pi));
61        }
62}
63
64int MutableParamList::findPI(ParamInfo *pi)
65{
66        return list.find((void*)pi);
67}
68
69int MutableParamList::findPI(ParamInterface *p)
70{
71        ParamInfo *pi;
72        for (int i = 0; pi = (ParamInfo*)list(i); i++)
73                if ((!pi->mpi) && (pi->pi == p)) return i;
74        return -1;
75}
76
77int MutableParamList::findPI(MutableParamInterface *p)
78{
79        ParamInfo *pi;
80        for (int i = 0; pi = (ParamInfo*)list(i); i++)
81                if ((pi->mpi) && (pi->mpi == p)) return i;
82        return -1;
83}
84
85void MutableParamList::adjustPI(int firstPI, int addprop, int addgroup)
86{
87        ParamInfo *pi;
88        for (int i = firstPI; pi = getParamInfo(i); i++)
89        {
90                pi->firstprop += addprop;
91                pi->firstgroup += addgroup;
92        }
93}
94
95void MutableParamList::removePI(int pi_index)
96{
97        if (pi_index < 0) return;
98        ParamInfo *pi = getParamInfo(pi_index);
99        for (int i = pi->propcount - 1; i >= 0; i--)
100        {
101                pi->propcount = i;
102                adjustPI(pi_index + 1, -1, 0);
103                ondelete.action(i);
104        }
105        for (int i = pi->groupcount - 1; i >= 0; i--)
106        {
107                pi->groupcount = i;
108                adjustPI(pi_index + 1, 0, -1);
109                ongroupdelete.action(i);
110        }
111        list -= (pi_index);
112        if (pi->mpi)
113        {
114                pi->mpi->onadd.remove(pi->anode);
115                pi->mpi->ongroupadd.remove(pi->ganode);
116                pi->mpi->ondelete.remove(pi->dnode);
117                pi->mpi->ongroupdelete.remove(pi->gdnode);
118                pi->mpi->onchange.remove(pi->cnode);
119                pi->mpi->ongroupchange.remove(pi->gcnode);
120                pi->mpi->onactivate.remove(pi->panode);
121        }
122        delete pi;
123}
124
125MutableParamList::~MutableParamList()
126{
127        for (int i = list.size() - 1; i >= 0; i--)
128                removePI(i);
129}
130
131void MutableParamList::onPropAdd(void* data, intptr_t i)
132{
133        ParamInfo *pi = (ParamInfo*)data;
134        pi->propcount++;
135        int j = findPI(pi);
136        if (j >= 0)
137                adjustPI(j + 1, 1, 0);
138        onadd.action(pi->firstprop + i);
139}
140
141void MutableParamList::onPropDelete(void* data, intptr_t i)
142{
143        ParamInfo *pi = (ParamInfo*)data;
144        pi->propcount--;
145        int j = findPI(pi);
146        if (j >= 0)
147                adjustPI(j + 1, -1, 0);
148        ondelete.action(pi->firstprop + i);
149}
150
151void MutableParamList::onPropChange(void* data, intptr_t i)
152{
153        ParamInfo *pi = (ParamInfo*)data;
154        onchange.action(pi->firstprop + i);
155}
156
157void MutableParamList::onPropActivate(void* data, intptr_t i)
158{
159        ParamInfo *pi = (ParamInfo*)data;
160        onactivate.action(pi->firstprop + i);
161}
162
163void MutableParamList::onGroupAdd(void* data, intptr_t i)
164{
165        ParamInfo *pi = (ParamInfo*)data;
166        pi->groupcount++;
167        int j = findPI(pi);
168        if (j >= 0)
169                adjustPI(j + 1, 0, 1);
170        ongroupadd.action(pi->firstgroup + i);
171}
172
173void MutableParamList::onGroupDelete(void* data, intptr_t i)
174{
175        ParamInfo *pi = (ParamInfo*)data;
176        pi->groupcount--;
177        int j = findPI(pi);
178        if (j >= 0)
179                adjustPI(j + 1, 0, -1);
180        ongroupdelete.action(pi->firstgroup + i);
181}
182
183void MutableParamList::onGroupChange(void* data, intptr_t i)
184{
185        ParamInfo *pi = (ParamInfo*)data;
186        ongroupchange.action(pi->firstgroup + i);
187}
188
189void MutableParamList::insert(int pos, MutableParamInterface *p)
190{
191        ParamInfo *pi = new ParamInfo();
192        pi->pi = (ParamInterface*)p;
193        pi->mpi = p;
194        pi->propcount = p->getPropCount();
195        pi->groupcount = p->getGroupCount();
196        addPI(pos, pi);
197}
198
199void MutableParamList::insert(int pos, ParamInterface *p)
200{
201        ParamInfo *pi = new ParamInfo();
202        pi->pi = p;
203        pi->mpi = 0;
204        pi->propcount = p->getPropCount();
205        pi->groupcount = p->getGroupCount();
206        addPI(pos, pi);
207}
208
209
210void MutableParamList::operator+=(ParamInterface *p)
211{
212        insert(list.size(), p);
213}
214
215void MutableParamList::operator+=(MutableParamInterface *p)
216{
217        insert(list.size(), p);
218}
219
220
221void MutableParamList::operator-=(ParamInterface *p)
222{
223        int i = findPI(p);
224        removePI(i);
225}
226
227void MutableParamList::operator-=(MutableParamInterface *p)
228{
229        int i = findPI(p);
230        removePI(i);
231}
232
233void MutableParamList::operator-=(int i)
234{
235        removePI(i);
236}
237
238int MutableParamList::getGroupCount()
239{
240        int count = 0;
241        FOREACH(ParamInfo*, pi, list)
242                count += pi->groupcount;
243        return count;
244}
245
246int MutableParamList::getPropCount()
247{
248        int count = 0;
249        FOREACH(ParamInfo*, pi, list)
250                count += pi->propcount;
251        return count;
252}
253
254int MutableParamList::getSubParam(int i, ParamInterface **sub_p, int *sub_i)
255{
256        int n;
257        FOREACH(ParamInfo*, pi, list)
258        {
259                if (i < (n = pi->propcount))
260                {
261                        *sub_p = pi->pi;
262                        *sub_i = i;
263                        return 1;
264                }
265                i -= n;
266        }
267        return 0;
268}
269
270int MutableParamList::getSubGroup(int i, ParamInterface **sub_p, int *sub_i)
271{
272        int n;
273        FOREACH(ParamInfo*, pi, list)
274        {
275                if (i < (n = pi->groupcount))
276                {
277                        *sub_p = pi->pi;
278                        *sub_i = i;
279                        return 1;
280                }
281                i -= n;
282        }
283        return 0;
284}
285
286#define FUN(_type_,_name_,_ret_) \
287_type_ MutableParamList:: _name_ (int i) \
288{ \
289int j; ParamInterface *pi; \
290if (!getSubParam(i,&pi,&j)) return _ret_; \
291return pi-> _name_ (j); \
292}
293
294FUN(const char*, id, 0)
295FUN(const char*, name, 0)
296FUN(const char*, type, 0)
297FUN(const char*, help, 0)
298FUN(int, flags, 0)
299FUN(SString, getString, SString())
300FUN(paInt, getInt, 0)
301FUN(double, getDouble, 0)
302FUN(ExtValue, getExtValue, ExtValue((paInt)0))
303FUN(ExtObject, getObject, ExtObject())
304
305int MutableParamList::group(int i)
306{
307        int n;
308        int g = 0;
309        FOREACH(ParamInfo*, pi, list)
310        {
311                if (i < (n = pi->propcount))
312                        return g + pi->pi->group(i);
313                g += pi->groupcount;
314                i -= n;
315        }
316        return 0;
317}
318
319#define FUN2(_type_,_name_,_argt_) \
320_type_ MutableParamList:: _name_ (int i,_argt_ v) \
321{ \
322int j; ParamInterface *pi; \
323if (!getSubParam(i,&pi,&j)) return 0; \
324return pi-> _name_ (j,v); \
325}
326
327FUN2(int, setInt, paInt)
328FUN2(int, setDouble, double)
329FUN2(int, setString, const SString &)
330FUN2(int, setObject, const ExtObject &)
331FUN2(int, setExtValue, const ExtValue &)
332
333void MutableParamList::call(int i, ExtValue* args, ExtValue *ret)
334{
335        int j; ParamInterface *pi;
336        if (!getSubParam(i, &pi, &j)) return;
337        pi->call(j, args, ret);
338}
339
340const char *MutableParamList::grname(int i)
341{
342        int j; ParamInterface *pi;
343        if (!getSubGroup(i, &pi, &j)) return 0;
344        return pi->grname(j);
345}
346
347int MutableParamList::grmember(int gi, int i)
348{
349        int n;
350        int count = 0;
351        FOREACH(ParamInfo*, pi, list)
352        {
353                if (gi < (n = pi->groupcount))
354                {
355                        int prop = pi->pi->grmember(gi, i);
356                        if (prop >= pi->propcount) return -9999;
357                        return count + prop;
358                }
359                count += pi->propcount;
360                gi -= n;
361        }
362        return -9999;
363}
364
365void MutableParamList::clear()
366{
367        for (int i = list.size() - 1; i >= 0; i--)
368                operator-=(i);
369}
Note: See TracBrowser for help on using the repository browser.