Merge branch 'main' into item-models

This commit is contained in:
MihailRis 2024-10-27 22:20:58 +03:00
commit 9ffc45cbdc
12 changed files with 141 additions and 7 deletions

View File

@ -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
```

View File

@ -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")

View File

@ -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
```

View File

@ -100,6 +100,7 @@
- `placeholder` - текст подстановки (используется если текстовое поле пусто)
- `supplier` - поставщик текста (вызывается каждый кадр)
- `consumer` - lua функция-приемник введенного текста. Вызывается только при завершении ввода
- `sub-consumer` - lua функция-приемник вводимого текста. Вызывается во время ввода или удаления текста.
- `autoresize` - автоматическое изменение размера элемента (по-умолчанию - false). Не влияет на размер шрифта.
- `multiline` - разрешает отображение многострочного текста.
- `text-wrap` - разрешает автоматический перенос текста (работает только при multiline: "true")

View File

@ -5,7 +5,7 @@
consumer='change_sensitivity'>
</trackbar>
<panel id='search_panel' size='380,60' padding='2' interval='1' color='#0000004C'>
<textbox id='search_textbox' multiline='false' size='300,20' consumer='function(x) refresh_search() end'></textbox>
<textbox id='search_textbox' multiline='false' size='300,20' sub-consumer='function(x) refresh_search() end'></textbox>
</panel>
<panel id='bindings_panel' size='380,204' padding='2' interval='1' max-length='300' color='#0000004C'>
<!-- content is generated in script -->

View File

@ -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)

View File

@ -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);
}

View File

@ -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

View File

@ -355,6 +355,13 @@ static std::shared_ptr<UINode> 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(),

View File

@ -1,6 +1,7 @@
#include "api_lua.hpp"
#include <vector>
#include <cwctype>
#include "../lua_custom_types.hpp"
#include "util/stringutil.hpp"
@ -63,11 +64,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<l_encode>},
{"tostring", lua::wrap<l_decode>},
{"length", lua::wrap<l_length>},
{"codepoint", lua::wrap<l_codepoint>},
{"sub", lua::wrap<l_sub>},
{"upper", lua::wrap<l_upper>},
{"lower", lua::wrap<l_lower>},
{NULL, NULL}
};

View File

@ -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);
}

View File

@ -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");
}