summaryrefslogtreecommitdiffstats
path: root/src/extras/frontendoption.cpp
blob: 88a930a9b3229fd5bd746b57e92210b8e3385c00 (plain) (blame)
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
#include "common.h"

#ifdef CUSTOM_FRONTEND_OPTIONS
#include "frontendoption.h"
#include "Text.h"

int numCustomFrontendOptions = 0;
FrontendOption *customFrontendOptions;

int numCustomFrontendScreens = 0;
FrontendScreen* customFrontendScreens;

int numFrontendOptionReplacements = 0;
CMenuScreen::CMenuEntry* frontendOptionReplacements;

int lastOgScreen = MENUPAGES; // means no new pages

int optionCursor = -2;
int currentMenu;
bool optionOverwrite = false;

void ChangeScreen(int screen, int option, bool fadeIn)
{
	FrontEndMenuManager.m_nPrevScreen = FrontEndMenuManager.m_nCurrScreen;
	FrontEndMenuManager.m_nCurrScreen = screen;
	FrontEndMenuManager.m_nCurrOption = option;
	if (fadeIn)
		FrontEndMenuManager.m_nMenuFadeAlpha = 0;
}

void GoBack(bool fadeIn)
{
	int screen = !FrontEndMenuManager.m_bGameNotLoaded ?
		aScreens[FrontEndMenuManager.m_nCurrScreen].m_PreviousPage[1] : aScreens[FrontEndMenuManager.m_nCurrScreen].m_PreviousPage[0];
	int option = !FrontEndMenuManager.m_bGameNotLoaded ?
		aScreens[FrontEndMenuManager.m_nCurrScreen].m_ParentEntry[1] : aScreens[FrontEndMenuManager.m_nCurrScreen].m_ParentEntry[0];

	FrontEndMenuManager.ThingsToDoBeforeGoingBack();

	ChangeScreen(screen, option, fadeIn);
}

uint8
GetNumberOfMenuOptions(int screen)
{
	uint8 Rows = 0;
	for (int i = 0; i < NUM_MENUROWS; i++) {
		if (aScreens[screen].m_aEntries[i].m_Action == MENUACTION_NOTHING)
			break;

		++Rows;
	}
	return Rows;
}

uint8
GetLastMenuScreen()
{
	int8 page = -1;
	for (int i = 0; i < MENUPAGES; i++) {
		if (strcmp(aScreens[i].m_ScreenName, "") == 0 && aScreens[i].unk == 0)
			break;

		++page;
	}
	return page;
}

// Used before populating options, but effective in InitialiseChangedLanguageSettings and debugmenu
void
RemoveCustomFrontendOptions()
{
	if (numCustomFrontendOptions != 0) {

		for (int i = 0; i < MENUPAGES; i++) {
			for (int j = 0; j < NUM_MENUROWS; j++) {
				if (aScreens[i].m_aEntries[j].m_SaveSlot == SAVESLOT_CFO) {
					int ogOptionId = customFrontendOptions[aScreens[i].m_aEntries[j].m_TargetMenu].ogOptionId;

					if (customFrontendOptions[aScreens[i].m_aEntries[j].m_TargetMenu].type == FEOPTION_SELECT)
						free(customFrontendOptions[aScreens[i].m_aEntries[j].m_TargetMenu].rightTexts);

					if (ogOptionId == -1) {
						int k;
						for (k = j; k < NUM_MENUROWS - 1; k++) {
							memcpy(&aScreens[i].m_aEntries[k], &aScreens[i].m_aEntries[k + 1], sizeof(CMenuScreen::CMenuEntry));
						}
						aScreens[i].m_aEntries[k].m_Action = MENUACTION_NOTHING;
						aScreens[i].m_aEntries[k].m_SaveSlot = SAVESLOT_NONE;
						aScreens[i].m_aEntries[k].m_EntryName[0] = '\0';
						j--;
					} else {
						memcpy(&aScreens[i].m_aEntries[j], &frontendOptionReplacements[ogOptionId], sizeof(CMenuScreen::CMenuEntry));
					}
				}
			}
		}
		free(customFrontendOptions);
		numCustomFrontendOptions = 0;

		if (numFrontendOptionReplacements != 0) {
			free(frontendOptionReplacements);
			numFrontendOptionReplacements = 0;
		}
	}

	if (numCustomFrontendScreens == 0)
		return;

	for (int i = 0; i < MENUPAGES; i++) {
		if (i > lastOgScreen) {
			aScreens[i].m_ScreenName[0] = '\0';
			aScreens[i].unk = 0;
		}
	}
	free(customFrontendScreens);
	numCustomFrontendScreens = 0;
	lastOgScreen = MENUPAGES;
}

