Server list changes

This commit is contained in:
Federico Cecchetto 2022-06-01 23:31:30 +02:00
parent 46454bae4a
commit 6898d6e842
2 changed files with 155 additions and 110 deletions

View File

@ -2,151 +2,189 @@ local Lobby = luiglobals.Lobby
local SystemLinkJoinMenu = LUI.mp_menus.SystemLinkJoinMenu local SystemLinkJoinMenu = LUI.mp_menus.SystemLinkJoinMenu
if (not SystemLinkJoinMenu) then if (not SystemLinkJoinMenu) then
return return
end end
game:addlocalizedstring("MENU_NUMPLAYERS", "Players") game:addlocalizedstring("MENU_NUMPLAYERS", "Players")
game:addlocalizedstring("MENU_PING", "Ping") game:addlocalizedstring("MENU_PING", "Ping")
local offsets = {10, 500, 950, 700, 1100} local columns = {
{
local columns = {"@MENU_HOST_NAME", "@MENU_MAP", "@MENU_NUMPLAYERS", "@MENU_TYPE1", "@MENU_PING"} offset = 10,
text = "@MENU_HOST_NAME",
dataindex = 0
},
{
offset = 500,
text = "@MENU_MAP",
dataindex = 1
},
{
offset = 700,
text = "@MENU_TYPE1",
dataindex = 3
},
{
offset = 950,
text = "@MENU_NUMPLAYERS",
dataindex = 2
},
{
offset = 1100,
text = "@MENU_PING",
dataindex = 4
}
}
function textlength(text, font, height) function textlength(text, font, height)
local _, _, width = luiglobals.GetTextDimensions(text, font, height) local _, _, width = luiglobals.GetTextDimensions(text, font, height)
return width return width
end end
function trimtext(text, font, height, maxwidth) function trimtext(text, font, height, maxwidth)
while (textlength(text, font, height) > maxwidth) do if (maxwidth < 0) then
text = text:sub(1, #text - 1) return text
end end
return text while (textlength(text, font, height) > maxwidth) do
text = text:sub(1, #text - 1)
end
return text
end end
SystemLinkJoinMenu.AddHeaderButton = function(menu, f12_arg1, width) SystemLinkJoinMenu.AddHeaderButton = function(menu, f12_arg1, width)
local state = CoD.CreateState(0, f12_arg1, nil, nil, CoD.AnchorTypes.TopLeft) local state = CoD.CreateState(0, f12_arg1, nil, nil, CoD.AnchorTypes.TopLeft)
state.width = width state.width = width
local element = LUI.UIElement.new(state) local element = LUI.UIElement.new(state)
local button = SystemLinkJoinMenu.CreateButton("header", 24) local button = SystemLinkJoinMenu.CreateButton("header", 24)
button:addElement(LUI.Divider.new(CoD.CreateState(nil, 0, nil, nil, CoD.AnchorTypes.TopLeftRight), 40, button:addElement(LUI.Divider.new(CoD.CreateState(nil, 0, nil, nil, CoD.AnchorTypes.TopLeftRight), 40,
LUI.Divider.Grey)) LUI.Divider.Grey))
button:makeNotFocusable() button:makeNotFocusable()
button:addElement(LUI.Divider.new(CoD.CreateState(nil, 0, nil, nil, CoD.AnchorTypes.BottomLeftRight), 40, button:addElement(LUI.Divider.new(CoD.CreateState(nil, 0, nil, nil, CoD.AnchorTypes.BottomLeftRight), 40,
LUI.Divider.Grey)) LUI.Divider.Grey))
local gettext = function(i) button.m_eventHandlers = {}
return Engine.Localize(columns[i])
end
for i = 1, #offsets do for i = 1, #columns do
SystemLinkJoinMenu.MakeText(button.textHolder, offsets[i], gettext(i), nil) SystemLinkJoinMenu.MakeText(button.textHolder, columns[i].offset, Engine.Localize(columns[i].text), nil)
end end
element:addElement(button) element:addElement(button)
menu:addElement(element) menu:addElement(element)
end end
SystemLinkJoinMenu.AddServerButton = function(menu, controller, index) SystemLinkJoinMenu.AddServerButton = function(menu, controller, index)
local button = SystemLinkJoinMenu.CreateButton(index or "header", 24) local button = SystemLinkJoinMenu.CreateButton(index or "header", 24)
button:makeFocusable() button:makeFocusable()
button.index = index button.index = index
button:addEventHandler("button_action", SystemLinkJoinMenu.OnJoinGame) button:addEventHandler("button_action", SystemLinkJoinMenu.OnJoinGame)
local gettext = function(i) local gettext = function(i)
local text = Lobby.GetServerData(controller, index, i - 1) local text = Lobby.GetServerData(controller, index, columns[i].dataindex)
return trimtext(text, CoD.TextSettings.TitleFontSmall.Font, 14, 400)
end
for i = 1, #offsets do local islast = not columns[i + 1]
SystemLinkJoinMenu.MakeText(button.textHolder, offsets[i], gettext(i), luiglobals.Colors.h1.medium_grey) local end_ = islast and 1130 or columns[i + 1].offset
end local maxlength = end_ - columns[i].offset
menu.list:addElement(button) if (maxlength < 0) then
return button maxlength = columns[i].offset - end_
end
if (not islast) then
maxlength = maxlength - 50
end
return trimtext(text, CoD.TextSettings.TitleFontSmall.Font, 14, maxlength)
end
for i = 1, #columns do
SystemLinkJoinMenu.MakeText(button.textHolder, columns[i].offset, gettext(i), luiglobals.Colors.h1.medium_grey)
end
menu.list:addElement(button)
return button
end end
SystemLinkJoinMenu.MakeText = function(menu, f5_arg1, text, color) SystemLinkJoinMenu.MakeText = function(menu, f5_arg1, text, color)
local state = CoD.CreateState(f5_arg1, nil, f5_arg1 + 200, nil, CoD.AnchorTypes.Left) local state = CoD.CreateState(f5_arg1, nil, f5_arg1 + 200, nil, CoD.AnchorTypes.Left)
state.font = CoD.TextSettings.TitleFontSmall.Font state.font = CoD.TextSettings.TitleFontSmall.Font
state.top = -6 state.top = -6
state.height = 14 state.height = 14
state.alignment = LUI.Alignment.Left state.alignment = nil
state.glow = LUI.GlowState.None state.glow = LUI.GlowState.None
state.color = color state.color = color
local el = LUI.UIText.new(state) local el = LUI.UIText.new(state)
el:registerAnimationState("focused", { el:registerAnimationState("focused", {
color = luiglobals.Colors.white color = luiglobals.Colors.white
}) })
el:registerEventHandler("focused", function(element, event) el:registerEventHandler("focused", function(element, event)
element:animateToState("focused", 0) element:animateToState("focused", 0)
end) end)
el:registerEventHandler("unfocused", function(element, event) el:registerEventHandler("unfocused", function(element, event)
element:animateToState("default", 0) element:animateToState("default", 0)
end) end)
el:setText(text) el:setText(text)
menu:addElement(el) menu:addElement(el)
end end
function menu_systemlink_join(f19_arg0, f19_arg1) function menu_systemlink_join(f19_arg0, f19_arg1)
local width = 1145 local width = 1145
local menu = LUI.MenuTemplate.new(f19_arg0, { local menu = LUI.MenuTemplate.new(f19_arg0, {
menu_title = "@PLATFORM_SYSTEM_LINK_TITLE", menu_title = "@PLATFORM_SYSTEM_LINK_TITLE",
menu_width = width, menu_width = width,
menu_top_indent = 20, menu_top_indent = 20,
disableDeco = true, disableDeco = true,
spacing = 1 spacing = 1
}) })
SystemLinkJoinMenu.AddHeaderButton(menu, 80, width) SystemLinkJoinMenu.AddHeaderButton(menu, 80, width)
SystemLinkJoinMenu.AddLowerCounter(menu, width) SystemLinkJoinMenu.AddLowerCounter(menu, width)
SystemLinkJoinMenu.UpdateCounterText(menu, nil) SystemLinkJoinMenu.UpdateCounterText(menu, nil)
Lobby.BuildServerList(Engine.GetFirstActiveController()) Lobby.BuildServerList(Engine.GetFirstActiveController())
menu.list:registerEventHandler(LUI.UIScrollIndicator.UpdateEvent, function(element, event) menu.list:registerEventHandler(LUI.UIScrollIndicator.UpdateEvent, function(element, event)
SystemLinkJoinMenu.UpdateCounterText(menu, event) SystemLinkJoinMenu.UpdateCounterText(menu, event)
end) end)
SystemLinkJoinMenu.UpdateGameList(menu) SystemLinkJoinMenu.UpdateGameList(menu)
menu:registerEventHandler("updateGameList", SystemLinkJoinMenu.UpdateGameList) menu:registerEventHandler("updateGameList", SystemLinkJoinMenu.UpdateGameList)
menu:addElement(LUI.UITimer.new(250, "updateGameList"))
LUI.ButtonHelperText.ClearHelperTextObjects(menu.help, { LUI.ButtonHelperText.ClearHelperTextObjects(menu.help, {
side = "all" side = "all"
}) })
menu:AddHelp({ menu:AddHelp({
name = "add_button_helper_text", name = "add_button_helper_text",
button_ref = "button_alt1", button_ref = "button_alt1",
helper_text = Engine.Localize("@MENU_SB_TOOLTIP_BTN_REFRESH"), helper_text = Engine.Localize("@MENU_SB_TOOLTIP_BTN_REFRESH"),
side = "right", side = "right",
clickable = true, clickable = true,
priority = -1000 priority = -1000
}, function(f21_arg0, f21_arg1) }, function(f21_arg0, f21_arg1)
SystemLinkJoinMenu.RefreshServers(f21_arg0, f21_arg1, menu) SystemLinkJoinMenu.RefreshServers(f21_arg0, f21_arg1, menu)
end) end)
menu:AddHelp({ menu:AddHelp({
name = "add_button_helper_text", name = "add_button_helper_text",
button_ref = "button_action", button_ref = "button_action",
helper_text = Engine.Localize("@MENU_JOIN_GAME1"), helper_text = Engine.Localize("@MENU_JOIN_GAME1"),
side = "left", side = "left",
clickable = false, clickable = false,
priority = -1000 priority = -1000
}, nil, nil, true) }, nil, nil, true)
menu:AddBackButton() menu:AddBackButton()
Lobby.RefreshServerList(Engine.GetFirstActiveController()) Lobby.RefreshServerList(Engine.GetFirstActiveController())
return menu return menu
end end
LUI.MenuBuilder.m_types_build["menu_systemlink_join"] = menu_systemlink_join LUI.MenuBuilder.m_types_build["menu_systemlink_join"] = menu_systemlink_join

View File

@ -1,24 +1,26 @@
#include <std_include.hpp> #include <std_include.hpp>
#include "loader/component_loader.hpp" #include "loader/component_loader.hpp"
#include "server_list.hpp" #include "server_list.hpp"
#include "localized_strings.hpp" #include "localized_strings.hpp"
#include "network.hpp" #include "network.hpp"
#include "scheduler.hpp" #include "scheduler.hpp"
#include "party.hpp" #include "party.hpp"
#include "console.hpp"
#include "command.hpp"
#include "game/game.hpp" #include "game/game.hpp"
#include "game/ui_scripting/execution.hpp"
#include <utils/cryptography.hpp> #include <utils/cryptography.hpp>
#include <utils/string.hpp> #include <utils/string.hpp>
#include <utils/hook.hpp> #include <utils/hook.hpp>
#include "console.hpp"
#include "command.hpp"
namespace server_list namespace server_list
{ {
namespace namespace
{ {
const int server_limit = 18; const int server_limit = 100;
struct server_info struct server_info
{ {
@ -107,11 +109,6 @@ namespace server_list
int ui_feeder_count() int ui_feeder_count()
{ {
std::lock_guard<std::mutex> _(mutex); std::lock_guard<std::mutex> _(mutex);
if (update_server_list)
{
update_server_list = false;
return 0;
}
const auto count = static_cast<int>(servers.size()); const auto count = static_cast<int>(servers.size());
const auto index = get_page_base_index(); const auto index = get_page_base_index();
const auto diff = count - index; const auto diff = count - index;
@ -272,6 +269,15 @@ namespace server_list
lui_open_menu_hook.invoke<void>(controllerIndex, menuName, isPopup, isModal, isExclusive); lui_open_menu_hook.invoke<void>(controllerIndex, menuName, isPopup, isModal, isExclusive);
} }
void check_refresh()
{
if (update_server_list)
{
update_server_list = false;
ui_scripting::notify("updateGameList", {});
}
}
} }
bool sl_key_event(const int key, const int down) bool sl_key_event(const int key, const int down)
@ -430,6 +436,7 @@ namespace server_list
}), true); }), true);
scheduler::loop(do_frame_work, scheduler::pipeline::main); scheduler::loop(do_frame_work, scheduler::pipeline::main);
scheduler::loop(check_refresh, scheduler::pipeline::lui, 10ms);
network::on("getServersResponse", [](const game::netadr_s& target, const std::string_view& data) network::on("getServersResponse", [](const game::netadr_s& target, const std::string_view& data)
{ {