From 1e76ef7d2df9d49a905f416774f33fdedfcf6f34 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Mon, 29 Jun 2020 22:45:32 +0930 Subject: Logic: created ned Object class and derived gates from that This class will allow us to make new devices (displays, custom gates etc) easily --- CMakeLists.txt | 1 + Gate.cpp | 4 ++-- Gate.h | 26 +++----------------------- MainWindow.cpp | 12 ++++++------ MainWindow.h | 3 ++- 5 files changed, 14 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a03582..18f1026 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,5 +17,6 @@ add_executable(foxlogicgates Gate.cpp Gate.h icons.h + Object.h ) target_link_libraries(foxlogicgates FOX-1.6) diff --git a/Gate.cpp b/Gate.cpp index 51a1652..a1ed3a7 100644 --- a/Gate.cpp +++ b/Gate.cpp @@ -15,7 +15,7 @@ #include "Gate.h" -int Gate::gate_id_counter = 0; +int Object::object_id_counter = 0; // initialise static object counter FIXME: do it somehwere else? Gate::Gate(GATE_TYPE type, int x, int y, int width, int height, int loaded_id) { @@ -28,7 +28,7 @@ Gate::Gate(GATE_TYPE type, int x, int y, int width, int height, int loaded_id) if (loaded_id != -1) this->id = loaded_id; else - this->id = Gate::gate_id_counter++; // increment counter after assigning + this->id = Object::object_id_counter++; // increment counter after assigning this->x = x; this->y = y; this->w = width; diff --git a/Gate.h b/Gate.h index 7073aa7..a0d8cf6 100644 --- a/Gate.h +++ b/Gate.h @@ -18,8 +18,9 @@ #include #include +#include "Object.h" -class Gate +class Gate : public Object { public: enum GATE_TYPE @@ -36,52 +37,31 @@ class Gate XNOR, }; - Gate(GATE_TYPE type = INPUT, int x = 0, int y = 0, int width = 70, int height = 50, int loaded_id = -1); + Gate(GATE_TYPE type = INPUT, int x = 0, int y = 0, int width = 70, int height = 50, int id = -1); ~Gate(); - int get_id() { return this->id; } - int get_x() { return this->x; }; - int get_y() { return this->y; }; - 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; }; - //Gate *get_output_gate() { return this->output_gate; }; std::string get_output_type_text(); - void set_x(int x) { this->x = x; }; - void set_y(int y) { this->y = y; }; void set_state(bool state) { this->output_state = state; }; 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 remove_input_gate(int id); void update_state(); - static void set_id_counter(int id) { gate_id_counter = id; }; - static int get_id_counter() { return gate_id_counter; }; private: GATE_TYPE gate_type; - int id; - int x; - int y; - int w; - int h; - - static int gate_id_counter; // used as the id of a new gate - this is NOT a count of the number of gates /* inputs/outputs */ 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 ace6a1d..49bee5a 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -263,7 +263,7 @@ MainWindow::draw() Gate *selgate; for (auto g = selected_gates.begin(); g != selected_gates.end(); ++g) { - selgate = (*g); + selgate = (Gate*)(*g); dc_image.drawHashBox(selgate->get_x(), selgate->get_y(), selgate->get_width(), selgate->get_height()); } } @@ -523,7 +523,7 @@ MainWindow::save_file() /* write meta data */ auto meta = doc.append_child("Meta"); pugi::xml_node info_xml = meta.append_child("Info"); - info_xml.append_attribute("next_id") = Gate::get_id_counter(); + info_xml.append_attribute("next_id") = Object::get_object_id_counter(); auto root = doc.append_child("Gates"); @@ -665,7 +665,7 @@ MainWindow::load_file() } /* set gate id counter */ - Gate::set_id_counter(next_gate_id); + Object::set_object_id_counter(next_gate_id); return true; } @@ -676,7 +676,7 @@ MainWindow::remove_all_gates() selected_gate = nullptr; selected_input.gate = nullptr; selected_input.input = -1; - Gate::set_id_counter(0); + Object::set_object_id_counter(0); draw(); } @@ -825,7 +825,7 @@ MainWindow::on_left_mouse_down(FXObject*, FXSelector, void *ptr) /* clear selection if we're not clicking on a selected gate */ for (auto g = selected_gates.begin(); g != selected_gates.end(); ++g) { - selgate = (*g); + selgate = (Gate*)(*g); if (gate->get_id() == selgate->get_id()) { found_gate = true; @@ -981,7 +981,7 @@ MainWindow::on_key_release(FXObject *sender, FXSelector sel, void *ptr) Gate *gate; for (auto g = selected_gates.begin(); g != selected_gates.end(); ++g) { - gate = (*g); + gate = (Gate*)(*g); remove_gate(*gate); } diff --git a/MainWindow.h b/MainWindow.h index b6ba8e6..965c822 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -28,6 +28,7 @@ #include #include "Gate.h" #include "icons.h" +#include "Object.h" #include "pugixml.hpp" // saving/loading class MainWindow : public FXMainWindow @@ -161,7 +162,7 @@ class MainWindow : public FXMainWindow Gate::GATE_TYPE selected_gate_type = Gate::NONE; // the type of gate we will place struct selected_input selected_input; - std::vector selected_gates; + std::vector selected_gates; /* mouse */ bool lmouse_down = false; -- cgit v1.2.3