int8 RegisterNewScreen(const char *name, int prevPage)
{
	if (lastOgScreen == MENUPAGES)
		lastOgScreen = GetLastMenuScreen();

	numCustomFrontendScreens++;
	if (numCustomFrontendScreens == 1)
		customFrontendScreens = (FrontendScreen*)malloc(5 * sizeof(FrontendScreen));
	else if (numCustomFrontendScreens % 5 == 1)
		customFrontendScreens = (FrontendScreen*)realloc(customFrontendScreens, (numCustomFrontendScreens + 4) * sizeof(FrontendScreen));

	assert(customFrontendScreens != nil && "Custom frontend screens can't be allocated");

	int id = lastOgScreen + numCustomFrontendScreens;
	assert(id < MENUPAGES && "No room for new custom frontend screens! Increase MENUPAGES");
	strncpy(aScreens[id].m_ScreenName, name, 8);
	aScreens[id].m_PreviousPage[0] = aScreens[id].m_PreviousPage[1] = prevPage;
	aScreens[id].unk = 1;
	return id;
}

int8 RegisterNewOption()
{
	numCustomFrontendOptions++;
	if (numCustomFrontendOptions == 1)
		customFrontendOptions = (FrontendOption*)malloc(5 * sizeof(FrontendOption));
	else if (numCustomFrontendOptions % 5 == 1)
		customFrontendOptions = (FrontendOption*)realloc(customFrontendOptions, (numCustomFrontendOptions + 4) * sizeof(FrontendOption));

	assert(customFrontendOptions != nil && "Custom frontend options can't be allocated");

	uint8 numOptions = GetNumberOfMenuOptions(currentMenu);
	uint8 curIdx;
	if (optionCursor < 0) {
		optionCursor = curIdx = numOptions + optionCursor + 1;
	} else 
		curIdx = optionCursor;

	if (!optionOverwrite) {
		if (aScreens[currentMenu].m_aEntries[curIdx].m_Action != MENUACTION_NOTHING) {
			for (int i = numOptions - 1; i >= curIdx; i--) {
				memcpy(&aScreens[currentMenu].m_aEntries[i + 1], &aScreens[currentMenu].m_aEntries[i], sizeof(CMenuScreen::CMenuEntry));
			}
		}
	}
	optionCursor++;

	if (optionOverwrite) {
		numFrontendOptionReplacements++;
		if (numFrontendOptionReplacements == 1)
			frontendOptionReplacements = (CMenuScreen::CMenuEntry*)malloc(5 * sizeof(CMenuScreen::CMenuEntry));
		else if (numFrontendOptionReplacements % 5 == 1)
			frontendOptionReplacements = (CMenuScreen::CMenuEntry*)realloc(frontendOptionReplacements, (numFrontendOptionReplacements + 4) * sizeof(CMenuScreen::CMenuEntry));

		memcpy(&frontendOptionReplacements[numFrontendOptionReplacements - 1], &aScreens[currentMenu].m_aEntries[curIdx], sizeof(CMenuScreen::CMenuEntry));
		customFrontendOptions[numCustomFrontendOptions - 1].ogOptionId = numFrontendOptionReplacements - 1;
	} else {
		customFrontendOptions[numCustomFrontendOptions - 1].ogOptionId = -1;
	}
	customFrontendOptions[numCustomFrontendOptions - 1].screen = currentMenu;

	aScreens[currentMenu].m_aEntries[curIdx].m_Action = MENUACTION_TRIGGERFUNC;
	aScreens[currentMenu].m_aEntries[curIdx].m_SaveSlot = SAVESLOT_CFO;
	aScreens[currentMenu].m_aEntries[curIdx].m_TargetMenu = numCustomFrontendOptions - 1;
	aScreens[currentMenu].m_aEntries[curIdx].m_EntryName[0] = 1; // just something to fool it
	return curIdx;
}

void FrontendOptionSetCursor(int screen, int8 option, bool overwrite)
{
	currentMenu = screen;
	optionCursor = option;
	optionOverwrite = overwrite;
}

