Velvet 0.1a
A beginner-friendly C++ GUI framework built on SFML.
Loading...
Searching...
No Matches
InputField.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <SFML/Graphics/RectangleShape.hpp>
3 #include <SFML/Graphics/RenderWindow.hpp>
4 #include <SFML/Graphics/Text.hpp>
5 #include <SFML/System/Vector2.hpp>
6 #include <unordered_map>
7 #include <variant>
9 #include <SFML/Graphics/Color.hpp>
10
25class InputField : public Widget {
26private:
27 sf::Text textField;
28 sf::Text placeholderText;
29
30 sf::Font font;
31 sf::RectangleShape container;
32
33 sf::View clipView;
34 sf::View ogView;
35
36 float width;
37 bool focused;
38
39 std::string type;
40 std::string content;
41 std::string placeholder;
42
43 std::unordered_map<std::string, std::variant<unsigned int, float, std::string>> styles = {
44 {"fontPath", "src/assets/AdwaitaSans-Regular.ttf"},
45 {"fontSize", 20.f},
46 {"letterSpacing", 1.f},
47 {"fontColor", 0x000000FFu},
48 {"outlineColor", 0x000000FFu},
49 {"activeOutlineColor", 0x0288D1FFu},
50 {"backgroundColor", 0xFFFFFFFFu},
51 {"placeholderColor", 0x6E6E6EFFu},
52 {"width", 200.f}
53 };
54
55 void draw(sf::RenderWindow &window) override;
56 void update(sf::RenderWindow &window) override;
57 void drawText(sf::Text, sf::RenderWindow &window);
58 void updateScroll();
59public:
60
62 void render(sf::RenderWindow &window) override;
63
65 void setPosition(float x, float y) override;
66
68 void handleEvent(const sf::Event &event, sf::RenderWindow &window) override;
69
71 sf::Vector2f getDimensions() override;
72
78 InputField(std::string placeholder = "", std::unordered_map<std::string, std::variant<unsigned int, float, std::string>> styling = {});
79
85 void overrideStyling(std::unordered_map<std::string, std::variant<unsigned int, float, std::string>> styling);
86
88 std::string getContent();
90 std::string getPlaceholder();
91
93 void setPlaceholder(std::string placeholderString);
94};
Single-line text input widget with placeholder and basic styling.
Definition InputField.hpp:25
std::string getPlaceholder()
Get value of placeholder text .
void setPosition(float x, float y) override
Set top-left position of the input box.
InputField(std::string placeholder="", std::unordered_map< std::string, std::variant< unsigned int, float, std::string > > styling={})
Create an input field with optional placeholder and style overrides.
void setPlaceholder(std::string placeholderString)
Set value of placeholder.
void overrideStyling(std::unordered_map< std::string, std::variant< unsigned int, float, std::string > > styling)
Apply/override style properties at runtime.
void handleEvent(const sf::Event &event, sf::RenderWindow &window) override
Handle focus changes and text input/backspace events.
sf::Vector2f getDimensions() override
Get input field dimensions (container bounds).
void render(sf::RenderWindow &window) override
Render the input field.
std::string getContent()
Get text content in the field.
Base class for all Velvet UI elements.
Definition Widget.hpp:16
float y
Definition Widget.hpp:31
float x
Definition Widget.hpp:31