Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <conio.h>
- #include <graphics.h>
- #include <iostream>
- #include <vector>
- enum NodeType {
- NONE = 0,
- START_NODE,
- STOP_NODE,
- INPUT_NODE,
- OUTPUT_NODE,
- ASSIGN_NODE,
- CONDITIONAL_NODE
- };
- const int WIDTH = 800;
- const int HEIGHT = 600;
- int pixelType[WIDTH][HEIGHT];
- struct Point {
- int x, y;
- void operator=(Point other) {
- x = other.x;
- y = other.y;
- }
- };
- struct Node {
- NodeType nodeType;
- char* nodeData;
- Point coord;
- int nodeId;
- Node(int _nodeId) {
- nodeType = NONE;
- nodeData = "Uninitialized node";
- coord = Point{0, 0};
- nodeId = _nodeId;
- m_nodeWidth = textwidth(nodeData) + 16;
- m_nodeHeight = textheight(nodeData) + 16;
- std::cout<<m_nodeHeight<<" " <<m_nodeWidth<<"\n"; std::cout.flush();
- }
- void showNode() {
- int tw = textwidth(nodeData);
- int th = textheight(nodeData);
- m_nodeWidth = tw+16;
- m_nodeHeight = th+16;
- setfillstyle(4, CYAN);
- bar(coord.x, coord.y, coord.x+m_nodeWidth, coord.y+m_nodeHeight);
- outtextxy(coord.x+8, coord.y+8, nodeData);
- rectangle(coord.x, coord.y, coord.x+m_nodeWidth-1, coord.y+m_nodeHeight-1);
- for(int x = coord.x; x < coord.x+tw+16; ++x) {
- for(int y = coord.y; y < coord.y+th+16; ++y) {
- pixelType[x][y] = nodeId;
- }
- }
- }
- void addNode() {
- int tw = textwidth(nodeData);
- int th = textheight(nodeData);
- setfillstyle(4, CYAN);
- // std::cout << "Adding a new node at: ";
- // std::cout.flush();
- for(int y = 0; y < HEIGHT-th-16; ++y) {
- for(int x = 0; x < WIDTH-tw-16; ++x) {
- bool ok = true;
- for(int _x = x; _x < x+tw+16; ++_x) {
- for(int _y = y; _y < y+th+16; ++_y) {
- ok &= (pixelType[_x][_y] < 0);
- }
- }
- if(ok) {
- // std::cout << x << " " << y << "\n";
- // std::cout.flush();
- coord = Point{x, y};
- showNode();
- return;
- }
- }
- }
- std::cout << "There is not enough space for another node";
- }
- void moveNode(Point newCoord) {
- int tmp[m_nodeWidth][m_nodeHeight];
- for(int x = coord.x; x < coord.x + m_nodeWidth; ++x) {
- for(int y = coord.y; y < coord.y + m_nodeHeight; ++y) {
- tmp[x-coord.x][y-coord.y] = getpixel(x, y);
- putpixel(x, y, BLACK);
- pixelType[x][y] = -1;
- }
- }
- for(int x = newCoord.x; x < newCoord.x + m_nodeWidth; ++x) {
- for(int y = newCoord.y; y < newCoord.y + m_nodeHeight; ++y) {
- putpixel(x, y, tmp[x-newCoord.x][y-newCoord.y]);
- pixelType[x][y] = nodeId;
- }
- }
- coord = newCoord;
- }
- private:
- int m_nodeWidth = 1;
- int m_nodeHeight = 1;
- };
- void line(Point A, Point B) {
- line(A.x, A.y, B.x, B.y);
- }
- int main() {
- initwindow(WIDTH, HEIGHT, "InterSchem");
- std::cout << "Press Left Click to add a node to the current position\n";
- std::cout << "Press Right Click to add a node to the first free position\n";
- std::cout << "Press and hold Middle Click on a node to move it to another location\n";
- for(int x = 0; x < WIDTH; ++x) {
- for(int y = 0; y < HEIGHT; ++y) {
- pixelType[x][y] = -1;
- }
- }
- int lastx = -1, lasty = -1;
- std::vector<Node*> nodes;
- while(1) {
- if(ismouseclick(WM_LBUTTONDOWN)) {
- clearmouseclick(WM_LBUTTONDOWN);
- int x = mousex();
- int y = mousey();
- if(pixelType[x][y] > 0) {
- continue;
- }
- Node* node = new Node(int(nodes.size()+1));
- node->coord = Point{x, y};
- node->nodeData = "This node is just a node";
- node->showNode();
- nodes.push_back(node);
- }
- if(ismouseclick(WM_RBUTTONDOWN)) {
- clearmouseclick(WM_RBUTTONDOWN);
- Node* node = new Node(int(nodes.size()));
- node->nodeData = "This is another ordinary node";
- node->addNode();
- nodes.push_back(node);
- }
- if(ismouseclick(WM_MBUTTONDOWN)) {
- clearmouseclick(WM_MBUTTONDOWN);
- int x = mousex();
- int y = mousey();
- if(pixelType[x][y] >= 0) {
- lastx = mousex();
- lasty = mousey();
- }
- }
- if(ismouseclick(WM_MBUTTONUP)) {
- clearmouseclick(WM_MBUTTONUP);
- if(lastx != -1 and lasty != -1) {
- int x = mousex();
- int y = mousey();
- Point p{x, y};
- nodes[pixelType[lastx][lasty]]->moveNode(p);
- }
- lastx = lasty = -1;
- }
- }
- getch();
- closegraph();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement