ui_node_abstractvalue.c

Go to the documentation of this file.
00001 
00007 /*
00008 Copyright (C) 2002-2010 UFO: Alien Invasion.
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00018 
00019 See the GNU General Public License for more details.
00020 
00021 You should have received a copy of the GNU General Public License
00022 along with this program; if not, write to the Free Software
00023 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00024 
00025 */
00026 
00027 #include "../ui_nodes.h"
00028 #include "../ui_parse.h"
00029 #include "../ui_internal.h"
00030 #include "ui_node_abstractvalue.h"
00031 
00032 #define EXTRADATA_TYPE abstractValueExtraData_t
00033 #define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
00034 #define EXTRADATACONST(node) UI_EXTRADATACONST(node, EXTRADATA_TYPE)
00035 
00036 static const uiBehaviour_t const *localBehaviour;
00037 
00038 static inline void UI_InitCvarOrFloat (float** adress, float defaultValue)
00039 {
00040     if (*adress == NULL) {
00041         *adress = UI_AllocStaticFloat(1);
00042         **adress = defaultValue;
00043     }
00044 }
00045 
00046 static void UI_AbstractValueLoaded (uiNode_t * node)
00047 {
00048     UI_InitCvarOrFloat((float**)&EXTRADATA(node).value, 0);
00049     UI_InitCvarOrFloat((float**)&EXTRADATA(node).delta, 1);
00050     UI_InitCvarOrFloat((float**)&EXTRADATA(node).max, 0);
00051     UI_InitCvarOrFloat((float**)&EXTRADATA(node).min, 0);
00052 }
00053 
00054 static void UI_AbstractValueNew (uiNode_t * node)
00055 {
00056     EXTRADATA(node).value = Mem_PoolAlloc(sizeof(float), ui_dynPool, 0);
00057     EXTRADATA(node).delta = Mem_PoolAlloc(sizeof(float), ui_dynPool, 0);
00058     EXTRADATA(node).max = Mem_PoolAlloc(sizeof(float), ui_dynPool, 0);
00059     EXTRADATA(node).min = Mem_PoolAlloc(sizeof(float), ui_dynPool, 0);
00060 }
00061 
00062 static void UI_AbstractValueDelete (uiNode_t * node)
00063 {
00064     Mem_Free(EXTRADATA(node).value);
00065     Mem_Free(EXTRADATA(node).delta);
00066     Mem_Free(EXTRADATA(node).max);
00067     Mem_Free(EXTRADATA(node).min);
00068     EXTRADATA(node).value = NULL;
00069     EXTRADATA(node).delta = NULL;
00070     EXTRADATA(node).max = NULL;
00071     EXTRADATA(node).min = NULL;
00072 }
00073 
00074 static void UI_CloneCvarOrFloat (const uiNode_t *source, uiNode_t *clone, const float*const* sourceData, float** cloneData)
00075 {
00076     /* dont update cvar */
00077     if (!strncmp(*(const char*const*)sourceData, "*cvar", 5)) {
00078         /* thats anyway a const string */
00079         assert(!source->dynamic);
00080         if (clone->dynamic)
00081             Mem_Free(*(char**)cloneData);
00082         *(const char**)cloneData = *(const char*const*)sourceData;
00083     } else {
00084         /* clone float */
00085         if (!clone->dynamic)
00086             *cloneData = UI_AllocStaticFloat(1);
00087         **cloneData = **sourceData;
00088     }
00089 }
00090 
00094 static void UI_AbstractValueClone (const uiNode_t *source, uiNode_t *clone)
00095 {
00096     localBehaviour->super->clone(source, clone);
00097     UI_CloneCvarOrFloat(source, clone, (const float*const*)&EXTRADATACONST(source).value, (float**)&EXTRADATA(clone).value);
00098     UI_CloneCvarOrFloat(source, clone, (const float*const*)&EXTRADATACONST(source).delta, (float**)&EXTRADATA(clone).delta);
00099     UI_CloneCvarOrFloat(source, clone, (const float*const*)&EXTRADATACONST(source).max, (float**)&EXTRADATA(clone).max);
00100     UI_CloneCvarOrFloat(source, clone, (const float*const*)&EXTRADATACONST(source).min, (float**)&EXTRADATA(clone).min);
00101 }
00102 
00103 static const value_t properties[] = {
00104     /* Current value of the node. It should be a cvar */
00105     {"current", V_CVAR_OR_FLOAT, UI_EXTRADATA_OFFSETOF(abstractValueExtraData_t, value), 0},
00106     /* Value of a positive step. Must be bigger than 1. */
00107     {"delta", V_CVAR_OR_FLOAT, UI_EXTRADATA_OFFSETOF(abstractValueExtraData_t, delta), 0},
00108     /* Maximum value we can set to the node. It can be a cvar. Default value is 0. */
00109     {"max", V_CVAR_OR_FLOAT, UI_EXTRADATA_OFFSETOF(abstractValueExtraData_t, max), 0},
00110     /* Minimum value we can set to the node. It can be a cvar. Default value is 1. */
00111     {"min", V_CVAR_OR_FLOAT, UI_EXTRADATA_OFFSETOF(abstractValueExtraData_t, min), 0},
00112 
00113     /* Callback value set when before calling onChange. It is used to know the change apply by the user
00114      * @Deprecated
00115      */
00116     {"lastdiff", V_FLOAT, UI_EXTRADATA_OFFSETOF(abstractValueExtraData_t, lastdiff), MEMBER_SIZEOF(abstractValueExtraData_t, lastdiff)},
00117     {NULL, V_NULL, 0, 0}
00118 };
00119 
00120 void UI_RegisterAbstractValueNode (uiBehaviour_t *behaviour)
00121 {
00122     localBehaviour = behaviour;
00123     behaviour->name = "abstractvalue";
00124     behaviour->loaded = UI_AbstractValueLoaded;
00125     behaviour->clone = UI_AbstractValueClone;
00126     behaviour->new = UI_AbstractValueNew;
00127     behaviour->delete = UI_AbstractValueDelete;
00128     behaviour->isAbstract = qtrue;
00129     behaviour->properties = properties;
00130     behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
00131 }

Generated by  doxygen 1.6.2