ui_node_keybinding.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_draw.h"
00028 #include "../ui_nodes.h"
00029 #include "../ui_font.h"
00030 #include "../ui_parse.h"
00031 #include "../ui_input.h"
00032 #include "../ui_actions.h"
00033 #include "../ui_render.h"
00034 #include "ui_node_keybinding.h"
00035 #include "ui_node_abstractnode.h"
00036 #include "ui_node_panel.h"
00037 
00038 #include "../../client.h"
00039 
00040 #define TILE_SIZE 64
00041 #define CORNER_SIZE 17
00042 #define MID_SIZE 1
00043 #define MARGE 3
00044 
00045 #define EXTRADATA_TYPE keyBindingExtraData_t
00046 #define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
00047 
00051 static void UI_KeyBindingNodeClick (uiNode_t *node, int x, int y)
00052 {
00053     if (node->disabled)
00054         return;
00055 
00056     /* no binding given */
00057     if (!node->text)
00058         return;
00059 
00060     if (strncmp(node->text, "*binding", 8))
00061         return;
00062 
00063     if (!UI_HasFocus(node)) {
00064         if (node->onClick)
00065             UI_ExecuteEventActions(node, node->onClick);
00066         UI_RequestFocus(node);
00067     }
00068 }
00069 
00074 static qboolean UI_KeyBindingNodeKeyPressed (uiNode_t *node, unsigned int key, unsigned short unicode)
00075 {
00076     const char *command;
00077     const char *binding;
00078 
00079     UI_RemoveFocus();
00080 
00082     if (strncmp(node->text, "*binding:", 9))
00083         return qfalse;
00084 
00085     command = node->text + 9;
00086 
00089     binding = Key_GetBinding(command, EXTRADATA(node).keySpace);
00090     if (binding[0] != '\0') {
00091         /* if it's the same command, do not change anything, otherwise
00092          * show the reason why nothing was changed */
00093         if (strcmp(binding, command)) {
00094             const char *keyStr = Key_KeynumToString(key);
00095             UI_DisplayNotice(va(_("Key %s already bound"), keyStr), 2000, NULL);
00096         }
00097         return qfalse;
00098     }
00099 
00100     /* fire change event */
00101     if (node->onChange)
00102         UI_ExecuteEventActions(node, node->onChange);
00103 
00104     Key_SetBinding(key, command, EXTRADATA(node).keySpace);
00105 
00106     return qtrue;
00107 }
00108 
00109 static void UI_KeyBindingNodeDraw (uiNode_t *node)
00110 {
00111     static const int panelTemplate[] = {
00112         CORNER_SIZE, MID_SIZE, CORNER_SIZE,
00113         CORNER_SIZE, MID_SIZE, CORNER_SIZE,
00114         MARGE
00115     };
00116     const char *binding, *description, *command;
00117     int texX, texY;
00118     const float *textColor;
00119     const char *image;
00120     vec2_t pos;
00121     const char *font = UI_GetFontFromNode(node);
00122     const int bindingWidth = EXTRADATA(node).bindingWidth;
00123     const int descriptionWidth = node->size[0] - bindingWidth;
00124     vec2_t descriptionPos, descriptionSize;
00125     vec2_t bindingPos, bindingSize;
00126 
00127     if (node->state) {
00128         textColor = node->color;
00129         texX = TILE_SIZE;
00130         texY = 0;
00131     } else {
00132         textColor = node->color;
00133         texX = 0;
00134         texY = 0;
00135     }
00136     if (UI_HasFocus(node))
00137         textColor = node->selectedColor;
00138 
00139     UI_GetNodeAbsPos(node, pos);
00140 
00141     Vector2Set(descriptionSize, descriptionWidth, node->size[1]);
00142     Vector2Set(bindingSize, bindingWidth, node->size[1]);
00143     Vector2Set(descriptionPos, pos[0], pos[1]);
00144     Vector2Set(bindingPos, pos[0] + descriptionWidth + node->padding, pos[1]);
00145 
00146     image = UI_GetReferenceString(node, node->image);
00147     if (image) {
00148         UI_DrawPanel(descriptionPos, descriptionSize, image, texX, texY, panelTemplate);
00149         UI_DrawPanel(bindingPos, bindingSize, image, texX, texY, panelTemplate);
00150     }
00151 
00152     binding = UI_GetReferenceString(node, node->text);
00153     if (binding == NULL || binding[0] == '\0')
00154         binding = _("NONE");
00155 
00157     command = node->text + 9;
00158     description = Cmd_GetCommandDesc(command);
00159     if (description[0] == '\0')
00160         description = command;
00161 
00162     R_Color(textColor);
00163 
00164     UI_DrawStringInBox(font, node->textalign,
00165         descriptionPos[0] + node->padding, descriptionPos[1] + node->padding,
00166         descriptionSize[0] - node->padding - node->padding, descriptionSize[1] - node->padding - node->padding,
00167         description, LONGLINES_PRETTYCHOP);
00168 
00169     UI_DrawStringInBox(font, node->textalign,
00170         bindingPos[0] + node->padding, bindingPos[1] + node->padding,
00171         bindingSize[0] - node->padding - node->padding, bindingSize[1] - node->padding - node->padding,
00172         binding, LONGLINES_PRETTYCHOP);
00173 
00174     R_Color(NULL);
00175 }
00176 
00180 static void UI_KeyBindingNodeLoading (uiNode_t *node)
00181 {
00182     node->padding = 8;
00183     node->textalign = ALIGN_CL;
00184     EXTRADATA(node).bindingWidth = 50;
00185     Vector4Set(node->color, 1, 1, 1, 1);
00186     Vector4Set(node->selectedColor, 1, 1, 1, 0.5);
00187 }
00188 
00189 static const value_t properties[] = {
00190     {"keyspace", V_INT, UI_EXTRADATA_OFFSETOF(keyBindingExtraData_t, keySpace), MEMBER_SIZEOF(keyBindingExtraData_t, keySpace)},
00191     {"bindingwidth", V_INT, UI_EXTRADATA_OFFSETOF(keyBindingExtraData_t, bindingWidth), MEMBER_SIZEOF(keyBindingExtraData_t, bindingWidth)},
00192 
00193     {NULL, V_NULL, 0, 0}
00194 };
00195 
00196 void UI_RegisterKeyBindingNode (uiBehaviour_t *behaviour)
00197 {
00198     behaviour->name = "keybinding";
00199     behaviour->leftClick = UI_KeyBindingNodeClick;
00200     behaviour->keyPressed = UI_KeyBindingNodeKeyPressed;
00201     behaviour->draw = UI_KeyBindingNodeDraw;
00202     behaviour->loading = UI_KeyBindingNodeLoading;
00203     behaviour->properties = properties;
00204     behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
00205 }

Generated by  doxygen 1.6.2