From 1179d5d921cf82513098239bbf05b526263d3f2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Musab=20K=C4=B1l=C4=B1=C3=A7?= Date: Sat, 28 Aug 2021 22:31:35 +0300 Subject: [PATCH] GameOfLife: Don't enable rotate button if a pattern isn't selected --- Userland/Games/GameOfLife/BoardWidget.cpp | 4 ++++ Userland/Games/GameOfLife/BoardWidget.h | 1 + Userland/Games/GameOfLife/main.cpp | 8 ++++++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Userland/Games/GameOfLife/BoardWidget.cpp b/Userland/Games/GameOfLife/BoardWidget.cpp index aca006bf9b..06da25be5b 100644 --- a/Userland/Games/GameOfLife/BoardWidget.cpp +++ b/Userland/Games/GameOfLife/BoardWidget.cpp @@ -28,6 +28,8 @@ BoardWidget::BoardWidget(size_t rows, size_t columns) on_pattern_selection = [this](auto* pattern) { m_selected_pattern = pattern; + if (on_pattern_selection_state_change) + on_pattern_selection_state_change(); }; setup_patterns(); @@ -245,6 +247,8 @@ void BoardWidget::place_pattern(size_t row, size_t column) y_offset++; } m_selected_pattern = nullptr; + if (on_pattern_selection_state_change) + on_pattern_selection_state_change(); if (m_pattern_preview_timer->is_active()) m_pattern_preview_timer->stop(); } diff --git a/Userland/Games/GameOfLife/BoardWidget.h b/Userland/Games/GameOfLife/BoardWidget.h index b72b4b1088..97d1b5df30 100644 --- a/Userland/Games/GameOfLife/BoardWidget.h +++ b/Userland/Games/GameOfLife/BoardWidget.h @@ -66,6 +66,7 @@ public: Function on_running_state_change; Function on_stall; + Function on_pattern_selection_state_change; Function on_cell_toggled; private: diff --git a/Userland/Games/GameOfLife/main.cpp b/Userland/Games/GameOfLife/main.cpp index 03f156bf93..483b2a1c2b 100644 --- a/Userland/Games/GameOfLife/main.cpp +++ b/Userland/Games/GameOfLife/main.cpp @@ -127,9 +127,9 @@ int main(int argc, char** argv) main_toolbar.add_action(randomize_cells_action); auto rotate_pattern_action = GUI::Action::create("&Rotate pattern", { 0, Key_R }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/redo.png"), [&](auto&) { - if (board_widget.selected_pattern() != nullptr) - board_widget.selected_pattern()->rotate_clockwise(); + board_widget.selected_pattern()->rotate_clockwise(); }); + rotate_pattern_action->set_enabled(false); main_toolbar.add_action(rotate_pattern_action); auto& game_menu = window->add_menu("&Game"); @@ -180,6 +180,10 @@ int main(int argc, char** argv) statusbar.set_text(click_tip); }; + board_widget.on_pattern_selection_state_change = [&] { + rotate_pattern_action->set_enabled(board_widget.selected_pattern() != nullptr); + }; + window->resize(500, 420); window->show();