| 1 | /* |
| 2 | * Copyright 2024 Andrey Kutejko <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * For more details see the file COPYING. |
| 18 | */ |
| 19 | |
| 20 | use super::{ROWS_TO_DISPLAY, scores::ScoreList}; |
| 21 | use gettextrs::gettext; |
| 22 | use gtk::prelude::*; |
| 23 | |
| 24 | pub fn create_scores_grid() -> gtk::Grid { |
| 25 | let grid = gtk::Grid::builder() |
| 26 | .row_homogeneous(true) |
| 27 | .column_spacing(40) |
| 28 | .margin_start(30) |
| 29 | .margin_end(30) |
| 30 | .margin_bottom(20) |
| 31 | .halign(gtk::Align::Center) |
| 32 | .baseline_row(0) |
| 33 | .build(); |
| 34 | |
| 35 | grid.attach(&title_label(&gettext("Rank")), 0, 0, 1, 1); |
| 36 | grid.attach(&title_label(&gettext("Score")), 1, 0, 1, 1); |
| 37 | grid.attach(&title_label(&gettext("Player")), 2, 0, 1, 1); |
| 38 | |
| 39 | for row in 1..=ROWS_TO_DISPLAY { |
| 40 | grid.attach( |
| 41 | >k::Label::builder() |
| 42 | .halign(gtk::Align::Center) |
| 43 | .valign(gtk::Align::Center) |
| 44 | .build(), |
| 45 | 0, |
| 46 | row as i32, |
| 47 | 1, |
| 48 | 1, |
| 49 | ); |
| 50 | grid.attach( |
| 51 | >k::Label::builder() |
| 52 | .halign(gtk::Align::Center) |
| 53 | .valign(gtk::Align::Center) |
| 54 | .build(), |
| 55 | 1, |
| 56 | row as i32, |
| 57 | 1, |
| 58 | 1, |
| 59 | ); |
| 60 | grid.attach( |
| 61 | >k::Label::builder() |
| 62 | .justify(gtk::Justification::Center) |
| 63 | .valign(gtk::Align::Center) |
| 64 | .build(), |
| 65 | 2, |
| 66 | row as i32, |
| 67 | 1, |
| 68 | 1, |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | grid |
| 73 | } |
| 74 | |
| 75 | pub fn scores_grid_update(grid: >k::Grid, score_list: &ScoreList) { |
| 76 | for row in 1..=ROWS_TO_DISPLAY { |
| 77 | let rank_label = grid |
| 78 | .child_at(0, row as i32) |
| 79 | .and_downcast::<gtk::Label>() |
| 80 | .unwrap(); |
| 81 | let score_label = grid |
| 82 | .child_at(1, row as i32) |
| 83 | .and_downcast::<gtk::Label>() |
| 84 | .unwrap(); |
| 85 | let user_label = grid |
| 86 | .child_at(2, row as i32) |
| 87 | .and_downcast::<gtk::Label>() |
| 88 | .unwrap(); |
| 89 | |
| 90 | match score_list.scores.get(row - 1) { |
| 91 | Some(score) => { |
| 92 | rank_label.set_text(&row.to_string()); |
| 93 | score_label.set_text(&score.score.to_string()); |
| 94 | user_label.set_text(&score.user); |
| 95 | } |
| 96 | None => { |
| 97 | rank_label.set_text(""); |
| 98 | score_label.set_text(""); |
| 99 | user_label.set_text(""); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | fn title_label(label: &str) -> gtk::Label { |
| 106 | gtk::Label::builder() |
| 107 | .label(format!("<b>{}</b>", label)) |
| 108 | .use_markup(true) |
| 109 | .build() |
| 110 | } |