Advertisement
halexandru11

main.cpp

Dec 7th, 2021
36
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #if 10
  2.  
  3. #include <SFML/Graphics.hpp>
  4.  
  5. #include <iostream>
  6. #include <vector>
  7.  
  8. #include "Constants.hpp"
  9. #include "Node.hpp"
  10.  
  11. int main() {
  12. sf::RenderWindow window(sf::VideoMode(Constants::Width, Constants::Height), "InterSchem", sf::Style::Close);
  13. window.setVerticalSyncEnabled(true);
  14.  
  15. sf::Font font;
  16. if (!font.loadFromFile("Fonts\\Poppins\\Poppins-Regular.ttf")) {
  17. std::cout << "Could not load the font\n";
  18. exit(1);
  19. }
  20.  
  21. std::vector<Node*> nodes;
  22. nodes.clear();
  23. nodes.push_back(new Node(Constants::StartNode, font));
  24. nodes.push_back(new Node(Constants::AssignNode, font));
  25. nodes.push_back(new Node(Constants::ConditionalNode, font));
  26. nodes.push_back(new Node(Constants::OutputNode, font));
  27. nodes.push_back(new Node(Constants::StopNode, font));
  28.  
  29. nodes[0]->setNodeCoordonates(sf::Vector2f{80, 50});
  30. nodes[1]->setNodeCoordonates(sf::Vector2f{80, 100});
  31. nodes[2]->setNodeCoordonates(sf::Vector2f{80, 150});
  32. nodes[3]->setNodeCoordonates(sf::Vector2f{80, 200});
  33. nodes[4]->setNodeCoordonates(sf::Vector2f{80, 250});
  34.  
  35. nodes[0]->setTextString("Start");
  36. nodes[1]->setTextString("Assign");
  37. nodes[2]->setTextString("If");
  38. nodes[3]->setTextString("Output");
  39. nodes[4]->setTextString("Stop");
  40.  
  41. while (window.isOpen()) {
  42.  
  43. sf::Event event;
  44. while (window.pollEvent(event)) {
  45. switch (event.type) {
  46. case sf::Event::Closed: // window closed
  47. window.close();
  48. break;
  49. case sf::Event::TextEntered: // key pressed
  50. std::cout << char(event.text.unicode);
  51. break;
  52.  
  53. // we don't process other types of events
  54. default:
  55. break;
  56. }
  57. }
  58.  
  59. window.clear(sf::Color(255, 128, 0, 100));
  60.  
  61. for(std::size_t index = 0; index < nodes.size(); ++index) {
  62. window.draw(nodes[index]->getShape());
  63. window.draw(nodes[index]->text);
  64. // window.draw(nodes[index]->hitbox); // Pentru debug
  65. }
  66. window.display();
  67. }
  68.  
  69. return 0;
  70. }
  71.  
  72. #else
  73.  
  74. #include <SFML/Graphics.hpp>
  75. #include <bits/stdc++.h>
  76. #define WindowHeight 720
  77. #define WindowWidth 1280
  78. using namespace sf;
  79. using namespace std;
  80.  
  81. bool isInside(Vector2f MousePos, RectangleShape q)
  82. {
  83. Vector2f qOrigin = q.getPosition();
  84. Vector2f qSize = q.getSize();
  85. Vector2f susStanga = qOrigin;
  86. Vector2f josDreapta = qOrigin;
  87. susStanga.x -= qSize.x / 2;
  88. susStanga.y -= qSize.y / 2;
  89. josDreapta.x += qSize.x / 2;
  90. josDreapta.y += qSize.y / 2;
  91. cout << MousePos.x << ' ' << MousePos.y << '\n';
  92. cout << susStanga.x << ' ' << susStanga.y << '\n';
  93. cout << josDreapta.x << ' ' << josDreapta.y << '\n';
  94. cout << "\n\n";
  95. if(josDreapta.x >= MousePos.x && MousePos.x >= susStanga.x &&
  96. josDreapta.y >= MousePos.y && MousePos.y >= susStanga.y)
  97. {
  98. cout << "GOOD\n";
  99. return 1;
  100. }
  101. return 0;
  102. }
  103.  
  104. void adauga_nod(vector <RectangleShape> &D)
  105. {
  106. RectangleShape patrat2(Vector2f(100.0f,100.0f));
  107. patrat2.setFillColor(Color::Green);
  108. patrat2.setOrigin(50.0f, 50.0f);
  109. patrat2.setPosition(200.0f, 200.f);
  110. D.push_back(patrat2);
  111. }
  112. int main()
  113. {
  114. RenderWindow window(VideoMode(WindowWidth,WindowHeight), "Interschem", Style::Close | Style::Titlebar);
  115. RectangleShape patrat(Vector2f(100.0f,100.0f));
  116. patrat.setFillColor(Color::Red);
  117. patrat.setOrigin(50.0f, 50.0f);
  118. patrat.setPosition(550.0f, 500.0f);
  119.  
  120. RectangleShape workbench(Vector2f(1100.0f,900.0f));
  121. workbench.setFillColor(Color(59,71,57,255));
  122. workbench.setOrigin(0.0f, 0.0f);
  123. workbench.setPosition(125.0f, 0.0f);
  124.  
  125.  
  126. RectangleShape button(Vector2f(125.0f,50.0f));
  127. button.setFillColor(Color::Blue);
  128. button.setOrigin(62.5f, 25.0f);
  129. button.setPosition(62.5f, 75.0f);
  130.  
  131. vector <RectangleShape> D;
  132. D.push_back(patrat);
  133. /*
  134. */
  135.  
  136. Font font;
  137. font.loadFromFile("Fonts\\Poppins\\Poppins-Regular.ttf");
  138. Text text;
  139. text.setFont(font);
  140. text.setString("Adauga Nod");
  141. text.setCharacterSize(18);
  142. text.setFillColor(Color::White);
  143. text.setPosition(5.0f, 58.0f);
  144. bool hold = false;
  145. Vector2i oldPos;
  146. int target = -1;
  147. int emptyRectangle = -1;
  148. while (window.isOpen())
  149. {
  150. Event evnt;
  151. while (window.pollEvent(evnt))
  152. {
  153. if(evnt.type == Event::Closed)
  154. {
  155. window.close();
  156. }
  157. else if(evnt.type == Event::Resized)
  158. {
  159. cout << evnt.size.width << ' ' << evnt.size.width << '\n';
  160. }
  161. else if(evnt.type == Event::MouseButtonPressed)
  162. {
  163. if(evnt.mouseButton.button == Mouse::Left)
  164. {
  165. hold = true;
  166. oldPos = Mouse::getPosition(window);
  167. Vector2f pos;
  168. pos.x = static_cast<float>(oldPos.x);
  169. pos.y = static_cast<float>(oldPos.y);
  170. for(int i = 0; i < D.size(); ++i)
  171. if(isInside(pos, D[i]))
  172. {
  173. target = i;
  174. break;
  175. }
  176. if(isInside(pos, button))
  177. {
  178. adauga_nod(D);
  179. }
  180. }
  181. else if(evnt.mouseButton.button == Mouse::Right)
  182. {
  183. oldPos = Mouse::getPosition(window);
  184. Vector2f pos;
  185. pos.x = static_cast<float>(oldPos.x);
  186. pos.y = static_cast<float>(oldPos.y);
  187. bool deletee = 0;
  188. for(int i = 0; i < D.size(); ++i)
  189. if(isInside(pos, D[i]))
  190. {
  191. swap(D[i], D[D.size()-1]);
  192. deletee = 1;
  193. break;
  194. }
  195. if(deletee)
  196. D.pop_back();
  197. }
  198. }
  199. /*
  200. else if(hold && evnt.type == Event::MouseMoved)
  201. {
  202. cout << "M AM MUTAT\n";
  203. if(D[target].getFillColor() == Color::Red)
  204. cout << "RED" << '\n';
  205. else if(D[target].getFillColor() == Color::Green)
  206. cout << "GREEN" << '\n';
  207. Vector2i pozitieMouse = Mouse::getPosition(window);
  208. D[target].setPosition(static_cast<float>(pozitieMouse.x), static_cast<float>(pozitieMouse.y));
  209. }
  210. */
  211. else if(evnt.type == Event::MouseButtonReleased)
  212. {
  213. target = -1;
  214. hold = false;
  215. }
  216. }
  217. if(hold && target != -1)
  218. {
  219. Vector2i pozitieMouse = Mouse::getPosition(window);
  220. D[target].setPosition(static_cast<float>(pozitieMouse.x), static_cast<float>(pozitieMouse.y));
  221. //D[target].setOrigin(static_cast<float>(pozitieMouse.x), static_cast<float>(pozitieMouse.y));
  222. }
  223. /*
  224. if(Mouse::isButtonPressed(Mouse::Left))
  225. {
  226. Vector2i pozitieMouse = Mouse::getPosition(window);
  227. patrat.setPosition(static_cast<float>(pozitieMouse.x), static_cast<float>(pozitieMouse.y));
  228. }
  229. */
  230. window.clear();
  231. window.draw(workbench);
  232. for(auto it : D)
  233. window.draw(it);
  234. window.draw(button);
  235. window.draw(text);
  236. window.display();
  237. }
  238.  
  239. return 0;
  240. }
  241.  
  242.  
  243.  
  244. #endif
  245.  
Advertisement
RAW Paste Data Copied
Advertisement