diff --git a/GUITest/GUIElements.hpp b/GUITest/GUIElements.hpp index e621d9e..2826826 100644 --- a/GUITest/GUIElements.hpp +++ b/GUITest/GUIElements.hpp @@ -1,59 +1,178 @@ #include "SynGame.hpp" -class SynButton -{ - private: - sf::Text textDisplay; - std::string text; - static sf::Font font; - int x; - int y; - int width; - int height; - void (*action)(int); - int *value; - public: - SynButton(std::string, int, int, int, int); - void Bind(void (*)(int), int *); - bool IsMouseOver(int, int); - void Click(); - ~SynButton(); -}; +sf::Font font; -SynButton::SynButton(std::string text, int x, int y, int width, int height) -{ - textDisplay.setFont(font); - textDisplay.setString(text); - this -> x = x; - this -> y = y; - this -> width = width; - this -> height = height; -} +#pragma region Text + class SynText + { + private: + int x; + int y; + int offsetX; + int offsetY; + bool isCentered; + sf::Text textDisplay; + public: + SynText(std::string = "", int = 0, int = 0, int = 0, int = 0, bool = false); + void SetText(std::string); + void SetOffset(int, int); + void SetPosition(int, int); + void SetCentered(bool); + sf::Text GetText(); + ~SynText(); + }; -void SynButton::Bind(void (*action)(int), int *value) -{ - this -> action = action; - this -> value = value; -} + SynText::SynText(std::string text, int x, int y, int offsetX, int offsetY, bool isCentered) + { + this -> isCentered = isCentered; + textDisplay.setFont(font); + SetText(text); + SetOffset(offsetX, offsetX); + SetPosition(x, y); + textDisplay.setColor(sf::Color::Red); + } -bool SynButton::IsMouseOver(int mouseX, int mouseY) -{ - bool isOver; - int halfWidth = width / 2; - int halfHeight = height / 2; - isOver = mouseX <= x + halfWidth && - mouseX >= x - halfWidth && - mouseY <= y + halfHeight && - mouseY >= y - halfHeight; - return isOver; -} + void SynText::SetText(std::string text) + { + textDisplay.setString(text); + SetPosition(x, y); + } -void SynButton::Click() -{ - if(value) - action(*value); -} + void SynText::SetPosition(int x, int y) + { + this -> x = x; + this -> y = y; + x += offsetX; + y += offsetY; -SynButton::~SynButton() -{ -} + if(isCentered) + textDisplay.setOrigin((int)textDisplay.getLocalBounds().width / 2, (int)textDisplay.getLocalBounds().height / 2); + + textDisplay.setPosition(x, y); + } + + void SynText::SetOffset(int offsetX, int offsetY) + { + this -> offsetX = offsetX; + this -> offsetY = offsetY; + SetPosition(x, y); + } + void SynText::SetCentered(bool isCentered) + { + this -> isCentered = isCentered; + } + + sf::Text SynText::GetText() + { + return textDisplay; + } + + SynText::~SynText() + { + } +#pragma endregion + +#pragma region Button + class SynButton + { + private: + SynText synText; + void (*action)(int); + int *value; + int x; + int y; + int width; + int height; + sf::Vertex vertices[4]; + void CalculateVertices(); + public: + SynButton(std::string = "", int = 0, int = 0, int = 0, int = 0); + void SetText(std::string); + void SetPosition(int, int); + void SetArea(int, int); + void Bind(void (*)(int), int *); + bool IsMouseOver(int, int); + void Click(); + sf::Text GetText(); + sf::Vertex *GetVertices(); + ~SynButton(); + }; + + void SynButton::CalculateVertices() + { + float newWidth = width / 2; + float newHeight = height / 2; + vertices[0].position = sf::Vector2f(x, y); + vertices[1].position = sf::Vector2f(x + newWidth, y); + vertices[2].position = sf::Vector2f(x + newWidth, y + newHeight); + vertices[3].position = sf::Vector2f(x, y + newHeight); + } + + SynButton::SynButton(std::string text, int x, int y, int width, int height) + { + synText.SetCentered(true); + synText.SetOffset(0, -5); + SetArea(width, height); + SetPosition(x, y); + CalculateVertices(); + } + + void SynButton::SetText(std::string text) + { + synText.SetText(text); + } + + void SynButton::SetPosition(int x, int y) + { + this -> x = x; + this -> y = y; + synText.SetPosition(x + width / 4, y + height / 4); + CalculateVertices(); + } + + void SynButton::SetArea(int width, int height) + { + this -> width = width; + this -> height = height; + synText.SetPosition(x + width / 4, y + height / 4); + CalculateVertices(); + } + + void SynButton::Bind(void (*action)(int), int *value) + { + this -> action = action; + this -> value = value; + } + + bool SynButton::IsMouseOver(int mouseX, int mouseY) + { + bool isOver; + int halfWidth = width / 2; + int halfHeight = height / 2; + isOver = mouseX <= x + halfWidth && + mouseX >= x && + mouseY <= y + halfHeight && + mouseY >= y; + return isOver; + } + + void SynButton::Click() + { + if(value) + action(*value); + else + action(y); + } + sf::Text SynButton::GetText() + { + return synText.GetText(); + } + sf::Vertex *SynButton::GetVertices() + { + return vertices; + } + + SynButton::~SynButton() + { + } +#pragma endregion diff --git a/GUITest/GUIWindow.hpp b/GUITest/GUIWindow.hpp index 80d516c..a2fa4ff 100644 --- a/GUITest/GUIWindow.hpp +++ b/GUITest/GUIWindow.hpp @@ -1,27 +1,106 @@ #include "SynGame.hpp" +#include class GUIWindow : public Window { private: + SynButton *buttons; + SynText output; + void BinaryButton(); + void QuaternaryButton(); + void OctalButton(); + void HexadecimalButton(); public: GUIWindow(unsigned int = 960, unsigned int = 540, std::string = "Window", sf::Uint32 = sf::Style::Titlebar | sf::Style::Close); void Update(); + void ButtonCheck(sf::Vector2i); ~GUIWindow(); }; +void GUIWindow::BinaryButton() +{ + output.SetText("Output: Binary"); +} + +void GUIWindow::QuaternaryButton() +{ + output.SetText("Output: Quaternary"); +} + +void GUIWindow::OctalButton() +{ + output.SetText("Output: Octal"); +} + +void GUIWindow::HexadecimalButton() +{ + output.SetText("Output: Hexadecimal"); +} + GUIWindow::GUIWindow(unsigned int width, unsigned int height, std::string title, sf::Uint32 style) : Window(width, height, title, style) { + output.SetText("Output: "); + output.SetCentered(true); + output.SetPosition(window.getSize().x / 2, window.getSize().y / 2); + output.SetOffset(0, 0); + buttons = new SynButton[4]; + (buttons + 0) -> SetText("Binary"); + (buttons + 1) -> SetText("Quaternary"); + (buttons + 2) -> SetText("Octal"); + (buttons + 3) -> SetText("Hexadecimal"); + + (buttons + 0) -> SetArea(400, 100); (buttons + 0) -> SetPosition(650, 100); + (buttons + 1) -> SetArea(400, 100); (buttons + 1) -> SetPosition(650, 200); + (buttons + 2) -> SetArea(400, 100); (buttons + 2) -> SetPosition(650, 300); + (buttons + 3) -> SetArea(400, 100); (buttons + 3) -> SetPosition(650, 400); + + (buttons + 0) -> Bind((void (*)(int))&BinaryButton, NULL); + (buttons + 1) -> Bind((void (*)(int))&QuaternaryButton, NULL); + (buttons + 2) -> Bind((void (*)(int))&OctalButton, NULL); + (buttons + 3) -> Bind((void (*)(int))&HexadecimalButton, NULL); } void GUIWindow::Update() { + + int buttonCounter; + + Window::Update(); + if(!isFocused) return; - Window::Update(); + window.clear(sf::Color::Black); + + for (buttonCounter = 0; buttonCounter < 4; buttonCounter++) + { + window.draw((buttons + buttonCounter) -> GetVertices(), 4, sf::PrimitiveType::Quads); + window.draw((buttons + buttonCounter) -> GetText()); + } + + window.draw(output.GetText()); + window.display(); +} + + +void GUIWindow::ButtonCheck(sf::Vector2i mousePos) +{ + int i; + SynButton *current; + for (i = 0; i < 4; i++) + { + current = buttons + i; + if(current -> IsMouseOver(mousePos.x, mousePos.y)) + { + (buttons + i) -> Click(); + break; + } + } + } GUIWindow::~GUIWindow() { + if(buttons) delete buttons; } diff --git a/GUITest/SynGame.hpp b/GUITest/SynGame.hpp index f52362b..031a3e1 100644 --- a/GUITest/SynGame.hpp +++ b/GUITest/SynGame.hpp @@ -8,7 +8,7 @@ #include #include #include + #include "GUIElements.hpp" #include "Window.hpp" #include "GUIWindow.hpp" - #include "GUIElements.hpp" #endif diff --git a/GUITest/Window.hpp b/GUITest/Window.hpp index 3ac1b79..3784819 100644 --- a/GUITest/Window.hpp +++ b/GUITest/Window.hpp @@ -62,6 +62,7 @@ bool isFocused; bool fullscreen; virtual void Update(); + virtual void ButtonCheck(sf::Vector2i); public: Window(unsigned int = 960, unsigned int = 540, std::string = "Window", sf::Uint32 = sf::Style::Titlebar | sf::Style::Close); virtual void CreateWindow(); @@ -82,6 +83,8 @@ isFocused = false; else if (event.type == sf::Event::GainedFocus) isFocused = true; + else if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) + ButtonCheck(sf::Mouse::getPosition(window)); if(sf::Keyboard::isKeyPressed(sf::Keyboard::F)) { fullscreen = !fullscreen; @@ -96,6 +99,11 @@ timer.UpdateTime(); } + void Window::ButtonCheck(sf::Vector2i mousePos) + { + + } + Window::Window(unsigned int width, unsigned int height, std::string title, sf::Uint32 style) { this -> size.x = width; @@ -130,7 +138,7 @@ size.y = videoMode.height; window.create(videoMode, title, sf::Style::Fullscreen); } - + window.setVerticalSyncEnabled(true); timer.ResetTimer(); } diff --git a/GUITest/main.cpp b/GUITest/main.cpp index af1d295..e2fbd5c 100644 --- a/GUITest/main.cpp +++ b/GUITest/main.cpp @@ -2,6 +2,7 @@ int main() { + font.loadFromFile("../OpenSans-Bold.ttf"); GUIWindow window(960, 540, "GUI Test"); while (window.IsOpen())