Added input-history feature to TextInputForm. Control via up/down arrows.

This commit is contained in:
2025-06-27 15:26:33 -05:00
parent ce0c69190a
commit 3b973c2c45
2 changed files with 163 additions and 28 deletions

View File

@@ -27,18 +27,32 @@ namespace JUI {
class TextInputForm : public TextRect, public Clickable {
public:
#pragma region Events
Event<> OnSelect;
Event<> OnDeselect;
Event<std::string> OnReturn;
#pragma endregion
public:
#pragma region Constructors
TextInputForm();
explicit TextInputForm(Widget* parent);
#pragma endregion
public:
#pragma region Methods
void Update(float elapsed) override;
void InnerDraw() override;
void Draw() override;
/// @return The number of characters the cursor is actually moved to the left by.
int MoveCursorLeft(int chars = 1);
/// @note The amount of
/// @return The number of characters the cursor is actually moved to the right by.
int MoveCursorRight(int chars = 1);
#pragma region Getters
void MoveCursorLeft();
#pragma endregion
#pragma region Setters
void MoveCursorRight();
#pragma endregion
/// Returns the maximum position of the cursor, which is determined by the input buffer's length.
[[nodiscard]] unsigned int CursorMaxPosition() const;
@@ -64,6 +78,11 @@ namespace JUI {
void PushKeyToCurrentPlaceInInputBuffer(const Key &key);
bool ObserveKeyInput(Key key, bool pressed) override;
void ShowNextHistory();
void ShowPrevHistory();
bool ObserveMouseInput(MouseButton btn, bool pressed) override;
bool ObserveMouseMovement(const Vector2 &latest_known_pos) override;
[[nodiscard]] std::string GetAutocompleteText() const;
@@ -103,22 +122,50 @@ namespace JUI {
void SetSelectionColor(const Color4& color);
bool HasSelection() const;
std::string GetSelectedText() const;
[[nodiscard]] bool KeepInputHistory() const;
void KeepInputHistory(bool value);
std::string InputHistory(int index) const;
#pragma endregion
protected:
#pragma region Properties
bool keep_input_history = false;
bool clear_text_on_return = true;
bool drop_focus_on_return = false;
bool focused = false;
bool selection_enabled;
bool selection_active;
int selection_start_index;
int selection_end_index;
unsigned int cursor_position = 0;
std::string input_buffer;
float cursor_blink_time = 0.f;
Color4 autocomplete_color = Style::InputForm::AutocompleteTextColor;
std::string autocomplete_text = "Hello World";
bool hide_autocomplete_on_select = true;
bool autocomplete_text_enabled = true;
std::set<std::string> blacklist;
bool selection_enabled;
#pragma endregion
#pragma region Working Variables
std::vector<std::string> history;
int history_index = -1;
bool selection_active;
int selection_start_index;
int selection_end_index;
unsigned int cursor_position = 0;
std::string input_buffer;
std::string saved_input_buffer;
/// Tracks the time (in seconds) since the TextInputForm was last opened.
/// @note This is used to circumvent a behavioral bug caused by how the input code is structured:
/// 1. User clicks a button that opens an InputForm.
/// 2. Grab InputForm Focus.
/// 3. User releases the button.
/// 4. The input form interprets this as "I am focused, but something else was just clicked".
/// 5. The input form drops its focus instantly, and it appears as if it was never focused.
float time_focused = 0.f;
#pragma endregion
private:
};
}