-
Notifications
You must be signed in to change notification settings - Fork 18
/
Js.java
78 lines (60 loc) · 1.85 KB
/
Js.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
package com.cocos2dx.javascript;
import android.util.Log;
import android.widget.Toast;
import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge;
public class Js{
public static void call(String fname, Object ...args) {
if (fname == null || fname.isEmpty()) {
return;
}
String[] s = new String[args.length];
String str = "native.callback('" + fname + "', ";
for (Object o : args) {
if (o instanceof String) {
str += "'" + o.toString() + "', ";
} else {
str += o.toString() + ", ";
}
}
eval(str + "undefined)");
}
static private Cocos2dxActivity getCtx() {
return (Cocos2dxActivity) Cocos2dxActivity.getContext();
}
public static void callGlobal(String fname, Object ...args) {
if (fname == null || fname.isEmpty()) {
return;
}
String[] s = new String[args.length];
String str = fname + "(";
for (Object o : args) {
if (o instanceof String) {
str += "'" + o.toString() + "', ";
} else {
str += o.toString() + ", ";
}
}
eval(str + "undefined)");
}
public static void eval(final String str) {
getCtx().runOnGLThread(new Runnable() {
@Override
public void run() {
print(str);
Cocos2dxJavascriptJavaBridge.evalString(str);
}
});
}
public static void print(String s) {
Log.e("@.@", s);
}
public static void tip(final String s) {
getCtx().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getCtx(), s, Toast.LENGTH_LONG).show();
}
});
}
}