This commit is contained in:
Asrın Doğan 2020-03-01 20:03:43 +03:00
parent 9c4dfcf5ec
commit a103be1145
5 changed files with 262 additions and 55 deletions

View File

@ -1,59 +1,178 @@
#include "SynGame.hpp" #include "SynGame.hpp"
class SynButton sf::Font font;
{
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();
};
SynButton::SynButton(std::string text, int x, int y, int width, int height) #pragma region Text
{ class SynText
textDisplay.setFont(font); {
textDisplay.setString(text); private:
this -> x = x; int x;
this -> y = y; int y;
this -> width = width; int offsetX;
this -> height = height; 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) SynText::SynText(std::string text, int x, int y, int offsetX, int offsetY, bool isCentered)
{ {
this -> action = action; this -> isCentered = isCentered;
this -> value = value; textDisplay.setFont(font);
} SetText(text);
SetOffset(offsetX, offsetX);
SetPosition(x, y);
textDisplay.setColor(sf::Color::Red);
}
bool SynButton::IsMouseOver(int mouseX, int mouseY) void SynText::SetText(std::string text)
{ {
bool isOver; textDisplay.setString(text);
int halfWidth = width / 2; SetPosition(x, y);
int halfHeight = height / 2; }
isOver = mouseX <= x + halfWidth &&
mouseX >= x - halfWidth &&
mouseY <= y + halfHeight &&
mouseY >= y - halfHeight;
return isOver;
}
void SynButton::Click() void SynText::SetPosition(int x, int y)
{ {
if(value) this -> x = x;
action(*value); 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

View File

@ -1,27 +1,106 @@
#include "SynGame.hpp" #include "SynGame.hpp"
#include <iostream>
class GUIWindow : public Window class GUIWindow : public Window
{ {
private: private:
SynButton *buttons;
SynText output;
void BinaryButton();
void QuaternaryButton();
void OctalButton();
void HexadecimalButton();
public: public:
GUIWindow(unsigned int = 960, unsigned int = 540, std::string = "Window", sf::Uint32 = sf::Style::Titlebar | sf::Style::Close); GUIWindow(unsigned int = 960, unsigned int = 540, std::string = "Window", sf::Uint32 = sf::Style::Titlebar | sf::Style::Close);
void Update(); void Update();
void ButtonCheck(sf::Vector2i);
~GUIWindow(); ~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) 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() void GUIWindow::Update()
{ {
int buttonCounter;
Window::Update();
if(!isFocused) if(!isFocused)
return; 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() GUIWindow::~GUIWindow()
{ {
if(buttons) delete buttons;
} }

View File

@ -8,7 +8,7 @@
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
#include "GUIElements.hpp"
#include "Window.hpp" #include "Window.hpp"
#include "GUIWindow.hpp" #include "GUIWindow.hpp"
#include "GUIElements.hpp"
#endif #endif

View File

@ -62,6 +62,7 @@
bool isFocused; bool isFocused;
bool fullscreen; bool fullscreen;
virtual void Update(); virtual void Update();
virtual void ButtonCheck(sf::Vector2i);
public: public:
Window(unsigned int = 960, unsigned int = 540, std::string = "Window", sf::Uint32 = sf::Style::Titlebar | sf::Style::Close); Window(unsigned int = 960, unsigned int = 540, std::string = "Window", sf::Uint32 = sf::Style::Titlebar | sf::Style::Close);
virtual void CreateWindow(); virtual void CreateWindow();
@ -82,6 +83,8 @@
isFocused = false; isFocused = false;
else if (event.type == sf::Event::GainedFocus) else if (event.type == sf::Event::GainedFocus)
isFocused = true; isFocused = true;
else if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
ButtonCheck(sf::Mouse::getPosition(window));
if(sf::Keyboard::isKeyPressed(sf::Keyboard::F)) if(sf::Keyboard::isKeyPressed(sf::Keyboard::F))
{ {
fullscreen = !fullscreen; fullscreen = !fullscreen;
@ -96,6 +99,11 @@
timer.UpdateTime(); timer.UpdateTime();
} }
void Window::ButtonCheck(sf::Vector2i mousePos)
{
}
Window::Window(unsigned int width, unsigned int height, std::string title, sf::Uint32 style) Window::Window(unsigned int width, unsigned int height, std::string title, sf::Uint32 style)
{ {
this -> size.x = width; this -> size.x = width;
@ -130,7 +138,7 @@
size.y = videoMode.height; size.y = videoMode.height;
window.create(videoMode, title, sf::Style::Fullscreen); window.create(videoMode, title, sf::Style::Fullscreen);
} }
window.setVerticalSyncEnabled(true);
timer.ResetTimer(); timer.ResetTimer();
} }

View File

@ -2,6 +2,7 @@
int main() int main()
{ {
font.loadFromFile("../OpenSans-Bold.ttf");
GUIWindow window(960, 540, "GUI Test"); GUIWindow window(960, 540, "GUI Test");
while (window.IsOpen()) while (window.IsOpen())