Add example android project.
This commit is contained in:
3
examples/android/.cvsignore
Normal file
3
examples/android/.cvsignore
Normal file
@@ -0,0 +1,3 @@
|
||||
bin
|
||||
gen
|
||||
libs
|
||||
27
examples/android/AndroidManifest.xml
Normal file
27
examples/android/AndroidManifest.xml
Normal file
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="org.luaj.vm2.android"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0" >
|
||||
|
||||
<uses-sdk
|
||||
android:minSdkVersion="11"
|
||||
android:targetSdkVersion="11" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@drawable/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/AppTheme" >
|
||||
<activity
|
||||
android:name="org.luaj.vm2.android.LuajActivity"
|
||||
android:label="@string/app_name" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
152
examples/android/assets/activity.lua
Normal file
152
examples/android/assets/activity.lua
Normal file
@@ -0,0 +1,152 @@
|
||||
-- Simple lua script that is executed when the activity start.
|
||||
--
|
||||
-- Arguments are the activity and the view.
|
||||
local activity, view = ...
|
||||
print('activity', activity, 'view', view)
|
||||
|
||||
-- forward declarations of local functions and variables
|
||||
local animate,render
|
||||
local chars = {}
|
||||
local W, H = 600, 800
|
||||
|
||||
-- called on key down event
|
||||
function onKeyDown(keyCode, event)
|
||||
print('onKeyDown', keyCode, event)
|
||||
end
|
||||
|
||||
-- called on key up event
|
||||
function onKeyUp(keyCode, event)
|
||||
print('onKeyUp', keyCode, event)
|
||||
end
|
||||
|
||||
-- called on touch event
|
||||
function onTouchEvent(event)
|
||||
-- print('onTouchEvent', event)
|
||||
local x1, y1 = event:getX(), event:getY()
|
||||
local i,j = math.floor(x1*256/W), math.floor(y1*256/H)
|
||||
local u = i + j * 256
|
||||
local c = u < 0x80 and string.char(u) or
|
||||
u < 0x800 and string.char(0xc0 + math.floor(u/0x80) % 0x20,
|
||||
u % 0x80 ) or
|
||||
string.char(0xe0 + math.floor(u/0x2000) % 0x8,
|
||||
0x80 + math.floor(u/0x80) % 0x80,
|
||||
u % 0x80)
|
||||
while #chars > 4 do table.remove(chars, 1) end
|
||||
table.insert(chars, {
|
||||
n=255,
|
||||
x=-4+12*math.random(),
|
||||
y=-8+12*math.random(),
|
||||
text=c})
|
||||
return true
|
||||
end
|
||||
|
||||
-- called on trackball event
|
||||
function onTrackballEvent(event)
|
||||
print('onTrackballEvent', event)
|
||||
return true
|
||||
end
|
||||
|
||||
function onWindowFocusChanged(hasWindowFocus)
|
||||
print('onWindowFocusChanged', hasWindowFocus)
|
||||
end
|
||||
|
||||
function onWindowSystemUiVisibilityChanged(visible)
|
||||
print('onWindowSystemUiVisibilityChanged', visible)
|
||||
end
|
||||
|
||||
-- called to draw the lua view
|
||||
local prev, interval = os.time(), 1/60
|
||||
local Thread = luajava.bindClass('java.lang.Thread')
|
||||
function draw(canvas)
|
||||
view:invalidate()
|
||||
local curr = os.time()
|
||||
local diff = curr - prev
|
||||
if diff >= interval then
|
||||
pcall(animate, canvas)
|
||||
local wait = math.floor(1000 * (prev + interval - os.time()))
|
||||
if wait > 0 then pcall(Thread.sleep, Thread, wait) end
|
||||
prev = os.time()
|
||||
end
|
||||
pcall(render, canvas)
|
||||
end
|
||||
|
||||
-- the animation step moves the line endpoints
|
||||
local x1,y1,x2,y2,xi,yi = 160,240,480,240,0,0
|
||||
local vx1,vy1,vx2,vy2,vxi,vyi = -5,-6,7,8,3,1
|
||||
local w, h = W, H
|
||||
local advance = function(x,vx,max,rnd)
|
||||
x = x + vx
|
||||
if x < 0 then
|
||||
return 0, math.random(2,10)
|
||||
elseif x > max then
|
||||
return max, math.random(-10,-2)
|
||||
end
|
||||
return x, vx
|
||||
end
|
||||
animate = function(canvas)
|
||||
x1,y1,x2,y2 = x1+1,y1+1,x2-1,y2-1
|
||||
x1,vx1 = advance(x1,vx1,w)
|
||||
y1,vy1 = advance(y1,vy1,h)
|
||||
x2,vx2 = advance(x2,vx2,w)
|
||||
y2,vy2 = advance(y2,vy2,h)
|
||||
xi,vxi = advance(xi,vxi,w-100)
|
||||
yi,vyi = advance(yi,vyi,h-100)
|
||||
while #chars > 0 and chars[1].n <= 1 do
|
||||
table.remove(chars, 1)
|
||||
end
|
||||
for i,c in pairs(chars) do
|
||||
c.n = c.n - 1
|
||||
end
|
||||
end
|
||||
|
||||
-- classes that we need for rendering
|
||||
local Color = luajava.bindClass('android.graphics.Color')
|
||||
local Paint = luajava.bindClass('android.graphics.Paint')
|
||||
local Style = luajava.bindClass('android.graphics.Paint$Style')
|
||||
print('Color, Paint, Style', Color, Paint, Style)
|
||||
|
||||
-- colors that we use
|
||||
local text_color, line_color = Paint.new(), Paint.new()
|
||||
text_color:setColor(0xffffff33)
|
||||
text_color:setTextSize(8.0)
|
||||
line_color:setColor(0xffffaa33)
|
||||
line_color:setStrokeWidth(1.5)
|
||||
line_color:setStyle(Style.STROKE)
|
||||
print('text_color, line_color', text_color, line_color)
|
||||
|
||||
-- load the logo image
|
||||
local istream = view:findResource('logo.gif')
|
||||
print('istream', istream)
|
||||
local BitmapFactory = luajava.bindClass('android.graphics.BitmapFactory')
|
||||
local logo = BitmapFactory:decodeStream(istream)
|
||||
print('logo', logo)
|
||||
|
||||
-- the render step draws the scene
|
||||
render = function(canvas)
|
||||
-- scale the drawing to approximagely 600 x 800
|
||||
W, H = canvas:getWidth(), canvas:getHeight();
|
||||
local scale = (W + H) / (600 + 800)
|
||||
canvas:scale(scale, scale)
|
||||
w, h = W / scale, H / scale
|
||||
|
||||
-- redraw the canvas
|
||||
canvas:drawColor(0xff112244)
|
||||
|
||||
-- line
|
||||
canvas:drawLine(x1, y1, x2, y2, line_color)
|
||||
|
||||
-- text
|
||||
canvas:translate(w/2,h/2)
|
||||
for i,c in pairs(chars) do
|
||||
local s = 200 / (256-c.n)
|
||||
canvas:scale(s, s)
|
||||
canvas:drawText(c.text, c.x-4, c.y+6, text_color)
|
||||
canvas:scale(1/s, 1/s)
|
||||
end
|
||||
canvas:translate(-w/2,-h/2)
|
||||
|
||||
-- image
|
||||
canvas:drawBitmap(logo,xi,yi)
|
||||
end
|
||||
|
||||
|
||||
BIN
examples/android/assets/logo.gif
Normal file
BIN
examples/android/assets/logo.gif
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.6 KiB |
26
examples/android/build.xml
Normal file
26
examples/android/build.xml
Normal file
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- Ant build file for sample Android project that includes luaj.
|
||||
-->
|
||||
<project name="LuajActivity" default="help">
|
||||
<property name="luaj.home" location="../.."/>
|
||||
<property name="luaj.lib" location="${luaj.home}/lib"/>
|
||||
<property file="../../version.properties"/>
|
||||
|
||||
<!-- location of the android sdk -->
|
||||
<property environment="env" />
|
||||
<property name="sdk.dir" value="${env.ANDROID_HOME}"/>
|
||||
<echo>sdk.dir: ${sdk.dir}</echo>
|
||||
|
||||
<!-- android project properties -->
|
||||
<property name="target" value="android-19"/>
|
||||
|
||||
<!-- prior to compiling copys the luaj jar into the 'libs' directory -->
|
||||
<target name="pre-compile">
|
||||
<mkdir dir="libs"/>
|
||||
<copy file="${luaj.lib}/luaj-jse-${version}.jar" todir="libs"/>
|
||||
</target>
|
||||
|
||||
<!-- version-tag: 1 -->
|
||||
<import file="${sdk.dir}/tools/ant/build.xml" />
|
||||
|
||||
</project>
|
||||
BIN
examples/android/res/drawable-hdpi/ic_launcher.png
Normal file
BIN
examples/android/res/drawable-hdpi/ic_launcher.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.3 KiB |
BIN
examples/android/res/drawable-mdpi/ic_launcher.png
Normal file
BIN
examples/android/res/drawable-mdpi/ic_launcher.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.5 KiB |
BIN
examples/android/res/drawable-xhdpi/ic_launcher.png
Normal file
BIN
examples/android/res/drawable-xhdpi/ic_launcher.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.2 KiB |
BIN
examples/android/res/drawable-xxhdpi/ic_launcher.png
Normal file
BIN
examples/android/res/drawable-xxhdpi/ic_launcher.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 11 KiB |
9
examples/android/res/menu/luaj.xml
Normal file
9
examples/android/res/menu/luaj.xml
Normal file
@@ -0,0 +1,9 @@
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
|
||||
|
||||
<item
|
||||
android:id="@+id/action_settings"
|
||||
android:orderInCategory="100"
|
||||
android:showAsAction="never"
|
||||
android:title="@string/action_settings"/>
|
||||
|
||||
</menu>
|
||||
7
examples/android/res/values/dimens.xml
Normal file
7
examples/android/res/values/dimens.xml
Normal file
@@ -0,0 +1,7 @@
|
||||
<resources>
|
||||
|
||||
<!-- Default screen margins, per the Android Design guidelines. -->
|
||||
<dimen name="activity_horizontal_margin">16dp</dimen>
|
||||
<dimen name="activity_vertical_margin">16dp</dimen>
|
||||
|
||||
</resources>
|
||||
8
examples/android/res/values/strings.xml
Normal file
8
examples/android/res/values/strings.xml
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<string name="app_name">Luaj</string>
|
||||
<string name="action_settings">Settings</string>
|
||||
<string name="hello_world">Hello world!</string>
|
||||
|
||||
</resources>
|
||||
20
examples/android/res/values/styles.xml
Normal file
20
examples/android/res/values/styles.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<resources>
|
||||
|
||||
<!--
|
||||
Base application theme, dependent on API level. This theme is replaced
|
||||
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
|
||||
-->
|
||||
<style name="AppBaseTheme" parent="android:Theme.Light">
|
||||
<!--
|
||||
Theme customizations available in newer API levels can go in
|
||||
res/values-vXX/styles.xml, while customizations related to
|
||||
backward-compatibility can go here.
|
||||
-->
|
||||
</style>
|
||||
|
||||
<!-- Application theme. -->
|
||||
<style name="AppTheme" parent="AppBaseTheme">
|
||||
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
|
||||
</style>
|
||||
|
||||
</resources>
|
||||
30
examples/android/src/android/LuajActivity.java
Normal file
30
examples/android/src/android/LuajActivity.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package org.luaj.vm2.android;
|
||||
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.lib.jse.CoerceJavaToLua;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.view.Menu;
|
||||
|
||||
public class LuajActivity extends Activity {
|
||||
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
LuajView view = new LuajView(this);
|
||||
setContentView(view);
|
||||
try {
|
||||
LuaValue activity = CoerceJavaToLua.coerce(this);
|
||||
LuaValue viewobj = CoerceJavaToLua.coerce(view);
|
||||
view.globals.loadfile("activity.lua").call(activity, viewobj);
|
||||
} catch ( Exception e ) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.luaj, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
122
examples/android/src/android/LuajView.java
Normal file
122
examples/android/src/android/LuajView.java
Normal file
@@ -0,0 +1,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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user