R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
RaylibTextInput.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** r-type
4** File description:
5** RaylibTextInput - Raylib implementation of ITextInput
6*/
7
8#pragma once
9
10#include <functional>
11#include <regex>
12#include <string>
13
15#include "UI/IButton.hpp"
16#include "UI/ITextInput.hpp"
17
18namespace UI {
31 class RaylibTextInput : public ITextInput {
32 public:
37 explicit RaylibTextInput(Graphics::IGraphics &graphics);
38
42 ~RaylibTextInput() override = default;
43
45 void Update() override;
46
48 void Render() override;
49
51 void SetOnTextChanged(std::function<void(const std::string &)> callback) override;
52
54 void SetPosition(float x, float y) override;
55
57 void GetPosition(float &x, float &y) const override;
58
60 void SetSize(float width, float height) override;
61
63 void GetSize(float &width, float &height) const override;
64
66 void SetBackgroundColor(unsigned int color) override;
67
69 void SetBorderColor(unsigned int color) override;
70
72 void SetActiveBorderColor(unsigned int color) override;
73
75 void SetTextColor(unsigned int color) override;
76
78 void SetPlaceholderColor(unsigned int color) override;
79
81 void SetTextSize(int size) override;
82
84 void SetPlaceholder(const std::string &placeholder) override;
85
87 void SetMaxLength(size_t maxLength) override;
88
90 void SetValidationRegex(const std::string &regexPattern) override;
91
93 [[nodiscard]] const std::string &GetText() const override;
94
96 void SetText(const std::string &text) override;
97
99 void Clear() override;
100
102 [[nodiscard]] bool IsFocused() const override;
103
105 void SetFocused(bool focused) override;
106
108 void SetFont(int fontHandle) override;
109
111 void SetAlign(Align align) override;
112
114 [[nodiscard]] Align GetAlign() const override;
115
117 void ApplyAlignment() override;
118
120 void SetEnabled(bool enabled) override;
121
123 [[nodiscard]] bool IsEnabled() const override;
124
126 void SetPasswordMode(bool passwordMode) override;
127
129 [[nodiscard]] bool IsPasswordMode() const override;
130
131 private:
135 [[nodiscard]] bool IsMouseOver() const;
136
142 [[nodiscard]] bool IsCharValid(char c) const;
143
144 // ===== Helper methods for Update() to reduce cognitive complexity =====
145
149 void HandleFocusClick();
150
154 void UpdateCursorBlink();
155
159 void HandlePaste();
160
164 void HandleBackspace();
165
170
176 bool TryAddCharacter(char c);
177
181 void ResetCursor();
182
184 std::function<void(const std::string &)> _onTextChanged{};
185
186 float _x{0.0F};
187 float _y{0.0F};
188 float _width{200.0F};
189 float _height{40.0F};
190
191 unsigned int _backgroundColor{0xFF2A2A2A};
192 unsigned int _borderColor{0xFF505050};
193 unsigned int _activeBorderColor{0xFF4CAF50};
194 unsigned int _textColor{0xFFFFFFFF};
195 unsigned int _placeholderColor{0xFF808080};
196
197 std::string _text;
198 std::string _placeholder;
199 int _textSize{20};
200 int _fontHandle{-1};
201
202 size_t _maxLength{0}; // 0 = unlimited
203 std::string _regexPattern;
204 std::unique_ptr<std::regex> _validationRegex{};
205
206 bool _focused{false};
207 bool _enabled{true};
208 bool _passwordMode{false};
209 float _cursorBlinkTimer{0.0F};
210 bool _cursorVisible{true};
211
212 // Backspace key repeat support
213 float _backspaceTimer{0.0F};
215
217
218 static constexpr float CURSOR_BLINK_RATE = 0.5F; // seconds
219 static constexpr float BORDER_THICKNESS = 2.0F;
220 static constexpr float TEXT_PADDING = 8.0F;
221 static constexpr float BACKSPACE_REPEAT_DELAY = 0.5F; // Initial delay before repeat (seconds)
222 static constexpr float BACKSPACE_REPEAT_RATE = 0.05F; // Time between repeats (seconds)
223 };
224} // namespace UI
Abstract interface for graphics rendering operations.
Definition IGraphics.hpp:32
Abstract interface for UI text input fields.
Raylib implementation of the ITextInput interface.
void GetSize(float &width, float &height) const override
Get the current size of the text input.
const std::string & GetText() const override
Get the current text content.
static constexpr float BORDER_THICKNESS
void GetPosition(float &x, float &y) const override
Get the current top-left position of the text input.
bool IsPasswordMode() const override
Check if password mode is enabled.
void SetTextSize(int size) override
Set the text size in pixels.
void SetFocused(bool focused) override
Set focus state programmatically.
static constexpr float BACKSPACE_REPEAT_DELAY
static constexpr float CURSOR_BLINK_RATE
void HandleBackspace()
Handle backspace key with repeat support.
void ResetCursor()
Show cursor and reset blink timer.
bool TryAddCharacter(char c)
Add a character to the text if valid.
void SetOnTextChanged(std::function< void(const std::string &)> callback) override
Set callback invoked when text changes.
bool IsEnabled() const override
Check if the text input is enabled.
unsigned int _activeBorderColor
void SetValidationRegex(const std::string &regexPattern) override
Set a regex pattern to restrict allowed characters.
bool IsMouseOver() const
Check whether the mouse cursor is currently over the input rectangle.
unsigned int _backgroundColor
void SetActiveBorderColor(unsigned int color) override
Set the border color when the input is focused/active.
void SetAlign(Align align) override
Set alignment mode relative to the window.
bool IsFocused() const override
Check if the text input is currently focused/active.
void SetBorderColor(unsigned int color) override
Set the border color when the input is not focused.
void SetText(const std::string &text) override
Set the text content programmatically.
void HandleFocusClick()
Handle mouse click to set focus state.
void SetTextColor(unsigned int color) override
Set the text color.
void ApplyAlignment() override
Apply the current alignment mode to the position.
void SetPasswordMode(bool passwordMode) override
Enable or disable password mode (masks characters with asterisks).
void Clear() override
Clear the text content.
void SetFont(int fontHandle) override
Set the font handle for rendering text.
Graphics::IGraphics & _graphics
void SetPlaceholderColor(unsigned int color) override
Set the placeholder text color (when input is empty).
void SetPosition(float x, float y) override
Set the top-left position of the text input.
std::function< void(const std::string &)> _onTextChanged
void Render() override
Render the text input box.
void SetSize(float width, float height) override
Set the text input size.
void SetBackgroundColor(unsigned int color) override
Set the background color of the text input box.
void SetEnabled(bool enabled) override
Enable or disable the text input.
void HandlePaste()
Handle paste operation (Ctrl+V or Cmd+V).
Align GetAlign() const override
Get the current alignment mode.
void UpdateCursorBlink()
Update cursor blink animation.
void HandleCharacterInput()
Handle regular character input.
void SetMaxLength(size_t maxLength) override
Set the maximum number of characters allowed.
void Update() override
Update the text input internal state (focus, cursor, input).
bool IsCharValid(char c) const
Validate a character against the current regex pattern.
static constexpr float BACKSPACE_REPEAT_RATE
void SetPlaceholder(const std::string &placeholder) override
Set the placeholder text displayed when the input is empty.
static constexpr float TEXT_PADDING
std::unique_ptr< std::regex > _validationRegex
unsigned int _placeholderColor
~RaylibTextInput() override=default
Destroy the RaylibTextInput.
Definition IButton.hpp:13
Align
Alignment modes relative to the current window.
Definition IButton.hpp:33
@ NONE
No alignment.