Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to show user defined templates instead of default onland/onwater templates #5586

Merged
merged 6 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/snippets/features.5586.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- (#6282) Add option to use regular templates with the context based template hotkey.

You can choose between simply replacing the default land/water templates or merging them with your own templates
18 changes: 18 additions & 0 deletions lua/options/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,24 @@ options = {
},
},

{
title = "<LOC gui_template_merge_context_ingame_title>Combine context based and normal templates",
key = 'gui_template_merge_context_ingame',
type = 'toggle',
default = "off",
set = function(key, value, startup)
if GetCurrentUIState() == 'game' then
import("/lua/ui/game/hotkeys/context-based-templates.lua").UseIngametemplates = value
end
end,
custom = {
states = {
{ text = "<LOC _Off>Off", key = "off" },
{ text = "<LOC _Replace>Replace", key = "replace" },
{ text = "<LOC _Merge>Merge", key = "merge" },
},
},
},
{
title = '<LOC OPTIONS_0324>Control groups',
type = 'header',
Expand Down
74 changes: 60 additions & 14 deletions lua/ui/game/hotkeys/context-based-templates.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ local TableHash = table.hash

local StringFormat = string.format

local Prefs = import("/lua/user/prefs.lua")
UseIngametemplates = Prefs.GetFromCurrentProfile('options.gui_template_merge_context_ingame')

-------------------------------------------------------------------------------
--#region Debugging

Expand All @@ -57,10 +60,46 @@ end
--#endregion

-------------------------------------------------------------------------------
--#region Template discovery
--#region ingame Template conversion
local TemplatesHelper = import("/lua/ui/game/build_templates.lua")
-- function to convert in game templates to context based templates
function loadUserTemplates(ConvertedTemplates)
UserTemplates = TemplatesHelper.GetTemplates()
-- clear table for reuse
for k,_ in ConvertedTemplates do
ConvertedTemplates[k] = nil
end
if UserTemplates then
for k = 1, TableGetn(UserTemplates) do
local template = {TemplateData = UserTemplates[k].templateData, TemplateSortingOrder = k, Name = UserTemplates[k].name, TriggersOnLand = true, TriggersOnWater = true,}
ConvertedTemplates[k] = template
end
end
end
---@type ContextBasedTemplate[]
ConvertedTemplates = {}
loadUserTemplates(ConvertedTemplates)
-- hook into add/remove/rename templates function to rebuild local conversion table when templates are added/removed/renamed
oldAddTemplate = TemplatesHelper.AddTemplate
TemplatesHelper.AddTemplate = function (newTemplate)
oldAddTemplate(newTemplate)
loadUserTemplates(ConvertedTemplates)
end

oldRemoveTemplate = TemplatesHelper.RemoveTemplate
TemplatesHelper.RemoveTemplate = function (templateID)
oldRemoveTemplate(templateID)
loadUserTemplates(ConvertedTemplates)
end

-- convert all known templates
oldRenameTemplate = TemplatesHelper.RenameTemplate
TemplatesHelper.RenameTemplate = function (templateID, name)
oldRenameTemplate(templateID, name)
loadUserTemplates(ConvertedTemplates)
end
--#endregion

--#region Template discovery
---@type table
local RawTemplates = import("/lua/ui/game/hotkeys/context-based-templates-data.lua")

Expand Down Expand Up @@ -312,28 +351,35 @@ local function FilterTemplatesByMouseContext(buildableUnits, prefix)
TableInsert(ContextBasedTemplates, template)
elseif hydroDeposits > 0 and template.TriggersOnHydroDeposit then
TableInsert(ContextBasedTemplates, template)
elseif noDeposits and onLand and template.TriggersOnLand then
TableInsert(ContextBasedTemplates, template)
elseif noDeposits and (not onLand) and template.TriggersOnWater then
TableInsert(ContextBasedTemplates, template)
end
end
end

-- no templates to use, default to those that trigger on land or water
if TableGetn(ContextBasedTemplates) == 0 then
for k = 1, TableGetn(Templates) do
local template = Templates[k]
local valid = ValidateTemplate(template, buildableUnits, prefix)
if valid then
if -- check conditions based on the context of the mouse
(template.TriggersOnLand and onLand) or
(template.TriggersOnWater and (not onLand))
then
if not (UseIngametemplates == "off") then
for k = 1, TableGetn(ConvertedTemplates) do
local template = ConvertedTemplates[k]
local valid = ValidateTemplate(template, buildableUnits, prefix)
if valid then
TableInsert(ContextBasedTemplates, template)
end
end
end
if not (UseIngametemplates == "replace") then
for k = 1, TableGetn(Templates) do
local template = Templates[k]
local valid = ValidateTemplate(template, buildableUnits, prefix)
if valid then
if -- check conditions based on the context of the mouse
(template.TriggersOnLand and onLand) or
(template.TriggersOnWater and (not onLand))
then
TableInsert(ContextBasedTemplates, template)
end
end
end
end
end
end

Expand Down
4 changes: 4 additions & 0 deletions lua/ui/help/tooltips.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,10 @@ Tooltips = {
title = '<LOC OPTIONS_0240>Template Name Cutoff',
description = '<LOC OPTIONS_0266>The first 7 characters are displayed (default). Number of characters omitted can be customized. (Needs restart for effects to be visible.)',
},
options_gui_template_merge_context_ingame = {
title = '<LOC gui_template_merge_context_ingame_title>Merge context based and normal templates',
description = '<LOC gui_template_merge_context_ingame_desc>Replace or Combine default Templates with user created ones when hovering over empty land/water.',
},
options_gui_detailed_unitview = {
title = '<LOC OPTIONS_0241>Display More Unit Stats',
description = '<LOC OPTIONS_0267>Displays Shield Hp, Regen rate, Hp Regen rate and Build rate. Adapted from Total Veterancy by Eni.',
Expand Down