SFML_Experiments/GUITest/GUIWindow.hpp

225 lines
5.9 KiB
C++
Raw Normal View History

2020-03-01 14:13:23 +03:00
#include "SynGame.hpp"
class GUIWindow : public Window
{
private:
2020-03-01 20:03:43 +03:00
SynButton *buttons;
SynText output;
2020-03-01 23:51:21 +03:00
SynInputField input;
2020-03-04 13:16:54 +03:00
void ButtonCheck(sf::Vector2i);
void MouseButtonPressedHandle(sf::Vector2i);
void KeyHandle(char);
2020-03-01 20:03:43 +03:00
void BinaryButton();
void QuaternaryButton();
void OctalButton();
2020-03-02 12:37:57 +03:00
void DecimalButton();
2020-03-01 20:03:43 +03:00
void HexadecimalButton();
2020-03-02 12:37:57 +03:00
void InputBinaryButton();
void InputQuaternaryButton();
void InputOctalButton();
void InputDecimalButton();
void InputHexadecimalButton();
2020-03-01 23:51:21 +03:00
std::string GetBase(unsigned int, int);
2020-03-01 14:13:23 +03:00
public:
GUIWindow(unsigned int = 960, unsigned int = 540, std::string = "Window", sf::Uint32 = sf::Style::Titlebar | sf::Style::Close);
void Update();
~GUIWindow();
};
2020-03-04 13:16:54 +03:00
void GUIWindow::MouseButtonPressedHandle(sf::Vector2i mousePos)
{
ButtonCheck(mousePos);
}
void GUIWindow::ButtonCheck(sf::Vector2i mousePos)
{
int i;
SynButton *current;
for (i = 0; i < 10; i++)
{
current = buttons + i;
if(current -> IsMouseOver(mousePos.x, mousePos.y))
{
(buttons + i) -> Click();
break;
}
}
input.Click(input.IsMouseOver(mousePos.x, mousePos.y));
}
void GUIWindow::KeyHandle(char character)
{
input.AddToInput(character);
}
2020-03-01 20:03:43 +03:00
void GUIWindow::BinaryButton()
{
2020-03-01 23:51:21 +03:00
output.SetText(GetBase(input.GetValue(), 2));
2020-03-01 20:03:43 +03:00
}
void GUIWindow::QuaternaryButton()
{
2020-03-01 23:51:21 +03:00
output.SetText(GetBase(input.GetValue(), 4));
2020-03-01 20:03:43 +03:00
}
void GUIWindow::OctalButton()
{
2020-03-01 23:51:21 +03:00
output.SetText(GetBase(input.GetValue(), 8));
2020-03-01 20:03:43 +03:00
}
2020-03-02 12:37:57 +03:00
void GUIWindow::DecimalButton()
{
output.SetText(GetBase(input.GetValue(), 10));
}
2020-03-01 20:03:43 +03:00
void GUIWindow::HexadecimalButton()
{
2020-03-01 23:51:21 +03:00
output.SetText(GetBase(input.GetValue(), 16));
}
2020-03-02 12:37:57 +03:00
void GUIWindow::InputBinaryButton()
{
input.SetBase(2);
}
void GUIWindow::InputQuaternaryButton()
{
input.SetBase(4);
}
void GUIWindow::InputOctalButton()
{
input.SetBase(8);
}
void GUIWindow::InputDecimalButton()
{
input.SetBase(10);
}
void GUIWindow::InputHexadecimalButton()
{
input.SetBase(16);
}
2020-03-01 23:51:21 +03:00
std::string GUIWindow::GetBase(unsigned int value, int base)
{
std::stringstream stringStream;
int counter = 0;
while (value > 0)
{
2020-03-02 12:37:57 +03:00
if(base != 10)
if(counter++ % 4 == 0)
stringStream << ' ';
2020-03-01 23:51:21 +03:00
if(base == 16)
if((value % base) > 9)
stringStream << (char)(((value % base) % 10) + 'A');
else
stringStream << (value % base);
else
stringStream << (value % base);
value = value / base;
}
2020-03-02 12:37:57 +03:00
if(base != 10)
while (counter++ % 4 != 0)
stringStream << '0';
2020-03-01 23:51:21 +03:00
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;
2020-03-01 20:03:43 +03:00
}
2020-03-01 14:13:23 +03:00
GUIWindow::GUIWindow(unsigned int width, unsigned int height, std::string title, sf::Uint32 style) : Window(width, height, title, style)
{
2020-03-02 12:37:57 +03:00
input.SetPosition(window.getSize().x / 2 - 120, 210);
2020-03-01 23:51:21 +03:00
input.SetArea(400, 100);
2020-03-02 12:37:57 +03:00
input.SetBase(10);
2020-03-01 23:51:21 +03:00
output.SetText("");
2020-03-01 20:03:43 +03:00
output.SetCentered(true);
2020-03-02 12:37:57 +03:00
output.SetPosition(window.getSize().x / 2 - 20, 310);
2020-03-01 20:03:43 +03:00
output.SetOffset(0, 0);
2020-03-02 12:37:57 +03:00
buttons = new SynButton[10];
2020-03-01 14:13:23 +03:00
2020-03-01 20:03:43 +03:00
(buttons + 0) -> SetText("Binary");
(buttons + 1) -> SetText("Quaternary");
(buttons + 2) -> SetText("Octal");
2020-03-02 12:37:57 +03:00
(buttons + 3) -> SetText("Decimal");
(buttons + 4) -> SetText("Hexadecimal");
(buttons + 5) -> SetText("Binary");
(buttons + 6) -> SetText("Quaternary");
(buttons + 7) -> SetText("Octal");
(buttons + 8) -> SetText("Decimal");
(buttons + 9) -> SetText("Hexadecimal");
(buttons + 0) -> SetArea(400, 100); (buttons + 0) -> SetPosition(650, 50);
(buttons + 1) -> SetArea(400, 100); (buttons + 1) -> SetPosition(650, 150);
(buttons + 2) -> SetArea(400, 100); (buttons + 2) -> SetPosition(650, 250);
(buttons + 3) -> SetArea(400, 100); (buttons + 3) -> SetPosition(650, 350);
(buttons + 4) -> SetArea(400, 100); (buttons + 4) -> SetPosition(650, 450);
2020-03-01 20:03:43 +03:00
2020-03-02 12:37:57 +03:00
(buttons + 5) -> SetArea(400, 100); (buttons + 5) -> SetPosition(50, 50);
(buttons + 6) -> SetArea(400, 100); (buttons + 6) -> SetPosition(50, 150);
(buttons + 7) -> SetArea(400, 100); (buttons + 7) -> SetPosition(50, 250);
(buttons + 8) -> SetArea(400, 100); (buttons + 8) -> SetPosition(50, 350);
(buttons + 9) -> SetArea(400, 100); (buttons + 9) -> SetPosition(50, 450);
2020-03-01 20:03:43 +03:00
2020-03-01 23:51:21 +03:00
(buttons + 0) -> Bind(this, &GUIWindow::BinaryButton);
(buttons + 1) -> Bind(this, &GUIWindow::QuaternaryButton);
(buttons + 2) -> Bind(this, &GUIWindow::OctalButton);
2020-03-02 12:37:57 +03:00
(buttons + 3) -> Bind(this, &GUIWindow::DecimalButton);
(buttons + 4) -> Bind(this, &GUIWindow::HexadecimalButton);
(buttons + 5) -> Bind(this, &GUIWindow::InputBinaryButton);
(buttons + 6) -> Bind(this, &GUIWindow::InputQuaternaryButton);
(buttons + 7) -> Bind(this, &GUIWindow::InputOctalButton);
(buttons + 8) -> Bind(this, &GUIWindow::InputDecimalButton);
(buttons + 9) -> Bind(this, &GUIWindow::InputHexadecimalButton);
2020-03-01 14:13:23 +03:00
}
void GUIWindow::Update()
{
2020-03-01 20:03:43 +03:00
int buttonCounter;
Window::Update();
2020-03-01 14:13:23 +03:00
if(!isFocused)
return;
2020-03-01 23:51:21 +03:00
window.clear(backColor);
2020-03-01 20:03:43 +03:00
2020-03-02 12:37:57 +03:00
for (buttonCounter = 0; buttonCounter < 10; buttonCounter++)
2020-03-01 20:03:43 +03:00
{
window.draw((buttons + buttonCounter) -> GetVertices(), 4, sf::PrimitiveType::Quads);
window.draw((buttons + buttonCounter) -> GetText());
}
window.draw(output.GetText());
2020-03-01 23:51:21 +03:00
window.draw(input.GetVertices(), 4, sf::PrimitiveType::Quads);
window.draw(input.GetText());
2020-03-01 20:03:43 +03:00
window.display();
}
2020-03-01 14:13:23 +03:00
GUIWindow::~GUIWindow()
{
2020-03-01 20:03:43 +03:00
if(buttons) delete buttons;
2020-03-01 14:13:23 +03:00
}