GUI Finish
This commit is contained in:
parent
a103be1145
commit
5cfcf3d457
|
@ -1,11 +1,16 @@
|
|||
#include "SynGame.hpp"
|
||||
|
||||
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
|
||||
class SynText
|
||||
{
|
||||
private:
|
||||
protected:
|
||||
int x;
|
||||
int y;
|
||||
int offsetX;
|
||||
|
@ -14,10 +19,10 @@ sf::Font font;
|
|||
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);
|
||||
virtual void SetText(std::string);
|
||||
virtual void SetOffset(int, int);
|
||||
virtual void SetPosition(int, int);
|
||||
virtual void SetCentered(bool);
|
||||
sf::Text GetText();
|
||||
~SynText();
|
||||
};
|
||||
|
@ -26,10 +31,10 @@ sf::Font font;
|
|||
{
|
||||
this -> isCentered = isCentered;
|
||||
textDisplay.setFont(font);
|
||||
textDisplay.setColor(textColor);
|
||||
SetText(text);
|
||||
SetOffset(offsetX, offsetX);
|
||||
SetPosition(x, y);
|
||||
textDisplay.setColor(sf::Color::Red);
|
||||
}
|
||||
|
||||
void SynText::SetText(std::string text)
|
||||
|
@ -72,13 +77,145 @@ sf::Font font;
|
|||
}
|
||||
#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
|
||||
class SynButton
|
||||
{
|
||||
private:
|
||||
SynText synText;
|
||||
void (*action)(int);
|
||||
int *value;
|
||||
GUIWindow *window;
|
||||
void (GUIWindow::*action)();
|
||||
int x;
|
||||
int y;
|
||||
int width;
|
||||
|
@ -90,7 +227,7 @@ sf::Font font;
|
|||
void SetText(std::string);
|
||||
void SetPosition(int, int);
|
||||
void SetArea(int, int);
|
||||
void Bind(void (*)(int), int *);
|
||||
void Bind(GUIWindow *, void (GUIWindow::*)());
|
||||
bool IsMouseOver(int, int);
|
||||
void Click();
|
||||
sf::Text GetText();
|
||||
|
@ -102,6 +239,7 @@ sf::Font font;
|
|||
{
|
||||
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);
|
||||
|
@ -111,10 +249,13 @@ sf::Font font;
|
|||
SynButton::SynButton(std::string text, int x, int y, int width, int height)
|
||||
{
|
||||
synText.SetCentered(true);
|
||||
synText.SetOffset(0, -5);
|
||||
synText.SetOffset(0, -7);
|
||||
SetArea(width, height);
|
||||
SetPosition(x, y);
|
||||
CalculateVertices();
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
vertices[i].color = frontColor;
|
||||
}
|
||||
|
||||
void SynButton::SetText(std::string text)
|
||||
|
@ -138,10 +279,10 @@ sf::Font font;
|
|||
CalculateVertices();
|
||||
}
|
||||
|
||||
void SynButton::Bind(void (*action)(int), int *value)
|
||||
void SynButton::Bind(GUIWindow *window, void (GUIWindow::*action)())
|
||||
{
|
||||
this -> window = window;
|
||||
this -> action = action;
|
||||
this -> value = value;
|
||||
}
|
||||
|
||||
bool SynButton::IsMouseOver(int mouseX, int mouseY)
|
||||
|
@ -158,11 +299,9 @@ sf::Font font;
|
|||
|
||||
void SynButton::Click()
|
||||
{
|
||||
if(value)
|
||||
action(*value);
|
||||
else
|
||||
action(y);
|
||||
((window)->*(action))();
|
||||
}
|
||||
|
||||
sf::Text SynButton::GetText()
|
||||
{
|
||||
return synText.GetText();
|
||||
|
|
|
@ -1,47 +1,92 @@
|
|||
#include "SynGame.hpp"
|
||||
#include <iostream>
|
||||
|
||||
class GUIWindow : public Window
|
||||
{
|
||||
private:
|
||||
SynButton *buttons;
|
||||
SynText output;
|
||||
SynInputField input;
|
||||
void BinaryButton();
|
||||
void QuaternaryButton();
|
||||
void OctalButton();
|
||||
void HexadecimalButton();
|
||||
std::string GetBase(unsigned int, int);
|
||||
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);
|
||||
void KeyHandle(char);
|
||||
~GUIWindow();
|
||||
};
|
||||
|
||||
void GUIWindow::BinaryButton()
|
||||
{
|
||||
output.SetText("Output: Binary");
|
||||
output.SetText(GetBase(input.GetValue(), 2));
|
||||
}
|
||||
|
||||
void GUIWindow::QuaternaryButton()
|
||||
{
|
||||
output.SetText("Output: Quaternary");
|
||||
output.SetText(GetBase(input.GetValue(), 4));
|
||||
}
|
||||
|
||||
void GUIWindow::OctalButton()
|
||||
{
|
||||
output.SetText("Output: Octal");
|
||||
output.SetText(GetBase(input.GetValue(), 8));
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
output.SetText("Output: ");
|
||||
input.SetPosition(window.getSize().x / 3 - 100, 210);
|
||||
input.SetArea(400, 100);
|
||||
output.SetText("");
|
||||
output.SetCentered(true);
|
||||
output.SetPosition(window.getSize().x / 2, window.getSize().y / 2);
|
||||
output.SetPosition(window.getSize().x / 3, 310);
|
||||
output.SetOffset(0, 0);
|
||||
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 + 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);
|
||||
(buttons + 0) -> Bind(this, &GUIWindow::BinaryButton);
|
||||
(buttons + 1) -> Bind(this, &GUIWindow::QuaternaryButton);
|
||||
(buttons + 2) -> Bind(this, &GUIWindow::OctalButton);
|
||||
(buttons + 3) -> Bind(this, &GUIWindow::HexadecimalButton);
|
||||
}
|
||||
|
||||
void GUIWindow::Update()
|
||||
|
@ -71,7 +116,7 @@ void GUIWindow::Update()
|
|||
if(!isFocused)
|
||||
return;
|
||||
|
||||
window.clear(sf::Color::Black);
|
||||
window.clear(backColor);
|
||||
|
||||
for (buttonCounter = 0; buttonCounter < 4; buttonCounter++)
|
||||
{
|
||||
|
@ -80,6 +125,8 @@ void GUIWindow::Update()
|
|||
}
|
||||
|
||||
window.draw(output.GetText());
|
||||
window.draw(input.GetVertices(), 4, sf::PrimitiveType::Quads);
|
||||
window.draw(input.GetText());
|
||||
window.display();
|
||||
}
|
||||
|
||||
|
@ -97,7 +144,12 @@ void GUIWindow::ButtonCheck(sf::Vector2i mousePos)
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
input.Click(input.IsMouseOver(mousePos.x, mousePos.y));
|
||||
}
|
||||
|
||||
void GUIWindow::KeyHandle(char character)
|
||||
{
|
||||
input.AddToInput(character);
|
||||
}
|
||||
|
||||
GUIWindow::~GUIWindow()
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include "GUIElements.hpp"
|
||||
#include "Window.hpp"
|
||||
#include "GUIElements.hpp"
|
||||
#include "GUIWindow.hpp"
|
||||
#endif
|
||||
|
|
|
@ -63,6 +63,7 @@
|
|||
bool fullscreen;
|
||||
virtual void Update();
|
||||
virtual void ButtonCheck(sf::Vector2i);
|
||||
virtual void KeyHandle(char);
|
||||
public:
|
||||
Window(unsigned int = 960, unsigned int = 540, std::string = "Window", sf::Uint32 = sf::Style::Titlebar | sf::Style::Close);
|
||||
virtual void CreateWindow();
|
||||
|
@ -83,14 +84,18 @@
|
|||
isFocused = false;
|
||||
else if (event.type == sf::Event::GainedFocus)
|
||||
isFocused = true;
|
||||
else if (event.type == sf::Event::KeyPressed)
|
||||
{
|
||||
KeyHandle((int)event.key.code);
|
||||
}
|
||||
else if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
|
||||
ButtonCheck(sf::Mouse::getPosition(window));
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::F))
|
||||
{
|
||||
fullscreen = !fullscreen;
|
||||
CloseWindow();
|
||||
CreateWindow();
|
||||
}
|
||||
// if(sf::Keyboard::isKeyPressed(sf::Keyboard::F))
|
||||
// {
|
||||
// fullscreen = !fullscreen;
|
||||
// CloseWindow();
|
||||
// CreateWindow();
|
||||
// }
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
this -> size.x = width;
|
||||
|
|
Loading…
Reference in New Issue