[5] | 1 | // This file is a part of Framsticks GDK library. |
---|
| 2 | // Copyright (C) 2002-2006 Szymon Ulatowski. See LICENSE.txt for details. |
---|
| 3 | // Refer to http://www.frams.alife.pl/ for further information. |
---|
| 4 | |
---|
| 5 | #ifndef _PARAM_H_ |
---|
| 6 | #define _PARAM_H_ |
---|
| 7 | |
---|
| 8 | #include <stdio.h> |
---|
| 9 | #include "sstring.h" |
---|
| 10 | #include "list.h" |
---|
| 11 | #include "statrick.h" |
---|
| 12 | #include "virtfile.h" |
---|
| 13 | |
---|
| 14 | class ExtValue; |
---|
| 15 | class ExtObject; |
---|
| 16 | |
---|
| 17 | // ParamInterface flags: |
---|
| 18 | #define PARAM_READONLY 1 |
---|
| 19 | #define PARAM_DONTSAVE 2 |
---|
| 20 | #define PARAM_SETLEVEL(x) (((x)&3)<<2) |
---|
| 21 | #define PARAM_LEVEL(x) (((x)>>2)&3) |
---|
| 22 | #define PARAM_USERREADONLY 16 |
---|
| 23 | #define PARAM_USERHIDDEN 32 |
---|
| 24 | // for mutableparam (private!) |
---|
| 25 | #define MUTPARAM_ALLOCENTRY 64 |
---|
| 26 | #define MUTPARAM_ALLOCDATA 128 |
---|
| 27 | #define PARAM_NOSTATIC 256 |
---|
| 28 | #define PARAM_CONST 512 |
---|
| 29 | #define PARAM_CANOMITNAME 1024 |
---|
| 30 | #define PARAM_DONTLOAD 2048 |
---|
| 31 | |
---|
| 32 | // wynik z param::set() to kombinacja bitow: |
---|
| 33 | |
---|
| 34 | #define PSET_RONLY 1 |
---|
| 35 | // oznacza,ze nie mozna zmienic |
---|
| 36 | |
---|
| 37 | #define PSET_CHANGED 2 |
---|
| 38 | // zaawansowane: wartosc zostala zmieniona |
---|
| 39 | |
---|
| 40 | #define PSET_HITMIN 4 |
---|
| 41 | #define PSET_HITMAX 8 |
---|
| 42 | // wartosc zostala dopasowana do min lub max |
---|
| 43 | |
---|
| 44 | #define PSET_WARN (PSET_RONLY | PSET_HITMIN | PSET_HITMAX) |
---|
| 45 | // pozyteczna kombinacja - oznacza, ze nalezy pobrac i wyswietlic |
---|
| 46 | // wartosc, zeby uzytkownik wiedzial, ze jego propozycja jest odrzucona |
---|
| 47 | |
---|
| 48 | #define PSET_NOPROPERTY 16 |
---|
| 49 | |
---|
| 50 | /** Property get/set interface - runtime access to named properties */ |
---|
| 51 | class ParamInterface |
---|
| 52 | { |
---|
| 53 | public: |
---|
| 54 | virtual int getGroupCount()=0; ///< @return number of property groups |
---|
| 55 | virtual int getPropCount()=0; ///< @return number of properties |
---|
| 56 | |
---|
| 57 | virtual const char* getName()=0; |
---|
| 58 | virtual const char* getDescription() {return 0;} |
---|
| 59 | |
---|
| 60 | int findId(const char *n); ///< find id number for internal name |
---|
| 61 | int findIdn(const char *naz,int n); |
---|
| 62 | |
---|
| 63 | virtual const char *id(int i)=0; ///< get internal name |
---|
| 64 | virtual const char *name(int i)=0; ///< get human readable name |
---|
| 65 | |
---|
| 66 | /** get type description. |
---|
| 67 | first character defines basic datatype: |
---|
| 68 | - d = integer |
---|
| 69 | - f = floating point |
---|
| 70 | - s = string |
---|
| 71 | - o = ExtObject |
---|
| 72 | - x = ExtValue (universal datatype) |
---|
| 73 | */ |
---|
| 74 | virtual const char *type(int i)=0; |
---|
| 75 | |
---|
| 76 | virtual const char *help(int i)=0; ///< get long description (tooltip) |
---|
| 77 | |
---|
| 78 | virtual int flags(int i)=0; ///< get flags |
---|
| 79 | |
---|
| 80 | virtual int group(int i)=0; ///< get group id for a property |
---|
| 81 | virtual const char *grname(int gi)=0; ///< get group name |
---|
| 82 | virtual int grmember(int gi,int n)=0; ///< get property id for n'th member of group "gi" |
---|
| 83 | |
---|
| 84 | virtual void call(int i,ExtValue* args,ExtValue *ret)=0; |
---|
| 85 | |
---|
| 86 | void get(int,ExtValue &retval); ///< most universal get, can be used for every datatype |
---|
| 87 | |
---|
| 88 | virtual SString getString(int)=0; ///< get string value, you can only use this for "s" type property |
---|
| 89 | virtual long getInt(int)=0; ///< get long value, you can only use this for "d" type property |
---|
| 90 | virtual double getDouble(int)=0; ///< get double value, you can only use this for "f" type property |
---|
| 91 | virtual ExtObject getObject(int)=0; ///< get object reference, you can only use this for "o" type property |
---|
| 92 | virtual ExtValue getExtValue(int)=0; ///< get extvalue object, you can only use this for "x" type property |
---|
| 93 | |
---|
| 94 | SString get(int); ///< old style get, can convert long or double to string |
---|
| 95 | SString getText(int); ///< like getString, returns enumeration label for subtype "d 0 n ~enum1~enum2... |
---|
| 96 | |
---|
| 97 | SString getStringById(const char*prop); ///< get string value, you can only use this for "s" type property |
---|
| 98 | long getIntById(const char* prop); ///< get long value, you can only use this for "d" type property |
---|
| 99 | double getDoubleById(const char* prop);///< get double value, you can only use this for "f" type property |
---|
| 100 | ExtObject getObjectById(const char* prop);///< get object reference, you can only use this for "o" type property |
---|
| 101 | ExtValue getExtValueById(const char* prop);///< get extvalue object, you can only use this for "x" type property |
---|
| 102 | ExtValue getById(const char* prop); |
---|
| 103 | |
---|
| 104 | virtual int setInt(int,long)=0; ///< set long value, you can only use this for "d" type prop |
---|
| 105 | virtual int setDouble(int,double)=0; ///< set double value, you can only use this for "f" type prop |
---|
| 106 | virtual int setString(int,const SString &)=0; ///< set string value, you can only use this for "s" type prop |
---|
| 107 | virtual int setObject(int,const ExtObject &)=0; ///< set object reference, you can only use this for "o" type prop |
---|
| 108 | virtual int setExtValue(int,const ExtValue &)=0; ///< 4 in 1 |
---|
| 109 | |
---|
| 110 | int set(int,const ExtValue &);///< most universal set, can be used for every datatype |
---|
| 111 | |
---|
| 112 | int set(int,const char*); ///< oldstyle set, can convert string to long or double |
---|
| 113 | |
---|
| 114 | int setIntById(const char* prop,long);///< set long value, you can only use this for "d" type prop |
---|
| 115 | int setDoubleById(const char* prop,double);///< set double value, you can only use this for "f" type prop |
---|
| 116 | int setStringById(const char* prop,const SString &);///< set string value, you can only use this for "s" type prop |
---|
| 117 | int setObjectById(const char* prop,const ExtObject &);///< set object reference, you can only use this for "o" type prop |
---|
| 118 | |
---|
| 119 | int setExtValueById(const char* prop,const ExtValue &); ///< 4 in 1 |
---|
| 120 | |
---|
| 121 | /** get valid minimum, maximum and default value for property 'prop' |
---|
| 122 | @return 0 if min/max/def information is not available */ |
---|
| 123 | int getMinMax(int prop,long& minumum,long& maximum,long& def); |
---|
| 124 | /** get valid minimum, maximum and default value for property 'prop' |
---|
| 125 | @return 0 if min/max/def information is not available */ |
---|
| 126 | int getMinMax(int prop,double& minumum,double& maximum,double& def); |
---|
| 127 | |
---|
| 128 | virtual void setDefault(bool numericonly=false); |
---|
| 129 | virtual void setDefault(int i,bool numericonly=false); |
---|
| 130 | void setMin(); |
---|
| 131 | void setMax(); |
---|
| 132 | void setMin(int i); |
---|
| 133 | void setMax(int i); |
---|
| 134 | |
---|
| 135 | /** copy all property values from other ParamInterface object */ |
---|
| 136 | void copyFrom(ParamInterface *src); |
---|
| 137 | |
---|
| 138 | /** copy all property values from compatible ParamInterface object. |
---|
| 139 | this method is more efficient than copyFrom, |
---|
| 140 | but can be used only if the other object has the same properties sequence, eg: |
---|
| 141 | - any two Param objects having common paramtab |
---|
| 142 | - any ParamInterface object and the Param with paramtab constructed by ParamObject::makeParamTab |
---|
| 143 | */ |
---|
| 144 | void quickCopyFrom(ParamInterface *src); |
---|
| 145 | |
---|
| 146 | int save(VirtFILE*,const SString* altname=0,bool force=0); |
---|
| 147 | int saveprop(VirtFILE*,int i,const char* p,bool force=0); |
---|
| 148 | void load(VirtFILE*); |
---|
| 149 | void load2(const SString &,int &); |
---|
| 150 | }; |
---|
| 151 | |
---|
| 152 | // implementations: |
---|
| 153 | |
---|
| 154 | extern char MakeCodeGuardHappy; |
---|
| 155 | |
---|
| 156 | #define PROCOFFSET(_proc_) ( (void (*)(void*,ExtValue*,ExtValue*)) &(FIELDSTRUCT :: _proc_ ## _statrick)) |
---|
| 157 | #define STATICPROCOFFSET(_proc_) ( (void (*)(void*,ExtValue*,ExtValue*)) &(FIELDSTRUCT :: _proc_)) |
---|
| 158 | #define GETOFFSET(_proc_) ( (void (*)(void*,ExtValue*)) &(FIELDSTRUCT :: _proc_ ## _statrick)) |
---|
| 159 | #define SETOFFSET(_proc_) ( (int (*)(void*,const ExtValue*)) &(FIELDSTRUCT :: _proc_ ## _statrick)) |
---|
| 160 | |
---|
| 161 | #define FIELDOFFSET(_fld_) ((long)((char*)(&((FIELDSTRUCT*)&MakeCodeGuardHappy)->_fld_)-((char*)((FIELDSTRUCT*)&MakeCodeGuardHappy)))) |
---|
| 162 | |
---|
| 163 | #define FIELD(_fld_) FIELDOFFSET(_fld_),0,0 |
---|
| 164 | #define LONGOFFSET(_o_) (_o_),0,0 |
---|
| 165 | #define PROCEDURE(_proc_) 0,(void*)PROCOFFSET(_proc_),0 |
---|
| 166 | #define STATICPROCEDURE(_proc_) 0,(void*)STATICPROCOFFSET(_proc_),0 |
---|
| 167 | #define GETSET(_proc_) 0,(void*)GETOFFSET(get_ ## _proc_),(void*)SETOFFSET(set_ ## _proc_) |
---|
| 168 | #define GETFIELD(_proc_) FIELDOFFSET(_proc_),(void*)GETOFFSET(get_ ## _proc_),0 |
---|
| 169 | #define SETFIELD(_proc_) FIELDOFFSET(_proc_),0,(void*)SETOFFSET(set_ ## _proc_) |
---|
| 170 | #define GETONLY(_proc_) 0,(void*)GETOFFSET(get_ ## _proc_),0 |
---|
| 171 | #define SETONLY(_proc_) 0,0,(void*)SETOFFSET(set_ ## _proc_) |
---|
| 172 | |
---|
| 173 | #define PARAMPROCARGS ExtValue* args,ExtValue* ret |
---|
| 174 | #define PARAMSETARGS const ExtValue* arg |
---|
| 175 | #define PARAMGETARGS ExtValue* ret |
---|
| 176 | |
---|
| 177 | #define PARAMPROCDEF(name) STATRICKDEF2(name,ExtValue*,ExtValue*) |
---|
| 178 | #define PARAMGETDEF(name) STATRICKDEF1(get_ ## name,ExtValue*) |
---|
| 179 | #define PARAMSETDEF(name) STATRICKRDEF1(int,set_ ## name,const ExtValue*) |
---|
| 180 | |
---|
| 181 | /////////////////////////////// |
---|
| 182 | |
---|
| 183 | struct ParamEntry |
---|
| 184 | { |
---|
| 185 | const char *id; |
---|
| 186 | short group,flags; |
---|
| 187 | const char *name,*type; |
---|
| 188 | long offset; |
---|
| 189 | void *fun1; ///< procedure or get |
---|
| 190 | void *fun2; ///< set |
---|
| 191 | const char *help; |
---|
| 192 | }; |
---|
| 193 | |
---|
| 194 | struct ParamEntryConstructor: public ParamEntry |
---|
| 195 | { |
---|
| 196 | public: |
---|
| 197 | ParamEntryConstructor(const char *_id,short _group=0,short _flags=0,const char *_name=0,const char *_type=0,long _offset=0,void *_fun1=0, void *_fun2=0, const char *_help=0) |
---|
| 198 | {id=_id;group=_group;flags=_flags;name=_name;type=_type;offset=_offset;fun1=_fun1;fun2=_fun2;help=_help;} |
---|
| 199 | }; |
---|
| 200 | |
---|
| 201 | class SimpleAbstractParam: public virtual ParamInterface |
---|
| 202 | { |
---|
| 203 | protected: |
---|
| 204 | virtual void *getTarget(int i); |
---|
| 205 | virtual ParamEntry *entry(int i)=0; |
---|
| 206 | const char* myname; |
---|
| 207 | bool dontcheckchanges; |
---|
| 208 | |
---|
| 209 | public: |
---|
| 210 | void *object; |
---|
| 211 | |
---|
| 212 | const char* getName() {return myname;} |
---|
| 213 | void setName(const char* n) {myname=n;} |
---|
| 214 | |
---|
| 215 | /** |
---|
| 216 | @param t ParamEntry table |
---|
| 217 | @param o controlled object |
---|
| 218 | @param n Param's name |
---|
| 219 | */ |
---|
| 220 | SimpleAbstractParam(void* o=0,const char*n=0):object(o),myname(n),dontcheckchanges(0) {} |
---|
| 221 | |
---|
| 222 | void select(void *o) {object=o;} |
---|
| 223 | void* getSelected() {return object;} |
---|
| 224 | |
---|
| 225 | const char *id(int i) {return (i>=getPropCount())?0:entry(i)->id;} |
---|
| 226 | const char *name(int i) {return entry(i)->name;} |
---|
| 227 | const char *type(int i) {return entry(i)->type;} |
---|
| 228 | const char *help(int i) {return entry(i)->help;} |
---|
| 229 | int flags(int i) {return entry(i)->flags;} |
---|
| 230 | int group(int i) {return entry(i)->group;} |
---|
| 231 | void call(int i,ExtValue* args,ExtValue *ret); |
---|
| 232 | |
---|
| 233 | SString getString(int); |
---|
| 234 | long getInt(int); |
---|
| 235 | double getDouble(int); |
---|
| 236 | ExtObject getObject(int); |
---|
| 237 | ExtValue getExtValue(int); |
---|
| 238 | |
---|
| 239 | int setInt(int,long); |
---|
| 240 | int setDouble(int,double); |
---|
| 241 | int setString(int,const SString &); |
---|
| 242 | int setObject(int,const ExtObject &); |
---|
| 243 | int setExtValue(int,const ExtValue &); |
---|
| 244 | |
---|
| 245 | int isequal(int i,void* defdata); |
---|
| 246 | void save2(SString&,void *defdata,int addcr=1); |
---|
| 247 | |
---|
| 248 | virtual void setDefault(bool numericonly=false); |
---|
| 249 | virtual void setDefault(int i,bool numericonly=false); |
---|
| 250 | }; |
---|
| 251 | |
---|
| 252 | class Param: public SimpleAbstractParam |
---|
| 253 | { |
---|
| 254 | protected: |
---|
| 255 | ParamEntry *entry(int i) {return tab+tab[0].group+i;} |
---|
| 256 | public: |
---|
| 257 | ParamEntry *tab; |
---|
| 258 | /** |
---|
| 259 | @param t ParamEntry table |
---|
| 260 | @param o controlled object |
---|
| 261 | @param n Param's name |
---|
| 262 | */ |
---|
| 263 | |
---|
| 264 | Param(ParamEntry *t=0,void* o=0,const char*n=0):SimpleAbstractParam(o,n),tab(t) |
---|
| 265 | {if (!n&&tab) myname=tab[0].name;} |
---|
| 266 | |
---|
| 267 | const char* getDescription() {return tab[0].type;} |
---|
| 268 | |
---|
| 269 | int getGroupCount() {return tab[0].group;} |
---|
| 270 | int getPropCount() {return tab[0].flags;} |
---|
| 271 | const char *grname(int i) {return (i<getGroupCount())?tab[i].id:0;} |
---|
| 272 | int grmember(int,int); |
---|
| 273 | void setParamTab(ParamEntry *t,int dontupdatename=0) {tab=t; if ((!dontupdatename)&&tab) myname=tab[0].name; } |
---|
| 274 | ParamEntry *getParamTab() const {return tab;} |
---|
| 275 | }; |
---|
| 276 | |
---|
| 277 | extern ParamEntry empty_paramtab[]; |
---|
| 278 | |
---|
| 279 | #endif |
---|