summaryrefslogtreecommitdiff
path: root/src/screen/choiceArena.c
blob: bf60c46676b4c93701e6c2a2047bb8e73a4f81f3 (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

#include <stdio.h>
#include <assert.h>

#include "main.h"
#include "list.h"
#include "arenaFile.h"

#include "interface.h"
#include "screen.h"
#include "image.h"
#include "hotKey.h"

#ifndef NO_SOUND
#    include "music.h"
#endif

#include "mainMenu.h"
#include "choiceArena.h"

#include "widget.h"
#include "widget_label.h"
#include "widget_button.h"
#include "widget_image.h"
#include "widget_buttonimage.h"

static widget_t *image_backgorund;

static widget_t *button_play;
static widget_t *button_back;
static widget_t *button_next;
static widget_t *button_prev;

//static list_t *listWidgetButtonimage; // map buttons to be showed and handled - well, show_map_buttons is used instead
static list_t *listAllWidgetButtonimage; // all map buttons
static int handled_map_button_group; // 0 - first 6 buttons, 1 - second 6 buttons, ....
static void activateHandledMapButtonGroup();
static struct show_map_buttons { 
	// geez, it'd be nicer to used two lists
	// but how the hell is list_t supposed to be used?! :-)
	int min; // see activateHandledMapButtonGroup for desc
	int max; //
	int next; // 0: max is last item+1 in listAllWidgetButtonimage, otherwise 1
	int prev; // 0: min is first item in listAllWidgetButtonimage, otherwise 1
} show_map_buttons = { 0,0,0,0 } ;
static arenaFile_t *currentArena;

static void hotkey_escape()
{
	setScreen("gameType");
}

void startScreenChoiceArena()
{
#ifndef NO_SOUND
	playMusic("menu", MUSIC_GROUP_BASE);
#endif

	registerHotKey(SDLK_ESCAPE, hotkey_escape);
}

void drawScreenChoiceArena()
{
	int i;

	drawWidgetImage(image_backgorund);

	drawWidgetButton(button_play);
	drawWidgetButton(button_back);
	if (show_map_buttons.next) { drawWidgetButton(button_next); }
	if (show_map_buttons.prev) { drawWidgetButton(button_prev); }

	for (i = show_map_buttons.min ; i < show_map_buttons.max; i++) {
		drawWidgetButtonimage((widget_t*)listAllWidgetButtonimage->list[i]);
	}
}

void eventScreenChoiceArena()
{
	int i;

	eventWidgetButton(button_play);
	eventWidgetButton(button_back);
	if (show_map_buttons.next) { eventWidgetButton(button_next); }
	if (show_map_buttons.prev) { eventWidgetButton(button_prev); }

	for (i = show_map_buttons.min ; i < show_map_buttons.max; i++) {
		eventWidgetButtonimage((widget_t*)listAllWidgetButtonimage->list[i]);
	}
}

void stopScreenChoiceArena()
{
	unregisterHotKey(SDLK_ESCAPE);
}

static void eventWidgetButtonImage(void *p)
{
	widget_t *buttonimage;
	int i;

	buttonimage = (widget_t *) (p);

	for (i = show_map_buttons.min; i < show_map_buttons.max; i++) { 
		widget_t *this;

		this = (widget_t *) (listAllWidgetButtonimage->list[i]);

		if (buttonimage == this) {
			setWidgetButtonimageActive(this, TRUE);
			currentArena = getArenaFile(i);
		} else {
			setWidgetButtonimageActive(this, FALSE);
		}
	}
}

arenaFile_t *getChoiceArena()
{
	return currentArena;
}

void setChoiceArena(arenaFile_t * arenaFile)
{
	widget_t *widget_buttonimage;
	int id;

	id = getArenaFileID(arenaFile);
	currentArena = arenaFile;

	if (id >= 0) {
		widget_buttonimage = (widget_t *) listAllWidgetButtonimage->list[id];
		setWidgetButtonimageActive(widget_buttonimage, TRUE);
	}
}

static void eventWidget(void *p)
{
	widget_t *button;

	button = (widget_t *) (p);

	if (button == button_play) {
		setScreen("world");
	}

	if (button == button_back) {
		setScreen("gameType");
	}
	if (button == button_next) {
		if (show_map_buttons.next) {
			handled_map_button_group++;
			activateHandledMapButtonGroup();
		}
	}
	if (button == button_prev) {
		if (show_map_buttons.prev) {
			handled_map_button_group--;
			activateHandledMapButtonGroup();
		}
	}
}

// groups of 6 map buttons to be shown and handled
// the group is set by static global variable handled_map_button_group
// sets static global show_map_buttons.min to index of first button to be drawn in listAllButtonimage
// and show_map_buttons.max to index of last button to be drawn+1 in listAllButtonimage
static void activateHandledMapButtonGroup(void) {
	int min = handled_map_button_group * 6;
	min = min < 0 ? 0 : min;
	min = min <= getArenaCount() ? min : getArenaCount();
	int max = min+6 <= getArenaCount() ? min+6 : getArenaCount();

	if (min) { show_map_buttons.prev = 1; }
	else { show_map_buttons.prev = 0; }
	if (max < getArenaCount()) { show_map_buttons.next = 1; }
	else { show_map_buttons.next = 0; }
	show_map_buttons.min = min;
	show_map_buttons.max = max;
}

void initScreenChoiceArena()
{
	image_t *image;
	int i;

	image = getImage(IMAGE_GROUP_BASE, "screen_main");
	image_backgorund = newWidgetImage(0, 0, image);

	button_back = newWidgetButton(_("back"), 100, WINDOW_SIZE_Y - 100, eventWidget);
	button_play = newWidgetButton(_("play"), WINDOW_SIZE_X - 200, WINDOW_SIZE_Y - 100, eventWidget);

	button_prev = newWidgetButton(_("prev maps"), 250, WINDOW_SIZE_Y - 100, eventWidget);
	button_next = newWidgetButton(_("next maps"), WINDOW_SIZE_X-350, WINDOW_SIZE_Y - 100, eventWidget);

	listAllWidgetButtonimage = newList();
	currentArena = NULL;

	for (i = 0; i < getArenaCount(); i++) {
		widget_t *widget_buttonimage;
		arenaFile_t *arenaFile;
		int x, y;

		arenaFile = getArenaFile(i);
		//image = addImageData(getArenaImage(i), IMAGE_NO_ALPHA, "none", IMAGE_GROUP_BASE);
		image = loadImageFromArena(arenaFile,  getArenaImage(arenaFile),
					   IMAGE_GROUP_BASE, "none", IMAGE_NO_ALPHA);

		// only 6 map buttons can be shown at once;
		// set buttons' positions in each group to the corresponding values
		// handled_map_button_group and activateHandledMapButtonGroup is used 
		// to choose group to be shown and handled
		int pos = i % 6 ;
		x = 100 + 200 * (pos - 3 * (pos / 3));
		y = 150 + 200 * (pos / 3);

		widget_buttonimage = newWidgetButtonimage(image, x, y, eventWidgetButtonImage);
/*
		if( i == choiceArenaId )
		{
			widget_buttonimage->active = 1;
		}
*/
		addList(listAllWidgetButtonimage, widget_buttonimage);
		handled_map_button_group = 0;
		activateHandledMapButtonGroup();
	}

	registerScreen( newScreen("chiceArena", startScreenChoiceArena,
			eventScreenChoiceArena, drawScreenChoiceArena,
			stopScreenChoiceArena));
}

void quitScreenChoiceArena()
{
	destroyWidgetImage(image_backgorund);

	destroyWidgetButton(button_play);
	destroyWidgetButton(button_back);

	destroyWidgetButton(button_next);
	destroyWidgetButton(button_prev);

	destroyListItem(listAllWidgetButtonimage, destroyWidgetButtonimage);
}