#include"ui.h" void UI::handleEvent(const sf::Event &event){ switch(event.type){ case sf::Event::MouseMoved: mousePos.x = event.mouseMove.x; mousePos.y = event.mouseMove.y; break; case sf::Event::MouseButtonPressed: previousMouse = currentMouse; switch(event.mouseButton.button) { case sf::Mouse::Right: currentMouse.rightDown = true; break; case sf::Mouse::Left: currentMouse.leftDown = true; break; case sf::Mouse::Middle: currentMouse.midDown = true; break; default: break; } break; case sf::Event::MouseButtonReleased: previousMouse = currentMouse; switch(event.mouseButton.button) { case sf::Mouse::Right: currentMouse.rightDown = false; break; case sf::Mouse::Left: currentMouse.leftDown = false; break; case sf::Mouse::Middle: currentMouse.midDown = false; break; default: break; } break; default: break; } } bool UI::mouseButtonPressed(MouseButton m){ switch(m) { case MouseButton::Left: return currentMouse.leftDown && !previousMouse.leftDown; case MouseButton::Mid: return currentMouse.midDown && !previousMouse.midDown; case MouseButton::Right: return currentMouse.rightDown && !previousMouse.rightDown; default: return false; } } bool UI::mouseButtonReleased(MouseButton m){ switch(m) { case MouseButton::Left: return !currentMouse.leftDown && previousMouse.leftDown; case MouseButton::Mid: return !currentMouse.midDown && previousMouse.midDown; case MouseButton::Right: return !currentMouse.rightDown && previousMouse.rightDown; default: return false; } } bool UI::mouseButtonIsDown(MouseButton m){ switch(m) { case MouseButton::Left: return currentMouse.leftDown; case MouseButton::Mid: return currentMouse.midDown; case MouseButton::Right: return currentMouse.rightDown; default: return false; } } bool UI::mouseButtonIsUp(MouseButton m){ switch(m) { case MouseButton::Left: return !currentMouse.leftDown; case MouseButton::Mid: return !currentMouse.midDown; case MouseButton::Right: return !currentMouse.rightDown; default: return false; } } sf::Vector2i UI::mousePosition(){ return mousePos; } void UI::initImUI(){ hotItem = 0; } void UI::finishImUI(){ if (mouseButtonIsUp(MouseButton::Left)) { activeItem = 0; } else { if (activeItem == 0) activeItem = -1; } } bool UI::button(sf::RenderWindow *win, int id, const std::string &text, sf::Vector2i pos, sf::Vector2i size){ bool result = false; sf::IntRect rect {pos, size}; if( rect.contains( mousePosition() ) ){ hotItem = id; } if (hotItem == id){ if(mouseButtonPressed(MouseButton::Left)){ activeItem = id; } } if (activeItem == id){ if (mouseButtonReleased(MouseButton::Left)){ if ( hotItem == id ){ result = true; } activeItem = -1; } } // Rendering: sf::RectangleShape rectangle { sf::Vector2f(size) }; rectangle.setPosition( sf::Vector2f(pos) ); rectangle.setFillColor(sf::Color::White); if (hotItem == id){ rectangle.setFillColor(sf::Color::Blue); } if (activeItem == id){ rectangle.setFillColor(sf::Color::Red); } win->draw(rectangle); return result; } bool UI::radioButton(sf::RenderWindow *win, Graphics::TextureFont font, int id, const std::string &text, bool state, sf::Vector2i pos, sf::Vector2i size){ sf::IntRect rect {pos, size}; if( rect.contains( mousePosition() ) ){ hotItem = id; } if (hotItem == id){ if(mouseButtonPressed(MouseButton::Left)){ activeItem = id; } } if (activeItem == id){ if (mouseButtonReleased(MouseButton::Left)){ if ( hotItem == id ){ state = !state; } activeItem = -1; } } // Rendering: sf::RectangleShape rectangle { sf::Vector2f(size) }; rectangle.setPosition( sf::Vector2f(pos) ); win->draw(rectangle); sf::CircleShape radio{ 4 }; radio.setPosition( sf::Vector2f(pos) + sf::Vector2f(10, size.y/2) ); radio.setOutlineColor( sf::Color::Red ); radio.setOutlineThickness( 1 ); if(state){ radio.setFillColor( sf::Color::Blue ); } else { } win->draw(radio); Graphics::Text textGraph {font, text, 1.4, size.x - 30, size.y}; textGraph.setPosition(sf::Vector2f(pos) + sf::Vector2f{30, size.y/2}); win->draw(textGraph); return state; }