TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
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-2026 by Eric EspiƩ, Bernhard Wymann
6 email : torcs@free.fr
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
22
23#include <stdlib.h>
24#ifdef WIN32
25#include <windows.h>
26#endif
27#include <tgfclient.h>
28#include "gui.h"
29#include "guifont.h"
30
31void
33{
34}
35
50int
51GfuiLabelCreateEx(void *scr, const char *text, float *fgColor, int font, int x, int y, int align, int maxlen)
52{
53 tGfuiLabel *label;
54 tGfuiObject *object;
55 int width;
56 tGfuiScreen *screen = (tGfuiScreen*)scr;
57
58 object = (tGfuiObject*)calloc(1, sizeof(tGfuiObject));
59 object->widget = GFUI_LABEL;
60 object->focusMode = GFUI_FOCUS_NONE;
61 object->visible = 1;
62 object->id = screen->curId++;
63
64 if (maxlen == 0) maxlen = strlen(text);
65 label = &(object->u.label);
66 label->text = (char*)calloc(maxlen+1, 1);
67 strncpy(label->text, text, maxlen);
68 label->text[maxlen] = '\0';
69 label->maxlen = maxlen;
70
71 label->bgColor = screen->bgColor;
72 //label->fgColor = fgColor;
73 label->fgColor.setRGBA(fgColor);
74
75 label->font = gfuiFont[font];
76 width = gfuiFont[font]->getWidth((const char *)label->text);
77 label->align = align;
78 switch(align&0xF0) {
79 case 0x00 /* LEFT */:
80 label->x = object->xmin = x;
81 label->y = y - gfuiFont[font]->getDescender();
82 object->ymin = y;
83 object->xmax = x + width;
84 object->ymax = y + gfuiFont[font]->getHeight() - gfuiFont[font]->getDescender();
85 break;
86 case 0x10 /* CENTER */:
87 label->x = object->xmin = x - width / 2;
88 label->y = y - gfuiFont[font]->getDescender();
89 object->ymin = y;
90 object->xmax = x + width / 2;
91 object->ymax = y + gfuiFont[font]->getHeight() - gfuiFont[font]->getDescender();
92 break;
93 case 0x20 /* RIGHT */:
94 label->x = object->xmin = x - width;
95 label->y = y - gfuiFont[font]->getDescender();
96 object->ymin = y;
97 object->xmax = x;
98 object->ymax = y + gfuiFont[font]->getHeight() - gfuiFont[font]->getDescender();
99 break;
100 }
101
102 gfuiAddObject(screen, object);
103
104 return object->id;
105}
106
120int
121GfuiLabelCreate(void *scr, const char *text, int font, int x, int y, int align, int maxlen)
122{
123 return GfuiLabelCreateEx(scr, text, &(GfuiColor[GFUI_LABELCOLOR][0]), font, x, y, align, maxlen);
124}
125
134int
135GfuiTipCreate(void *scr, const char *text, int maxlen)
136{
137 return GfuiLabelCreateEx(scr, text, &(GfuiColor[GFUI_TIPCOLOR][0]), GFUI_FONT_SMALL, 320, 15, GFUI_ALIGN_HC_VB, maxlen);
138}
139
149int
150GfuiTitleCreate(void *scr, const char *text, int maxlen)
151{
152 return GfuiLabelCreateEx(scr, text, &(GfuiColor[GFUI_TITLECOLOR][0]), GFUI_FONT_BIG, 320, 440, GFUI_ALIGN_HC_VB, maxlen);
153}
154
155void
156gfuiSetLabelText(tGfuiObject *curObject, tGfuiLabel *label, const char *text)
157{
158 int pw, w;
159
160 if (!text) {
161 return;
162 }
163
164 pw = label->font->getWidth((const char *)label->text);
165 strncpy(label->text, text, label->maxlen);
166 label->text[label->maxlen] = '\0';
167 w = label->font->getWidth((const char *)label->text);
168
169 switch(label->align&0xF0) {
170 case 0x00 /* LEFT */:
171 curObject->xmax = label->x + w;
172 break;
173 case 0x10 /* CENTER */:
174 label->x = curObject->xmin = label->x + pw / 2 - w / 2;
175 curObject->xmax = curObject->xmax - pw / 2 + w / 2;
176 break;
177 case 0x20 /* RIGHT */:
178 label->x = curObject->xmin = curObject->xmax - w;
179 break;
180 }
181}
182
192void
193GfuiLabelSetText(void *scr, int id, const char *text)
194{
195 tGfuiObject *curObject;
196 tGfuiScreen *screen = (tGfuiScreen*)scr;
197
198 curObject = screen->objects;
199 if (curObject != NULL) {
200 do {
201 curObject = curObject->next;
202 if (curObject->id == id) {
203 if (curObject->widget == GFUI_LABEL) {
204 gfuiSetLabelText(curObject, &(curObject->u.label), text);
205 }
206 return;
207 }
208 } while (curObject != screen->objects);
209 }
210}
211
220void
221GfuiLabelSetColor(void *scr, int id, float *color)
222{
223 tGfuiObject *curObject;
224 tGfuiScreen *screen = (tGfuiScreen*)scr;
225
226 curObject = screen->objects;
227 if (curObject != NULL) {
228 do {
229 curObject = curObject->next;
230 if (curObject->id == id) {
231 if (curObject->widget == GFUI_LABEL) {
232 curObject->u.label.fgColor.setRGBA(color);
233 }
234 return;
235 }
236 } while (curObject != screen->objects);
237 }
238}
239
240
241void
243{
244 tGfuiLabel *label;
245
246 label = &(obj->u.label);
247 if (label->bgColor[3] != 0.0) {
248 glColor4fv(label->bgColor);
249 glBegin(GL_QUADS);
250 glVertex2i(obj->xmin, obj->ymin);
251 glVertex2i(obj->xmin, obj->ymax);
252 glVertex2i(obj->xmax, obj->ymax);
253 glVertex2i(obj->xmax, obj->ymin);
254 glEnd();
255 }
256 glColor4fv(label->fgColor.getRGBA());
257 gfuiPrintString(label->x, label->y, label->font, label->text);
258}
259
260void
262{
263 tGfuiLabel *label;
264
265 label = &(obj->u.label);
266
267 free(label->text);
268 free(obj);
269}
static Vector y[4]
Definition Convex.cpp:56
const float * getRGBA()
Definition gui.h:69
void setRGBA(const float prgba[4])
Definition gui.h:63
int getWidth(const char *text)
Definition guifont.cpp:222
void GfuiLabelSetText(void *scr, int id, const char *text)
Change the text of a label.
Definition guilabel.cpp:193
int GfuiTitleCreate(void *scr, const char *text, int maxlen)
Add a Title to the screen.
Definition guilabel.cpp:150
int GfuiTipCreate(void *scr, const char *text, int maxlen)
Add a Tip (generally associated with a button).
Definition guilabel.cpp:135
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:51
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:121
void GfuiLabelSetColor(void *scr, int id, float *color)
Change the color of a label.
Definition guilabel.cpp:221
float GfuiColor[GFUI_COLORNB][4]
Definition gui.cpp:38
#define GFUI_FOCUS_NONE
Definition gui.h:128
void gfuiPrintString(int x, int y, GfuiFontClass *font, const char *string)
Definition guiobject.cpp:32
void gfuiAddObject(tGfuiScreen *screen, tGfuiObject *object)
struct GfuiObject tGfuiObject
#define GFUI_TIPCOLOR
Definition gui.h:38
#define GFUI_TITLECOLOR
Definition gui.h:28
#define GFUI_LABELCOLOR
Definition gui.h:37
GfuiFontClass * gfuiFont[FONT_NB]
Definition guifont.cpp:43
void gfuiReleaseLabel(tGfuiObject *obj)
Definition guilabel.cpp:261
void gfuiDrawLabel(tGfuiObject *obj)
Definition guilabel.cpp:242
void gfuiSetLabelText(tGfuiObject *curObject, tGfuiLabel *label, const char *text)
Definition guilabel.cpp:156
void gfuiLabelInit(void)
Definition guilabel.cpp:32
int ymin
Definition gui.h:199
int xmin
Definition gui.h:199
int xmax
Definition gui.h:200
int ymax
Definition gui.h:200
tGfuiLabel label
Definition gui.h:203
int id
Definition gui.h:194
union GfuiObject::@107216344126174123112226307372375007210201317144 u
int widget
Definition gui.h:193
struct GfuiObject * next
Definition gui.h:211
float * bgColor
Definition gui.h:81
char * text
Definition gui.h:80
int maxlen
Definition gui.h:86
int y
Definition gui.h:84
ForeGroundColor fgColor
Definition gui.h:82
int align
Definition gui.h:85
int x
Definition gui.h:84
GfuiFontClass * font
Definition gui.h:83
tGfuiObject * objects
Definition gui.h:236
float * bgColor
Definition gui.h:232
int curId
Definition gui.h:238
The Gaming Framework API (client part).
#define GFUI_FONT_BIG
Definition tgfclient.h:247
#define GFUI_LABEL
Definition tgfclient.h:63
#define GFUI_FONT_SMALL
Definition tgfclient.h:250
#define GFUI_ALIGN_HC_VB
Definition tgfclient.h:74