ui_tooltip.c

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 2002-2010 UFO: Alien Invasion.
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016 
00017 See the GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00023 */
00024 
00025 #include "../client.h"
00026 #include "node/ui_node_window.h"
00027 #include "ui_tooltip.h"
00028 #include "ui_nodes.h"
00029 #include "ui_parse.h"
00030 #include "ui_render.h"
00031 #include "ui_input.h"
00032 
00033 #include "../client.h" /* gettext _() */
00034 
00035 static const vec4_t tooltipBG = { 0.0f, 0.0f, 0.0f, 0.7f };
00036 static const vec4_t tooltipColor = { 0.0f, 0.8f, 0.0f, 1.0f };
00037 
00041 int UI_DrawTooltip (const char *string, int x, int y, int maxWidth)
00042 {
00043     const char *font = "f_small";
00044     int height = 0, width = 0;
00045 
00046     if (!string || string[0] == '\0' || !font)
00047         return 0;
00048 
00049     R_FontTextSize(font, string, maxWidth, LONGLINES_WRAP, &width, &height, NULL, NULL);
00050 
00051     if (!width)
00052         return 0;
00053 
00054     x += 5;
00055     y += 5;
00056 
00057     if (x + width + 3 > VID_NORM_WIDTH)
00058         x -= width + 10;
00059 
00060     if (y + height + 3 > VID_NORM_HEIGHT)
00061         y -= height + 10;
00062 
00063     UI_DrawFill(x - 1, y - 1, width + 4, height + 4, tooltipBG);
00064     R_Color(tooltipColor);
00065     UI_DrawString(font, ALIGN_UL, x + 1, y + 1, x + 1, maxWidth, 0, string, 0, 0, NULL, qfalse, LONGLINES_WRAP);
00066     R_Color(NULL);
00067 
00068     return width;
00069 }
00070 
00074 void UI_Tooltip (uiNode_t *node, int x, int y)
00075 {
00076     const char *string;
00077     const char *key = NULL;
00078     const char *tooltip = NULL;
00079     static const int maxWidth = 200;
00080 
00081     /* check values */
00082     if (node->key)
00083         key = Key_KeynumToString(node->key->key);
00084     if (node->tooltip)
00085         tooltip = UI_GetReferenceString(node, node->tooltip);
00086 
00087     /* normalize */
00088     if (tooltip && tooltip[0] == '\0')
00089         tooltip = NULL;
00090     if (key && key[0] == '\0')
00091         key = NULL;
00092 
00093     /* create tooltip */
00094     if (key && tooltip) {
00095         char buf[MAX_VAR];
00096         Com_sprintf(buf, sizeof(buf), _("Key: %s"), key);
00097         string = va("%s\n%s", tooltip, buf);
00098     } else if (tooltip) {
00099         string = tooltip;
00100     } else if (key) {
00101         string = va(_("Key: %s"), key);
00102     } else {
00103         return;
00104     }
00105 
00106     UI_DrawTooltip(string, x, y, maxWidth);
00107 }

Generated by  doxygen 1.6.2