Megaddd

Untitled

Nov 16th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. class Button_Square
  2. {
  3. private:
  4. sf::RectangleShape button_box;
  5. sf::Text button_text;
  6. sf::Font font;
  7. sf::FloatRect bounds;
  8. bool toggleable;
  9. string eventID;
  10. int state; //-1: disabled (greyed-out), 0: nominal, 1: moused-over, 2: held (pressed), 3: activated ('toggled on')
  11. public:
  12. void Draw(sf::RenderWindow* window)
  13. {
  14. window->draw(button_box);
  15. window->draw(button_text);
  16. }
  17. Button_Square(string text, int x, int y, int xs, int ys, string eventID)
  18. {
  19. this->eventID = eventID;
  20. button_box.setSize(sf::Vector2f(xs, ys));
  21. button_box.setPosition(sf::Vector2f(x, y));
  22. button_box.setFillColor(sf::Color(0, 0, 0, 50));
  23. button_box.setOutlineColor(sf::Color(0, 0, 0, 255));
  24. button_box.setOutlineThickness(4);
  25. if (!font.loadFromFile("Starcraft.ttf")) { throw "Could not find font 'Starcraft.ttf'."; }
  26. button_text.setCharacterSize(y - 10);
  27. button_text.setFillColor(sf::Color::Black);
  28. button_text.setString(text);
  29. button_text.setPosition(sf::Vector2f(x, y));
  30. this->state = 0;
  31. this->toggleable = false;
  32. this->bounds = button_box.getGlobalBounds;
  33. }
  34. Button_Square(string text, int x, int y, int xs, int ys, string eventID, bool toggleable)
  35. {
  36. this->eventID = eventID;
  37. button_box.setSize(sf::Vector2f(xs, ys));
  38. button_box.setPosition(sf::Vector2f(x, y));
  39. button_box.setFillColor(sf::Color(26, 26, 26, 26));
  40. button_box.setOutlineColor(sf::Color(26, 26, 26, 255));
  41. button_box.setOutlineThickness(4);
  42. if (!font.loadFromFile("Starcraft.ttf")) { throw "Could not find font 'Starcraft.ttf'."; }
  43. button_text.setCharacterSize(y - 10);
  44. button_text.setFillColor(sf::Color(26, 26, 26, 255));
  45. button_text.setString(text);
  46. button_text.setPosition(sf::Vector2f(x, y));
  47. this->state = 0;
  48. this->toggleable = toggleable;
  49. this->bounds = button_box.getGlobalBounds;
  50. }
  51. void shape_moused()
  52. {
  53. button_box.setOutlineThickness(5);
  54. button_box.setOutlineColor(sf::Color(0, 0, 0, 255));
  55. button_box.setFillColor(sf::Color(0, 0, 0, 55));
  56. button_text.setFillColor(sf::Color(0, 0, 0, 255));
  57. }
  58. void shape_default()
  59. {
  60. button_box.setOutlineThickness(4);
  61. button_box.setFillColor(sf::Color(26, 26, 26, 26));
  62. button_box.setOutlineColor(sf::Color(26, 26, 26, 255));
  63. button_text.setFillColor(sf::Color(26, 26, 26, 255));
  64. }
  65. void shape_pressed()
  66. {
  67. button_box.setOutlineThickness(5);
  68. button_box.setOutlineColor(sf::Color(60, 216, 13, 255));
  69. button_box.setFillColor(sf::Color(102, 204, 0, 55));
  70. button_text.setColor(sf::Color(20, 40, 5, 255));
  71. }
  72. void shape_disabled()
  73. {
  74. button_box.setOutlineThickness(3);
  75. button_box.setFillColor(sf::Color(90, 90, 90, 90));
  76. button_box.setOutlineColor(sf::Color(90, 90, 90, 255));
  77. button_text.setFillColor(sf::Color(90, 90, 90, 255));
  78. }
  79. void shape_activated()
  80. {
  81. button_box.setOutlineThickness(4);
  82. button_box.setOutlineColor(sf::Color(255, 101, 0, 255));
  83. button_box.setFillColor(sf::Color(255, 101, 0, 55));
  84. button_text.setColor(sf::Color(60, 30, 0, 255));
  85. }
  86. void mouseMoved(sf::Event* mouseEvent)
  87. {
  88. if (state == 0 && bounds.contains(sf::Vector2f(mouseEvent->mouseMove.x, mouseEvent->mouseMove.y)))
  89. {
  90. state = 1;
  91. this->shape_moused();
  92. }
  93. else if ((state == 1 || state == 2) && !bounds.contains(sf::Vector2f(mouseEvent->mouseMove.x, mouseEvent->mouseMove.y)))
  94. {
  95. state = 0;
  96. this->shape_default();
  97. }
  98. }
  99. void mousePressed(sf::Event* mouseEvent)
  100. {
  101. if ((state == 1 || state == 0) && bounds.contains(sf::Vector2f(mouseEvent->mouseMove.x, mouseEvent->mouseMove.y)))
  102. {
  103. state = 2;
  104. this->shape_pressed();
  105. }
  106. }
  107. string mouseReleased(sf::Event* mouseEvent)
  108. {
  109. if ((state == 2) && bounds.contains(sf::Vector2f(mouseEvent->mouseMove.x, mouseEvent->mouseMove.y)))
  110. {
  111. if (toggleable)
  112. {
  113. state = 3;
  114. this->shape_activated();
  115. }
  116. else
  117. {
  118. state = 1;
  119. this->shape_moused();
  120. }
  121. return eventID;
  122. }
  123. return "";
  124. }
  125. void disable()
  126. {
  127. this->state = -1;
  128. this->shape_disabled();
  129. }
  130. void enable()
  131. {
  132. this->state = 0;
  133. this->shape_default();
  134. }
  135. };
Advertisement
Add Comment
Please, Sign In to add comment