Files
OpenSpace/data/assets/util/scene_helper.asset
T
Alexander Bock d2fd3b8f1f Include helper assets and base assets explicitly
Add a new function to export profile struct to module text
2020-06-17 11:12:01 +02:00

145 lines
4.5 KiB
Plaintext

local bindKeys = function(t, ignoreWarning)
ignoreWarning = ignoreWarning or false
for _, k in ipairs(t) do
assert(k.Key, 'No key provided')
assert(k.Command, 'No command provided for key ' .. k.Key)
k.Name = k.Name or k.Key
k.GuiPath = k.GuiPath or ''
local currentKey = openspace.getKeyBinding(k.Key)
if (next(currentKey) ~= nil) and (not ignoreWarning) then
openspace.printWarning('New keybind for "' .. k.Key .. '" is added, but a previous keybind already existed. If you want to silence this warning, pass "true", to this call to bindKeys')
end
if k.Local then
openspace.bindKeyLocal(k.Key, k.Command, k.Documentation, k.Name, k.GuiPath)
else
openspace.bindKey(k.Key, k.Command, k.Documentation, k.Name, k.GuiPath)
end
end
end
asset.export("bindKeys", bindKeys)
local unbindKeys = function(keys)
-- We check against k and k.Key to provide compatability
-- for both calls with the same table that goes to bindKeys
-- as well as the return values from setDeltaTimeKeys
for _, k in ipairs(keys) do
openspace.clearKey(k.Key or k)
end
end
asset.export("unbindKeys", unbindKeys)
local deltaTimeKeys = {}
local setDeltaTimeKeys = function(t)
local Keys = {
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'Shift+1', 'Shift+2', 'Shift+3', 'Shift+4', 'Shift+5', 'Shift+6', 'Shift+7', 'Shift+8', 'Shift+9', 'Shift+0',
'Ctrl+1', 'Ctrl+2', 'Ctrl+3', 'Ctrl+4', 'Ctrl+5', 'Ctrl+6', 'Ctrl+7', 'Ctrl+8', 'Ctrl+9', 'Ctrl+0',
'Alt+1', 'Alt+2', 'Alt+3', 'Alt+4', 'Alt+5', 'Alt+6', 'Alt+7', 'Alt+8', 'Alt+9', 'Alt+0'
}
if #t > #Keys then
openspace.printError("Error settings delta time keys: Too many delta times (" .. #t .. ")")
return
end
unbindKeys(deltaTimeKeys)
result = {}
for i, v in ipairs(t) do
openspace.bindKeyLocal(
Keys[i],
'openspace.time.interpolateDeltaTime(' .. v .. ")",
'Setting the simulation speed to ' .. v .. ' seconds per realtime second',
'Set sim speed ' .. v,
'/Simulation Speed'
)
table.insert(result, Keys[i])
end
deltaTimeKeys = result
return result
end
asset.export("setDeltaTimeKeys", setDeltaTimeKeys)
--shortcut function
local function has_value (tab, val)
for index, value in ipairs(tab) do
-- We grab the first index of our sub-table instead
if value[1] == val then
return true
end
end
return false
end
local extractShortcuts = function(names, shortcuts)
local foundShortcuts = {};
if type(names) ~= "table" then
openspace.printWarning("scene_helper.extractShortcuts invalid paramater names (not Table)")
end
if type(shortcuts) ~= "table" then
openspace.printWarning("scene_helper.extractShortcuts invalid paramater shortcuts (not Table)")
end
for _, shortcut in ipairs(shortcuts) do
for _, name in ipairs(names ) do
if (shortcut.Name == name) then
foundShortcuts[#foundShortcuts+1] = shortcut
end
end
end
return foundShortcuts
end
asset.export("extractShortcuts", extractShortcuts)
local createKeyBindFromShortcuts = function(key, shortcuts, guipath, title, documentation)
if type(key) ~= "string" then
openspace.printWarning("scene_helper.createKeyBindFromShortcuts invalid paramater key (not String)")
end
if type(shortcuts) ~= "table" or #shortcuts == 0 then
openspace.printWarning("scene_helper.createKeyBindFromShortcuts invalid paramater shortcuts (not Table or empty)")
end
-- if type(guipath) ~= "string" then
-- guipath = shortcuts[0].GuiPath
-- end
local concatTitle = type(title) ~= "string"
local concatDocumentation = type(documentation) ~= "string"
local keybind = {
Key = key,
Command = "",
Name = name or "",
Documentation = documentation or "",
GuiPath = guipath or "",
Local = false
}
for _, shortcut in ipairs(shortcuts) do
keybind.Command = keybind.Command .. shortcut.Command
if concatTitle then
keybind.Name = keybind.Name .. "/" .. shortcut.Name
end
if concatDocumentation then
keybind.Documentation = keybind.Documentation .. "," .. shortcut.Documentation
end
keybind.Local = keybind.Local and shortcut.Local
end
return keybind
end
asset.export("createKeyBindFromShortcuts", createKeyBindFromShortcuts)