ui_icon.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 "ui_main.h"
00026 #include "ui_internal.h"
00027 #include "ui_parse.h"
00028 #include "ui_icon.h"
00029 #include "ui_render.h"
00030 
00035 #define TILE_HEIGHT 64
00036 
00037 const value_t mn_iconProperties[] = {
00038     {"size", V_POS, offsetof(uiIcon_t, size), MEMBER_SIZEOF(uiIcon_t, size)},
00039     {"single", V_BOOL, offsetof(uiIcon_t, single), 0},
00040     {"blend", V_BOOL, offsetof(uiIcon_t, blend), 0},
00041     {"pack64", V_BOOL, offsetof(uiIcon_t, pack64), 0},
00042 
00043     {"texl", V_POS, offsetof(uiIcon_t, pos[ICON_STATUS_NORMAL]), MEMBER_SIZEOF(uiIcon_t, pos[ICON_STATUS_NORMAL])},
00044     {"hoveredtexl", V_POS, offsetof(uiIcon_t, pos[ICON_STATUS_HOVER]), MEMBER_SIZEOF(uiIcon_t, pos[ICON_STATUS_HOVER])},
00045     {"disabledtexl", V_POS, offsetof(uiIcon_t, pos[ICON_STATUS_DISABLED]), MEMBER_SIZEOF(uiIcon_t, pos[ICON_STATUS_DISABLED])},
00046     {"clickedtexl", V_POS, offsetof(uiIcon_t, pos[ICON_STATUS_CLICKED]), MEMBER_SIZEOF(uiIcon_t, pos[ICON_STATUS_CLICKED])},
00047 
00048     {"image", V_REF_OF_STRING, offsetof(uiIcon_t, image[ICON_STATUS_NORMAL]), 0},
00049     {"hoveredimage", V_REF_OF_STRING, offsetof(uiIcon_t, image[ICON_STATUS_HOVER]), 0},
00050     {"disabledimage", V_REF_OF_STRING, offsetof(uiIcon_t, image[ICON_STATUS_DISABLED]), 0},
00051     {"clickedimage", V_REF_OF_STRING, offsetof(uiIcon_t, image[ICON_STATUS_CLICKED]), 0},
00052 
00053     {"color", V_COLOR, offsetof(uiIcon_t, color[ICON_STATUS_NORMAL]), MEMBER_SIZEOF(uiIcon_t, color[ICON_STATUS_NORMAL])},
00054     {"hoveredcolor", V_COLOR, offsetof(uiIcon_t, color[ICON_STATUS_HOVER]), MEMBER_SIZEOF(uiIcon_t, color[ICON_STATUS_HOVER])},
00055     {"disabledcolor", V_COLOR, offsetof(uiIcon_t, color[ICON_STATUS_DISABLED]), MEMBER_SIZEOF(uiIcon_t, color[ICON_STATUS_DISABLED])},
00056     {"clickedcolor", V_COLOR, offsetof(uiIcon_t, color[ICON_STATUS_CLICKED]), MEMBER_SIZEOF(uiIcon_t, color[ICON_STATUS_CLICKED])},
00057 
00058     {NULL, V_NULL, 0, 0}
00059 };
00060 
00067 static uiIcon_t* UI_AutoGenerateIcon (const char* name)
00068 {
00069     uiIcon_t* icon = NULL;
00070     const char* suffix[ICON_STATUS_MAX] = {"", "_hovered", "_disabled", "_clicked"};
00071     int i;
00072 
00073     const char *picName = va("icons/%s", name);
00074     const image_t *pic = UI_LoadImage(picName);
00075     if (pic == NULL)
00076         return NULL;
00077 
00078     icon = UI_AllocStaticIcon(name);
00079     icon->image[ICON_STATUS_NORMAL] = UI_AllocStaticString(picName, 0);
00080     icon->size[0] = pic->width;
00081     icon->size[1] = pic->height;
00082     for (i = 1; i < ICON_STATUS_MAX; i++) {
00083         picName = va("icons/%s%s", name, suffix[i]);
00084         pic = UI_LoadImage(picName);
00085         if (pic != NULL)
00086             icon->image[i] = UI_AllocStaticString(picName, 0);
00087     }
00088     return icon;
00089 }
00090 
00091 #ifdef DEBUG
00092 
00098 static qboolean UI_IconExists (const char* name)
00099 {
00100     int i;
00101     for (i = 0; i < ui_global.numIcons; i++) {
00102         if (strncmp(name, ui_global.icons[i].name, MEMBER_SIZEOF(uiIcon_t, name)) != 0)
00103             continue;
00104         return qtrue;
00105     }
00106     return qfalse;
00107 }
00108 #endif
00109 
00116 uiIcon_t* UI_GetIconByName (const char* name)
00117 {
00118     int i;
00119     for (i = 0; i < ui_global.numIcons; i++) {
00120         if (strncmp(name, ui_global.icons[i].name, MEMBER_SIZEOF(uiIcon_t, name)) != 0)
00121             continue;
00122         return &ui_global.icons[i];
00123     }
00124     return UI_AutoGenerateIcon(name);
00125 }
00126 
00133 uiIcon_t* UI_AllocStaticIcon (const char* name)
00134 {
00135     uiIcon_t* result;
00137 #ifdef DEBUG
00138     assert(!UI_IconExists(name));
00139 #endif
00140     if (ui_global.numIcons >= UI_MAX_ICONS)
00141         Com_Error(ERR_FATAL, "UI_AllocStaticIcon: UI_MAX_ICONS hit");
00142 
00143     result = &ui_global.icons[ui_global.numIcons];
00144     ui_global.numIcons++;
00145 
00146     memset(result, 0, sizeof(*result));
00147     Q_strncpyz(result->name, name, sizeof(result->name));
00148     return result;
00149 }
00150 
00160 void UI_DrawIconInBox (const uiIcon_t* icon, uiIconStatus_t status, int posX, int posY, int sizeX, int sizeY)
00161 {
00162     int texX;
00163     int texY;
00164     const char* image;
00165 
00167     if (status < 0 && status >= ICON_STATUS_MAX)
00168         return;
00169 
00171     if (icon->single || icon->blend) {
00172         texX = icon->pos[ICON_STATUS_NORMAL][0];
00173         texY = icon->pos[ICON_STATUS_NORMAL][1];
00174         image = icon->image[ICON_STATUS_NORMAL];
00175     } else if (icon->pack64) {
00176         texX = icon->pos[ICON_STATUS_NORMAL][0];
00177         texY = icon->pos[ICON_STATUS_NORMAL][1] + (TILE_HEIGHT * status);
00178         image = icon->image[ICON_STATUS_NORMAL];
00179     } else {
00180         texX = icon->pos[status][0];
00181         texY = icon->pos[status][1];
00182         image = icon->image[status];
00183         if (!image) {
00184             texX = icon->pos[ICON_STATUS_NORMAL][0];
00185             texY = icon->pos[ICON_STATUS_NORMAL][1];
00186             image = icon->image[ICON_STATUS_NORMAL];
00187         }
00188     }
00189     if (!image)
00190         return;
00191 
00192     posX += (sizeX - icon->size[0]) / 2;
00193     posY += (sizeY - icon->size[1]) / 2;
00194 
00195     if (icon->blend) {
00196         const vec_t *color;
00197         color = icon->color[status];
00198         R_Color(color);
00199     }
00200 
00201     UI_DrawNormImageByName(posX, posY, icon->size[0], icon->size[1],
00202         texX + icon->size[0], texY + icon->size[1], texX, texY, image);
00203     if (icon->blend)
00204         R_Color(NULL);
00205 }

Generated by  doxygen 1.6.2