Compare commits

...

2 Commits

Author SHA1 Message Date
swagtoy
b8959a0bac Settings formspec demonstration
This is really hacky, but it demonstrates that everything works (kinda)
2024-06-27 12:27:58 -04:00
swagtoy
4b74e8d071 Move pause formspec events to Lua 2024-06-27 10:58:14 -04:00
7 changed files with 201 additions and 67 deletions

@ -88,9 +88,29 @@ Menu/inventory open:
end
function core.show_pause_menu(is_singleplayer, is_touchscreen, address)
minetest.show_formspec("MT_PAUSE_MENU", menu_formspec(is_singleplayer, is_touchscreen, address))
minetest.show_formspec("builtin:MT_PAUSE_MENU", menu_formspec(is_singleplayer, is_touchscreen, address))
end
core.register_on_formspec_input(function(formname, fields)
if formname ~= "builtin:MT_PAUSE_MENU" then return end
if fields.btn_continue then
core.unpause()
elseif fields.btn_key_config then
core.key_config() -- Don't want this
elseif fields.btn_change_password then
core.change_password()
elseif fields.btn_settings then
core.show_settings()
elseif fields.btn_exit_menu then
core.disconnect()
elseif fields.btn_exit_os then
core.exit_to_os()
end
return
end)
local scriptpath = core.get_builtin_path()
local path = scriptpath.."mainmenu"..DIR_DELIM.."settings"
@ -107,12 +127,134 @@ function dialog_create(name, spec, buttonhandler, eventhandler)
minetest.show_formspec(name, spec({}))
end
load(true, false)
function core.show_settings(page_id)
if not page_id then
show_settings_client_formspec("MT_PAUSE_MENU_SETTINGS", {})
else
show_settings_client_formspec("MT_PAUSE_MENU_SETTINGS", {page_id = page_id})
local settings_data = {}
settings_data.data = {
leftscroll = 0,
query = "",
rightscroll = 0,
components = {},
page_id = "accessibility"
}
core.register_on_formspec_input(function(formname, fields)
if formname ~= "builtin:MT_PAUSE_MENU_SETTINGS" then return true end
--local this = data
--buttonhandler(settings_data, fields)
local dialogdata = settings_data.data
dialogdata.leftscroll = core.explode_scrollbar_event(fields.leftscroll).value or dialogdata.leftscroll
dialogdata.rightscroll = core.explode_scrollbar_event(fields.rightscroll).value or dialogdata.rightscroll
dialogdata.query = fields.search_query
local update = false
if fields.back then
this:delete()
return true
end
if fields.show_technical_names ~= nil then
local value = core.is_yes(fields.show_technical_names)
core.settings:set_bool("show_technical_names", value)
write_settings_early()
update = true
--return true
end
if fields.show_advanced ~= nil then
local value = core.is_yes(fields.show_advanced)
core.settings:set_bool("show_advanced", value)
write_settings_early()
core.show_settings()
update = true
end
-- enable_touch is a checkbox in a setting component. We handle this
-- setting differently so we can hide/show pages using the next if-statement
if fields.enable_touch ~= nil then
local value = core.is_yes(fields.enable_touch)
core.settings:set_bool("enable_touch", value)
write_settings_early()
core.show_settings()
update = true
end
if fields.show_advanced ~= nil or fields.enable_touch ~= nil then
local suggested_page_id = update_filtered_pages(dialogdata.query)
dialogdata.components = nil
if not filtered_page_by_id[dialogdata.page_id] then
dialogdata.leftscroll = 0
dialogdata.rightscroll = 0
dialogdata.page_id = suggested_page_id
end
return true
end
if fields.search or fields.key_enter_field == "search_query" then
dialogdata.components = nil
dialogdata.leftscroll = 0
dialogdata.rightscroll = 0
dialogdata.page_id = update_filtered_pages(dialogdata.query)
return true
end
if fields.search_clear then
dialogdata.query = ""
dialogdata.components = nil
dialogdata.leftscroll = 0
dialogdata.rightscroll = 0
dialogdata.page_id = update_filtered_pages("")
return true
end
for _, page in ipairs(all_pages) do
if fields["page_" .. page.id] then
dialogdata.page_id = page.id
dialogdata.components = nil
dialogdata.rightscroll = 0
core.show_settings()
return true
end
end
if dialogdata.components then
for i, comp in ipairs(dialogdata.components) do
if comp.on_submit and comp:on_submit(fields, this) then
write_settings_early()
core.show_settings()
-- Clear components so they regenerate
--dialogdata.components = nil
return true
end
if comp.setting and fields["reset_" .. i] then
core.settings:remove(comp.setting.name)
write_settings_early()
core.show_settings()
-- Clear components so they regenerate
--dialogdata.components = nil
return true
end
end
end
if update then
core.show_settings()
end
return false
end)
load(true, false)
--settings_data.data.page_id = update_filtered_pages("")
function core.show_settings()
show_settings_client_formspec("builtin:MT_PAUSE_MENU_SETTINGS", settings_data.data)
core.unpause()
end

