Line data Source code
1 : #include <client.hpp>
2 : #include <iostream>
3 : #include <fstream>
4 :
5 : #ifndef RESOURCES_PATH
6 : #define RESOURCES_PATH "../resources"
7 : #endif
8 :
9 : const sf::Color BACKGROUND_COLOR = sf::Color(0, 0, 0, 150);
10 : const sf::Color NUMBER_BUTTON_COLOR = sf::Color(247, 200, 195, 130);
11 :
12 : using namespace client;
13 :
14 0 : void setImage(std::unique_ptr<TextureDisplayer> &texture, std::string path, sf::Vector2f position, sf::Vector2f scale)
15 : {
16 0 : texture = std::make_unique<TextureDisplayer>(RESOURCES_PATH + path);
17 0 : texture->addSprite();
18 0 : texture->getSprite().setPosition(position);
19 0 : texture->getSprite().setScale(scale);
20 0 : }
21 :
22 0 : void setText(std::unique_ptr<sf::Text> &text, sf::Font &font, int size)
23 : {
24 0 : text = std::make_unique<sf::Text>("", font, size);
25 0 : text->setFillColor(sf::Color::Black);
26 0 : }
27 :
28 :
29 : /*!
30 : * @brief Function to set the window to choose the number of cases to use
31 : */
32 0 : void PopUpWindow::setUpValidateBoxesWindow()
33 : {
34 :
35 0 : if (!font.loadFromFile(RESOURCES_PATH "/font/Calibri.ttf"))
36 : {
37 0 : std::cerr << "Font not loaded" << std::endl;
38 : }
39 :
40 0 : setImage(littleBackground,
41 : "/pop-up/little-background.png",
42 0 : sf::Vector2f(data["little-background-pos-x"].asFloat(), data["little-background-pos-y"].asFloat()),
43 0 : sf::Vector2f(data["little-background-scale-x"].asFloat(), 1));
44 :
45 0 : std::string questionString = "You have X boxes, \nHow many boxes do you want to play?";
46 0 : title = std::make_unique<sf::Text>(questionString, font, data["text-size"].asInt());
47 0 : title->setFillColor(sf::Color::Black);
48 0 : title->setPosition(
49 0 : windowLength / 2 - title->getGlobalBounds().width / 2,
50 0 : littleBackground->getSprite().getPosition().y + data["text-offset-y"].asInt());
51 :
52 0 : chooseNumberOfBoxesButton = std::make_unique<Button>(
53 0 : sf::Vector2f(data["boxes-button-size"].asInt(), data["boxes-button-size"].asInt()),
54 0 : sf::Vector2f(data["boxes-button-pos-x"].asInt(), data["boxes-button-pos-y"].asInt()),
55 : NUMBER_BUTTON_COLOR,
56 0 : false);
57 0 : chooseNumberOfBoxesButton->setText(data["boxes-button-text-size"].asInt(), sf::Vector2f(0, 0), "X", font);
58 :
59 0 : setImage(arrowLessTexture,
60 : "/pop-up/arrow-less.png",
61 0 : sf::Vector2f(data["arrow-less-pos-x"].asFloat(), data["arrow-less-pos-y"].asFloat()),
62 : sf::Vector2f(1, 1));
63 :
64 0 : setImage(arrowMoreTexture,
65 : "/pop-up/arrow-more.png",
66 0 : sf::Vector2f(data["arrow-more-pos-x"].asFloat(), data["arrow-more-pos-y"].asFloat()),
67 : sf::Vector2f(1, 1));
68 :
69 0 : setImage(doneTexture,
70 : "/pop-up/done.png",
71 0 : sf::Vector2f(data["done-pos-x"].asFloat(), data["done-pos-y"].asFloat()),
72 : sf::Vector2f(1, 1));
73 0 : }
74 :
75 : /*!
76 : * @brief Constructor
77 : * Constructor of PopUpWindow class
78 : * @param windowLength length of the window
79 : * @param windowWidth width of the window
80 : * @param data data of the json file (parameters of the buttons)
81 : * @param isActive true: the window is the validate boxes window, false: the window is the winner window
82 : */
83 0 : PopUpWindow::PopUpWindow(int windowLength, int windowWidth, const Json::Value &data, bool isActive)
84 : {
85 0 : this->data = data;
86 0 : this->windowLength = windowLength;
87 0 : this->windowWidth = windowWidth;
88 :
89 0 : blackBackground = std::make_unique<Button>(sf::Vector2f(windowLength, windowWidth), sf::Vector2f(0, 0), BACKGROUND_COLOR, false);
90 :
91 0 : if (isActive)
92 : {
93 0 : setUpValidateBoxesWindow();
94 : }
95 :
96 : else
97 : {
98 0 : if (!font.loadFromFile(RESOURCES_PATH "/font/MorrisRomanBlack.otf"))
99 : {
100 0 : std::cerr << "Font not loaded" << std::endl;
101 : }
102 :
103 0 : setImage(littleBackground,
104 : "/pop-up/little-background.png",
105 0 : sf::Vector2f(data["winner-little-background-pos-x"].asFloat(), data["winner-little-background-pos-y"].asFloat()),
106 0 : sf::Vector2f(data["winner-scale"].asFloat(), data["winner-scale"].asFloat()));
107 :
108 0 : setText(title, font, data["winner-text-size"].asInt());
109 :
110 0 : setText(body, font, data["text-size"].asInt());
111 :
112 0 : centerText();
113 : }
114 0 : }
115 :
116 : /*!
117 : * @brief Function to draw the common elements of the 2 windows (validateBoxes and Winner)
118 : */
119 0 : void drawGeneral(std::shared_ptr<sf::RenderWindow> window, std::shared_ptr<sf::RectangleShape> &blackBackgroundButton, sf::Sprite littleBackground, std::unique_ptr<sf::Text> &title)
120 : {
121 0 : window->draw(*blackBackgroundButton);
122 0 : window->draw(littleBackground);
123 0 : window->draw(*title);
124 0 : }
125 :
126 : /*!
127 : * @brief Function to draw the elements of the winner window
128 : */
129 0 : void PopUpWindow::drawWinnerWindow(std::shared_ptr<sf::RenderWindow> window)
130 : {
131 0 : drawGeneral(window, blackBackground->buttonRect, littleBackground->getSprite(), title);
132 0 : window->draw(*body);
133 0 : }
134 :
135 : /*!
136 : * @brief Function to draw the elements of the validate boxes window
137 : */
138 0 : void PopUpWindow::drawValidateBoxesButtons(std::shared_ptr<sf::RenderWindow> window)
139 : {
140 0 : drawGeneral(window, blackBackground->buttonRect, littleBackground->getSprite(), title);
141 0 : window->draw(*chooseNumberOfBoxesButton->buttonRect);
142 0 : window->draw(*chooseNumberOfBoxesButton->buttonText);
143 0 : window->draw(arrowLessTexture->getSprite());
144 0 : window->draw(arrowMoreTexture->getSprite());
145 0 : window->draw(doneTexture->getSprite());
146 0 : }
147 :
148 : /*!
149 : * @brief Function to center the text on the winner window
150 : */
151 0 : void PopUpWindow::centerText()
152 : {
153 0 : body->setPosition(
154 0 : windowLength / 2 - body->getGlobalBounds().width / 2,
155 0 : littleBackground->getSprite().getPosition().y + data["winner-body-offset-y"].asInt());
156 :
157 0 : title->setPosition(
158 0 : windowLength / 2 - title->getGlobalBounds().width / 2,
159 0 : littleBackground->getSprite().getPosition().y + data["winner-text-offset-y"].asInt());
160 0 : }
|