Add TextInputForm::ClearTextOnReturn and ::DropFocusOnReturn

This commit is contained in:
2025-02-13 22:41:33 -05:00
parent 3aa9f419a2
commit 9cf78742ae
3 changed files with 50 additions and 10 deletions

View File

@@ -15,10 +15,8 @@
#include "TextRect.hpp"
namespace JUI
{
class TextInputForm : public TextRect, public Clickable
{
namespace JUI {
class TextInputForm : public TextRect, public Clickable {
public:
Event<> OnSelect;
Event<> OnDeselect;
@@ -40,20 +38,33 @@ namespace JUI
[[nodiscard]] bool AutocompleteTextEnabled() const;
void SetAutocompleteTextEnabled(bool enabled);
[[nodiscard]] std::string InputBuffer() const;
void SetInputBuffer(const std::string& value);
void ClearInputBuffer();
// TODO: Implement selection of part of input text.
Color4 GetSelectionColor() const;
void SetSelectionColor(const Color4& color);
bool HasSelection() const;
std::string GetSelectedText() const;
bool HasFocus() const;
[[nodiscard]] bool HasFocus() const;
void SetFocused(bool focused);
// TODO: Implement procedure to allow API consumer to validate the input, **before** clearing the buffer.
[[nodiscard]] bool ClearTextOnReturn() const;
void ClearTextOnReturn(bool value);
[[nodiscard]] bool DropFocusOnReturn() const;
void DropFocusOnReturn(bool value);
void GrabFocus();
void DropFocus();
std::vector<char> GetBlacklist() const { return blacklist;}
[[nodiscard]] std::vector<char> GetBlacklist() const;
protected:
bool clear_text_on_return;
bool clear_text_on_return = true;
bool drop_focus_on_return = false;
bool focused = false;
bool selection_enabled;
bool selection_active;

View File

@@ -47,6 +47,21 @@ void TextBase::Draw(const Vector2& abs_pos, const Vector2& abs_size, const std::
float align_x = abs_pos.x;
float align_y = abs_pos.y;
std::string start = content;
std::vector<std::string> lines;
/*for (int i = 0; i < start.length(); i++) {
std::string substr = start.substr(0, i);
auto sub_bounds = this->GetFont().MeasureString(substr, size);
if (sub_bounds.x > abs_size.x)
}*/
auto bounds = this->GetFont().MeasureString(content, size);
// Do nothing if there is no text.
if (bounds.x == 0 || bounds.y == 0)

View File

@@ -71,7 +71,7 @@ namespace JUI {
if (!focused || !hide_autocomplete_on_select)
TextBase::Draw(pos, size, autocomplete_text, this->text_size, autocomplete_color);
TextBase::Draw(pos, size);
TextBase::Draw(pos, size, content, text_size, autocomplete_color);
//TextRect::InnerDraw();
}
@@ -113,8 +113,6 @@ namespace JUI {
return s;
}
void TextInputForm::ObserveKeyInput(Key key, bool pressed) {
Widget::ObserveKeyInput(key, pressed);
@@ -213,4 +211,20 @@ namespace JUI {
void TextInputForm::DropFocus() { SetFocused(false); }
bool TextInputForm::ClearTextOnReturn() const { return clear_text_on_return; }
void TextInputForm::ClearTextOnReturn(bool value) { clear_text_on_return = value;}
bool TextInputForm::DropFocusOnReturn() const { return drop_focus_on_return;}
void TextInputForm::DropFocusOnReturn(bool value) { drop_focus_on_return = value;}
std::vector<char> TextInputForm::GetBlacklist() const { return blacklist;}
std::string TextInputForm::InputBuffer() const { return input_buffer;}
void TextInputForm::SetInputBuffer(const std::string &value) { input_buffer = value;}
void TextInputForm::ClearInputBuffer() { SetInputBuffer(""); }
}