2
0
mirror of https://github.com/FWGS/hlsdk-xash3d synced 2024-11-22 01:47:45 +01:00
hlsdk-xash3d/cl_dll/menu.cpp

197 lines
4.6 KiB
C++
Raw Normal View History

2016-06-04 15:24:23 +02:00
/***
*
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
//
// menu.cpp
//
// generic menu handler
//
2016-07-03 15:39:55 +02:00
2016-06-04 15:24:23 +02:00
#include "hud.h"
#include "cl_util.h"
#include "parsemsg.h"
#include <string.h>
#include <stdio.h>
#if USE_VGUI
#include "vgui_TeamFortressViewport.h"
#endif
2016-06-04 15:24:23 +02:00
#define MAX_MENU_STRING 512
char g_szMenuString[MAX_MENU_STRING];
char g_szPrelocalisedMenuString[MAX_MENU_STRING];
int KB_ConvertString( char *in, char **ppout );
DECLARE_MESSAGE( m_Menu, ShowMenu )
2016-06-04 15:24:23 +02:00
2016-07-03 15:39:55 +02:00
int CHudMenu::Init( void )
2016-06-04 15:24:23 +02:00
{
gHUD.AddHudElem( this );
HOOK_MESSAGE( ShowMenu );
InitHUDData();
return 1;
}
2016-07-03 15:39:55 +02:00
void CHudMenu::InitHUDData( void )
2016-06-04 15:24:23 +02:00
{
m_fMenuDisplayed = 0;
m_bitsValidSlots = 0;
Reset();
}
2016-07-03 15:39:55 +02:00
void CHudMenu::Reset( void )
2016-06-04 15:24:23 +02:00
{
g_szPrelocalisedMenuString[0] = 0;
m_fWaitingForMore = FALSE;
}
2016-07-03 15:39:55 +02:00
int CHudMenu::VidInit( void )
2016-06-04 15:24:23 +02:00
{
return 1;
}
2016-07-03 15:39:55 +02:00
int CHudMenu::Draw( float flTime )
2016-06-04 15:24:23 +02:00
{
2016-04-17 22:30:05 +02:00
int i;
2016-06-04 15:24:23 +02:00
// check for if menu is set to disappear
2016-07-03 15:39:55 +02:00
if( m_flShutoffTime > 0 )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( m_flShutoffTime <= gHUD.m_flTime )
{
// times up, shutoff
2016-06-04 15:24:23 +02:00
m_fMenuDisplayed = 0;
m_iFlags &= ~HUD_ACTIVE;
return 1;
}
}
// don't draw the menu if the scoreboard is being shown
#if USE_VGUI
if( gViewPort && gViewPort->IsScoreBoardVisible() )
return 1;
#endif
2016-06-04 15:24:23 +02:00
// draw the menu, along the left-hand side of the screen
// count the number of newlines
int nlc = 0;
2016-07-03 15:39:55 +02:00
for( i = 0; i < MAX_MENU_STRING && g_szMenuString[i] != '\0'; i++ )
2016-06-04 15:24:23 +02:00
{
if( g_szMenuString[i] == '\n' )
2016-06-04 15:24:23 +02:00
nlc++;
}
// center it
2016-07-03 15:39:55 +02:00
int y = ( ScreenHeight / 2 ) - ( ( nlc / 2 ) * 12 ) - 40; // make sure it is above the say text
2016-06-04 15:24:23 +02:00
int x = 20;
i = 0;
2016-07-03 15:39:55 +02:00
while( i < MAX_MENU_STRING && g_szMenuString[i] != '\0' )
2016-06-04 15:24:23 +02:00
{
gHUD.DrawHudString( x, y, 320, g_szMenuString + i, 255, 255, 255 );
y += 12;
2016-07-03 15:39:55 +02:00
while( i < MAX_MENU_STRING && g_szMenuString[i] != '\0' && g_szMenuString[i] != '\n' )
2016-06-04 15:24:23 +02:00
i++;
2016-07-03 15:39:55 +02:00
if( g_szMenuString[i] == '\n' )
2016-06-04 15:24:23 +02:00
i++;
}
2016-07-03 15:39:55 +02:00
2016-06-04 15:24:23 +02:00
return 1;
}
// selects an item from the menu
2016-07-03 15:39:55 +02:00
void CHudMenu::SelectMenuItem( int menu_item )
2016-06-04 15:24:23 +02:00
{
// if menu_item is in a valid slot, send a menuselect command to the server
2016-07-03 15:39:55 +02:00
if( ( menu_item > 0 ) && ( m_bitsValidSlots & ( 1 << ( menu_item - 1 ) ) ) )
2016-06-04 15:24:23 +02:00
{
char szbuf[32];
sprintf( szbuf, "menuselect %d\n", menu_item );
ClientCmd( szbuf );
// remove the menu
m_fMenuDisplayed = 0;
m_iFlags &= ~HUD_ACTIVE;
}
}
// Message handler for ShowMenu message
// takes four values:
// short: a bitfield of keys that are valid input
// char : the duration, in seconds, the menu should stay up. -1 means is stays until something is chosen.
// byte : a boolean, TRUE if there is more string yet to be received before displaying the menu, FALSE if it's the last string
// string: menu string to display
// if this message is never received, then scores will simply be the combined totals of the players.
2016-07-03 15:39:55 +02:00
int CHudMenu::MsgFunc_ShowMenu( const char *pszName, int iSize, void *pbuf )
2016-06-04 15:24:23 +02:00
{
char *temp = NULL;
BEGIN_READ( pbuf, iSize );
m_bitsValidSlots = READ_SHORT();
int DisplayTime = READ_CHAR();
int NeedMore = READ_BYTE();
2016-07-03 15:39:55 +02:00
if( DisplayTime > 0 )
2016-06-04 15:24:23 +02:00
m_flShutoffTime = DisplayTime + gHUD.m_flTime;
else
m_flShutoffTime = -1;
2016-07-03 15:39:55 +02:00
if( m_bitsValidSlots )
2016-06-04 15:24:23 +02:00
{
2016-07-03 15:39:55 +02:00
if( !m_fWaitingForMore ) // this is the start of a new menu
2016-06-04 15:24:23 +02:00
{
2022-11-14 04:47:47 +01:00
strncpy( g_szPrelocalisedMenuString, READ_STRING(), MAX_MENU_STRING - 1 );
2016-06-04 15:24:23 +02:00
}
else
2016-07-03 15:39:55 +02:00
{
// append to the current menu string
strncat( g_szPrelocalisedMenuString, READ_STRING(), MAX_MENU_STRING - strlen( g_szPrelocalisedMenuString ) - 1 );
2016-06-04 15:24:23 +02:00
}
2016-07-03 15:39:55 +02:00
g_szPrelocalisedMenuString[MAX_MENU_STRING - 1] = 0; // ensure null termination (strncat/strncpy does not)
2016-06-04 15:24:23 +02:00
2016-07-03 15:39:55 +02:00
if( !NeedMore )
{
// we have the whole string, so we can localise it now
2022-11-14 04:47:47 +01:00
strncpy( g_szMenuString, gHUD.m_TextMessage.BufferedLocaliseTextString( g_szPrelocalisedMenuString ), MAX_MENU_STRING - 1 );
g_szMenuString[MAX_MENU_STRING - 1] = '\0';
2016-06-04 15:24:23 +02:00
// Swap in characters
2016-07-03 15:39:55 +02:00
if( KB_ConvertString( g_szMenuString, &temp ) )
2016-06-04 15:24:23 +02:00
{
2022-11-14 04:47:47 +01:00
strncpy( g_szMenuString, temp, MAX_MENU_STRING - 1 );
g_szMenuString[MAX_MENU_STRING - 1] = '\0';
2016-06-04 15:24:23 +02:00
free( temp );
}
}
m_fMenuDisplayed = 1;
m_iFlags |= HUD_ACTIVE;
}
else
{
m_fMenuDisplayed = 0; // no valid slots means that the menu should be turned off
m_iFlags &= ~HUD_ACTIVE;
}
m_fWaitingForMore = NeedMore;
return 1;
}