Cube  Diff

Differences From Artifact [b007e3d965]:

To Artifact [1d03f1ff21]:


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

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




-
+
-
-

-
+
-
-
-
-
+
-

-
-
-
+
+
+
-
-











-
+

-
-
-
+
+
+
-



+




-
-
-
-
-
-
-
-
-
-
-
-

-
+

-
+
-
-
+






+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
-
-
+
+
-
+
+




-
-
+
-
-
-
+
+
+




-
+

-
-
-
-
-
+
+
+
+
+





-
-
+
-
-
-
-
-
+
+
+
+
+








+




-
-
+
+
+
+
+
+





-
+







-
+





-
+
+
+
+
+

-
+
+
+


+

-
+
// menus.cpp: ingame menu system (also used for scores and serverlist)

#include "cube.h"

struct mitem {
#include <memory>
	char *text, *action;
};

struct gmenu {
#import "Menu.h"
	char *name;
	vector<mitem> items;
	int mwidth;
	int menusel;
#import "MenuItem.h"
};

vector<gmenu> menus;

int vmenu = -1;
static OFMutableArray<OFNumber *> *menuStack;
static OFMutableArray<Menu *> *menus;
static int vmenu = -1;

ivector menustack;

void
menuset(int menu)
{
	if ((vmenu = menu) >= 1)
		resetmovement(player1);
	if (vmenu == 1)
		menus[1].menusel = 0;
}

void
showmenu(OFString *name_)
showmenu(OFString *name)
{
	@autoreleasepool {
		const char *name = name_.UTF8String;
		loopv(menus) if (i > 1 && strcmp(menus[i].name, name) == 0)
	int i = 0;
	for (Menu *menu in menus) {
		if (i > 1 && [menu.name isEqual:name]) {
		{
			menuset(i);
			return;
		}
		i++;
	}
}
COMMAND(showmenu, ARG_1STR)

int
menucompare(mitem *a, mitem *b)
{
	int x = atoi(a->text);
	int y = atoi(b->text);
	if (x > y)
		return -1;
	if (x < y)
		return 1;
	return 0;
};

void
sortmenu(int start, int num)
sortmenu()
{
	qsort(&menus[0].items[start], num, sizeof(mitem),
	[menus[0].items sort];
	    (int(__cdecl *)(const void *, const void *))menucompare);
};
}

void refreshservers();

bool
rendermenu()
{
	@autoreleasepool {
	if (vmenu < 0) {
		menustack.setsize(0);
		return false;
	};
	if (vmenu == 1)
		refreshservers();
	gmenu &m = menus[vmenu];
	sprintf_sd(title)(vmenu > 1 ? "[ %s menu ]" : "%s", m.name);
	int mdisp = m.items.length();
	int w = 0;
	loopi(mdisp)
	{
		int x = text_width(m.items[i].text);
		if (x > w)
			w = x;
		if (vmenu < 0) {
			[menuStack removeAllObjects];
			return false;
		}

		if (vmenu == 1)
			refreshservers();

		Menu *m = menus[vmenu];
		OFString *title;
		if (vmenu > 1)
			title =
			    [OFString stringWithFormat:@"[ %@ menu ]", m.name];
		else
			title = m.name;
		int mdisp = m.items.count;
		int w = 0;
		loopi(mdisp)
		{
			int x = text_width(m.items[i].text);
			if (x > w)
				w = x;
	};
	int tw = text_width(title);
	if (tw > w)
		w = tw;
	int step = FONTH / 4 * 5;
	int h = (mdisp + 2) * step;
	int y = (VIRTH - h) / 2;
	int x = (VIRTW - w) / 2;
	blendbox(x - FONTH / 2 * 3, y - FONTH, x + w + FONTH / 2 * 3,
	    y + h + FONTH, true);
	draw_text(title, x, y, 2);
	y += FONTH * 2;
	if (vmenu) {
		int bh = y + m.menusel * step;
		blendbox(
		    x - FONTH, bh - 10, x + w + FONTH, bh + FONTH + 10, false);
		}
		int tw = text_width(title);
		if (tw > w)
			w = tw;
		int step = FONTH / 4 * 5;
		int h = (mdisp + 2) * step;
		int y = (VIRTH - h) / 2;
		int x = (VIRTW - w) / 2;
		blendbox(x - FONTH / 2 * 3, y - FONTH, x + w + FONTH / 2 * 3,
		    y + h + FONTH, true);
		draw_text(title, x, y, 2);
		y += FONTH * 2;
		if (vmenu) {
			int bh = y + m.menusel * step;
			blendbox(x - FONTH, bh - 10, x + w + FONTH,
			    bh + FONTH + 10, false);
	};
	loopj(mdisp)
	{
		draw_text(m.items[j].text, x, y, 2);
		y += step;
		}
		loopj(mdisp)
		{
			draw_text(m.items[j].text, x, y, 2);
			y += step;
	};
	return true;
		}
		return true;
};
	}
}

