Allow challenging your friends on Android (closes #10)
This commit is contained in:
parent
5b942374e8
commit
ac990ae05d
6 changed files with 91 additions and 10 deletions
BIN
android/assets/share.png
Normal file
BIN
android/assets/share.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 134 KiB |
|
@ -7,10 +7,11 @@ import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
|
|||
import io.github.lonamiwebs.klooni.Klooni;
|
||||
|
||||
public class AndroidLauncher extends AndroidApplication {
|
||||
@Override
|
||||
protected void onCreate (Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
|
||||
initialize(new Klooni(), config);
|
||||
}
|
||||
@Override
|
||||
protected void onCreate (Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
final AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
|
||||
final AndroidShareChallenge shareChallenge = new AndroidShareChallenge(this);
|
||||
initialize(new Klooni(shareChallenge), config);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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..."));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -21,6 +21,8 @@ public class Klooni extends Game {
|
|||
public static Theme theme;
|
||||
public Skin skin;
|
||||
|
||||
public ShareChallenge shareChallenge;
|
||||
|
||||
public static boolean onDesktop;
|
||||
|
||||
private final static float SCORE_TO_MONEY = 1f / 100f;
|
||||
|
@ -32,6 +34,12 @@ public class Klooni extends Game {
|
|||
|
||||
//region Creation
|
||||
|
||||
// TODO Possibly implement a 'ShareChallenge'
|
||||
// for other platforms instead passing null
|
||||
public Klooni(final ShareChallenge shareChallenge) {
|
||||
this.shareChallenge = shareChallenge;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create() {
|
||||
onDesktop = Gdx.app.getType().equals(Application.ApplicationType.Desktop);
|
||||
|
|
|
@ -19,17 +19,21 @@ public class SoftButton extends ImageButton {
|
|||
|
||||
//region Constructor
|
||||
|
||||
public SoftButton(int styleIndex, String imageName) {
|
||||
public SoftButton(final int styleIndex, final String imageName) {
|
||||
super(Klooni.theme.getStyle(styleIndex));
|
||||
|
||||
this.styleIndex = styleIndex;
|
||||
image = Theme.skin.getDrawable(imageName);
|
||||
updateImage(imageName);
|
||||
}
|
||||
|
||||
//endregion
|
||||
|
||||
//region Public methods
|
||||
|
||||
public void updateImage(final String imageName) {
|
||||
image = Theme.skin.getDrawable(imageName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Batch batch, float parentAlpha) {
|
||||
// Always update the style to make sure we're using the right image.
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package io.github.lonamiwebs.klooni.screens;
|
||||
|
||||
import com.badlogic.gdx.Application;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import com.badlogic.gdx.InputProcessor;
|
||||
import com.badlogic.gdx.files.FileHandle;
|
||||
import com.badlogic.gdx.graphics.GL20;
|
||||
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
|
||||
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.utils.ChangeListener;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
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.SoftButton;
|
||||
import io.github.lonamiwebs.klooni.game.BaseScorer;
|
||||
|
@ -31,8 +36,10 @@ class PauseMenuStage extends Stage {
|
|||
|
||||
private final ShapeRenderer shapeRenderer;
|
||||
|
||||
private final Klooni game;
|
||||
private final Band band;
|
||||
private final BaseScorer scorer;
|
||||
private final SoftButton playButton;
|
||||
|
||||
//endregion
|
||||
|
||||
|
@ -40,6 +47,7 @@ class PauseMenuStage extends Stage {
|
|||
|
||||
// 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) {
|
||||
this.game = game;
|
||||
this.scorer = scorer;
|
||||
|
||||
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
|
||||
// TODO Enable both actions for this button? Or leave play?
|
||||
final SoftButton playButton = new SoftButton(2, "play_texture");
|
||||
playButton = new SoftButton(2, "play_texture");
|
||||
table.add(playButton).space(16);
|
||||
|
||||
playButton.addListener(new ChangeListener() {
|
||||
|
@ -143,6 +150,16 @@ class PauseMenuStage extends Stage {
|
|||
}
|
||||
|
||||
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);
|
||||
show();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue