summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorimplicitfield <114500360+implicitfield@users.noreply.github.com>2022-12-02 18:58:47 +0200
committerAndrew Kaster <andrewdkaster@gmail.com>2022-12-31 04:20:59 -0700
commit1a68977457d9c05450797a32d1cf5bae527ddcc8 (patch)
tree96ec929844591eeae05bd926faca4a77df60312c
parent800c292be800b554f6fe754a2c18fc8708085dc0 (diff)
downloadserenity-1a68977457d9c05450797a32d1cf5bae527ddcc8.zip
Flood: Get the color scheme from the system theme
-rw-r--r--Userland/Games/Flood/Board.cpp2
-rw-r--r--Userland/Games/Flood/BoardWidget.cpp34
-rw-r--r--Userland/Games/Flood/BoardWidget.h18
-rw-r--r--Userland/Games/Flood/SettingsDialog.cpp21
-rw-r--r--Userland/Games/Flood/SettingsDialog.gml15
-rw-r--r--Userland/Games/Flood/SettingsDialog.h4
-rw-r--r--Userland/Games/Flood/main.cpp59
7 files changed, 41 insertions, 112 deletions
diff --git a/Userland/Games/Flood/Board.cpp b/Userland/Games/Flood/Board.cpp
index dd204b2416..dbc8bfdd75 100644
--- a/Userland/Games/Flood/Board.cpp
+++ b/Userland/Games/Flood/Board.cpp
@@ -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)
diff --git a/Userland/Games/Flood/BoardWidget.cpp b/Userland/Games/Flood/BoardWidget.cpp
index b8c0e2dd02..306b88a5e7 100644
--- a/Userland/Games/Flood/BoardWidget.cpp
+++ b/Userland/Games/Flood/BoardWidget.cpp
@@ -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);
diff --git a/Userland/Games/Flood/BoardWidget.h b/Userland/Games/Flood/BoardWidget.h
index e584252483..645dfcec1b 100644
--- a/Userland/Games/Flood/BoardWidget.h
+++ b/Userland/Games/Flood/BoardWidget.h
@@ -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;
};
diff --git a/Userland/Games/Flood/SettingsDialog.cpp b/Userland/Games/Flood/SettingsDialog.cpp
index 83f0132ec9..2dcad063c8 100644
--- a/Userland/Games/Flood/SettingsDialog.cpp
+++ b/Userland/Games/Flood/SettingsDialog.cpp
@@ -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);
diff --git a/Userland/Games/Flood/SettingsDialog.gml b/Userland/Games/Flood/SettingsDialog.gml
index 84c3804df1..fe524d2e94 100644
--- a/Userland/Games/Flood/SettingsDialog.gml
+++ b/Userland/Games/Flood/SettingsDialog.gml
@@ -40,21 +40,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
}
diff --git a/Userland/Games/Flood/SettingsDialog.h b/Userland/Games/Flood/SettingsDialog.h
index b2438029db..8b8f366f0a 100644
--- a/Userland/Games/Flood/SettingsDialog.h
+++ b/Userland/Games/Flood/SettingsDialog.h
@@ -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;
};
diff --git a/Userland/Games/Flood/main.cpp b/Userland/Games/Flood/main.cpp
index 98f76a3a06..f6a208201b 100644
--- a/Userland/Games/Flood/main.cpp
+++ b/Userland/Games/Flood/main.cpp
@@ -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);
};