Allow challenging your friends on Android (closes #10)

This commit is contained in:
Lonami Exo 2017-05-07 10:54:32 +02:00
parent 5b942374e8
commit ac990ae05d
6 changed files with 91 additions and 10 deletions

BIN
android/assets/share.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 134 KiB

View file

@ -10,7 +10,8 @@ public class AndroidLauncher extends AndroidApplication {
@Override @Override
protected void onCreate (Bundle savedInstanceState) { protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); final AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new Klooni(), config); final AndroidShareChallenge shareChallenge = new AndroidShareChallenge(this);
initialize(new Klooni(shareChallenge), config);
} }
} }

View file

@ -0,0 +1,51 @@
package io.github.lonamiwebs.klooni;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.widget.Toast;
import java.io.File;
class AndroidShareChallenge extends ShareChallenge {
private final Handler handler;
private final Context context;
AndroidShareChallenge(final Context context) {
handler = new Handler();
this.context = context;
}
@Override
File getShareImageFilePath() {
return new File(context.getExternalCacheDir(), "share_challenge.png");
}
@Override
public void shareScreenshot(final boolean ok) {
handler.post(new Runnable() {
@Override
public void run() {
if (!ok) {
Toast.makeText(context, "Failed to create the file", Toast.LENGTH_SHORT).show();
return;
}
final String text = "Check out my score at 1010 Klooni!";
final Uri pictureUri = Uri.fromFile(getShareImageFilePath());
final Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/png");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, pictureUri);
context.startActivity(Intent.createChooser(shareIntent, "Challenge your friends..."));
}
});
}
}

View file

@ -21,6 +21,8 @@ public class Klooni extends Game {
public static Theme theme; public static Theme theme;
public Skin skin; public Skin skin;
public ShareChallenge shareChallenge;
public static boolean onDesktop; public static boolean onDesktop;
private final static float SCORE_TO_MONEY = 1f / 100f; private final static float SCORE_TO_MONEY = 1f / 100f;
@ -32,6 +34,12 @@ public class Klooni extends Game {
//region Creation //region Creation
// TODO Possibly implement a 'ShareChallenge'
// for other platforms instead passing null
public Klooni(final ShareChallenge shareChallenge) {
this.shareChallenge = shareChallenge;
}
@Override @Override
public void create() { public void create() {
onDesktop = Gdx.app.getType().equals(Application.ApplicationType.Desktop); onDesktop = Gdx.app.getType().equals(Application.ApplicationType.Desktop);

View file

@ -19,17 +19,21 @@ public class SoftButton extends ImageButton {
//region Constructor //region Constructor
public SoftButton(int styleIndex, String imageName) { public SoftButton(final int styleIndex, final String imageName) {
super(Klooni.theme.getStyle(styleIndex)); super(Klooni.theme.getStyle(styleIndex));
this.styleIndex = styleIndex; this.styleIndex = styleIndex;
image = Theme.skin.getDrawable(imageName); updateImage(imageName);
} }
//endregion //endregion
//region Public methods //region Public methods
public void updateImage(final String imageName) {
image = Theme.skin.getDrawable(imageName);
}
@Override @Override
public void draw(Batch batch, float parentAlpha) { public void draw(Batch batch, float parentAlpha) {
// Always update the style to make sure we're using the right image. // Always update the style to make sure we're using the right image.

View file

@ -1,8 +1,10 @@
package io.github.lonamiwebs.klooni.screens; package io.github.lonamiwebs.klooni.screens;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input; import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor; import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Interpolation; import com.badlogic.gdx.math.Interpolation;
@ -13,7 +15,10 @@ import com.badlogic.gdx.scenes.scene2d.actions.RunnableAction;
import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import java.io.File;
import io.github.lonamiwebs.klooni.Klooni; import io.github.lonamiwebs.klooni.Klooni;
import io.github.lonamiwebs.klooni.ShareChallenge;
import io.github.lonamiwebs.klooni.actors.Band; import io.github.lonamiwebs.klooni.actors.Band;
import io.github.lonamiwebs.klooni.actors.SoftButton; import io.github.lonamiwebs.klooni.actors.SoftButton;
import io.github.lonamiwebs.klooni.game.BaseScorer; import io.github.lonamiwebs.klooni.game.BaseScorer;
@ -31,8 +36,10 @@ class PauseMenuStage extends Stage {
private final ShapeRenderer shapeRenderer; private final ShapeRenderer shapeRenderer;
private final Klooni game;
private final Band band; private final Band band;
private final BaseScorer scorer; private final BaseScorer scorer;
private final SoftButton playButton;
//endregion //endregion
@ -40,6 +47,7 @@ class PauseMenuStage extends Stage {
// We need the score to save the maximum score if a new record was beaten // We need the score to save the maximum score if a new record was beaten
PauseMenuStage(final GameLayout layout, final Klooni game, final BaseScorer scorer, final int gameMode) { PauseMenuStage(final GameLayout layout, final Klooni game, final BaseScorer scorer, final int gameMode) {
this.game = game;
this.scorer = scorer; this.scorer = scorer;
shapeRenderer = new ShapeRenderer(20); // 20 vertex seems to be enough for a rectangle shapeRenderer = new ShapeRenderer(20); // 20 vertex seems to be enough for a rectangle
@ -90,8 +98,7 @@ class PauseMenuStage extends Stage {
}); });
// Continue playing OR share (if game over) button // Continue playing OR share (if game over) button
// TODO Enable both actions for this button? Or leave play? playButton = new SoftButton(2, "play_texture");
final SoftButton playButton = new SoftButton(2, "play_texture");
table.add(playButton).space(16); table.add(playButton).space(16);
playButton.addListener(new ChangeListener() { playButton.addListener(new ChangeListener() {
@ -143,6 +150,16 @@ class PauseMenuStage extends Stage {
} }
void showGameOver(final String gameOverReason) { void showGameOver(final String gameOverReason) {
if (game.shareChallenge != null) {
playButton.updateImage("share_texture");
playButton.addListener(new ChangeListener() {
public void changed(ChangeEvent event, Actor actor) {
game.shareChallenge.shareScreenshot(
game.shareChallenge.saveChallengeImage(scorer.getCurrentScore()));
}
});
}
band.setMessage(gameOverReason); band.setMessage(gameOverReason);
show(); show();
} }