ui_node_textlist.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_font.h"
00028 #include "../ui_actions.h"
00029 #include "../ui_parse.h"
00030 #include "../ui_render.h"
00031 #include "../ui_data.h"
00032 #include "ui_node_text.h"
00033 #include "ui_node_textlist.h"
00034 #include "ui_node_abstractnode.h"
00035 
00036 #include "../../client.h"
00037 #include "../../../shared/parse.h"
00038 
00039 #define EXTRADATA(node) UI_EXTRADATA(node, textExtraData_t)
00040 #define EXTRADATACONST(node) UI_EXTRADATACONST(node, textExtraData_t)
00041 
00049 static int UI_TextListNodeGetLine (const uiNode_t *node, int x, int y)
00050 {
00051     int lineHeight = EXTRADATACONST(node).lineHeight;
00052     if (lineHeight == 0) {
00053         const char *font = UI_GetFontFromNode(node);
00054         lineHeight = UI_FontGetHeight(font);
00055     }
00056 
00057     UI_NodeAbsoluteToRelativePos(node, &x, &y);
00058     y -= node->padding;
00059     return (int) (y / lineHeight) + EXTRADATACONST(node).super.scrollY.viewPos;
00060 }
00061 
00062 static void UI_TextListNodeMouseMove (uiNode_t *node, int x, int y)
00063 {
00064     EXTRADATA(node).lineUnderMouse = UI_TextListNodeGetLine(node, x, y);
00065 }
00066 
00072 static void UI_TextLineNodeDrawText (uiNode_t* node, const linkedList_t* list)
00073 {
00074     vec4_t colorHover;
00075     vec4_t colorSelectedHover;
00076     int count; /* variable x position */
00077     const char *font = UI_GetFontFromNode(node);
00078     vec2_t pos;
00079     int currentY;
00080     int viewSizeY;
00081     int lineHeight;
00082     int maxHeight;
00083 
00084     lineHeight = EXTRADATA(node).lineHeight;
00085     if (lineHeight == 0)
00086         lineHeight = UI_FontGetHeight(font);
00087 
00088     if (UI_AbstractScrollableNodeIsSizeChange(node))
00089         viewSizeY = node->size[1] / lineHeight;
00090     else
00091         viewSizeY = EXTRADATA(node).super.scrollY.viewSize;
00092 
00093     /* text box */
00094     UI_GetNodeAbsPos(node, pos);
00095     pos[0] += node->padding;
00096     pos[1] += node->padding;
00097 
00098     /* prepare colors */
00099     VectorScale(node->color, 0.8, colorHover);
00100     colorHover[3] = node->color[3];
00101     VectorScale(node->selectedColor, 0.8, colorSelectedHover);
00102     colorSelectedHover[3] = node->selectedColor[3];
00103 
00104     /* skip lines over the scroll */
00105     count = 0;
00106     while (list && count < EXTRADATA(node).super.scrollY.viewPos) {
00107         list = list->next;
00108         count++;
00109     }
00110 
00111     currentY = pos[1];
00112     maxHeight = currentY + node->size[1] - node->padding - node->padding - node->padding - lineHeight;
00113     while (list) {
00114         const int width = node->size[0] - node->padding - node->padding;
00115         const char* text = (const char*) list->data;
00116         if (currentY > maxHeight)
00117             break;
00118 
00119         /* highlight selected line */
00120         if (count == EXTRADATA(node).textLineSelected && EXTRADATA(node).textLineSelected >= 0)
00121             R_Color(node->selectedColor);
00122         else
00123             R_Color(node->color);
00124 
00125         /* highlight line under mouse */
00126         if (node->state && count == EXTRADATA(node).lineUnderMouse) {
00127             if (count == EXTRADATA(node).textLineSelected && EXTRADATA(node).textLineSelected >= 0)
00128                 R_Color(colorSelectedHover);
00129             else
00130                 R_Color(colorHover);
00131         }
00132 
00133         UI_DrawStringInBox(font, node->textalign, pos[0], currentY, width, lineHeight, text, LONGLINES_PRETTYCHOP);
00134 
00135         /* next entries' position */
00136         currentY += lineHeight;
00137 
00138 
00139         list = list->next;
00140         count++;
00141     }
00142 
00143     /* count all elements */
00144     while (list) {
00145         list = list->next;
00146         count++;
00147     }
00148 
00149     /* update scroll status */
00150     UI_AbstractScrollableNodeSetY(node, -1, viewSizeY, count);
00151 
00152     R_Color(NULL);
00153 }
00154 
00158 static void UI_TextListNodeDraw (uiNode_t *node)
00159 {
00160     const uiSharedData_t *shared;
00161     shared = &ui_global.sharedData[EXTRADATA(node).dataID];
00162 
00163     /* nothing set yet? */
00164     if (shared->type == UI_SHARED_NONE)
00165         return;
00166     if (shared->type != UI_SHARED_LINKEDLISTTEXT) {
00167         Com_Printf("UI_TextListNodeDraw: Only linkedlist text supported (dataid %d).\n", EXTRADATA(node).dataID);
00168         UI_ResetData(EXTRADATA(node).dataID);
00169         return;
00170     }
00171 
00172     UI_TextLineNodeDrawText(node, shared->data.linkedListText);
00173 }
00174 
00179 static void UI_TextListNodeClick (uiNode_t * node, int x, int y)
00180 {
00181     const int line = UI_TextListNodeGetLine(node, x, y);
00182 
00183     if (line < 0 || line >= EXTRADATA(node).super.scrollY.fullSize)
00184         return;
00185 
00186     if (line != EXTRADATA(node).textLineSelected) {
00187         EXTRADATA(node).textLineSelected = line;
00188         if (node->onChange)
00189             UI_ExecuteEventActions(node, node->onChange);
00190     }
00191 
00192     if (node->onClick)
00193         UI_ExecuteEventActions(node, node->onClick);
00194 }
00195 
00200 static void UI_TextListNodeRightClick (uiNode_t * node, int x, int y)
00201 {
00202     const int line = UI_TextListNodeGetLine(node, x, y);
00203 
00204     if (line < 0 || line >= EXTRADATA(node).super.scrollY.fullSize)
00205         return;
00206 
00207     if (line != EXTRADATA(node).textLineSelected) {
00208         EXTRADATA(node).textLineSelected = line;
00209         if (node->onChange)
00210             UI_ExecuteEventActions(node, node->onChange);
00211     }
00212 
00213     if (node->onRightClick)
00214         UI_ExecuteEventActions(node, node->onRightClick);
00215 }
00216 
00219 static void UI_TextListNodeMouseWheel (uiNode_t *node, qboolean down, int x, int y)
00220 {
00221     UI_AbstractScrollableNodeScrollY(node, (down ? 1 : -1));
00222     if (node->onWheelUp && !down)
00223         UI_ExecuteEventActions(node, node->onWheelUp);
00224     if (node->onWheelDown && down)
00225         UI_ExecuteEventActions(node, node->onWheelDown);
00226     if (node->onWheel)
00227         UI_ExecuteEventActions(node, node->onWheel);
00228 }
00229 
00230 static void UI_TextListNodeLoading (uiNode_t *node)
00231 {
00232     EXTRADATA(node).textLineSelected = -1; 
00233     Vector4Set(node->selectedColor, 1.0, 1.0, 1.0, 1.0);
00234     Vector4Set(node->color, 1.0, 1.0, 1.0, 1.0);
00235     node->textalign = ALIGN_CL; 
00236 }
00237 
00238 void UI_RegisterTextListNode (uiBehaviour_t *behaviour)
00239 {
00240     behaviour->name = "textlist";
00241     behaviour->extends = "text";
00242     behaviour->draw = UI_TextListNodeDraw;
00243     behaviour->leftClick = UI_TextListNodeClick;
00244     behaviour->rightClick = UI_TextListNodeRightClick;
00245     behaviour->mouseWheel = UI_TextListNodeMouseWheel;
00246     behaviour->mouseMove = UI_TextListNodeMouseMove;
00247     behaviour->loading = UI_TextListNodeLoading;
00248 }

Generated by  doxygen 1.6.2