summaryrefslogtreecommitdiff
path: root/src/script_menu.c
blob: 51230b66b737c3c0a7953ef0fe00b9ec48371bb2 (plain)
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
#include "global.h"
#include "event_data.h"
#include "menu.h"
#include "palette.h"
#include "script.h"
#include "sound.h"
#include "sprite.h"
#include "task.h"

struct MultichoiceListStruct
{
    struct MenuAction *list;
    u8 count;
};

extern const struct MultichoiceListStruct gMultichoiceLists[];

extern u16 gScriptResult;

extern void FreeResourcesAndDestroySprite(struct Sprite *sprite);
extern u8 CreateMonSprite_PicBox(u16, s16, s16, u8);
extern u8 sub_80B59AC(void);

extern u8 gPCText_PlayersPC[];
extern u8 gPCText_SomeonesPC[];
extern u8 gPCText_HallOfFame[];
extern u8 gPCText_LogOff[];
extern u8 gPCText_LanettesPC[];
extern u8 gPCText_WhichPCShouldBeAccessed[];

void DrawMultichoiceMenu(u8, u8, u8, struct MenuAction *list, u8, u8);
void sub_80B53B4(u8, u8, u8, struct MenuAction *list, u8);
void sub_80B52B4(u8);
void sub_80B5230(u8, u8, u8, u8, u8, u8);
void task_yes_no_maybe(u8);
void sub_80B5684(u8);
void CreatePCMenu(void);

bool8 sub_80B5054(u8 left, u8 top, u8 var3, u8 var4)
{
    if (FuncIsActiveTask(sub_80B52B4) == 1)
        return FALSE;
    else
    {
        gScriptResult = 0xFF;
        DrawMultichoiceMenu(left, top, gMultichoiceLists[var3].count, gMultichoiceLists[var3].list, var4, 0);
        return TRUE;
    }
}

bool8 sub_80B50B0(u8 left, u8 top, u8 var3, u8 var4, u8 var5)
{
    if (FuncIsActiveTask(sub_80B52B4) == 1)
        return FALSE;
    else
    {
        gScriptResult = 0xFF;
        DrawMultichoiceMenu(left, top, gMultichoiceLists[var3].count, gMultichoiceLists[var3].list, var4, var5);
        return TRUE;
    }
}

u16 GetStringWidthInTilesForScriptMenu(u8 *str)
{
    // each tile on screen is 8x8, so it needs the number of tiles and not pixels, hence the division by 8.
    return (GetStringWidthGivenWindowConfig((struct WindowConfig *)&gWindowConfig_81E6CE4, str) + 7) / 8;
}

void DrawMultichoiceMenu(u8 left, u8 top, u8 count, struct MenuAction *list, u8 var4, u8 cursorPos)
{
    u16 width = GetStringWidthInTilesForScriptMenu(list[0].text);
    u16 newWidth;
    u8 i;
    u8 right;
    u8 bottom;

    for (i = 1; i < count; i++)
    {
        newWidth = GetStringWidthInTilesForScriptMenu(list[i].text);
        if (width < newWidth)
            width = newWidth;
    }

    right = width;
    right = (right + left) + 1;

    if (right > 29)
    {
        left = left + (29 - right);
        right = 29;
    }

    bottom = top + (2 * count + 1);

    MenuDrawTextWindow(left, top, right, bottom);
    PrintMenuItems(left + 1, top + 1, count, list);
    InitMenu(0, left + 1, top + 1, count, cursorPos, right - left - 1);
    sub_80B5230(left, top, right, bottom, var4, count);
}

void sub_80B5230(u8 left, u8 top, u8 right, u8 bottom, u8 unkVar, u8 count)
{
    u8 taskId = CreateTask(sub_80B52B4, 80);

    gTasks[taskId].data[0] = left;
    gTasks[taskId].data[1] = top;
    gTasks[taskId].data[2] = right;
    gTasks[taskId].data[3] = bottom;
    gTasks[taskId].data[4] = unkVar;

    if (count > 3)
        gTasks[taskId].data[5] = TRUE;
    else
        gTasks[taskId].data[5] = FALSE;
}

