127 lines
3.6 KiB
Java
127 lines
3.6 KiB
Java
package org.openautonomousconnection.luascript.tables;
|
|
|
|
import org.luaj.vm2.LuaValue;
|
|
import org.luaj.vm2.Varargs;
|
|
import org.luaj.vm2.libs.VarArgFunction;
|
|
import org.luaj.vm2.libs.ZeroArgFunction;
|
|
import org.openautonomousconnection.luascript.hosts.HostServices;
|
|
import org.openautonomousconnection.luascript.hosts.VideoHost;
|
|
import org.openautonomousconnection.luascript.utils.ScriptTable;
|
|
|
|
import java.io.File;
|
|
|
|
/**
|
|
* Lua table: video
|
|
*
|
|
* <p>Functions:</p>
|
|
* <ul>
|
|
* <li>video.play(pathOrUrl)</li>
|
|
* <li>video.pause()</li>
|
|
* <li>video.resume()</li>
|
|
* <li>video.stop()</li>
|
|
* <li>video.hide()</li>
|
|
* <li>video.rect(x, y, w, h)</li>
|
|
* <li>video.volume(v)</li>
|
|
* <li>video.loop(boolean)</li>
|
|
* <li>video.seek(seconds)</li>
|
|
* </ul>
|
|
*/
|
|
public final class VideoTable extends ScriptTable {
|
|
|
|
public VideoTable() {
|
|
super("video");
|
|
}
|
|
|
|
@Override
|
|
protected void define(HostServices services) {
|
|
VideoHost host = services.video()
|
|
.orElseThrow(() -> new IllegalStateException("VideoHost not provided"));
|
|
|
|
table().set("play", new VarArgFunction() {
|
|
@Override
|
|
public Varargs invoke(Varargs args) {
|
|
String src = args.arg(1).checkjstring();
|
|
if (looksLikeUrl(src)) host.playUrl(src);
|
|
else host.playFile(new File(src));
|
|
return LuaValue.NIL;
|
|
}
|
|
});
|
|
|
|
table().set("pause", new ZeroArgFunction() {
|
|
@Override
|
|
public LuaValue call() {
|
|
host.pause();
|
|
return LuaValue.NIL;
|
|
}
|
|
});
|
|
|
|
table().set("resume", new ZeroArgFunction() {
|
|
@Override
|
|
public LuaValue call() {
|
|
host.resume();
|
|
return LuaValue.NIL;
|
|
}
|
|
});
|
|
|
|
table().set("stop", new ZeroArgFunction() {
|
|
@Override
|
|
public LuaValue call() {
|
|
host.stop();
|
|
return LuaValue.NIL;
|
|
}
|
|
});
|
|
|
|
table().set("hide", new ZeroArgFunction() {
|
|
@Override
|
|
public LuaValue call() {
|
|
host.hide();
|
|
return LuaValue.NIL;
|
|
}
|
|
});
|
|
|
|
table().set("rect", new VarArgFunction() {
|
|
@Override
|
|
public Varargs invoke(Varargs args) {
|
|
double x = args.arg(1).checkdouble();
|
|
double y = args.arg(2).checkdouble();
|
|
double w = args.arg(3).checkdouble();
|
|
double h = args.arg(4).checkdouble();
|
|
host.setRect(x, y, w, h);
|
|
return LuaValue.NIL;
|
|
}
|
|
});
|
|
|
|
table().set("volume", new VarArgFunction() {
|
|
@Override
|
|
public Varargs invoke(Varargs args) {
|
|
double v = args.arg(1).checkdouble();
|
|
host.setVolume(v);
|
|
return LuaValue.NIL;
|
|
}
|
|
});
|
|
|
|
table().set("loop", new VarArgFunction() {
|
|
@Override
|
|
public Varargs invoke(Varargs args) {
|
|
boolean b = args.arg(1).checkboolean();
|
|
host.setLoop(b);
|
|
return LuaValue.NIL;
|
|
}
|
|
});
|
|
|
|
table().set("seek", new VarArgFunction() {
|
|
@Override
|
|
public Varargs invoke(Varargs args) {
|
|
double s = args.arg(1).checkdouble();
|
|
host.seek(s);
|
|
return LuaValue.NIL;
|
|
}
|
|
});
|
|
}
|
|
|
|
private static boolean looksLikeUrl(String s) {
|
|
if (s == null) return false;
|
|
int idx = s.indexOf("://");
|
|
return idx > 0 && idx < 16;
|
|
}
|
|
} |