Cleanup ui scripts

This commit is contained in:
Federico Cecchetto 2022-03-20 00:57:07 +01:00
parent 769d2c74cb
commit 2f5eaa8298
5 changed files with 267 additions and 266 deletions

View File

@ -5,88 +5,88 @@ local extrawidth = 50
local extraheight = 40 local extraheight = 40
LUI.MenuBuilder.m_types_build["SystemInfo"] = function (f6_arg0, f6_arg1) LUI.MenuBuilder.m_types_build["SystemInfo"] = function (f6_arg0, f6_arg1)
local f6_local0 = LUI.MenuTemplate.spMenuOffset local f6_local0 = LUI.MenuTemplate.spMenuOffset
local title = "LUA_MENU_SYSTEM_INFO_CAPS" local title = "LUA_MENU_SYSTEM_INFO_CAPS"
local f6_local2 = false local f6_local2 = false
local f6_local3 = 0 local f6_local3 = 0
local menu = LUI.MenuTemplate.new(f6_arg0, { local menu = LUI.MenuTemplate.new(f6_arg0, {
menu_title = title, menu_title = title,
menu_top_indent = f6_local0 + f6_local3, menu_top_indent = f6_local0 + f6_local3,
showSelectButton = false, showSelectButton = false,
skipAnim = f6_local2 skipAnim = f6_local2
}) })
menu:setClass(LUI.SystemInfo) menu:setClass(LUI.SystemInfo)
menu:PopulateMissingProps(f6_arg1) menu:PopulateMissingProps(f6_arg1)
menu:ValidateProps(f6_arg1) menu:ValidateProps(f6_arg1)
menu.id = "systemInfo_id" menu.id = "systemInfo_id"
local f6_local5 = 300 local f6_local5 = 300
local f6_local6 = LUI.MenuTemplate.ListTop + f6_local0 local f6_local6 = LUI.MenuTemplate.ListTop + f6_local0
local f6_local7 = f6_arg1.menu_height local f6_local7 = f6_arg1.menu_height
if not f6_local7 then if not f6_local7 then
f6_local7 = f6_local5 f6_local7 = f6_local5
end end
f6_local7 = f6_local7 + f6_local6 - extraheight f6_local7 = f6_local7 + f6_local6 - extraheight
local f6_local9 = luiglobals.GenericMenuDims.OptionMenuWidth + 100 local f6_local9 = luiglobals.GenericMenuDims.OptionMenuWidth + 100
local f6_local10 = menu.properties local f6_local10 = menu.properties
local topoffset2 = LUI.MenuTemplate.ListTop + LUI.MenuTemplate.spMenuOffset local topoffset2 = LUI.MenuTemplate.ListTop + LUI.MenuTemplate.spMenuOffset
local decobox = LUI.MenuBuilder.BuildRegisteredType("h1_box_deco", { local decobox = LUI.MenuBuilder.BuildRegisteredType("h1_box_deco", {
decoTopOffset = topoffset2 - topoffset + 15, decoTopOffset = topoffset2 - topoffset + 15,
decoBottomOffset = -f6_local7, decoBottomOffset = -f6_local7,
decoRightOffset = -665 + extrawidth decoRightOffset = -665 + extrawidth
}) })
menu:addElement(decobox) menu:addElement(decobox)
local decoleft = CoD.CreateState(0, 0.5, 8, 0.5, CoD.AnchorTypes.TopLeft) local decoleft = CoD.CreateState(0, 0.5, 8, 0.5, CoD.AnchorTypes.TopLeft)
decoleft.color = luiglobals.Colors.h1.light_grey decoleft.color = luiglobals.Colors.h1.light_grey
decobox:addElement(LUI.UILine.new(decoleft)) decobox:addElement(LUI.UILine.new(decoleft))
local decoright = CoD.CreateState(0, 0.5, -8, 0.5, CoD.AnchorTypes.TopRight) local decoright = CoD.CreateState(0, 0.5, -8, 0.5, CoD.AnchorTypes.TopRight)
decoright.color = luiglobals.Colors.h1.light_grey decoright.color = luiglobals.Colors.h1.light_grey
decobox:addElement(LUI.UILine.new(decoright)) decobox:addElement(LUI.UILine.new(decoright))
local element = LUI.UIVerticalList.new({ local element = LUI.UIVerticalList.new({
leftAnchor = true, leftAnchor = true,
rightAnchor = true, rightAnchor = true,
topAnchor = true, topAnchor = true,
bottomAnchor = true, bottomAnchor = true,
left = spacing, left = spacing,
right = 100, right = 100,
top = topoffset2 + 15, top = topoffset2 + 15,
bottom = 0, bottom = 0,
spacing = spacing * 0.8 spacing = spacing * 0.8
}) })
element.id = "systemInfoList_id" element.id = "systemInfoList_id"
menu.vlist = element menu.vlist = element
menu:addElement(element) menu:addElement(element)
local optionmenuwidth = luiglobals.GenericMenuDims.OptionMenuWidth local optionmenuwidth = luiglobals.GenericMenuDims.OptionMenuWidth
luiglobals.GenericMenuDims.OptionMenuWidth = optionmenuwidth + extrawidth luiglobals.GenericMenuDims.OptionMenuWidth = optionmenuwidth + extrawidth
menu:AddInfo(Engine.Localize("MENU_SYSINFO_VERSION"), function() menu:AddInfo(Engine.Localize("MENU_SYSINFO_VERSION"), function()
return Engine.GetBuildNumber() return Engine.GetBuildNumber()
end) end)
menu:AddInfo(Engine.Localize("MENU_SYSINFO_CUSTOMER_SUPPORT_LINK"), function() menu:AddInfo(Engine.Localize("MENU_SYSINFO_CUSTOMER_SUPPORT_LINK"), function()
return Engine.Localize("MENU_SYSINFO_CUSTOMER_SUPPORT_URL") return Engine.Localize("MENU_SYSINFO_CUSTOMER_SUPPORT_URL")
end) end)
menu:AddInfo(Engine.Localize("MENU_SYSINFO_DONATION_LINK"), function() menu:AddInfo(Engine.Localize("MENU_SYSINFO_DONATION_LINK"), function()
return Engine.Localize("MENU_SYSINFO_DONATION_URL") return Engine.Localize("MENU_SYSINFO_DONATION_URL")
end) end)
luiglobals.GenericMenuDims.OptionMenuWidth = optionmenuwidth luiglobals.GenericMenuDims.OptionMenuWidth = optionmenuwidth
menu:AddBackButton() menu:AddBackButton()
menu:registerEventHandler("menu_close", LUI.SystemInfo.LeaveMenu) menu:registerEventHandler("menu_close", LUI.SystemInfo.LeaveMenu)
return menu return menu
end end

