Advertisement
halexandru11

Alpha 002

Dec 1st, 2021
843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.07 KB | None | 0 0
  1. #include <conio.h>
  2. #include <graphics.h>
  3. #include <iostream>
  4. #include <vector>
  5.  
  6. enum NodeType {
  7.     NONE = 0,
  8.     START_NODE,
  9.     STOP_NODE,
  10.     INPUT_NODE,
  11.     OUTPUT_NODE,
  12.     ASSIGN_NODE,
  13.     CONDITIONAL_NODE
  14. };
  15.  
  16. const int WIDTH = 800;
  17. const int HEIGHT = 600;
  18. int pixelType[WIDTH][HEIGHT];
  19.  
  20. struct Point {
  21.     int x, y;
  22.  
  23.     void operator=(Point other) {
  24.         x = other.x;
  25.         y = other.y;
  26.     }
  27. };
  28.  
  29. struct Node {
  30.     NodeType nodeType;
  31.     char* nodeData;
  32.  
  33.     Point coord;
  34.     int nodeId;
  35.  
  36.     Node(int _nodeId) {
  37.         nodeType = NONE;
  38.         nodeData = "Uninitialized node";
  39.         coord = Point{0, 0};
  40.         nodeId = _nodeId;
  41.         m_nodeWidth = textwidth(nodeData) + 16;
  42.         m_nodeHeight = textheight(nodeData) + 16;
  43.         std::cout<<m_nodeHeight<<" " <<m_nodeWidth<<"\n"; std::cout.flush();
  44.     }
  45.  
  46.     void showNode() {
  47.         int tw = textwidth(nodeData);
  48.         int th = textheight(nodeData);
  49.         m_nodeWidth = tw+16;
  50.         m_nodeHeight = th+16;
  51.         setfillstyle(4, CYAN);
  52.         bar(coord.x, coord.y, coord.x+m_nodeWidth, coord.y+m_nodeHeight);
  53.         outtextxy(coord.x+8, coord.y+8, nodeData);
  54.         rectangle(coord.x, coord.y, coord.x+m_nodeWidth-1, coord.y+m_nodeHeight-1);
  55.  
  56.         for(int x = coord.x; x < coord.x+tw+16; ++x) {
  57.             for(int y = coord.y; y < coord.y+th+16; ++y) {
  58.                 pixelType[x][y] = nodeId;
  59.             }
  60.         }
  61.     }
  62.  
  63.     void addNode() {
  64.         int tw = textwidth(nodeData);
  65.         int th = textheight(nodeData);
  66.         setfillstyle(4, CYAN);
  67.  
  68. //        std::cout << "Adding a new node at: ";
  69. //        std::cout.flush();
  70.         for(int y = 0; y < HEIGHT-th-16; ++y) {
  71.             for(int x = 0; x < WIDTH-tw-16; ++x) {
  72.                 bool ok = true;
  73.                 for(int _x = x; _x < x+tw+16; ++_x) {
  74.                     for(int _y = y; _y < y+th+16; ++_y) {
  75.                         ok &= (pixelType[_x][_y] < 0);
  76.                     }
  77.                 }
  78.                 if(ok) {
  79. //                    std::cout << x << " " << y << "\n";
  80. //                    std::cout.flush();
  81.                     coord = Point{x, y};
  82.                     showNode();
  83.                     return;
  84.                 }
  85.             }
  86.         }
  87.         std::cout << "There is not enough space for another node";
  88.     }
  89.  
  90.     void moveNode(Point newCoord) {
  91.         int tmp[m_nodeWidth][m_nodeHeight];
  92.         for(int x = coord.x; x < coord.x + m_nodeWidth; ++x) {
  93.             for(int y = coord.y; y < coord.y + m_nodeHeight; ++y) {
  94.                 tmp[x-coord.x][y-coord.y] = getpixel(x, y);
  95.                 putpixel(x, y, BLACK);
  96.                 pixelType[x][y] = -1;
  97.             }
  98.         }
  99.         for(int x = newCoord.x; x < newCoord.x + m_nodeWidth; ++x) {
  100.             for(int y = newCoord.y; y < newCoord.y + m_nodeHeight; ++y) {
  101.                 putpixel(x, y, tmp[x-newCoord.x][y-newCoord.y]);
  102.                 pixelType[x][y] = nodeId;
  103.             }
  104.         }
  105.         coord = newCoord;
  106.     }
  107.  
  108. private:
  109.     int m_nodeWidth = 1;
  110.     int m_nodeHeight = 1;
  111. };
  112.  
  113. void line(Point A, Point B) {
  114.     line(A.x, A.y, B.x, B.y);
  115. }
  116.  
  117. int main() {
  118.     initwindow(WIDTH, HEIGHT, "InterSchem");
  119.  
  120.     std::cout << "Press Left Click to add a node to the current position\n";
  121.     std::cout << "Press Right Click to add a node to the first free position\n";
  122.     std::cout << "Press and hold Middle Click on a node to move it to another location\n";
  123.  
  124.     for(int x = 0; x < WIDTH; ++x) {
  125.         for(int y = 0; y < HEIGHT; ++y) {
  126.             pixelType[x][y] = -1;
  127.         }
  128.     }
  129.  
  130.     int lastx = -1, lasty = -1;
  131.     std::vector<Node*> nodes;
  132.     while(1) {
  133.         if(ismouseclick(WM_LBUTTONDOWN)) {
  134.             clearmouseclick(WM_LBUTTONDOWN);
  135.             int x = mousex();
  136.             int y = mousey();
  137.             if(pixelType[x][y] > 0) {
  138.                 continue;
  139.             }
  140.             Node* node = new Node(int(nodes.size()+1));
  141.             node->coord = Point{x, y};
  142.             node->nodeData = "This node is just a node";
  143.             node->showNode();
  144.             nodes.push_back(node);
  145.         }
  146.         if(ismouseclick(WM_RBUTTONDOWN)) {
  147.             clearmouseclick(WM_RBUTTONDOWN);
  148.             Node* node = new Node(int(nodes.size()));
  149.             node->nodeData = "This is another ordinary node";
  150.             node->addNode();
  151.             nodes.push_back(node);
  152.         }
  153.         if(ismouseclick(WM_MBUTTONDOWN)) {
  154.             clearmouseclick(WM_MBUTTONDOWN);
  155.             int x = mousex();
  156.             int y = mousey();
  157.             if(pixelType[x][y] >= 0) {
  158.                 lastx = mousex();
  159.                 lasty = mousey();
  160.             }
  161.         }
  162.         if(ismouseclick(WM_MBUTTONUP)) {
  163.             clearmouseclick(WM_MBUTTONUP);
  164.             if(lastx != -1 and lasty != -1) {
  165.                 int x = mousex();
  166.                 int y = mousey();
  167.                 Point p{x, y};
  168.                 nodes[pixelType[lastx][lasty]]->moveNode(p);
  169.             }
  170.             lastx = lasty = -1;
  171.         }
  172.     }
  173.  
  174.     getch();
  175.     closegraph();
  176. }
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement