From 11937e7d2256702698baba3acaf847523fe0f066 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Sat, 27 Jun 2020 23:02:11 +0930 Subject: Functionality: deleting multiple gates added --- Gate.cpp | 1 - MainWindow.cpp | 78 +++++++++++++++++++++++++++++++++++----------------------- MainWindow.h | 1 + 3 files changed, 48 insertions(+), 32 deletions(-) diff --git a/Gate.cpp b/Gate.cpp index dab8b7e..51a1652 100644 --- a/Gate.cpp +++ b/Gate.cpp @@ -80,7 +80,6 @@ void Gate::update_state() case OUTPUT: { - /* we're a simple output, do nothing */ if (input_gate1 && input_gate1->get_output_state() == true) { this->output_state = true; diff --git a/MainWindow.cpp b/MainWindow.cpp index 20f7a8c..ace6a1d 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -712,6 +712,44 @@ MainWindow::find_gates_in_area(int x, int y, int width, int height) } } +void +MainWindow::remove_gate(Gate &gate) +{ + Gate *out_gate; + /* delete inputs */ + if (gate.get_input_gate1()) + { + gate.get_input_gate1()->remove_output_gate_id(gate.get_id()); + update_gate_state(gate.get_input_gate1()); + } + if (gate.get_input_gate2()) + { + gate.get_input_gate2()->remove_output_gate_id(gate.get_id()); + update_gate_state(gate.get_input_gate2()); + } + + /* delete outputs */ + for(auto g = gate.get_output_gates()->begin(); g != gate.get_output_gates()->end(); ++g) + { + out_gate = find_gate_by_id((*g)); + if (!out_gate) + continue; + out_gate->remove_input_gate(gate.get_id()); + update_gate_state(out_gate); + } + int pos = 0; + for (auto g = gates.begin(); g != gates.end(); ++g) + { + out_gate = (*g).get(); + if (out_gate->get_id() == gate.get_id()) + { + gates.erase(gates.begin() + pos); + break; + } + pos++; + } +} + long MainWindow::on_paint(FXObject*, FXSelector, void *ptr) { @@ -934,40 +972,19 @@ MainWindow::on_key_release(FXObject *sender, FXSelector sel, void *ptr) } else if (selected_gate) { + remove_gate(*selected_gate); + selected_gate = nullptr; + } + else if (!selected_gates.empty()) + { + /* deleted multiple gates */ 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()) + for (auto g = selected_gates.begin(); g != selected_gates.end(); ++g) { - selected_gate->get_input_gate2()->remove_output_gate_id(selected_gate->get_id()); - update_gate_state(selected_gate->get_input_gate2()); + gate = (*g); + remove_gate(*gate); } - /* 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++; - } - selected_gate = nullptr; } break; } @@ -1075,7 +1092,6 @@ MainWindow::nor_button_press(FXObject *sender, FXSelector sel, void *ptr) long MainWindow::xnor_button_press(FXObject *sender, FXSelector sel, void *ptr) { - puts("xnor"); selected_gate = nullptr; selected_gate_type = Gate::XNOR; return 1; diff --git a/MainWindow.h b/MainWindow.h index 3623a1f..b6ba8e6 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -113,6 +113,7 @@ class MainWindow : public FXMainWindow Gate *find_gate_by_id(int id); void remove_all_gates(); void find_gates_in_area(int x, int y, int width, int height); + void remove_gate(Gate &gate); bool save_file(); bool load_file(); -- cgit v1.2.3