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

Last change on this file since 226 was 197, checked in by Maciej Komosinski, 11 years ago

GDK used by developers since 1999, distributed on the web since 2002

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