GameOfLife: Don't toggle cells on mouse move when placing patterns

Previously, it was very easy to inadvertently toggle cells when
placing a pattern by dragging the mouse slightly.
This commit is contained in:
Tim Ledbetter 2023-09-19 22:15:17 +01:00 committed by Andreas Kling
parent 6eaae726fa
commit 12adaac08d
Notes: sideshowbarker 2024-07-17 03:27:40 +09:00
3 changed files with 5 additions and 2 deletions

View File

@ -196,6 +196,7 @@ void BoardWidget::paint_event(GUI::PaintEvent& event)
void BoardWidget::mousedown_event(GUI::MouseEvent& event)
{
if (event.button() == GUI::MouseButton::Primary) {
m_dragging_enabled = (m_selected_pattern == nullptr);
set_toggling_cells(true);
auto row_and_column = get_row_and_column_for_point(event.x(), event.y());
if (!row_and_column.has_value())
@ -239,7 +240,7 @@ void BoardWidget::mousemove_event(GUI::MouseEvent& event)
if (!row_and_column.has_value())
return;
auto [row, column] = row_and_column.value();
if (m_toggling_cells) {
if (m_toggling_cells && m_dragging_enabled) {
if (m_last_cell_toggled.row != row || m_last_cell_toggled.column != column)
toggle_cell(row, column);
}
@ -253,6 +254,7 @@ void BoardWidget::mousemove_event(GUI::MouseEvent& event)
void BoardWidget::mouseup_event(GUI::MouseEvent&)
{
set_toggling_cells(false);
m_dragging_enabled = true;
}
Optional<Board::RowAndColumn> BoardWidget::get_row_and_column_for_point(int x, int y) const

View File

@ -84,6 +84,7 @@ private:
NonnullOwnPtr<Board> m_board;
bool m_running { false };
bool m_dragging_enabled { true };
int m_running_timer_interval { 500 };
int m_running_pattern_preview_timer_interval { 100 };

View File

@ -71,7 +71,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
statusbar.segment(1).set_fixed_width(ceil(width));
auto show_statusbar_hint = [&]() {
auto tip = board_widget->selected_pattern() ? pattern_place_tip : toggle_cells_tip;
auto tip = board_widget.selected_pattern() ? pattern_place_tip : toggle_cells_tip;
statusbar.segment(0).set_text(tip);
};
show_statusbar_hint();