Skip to content

Commit

Permalink
reorganize configs
Browse files Browse the repository at this point in the history
  • Loading branch information
thehobojoe committed May 31, 2023
1 parent b98e8bf commit e0f02ae
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 173 deletions.
2 changes: 1 addition & 1 deletion luaui/Widgets/gui_buildmenu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ local advplayerlistLeft = vsx * 0.8
local ui_opacity = tonumber(Spring.GetConfigFloat("ui_opacity", 0.7) or 0.6)
local ui_scale = tonumber(Spring.GetConfigFloat("ui_scale", 1) or 1)

local units = VFS.Include("luaui/configs/gridmenu_categories.lua")
local units = VFS.Include("luaui/configs/unit_config.lua")

local isSpec = Spring.GetSpectatingState()
local myTeamID = Spring.GetMyTeamID()
Expand Down
51 changes: 26 additions & 25 deletions luaui/Widgets/gui_gridmenu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ local buildOpts = {}
local buildOptsCount
local categories = {}
local catRects = {}
local currentBuildCategory, currentCategoryIndex
local currentCategory, currentCategoryIndex
local currentPage = 1
local pages = 1
local nextPageRect = Rect:new(0, 0, 0, 0)
Expand Down Expand Up @@ -207,7 +207,8 @@ local ui_opacity, ui_scale


local selectNextFrame, switchedCategory
local units = VFS.Include("luaui/configs/gridmenu_categories.lua")
local units = VFS.Include("luaui/configs/unit_config.lua")
local grid = VFS.Include("luaui/configs/gridmenu_config.lua")

local showWaterUnits = false

Expand Down Expand Up @@ -242,11 +243,11 @@ local function RefreshCommands()
selectedBuilder = startDefID
end

if currentBuildCategory then
gridPos = units.unitGridPos[selectedBuilder] and units.unitGridPos[selectedBuilder][currentCategoryIndex]
lHasUnitGrid = units.hasUnitGrid[selectedBuilder] -- Ensure if unit has static grid to not repeat unit on different category
if currentCategory then
gridPos = grid.unitGridPos[selectedBuilder] and grid.unitGridPos[selectedBuilder][currentCategoryIndex]
lHasUnitGrid = grid.hasUnitGrid[selectedBuilder] -- Ensure if unit has static grid to not repeat unit on different category
elseif selectedFactory then
gridPos = units.unitGridPos[selectedFactory]
gridPos = grid.unitGridPos[selectedFactory]
end

