Feature/state machine (#1705)

* Fix script error in old state machine example

* Add a module with a more complex state machine, that can be created and controlled through the Lua API. Useful for interactive installations

* Add an example asset for the new state machine and rename the old linear "state machine" to luastatemachine


Co-authored-by: Malin Ejdbo <malin.ejdbo@gmail.com>
This commit is contained in:
Emma Broman
2021-08-16 09:29:45 +02:00
committed by GitHub
parent afe1f960cd
commit 8d7d8b9ba4
15 changed files with 1149 additions and 39 deletions

View File

@@ -0,0 +1,47 @@
local stateMachineHelper = asset.require('util/lua_state_machine_helper')
local states = {
{
Title = "Highlight EarthTrail",
Play = function ()
openspace.setPropertyValue("Scene.EarthTrail.Renderable.Appearance.LineWidth", 10, 1)
end,
Rewind = function ()
openspace.setPropertyValue("Scene.EarthTrail.Renderable.Appearance.LineWidth", 2, 1)
end
},
{
Title = "Highlight MarsTrail",
Play = function ()
openspace.setPropertyValue("Scene.EarthTrail.Renderable.Appearance.LineWidth", 2, 1)
openspace.setPropertyValue("Scene.MarsTrail.Renderable.Appearance.LineWidth", 10, 1)
end,
Rewind = function ()
openspace.setPropertyValue("Scene.MarsTrail.Renderable.Appearance.LineWidth", 2, 1)
openspace.setPropertyValue("Scene.EarthTrail.Renderable.Appearance.LineWidth", 10, 1)
end
}
}
local stateMachine
function next()
stateMachine.goToNextState()
end
function previous()
stateMachine.goToPreviousState()
end
asset.onInitialize(function ()
stateMachine = stateMachineHelper.createStateMachine(states)
openspace.bindKey('RIGHT', 'next()')
openspace.bindKey('LEFT', 'previous()')
end)
asset.onDeinitialize(function ()
stateMachine = nil
openspace.clearKey('RIGHT')
openspace.clearKey('LEFT')
end)

View File

@@ -1,47 +1,75 @@
local stateMachineHelper = asset.require('util/state_machine_helper')
-- Create a state machine with a few different states. The state machine can be controlled through
-- the scripting commands from the state machine module.
states = {
local targetNode = function(nodeIdentifier)
return [[
openspace.setPropertyValueSingle("NavigationHandler.OrbitalNavigator.RetargetAnchor", nil)
openspace.setPropertyValueSingle(
"NavigationHandler.OrbitalNavigator.Anchor",
']] .. nodeIdentifier .. [['
)
openspace.setPropertyValueSingle("NavigationHandler.OrbitalNavigator.Aim", '')
]]
end
local states = {
{
Title = "Highlight EarthTrail",
Play = function ()
openspace.setPropertyValue("Scene.EarthTrail.Renderable.LineWidth", 10, 1)
end,
Rewind = function ()
openspace.setPropertyValue("Scene.EarthTrail.Renderable.LineWidth", 2, 1)
end
},
Identifier = "Constellations",
Enter = [[
openspace.setPropertyValueSingle('Scene.Constellations.Renderable.Opacity', 1.0, 1.0)
]],
Exit = [[
openspace.setPropertyValueSingle('Scene.Constellations.Renderable.Opacity', 0.0, 1.0)
]]
},
{
Title = "Highlight MarsTrail",
Play = function ()
openspace.setPropertyValue("Scene.EarthTrail.Renderable.LineWidth", 2, 1)
openspace.setPropertyValue("Scene.MarsTrail.Renderable.LineWidth", 10, 1)
end,
Rewind = function ()
openspace.setPropertyValue("Scene.MarsTrail.Renderable.LineWidth", 2, 1)
openspace.setPropertyValue("Scene.EarthTrail.Renderable.LineWidth", 10, 1)
end
Identifier = "Earth",
Enter = "openspace.setPropertyValueSingle('Scene.EarthLabel.Renderable.Enabled', true)",
Exit = "openspace.setPropertyValueSingle('Scene.EarthLabel.Renderable.Enabled', false)"
},
{
Identifier = "Moon",
Enter = "",
Exit = ""
}
}
local stateMachine
local transitions = {
{
From = "Earth",
To = "Moon",
Action = targetNode("Moon")
},
{
From = "Moon",
To = "Earth",
Action = targetNode("Earth")
},
{
From = "Earth",
To = "Constellations",
-- action is optional
},
{
From = "Constellations",
To = "Earth"
},
{
From = "Moon",
To = "Constellations",
Action = targetNode("Earth")
},
{
From = "Constellations",
To = "Moon",
Action = targetNode("Moon")
}
}
function next()
stateMachine.goToNextState()
end
asset.onInitialize(function()
-- Setup
openspace.setPropertyValueSingle('Scene.Constellations.Renderable.Enabled', true)
openspace.setPropertyValueSingle('Scene.Constellations.Renderable.Opacity', 0.0)
function previous()
stateMachine.goToPreviousState()
end
asset.onInitialize(function ()
stateMachine = stateMachineHelper.createStateMachine(states)
openspace.bindKey('RIGHT', 'next()')
openspace.bindKey('LEFT', 'previous()')
end)
asset.onDeinitialize(function ()
stateMachine = nil
openspace.clearKey('RIGHT')
openspace.clearKey('LEFT')
openspace.statemachine.createStateMachine(states, transitions, "Earth")
end)

View File

@@ -1,3 +1,8 @@
-- Contains the required functions to create a simple Lua state machine, that can step
-- forwards and backwards through a list of states.
--
-- A state is given as a table with a Title string, and two functions: Play and Rewind
-- (see example asset)
local goToNextStateFunction = function (machine)
if (machine.currentStateIndex >= #machine.states) then