| 1 | /* |
| 2 | * Copyright 2020-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 crate::assets::Assets; |
| 21 | use crate::game::{Game, PlayerCommand, State}; |
| 22 | use crate::game_area::GameArea; |
| 23 | use crate::game_config::GameConfigs; |
| 24 | use crate::properties::Properties; |
| 25 | use crate::scores::scores::{Category, show_scores}; |
| 26 | use crate::window_size::remember_window_size; |
| 27 | use adw::{prelude::*, subclass::prelude::*}; |
| 28 | use gettextrs::gettext; |
| 29 | use gtk::{gio, glib}; |
| 30 | use rand::rng; |
| 31 | use std::error::Error; |
| 32 | use std::rc::Rc; |
| 33 | |
| 34 | mod imp { |
| 35 | use super::*; |
| 36 | use std::cell::OnceCell; |
| 37 | |
| 38 | pub struct RobotsWindow { |
| 39 | pub window_title: adw::WindowTitle, |
| 40 | pub safe_teleports_label: gtk::Label, |
| 41 | pub game_area: OnceCell<GameArea>, |
| 42 | pub settings: OnceCell<gio::Settings>, |
| 43 | } |
| 44 | |
| 45 | #[glib::object_subclass] |
| 46 | impl ObjectSubclass for RobotsWindow { |
| 47 | const NAME: &'static str = "RobotsWindow"; |
| 48 | type Type = super::RobotsWindow; |
| 49 | type ParentType = adw::ApplicationWindow; |
| 50 | |
| 51 | fn new() -> Self { |
| 52 | Self { |
| 53 | window_title: adw::WindowTitle::builder().title(gettext("Robots")).build(), |
| 54 | safe_teleports_label: gtk::Label::builder().use_markup(true).build(), |
| 55 | game_area: Default::default(), |
| 56 | settings: Default::default(), |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | fn class_init(klass: &mut Self::Class) { |
| 61 | klass.install_action("win.random-teleport", None, |obj, _, _| { |
| 62 | obj.imp().random_teleport_cb(); |
| 63 | }); |
| 64 | klass.install_action("win.safe-teleport", None, |obj, _, _| { |
| 65 | obj.imp().safe_teleport_cb(); |
| 66 | }); |
| 67 | klass.install_action("win.wait", None, |obj, _, _| { |
| 68 | obj.imp().wait_cb(); |
| 69 | }); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | impl ObjectImpl for RobotsWindow {} |
| 74 | impl WidgetImpl for RobotsWindow {} |
| 75 | impl WindowImpl for RobotsWindow {} |
| 76 | impl ApplicationWindowImpl for RobotsWindow {} |
| 77 | impl AdwApplicationWindowImpl for RobotsWindow {} |
| 78 | |
| 79 | impl RobotsWindow { |
| 80 | fn random_teleport_cb(&self) { |
| 81 | self.game_area |
| 82 | .get() |
| 83 | .unwrap() |
| 84 | .player_command(PlayerCommand::RandomTeleport); |
| 85 | } |
| 86 | |
| 87 | fn safe_teleport_cb(&self) { |
| 88 | self.game_area |
| 89 | .get() |
| 90 | .unwrap() |
| 91 | .player_command(PlayerCommand::SafeTeleport); |
| 92 | } |
| 93 | |
| 94 | fn wait_cb(&self) { |
| 95 | self.game_area |
| 96 | .get() |
| 97 | .unwrap() |
| 98 | .player_command(PlayerCommand::Wait); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | pub fn button_box(safe_teleports_label: >k::Label) -> gtk::Box { |
| 103 | let hbox = gtk::Box::builder() |
| 104 | .orientation(gtk::Orientation::Horizontal) |
| 105 | .spacing(10) |
| 106 | .build(); |
| 107 | let size_group = gtk::SizeGroup::new(gtk::SizeGroupMode::Both); |
| 108 | |
| 109 | { |
| 110 | let label = gtk::Label::builder() |
| 111 | .label(gettext("Teleport _Randomly")) |
| 112 | .use_underline(true) |
| 113 | .margin_top(15) |
| 114 | .margin_bottom(15) |
| 115 | .build(); |
| 116 | let button = gtk::Button::builder() |
| 117 | .action_name("win.random-teleport") |
| 118 | .child(&label) |
| 119 | .hexpand(true) |
| 120 | .build(); |
| 121 | size_group.add_widget(&button); |
| 122 | hbox.append(&button); |
| 123 | } |
| 124 | |
| 125 | { |
| 126 | let vbox = gtk::Box::builder() |
| 127 | .orientation(gtk::Orientation::Vertical) |
| 128 | .margin_top(15) |
| 129 | .margin_bottom(15) |
| 130 | .build(); |
| 131 | let label = gtk::Label::builder() |
| 132 | .label(gettext("Teleport _Safely")) |
| 133 | .use_underline(true) |
| 134 | .build(); |
| 135 | vbox.append(&label); |
| 136 | vbox.append(safe_teleports_label); |
| 137 | let button = gtk::Button::builder() |
| 138 | .action_name("win.safe-teleport") |
| 139 | .child(&vbox) |
| 140 | .hexpand(true) |
| 141 | .build(); |
| 142 | size_group.add_widget(&button); |
| 143 | hbox.append(&button); |
| 144 | } |
| 145 | |
| 146 | { |
| 147 | let label = gtk::Label::builder() |
| 148 | .label(gettext("_Wait for Robots")) |
| 149 | .use_underline(true) |
| 150 | .margin_top(15) |
| 151 | .margin_bottom(15) |
| 152 | .build(); |
| 153 | let button = gtk::Button::builder() |
| 154 | .action_name("win.wait") |
| 155 | .child(&label) |
| 156 | .hexpand(true) |
| 157 | .build(); |
| 158 | size_group.add_widget(&button); |
| 159 | hbox.append(&button); |
| 160 | } |
| 161 | |
| 162 | hbox |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | glib::wrapper! { |
| 167 | pub struct RobotsWindow(ObjectSubclass<imp::RobotsWindow>) |
| 168 | @extends adw::ApplicationWindow, gtk::ApplicationWindow, gtk::Window, gtk::Widget, |
| 169 | @implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable, |
| 170 | gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager; |
| 171 | } |
| 172 | |
| 173 | impl RobotsWindow { |
| 174 | pub fn new( |
| 175 | application: &impl IsA<gtk::Application>, |
| 176 | settings: &gio::Settings, |
| 177 | game_configs: GameConfigs, |
| 178 | assets: &Rc<dyn Assets>, |
| 179 | ) -> Result<Self, Box<dyn Error>> { |
| 180 | let this: Self = glib::Object::builder() |
| 181 | .property("application", application) |
| 182 | .build(); |
| 183 | |
| 184 | remember_window_size(this.upcast_ref(), settings); |
| 185 | this.imp().settings.set(settings.clone()).ok().unwrap(); |
| 186 | |
| 187 | let vbox = gtk::Box::builder() |
| 188 | .orientation(gtk::Orientation::Vertical) |
| 189 | .build(); |
| 190 | this.set_content(Some(&vbox)); |
| 191 | |
| 192 | let headerbar = adw::HeaderBar::builder() |
| 193 | .title_widget(&this.imp().window_title) |
| 194 | .build(); |
| 195 | vbox.append(&headerbar); |
| 196 | |
| 197 | let appmenu = gio::Menu::new(); |
| 198 | appmenu.append_section(None, &{ |
| 199 | let section = gio::Menu::new(); |
| 200 | section.append_item(&gio::MenuItem::new( |
| 201 | Some(&gettext("_New Game")), |
| 202 | Some("app.new-game"), |
| 203 | )); |
| 204 | section.append_item(&gio::MenuItem::new( |
| 205 | Some(&gettext("_Scores")), |
| 206 | Some("app.scores"), |
| 207 | )); |
| 208 | section |
| 209 | }); |
| 210 | appmenu.append_section(None, &{ |
| 211 | let section = gio::Menu::new(); |
| 212 | section.append_item(&gio::MenuItem::new( |
| 213 | Some(&gettext("_Preferences")), |
| 214 | Some("app.preferences"), |
| 215 | )); |
| 216 | section.append_item(&gio::MenuItem::new( |
| 217 | Some(&gettext("_Help")), |
| 218 | Some("app.help"), |
| 219 | )); |
| 220 | section.append_item(&gio::MenuItem::new( |
| 221 | Some(&gettext("_About Robots")), |
| 222 | Some("app.about"), |
| 223 | )); |
| 224 | section |
| 225 | }); |
| 226 | |
| 227 | let menu_button = gtk::MenuButton::builder() |
| 228 | .icon_name("open-menu-symbolic") |
| 229 | .menu_model(&appmenu) |
| 230 | .build(); |
| 231 | headerbar.pack_end(&menu_button); |
| 232 | |
| 233 | let game = Game::new(game_configs.best_match(&settings.selected_config()), rng()); |
| 234 | game.start_new_game(); |
| 235 | |
| 236 | let game_area = GameArea::new(game_configs, assets, settings)?; |
| 237 | game_area.connect_updated(glib::clone!( |
| 238 | #[weak] |
| 239 | this, |
| 240 | move |ga| this.update_game_status(ga.game().as_ref().unwrap()) |
| 241 | )); |
| 242 | |
| 243 | let gridframe = gtk::AspectFrame::builder() |
| 244 | .ratio((game.width() as f32) / (game.height() as f32)) |
| 245 | .hexpand(true) |
| 246 | .vexpand(true) |
| 247 | .child(&game_area) |
| 248 | .build(); |
| 249 | |
| 250 | game_area.set_game(game); |
| 251 | this.imp().game_area.set(game_area).ok().unwrap(); |
| 252 | |
| 253 | let toolbar_view = adw::ToolbarView::builder().content(&gridframe).build(); |
| 254 | |
| 255 | let action_bar = gtk::ActionBar::builder().build(); |
| 256 | action_bar.set_center_widget(Some(&imp::button_box(&this.imp().safe_teleports_label))); |
| 257 | toolbar_view.add_bottom_bar(&action_bar); |
| 258 | |
| 259 | vbox.append(&toolbar_view); |
| 260 | |
| 261 | Ok(this) |
| 262 | } |
| 263 | |
| 264 | fn update_game_status(&self, game: &Game) { |
| 265 | let status = game.status(); |
| 266 | |
| 267 | self.imp().window_title.set_subtitle( |
| 268 | &gettext("Level: {level}\tScore: {score}") |
| 269 | .replace("{level}", &status.current_level.to_string()) |
| 270 | .replace("{score}", &status.score.to_string()), |
| 271 | ); |
| 272 | |
| 273 | let remaining_teleports_text = |
| 274 | gettext("(Remaining: %d)").replacen("%d", &status.safe_teleports.to_string(), 1); |
| 275 | self.imp() |
| 276 | .safe_teleports_label |
| 277 | .set_markup(&format!("<small>{}</small>", remaining_teleports_text)); |
| 278 | |
| 279 | let is_playing = game.state() != State::Complete && game.state() != State::Dead; |
| 280 | self.enable_action("random-teleport", is_playing); |
| 281 | self.enable_action("safe-teleport", is_playing && status.safe_teleports > 0); |
| 282 | self.enable_action("wait", is_playing); |
| 283 | } |
| 284 | |
| 285 | fn enable_action(&self, action_name: &str, enabled: bool) { |
| 286 | if let Some(action) = self |
| 287 | .lookup_action(action_name) |
| 288 | .and_downcast::<gio::SimpleAction>() |
| 289 | { |
| 290 | action.set_enabled(enabled); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | pub fn focus_game(&self) { |
| 295 | self.imp().game_area.get().unwrap().grab_focus(); |
| 296 | } |
| 297 | |
| 298 | pub fn start_new_game(&self) { |
| 299 | self.imp().game_area.get().unwrap().start_new_game(); |
| 300 | } |
| 301 | |
| 302 | pub fn show_highscores(&self) { |
| 303 | let settings = self.imp().settings.get().unwrap(); |
| 304 | |
| 305 | let category = Category { |
| 306 | key: settings.selected_config(), |
| 307 | safety: settings.move_safety(), |
| 308 | }; |
| 309 | |
| 310 | show_scores(Some(&category), self); |
| 311 | } |
| 312 | } |