summaryrefslogtreecommitdiff
path: root/src/screen/analyze.c
blob: 450da41eabd3a3f2c38056ac52109ee1d92a03e8 (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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

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

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

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

#include "mainMenu.h"
#include "analyze.h"

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

static widget_t *image_backgorund;

static list_t *listWidgetLabelName;
static list_t *listWidgetLabelScore;
static list_t *listAnalyze;
static widget_t *widgetLabelMsg;

static widget_t *button_ok;

static analyze_t *newAnalyze(char *name, int score)
{
	analyze_t *new;

	new = malloc(sizeof(analyze_t));
	new->name = strdup(name);
	new->score = score;

	return new;
}

static void destroyAnalyze(analyze_t *p)
{
	free(p->name);
	free(p);
}

static void hotkey_escape()
{
	screen_set("mainMenu");
}

void analyze_start()
{
#ifndef NO_SOUND
	music_play("menu", MUSIC_GROUP_BASE);
#endif

	hot_key_register(SDLK_ESCAPE, hotkey_escape);
}

void analyze_draw()
{
	widget_t *this;
	int i;

	wid_image_draw(image_backgorund);

	for (i = 0; i < listWidgetLabelName->count; i++) {
		this = (widget_t *) listWidgetLabelName->list[i];
		label_draw(this);
	}

	for (i = 0; i < listWidgetLabelScore->count; i++) {
		this = (widget_t *) listWidgetLabelScore->list[i];
		label_draw(this);
	}

	label_draw(widgetLabelMsg);
	button_draw(button_ok);
}

void analyze_event()
{
	button_event(button_ok);
}

void analyze_stop()
{
	label_destroy(widgetLabelMsg);
	widgetLabelMsg = label_new("", WINDOW_SIZE_X / 2, 250, WIDGET_LABEL_CENTER);

	hot_key_unregister(SDLK_ESCAPE);
}

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

	button = (widget_t *) p;

	if (button == button_ok) {
		screen_set("mainMenu");
	}
}

void analyze_restart()
{
	list_destroy_item(listWidgetLabelName, label_destroy);
	list_destroy_item(listWidgetLabelScore, label_destroy);
	list_destroy_item(listAnalyze, destroyAnalyze);

	listWidgetLabelName = list_new();
	listWidgetLabelScore = list_new();
	listAnalyze = list_new();
}

void analyze_add(char *name, int score)
{
	list_add(listAnalyze, newAnalyze(name, score));
}

void analyze_set_msg(char *msg)
{
	label_destroy(widgetLabelMsg);
	widgetLabelMsg = label_new(msg, WINDOW_SIZE_X / 2, 250, WIDGET_LABEL_CENTER);
}

void analyze_end()
{
	int i;

	for (i = 0; i < listAnalyze->count; i++) {
		analyze_t *this;
		char *str;

		this = (analyze_t *) listAnalyze->list[i];

		list_add(listWidgetLabelName, label_new(this->name,
			 100, 200 + 20 * i,
			 WIDGET_LABEL_LEFT));

		str = getString(this->score);

		list_add(listWidgetLabelScore, label_new(str,
			 WINDOW_SIZE_X - 100, 200 + 20 * i,
			 WIDGET_LABEL_RIGHT));

		free(str);
	}
}

void analyze_init()
{
	image_t *image;

	image = image_get(IMAGE_GROUP_BASE, "screen_main");
	image_backgorund = wid_image_new(0, 0, image);

	listWidgetLabelName = list_new();
	listWidgetLabelScore = list_new();
	listAnalyze = list_new();

	button_ok = button_new(_("OK"), WINDOW_SIZE_X / 2 - WIDGET_BUTTON_WIDTH / 2,
				     WINDOW_SIZE_Y - 100, eventWidget);

	screen_register(screen_new("analyze", analyze_start, analyze_event,
			analyze_draw, analyze_stop));

	widgetLabelMsg = label_new("", WINDOW_SIZE_X / 2, 250, WIDGET_LABEL_CENTER);
}

void analyze_quit()
{
	wid_image_destroy(image_backgorund);

	list_destroy_item(listWidgetLabelName, label_destroy);
	list_destroy_item(listWidgetLabelScore, label_destroy);
	list_destroy_item(listAnalyze, destroyAnalyze);

	button_destroy(button_ok);
	label_destroy(widgetLabelMsg);
}