Advertisement
halexandru11

Node.hpp

Dec 7th, 2021
41
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2.  
  3. #include <string>
  4.  
  5. #include "Constants.hpp"
  6.  
  7. struct Node {
  8. public:
  9. Node(Constants::NodeType nodeType_, sf::Font& font) {
  10. nodeType = nodeType_;
  11.  
  12. text.setFont(font);
  13. text.setString("");
  14. text.setCharacterSize(16);
  15. text.setFillColor(sf::Color::Black);
  16. }
  17.  
  18. void setNodeCoordonates(sf::Vector2f coord) {
  19. m_coord = coord;
  20. }
  21.  
  22. sf::Vector2f getNodeCoordonates() {
  23. return m_coord;
  24. }
  25.  
  26. void setTextString(std::string textString) {
  27. text.setString(textString);
  28.  
  29. // determin dimensiunile nodului
  30. width = text.getGlobalBounds().width + 2*m_padding;
  31. height = text.getGlobalBounds().height + 2*m_padding;
  32.  
  33.  
  34. }
  35.  
  36. sf::ConvexShape getShape() {
  37. if(m_coord == sf::Vector2f{-1, -1}) {
  38. throw std::invalid_argument("You did not set the coordinates for this shape");
  39. }
  40. if(!m_shapeAssigned) {
  41. m_shapeAssigned = true;
  42. m_shape = setShape();
  43. }
  44. return m_shape;
  45. }
  46.  
  47. public:
  48. Constants::NodeType nodeType;
  49. int height = 30;
  50. int width = 100;
  51. sf::RectangleShape hitbox;
  52. sf::Text text;
  53. sf::Vector2f coordIn;
  54. sf::Vector2f coordOut;
  55. sf::Vector2f coordOut1;
  56. sf::Vector2f coordOut2;
  57.  
  58. private:
  59. sf::ConvexShape setShape() {
  60. hitbox = sf::RectangleShape(sf::Vector2f{width, height});
  61. hitbox.setPosition(m_coord - sf::Vector2f{width/2, height/2});
  62. hitbox.setFillColor(sf::Color::Transparent);
  63. hitbox.setOutlineColor(sf::Color::Green);
  64. hitbox.setOutlineThickness(2);
  65.  
  66. int textH = text.getGlobalBounds().height;
  67. int textW = text.getGlobalBounds().width;
  68. text.setPosition(m_coord - sf::Vector2f{textW/2, 4*textH/5});
  69.  
  70. switch(nodeType) {
  71. case Constants::StartNode:
  72. return startNodeShape();
  73. case Constants::AssignNode:
  74. return assignNodeShape();
  75. case Constants::ConditionalNode:
  76. return conditionalNodeShape();
  77. case Constants::StopNode:
  78. return stopNodeShape();
  79. case Constants::OutputNode:
  80. return outputNodeShape();
  81. default:
  82. return sf::ConvexShape(0);
  83. }
  84. }
  85.  
  86. sf::ConvexShape startNodeShape() {
  87. sf::ConvexShape convexShape;
  88. convexShape.setPointCount(4);
  89.  
  90. convexShape.setPoint(0, m_coord + sf::Vector2f{-width/2, -height/2});
  91. convexShape.setPoint(1, m_coord + sf::Vector2f{ width/2, -height/2});
  92. convexShape.setPoint(2, m_coord + sf::Vector2f{ width/2, height/2});
  93. convexShape.setPoint(3, m_coord + sf::Vector2f{-width/2, height/2});
  94.  
  95. return convexShape;
  96. }
  97.  
  98. sf::ConvexShape assignNodeShape() {
  99. sf::ConvexShape convexShape;
  100. convexShape.setPointCount(4);
  101.  
  102. convexShape.setPoint(0, m_coord + sf::Vector2f{-width/2, -height/2});
  103. convexShape.setPoint(1, m_coord + sf::Vector2f{ width/2, -height/2});
  104. convexShape.setPoint(2, m_coord + sf::Vector2f{ width/2, height/2});
  105. convexShape.setPoint(3, m_coord + sf::Vector2f{-width/2, height/2});
  106.  
  107. return convexShape;
  108. }
  109.  
  110. sf::ConvexShape conditionalNodeShape() {
  111. sf::ConvexShape convexShape;
  112. convexShape.setPointCount(4);
  113.  
  114. convexShape.setPoint(0, m_coord + sf::Vector2f{-width/2, -height/2});
  115. convexShape.setPoint(1, m_coord + sf::Vector2f{ width/2, -height/2});
  116. convexShape.setPoint(2, m_coord + sf::Vector2f{ width/2, height/2});
  117. convexShape.setPoint(3, m_coord + sf::Vector2f{-width/2, height/2});
  118.  
  119. return convexShape;
  120. }
  121.  
  122. sf::ConvexShape stopNodeShape() {
  123. sf::ConvexShape convexShape;
  124. convexShape.setPointCount(4);
  125.  
  126. convexShape.setPoint(0, m_coord + sf::Vector2f{-width/2, -height/2});
  127. convexShape.setPoint(1, m_coord + sf::Vector2f{ width/2, -height/2});
  128. convexShape.setPoint(2, m_coord + sf::Vector2f{ width/2, height/2});
  129. convexShape.setPoint(3, m_coord + sf::Vector2f{-width/2, height/2});
  130.  
  131. return convexShape;
  132. }
  133.  
  134. sf::ConvexShape outputNodeShape() {
  135. sf::ConvexShape convexShape;
  136. convexShape.setPointCount(4);
  137.  
  138. convexShape.setPoint(0, m_coord + sf::Vector2f{-width/2, -height/2});
  139. convexShape.setPoint(1, m_coord + sf::Vector2f{ width/2, -height/2});
  140. convexShape.setPoint(2, m_coord + sf::Vector2f{ width/2, height/2});
  141. convexShape.setPoint(3, m_coord + sf::Vector2f{-width/2, height/2});
  142.  
  143. return convexShape;
  144. }
  145.  
  146. private:
  147. sf::Vector2f m_coord = sf::Vector2f{-1, -1};
  148. sf::ConvexShape m_shape;
  149. bool m_shapeAssigned = false;
  150. int m_padding = 10;
  151. };
  152.  
Advertisement
RAW Paste Data Copied
Advertisement