TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
pitmenu.cpp
Go to the documentation of this file.
1/***************************************************************************
2
3 file : pitmenu.cpp
4 created : Mon Apr 24 18:16:37 CEST 2000
5 copyright : (C) 2013-2026 by Eric EspiƩ, Bernhard Wymann
6 email : berniw@bluewin.ch
7
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
23
24#include <stdlib.h>
25#ifdef WIN32
26#include <windows.h>
27#endif
28#include <tgfclient.h>
29#include <car.h>
30#include <portability.h>
31#include <racescreens.h>
32
33static void *menuHandle = NULL;
34static int fuelId;
35static int repairId;
36static tCarElt *rmCar;
38static int tireChangeId;
39static int serviceButtonId = -1;
40static int stopAndGoButtonId = -1;
41static int setupButtonId = -1;
42static int serviceTipLabelId = -1;
43static int stopAndGoTipLabelId = -1;
45static int setupTipLabelId = -1;
47static bool stopAndGoAvailable = false;
48static bool setupAvailable = false;
49
50static float LabelColor[] = {1.0, 0.0, 1.0, 1.0};
51static float DisabledTipColor[] = {1.0, 0.0, 0.0, 1.0};
52static const char *ServiceTipMessage = "Perform selected pit service";
53static const char *StopAndGoTipMessage = "Serve current Stop & Go penalty";
54static const char *NoStopAndGoMessage = "No current Stop & Go penalty to serve";
55static const char *SetupTipMessage = "Change car setup";
56static const char *SetupDisabledMessage = "Only available in practice and qualifying sessions";
57
58// list of tire change options.
59static const char *tireChangeLabel[] = {
60 "All",
61 "None"
62};
67static const int nbtireChangeOptions = sizeof(tireChangeLabel) / sizeof(tireChangeLabel[0]);
69
70
71static void rmClampPitFuel(void)
72{
73 if (!rmCar) {
74 return;
75 }
76
77 tdble maxFuel = rmCar->_tank - rmCar->_fuel;
78 if (maxFuel < 0.0f) {
79 maxFuel = 0.0f;
80 }
81 if (rmCar->_pitFuel < 0.0f) {
82 rmCar->_pitFuel = 0.0f;
83 } else if (rmCar->_pitFuel > maxFuel) {
84 rmCar->_pitFuel = maxFuel;
85 }
86}
87
88
89static void rmClampPitRepair(void)
90{
91 if (!rmCar) {
92 return;
93 }
94
95 int maxRepair = rmCar->_dammage;
96 if (maxRepair < 0) {
97 maxRepair = 0;
98 }
99 if (rmCar->_pitRepair < 0) {
100 rmCar->_pitRepair = 0;
101 } else if (rmCar->_pitRepair > maxRepair) {
102 rmCar->_pitRepair = maxRepair;
103 }
104}
105
106
107static void rmClampPitCmd(void)
108{
111}
112
113
114static void rmSetFuelText(void)
115{
116 const int BUFSIZE = 32;
117 char buf[BUFSIZE];
118
119 snprintf(buf, BUFSIZE, "%.1f", rmCar->_pitFuel);
121}
122
123
124static void rmSetRepairText(void)
125{
126 const int BUFSIZE = 32;
127 char buf[BUFSIZE];
128
129 snprintf(buf, BUFSIZE, "%d", rmCar->_pitRepair);
131}
132
133static void rmUpdtFuel(void)
134{
135 char *val;
136
138 rmCar->pitcmd.fuel = (tdble)strtod(val, (char **)NULL);
141}
142
143
144static void rmUpdtRepair(void)
145{
146 char *val;
147
149 rmCar->pitcmd.repair = strtol(val, (char **)NULL, 0);
152}
153
154
173
174
175static void rmShowButtonTip(int buttonId, int tipLabelId, int disabledTipLabelId, bool available)
176{
178 if (!GfuiScreenIsActive(menuHandle) || GfuiButtonGetFocused() != buttonId) {
179 return;
180 }
181
182 int labelId = available ? tipLabelId : disabledTipLabelId;
183 if (labelId >= 0) {
185 }
186}
187
188
189static void rmServiceFocus(void)
190{
192}
193
194
199
200
205
206
208
209
210static void rmStopAndGo(void)
211{
212 rmCar->_pitStopType = RM_PIT_STOPANDGO;
213 if (rmCallback) {
214 rmCallback();
215 }
216}
217
218
219static void rmService(void)
220{
222 rmCar->_pitStopType = RM_PIT_REPAIR;
223 if (rmCallback) {
224 rmCallback();
225 }
226}
227
228// Manage tire change state.
246
247
248
255void RmPitMenuStartAction(tCarElt *car, tRmInfo *reInfo, const tGfuiAction &callback)
256{
257 const int BUFSIZE = 256;
258 char buf[BUFSIZE];
259 int y, x, dy;
260
261 rmCar = car;
262 rmInfo = reInfo;
264
265 if (menuHandle) {
267 }
268 serviceButtonId = -1;
270 setupButtonId = -1;
274 setupTipLabelId = -1;
276 menuHandle = GfuiMenuScreenCreate("Pit Stop Info");
277
278 x = 80;
279 y = 380;
280 snprintf(buf, BUFSIZE, "Driver: %s", car->_name);
283
284 y -= dy;
285 snprintf(buf, BUFSIZE, "Remaining Laps: %d", car->_remainingLaps);
287
288 y -= dy;
289 snprintf(buf, BUFSIZE, "Remaining Fuel: %.1f l", car->_fuel);
291
292 y -= dy;
293 GfuiLabelCreate(menuHandle, "Fuel amount (liters):", GFUI_FONT_MEDIUM_C, x, y, GFUI_ALIGN_HL_VB, 0);
294
295 int dx = GfuiFontWidth(GFUI_FONT_MEDIUM_C, "Fuel amount (liters)") + 20;
296 snprintf(buf, BUFSIZE, "%.1f", car->pitcmd.fuel);
298 x + dx, y,
299 100, 10, tGfuiAction(), rmUpdtFuel);
300
301 y -= dy;
303
304 snprintf(buf, BUFSIZE, "%d", (int)car->pitcmd.repair);
306 x + dx, y,
307 100, 10, tGfuiAction(), rmUpdtRepair);
308
309 y -= dy;
310
311 int x2 = x + dx - 15;
312 int x3 = x + dx + 100 + 14;
313 int x4 = (x2+x3)/2;
314
316
317 if (!(reInfo->raceRules.tireFactor > 0.0f) || car->info.skillLevel != 3) {
319 rmCar->pitcmd.tireChange = tCarPitCmd::NONE;
320 tireChangeId = GfuiLabelCreate(menuHandle, "Tire wear is off", GFUI_FONT_MEDIUM_C, x + dx, y, GFUI_ALIGN_HL_VB, 32);
321 } else {
323
324 GfuiGrButtonCreateAction(menuHandle, "data/img/arrow-left.png", "data/img/arrow-left.png",
325 "data/img/arrow-left.png", "data/img/arrow-left-pushed.png",
326 x2, y-5, GFUI_ALIGN_HL_VB, 1,
327 [] { changeTireState(-1); },
329 GfuiGrButtonCreateAction(menuHandle, "data/img/arrow-right.png", "data/img/arrow-right.png",
330 "data/img/arrow-right.png", "data/img/arrow-right-pushed.png",
331 x3, y-5, GFUI_ALIGN_HR_VB, 1,
332 [] { changeTireState(1); },
334
335 rmCar->pitcmd.tireChange = tireChangeList[currentTireChangeOption];
337 }
338
341 rmCallback = callback;
342
343 // Just enable stop and go if penalty is pending
344 tCarPenalty *penalty = GF_TAILQ_FIRST(&(car->_penaltyList));
345 stopAndGoAvailable = penalty && penalty->penalty == RM_PENALTY_STOPANDGO;
348 if (!stopAndGoAvailable) {
350 }
351
352 // Just enable car setup button in practice and qualifying sessions
353 void *setupScreen = RmCarSetupScreenInit(menuHandle, rmCar, rmInfo);
356 [setupScreen] { GfuiScreenActivate(setupScreen); }, rmSetupFocus, rmHideButtonTips);
357 if (!setupAvailable) {
359 }
360
366 320, 15, GFUI_ALIGN_HC_VB, 64);
371 320, 15, GFUI_ALIGN_HC_VB, 64);
373
375}
376
377void RmPitMenuStart(tCarElt *car, tRmInfo *reInfo, void *userdata, tfuiCallback callback)
378{
379 RmPitMenuStartAction(car, reInfo,
380 callback ? tGfuiAction([userdata, callback] { callback(userdata); }) : tGfuiAction());
381}
static Vector y[4]
Definition Convex.cpp:56
#define RM_PIT_STOPANDGO
Definition car.h:441
#define RM_PIT_REPAIR
Definition car.h:440
struct CarElt tCarElt
Car structure (tCarElt).
struct CarPenalty tCarPenalty
One penalty.
#define RM_PENALTY_STOPANDGO
Definition car.h:127
static float LabelColor[]
float tdble
Floating point type used in TORCS.
Definition tgf.h:47
int GfuiButtonGetFocused(void)
Get the Id of the button focused in the current screen.
void GfuiLabelSetText(void *scr, int id, const char *text)
Change the text of a label.
Definition guilabel.cpp:193
int GfuiFontHeight(int font)
Get the height of an initialized GUI font.
Definition guiobject.cpp:65
void GfuiScreenRelease(void *scr)
Release the given screen.
Definition gui.cpp:670
int GfuiFontWidth(int font, const char *text)
Measure text using an initialized GUI font.
Definition guiobject.cpp:70
int GfuiEditboxCreateAction(void *scr, const char *text, int font, int x, int y, int width, int maxlen, const tGfuiAction &onFocus, const tGfuiAction &onFocusLost, int margin)
Add an edit box to a screen.
Definition guiedit.cpp:53
void * GfuiMenuScreenCreate(const char *title)
Create a new menu screen.
Definition guimenu.cpp:77
int GfuiVisibilitySet(void *scr, int id, int visible)
Set/unset the visibility attribute of an object.
int GfuiEnable(void *scr, int id, int flag)
Enable / Disable an object.
void GfuiEditboxSetString(void *scr, int id, const char *text)
Set a new string.
Definition guiedit.cpp:390
char * GfuiEditboxGetString(void *scr, int id)
Get the current text of an edit box.
Definition guiedit.cpp:364
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
int GfuiGrButtonCreateAction(void *scr, const char *disabled, const char *enabled, const char *focused, const char *pushed, int x, int y, int align, int mouse, const tGfuiAction &onPush, const tGfuiAction &onFocus, const tGfuiAction &onFocusLost)
Add a graphical button to a screen.
Definition guibutton.cpp:58
int GfuiScreenIsActive(void *screen)
Tell if the screen is active or not.
Definition gui.cpp:495
void GfuiScreenActivate(void *screen)
Activate a screen and make it current.
Definition gui.cpp:508
int GfuiButtonCreateAction(void *scr, const char *text, int font, int x, int y, int width, int align, int mouse, const tGfuiAction &onPush, const tGfuiAction &onFocus, const tGfuiAction &onFocusLost)
Add a button to a screen.
void * RmCarSetupScreenInit(void *prevMenu, tCarElt *car, tRmInfo *reInfo)
Display car setup screen, for loading, saving and changing car setups.
void RmPitMenuStartAction(tCarElt *car, tRmInfo *reInfo, const tGfuiAction &callback)
Pit menu.
Definition pitmenu.cpp:255
#define GF_TAILQ_FIRST(head)
First element of a TAILQ.
Definition tgf.h:463
void * menuHandle
Definition mainmenu.cpp:30
static void rmClampPitFuel(void)
Definition pitmenu.cpp:71
static int stopAndGoDisabledTipLabelId
Definition pitmenu.cpp:44
static void rmSetFuelText(void)
Definition pitmenu.cpp:114
static int serviceButtonId
Definition pitmenu.cpp:39
static void rmClampPitRepair(void)
Definition pitmenu.cpp:89
static float DisabledTipColor[]
Definition pitmenu.cpp:51
static const char * StopAndGoTipMessage
Definition pitmenu.cpp:53
static tCarElt * rmCar
Definition pitmenu.cpp:36
static void rmStopAndGoFocus(void)
Definition pitmenu.cpp:195
static void rmHideButtonTips(void)
Definition pitmenu.cpp:155
static int stopAndGoTipLabelId
Definition pitmenu.cpp:43
static void rmUpdtRepair(void)
Definition pitmenu.cpp:144
static const char * NoStopAndGoMessage
Definition pitmenu.cpp:54
static int stopAndGoButtonId
Definition pitmenu.cpp:40
static const int nbtireChangeOptions
Definition pitmenu.cpp:67
static void rmSetupFocus(void)
Definition pitmenu.cpp:201
static void rmServiceFocus(void)
Definition pitmenu.cpp:189
static int serviceTipLabelId
Definition pitmenu.cpp:42
static int currentTireChangeOption
Definition pitmenu.cpp:68
static void changeTireState(int i)
Definition pitmenu.cpp:229
static int tireChangeId
Definition pitmenu.cpp:38
static const tCarPitCmd::TireChange tireChangeList[]
Definition pitmenu.cpp:63
static const char * ServiceTipMessage
Definition pitmenu.cpp:52
static int setupButtonId
Definition pitmenu.cpp:41
static void rmUpdtFuel(void)
Definition pitmenu.cpp:133
void RmPitMenuStart(tCarElt *car, tRmInfo *reInfo, void *userdata, tfuiCallback callback)
Definition pitmenu.cpp:377
static bool stopAndGoAvailable
Definition pitmenu.cpp:47
static const char * tireChangeLabel[]
Definition pitmenu.cpp:59
static tGfuiAction rmCallback
Definition pitmenu.cpp:207
static void rmService(void)
Definition pitmenu.cpp:219
static int fuelId
Definition pitmenu.cpp:34
static const char * SetupDisabledMessage
Definition pitmenu.cpp:56
static tRmInfo * rmInfo
Definition pitmenu.cpp:37
static int setupTipLabelId
Definition pitmenu.cpp:45
static int repairId
Definition pitmenu.cpp:35
static void rmShowButtonTip(int buttonId, int tipLabelId, int disabledTipLabelId, bool available)
Definition pitmenu.cpp:175
static void rmClampPitCmd(void)
Definition pitmenu.cpp:107
static void rmStopAndGo(void)
Definition pitmenu.cpp:210
static void rmSetRepairText(void)
Definition pitmenu.cpp:124
static bool setupAvailable
Definition pitmenu.cpp:48
static int setupDisabledTipLabelId
Definition pitmenu.cpp:46
static const char * SetupTipMessage
Definition pitmenu.cpp:55
struct RmInfo tRmInfo
Race Manager General Info.
#define RM_TYPE_PRACTICE
Definition raceman.h:70
#define RM_TYPE_QUALIF
Definition raceman.h:71
tCarPitCmd pitcmd
private
Definition car.h:465
tInitCar info
public
Definition car.h:460
int penalty
penalty type
Definition car.h:132
tRmRaceRules raceRules
Definition raceman.h:230
tSituation * s
Situation during race.
Definition raceman.h:222
tdble tireFactor
Definition raceman.h:194
tRaceAdmInfo raceInfo
Definition raceman.h:85
TireChange
Definition car.h:445
@ NONE
Definition car.h:445
@ ALL
Definition car.h:445
int repair
Definition car.h:439
tdble fuel
Definition car.h:438
int skillLevel
Driver's skill level (0=rookie -> 3=pro)
Definition car.h:81
int type
Race type.
Definition raceman.h:69
void(* tfuiCallback)(void *)
Definition tgfclient.h:108
#define GFUI_FONT_LARGE
Definition tgfclient.h:248
#define GFUI_INVISIBLE
Object invisibility flag.
Definition tgfclient.h:239
#define GFUI_FONT_LARGE_C
Definition tgfclient.h:252
#define GFUI_FONT_SMALL
Definition tgfclient.h:250
#define GFUI_FONT_MEDIUM_C
Definition tgfclient.h:253
#define GFUI_ALIGN_HL_VB
Definition tgfclient.h:71
#define GFUI_MOUSE_UP
Definition tgfclient.h:82
std::function< void()> tGfuiAction
Definition tgfclient.h:107
#define GFUI_ALIGN_HR_VB
Definition tgfclient.h:77
#define GFUI_ALIGN_HC_VB
Definition tgfclient.h:74
#define GFUI_VISIBLE
Object visibility flag.
Definition tgfclient.h:238
#define GFUI_DISABLE
Definition tgfclient.h:241