summaryrefslogtreecommitdiff
path: root/src/script_menu.c
blob: d375ece08bf80cfcbc73584be44a607a42df56dd (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
#include "global.h"
#include "palette.h"
#include "text.h"
#include "list_menu.h"
#include "menu.h"
#include "task.h"
#include "sound.h"
#include "script_menu.h"
#include "quest_log.h"
#include "window.h"
#include "new_menu_helpers.h"
#include "event_data.h"
#include "script.h"
#include "constants/songs.h"

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

EWRAM_DATA u8 gUnknown_2039988 = 0;

void sub_809CA64(u8 left, u8 top, u8 mcId, u8 ignoreBpress, u8 initPos);
u8 sub_809CBB4(u8 count);
void sub_809CC18(u8 ignoreBpress, u8 count, u8 windowId, u8 mcId);
void sub_809CC98(u8 taskId);
void sub_809CD48(u8 mcId);
void task_yes_no_maybe(u8 taskId);
void sub_809D6B0(u8 windowId);
u8 CreateWindowFromRect(u8 left, u8 top, u8 width, u8 height);

extern const u8 *const gUnknown_83E072C[];
extern const u8 *const gUnknown_83E0738[];
extern const u8 *const gUnknown_83E0748[];
extern const struct MultichoiceListStruct gUnknown_83E04B0[];

u16 sub_809C954(const u8 *str)
{
    return (GetStringWidth(1, str, 0) + 7) / 8;
}

u8 sub_809C974(const struct ListMenuItem * items, u8 count)
{
    u16 i;
    u8 width = sub_809C954(items[0].label);
    u8 tmp;

    for (i = 1; i < count; i++)
    {
        tmp = sub_809C954(items[i].label);
        if (width < tmp)
            width = tmp;
    }
    return width;
}

bool8 ScriptMenu_Multichoice(u8 left, u8 top, u8 mcId, u8 ignoreBpress)
{
    if (FuncIsActiveTask(sub_809CC98) == TRUE)
        return FALSE;
    gSpecialVar_Result = 0xFF;
    sub_809CA64(left, top, mcId, ignoreBpress, 0);
    return TRUE;
}

bool8 ScriptMenu_MultichoiceWithDefault(u8 left, u8 top, u8 mcId, u8 ignoreBpress, u8 cursorPos)
{
    if (FuncIsActiveTask(sub_809CC98) == TRUE)
        return FALSE;
    gSpecialVar_Result = 0xFF;
    sub_809CA64(left, top, mcId, ignoreBpress, cursorPos);
    return TRUE;
}

void sub_809CA64(u8 left, u8 top, u8 mcId, u8 ignoreBpress, u8 initPos)
{
    s32 i;
    s32 strWidth;
    s32 tmp;
    u8 width;
    u8 height;
    u8 count;
    u8 windowId;
    const struct MenuAction * list;

    if ((ignoreBpress & 2) || sub_81119D4(sub_809D6D4) != TRUE)
    {
        ignoreBpress &= 1;
        count = gUnknown_83E04B0[mcId].count;
        list = gUnknown_83E04B0[mcId].list;
        strWidth = 0;
        for (i = 0; i < count; i++)
        {
            tmp = GetStringWidth(2, list[i].text, 0);
            if (tmp > strWidth)
                strWidth = tmp;
        }
        width = (strWidth + 9) / 8 + 1;
        if (left + width > 28)
            left = 28 - width;
        height = sub_809CBB4(count);
        windowId = CreateWindowFromRect(left, top, width, height);
        SetStdWindowBorderStyle(windowId, FALSE);
        if (mcId == 30 || mcId == 13 || mcId == 41)
            MultichoiceList_PrintItems(windowId, 2, 8, 2, 14, count, list, 0, 2);
        else
            MultichoiceList_PrintItems(windowId, 2, 8, 2, 14, count, list, 0, 2);
        Menu_InitCursor(windowId, 2, 0, 2, 14, count, initPos);
        sub_809CC18(ignoreBpress, count, windowId, mcId);
        ScheduleBgCopyTilemapToVram(0);
    }
}

u8 sub_809CBB4(u8 count)
{
    switch (count)
    {
    case 0:
        return 1;
    case 1:
        return 2;
    case 2:
        return 4;
    case 3:
        return 6;
    case 4:
        return 7;
    case 5:
        return 9;
    case 6:
        return 11;
    case 7:
        return 13;
    case 8:
        return 14;
    default:
        return 1;
    }
}

void sub_809CC18(u8 ignoreBpress, u8 count, u8 windowId, u8 mcId)
{
    u8 taskId;
    if (mcId == 39 || mcId == 47 || mcId == 50)
        gUnknown_2039988 = 12;
    else
        gUnknown_2039988 = 0;
    taskId = CreateTask(sub_809CC98, 80);
    gTasks[taskId].data[4] = ignoreBpress;
    if (count > 3)
        gTasks[taskId].data[5] = TRUE;
    else
        gTasks[taskId].data[5] = FALSE;
    gTasks[taskId].data[6] = windowId;
    gTasks[taskId].data[7] = mcId;
    sub_809CD48(mcId);
}

void sub_809CC98(u8 taskId)
{
    s16 *data = gTasks[taskId].data;
    s8 input;
    if (!gPaletteFade.active)
    {
        if (gUnknown_2039988 != 0)
            gUnknown_2039988--;
        else
        {
            if (data[5] == FALSE)
                input = Menu_ProcessInputNoWrapAround();
            else
                input = Menu_ProcessInput();
            if (JOY_NEW(DPAD_UP | DPAD_DOWN))
                sub_809CD48(data[7]);
            switch (input)
            {
            case -2:
                return;
            case -1:
                if (data[4])
                    return;
                PlaySE(SE_SELECT);
                gSpecialVar_Result = 127;
                break;
            default:
                gSpecialVar_Result = input;
                break;
            }
            sub_809D6B0(data[6]);
            DestroyTask(taskId);
            EnableBothScriptContexts();
        }
    }
}

void sub_809CD48(u8 mcId)
{
    switch (mcId)
    {
    case 39:
        FillWindowPixelBuffer(0, PIXEL_FILL(1));
        AddTextPrinterParameterized2(0, 2, gUnknown_83E072C[Menu_GetCursorPos()], 0, NULL, 2, 1, 3);
        break;
    case 47:
        FillWindowPixelBuffer(0, PIXEL_FILL(1));
        AddTextPrinterParameterized2(0, 2, gUnknown_83E0738[Menu_GetCursorPos()], 0, NULL, 2, 1, 3);
        break;
    case 50:
        FillWindowPixelBuffer(0, PIXEL_FILL(1));
        AddTextPrinterParameterized2(0, 2, gUnknown_83E0748[Menu_GetCursorPos()], 0, NULL, 2, 1, 3);
        break;
    }
}

bool8 ScriptMenu_YesNo(u8 unused, u8 stuff)
{
    if (FuncIsActiveTask(task_yes_no_maybe) == TRUE)
        return FALSE;
    gSpecialVar_Result = 255;
    if (!sub_81119D4(sub_809D6D4))
    {
        DisplayYesNoMenuDefaultYes();
        CreateTask(task_yes_no_maybe, 80);
    }
    return TRUE;
}

bool8 sub_809CE38(void)
{
    if (gSpecialVar_Result == 255)
        return FALSE;
    else
        return TRUE;
}

void task_yes_no_maybe(u8 taskId)
{
    s8 input;
    if (gTasks[taskId].data[2] < 5)
    {
        gTasks[taskId].data[2]++;
    }
    else
    {
        input = Menu_ProcessInputNoWrapClearOnChoose();
        switch (input)
        {
        case -2:
            return;
        case -1:
        case 1:
            PlaySE(SE_SELECT);
            gSpecialVar_Result = FALSE;
            break;
        case 0:
            gSpecialVar_Result = TRUE;
            break;
        }
        DestroyTask(taskId);
        EnableBothScriptContexts();
    }
}