Skip to content

Commit

Permalink
Added PlayN.LifecycleListener (onPause, onResume, onExit).
Browse files Browse the repository at this point in the history
It's implemented in Android, iOS, and Java. TBD on HTML and Flash. In Java, the
app is paused and resumed every time the window loses and regains focus.
  • Loading branch information
samskivert committed Jun 22, 2012
1 parent 3829b13 commit c4310a8
Show file tree
Hide file tree
Showing 16 changed files with 329 additions and 114 deletions.
28 changes: 11 additions & 17 deletions android/src/playn/android/AndroidPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@
import android.content.Intent;
import android.net.Uri;

import playn.core.AbstractPlatform;
import playn.core.Game;
import playn.core.Json;
import playn.core.Mouse;
import playn.core.MouseStub;
import playn.core.Platform;
import playn.core.PlayN;
import playn.core.TouchImpl;
import playn.core.json.JsonImpl;
import playn.core.util.RunQueue;

public class AndroidPlatform implements Platform {
public class AndroidPlatform extends AbstractPlatform {

public static final boolean DEBUG_LOGS = true;

Expand All @@ -46,19 +45,17 @@ public static AndroidPlatform register(AndroidGL20 gl20, GameActivity activity)
private final AndroidAudio audio;
private final AndroidGraphics graphics;
private final AndroidKeyboard keyboard;
private final AndroidLog log;
private final AndroidNet net;
private final AndroidPointer pointer;
private final AndroidStorage storage;
private final TouchImpl touch;
private final AndroidTouchEventHandler touchHandler;
private final Json json;
private final RunQueue runQueue;

protected AndroidPlatform(GameActivity activity, AndroidGL20 gl20) {
super(new AndroidLog());
this.activity = activity;

log = new AndroidLog();
audio = new AndroidAudio(this);
graphics = new AndroidGraphics(this, gl20, activity.scaleFactor());
analytics = new AndroidAnalytics();
Expand All @@ -70,7 +67,6 @@ protected AndroidPlatform(GameActivity activity, AndroidGL20 gl20) {
storage = new AndroidStorage(activity);
touch = new TouchImpl();
touchHandler = new AndroidTouchEventHandler(graphics, activity.gameView());
runQueue = new RunQueue(log);
}

@Override
Expand Down Expand Up @@ -103,11 +99,6 @@ public AndroidKeyboard keyboard() {
return keyboard;
}

@Override
public AndroidLog log() {
return log;
}

@Override
public AndroidNet net() {
return net;
Expand All @@ -119,11 +110,6 @@ public void openURL(String url) {
activity.startActivity(browserIntent);
}

@Override
public void invokeLater(Runnable runnable) {
runQueue.add(runnable);
}

@Override
public Mouse mouse() {
return new MouseStub();
Expand Down Expand Up @@ -174,6 +160,14 @@ public Type type() {
return Type.ANDROID;
}

// allow these to be called by GameViewGL
protected void onPause() {
super.onPause();
}
protected void onResume() {
super.onResume();
}

void update(float delta) {
runQueue.execute();
if (game != null) {
Expand Down
4 changes: 0 additions & 4 deletions android/src/playn/android/GameActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ protected void onPause() {
if (platform() != null)
platform().audio().onPause();
super.onPause();

// TODO: Notify game
}

@Override
Expand All @@ -169,8 +167,6 @@ protected void onResume() {
if (platform() != null)
platform().audio().onResume();
super.onResume();

// TODO: Notify game
}

/**
Expand Down
35 changes: 22 additions & 13 deletions android/src/playn/android/GameViewGL.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ public void onDrawFrame(GL10 gl) {
if (loop.running())
loop.run();
}

void onPause() {
if (platform != null) {
platform.graphics().ctx.onSurfaceLost();
}
}
}

public GameViewGL(AndroidGL20 _gl20, GameActivity activity, Context context) {
Expand Down Expand Up @@ -141,16 +135,31 @@ public void notifyVisibilityChanged(int visibility) {

@Override
public void onPause() {
queueEvent(new Runnable() {
// This method will be called on the rendering thread:
@Override
public void run() {
renderer.onPause();
}
});
if (platform != null) {
queueEvent(new Runnable() {
@Override
public void run() {
platform.graphics().ctx.onSurfaceLost();
platform.onPause();
}
});
}
super.onPause();
}

@Override
public void onResume() {
super.onResume();
if (platform != null) {
queueEvent(new Runnable() {
@Override
public void run() {
platform.onResume();
}
});
}
}

void onKeyDown(final Keyboard.Event event) {
queueEvent(new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace ${package}
{
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate {
public partial class AppDelegate : IOSApplicationDelegate {
public override bool FinishedLaunching (UIApplication app, NSDictionary options) {
app.SetStatusBarHidden(true, true);
var pf = IOSPlatform.register(app, IOSPlatform.SupportedOrients.PORTRAITS);
Expand Down
79 changes: 79 additions & 0 deletions core/src/playn/core/AbstractPlatform.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright 2012 The PlayN Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package playn.core;

import playn.core.util.RunQueue;

/**
* Implements some common {@link Platform} bits.
*/
public abstract class AbstractPlatform implements Platform {

protected final RunQueue runQueue;
protected final Log log;

private PlayN.LifecycleListener lifecycleListener;

@Override
public void invokeLater(Runnable runnable) {
runQueue.add(runnable);
}

@Override
public void setLifecycleListener(PlayN.LifecycleListener listener) {
lifecycleListener = listener;
}

@Override
public Log log() {
return log;
}

protected AbstractPlatform(Log log) {
this.log = log;
this.runQueue = new RunQueue(log);
}

protected void onPause() {
if (lifecycleListener != null) {
try {
lifecycleListener.onPause();
} catch (Exception e) {
log.warn("LifecycleListener.onPause failure", e);
}
}
}

protected void onResume() {
if (lifecycleListener != null) {
try {
lifecycleListener.onResume();
} catch (Exception e) {
log.warn("LifecycleListener.onResume failure", e);
}
}
}

protected void onExit() {
if (lifecycleListener != null) {
try {
lifecycleListener.onExit();
} catch (Exception e) {
log.warn("LifecycleListener.onExit failure", e);
}
}
}
}
3 changes: 3 additions & 0 deletions core/src/playn/core/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* Generic platform interface. New platforms are defined as implementations of this interface.
*/
public interface Platform {

public enum Type { JAVA, HTML, ANDROID, IOS, FLASH }

void run(Game game);
Expand All @@ -33,6 +34,8 @@ public enum Type { JAVA, HTML, ANDROID, IOS, FLASH }

void invokeLater(Runnable runnable);

void setLifecycleListener(PlayN.LifecycleListener listener);

Audio audio();

Graphics graphics();
Expand Down
18 changes: 18 additions & 0 deletions core/src/playn/core/PlayN.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public class PlayN {

private static Platform platform;

/** Used to receive lifecycle notifications. See {@link #setLifecycleListener}. */
public static interface LifecycleListener {
/** Called just before the game is paused (made non-active on mobile platforms). */
void onPause();
/** Called just after the game is resumed (made active on mobile platforms). */
void onResume();
/** Called just before the game will be closed (on mobile and web platforms). */
void onExit();
}

/**
* Call this method to start your {@link Game}. It must be called only once,
* and all work after this call is made will be performed in {@link Game}'s
Expand Down Expand Up @@ -77,6 +87,14 @@ public static void invokeLater(Runnable runnable) {
platform.invokeLater(runnable);
}

/**
* Configures a listener to be notified of lifecycle events. Any previous listener will be
* overwritten. Supply null to clear the listener.
*/
public static void setLifecycleListener(LifecycleListener listener) {
platform.setLifecycleListener(listener);
}

/**
* Returns the {@link Audio} service.
*/
Expand Down
22 changes: 4 additions & 18 deletions flash/src/playn/flash/FlashPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import flash.display.Sprite;
import flash.events.EventType;

import playn.core.AbstractPlatform;
import playn.core.Analytics;
import playn.core.Audio;
import playn.core.Game;
Expand All @@ -31,18 +32,16 @@
import playn.core.Log;
import playn.core.Mouse;
import playn.core.Net;
import playn.core.Platform;
import playn.core.PlayN;
import playn.core.Pointer;
import playn.core.RegularExpression;
import playn.core.Storage;
import playn.core.Touch;
import playn.core.TouchStub;
import playn.core.json.JsonImpl;
import playn.core.util.RunQueue;
import playn.html.HtmlRegularExpression;

public class FlashPlatform implements Platform {
public class FlashPlatform extends AbstractPlatform {

static final int DEFAULT_WIDTH = 640;
static final int DEFAULT_HEIGHT = 480;
Expand Down Expand Up @@ -71,11 +70,9 @@ static native void addEventListener(JavaScriptObject target, String name, EventH
private final FlashGraphics graphics;
private final Json json;
private final FlashKeyboard keyboard;
private final FlashLog log;
private final FlashNet net;
private final FlashPointer pointer;
private final FlashMouse mouse;
private final RunQueue runQueue;

private Game game;
private TimerCallback paintCallback;
Expand All @@ -84,7 +81,7 @@ static native void addEventListener(JavaScriptObject target, String name, EventH
private Analytics analytics;

protected FlashPlatform() {
log = new FlashLog();
super(new FlashLog());
regularExpression = new HtmlRegularExpression();
net = new FlashNet();
audio = new FlashAudio();
Expand All @@ -95,7 +92,6 @@ protected FlashPlatform() {
graphics = new FlashGraphics();
storage = new FlashStorage();
analytics = new FlashAnalytics();
runQueue = new RunQueue(log);
}

@Override
Expand Down Expand Up @@ -128,11 +124,6 @@ public Keyboard keyboard() {
return keyboard;
}

@Override
public Log log() {
return log;
}

@Override
public Net net() {
return net;
Expand Down Expand Up @@ -174,11 +165,6 @@ public void openURL(String url) {
//TODO: implement
}

@Override
public void invokeLater(Runnable runnable) {
runQueue.add(runnable);
}

@Override
public void run(final Game game) {
final int updateRate = game.updateRate();
Expand Down Expand Up @@ -225,7 +211,7 @@ public void fire() {
if (frameCounter == FPS_COUNTER_MAX) {
double frameRate = frameCounter /
((time() - frameCounterStart) / 1000.0);
PlayN.log().info("FPS: " + frameRate);
log().info("FPS: " + frameRate);
frameCounter = 0;
}
}
Expand Down
Loading

0 comments on commit c4310a8

Please sign in to comment.