Add open link button to marketing popup

This commit is contained in:
fed 2023-03-02 20:53:06 +01:00
parent d3f77f62c9
commit 66ee0c0c50
5 changed files with 108 additions and 52 deletions

View File

@ -12,42 +12,22 @@ LUI.onmenuopen("main_campaign", function(menu)
end
end)
local function makelink(element, link)
element:setHandleMouseMove(true)
element:setHandleMouseButton(true)
element:registerAnimationState("focused", {
color = {
r = 1,
g = 1,
b = 1
}
})
local entered = false
element:registerEventHandler("mouseenter", function()
if (not entered) then
Engine.PlaySound(CoD.SFX.MouseOver)
entered = true
end
element:animateToState("focused")
end)
element:registerEventHandler("mouseleave", function()
entered = false
element:animateToState("default")
end)
element:registerEventHandler("leftmousedown", function()
Engine.PlaySound(CoD.SFX.MouseClick)
game:openlink(link)
end)
LUI.common_menus.MarketingPopup.OnPopupAction = function(a1, a2)
local data = a1.popupData
if (type(data.link) == "string") then
game:openlink(data.link)
end
end
local marketingbase = LUI.MarketingPopup.Base
LUI.MarketingPopup.Base = function(a1, data, a3)
local element = marketingbase(a1, data, a3)
local haslink = data.popupAction ~= nil and game:islink(data.popupAction)
if (haslink) then
data.link = data.popupAction
data.popupAction = "depot"
end
local element = marketingbase(a1, data, a3)
local blur = element:getFirstDescendentById("generic_popup_screen_overlay_blur"):getNextSibling()
local parent = blur:getFirstChild():getNextSibling():getNextSibling():getNextSibling()
local image = parent:getFirstChild()
@ -56,6 +36,11 @@ LUI.MarketingPopup.Base = function(a1, data, a3)
local state = LUI.DeepCopy(image:getAnimationStateInC("default"))
local imagecontainer = LUI.UIStencilText.new(state)
local material = RegisterMaterial(data.image)
local helpertext = element:getFirstDescendentById("helper_text_text")
if (haslink and helpertext) then
helpertext:setText(Engine.Localize("@MENU_OPEN_LINK"))
end
local ratio = Engine.GetMaterialAspectRatio(material)
local width = 525

View File

@ -116,5 +116,8 @@
"LUA_MENU_SP_LOCATION_OILRIG": "LUA_MENU_SP_LOCATION_OILRIG",
"LUA_MENU_SP_LOCATION_ROADKILL": "LUA_MENU_SP_LOCATION_ROADKILL",
"LUA_MENU_SP_LOCATION_TRAINER": "LUA_MENU_SP_LOCATION_TRAINER",
"LUA_MENU_SP_LOCATION_MUSEUM": "LUA_MENU_SP_LOCATION_MUSEUM"
"LUA_MENU_SP_LOCATION_MUSEUM": "LUA_MENU_SP_LOCATION_MUSEUM",
"DEPOT_GO_TO_THE_DEPOT": "DEPOT_GO_TO_THE_DEPOT",
"MENU_OPEN_LINK": "Open link"
}

View File

@ -16,6 +16,8 @@ namespace motd
{
namespace
{
utils::concurrency::container<links_map_t> links;
utils::concurrency::container<nlohmann::json, std::recursive_mutex> marketing;
std::unordered_map<std::string, std::string> image_cache;
@ -91,22 +93,63 @@ namespace motd
}
}
void download_images()
void download_images(nlohmann::json& data)
{
marketing.access([&](nlohmann::json& data)
if (!data.is_object())
{
if (!data.is_object())
return;
}
download_motd_image(data);
download_featured_tabs_images(data);
}
void init_links(links_map_t& map)
{
map =
{
{"github", "https://github.com/fedddddd/h2-mod"},
{"donate", "https://www.paypal.com/donate/?hosted_button_id=LM5BA9UABEV4Q"},
{"credits_1", "https://github.com/momo5502"},
{"credits_2", "https://github.com/VladWinner"},
{"credits_3", "https://github.com/diamante0018"},
{"credits_4", "https://github.com/JariKCoding"},
{"credits_5", "https://github.com/netadr"},
{"credits_6", "https://github.com/Joelrau"},
{"credits_7", "https://github.com/xensik"},
{"credits_8", "https://github.com/ZoneTool/zonetool"},
};
}
void add_links(nlohmann::json& data)
{
links.access([&](links_map_t& map)
{
init_links(map);
if (!data.is_object() || !data["links"].is_object())
{
return;
}
download_motd_image(data);
download_featured_tabs_images(data);
for (const auto& [link, url] : data["links"].items())
{
if (!url.is_string())
{
continue;
}
map.insert(std::make_pair(link, url.get<std::string>()));
}
});
}
void init(bool load_images = true)
{
links.access([](links_map_t& map)
{
init_links(map);
});
marketing.access([&](nlohmann::json& data)
{
image_cache.clear();
@ -119,9 +162,11 @@ namespace motd
{
const auto& value = marketing_data.value();
data = nlohmann::json::parse(value);
add_links(data);
if (load_images)
{
download_images();
download_images(data);
}
}
catch (const std::exception& e)
@ -133,6 +178,14 @@ namespace motd
}
}
links_map_t get_links()
{
return links.access<links_map_t>([&](links_map_t& map)
{
return map;
});
}
int get_num_featured_tabs()
{
return marketing.access<nlohmann::json>([&](nlohmann::json& data)

View File

@ -2,6 +2,9 @@
namespace motd
{
using links_map_t = std::unordered_map<std::string, std::string>;
links_map_t get_links();
int get_num_featured_tabs();
nlohmann::json get_motd();
nlohmann::json get_featured_tab(const int index);

View File

@ -334,20 +334,7 @@ namespace ui_scripting
game_type["openlink"] = [](const game&, const std::string& name)
{
static std::unordered_map<std::string, std::string> links =
{
{"github", "https://github.com/fedddddd/h2-mod"},
{"donate", "https://www.paypal.com/donate/?hosted_button_id=LM5BA9UABEV4Q"},
{"credits_1", "https://github.com/momo5502"},
{"credits_2", "https://github.com/VladWinner"},
{"credits_3", "https://github.com/diamante0018"},
{"credits_4", "https://github.com/JariKCoding"},
{"credits_5", "https://github.com/netadr"},
{"credits_6", "https://github.com/Joelrau"},
{"credits_7", "https://github.com/xensik"},
{"credits_8", "https://github.com/ZoneTool/zonetool"},
};
const auto links = motd::get_links();
const auto link = links.find(name);
if (link == links.end())
{
@ -357,6 +344,31 @@ namespace ui_scripting
ShellExecuteA(nullptr, "open", link->second.data(), nullptr, nullptr, SW_SHOWNORMAL);
};
game_type["getlinkurl"] = [](const game&, const std::string& name)
-> script_value
{
const auto links = motd::get_links();
const auto link = links.find(name);
if (link == links.end())
{
return script_value();
}
return link->second;
};
game_type["islink"] = [](const game&, const std::string& name)
{
const auto links = motd::get_links();
const auto link = links.find(name);
if (link == links.end())
{
return false;
}
return true;
};
lua["string"]["escapelocalization"] = [](const std::string& str)
{
return "\x1F"s.append(str);