Skip to content

Commit

Permalink
Fixes #10 Basic framework for iOS binding
Browse files Browse the repository at this point in the history
  • Loading branch information
avh4 committed Sep 16, 2011
1 parent f87f451 commit 932f1c2
Show file tree
Hide file tree
Showing 19 changed files with 305 additions and 63 deletions.
2 changes: 1 addition & 1 deletion uilayer-android/AndroidManifest.xml
Expand Up @@ -15,7 +15,7 @@
</intent-filter>
</activity>

<activity android:name=".android.UILayerActivity">
<activity android:name=".android.AndroidSceneRendererActivity">
</activity>
</application>
</manifest>
@@ -1,9 +1,9 @@
package net.avh4.framework.uilayer;

import net.avh4.demo.uilayer.DemoGame;
import net.avh4.framework.uilayer.android.UILayerActivity;
import net.avh4.framework.uilayer.android.AndroidSceneRendererActivity;

public class DemoGameActivity extends UILayerActivity {
public class DemoGameActivity extends AndroidSceneRendererActivity {

public DemoGameActivity() {
super(new DemoGame());
Expand Down
Expand Up @@ -9,11 +9,11 @@
import android.graphics.Paint;
import android.view.View;

public class UILayerView extends View {
public class AndroidSceneRenderer extends View {

private final UI mUi;

public UILayerView(final Context context, final UI ui) {
public AndroidSceneRenderer(final Context context, final UI ui) {
super(context);
if (ui == null) {
throw new RuntimeException("UI must not be null");
Expand Down
Expand Up @@ -7,15 +7,15 @@
import android.view.Window;
import android.widget.ImageView;

public class UILayerActivity extends Activity {
public class AndroidSceneRendererActivity extends Activity {

private UI mUi;

public UILayerActivity() {
public AndroidSceneRendererActivity() {
mUi = null;
}

public UILayerActivity(final UI ui) {
public AndroidSceneRendererActivity(final UI ui) {
mUi = ui;
}

Expand All @@ -33,10 +33,10 @@ public void setUI(final UI ui) {
@Override
public void run() {
if (mUi != null) {
setContentView(new UILayerView(UILayerActivity.this, mUi));
setContentView(new AndroidSceneRenderer(AndroidSceneRendererActivity.this, mUi));
} else {
final ImageView placeholder = new ImageView(
UILayerActivity.this);
AndroidSceneRendererActivity.this);
placeholder.setImageResource(R.drawable.icon);
setContentView(placeholder);
}
Expand Down
Expand Up @@ -3,22 +3,22 @@
import static org.hamcrest.MatcherAssert.assertThat;
import net.avh4.framework.uilayer.UI;
import net.avh4.framework.uilayer.UILayer;
import net.avh4.framework.uilayer.android.UILayerActivity;
import net.avh4.framework.uilayer.android.AndroidSceneRendererActivity;
import net.avh4.framework.uilayer.scene.Scene;

import org.hamcrest.Matcher;

import android.test.ActivityInstrumentationTestCase2;

public class AndroidSceneRendererTest extends
ActivityInstrumentationTestCase2<UILayerActivity> {
ActivityInstrumentationTestCase2<AndroidSceneRendererActivity> {

private static final int STATUS_BAR_HEIGHT = 50;
private UI mUi;
private Scene mScene;

public AndroidSceneRendererTest() {
super(UILayerActivity.class);
super(AndroidSceneRendererActivity.class);
}

@Override
Expand Down Expand Up @@ -53,7 +53,7 @@ public void testRenderPlaceholders() throws Exception {
assertThat(getActivity(), isApproved());
}

private Matcher<? super UILayerActivity> isApproved() {
private Matcher<? super AndroidSceneRendererActivity> isApproved() {
return net.avh4.util.imagecomparison.android.Matchers
.isApproved(getInstrumentation().getContext());
}
Expand Down
2 changes: 1 addition & 1 deletion uilayer-core/pom.xml
Expand Up @@ -8,7 +8,7 @@

<groupId>net.avh4.framework</groupId>
<artifactId>uilayer-core</artifactId>
<version>0.0.3</version>
<version>0.0.4-SNAPSHOT</version>
<packaging>jar</packaging>

<name>uilayer-core</name>
Expand Down
Expand Up @@ -6,7 +6,12 @@ public class UILayer {

private static final String SWING_SERVICE = "net.avh4.framework.uilayer.swing.SwingUILayerService";
private static final String ANDROID_SERVICE = "net.avh4.framework.uilayer.android.AndroidUILayerService";
@SuppressWarnings("unused")
private static final String IOS_SERVICE = "net.avh4.framework.uilayer.ios.IOSUILayerService";

// IOS_SERVICE is not added here because xmlvm does not implement
// ClassLoader for iOS. Therefore we must replace this entire class in
// uilayer-ios-xmlvm and not rely on dynamic loading.
private static final String[] KNOWN_SERVICES = new String[] {
SWING_SERVICE, ANDROID_SERVICE };

Expand Down Expand Up @@ -52,7 +57,7 @@ public static Scene newScene(final String title) {

public static void main(final SceneCreator game,
final ClickReceiver receiver, final KeyReceiver keyReceiver) {
service.main(game, receiver, keyReceiver);
service.run(game, receiver, keyReceiver);
}

}
Expand Up @@ -4,7 +4,7 @@

public interface UILayerService {

void main(SceneCreator game, ClickReceiver receiver, KeyReceiver keyReceiver);
void run(SceneCreator game, ClickReceiver receiver, KeyReceiver keyReceiver);

Scene newScene(String title);

Expand Down
2 changes: 2 additions & 0 deletions uilayer-ios-xmlvm/nbproject/build-Xcode.xml
Expand Up @@ -55,6 +55,8 @@
<echo message="Creating Xcode project with the ${xmlvm.backend.canonical} backend."/>
<java jar="${xmlvm.sdk.jar}" fork="true" failonerror="true">
<jvmarg value="-Xmx512m"/>
<arg value="--in=${file.reference.uilayer-core-0.0.4-SNAPSHOT.jar}"/>
<!-- classes must come after uilayer-core, since we are replacing net.avh4.framework.uilayer.UILayer with an ios-xmlvm-specific implementation -->
<arg value="--in=${out.dir}/classes"/>
<arg value="--out=${out.dir}/.xcode"/>
<arg value="--target=${xmlvm.backend.target}"/>
Expand Down
7 changes: 5 additions & 2 deletions uilayer-ios-xmlvm/nbproject/project.properties
Expand Up @@ -2,6 +2,7 @@ annotation.processing.enabled=true
annotation.processing.enabled.in.editor=false
annotation.processing.run.all.processors=true
endorsed.classpath=
file.reference.uilayer-core-0.0.4-SNAPSHOT.jar=/Users/avh4/.m2/repository/net/avh4/framework/uilayer-core/0.0.4-SNAPSHOT/uilayer-core-0.0.4-SNAPSHOT.jar
javac.processorpath=\
${javac.classpath}
mkdist.disabled=false
Expand Down Expand Up @@ -45,7 +46,9 @@ dist.javadoc.dir=${dist.dir}/javadoc
excludes=
includes=**
jar.compress=true
javac.classpath=${xmlvm.sdk.jar}
javac.classpath=\
${xmlvm.sdk.jar}:\
${file.reference.uilayer-core-0.0.4-SNAPSHOT.jar}
# Space-separated list of extra javac options
javac.compilerargs=
javac.deprecation=false
Expand All @@ -65,7 +68,7 @@ javadoc.splitindex=true
javadoc.use=true
javadoc.version=false
javadoc.windowtitle=
main.class=my.hellothere.Main
main.class=net.avh4.demo.uilayer.DemoGame
manifest.file=manifest.mf
meta.inf.dir=${src.java.dir}/META-INF
platform.active=default_platform
Expand Down
5 changes: 5 additions & 0 deletions uilayer-ios-xmlvm/pom.xml
Expand Up @@ -64,5 +64,10 @@
<version>0.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.avh4.framework</groupId>
<artifactId>uilayer-core</artifactId>
<version>0.0.4-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,40 @@
package net.avh4.demo.uilayer;

import net.avh4.framework.uilayer.UI;
import net.avh4.framework.uilayer.UILayer;
import net.avh4.framework.uilayer.scene.Scene;

public class DemoGame implements UI {

public static void main(final String[] args) {
final DemoGame game = new DemoGame();
UILayer.run(game);
}

private Scene s;

public DemoGame() {
s = UILayer.newScene("UILayer Demo Game");
s.addImage(0, 0, 640, 960, "background.jpg");
click(100, 100);
}

@Override
public Scene getScene() {
return s;
}

@Override
public void click(final int x, final int y) {
s.addPlaceholder("Box", x, y, 50, 50);
}

@Override
public void key(final int keyCode) {
// if (keyCode == KeyEvent.VK_SPACE) {
s = UILayer.newScene("UILayer Demo Game");
s.addImage(0, 0, 640, 960, "background.jpg");
// }
}

}
@@ -0,0 +1,32 @@
package net.avh4.framework.uilayer;

import net.avh4.framework.uilayer.ios.IOSUILayerService;
import net.avh4.framework.uilayer.scene.Scene;

/**
* This class replaces the version in uilayer-core when compiling for xmlvm
* because the uilayer-core version uses ClassLoader, which is not implemented
* for iOS with xmlvm.
*/
public class UILayer {

public static final UILayerService service = loadService();

public static void run(final UI game) {
UILayer.run(game, game, game);
}

private static UILayerService loadService() {
return new IOSUILayerService();
}

public static Scene newScene(final String title) {
return service.newScene(title);
}

public static void run(final SceneCreator game,
final ClickReceiver receiver, final KeyReceiver keyReceiver) {
service.run(game, receiver, keyReceiver);
}

}
@@ -0,0 +1,36 @@
package net.avh4.framework.uilayer.ios;

import net.avh4.framework.uilayer.scene.SceneObject;

import org.xmlvm.iphone.CGContext;
import org.xmlvm.iphone.CGRect;
import org.xmlvm.iphone.UIGraphics;

public class IOSPlaceholder extends SceneObject<CGRect> {

private static final int MARGIN = 5;
private final String name;
private final float[] color = new float[] { .2f, .2f, .2f, 1 };
private final float[] textColor = new float[] { 1, 1, 1, 1 };

public IOSPlaceholder(final String name, final int x, final int y,
final int width, final int height) {
super(x, y, width, height);
this.name = name;
}

@Override
public void draw(final CGRect rect) {
final CGContext g = UIGraphics.getCurrentContext();
g.setFillColor(color);
g.fillRect(new CGRect(x, y, width, height));
g.setStrokeColor(new float[] { 0, 0, 0, 1 });
g.strokeRect(new CGRect(x, y, width, height));

g.selectFont("Helvetica", 10);
g.setTextDrawingMode(CGContext.kCGTextFill);
g.setFillColor(textColor);
g.showTextAtPoint(x + MARGIN, y + height - MARGIN, name);
}

}
@@ -0,0 +1,68 @@
package net.avh4.framework.uilayer.ios;

import java.util.ArrayList;
import java.util.Iterator;

import net.avh4.framework.uilayer.scene.Scene;
import net.avh4.framework.uilayer.scene.SceneImage;
import net.avh4.framework.uilayer.scene.SceneObject;

import org.xmlvm.iphone.CGRect;

public class IOSScene implements Scene, Iterable<SceneObject<CGRect>> {

private final ArrayList<SceneObject<CGRect>> children = new ArrayList<SceneObject<CGRect>>();
private final String title;

public IOSScene(final String title) {
this.title = title;
}

@Override
public Iterator<SceneObject<CGRect>> iterator() {
return children.iterator();
}

@Override
public String getTitle() {
return title;
}

@Override
public int getWidth() {
return 640;
}

@Override
public int getHeight() {
return 960;
}

@Override
public void addText(final String text, final int x, final int y,
final int width, final String customFontResource, final int fontSize) {
addPlaceholder(text, x, y, width, 10);
}

@Override
public void addPlaceholder(final String name, final int x, final int y,
final int width, final int height) {
children.add(new IOSPlaceholder(name, x, y, width, height));
}

@Override
public SceneImage addImage(final int x, final int y, final int width,
final int height, final String imageName, final int clipX,
final int clipY, final int clipWidth, final int clipHeight) {
addPlaceholder(imageName, x, y, width, height);
return null;
}

@Override
public SceneImage addImage(final int x, final int y, final int width,
final int height, final String imageName) {
addPlaceholder(imageName, x, y, width, height);
return null;
}

}

0 comments on commit 932f1c2

Please sign in to comment.