summaryrefslogtreecommitdiffstats
path: root/src/extras/frontendoption.cpp
blob: 51814f3571cdc622f3c5f7ad14b3ef1f82d8a25e (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
#include "common.h"

#ifdef CUSTOM_FRONTEND_OPTIONS
#include "frontendoption.h"

int numCustomFrontendOptions = 0;
FrontendOption *customFrontendOptions;

int optionCursor = -1;
eMenuScreen currentMenu;

void ChangeScreen(eMenuScreen 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((eMenuScreen)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;
}

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

	for (int i = 0; i < MENUPAGES; i++) {
		for (int j = 0; j < NUM_MENUROWS; j++) {
			if (aScreens[i].m_aEntries[j].m_Action == MENUACTION_TRIGGERFUNC) {
				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_EntryName[0] = '\0';
				j--;
			}
		}
	}
	free(customFrontendOptions);
	numCustomFrontendOptions = 0;
}

int8 RegisterNewOption(int screen)
{
	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 nth = GetNumberOfMenuOptions(screen);
	if (optionCursor < 0) {
		if (optionCursor == -1) {
			if (!strcmp(aScreens[screen].m_aEntries[nth - 1].m_EntryName, "FEDS_TB") || !strcmp(aScreens[screen].m_aEntries[nth - 1].m_EntryName, "FESZ_CA")) {
				// Move back button one below
				memcpy(&aScreens[screen].m_aEntries[nth], &aScreens[screen].m_aEntries[nth - 1], sizeof(CMenuScreen::CMenuEntry));
				nth--;
			}
		}
	} else {
		if (aScreens[screen].m_aEntries[optionCursor].m_Action != MENUACTION_NOTHING) {
			for (int i = nth - 1; i >= optionCursor; i--) {
				memcpy(&aScreens[screen].m_aEntries[i + 1], &aScreens[screen].m_aEntries[i], sizeof(CMenuScreen::CMenuEntry));
			}
		}
		nth = optionCursor;
		optionCursor++;
	}

	aScreens[screen].m_aEntries[nth].m_Action = MENUACTION_TRIGGERFUNC;
	aScreens[screen].m_aEntries[nth].m_TargetMenu = numCustomFrontendOptions - 1;
	aScreens[screen].m_aEntries[nth].m_EntryName[0] = 1; // just something to fool it
	return nth;
}

void FrontendOptionSetPosition(eMenuScreen screen, int8 option)
{
	currentMenu = screen;
	optionCursor = option;
}

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

	FrontendOption& option = customFrontendOptions[numCustomFrontendOptions - 1];
	option.screen = currentMenu;
	option.type = FEOPTION_SELECT;
	option.leftText = leftText;
	option.rightTexts = rightTexts;
	option.numRightTexts = numRightTexts;
	option.value = var;
	option.displayedValue = *var;
	option.onlyApplyOnEnter = onlyApplyOnEnter;
	option.changeFunc = changeFunc;
	option.screenOptionOrder = screenOptionOrder;
	option.returnPrevPageFunc = returnPrevPageFunc;
}

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

	FrontendOption& option = customFrontendOptions[numCustomFrontendOptions - 1];
	option.screen = currentMenu;
	option.type = FEOPTION_DYNAMIC;
	option.drawFunc = drawFunc;
	option.buttonPressFunc = buttonPressFunc;
	option.leftText = leftText;
	option.onlyApplyOnEnter = false;
	option.screenOptionOrder = screenOptionOrder;
	option.returnPrevPageFunc = returnPrevPageFunc;
}

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

	FrontendOption &option = customFrontendOptions[numCustomFrontendOptions - 1];
	option.screen = currentMenu;
	option.type = FEOPTION_REDIRECT;
	option.to = to;
	option.option = selectedOption;
	option.fadeIn = fadeIn;
	option.leftText = text;
	option.onlyApplyOnEnter = false;
	option.screenOptionOrder = screenOptionOrder;
	option.returnPrevPageFunc = nil;
}

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

	FrontendOption& option = customFrontendOptions[numCustomFrontendOptions - 1];
	option.screen = currentMenu;
	option.type = FEOPTION_GOBACK;
	option.fadeIn = fadeIn;
	option.leftText = text;
	option.onlyApplyOnEnter = false;
	option.screenOptionOrder = screenOptionOrder;
	option.returnPrevPageFunc = nil;
}
#endif