Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////
- ///////// Main.cpp //////////
- //////////////////////////////////////////////////////////////////
- #include <iostream>
- #include <stdlib.h>
- #include <SFML/Graphics.hpp>
- #include "GUI.h"
- bool mainMenu = true;
- int main()
- {
- sf::Clock clock;
- sf::Time timeSinceLastUpdate(sf::Time::Zero);
- int i = 0;
- const sf::Time TimePerFrame(sf::seconds(1.f / 60.f));
- sf::RenderWindow window(sf::VideoMode(1024, 768), "ENGINE"); // ініціалізація вікна
- GUI gui;
- if(mainMenu) {
- gui.CreateProgressBar(sf::Vector2f(0,100), sf::Vector2f(150,45), 150, 50, 0);
- gui.CreateButtonList(sf::Vector2f(300,100), sf::Vector2f(300,300), 10,10,1);
- //gui.buttonList[0]->button.setFillColor(sf::Color::Red);
- //gui.buttonList[2]->button.setFillColor(sf::Color::Blue);
- //gui.buttonList[15]->button.setFillColor(sf::Color::Yellow);
- } // якщо ми в головному меню, створюємо кнопки
- window.clear(sf::Color::Black); // очищаємо вікно
- window.display(); // показуємо кнопки
- while(window.isOpen()) { // якщо вікно відкрито, то :
- sf::Time elapsedTime = clock.restart();
- timeSinceLastUpdate += elapsedTime;
- while(timeSinceLastUpdate > TimePerFrame) // ставимо фпс 60
- {
- timeSinceLastUpdate -= TimePerFrame;
- gui.RenderGUI(window);
- }
- while(window.pollEvent(gui.event)) { // прописуємо всі івенти
- gui.OnButtonGuidance(window, sf::Color(100,0,0), sf::Color(200,0,0));
- gui.OnButtonClick(window);
- //gui.OnButtonListMove(window);
- /////////////////////////////////////////
- //// РОБОТА З ГРАФІЧНИМ ІНТЕРФЕЙСОМ ////
- /////////////////////////////////////////
- }
- }
- return EXIT_SUCCESS;
- }
- //////////////////////////////////////////////////////////////////
- ///////// GUI.h //////////
- //////////////////////////////////////////////////////////////////
- #include <SFML/Graphics.hpp>
- #include <memory>
- class GUI {
- public:
- GUI();
- //////////////
- /// КНОПКА ///
- //////////////
- void CreateButton(sf::Vector2f position, sf::Vector2f size); // Кнопка
- void OnButtonGuidance(sf::RenderWindow &_window, sf::Color defColor, sf::Color debColor); // При націлюванні на кнопку
- int OnButtonClick(sf::RenderWindow & _window); // При кліку на кнопку
- typedef struct buttonSettings {
- sf::RectangleShape btns;
- };
- ///////////////////
- /// ПРОГРЕС БАР ///
- ///////////////////
- void CreateProgressBar(sf::Vector2f position, sf::Vector2f size, float maxSize, float maxValue, float value); // Прогрес бар
- void ProgressBarInsertValue(int index,float value);
- float GetProgressBarValue (int index);
- typedef struct progressBarSettings {
- float progressBarMaxValue;
- float progressBarMaxSize;
- sf::Vector2f progressBarSize;
- sf::RectangleShape progressBar;
- };
- ///////////////
- /// Чекбокс ///
- ///////////////
- void CreateCheckBox(sf::Vector2f position, bool enabled); // Чекбокс
- int OnCheckBoxClick(sf::RenderWindow & _window); // При кліку на чекбокс
- void CheckBoxColorChange(int index);
- typedef struct checkBoxSettings {
- sf::RectangleShape checkBox;
- bool checkBoxesStates;
- };
- //////////////////
- /// БаттонЛист ///
- //////////////////
- void CreateButtonList(sf::Vector2f position, sf::Vector2f size, int xCount, int yCount, float spacing);
- int OnButtonListClick(sf::RenderWindow & _window);
- void OnButtonListMove(sf::RenderWindow & _window);
- typedef struct ButtonListSettings {
- sf::RectangleShape button;
- };
- /////////////////
- /////////////////
- /////////////////
- std::vector<std::unique_ptr<ButtonListSettings>> buttonList;
- std::vector<std::unique_ptr<buttonSettings>> buttons;
- std::vector<std::unique_ptr<progressBarSettings>> progressBars;
- std::vector<std::unique_ptr<checkBoxSettings>> checkBoxes;
- sf::Event event;
- unsigned int btnListID;
- unsigned int btnID;
- unsigned int checkID;
- bool holding;
- unsigned int holdingID;
- void RenderGUI(sf::RenderWindow & _window); // Рендер граф. інтерфейсу
- private:
- sf::RectangleShape mask;
- sf::Color maskCol;
- unsigned int i;
- };
- //////////////////////////////////////////////////////////////////
- ///////// GUI.cpp //////////
- //////////////////////////////////////////////////////////////////
- #include <iostream>
- #include <SFML\Graphics.hpp>
- #include "GUI.h"
- GUI::GUI() {
- btnID = 0;
- checkID = 0;
- btnListID = 0;
- i = 0;
- holding = false;
- holdingID = 0;
- }
- ///////////////////////////
- /// КНОПКА ///
- ///////////////////////////
- void GUI::CreateButton(sf::Vector2f position, sf::Vector2f size)
- {
- std::unique_ptr<struct buttonSettings> rect (new struct buttonSettings()); // створюємо кнопку
- rect->btns.setPosition(position);
- rect->btns.setSize(size);
- buttons.push_back(std::move(rect));
- }
- int GUI::OnButtonClick(sf::RenderWindow & _window) {
- for(btnID = 0; btnID != buttons.size(); btnID++) {
- sf::FloatRect bounds(buttons[btnID]->btns.getGlobalBounds());
- sf::Vector2f mousePosition(sf::Mouse::getPosition(_window));
- if(bounds.contains(mousePosition) && event.type == sf::Event::MouseButtonPressed) { // якщо в кнопці
- if(event.mouseButton.button == sf::Mouse::Left) {
- return btnID;
- }
- }
- }
- }
- void GUI::OnButtonGuidance(sf::RenderWindow & _window, sf::Color defColor, sf::Color debColor) {
- for(unsigned int a = 0; a != buttons.size(); a++) {
- sf::FloatRect bounds(buttons[a]->btns.getGlobalBounds());
- sf::Vector2f mousePosition(sf::Mouse::getPosition(_window));
- if(bounds.contains(mousePosition)) { // якщо курсор на кнопці
- buttons[a]->btns.setFillColor(debColor);
- }
- else {
- buttons[a]->btns.setFillColor(defColor);
- }
- }
- }
- ///////////////////////////////
- ///ПРОГРЕС БАР ///
- ///////////////////////////////
- void GUI::CreateProgressBar(sf::Vector2f position, sf::Vector2f size, float maxSize, float maxValue, float value){
- std::unique_ptr<struct progressBarSettings> pbar (new struct progressBarSettings());
- pbar->progressBar.setPosition(position);
- pbar->progressBarMaxSize = maxSize;
- pbar->progressBarMaxValue = maxValue;
- pbar->progressBarSize.y = size.y;
- pbar->progressBarSize.x = value;
- pbar->progressBar.setSize(pbar->progressBarSize);
- progressBars.push_back(std::move(pbar));
- }
- void GUI::ProgressBarInsertValue(int index,float value) {
- float one_percent = (progressBars[index]->progressBarMaxSize / progressBars[index]->progressBarMaxValue);
- if(progressBars[index]->progressBarSize.x < progressBars[index]->progressBarMaxSize) {
- progressBars[index]->progressBarSize.x += (one_percent * value);
- }
- else {
- if(value < 0)
- if(progressBars[index]->progressBarSize.x > 0)
- progressBars[index]->progressBarSize.x += (one_percent * value);
- }
- }
- float GUI::GetProgressBarValue(int index) {
- float one_percent = (progressBars[index]->progressBarMaxSize / progressBars[index]->progressBarMaxValue);
- return progressBars[index]->progressBarSize.x / one_percent;
- }
- ////////////////////////////
- ////Чекбокс ///
- ////////////////////////////
- void GUI::CreateCheckBox(sf::Vector2f position, bool enabled) {
- std::unique_ptr<struct checkBoxSettings> checkBox (new struct checkBoxSettings());
- checkBox->checkBox.setPosition(position);
- checkBox->checkBox.setSize(sf::Vector2f(25,25));
- checkBox->checkBoxesStates = enabled;
- if(enabled)
- checkBox->checkBox.setFillColor(sf::Color(0,155,0));
- else
- checkBox->checkBox.setFillColor(sf::Color(155,0,0));
- checkBoxes.push_back(std::move(checkBox));
- }
- int GUI::OnCheckBoxClick(sf::RenderWindow& _window) {
- for(checkID = 0; checkID != buttons.size(); checkID++) {
- sf::FloatRect bounds(checkBoxes[checkID]->checkBox.getGlobalBounds());
- sf::Vector2f mousePosition(sf::Mouse::getPosition(_window));
- if(bounds.contains(mousePosition) && event.type == sf::Event::MouseButtonPressed) { // якщо в кнопці
- if(event.mouseButton.button == sf::Mouse::Left) {
- CheckBoxColorChange(checkID);
- return checkID;
- }
- }
- }
- }
- void GUI::CheckBoxColorChange(int index) {
- if(checkBoxes[index]->checkBoxesStates == true)
- checkBoxes[index]->checkBox.setFillColor(sf::Color(0,155,0));
- else
- checkBoxes[index]->checkBox.setFillColor(sf::Color(155,0,0));
- }
- ///////////////////////////////////////////
- ///BUTTON LIST ///
- ///////////////////////////////////////////
- void GUI::CreateButtonList(sf::Vector2f position, sf::Vector2f size, int xCount, int yCount, float spacing) {
- for(int y = 1; y<yCount+1; y++) {
- for(int x = 1; x<xCount+1; x++) {
- std::unique_ptr<struct ButtonListSettings> bls (new struct ButtonListSettings()); //here's the problem, I think
- bls->button.setSize(sf::Vector2f((size.x / (float)xCount)-spacing, (size.y / (float)yCount)-spacing));
- bls->button.setPosition(((x * size.x/xCount))+position.x, ((y * size.y/yCount))+position.y);
- bls->button.setFillColor(sf::Color::White);
- buttonList.push_back(std::move(bls));
- }
- }
- }
- int GUI::OnButtonListClick(sf::RenderWindow & _window) {
- for(btnListID = 0; btnListID != buttonList.size(); btnListID++) {
- sf::FloatRect bounds(buttonList[btnListID]->button.getGlobalBounds());
- sf::Vector2f mousePosition(sf::Mouse::getPosition(_window));
- if(bounds.contains(mousePosition) && event.type == sf::Event::MouseButtonPressed) { // якщо в кнопці
- if(event.mouseButton.button == sf::Mouse::Right) {
- return btnListID;
- }
- }
- }
- }
- void GUI::OnButtonListMove(sf::RenderWindow & _window) {//and, idk why, but in this function, there's a problem too
- for(btnListID = 0; btnListID != buttonList.size(); btnListID++) {
- sf::FloatRect bounds(buttonList[btnListID]->button.getGlobalBounds());
- sf::Vector2f mousePosition(sf::Mouse::getPosition(_window));
- if(event.type == sf::Event::MouseButtonPressed) { // якщо в кнопці
- if(event.mouseButton.button == sf::Mouse::Left && bounds.contains(mousePosition)) {
- holdingID = btnListID;
- holding = true;
- }
- }
- if(holding) {
- mask.setPosition(mousePosition.x-10, mousePosition.y-10);
- mask.setFillColor(buttonList[holdingID]->button.getFillColor());
- mask.setSize(buttonList[btnListID]->button.getSize());
- }
- if(event.type == sf::Event::MouseButtonReleased) {
- if(event.mouseButton.button == sf::Mouse::Left) {
- if(bounds.contains(mousePosition) && holdingID != btnListID) {
- maskCol = sf::Color(buttonList[btnListID]->button.getFillColor());
- buttonList[btnListID]->button.setFillColor(mask.getFillColor());
- buttonList[holdingID]->button.setFillColor(maskCol);
- }
- if(holding) {
- holding = false;
- }
- mask.setSize(sf::Vector2f(0,0));
- }
- }
- }
- }
- ///////////////////////////////////////////////////////////
- /// РЕНДЕР ГРАФ.ІНТЕРФЕЙСУ ///
- ///////////////////////////////////////////////////////////
- void GUI::RenderGUI(sf::RenderWindow & _window) {
- for(unsigned int b = 0; b<buttons.size(); b++) { // перемальовуємо кнопки
- _window.draw(buttons[b]->btns);
- }
- for(unsigned int d = 0; d<buttonList.size(); d++) {
- _window.draw(buttonList[d]->button);
- }
- for(unsigned int a = 0; a<progressBars.size(); a++) {
- if(progressBars[a]->progressBarSize.x > progressBars[a]->progressBarMaxSize) // якщо розмір більший > max || < 0
- progressBars[a]->progressBarSize.x = progressBars[a]->progressBarMaxSize; // змінюємо розмір до максимального
- progressBars[a]->progressBar.setSize(progressBars[a]->progressBarSize); // ставимо розмір
- _window.draw(progressBars[a]->progressBar); // промальовуємо
- }
- for(unsigned int c = 0; c<checkBoxes.size(); c++) {
- _window.draw(checkBoxes[c]->checkBox);
- }
- _window.draw(mask);
- _window.display();
- _window.clear();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement