From f81202d509bebf98a986113c1b7a087cd32c43d8 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Wed, 24 Jun 2020 13:00:30 +0930 Subject: logic: now recursively updates gates when a change is made by keeping track of all connected gates --- Gate.cpp | 16 +++++++++++++++- Gate.h | 5 +++++ MainWindow.cpp | 31 ++++++++++++++++++++++++++++--- MainWindow.h | 1 + 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/Gate.cpp b/Gate.cpp index 9ffa50d..a0ac6ea 100644 --- a/Gate.cpp +++ b/Gate.cpp @@ -32,6 +32,20 @@ Gate::Gate(GATE_TYPE type, int x, int y, int width, int height) Gate::~Gate() {} +void +Gate::remove_output_gate_id(int id) +{ + int pos = 0; + for(auto g = output_gate_ids.begin(); g != output_gate_ids.end(); ++g) + { + if (id == (*g)) + { + output_gate_ids.erase(output_gate_ids.begin() + pos); + } + pos++; + } +} + void Gate::update_state() { switch (this->gate_type) @@ -116,6 +130,7 @@ void Gate::update_state() break; } } + std::string Gate::get_output_type_text() { @@ -176,7 +191,6 @@ Gate::get_output_type_text() default: return "?"; } - } diff --git a/Gate.h b/Gate.h index 655c473..68afab0 100644 --- a/Gate.h +++ b/Gate.h @@ -17,6 +17,7 @@ #define GATE_H #include +#include class Gate { @@ -44,6 +45,7 @@ class Gate int get_width() { return this->w; }; int get_height() { return this->h; }; bool get_output_state() { return this->output_state; }; + std::vector *get_output_gates() { return &this->output_gate_ids; }; GATE_TYPE get_gate_type() { return this->gate_type; }; Gate *get_input_gate1() { return this->input_gate1; }; Gate *get_input_gate2() { return this->input_gate2; }; @@ -55,6 +57,8 @@ class Gate void set_input_gate1(Gate *gate) { this->input_gate1 = gate; }; void set_input_gate2(Gate *gate) { this->input_gate2 = 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 update_state(); private: @@ -70,6 +74,7 @@ class Gate Gate *input_gate1; Gate *input_gate2; //Gate *output_gate; + std::vector output_gate_ids; /* states */ bool output_state = false; diff --git a/MainWindow.cpp b/MainWindow.cpp index 91fd4a4..3a5f359 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -146,11 +146,13 @@ MainWindow::draw() Gate *gate1; /* update every gate */ + /* for(auto g1 = gates.begin(); g1 != gates.end(); ++g1) { gate1 = (*g1).get(); gate1->update_state(); } + */ /* draw gates */ for(auto g1 = gates.begin(); g1 != gates.end(); ++g1) @@ -357,6 +359,24 @@ Gate return gate; } +void +MainWindow::update_gate_state(Gate *gate) +{ + gate->update_state(); + /* update all gates that are using this gate as an input */ + Gate *gate2; + printf("out gate size: %ld\n", gate->get_output_gates()->size()); + for(auto g = gate->get_output_gates()->begin(); g != gate->get_output_gates()->end(); ++g) + { + gate2 = find_gate_by_id((*g)); + if (gate2) + { + printf("updating gate %p from gate %p\n", gate2, gate); + update_gate_state(gate2); + } + } + +} long MainWindow::on_paint(FXObject*, FXSelector, void *ptr) @@ -391,7 +411,8 @@ MainWindow::on_left_mouse_down(FXObject*, FXSelector, void *ptr) { dragging_link = true; } - gate->update_state(); + //gate->update_state(); + update_gate_state(gate); } else { @@ -444,7 +465,10 @@ MainWindow::on_left_mouse_up(FXObject*, FXSelector, void *ptr) } } //selected_gate->set_output_gate(gate); - gate->update_state(); + selected_gate->add_output_gate_id(gate->get_id()); + printf("adding output gate"); + //gate->update_state(); + update_gate_state(gate); } dragging_link = false; } @@ -465,7 +489,8 @@ MainWindow::on_right_mouse_down(FXObject*, FXSelector, void *ptr) gate->set_state(!gate->get_output_state()); } } - gate->update_state(); + //gate->update_state(); + update_gate_state(gate); draw(); return 1; diff --git a/MainWindow.h b/MainWindow.h index d150325..ac3ff2e 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -83,6 +83,7 @@ class MainWindow : public FXMainWindow private: void create_ui(); void draw(); + void update_gate_state(Gate *gate); Gate *find_gate_at(int x, int y); Gate *find_gate_by_id(int id); -- cgit v1.2.3