summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-26 14:35:41 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-02-26 14:35:41 +0100
commit8672b318c3dc8b7e0cac47014e5bbb3527d0db87 (patch)
tree093d2aa177eac5eafd9897cdbde93f65c68d3d67
parent930596730c3df733dc5c5601579fd044f55682c8 (diff)
Add a graphics demo.
Uses a variable to hold a position and draws graphics based on the variable. Works on multiple devices.
-rw-r--r--same-android/AndroidManifest.xml1
-rw-r--r--same-android/res/menu/main_menu.xml4
-rw-r--r--same-android/src/main/java/com/orbekk/same/GameView.java59
-rw-r--r--same-android/src/main/java/com/orbekk/same/GraphicsActivity.java34
-rw-r--r--same-android/src/main/java/com/orbekk/same/MainActivity.java3
-rw-r--r--same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java4
6 files changed, 90 insertions, 15 deletions
diff --git a/same-android/AndroidManifest.xml b/same-android/AndroidManifest.xml
index ea233e5..dcd28b2 100644
--- a/same-android/AndroidManifest.xml
+++ b/same-android/AndroidManifest.xml
@@ -21,6 +21,7 @@
<activity android:name=".SameControllerActivity"/>
<activity android:name=".VariableTestActivity"/>
<activity android:name=".StateViewerActivity"/>
+ <activity android:name=".GraphicsActivity"/>
<service android:name=".SameService" />
</application>
diff --git a/same-android/res/menu/main_menu.xml b/same-android/res/menu/main_menu.xml
index 670ebac..4235d2b 100644
--- a/same-android/res/menu/main_menu.xml
+++ b/same-android/res/menu/main_menu.xml
@@ -10,5 +10,9 @@
<item
android:id="@+id/state_viewer"
android:title="State Monitor"/>
+
+ <item
+ android:id="@+id/graphics_demo"
+ android:title="Graphics demo"/>
</menu> \ No newline at end of file
diff --git a/same-android/src/main/java/com/orbekk/same/GameView.java b/same-android/src/main/java/com/orbekk/same/GameView.java
index 597bbc7..527d3da 100644
--- a/same-android/src/main/java/com/orbekk/same/GameView.java
+++ b/same-android/src/main/java/com/orbekk/same/GameView.java
@@ -6,7 +6,6 @@ import org.slf4j.LoggerFactory;
import com.orbekk.same.State.Component;
import com.orbekk.same.ClientService;
import com.orbekk.same.Client;
-import com.orbekk.same.SameInterface;
import com.orbekk.same.UpdateConflict;
import android.content.Context;
@@ -15,29 +14,48 @@ import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
+import android.widget.Toast;
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private Logger logger = LoggerFactory.getLogger(getClass());
private GameThread thread;
+ static class Player {
+ public float posX;
+ public float posY;
+ }
+
static class GameThread extends Thread
- implements GameController.ChangeListener {
+ implements Variable.OnChangeListener<Player> {
private Logger logger = LoggerFactory.getLogger(getClass());
private int height = 0;
private int width = 0;
private SurfaceHolder holder;
private Context context;
private Paint background;
- private GameController controller;
+ private Variable<Player> player;
+ private Paint color = new Paint();
public GameThread(SurfaceHolder holder, Context context,
- GameController controller) {
+ Variable<Player> player) {
this.holder = holder;
this.context = context;
- this.controller = controller;
- this.controller.setChangeListener(this);
+ this.player = player;
background = new Paint();
background.setARGB(255, 0, 0, 0);
+ color.setARGB(255, 255, 0, 0);
+ }
+
+ public void setUp() {
+ Player player_ = new Player();
+ player_.posX = 0.5f;
+ player_.posY = 0.5f;
+ player.setOnChangeListener(this);
+ try {
+ player.set(player_);
+ } catch (UpdateConflict e) {
+ e.printStackTrace();
+ }
}
public void setSize(int width, int height) {
@@ -49,12 +67,12 @@ public class GameView extends SurfaceView implements SurfaceHolder.Callback {
private void doDraw(Canvas c) {
c.drawRect(0.0f, 0.0f, width+1.0f, height+1.0f, background);
- for (GameController.Player p : controller.getRemotePlayers()) {
- c.drawCircle(p.posX * width, p.posY * height, 20.0f, p.color);
+ Player player_ = player.get();
+ if (player_ == null) {
+ return;
}
- GameController.Player localPlayer = controller.getLocalPlayer();
- c.drawCircle(localPlayer.posX * width, localPlayer.posY * height,
- 20.0f, localPlayer.color);
+ c.drawCircle(player_.posX * width, player_.posY * height,
+ 20.0f, color);
}
@Override public void run() {
@@ -70,19 +88,30 @@ public class GameView extends SurfaceView implements SurfaceHolder.Callback {
}
private synchronized void setPosition(float x, float y) {
-// controller.setMyPosition(x / this.width, y / this.height);
+ Player newPlayer = new Player();
+ newPlayer.posX = x / width;
+ newPlayer.posY = y / width;
+ try {
+ player.set(newPlayer);
+ } catch (UpdateConflict e) {
+ Toast.makeText(context, "Failed to update position.",
+ Toast.LENGTH_SHORT).show();
+ }
}
@Override
- public void playerStatesChanged() {
+ public void valueChanged(Variable<Player> unused) {
+ logger.info("Variable updated.");
+ player.update();
run();
}
}
- public GameView(Context context, GameController controller) {
+ public GameView(Context context, Variable<Player> player) {
super(context);
getHolder().addCallback(this);
- thread = new GameThread(getHolder(), context, controller);
+ thread = new GameThread(getHolder(), context, player);
+ thread.setUp();
}
@Override
diff --git a/same-android/src/main/java/com/orbekk/same/GraphicsActivity.java b/same-android/src/main/java/com/orbekk/same/GraphicsActivity.java
new file mode 100644
index 0000000..7f747a1
--- /dev/null
+++ b/same-android/src/main/java/com/orbekk/same/GraphicsActivity.java
@@ -0,0 +1,34 @@
+package com.orbekk.same;
+
+import org.codehaus.jackson.type.TypeReference;
+
+import com.orbekk.same.android.ClientInterfaceBridge;
+
+import android.app.Activity;
+import android.os.Bundle;
+
+public class GraphicsActivity extends Activity {
+ private GameView gameView;
+ private ClientInterfaceBridge client;
+
+ @Override
+ public void onCreate(Bundle savedBundle) {
+ super.onCreate(savedBundle);
+ }
+
+ public void onResume() {
+ super.onResume();
+ client = new ClientInterfaceBridge(this);
+ client.connect();
+ TypeReference playerType = new TypeReference<GameView.Player>() {};
+ Variable<GameView.Player> player = client.createVariableFactory()
+ .create("Player", playerType);
+ gameView = new GameView(this, player);
+ setContentView(gameView);
+ }
+
+ public void onStop() {
+ super.onStop();
+ client.disconnect();
+ }
+}
diff --git a/same-android/src/main/java/com/orbekk/same/MainActivity.java b/same-android/src/main/java/com/orbekk/same/MainActivity.java
index a8624ce..5c1f144 100644
--- a/same-android/src/main/java/com/orbekk/same/MainActivity.java
+++ b/same-android/src/main/java/com/orbekk/same/MainActivity.java
@@ -36,6 +36,9 @@ public class MainActivity extends Activity {
case R.id.state_viewer:
startActivity(new Intent(this, StateViewerActivity.class));
break;
+ case R.id.graphics_demo:
+ startActivity(new Intent(this, GraphicsActivity.class));
+ break;
default:
logger.error("Unknown menu entry: {}", item);
}
diff --git a/same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java b/same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java
index cbfeff3..4dde60c 100644
--- a/same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java
+++ b/same-android/src/main/java/com/orbekk/same/android/ClientInterfaceBridge.java
@@ -121,6 +121,10 @@ public class ClientInterfaceBridge implements ClientInterface {
public void set(Component component) throws UpdateConflict {
Message message = Message.obtain(null, SameService.SET_STATE);
message.obj = component;
+ if (serviceMessenger == null) {
+ logger.warn("Not connected to service. Ignore update: {}", component);
+ return;
+ }
try {
serviceMessenger.send(message);
} catch (RemoteException e) {