/
yaseeeeechka
/
CenterControl
Обзор
Документация
Войти
/
yaseeeeechka
/
CenterControl
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
CenterControl_TCP/include/client/TextBox.h
61 строка
2 KB
Yaroslava
New structure of Repo. TCP impl
11 дек 2025, 21:12
11 дек 2025, 21:12
6fdf9d7
Код
Авторство
О чём код?
#ifndef TEXTBOX_H #define TEXTBOX_H #include <SFML/Graphics.hpp> // ==================== Компоненты GUI =================================== class TextBox { public: TextBox(float x, float y, float w, float h, const sf::Font &font) : box_({w, h}), text_(font, "", 20), font_(font) { box_.setPosition({x, y}); box_.setFillColor(sf::Color::White); box_.setOutlineThickness(2); box_.setOutlineColor(sf::Color::Black); text_.setPosition({x + 5, y + 3}); text_.setFillColor(sf::Color::Black); } void HandleEvent(const sf::Event &e, const sf::Window &win) { if (e.is<sf::Event::MouseButtonPressed>()) { auto m = sf::Mouse::getPosition(win); is_selected_ = box_.getGlobalBounds().contains({(float) m.x, (float) m.y}); box_.setOutlineColor(is_selected_ ? sf::Color::Blue : sf::Color::Black); } if (e.is<sf::Event::TextEntered>() && is_selected_) { auto unicode = e.getIf<sf::Event::TextEntered>()->unicode; if (unicode == '\b') { if (!text_.getString().isEmpty()) { auto s = text_.getString(); s.erase(s.getSize() - 1); text_.setString(s); } } else if (unicode >= 32 && unicode < 127) { text_.setString(text_.getString() + static_cast<char>(unicode)); } } } void Draw(sf::RenderTarget &t) const { t.draw(box_); t.draw(text_); } std::string GetText() const { return text_.getString(); } void SetText(const std::string &s) { text_.setString(s); } void Clear() { text_.setString(""); } private: sf::RectangleShape box_; sf::Text text_; sf::Font font_; bool is_selected_ = false; }; #endif //TEXTBOX_H