Reformat menu.c

This commit is contained in:
Yamagi Burmeister 2011-10-15 09:05:55 +00:00
parent 3ebbe127d2
commit 55c85f474e

View file

@ -1,38 +1,50 @@
/* /*
Copyright (C) 1997-2001 Id Software, Inc. * Copyright (C) 1997-2001 Id Software, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
* =======================================================================
*
* The CTF ingame menu.
*
* =======================================================================
*/
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../header/local.h" #include "../header/local.h"
// Note that the pmenu entries are duplicated /* Note that the pmenu entries are duplicated
// this is so that a static set of pmenu entries can be used this is so that a static set of pmenu entries can be used
// for multiple clients and changed without interference for multiple clients and changed without interference
// note that arg will be freed when the menu is closed, it must be allocated memory note that arg will be freed when the menu is closed, it
pmenuhnd_t *PMenu_Open(edict_t *ent, pmenu_t *entries, int cur, int num, void *arg) must be allocated memory */
pmenuhnd_t *
PMenu_Open(edict_t *ent, pmenu_t *entries, int cur, int num, void *arg)
{ {
pmenuhnd_t *hnd; pmenuhnd_t *hnd;
pmenu_t *p; pmenu_t *p;
int i; int i;
if (!ent->client) if (!ent->client)
{
return NULL; return NULL;
}
if (ent->client->menu) { if (ent->client->menu)
{
gi.dprintf("warning, ent already has a menu\n"); gi.dprintf("warning, ent already has a menu\n");
PMenu_Close(ent); PMenu_Close(ent);
} }
@ -42,66 +54,104 @@ pmenuhnd_t *PMenu_Open(edict_t *ent, pmenu_t *entries, int cur, int num, void *a
hnd->arg = arg; hnd->arg = arg;
hnd->entries = malloc(sizeof(pmenu_t) * num); hnd->entries = malloc(sizeof(pmenu_t) * num);
memcpy(hnd->entries, entries, sizeof(pmenu_t) * num); memcpy(hnd->entries, entries, sizeof(pmenu_t) * num);
// duplicate the strings since they may be from static memory
/* duplicate the strings since they may be from static memory */
for (i = 0; i < num; i++) for (i = 0; i < num; i++)
{
if (entries[i].text) if (entries[i].text)
{
hnd->entries[i].text = strdup(entries[i].text); hnd->entries[i].text = strdup(entries[i].text);
}
}
hnd->num = num; hnd->num = num;
if (cur < 0 || !entries[cur].SelectFunc) { if ((cur < 0) || !entries[cur].SelectFunc)
{
for (i = 0, p = entries; i < num; i++, p++) for (i = 0, p = entries; i < num; i++, p++)
{
if (p->SelectFunc) if (p->SelectFunc)
{
break; break;
} else }
}
}
else
{
i = cur; i = cur;
}
if (i >= num) if (i >= num)
{
hnd->cur = -1; hnd->cur = -1;
}
else else
{
hnd->cur = i; hnd->cur = i;
}
ent->client->showscores = true; ent->client->showscores = true;
ent->client->inmenu = true; ent->client->inmenu = true;
ent->client->menu = hnd; ent->client->menu = hnd;
PMenu_Do_Update(ent); PMenu_Do_Update(ent);
gi.unicast (ent, true); gi.unicast(ent, true);
return hnd; return hnd;
} }
void PMenu_Close(edict_t *ent) void
PMenu_Close(edict_t *ent)
{ {
int i; int i;
pmenuhnd_t *hnd; pmenuhnd_t *hnd;
if (!ent->client->menu) if (!ent->client->menu)
{
return; return;
}
hnd = ent->client->menu; hnd = ent->client->menu;
for (i = 0; i < hnd->num; i++) for (i = 0; i < hnd->num; i++)
{
if (hnd->entries[i].text) if (hnd->entries[i].text)
{
free(hnd->entries[i].text); free(hnd->entries[i].text);
}
}
free(hnd->entries); free(hnd->entries);
if (hnd->arg) if (hnd->arg)
{
free(hnd->arg); free(hnd->arg);
}
free(hnd); free(hnd);
ent->client->menu = NULL; ent->client->menu = NULL;
ent->client->showscores = false; ent->client->showscores = false;
} }
// only use on pmenu's that have been called with PMenu_Open /*
void PMenu_UpdateEntry(pmenu_t *entry, const char *text, int align, SelectFunc_t SelectFunc) * Only use on pmenu's that have been called with PMenu_Open
*/
void
PMenu_UpdateEntry(pmenu_t *entry, const char *text, int align,
SelectFunc_t SelectFunc)
{ {
if (entry->text) if (entry->text)
{
free(entry->text); free(entry->text);
}
entry->text = strdup(text); entry->text = strdup(text);
entry->align = align; entry->align = align;
entry->SelectFunc = SelectFunc; entry->SelectFunc = SelectFunc;
} }
void PMenu_Do_Update(edict_t *ent) void
PMenu_Do_Update(edict_t *ent)
{ {
char string[1400]; char string[1400];
int i; int i;
@ -111,7 +161,8 @@ void PMenu_Do_Update(edict_t *ent)
char *t; char *t;
qboolean alt = false; qboolean alt = false;
if (!ent->client->menu) { if (!ent->client->menu)
{
gi.dprintf("warning: ent has no menu\n"); gi.dprintf("warning: ent has no menu\n");
return; return;
} }
@ -120,63 +171,89 @@ void PMenu_Do_Update(edict_t *ent)
strcpy(string, "xv 32 yv 8 picn inventory "); strcpy(string, "xv 32 yv 8 picn inventory ");
for (i = 0, p = hnd->entries; i < hnd->num; i++, p++) { for (i = 0, p = hnd->entries; i < hnd->num; i++, p++)
{
if (!p->text || !*(p->text)) if (!p->text || !*(p->text))
continue; // blank line {
continue; /* blank line */
}
t = p->text; t = p->text;
if (*t == '*') {
if (*t == '*')
{
alt = true; alt = true;
t++; t++;
} }
sprintf(string + strlen(string), "yv %d ", 32 + i * 8);
if (p->align == PMENU_ALIGN_CENTER)
x = 196/2 - strlen(t)*4 + 64;
else if (p->align == PMENU_ALIGN_RIGHT)
x = 64 + (196 - strlen(t)*8);
else
x = 64;
sprintf(string + strlen(string), "xv %d ", sprintf(string + strlen(string), "yv %d ", 32 + i * 8);
x - ((hnd->cur == i) ? 8 : 0));
if (p->align == PMENU_ALIGN_CENTER)
{
x = 196 / 2 - strlen(t) * 4 + 64;
}
else if (p->align == PMENU_ALIGN_RIGHT)
{
x = 64 + (196 - strlen(t) * 8);
}
else
{
x = 64;
}
sprintf(string + strlen(string), "xv %d ", x - ((hnd->cur == i) ? 8 : 0));
if (hnd->cur == i) if (hnd->cur == i)
{
sprintf(string + strlen(string), "string2 \"\x0d%s\" ", t); sprintf(string + strlen(string), "string2 \"\x0d%s\" ", t);
}
else if (alt) else if (alt)
{
sprintf(string + strlen(string), "string2 \"%s\" ", t); sprintf(string + strlen(string), "string2 \"%s\" ", t);
}
else else
{
sprintf(string + strlen(string), "string \"%s\" ", t); sprintf(string + strlen(string), "string \"%s\" ", t);
}
alt = false; alt = false;
} }
gi.WriteByte (svc_layout); gi.WriteByte(svc_layout);
gi.WriteString (string); gi.WriteString(string);
} }
void PMenu_Update(edict_t *ent) void
PMenu_Update(edict_t *ent)
{ {
if (!ent->client->menu) { if (!ent->client->menu)
{
gi.dprintf("warning: ent has no menu\n"); gi.dprintf("warning: ent has no menu\n");
return; return;
} }
if (level.time - ent->client->menutime >= 1.0) { if (level.time - ent->client->menutime >= 1.0)
// been a second or more since last update, update now {
/* been a second or more since last update, update now */
PMenu_Do_Update(ent); PMenu_Do_Update(ent);
gi.unicast (ent, true); gi.unicast(ent, true);
ent->client->menutime = level.time; ent->client->menutime = level.time;
ent->client->menudirty = false; ent->client->menudirty = false;
} }
ent->client->menutime = level.time + 0.2; ent->client->menutime = level.time + 0.2;
ent->client->menudirty = true; ent->client->menudirty = true;
} }
void PMenu_Next(edict_t *ent) void
PMenu_Next(edict_t *ent)
{ {
pmenuhnd_t *hnd; pmenuhnd_t *hnd;
int i; int i;
pmenu_t *p; pmenu_t *p;
if (!ent->client->menu) { if (!ent->client->menu)
{
gi.dprintf("warning: ent has no menu\n"); gi.dprintf("warning: ent has no menu\n");
return; return;
} }
@ -184,30 +261,43 @@ void PMenu_Next(edict_t *ent)
hnd = ent->client->menu; hnd = ent->client->menu;
if (hnd->cur < 0) if (hnd->cur < 0)
return; // no selectable entries {
return; /* no selectable entries */
}
i = hnd->cur; i = hnd->cur;
p = hnd->entries + hnd->cur; p = hnd->entries + hnd->cur;
do {
do
{
i++, p++; i++, p++;
if (i == hnd->num) if (i == hnd->num)
{
i = 0, p = hnd->entries; i = 0, p = hnd->entries;
}
if (p->SelectFunc) if (p->SelectFunc)
{
break; break;
} while (i != hnd->cur); }
}
while (i != hnd->cur);
hnd->cur = i; hnd->cur = i;
PMenu_Update(ent); PMenu_Update(ent);
} }
void PMenu_Prev(edict_t *ent) void
PMenu_Prev(edict_t *ent)
{ {
pmenuhnd_t *hnd; pmenuhnd_t *hnd;
int i; int i;
pmenu_t *p; pmenu_t *p;
if (!ent->client->menu) { if (!ent->client->menu)
{
gi.dprintf("warning: ent has no menu\n"); gi.dprintf("warning: ent has no menu\n");
return; return;
} }
@ -215,31 +305,45 @@ void PMenu_Prev(edict_t *ent)
hnd = ent->client->menu; hnd = ent->client->menu;
if (hnd->cur < 0) if (hnd->cur < 0)
return; // no selectable entries {
return; /* no selectable entries */
}
i = hnd->cur; i = hnd->cur;
p = hnd->entries + hnd->cur; p = hnd->entries + hnd->cur;
do {
if (i == 0) { do
{
if (i == 0)
{
i = hnd->num - 1; i = hnd->num - 1;
p = hnd->entries + i; p = hnd->entries + i;
} else }
else
{
i--, p--; i--, p--;
}
if (p->SelectFunc) if (p->SelectFunc)
{
break; break;
} while (i != hnd->cur); }
}
while (i != hnd->cur);
hnd->cur = i; hnd->cur = i;
PMenu_Update(ent); PMenu_Update(ent);
} }
void PMenu_Select(edict_t *ent) void
PMenu_Select(edict_t *ent)
{ {
pmenuhnd_t *hnd; pmenuhnd_t *hnd;
pmenu_t *p; pmenu_t *p;
if (!ent->client->menu) { if (!ent->client->menu)
{
gi.dprintf("warning: ent has no menu\n"); gi.dprintf("warning: ent has no menu\n");
return; return;
} }
@ -247,11 +351,15 @@ void PMenu_Select(edict_t *ent)
hnd = ent->client->menu; hnd = ent->client->menu;
if (hnd->cur < 0) if (hnd->cur < 0)
return; // no selectable entries {
return; /* no selectable entries */
}
p = hnd->entries + hnd->cur; p = hnd->entries + hnd->cur;
if (p->SelectFunc) if (p->SelectFunc)
{
p->SelectFunc(ent, hnd); p->SelectFunc(ent, hnd);
}
} }