feat: 'edited' property reset & add 'oncontrolkey' textbox callback
This commit is contained in:
@@ -15,6 +15,7 @@ using wstringsupplier = std::function<std::wstring()>;
|
||||
using doublesupplier = std::function<double()>;
|
||||
using boolsupplier = std::function<bool()>;
|
||||
using vec2supplier = std::function<glm::vec2()>;
|
||||
using key_handler = std::function<bool(int)>;
|
||||
|
||||
using stringconsumer = std::function<void(const std::string&)>;
|
||||
using wstringconsumer = std::function<void(const std::wstring&)>;
|
||||
|
||||
@@ -145,6 +145,10 @@ public:
|
||||
: history(history), historySize(history.size()) {
|
||||
}
|
||||
|
||||
Combination(const Combination&) = delete;
|
||||
|
||||
Combination(Combination&&) = default;
|
||||
|
||||
~Combination() {
|
||||
history.squash(history.size() - historySize);
|
||||
}
|
||||
|
||||
@@ -564,7 +564,12 @@ bool TextBox::isEditable() const {
|
||||
}
|
||||
|
||||
bool TextBox::isEdited() const {
|
||||
return history->size() != 0 || !historian->isSynced();
|
||||
return history->size() != editedHistorySize || !historian->isSynced();
|
||||
}
|
||||
|
||||
void TextBox::setUnedited() {
|
||||
historian->sync();
|
||||
editedHistorySize = history->size();
|
||||
}
|
||||
|
||||
size_t TextBox::getSelectionStart() const {
|
||||
@@ -575,7 +580,6 @@ size_t TextBox::getSelectionEnd() const {
|
||||
return selectionEnd;
|
||||
}
|
||||
|
||||
|
||||
void TextBox::setOnEditStart(runnable oneditstart) {
|
||||
onEditStart = oneditstart;
|
||||
}
|
||||
@@ -849,7 +853,12 @@ void TextBox::keyPressed(keycode key) {
|
||||
if (editable) {
|
||||
performEditingKeyboardEvents(key);
|
||||
}
|
||||
if (Events::pressed(keycode::LEFT_CONTROL)) {
|
||||
if (Events::pressed(keycode::LEFT_CONTROL) && key != keycode::LEFT_CONTROL) {
|
||||
if (controlCombinationsHandler) {
|
||||
if (controlCombinationsHandler(static_cast<int>(key))) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Copy selected text to clipboard
|
||||
if (key == keycode::C || key == keycode::X) {
|
||||
std::string text = util::wstr2str_utf8(getSelection());
|
||||
@@ -963,6 +972,10 @@ void TextBox::setTextValidator(wstringchecker validator) {
|
||||
this->validator = std::move(validator);
|
||||
}
|
||||
|
||||
void TextBox::setOnControlCombination(key_handler handler) {
|
||||
this->controlCombinationsHandler = std::move(handler);
|
||||
}
|
||||
|
||||
void TextBox::setFocusedColor(glm::vec4 color) {
|
||||
this->focusedColor = color;
|
||||
}
|
||||
@@ -999,6 +1012,7 @@ void TextBox::setText(const std::wstring& value) {
|
||||
input.erase(std::remove(input.begin(), input.end(), '\r'), input.end());
|
||||
historian->reset();
|
||||
history->clear();
|
||||
editedHistorySize = 0;
|
||||
refreshSyntax();
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace gui {
|
||||
LabelCache rawTextCache;
|
||||
std::shared_ptr<ActionsHistory> history;
|
||||
std::unique_ptr<TextBoxHistorian> historian;
|
||||
int editedHistorySize = 0;
|
||||
protected:
|
||||
glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f};
|
||||
glm::vec4 invalidColor {0.1f, 0.05f, 0.03f, 1.0f};
|
||||
@@ -33,6 +34,7 @@ namespace gui {
|
||||
wstringconsumer subconsumer = nullptr;
|
||||
/// @brief Text validator returning boolean value
|
||||
wstringchecker validator = nullptr;
|
||||
key_handler controlCombinationsHandler = nullptr;
|
||||
/// @brief Function called on focus
|
||||
runnable onEditStart = nullptr;
|
||||
/// @brief Function called on up arrow pressed
|
||||
@@ -117,6 +119,8 @@ namespace gui {
|
||||
/// @param validator std::wstring consumer returning boolean
|
||||
virtual void setTextValidator(wstringchecker validator);
|
||||
|
||||
virtual void setOnControlCombination(key_handler handler);
|
||||
|
||||
virtual void setFocusedColor(glm::vec4 color);
|
||||
virtual glm::vec4 getFocusedColor() const;
|
||||
|
||||
@@ -205,6 +209,7 @@ namespace gui {
|
||||
virtual bool isEditable() const;
|
||||
|
||||
virtual bool isEdited() const;
|
||||
virtual void setUnedited();
|
||||
|
||||
virtual void setPadding(glm::vec4 padding);
|
||||
glm::vec4 getPadding() const;
|
||||
|
||||
@@ -493,6 +493,13 @@ static std::shared_ptr<UINode> read_text_box(
|
||||
reader.getFilename()
|
||||
));
|
||||
}
|
||||
if (element.has("oncontrolkey")) {
|
||||
textbox->setOnControlCombination(scripting::create_key_handler(
|
||||
reader.getEnvironment(),
|
||||
element.attr("oncontrolkey").getText(),
|
||||
reader.getFilename()
|
||||
));
|
||||
}
|
||||
if (auto onUpPressed = create_runnable(reader, element, "onup")) {
|
||||
textbox->setOnUpPressed(onUpPressed);
|
||||
}
|
||||
|
||||
@@ -553,6 +553,13 @@ static void p_set_editable(UINode* node, lua::State* L, int idx) {
|
||||
box->setEditable(lua::toboolean(L, idx));
|
||||
}
|
||||
}
|
||||
static void p_set_edited(UINode* node, lua::State* L, int idx) {
|
||||
if (auto box = dynamic_cast<TextBox*>(node)) {
|
||||
if (!lua::toboolean(L, idx)) {
|
||||
box->setUnedited();
|
||||
}
|
||||
}
|
||||
}
|
||||
static void p_set_line_numbers(UINode* node, lua::State* L, int idx) {
|
||||
if (auto box = dynamic_cast<TextBox*>(node)) {
|
||||
box->setShowLineNumbers(lua::toboolean(L, idx));
|
||||
@@ -675,6 +682,7 @@ static int l_gui_setattr(lua::State* L) {
|
||||
{"hint", p_set_hint},
|
||||
{"text", p_set_text},
|
||||
{"editable", p_set_editable},
|
||||
{"edited", p_set_edited},
|
||||
{"lineNumbers", p_set_line_numbers},
|
||||
{"syntax", p_set_syntax},
|
||||
{"markup", p_set_markup},
|
||||
|
||||
@@ -36,6 +36,28 @@ static lua::State* process_callback(
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
key_handler scripting::create_key_handler(
|
||||
const scriptenv& env, const std::string& src, const std::string& file
|
||||
) {
|
||||
return [=](int code) {
|
||||
if (auto L = process_callback(env, src, file)) {
|
||||
int top = lua::gettop(L);
|
||||
if (lua::isfunction(L, -1)) {
|
||||
lua::pushinteger(L, code);
|
||||
lua::call_nothrow(L, 1);
|
||||
}
|
||||
int returned = lua::gettop(L) - top + 1;
|
||||
if (returned) {
|
||||
bool x = lua::toboolean(L, -1);
|
||||
lua::pop(L, returned);
|
||||
return x;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
wstringconsumer scripting::create_wstring_consumer(
|
||||
const scriptenv& env, const std::string& src, const std::string& file
|
||||
) {
|
||||
|
||||
@@ -17,6 +17,12 @@ namespace scripting {
|
||||
const std::string& file = "[string]"
|
||||
);
|
||||
|
||||
key_handler create_key_handler(
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
const std::string& file = "[string]"
|
||||
);
|
||||
|
||||
wstringconsumer create_wstring_consumer(
|
||||
const scriptenv& env,
|
||||
const std::string& src,
|
||||
|
||||
Reference in New Issue
Block a user