-
Notifications
You must be signed in to change notification settings - Fork 0
/
LuajView.java
122 lines (107 loc) · 2.85 KB
/
LuajView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package org.luaj.vm2.android;
import java.io.InputStream;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.ResourceFinder;
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
import org.luaj.vm2.lib.jse.JsePlatform;
import android.content.Context;
import android.graphics.Canvas;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
public class LuajView extends View implements ResourceFinder {
public final Globals globals;
public LuajView(Context context) {
super(context);
this.globals = JsePlatform.standardGlobals();
this.globals.finder = this;
}
// Implement a finder that loads from the assets directory.
public InputStream findResource(String name) {
try {
return getContext().getAssets().open(name);
} catch (java.io.IOException ioe) {
return null;
}
}
public void draw(Canvas canvas) {
LuaValue f = globals.get("draw");
if (!f.isnil())
try {
f.call(CoerceJavaToLua.coerce(canvas));
} catch (Exception e) {
e.printStackTrace();
}
else
super.draw(canvas);
}
public boolean f(int keyCode, KeyEvent event) {
LuaValue f = globals.get("onKeyDown");
if (!f.isnil())
try {
return f.call(CoerceJavaToLua.coerce(keyCode),
CoerceJavaToLua.coerce(event)).toboolean();
} catch (Exception e) {
e.printStackTrace();
return true;
}
else
return super.onKeyDown(keyCode, event);
}
public boolean onKeyUp(int keyCode, KeyEvent event) {
LuaValue f = globals.get("onKeyUp");
if (!f.isnil())
try {
return f.call(CoerceJavaToLua.coerce(keyCode),
CoerceJavaToLua.coerce(event)).toboolean();
} catch (Exception e) {
e.printStackTrace();
return true;
}
else
return super.onKeyUp(keyCode, event);
}
public boolean onTouchEvent(MotionEvent event) {
LuaValue f = globals.get("onTouchEvent");
if (!f.isnil())
try {
return f.call(CoerceJavaToLua.coerce(event)).toboolean();
} catch (Exception e) {
e.printStackTrace();
return true;
}
else
return super.onTouchEvent(event);
}
public boolean onTrackballEvent(MotionEvent event) {
LuaValue f = globals.get("onTrackballEvent");
if (!f.isnil())
try {
return f.call(CoerceJavaToLua.coerce(event)).toboolean();
} catch (Exception e) {
e.printStackTrace();
return true;
}
else
return super.onTrackballEvent(event);
}
public void onWindowFocusChanged(boolean hasWindowFocus) {
LuaValue f = globals.get("onWindowFocusChanged");
if (!f.isnil())
try {
f.call(CoerceJavaToLua.coerce(hasWindowFocus));
} catch (Exception e) {
e.printStackTrace();
}
}
public void onWindowSystemUiVisibilityChanged(int visible) {
LuaValue f = globals.get("onWindowSystemUiVisibilityChanged");
if (!f.isnil())
try {
f.call(CoerceJavaToLua.coerce(visible));
} catch (Exception e) {
e.printStackTrace();
}
}
}