diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-05-18 11:15:34 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-05-18 17:57:23 +0200 |
commit | 7d062d0033cb5d7b966ec85ef175a68a3171a7bb (patch) | |
tree | 39e7809aed07462b1fbe1ed37765b986727e7f54 /Userland | |
parent | 5a8c2201018a134528cf49480ed85fca54e28d7b (diff) | |
download | serenity-7d062d0033cb5d7b966ec85ef175a68a3171a7bb.zip |
Solitaire: Persist game mode to config file
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Games/Solitaire/Game.cpp | 3 | ||||
-rw-r--r-- | Userland/Games/Solitaire/Game.h | 3 | ||||
-rw-r--r-- | Userland/Games/Solitaire/main.cpp | 35 |
3 files changed, 34 insertions, 7 deletions
diff --git a/Userland/Games/Solitaire/Game.cpp b/Userland/Games/Solitaire/Game.cpp index f38dcfc47e..8578b7d446 100644 --- a/Userland/Games/Solitaire/Game.cpp +++ b/Userland/Games/Solitaire/Game.cpp @@ -196,6 +196,9 @@ void Game::mousedown_event(GUI::MouseEvent& event) case Mode::ThreeCardDraw: cards_to_draw = 3; break; + default: + VERIFY_NOT_REACHED(); + break; } update(stock.bounding_box()); diff --git a/Userland/Games/Solitaire/Game.h b/Userland/Games/Solitaire/Game.h index 220532c931..24c1bfab05 100644 --- a/Userland/Games/Solitaire/Game.h +++ b/Userland/Games/Solitaire/Game.h @@ -12,9 +12,10 @@ namespace Solitaire { -enum class Mode { +enum class Mode : u8 { SingleCardDraw, ThreeCardDraw, + __Count }; class Game final : public GUI::Frame { diff --git a/Userland/Games/Solitaire/main.cpp b/Userland/Games/Solitaire/main.cpp index df1621f73a..ec62f497a9 100644 --- a/Userland/Games/Solitaire/main.cpp +++ b/Userland/Games/Solitaire/main.cpp @@ -6,6 +6,7 @@ #include "Game.h" #include <Games/Solitaire/SolitaireGML.h> +#include <LibCore/ConfigFile.h> #include <LibCore/Timer.h> #include <LibGUI/Action.h> #include <LibGUI/ActionGroup.h> @@ -13,6 +14,7 @@ #include <LibGUI/Icon.h> #include <LibGUI/Menu.h> #include <LibGUI/Menubar.h> +#include <LibGUI/MessageBox.h> #include <LibGUI/Statusbar.h> #include <LibGUI/Window.h> #include <stdio.h> @@ -22,8 +24,9 @@ int main(int argc, char** argv) { auto app = GUI::Application::construct(argc, argv); auto app_icon = GUI::Icon::default_icon("app-solitaire"); + auto config = Core::ConfigFile::get_for_app("Solitaire"); - if (pledge("stdio recvfd sendfd rpath", nullptr) < 0) { + if (pledge("stdio recvfd sendfd rpath wpath cpath", nullptr) < 0) { perror("pledge"); return 1; } @@ -33,6 +36,11 @@ int main(int argc, char** argv) return 1; } + if (unveil(config->filename().characters(), "crw") < 0) { + perror("unveil"); + return 1; + } + if (unveil(nullptr, nullptr) < 0) { perror("unveil"); return 1; @@ -41,6 +49,18 @@ int main(int argc, char** argv) auto window = GUI::Window::construct(); window->set_title("Solitaire"); + auto mode = static_cast<Solitaire::Mode>(config->read_num_entry("Settings", "Mode", static_cast<int>(Solitaire::Mode::SingleCardDraw))); + + auto update_mode = [&](Solitaire::Mode new_mode) { + mode = new_mode; + config->write_num_entry("Settings", "Mode", static_cast<int>(mode)); + if (!config->sync()) + GUI::MessageBox::show(window, "Configuration could not be saved", "Error", GUI::MessageBox::Type::Error); + }; + + if (mode >= Solitaire::Mode::__Count) + update_mode(Solitaire::Mode::SingleCardDraw); + auto& widget = window->set_main_widget<GUI::Widget>(); widget.load_from_gml(solitaire_gml); @@ -91,15 +111,18 @@ int main(int argc, char** argv) draw_settng_actions.set_exclusive(true); auto single_card_draw_action = GUI::Action::create_checkable("&Single Card Draw", [&](auto&) { - game.setup(Solitaire::Mode::SingleCardDraw); + update_mode(Solitaire::Mode::SingleCardDraw); + game.setup(mode); }); - single_card_draw_action->set_checked(true); + single_card_draw_action->set_checked(mode == Solitaire::Mode::SingleCardDraw); single_card_draw_action->set_status_tip("Draw one card at a time"); draw_settng_actions.add_action(single_card_draw_action); auto three_card_draw_action = GUI::Action::create_checkable("&Three Card Draw", [&](auto&) { - game.setup(Solitaire::Mode::ThreeCardDraw); + update_mode(Solitaire::Mode::ThreeCardDraw); + game.setup(mode); }); + three_card_draw_action->set_checked(mode == Solitaire::Mode::ThreeCardDraw); three_card_draw_action->set_status_tip("Draw three cards at a time"); draw_settng_actions.add_action(three_card_draw_action); @@ -107,7 +130,7 @@ int main(int argc, char** argv) auto& game_menu = menubar->add_menu("&Game"); game_menu.add_action(GUI::Action::create("&New Game", { Mod_None, Key_F2 }, [&](auto&) { - game.setup(game.mode()); + game.setup(mode); })); game_menu.add_separator(); game_menu.add_action(single_card_draw_action); @@ -123,7 +146,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.mode()); + game.setup(mode); return app->exec(); } |