Example implementation
This commit is contained in:
@@ -0,0 +1,9 @@
|
|||||||
|
package org.openautonomousconnection.luascript.hosts;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public interface AudioHost {
|
||||||
|
|
||||||
|
void play(File audioFile);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -45,6 +45,8 @@ public interface HostServices {
|
|||||||
*/
|
*/
|
||||||
Optional<ConsoleHost> console();
|
Optional<ConsoleHost> console();
|
||||||
|
|
||||||
|
Optional<AudioHost> audio();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple immutable implementation.
|
* Simple immutable implementation.
|
||||||
*/
|
*/
|
||||||
@@ -54,6 +56,7 @@ public interface HostServices {
|
|||||||
private final EventHost events;
|
private final EventHost events;
|
||||||
private final ResourceHost resources;
|
private final ResourceHost resources;
|
||||||
private final ConsoleHost console;
|
private final ConsoleHost console;
|
||||||
|
private final AudioHost audioHost;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a HostServices container.
|
* Creates a HostServices container.
|
||||||
@@ -64,12 +67,13 @@ public interface HostServices {
|
|||||||
* @param resources resource host
|
* @param resources resource host
|
||||||
* @param console console host
|
* @param console console host
|
||||||
*/
|
*/
|
||||||
public Default(UiHost ui, DomHost dom, EventHost events, ResourceHost resources, ConsoleHost console) {
|
public Default(UiHost ui, DomHost dom, EventHost events, ResourceHost resources, ConsoleHost console, AudioHost audioHost) {
|
||||||
this.ui = ui;
|
this.ui = ui;
|
||||||
this.dom = dom;
|
this.dom = dom;
|
||||||
this.events = events;
|
this.events = events;
|
||||||
this.resources = resources;
|
this.resources = resources;
|
||||||
this.console = console;
|
this.console = console;
|
||||||
|
this.audioHost = audioHost;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -96,6 +100,11 @@ public interface HostServices {
|
|||||||
public Optional<ConsoleHost> console() {
|
public Optional<ConsoleHost> console() {
|
||||||
return Optional.ofNullable(console);
|
return Optional.ofNullable(console);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<AudioHost> audio() {
|
||||||
|
return Optional.ofNullable(audioHost);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import org.openautonomousconnection.luascript.fx.FxDomHost;
|
|||||||
import org.openautonomousconnection.luascript.fx.FxEventHost;
|
import org.openautonomousconnection.luascript.fx.FxEventHost;
|
||||||
import org.openautonomousconnection.luascript.fx.FxUiHost;
|
import org.openautonomousconnection.luascript.fx.FxUiHost;
|
||||||
import org.openautonomousconnection.luascript.fx.FxWebViewResourceHost;
|
import org.openautonomousconnection.luascript.fx.FxWebViewResourceHost;
|
||||||
|
import org.openautonomousconnection.luascript.hosts.AudioHost;
|
||||||
import org.openautonomousconnection.luascript.hosts.HostServices;
|
import org.openautonomousconnection.luascript.hosts.HostServices;
|
||||||
import org.openautonomousconnection.luascript.security.LuaExecutionPolicy;
|
import org.openautonomousconnection.luascript.security.LuaExecutionPolicy;
|
||||||
import org.openautonomousconnection.luascript.utils.LuaGlobalsFactory;
|
import org.openautonomousconnection.luascript.utils.LuaGlobalsFactory;
|
||||||
@@ -86,7 +87,8 @@ public final class FxLuaScriptEngine implements AutoCloseable {
|
|||||||
FxUiHost uiHost = new FxUiHost(engine, dom);
|
FxUiHost uiHost = new FxUiHost(engine, dom);
|
||||||
FxWebViewResourceHost resourceHost = new FxWebViewResourceHost(engine);
|
FxWebViewResourceHost resourceHost = new FxWebViewResourceHost(engine);
|
||||||
FxEventHost eventHost = new FxEventHost(dom);
|
FxEventHost eventHost = new FxEventHost(dom);
|
||||||
HostServices services = new HostServices.Default(uiHost, dom, eventHost, resourceHost, console);
|
// TODO: Default implementation or parameter for "audioHost"
|
||||||
|
HostServices services = new HostServices.Default(uiHost, dom, eventHost, resourceHost, console, audioHost);
|
||||||
LuaRuntime rt = new LuaRuntime(globals, services, policy);
|
LuaRuntime rt = new LuaRuntime(globals, services, policy);
|
||||||
eventHost.setRouter(rt.eventRouter());
|
eventHost.setRouter(rt.eventRouter());
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package org.openautonomousconnection.luascript.tables;
|
||||||
|
|
||||||
|
import org.luaj.vm2.LuaValue;
|
||||||
|
import org.luaj.vm2.lib.OneArgFunction;
|
||||||
|
import org.openautonomousconnection.luascript.hosts.AudioHost;
|
||||||
|
import org.openautonomousconnection.luascript.hosts.HostServices;
|
||||||
|
import org.openautonomousconnection.luascript.utils.ScriptTable;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class AudioTable extends ScriptTable {
|
||||||
|
protected AudioTable() {
|
||||||
|
super("audio");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void define(HostServices services) {
|
||||||
|
AudioHost audioHost = services.audio().orElseThrow(() -> new IllegalStateException("DomHost not provided"));
|
||||||
|
|
||||||
|
table().set("play", new OneArgFunction() {
|
||||||
|
@Override
|
||||||
|
public LuaValue call(LuaValue arg) {
|
||||||
|
String fileName = arg.checkjstring();
|
||||||
|
audioHost.play(new File(fileName));
|
||||||
|
return LuaValue.NIL;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user