1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-09-15 09:26:18 +00:00

Hearts: Allow player to set their name

Added a new settings dialog to Hearts with a textbox to allow the
player to set a name, which is persisted in the Hearts config file.
This commit is contained in:
Josh Perry 2021-05-23 02:07:20 +01:00 committed by Linus Groh
parent e706de8f91
commit d27616cf36
6 changed files with 104 additions and 4 deletions

View file

@ -6,6 +6,7 @@
*/
#include "Game.h"
#include "SettingsDialog.h"
#include <Games/Hearts/HeartsGML.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/Timer.h>
@ -59,6 +60,8 @@ int main(int argc, char** argv)
auto& statusbar = *widget.find_descendant_of_type_named<GUI::Statusbar>("statusbar");
statusbar.set_text(0, "Score: 0");
String player_name = config->read_entry("", "player_name", "Gunnar");
game.on_status_change = [&](const AK::StringView& status) {
statusbar.set_override_text(status);
};
@ -74,11 +77,32 @@ int main(int argc, char** argv)
statusbar.set_override_text({});
};
auto change_settings = [&] {
auto settings_dialog = SettingsDialog::construct(window, player_name);
if (settings_dialog->exec() || settings_dialog->result() != GUI::Dialog::ExecOK)
return;
player_name = settings_dialog->player_name();
config->write_entry("", "player_name", player_name);
if (!config->sync()) {
GUI::MessageBox::show(window, "Settings could not be saved!", "Error", GUI::MessageBox::Type::Error);
return;
}
GUI::MessageBox::show(window, "Settings have been successfully saved and will take effect in the next game.", "Settings Changed Successfully", GUI::MessageBox::Type::Information);
};
auto menubar = GUI::Menubar::construct();
auto& game_menu = menubar->add_menu("&Game");
game_menu.add_action(GUI::Action::create("&New Game", { Mod_None, Key_F2 }, [&](auto&) {
game.setup();
game.setup(player_name);
}));
game_menu.add_separator();
game_menu.add_action(GUI::Action::create("&Settings...", [&](auto&) {
change_settings();
}));
game_menu.add_separator();
game_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
@ -91,7 +115,7 @@ int main(int argc, char** argv)
window->set_menubar(move(menubar));
window->set_icon(app_icon.bitmap_for_size(16));
window->show();
game.setup();
game.setup(player_name);
return app->exec();
}