[138] | 1 | // This file is a part of the Framsticks GDK. |
---|
| 2 | // Copyright (C) 2002-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 | |
---|
| 8 | struct 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 | |
---|
| 17 | ParamInfo* MutableParamList::getParamInfo(int i) |
---|
| 18 | { |
---|
| 19 | return (ParamInfo*)list(i); |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | void MutableParamList::addPI(int pos,ParamInfo *pi) |
---|
| 23 | { |
---|
| 24 | int propcount=pi->propcount; |
---|
| 25 | int groupcount=pi->groupcount; |
---|
| 26 | if (pos==0) |
---|
| 27 | { pi->firstprop=0; pi->firstgroup=0; } |
---|
| 28 | else |
---|
| 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 | } |
---|
| 34 | list.insert(pos,pi); |
---|
| 35 | pi->propcount=0; |
---|
| 36 | pi->groupcount=0; |
---|
| 37 | for (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 | } |
---|
| 43 | for (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 | } |
---|
| 49 | if (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 | |
---|
| 61 | int MutableParamList::findPI(ParamInfo *pi) |
---|
| 62 | { |
---|
| 63 | return list.find((void*)pi); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | int MutableParamList::findPI(ParamInterface *p) |
---|
| 67 | { |
---|
| 68 | ParamInfo *pi; |
---|
| 69 | for(int i=0;pi=(ParamInfo*)list(i);i++) |
---|
| 70 | if ((!pi->mpi)&&(pi->pi==p)) return i; |
---|
| 71 | return -1; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | int MutableParamList::findPI(MutableParamInterface *p) |
---|
| 75 | { |
---|
| 76 | ParamInfo *pi; |
---|
| 77 | for(int i=0;pi=(ParamInfo*)list(i);i++) |
---|
| 78 | if ((pi->mpi)&&(pi->mpi==p)) return i; |
---|
| 79 | return -1; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | void MutableParamList::adjustPI(int firstPI,int addprop,int addgroup) |
---|
| 83 | { |
---|
| 84 | ParamInfo *pi; |
---|
| 85 | for (int i=firstPI;pi=getParamInfo(i);i++) |
---|
| 86 | { |
---|
| 87 | pi->firstprop+=addprop; |
---|
| 88 | pi->firstgroup+=addgroup; |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | void MutableParamList::removePI(int pi_index) |
---|
| 93 | { |
---|
| 94 | if (pi_index<0) return; |
---|
| 95 | ParamInfo *pi=getParamInfo(pi_index); |
---|
| 96 | for (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 | } |
---|
| 102 | for (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 | } |
---|
| 108 | list-=(pi_index); |
---|
| 109 | if (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 | } |
---|
| 119 | delete pi; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | MutableParamList::~MutableParamList() |
---|
| 123 | { |
---|
| 124 | for (int i=list.size()-1;i>=0;i--) |
---|
| 125 | removePI(i); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | void MutableParamList::onPropAdd(void* data,long i) |
---|
| 129 | { |
---|
| 130 | ParamInfo *pi=(ParamInfo*)data; |
---|
| 131 | pi->propcount++; |
---|
| 132 | int j=findPI(pi); |
---|
| 133 | if (j>=0) |
---|
| 134 | adjustPI(j+1,1,0); |
---|
| 135 | onadd.action(pi->firstprop+i); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | void MutableParamList::onPropDelete(void* data,long i) |
---|
| 139 | { |
---|
| 140 | ParamInfo *pi=(ParamInfo*)data; |
---|
| 141 | pi->propcount--; |
---|
| 142 | int j=findPI(pi); |
---|
| 143 | if (j>=0) |
---|
| 144 | adjustPI(j+1,-1,0); |
---|
| 145 | ondelete.action(pi->firstprop+i); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | void MutableParamList::onPropChange(void* data,long i) |
---|
| 149 | { |
---|
| 150 | ParamInfo *pi=(ParamInfo*)data; |
---|
| 151 | onchange.action(pi->firstprop+i); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | void MutableParamList::onPropActivate(void* data,long i) |
---|
| 155 | { |
---|
| 156 | ParamInfo *pi=(ParamInfo*)data; |
---|
| 157 | onactivate.action(pi->firstprop+i); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | void MutableParamList::onGroupAdd(void* data,long i) |
---|
| 161 | { |
---|
| 162 | ParamInfo *pi=(ParamInfo*)data; |
---|
| 163 | pi->groupcount++; |
---|
| 164 | int j=findPI(pi); |
---|
| 165 | if (j>=0) |
---|
| 166 | adjustPI(j+1,0,1); |
---|
| 167 | ongroupadd.action(pi->firstgroup+i); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | void MutableParamList::onGroupDelete(void* data,long i) |
---|
| 171 | { |
---|
| 172 | ParamInfo *pi=(ParamInfo*)data; |
---|
| 173 | pi->groupcount--; |
---|
| 174 | int j=findPI(pi); |
---|
| 175 | if (j>=0) |
---|
| 176 | adjustPI(j+1,0,-1); |
---|
| 177 | ongroupdelete.action(pi->firstgroup+i); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | void MutableParamList::onGroupChange(void* data,long i) |
---|
| 181 | { |
---|
| 182 | ParamInfo *pi=(ParamInfo*)data; |
---|
| 183 | ongroupchange.action(pi->firstgroup+i); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | void MutableParamList::insert(int pos,MutableParamInterface *p) |
---|
| 187 | { |
---|
| 188 | ParamInfo *pi=new ParamInfo(); |
---|
| 189 | pi->pi=(ParamInterface*)p; |
---|
| 190 | pi->mpi=p; |
---|
| 191 | pi->propcount=p->getPropCount(); |
---|
| 192 | pi->groupcount=p->getGroupCount(); |
---|
| 193 | addPI(pos,pi); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | void MutableParamList::insert(int pos,ParamInterface *p) |
---|
| 197 | { |
---|
| 198 | ParamInfo *pi=new ParamInfo(); |
---|
| 199 | pi->pi=p; |
---|
| 200 | pi->mpi=0; |
---|
| 201 | pi->propcount=p->getPropCount(); |
---|
| 202 | pi->groupcount=p->getGroupCount(); |
---|
| 203 | addPI(pos,pi); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | void MutableParamList::operator+=(ParamInterface *p) |
---|
| 208 | { |
---|
| 209 | insert(list.size(),p); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | void MutableParamList::operator+=(MutableParamInterface *p) |
---|
| 213 | { |
---|
| 214 | insert(list.size(),p); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | |
---|
| 218 | void MutableParamList::operator-=(ParamInterface *p) |
---|
| 219 | { |
---|
| 220 | int i=findPI(p); |
---|
| 221 | removePI(i); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | void MutableParamList::operator-=(MutableParamInterface *p) |
---|
| 225 | { |
---|
| 226 | int i=findPI(p); |
---|
| 227 | removePI(i); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | void MutableParamList::operator-=(int i) |
---|
| 231 | { |
---|
| 232 | removePI(i); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | int MutableParamList::getGroupCount() |
---|
| 236 | { |
---|
| 237 | int count=0; |
---|
| 238 | FOREACH(ParamInfo*,pi,list) |
---|
| 239 | count+=pi->groupcount; |
---|
| 240 | return count; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | int MutableParamList::getPropCount() |
---|
| 244 | { |
---|
| 245 | int count=0; |
---|
| 246 | FOREACH(ParamInfo*,pi,list) |
---|
| 247 | count+=pi->propcount; |
---|
| 248 | return count; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | int MutableParamList::getSubParam(int i,ParamInterface **sub_p,int *sub_i) |
---|
| 252 | { |
---|
| 253 | int n; |
---|
| 254 | FOREACH(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 | } |
---|
| 264 | return 0; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | int MutableParamList::getSubGroup(int i,ParamInterface **sub_p,int *sub_i) |
---|
| 268 | { |
---|
| 269 | int n; |
---|
| 270 | FOREACH(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 | } |
---|
| 280 | return 0; |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | #define FUN(_type_,_name_,_ret_) \ |
---|
| 284 | _type_ MutableParamList:: _name_ (int i) \ |
---|
| 285 | { \ |
---|
| 286 | int j; ParamInterface *pi; \ |
---|
| 287 | if (!getSubParam(i,&pi,&j)) return _ret_; \ |
---|
| 288 | return pi-> _name_ (j); \ |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | FUN(const char*,id,0) |
---|
| 292 | FUN(const char*,name,0) |
---|
| 293 | FUN(const char*,type,0) |
---|
| 294 | FUN(const char*,help,0) |
---|
| 295 | FUN(int,flags,0) |
---|
| 296 | FUN(SString,getString,SString()) |
---|
| 297 | FUN(long,getInt,0) |
---|
| 298 | FUN(double,getDouble,0) |
---|
| 299 | FUN(ExtValue,getExtValue,ExtValue((long)0)) |
---|
| 300 | FUN(ExtObject,getObject,ExtObject()) |
---|
| 301 | |
---|
| 302 | int MutableParamList::group(int i) |
---|
| 303 | { |
---|
| 304 | int n; |
---|
| 305 | int g=0; |
---|
| 306 | FOREACH(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 | } |
---|
| 313 | return 0; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | #define FUN2(_type_,_name_,_argt_) \ |
---|
| 317 | _type_ MutableParamList:: _name_ (int i,_argt_ v) \ |
---|
| 318 | { \ |
---|
| 319 | int j; ParamInterface *pi; \ |
---|
| 320 | if (!getSubParam(i,&pi,&j)) return 0; \ |
---|
| 321 | return pi-> _name_ (j,v); \ |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | FUN2(int,setInt,long) |
---|
| 325 | FUN2(int,setDouble,double) |
---|
| 326 | FUN2(int,setString,const SString &) |
---|
| 327 | FUN2(int,setObject,const ExtObject &) |
---|
| 328 | FUN2(int,setExtValue,const ExtValue &) |
---|
| 329 | |
---|
| 330 | void MutableParamList::call(int i,ExtValue* args,ExtValue *ret) |
---|
| 331 | { |
---|
| 332 | int j; ParamInterface *pi; |
---|
| 333 | if (!getSubParam(i,&pi,&j)) return; |
---|
| 334 | pi->call(j,args,ret); |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | const char *MutableParamList::grname(int i) |
---|
| 338 | { |
---|
| 339 | int j; ParamInterface *pi; |
---|
| 340 | if (!getSubGroup(i,&pi,&j)) return 0; |
---|
| 341 | return pi->grname(j); |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | int MutableParamList::grmember(int gi,int i) |
---|
| 345 | { |
---|
| 346 | int n; |
---|
| 347 | int count=0; |
---|
| 348 | FOREACH(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 | } |
---|
| 359 | return -9999; |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | void MutableParamList::clear() |
---|
| 363 | { |
---|
| 364 | for (int i=list.size()-1;i>=0;i--) |
---|
| 365 | operator-=(i); |
---|
| 366 | } |
---|