Solitaire: Persist game mode to config file

This commit is contained in:
Timothy Flynn 2021-05-18 11:15:34 -04:00 committed by Andreas Kling
parent 5a8c220101
commit 7d062d0033
Notes: sideshowbarker 2024-07-18 17:50:09 +09:00
3 changed files with 34 additions and 7 deletions

View file

@ -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());

View file

@ -12,9 +12,10 @@
namespace Solitaire {
enum class Mode {
enum class Mode : u8 {
SingleCardDraw,
ThreeCardDraw,
__Count
};
class Game final : public GUI::Frame {

View file

@ -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();
}