From 91aff1783d364f3f2c814a90ec292cb89d5deb88 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Wed, 24 Jun 2020 21:24:22 +0930 Subject: functionality: implemented deleting selected gate with the delete key. --- Gate.cpp | 17 ++++++++++++++++- Gate.h | 1 + MainWindow.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/Gate.cpp b/Gate.cpp index 70e55fe..89a5daa 100644 --- a/Gate.cpp +++ b/Gate.cpp @@ -40,7 +40,6 @@ Gate::remove_output_gate_id(int id) { if (id == (*g)) { - printf("%id = %d\n", id, *g); output_gate_ids.erase(output_gate_ids.begin() + pos); break; } @@ -48,6 +47,22 @@ Gate::remove_output_gate_id(int id) } } +void +Gate::remove_input_gate(int id) +{ + if (input_gate1) + { + if (input_gate1->get_id() == id) + input_gate1 = nullptr; + } + + if (input_gate2) + { + if (input_gate2->get_id() == id) + input_gate2 = nullptr; + } +} + void Gate::update_state() { switch (this->gate_type) diff --git a/Gate.h b/Gate.h index 68afab0..ad2f212 100644 --- a/Gate.h +++ b/Gate.h @@ -59,6 +59,7 @@ class Gate //void set_output_gate(Gate *gate) { this->output_gate = gate; }; void add_output_gate_id(int id) { this->output_gate_ids.push_back(id); }; void remove_output_gate_id(int id); + void remove_input_gate(int id); void update_state(); private: diff --git a/MainWindow.cpp b/MainWindow.cpp index 23ca61c..8b22243 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -604,6 +604,7 @@ MainWindow::on_key_release(FXObject *sender, FXSelector sel, void *ptr) { if (selected_input.gate != nullptr) { + /* delete link */ switch (selected_input.input) { case 1: @@ -622,8 +623,59 @@ MainWindow::on_key_release(FXObject *sender, FXSelector sel, void *ptr) selected_input.input = -1; draw(); } + else if (selected_gate) + { + /* delete gate and all associations */ + /* + * 2 inputs + * x outputs + * + * inputs: + * if input1/2 != null: + * call their remove_gate_id with this gate id + * update that gate + * outputs: + * iterate through ouputs + * call remove_input_gate with this gate id + * update that gate + */ + Gate *gate; + /* delete inputs */ + if (selected_gate->get_input_gate1()) + { + selected_gate->get_input_gate1()->remove_output_gate_id(selected_gate->get_id()); + update_gate_state(selected_gate->get_input_gate1()); + } + if (selected_gate->get_input_gate2()) + { + selected_gate->get_input_gate2()->remove_output_gate_id(selected_gate->get_id()); + update_gate_state(selected_gate->get_input_gate2()); + } + + /* delete outputs */ + for(auto g = selected_gate->get_output_gates()->begin(); g != selected_gate->get_output_gates()->end(); ++g) + { + gate = find_gate_by_id((*g)); + if (!gate) + continue; + gate->remove_input_gate(selected_gate->get_id()); + update_gate_state(gate); + } + int pos = 0; + for (auto g = gates.begin(); g != gates.end(); ++g) + { + gate = (*g).get(); + if (gate->get_id() == selected_gate->get_id()) + { + gates.erase(gates.begin() + pos); + break; + } + pos++; + } + } break; } + default: this->onKeyPress(sender, sel, ptr); break; -- cgit v1.2.3