TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
guimenu.cpp
Go to the documentation of this file.
1/***************************************************************************
2 menu.cpp -- menu management
3 -------------------
4 created : Fri Aug 13 22:23:19 CEST 1999
5 copyright : (C) 1999-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
25#include <stdio.h>
26#include <stdlib.h>
27#include <memory>
28#ifdef WIN32
29#include <windows.h>
30#endif
31#include <GL/glut.h>
32
33#include <tgfclient.h>
34#include "gui.h"
35#include "guimenu.h"
36
37void
39{
40}
41
55void
57{
58 GfuiAddKeyAction(scr, 9, "Select Next Entry", [] { gfuiSelectNext(NULL); }, tGfuiAction());
59 GfuiAddKeyAction(scr, 13, "Perform Action", [] { gfuiMouseAction(2); }, tGfuiAction());
60 GfuiAddSKeyAction(scr, GLUT_KEY_UP, "Select Previous Entry", [] { gfuiSelectPrev(NULL); }, tGfuiAction());
61 GfuiAddSKeyAction(scr, GLUT_KEY_DOWN, "Select Next Entry", [] { gfuiSelectNext(NULL); }, tGfuiAction());
62 GfuiAddSKeyAction(scr, GLUT_KEY_PAGE_UP, "Select Previous Entry", [] { gfuiSelectPrev(NULL); }, tGfuiAction());
63 GfuiAddSKeyAction(scr, GLUT_KEY_PAGE_DOWN, "Select Next Entry", [] { gfuiSelectNext(NULL); }, tGfuiAction());
64 GfuiAddSKeyAction(scr, GLUT_KEY_F1, "Help", [scr] { GfuiHelpScreen(scr); }, tGfuiAction());
65 GfuiAddSKeyAction(scr, GLUT_KEY_F12, "Screen-Shot", [] { GfuiScreenShot(NULL); }, tGfuiAction());
66
67}
68
76void *
77GfuiMenuScreenCreate(const char *title)
78{
79 void *scr;
80
81 scr = GfuiScreenCreate();
82 GfuiTitleCreate(scr, title, strlen(title));
83
85
86 return scr;
87}
88
89static void
91{
92 tGfuiObject *button = gfuiGetObject(cbinfo->screen, cbinfo->buttonId);
93
94 if (button && button->state == GFUI_DISABLE && !cbinfo->disabledTip.empty()) {
95 GfuiLabelSetText(cbinfo->screen, cbinfo->labelId, cbinfo->disabledTip.c_str());
96 static float disabledTipColor[4] = {1.0, 0.0, 0.0, 1.0};
97 GfuiLabelSetColor(cbinfo->screen, cbinfo->labelId, disabledTipColor);
98 } else {
99 GfuiLabelSetText(cbinfo->screen, cbinfo->labelId, cbinfo->tip.c_str());
100 GfuiLabelSetColor(cbinfo->screen, cbinfo->labelId, &(GfuiColor[GFUI_TIPCOLOR][0]));
101 }
102 GfuiVisibilitySet(cbinfo->screen, cbinfo->labelId, 1);
103}
104static void
106{
107 GfuiVisibilitySet(cbinfo->screen, cbinfo->labelId, 0);
108}
109
110
123int
124GfuiMenuButtonCreateActionWithDisabledTip(void *scr, const char *text, const char *tip, const char *disabledTip, const tGfuiAction &onpush)
125{
126 std::shared_ptr<tMnuCallbackInfo> cbinfo;
127 int xpos, ypos;
128 int nbItems = ((tGfuiScreen*)scr)->nbItems++;
129 int bId;
130
131 if (nbItems < 11) {
132 xpos = 320;
133 ypos = 380 - 30 * nbItems;
134 } else {
135 if (nbItems > 22) {
136 GfTrace("Too many items in that menu !!!\n");
137 return -1;
138 }
139 xpos = 380;
140 ypos = 380 - 30 * (nbItems - 11);
141 }
142
143 cbinfo = std::make_shared<tMnuCallbackInfo>();
144 cbinfo->screen = scr;
145 cbinfo->buttonId = -1;
146 cbinfo->tip = tip ? tip : "";
147 cbinfo->disabledTip = disabledTip ? disabledTip : "";
148 const int maxTipLen = (int)(cbinfo->tip.length() > cbinfo->disabledTip.length() ? cbinfo->tip.length() : cbinfo->disabledTip.length());
149 cbinfo->labelId = GfuiTipCreate(scr, cbinfo->tip.c_str(), maxTipLen);
150
151 GfuiVisibilitySet(scr, cbinfo->labelId, 0);
152
153 bId = GfuiButtonCreateAction(scr,
154 text,
156 xpos, ypos, GFUI_BTNSZ, GFUI_ALIGN_HC_VB, 0,
157 onpush,
158 [cbinfo] { dispInfo(cbinfo.get()); },
159 [cbinfo] { remInfo(cbinfo.get()); });
160 cbinfo->buttonId = bId;
161 return bId;
162}
163
164int
165GfuiMenuButtonCreateAction(void *scr, const char *text, const char *tip, const tGfuiAction &onpush)
166{
167 return GfuiMenuButtonCreateActionWithDisabledTip(scr, text, tip, NULL, onpush);
168}
169
170int
171GfuiMenuButtonCreate(void *scr, const char *text, const char *tip, void *userdata, tfuiCallback onpush)
172{
173 return GfuiMenuButtonCreateAction(scr, text, tip, gfuiMakeAction(userdata, onpush));
174}
175
186int
187GfuiMenuBackQuitButtonCreateAction(void *scr, const char *text, const char *tip, const tGfuiAction &onpush)
188{
189 std::shared_ptr<tMnuCallbackInfo> cbinfo;
190 int xpos, ypos;
191 int bId;
192
193 xpos = 320;
194 ypos = 40;
195
196 cbinfo = std::make_shared<tMnuCallbackInfo>();
197 cbinfo->screen = scr;
198 cbinfo->buttonId = -1;
199 cbinfo->tip = tip ? tip : "";
200 cbinfo->labelId = GfuiTipCreate(scr, cbinfo->tip.c_str(), (int)cbinfo->tip.length());
201
202 GfuiVisibilitySet(scr, cbinfo->labelId, 0);
203
204 bId = GfuiButtonCreateAction(scr,
205 text,
207 xpos, ypos, GFUI_BTNSZ, GFUI_ALIGN_HC_VB, 0,
208 onpush,
209 [cbinfo] { dispInfo(cbinfo.get()); },
210 [cbinfo] { remInfo(cbinfo.get()); });
211 cbinfo->buttonId = bId;
212
213 GfuiAddKeyAction(scr, (unsigned char)27, tip, onpush, tGfuiAction());
214
215 return bId;
216}
217
218int
219GfuiMenuBackQuitButtonCreate(void *scr, const char *text, const char *tip, void *userdata, tfuiCallback onpush)
220{
221 return GfuiMenuBackQuitButtonCreateAction(scr, text, tip, gfuiMakeAction(userdata, onpush));
222}
int GfuiMenuButtonCreateAction(void *scr, const char *text, const char *tip, const tGfuiAction &onpush)
Add a menu button with a focus tip.
Definition guimenu.cpp:165
void GfuiHelpScreen(void *prevScreen)
Generate a help screen.
Definition guihelp.cpp:47
void GfuiMenuDefaultKeysAdd(void *scr)
Add the default menu keyboard callback to a screen.
Definition guimenu.cpp:56
void GfuiLabelSetText(void *scr, int id, const char *text)
Change the text of a label.
Definition guilabel.cpp:193
int GfuiMenuButtonCreate(void *scr, const char *text, const char *tip, void *userdata, tfuiCallback onpush)
Add a menu button using a callback with caller-supplied data.
Definition guimenu.cpp:171
int GfuiTitleCreate(void *scr, const char *text, int maxlen)
Add a Title to the screen.
Definition guilabel.cpp:150
void GfuiAddKeyAction(void *scr, unsigned char key, const char *descr, const tGfuiAction &onKeyPressed, const tGfuiAction &onKeyReleased)
Add a Keyboard callback to a screen.
Definition gui.cpp:809
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.
void GfuiAddSKeyAction(void *scr, int key, const char *descr, const tGfuiAction &onKeyPressed, const tGfuiAction &onKeyReleased)
Add a Special Keyboard shortcut to the screen.
Definition gui.cpp:895
int GfuiMenuButtonCreateActionWithDisabledTip(void *scr, const char *text, const char *tip, const char *disabledTip, const tGfuiAction &onpush)
Add a button to a menu screen.
Definition guimenu.cpp:124
int GfuiMenuBackQuitButtonCreate(void *scr, const char *text, const char *tip, void *userdata, tfuiCallback onpush)
Add the bottom Back/Quit menu button using a callback with caller-supplied data.
Definition guimenu.cpp:219
int GfuiTipCreate(void *scr, const char *text, int maxlen)
Add a Tip (generally associated with a button).
Definition guilabel.cpp:135
void GfuiLabelSetColor(void *scr, int id, float *color)
Change the color of a label.
Definition guilabel.cpp:221
void * GfuiScreenCreate(void)
Create a new screen.
Definition gui.cpp:588
int GfuiMenuBackQuitButtonCreateAction(void *scr, const char *text, const char *tip, const tGfuiAction &onpush)
Add the "Back" or "Quit" button at the bottom of the menu screen.
Definition guimenu.cpp:187
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 GfuiScreenShot(void *)
Save a screen shot in png format.
Definition gui.cpp:998
tGfuiAction gfuiMakeAction(void *userData, tfuiCallback callback)
Definition gui.cpp:48
float GfuiColor[GFUI_COLORNB][4]
Definition gui.cpp:38
void gfuiSelectPrev(void *)
void gfuiSelectNext(void *)
struct GfuiObject tGfuiObject
#define GFUI_TIPCOLOR
Definition gui.h:38
tGfuiObject * gfuiGetObject(void *scr, int id)
void gfuiMouseAction(int action)
static void remInfo(const tMnuCallbackInfo *cbinfo)
Definition guimenu.cpp:105
static void dispInfo(const tMnuCallbackInfo *cbinfo)
Definition guimenu.cpp:90
void gfMenuInit(void)
Definition guimenu.cpp:38
int state
Definition gui.h:198
std::string disabledTip
Definition guimenu.h:32
void * screen
Definition guimenu.h:28
std::string tip
Definition guimenu.h:31
#define GfTrace
Definition tgf.h:335
The Gaming Framework API (client part).
void(* tfuiCallback)(void *)
Definition tgfclient.h:108
#define GFUI_FONT_LARGE
Definition tgfclient.h:248
std::function< void()> tGfuiAction
Definition tgfclient.h:107
#define GFUI_BTNSZ
Definition tgfclient.h:292
#define GFUI_ALIGN_HC_VB
Definition tgfclient.h:74
#define GFUI_DISABLE
Definition tgfclient.h:241