ui_node_radiobutton.c

Go to the documentation of this file.
00001 
00017 /*
00018 Copyright (C) 2002-2010 UFO: Alien Invasion.
00019 
00020 This program is free software; you can redistribute it and/or
00021 modify it under the terms of the GNU General Public License
00022 as published by the Free Software Foundation; either version 2
00023 of the License, or (at your option) any later version.
00024 
00025 This program is distributed in the hope that it will be useful,
00026 but WITHOUT ANY WARRANTY; without even the implied warranty of
00027 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00028 
00029 See the GNU General Public License for more details.
00030 
00031 You should have received a copy of the GNU General Public License
00032 along with this program; if not, write to the Free Software
00033 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00034 
00035 */
00036 
00037 #include "../ui_main.h"
00038 #include "../ui_actions.h"
00039 #include "../ui_icon.h"
00040 #include "../ui_parse.h"
00041 #include "../ui_input.h"
00042 #include "../ui_render.h"
00043 #include "ui_node_radiobutton.h"
00044 #include "ui_node_abstractnode.h"
00045 
00046 #define EXTRADATA_TYPE radioButtonExtraData_t
00047 #define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
00048 #define EXTRADATACONST(node) UI_EXTRADATACONST(node, EXTRADATA_TYPE)
00049 
00050 #define EPSILON 0.001f
00051 
00053 #define UI_4STATUS_TEX_HEIGHT 64
00054 
00059 static void UI_RadioButtonNodeDraw (uiNode_t *node)
00060 {
00061     vec2_t pos;
00062     uiIconStatus_t iconStatus;
00063     const float current = UI_GetReferenceFloat(node, EXTRADATA(node).cvar);
00064     const qboolean disabled = node->disabled || node->parent->disabled;
00065     int texY;
00066     const char *image;
00067 
00068     if (disabled) {
00069         iconStatus = ICON_STATUS_DISABLED;
00070         texY = UI_4STATUS_TEX_HEIGHT * 2;
00071     } else if (current > EXTRADATA(node).value - EPSILON && current < EXTRADATA(node).value + EPSILON) {
00072         iconStatus = ICON_STATUS_CLICKED;
00073         texY = UI_4STATUS_TEX_HEIGHT * 3;
00074     } else if (node->state) {
00075         iconStatus = ICON_STATUS_HOVER;
00076         texY = UI_4STATUS_TEX_HEIGHT;
00077     } else {
00078         iconStatus = ICON_STATUS_NORMAL;
00079         texY = 0;
00080     }
00081 
00082     UI_GetNodeAbsPos(node, pos);
00083 
00084     image = UI_GetReferenceString(node, node->image);
00085     if (image) {
00086         const int texX = 0;
00087         UI_DrawNormImageByName(pos[0], pos[1], node->size[0], node->size[1],
00088             texX + node->size[0], texY + node->size[1], texX, texY, image);
00089     }
00090 
00091     if (EXTRADATA(node).icon) {
00092         UI_DrawIconInBox(EXTRADATA(node).icon, iconStatus, pos[0], pos[1], node->size[0], node->size[1]);
00093     }
00094 }
00095 
00099 static void UI_RadioButtonNodeActivate (uiNode_t * node)
00100 {
00101     float current;
00102 
00103     /* no cvar given? */
00104     if (!EXTRADATA(node).cvar || !*(char*)(EXTRADATA(node).cvar)) {
00105         Com_Printf("UI_RadioButtonNodeClick: node '%s' doesn't have a valid cvar assigned\n", UI_GetPath(node));
00106         return;
00107     }
00108 
00109     /* its not a cvar! */
00111     if (strncmp((const char *)(EXTRADATA(node).cvar), "*cvar", 5))
00112         return;
00113 
00114     current = UI_GetReferenceFloat(node, EXTRADATA(node).cvar);
00115     /* Is we click on the action button, we can continue */
00116     if (current > EXTRADATA(node).value - EPSILON && current < EXTRADATA(node).value + EPSILON)
00117         return;
00118 
00119     {
00120         const char *cvarName = &((const char *)(EXTRADATA(node).cvar))[6];
00121         UI_SetCvar(cvarName, NULL, EXTRADATA(node).value);
00122         if (node->onChange)
00123             UI_ExecuteEventActions(node, node->onChange);
00124     }
00125 }
00126 
00130 static void UI_RadioButtonNodeClick (uiNode_t * node, int x, int y)
00131 {
00132     if (node->onClick)
00133         UI_ExecuteEventActions(node, node->onClick);
00134 
00135     UI_RadioButtonNodeActivate(node);
00136 }
00137 
00138 static const value_t properties[] = {
00139     /* Value defining the radiobutton. Cvar is updated with this value when the radio button is selected. */
00140     {"value", V_FLOAT, UI_EXTRADATA_OFFSETOF(EXTRADATA_TYPE, value), MEMBER_SIZEOF(EXTRADATA_TYPE, value)},
00141     /* Cvar name shared with the radio button group to identify when a radio button is selected. */
00142     {"cvar", V_UI_CVAR, UI_EXTRADATA_OFFSETOF(EXTRADATA_TYPE, cvar), 0},
00143     /* Icon used to display the node */
00144     {"icon", V_UI_ICONREF, UI_EXTRADATA_OFFSETOF(EXTRADATA_TYPE, icon), MEMBER_SIZEOF(EXTRADATA_TYPE, icon)},
00145 
00146     {NULL, V_NULL, 0, 0}
00147 };
00148 
00149 void UI_RegisterRadioButtonNode (uiBehaviour_t *behaviour)
00150 {
00151     behaviour->name = "radiobutton";
00152     behaviour->draw = UI_RadioButtonNodeDraw;
00153     behaviour->leftClick = UI_RadioButtonNodeClick;
00154     behaviour->activate = UI_RadioButtonNodeActivate;
00155     behaviour->properties = properties;
00156     behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
00157 }

Generated by  doxygen 1.6.2