mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:37:44 +00:00
Snake: Implement image-based skins
Co-authored-by: HawDevelopment <hawdevelopment@gmail.com>
This commit is contained in:
parent
da7c883dfa
commit
5708a47157
20 changed files with 425 additions and 33 deletions
|
@ -1,16 +1,20 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include "Game.h"
|
||||
#include "Skins/SnakeSkin.h"
|
||||
#include <AK/URL.h>
|
||||
#include <Games/Snake/SnakeGML.h>
|
||||
#include <LibConfig/Client.h>
|
||||
#include <LibCore/Directory.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibDesktop/Launcher.h>
|
||||
#include <LibGUI/Action.h>
|
||||
#include <LibGUI/ActionGroup.h>
|
||||
#include <LibGUI/Application.h>
|
||||
#include <LibGUI/BoxLayout.h>
|
||||
#include <LibGUI/Button.h>
|
||||
|
@ -21,7 +25,6 @@
|
|||
#include <LibGUI/Statusbar.h>
|
||||
#include <LibGUI/Window.h>
|
||||
#include <LibMain/Main.h>
|
||||
#include <stdio.h>
|
||||
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
|
@ -30,6 +33,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
auto app = TRY(GUI::Application::try_create(arguments));
|
||||
|
||||
Config::pledge_domain("Snake");
|
||||
Config::monitor_domain("Snake");
|
||||
|
||||
TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_scheme("/usr/share/man/man6/Snake.md") }));
|
||||
TRY(Desktop::Launcher::seal_allowlist());
|
||||
|
@ -55,6 +59,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
game.set_focus(true);
|
||||
|
||||
auto high_score = Config::read_u32("Snake"sv, "Snake"sv, "HighScore"sv, 0);
|
||||
auto snake_skin_name = Config::read_string("Snake"sv, "Snake"sv, "SnakeSkin"sv, "classic"sv);
|
||||
|
||||
auto& statusbar = *widget->find_descendant_of_type_named<GUI::Statusbar>("statusbar"sv);
|
||||
statusbar.set_text(0, "Score: 0"sv);
|
||||
|
@ -92,17 +97,48 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
action.set_icon(pause_icon);
|
||||
}
|
||||
})));
|
||||
TRY(game_menu->try_add_action(GUI::Action::create("&Change snake color", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/color-chooser.png"sv)), [&](auto&) {
|
||||
game.pause();
|
||||
|
||||
auto change_snake_color = GUI::Action::create("&Change snake color", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/color-chooser.png"sv)), [&](auto&) {
|
||||
auto was_paused = game.is_paused();
|
||||
if (!was_paused)
|
||||
game.pause();
|
||||
auto dialog = GUI::ColorPicker::construct(Gfx::Color::White, window);
|
||||
if (dialog->exec() == GUI::Dialog::ExecResult::OK)
|
||||
game.set_snake_base_color(dialog->color());
|
||||
if (dialog->exec() == GUI::Dialog::ExecResult::OK) {
|
||||
Config::write_u32("Snake"sv, "Snake"sv, "BaseColor"sv, dialog->color().value());
|
||||
game.set_skin_color(dialog->color());
|
||||
}
|
||||
if (!was_paused)
|
||||
game.start();
|
||||
})));
|
||||
});
|
||||
change_snake_color->set_enabled(snake_skin_name == "classic"sv);
|
||||
TRY(game_menu->try_add_action(change_snake_color));
|
||||
|
||||
GUI::ActionGroup skin_action_group;
|
||||
skin_action_group.set_exclusive(true);
|
||||
|
||||
auto skin_menu = TRY(game_menu->try_add_submenu("&Skin"));
|
||||
skin_menu->set_icon(app_icon.bitmap_for_size(16));
|
||||
|
||||
auto add_skin_action = [&](StringView name, bool enable_color) -> ErrorOr<void> {
|
||||
auto action = TRY(GUI::Action::try_create_checkable(name, {}, [&, enable_color](auto& action) {
|
||||
Config::write_string("Snake"sv, "Snake"sv, "SnakeSkin"sv, action.text());
|
||||
game.set_skin_name(action.text());
|
||||
change_snake_color->set_enabled(enable_color);
|
||||
}));
|
||||
|
||||
skin_action_group.add_action(*action);
|
||||
if (snake_skin_name == name)
|
||||
action->set_checked(true);
|
||||
TRY(skin_menu->try_add_action(*action));
|
||||
return {};
|
||||
};
|
||||
|
||||
TRY(Core::Directory::for_each_entry("/res/graphics/snake/skins/"sv, Core::DirIterator::SkipParentAndBaseDir, [&](auto& entry, auto&) -> ErrorOr<IterationDecision> {
|
||||
TRY(add_skin_action(entry.name, false));
|
||||
return IterationDecision::Continue;
|
||||
}));
|
||||
TRY(add_skin_action("classic"sv, true));
|
||||
|
||||
TRY(game_menu->try_add_separator());
|
||||
TRY(game_menu->try_add_action(GUI::CommonActions::make_quit_action([](auto&) {
|
||||
GUI::Application::the()->quit();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue