TORCS  1.3.9
The Open Racing Car Simulator
guilabel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  guilabel.cpp -- labels management
3  -------------------
4  created : Fri Aug 13 22:22:12 CEST 1999
5  copyright : (C) 1999-2013 by Eric Espie, Bernhard Wymann
6  email : torcs@free.fr
7  version : $Id$
8  ***************************************************************************/
9 
10 /***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
26 #include <stdlib.h>
27 #ifdef WIN32
28 #include <windows.h>
29 #endif
30 #include <tgfclient.h>
31 #include "gui.h"
32 #include "guifont.h"
33 
34 void
36 {
37 }
38 
62 int
63 GfuiLabelCreateEx(void *scr, const char *text, float *fgColor, int font, int x, int y, int align, int maxlen)
64 {
65  tGfuiLabel *label;
66  tGfuiObject *object;
67  int width;
68  tGfuiScreen *screen = (tGfuiScreen*)scr;
69 
70  object = (tGfuiObject*)calloc(1, sizeof(tGfuiObject));
71  object->widget = GFUI_LABEL;
72  object->focusMode = GFUI_FOCUS_NONE;
73  object->visible = 1;
74  object->id = screen->curId++;
75 
76  if (maxlen == 0) maxlen = strlen(text);
77  label = &(object->u.label);
78  label->text = (char*)calloc(maxlen+1, 1);
79  strncpy(label->text, text, maxlen);
80  label->text[maxlen] = '\0';
81  label->maxlen = maxlen;
82 
83  label->bgColor = screen->bgColor;
84  //label->fgColor = fgColor;
85  label->fgColor.setRGBA(fgColor);
86 
87  label->font = gfuiFont[font];
88  width = gfuiFont[font]->getWidth((const char *)label->text);
89  label->align = align;
90  switch(align&0xF0) {
91  case 0x00 /* LEFT */:
92  label->x = object->xmin = x;
93  label->y = y - gfuiFont[font]->getDescender();
94  object->ymin = y;
95  object->xmax = x + width;
96  object->ymax = y + gfuiFont[font]->getHeight() - gfuiFont[font]->getDescender();
97  break;
98  case 0x10 /* CENTER */:
99  label->x = object->xmin = x - width / 2;
100  label->y = y - gfuiFont[font]->getDescender();
101  object->ymin = y;
102  object->xmax = x + width / 2;
103  object->ymax = y + gfuiFont[font]->getHeight() - gfuiFont[font]->getDescender();
104  break;
105  case 0x20 /* RIGHT */:
106  label->x = object->xmin = x - width;
107  label->y = y - gfuiFont[font]->getDescender();
108  object->ymin = y;
109  object->xmax = x;
110  object->ymax = y + gfuiFont[font]->getHeight() - gfuiFont[font]->getDescender();
111  break;
112  }
113 
114  gfuiAddObject(screen, object);
115 
116  return object->id;
117 }
118 
141 int
142 GfuiLabelCreate(void *scr, const char *text, int font, int x, int y, int align, int maxlen)
143 {
144  return GfuiLabelCreateEx(scr, text, &(GfuiColor[GFUI_LABELCOLOR][0]), font, x, y, align, maxlen);
145 }
146 
154 int
155 GfuiTipCreate(void *scr, const char *text, int maxlen)
156 {
157  return GfuiLabelCreateEx(scr, text, &(GfuiColor[GFUI_TIPCOLOR][0]), GFUI_FONT_SMALL, 320, 15, GFUI_ALIGN_HC_VB, maxlen);
158 }
159 
169 int
170 GfuiTitleCreate(void *scr, const char *text, int maxlen)
171 {
172  return GfuiLabelCreateEx(scr, text, &(GfuiColor[GFUI_TITLECOLOR][0]), GFUI_FONT_BIG, 320, 440, GFUI_ALIGN_HC_VB, maxlen);
173 }
174 
175 void
176 gfuiSetLabelText(tGfuiObject *curObject, tGfuiLabel *label, const char *text)
177 {
178  int pw, w;
179 
180  if (!text) {
181  return;
182  }
183 
184  pw = label->font->getWidth((const char *)label->text);
185  strncpy(label->text, text, label->maxlen);
186  label->text[label->maxlen] = '\0';
187  w = label->font->getWidth((const char *)label->text);
188 
189  switch(label->align&0xF0) {
190  case 0x00 /* LEFT */:
191  curObject->xmax = label->x + w;
192  break;
193  case 0x10 /* CENTER */:
194  label->x = curObject->xmin = label->x + pw / 2 - w / 2;
195  curObject->xmax = curObject->xmax - pw / 2 + w / 2;
196  break;
197  case 0x20 /* RIGHT */:
198  label->x = curObject->xmin = curObject->xmax - w;
199  break;
200  }
201 }
202 
211 void
212 GfuiLabelSetText(void *scr, int id, const char *text)
213 {
214  tGfuiObject *curObject;
215  tGfuiScreen *screen = (tGfuiScreen*)scr;
216 
217  curObject = screen->objects;
218  if (curObject != NULL) {
219  do {
220  curObject = curObject->next;
221  if (curObject->id == id) {
222  if (curObject->widget == GFUI_LABEL) {
223  gfuiSetLabelText(curObject, &(curObject->u.label), text);
224  }
225  return;
226  }
227  } while (curObject != screen->objects);
228  }
229 }
230 
238 void
239 GfuiLabelSetColor(void *scr, int id, float *color)
240 {
241  tGfuiObject *curObject;
242  tGfuiScreen *screen = (tGfuiScreen*)scr;
243 
244  curObject = screen->objects;
245  if (curObject != NULL) {
246  do {
247  curObject = curObject->next;
248  if (curObject->id == id) {
249  if (curObject->widget == GFUI_LABEL) {
250  curObject->u.label.fgColor.setRGBA(color);
251  }
252  return;
253  }
254  } while (curObject != screen->objects);
255  }
256 }
257 
258 
259 void
261 {
262  tGfuiLabel *label;
263 
264  label = &(obj->u.label);
265  if (label->bgColor[3] != 0.0) {
266  glColor4fv(label->bgColor);
267  glBegin(GL_QUADS);
268  glVertex2i(obj->xmin, obj->ymin);
269  glVertex2i(obj->xmin, obj->ymax);
270  glVertex2i(obj->xmax, obj->ymax);
271  glVertex2i(obj->xmax, obj->ymin);
272  glEnd();
273  }
274  glColor4fv(label->fgColor.getRGBA());
275  gfuiPrintString(label->x, label->y, label->font, label->text);
276 }
277 
278 void
280 {
281  tGfuiLabel *label;
282 
283  label = &(obj->u.label);
284 
285  free(label->text);
286  free(obj);
287 }
int align
Definition: gui.h:85
int id
Definition: gui.h:200
float * bgColor
Definition: gui.h:239
int y
Definition: gui.h:84
int GfuiLabelCreate(void *scr, const char *text, int font, int x, int y, int align, int maxlen)
Add a label to a screen.
Definition: guilabel.cpp:142
void gfuiReleaseLabel(tGfuiObject *obj)
Definition: guilabel.cpp:279
#define GFUI_TIPCOLOR
Definition: gui.h:38
GfuiFontClass * font
Definition: gui.h:83
int curId
Definition: gui.h:245
int ymin
Definition: gui.h:205
The Gaming Framework API (client part).
GfuiFontClass * gfuiFont[FONT_NB]
Definition: guifont.cpp:43
void GfuiLabelSetText(void *scr, int id, const char *text)
Change the text of a label.
Definition: guilabel.cpp:212
int xmax
Definition: gui.h:206
#define GFUI_FOCUS_NONE
Definition: gui.h:132
void GfuiLabelSetColor(void *scr, int id, float *color)
Change the color of a label.
Definition: guilabel.cpp:239
int x
Definition: gui.h:84
union GfuiObject::@17 u
char * text
Definition: gui.h:80
struct GfuiObject * next
Definition: gui.h:217
ForeGroundColor fgColor
Definition: gui.h:82
void gfuiAddObject(tGfuiScreen *screen, tGfuiObject *object)
Definition: guiobject.cpp:485
#define GFUI_ALIGN_HC_VB
Definition: tgfclient.h:72
float GfuiColor[GFUI_COLORNB][4]
Definition: gui.cpp:41
#define GFUI_FONT_SMALL
Definition: tgfclient.h:170
static Vector y[4]
Definition: Convex.cpp:56
int getHeight() const
Definition: guifont.cpp:261
Definition: gui.h:78
int GfuiLabelCreateEx(void *scr, const char *text, float *fgColor, int font, int x, int y, int align, int maxlen)
Create a new label (extended version).
Definition: guilabel.cpp:63
int GfuiTitleCreate(void *scr, const char *text, int maxlen)
Add a Title to the screen.
Definition: guilabel.cpp:170
int maxlen
Definition: gui.h:86
tGfuiLabel label
Definition: gui.h:209
#define GFUI_LABEL
Definition: tgfclient.h:61
int ymax
Definition: gui.h:206
tGfuiObject * objects
Definition: gui.h:243
void gfuiLabelInit(void)
Definition: guilabel.cpp:35
void gfuiDrawLabel(tGfuiObject *obj)
Definition: guilabel.cpp:260
float * bgColor
Definition: gui.h:81
void setRGBA(const float prgba[4])
Definition: gui.h:63
void gfuiPrintString(int x, int y, GfuiFontClass *font, const char *string)
Definition: guiobject.cpp:32
const float * getRGBA()
Definition: gui.h:69
#define GFUI_FONT_BIG
Definition: tgfclient.h:167
void gfuiSetLabelText(tGfuiObject *curObject, tGfuiLabel *label, const char *text)
Definition: guilabel.cpp:176
int GfuiTipCreate(void *scr, const char *text, int maxlen)
Add a Tip (generally associated with a button).
Definition: guilabel.cpp:155
#define GFUI_TITLECOLOR
Definition: gui.h:28
int widget
Definition: gui.h:199
int xmin
Definition: gui.h:205
int getWidth(const char *text)
Definition: guifont.cpp:222
int getDescender() const
Definition: guifont.cpp:272
#define GFUI_LABELCOLOR
Definition: gui.h:37