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

WorldviewCommandModeTableStreamline #6585

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions changelog/snippets/other.6585.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(#6585) Minor streamlining to GetCommandModeCalls in controls/worldview.lua, and allow alt attack move pref to work properly.
15 changes: 6 additions & 9 deletions lua/ui/controls/worldview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ local KeyCodeAlt = 18
local KeyCodeCtrl = 17
local KeyCodeShift = 16

local commandModeTable = CommandMode.GetCommandMode()

local unitsToWeaponsCached = { }

---@class Renderable : Destroyable
Expand Down Expand Up @@ -346,7 +348,7 @@ WorldView = ClassUI(moho.UIWorldView, Control) {
OnUpdateCursor = function(self)
-- gather all information
local selection = GetSelectedUnits()
local command_mode, command_data = unpack(CommandMode.GetCommandMode()) -- is set when we issue orders manually, try to build something, etc
local command_mode, command_data = unpack(commandModeTable) -- is set when we issue orders manually, try to build something, etc
local orderViaMouse = self:GetRightMouseButtonOrder() -- is set when our mouse is over a hostile unit, reclaim, etc and not in command mode
local holdAltToAttackMove = Prefs.GetFieldFromCurrentProfile('options').alt_to_force_attack_move

Expand Down Expand Up @@ -377,9 +379,6 @@ WorldView = ClassUI(moho.UIWorldView, Control) {
-- 3. then whatever is below the mouse
elseif orderViaMouse and orderViaMouse != 'RULEUCC_Move' then
order = orderViaMouse
-- 4. then if we hold alt, we'll show the attack cursor
elseif IsKeyDown(KeyCodeAlt) and selection then
order = 'RULEUCC_Attack'
end
end

Expand Down Expand Up @@ -556,8 +555,7 @@ WorldView = ClassUI(moho.UIWorldView, Control) {

-- otherwise we only show it if we're in command mode
else
local commandData = CommandMode.GetCommandMode()
local viaCommandMode = commandData[1] and commandData[1] == 'order' and commandData[2].name == 'RULEUCC_Attack'
local viaCommandMode = commandModeTable[1] and commandModeTable[1] == 'order' and commandModeTable[2].name == 'RULEUCC_Attack'
if viaCommandMode then
local commandModeChange = (viaCommandMode != self.ViaCommandModeOld)
self:OnCursorDecals(identifier, enabled or commandModeChange, changed or commandModeChange, AttackDecalFunc)
Expand Down Expand Up @@ -635,7 +633,7 @@ WorldView = ClassUI(moho.UIWorldView, Control) {
local cursor = self.Cursor
cursor[1], cursor[2], cursor[3], cursor[4], cursor[5] = UIUtil.GetCursor(identifier)
self:ApplyCursor()
CommandMode.GetCommandMode()[2].reticle = TeleportReticle(self)
commandModeTable[2].reticle = TeleportReticle(self)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels wrong to stuff data into the commad mode data that is UI related. Imagine if this is a script command with a reticle and we pass unserializable userdata inside a table: the game would immediately crash (as I discovered when annotating script tasks, and I doubt that LuaParams gets passed with any other orders, as it is logically not necessary).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that line can just be taken out; originally I had the idea that the command mode table was a good vehicle for different UI elements to get access to the reticle (or anything) but it doesn't apply in this case.

(believe I said this before)

end
end
end,
Expand Down Expand Up @@ -705,8 +703,7 @@ WorldView = ClassUI(moho.UIWorldView, Control) {
OnCursorReclaim = function(self, identifier, enabled, changed)

-- allows us to make easier distinctions for the status quo, note that this becomes invalid once we change
local commandData = CommandMode.GetCommandMode()
local viaCommandMode = commandData[1] and commandData[1] == 'order' and commandData[2].name == 'RULEUCC_Reclaim'
local viaCommandMode = commandModeTable[1] and commandModeTable[1] == 'order' and commandModeTable[2].name == 'RULEUCC_Reclaim'
local viaRightMouseButton = self:GetRightMouseButtonOrder() == 'RULEUCC_Reclaim' -- always returns nil when in command mode
local canIssueReclaimOrders = self:CanIssueReclaimOrders()

Expand Down
16 changes: 10 additions & 6 deletions lua/ui/game/commandmode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ local issuedOneCommand = false
local startBehaviors = {}
local endBehaviors = {}

-- allocate the table once for performance
local commandModeTable = {commandMode, modeData}

--- Callback triggers when command mode starts
---@param behavior fun(mode?: CommandMode, data?: CommandModeData)
---@param identifier? string
Expand Down Expand Up @@ -172,6 +175,8 @@ function StartCommandMode(newCommandMode, data)
-- update our local state
commandMode = newCommandMode
modeData = data
commandModeTable[1] = commandMode
commandModeTable[2] = modeData

-- do start behaviors
for i, v in startBehaviors do
Expand Down Expand Up @@ -210,6 +215,8 @@ function EndCommandMode(isCancel)
-- update our local state
commandMode = false
modeData = false
commandModeTable[1] = commandMode
commandModeTable[2] = modeData
issuedOneCommand = false
end

Expand All @@ -223,6 +230,8 @@ function CacheAndClearCommandMode()
CacheCommandMode()
commandMode = false
modeData = false
commandModeTable[1] = commandMode
commandModeTable[2] = modeData
end

--- Restores the cached command mode
Expand All @@ -236,14 +245,9 @@ function RestoreCommandMode(ignorePreviousCommands)
end
end

-- allocate the table once for performance
local commandModeTable = {}

--- Retrieves the current command mode information.
--- Retrieves the commandModeTable. You typically only need to call this once, after which the table reference can be stored as a local.
---@return { [1]: CommandModeDataOrder, [2]: CommandModeData }
function GetCommandMode()
commandModeTable[1] = commandMode
commandModeTable[2] = modeData
return commandModeTable
end

Expand Down
Loading