Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.11 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////
  2. /////////       Main.cpp            //////////
  3. //////////////////////////////////////////////////////////////////
  4. #include <iostream>
  5. #include <stdlib.h>
  6. #include <SFML/Graphics.hpp>
  7. #include "GUI.h"
  8.  
  9. bool mainMenu = true;
  10.  
  11. int main()
  12. {            
  13.         sf::Clock clock;
  14.         sf::Time timeSinceLastUpdate(sf::Time::Zero);
  15.  
  16.         int i = 0;
  17.  
  18.         const sf::Time TimePerFrame(sf::seconds(1.f / 60.f));
  19.         sf::RenderWindow window(sf::VideoMode(1024, 768), "ENGINE"); // ініціалізація вікна
  20.  
  21.         GUI gui;
  22.         if(mainMenu) {
  23.             gui.CreateProgressBar(sf::Vector2f(0,100), sf::Vector2f(150,45), 150, 50, 0);
  24.             gui.CreateButtonList(sf::Vector2f(300,100), sf::Vector2f(300,300), 10,10,1);
  25.             //gui.buttonList[0]->button.setFillColor(sf::Color::Red);
  26.             //gui.buttonList[2]->button.setFillColor(sf::Color::Blue);
  27.             //gui.buttonList[15]->button.setFillColor(sf::Color::Yellow);
  28.         } // якщо ми в головному меню, створюємо кнопки
  29.  
  30.         window.clear(sf::Color::Black); // очищаємо вікно
  31.         window.display(); // показуємо кнопки
  32.         while(window.isOpen()) { // якщо вікно відкрито, то :
  33.  
  34.         sf::Time elapsedTime = clock.restart();
  35.         timeSinceLastUpdate += elapsedTime;
  36.         while(timeSinceLastUpdate > TimePerFrame) // ставимо фпс 60
  37.         {
  38.             timeSinceLastUpdate -= TimePerFrame;
  39.             gui.RenderGUI(window);
  40.         }
  41.         while(window.pollEvent(gui.event)) { // прописуємо всі івенти
  42.             gui.OnButtonGuidance(window, sf::Color(100,0,0), sf::Color(200,0,0));
  43.             gui.OnButtonClick(window);
  44.             //gui.OnButtonListMove(window);
  45.             /////////////////////////////////////////
  46.             //// РОБОТА З ГРАФІЧНИМ ІНТЕРФЕЙСОМ  ////
  47.             /////////////////////////////////////////
  48.         }
  49.    }
  50.         return EXIT_SUCCESS;
  51. }
  52.  
  53. //////////////////////////////////////////////////////////////////
  54. /////////       GUI.h               //////////
  55. //////////////////////////////////////////////////////////////////
  56. #include <SFML/Graphics.hpp>
  57. #include <memory>
  58.  
  59. class GUI {
  60. public:
  61.     GUI();
  62.     //////////////
  63.     /// КНОПКА ///
  64.     //////////////
  65.     void CreateButton(sf::Vector2f position, sf::Vector2f size); // Кнопка
  66.     void OnButtonGuidance(sf::RenderWindow &_window, sf::Color defColor, sf::Color debColor); // При націлюванні на кнопку
  67.     int OnButtonClick(sf::RenderWindow & _window); // При кліку на кнопку  
  68.  
  69.     typedef struct buttonSettings {
  70.     sf::RectangleShape btns;
  71.     };
  72.  
  73.     ///////////////////
  74.     /// ПРОГРЕС БАР ///
  75.     ///////////////////
  76.     void CreateProgressBar(sf::Vector2f position, sf::Vector2f size, float maxSize, float maxValue, float value); // Прогрес бар
  77.     void ProgressBarInsertValue(int index,float value);
  78.     float GetProgressBarValue (int index);
  79.  
  80.     typedef struct progressBarSettings {
  81.     float progressBarMaxValue;
  82.     float progressBarMaxSize;
  83.     sf::Vector2f progressBarSize;
  84.     sf::RectangleShape progressBar;
  85.     };
  86.  
  87.  
  88.    
  89.     ///////////////
  90.     /// Чекбокс ///
  91.     ///////////////
  92.     void CreateCheckBox(sf::Vector2f position, bool enabled); // Чекбокс
  93.     int OnCheckBoxClick(sf::RenderWindow & _window); // При кліку на чекбокс
  94.     void CheckBoxColorChange(int index);
  95.  
  96.     typedef struct checkBoxSettings {
  97.     sf::RectangleShape checkBox;
  98.     bool checkBoxesStates;
  99.     };
  100.  
  101.     //////////////////
  102.     /// БаттонЛист ///
  103.     //////////////////
  104.     void CreateButtonList(sf::Vector2f position, sf::Vector2f size, int xCount, int yCount, float spacing);
  105.     int OnButtonListClick(sf::RenderWindow & _window);
  106.     void OnButtonListMove(sf::RenderWindow & _window);
  107.  
  108.     typedef struct ButtonListSettings {
  109.     sf::RectangleShape button;
  110.     };
  111.  
  112.     /////////////////
  113.     /////////////////
  114.     /////////////////
  115.  
  116.     std::vector<std::unique_ptr<ButtonListSettings>> buttonList;
  117.     std::vector<std::unique_ptr<buttonSettings>> buttons;
  118.     std::vector<std::unique_ptr<progressBarSettings>> progressBars;
  119.     std::vector<std::unique_ptr<checkBoxSettings>> checkBoxes;
  120.  
  121.     sf::Event event;
  122.  
  123.     unsigned int btnListID;
  124.     unsigned int btnID;
  125.     unsigned int checkID;
  126.  
  127.     bool holding;
  128.     unsigned int holdingID;
  129.  
  130.     void RenderGUI(sf::RenderWindow & _window); // Рендер граф. інтерфейсу
  131.  
  132. private:
  133.     sf::RectangleShape mask;
  134.     sf::Color maskCol;
  135.     unsigned int i;
  136. };
  137.  
  138. //////////////////////////////////////////////////////////////////
  139. /////////       GUI.cpp             //////////
  140. //////////////////////////////////////////////////////////////////
  141. #include <iostream>
  142. #include <SFML\Graphics.hpp>
  143. #include "GUI.h"
  144.  
  145. GUI::GUI() {
  146.     btnID = 0;
  147.     checkID = 0;
  148.     btnListID = 0;
  149.     i = 0;
  150.     holding = false;
  151.     holdingID = 0;
  152. }
  153.  
  154. ///////////////////////////
  155. /// КНОПКА        ///
  156. ///////////////////////////
  157.  
  158. void GUI::CreateButton(sf::Vector2f position, sf::Vector2f size)
  159. {
  160.     std::unique_ptr<struct buttonSettings> rect (new struct buttonSettings()); // створюємо кнопку
  161.  
  162.     rect->btns.setPosition(position);
  163.  
  164.     rect->btns.setSize(size);
  165.  
  166.     buttons.push_back(std::move(rect));
  167. }
  168.  
  169. int GUI::OnButtonClick(sf::RenderWindow & _window) {
  170.     for(btnID = 0; btnID != buttons.size(); btnID++) {
  171.         sf::FloatRect bounds(buttons[btnID]->btns.getGlobalBounds());
  172.  
  173.         sf::Vector2f mousePosition(sf::Mouse::getPosition(_window));
  174.  
  175.         if(bounds.contains(mousePosition) && event.type == sf::Event::MouseButtonPressed) { // якщо в кнопці
  176.             if(event.mouseButton.button == sf::Mouse::Left) {
  177.                 return btnID;
  178.             }
  179.         }
  180.     }
  181. }
  182.  
  183. void GUI::OnButtonGuidance(sf::RenderWindow & _window, sf::Color defColor, sf::Color debColor) {
  184.     for(unsigned int a = 0; a != buttons.size(); a++) {
  185.             sf::FloatRect bounds(buttons[a]->btns.getGlobalBounds());
  186.  
  187.             sf::Vector2f mousePosition(sf::Mouse::getPosition(_window));
  188.  
  189.                 if(bounds.contains(mousePosition)) { // якщо курсор на кнопці
  190.                     buttons[a]->btns.setFillColor(debColor);
  191.                 }
  192.                 else {
  193.                     buttons[a]->btns.setFillColor(defColor);
  194.                 }
  195.     }
  196. }
  197.  
  198. ///////////////////////////////
  199. ///ПРОГРЕС БАР            ///
  200. ///////////////////////////////
  201.  
  202. void GUI::CreateProgressBar(sf::Vector2f position, sf::Vector2f size, float maxSize, float maxValue, float value){
  203.     std::unique_ptr<struct progressBarSettings> pbar (new struct progressBarSettings());
  204.  
  205.     pbar->progressBar.setPosition(position);
  206.  
  207.     pbar->progressBarMaxSize = maxSize;
  208.     pbar->progressBarMaxValue = maxValue;
  209.     pbar->progressBarSize.y = size.y;
  210.     pbar->progressBarSize.x = value;
  211.     pbar->progressBar.setSize(pbar->progressBarSize);
  212.  
  213.     progressBars.push_back(std::move(pbar));
  214. }
  215.  
  216. void GUI::ProgressBarInsertValue(int index,float value) {
  217.     float one_percent = (progressBars[index]->progressBarMaxSize / progressBars[index]->progressBarMaxValue);
  218.     if(progressBars[index]->progressBarSize.x < progressBars[index]->progressBarMaxSize) {
  219.         progressBars[index]->progressBarSize.x += (one_percent * value);
  220.     }
  221.     else {
  222.         if(value < 0)
  223.             if(progressBars[index]->progressBarSize.x > 0)
  224.                 progressBars[index]->progressBarSize.x += (one_percent * value);
  225.     }
  226. }
  227.  
  228. float GUI::GetProgressBarValue(int index) {
  229.     float one_percent = (progressBars[index]->progressBarMaxSize / progressBars[index]->progressBarMaxValue);
  230.     return progressBars[index]->progressBarSize.x / one_percent;
  231. }
  232.  
  233. ////////////////////////////
  234. ////Чекбокс       ///
  235. ////////////////////////////
  236. void GUI::CreateCheckBox(sf::Vector2f position,  bool enabled) {
  237.     std::unique_ptr<struct checkBoxSettings> checkBox (new struct checkBoxSettings());
  238.  
  239.     checkBox->checkBox.setPosition(position);
  240.     checkBox->checkBox.setSize(sf::Vector2f(25,25));
  241.  
  242.     checkBox->checkBoxesStates = enabled;
  243.  
  244.     if(enabled)
  245.         checkBox->checkBox.setFillColor(sf::Color(0,155,0));
  246.     else
  247.         checkBox->checkBox.setFillColor(sf::Color(155,0,0));
  248.  
  249.     checkBoxes.push_back(std::move(checkBox));
  250. }
  251.  
  252. int GUI::OnCheckBoxClick(sf::RenderWindow& _window) {
  253.     for(checkID = 0; checkID != buttons.size(); checkID++) {
  254.         sf::FloatRect bounds(checkBoxes[checkID]->checkBox.getGlobalBounds());
  255.  
  256.         sf::Vector2f mousePosition(sf::Mouse::getPosition(_window));
  257.  
  258.         if(bounds.contains(mousePosition) && event.type == sf::Event::MouseButtonPressed) { // якщо в кнопці
  259.             if(event.mouseButton.button == sf::Mouse::Left) {
  260.                 CheckBoxColorChange(checkID);
  261.                 return checkID;
  262.             }
  263.         }
  264.     }
  265. }
  266.  
  267. void GUI::CheckBoxColorChange(int index) {
  268.     if(checkBoxes[index]->checkBoxesStates == true)
  269.         checkBoxes[index]->checkBox.setFillColor(sf::Color(0,155,0));
  270.     else
  271.         checkBoxes[index]->checkBox.setFillColor(sf::Color(155,0,0));
  272. }
  273.  
  274. ///////////////////////////////////////////
  275. ///BUTTON LIST              ///
  276. ///////////////////////////////////////////
  277. void GUI::CreateButtonList(sf::Vector2f position, sf::Vector2f size, int xCount, int yCount, float spacing) {
  278.     for(int y = 1; y<yCount+1; y++) {  
  279.         for(int x = 1; x<xCount+1; x++) {
  280.             std::unique_ptr<struct ButtonListSettings> bls (new struct ButtonListSettings()); //here's the problem, I think
  281.                 bls->button.setSize(sf::Vector2f((size.x / (float)xCount)-spacing, (size.y / (float)yCount)-spacing));
  282.                 bls->button.setPosition(((x * size.x/xCount))+position.x, ((y * size.y/yCount))+position.y);
  283.                 bls->button.setFillColor(sf::Color::White);
  284.                 buttonList.push_back(std::move(bls));
  285.             }
  286.         }
  287. }
  288.  
  289. int GUI::OnButtonListClick(sf::RenderWindow & _window) {
  290.     for(btnListID = 0; btnListID != buttonList.size(); btnListID++) {
  291.         sf::FloatRect bounds(buttonList[btnListID]->button.getGlobalBounds());
  292.  
  293.         sf::Vector2f mousePosition(sf::Mouse::getPosition(_window));
  294.  
  295.         if(bounds.contains(mousePosition) && event.type == sf::Event::MouseButtonPressed) { // якщо в кнопці
  296.             if(event.mouseButton.button == sf::Mouse::Right) {
  297.                 return btnListID;
  298.             }
  299.         }
  300.     }
  301. }
  302.  
  303. void GUI::OnButtonListMove(sf::RenderWindow & _window) {//and, idk why, but in this function, there's a problem too
  304.     for(btnListID = 0; btnListID != buttonList.size(); btnListID++) {
  305.         sf::FloatRect bounds(buttonList[btnListID]->button.getGlobalBounds());
  306.         sf::Vector2f mousePosition(sf::Mouse::getPosition(_window));
  307.         if(event.type == sf::Event::MouseButtonPressed) { // якщо в кнопці
  308.             if(event.mouseButton.button == sf::Mouse::Left && bounds.contains(mousePosition)) {
  309.                 holdingID = btnListID;
  310.                 holding = true;
  311.             }
  312.         }
  313.         if(holding) {
  314.             mask.setPosition(mousePosition.x-10, mousePosition.y-10);
  315.             mask.setFillColor(buttonList[holdingID]->button.getFillColor());
  316.             mask.setSize(buttonList[btnListID]->button.getSize());
  317.         }
  318.         if(event.type == sf::Event::MouseButtonReleased) {
  319.             if(event.mouseButton.button == sf::Mouse::Left) {
  320.             if(bounds.contains(mousePosition) && holdingID != btnListID) {
  321.                 maskCol = sf::Color(buttonList[btnListID]->button.getFillColor());
  322.                 buttonList[btnListID]->button.setFillColor(mask.getFillColor());
  323.                 buttonList[holdingID]->button.setFillColor(maskCol);
  324.             }
  325.             if(holding) {
  326.                 holding = false;
  327.             }
  328.             mask.setSize(sf::Vector2f(0,0));
  329.             }
  330.         }
  331.     }
  332. }
  333. ///////////////////////////////////////////////////////////
  334. ///        РЕНДЕР ГРАФ.ІНТЕРФЕЙСУ       ///
  335. ///////////////////////////////////////////////////////////
  336.  
  337. void GUI::RenderGUI(sf::RenderWindow & _window) {
  338.     for(unsigned int b = 0; b<buttons.size(); b++) { // перемальовуємо кнопки
  339.         _window.draw(buttons[b]->btns);
  340.     }
  341.     for(unsigned int d = 0; d<buttonList.size(); d++) {
  342.         _window.draw(buttonList[d]->button);
  343.     }
  344.     for(unsigned int a = 0; a<progressBars.size(); a++) {
  345.         if(progressBars[a]->progressBarSize.x > progressBars[a]->progressBarMaxSize) // якщо розмір більший > max || < 0
  346.             progressBars[a]->progressBarSize.x = progressBars[a]->progressBarMaxSize; // змінюємо розмір до максимального
  347.         progressBars[a]->progressBar.setSize(progressBars[a]->progressBarSize); // ставимо розмір
  348.         _window.draw(progressBars[a]->progressBar); // промальовуємо
  349.     }
  350.     for(unsigned int c = 0; c<checkBoxes.size(); c++) {
  351.         _window.draw(checkBoxes[c]->checkBox);
  352.     }
  353.             _window.draw(mask);
  354.         _window.display();
  355.         _window.clear();
  356. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement