Send textbox rectangle and textbox type to SDL

This commit is contained in:
Yousef Khadadeh 2026-03-05 23:34:30 +03:00
parent d38156ec26
commit f3671e667d
8 changed files with 179 additions and 32 deletions

View file

@ -13,6 +13,7 @@
#include <Helpers/Serializers.hpp>
#include <nlohmann/json.hpp>
#include "../../RichText/TextBox.hpp"
#include "../../InputManager.hpp"
using namespace Eigen;
@ -97,6 +98,8 @@ struct UpdateInputData {
std::shared_ptr<RichText::TextBox> previousRichTextBoxToEdit;
std::shared_ptr<RichText::TextBox> richTextBoxToEdit;
std::shared_ptr<RichText::TextBox::Cursor> richTextBoxToEditCursor;
InputManager::TextInputProperties richTextInputProperties;
std::shared_ptr<SCollision::AABB<float>> richTextBoxToEditRectangle;
SkFont get_font(float fSize) const;
@ -110,6 +113,9 @@ struct UpdateInputData {
float deltaTime = 0.0f;
bool isTouch = true;
float guiScaleMultiplier = 1.0f;
Vector2f windowPos = {0.0f, 0.0f};
std::shared_ptr<FontData> fonts;
sk_sp<SkTypeface> textTypeface;

View file

@ -4,12 +4,13 @@
#include <limits>
#include <modules/skparagraph/include/TextStyle.h>
#include "../../FontData.hpp"
#include "../../InputManager.hpp"
namespace GUIStuff {
template <typename T> class TextBox : public Element {
public:
bool update(UpdateInputData& io, T* newData, const std::function<std::optional<T>(const std::string&)>& newFromStr, const std::function<std::string(const T&)> newToStr, bool newSingleLine, bool updateEveryEdit, const std::function<void(SelectionHelper&)>& elemUpdate) {
bool update(UpdateInputData& io, T* newData, const std::function<std::optional<T>(const std::string&)>& newFromStr, const std::function<std::string(const T&)> newToStr, bool newSingleLine, bool updateEveryEdit, const InputManager::TextInputProperties& textInputProps, const std::function<void(SelectionHelper&)>& elemUpdate) {
bool isUpdating = false;
if(!textbox || !cur)
@ -33,6 +34,8 @@ template <typename T> class TextBox : public Element {
if(data && selection.selected) {
io.richTextBoxToEdit = textbox;
io.richTextBoxToEditCursor = cur;
io.richTextBoxToEditRectangle = rect;
io.richTextInputProperties = textInputProps;
textbox->process_mouse_left_button(*cur, io.mouse.pos - bb.min, io.mouse.leftClick, io.mouse.leftHeld, io.key.leftShift);
if((updateEveryEdit && textbox->inputChangedTextBox) || io.key.enter) {
update_on_edit();
@ -53,6 +56,8 @@ template <typename T> class TextBox : public Element {
if(textbox)
textbox->inputChangedTextBox = false;
*rect = {(bb.min + io.windowPos) * io.guiScaleMultiplier, (bb.max + io.windowPos) * io.guiScaleMultiplier};
return isUpdating;
}
@ -108,6 +113,7 @@ template <typename T> class TextBox : public Element {
textbox->set_allow_newlines(false);
cur = std::make_shared<RichText::TextBox::Cursor>();
rect = std::make_shared<SCollision::AABB<float>>();
if(data) {
cur->pos = cur->selectionBeginPos = cur->selectionEndPos = textbox->insert({0, 0}, toStr(*data));
@ -131,8 +137,9 @@ template <typename T> class TextBox : public Element {
std::shared_ptr<RichText::TextBox> textbox;
std::shared_ptr<RichText::TextBox::Cursor> cur;
std::shared_ptr<SCollision::AABB<float>> rect;
SCollision::AABB<float> bb;
SCollision::AABB<float> bb = {{1.0f, 1.0f}, {2.0f, 2.0f}};
};
}

View file

@ -393,7 +393,15 @@ bool GUIManager::input_color_component_255(const char* id, float* val, const std
std::stringstream ss;
ss << convertTo255;
return ss.str();
}, true, false, elemUpdate);
}, true, false,
{
.inputType = SDL_TextInputType::SDL_TEXTINPUT_TYPE_NUMBER,
.capitalization = SDL_Capitalization::SDL_CAPITALIZE_NONE,
.autocorrect = false,
.multiline = false,
.androidInputType = InputManager::AndroidInputType::ANDROIDTEXT_TYPE_CLASS_NUMBER
},
elemUpdate);
pop_id();
return isUpdating;
}
@ -406,7 +414,15 @@ bool GUIManager::input_text(const char* id, std::string* val, bool updateEveryEd
},
[&](const std::string& str) {
return str;
}, true, updateEveryEdit, elemUpdate);
}, true, updateEveryEdit,
{
.inputType = SDL_TextInputType::SDL_TEXTINPUT_TYPE_TEXT,
.capitalization = SDL_Capitalization::SDL_CAPITALIZE_NONE,
.autocorrect = false,
.multiline = false,
.androidInputType = InputManager::AndroidInputType::ANDROIDTEXT_TYPE_CLASS_TEXT
},
elemUpdate);
pop_id();
return toRet;
}
@ -533,7 +549,15 @@ bool GUIManager::input_scalar(const char* id, uint8_t* val, uint8_t min, uint8_t
},
[&](const uint8_t& a) {
return std::to_string(static_cast<uint32_t>(a));
}, true, false, elemUpdate);
}, true, false,
{
.inputType = SDL_TextInputType::SDL_TEXTINPUT_TYPE_NUMBER,
.capitalization = SDL_Capitalization::SDL_CAPITALIZE_NONE,
.autocorrect = false,
.multiline = false,
.androidInputType = InputManager::AndroidInputType::ANDROIDTEXT_TYPE_CLASS_NUMBER
},
elemUpdate);
pop_id();
return isUpdating;
}
@ -549,7 +573,15 @@ void GUIManager::input_path(const char* id, std::filesystem::path* val, std::fil
},
[&](const std::filesystem::path& a) {
return a.string();
}, true, false, elemUpdate);
}, true, false,
{
.inputType = SDL_TextInputType::SDL_TEXTINPUT_TYPE_TEXT,
.capitalization = SDL_Capitalization::SDL_CAPITALIZE_NONE,
.autocorrect = false,
.multiline = false,
.androidInputType = InputManager::AndroidInputType::ANDROIDTEXT_TYPE_CLASS_TEXT
},
elemUpdate);
pop_id();
}