void sub_80B52B4(u8 taskId)
{
    s8 var;

    if (!gPaletteFade.active)
    {
        if (!gTasks[taskId].data[5])
            var = ProcessMenuInputNoWrap();
        else
            var = ProcessMenuInput();

        if (var != -2)
        {
            if (var == -1)
            {
                if (!gTasks[taskId].data[4])
                {
                    PlaySE(5);
                    gScriptResult = 127;
                }
                else
                {
                    return;
                }
            }
            else
            {
                gScriptResult = var;
            }
            sub_8072DEC();
            MenuZeroFillWindowRect(gTasks[taskId].data[0], gTasks[taskId].data[1], gTasks[taskId].data[2], gTasks[taskId].data[3]);
            DestroyTask(taskId);
            EnableBothScriptContexts();
        }
    }
}

bool8 Multichoice(u8 var1, u8 var2, u8 var3, u8 var4)
{
    if (FuncIsActiveTask(sub_80B52B4) == 1)
        return FALSE;
    else
    {
        gScriptResult = 0xFF;
        sub_80B53B4(var1, var2, gMultichoiceLists[var3].count, gMultichoiceLists[var3].list, var4);
        return TRUE;
    }
}

void sub_80B53B4(u8 left, u8 top, u8 count, struct MenuAction *list, u8 var4)
{
    u16 width = GetStringWidthInTilesForScriptMenu(list[0].text);
    u16 newWidth;
    u8 i;
    u8 right;
    u8 bottom;

    for (i = 1; i < count; i++)
    {
        newWidth = GetStringWidthInTilesForScriptMenu(list[i].text);
        if (width < newWidth)
            width = newWidth;
    }

    right = width;
    right = (right + left) + 2;
    bottom = top + (2 * count + 1);

    PrintMenuItems(left, top, count, list);
    InitMenu(0, left, top, count, 0, right - left - 1);
    sub_80B5230(left, top, right, bottom, var4, count);
}

bool8 yes_no_box(u8 var1, u8 var2)
{
    u8 taskId;

    if (FuncIsActiveTask(task_yes_no_maybe) == 1)
        return FALSE;
    else
    {
        gScriptResult = 0xFF;
        DisplayYesNoMenu(var1, var2, 1);
        taskId = CreateTask(task_yes_no_maybe, 0x50);
        gTasks[taskId].data[0] = var1;
        gTasks[taskId].data[1] = var2;
        return TRUE;
    }
}

// unused
bool8 IsScriptActive(void)
{
    if (gScriptResult == 0xFF)
        return FALSE;
    else
        return TRUE;
}

void task_yes_no_maybe(u8 taskId)
{
    u8 left, top;

    if (gTasks[taskId].data[2] < 5)
    {
        gTasks[taskId].data[2]++;
        return;
    }

    switch (ProcessMenuInputNoWrap())
    {
    case -2:
        return;
    case -1:
    case 1:
        PlaySE(5);
        gScriptResult = 0;
        break;
    case 0:
        gScriptResult = 1;
        break;
    }

    left = gTasks[taskId].data[0];
    top = gTasks[taskId].data[1];

    MenuZeroFillWindowRect(left, top, left + 6, top + 5);
    DestroyTask(taskId);
    EnableBothScriptContexts();
}

bool8 sub_80B5578(u8 left, u8 top, u8 multichoiceId, u8 a4, u8 columnCount)
{
    u8 bottom = 0;

    if (FuncIsActiveTask(sub_80B5684) == TRUE)
    {
        return FALSE;
    }
    else
    {
        u8 taskId;
        u8 width;

        gScriptResult = 0xFF;

        sub_807274C(left, top, gMultichoiceLists[multichoiceId].count, 0, gMultichoiceLists[multichoiceId].list, columnCount, 0);

        taskId = CreateTask(sub_80B5684, 80);

        if (!((gMultichoiceLists[multichoiceId].count >> 1) < columnCount || (gMultichoiceLists[multichoiceId].count & 1))
         || columnCount == 1 || gMultichoiceLists[multichoiceId].count == columnCount)
        {
            bottom = (2 * (gMultichoiceLists[multichoiceId].count / columnCount)) + 1 + top;
        }
        else
        {
            bottom = (2 * (gMultichoiceLists[multichoiceId].count / columnCount)) + 3 + top;
        }

        width = sub_807288C(columnCount);
        gTasks[taskId].data[0] = left;
        gTasks[taskId].data[1] = top;
        gTasks[taskId].data[2] = width + left + 2;
        gTasks[taskId].data[3] = bottom;
        gTasks[taskId].data[4] = a4;
        return TRUE;
    }
}

