From 699d8ce2bbb1bf854a0e674beb96a9f33665a3de Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 27 Oct 2024 21:04:32 +0300 Subject: [PATCH 1/7] add 'sub-consumer' to textbox --- doc/en/xml-ui-layouts.md | 1 + doc/ru/xml-ui-layouts.md | 1 + res/layouts/pages/settings_controls.xml | 2 +- src/graphics/ui/elements/TextBox.cpp | 24 ++++++++++++++++++++---- src/graphics/ui/elements/TextBox.hpp | 7 +++++++ src/graphics/ui/gui_xml.cpp | 7 +++++++ 6 files changed, 37 insertions(+), 5 deletions(-) diff --git a/doc/en/xml-ui-layouts.md b/doc/en/xml-ui-layouts.md index 61451b80..ac657e44 100644 --- a/doc/en/xml-ui-layouts.md +++ b/doc/en/xml-ui-layouts.md @@ -99,6 +99,7 @@ Inner text - initially entered text - `placeholder` - placeholder text (used if the text field is empty) - `supplier` - text supplier (called every frame) - `consumer` - lua function that receives the entered text. Called only when input is complete +- `sub-consumer` - lua function-receiver of the input text. Called during text input or deletion. - `autoresize` - automatic change of element size (default - false). Does not affect font size. - `multiline` - allows display of multiline text. - `text-wrap` - allows automatic text wrapping (works only with multiline: "true") diff --git a/doc/ru/xml-ui-layouts.md b/doc/ru/xml-ui-layouts.md index 16007a2e..bdf82f1b 100644 --- a/doc/ru/xml-ui-layouts.md +++ b/doc/ru/xml-ui-layouts.md @@ -100,6 +100,7 @@ - `placeholder` - текст подстановки (используется если текстовое поле пусто) - `supplier` - поставщик текста (вызывается каждый кадр) - `consumer` - lua функция-приемник введенного текста. Вызывается только при завершении ввода +- `sub-consumer` - lua функция-приемник вводимого текста. Вызывается во время ввода или удаления текста. - `autoresize` - автоматическое изменение размера элемента (по-умолчанию - false). Не влияет на размер шрифта. - `multiline` - разрешает отображение многострочного текста. - `text-wrap` - разрешает автоматический перенос текста (работает только при multiline: "true") diff --git a/res/layouts/pages/settings_controls.xml b/res/layouts/pages/settings_controls.xml index 678b129f..30927037 100644 --- a/res/layouts/pages/settings_controls.xml +++ b/res/layouts/pages/settings_controls.xml @@ -5,7 +5,7 @@ consumer='change_sensitivity'> - + diff --git a/src/graphics/ui/elements/TextBox.cpp b/src/graphics/ui/elements/TextBox.cpp index 12c59508..128d3ea6 100644 --- a/src/graphics/ui/elements/TextBox.cpp +++ b/src/graphics/ui/elements/TextBox.cpp @@ -170,7 +170,9 @@ void TextBox::paste(const std::wstring& text) { input.erase(std::remove(input.begin(), input.end(), '\r'), input.end()); refreshLabel(); setCaret(caret + text.length()); - validate(); + if (validate()) { + onInput(); + } } /// @brief Remove part of the text and move caret to start of the part @@ -470,6 +472,12 @@ void TextBox::stepDefaultUp(bool shiftPressed, bool breakSelection) { } } +void TextBox::onInput() { + if (subconsumer) { + subconsumer(input); + } +} + void TextBox::performEditingKeyboardEvents(keycode key) { bool shiftPressed = Events::pressed(keycode::LEFT_SHIFT); bool breakSelection = getSelectionLength() != 0 && !shiftPressed; @@ -480,19 +488,23 @@ void TextBox::performEditingKeyboardEvents(keycode key) { } input = input.substr(0, caret-1) + input.substr(caret); setCaret(caret-1); - validate(); + if (validate()) { + onInput(); + } } } else if (key == keycode::DELETE) { if (!eraseSelected() && caret < input.length()) { input = input.substr(0, caret) + input.substr(caret + 1); - validate(); + if (validate()) { + onInput(); + } } } else if (key == keycode::ENTER) { if (multiline) { paste(L"\n"); } else { defocus(); - if (validate() && consumer) { + if (validate()) { consumer(label->getText()); } } @@ -591,6 +603,10 @@ void TextBox::setTextConsumer(wstringconsumer consumer) { this->consumer = std::move(consumer); } +void TextBox::setTextSubConsumer(wstringconsumer consumer) { + this->subconsumer = std::move(consumer); +} + void TextBox::setTextValidator(wstringchecker validator) { this->validator = std::move(validator); } diff --git a/src/graphics/ui/elements/TextBox.hpp b/src/graphics/ui/elements/TextBox.hpp index 376244cb..c7898307 100644 --- a/src/graphics/ui/elements/TextBox.hpp +++ b/src/graphics/ui/elements/TextBox.hpp @@ -17,6 +17,7 @@ namespace gui { std::wstring placeholder; wstringsupplier supplier = nullptr; wstringconsumer consumer = nullptr; + wstringconsumer subconsumer = nullptr; wstringchecker validator = nullptr; runnable onEditStart = nullptr; runnable onUpPressed; @@ -65,6 +66,8 @@ namespace gui { void performEditingKeyboardEvents(keycode key); void refreshLabel(); + + void onInput(); public: TextBox( std::wstring placeholder, @@ -79,6 +82,10 @@ namespace gui { /// @param consumer std::wstring consumer function virtual void setTextConsumer(wstringconsumer consumer); + /// @brief Sub-consumer called while editing text + /// @param consumer std::wstring consumer function + virtual void setTextSubConsumer(wstringconsumer consumer); + /// @brief Text validator called while text editing and returns true if /// text is valid /// @param validator std::wstring consumer returning boolean diff --git a/src/graphics/ui/gui_xml.cpp b/src/graphics/ui/gui_xml.cpp index 215be1ef..777ee5bc 100644 --- a/src/graphics/ui/gui_xml.cpp +++ b/src/graphics/ui/gui_xml.cpp @@ -355,6 +355,13 @@ static std::shared_ptr readTextBox(UiXmlReader& reader, const xml::xmlel reader.getFilename() )); } + if (element->has("sub-consumer")) { + textbox->setTextSubConsumer(scripting::create_wstring_consumer( + reader.getEnvironment(), + element->attr("sub-consumer").getText(), + reader.getFilename() + )); + } if (element->has("supplier")) { textbox->setTextSupplier(scripting::create_wstring_supplier( reader.getEnvironment(), From 3e86d49a7bb0bb6b882bb85800354c3cdcc88106 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 27 Oct 2024 21:32:30 +0300 Subject: [PATCH 2/7] add utf8.upper, utf8.lower --- doc/en/scripting/builtins/libutf8.md | 6 ++++++ doc/ru/scripting/builtins/libutf8.md | 6 ++++++ src/logic/scripting/lua/libs/libutf8.cpp | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/doc/en/scripting/builtins/libutf8.md b/doc/en/scripting/builtins/libutf8.md index e7801f97..db8b31c5 100644 --- a/doc/en/scripting/builtins/libutf8.md +++ b/doc/en/scripting/builtins/libutf8.md @@ -18,4 +18,10 @@ utf8.codepoint(chars: str) -> int -- Returns a substring from position startchar to endchar inclusive utf8.sub(text: str, startchar: int, [optional] endchar: int) -> str + +-- Converts a string to uppercase +utf8.upper(text: str) -> str + +-- Converts a string to lowercase +utf8.lower(text: str) -> str ``` diff --git a/doc/ru/scripting/builtins/libutf8.md b/doc/ru/scripting/builtins/libutf8.md index b29bb403..50d64e72 100644 --- a/doc/ru/scripting/builtins/libutf8.md +++ b/doc/ru/scripting/builtins/libutf8.md @@ -18,4 +18,10 @@ utf8.codepoint(chars: str) -> int -- Возвращает подстроку от позиции startchar до endchar включительно utf8.sub(text: str, startchar: int, [опционально] endchar: int) -> str + +-- Переводит строку в вверхний регистр +utf8.upper(text: str) -> str + +-- Переводит строку в нижний регистр +utf8.lower(text: str) -> str ``` diff --git a/src/logic/scripting/lua/libs/libutf8.cpp b/src/logic/scripting/lua/libs/libutf8.cpp index 182f6295..23283ab4 100644 --- a/src/logic/scripting/lua/libs/libutf8.cpp +++ b/src/logic/scripting/lua/libs/libutf8.cpp @@ -63,11 +63,29 @@ static int l_sub(lua::State* L) { return lua::pushstring(L, util::u32str2str_utf8(string.substr(start, end))); } +static int l_upper(lua::State* L) { + auto string = util::str2u32str_utf8(lua::require_string(L, 1)); + for (auto& c : string) { + c = std::towupper(c); + } + return lua::pushstring(L, util::u32str2str_utf8(string)); +} + +static int l_lower(lua::State* L) { + auto string = util::str2u32str_utf8(lua::require_string(L, 1)); + for (auto& c : string) { + c = std::towlower(c); + } + return lua::pushstring(L, util::u32str2str_utf8(string)); +} + const luaL_Reg utf8lib[] = { {"tobytes", lua::wrap}, {"tostring", lua::wrap}, {"length", lua::wrap}, {"codepoint", lua::wrap}, {"sub", lua::wrap}, + {"upper", lua::wrap}, + {"lower", lua::wrap}, {NULL, NULL} }; From 329d4903d818998cf53d1c60b9962bbc76559f46 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 27 Oct 2024 21:39:46 +0300 Subject: [PATCH 3/7] replace string.lower, string.upper with utf8 versions --- res/scripts/stdmin.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/res/scripts/stdmin.lua b/res/scripts/stdmin.lua index e38a39ef..9a955fff 100644 --- a/res/scripts/stdmin.lua +++ b/res/scripts/stdmin.lua @@ -159,6 +159,9 @@ function string.trim_left(s, char) return string.match(s, "^" .. char .. "*(.+)$") or s end +string.lower = utf8.lower +string.upper = utf8.upper + local meta = getmetatable("") function meta:__index(key) From bb239c0f4928dd3167355b09c45a3b701f7be156 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 27 Oct 2024 21:44:15 +0300 Subject: [PATCH 4/7] add missing include --- src/logic/scripting/lua/libs/libutf8.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/logic/scripting/lua/libs/libutf8.cpp b/src/logic/scripting/lua/libs/libutf8.cpp index 23283ab4..13956dc1 100644 --- a/src/logic/scripting/lua/libs/libutf8.cpp +++ b/src/logic/scripting/lua/libs/libutf8.cpp @@ -1,6 +1,7 @@ #include "api_lua.hpp" #include +#include #include "../lua_custom_types.hpp" #include "util/stringutil.hpp" From 2bf6ca9b7acef4343821dfbbe73a8b24c97b8461 Mon Sep 17 00:00:00 2001 From: ChancellorIkseew Date: Mon, 28 Oct 2024 04:47:40 +1000 Subject: [PATCH 5/7] add cubic interpolation type --- .../lua/usertypes/lua_type_heightmap.cpp | 4 +- src/maths/Heightmap.cpp | 68 ++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/logic/scripting/lua/usertypes/lua_type_heightmap.cpp b/src/logic/scripting/lua/usertypes/lua_type_heightmap.cpp index cdefa4f0..a59986ac 100644 --- a/src/logic/scripting/lua/usertypes/lua_type_heightmap.cpp +++ b/src/logic/scripting/lua/usertypes/lua_type_heightmap.cpp @@ -229,8 +229,10 @@ static int l_resize(lua::State* L) { uint height = touinteger(L, 3); auto interpName = tostring(L, 4); auto interpolation = InterpolationType::NEAREST; - if (!std::strcmp(interpName, "linear")) { + if (std::strcmp(interpName, "linear") == 0) { interpolation = InterpolationType::LINEAR; + } else if (std::strcmp(interpName, "cubic") == 0) { + interpolation = InterpolationType::CUBIC; } heightmap->getHeightmap()->resize(width, height, interpolation); } diff --git a/src/maths/Heightmap.cpp b/src/maths/Heightmap.cpp index 8db33f6b..7bf45c05 100644 --- a/src/maths/Heightmap.cpp +++ b/src/maths/Heightmap.cpp @@ -50,7 +50,73 @@ static inline float sample_at( return a00 + a10*tx + a01*ty + a11*tx*ty; } - // TODO: implement CUBIC (Bicubic) interpolation + case InterpolationType::CUBIC: { + float b1 = + ((tx - 1) * (tx - 2) * (tx + 1) * (ty - 1) * (ty - 2) * (ty + 1)) / 4; + float b2 = + (tx * (tx + 1) * (tx - 2) * (ty - 1) * (ty - 2) * (ty + 1)) / -4; + float b3 = + (tx * (tx + 1) * (tx - 2) * (ty - 1) * (ty - 2) * (ty + 1)) / -4; + float b4 = + (tx * (tx + 1) * (tx + 2) * ty * (ty + 1) * (ty - 2)) / 4; + float b5 = + (tx * (tx - 1) * (tx - 2) * (ty - 1) * (ty - 2) * (ty + 1)) / -12; + float b6 = + ((tx - 1) * (tx - 2) * (tx + 1) * ty * (ty - 1) * (ty - 2)) / -12; + float b7 = + (tx * (tx - 1) * (tx - 2) * ty * (ty + 1) * (ty - 2)) / 12; + float b8 = + (tx * (tx + 1) * (tx - 2) * ty * (ty - 1) * (ty - 2)) / 12; + float b9 = + (tx * (tx - 1) * (tx + 1) * (ty - 1) * (ty - 2) * (ty + 1)) / 12; + float b10 = + ((tx - 1) * (tx - 2) * (tx + 1) * ty * (ty - 1) * (ty + 1)) /12; + float b11 = + (tx * (tx - 1) * (tx - 2) * ty * (ty + 1) * (ty - 2)) / 36; + float b12 = + (tx * (tx - 1) * (tx + 1) * ty * (ty + 1) * (ty - 2)) / -12; + float b13 = + (tx * (tx + 1) * (tx - 2) * ty * (ty - 1) * (ty + 1)) / -12; + float b14 = + (tx * (tx - 1) * (tx + 1) * ty * (ty - 1) * (ty - 2)) / -36; + float b15 = + (tx * (tx - 1) * (tx - 2) * ty * (ty - 1) * (ty + 1)) / -36; + float b16 = + (tx * (tx - 1) * (tx + 1) * ty * (ty - 1) * (ty + 1)) / 36; + + + float a1 = b1 * val; + float a2 = b2 * sample_at(buffer, width, ix, iy + 1 < height ? iy + 1 : iy); + float a3 = b3 * sample_at(buffer, width, ix + 1 < width ? ix + 1 : ix, iy); + float a4 = b4 * sample_at(buffer, width, + ix + 1 < width ? ix + 1 : ix, iy + 1 < height ? iy + 1 : iy); + float a5 = b5 * sample_at(buffer, width, ix, iy > 1 ? iy - 1 : iy); + float a6 = b6 * sample_at(buffer, width, ix > 1 ? ix - 1 : ix, iy); + float a7 = b7 * sample_at(buffer, width, + ix + 1 < width ? ix + 1 : ix, iy > 1 ? iy - 1 : iy); + float a8 = b8 * sample_at(buffer, width, + ix > 1 ? ix - 1 : ix, iy + 1 < height ? iy + 1 : iy); + float a9 = b9 * sample_at(buffer, width, + ix, iy + 2 < height ? iy + 2 : iy); + float a10 = b10 * sample_at(buffer, width, + ix + 2 < width ? ix + 2 : ix, iy); + float a11 = b11 * sample_at( buffer, width, + ix > 1 ? ix - 1 : ix, iy > 1 ? iy - 1 : iy); + float a12 = b12 * sample_at(buffer, width, + ix + 1 < width ? ix + 1 : ix, iy + 2 < height ? iy + 2 : iy); + float a13 = b13 * sample_at(buffer, width, + ix + 2 < width ? ix + 2 : ix, iy + 1 < height ? iy + 1 : iy); + float a14 = b14 * sample_at(buffer, width, + ix > 1 ? ix - 1 : ix, iy + 2 < height ? iy + 2 : iy); + float a15 = b15 * sample_at(buffer, width, + ix + 2 < width ? ix + 2 : ix, iy > 1 ? iy - 1 : iy); + float a16 = b16 * sample_at(buffer, width, + ix + 2 < width ? ix + 2 : ix, iy + 2 < height ? iy + 2 : iy); + + return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + + a9 + a10 + a11 + a12 + a13 + a14 + a15 + a16; + } + default: throw std::runtime_error("interpolation type is not implemented"); } From 8ae6d89ed4a95ac0375892432e6d23ee16df0399 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 27 Oct 2024 21:54:25 +0300 Subject: [PATCH 6/7] fix msvc build again --- src/logic/scripting/lua/libs/libutf8.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logic/scripting/lua/libs/libutf8.cpp b/src/logic/scripting/lua/libs/libutf8.cpp index 13956dc1..127ea464 100644 --- a/src/logic/scripting/lua/libs/libutf8.cpp +++ b/src/logic/scripting/lua/libs/libutf8.cpp @@ -1,7 +1,7 @@ #include "api_lua.hpp" #include -#include +#include #include "../lua_custom_types.hpp" #include "util/stringutil.hpp" From 176267e7ac31c31a7bf5bf63aaa8323c790daefb Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 27 Oct 2024 22:05:57 +0300 Subject: [PATCH 7/7] fix msvc build again --- src/logic/scripting/lua/libs/libutf8.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/logic/scripting/lua/libs/libutf8.cpp b/src/logic/scripting/lua/libs/libutf8.cpp index 127ea464..3512624f 100644 --- a/src/logic/scripting/lua/libs/libutf8.cpp +++ b/src/logic/scripting/lua/libs/libutf8.cpp @@ -1,7 +1,7 @@ #include "api_lua.hpp" #include -#include +#include #include "../lua_custom_types.hpp" #include "util/stringutil.hpp"