diff options
Diffstat (limited to 'same-android/src/main/java')
4 files changed, 85 insertions, 15 deletions
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) { |