~ubuntu-branches/ubuntu/hardy/sauerbraten/hardy-backports

« back to all changes in this revision

Viewing changes to engine/menus.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bruno "Fuddl" Kleinert
  • Date: 2007-01-09 18:19:30 UTC
  • Revision ID: james.westby@ubuntu.com-20070109181930-zy2ulzq3ukfjhrix
Tags: upstream-0.0.20061204.dfsg
ImportĀ upstreamĀ versionĀ 0.0.20061204.dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// menus.cpp: ingame menu system (also used for scores and serverlist)
 
2
 
 
3
#include "pch.h"
 
4
#include "engine.h"
 
5
 
 
6
static vec menupos;
 
7
static int menustart = 0;
 
8
static int menutab = 1;
 
9
static g3d_gui *cgui = NULL;
 
10
 
 
11
static hashtable<char *, char *> guis;
 
12
static vector<char *> guistack;
 
13
static vector<char *> executelater;
 
14
static bool clearlater = false;
 
15
 
 
16
VARP(menudistance, 16, 40, 256);
 
17
 
 
18
vec menuinfrontofplayer() 
 
19
 
20
    vec dir;
 
21
    vecfromyawpitch(camera1->yaw, 0, 1, 0, dir, false);
 
22
    dir.mul(menudistance).add(camera1->o);
 
23
    dir.z -= player->eyeheight-1;
 
24
    return dir;
 
25
};
 
26
 
 
27
int cleargui(int n = 0)
 
28
{
 
29
    int clear = guistack.length();
 
30
    if(n>0) clear = min(clear, n);
 
31
    loopi(clear) delete[] guistack.pop();
 
32
    if(!guistack.empty()) showgui(guistack.last());
 
33
    return clear;
 
34
};
 
35
 
 
36
void cleargui_(int *n)
 
37
{
 
38
    intret(cleargui(*n));
 
39
};
 
40
 
 
41
#define GUI_TITLE_COLOR  0xFFDD88
 
42
#define GUI_BUTTON_COLOR 0xFFFFFF
 
43
#define GUI_TEXT_COLOR   0xDDFFDD
 
44
 
 
45
//@DOC name and icon are optional
 
46
void guibutton(char *name, char *action, char *icon)
 
47
{
 
48
    if(!cgui) return;
 
49
    int ret = cgui->button(name, GUI_BUTTON_COLOR, *icon ? icon : (strstr(action, "showgui") ? "menu" : "action"));
 
50
    if(ret&G3D_UP) 
 
51
    {
 
52
        executelater.add(newstring(*action ? action : name));
 
53
        clearlater = true;
 
54
    }
 
55
    else if(ret&G3D_ROLLOVER)
 
56
    {
 
57
        alias("guirollovername", name);
 
58
        alias("guirolloveraction", action);
 
59
    };
 
60
};
 
61
 
 
62
void guiimage(char *path, char *action, float *scale, int *overlaid)
 
63
{
 
64
    if(cgui && cgui->image(path, *scale, *overlaid!=0)&G3D_UP && *action)
 
65
    {
 
66
        executelater.add(newstring(action));
 
67
        clearlater = true;
 
68
    };
 
69
};
 
70
 
 
71
void guitext(char *name)
 
72
{
 
73
    if(cgui) cgui->text(name, GUI_TEXT_COLOR, "info");
 
74
};
 
75
 
 
76
void guititle(char *name)
 
77
{
 
78
    if(cgui) cgui->title(name, GUI_TITLE_COLOR);
 
79
};
 
80
 
 
81
void guitab(char *name)
 
82
{
 
83
    if(cgui) cgui->tab(name, GUI_TITLE_COLOR);
 
84
};
 
85
 
 
86
void guibar()
 
87
{
 
88
    if(cgui) cgui->separator();
 
89
};
 
90
 
 
91
static void updateval(char *var, int val, char *onchange)
 
92
{
 
93
    ident *id = getident(var);
 
94
    string assign;
 
95
    if(!id) return;
 
96
    else if(id->_type==ID_VAR) s_sprintf(assign)("%s %d", var, val);
 
97
    else if(id->_type==ID_ALIAS) s_sprintf(assign)("%s = %d", var, val);
 
98
    else return;
 
99
    executelater.add(newstring(assign));
 
100
    if(onchange[0]) executelater.add(newstring(onchange)); 
 
101
};
 
102
 
 
103
static int getval(char *var)
 
104
{
 
105
    ident *id = getident(var);
 
106
    if(!id) return 0;
 
107
    else if(id->_type==ID_VAR) return *id->_storage;
 
108
    else if(id->_type==ID_ALIAS) return atoi(id->_action);
 
109
    else return 0;
 
110
};
 
111
 
 
112
void guislider(char *var, int *min, int *max, char *onchange)
 
