Skip to content

Commit

Permalink
Minesweeper: Allow single-click chording
Browse files Browse the repository at this point in the history
This is how other Minesweeper games I've played usually behave.
Single-click chording can be disabled from the menu or config file.
  • Loading branch information
Jookia authored and awesomekling committed Jul 1, 2019
1 parent d2b6f79 commit 9dbf453
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Games/Minesweeper/Field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ class SquareLabel final : public GLabel {

virtual void mousedown_event(GMouseEvent& event) override
{
if (event.buttons() == (GMouseButton::Right | GMouseButton::Left)) {
if (event.button() == GMouseButton::Left || event.button() == GMouseButton::Right) {
if (event.button() == GMouseButton::Right || event.button() == GMouseButton::Left) {
if (event.buttons() == (GMouseButton::Right | GMouseButton::Left) ||
m_square.field->is_single_chording()) {
m_chord = true;
m_square.field->set_chord_preview(m_square, true);
}
Expand Down Expand Up @@ -461,6 +462,12 @@ void Field::set_field_size(int rows, int columns, int mine_count)
on_size_changed();
}

void Field::set_single_chording(bool enabled) {
auto config = CConfigFile::get_for_app("Minesweeper");
m_single_chording = enabled;
config->write_bool_entry("Minesweeper", "SingleChording", m_single_chording);
}

Square::~Square()
{
delete label;
Expand Down
3 changes: 3 additions & 0 deletions Games/Minesweeper/Field.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ class Field final : public GFrame {
int columns() const { return m_columns; }
int mine_count() const { return m_mine_count; }
int square_size() const { return 15; }
bool is_single_chording() const { return m_single_chording; }

void set_field_size(int rows, int columns, int mine_count);
void set_single_chording(bool new_val);

void reset();

Expand Down Expand Up @@ -100,4 +102,5 @@ class Field final : public GFrame {
Face m_face { Face::Default };
bool m_chord_preview { false };
bool m_first_click { true };
bool m_single_chording { true };
};
14 changes: 14 additions & 0 deletions Games/Minesweeper/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,29 @@ int main(int argc, char** argv)

{
auto config = CConfigFile::get_for_app("Minesweeper");
bool single_chording = config->read_num_entry("Minesweeper", "SingleChording", false);
int mine_count = config->read_num_entry("Game", "MineCount", 10);
int rows = config->read_num_entry("Game", "Rows", 9);
int columns = config->read_num_entry("Game", "Columns", 9);
field->set_field_size(rows, columns, mine_count);
field->set_single_chording(single_chording);
}

auto menubar = make<GMenuBar>();

auto app_menu = make<GMenu>("Minesweeper");

RefPtr<GAction> chord_toggler_action;
chord_toggler_action = GAction::create("Single-click chording", [&](const GAction&) {
bool toggled = !field->is_single_chording();
field->set_single_chording(toggled);
chord_toggler_action->set_checked(toggled);
});
chord_toggler_action->set_checkable(true);
chord_toggler_action->set_checked(field->is_single_chording());
app_menu->add_action(*chord_toggler_action);
app_menu->add_separator();

app_menu->add_action(GAction::create("Quit", { Mod_Alt, Key_F4 }, [](const GAction&) {
GApplication::the().quit(0);
return;
Expand Down

0 comments on commit 9dbf453

Please sign in to comment.