Advertisement
Korotkodul

logic_logic_shapes.h

Mar 16th, 2025
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.51 KB | None | 0 0
  1. #ifndef LOGIC_SHAPES_H
  2. #define LOGIC_SHAPES_H 1
  3.  
  4. //#include <Graph_lib/Graph.h>
  5. #include <Graph_lib/Graph.h>
  6. #include <Graph_lib/ext/graph.h>
  7.  
  8. #include "logic.h"
  9.  
  10.  
  11. namespace Logic {
  12.  
  13. const Graph_lib::Color BACKGROUND_COLOR { Graph_lib::Color::Color_type::white };
  14. const Graph_lib::Color FALSE_COLOR      { Graph_lib::Color::Color_type::dark_red };
  15. const Graph_lib::Color TRUE_COLOR       { Graph_lib::Color::Color_type::dark_green };
  16. const Graph_lib::Color LABEL_COLOR      { Graph_lib::Color::Color_type::dark_cyan };
  17.  
  18. const Graph_lib::Line_style SHAPE_LINE_TYPE      { Graph_lib::Line_style::Line_style_type::solid, 3 };
  19. const Graph_lib::Line_style CONNECTION_LINE_TYPE { Graph_lib::Line_style::Line_style_type::solid, 2 };
  20. const Graph_lib::Line_style CIRCLE_LINE_TYPE     { Graph_lib::Line_style::Line_style_type::solid, 2 };
  21.  
  22. const Graph_lib::Font LABEL_FONT { Graph_lib::Font::Font_type::helvetica_bold };
  23.  
  24.  
  25. constexpr int DEFAULT_WIDTH  = 40;
  26. constexpr int DEFAULT_HEIGHT = 31;  // preferably odd
  27. constexpr int CIRCLES_RADIUS = 4;
  28. constexpr int LABEL_MARGIN_Y = 3;
  29.  
  30.  
  31. class ElementShape;
  32. class OperatorShape;
  33. class ConnectionShape;
  34.  
  35.  
  36. class SchemeShape : public Graph_lib::Rectangle
  37. {
  38. public:
  39.   SchemeShape (const Graph_lib::Point& pos, int width, int height);
  40.  
  41.   void attach (ElementShape& elem_shape);
  42.   void attach (OperatorShape& elem_shape);
  43.  
  44.   void update_connections ();
  45. protected:
  46.   virtual void draw_lines () const override;
  47.  
  48.   Graph_lib::Vector_ref<ElementShape> elem_shapes;     // to be drawn
  49.   Graph_lib::Vector_ref<ConnectionShape> connections;  // to be drawn
  50.   Graph_lib::Vector_ref<OperatorShape> oper_shapes;    // not to be draw (used to determine connections)
  51. };
  52.  
  53.  
  54. class ElementShape : public Graph_lib::Shape
  55. {
  56. protected:
  57.   ElementShape (SchemeShape& s,
  58.                 Element& e,
  59.                 const std::string& name,
  60.                 const Graph_lib::Point& pos,    // left top
  61.                 int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
  62. public:
  63.   virtual ~ElementShape () override  { elem.set_callback(nullptr); }
  64.  
  65.   Graph_lib::Point output_pos () const  { return point(0) + Graph_lib::Point{ w, h/2 }; }
  66.   const Element& parent () const  { return elem; }
  67.  
  68.   // method to be called when elem out is changed
  69.   virtual void on_change (const Element& e);
  70.  
  71.   void callback_func (const Element& e);
  72.  
  73. protected:
  74.   virtual void draw_lines () const override;
  75.  
  76.   SchemeShape& scheme;
  77.   Element& elem;
  78.   int w, h;
  79.   Graph_lib::Text label;
  80.   Graph_lib::Circle out_circle;
  81. };
  82.  
  83.  
  84. class SourceShape : public ElementShape
  85. {
  86. public:
  87.   SourceShape (SchemeShape& scheme,
  88.                Source& e,
  89.                const std::string& name,
  90.                const Graph_lib::Point& pos,  // left top
  91.                int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
  92.  
  93.   virtual void on_change (const Element& e) override;
  94.  
  95. protected:
  96.   virtual void draw_lines () const override;
  97.  
  98.   // shapes of the body
  99.   Graph_lib::Rectangle body;
  100. };
  101.  
  102.  
  103. // connection consist of line and may be circle if needed
  104. class ConnectionShape : public Graph_lib::Shape
  105. {
  106. public:
  107.   ConnectionShape (const ElementShape& elem, const OperatorShape& oper, size_t i);
  108.  
  109. protected:
  110.   virtual void draw_lines () const override;
  111.  
  112.   Graph_lib::Line line;
  113.   Graph_lib::Line dash;
  114.   Graph_lib::Circle circle;
  115.   bool inverted{ false };
  116. };
  117.  
  118.  
  119. class OperatorShape : public ElementShape
  120. {
  121. protected:
  122.   OperatorShape (SchemeShape& scheme,
  123.                  Operation& e,
  124.                  const std::string& name,
  125.                  const Graph_lib::Point& pos,  // left top
  126.                  int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
  127. public:
  128.   virtual Graph_lib::Point input_pos (const Element& e, size_t i)   const;
  129.  
  130.   const Operation& parent () const  { return static_cast<const Operation&>(elem); }
  131.  
  132. protected:
  133.   virtual void draw_lines () const override;
  134.  
  135.   size_t inputs_count () const  { return parent().get_inputs().size(); }
  136. };
  137.  
  138.  
  139. class AndShape : public OperatorShape
  140. {
  141. public:
  142.   AndShape (SchemeShape& scheme,
  143.             And& e,
  144.             const std::string& name,
  145.             const Graph_lib::Point& pos,  // left top
  146.             int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
  147.  
  148.   virtual void on_change (const Element& e) override;
  149.  
  150. protected:
  151.   virtual void draw_lines () const override;
  152.  
  153.   // shapes of the body
  154.   Graph_lib::Open_polyline left_side;
  155.   Graph_lib::Arc right_side;
  156. };
  157.  
  158. class OrShape : public OperatorShape
  159. {
  160. public:
  161.   OrShape (SchemeShape& scheme,
  162.            Or& e,
  163.            const std::string& name,
  164.            const Graph_lib::Point& pos,  // left top
  165.            int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
  166.  
  167.   virtual Graph_lib::Point input_pos (const Element& e, size_t i) const override;
  168.  
  169.   virtual void on_change (const Element& e) override;
  170.  
  171. protected:
  172.   virtual void draw_lines () const override;
  173.  
  174.   // shapes of the body
  175.   Graph_lib::Arc left_side;
  176.   Graph_lib::Arc right_side;
  177. };
  178.  
  179. class XorShape : public OperatorShape
  180. {
  181. public:
  182.   XorShape (SchemeShape& scheme,
  183.             Xor& e,
  184.             const std::string& name,
  185.             const Graph_lib::Point& pos,  // left top
  186.             int width = DEFAULT_WIDTH, int height = DEFAULT_HEIGHT);
  187.  
  188.   virtual void on_change (const Element& e) override;
  189.  
  190. protected:
  191.   virtual void draw_lines () const override;
  192.  
  193.   // shapes of the body
  194.   Graph_lib::Open_polyline left_side;
  195.   Graph_lib::Arc right_side;
  196. };
  197.  
  198.  
  199. } // namespace Logic
  200.  
  201. #endif // LOGIC_SHAPES_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement