mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-04 05:20:30 +00:00
Flood: Get the color scheme from the system theme
This commit is contained in:
parent
800c292be8
commit
1a68977457
Notes:
sideshowbarker
2024-07-17 06:40:35 +09:00
Author: https://github.com/implicitfield Commit: https://github.com/SerenityOS/serenity/commit/1a68977457 Pull-request: https://github.com/SerenityOS/serenity/pull/16291 Reviewed-by: https://github.com/davidot ✅
7 changed files with 41 additions and 112 deletions
|
@ -42,6 +42,8 @@ void Board::randomize()
|
|||
set_cell(row, column, get_random_uniform(m_colors.size()));
|
||||
}
|
||||
}
|
||||
m_previous_value = m_cells[0][0];
|
||||
m_current_value = m_cells[0][0];
|
||||
}
|
||||
|
||||
void Board::resize(size_t rows, size_t columns)
|
||||
|
|
|
@ -8,12 +8,28 @@
|
|||
|
||||
#include <LibGUI/Menu.h>
|
||||
#include <LibGUI/Painter.h>
|
||||
#include <LibGfx/Palette.h>
|
||||
|
||||
BoardWidget::BoardWidget(size_t rows, size_t columns, Vector<Color> colors, Color background_color)
|
||||
BoardWidget::BoardWidget(size_t rows, size_t columns)
|
||||
: m_board(make<Board>(rows, columns))
|
||||
, m_background_color(background_color)
|
||||
{
|
||||
m_board->set_color_scheme(move(colors));
|
||||
update_color_scheme();
|
||||
}
|
||||
|
||||
void BoardWidget::update_color_scheme()
|
||||
{
|
||||
auto const& palette = GUI::Widget::palette();
|
||||
m_board->set_color_scheme({
|
||||
palette.bright_black(),
|
||||
palette.bright_red(),
|
||||
palette.bright_green(),
|
||||
palette.bright_yellow(),
|
||||
palette.bright_blue(),
|
||||
palette.bright_magenta(),
|
||||
palette.bright_cyan(),
|
||||
palette.bright_white(),
|
||||
});
|
||||
m_background_color = palette.background();
|
||||
}
|
||||
|
||||
void BoardWidget::resize_board(size_t rows, size_t columns)
|
||||
|
@ -23,11 +39,6 @@ void BoardWidget::resize_board(size_t rows, size_t columns)
|
|||
m_board->resize(rows, columns);
|
||||
}
|
||||
|
||||
void BoardWidget::set_background_color(Color const background)
|
||||
{
|
||||
m_background_color = background;
|
||||
}
|
||||
|
||||
int BoardWidget::get_cell_size() const
|
||||
{
|
||||
int width = rect().width() / m_board->columns();
|
||||
|
@ -45,6 +56,13 @@ Gfx::IntSize BoardWidget::get_board_offset() const
|
|||
};
|
||||
}
|
||||
|
||||
void BoardWidget::event(Core::Event& event)
|
||||
{
|
||||
if (event.type() == GUI::Event::ThemeChange)
|
||||
update_color_scheme();
|
||||
GUI::Frame::event(event);
|
||||
}
|
||||
|
||||
void BoardWidget::paint_event(GUI::PaintEvent& event)
|
||||
{
|
||||
GUI::Widget::paint_event(event);
|
||||
|
|
|
@ -11,20 +11,15 @@
|
|||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/RefPtr.h>
|
||||
#include <LibGUI/Frame.h>
|
||||
#include <LibGUI/Menu.h>
|
||||
#include <LibGUI/Widget.h>
|
||||
|
||||
class BoardWidget final : public GUI::Widget {
|
||||
class BoardWidget final : public GUI::Frame {
|
||||
C_OBJECT(BoardWidget);
|
||||
|
||||
public:
|
||||
Function<void(Board::RowAndColumn)> on_move;
|
||||
|
||||
virtual void paint_event(GUI::PaintEvent&) override;
|
||||
virtual void mousedown_event(GUI::MouseEvent&) override;
|
||||
|
||||
void set_background_color(Color const background);
|
||||
|
||||
int get_cell_size() const;
|
||||
Gfx::IntSize get_board_offset() const;
|
||||
|
||||
|
@ -34,8 +29,13 @@ public:
|
|||
Board* board() { return m_board.ptr(); }
|
||||
|
||||
private:
|
||||
BoardWidget(size_t rows, size_t columns, Vector<Color> colors, Color background_color);
|
||||
BoardWidget(size_t rows, size_t columns);
|
||||
void update_color_scheme();
|
||||
NonnullOwnPtr<Board> m_board;
|
||||
|
||||
Color m_background_color;
|
||||
virtual void event(Core::Event&) override;
|
||||
virtual void paint_event(GUI::PaintEvent&) override;
|
||||
virtual void mousedown_event(GUI::MouseEvent&) override;
|
||||
|
||||
Color m_background_color = Color::Black;
|
||||
};
|
||||
|
|
|
@ -17,11 +17,10 @@
|
|||
#include <LibGUI/Label.h>
|
||||
#include <LibGUI/SpinBox.h>
|
||||
|
||||
SettingsDialog::SettingsDialog(GUI::Window* parent, size_t board_rows, size_t board_columns, StringView color_scheme)
|
||||
SettingsDialog::SettingsDialog(GUI::Window* parent, size_t board_rows, size_t board_columns)
|
||||
: GUI::Dialog(parent)
|
||||
, m_board_rows(board_rows)
|
||||
, m_board_columns(board_columns)
|
||||
, m_color_scheme(color_scheme)
|
||||
{
|
||||
set_rect({ 0, 0, 250, 150 });
|
||||
set_title("New Game");
|
||||
|
@ -46,24 +45,6 @@ SettingsDialog::SettingsDialog(GUI::Window* parent, size_t board_rows, size_t bo
|
|||
m_board_columns = value;
|
||||
};
|
||||
|
||||
static Vector<DeprecatedString> color_scheme_names;
|
||||
color_scheme_names.clear();
|
||||
Core::DirIterator iterator("/res/color-schemes", Core::DirIterator::SkipParentAndBaseDir);
|
||||
while (iterator.has_next()) {
|
||||
auto path = iterator.next_path();
|
||||
color_scheme_names.append(path.replace(".ini"sv, ""sv, ReplaceMode::FirstOnly));
|
||||
}
|
||||
quick_sort(color_scheme_names);
|
||||
|
||||
auto color_scheme_combo = main_widget.find_descendant_of_type_named<GUI::ComboBox>("color_scheme_combo");
|
||||
color_scheme_combo->set_only_allow_values_from_model(true);
|
||||
color_scheme_combo->set_model(*GUI::ItemListModel<DeprecatedString>::create(color_scheme_names));
|
||||
color_scheme_combo->set_selected_index(color_scheme_names.find_first_index(m_color_scheme).value());
|
||||
color_scheme_combo->set_enabled(color_scheme_names.size() > 1);
|
||||
color_scheme_combo->on_change = [&](auto&, const GUI::ModelIndex& index) {
|
||||
m_color_scheme = index.data().as_string();
|
||||
};
|
||||
|
||||
auto cancel_button = main_widget.find_descendant_of_type_named<GUI::Button>("cancel_button");
|
||||
cancel_button->on_click = [this](auto) {
|
||||
done(ExecResult::Cancel);
|
||||
|
|
|
@ -38,21 +38,6 @@
|
|||
}
|
||||
}
|
||||
|
||||
@GUI::Widget {
|
||||
layout: @GUI::HorizontalBoxLayout {
|
||||
spacing: 4
|
||||
}
|
||||
|
||||
@GUI::Label {
|
||||
text: "Color scheme"
|
||||
text_alignment: "CenterLeft"
|
||||
}
|
||||
|
||||
@GUI::ComboBox {
|
||||
name: "color_scheme_combo"
|
||||
}
|
||||
}
|
||||
|
||||
@GUI::Widget {
|
||||
layout: @GUI::HorizontalBoxLayout {
|
||||
spacing: 10
|
||||
|
|
|
@ -14,12 +14,10 @@ class SettingsDialog : public GUI::Dialog {
|
|||
public:
|
||||
size_t board_rows() const { return m_board_rows; }
|
||||
size_t board_columns() const { return m_board_columns; }
|
||||
StringView color_scheme() const { return m_color_scheme; }
|
||||
|
||||
private:
|
||||
SettingsDialog(GUI::Window* parent, size_t board_rows, size_t board_columns, StringView color_scheme);
|
||||
SettingsDialog(GUI::Window* parent, size_t board_rows, size_t board_columns);
|
||||
|
||||
size_t m_board_rows;
|
||||
size_t m_board_columns;
|
||||
DeprecatedString m_color_scheme;
|
||||
};
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
#include <AK/URL.h>
|
||||
#include <Games/Flood/FloodWindowGML.h>
|
||||
#include <LibConfig/Client.h>
|
||||
#include <LibCore/ConfigFile.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibDesktop/Launcher.h>
|
||||
#include <LibGUI/Action.h>
|
||||
|
@ -22,49 +21,6 @@
|
|||
#include <LibGfx/Painter.h>
|
||||
#include <LibMain/Main.h>
|
||||
|
||||
// FIXME: Move this into a library. Also consider simplifying obtaining 'color_scheme_names' in
|
||||
// SettingsDialog.cpp and Userland/Applications/TerminalSettings/TerminalSettingsWidget.cpp.
|
||||
// Adapted from Libraries/LibVT/TerminalWidget.cpp::TerminalWidget::set_color_scheme.
|
||||
static ErrorOr<Vector<Color>> get_color_scheme_from_string(StringView name)
|
||||
{
|
||||
if (name.contains('/')) {
|
||||
return Error::from_string_literal("Shenanigans! Color scheme names can't contain slashes.");
|
||||
}
|
||||
|
||||
constexpr StringView color_names[] = {
|
||||
"Black"sv,
|
||||
"Red"sv,
|
||||
"Green"sv,
|
||||
"Yellow"sv,
|
||||
"Blue"sv,
|
||||
"Magenta"sv,
|
||||
"Cyan"sv,
|
||||
"White"sv
|
||||
};
|
||||
|
||||
auto const path = DeprecatedString::formatted("/res/color-schemes/{}.ini", name);
|
||||
auto color_config_or_error = Core::ConfigFile::open(path);
|
||||
if (color_config_or_error.is_error()) {
|
||||
return Error::from_string_view(DeprecatedString::formatted("Unable to read color scheme file '{}': {}", path, color_config_or_error.error()));
|
||||
}
|
||||
auto const color_config = color_config_or_error.release_value();
|
||||
Vector<Color> colors;
|
||||
|
||||
for (u8 color_index = 0; color_index < 8; ++color_index) {
|
||||
auto const rgb = Gfx::Color::from_string(color_config->read_entry("Bright", color_names[color_index]));
|
||||
if (rgb.has_value())
|
||||
colors.append(Color::from_argb(rgb.value().value()));
|
||||
}
|
||||
|
||||
auto const default_background = Gfx::Color::from_string(color_config->read_entry("Primary", "Background"));
|
||||
if (default_background.has_value())
|
||||
colors.append(default_background.value());
|
||||
else
|
||||
colors.append(Color::DarkGray);
|
||||
|
||||
return colors;
|
||||
}
|
||||
|
||||
// FIXME: Improve this AI.
|
||||
// Currently, this AI always chooses a move that gets the most cells flooded immediately.
|
||||
// This far from being able to generate an optimal solution, and is something that needs to be improved
|
||||
|
@ -118,11 +74,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
size_t board_rows = Config::read_i32("Flood"sv, ""sv, "board_rows"sv, 16);
|
||||
size_t board_columns = Config::read_i32("Flood"sv, ""sv, "board_columns"sv, 16);
|
||||
DeprecatedString color_scheme = Config::read_string("Flood"sv, ""sv, "color_scheme"sv, "Default"sv);
|
||||
|
||||
Config::write_i32("Flood"sv, ""sv, "board_rows"sv, board_rows);
|
||||
Config::write_i32("Flood"sv, ""sv, "board_columns"sv, board_columns);
|
||||
Config::write_string("Flood"sv, ""sv, "color_scheme"sv, color_scheme);
|
||||
|
||||
window->set_double_buffering_enabled(false);
|
||||
window->set_title("Flood");
|
||||
|
@ -132,10 +86,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
if (!main_widget.load_from_gml(flood_window_gml))
|
||||
VERIFY_NOT_REACHED();
|
||||
|
||||
auto colors = TRY(get_color_scheme_from_string(color_scheme));
|
||||
auto background_color = colors.take_last();
|
||||
|
||||
auto board_widget = TRY(main_widget.find_descendant_of_type_named<GUI::Widget>("board_widget_container")->try_add<BoardWidget>(board_rows, board_columns, move(colors), move(background_color)));
|
||||
auto board_widget = TRY(main_widget.find_descendant_of_type_named<GUI::Widget>("board_widget_container")->try_add<BoardWidget>(board_rows, board_columns));
|
||||
board_widget->board()->randomize();
|
||||
int ai_moves = get_number_of_moves_from_ai(*board_widget->board());
|
||||
int moves_made = 0;
|
||||
|
@ -161,21 +112,15 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
update();
|
||||
|
||||
auto change_settings = [&] {
|
||||
auto settings_dialog = SettingsDialog::construct(window, board_rows, board_columns, color_scheme);
|
||||
auto settings_dialog = SettingsDialog::construct(window, board_rows, board_columns);
|
||||
if (settings_dialog->exec() != GUI::Dialog::ExecResult::OK)
|
||||
return;
|
||||
|
||||
board_rows = settings_dialog->board_rows();
|
||||
board_columns = settings_dialog->board_columns();
|
||||
color_scheme = settings_dialog->color_scheme();
|
||||
|
||||
Config::write_i32("Flood"sv, ""sv, "board_rows"sv, board_rows);
|
||||
Config::write_i32("Flood"sv, ""sv, "board_columns"sv, board_columns);
|
||||
Config::write_string("Flood"sv, ""sv, "color_scheme"sv, color_scheme);
|
||||
|
||||
auto colors = MUST(get_color_scheme_from_string(color_scheme));
|
||||
board_widget->set_background_color(colors.take_last());
|
||||
board_widget->board()->set_color_scheme(move(colors));
|
||||
|
||||
GUI::MessageBox::show(settings_dialog, "New settings have been saved and will be applied on a new game"sv, "Settings Changed Successfully"sv, GUI::MessageBox::Type::Information);
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue