Added empty text string to textbox gui element
This commit is contained in:
parent
e0a19a9c49
commit
6feebe351d
4 changed files with 38 additions and 17 deletions
|
|
@ -16,6 +16,7 @@ template <typename T> struct TextBoxData {
|
|||
bool singleLine = true;
|
||||
bool immutable = false;
|
||||
InputManager::TextInputProperties textInputProps;
|
||||
std::string emptyText;
|
||||
std::function<void()> onEdit;
|
||||
std::function<void()> onEnter;
|
||||
std::function<void()> onSelect;
|
||||
|
|
@ -48,6 +49,7 @@ template <typename T> class TextBox : public Element {
|
|||
virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) override;
|
||||
~TextBox();
|
||||
private:
|
||||
void populate_empty_textbox();
|
||||
void after_text_input_callback();
|
||||
void init_textbox(UpdateInputData& io);
|
||||
void reset_textbox_text();
|
||||
|
|
@ -60,10 +62,12 @@ template <typename T> class TextBox : public Element {
|
|||
RichText::TextBox::Cursor cur;
|
||||
} oldDD;
|
||||
|
||||
bool isEmptyText = false;
|
||||
bool isHeld = false;
|
||||
std::optional<TextEditData> edit;
|
||||
|
||||
std::optional<T> oldData;
|
||||
std::string oldEmptyText;
|
||||
TextBoxData<T> userInfo;
|
||||
std::shared_ptr<RichText::TextBox> textbox;
|
||||
std::shared_ptr<RichText::TextBox::Cursor> cur;
|
||||
|
|
|
|||
|
|
@ -9,8 +9,11 @@ template <typename T> void TextBox<T>::layout(const Clay_ElementId& id, const Te
|
|||
auto& io = gui.io;
|
||||
this->userInfo = userInfo;
|
||||
init_textbox(io);
|
||||
if(!oldData.has_value() || oldData.value() != *userInfo.data)
|
||||
if(!oldData.has_value() || oldData.value() != *userInfo.data || (oldEmptyText != userInfo.emptyText)) {
|
||||
reset_textbox_text();
|
||||
populate_empty_textbox();
|
||||
gui.invalidate_draw_element(this);
|
||||
}
|
||||
|
||||
CLAY(id, {
|
||||
.layout = {
|
||||
|
|
@ -75,6 +78,8 @@ template <typename T> void TextBox<T>::select() {
|
|||
if(!is_selected()) {
|
||||
SCollision::AABB<float> rect = {boundingBox.value().min * gui.io.guiScaleMultiplier, boundingBox.value().max * gui.io.guiScaleMultiplier};
|
||||
unsigned id = gui.io.input->set_text_box_front(rect, userInfo.textInputProps);
|
||||
if(isEmptyText)
|
||||
textbox->clear_text();
|
||||
edit = TextEditData(id, textbox, cur);
|
||||
}
|
||||
}
|
||||
|
|
@ -85,6 +90,7 @@ template <typename T> void TextBox<T>::deselect() {
|
|||
if(userInfo.onDeselect) userInfo.onDeselect();
|
||||
reset_textbox_text();
|
||||
edit = std::nullopt;
|
||||
populate_empty_textbox();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -114,6 +120,21 @@ template <typename T> void TextBox<T>::input_text_key_callback(const InputManage
|
|||
}
|
||||
}
|
||||
|
||||
template <typename T> void TextBox<T>::populate_empty_textbox() {
|
||||
if(!is_selected() && isEmptyText && !userInfo.emptyText.empty()) {
|
||||
RichText::TextData text;
|
||||
RichText::TextData::Paragraph& par = text.paragraphs.emplace_back();
|
||||
par.text = userInfo.emptyText;
|
||||
|
||||
RichText::PositionedTextStyleMod& positionedMod = text.tStyleMods.emplace_back();
|
||||
positionedMod.pos = {0, 0};
|
||||
positionedMod.mods[RichText::TextStyleModifier::ModifierType::SLANT] = std::make_shared<RichText::SlantTextStyleModifier>(SkFontStyle::kItalic_Slant);
|
||||
positionedMod.mods[RichText::TextStyleModifier::ModifierType::COLOR] = std::make_shared<RichText::ColorTextStyleModifier>(convert_vec4<Vector4f>(gui.io.theme->frontColor2));
|
||||
|
||||
textbox->set_rich_text_data(text);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T> void TextBox<T>::after_text_input_callback() {
|
||||
if(userInfo.immutable)
|
||||
cur->pos = cur->selectionBeginPos = cur->selectionEndPos = textbox->insert({0, 0}, userInfo.toStr(*userInfo.data));
|
||||
|
|
@ -167,7 +188,9 @@ template <typename T> void TextBox<T>::init_textbox(UpdateInputData& io) {
|
|||
}
|
||||
|
||||
template <typename T> void TextBox<T>::reset_textbox_text() {
|
||||
textbox->set_string(userInfo.toStr(*userInfo.data));
|
||||
std::string newStr = userInfo.toStr(*userInfo.data);
|
||||
isEmptyText = newStr.empty();
|
||||
textbox->set_string(newStr);
|
||||
cur->pos = textbox->move(RichText::TextBox::Movement::NOWHERE, cur->pos);
|
||||
cur->selectionBeginPos = textbox->move(RichText::TextBox::Movement::NOWHERE, cur->selectionBeginPos);
|
||||
cur->selectionEndPos = textbox->move(RichText::TextBox::Movement::NOWHERE, cur->selectionEndPos);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue