ui_node_abstractoption.c

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2002-2010 UFO: Alien Invasion.
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018 See the GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 
00024 */
00025 
00026 #include "../ui_main.h"
00027 #include "../ui_internal.h"
00028 #include "../ui_parse.h"
00029 #include "../ui_draw.h"
00030 #include "../ui_data.h"
00031 #include "ui_node_abstractoption.h"
00032 #include "ui_node_abstractnode.h"
00033 
00034 #define EXTRADATA_TYPE abstractOptionExtraData_t
00035 #define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
00036 
00037 const uiBehaviour_t *abstractOptionBehaviour;
00038 
00042 void UI_OptionNodeSortOptions (uiNode_t *node)
00043 {
00044     uiNode_t *option;
00045     assert(UI_NodeInstanceOf(node, "abstractoption"));
00046     UI_SortOptions(&node->firstChild);
00047 
00050     option = node->firstChild;
00051     while (option->next)
00052         option = option->next;
00053     node->lastChild = option;
00054 }
00055 
00056 const char *UI_AbstractOptionGetCurrentValue (uiNode_t * node)
00057 {
00058     /* no cvar given? */
00059     if (!(EXTRADATA(node).cvar) || !*(char*)(EXTRADATA(node).cvar)) {
00060         Com_Printf("UI_AbstractOptionGetCurrentValue: node '%s' doesn't have a valid cvar assigned\n", UI_GetPath(node));
00061         return NULL;
00062     }
00063 
00064     /* not a cvar? */
00065     if (strncmp((const char *)(EXTRADATA(node).cvar), "*cvar", 5))
00066         return NULL;
00067 
00068     return UI_GetReferenceString(node, (EXTRADATA(node).cvar));
00069 }
00070 
00071 void UI_AbstractOptionSetCurrentValue(uiNode_t * node, const char *value)
00072 {
00073     const char *cvarName = &((const char *)(EXTRADATA(node).cvar))[6];
00074     UI_SetCvar(cvarName, value, 0);
00075     if (node->onChange)
00076         UI_ExecuteEventActions(node, node->onChange);
00077 }
00078 
00079 static const value_t properties[] = {
00081     {"dataid", V_UI_DATAID, UI_EXTRADATA_OFFSETOF(EXTRADATA_TYPE, dataId), MEMBER_SIZEOF(EXTRADATA_TYPE, dataId)},
00083     {"lineheight", V_INT, UI_EXTRADATA_OFFSETOF(EXTRADATA_TYPE, lineHeight),  MEMBER_SIZEOF(EXTRADATA_TYPE, lineHeight)},
00084 
00085     /* position of the vertical view (into the full number of elements the node contain) */
00086     {"viewpos", V_INT, UI_EXTRADATA_OFFSETOF(EXTRADATA_TYPE, scrollY.viewPos),  MEMBER_SIZEOF(EXTRADATA_TYPE, scrollY.viewPos)},
00087     /* size of the vertical view (proportional to the number of elements the node can display without moving) */
00088     {"viewsize", V_INT, UI_EXTRADATA_OFFSETOF(EXTRADATA_TYPE, scrollY.viewSize),  MEMBER_SIZEOF(EXTRADATA_TYPE, scrollY.viewSize)},
00089     /* full vertical size (proportional to the number of elements the node contain) */
00090     {"fullsize", V_INT, UI_EXTRADATA_OFFSETOF(EXTRADATA_TYPE, scrollY.fullSize),  MEMBER_SIZEOF(EXTRADATA_TYPE, scrollY.fullSize)},
00091 
00092     /* number of elements contain the node */
00093     {"count", V_INT, UI_EXTRADATA_OFFSETOF(EXTRADATA_TYPE, count),  MEMBER_SIZEOF(EXTRADATA_TYPE, count)},
00094 
00095     /* Define the cvar containing the value of the current selected option */
00096     {"cvar", V_UI_CVAR, UI_EXTRADATA_OFFSETOF(EXTRADATA_TYPE, cvar), 0},
00097 
00098     /* Called when one of the properties viewpos/viewsize/fullsize change */
00099     {"onviewchange", V_UI_ACTION, UI_EXTRADATA_OFFSETOF(EXTRADATA_TYPE, onViewChange), MEMBER_SIZEOF(EXTRADATA_TYPE, onViewChange)},
00100 
00101     {NULL, V_NULL, 0, 0}
00102 };
00103 
00104 static void UI_AbstractOptionDoLayout (uiNode_t *node) {
00105     uiNode_t *option = node->firstChild;
00106 
00107     if (EXTRADATA(node).dataId == 0) {
00108         int count = 0;
00109         while(option && option->behaviour == ui_optionBehaviour) {
00110             UI_Validate(option);
00111             if (!option->invis)
00112                 count++;
00113             option = option->next;
00114         }
00115 
00116         EXTRADATA(node).count = count;
00117     }
00118 
00119     node->invalidated = qfalse;
00120 }
00121 
00126 uiNode_t*  UI_AbstractOptionGetFirstOption (uiNode_t * node)
00127 {
00128     if (node->firstChild && node->firstChild->behaviour == ui_optionBehaviour) {
00129         return node->firstChild;
00130     } else {
00131         const int v = UI_GetDataVersion(EXTRADATA(node).dataId);
00132         if (v != EXTRADATA(node).dataId) {
00133             int count = 0;
00134             uiNode_t *option = UI_GetOption(EXTRADATA(node).dataId);
00135             while (option) {
00136                 if (option->invis == qfalse)
00137                     count++;
00138                 option = option->next;
00139             }
00140             EXTRADATA(node).count = count;
00141             EXTRADATA(node).versionId = v;
00142         }
00143         return UI_GetOption(EXTRADATA(node).dataId);
00144     }
00145 }
00146 
00147 void UI_RegisterAbstractOptionNode (uiBehaviour_t *behaviour)
00148 {
00149     behaviour->name = "abstractoption";
00150     behaviour->isAbstract = qtrue;
00151     behaviour->properties = properties;
00152     behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
00153     behaviour->drawItselfChild = qtrue;
00154     behaviour->doLayout = UI_AbstractOptionDoLayout;
00155     abstractOptionBehaviour = behaviour;
00156 }

Generated by  doxygen 1.6.2