File: cl_uiloadsave.cpp

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (379 lines) | stat: -rw-r--r-- 9,648 bytes parent folder | download
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
/*
===========================================================================
Copyright (C) 2023 the OpenMoHAA team

This file is part of OpenMoHAA source code.

OpenMoHAA source code 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.

OpenMoHAA source code 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 OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

#include "cl_ui.h"
#include "../qcommon/localization.h"

class FAKKLoadGameItem : public UIListCtrlItem
{
    str strings[4];

public:
    FAKKLoadGameItem(const str& missionName, const str& elapsedTime, const str& dateTime, const str& fileName);

    int            getListItemValue(int which) const override;
    griditemtype_t getListItemType(int which) const override;
    str            getListItemString(int which) const override;
    void           DrawListItem(int iColumn, const UIRect2D& drawRect, bool bSelected, UIFont *pFont) override;
    qboolean       IsHeaderEntry() const override;
};

static UIFAKKLoadGameClass *loadgame_ui = NULL;

Event EV_FAKKLoadGame_LoadGame
(
    "loadgame",
    EV_DEFAULT,
    NULL,
    NULL,
    "Load the currently selected game"
);
Event EV_FAKKLoadGame_RemoveGame
(
    "removegame",
    EV_DEFAULT,
    NULL,
    NULL,
    "Delete the currently selected game"
);
Event EV_FAKKLoadGame_DeleteGame
(
    "deletegame",
    EV_DEFAULT,
    NULL,
    NULL,
    "Delete the currently selected game... for real"
);
Event EV_FAKKLoadGame_NoDeleteGame
(
    "nodeletegame",
    EV_DEFAULT,
    NULL,
    NULL,
    "Delete the currently selected game... for real"
);
Event EV_FAKKLoadGame_SaveGame
(
    "savegame",
    EV_DEFAULT,
    NULL,
    NULL,
    "Save the currently selected game"
);

CLASS_DECLARATION(UIListCtrl, UIFAKKLoadGameClass, NULL) {
    {&EV_UIListBase_ItemSelected,      &UIFAKKLoadGameClass::SelectGame  },
    {&EV_UIListBase_ItemDoubleClicked, &UIFAKKLoadGameClass::LoadGame    },
    {&EV_FAKKLoadGame_RemoveGame,      &UIFAKKLoadGameClass::RemoveGame  },
    {&EV_FAKKLoadGame_DeleteGame,      &UIFAKKLoadGameClass::DeleteGame  },
    {&EV_FAKKLoadGame_NoDeleteGame,    &UIFAKKLoadGameClass::NoDeleteGame},
    {&EV_FAKKLoadGame_LoadGame,        &UIFAKKLoadGameClass::LoadGame    },
    {&EV_FAKKLoadGame_SaveGame,        &UIFAKKLoadGameClass::SaveGame    },
    {NULL,                             NULL                              }
};

UIFAKKLoadGameClass::UIFAKKLoadGameClass()
{
    Connect(this, EV_UIListBase_ItemDoubleClicked, EV_UIListBase_ItemDoubleClicked);
    Connect(this, EV_UIListBase_ItemSelected, EV_UIListBase_ItemSelected);
    AllowActivate(true);
    m_bRemovePending = false;

    setHeaderFont("facfont-20");
    loadgame_ui = this;
}

UIFAKKLoadGameClass::~UIFAKKLoadGameClass()
{
    loadgame_ui = NULL;
}

void UIFAKKLoadGameClass::UpdateUIElement(void)
{
    float width;

    RemoveAllColumns();

    width = getClientFrame().size.width;
    AddColumn(Sys_LV_CL_ConvertString("Mission"), 0, width * 0.555, false, false);
    AddColumn(Sys_LV_CL_ConvertString("Elapsed Time"), 1, width * 0.17f, true, true);
    AddColumn(Sys_LV_CL_ConvertString("Date & Time Logged"), 2, width * 0.275f, true, true);

    uWinMan.ActivateControl(this);

    SetupFiles();
}

void UIFAKKLoadGameClass::SetupFiles(void)
{
    char      **filenames;
    int         numfiles;
    int         i;
    const char *searchFolder = Com_GetArchiveFolder();

    // cleanup
    DeleteAllItems();

    filenames = FS_ListFiles(searchFolder, "ssv", qfalse, &numfiles);

    for (i = 0; i < numfiles; i++) {
        const char      *filename;
        str              work;
        str              gametime;
        str              date;
        fileHandle_t     f;
        savegamestruct_t save;

        filename = filenames[i];
        work     = searchFolder;
        work += "/";
        work += filename;

        FS_FOpenFileRead(work, &f, qfalse, qtrue);
        if (!f) {
            continue;
        }

        FS_Read(&save, sizeof(savegamestruct_t), f);
        FS_FCloseFile(f);
        Com_SwapSaveStruct(&save);

        if (save.version != SAVEGAME_STRUCT_VERSION) {
            // wrong save game version
            continue;
        }

        if (save.type != com_target_game->integer) {
            continue;
        }

        gametime = (save.mapTime / 1000);
        date     = save.time;

        AddItem(new FAKKLoadGameItem(save.comment, gametime, date, save.saveName));
    }

    FS_FreeFileList(filenames);

    // sort by date
    SortByColumn(2);

    // select the first item
    TrySelectItem(1);
    SelectGame(NULL);
}

void UIFAKKLoadGameClass::SelectGame(Event *ev)
{
    UIWidget   *wid;
    const char *shotName;

    if (getCurrentItem() > 0) {
        shotName = Com_GetArchiveFileName(GetItem(getCurrentItem())->getListItemString(3), "tga");
    } else {
        shotName = "textures/menu/no_saved_games.tga";
    }

    wid = findSibling("LoadSaveShot");
    if (!wid) {
        return;
    }

    wid->setMaterial(uWinMan.RefreshShader(shotName));
}

void UIFAKKLoadGameClass::RemoveGame(Event *ev)
{
    if (m_bRemovePending || getCurrentItem() <= 0) {
        return;
    }

    Cbuf_ExecuteText(
        EXEC_NOW,
        "dialog \"\" \"\" \"widgetcommand LoadSaveList deletegame\" \"widgetcommand LoadSaveList nodeletegame\" 256 64 "
        "confirm_delete menu_button_trans menu_button_trans\n"
    );
    m_bRemovePending = true;
}

void UIFAKKLoadGameClass::NoDeleteGame(Event *ev)
{
    m_bRemovePending = false;
}

void UIFAKKLoadGameClass::DeleteGame(Event *ev)
{
    str     name;
    cvar_t *var;
    m_bRemovePending = false;

    if (getCurrentItem() <= 0) {
        return;
    }

    name = GetItem(getCurrentItem())->getListItemString(3);
    var  = Cvar_Get("g_lastsave", "", 0);

    if (!strcmp(name, var->string)) {
        // Make sure the last save is not the save being deleted
        Cvar_Set("g_lastsave", "");
    }

    Com_WipeSavegame(name);
    SetupFiles();
}

void UIFAKKLoadGameClass::LoadGame(Event *ev)
{
    char cmdString[266];
    str  name;

    if (getCurrentItem() <= 0) {
        return;
    }

    name = GetItem(getCurrentItem())->getListItemString(3);
    // Execute the command
    Com_sprintf(cmdString, sizeof(cmdString), "loadgame %s\n", name.c_str());
    Cbuf_AddText(cmdString);
}

void UIFAKKLoadGameClass::SaveGame(Event *ev)
{
    Cbuf_ExecuteText(EXEC_NOW, "savegame");
}

qboolean UIFAKKLoadGameClass::KeyEvent(int key, unsigned int time)
{
    switch (key) {
    case K_DEL:
        RemoveGame(NULL);
        return qtrue;
    case K_ENTER:
    case K_KP_ENTER:
        LoadGame(NULL);
        return qtrue;
    case K_UPARROW:
        if (getCurrentItem() > 1) {
            TrySelectItem(getCurrentItem() - 1);
            SelectGame(NULL);
            return qtrue;
        }
        break;
    case K_DOWNARROW:
        if (getCurrentItem() < getNumItems()) {
            TrySelectItem(getCurrentItem() + 1);
            SelectGame(NULL);
            return qtrue;
        }
        break;
    default:
        return UIListCtrl::KeyEvent(key, time);
    }

    return qfalse;
}

void UI_SetupFiles(void)
{
    if (loadgame_ui && loadgame_ui->getShow()) {
        loadgame_ui->SetupFiles();
    }
}

FAKKLoadGameItem::FAKKLoadGameItem(
    const str& missionName, const str& elapsedTime, const str& dateTime, const str& fileName
)
{
    strings[0] = missionName;
    strings[1] = elapsedTime;
    strings[2] = dateTime;
    strings[3] = fileName;
}

int FAKKLoadGameItem::getListItemValue(int which) const
{
    return atoi(strings[which]);
}

griditemtype_t FAKKLoadGameItem::getListItemType(int which) const
{
    return griditemtype_t::TYPE_STRING;
}

str FAKKLoadGameItem::getListItemString(int which) const
{
    str itemstring;

    switch (which) {
    case 0:
    case 3:
        itemstring = strings[which];
        break;
    case 1:
        {
            int numseconds;
            int numseconds_hours;
            int seconds;

            // hours
            numseconds = atol(strings[1]);
            itemstring += (numseconds / 3600);
            itemstring += ":";

            // minutes
            numseconds_hours = numseconds % 3600;
            if (numseconds_hours / 60 < 10) {
                itemstring += "0";
            }
            itemstring += (numseconds_hours / 60);
            itemstring += ":";

            // seconds
            seconds = numseconds_hours % 60;
            if (seconds < 10) {
                itemstring += "0";
            }
            itemstring += seconds;
        }
        break;
    case 2:
        {
            time_t time;
            char   buffer[2048];

            time = atol(strings[2]);
            strftime(buffer, sizeof(buffer), "%a %b %d %Y %H:%M:%S", localtime(&time));
            itemstring = buffer;
        }
        break;
    }

    return itemstring;
}

void FAKKLoadGameItem::DrawListItem(int iColumn, const UIRect2D& drawRect, bool bSelected, UIFont *pFont) {}

qboolean FAKKLoadGameItem::IsHeaderEntry() const
{
    return qfalse;
}