void
newmenu(OFString *name)
{
	@autoreleasepool {
		gmenu &menu = menus.add();
	if (menus == nil)
		menu.name = newstring(name.UTF8String);
		menu.menusel = 0;
	}
		menus = [[OFMutableArray alloc] init];

	[menus addObject:[[Menu alloc] initWithName:name]];
}
COMMAND(newmenu, ARG_1STR)

void
menumanual(int m, int n, char *text)
menumanual(int m, int n, OFString *text)
{
	if (!n)
		menus[m].items.setsize(0);
	mitem &mitem = menus[m].items.add();
	mitem.text = text;
	mitem.action = "";
	if (n == 0)
		[menus[m].items removeAllObjects];

	MenuItem *item = [[MenuItem alloc] initWithText:text action:@""];
	[menus[m].items addObject:item];
}

void
menuitem(OFString *text, OFString *action)
{
	@autoreleasepool {
		gmenu &menu = menus.last();
	Menu *menu = menus.lastObject;
		mitem &mi = menu.items.add();
		mi.text = newstring(text.UTF8String);
		mi.action =
		    action.length > 0 ? newstring(action.UTF8String) : mi.text;
	}

	MenuItem *item =
	    [[MenuItem alloc] initWithText:text
	                            action:(action.length > 0 ? action : text)];
	[menu.items addObject:item];
}
COMMAND(menuitem, ARG_2STR)

bool
menukey(int code, bool isdown)
{
	if (vmenu <= 0)
		return false;

	int menusel = menus[vmenu].menusel;
	if (isdown) {
		if (code == SDLK_ESCAPE) {
			menuset(-1);
			if (!menustack.empty())
				menuset(menustack.pop());

			if (menuStack.count > 0) {
				menuset(menuStack.lastObject.intValue);
				[menuStack removeLastObject];
			}

			return true;
		} else if (code == SDLK_UP || code == -4)
			menusel--;
		else if (code == SDLK_DOWN || code == -5)
			menusel++;
		int n = menus[vmenu].items.length();
		int n = menus[vmenu].items.count;
		if (menusel < 0)
			menusel = n - 1;
		else if (menusel >= n)
			menusel = 0;
		menus[vmenu].menusel = menusel;
	} else {
		if (code == SDLK_RETURN || code == -2) {
			char *action = menus[vmenu].items[menusel].action;
			OFString *action = menus[vmenu].items[menusel].action;
			if (vmenu == 1) {
				@autoreleasepool {
					connects(@(getservername(menusel)));
				}
			}
			menustack.add(vmenu);

			if (menuStack == nil)
				menuStack = [[OFMutableArray alloc] init];

			[menuStack addObject:@(vmenu)];
			menuset(-1);
			execute(action, true);

			std::unique_ptr<char> copy(strdup(action.UTF8String));
			execute(copy.get(), true);
		}
	}

	return true;
};
}