void FrontendOptionAddBuiltinAction(const wchar* leftText, int action, ButtonPressFunc buttonPressFunc, ReturnPrevPageFunc returnPrevPageFunc) {
	int8 screenOptionOrder = RegisterNewOption();

	FrontendOption& option = customFrontendOptions[numCustomFrontendOptions - 1];

	// To fool the Frontend, we will still display the text passed via first param.
	switch (action) {
		case MENUACTION_SCREENRES:
			strcpy(aScreens[currentMenu].m_aEntries[screenOptionOrder].m_EntryName, "FED_RES");
			break;
		case MENUACTION_AUDIOHW:
			strcpy(aScreens[currentMenu].m_aEntries[screenOptionOrder].m_EntryName, "FEA_3DH");
			break;
	}
	aScreens[currentMenu].m_aEntries[screenOptionOrder].m_Action = action;
	option.type = FEOPTION_BUILTIN_ACTION;
	option.buttonPressFunc = buttonPressFunc;
	TextCopy(option.leftText, leftText);
	option.screenOptionOrder = screenOptionOrder;
	option.returnPrevPageFunc = returnPrevPageFunc;
	option.save = nil;
}

void FrontendOptionAddSelect(const wchar* leftText, const wchar** rightTexts, int8 numRightTexts, int8 *var, bool onlyApplyOnEnter, ChangeFunc changeFunc, ReturnPrevPageFunc returnPrevPageFunc, const char* saveName)
{
	int8 screenOptionOrder = RegisterNewOption();

	FrontendOption& option = customFrontendOptions[numCustomFrontendOptions - 1];
	option.type = FEOPTION_SELECT;
	TextCopy(option.leftText, leftText);
	option.rightTexts = (wchar**)malloc(numRightTexts * sizeof(wchar*));
	memcpy(option.rightTexts, rightTexts, numRightTexts * sizeof(wchar*));
	option.numRightTexts = numRightTexts;
	option.value = var;
	option.displayedValue = *var;
	option.lastSavedValue = *var;
	option.save = saveName;
	option.onlyApplyOnEnter = onlyApplyOnEnter;
	option.changeFunc = changeFunc;
	option.screenOptionOrder = screenOptionOrder;
	option.returnPrevPageFunc = returnPrevPageFunc;
}

void FrontendOptionAddDynamic(const wchar* leftText, DrawFunc drawFunc, int8 *var, ButtonPressFunc buttonPressFunc, ReturnPrevPageFunc returnPrevPageFunc, const char* saveName)
{
	int8 screenOptionOrder = RegisterNewOption();

	FrontendOption& option = customFrontendOptions[numCustomFrontendOptions - 1];
	option.type = FEOPTION_DYNAMIC;
	option.drawFunc = drawFunc;
	option.buttonPressFunc = buttonPressFunc;
	TextCopy(option.leftText, leftText);
	option.value = var;
	option.save = saveName;
	option.screenOptionOrder = screenOptionOrder;
	option.returnPrevPageFunc = returnPrevPageFunc;
}

void FrontendOptionAddRedirect(const wchar* text, int to, int8 selectedOption, bool fadeIn)
{
	int8 screenOptionOrder = RegisterNewOption();

	FrontendOption &option = customFrontendOptions[numCustomFrontendOptions - 1];
	option.type = FEOPTION_REDIRECT;
	option.to = to;
	option.option = selectedOption;
	option.fadeIn = fadeIn;
	TextCopy(option.leftText, text);
	option.screenOptionOrder = screenOptionOrder;
	option.returnPrevPageFunc = nil;
	option.save = nil;
}

void FrontendOptionAddBackButton(const wchar* text, bool fadeIn)
{
	int8 screenOptionOrder = RegisterNewOption();

	FrontendOption& option = customFrontendOptions[numCustomFrontendOptions - 1];
	option.type = FEOPTION_GOBACK;
	option.fadeIn = fadeIn;
	TextCopy(option.leftText, text);
	option.screenOptionOrder = screenOptionOrder;
	option.returnPrevPageFunc = nil;
	option.save = nil;
}

uint8 FrontendScreenAdd(const char* gxtKey, eMenuSprites sprite, int prevPage, int columnWidth, int headerHeight, int lineHeight,
	int8 font, float fontScaleX, float fontScaleY, int8 alignment, bool showLeftRightHelper, ReturnPrevPageFunc returnPrevPageFunc) {

	uint8 screenOrder = RegisterNewScreen(gxtKey, prevPage);

	FrontendScreen &screen = customFrontendScreens[numCustomFrontendScreens - 1];
	screen.id = screenOrder;
	screen.sprite = sprite;
	screen.prevPage = prevPage;
	strncpy(screen.name, gxtKey, 8);
	screen.columnWidth = columnWidth;
	screen.headerHeight = headerHeight;
	screen.lineHeight = lineHeight;
	screen.font = font;
	screen.fontScaleX = fontScaleX;
	screen.fontScaleY = fontScaleY;
	screen.alignment = alignment;
	screen.returnPrevPageFunc = returnPrevPageFunc;

	return screenOrder;
}
#endif