From 010b96610730b5028c42faa633d067ebb411a744 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Wed, 1 Jul 2020 19:31:11 +0930 Subject: move remove_input_object() into object superclass --- BinaryDisplay.cpp | 2 +- BinaryDisplay.h | 2 +- Gate.cpp | 3 +- Gate.h | 3 +- MainWindow.cpp | 100 ++++++++++++++++++++++++++++++------------------------ Object.h | 17 +++++++--- 6 files changed, 73 insertions(+), 54 deletions(-) diff --git a/BinaryDisplay.cpp b/BinaryDisplay.cpp index ff2fbc3..f2d6d69 100644 --- a/BinaryDisplay.cpp +++ b/BinaryDisplay.cpp @@ -32,7 +32,7 @@ BinaryDisplay::update_state() } void -BinaryDisplay::remove_input(int id) +BinaryDisplay::remove_input_object(int id) { if (input0 && input0->get_id() == id) input0 = nullptr; diff --git a/BinaryDisplay.h b/BinaryDisplay.h index 488e889..b6afca2 100644 --- a/BinaryDisplay.h +++ b/BinaryDisplay.h @@ -29,7 +29,7 @@ class BinaryDisplay : public Object void set_input6(Object *object_) { input6 = object_; }; void set_input7(Object *object_) { input7 = object_; }; - void remove_input(int id); + void remove_input_object(int id) override; int get_sum_value() { return sum_value; }; diff --git a/Gate.cpp b/Gate.cpp index 7475dd9..0e206d0 100644 --- a/Gate.cpp +++ b/Gate.cpp @@ -38,8 +38,9 @@ Gate::Gate(GATE_TYPE type, int x, int y, int width, int height, int loaded_id) Gate::~Gate() {} + void -Gate::remove_input_gate(int id) +Gate::remove_input_object(int id) { if (input_gate1) { diff --git a/Gate.h b/Gate.h index ab6b238..c8cd492 100644 --- a/Gate.h +++ b/Gate.h @@ -46,12 +46,11 @@ class Gate : public Object Gate *get_input_gate2() { return this->input_gate2; }; void set_input_gate1(Gate *gate) { this->input_gate1 = gate; }; void set_input_gate2(Gate *gate) { this->input_gate2 = gate; }; - void remove_input_gate(int id); + void remove_input_object(int id) override; void update_state() override; std::string get_object_name() override { return get_output_type_text(); }; - private: GATE_TYPE gate_type; diff --git a/MainWindow.cpp b/MainWindow.cpp index 3b60f59..6bb8f2f 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -530,10 +530,10 @@ MainWindow::update_object_state(Object *object) void MainWindow::find_selected_input(int x, int y) { - Gate *input_gate = nullptr; + Object *input_object = nullptr; //FIXME: make object - Gate *gate = (Gate*)selected_object; + Object *object = selected_object; if (!selected_object) { selected_input.object = nullptr; @@ -544,53 +544,64 @@ MainWindow::find_selected_input(int x, int y) if (x >= selected_object->get_x() && x <= selected_object->get_x() + 20) { - if (y-selected_object->get_y() <= selected_object->get_height()/2) - { - input = 1; - } - else - { - input = 2; - } - - switch (input) + switch (selected_object->get_object_type()) { - case 1: + case Object::GATE: { - if (gate->get_input_gate1() != nullptr) + Gate *gate = (Gate*)object; + if (y-selected_object->get_y() <= selected_object->get_height()/2) + { input = 1; } else { input = 2; } + switch (input) { - input_gate = gate->get_input_gate1(); - } - break; - } + case 1: + { + if (gate->get_input_gate1() != nullptr) + { + input_object = gate->get_input_gate1(); + } + break; + } - case 2: - { - if (gate->get_input_gate2() != nullptr) - { - input_gate = gate->get_input_gate2(); - } - else - { - /* special check for gates with only one input */ - if (gate->get_gate_type() == Gate::NOT || gate->get_gate_type() == Gate::OUTPUT) + case 2: { - input_gate = gate->get_input_gate1(); - input = 1; + if (gate->get_input_gate2() != nullptr) + { + input_object = gate->get_input_gate2(); + } + else + { + /* special check for gates with only one input */ + if (gate->get_gate_type() == Gate::NOT || gate->get_gate_type() == Gate::OUTPUT) + { + input_object = gate->get_input_gate1(); + input = 1; + } + } + break; } + + default: + input = -1; + break; } break; } + case Object::BINARYDISPLAY: + { + break; + } + case Object::NONE: default: - input = -1; + printf("find_selected_input() object not implemented\n"); break; } - if (input_gate != nullptr) + + if (input_object != nullptr) { - selected_input.object = input_gate; + selected_input.object = input_object; selected_input.input = input; - printf("selected input #%d of gate id %d\n", input, selected_object->get_id()); + printf("selected input #%d of object id %d\n", input, selected_object->get_id()); } } } @@ -844,7 +855,6 @@ MainWindow::remove_object(Object &object) case Object::GATE: { Gate &gate = (Gate&)object; - Gate *out_gate; /* delete inputs */ if (gate.get_input_gate1()) { @@ -856,16 +866,6 @@ MainWindow::remove_object(Object &object) gate.get_input_gate2()->remove_output_object_id(gate.get_id()); update_object_state(gate.get_input_gate2()); } - - /* delete outputs */ - for(auto g = gate.get_output_objects()->begin(); g != gate.get_output_objects()->end(); ++g) - { - out_gate = (Gate*)find_object_by_id((*g)); - if (!out_gate) - continue; - out_gate->remove_input_gate(gate.get_id()); - update_object_state(out_gate); - } break; } case Object::BINARYDISPLAY: @@ -886,6 +886,16 @@ MainWindow::remove_object(Object &object) printf("remove_object implement other objects\n"); break; } + /* delete outputs */ + for(auto g = object.get_output_objects()->begin(); g != object.get_output_objects()->end(); ++g) + { + out_object = find_object_by_id((*g)); + if (!out_object) + continue; + out_object->remove_input_object(object.get_id()); + update_object_state(out_object); + } + int pos = 0; for (auto g = objects.begin(); g != objects.end(); ++g) { diff --git a/Object.h b/Object.h index 2d2f175..10184dc 100644 --- a/Object.h +++ b/Object.h @@ -38,6 +38,13 @@ class Object BINARYDISPLAY, }; + struct selected_input_object + { + Object *object; + int input; + }; + + virtual ~Object() = default; int get_id() { return this->id; }; int get_x() { return this->x; }; @@ -47,11 +54,11 @@ class Object void set_x(int x) { this->x = x; }; void set_y(int y) { this->y = y; }; - void add_output_object_id(int id) { this->output_object_ids.push_back(id); }; + void add_output_object_id(int id_) { this->output_object_ids.push_back(id_); }; virtual void update_state() {}; // subclasses must implement - static void set_object_id_counter(int id) { object_id_counter = id; }; + static void set_object_id_counter(int id_) { object_id_counter = id_; }; static int get_object_id_counter() { return object_id_counter; }; OBJECT_TYPE get_object_type() { return object_type; }; @@ -60,12 +67,12 @@ class Object std::vector *get_output_objects() { return &this->output_object_ids; }; - void remove_output_object_id(int id) + void remove_output_object_id(int id_) { int pos = 0; for(auto o = output_object_ids.begin(); o != output_object_ids.end(); ++o) { - if (id == (*o)) + if (id_ == (*o)) { output_object_ids.erase(output_object_ids.begin() + pos); break; @@ -77,6 +84,8 @@ class Object bool get_output_state() { return this->output_state; }; void set_state(bool state) { this->output_state = state; }; + virtual void remove_input_object(int id_) { printf("remove_input_object not implemented\n"); }; + protected: int id; int x; -- cgit v1.2.3