Text Input Form Autocomplete text.
This commit is contained in:
@@ -112,6 +112,9 @@ namespace JUI
|
||||
|
||||
void SetWordWrap(bool wrap);
|
||||
|
||||
void Draw(const Vector2 &abs_pos, const Vector2 &abs_size, const std::string &content, uint size,
|
||||
const Color4 &color);
|
||||
|
||||
protected:
|
||||
std::string content = "Sample Text";
|
||||
Color4 text_color = {255,255,255};
|
||||
|
@@ -20,35 +20,45 @@ namespace JUI
|
||||
class TextInputForm : public TextRect, public Clickable
|
||||
{
|
||||
public:
|
||||
|
||||
Event<> OnSelect;
|
||||
Event<> OnDeselect;
|
||||
Event<std::string> OnReturn;
|
||||
|
||||
TextInputForm();
|
||||
explicit TextInputForm(Widget* parent);
|
||||
void Update(float elapsed) override;
|
||||
void InnerDraw() override;
|
||||
void Draw() override;
|
||||
|
||||
void ObserveKeyInput(Key key, bool pressed) override;
|
||||
void ObserveMouseInput(MouseButton btn, bool pressed) override;
|
||||
void ObserveMouseMovement(const Vector2 &latest_known_pos) override;
|
||||
[[nodiscard]] std::string GetAutocompleteText() const;
|
||||
void SetAutoCompleteText(const std::string& text);
|
||||
[[nodiscard]] Color4 GetAutocompleteTextColor() const;
|
||||
void SetAutocompleteTextColor(const Color4& color);
|
||||
[[nodiscard]] bool HideAutocompleteOnSelect() const;
|
||||
void SetHideAutocompleteOnSelect(bool hide);
|
||||
[[nodiscard]] bool AutocompleteTextEnabled() const;
|
||||
void SetAutocompleteTextEnabled(bool enabled);
|
||||
|
||||
|
||||
// TODO: Implement selection of part of input text.
|
||||
Color4 GetSelectionColor() const;
|
||||
void SetSelectionColor(const Color4& color);
|
||||
bool HasSelection() const;
|
||||
std::string GetSelectedText() const;
|
||||
protected:
|
||||
bool clear_text_on_return;
|
||||
bool focused = false;
|
||||
|
||||
bool selection_enabled;
|
||||
|
||||
bool selection_active;
|
||||
|
||||
int selection_start_index;
|
||||
int selection_end_index;
|
||||
|
||||
int cursor_position = 0;
|
||||
|
||||
std::string input_buffer;
|
||||
|
||||
float cursor_blink_time = 0.f;
|
||||
Color4 autocomplete_color = Colors::Black;
|
||||
std::string autocomplete_text = "Hello World";
|
||||
bool hide_autocomplete_on_select = true;
|
||||
bool autocomplete_text_enabled = true;
|
||||
private:
|
||||
};
|
||||
}
|
2
main.cpp
2
main.cpp
@@ -105,7 +105,7 @@ JUI::Scene* CreateScene() {
|
||||
|
||||
auto* input_form = new TextInputForm(s1_vert);
|
||||
input_form->Size({0,30, 1, 0});
|
||||
input_form->SetContent("This Diqq");
|
||||
input_form->SetContent("");
|
||||
input_form->SetTextSize(14);
|
||||
|
||||
|
||||
|
@@ -42,13 +42,12 @@ namespace JUI {
|
||||
|
||||
void TextBase::SetWordWrap(bool wrap) { word_wrap = wrap; }
|
||||
|
||||
void TextBase::Draw(const Vector2 &abs_pos, const Vector2 &abs_size) {
|
||||
|
||||
void TextBase::Draw(const Vector2& abs_pos, const Vector2& abs_size, const std::string& content, uint size, const Color4& color) {
|
||||
// Calculate how much to origin the text based on alignment.
|
||||
float align_x = abs_pos.x;
|
||||
float align_y = abs_pos.y;
|
||||
|
||||
auto bounds = this->GetFont().MeasureString(this->content, this->text_size);
|
||||
auto bounds = this->GetFont().MeasureString(content, size);
|
||||
|
||||
if (h_align == TextAlign::H::Left) {
|
||||
// Render from left-side of boundary.
|
||||
@@ -79,16 +78,20 @@ namespace JUI {
|
||||
float scale = 1.f;
|
||||
|
||||
JGL::J2D::Begin();
|
||||
JGL::J2D::DrawString(
|
||||
this->text_color,
|
||||
this->content,
|
||||
align_x, align_y,
|
||||
scale,
|
||||
this->text_size,
|
||||
this->set_font);
|
||||
JGL::J2D::DrawString(
|
||||
color,
|
||||
content,
|
||||
align_x, align_y,
|
||||
scale,
|
||||
size,
|
||||
this->set_font);
|
||||
JGL::J2D::End();
|
||||
}
|
||||
|
||||
void TextBase::Draw(const Vector2 &abs_pos, const Vector2 &abs_size) {
|
||||
Draw(abs_pos, abs_size, this->content, this->text_size, this->text_color);
|
||||
}
|
||||
|
||||
std::string TextBase::GetContent() const { return content;}
|
||||
|
||||
Color4 TextBase::GetTextColor() const { return text_color;}
|
||||
|
@@ -11,18 +11,71 @@ namespace JUI {
|
||||
Widget::ObserveMouseMovement(latest_known_pos);
|
||||
}
|
||||
|
||||
std::string TextInputForm::GetAutocompleteText() const { return autocomplete_text;}
|
||||
|
||||
void TextInputForm::SetAutoCompleteText(const std::string &text) { autocomplete_text = text;}
|
||||
|
||||
Color4 TextInputForm::GetAutocompleteTextColor() const { return autocomplete_color; }
|
||||
|
||||
void TextInputForm::SetAutocompleteTextColor(const Color4 &color) { autocomplete_color = color; }
|
||||
|
||||
bool TextInputForm::HideAutocompleteOnSelect() const { return hide_autocomplete_on_select; }
|
||||
|
||||
void TextInputForm::SetHideAutocompleteOnSelect(bool hide) { hide_autocomplete_on_select = hide; }
|
||||
|
||||
bool TextInputForm::AutocompleteTextEnabled() const { return autocomplete_text_enabled; }
|
||||
|
||||
void TextInputForm::SetAutocompleteTextEnabled(bool enabled) { autocomplete_text_enabled = enabled; }
|
||||
|
||||
void TextInputForm::ObserveMouseInput(MouseButton btn, bool pressed) {
|
||||
Widget::ObserveMouseInput(btn, pressed);
|
||||
if (pressed && btn == MouseButton::Left) {
|
||||
if (IsMouseInside()) {
|
||||
if (!focused) {
|
||||
OnSelect.Invoke();
|
||||
|
||||
if (IsMouseInside())
|
||||
}
|
||||
focused = true;
|
||||
else
|
||||
}
|
||||
else {
|
||||
if (focused)
|
||||
OnDeselect.Invoke();
|
||||
focused = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TextInputForm::InnerDraw()
|
||||
{
|
||||
|
||||
Rect::InnerDraw();
|
||||
|
||||
Vector2 abs_pos = this->GetAbsolutePosition();
|
||||
Vector2 abs_size = this->GetAbsoluteSize();
|
||||
|
||||
|
||||
// TODO: Apply this everywhere..
|
||||
Vector2 pad_shifts_pos_by = {
|
||||
pad_left.Pixels + (pad_left.Scale * abs_size.x),
|
||||
pad_top.Pixels + (pad_left.Scale * abs_size.y)
|
||||
};
|
||||
|
||||
Vector2 pad_shrinks_size_by = {
|
||||
pad_right.Pixels + (pad_left.Scale * abs_size.x),
|
||||
pad_bottom.Pixels + (pad_right.Scale * abs_size.y)
|
||||
};
|
||||
|
||||
Vector2 pos = abs_pos + pad_shifts_pos_by;
|
||||
Vector2 size = abs_pos - (pad_shrinks_size_by*2.f);
|
||||
|
||||
if (!focused || !hide_autocomplete_on_select)
|
||||
TextBase::Draw(pos, size, autocomplete_text, this->text_size, autocomplete_color);
|
||||
|
||||
TextBase::Draw(pos, size);
|
||||
|
||||
//TextRect::InnerDraw();
|
||||
}
|
||||
|
||||
void TextInputForm::Draw() {
|
||||
TextRect::Draw();
|
||||
}
|
||||
@@ -60,6 +113,8 @@ namespace JUI {
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void TextInputForm::ObserveKeyInput(Key key, bool pressed) {
|
||||
Widget::ObserveKeyInput(key, pressed);
|
||||
|
||||
@@ -82,7 +137,6 @@ namespace JUI {
|
||||
|
||||
if (clear_text_on_return) {
|
||||
|
||||
|
||||
input_buffer = "";
|
||||
cursor_position = 0;
|
||||
}
|
||||
@@ -90,12 +144,14 @@ namespace JUI {
|
||||
}
|
||||
|
||||
if (key == Keys::LeftArrow) {
|
||||
cursor_position -= 1;
|
||||
if (cursor_position > 0)
|
||||
cursor_position -= 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (key == Keys::RightArrow) {
|
||||
cursor_position += 1;
|
||||
if (cursor_position < input_buffer.length() - 1)
|
||||
cursor_position += 1;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -104,6 +160,7 @@ namespace JUI {
|
||||
if (key == Keys::C) {
|
||||
|
||||
// TODO: Implement Copy
|
||||
|
||||
return;
|
||||
}
|
||||
if (key == Keys::V) {
|
||||
@@ -113,6 +170,7 @@ namespace JUI {
|
||||
|
||||
if (key == Keys::X) {
|
||||
// TODO: Implement Cut
|
||||
cursor_position = 0; // TODO: Set to size of pasted content.
|
||||
return;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user