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

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

int lastOgScreen = MENUPAGES; // means no new pages

int numCustomFrontendOptions = 0;
int numCustomFrontendScreens = 0;

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

void GoBack()
{
	FrontEndMenuManager.SwitchToNewScreen(-1);
}

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].m_PreviousPage == MENUPAGE_NONE)
			break;

		++page;
	}
	return page;
}

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

	numCustomFrontendScreens++;
	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 = prevPage;
	aScreens[id].returnPrevPageFunc = returnPrevPageFunc;
	return id;
}

int8 RegisterNewOption()
{
	numCustomFrontendOptions++;
	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(CMenuScreenCustom::CMenuEntry));
			}
		}
	}
	optionCursor++;
	return curIdx;
}

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

void FrontendOptionAddBuiltinAction(const char* gxtKey, uint16 x, uint16 y, uint8 align, int action, int targetMenu, int saveSlot) {
	int8 screenOptionOrder = RegisterNewOption();

	CMenuScreenCustom::CMenuEntry &option = aScreens[currentMenu].m_aEntries[screenOptionOrder];

	// We can't use custom text on those :shrug:
	switch (action) {
		case MENUACTION_SCREENRES:
			strcpy(option.m_EntryName, "FED_RES");
			break;
		case MENUACTION_AUDIOHW:
			strcpy(option.m_EntryName, "FEA_3DH");
			break;
		default:
			strncpy(option.m_EntryName, gxtKey, 8);
			break;
	}
	option.m_X = x;
	option.m_Y = y;
	option.m_Align = align;
	option.m_Action = action;
	option.m_SaveSlot = saveSlot;
	option.m_TargetMenu = targetMenu;
}

void FrontendOptionAddSelect(const char* gxtKey, uint16 x, uint16 y, uint8 align, const char** rightTexts, int8 numRightTexts, int8 *var, bool onlyApplyOnEnter, ChangeFunc changeFunc, const char* saveName, bool disableIfGameLoaded)
{	
	int8 screenOptionOrder = RegisterNewOption();

	CMenuScreenCustom::CMenuEntry &option = aScreens[currentMenu].m_aEntries[screenOptionOrder];
	option.m_Action = MENUACTION_CFO_SELECT;
	option.m_X = x;
	option.m_Y = y;
	option.m_Align = align;
	strncpy(option.m_EntryName, gxtKey, 8);
	option.m_CFOSelect = new CCFOSelect();
	option.m_CFOSelect->rightTexts = (char**)malloc(numRightTexts * sizeof(char*));
	memcpy(option.m_CFOSelect->rightTexts, rightTexts, numRightTexts * sizeof(char*));
	option.m_CFOSelect->numRightTexts = numRightTexts;
	option.m_CFOSelect->value = var;
	if (var) {
		option.m_CFOSelect->displayedValue = *var;
		option.m_CFOSelect->lastSavedValue = *var;
	}
	option.m_CFOSelect->save = saveName;
	option.m_CFOSelect->onlyApplyOnEnter = onlyApplyOnEnter;
	option.m_CFOSelect->changeFunc = changeFunc;
	option.m_CFOSelect->disableIfGameLoaded = disableIfGameLoaded;
}

void FrontendOptionAddDynamic(const char* gxtKey, uint16 x, uint16 y, uint8 align, DrawFunc drawFunc, int8 *var, ButtonPressFunc buttonPressFunc, const char* saveName)
{
	int8 screenOptionOrder = RegisterNewOption();

	CMenuScreenCustom::CMenuEntry &option = aScreens[currentMenu].m_aEntries[screenOptionOrder];
	option.m_Action = MENUACTION_CFO_DYNAMIC;
	option.m_X = x;
	option.m_Y = y;
	option.m_Align = align;
	strncpy(option.m_EntryName, gxtKey, 8);
	option.m_CFODynamic = new CCFODynamic();
	option.m_CFODynamic->drawFunc = drawFunc;
	option.m_CFODynamic->buttonPressFunc = buttonPressFunc;
	option.m_CFODynamic->value = var;
	option.m_CFODynamic->save = saveName;
}

// lineHeight = 0 means game will use MENU_DEFAULT_LINE_HEIGHT
uint8 FrontendScreenAdd(const char* gxtKey, int prevPage, int lineHeight, bool showLeftRightHelper, ReturnPrevPageFunc returnPrevPageFunc) {

	uint8 screenOrder = RegisterNewScreen(gxtKey, prevPage, returnPrevPageFunc);

	CCustomScreenLayout *screen = new CCustomScreenLayout();
	aScreens[screenOrder].layout = screen;
	screen->lineHeight = lineHeight;
	screen->showLeftRightHelper = showLeftRightHelper;

	return screenOrder;
}
#endif