1
0
Fork 0

MainMenu improvements and cleanup (#141)

Co-authored-by: luk3yx <luk3yx@users.noreply.github.com>
(cherry picked from commit 75452a9e9a)
merge-requests/1/head
Maksym H 2023-08-04 14:33:33 +03:00 committed by mckaygerhard
parent 365c63e99e
commit 9db28e0daa
20 changed files with 174 additions and 190 deletions

View File

@ -26,6 +26,9 @@
-- element.getFormspec() returns formspec of tabview --
--------------------------------------------------------------------------------
local fmt = string.format
local tconcat = table.concat
--------------------------------------------------------------------------------
local function add_tab(self,tab)
assert(tab.size == nil or (type(tab.size) == table and
@ -78,78 +81,149 @@ end
--------------------------------------------------------------------------------
local tpath = defaulttexturedir_esc .. "gui" .. DIR_DELIM_esc
local function get_side_menu(self, selected_tab)
if #self.side_buttons == 0 then return "" end
local function add_side_menu(self, fs, selected_tab, x, fs_h, darken)
if #self.side_buttons == 0 then return end
local side_menu_h = #self.side_buttons * 1.2 + 0.2
local bg_y = 2.35 - side_menu_h / 2
local fs = {
"background9[12.13,", bg_y, ";0.9,", side_menu_h, ";", tpath,
"side_menu.png;false;30]"
}
local side_menu_h = #self.side_buttons * 1.41 + 0.14
local bg_y = fs_h / 2 - side_menu_h / 2
fs[#fs + 1] = fmt("image[%s,%s;1.15,%s;%sside_menu.png;30]", x, bg_y, side_menu_h, tpath)
for i, btn in ipairs(self.side_buttons) do
local y = bg_y - 1.15 + 1.2 * i
local y = bg_y - 1.31 + 1.41 * i
if i > 1 then
fs[#fs + 1] = "image[12.15," .. y - 0.04 .. ";0.9,0.06;" ..
tpath .. "side_menu_divider.png]"
fs[#fs + 1] = fmt("image[%s,%s;0.9,0.06;%sside_menu_divider.png]", x + 0.1, y - 0.06, tpath)
end
local btn_name = self.name .. "_side_" .. i
fs[#fs + 1] = "style[" .. btn_name .. ";bgimg="
fs[#fs + 1] = fmt("style[%s;bgimg=", btn_name)
local texture_prefix = btn.texture_prefix or btn.tab_name
if btn.tab_name and btn.tab_name == selected_tab then
local tab_name = btn.tab_name or btn.tab_name_selected
local texture_prefix = btn.texture_prefix or tab_name
if tab_name and tab_name == selected_tab then
fs[#fs + 1] = btn.texture_selected or tpath .. texture_prefix .. "_menu_selected.png"
else
fs[#fs + 1] = btn.texture or tpath .. texture_prefix .. "_menu.png"
fs[#fs + 1] = ";bgimg_hovered="
fs[#fs + 1] = btn.texture_hover or tpath .. texture_prefix .. "_menu_hover.png]"
fs[#fs + 1] = btn.texture_hover or tpath .. texture_prefix .. "_menu_hover.png"
end
fs[#fs + 1] = "]"
fs[#fs + 1] = "image_button[12.1," .. y .. ";1,1.3;;" .. btn_name .. ";;true;false]"
fs[#fs + 1] = "tooltip[" .. btn_name .. ";" .. btn.tooltip .. "]"
fs[#fs + 1] = fmt("image_button[%s,%s;1,1.35;;%s;;true;false]", x + 0.09, y, btn_name)
fs[#fs + 1] = fmt("tooltip[%s;%s]", btn_name, btn.tooltip)
end
return table.concat(fs)
if darken then
fs[#fs + 1] = fmt("style[cancel;bgimg=%sside_menu_darken.png;bgimg_middle=30]", tpath)
fs[#fs + 1] = fmt("image_button[%s,%s;1.15,%s;;cancel;;true;false]", x, bg_y, side_menu_h)
-- Reset "cancel" button styling
fs[#fs + 1] = "style[cancel;bgimg=;bgimg_middle=]"
end
end
local function get_formspec(self)
local formspec = ""
if not self.hidden and (self.parent == nil or not self.parent.hidden) then
local name = self.tablist[self.last_tab_index].name
if self.parent == nil then
local tsize = self.tablist[self.last_tab_index].tabsize or
{width=self.width, height=self.height}
formspec = formspec ..
string.format("size[%f,%f,%s]",tsize.width + 2,tsize.height + 1,
dump(self.fixed_size)) ..
"bgcolor[#0000]" ..
"listcolors[#000;#000;#000;#dff6f5;#302c2e]" ..
"container[1,1]" ..
"background9[-0.2,-1.26;" .. tsize.width + 0.4 .. "," ..
tsize.height + 1.75 .. ";" .. defaulttexturedir_esc ..
"bg_common.png;false;40]" ..
get_side_menu(self, name)
end
-- formspec = formspec .. self:tab_header()
formspec = formspec .. self:button_header() ..
self.tablist[self.last_tab_index].get_formspec(
self,
name,
self.tablist[self.last_tab_index].tabdata,
self.tablist[self.last_tab_index].tabsize
)
if self.parent == nil then
formspec = formspec .. "container_end[]"
--------------------------------------------------------------------------------
local function add_button_header(self, fs, fs_w)
local visible_tabs = {}
for _, tab in ipairs(self.tablist) do
if not tab.hidden and tab.caption ~= "" then
visible_tabs[#visible_tabs + 1] = tab
end
end
return formspec
local x = 1.5
local w = (fs_w - 0.53) / #visible_tabs
for i = 1, #visible_tabs do
local caption = visible_tabs[i].caption
local side = i == 1 and "left" or (i < #visible_tabs and "middle" or "right")
local btn_name = self.name .. "_" .. i
if i == math.abs(self.last_tab_index) then
side = side .. "_pressed"
end
fs[#fs + 1] = btn_style(btn_name, side)
fs[#fs + 1] = fmt("style[%s;content_offset=0;font_size=+1]", btn_name)
fs[#fs + 1] = fmt("image_button[%s,0.25;%s,0.9;;%s;%s;true;false]",
x, w + 0.03, btn_name, caption)
x = x + w
end
end
--------------------------------------------------------------------------------
local function get_formspec(self, popup_w, popup_h, popup_fs)
if (self.hidden or (self.parent and self.parent.hidden)) and not popup_fs then
return ""
end
local fs = {}
local current_tab = self.tablist[self.last_tab_index]
local content, prepend_override = current_tab.get_formspec(self,
current_tab.name, current_tab.tabdata, current_tab.size)
local real_coords = current_tab.formspec_version and current_tab.formspec_version > 1
local w, h
if prepend_override then
fs[#fs + 1] = prepend_override
elseif not self.parent then
fs[#fs + 1] = "formspec_version[4]"
local s = current_tab.tabsize or self
w, h = s.width, s.height
if not real_coords then
w = w * 1.25 + 0.6
h = h * 1.15 + 0.9
end
fs[#fs + 1] = fmt("size[%s,%s]", w + 2.5, h + 1.15)
-- Background
fs[#fs + 1] = "bgcolor[;neither]"
fs[#fs + 1] = "listcolors[#000;#000;#000;#dff6f5;#302c2e]"
fs[#fs + 1] = fmt("background9[1.25,0;%s,%s;%sbg_common.png;false;32]",
w, h + 1.15, defaulttexturedir)
add_button_header(self, fs, w)
-- This container isn't ideal for real_coordinates formspecs but
-- keeps compatibility with existing code
fs[#fs + 1] = "container[1,1]"
fs[#fs + 1] = "real_coordinates[false]"
end
fs[#fs + 1] = content
if not prepend_override and not self.parent then
-- Make sure that real_coordinates is enabled
fs[#fs + 1] = "real_coordinates[true]"
fs[#fs + 1] = "container_end[]"
-- Darken background
if popup_fs then
-- This styling gets reset in add_side_menu
fs[#fs + 1] = fmt("style[cancel;bgimg=%sbg_darken.png;bgimg_middle=32]", tpath)
fs[#fs + 1] = fmt("image_button[1.25,0;%s,%s;;cancel;;false;false]",
w, h + 1.15)
end
-- Add the side menu after the darkened background
add_side_menu(self, fs, current_tab.name, w + 1.175, h + 1.15, popup_fs)
-- Draw the popup
if popup_fs then
fs[#fs + 1] = fmt("container[%s,%s]", (w + 2.5 - popup_w) / 2,
(h + 1.15 - popup_h) / 2)
fs[#fs + 1] = fmt("style[popup_bg;bgimg=%sbg_common.png;bgimg_middle=32]",
defaulttexturedir)
fs[#fs + 1] = fmt("image_button[0,0;%s,%s;;popup_bg;;false;false]",
popup_w, popup_h)
fs[#fs + 1] = popup_fs
fs[#fs + 1] = "container_end[]"
end
-- Disable real_coordinates again in case there are other UI elements
-- (like the game switcher in buttonbar.lua)
fs[#fs + 1] = "real_coordinates[false]"
end
return tconcat(fs)
end
local set_tab_by_name
@ -207,69 +281,6 @@ local function handle_events(self,event)
return false
end
--------------------------------------------------------------------------------
--[[local function tab_header(self)
local captions = {}
for i = 1, #self.tablist do
captions[i] = self.tablist[i].caption
end
local toadd = table.concat(captions, ",")
return string.format("tabheader[%f,%f;%s;%s;%i;true;false]",
self.header_x, self.header_y, self.name, toadd,
math.max(self.last_tab_index, 1))
end]]
--------------------------------------------------------------------------------
local function button_header(self)
local visible_tabs = {}
-- local btn_widths = {}
-- local total_width = 0
for i, tab in ipairs(self.tablist) do
if not tab.hidden and tab.caption ~= "" then
visible_tabs[#visible_tabs + 1] = tab
-- local w = math.max(utf8.len(core.get_translated_string(tab.caption)), 10)
-- btn_widths[#visible_tabs] = w
-- total_width = total_width + w
end
end
local toadd = ""
-- local coords_per_char = 12 / total_width
-- The formspec is 15.4875 "real" coordinates wide
-- local x = (12.39 - total_width) / 2 - 0.3
local x = -0.1
local w = 12 / #visible_tabs
for i = 1, #visible_tabs do
local caption = visible_tabs[i].caption
-- local w = btn_widths[i] * coords_per_char
local side = "middle"
if i == 1 then
side = "left"
elseif i == #visible_tabs then
side = "right"
end
local btn_name = self.name .. "_" .. i
if i == math.abs(self.last_tab_index) then
side = side .. "_pressed"
end
toadd = toadd ..
btn_style(btn_name, side) ..
"style[" .. btn_name .. ";content_offset=0]" ..
"image_button[" .. x .. ",-1.1;" .. w + 0.22 .. ",0.9;;" ..
btn_name .. ";" .. caption .. ";true;false]"
x = x + w
end
return toadd
end
--------------------------------------------------------------------------------
local function switch_to_tab(self, index)
--first call on_change for tab to leave
@ -387,8 +398,6 @@ local tabview_metatable = {
set_fixed_size =
function(self,state) self.fixed_size = state end,
add_side_button = add_side_button,
-- tab_header = tab_header,
button_header = button_header,
handle_tab_buttons = handle_tab_buttons
}

View File

@ -457,7 +457,8 @@ local confirm_overwrite = {}
function confirm_overwrite.get_formspec()
local package = confirm_overwrite.package
return "size[11.5,4.5]" ..
return "size[11.5,4.5]bgcolor[#0000]" ..
"background9[0,0;0,0;" .. defaulttexturedir_esc .. "bg_common.png;true;40]" ..
"label[2,2;" ..
fgettext("\"$1\" already exists. Would you like to overwrite it?", package.name) .. "]"..
btn_style("install", "red") ..

View File

@ -47,6 +47,7 @@ local function version_info_formspec(data)
image[4.9,0;2.5,2.5;%slogo.png]
style[msg;content_offset=0]
image_button[1,2;10,0.8;;msg;%s;false;false]
%s
hypertext[1.3,2.6;10,2;;<center>%s</center>]
%s
button[2,4.5;4,0.8;version_check_remind;%s]
@ -55,6 +56,7 @@ local function version_info_formspec(data)
]]):format(
defaulttexturedir_esc,
esc(data.title),
scrollbar_style("hypertext", true),
esc(changes),
btn_style("version_check_remind", "yellow"),
fgettext("Cancel"),

View File

@ -74,7 +74,7 @@ local func = loadfile(basepath .. DIR_DELIM .. "hosting" .. DIR_DELIM .. "init.l
--------------------------------------------------------------------------------
local function main_event_handler(tabview, event)
if event == "MenuQuit" then
if event == "MenuQuit" and PLATFORM ~= "iOS" then
core.close()
end
return true
@ -111,6 +111,33 @@ function menudata.init_tabs()
-- Create main tabview
local tv_main = tabview_create("maintab", {x = 12, y = 5.4}, {x = 0.1, y = 0})
tv_main:add_side_button({
tooltip = fgettext("Browse online content"),
tab_name_selected = "content",
is_open_cdb = true,
on_click = function(this)
if #pkgmgr.games > 1 or (pkgmgr.games[1] and pkgmgr.games[1].id ~= "default") then
this:set_tab("content")
else
local dialog = create_store_dlg()
dialog:set_parent(this)
this:hide()
dialog:show()
end
end,
})
tv_main:add_side_button({
tooltip = fgettext("Settings"),
tab_name = "settings",
})
tv_main:add_side_button({
tooltip = fgettext("Credits"),
tab_name = "credits",
texture_prefix = "authors"
})
for i = 1, #pkgmgr.games do
if pkgmgr.games[i].id == "default" then
tv_main:add(tabs.local_default_game)
@ -152,17 +179,6 @@ function menudata.init_tabs()
end
end
tv_main:add_side_button({
tooltip = fgettext("Settings"),
tab_name = "settings",
})
tv_main:add_side_button({
tooltip = fgettext("Credits"),
tab_name = "credits",
texture_prefix = "authors"
})
ui.set_default("maintab")
check_new_version()

View File

@ -99,10 +99,13 @@ local function get_formspec(tabview, name, tabdata)
desc = info.description
end
local pkg_name = selected_pkg.name
pkg_name = (pkg_name:sub(1, 1)):upper() .. pkg_name:sub(2)
retval = retval ..
"image[5.5,0;3,2;" .. core.formspec_escape(modscreenshot) .. "]" ..
"image[5.5,0;3,2;" .. defaulttexturedir_esc .. "gui" .. DIR_DELIM_esc .. "cdb_img_corners.png;15]" ..
"label[8.25,0.6;" .. core.formspec_escape(selected_pkg.name) .. "]" ..
"label[8.25,0.6;" .. core.formspec_escape(pkg_name) .. "]" ..
"background9[5.6,2.3;6.2,2.4;" .. defaulttexturedir_esc .. "desc_bg.png;false;32]"
if selected_pkg.type == "mod" then
@ -150,8 +153,8 @@ local function get_formspec(tabview, name, tabdata)
end
end
retval = retval .. "textarea[5.85,2.2;6.35,2.9;;" ..
fgettext("Information:") .. ";" .. desc .. "]"
retval = retval .. scrollbar_style("textarea", true) ..
"textarea[5.83,2.23;6.33,2.89;;" .. fgettext("Information:") .. ";" .. desc .. "]"
if core.may_modify_path(selected_pkg.path) then
retval = retval ..

View File

@ -159,7 +159,9 @@ local function get_formspec(_, _, tab_data)
btn_style("switch_local_default") ..
"style[switch_local_default;fgimg=" .. defaulttexturedir_esc .. "switch_local_default.png;fgimg_hovered=" ..
defaulttexturedir_esc .. "switch_local_default_hover.png;padding=" .. (is_high_dpi() and -42 or -30) .. "]" ..
"image_button[10.6,-0.1;1.5,1.5;;switch_local_default;;true;false]"
"real_coordinates[true]" ..
"image_button[13.98,0.31;1.6,1.6;;switch_local_default;;true;false]" ..
"real_coordinates[false]"
end
local enable_server = core.settings:get_bool("enable_server")
@ -310,7 +312,7 @@ local function main_button_handler(this, fields, name, tab_data)
world.name ~= "" then
local index = menudata.worldlist:get_raw_index(selected)
local game = pkgmgr.find_by_gameid(world.gameid)
local delete_world_dlg = create_delete_world_dlg(world.name, index, game.name)
local delete_world_dlg = create_delete_world_dlg(world.name, index, game and game.name or world.gameid)
delete_world_dlg:set_parent(this)
this:hide()
delete_world_dlg:show()

View File

@ -17,14 +17,6 @@
local small_screen = (PLATFORM == "Android" or PLATFORM == "iOS") and not core.settings:get_bool("device_is_tablet")
local default_worlds = {
{name = "World 1", mg_name = "v7p", seed = "15823438331521897617"},
{name = "World 2", mg_name = "v7p", seed = "1841722166046826822"},
{name = "World 3", mg_name = "v7p", seed = "CC"},
{name = "World 4", mg_name = "valleys", seed = "8572"},
{name = "World 5 Flat", mg_name = "flat", seed = "2"}
}
local function menu_render_worldlist()
local retval = {}
@ -48,46 +40,7 @@ local function menu_render_worldlist()
return table.concat(retval, ",")
end
local function create_default_worlds()
if #menudata.worldlist:get_list() > 0 then return end
local _, gameindex = pkgmgr.find_by_gameid("default")
if not gameindex or gameindex == 0 then return end
-- Preserve the old map seed and mapgen values
local old_map_seed = core.settings:get("fixed_map_seed")
local old_mapgen = core.settings:get("mg_name")
-- Create the worlds
for _, world in ipairs(default_worlds) do
core.settings:set("fixed_map_seed", world.seed)
core.settings:set("mg_name", world.mg_name)
core.create_world(world.name, gameindex)
end
-- Restore the old values
if old_map_seed then
core.settings:set("fixed_map_seed", old_map_seed)
else
core.settings:remove("fixed_map_seed")
end
if old_mapgen then
core.settings:set("mg_name", old_mapgen)
else
core.settings:remove("mg_name")
end
menudata.worldlist:refresh()
end
local checked_worlds = false
local function get_formspec(this)
-- Only check the worlds once (on restart)
if not checked_worlds then
create_default_worlds()
end
checked_worlds = true
local index = filterlist.get_current_index(menudata.worldlist,
tonumber(core.settings:get("mainmenu_last_selected_world")))
if not index or index < 1 then
@ -130,12 +83,7 @@ local function get_formspec(this)
"tableoptions[background=#0000;border=false]" ..
"tablecolumns[" .. image_column(fgettext("Creative mode")) .. ";text]" ..
scrollbar_style("sp_worlds") ..
"table[0,0;6.28,4.64;sp_worlds;" .. menu_render_worldlist() .. ";" .. index .. "]" ..
btn_style("switch_local") ..
"style[switch_local;fgimg=" .. defaulttexturedir_esc .. "switch_local.png;fgimg_hovered=" ..
defaulttexturedir_esc .. "switch_local_hover.png;padding=" .. (is_high_dpi() and -42 or -30) .. "]" ..
"image_button[10.6,-0.1;1.5,1.5;;switch_local;;true;false]"
"table[0,0;6.28,4.64;sp_worlds;" .. menu_render_worldlist() .. ";" .. index .. "]"
local enable_server = core.settings:get_bool("enable_server")
if enable_server then

View File

@ -94,6 +94,7 @@ local function get_formspec(tabview, name, tabdata)
end
if selected.description then
retval = retval ..
scrollbar_style("textarea", true) ..
"textarea[7.5,2.2;4.8,3;;" .. esc((gamedata.serverdescription or ""), true) .. ";]"
end
end

View File

@ -1862,7 +1862,7 @@ void GUIFormSpecMenu::parseHyperText(parserData *data, const std::string &elemen
spec.sound = style.get(StyleSpec::Property::SOUND, "");
GUIHyperText *e = new GUIHyperText(spec.flabel.c_str(), Environment,
data->current_parent, spec.fid, rect, m_client, m_tsrc);
data->current_parent, spec.fid, rect, m_client, m_tsrc, style);
e->drop();
m_fields.push_back(spec);

View File

@ -997,7 +997,7 @@ void TextDrawer::draw(const core::rect<s32> &clip_rect,
//! constructor
GUIHyperText::GUIHyperText(const wchar_t *text, IGUIEnvironment *environment,
IGUIElement *parent, s32 id, const core::rect<s32> &rectangle,
Client *client, ISimpleTextureSource *tsrc) :
Client *client, ISimpleTextureSource *tsrc, const StyleSpec &style) :
IGUIElement(EGUIET_ELEMENT, environment, parent, id, rectangle),
m_client(client), m_vscrollbar(nullptr),
m_drawer(text, client, environment, tsrc), m_text_scrollpos(0, 0)
@ -1019,6 +1019,7 @@ GUIHyperText::GUIHyperText(const wchar_t *text, IGUIEnvironment *environment,
m_vscrollbar = new GUIScrollBar(Environment, this, -1, rect, false, true);
m_vscrollbar->setVisible(false);
m_vscrollbar->setStyle(style, tsrc);
}
//! destructor

View File

@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include "config.h" // for USE_FREETYPE
#include "StyleSpec.h"
using namespace irr;
@ -201,7 +202,7 @@ public:
GUIHyperText(const wchar_t *text, gui::IGUIEnvironment *environment,
gui::IGUIElement *parent, s32 id,
const core::rect<s32> &rectangle, Client *client,
ISimpleTextureSource *tsrc);
ISimpleTextureSource *tsrc, const StyleSpec &style);
//! destructor
virtual ~GUIHyperText();

Binary file not shown.

After

Width:  |  Height:  |  Size: 172 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 822 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 812 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 812 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 152 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 816 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 810 B