-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathcmd_layout_handler.lua
More file actions
211 lines (182 loc) · 6.31 KB
/
Copy pathcmd_layout_handler.lua
File metadata and controls
211 lines (182 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function widget:GetInfo()
return {
name = "Layout Handler",
desc = "Handles Custom Commands",
author = "GoogleFrog",
date = "8 Novemember 2016",
license = "GNU GPL, v2 or later",
layer = math.huge-1,
enabled = true,
handler = true,
}
end
local emptyTable = {}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Add commands to epic menu
local spSendCommands = Spring.SendCommands
local custom_cmd_actions = include("Configs/customCmdTypes.lua")
local function CapCase(str)
local str = str:lower()
str = str:gsub( '_', ' ' )
str = str:sub(1,1):upper() .. str:sub(2)
str = str:gsub( ' (.)',
function(x) return (' ' .. x):upper(); end
)
return str
end
options = {}
options_order = {}
local function AddHotkeyOptions()
local options_order_tmp_cmd = {}
local options_order_tmp_cmd_instant = {}
local options_order_tmp_states = {}
for cmdname, cmdData in pairs(custom_cmd_actions) do
local number = cmdData.cmdType
local cmdnamel = cmdname:lower()
local cmdname_disp = cmdData.name or CapCase(cmdname)
local path = 'Hotkeys/Commands' .. ((number == 2 and "/State") or (number == 3 and "/Instant") or "/Targeted")
if cmdData.setValue and cmdData.cmdID then
options[cmdname_disp] = {
name = cmdname_disp,
type = 'button',
OnChange = function (self)
if WG.SetStateToggle then
WG.SetStateToggle(cmdData.cmdID, cmdData.setValue)
if WG.noises then
WG.noises.PlayResponse(false, cmdData.cmdID)
end
end
end,
path = path
}
else
options[cmdname_disp] = {
name = cmdname_disp,
type = 'button',
action = cmdnamel,
path = path
}
end
if number == 2 then
options_order_tmp_states[#options_order_tmp_states+1] = cmdname_disp
--options[cmdnamel].isUnitStateCommand = true
elseif number == 3 then
options_order_tmp_cmd_instant[#options_order_tmp_cmd_instant+1] = cmdname_disp
--options[cmdnamel].isUnitInstantCommand = true
else
options_order_tmp_cmd[#options_order_tmp_cmd+1] = cmdname_disp
--options[cmdnamel].isUnitCommand = true
end
end
--options.lblcmd = { type='label', name='Targeted Commands', path = 'Hotkeys/Commands',}
--options.lblcmdinstant = { type='label', name='Instant Commands', path = 'Hotkeys/Commands',}
--options.lblstate = { type='label', name='State Commands', path = 'Hotkeys/Commands',}
table.sort(options_order_tmp_cmd)
table.sort(options_order_tmp_cmd_instant)
table.sort(options_order_tmp_states)
--options_order[#options_order+1] = 'lblcmd'
for i=1, #options_order_tmp_cmd do
options_order[#options_order+1] = options_order_tmp_cmd[i]
end
--options_order[#options_order+1] = 'lblcmdinstant'
for i=1, #options_order_tmp_cmd_instant do
options_order[#options_order+1] = options_order_tmp_cmd_instant[i]
end
--options_order[#options_order+1] = 'lblstate'
for i=1, #options_order_tmp_states do
options_order[#options_order+1] = options_order_tmp_states[i]
end
end
AddHotkeyOptions()
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Command List Processing
local function CopyTable(outtable,intable)
for i,v in pairs(intable) do
if (type(v)=='table') then
if (type(outtable[i])~='table') then
outtable[i] = {}
end
CopyTable(outtable[i],v)
else
outtable[i] = v
end
end
end
-- layout handler - its needed for custom commands to work and to delete normal spring menu
local function LayoutHandler(xIcons, yIcons, cmdCount, commands)
widgetHandler.commands = commands
widgetHandler.commands.n = cmdCount
widgetHandler:CommandsChanged()
local reParamsCmds = {}
local customCmds = {}
local cnt = 0
local AddCommand = function(command)
local cc = {}
CopyTable(cc,command )
cnt = cnt + 1
cc.cmdDescID = cmdCount+cnt
if (cc.params) then
if (not cc.actions) then --// workaround for params
local params = cc.params
for i=1,#params+1 do
params[i-1] = params[i]
end
cc.actions = params
end
reParamsCmds[cc.cmdDescID] = cc.params
end
--// remove api keys (custom keys are prohibited in the engine handler)
--// disabled is a boolean read by the integral menu from the raw
--// customCommands list; the engine's descriptor parser rejects any
--// non-string/table value and logs "GetLuaCmdDescList() bad entry", so it
--// must be stripped from the copy sent to the engine.
cc.pos = nil
cc.cmdDescID = nil
cc.params = nil
cc.disabled = nil
customCmds[#customCmds+1] = cc
end
--// preprocess the Custom Commands
for i=1,#widgetHandler.customCommands do
AddCommand(widgetHandler.customCommands[i])
end
if (cmdCount <= 0) then
return "", xIcons, yIcons, {}, customCmds, {}, {}, {}, {}, reParamsCmds, {} --prevent CommandChanged() from being called twice when deselecting all units (copied from ca_layout.lua)
end
return "", xIcons, yIcons, {}, customCmds, {}, {}, {}, {}, reParamsCmds, {[1337]=9001}
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Widget Interface
-- To get a list of used action just uncomment this
--local seenAction = {}
--function widget:CommandsChanged()
-- local commands = widgetHandler.commands
-- local customCommands = widgetHandler.customCommands
-- for i = 1, #commands do
-- if commands[i].action and not seenAction[commands[i].action] then
-- seenAction[commands[i].action] = true
-- Spring.Echo("action", commands[i].action)
-- end
-- end
--
-- for i = 1, #customCommands do
-- if customCommands[i].action and not seenAction[customCommands[i].action] then
-- seenAction[customCommands[i].action] = true
-- Spring.Echo("action", customCommands[i].action)
-- end
-- end
--end
-- Then find repalce '[f=0000678] action, ' with '"] = true,\n\t["'
function widget:Initialize()
widgetHandler:ConfigLayoutHandler(LayoutHandler)
Spring.ForceLayoutUpdate()
end
function widget:Shutdown()
widgetHandler:ConfigLayoutHandler(true)
Spring.ForceLayoutUpdate()
end