From 93f4f323e5374e65a9cadd5903fa020188e59ee1 Mon Sep 17 00:00:00 2001 From: Daniel Jones Date: Tue, 30 Jun 2020 16:41:17 +0930 Subject: Functionality: basic binary display functions, needs work --- BinaryDisplay.cpp | 43 +++++++++++++++++- BinaryDisplay.h | 11 +++++ Gate.h | 4 -- MainWindow.cpp | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- Object.h | 6 +++ 5 files changed, 188 insertions(+), 9 deletions(-) diff --git a/BinaryDisplay.cpp b/BinaryDisplay.cpp index 0e55ba3..8526511 100644 --- a/BinaryDisplay.cpp +++ b/BinaryDisplay.cpp @@ -12,5 +12,46 @@ BinaryDisplay::BinaryDisplay(int x_, int y_, int width, int height) void BinaryDisplay::update_state() { - puts("update binary display"); + sum_value = 0; + if (input0 && input0->get_object_type() == Object::GATE && input0->get_output_state()) + sum_value |= 1 << 0; + if (input1 && input1->get_object_type() == Object::GATE && input1->get_output_state()) + sum_value |= 1 << 1; + if (input2 && input2->get_object_type() == Object::GATE && input2->get_output_state()) + sum_value |= 1 << 2; + if (input3 && input3->get_object_type() == Object::GATE && input3->get_output_state()) + sum_value |= 1 << 3; + if (input4 && input4->get_object_type() == Object::GATE && input4->get_output_state()) + sum_value |= 1 << 4; + if (input5 && input5->get_object_type() == Object::GATE && input5->get_output_state()) + sum_value |= 1 << 5; + if (input6 && input6->get_object_type() == Object::GATE && input6->get_output_state()) + sum_value |= 1 << 6; + if (input7 && input7->get_object_type() == Object::GATE && input7->get_output_state()) + sum_value |= 1 << 7; } + +void +BinaryDisplay::remove_input(int id) +{ + if (input0 && input0->get_id() == id) + input0 = nullptr; + + if (input1 && input1->get_id() == id) + input1 = nullptr; + if (input2 && input2->get_id() == id) + input2 = nullptr; + if (input3 && input3->get_id() == id) + input3 = nullptr; + if (input4 && input5->get_id() == id) + input5 = nullptr; + if (input5 && input5->get_id() == id) + input5 = nullptr; + if (input6 && input6->get_id() == id) + input6 = nullptr; + if (input7 && input7->get_id() == id) + input7 = nullptr; + update_state(); +} + + diff --git a/BinaryDisplay.h b/BinaryDisplay.h index a92204c..488e889 100644 --- a/BinaryDisplay.h +++ b/BinaryDisplay.h @@ -20,6 +20,17 @@ class BinaryDisplay : public Object Object *get_input6() { return input6; }; Object *get_input7() { return input7; }; + void set_input0(Object *object_) { input0 = object_; }; + void set_input1(Object *object_) { input1 = object_; }; + void set_input2(Object *object_) { input2 = object_; }; + void set_input3(Object *object_) { input3 = object_; }; + void set_input4(Object *object_) { input4 = object_; }; + void set_input5(Object *object_) { input5 = object_; }; + void set_input6(Object *object_) { input6 = object_; }; + void set_input7(Object *object_) { input7 = object_; }; + + void remove_input(int id); + int get_sum_value() { return sum_value; }; private: diff --git a/Gate.h b/Gate.h index b677704..ab6b238 100644 --- a/Gate.h +++ b/Gate.h @@ -40,12 +40,10 @@ class Gate : public Object Gate(GATE_TYPE type = INPUT, int x = 0, int y = 0, int width = 70, int height = 50, int id = -1); ~Gate(); - bool get_output_state() { return this->output_state; }; std::vector *get_output_objects() { return &this->output_object_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; }; - 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 remove_input_gate(int id); @@ -63,8 +61,6 @@ class Gate : public Object std::string get_output_type_text(); - /* states */ - bool output_state = false; }; #endif diff --git a/MainWindow.cpp b/MainWindow.cpp index 13d683a..18f9a61 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -358,7 +358,56 @@ MainWindow::draw() } case Object::BINARYDISPLAY: { - printf("imeplement bdsp link drawing\n"); + class BinaryDisplay *bdsp = (class BinaryDisplay*)(*g1).get(); + Object *input; + int gap = 13; + if ((input = bdsp->get_input0())) + { + dc_image.drawLine(input->get_x()+input->get_width()-5, input->get_y()+(input->get_height()/2), + bdsp->get_x()+10, bdsp->get_y()+(gap*7)); + } + if ((input = bdsp->get_input1())) + { + dc_image.drawLine(input->get_x()+input->get_width()-5, input->get_y()+(input->get_height()/2), + bdsp->get_x()+10, bdsp->get_y()+(gap*6)); + } + if ((input = bdsp->get_input2())) + { + dc_image.drawLine(input->get_x()+input->get_width()-5, input->get_y()+(input->get_height()/2), + bdsp->get_x()+10, bdsp->get_y()+(gap*5)); + } + if ((input = bdsp->get_input3())) + { + dc_image.drawLine(input->get_x()+input->get_width()-5, input->get_y()+(input->get_height()/2), + bdsp->get_x()+10, bdsp->get_y()+(gap*4)); + } + if ((input = bdsp->get_input4())) + { + dc_image.drawLine(input->get_x()+input->get_width()-5, input->get_y()+(input->get_height()/2), + bdsp->get_x()+10, bdsp->get_y()+(gap*3)); + } + if ((input = bdsp->get_input5())) + { + dc_image.drawLine(input->get_x()+input->get_width()-5, input->get_y()+(input->get_height()/2), + bdsp->get_x()+10, bdsp->get_y()+(gap*2)); + } + if ((input = bdsp->get_input6())) + { + dc_image.drawLine(input->get_x()+input->get_width()-5, input->get_y()+(input->get_height()/2), + bdsp->get_x()+10, bdsp->get_y()+(gap*2)); + } + if ((input = bdsp->get_input7())) + { + dc_image.drawLine(input->get_x()+input->get_width()-5, input->get_y()+(input->get_height()/2), + bdsp->get_x()+10, bdsp->get_y()+(gap*1)); + } + + + + + + + break; } case Object::NONE: @@ -965,13 +1014,13 @@ MainWindow::on_left_mouse_up(FXObject*, FXSelector, void *ptr) object = find_object_at(ev->last_x, ev->last_y); if (object) { + if (object == selected_object) /* objects cannot connect to themselves, probably */ + return 1; switch (object->get_object_type()) { case Object::GATE: { Gate *gate = (Gate*)object; - if (gate == selected_object) /* gates cannot connect to themselves, probably */ - return 1; if (gate && gate->get_gate_type() != Gate::INPUT) { int input = -1; @@ -1005,9 +1054,85 @@ MainWindow::on_left_mouse_up(FXObject*, FXSelector, void *ptr) } break; } + case Object::BINARYDISPLAY: + { + /* connect object to bdsp input */ + class BinaryDisplay *bdsp = (class BinaryDisplay*)object; + /* figure out which input we're connecting to */ + int input = -1; + int gap = 13; + if (ev->last_y-bdsp->get_y() <= gap) + { + input = 7; + puts("input 7"); + bdsp->set_input7(selected_object); + selected_object->add_output_object_id(bdsp->get_id()); + update_object_state(bdsp); + } + else if (ev->last_y-(bdsp->get_y()+(gap * 1)) <= gap) + { + input = 6; + puts("input 6"); + bdsp->set_input6(selected_object); + selected_object->add_output_object_id(bdsp->get_id()); + update_object_state(bdsp); + } + else if (ev->last_y-(bdsp->get_y()+(gap * 2)) <= gap) + { + input = 5; + puts("input 5"); + bdsp->set_input5(selected_object); + selected_object->add_output_object_id(bdsp->get_id()); + update_object_state(bdsp); + } + else if (ev->last_y-(bdsp->get_y()+(gap * 3)) <= gap) + { + input = 4; + puts("input 4"); + bdsp->set_input4(selected_object); + selected_object->add_output_object_id(bdsp->get_id()); + update_object_state(bdsp); + } + else if (ev->last_y-(bdsp->get_y()+(gap * 4)) <= gap) + { + input = 3; + puts("input 3"); + bdsp->set_input3(selected_object); + selected_object->add_output_object_id(bdsp->get_id()); + update_object_state(bdsp); + } + else if (ev->last_y-(bdsp->get_y()+(gap * 5)) <= gap) + { + input = 2; + puts("input 2"); + bdsp->set_input2(selected_object); + selected_object->add_output_object_id(bdsp->get_id()); + update_object_state(bdsp); + } + else if (ev->last_y-(bdsp->get_y()+(gap * 6)) <= gap) + { + input = 1; + puts("input 1"); + bdsp->set_input1(selected_object); + selected_object->add_output_object_id(bdsp->get_id()); + update_object_state(bdsp); + } + + else if (ev->last_y-(bdsp->get_y()+(gap * 7)) <= gap) + { + input = 0; + puts("input 0"); + bdsp->set_input0(selected_object); + selected_object->add_output_object_id(bdsp->get_id()); + update_object_state(bdsp); + } + + + break; + } case Object::NONE: default: - printf("not implemented object left down\n"); + printf("not implemented object left down link\n"); break; } } diff --git a/Object.h b/Object.h index 49e750e..36a2efc 100644 --- a/Object.h +++ b/Object.h @@ -74,6 +74,9 @@ class Object } } + bool get_output_state() { return this->output_state; }; + void set_state(bool state) { this->output_state = state; }; + protected: int id; int x; @@ -86,6 +89,9 @@ class Object void set_object_type(OBJECT_TYPE type) { object_type = type; }; // every object must set this, none by default static int object_id_counter; // used as the id of a new objecct - this is NOT a count of the number of objects + + /* states */ + bool output_state = false; private: enum OBJECT_TYPE object_type = NONE; -- cgit v1.2.3