Debian Codegraph @

games/scores/dialog.vala

raw ◀ ▶
1 Debian patch touch this file
1 /* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright © 2014 Nikhar Agrawal
4 *
5 * This file is part of libgnome-games-support.
6 *
7 * libgnome-games-support is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * libgnome-games-support is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with libgnome-games-support. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 namespace Games {
22 namespace Scores {
23
24 private class Dialog : Gtk.Dialog
25 {
26 private Context context;
27 private Category? active_category = null;
28 private int rows_to_display = 10;
29
30 private Gtk.ComboBoxText? combo = null;
31 private Gtk.Label? category_label = null;
32 private Gtk.HeaderBar? headerbar = null;
33 private Gtk.Grid grid;
34
35 private Style scores_style;
36 private Score? new_high_score;
37 private Category? scores_active_category;
38
39 public Dialog (Context context, string category_type, Style style, Score? new_high_score, Category? current_cat, Gtk.Window window, string icon_name)
40 {
41 Object (use_header_bar : 1);
42
43 resizable = false;
44
45 this.context = context;
46 this.transient_for = window;
47 this.new_high_score = new_high_score;
48
49 scores_style = style;
50 scores_active_category = current_cat;
51
52 headerbar = (Gtk.HeaderBar) this.get_header_bar ();
53
54 headerbar.show_close_button = (new_high_score == null);
55
56 if (new_high_score != null)
57 /* Appears at the top of the dialog, as the heading of the dialog */
58 headerbar.title = _("Congratulations!");
59 else if (scores_style == Style.POINTS_GREATER_IS_BETTER || scores_style == Style.POINTS_LESS_IS_BETTER)
60 headerbar.title = _("High Scores");
61 else
62 headerbar.title = _("Best Times");
63
64 var vbox = this.get_content_area ();
65 vbox.orientation = Gtk.Orientation.VERTICAL;
66
67 if (!context.has_scores () && new_high_score == null)
68 {
69 vbox.spacing = 4;
70 vbox.hexpand = true;
71 vbox.vexpand = true;
72 vbox.valign = Gtk.Align.CENTER;
73 vbox.get_style_context ().add_class ("dim-label");
74
75 var image = new Gtk.Image ();
76 image.icon_name = icon_name + "-symbolic";
77 image.pixel_size = 64;
78 image.opacity = 0.2;
79 vbox.add (image);
80
81 var title_label = new Gtk.Label ("<b><span size=\"large\">" + _("No scores yet") + "</span></b>");
82 title_label.use_markup = true;
83 vbox.add (title_label);
84
85 var description_label = new Gtk.Label (_("Play some games and your scores will show up here."));
86 vbox.add (description_label);
87
88 vbox.show_all ();
89
90 width_request = 450;
91 height_request = 500;
92
93 return;
94 }
95
96 vbox.spacing = 20;
97
98 var catbar = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 12);
99 catbar.margin_top = 20;
100 catbar.margin_start = 20;
101 catbar.margin_end = 20;
102 catbar.halign = Gtk.Align.CENTER;
103
104 vbox.add (catbar);
105
106 var hdiv = new Gtk.Separator (Gtk.Orientation.HORIZONTAL);
107 vbox.add (hdiv);
108
109 var label = new Gtk.Label (category_type);
110 label.use_markup = true;
111 label.halign = Gtk.Align.CENTER;
112 catbar.add (label);
113
114 var categories = context.get_categories ();
115 if (new_high_score != null || categories.length () == 1)
116 {
117 if (new_high_score == null)
118 scores_active_category = ((!) categories.first ()).data;
119 category_label = new Gtk.Label (scores_active_category.name);
120 category_label.use_markup = true;
121 category_label.halign = Gtk.Align.CENTER;
122 category_label.valign = Gtk.Align.CENTER;
123 catbar.add (category_label);
124 }
125 else
126 {
127 combo = new Gtk.ComboBoxText ();
128 combo.focus_on_click = false;
129 catbar.add (combo);
130 combo.changed.connect (load_scores);
131 }
132
133 grid = new Gtk.Grid ();
134 vbox.add (grid);
135
136 grid.row_homogeneous = true;
137 grid.column_spacing = 40;
138 grid.margin_start = 30;
139 grid.margin_end = 30;
140 grid.margin_bottom = 20;
141 grid.halign = Gtk.Align.CENTER;
142
143 /* A column heading in the scores dialog */
144 string string_rank = _("Rank");
145 var label_column_1 = new Gtk.Label ("<span weight='bold'>" + string_rank + "</span>");
146 label_column_1.use_markup = true;
147 grid.attach (label_column_1, 0, 0, 1, 1);
148
149 string score_or_time = "";
150
151 if (scores_style == Style.POINTS_GREATER_IS_BETTER || scores_style == Style.POINTS_LESS_IS_BETTER)
152 /* A column heading in the scores dialog */
153 score_or_time = _("Score");
154 else
155 score_or_time = _("Time");
156
157 var label_column_2 = new Gtk.Label ("<span weight='bold'>" + score_or_time + "</span>");
158 label_column_2.use_markup = true;
159 grid.attach (label_column_2, 1, 0, 1, 1);
160
161 /* A column heading in the scores dialog */
162 string string_player = _("Player");
163 var label_column_3 = new Gtk.Label ("<span weight='bold'>" + string_player + "</span>");
164 label_column_3.use_markup = true;
165 grid.attach (label_column_3, 2, 0, 1, 1);
166
167 grid.baseline_row = 0;
168 fill_grid_with_labels ();
169
170 if (new_high_score != null)
171 /* Appears on the top right corner of the dialog. Clicking the button closes the dialog. */
172 add_button (_("_Done"), Gtk.ResponseType.OK).get_style_context ().add_class ("suggested-action");
173
174 load_categories ();
175
176 vbox.show_all ();
177 }
178
179 private void fill_grid_with_labels ()
180 {
181 for (int row = 1; row <= rows_to_display; row++)
182 {
183 for (int column = 0; column <= 1; column++)
184 {
185 var label = new Gtk.Label ("");
186 label.visible = true;
187 label.halign = Gtk.Align.CENTER;
188 label.valign = Gtk.Align.CENTER;
189
190 grid.attach (label, column, row, 1, 1);
191 }
192
193 var stack = new Gtk.Stack ();
194 stack.visible = true;
195 stack.homogeneous = false;
196 stack.vhomogeneous = true;
197 stack.transition_type = Gtk.StackTransitionType.NONE;
198
199 var label = new Gtk.Label ("");
200 label.visible = true;
201 label.justify = Gtk.Justification.CENTER;
202 label.valign = Gtk.Align.CENTER;
203 stack.add_named (label, "label");
204
205 var entry = new Gtk.Entry ();
206 entry.visible = true;
207 entry.set_size_request (20, 20);
208 entry.hexpand = false;
209 entry.vexpand = false;
210 stack.add_named (entry, "entry");
211
212 stack.visible_child_name = "label";
213 grid.attach (stack, 2, row, 1, 1);
214 }
215 }
216
217 /* load names and keys of all categories in ComboBoxText */
218 private void load_categories ()
219 {
220 /* If we are adding a high score, we don't wish to load all categories. We only wish to load scores of active category. */
221 if (new_high_score != null || combo == null)
222 {
223 load_scores ();
224 }
225
226 if (combo == null)
227 return;
228
229 var categories = context.get_categories ();
230 categories.foreach ((x) => combo.append (x.key, x.name));
231
232 if (scores_active_category == null)
233 combo.active_id = categories.nth_data (0).key;
234 else
235 combo.active_id = scores_active_category.key;
236
237 if (active_category == null)
238 {
239 active_category = new Category (categories.nth_data (0).key, categories.nth_data (0).name);
240 }
241 else
242 {
243 active_category.key = categories.nth_data (0).key;
244 active_category.name = categories.nth_data (0).name;
245 }
246 }
247
248 /* loads the scores of current active_category */
249 private void load_scores ()
250 {
251 if (new_high_score != null || combo == null)
252 active_category = new Category (scores_active_category.key, scores_active_category.name);
253 else
254 active_category = new Category (combo.get_active_id (), combo.get_active_text ());
255
256 var best_n_scores = context.get_high_scores (active_category, rows_to_display);
257
258 int row_count = 1;
259
260 foreach (var score in best_n_scores)
261 {
262 display_single_score (score, row_count, best_n_scores.size);
263 row_count++;
264 }
265
266 if (row_count < rows_to_display + 1)
267 make_remaining_labels_empty (row_count);
268 }
269
270 /* Use Stack to switch between Entry and Label. All data displayed as labels except when a new high score is being added.
271 In which case, Label needs to be replaced by Entry allowing for player to enter name. */
272 private void display_single_score (Score score, int row_count, uint no_scores)
273 {
274 var rank_label = (Gtk.Label) grid.get_child_at (0, row_count);
275 rank_label.set_text (row_count.to_string ());
276
277 var score_label = (Gtk.Label) grid.get_child_at (1, row_count);
278 if (scores_style == Style.POINTS_GREATER_IS_BETTER || scores_style == Style.POINTS_LESS_IS_BETTER)
279 {
280 score_label.set_text (score.score.to_string ());
281 }
282 else
283 {
284 var minutes = score.score / 60;
285 var seconds = score.score % 60;
286 score_label.set_text ("%s %s".printf (
287 /* Time which may be displayed on a scores dialog. */
•288 dngettext (GETTEXT_PACKAGE, "%ld minute", "%ld minutes", minutes).printf (minutes),
289 /* Time which may be displayed on a scores dialog. */
•290 dngettext (GETTEXT_PACKAGE, "%ld second", "%ld seconds", seconds).printf (seconds)));
291 }
292
293 if (new_high_score != null && Score.equals (score, new_high_score))
294 {
295 if (no_scores > 1 && row_count == 1)
296 headerbar.subtitle = _("Your score is the best!");
297 else
298 headerbar.subtitle = _("Your score has made the top ten.");
299
300 var temp_stack = (Gtk.Stack) grid.get_child_at (2, row_count);
301 temp_stack.visible_child_name = "entry";
302
303 var entry = (Gtk.Entry) temp_stack.get_visible_child ();
304 entry.text = score.user;
305 entry.notify["text"].connect (() => {
306 context.update_score_name (score, active_category, entry.get_text ());
307 score.user = entry.get_text ();
308 });
309 }
310
311 var name_stack = (Gtk.Stack) grid.get_child_at (2, row_count);
312 var widget = name_stack.get_visible_child ();
313
314 if (name_stack.get_visible_child_name () == "label")
315 {
316 var user_label = (Gtk.Label) widget;
317 user_label.set_text (score.user);
318 }
319 else
320 {
321 var entry = (Gtk.Entry) widget;
322 entry.text = score.user;
323 }
324 }
325
326 /* Fill all labels from row row_count onwards with empty strings. */
327 private void make_remaining_labels_empty (int row_count)
328 {
329 for (int i = row_count; i <= rows_to_display; i++)
330 {
331 for (int j = 0; j <= 1; j++)
332 {
333 var label = (Gtk.Label) grid.get_child_at (j, i);
334 label.set_text ("");
335 }
336
337 var stack = (Gtk.Stack) grid.get_child_at (2, i);
338 var label = (Gtk.Label) stack.get_visible_child ();
339 label.set_text ("");
340 }
341 }
342 }
343
344 } /* namespace Scores */
345 } /* namespace Games */