mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-04-24 04:58:59 -05:00
Feature/state machine (#673)
* Implement simple state machine example in Lua * Use defaults for trail widths
This commit is contained in:
committed by
Alexander Bock
parent
13edfb64b4
commit
c2cc2bab17
@@ -1,4 +1,4 @@
|
||||
local helper = asset.require('util/slidedeck_helper')
|
||||
local helper = asset.require('util/slide_deck_helper')
|
||||
local deck = nil
|
||||
|
||||
asset.onInitialize(function ()
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
local stateMachineHelper = asset.require('util/state_machine_helper')
|
||||
|
||||
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
|
||||
},
|
||||
{
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
|
||||
local goToNextStateFunction = function (machine)
|
||||
if (machine.currentStateIndex >= #machine.states) then
|
||||
machine.currentStateIndex = #machine.states
|
||||
return
|
||||
end
|
||||
|
||||
if (machine.currentStateIndex == 0) then
|
||||
openspace.printInfo("Starting state machine with " ..
|
||||
machine.states[machine.currentStateIndex + 1].Title)
|
||||
elseif (machine.currentStateIndex == #machine.states) then
|
||||
openspace.printInfo("Proceed from " ..
|
||||
machine.states[machine.currentStateIndex].Title
|
||||
)
|
||||
else
|
||||
openspace.printInfo("Proceed from " ..
|
||||
machine.states[machine.currentStateIndex].Title .. " to " ..
|
||||
machine.states[machine.currentStateIndex + 1].Title
|
||||
)
|
||||
end
|
||||
|
||||
machine.currentStateIndex = machine.currentStateIndex + 1
|
||||
|
||||
machine.states[machine.currentStateIndex].Play()
|
||||
end
|
||||
|
||||
local goToPreviousStateFunction = function (machine)
|
||||
if (machine.currentStateIndex < 1) then
|
||||
machine.currentStateIndex = 0
|
||||
return
|
||||
end
|
||||
|
||||
if (machine.currentStateIndex == 1) then
|
||||
openspace.printInfo("Rewind from " ..
|
||||
machine.states[machine.currentStateIndex].Title
|
||||
)
|
||||
else
|
||||
openspace.printInfo("Rewind from " ..
|
||||
machine.states[machine.currentStateIndex].Title .. " to " ..
|
||||
machine.states[machine.currentStateIndex - 1].Title
|
||||
)
|
||||
end
|
||||
|
||||
machine.states[machine.currentStateIndex].Rewind()
|
||||
machine.currentStateIndex = machine.currentStateIndex - 1
|
||||
end
|
||||
|
||||
local createStateMachine = function (states)
|
||||
local machine = {
|
||||
states = states,
|
||||
currentStateIndex = 0
|
||||
}
|
||||
machine.goToNextState = function () goToNextStateFunction(machine) end
|
||||
machine.goToPreviousState = function () goToPreviousStateFunction(machine) end
|
||||
return machine
|
||||
end
|
||||
|
||||
|
||||
asset.export('createStateMachine', createStateMachine)
|
||||
Reference in New Issue
Block a user