113
{
 
114
        if(!cgui) return;
 
115
    int oldval = getval(var), val = oldval, vmin = *max ? *min : getvarmin(var), vmax = *max ? *max : getvarmax(var);
 
116
    cgui->slider(val, vmin, vmax, GUI_TITLE_COLOR);
 
117
    if(val != oldval) updateval(var, val, onchange);
 
118
};
 
119
 
 
120
void guicheckbox(char *name, char *var, int *on, int *off, char *onchange)
 
121
{
 
122
    bool enabled = getval(var)!=*off;
 
123
    if(cgui && cgui->button(name, GUI_BUTTON_COLOR, enabled ? "checkbox_on" : "checkbox_off")&G3D_UP)
 
124
    {
 
125
        updateval(var, enabled ? *off : (*on || *off ? *on : 1), onchange);
 
126
    };
 
127
};
 
128
 
 
129
void guiradio(char *name, char *var, int *n, char *onchange)
 
130
{
 
131
    bool enabled = getval(var)==*n;
 
132
    if(cgui && cgui->button(name, GUI_BUTTON_COLOR, enabled ? "radio_on" : "radio_off")&G3D_UP)
 
133
    {
 
134
        if(!enabled) updateval(var, *n, onchange);
 
135
    };
 
136
};
 
137
 
 
138
void guilist(char *contents)
 
139
{
 
140
    if(!cgui) return;
 
141
    cgui->pushlist();
 
142
    execute(contents);
 
143
    cgui->poplist();
 
144
};
 
145
 
 
146
void newgui(char *name, char *contents) 
 
147
 
148
    if(guis.access(name))
 
149
    {
 
150
        delete[] guis[name];
 
151
        guis[name] = newstring(contents);
 
152
    }
 
153
    else guis[newstring(name)] = newstring(contents); 
 
154
};
 
155
 
 
156
void showgui(char *name)
 
157
{
 
158
    int pos = guistack.find(name);
 
159
    if(pos<0) 
 
160
    {   
 
161
        if(!guis.access(name)) return;
 
162
        if(guistack.empty()) menupos = menuinfrontofplayer();
 
163
        guistack.add(newstring(name));
 
164
    }
 
165
    else
 
166
    {
 
167
        pos = guistack.length()-pos-1;
 
168
        loopi(pos) delete[] guistack.pop();
 
169
    };
 
170
    menutab = 1;
 
171
    menustart = lastmillis;    
 
172
};
 
173
 
 
174
void guiservers()
 
175
{
 
176
    extern const char *showservers(g3d_gui *cgui);
 
177
    if(cgui) 
 
178
    {
 
179
        const char *name = showservers(cgui); 
 
180
        if(name)
 
181
        {
 
182
            s_sprintfd(connect)("connect %s", name);
 
183
            executelater.add(newstring(connect));
 
184
            clearlater = true;
 
185
        };
 
186
    };
 
187
};
 
188
 
 
189
COMMAND(newgui, "ss");
 
190
COMMAND(guibutton, "sss");
 
191
COMMAND(guitext, "s");
 
192
COMMAND(guiservers, "s");
 
193
COMMANDN(cleargui, cleargui_, "i");
 
194
COMMAND(showgui, "s");
 
195
 
 
196
COMMAND(guilist, "s");
 
197
COMMAND(guititle, "s");
 
198
COMMAND(guibar,"");
 
199
COMMAND(guiimage,"ssfi");
 
200
COMMAND(guislider,"siis");
 
201
COMMAND(guiradio,"ssis");
 
202
COMMAND(guicheckbox, "ssiis");
 
203
COMMAND(guitab, "s");
 
204
 
 
205
static struct mainmenucallback : g3d_callback
 
206
{
 
207
    void gui(g3d_gui &g, bool firstpass)
 
208
    {
 
209
        if(guistack.empty()) return;
 
210
        char *name = guistack.last();
 
211
        char **contents = guis.access(name);
 
212
        if(!contents) return;
 
213
                cgui = &g;
 
214
        cgui->start(menustart, 0.03f, &menutab);
 
215
                guitab(name);           
 
216
                execute(*contents);
 
217
        cgui->end();
 
218
                cgui = NULL;
 
219
    };
 
220
} mmcb;
 
221
 
 
222
void menuprocess()
 
223
{
 
224
    int level = guistack.length();
 
225
    loopv(executelater) execute(executelater[i]);
 
226
    executelater.deletecontentsa();
 
227
    if(clearlater)
 
228
    {
 
229
        if(level==guistack.length()) guistack.deletecontentsa();
 
230
        clearlater = false;
 
231
    };
 
232
};
 
233
 
 
234
void g3d_mainmenu()
 
235
{
 
236
    if(!guistack.empty()) 
 
237
    {   
 
238
        if(camera1->o.dist(menupos) > menudistance*3) cleargui();
 
239
        else g3d_addgui(&mmcb, menupos);
 
240
    };
 
241
};
 
242