-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathLoadGame.cpp
More file actions
507 lines (416 loc) · 11.8 KB
/
Copy pathLoadGame.cpp
File metadata and controls
507 lines (416 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
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.
*/
#include "Framework.h"
#include "keydefs.h"
#include "Bitmap.h"
#include "PicButton.h"
#include "Table.h"
#include "Action.h"
#include "YesNoMessageBox.h"
#define ART_BANNER_LOAD "gfx/shell/head_load"
#define ART_BANNER_SAVE "gfx/shell/head_save"
#define LEVELSHOT_X 72
#define LEVELSHOT_Y 400
#define LEVELSHOT_W 260
#define LEVELSHOT_H 160
#define MAX_CELLSTRING CS_SIZE
class CMenuLoadGame;
struct save_t
{
char name[CS_SIZE];
private:
friend class CMenuSavesListModel;
char date[CS_SIZE];
char comment[256];
char elapsed_time[CS_SIZE];
};
/*
============
COM_CompareSaves
============
*/
static int COM_CompareSaves( const void *a, const void *b )
{
const char *file1 = *((const char **)a);
const char *file2 = *((const char **)b);
int bResult = 0;
EngFuncs::CompareFileTime( file2, file1, &bResult );
return bResult;
}
class CMenuSavePreview : public CMenuBaseItem
{
public:
CMenuSavePreview() : CMenuBaseItem(), fallback( "{GRAF001" )
{
iFlags = QMF_INACTIVE;
}
void Draw() override;
void SetSaveName( const char *name )
{
if( name == nullptr )
saveshot.ForceUnload();
else
{
char path[128];
snprintf( path, sizeof( path ), "save/%s.bmp", name );
saveshot.ForceUnload();
if( EngFuncs::FileExists( path, true ) )
saveshot.Load( path );
if( saveshot.IsValid( ))
{
double w = EngFuncs::PIC_Width( saveshot.Handle( ));
double h = EngFuncs::PIC_Height( saveshot.Handle( ));
size.h = round( size.w / ( w / h ));
CalcSizes();
}
}
}
CImage fallback;
CImage saveshot;
};
class CMenuSavesListModel : public CMenuBaseModel, public CUtlVector<save_t>
{
public:
CMenuSavesListModel( CMenuLoadGame *parent ) : parent( parent ) { }
void Update() override;
int GetColumns() const override
{
// time, name, gametime
return 3;
}
int GetRows() const override
{
return Count();
}
const char *GetCellText( int line, int column ) override
{
switch( column )
{
case 0: return Element( line ).date;
case 1: return Element( line ).comment;
case 2: return Element( line ).elapsed_time;
}
ASSERT( 0 );
return NULL;
}
unsigned int GetAlignmentForColumn(int column) const override
{
return column == 2 ? QM_RIGHT : QM_LEFT;
}
void OnDeleteEntry( int line ) override;
private:
CMenuLoadGame *parent;
};
class CMenuLoadGame : public CMenuFramework
{
public:
CMenuLoadGame() : CMenuFramework( "CMenuLoadGame" ), savesListModel( this ) { }
// true to turn this menu into save mode, false to turn into load mode
void SetSaveMode( bool saveMode );
bool IsSaveMode() { return m_fSaveMode; }
void UpdateList() { savesListModel.Update(); }
private:
void _Init( void );
void LoadGame();
void SaveGame();
void UpdateGame();
void DeleteGame();
CMenuPicButton load;
CMenuPicButton save;
CMenuPicButton remove;
CMenuPicButton cancel;
CMenuTable savesList;
CMenuSavePreview levelShot;
bool m_fSaveMode;
char hintText[MAX_HINT_TEXT];
// prompt dialog
CMenuYesNoMessageBox msgBox;
CMenuSavesListModel savesListModel;
friend class CMenuSavesListModel;
};
void CMenuSavePreview::Draw()
{
if( saveshot.IsValid( ))
UI_DrawPic( m_scPos, m_scSize, uiColorWhite, saveshot );
else
UI_DrawPic( m_scPos, m_scSize, uiColorWhite, fallback, QM_DRAWADDITIVE );
// draw the rectangle
UI_DrawRectangle( m_scPos, m_scSize, uiInputFgColor );
}
/*
=================
CMenuSavesListModel::Update
=================
*/
void CMenuSavesListModel::Update( void )
{
char **filenames;
int numFiles;
RemoveAll();
filenames = EngFuncs::GetFilesList( "save/*.sav", &numFiles, true );
// sort the saves in reverse order (oldest past at the end)
qsort( filenames, numFiles, sizeof( *filenames ), COM_CompareSaves );
if( parent->IsSaveMode( ) && CL_IsActive( ))
{
// create new entry for current save game
save_t save;
Q_strncpy( save.name, "new", sizeof( save.name )); // special name, handled in SV_Save_f
Q_strncpy( save.date, L( "GameUI_SaveGame_Current" ), sizeof( save.date ));
Q_strncpy( save.comment, L( "GameUI_SaveGame_NewSavedGame" ), sizeof( save.comment ));
Q_strncpy( save.elapsed_time, L( "GameUI_SaveGame_New" ), sizeof( save.elapsed_time ));
AddToTail( save );
}
for( int i = 0; i < numFiles; i++ )
{
save_t save;
char comment[256];
char time[CS_TIME];
char date[CS_TIME];
// strip path, leave only filename (empty slots doesn't have savename)
COM_FileBase( filenames[i], save.name, sizeof( save.name ));
if( !EngFuncs::GetSaveComment( filenames[i], comment ))
{
if( comment[0] )
{
// get name string even if not found - SV_GetComment can be mark saves
// as <CORRUPTED> <OLD VERSION> etc
Q_strncpy( save.date, comment, sizeof( save.date ));
save.elapsed_time[0] = 0;
save.comment[0] = 0;
AddToTail( save );
}
continue;
}
// they are defined by comment string format
// time and date
Q_strncpy( time, comment + CS_SIZE, CS_TIME );
Q_strncpy( date, comment + CS_SIZE + CS_TIME, CS_TIME );
snprintf( save.date, sizeof( save.date ), "%s %s", time, date );
// ingame time
Q_strncpy( save.elapsed_time, comment + CS_SIZE + CS_TIME * 2, sizeof( save.elapsed_time ));
char *title, *type, *p;
type = p = nullptr;
// we need real title
// so search for square brackets
if( comment[0] == '[' && ( p = strchr( comment, ']' )))
{
type = comment + 1; // this might be "autosave", "quick", etc...
title = p + 1; // this is a title
}
else title = comment;
if( title[0] == '#' )
{
char s[CS_SIZE];
// remove the second ], we don't need it to concatenate
if( p )
*p = 0;
// strip everything after first space, assume translatable save titles have no space
p = strchr( title, ' ' );
if( p )
*p = 0;
Q_strncpy( s, title, sizeof( s ));
if( type )
{
// Localize known save-type identifiers (quick/autosave). Otherwise, fall back to original behavior.
if( !stricmp( type, "quick" ))
snprintf( save.comment, sizeof( save.comment ), "%s%s", L( "GameUI_QuickSave" ), L( s ));
else if( !stricmp( type, "autosave" ))
snprintf( save.comment, sizeof( save.comment ), "%s%s", L( "GameUI_AutoSave" ), L( s ));
else
snprintf( save.comment, sizeof( save.comment ), "[%.16s]%s", type, L( s ));
}
else Q_strncpy( save.comment, L( s ), sizeof( save.comment ));
}
else
{
// strip whitespace from the end of string
for( size_t len = strlen( title ) - 1; len >= 0; len-- )
{
if( !isspace( title[len] ))
break;
title[len] = '\0';
}
Q_strncpy( save.comment, comment, sizeof( save.comment ));
}
AddToTail( save );
}
if( !parent->IsSaveMode( ))
{
parent->load.SetGrayed( !IsValidIndex( 0 ) );
}
else
{
parent->save.SetGrayed( !IsValidIndex( 0 ) || !CL_IsActive( ));
}
parent->remove.SetGrayed( !IsValidIndex( 0 ));
parent->UpdateGame();
}
void CMenuSavesListModel::OnDeleteEntry( int line )
{
parent->msgBox.Show();
}
/*
=================
UI_LoadGame_Init
=================
*/
void CMenuLoadGame::_Init( void )
{
save.szName = L( "GameUI_Save" );
save.SetPicture( PC_SAVE_GAME );
save.onReleased = VoidCb( &CMenuLoadGame::SaveGame );
save.SetCoord( 72, 230 );
load.szName = L( "GameUI_Load" );
load.SetPicture( PC_LOAD_GAME );
load.onReleased = VoidCb( &CMenuLoadGame::LoadGame );
load.SetCoord( 72, 230 );
remove.szName = L( "Delete" );
remove.SetPicture( PC_DELETE );
remove.onReleased = msgBox.MakeOpenEvent();
remove.SetCoord( 72, 280 );
cancel.szName = L( "GameUI_Cancel" );
cancel.SetPicture( PC_CANCEL );
cancel.onReleased = VoidCb( &CMenuLoadGame::Hide );
cancel.SetCoord( 72, 330 );
savesList.szName = hintText;
savesList.onChanged = VoidCb( &CMenuLoadGame::UpdateGame );
savesList.SetupColumn( 0, L( "GameUI_Time" ), 0.30f );
savesList.SetupColumn( 1, L( "GameUI_Game" ), 0.55f );
savesList.SetupColumn( 2, L( "GameUI_ElapsedTime" ), 0.15f );
savesList.SetModel( &savesListModel );
savesList.SetCharSize( QM_SMALLFONT );
savesList.SetRect( 360, 230, -20, 465 );
msgBox.SetMessage( L( "Delete this saved game?" ) );
msgBox.onPositive = VoidCb( &CMenuLoadGame::DeleteGame );
msgBox.Link( this );
levelShot.SetRect( LEVELSHOT_X, LEVELSHOT_Y, LEVELSHOT_W, LEVELSHOT_H );
AddItem( banner );
AddItem( load );
AddItem( save );
AddItem( remove );
AddItem( cancel );
AddItem( levelShot );
AddItem( savesList );
}
void CMenuLoadGame::LoadGame()
{
if( !savesListModel.IsValidIndex( savesList.GetCurrentIndex( )))
return;
char cmd[128];
const char *name = savesListModel[savesList.GetCurrentIndex( )].name;
snprintf( cmd, sizeof( cmd ), "load \"%s\"\n", name );
EngFuncs::StopBackgroundTrack( );
EngFuncs::ClientCmd( false, cmd );
UI_CloseMenu();
}
void CMenuLoadGame::SaveGame()
{
if( !savesListModel.IsValidIndex( savesList.GetCurrentIndex( )))
return;
char cmd[128];
const char *name = savesListModel[savesList.GetCurrentIndex( )].name;
snprintf( cmd, sizeof( cmd ), "save/%s.bmp", name );
EngFuncs::PIC_Free( cmd );
snprintf( cmd, sizeof( cmd ), "save \"%s\"\n", name );
EngFuncs::ClientCmd( false, cmd );
UI_CloseMenu();
}
void CMenuLoadGame::UpdateGame()
{
// first item is for creating new saves
if(( IsSaveMode() && savesList.GetCurrentIndex() == 0 ) || !savesListModel.IsValidIndex( savesList.GetCurrentIndex( )))
{
remove.SetGrayed( true );
levelShot.SetSaveName( nullptr );
}
else
{
remove.SetGrayed( false );
levelShot.SetSaveName( savesListModel[savesList.GetCurrentIndex( )].name );
}
}
void CMenuLoadGame::DeleteGame()
{
if( !savesListModel.IsValidIndex( savesList.GetCurrentIndex( )))
return;
char cmd[128];
const char *name = savesListModel[savesList.GetCurrentIndex( )].name;
snprintf( cmd, sizeof( cmd ), "killsave \"%s\"\n", name );
EngFuncs::ClientCmd( true, cmd );
snprintf( cmd, sizeof( cmd ), "save/%s.bmp", name );
EngFuncs::PIC_Free( cmd );
savesListModel.Update();
}
void CMenuLoadGame::SetSaveMode( bool saveMode )
{
m_fSaveMode = saveMode;
if( saveMode )
{
banner.SetPicture( ART_BANNER_SAVE );
save.SetVisibility( true );
load.SetVisibility( false );
szName = "CMenuSaveGame";
}
else
{
banner.SetPicture( ART_BANNER_LOAD );
save.SetVisibility( false );
load.SetVisibility( true );
szName = "CMenuLoadGame";
}
}
static CMenuLoadGame *menu_loadgame = NULL;
/*
=================
UI_LoadGame_Precache
=================
*/
void UI_LoadSaveGame_Precache( void )
{
menu_loadgame = new CMenuLoadGame();
EngFuncs::PIC_Load( ART_BANNER_SAVE );
EngFuncs::PIC_Load( ART_BANNER_LOAD );
}
void UI_LoadSaveGame_Menu( bool saveMode )
{
if( gMenu.m_gameinfo.gamemode == GAME_MULTIPLAYER_ONLY )
{
// completely ignore save\load menus for multiplayer_only
return;
}
if( !EngFuncs::CheckGameDll( )) return;
menu_loadgame->Show();
menu_loadgame->SetSaveMode( saveMode );
menu_loadgame->UpdateList();
}
void UI_LoadSaveGame_Shutdown( void )
{
delete menu_loadgame;
}
/*
=================
UI_LoadGame_Menu
=================
*/
void UI_LoadGame_Menu( void )
{
UI_LoadSaveGame_Menu( false );
}
void UI_SaveGame_Menu( void )
{
UI_LoadSaveGame_Menu( true );
}
ADD_MENU4( menu_loadgame, UI_LoadSaveGame_Precache, UI_LoadGame_Menu, UI_LoadSaveGame_Shutdown );
ADD_MENU4( menu_savegame, NULL, UI_SaveGame_Menu, NULL );