File size: 5,224 Bytes
b6a38d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
if FirstLoad then
XShortcutsTarget = false
XShortcutsThread = false
XShortcutsModeExitFunc = empty_func
end
XShortcutsThread = XShortcutsThread or CreateRealTimeThread(function()
	if not XShortcutsTarget then
		XShortcutsTarget = DeveloperInterface:new({}, terminal.desktop)
		XShortcutsTarget:SetUIVisible(false)
		XShortcutsTarget:Open()
	end
	if not Platform.ged then
		WaitDataLoaded()
		-- Wait for default action shortcuts from AccountStorage.Shortcuts
		while not AccountStorage and not PlayWithoutStorage() do
			WaitMsg("AccountStorageChanged")
		end
	end
	ReloadShortcuts()
	NonBindableKeys = GatherNonBindableKeys()
end)

function OnMsg.DataLoaded()
	XShortcutsThread = XShortcutsThread or CreateRealTimeThread(ReloadShortcuts)
end

function OnMsg.PresetSave(class)
	local classdef = g_Classes[class]
	if IsKindOf(classdef, "XTemplate") then
		XShortcutsThread = XShortcutsThread or CreateRealTimeThread(ReloadShortcuts)
	end
end

function ReloadShortcuts()
	PauseInfiniteLoopDetection("ReloadShortcuts")
	table.clear(XShortcutsTarget.actions)
	table.clear(XShortcutsTarget.shortcut_to_actions)
	table.clear(XShortcutsTarget.menubar_actions)
	table.clear(XShortcutsTarget.toolbar_actions)
	
	if Platform.ged then
		if XTemplates.CommonShortcuts then
			XTemplateSpawn("CommonShortcuts", XShortcutsTarget)
		end
		if XTemplates.GedShortcuts then
			XTemplateSpawn("GedShortcuts", XShortcutsTarget)
		end
	else
		ForEachPresetInGroup("XTemplate", "Shortcuts", function(preset)
			XTemplateSpawn(preset.id, XShortcutsTarget)
		end)
		Msg("Shortcuts", XShortcutsTarget)
	end

	ResumeInfiniteLoopDetection("ReloadShortcuts")
	Msg("ShortcutsReloaded")
	XShortcutsTarget:UpdateToolbar()
	XShortcutsThread = false
end

function XDumpShortcuts(filename)
	local shortcut_to_actions = {}
	local action_to_shortcuts = {}
	local function Add(action, shortcut)
		if (shortcut or "") == "" then
			return
		end
		local name = action.ActionName or ""
		if name == "" then
			name = action.ActionId or "?"
		end
		if IsT(name) then
			name = TTranslate(name, action)
		end
		local menu = action.ActionMenubar or ""
		if menu ~= "" then
			name = name .. " (" .. menu .. ")"
		end
		shortcut_to_actions[shortcut] = table.create_add_unique(shortcut_to_actions[shortcut], name)
		action_to_shortcuts[name] = table.create_add_unique(action_to_shortcuts[name], shortcut)
	end
	for _, action in ipairs(XShortcutsTarget:GetActions()) do
		Add(action, action.ActionShortcut)
		Add(action, action.ActionShortcut2)
	end
	
	local list = {}
	for shortcut, actions in pairs(shortcut_to_actions) do
		list[#list + 1] = shortcut .. ": " .. table.concat(actions, ", ")
	end
	table.sort(list)
	local shortcut_to_actions_result = table.concat(list, "\n")
	
	local list = {}
	for name, shortcuts in pairs(action_to_shortcuts) do
		list[#list + 1] = name .. ": " .. table.concat(shortcuts, ", ")
	end
	table.sort(list)
	local action_to_shortcuts_result = table.concat(list, "\n")
	
	local result = {
		"Shortcut to Actions:\n",
		"----------------------------------------------------------------------------------\n",
		"\n",
		shortcut_to_actions_result,
		"\n\n\n\n\n\n\n\n\n\n\n\n\n\n",
		"Action to Shortcuts:\n",
		"----------------------------------------------------------------------------------\n",
		"\n",
		action_to_shortcuts_result
	}
	filename = filename or "XShortcuts.txt"
	AsyncStringToFile(filename, result)
	OpenTextFileWithEditorOfChoice(filename)
end

function XShortcutsSetMode(mode, exit_func)
	if XShortcutsTarget and XShortcutsTarget:GetActionsMode() ~= mode then
		XShortcutsTarget:SetActionsMode(mode)
		XShortcutsTarget:SetUIVisible(mode == "Editor")
		local old_exit_func = XShortcutsModeExitFunc
		XShortcutsModeExitFunc = exit_func or empty_func
		old_exit_func()
	end
end

function SplitShortcut(shortcut)
	local keys
	if shortcut ~= "" then
		keys = string.split(shortcut, "-")
		local count = #keys
		--fix for when the last key is "-" or "Numpad -"
		if keys[count] == "" then
			keys[count] = nil
			keys[count-1] = keys[count-1] .. "-"
		end
	end
	return keys or {}
end

if FirstLoad then
	s_XShortcutsTargetCache = {}
end

function GetShortcuts(action_id) --cpy paste from sim/ui/shortcuts
	local action = s_XShortcutsTargetCache[action_id]
	if not action then
		action = XShortcutsTarget and XShortcutsTarget:ActionById(action_id)
		s_XShortcutsTargetCache[action_id] = action
	end
	local saved = AccountStorage and AccountStorage.Shortcuts[action_id]
	if saved or action then
		local shortcut = (saved and saved[1]) or (action and action.ActionShortcut)
		local shortcut2 = (saved and saved[2]) or (action and action.ActionShortcut2)
		local shortcut_gamepad = (saved and saved[3]) or (action and action.ActionGamepad)
		if (shortcut or "") ~= "" or (shortcut2 or "") ~= "" or (shortcut_gamepad or "") ~= "" then
			return {shortcut, shortcut2, shortcut_gamepad}
		end
	end
	return false
end

function GetGamepadShortcut(action_id)
	local shortcuts = GetShortcuts(action_id)
	return shortcuts and shortcuts[3]
end

function OnMsg.ShortcutsReloaded()
	s_XShortcutsTargetCache = {}
end

function CheckShortcutBinding(binding, shortcut_id)
	return table.find(GetShortcuts(shortcut_id) or empty_table, binding)
end