Receive events from each key pressed in field and textarea (by @kilbith)

master
MoNTE48 2019-07-02 00:37:34 +02:00
parent c2622c1b75
commit 26bff3450e
5 changed files with 48 additions and 6 deletions

View File

@ -1914,6 +1914,7 @@ examples.
* Fields are a set height, but will be vertically centred on `h`
* Position and size units are inventory slots
* `name` is the name of the field as returned in fields to `on_receive_fields`
* `!name` to receive an event from each key pressed
* `label`, if not blank, will be text printed on the top left above the field
* `default` is the default value of the field
* `default` may contain variable references such as `${text}'` which

View File

@ -263,9 +263,14 @@ local function get_creative_formspec(player_name, start_i, pagenum, page, pagema
if page == "all" then
local inv = player_inventory[player_name] or {}
local filter = inv.filter or ""
formspec = formspec .. "field_close_on_enter[search;false]"..
"field[5.31,1.27;4.0,0.75;search;;"..filter.."]"..
"image_button[9.14,0.93;0.81,0.82;creative_search.png;creative_search;;;false]"
if minetest.is_singleplayer() then
formspec = formspec .. "field_close_on_enter[search;false]"..
"field[5.31,1.27;4.0,0.75;!search;;"..filter.."]"
else
formspec = formspec .. "field_close_on_enter[search;false]"..
"field[5.31,1.27;4.0,0.75;search;;"..filter.."]"..
"image_button[9.14,0.93;0.81,0.82;creative_search.png;creative_search;;;false]"
end
end
if pagenum ~= nil then
formspec = formspec .. "p"..tostring(pagenum)

View File

@ -156,7 +156,11 @@ minetest.register_node("signs:sign", {
end,
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", "field[text;;${sign_text}]")
if minetest.is_singleplayer() then
meta:set_string("formspec", "field[!text;Enter your text:;${sign_text}]")
else
meta:set_string("formspec", "field[text;Enter your text:;${sign_text}]")
end
end,
on_destruct = function(pos)
for _, obj in pairs(minetest.get_objects_inside_radius(pos, 0.5)) do
@ -219,7 +223,11 @@ minetest.register_node("signs:wall_sign", {
drop = "signs:sign",
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("formspec", "field[text;;${sign_text}]")
if minetest.is_singleplayer() then
meta:set_string("formspec", "field[!text;Enter your text:;${sign_text}]")
else
meta:set_string("formspec", "field[text;Enter your text:;${sign_text}]")
end
end,
on_destruct = function(pos)
for _, obj in pairs(minetest.get_objects_inside_radius(pos, 0.5)) do

View File

@ -1006,6 +1006,10 @@ void GUIFormSpecMenu::parseSimpleField(parserData* data,
std::string label = parts[1];
std::string default_val = parts[2];
bool is_dynamic = (name.length() > 0 && name[0] == '!');
if (is_dynamic)
name = name.substr(1);
core::rect<s32> rect;
if(data->explicit_size)
@ -1032,6 +1036,8 @@ void GUIFormSpecMenu::parseSimpleField(parserData* data,
258+m_fields.size()
);
spec.is_dynamic = is_dynamic;
if (name == "")
{
// spec field id to 0, this stops submit searching for a value that isn't there
@ -1094,6 +1100,10 @@ void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>&
std::string label = parts[3];
std::string default_val = parts[4];
bool is_dynamic = (name.length() > 0 && name[0] == '!');
if (is_dynamic)
name = name.substr(1);
MY_CHECKPOS(type,0);
MY_CHECKGEOM(type,1);
@ -1135,6 +1145,8 @@ void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>&
258+m_fields.size()
);
spec.is_dynamic = is_dynamic;
if (name == "")
{
// spec field id to 0, this stops submit searching for a value that isn't there
@ -3817,6 +3829,20 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
}
}
}
if (event.GUIEvent.EventType == gui::EGET_EDITBOX_CHANGED) {
if (event.GUIEvent.Caller->getID() > 257) {
for (GUIFormSpecMenu::FieldSpec &s : m_fields) {
if (s.ftype == f_Unknown && s.is_dynamic &&
s.fid == event.GUIEvent.Caller->getID()) {
s.send = true;
acceptInput();
s.send = false;
}
}
return true;
}
}
if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
if (event.GUIEvent.Caller->getID() > 257) {

View File

@ -214,7 +214,8 @@ class GUIFormSpecMenu : public GUIModalMenu
fid(id),
send(false),
ftype(f_Unknown),
is_exit(false)
is_exit(false),
is_dynamic(false)
{
}
@ -225,6 +226,7 @@ class GUIFormSpecMenu : public GUIModalMenu
bool send;
FormspecFieldType ftype;
bool is_exit;
bool is_dynamic;
core::rect<s32> rect;
};