View file

@ -147,15 +147,26 @@ class GUIManager {
return toRet;
}
template <typename T> bool input_generic(const char* id, T* val, const std::function<std::optional<T>(const std::string&)>& fromStr, const std::function<std::string(const T&)>& toStr, bool singleLine, bool updateEveryEdit, const std::function<void(SelectionHelper&)>& elemUpdate = nullptr) {
template <typename T> bool input_generic(const char* id, T* val, const std::function<std::optional<T>(const std::string&)>& fromStr, const std::function<std::string(const T&)>& toStr, bool singleLine, bool updateEveryEdit, const InputManager::TextInputProperties& inputProps, const std::function<void(SelectionHelper&)>& elemUpdate = nullptr) {
bool isUpdating = false;
push_id(id);
isUpdating = insert_element<TextBox<T>>()->update(*io, val, fromStr, toStr, singleLine, updateEveryEdit, elemUpdate);
isUpdating = insert_element<TextBox<T>>()->update(*io, val, fromStr, toStr, singleLine, updateEveryEdit, inputProps, elemUpdate);
pop_id();
return isUpdating;
}
template <typename T> bool input_scalar(const char* id, T* val, T min, T max, int decimalPrecision = 0, const std::function<void(SelectionHelper&)>& elemUpdate = nullptr) {
InputManager::TextInputProperties textInputProps {
.inputType = SDL_TextInputType::SDL_TEXTINPUT_TYPE_NUMBER,
.capitalization = SDL_Capitalization::SDL_CAPITALIZE_NONE,
.autocorrect = false,
.multiline = false,
.androidInputType = InputManager::AndroidInputType::ANDROIDTEXT_TYPE_CLASS_NUMBER
};
if(std::is_signed<T>())
textInputProps.androidInputType |= InputManager::AndroidInputType::ANDROIDTEXT_TYPE_NUMBER_FLAG_SIGNED;
if(std::is_floating_point<T>())
textInputProps.androidInputType |= InputManager::AndroidInputType::ANDROIDTEXT_TYPE_NUMBER_FLAG_DECIMAL;
return input_generic<T>(id, val,
[&](const std::string& a) {
if(a.empty())
@ -177,7 +188,7 @@ class GUIManager {
else
ss << *val;
return ss.str();
}, true, false, elemUpdate);
}, true, false, textInputProps, elemUpdate);
}
bool input_scalar(const char* id, uint8_t* val, uint8_t min, uint8_t max, int decimalPrecision, const std::function<void(SelectionHelper&)>& elemUpdate);
@ -231,7 +242,15 @@ class GUIManager {
}
return ss.str();
},
true, false, elemUpdate);
true, false,
{
.inputType = SDL_TextInputType::SDL_TEXTINPUT_TYPE_TEXT,
.capitalization = SDL_Capitalization::SDL_CAPITALIZE_NONE,
.autocorrect = false,
.multiline = false,
.androidInputType = InputManager::AndroidInputType::ANDROIDTEXT_TYPE_CLASS_TEXT
},
elemUpdate);
}
template <typename T> bool color_picker(const char* id, T* val, bool selectAlpha, const std::function<void()>& elemUpdate = nullptr) {