GUI.cpp code
Vector2f GUI::mousePosition(RenderWindow& _window){
Vector2f pos(Mouse::getPosition(_window));
return pos;
}
void GUI::CreateButton(Vector2f position, Vector2f size, String text, Texture buttonIMG[3])
{
buttonSettings rect; // creating button
rect.buttonIMG = buttonIMG[0];
rect.buttonGUIDANCE = buttonIMG[1];
rect.buttonCLICK = buttonIMG[2];
rect.btns.setSize(size);
rect.btnText.setFont(font);
rect.btnText.setCharacterSize(16);
rect.btnText.setColor(Color(200,200,200));
rect.btnText.setString(text);
rect.btnText.setPosition(rect.btns.getPosition().x + (rect.btns.getSize().x/2 - rect.btns.getSize().x/5) , rect.btns.getPosition().y + (rect.btns.getSize().y/3)); // setting text in the middle
buttons.push_back(rect);
}
int GUI::OnButtonClick(RenderWindow& _window) {
for(btnID = 0; btnID != buttons.size(); btnID++) {
FloatRect bounds(buttons[btnID].btns.getGlobalBounds());
if(bounds.contains(mousePosition(_window)) && event.type == Event::MouseButtonPressed) {
if(event.mouseButton.button == Mouse::Left) {
buttons[btnID].btns.setTexture(&buttons[btnID].buttonCLICK); // on button click
return btnID;
}
}
if(bounds.contains(mousePosition(_window))) { // on button guidance
buttons[btnID].btns.setTexture(&buttons[btnID].buttonGUIDANCE);
}
else {
buttons[btnID].btns.setTexture(&buttons[btnID].buttonIMG); // button idle
}
}
}
GUI.h
typedef struct buttonSettings {
Text btnText;
RectangleShape btns;
Vector2f size;
Texture buttonIMG, buttonGUIDANCE, buttonCLICK;
};
vector<buttonSettings> buttons;
main.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
Texture img[3];
string getPath()
{
char result[ MAX_PATH ];
return string( result, GetCurrentDirectory(100, result));
}
int main() {
RenderWindow window(VideoMode(1024,768), "game"); // window initialisation
window.clear();
for(unsigned short i = 0; i<3; i++) {
img[i].loadFromFile(getPath() + "\\GUI\\BUTTON\\BUTTON_" + to_string(i) + ".png");
}
gui.CreateButton(Vector2f(window.getSize().x/2, window.getSize().y/18), Vector2f(200, 70), "", img); // creating button
window.display();
while(window.isOpen()) {
while(window.pollEvent(Event event) {
gui.OnButtonClick(window);
}
}
}
here, all working code, but textures changes, not color and I'm resizing window manually, so all the elements are scaling