GUI Finish

This commit is contained in:
Asrın Doğan 2020-03-01 23:51:21 +03:00
parent a103be1145
commit 5cfcf3d457
4 changed files with 237 additions and 36 deletions

View File

@ -1,11 +1,16 @@
#include "SynGame.hpp" #include "SynGame.hpp"
sf::Font font; sf::Font font;
sf::Color textColor = sf::Color(35, 35, 35);
sf::Color backColor = sf::Color(125, 162, 169);
sf::Color frontColor = sf::Color(247, 247, 247);
class GUIWindow;
#pragma region Text #pragma region Text
class SynText class SynText
{ {
private: protected:
int x; int x;
int y; int y;
int offsetX; int offsetX;
@ -14,10 +19,10 @@ sf::Font font;
sf::Text textDisplay; sf::Text textDisplay;
public: public:
SynText(std::string = "", int = 0, int = 0, int = 0, int = 0, bool = false); SynText(std::string = "", int = 0, int = 0, int = 0, int = 0, bool = false);
void SetText(std::string); virtual void SetText(std::string);
void SetOffset(int, int); virtual void SetOffset(int, int);
void SetPosition(int, int); virtual void SetPosition(int, int);
void SetCentered(bool); virtual void SetCentered(bool);
sf::Text GetText(); sf::Text GetText();
~SynText(); ~SynText();
}; };
@ -26,10 +31,10 @@ sf::Font font;
{ {
this -> isCentered = isCentered; this -> isCentered = isCentered;
textDisplay.setFont(font); textDisplay.setFont(font);
textDisplay.setColor(textColor);
SetText(text); SetText(text);
SetOffset(offsetX, offsetX); SetOffset(offsetX, offsetX);
SetPosition(x, y); SetPosition(x, y);
textDisplay.setColor(sf::Color::Red);
} }
void SynText::SetText(std::string text) void SynText::SetText(std::string text)
@ -72,13 +77,145 @@ sf::Font font;
} }
#pragma endregion #pragma endregion
#pragma region InputField
class SynInputField : public SynText
{
private:
int width;
int height;
std::stringstream stream;
sf::Vertex vertices[4];
void CalculateVertices();
bool isFocused;
public:
SynInputField();
void SetText(std::string);
void SetArea(int, int);
void SetOffset(int, int);
void SetPosition(int, int);
bool IsMouseOver(int, int);
void AddToInput(char);
void Click(bool);
unsigned int GetValue();
sf::Vertex *GetVertices();
~SynInputField();
};
SynInputField::SynInputField()
{
isFocused = false;
stream.clear();
SetOffset(7, 7);
SetText(stream.str());
for (int i = 0; i < 4; i++)
vertices[i].color = frontColor;
}
void SynInputField::SetText(std::string newText)
{
SynText::SetText(newText);
CalculateVertices();
}
void SynInputField::SetOffset(int x, int y)
{
SynText::SetOffset(x, y);
CalculateVertices();
}
void SynInputField::SetPosition(int x, int y)
{
SynText::SetPosition(x, y);
CalculateVertices();
}
void SynInputField::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);
}
sf::Vertex *SynInputField::GetVertices()
{
return vertices;
}
unsigned int SynInputField::GetValue()
{
unsigned int value = 0;
std::stringstream temp;
temp << stream.str();
temp >> value;
return value;
}
bool SynInputField::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 SynInputField::Click(bool isFocused)
{
this -> isFocused = isFocused;
}
void SynInputField::AddToInput(char character)
{
if(!isFocused) return;
if(character == 59 && stream.str().length() > 0) // Backspace
{
std::string temp = stream.str();
temp.pop_back();
stream.str("");
stream << temp;
SetText(stream.str());
return;
}
if(character < 26 || character > 35) // 0 - 9 keys
return;
if(stream.str().length() > 8)
return;
stream << character - 26;
SetText(stream.str());
}
void SynInputField::SetArea(int width, int height)
{
this -> width = width;
this -> height = height;
SetPosition(x, y);
CalculateVertices();
}
SynInputField::~SynInputField()
{
}
#pragma endregion
#pragma region Button #pragma region Button
class SynButton class SynButton
{ {
private: private:
SynText synText; SynText synText;
void (*action)(int); GUIWindow *window;
int *value; void (GUIWindow::*action)();
int x; int x;
int y; int y;
int width; int width;
@ -90,7 +227,7 @@ sf::Font font;
void SetText(std::string); void SetText(std::string);
void SetPosition(int, int); void SetPosition(int, int);
void SetArea(int, int); void SetArea(int, int);
void Bind(void (*)(int), int *); void Bind(GUIWindow *, void (GUIWindow::*)());
bool IsMouseOver(int, int); bool IsMouseOver(int, int);
void Click(); void Click();
sf::Text GetText(); sf::Text GetText();
@ -102,6 +239,7 @@ sf::Font font;
{ {
float newWidth = width / 2; float newWidth = width / 2;
float newHeight = height / 2; float newHeight = height / 2;
vertices[0].position = sf::Vector2f(x, y); vertices[0].position = sf::Vector2f(x, y);
vertices[1].position = sf::Vector2f(x + newWidth, y); vertices[1].position = sf::Vector2f(x + newWidth, y);
vertices[2].position = sf::Vector2f(x + newWidth, y + newHeight); vertices[2].position = sf::Vector2f(x + newWidth, y + newHeight);
@ -111,10 +249,13 @@ sf::Font font;
SynButton::SynButton(std::string text, int x, int y, int width, int height) SynButton::SynButton(std::string text, int x, int y, int width, int height)
{ {
synText.SetCentered(true); synText.SetCentered(true);
synText.SetOffset(0, -5); synText.SetOffset(0, -7);
SetArea(width, height); SetArea(width, height);
SetPosition(x, y); SetPosition(x, y);
CalculateVertices(); CalculateVertices();
for (int i = 0; i < 4; i++)
vertices[i].color = frontColor;
} }
void SynButton::SetText(std::string text) void SynButton::SetText(std::string text)
@ -138,10 +279,10 @@ sf::Font font;
CalculateVertices(); CalculateVertices();
} }
void SynButton::Bind(void (*action)(int), int *value) void SynButton::Bind(GUIWindow *window, void (GUIWindow::*action)())
{ {
this -> window = window;
this -> action = action; this -> action = action;
this -> value = value;
} }
bool SynButton::IsMouseOver(int mouseX, int mouseY) bool SynButton::IsMouseOver(int mouseX, int mouseY)
@ -158,11 +299,9 @@ sf::Font font;
void SynButton::Click() void SynButton::Click()
{ {
if(value) ((window)->*(action))();
action(*value);
else
action(y);
} }
sf::Text SynButton::GetText() sf::Text SynButton::GetText()
{ {
return synText.GetText(); return synText.GetText();

View File

@ -1,47 +1,92 @@
#include "SynGame.hpp" #include "SynGame.hpp"
#include <iostream>
class GUIWindow : public Window class GUIWindow : public Window
{ {
private: private:
SynButton *buttons; SynButton *buttons;
SynText output; SynText output;
SynInputField input;
void BinaryButton(); void BinaryButton();
void QuaternaryButton(); void QuaternaryButton();
void OctalButton(); void OctalButton();
void HexadecimalButton(); void HexadecimalButton();
std::string GetBase(unsigned int, int);
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); void ButtonCheck(sf::Vector2i);
void KeyHandle(char);
~GUIWindow(); ~GUIWindow();
}; };
void GUIWindow::BinaryButton() void GUIWindow::BinaryButton()
{ {
output.SetText("Output: Binary"); output.SetText(GetBase(input.GetValue(), 2));
} }
void GUIWindow::QuaternaryButton() void GUIWindow::QuaternaryButton()
{ {
output.SetText("Output: Quaternary"); output.SetText(GetBase(input.GetValue(), 4));
} }
void GUIWindow::OctalButton() void GUIWindow::OctalButton()
{ {
output.SetText("Output: Octal"); output.SetText(GetBase(input.GetValue(), 8));
} }
void GUIWindow::HexadecimalButton() void GUIWindow::HexadecimalButton()
{ {
output.SetText("Output: Hexadecimal"); output.SetText(GetBase(input.GetValue(), 16));
}
std::string GUIWindow::GetBase(unsigned int value, int base)
{
std::stringstream stringStream;
int counter = 0;
while (value > 0)
{
if(counter++ % 4 == 0)
stringStream << ' ';
if(base == 16)
if((value % base) > 9)
stringStream << (char)(((value % base) % 10) + 'A');
else
stringStream << (value % base);
else
stringStream << (value % base);
value = value / base;
}
while (counter++ % 4 != 0)
stringStream << '0';
std::string displayValue = stringStream.str();
char temp;
int half = displayValue.length() / 2;
int last = displayValue.length() - 1;
for (int i = 0; i < half; i++)
{
temp = displayValue[i];
displayValue[i] = displayValue[last];
displayValue[last--] = temp;
}
displayValue.insert(0, "Sonuc: ");
return displayValue;
} }
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: "); input.SetPosition(window.getSize().x / 3 - 100, 210);
input.SetArea(400, 100);
output.SetText("");
output.SetCentered(true); output.SetCentered(true);
output.SetPosition(window.getSize().x / 2, window.getSize().y / 2); output.SetPosition(window.getSize().x / 3, 310);
output.SetOffset(0, 0); output.SetOffset(0, 0);
buttons = new SynButton[4]; buttons = new SynButton[4];
@ -55,10 +100,10 @@ GUIWindow::GUIWindow(unsigned int width, unsigned int height, std::string title,
(buttons + 2) -> SetArea(400, 100); (buttons + 2) -> SetPosition(650, 300); (buttons + 2) -> SetArea(400, 100); (buttons + 2) -> SetPosition(650, 300);
(buttons + 3) -> SetArea(400, 100); (buttons + 3) -> SetPosition(650, 400); (buttons + 3) -> SetArea(400, 100); (buttons + 3) -> SetPosition(650, 400);
(buttons + 0) -> Bind((void (*)(int))&BinaryButton, NULL); (buttons + 0) -> Bind(this, &GUIWindow::BinaryButton);
(buttons + 1) -> Bind((void (*)(int))&QuaternaryButton, NULL); (buttons + 1) -> Bind(this, &GUIWindow::QuaternaryButton);
(buttons + 2) -> Bind((void (*)(int))&OctalButton, NULL); (buttons + 2) -> Bind(this, &GUIWindow::OctalButton);
(buttons + 3) -> Bind((void (*)(int))&HexadecimalButton, NULL); (buttons + 3) -> Bind(this, &GUIWindow::HexadecimalButton);
} }
void GUIWindow::Update() void GUIWindow::Update()
@ -71,7 +116,7 @@ void GUIWindow::Update()
if(!isFocused) if(!isFocused)
return; return;
window.clear(sf::Color::Black); window.clear(backColor);
for (buttonCounter = 0; buttonCounter < 4; buttonCounter++) for (buttonCounter = 0; buttonCounter < 4; buttonCounter++)
{ {
@ -80,6 +125,8 @@ void GUIWindow::Update()
} }
window.draw(output.GetText()); window.draw(output.GetText());
window.draw(input.GetVertices(), 4, sf::PrimitiveType::Quads);
window.draw(input.GetText());
window.display(); window.display();
} }
@ -97,7 +144,12 @@ void GUIWindow::ButtonCheck(sf::Vector2i mousePos)
break; break;
} }
} }
input.Click(input.IsMouseOver(mousePos.x, mousePos.y));
}
void GUIWindow::KeyHandle(char character)
{
input.AddToInput(character);
} }
GUIWindow::~GUIWindow() GUIWindow::~GUIWindow()

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 "GUIElements.hpp"
#include "GUIWindow.hpp" #include "GUIWindow.hpp"
#endif #endif

View File

@ -63,6 +63,7 @@
bool fullscreen; bool fullscreen;
virtual void Update(); virtual void Update();
virtual void ButtonCheck(sf::Vector2i); virtual void ButtonCheck(sf::Vector2i);
virtual void KeyHandle(char);
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();
@ -83,14 +84,18 @@
isFocused = false; isFocused = false;
else if (event.type == sf::Event::GainedFocus) else if (event.type == sf::Event::GainedFocus)
isFocused = true; isFocused = true;
else if (event.type == sf::Event::KeyPressed)
{
KeyHandle((int)event.key.code);
}
else if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) else if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
ButtonCheck(sf::Mouse::getPosition(window)); ButtonCheck(sf::Mouse::getPosition(window));
if(sf::Keyboard::isKeyPressed(sf::Keyboard::F)) // if(sf::Keyboard::isKeyPressed(sf::Keyboard::F))
{ // {
fullscreen = !fullscreen; // fullscreen = !fullscreen;
CloseWindow(); // CloseWindow();
CreateWindow(); // CreateWindow();
} // }
} }
if(!isFocused) if(!isFocused)
@ -104,6 +109,11 @@
} }
void Window::KeyHandle(char character)
{
}
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;