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 widgetHandler:GetSelectedUnits. #4660

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions LuaUI/Widgets/cmd_unhandled.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function widget:GetInfo()
return {
name = "Unhandled Commands",
desc = "handles unhandled commands.",
author = "Amnykon",
date = "March 22, 2022",
license = "GNU GPL, v2 or later",
layer = math.huge,
handler = true,
enabled = true -- loaded by default?
}
end

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function widget:CommandNotify(id, params, options)
local units = widgetHandler:GetSelectedUnits(id, params, options)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unhanded behavior does not call widgetHandler:GetSelectedUnits, which does not allow widgets to modify the selection units.

for i=1,#units do
widgetHandler:UnitCommandNotify(units[i], id, params, options)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Default behavior does not call widgetHandler:UnitCommandNotify, preventing widgets from using this hook.

end
return true
end

function widget:UnitCommandNotify(unitID, id, params, options)
Spring.GiveOrderToUnit (unitID, id, params, options)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

calls that use this check for unhanded events and call this anyway. This makes that calls easier as they don't need to check if it is unhanded.

return true
end
1 change: 1 addition & 0 deletions LuaUI/callins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CallInsList = {
"Shutdown",
"LayoutButtons",
"ConfigureLayout",
"GetSelectedUnits",
"CommandNotify",
"UnitCommandNotify",

Expand Down
4 changes: 4 additions & 0 deletions LuaUI/camain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ function ConfigureLayout(command)
return widgetHandler:ConfigureLayout(command)
end

function GetSelectedUnits(cmdID, cmdParams, cmdOpts)
return widgetHandler:GetSelectedUnits(cmdID, cmdParams, cmdOpts)
end

function CommandNotify(id, params, options)
return widgetHandler:CommandNotify(id, params, options)
end
Expand Down
15 changes: 15 additions & 0 deletions LuaUI/cawidgets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ local callInLists = {
'Shutdown',
'Update',
'TextCommand',
'GetSelectedUnits',
'CommandNotify',
'UnitCommandNotify',
'AddConsoleLine',
Expand Down Expand Up @@ -1388,6 +1389,20 @@ function widgetHandler:ConfigureLayout(command)
return false
end

function widgetHandler:GetSelectedUnits(id, params, options)
Copy link
Contributor

Choose a reason for hiding this comment

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

This callin seems misnamed. Spring.GetSelectedUnits already exists but widget:GetSelectedUnits seems to be doing something different. Also why is a callin a getter?

Copy link
Contributor Author

@amnykon amnykon Mar 28, 2022

Choose a reason for hiding this comment

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

Would widgetHandler:filterSelectedUnits(selected, id, params, options) be better. and make callers call Spring.GetSelectedUnits() for selected argument? should widgetHandler call Spring.GetSelectedUnits() if selected is nil, as Spring.GetSelectedUnits() is the expected value for this argument?

local selected = Spring.GetSelectedUnits()

for _, w in r_ipairs(self.CommandNotifyList) do
if w.GetSelectedUnits then
local modified = w:GetSelectedUnits(selected, id, params, options)
if modified then
selected = modified
end
end
end
return selected
end

function widgetHandler:CommandNotify(id, params, options)
for _, w in r_ipairs(self.CommandNotifyList) do
if (w:CommandNotify(id, params, options)) then
Expand Down