ui_node_bar.c

Go to the documentation of this file.
00001 
00008 /*
00009 Copyright (C) 2002-2010 UFO: Alien Invasion.
00010 
00011 This program is free software; you can redistribute it and/or
00012 modify it under the terms of the GNU General Public License
00013 as published by the Free Software Foundation; either version 2
00014 of the License, or (at your option) any later version.
00015 
00016 This program is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00019 
00020 See the GNU General Public License for more details.
00021 
00022 You should have received a copy of the GNU General Public License
00023 along with this program; if not, write to the Free Software
00024 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00025 
00026 */
00027 
00028 #include "../ui_nodes.h"
00029 #include "../ui_parse.h"
00030 #include "../ui_main.h"
00031 #include "../ui_input.h"
00032 #include "../ui_render.h"
00033 #include "../ui_actions.h"
00034 #include "ui_node_bar.h"
00035 #include "ui_node_abstractvalue.h"
00036 #include "ui_node_abstractnode.h"
00037 
00038 #include "../../input/cl_keys.h"
00039 
00040 #define EXTRADATA_TYPE barExtraData_t
00041 #define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
00042 
00043 static void UI_BarNodeDraw (uiNode_t *node)
00044 {
00045     vec4_t color;
00046     float fac;
00047     vec2_t nodepos;
00048     const float min = UI_GetReferenceFloat(node, EXTRADATA(node).super.min);
00049     const float max = UI_GetReferenceFloat(node, EXTRADATA(node).super.max);
00050     const float value = UI_GetReferenceFloat(node, EXTRADATA(node).super.value);
00051 
00052     UI_GetNodeAbsPos(node, nodepos);
00053 
00054     if (node->state && !EXTRADATA(node).readOnly) {
00055         Vector4Copy(node->color, color);
00056     } else {
00057         VectorScale(node->color, 0.8, color);
00058         color[3] = node->color[3];
00059     }
00060 
00061     /* shoud it return an error? */
00062     if (max > min)
00063         fac = (value - min) / (max - min);
00064     else
00065         fac = 1;
00066     if (fac <= 0 || fac > 1)
00067         return;
00068 
00069     switch (EXTRADATA(node).orientation) {
00070     case ALIGN_UC:
00071         UI_DrawFill(nodepos[0], nodepos[1] + node->size[1] - fac * node->size[1], node->size[0], fac * node->size[1], color);
00072         break;
00073     case ALIGN_LC:
00074         UI_DrawFill(nodepos[0], nodepos[1], node->size[0], fac * node->size[1], color);
00075         break;
00076     case ALIGN_CR:
00077         UI_DrawFill(nodepos[0], nodepos[1], fac * node->size[0], node->size[1], color);
00078         break;
00079     case ALIGN_CL:
00080         UI_DrawFill(nodepos[0] + node->size[0] - fac * node->size[0], nodepos[1], fac * node->size[0], node->size[1], color);
00081         break;
00082     default:
00083         Com_Printf("UI_BarNodeDraw: Orientation %d not supported\n", EXTRADATA(node).orientation);
00084     }
00085 }
00086 
00090 static void UI_BarNodeCapturedMouseMove (uiNode_t *node, int x, int y)
00091 {
00092     char var[MAX_VAR];
00093     vec2_t pos;
00094 
00095     UI_NodeAbsoluteToRelativePos(node, &x, &y);
00096 
00097     if (x < 0)
00098         x = 0;
00099     else if (x > node->size[0])
00100         x = node->size[0];
00101     if (y < 0)
00102         y = 0;
00103     else if (y > node->size[1])
00104         y = node->size[1];
00105 
00106     UI_GetNodeAbsPos(node, pos);
00107     Q_strncpyz(var, EXTRADATA(node).super.value, sizeof(var));
00108     /* no cvar? */
00109     if (!strncmp(var, "*cvar:", 6)) {
00110         /* normalize it */
00111         float frac;
00112         const float min = UI_GetReferenceFloat(node, EXTRADATA(node).super.min);
00113         const float max = UI_GetReferenceFloat(node, EXTRADATA(node).super.max);
00114 
00115         switch (EXTRADATA(node).orientation) {
00116         case ALIGN_UC:
00117             frac = (node->size[1] - (float) y) / node->size[1];
00118             break;
00119         case ALIGN_LC:
00120             frac = (float) y / node->size[1];
00121             break;
00122         case ALIGN_CR:
00123             frac = (float) x / node->size[0];
00124             break;
00125         case ALIGN_CL:
00126             frac = (node->size[0] - (float) x) / node->size[0];
00127             break;
00128         default:
00129             frac = 0;
00130             Com_Printf("UI_BarNodeCapturedMouseMove: Orientation %d not supported\n", EXTRADATA(node).orientation);
00131         }
00132         UI_SetCvar(&var[6], NULL, min + frac * (max - min));
00133     }
00134 
00135     /* callback */
00136     if (node->onChange)
00137         UI_ExecuteEventActions(node, node->onChange);
00138 }
00139 
00140 static void UI_BarNodeMouseDown (uiNode_t *node, int x, int y, int button)
00141 {
00142     if (node->disabled || EXTRADATA(node).readOnly)
00143         return;
00144 
00145     if (button == K_MOUSE1) {
00146         UI_SetMouseCapture(node);
00147         UI_BarNodeCapturedMouseMove(node, x, y);
00148     }
00149 }
00150 
00151 static void UI_BarNodeMouseUp (uiNode_t *node, int x, int y, int button)
00152 {
00153     if (button == K_MOUSE1)
00154         UI_MouseRelease();
00155 }
00156 
00160 static void UI_BarNodeLoading (uiNode_t *node)
00161 {
00162     Vector4Set(node->color, 1, 1, 1, 1);
00163     EXTRADATA(node).orientation = ALIGN_CR;
00164 }
00165 
00169 static const value_t properties[] = {
00173     {"direction", V_UI_ALIGN, UI_EXTRADATA_OFFSETOF(barExtraData_t, orientation), MEMBER_SIZEOF(barExtraData_t, orientation)},
00177     {"readonly", V_BOOL, UI_EXTRADATA_OFFSETOF(barExtraData_t, readOnly),  MEMBER_SIZEOF(barExtraData_t, readOnly)},
00178 
00179     {NULL, V_NULL, 0, 0}
00180 };
00181 
00182 void UI_RegisterBarNode (uiBehaviour_t *behaviour)
00183 {
00184     behaviour->name = "bar";
00185     behaviour->extends = "abstractvalue";
00186     behaviour->properties = properties;
00187     behaviour->draw = UI_BarNodeDraw;
00188     behaviour->loading = UI_BarNodeLoading;
00189     behaviour->mouseDown = UI_BarNodeMouseDown;
00190     behaviour->mouseUp = UI_BarNodeMouseUp;
00191     behaviour->capturedMouseMove = UI_BarNodeCapturedMouseMove;
00192     behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
00193 }

Generated by  doxygen 1.6.2