uncategorizedBuildOpts = {}
Expand All @@ -271,7 +272,7 @@ local function RefreshCommands()
name = UnitDefs[udefid].name,
params = {}
}
elseif currentBuildCategory == nil or (units.unitCategories[udefid] == currentBuildCategory and not (lHasUnitGrid and lHasUnitGrid[udefid])) then
elseif currentCategory == nil or (grid.unitCategories[udefid] == currentCategory and not (lHasUnitGrid and lHasUnitGrid[udefid])) then
Spring.Echo("uncategorized options")
buildOptsCount = buildOptsCount + 1
buildOpts[udefid] = {
Expand Down Expand Up @@ -307,7 +308,7 @@ local function RefreshCommands()
Spring.Echo("categorized options")
buildOptsCount = buildOptsCount + 1
buildOpts[cmd.id * -1] = activeCmdDescs[index]
elseif currentBuildCategory == nil or (units.unitCategories[cmd.id * -1] == currentBuildCategory and not (lHasUnitGrid and lHasUnitGrid[cmd.id * -1])) then
elseif currentCategory == nil or (grid.unitCategories[cmd.id * -1] == currentCategory and not (lHasUnitGrid and lHasUnitGrid[cmd.id * -1])) then
Spring.Echo("uncategorized options")
buildOptsCount = buildOptsCount + 1
buildOpts[cmd.id * -1] = activeCmdDescs[index]
Expand Down Expand Up @@ -416,7 +417,7 @@ local function setPreGamestartDefID(uDefID)
selBuildQueueDefID = uDefID
WG['pregame-build'].setPreGamestartDefID(uDefID)
if not uDefID then
currentBuildCategory = nil
currentCategory = nil
currentCategoryIndex = nil
doUpdate = true
end
Expand All @@ -430,15 +431,15 @@ local function gridmenuCategoryHandler(_, _, args)
return
end

if not selectedBuilder or (currentBuildCategory and hotkeyActions['1' .. cIndex]) then
if not selectedBuilder or (currentCategory and hotkeyActions['1' .. cIndex]) then
return
end

local alt, ctrl, meta, _ = Spring.GetModKeyState()

if alt or ctrl or meta then return end

currentBuildCategory = categories[cIndex]
currentCategory = categories[cIndex]
currentCategoryIndex = cIndex
switchedCategory = os.clock()
doUpdate = true
Expand Down Expand Up @@ -498,7 +499,7 @@ local function gridmenuKeyHandler(_, _, args, _, isRepeat)
enqueueUnit(uDefID, opts)

return true
elseif preGamestartPlayer and currentBuildCategory then
elseif preGamestartPlayer and currentCategory then
if alt or ctrl or meta then return end
if args[3] and args[3] == 'factory' then return false end

Expand All @@ -507,7 +508,7 @@ local function gridmenuKeyHandler(_, _, args, _, isRepeat)
doUpdate = true

return true
elseif selectedBuilder and currentBuildCategory then
elseif selectedBuilder and currentCategory then
if args[3] and args[3] == 'factory' then return false end
if alt or ctrl or meta then return end

Expand All @@ -528,7 +529,7 @@ function widget:CommandNotify(cmdID, _, cmdOpts)
end

if returnToCategoriesOnPick or not cmdOpts.shift then
currentBuildCategory = nil
currentCategory = nil
doUpdate = true
end
end
Expand Down Expand Up @@ -557,9 +558,9 @@ local function prevPageHandler()
end

local function gridmenuCategoriesHandler()
if not (selectedBuilder and currentBuildCategory) then return end
if not (selectedBuilder and currentCategory) then return end

currentBuildCategory = nil
currentCategory = nil
currentCategoryIndex = nil
doUpdate = true

Expand Down Expand Up @@ -808,7 +809,7 @@ function widget:Update(dt)

selectedBuilder = nil
selectedFactory = nil
currentBuildCategory = nil
currentCategory = nil
currentCategoryIndex = nil
selectedBuilders = {}
currentPage = 1
Expand Down Expand Up @@ -1025,7 +1026,7 @@ local function drawCell(id, usedZoom, cellColor, disabled)
end

-- hotkey draw
if cmd.hotkey and (selectedFactory or (selectedBuilder and currentBuildCategory)) then
if cmd.hotkey and (selectedFactory or (selectedBuilder and currentCategory)) then
local hotkeyText = keyConfig.sanitizeKey(cmd.hotkey, currentLayout)

local hotkeyFontSize = priceFontSize * 1.1
Expand Down Expand Up @@ -1108,7 +1109,7 @@ local function drawCategories()
local rect = catRects[cat]

local opts = {
highlight = (cat == currentBuildCategory),
highlight = (cat == currentCategory),
hovered = (hoveredButton == rect:getId()),
}

Expand Down Expand Up @@ -1162,9 +1163,9 @@ local function drawGrid()
local cellRectID = 0
local unitGrid
if selectedFactory then
unitGrid = units.gridPosUnit[selectedFactory]
unitGrid = grid.gridPosUnit[selectedFactory]
else
unitGrid = units.gridPosUnit[selectedBuilder]
unitGrid = grid.gridPosUnit[selectedBuilder]
end
local curCmd = currentPage > 1 and (numCellsPerPage * (currentPage - 1) - (buildOptsCount - uncategorizedBuildOptsCount) + 1) or 1

Expand Down Expand Up @@ -1195,7 +1196,7 @@ local function drawGrid()
uDefID = uncategorizedBuildOpts[curCmd].id * -1
curCmd = curCmd + 1
end
elseif currentPage == 1 and currentBuildCategory and unitGrid and unitGrid[currentCategoryIndex .. arow .. coll] then
elseif currentPage == 1 and currentCategory and unitGrid and unitGrid[currentCategoryIndex .. arow .. coll] then
uDefID = unitGrid[currentCategoryIndex .. arow .. coll]
elseif uncategorizedBuildOpts[curCmd] then
uDefID = uncategorizedBuildOpts[curCmd].id * -1
Expand Down Expand Up @@ -1599,7 +1600,7 @@ function widget:KeyRelease(key)
if preGamestartPlayer then
setPreGamestartDefID(nil)
else
currentBuildCategory = nil
currentCategory = nil
currentCategoryIndex = nil
doUpdate = true
end
Expand All @@ -1624,7 +1625,7 @@ function widget:MousePress(x, y, button)
if not disableInput then
for cat, catRect in pairs(catRects) do
if catRect:contains(x, y) then
currentBuildCategory = cat
currentCategory = cat
switchedCategory = os.clock()
Spring.PlaySoundFile(Cfgs.sound_queue_add, 0.75, 'ui')

Expand Down Expand Up @@ -1663,7 +1664,7 @@ function widget:MousePress(x, y, button)
return true
end
elseif selectedBuilder and button == 3 then
currentBuildCategory = nil
currentCategory = nil
currentCategoryIndex = nil
doUpdate = true
end
Expand Down
109 changes: 109 additions & 0 deletions luaui/configs/gridmenu_config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@


local configs = VFS.Include('luaui/configs/gridmenu_layouts.lua')
local labGrids = configs.LabGrids
local unitGrids = configs.UnitGrids

local unitGridPos = { }
local gridPosUnit = { }
local hasUnitGrid = { }

local unitCategories = {}

local BUILDCAT_ECONOMY = "Economy"
local BUILDCAT_COMBAT = "Combat"
local BUILDCAT_UTILITY = "Utility"
local BUILDCAT_PRODUCTION = "Build"

local categoryGroupMapping = {
energy = BUILDCAT_ECONOMY,
metal = BUILDCAT_ECONOMY,
builder = BUILDCAT_PRODUCTION,
buildert2 = BUILDCAT_PRODUCTION,
buildert3 = BUILDCAT_PRODUCTION,
buildert4 = BUILDCAT_PRODUCTION,
util = BUILDCAT_UTILITY,
weapon = BUILDCAT_COMBAT,
explo = BUILDCAT_COMBAT,
weaponaa = BUILDCAT_COMBAT,
weaponsub = BUILDCAT_COMBAT,
aa = BUILDCAT_COMBAT,
emp = BUILDCAT_COMBAT,
sub = BUILDCAT_COMBAT,
nuke = BUILDCAT_COMBAT,
antinuke = BUILDCAT_COMBAT,
}

for uname, ugrid in pairs(unitGrids) do
local builder = UnitDefNames[uname]
local uid = builder.id

unitGridPos[uid] = {{},{},{},{}}
gridPosUnit[uid] = {}
hasUnitGrid[uid] = {}
local uCanBuild = {}

local uBuilds = builder.buildOptions
for i = 1, #uBuilds do
uCanBuild[uBuilds[i]] = true
end

for cat=1,4 do
for r=1,3 do
for c=1,4 do
local ugdefname = ugrid[cat] and ugrid[cat][r] and ugrid[cat][r][c]

if ugdefname then
local ugdef = UnitDefNames[ugdefname]

if ugdef and ugdef.id and uCanBuild[ugdef.id] then
gridPosUnit[uid][cat .. r .. c] = ugdef.id
unitGridPos[uid][cat][ugdef.id] = cat .. r .. c
hasUnitGrid[uid][ugdef.id] = true
end
end
end
end
end
end

for uname, ugrid in pairs(labGrids) do
local udef = UnitDefNames[uname]
local uid = udef.id

unitGridPos[uid] = {}
gridPosUnit[uid] = {}
local uCanBuild = {}

local uBuilds = udef.buildOptions
for i = 1, #uBuilds do
uCanBuild[uBuilds[i]] = true
end

for r=1,3 do
for c=1,4 do
local index = (r - 1) * 4 + c
local ugdefname = ugrid[index]

if ugdefname then
local ugdef = UnitDefNames[ugdefname]

if ugdef and ugdef.id and uCanBuild[ugdef.id] then
gridPosUnit[uid][r .. c] = ugdef.id
unitGridPos[uid][ugdef.id] = r .. c
end
end
end
end
end

for unitDefID, unitDef in pairs(UnitDefs) do
unitCategories[unitDefID] = categoryGroupMapping[unitDef.customParams.unitgroup] or BUILDCAT_UTILITY
end

return {
unitGridPos = unitGridPos,
gridPosUnit = gridPosUnit,
hasUnitGrid = hasUnitGrid,
unitCategories = unitCategories,
}
Loading

0 comments on commit e0f02ae

Please sign in to comment.