ui_node_checkbox.c

Go to the documentation of this file.
00001 
00014 /*
00015 Copyright (C) 2002-2010 UFO: Alien Invasion.
00016 
00017 This program is free software; you can redistribute it and/or
00018 modify it under the terms of the GNU General Public License
00019 as published by the Free Software Foundation; either version 2
00020 of the License, or (at your option) any later version.
00021 
00022 This program is distributed in the hope that it will be useful,
00023 but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00025 
00026 See the GNU General Public License for more details.
00027 
00028 You should have received a copy of the GNU General Public License
00029 along with this program; if not, write to the Free Software
00030 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00031 
00032 */
00033 
00034 #include "../ui_nodes.h"
00035 #include "../ui_parse.h"
00036 #include "../ui_main.h"
00037 #include "../ui_actions.h"
00038 #include "../ui_render.h"
00039 #include "ui_node_checkbox.h"
00040 #include "ui_node_abstractnode.h"
00041 #include "ui_node_abstractvalue.h"
00042 
00043 #define EXTRADATA(node) UI_EXTRADATA(node, abstractValueExtraData_t)
00044 
00045 static void UI_CheckBoxNodeDraw (uiNode_t* node)
00046 {
00047     const float value = UI_GetReferenceFloat(node, EXTRADATA(node).value);
00048     vec2_t pos;
00049     const char *image = UI_GetReferenceString(node, node->image);
00050     int texx, texy;
00051 
00052     /* image set? */
00053     if (!image || image[0] == '\0')
00054         return;
00055 
00056     /* outer status */
00057     if (node->disabled) {
00058         texy = 96;
00059     } else if (node->state) {
00060         texy = 32;
00061     } else {
00062         texy = 0;
00063     }
00064 
00065     /* inner status */
00066     if (value == 0) {
00067         texx = 0;
00068     } else if (value > 0) {
00069         texx = 32;
00070     } else { /* value < 0 */
00071         texx = 64;
00072     }
00073 
00074     UI_GetNodeAbsPos(node, pos);
00075     UI_DrawNormImageByName(pos[0], pos[1], node->size[0], node->size[1],
00076         texx + node->size[0], texy + node->size[1], texx, texy, image);
00077 }
00078 
00082 static void UI_CheckBoxNodeActivate (uiNode_t *node)
00083 {
00084     const float last = UI_GetReferenceFloat(node, EXTRADATA(node).value);
00085     float value;
00086 
00087     if (node->disabled)
00088         return;
00089 
00090     /* update value */
00091     value = (last > 0) ? 0 : 1;
00092     if (last == value)
00093         return;
00094 
00095     /* save result */
00096     EXTRADATA(node).lastdiff = value - last;
00097     if (!strncmp(EXTRADATA(node).value, "*cvar:", 6)) {
00098         UI_SetCvar(&((char*)EXTRADATA(node).value)[6], NULL, value);
00099     } else {
00100         *(float*) EXTRADATA(node).value = value;
00101     }
00102 
00103     /* fire change event */
00104     if (node->onChange) {
00105         UI_ExecuteEventActions(node, node->onChange);
00106     }
00107 }
00108 
00109 static void UI_CheckBoxNodeCallActivate (uiNode_t *node, const uiCallContext_t *context)
00110 {
00111     UI_CheckBoxNodeActivate(node);
00112 }
00113 
00117 static void UI_CheckBoxNodeClick (uiNode_t * node, int x, int y)
00118 {
00119     if (node->onClick)
00120         UI_ExecuteEventActions(node, node->onClick);
00121 
00122     UI_CheckBoxNodeActivate(node);
00123 }
00124 
00128 static void UI_CheckBoxNodeLoading (uiNode_t *node)
00129 {
00130 }
00131 
00132 static const value_t properties[] = {
00133     /* @override image
00134      * Texture used for the widget. Its a 128x128 template image with all
00135      * three status according to the value, and four status according to the
00136      * interaction. From left to right: unchecked, checked, and invalidate.
00137      * From top to bottom: normal, hovered by the mouse, clicked, disabled.
00138      * @image html http://ufoai.ninex.info/wiki/images/Checkbox_template.png
00139      */
00140 
00141     /* Call it to toggle the node status. */
00142     {"toggle", V_UI_NODEMETHOD, ((size_t) UI_CheckBoxNodeCallActivate), 0},
00143     {NULL, V_NULL, 0, 0}
00144 };
00145 
00146 void UI_RegisterCheckBoxNode (uiBehaviour_t *behaviour)
00147 {
00148     behaviour->name = "checkbox";
00149     behaviour->extends = "abstractvalue";
00150     behaviour->draw = UI_CheckBoxNodeDraw;
00151     behaviour->leftClick = UI_CheckBoxNodeClick;
00152     behaviour->loading = UI_CheckBoxNodeLoading;
00153     behaviour->activate = UI_CheckBoxNodeActivate;
00154     behaviour->properties = properties;
00155 }

Generated by  doxygen 1.6.2