@ -23,13 +23,13 @@ local shadows_component = dofile(core.get_mainmenu_path() .. DIR_DELIM ..
"settings" .. DIR_DELIM .. "shadows_component.lua")
local loaded = false
local full_settings
full_settings = {}
local info_icon_path = core.formspec_escape(defaulttexturedir .. "settings_info.png")
local reset_icon_path = core.formspec_escape(defaulttexturedir .. "settings_reset.png")
local all_pages = {}
local page_by_id = {}
local filtered_pages = all_pages
local filtered_page_by_id = page_by_id
all_pages = {}
page_by_id = {}
filtered_pages = all_pages
filtered_page_by_id = page_by_id
local function get_setting_info(name)
@ -288,7 +288,7 @@ local function filter_page_content(page, query_keywords)
end
local function update_filtered_pages(query)
function update_filtered_pages(query)
filtered_pages = {}
filtered_page_by_id = {}
@ -633,11 +633,13 @@ function write_settings_early()
end
local function buttonhandler(this, fields)
function buttonhandler(this, fields)
local dialogdata = this.data
dialogdata.leftscroll = core.explode_scrollbar_event(fields.leftscroll).value or dialogdata.leftscroll
dialogdata.rightscroll = core.explode_scrollbar_event(fields.rightscroll).value or dialogdata.rightscroll
dialogdata.query = fields.search_query
minetest.log(dump(fields))
if fields.back then
this:delete()
@ -709,6 +711,7 @@ local function buttonhandler(this, fields)
end
end
if dialogdata.components then
for i, comp in ipairs(dialogdata.components) do
if comp.on_submit and comp:on_submit(fields, this) then
write_settings_early()
@ -726,6 +729,7 @@ local function buttonhandler(this, fields)
return true
end
end
end
return false
end

@ -149,48 +149,7 @@ struct LocalFormspecHandler : public TextDest
void gotText(const StringMap &fields)
{
if (m_formname == "MT_PAUSE_MENU") {
if (fields.find("btn_sound") != fields.end()) {
g_gamecallback->changeVolume();
return;
}
else if (fields.find("btn_key_config") != fields.end()) {
g_gamecallback->keyConfig();
return;
}
else if (fields.find("btn_settings") != fields.end()) {
g_gamecallback->showSettings();
}
else if (fields.find("btn_exit_menu") != fields.end()) {
g_gamecallback->disconnect();
return;
}
else if (fields.find("btn_exit_os") != fields.end()) {
g_gamecallback->exitToOS();
#ifndef __ANDROID__
RenderingEngine::get_raw_device()->closeDevice();
#endif
return;
}
else if (fields.find("btn_change_password") != fields.end()) {
g_gamecallback->changePassword();
return;
}
else {
g_gamecallback->unpause();
return;
}
return;
}
if (m_formname == "MT_PAUSE_MENU_SETTINGS")
if (m_formname == "MT_PAUSE_MENU_SETTINGS" && false)
{
// Loop through settings
for (auto i : fields)
@ -201,7 +160,7 @@ struct LocalFormspecHandler : public TextDest
}
if (i.first.rfind("page_", 0) == 0)
{
m_client->getScript()->show_settings(i.first.substr(5));
//m_client->getScript()->show_settings(i.first.substr(5));
}
//std::cout << "Setting " << i.first << " set!" << std::endl;

@ -310,7 +310,7 @@ void ScriptApiClient::show_pause_menu(bool is_singleplayer, bool is_touchscreen,
lua_pop(L, 1);
}
void ScriptApiClient::show_settings(const std::string& page)
void ScriptApiClient::show_settings()
{
SCRIPTAPI_PRECHECKHEADER
@ -319,14 +319,7 @@ void ScriptApiClient::show_settings(const std::string& page)
lua_getglobal(L, "core");
lua_getfield(L, -1, "show_settings");
if (page != "")
{
lua_pushstring(L, page.c_str());
}
else
lua_pushnil(L);
PCALL_RES(lua_pcall(L, 1, 0, error_handler));
PCALL_RES(lua_pcall(L, 0, 0, error_handler));
lua_pop(L, 1);
}

@ -61,7 +61,7 @@ class ScriptApiClient : virtual public ScriptApiBase
bool on_inventory_open(Inventory *inventory);
void show_pause_menu(bool is_singleplayer, bool is_touchscreen, const std::string& server_address);
void show_settings(const std::string& page = "");
void show_settings();
void setEnv(ClientEnvironment *env);
};

@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "client/client.h"
#include "client/clientevent.h"
#include "client/sound.h"
#include "client/renderingengine.h"
#include "client/clientenvironment.h"
#include "common/c_content.h"
#include "common/c_converter.h"
@ -178,6 +179,29 @@ int ModApiClient::l_disconnect(lua_State *L)
return 1;
}
// unpause()
int ModApiClient::l_unpause(lua_State *L)
{
g_gamecallback->unpause();
//lua_pushboolean(L, true);
return 1;
}
int ModApiClient::l_exit_to_os(lua_State *L)
{
g_gamecallback->exitToOS();
#ifndef __ANDROID__
RenderingEngine::get_raw_device()->closeDevice();
#endif
return 1;
}
int ModApiClient::l_key_config(lua_State *L)
{
g_gamecallback->keyConfig();
return 1;
}
// gettext(text)
int ModApiClient::l_gettext(lua_State *L)
{
@ -366,6 +390,9 @@ void ModApiClient::Initialize(lua_State *L, int top)
API_FCT(gettext);
API_FCT(get_node_or_nil);
API_FCT(disconnect);
API_FCT(unpause);
API_FCT(exit_to_os);
API_FCT(key_config);
API_FCT(get_meta);
API_FCT(get_server_info);
API_FCT(get_item_def);

@ -50,6 +50,15 @@ class ModApiClient : public ModApiBase
// show_formspec(name, formspec)
static int l_show_formspec(lua_State *L);
// unpause()
static int l_unpause(lua_State *L);
// exit_to_os()
static int l_exit_to_os(lua_State *L);
// key_config()
static int l_key_config(lua_State *L);
// send_respawn()
static int l_send_respawn(lua_State *L);