ui_node_button.c

Go to the documentation of this file.
00001 
00010 /*
00011 Copyright (C) 2002-2010 UFO: Alien Invasion.
00012 
00013 This program is free software; you can redistribute it and/or
00014 modify it under the terms of the GNU General Public License
00015 as published by the Free Software Foundation; either version 2
00016 of the License, or (at your option) any later version.
00017 
00018 This program is distributed in the hope that it will be useful,
00019 but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00021 
00022 See the GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License
00025 along with this program; if not, write to the Free Software
00026 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 
00028 */
00029 
00030 #include "../ui_main.h"
00031 #include "../ui_actions.h"
00032 #include "../ui_parse.h"
00033 #include "../ui_font.h"
00034 #include "../ui_render.h"
00035 #include "../ui_icon.h"
00036 #include "ui_node_button.h"
00037 #include "ui_node_custombutton.h"
00038 #include "ui_node_abstractnode.h"
00039 #include "ui_node_panel.h"
00040 
00041 #define EXTRADATA_TYPE buttonExtraData_t
00042 #define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
00043 #define EXTRADATACONST(node) UI_EXTRADATACONST(node, EXTRADATA_TYPE)
00044 
00045 #include "../../client.h"
00046 
00047 #define TILE_SIZE 64
00048 #define CORNER_SIZE 17
00049 #define MID_SIZE 1
00050 #define MARGE 3
00051 
00055 static void UI_ButtonNodeClick (uiNode_t * node, int x, int y)
00056 {
00057     if (node->onClick) {
00058         UI_ExecuteEventActions(node, node->onClick);
00059     }
00060 }
00061 
00065 static void UI_ButtonNodeDraw (uiNode_t *node)
00066 {
00067     static const int panelTemplate[] = {
00068         CORNER_SIZE, MID_SIZE, CORNER_SIZE,
00069         CORNER_SIZE, MID_SIZE, CORNER_SIZE,
00070         MARGE
00071     };
00072     const char *text;
00073     int texX, texY;
00074     const float *textColor;
00075     const char *image;
00076     vec2_t pos;
00077     static vec4_t disabledColor = {0.5, 0.5, 0.5, 1.0};
00078     int iconPadding = 0;
00079     uiIconStatus_t iconStatus = ICON_STATUS_NORMAL;
00080     const char *font = UI_GetFontFromNode(node);
00081 
00082     if (!node->onClick || node->disabled) {
00084         textColor = disabledColor;
00085         texX = TILE_SIZE;
00086         texY = TILE_SIZE;
00087         iconStatus = ICON_STATUS_DISABLED;
00088     } else if (node->state) {
00089         textColor = node->selectedColor;
00090         texX = TILE_SIZE;
00091         texY = 0;
00092         iconStatus = ICON_STATUS_HOVER;
00093     } else {
00094         textColor = node->color;
00095         texX = 0;
00096         texY = 0;
00097     }
00098 
00099     UI_GetNodeAbsPos(node, pos);
00100 
00101     image = UI_GetReferenceString(node, node->image);
00102     if (image)
00103         UI_DrawPanel(pos, node->size, image, texX, texY, panelTemplate);
00104 
00105     /* display the icon at the left */
00107     if (EXTRADATA(node).icon) {
00108         /* use at least a box size equals to button height */
00109         int size = node->size[1] - node->padding - node->padding;
00110         if (size < EXTRADATA(node).icon->size[0])
00111             size = EXTRADATA(node).icon->size[0];
00112         UI_DrawIconInBox(EXTRADATA(node).icon, iconStatus, pos[0] + node->padding, pos[1] + node->padding, size, node->size[1] - node->padding - node->padding);
00113         iconPadding = size + node->padding;
00114     }
00115 
00116     text = UI_GetReferenceString(node, node->text);
00117     if (text != NULL && *text != '\0') {
00118         R_Color(textColor);
00119         text = _(text);
00120         UI_DrawStringInBox(font, node->textalign,
00121             pos[0] + node->padding + iconPadding, pos[1] + node->padding,
00122             node->size[0] - node->padding - node->padding - iconPadding, node->size[1] - node->padding - node->padding,
00123             text, LONGLINES_PRETTYCHOP);
00124         R_Color(NULL);
00125     }
00126 }
00127 
00131 static void UI_ButtonNodeLoading (uiNode_t *node)
00132 {
00133     node->padding = 8;
00134     node->textalign = ALIGN_CC;
00135     Vector4Set(node->selectedColor, 1, 1, 1, 1);
00136     Vector4Set(node->color, 1, 1, 1, 1);
00137 }
00138 
00142 static void UI_ButtonNodeLoaded (uiNode_t *node)
00143 {
00144     /* auto calc the size if none was given via script files */
00145     if (node->size[1] == 0) {
00146         const char *font = UI_GetFontFromNode(node);
00147         node->size[1] = (UI_FontGetHeight(font) / 2) + (node->padding * 2);
00148     }
00149 #ifdef DEBUG
00150     if (node->size[0] < CORNER_SIZE + MID_SIZE + CORNER_SIZE || node->size[1] < CORNER_SIZE + MID_SIZE + CORNER_SIZE)
00151         Com_DPrintf(DEBUG_CLIENT, "Node '%s' too small. It can create graphical glitches\n", UI_GetPath(node));
00152 #endif
00153 }
00154 
00155 static const value_t properties[] = {
00156     /* @override image
00157      * Texture used by the button. It's a normalized texture of 128x128.
00158      * Normal button start at 0x0, mouse over start at 64x0, mouse click
00159      * start at 0x64 (but not yet implemented), and disabled start at 64x64.
00160      * See the image to have a usable template for this node.
00161      * @image html http://ufoai.ninex.info/wiki/images/Button_blue.png
00162      */
00163 
00164     /* Icon used to display the node
00165      */
00166     {"icon", V_UI_ICONREF, UI_EXTRADATA_OFFSETOF(EXTRADATA_TYPE, icon), MEMBER_SIZEOF(EXTRADATA_TYPE, icon)},
00167 
00168     {NULL, V_NULL, 0, 0}
00169 };
00170 
00171 
00172 void UI_RegisterButtonNode (uiBehaviour_t *behaviour)
00173 {
00174     behaviour->name = "button";
00175     behaviour->draw = UI_ButtonNodeDraw;
00176     behaviour->loaded = UI_ButtonNodeLoaded;
00177     behaviour->leftClick = UI_ButtonNodeClick;
00178     behaviour->loading = UI_ButtonNodeLoading;
00179     behaviour->properties = properties;
00180     behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
00181 }

Generated by  doxygen 1.6.2