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