View File

@ -1 +1 @@
require("loading") require("loading")

View File

@ -1,3 +1,14 @@
game:addlocalizedstring("MENU_MODS", "MODS")
game:addlocalizedstring("MENU_MODS_DESC", "Load installed mods.")
game:addlocalizedstring("LUA_MENU_MOD_DESC_DEFAULT", "Load &&1.")
game:addlocalizedstring("LUA_MENU_MOD_DESC", "&&1\nAuthor: &&2\nVersion: &&3")
game:addlocalizedstring("LUA_MENU_OPEN_STORE", "Open store")
game:addlocalizedstring("LUA_MENU_OPEN_STORE_DESC", "Download and install mods.")
game:addlocalizedstring("LUA_MENU_LOADED_MOD", "Loaded mod: ^3&&1")
game:addlocalizedstring("LUA_MENU_AVAILABLE_MODS", "Available mods")
game:addlocalizedstring("LUA_MENU_UNLOAD", "Unload")
game:addlocalizedstring("LUA_MENU_UNLOAD_DESC", "Unload the currently loaded mod.")
function createdivider(menu, text) function createdivider(menu, text)
local element = LUI.UIElement.new( { local element = LUI.UIElement.new( {
leftAnchor = true, leftAnchor = true,
@ -12,112 +23,102 @@ function createdivider(menu, text)
element.scrollingToNext = true element.scrollingToNext = true
element:addElement(LUI.MenuBuilder.BuildRegisteredType("h1_option_menu_titlebar", { element:addElement(LUI.MenuBuilder.BuildRegisteredType("h1_option_menu_titlebar", {
title_bar_text = Engine.ToUpperCase(Engine.Localize(text)) title_bar_text = Engine.ToUpperCase(text)
})) }))
menu.list:addElement(element) menu.list:addElement(element)
end end
function string:truncate(length) function string:truncate(length)
if (#self <= length) then if (#self <= length) then
return self return self
end end
return self:sub(1, length - 3) .. "..." return self:sub(1, length - 3) .. "..."
end end
LUI.addmenubutton("main_campaign", { LUI.addmenubutton("main_campaign", {
index = 6, index = 6,
text = "$_MODS", text = "@MENU_MODS",
description = "Load installed mods.", description = Engine.Localize("@MENU_MODS_DESC"),
callback = function() callback = function()
LUI.FlowManager.RequestAddMenu(nil, "mods_menu") LUI.FlowManager.RequestAddMenu(nil, "mods_menu")
end end
}) })
function getmodname(path) function getmodname(path)
local name = path local name = path
local desc = "Load " .. name local desc = Engine.Localize("@LUA_MENU_MOD_DESC_DEFAULT", name)
local infofile = path .. "/info.json" local infofile = path .. "/info.json"
if (io.fileexists(infofile)) then if (io.fileexists(infofile)) then
pcall(function() pcall(function()
local data = json.decode(io.readfile(infofile)) local data = json.decode(io.readfile(infofile))
desc = string.format("%s\nAuthor: %s\nVersion: %s", desc = Engine.Localize("@LUA_MENU_MOD_DESC",
data.description, data.author, data.version) data.description, data.author, data.version)
name = data.name name = data.name
end) end)
end end
return name, desc return name, desc
end end
LUI.MenuBuilder.m_types_build["mods_menu"] = function(a1) LUI.MenuBuilder.m_types_build["mods_menu"] = function(a1)
local menu = LUI.MenuTemplate.new(a1, { local menu = LUI.MenuTemplate.new(a1, {
menu_title = "$_MODS", menu_title = "@MENU_MODS",
exclusiveController = 0, exclusiveController = 0,
menu_width = 400, menu_width = 400,
menu_top_indent = LUI.MenuTemplate.spMenuOffset, menu_top_indent = LUI.MenuTemplate.spMenuOffset,
showTopRightSmallBar = true showTopRightSmallBar = true
}) })
menu:AddButton("$_OPEN STORE", function() menu:AddButton("@LUA_MENU_OPEN_STORE", function()
if (LUI.MenuBuilder.m_types_build["mod_store_menu"]) then if (LUI.MenuBuilder.m_types_build["mod_store_menu"]) then
LUI.FlowManager.RequestAddMenu(nil, "mod_store_menu") LUI.FlowManager.RequestAddMenu(nil, "mod_store_menu")
end end
end, nil, true, nil, { end, nil, true, nil, {
desc_text = "Download and install mods." desc_text = Engine.Localize("@LUA_MENU_OPEN_STORE_DESC")
}) })
local modfolder = game:getloadedmod() local modfolder = game:getloadedmod()
if (modfolder ~= "") then if (modfolder ~= "") then
createdivider(menu, "$_Loaded mod: ^3" .. getmodname(modfolder):truncate(20)) createdivider(menu, Engine.Localize("@LUA_MENU_LOADED_MOD", getmodname(modfolder):truncate(24)))
menu:AddButton("$_UNLOAD", function() menu:AddButton("@LUA_MENU_UNLOAD", function()
game:executecommand("unloadmod") game:executecommand("unloadmod")
end, nil, true, nil, { end, nil, true, nil, {
desc_text = "Unload the currently loaded mod." desc_text = Engine.Localize("@LUA_MENU_UNLOAD_DESC")
}) })
end end
createdivider(menu, "$_Available mods") createdivider(menu, Engine.Localize("@LUA_MENU_AVAILABLE_MODS"))
if (io.directoryexists("mods")) then if (io.directoryexists("mods")) then
local mods = io.listfiles("mods/") local mods = io.listfiles("mods/")
for i = 1, #mods do for i = 1, #mods do
if (io.directoryexists(mods[i]) and not io.directoryisempty(mods[i])) then if (io.directoryexists(mods[i]) and not io.directoryisempty(mods[i])) then
local name, desc = getmodname(mods[i]) local name, desc = getmodname(mods[i])
if (mods[i] ~= modfolder) then if (mods[i] ~= modfolder) then
menu:AddButton("$_" .. name, function() game:addlocalizedstring(name, name)
game:executecommand("loadmod " .. mods[i]) menu:AddButton(name, function()
end, nil, true, nil, { game:executecommand("loadmod " .. mods[i])
desc_text = desc end, nil, true, nil, {
}) desc_text = desc
end })
end end
end end
end end
end
menu:AddBackButton(function(a1)
Engine.PlaySound(CoD.SFX.MenuBack) menu:AddBackButton(function(a1)
LUI.FlowManager.RequestLeaveMenu(a1) Engine.PlaySound(CoD.SFX.MenuBack)
end) LUI.FlowManager.RequestLeaveMenu(a1)
end)
LUI.Options.InitScrollingList(menu.list, nil) LUI.Options.InitScrollingList(menu.list, nil)
menu:CreateBottomDivider() menu:CreateBottomDivider()
menu.optionTextInfo = LUI.Options.AddOptionTextInfo(menu) menu.optionTextInfo = LUI.Options.AddOptionTextInfo(menu)
return menu return menu
end end
local localize = Engine.Localize
Engine.Localize = function(...)
local args = {...}
if (type(args[1]) == "string" and args[1]:sub(1, 2) == "$_") then
return args[1]:sub(3, -1)
end
return localize(table.unpack(args))
end

View File

@ -1,4 +1,4 @@
game:addlocalizedstring("MENU_GENERAL", "General") game:addlocalizedstring("MENU_GENERAL", "GENERAL")
game:addlocalizedstring("MENU_GENERAL_DESC", "Set the client's settings.") game:addlocalizedstring("MENU_GENERAL_DESC", "Set the client's settings.")
game:addlocalizedstring("LUA_MENU_AUTO_UPDATE", "Automatic updates") game:addlocalizedstring("LUA_MENU_AUTO_UPDATE", "Automatic updates")
game:addlocalizedstring("LUA_MENU_CHECK_UPDATES", "Check for updates") game:addlocalizedstring("LUA_MENU_CHECK_UPDATES", "Check for updates")
@ -46,7 +46,7 @@ end
LUI.addmenubutton("pc_controls", { LUI.addmenubutton("pc_controls", {
index = 4, index = 4,
text = "@MENU_GENERAL", text = "@MENU_GENERAL",
description = "@MENU_GENERAL_DESC", description = Engine.Localize("@MENU_GENERAL_DESC"),
callback = function() callback = function()
LUI.FlowManager.RequestAddMenu(nil, "settings_menu") LUI.FlowManager.RequestAddMenu(nil, "settings_menu")
end end
@ -174,4 +174,4 @@ LUI.MenuBuilder.m_types_build["settings_menu"] = function(a1)
menu:AddBackButton() menu:AddBackButton()
return menu return menu
end end

View File

@ -4,157 +4,157 @@ taskinterval = 100
updater.cancelupdate() updater.cancelupdate()
function startupdatecheck(popup, autoclose) function startupdatecheck(popup, autoclose)
updatecancelled = false updatecancelled = false
local callback = function() local callback = function()
if (not updater.getupdatecheckstatus()) then if (not updater.getupdatecheckstatus()) then
if (autoclose) then if (autoclose) then
LUI.FlowManager.RequestLeaveMenu(popup) LUI.FlowManager.RequestLeaveMenu(popup)
return return
end end
popup.text:setText("Error: " .. updater.getlasterror()) popup.text:setText("Error: " .. updater.getlasterror())
return return
end end
if (not updater.isupdateavailable()) then if (not updater.isupdateavailable()) then
if (autoclose) then if (autoclose) then
LUI.FlowManager.RequestLeaveMenu(popup) LUI.FlowManager.RequestLeaveMenu(popup)
return return
end end
popup.text:setText("No updates available") popup.text:setText("No updates available")
return return
end end
LUI.yesnopopup({ LUI.yesnopopup({
title = "NOTICE", title = "NOTICE",
text = "An update is available, proceed with installation?", text = "An update is available, proceed with installation?",
callback = function(result) callback = function(result)
if (result) then if (result) then
startupdatedownload(popup, autoclose) startupdatedownload(popup, autoclose)
else else
LUI.FlowManager.RequestLeaveMenu(popup) LUI.FlowManager.RequestLeaveMenu(popup)
end end
end end
}) })
end end
updater.startupdatecheck() updater.startupdatecheck()
createtask({ createtask({
done = updater.isupdatecheckdone, done = updater.isupdatecheckdone,
cancelled = isupdatecancelled, cancelled = isupdatecancelled,
callback = callback, callback = callback,
interval = taskinterval interval = taskinterval
}) })
end end
function startupdatedownload(popup, autoclose) function startupdatedownload(popup, autoclose)
updater.startupdatedownload() updater.startupdatedownload()
local textupdate = nil local textupdate = nil
local previousfile = nil local previousfile = nil
textupdate = game:oninterval(function() textupdate = game:oninterval(function()
local file = updater.getcurrentfile() local file = updater.getcurrentfile()
if (file == previousfile) then if (file == previousfile) then
return return
end end
file = previousfile file = previousfile
popup.text:setText("Downloading file " .. updater.getcurrentfile() .. "...") popup.text:setText("Downloading file " .. updater.getcurrentfile() .. "...")
end, 10) end, 10)
local callback = function() local callback = function()
textupdate:clear() textupdate:clear()
if (not updater.getupdatedownloadstatus()) then if (not updater.getupdatedownloadstatus()) then
if (autoclose) then if (autoclose) then
LUI.FlowManager.RequestLeaveMenu(popup) LUI.FlowManager.RequestLeaveMenu(popup)
return return
end end
popup.text:setText("Error: " .. updater.getlasterror()) popup.text:setText("Error: " .. updater.getlasterror())
return return
end end
popup.text:setText("Update successful") popup.text:setText("Update successful")
if (updater.isrestartrequired()) then if (updater.isrestartrequired()) then
LUI.confirmationpopup({ LUI.confirmationpopup({
title = "RESTART REQUIRED", title = "RESTART REQUIRED",
text = "Update requires restart", text = "Update requires restart",
buttontext = "RESTART", buttontext = "RESTART",
callback = function() callback = function()
updater.relaunch() updater.relaunch()
end end
}) })
else else
Engine.Exec("lui_restart") Engine.Exec("lui_restart")
end end
if (autoclose) then if (autoclose) then
LUI.FlowManager.RequestLeaveMenu(popup) LUI.FlowManager.RequestLeaveMenu(popup)
end end
end end
createtask({ createtask({
done = updater.isupdatedownloaddone, done = updater.isupdatedownloaddone,
cancelled = isupdatecancelled, cancelled = isupdatecancelled,
callback = callback, callback = callback,
interval = taskinterval interval = taskinterval
}) })
end end
function updaterpopup(oncancel) function updaterpopup(oncancel)
return LUI.openpopupmenu("generic_waiting_popup_", { return LUI.openpopupmenu("generic_waiting_popup_", {
oncancel = oncancel, oncancel = oncancel,
withcancel = true, withcancel = true,
text = "Checking for updates..." text = "Checking for updates..."
}) })
end end
function createtask(data) function createtask(data)
local interval = nil local interval = nil
interval = game:oninterval(function() interval = game:oninterval(function()
if (data.cancelled()) then if (data.cancelled()) then
interval:clear() interval:clear()
return return
end end
if (data.done()) then if (data.done()) then
interval:clear() interval:clear()
data.callback() data.callback()
end end
end, data.interval) end, data.interval)
return interval return interval
end end
function isupdatecancelled() function isupdatecancelled()
return updatecancelled return updatecancelled
end end
function tryupdate(autoclose) function tryupdate(autoclose)
updatecancelled = false updatecancelled = false
local popup = updaterpopup(function() local popup = updaterpopup(function()
updater.cancelupdate() updater.cancelupdate()
updatecancelled = true updatecancelled = true
end) end)
startupdatecheck(popup, autoclose) startupdatecheck(popup, autoclose)
end end
function tryautoupdate() function tryautoupdate()
if (not updater.autoupdatesenabled()) then if (not updater.autoupdatesenabled()) then
return return
end end
if (not updater.gethastriedupdate()) then if (not updater.gethastriedupdate()) then
game:ontimeout(function() game:ontimeout(function()
updater.sethastriedupdate(true) updater.sethastriedupdate(true)
tryupdate(true) tryupdate(true)
end, 100) end, 100)
end end
end end
LUI.tryupdating = tryupdate LUI.tryupdating = tryupdate
LUI.onmenuopen("main_lockout", tryautoupdate) LUI.onmenuopen("main_lockout", tryautoupdate)