void sub_80B5684(u8 taskId)
{
    s8 var = sub_80727CC();

    if (var != -2)
    {
        if (var == -1)
        {
            if (!gTasks[taskId].data[4])
            {
                PlaySE(5);
                gScriptResult = 127;
            }
            else
            {
                return;
            }
        }
        else
        {
            gScriptResult = var;
        }
        sub_8072DEC();
        MenuZeroFillWindowRect(gTasks[taskId].data[0], gTasks[taskId].data[1], gTasks[taskId].data[2], gTasks[taskId].data[3]);
        DestroyTask(taskId);
        EnableBothScriptContexts();
    }
}

bool8 TryCreatePCMenu(void)
{
    if (FuncIsActiveTask(sub_80B52B4) == 1)
        return FALSE;
    else
    {
        gScriptResult = 0xFF;
        CreatePCMenu();
        return TRUE;
    }
}

void CreatePCMenu(void)
{
    u16 playersPCWidth = GetStringWidthInTilesForScriptMenu(gPCText_PlayersPC);
    u8 width;
    u8 numChoices;

    if (playersPCWidth > GetStringWidthInTilesForScriptMenu(gPCText_SomeonesPC))
        width = playersPCWidth;
    else
        width = 8;

    if (FlagGet(SYS_GAME_CLEAR)) // player has cleared game?
    {
        numChoices = 4;
        MenuDrawTextWindow(0, 0, width + 2, 9);
        MenuPrint(gPCText_HallOfFame, 1, 5);
        MenuPrint(gPCText_LogOff, 1, 7);
    }
    else
    {
        numChoices = 3;
        MenuDrawTextWindow(0, 0, width + 2, 7);
        MenuPrint(gPCText_LogOff, 1, 5);
    }

    if (FlagGet(SYS_PC_LANETTE)) // player met lanette?
        MenuPrint(gPCText_LanettesPC, 1, 1);
    else
        MenuPrint(gPCText_SomeonesPC, 1, 1);

    MenuPrint(gPCText_PlayersPC, 1, 3);
    InitMenu(0, 1, 1, numChoices, 0, width + 1);
    sub_80B5230(0, 0, width + 2, 2 * numChoices + 1, 0, numChoices);
}

void sub_80B5838(void)
{
    MenuDisplayMessageBox();
    MenuPrint(gPCText_WhichPCShouldBeAccessed, 2, 15);
}

void task_picbox(u8 taskId)
{
    struct Task *task = &gTasks[taskId];

    switch (task->data[0])
    {
        case 0:
            task->data[0]++;
            break;
        case 1:
            break;
        case 2:
            FreeResourcesAndDestroySprite(&gSprites[task->data[2]]);
            task->data[0]++;
            break;
        case 3:
            MenuZeroFillWindowRect(task->data[3], task->data[4], task->data[3] + 9, task->data[4] + 10);
            DestroyTask(taskId);
            break;
    }
}

bool8 sub_80B58C4(u16 var1, u8 var2, u8 var3)
{
    u8 taskId;
    u8 var;

    if (FindTaskIdByFunc(task_picbox) != 0xFF)
        return FALSE;
    else
    {
        MenuDrawTextWindow(var2, var3, var2 + 9, var3 + 10);
        taskId = CreateTask(task_picbox, 0x50);
        gTasks[taskId].data[0] = 0;
        gTasks[taskId].data[1] = var1;
        var = CreateMonSprite_PicBox(var1, var2 * 8 + 40, var3 * 8 + 40, 0);
        gTasks[taskId].data[2] = var;
        gTasks[taskId].data[3] = var2;
        gTasks[taskId].data[4] = var3;
        gSprites[var].callback = SpriteCallbackDummy;
        gSprites[var].oam.priority = 0;
        return TRUE;
    }
}

void *picbox_close(void)
{
    u8 taskId = FindTaskIdByFunc(task_picbox);

    if (taskId == 0xFF)
        return NULL;

    gTasks[taskId].data[0]++;
    return (void *)sub_80B59AC;
}

bool8 sub_80B59AC(void)
{
    if (FindTaskIdByFunc(task_picbox) == 0xFF)
        return TRUE;
    else
        return FALSE;
}