/
StoryActivity.java
94 lines (75 loc) · 3 KB
/
StoryActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.harryjamesuk.interactivestory.ui;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.harryjamesuk.interactivestory.R;
import com.harryjamesuk.interactivestory.model.Page;
import com.harryjamesuk.interactivestory.model.Story;
public class StoryActivity extends Activity {
public static final String TAG = StoryActivity.class.getSimpleName();
private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Button mPreviousButton;
private Button mNextButton;
private String mName;
private Page mCurrentPage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story);
Intent intent = getIntent();
mName = intent.getStringExtra(getString(R.string.key_name));
if (mName == null) {
mName = "Friend";
}
Log.d(TAG, mName);
mImageView = (ImageView) findViewById(R.id.storyImageView);
mTextView = (TextView) findViewById(R.id.storyTextView);
mPreviousButton = (Button) findViewById(R.id.previousButton);
mNextButton = (Button) findViewById(R.id.nextButton);
loadPage(0);
}
private void loadPage(int choice) {
mCurrentPage = mStory.getPage(choice);
Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = mCurrentPage.getText();
// Add the name if placeholder included. Won't add if no placeholder :)
pageText = String.format(pageText, mName);
mTextView.setText(pageText);
if (mCurrentPage.isFinal()) {
mPreviousButton.setVisibility(View.INVISIBLE);
mNextButton.setText("PLAY AGAIN");
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
} else {
mPreviousButton.setText(mCurrentPage.getChoices().getText1());
mNextButton.setText(mCurrentPage.getChoices().getText2());
mPreviousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int previousPage = mCurrentPage.getChoices().getPreviousPage();
loadPage(previousPage);
}
});
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int nextPage = mCurrentPage.getChoices().getNextPage();
loadPage(nextPage);
}
});
}
}
}