Advertisement
yo2man

Loading additonal pages and ending the story

Jul 5th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.87 KB | None | 0 0
  1. // Loading additional pages
  2.  
  3. StoryActivity.java
  4.  
  5. package teamtreehouse.com.interactivestory.ui;
  6.  
  7. import android.app.Activity;
  8. import android.content.Intent;
  9. import android.graphics.drawable.Drawable;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.View;
  13. import android.widget.Button;
  14. import android.widget.ImageView;
  15. import android.widget.TextView;
  16.  
  17. import teamtreehouse.com.interactivestory.R;
  18. import teamtreehouse.com.interactivestory.model.Page;
  19. import teamtreehouse.com.interactivestory.model.Story;
  20.  
  21.  
  22. public class StoryActivity extends Activity {
  23.  
  24.     public static final String TAG = StoryActivity.class.getSimpleName();
  25.        
  26.        
  27.     private Story mStory = new Story();
  28.     private ImageView mImageView;
  29.     private TextView mTextView;  
  30.     private Button mChoice1;    
  31.     private Button mChoice2;      
  32.     private String mName;
  33.     private Page mCurrentPage;
  34.  
  35.     @Override
  36.     protected void onCreate(Bundle savedInstanceState) {
  37.         super.onCreate(savedInstanceState);
  38.         setContentView(R.layout.activity_story);
  39.  
  40.         Intent intent = getIntent();
  41.         mName = intent.getStringExtra(getString(R.string.key_name));
  42.  
  43.         if (mName == null) {
  44.             mName = "Friend";
  45.         }
  46.         Log.d(TAG, mName);
  47.  
  48.         mImageView = (ImageView)findViewById(R.id.storyImageView);
  49.         mTextView = (TextView)findViewById(R.id.storyTextView);    
  50.         mChoice1 = (Button)findViewById(R.id.choiceButton1);      
  51.         mChoice2 = (Button)findViewById(R.id.choiceButton2);      
  52.  
  53.         loadPage(0); // changed to load page of the story based on whatever the user taps on [cont.]
  54.     }
  55.  
  56.  
  57.     private void loadPage(int choice) {  // [cont.] So to do that, we need a new parameter. And the parameter will be the index of the new page that we want to navigate to. The index is an int so we can use an int here: "int choice.
  58.         mCurrentPage = mStory.getPage(choice);
  59.         Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
  60.         mImageView.setImageDrawable(drawable);
  61.  
  62.         String pageText = mCurrentPage.getText();
  63.         pageText = String.format(pageText, mName);
  64.         mTextView.setText(pageText);
  65.  
  66.         if (mCurrentPage.isFinal()) {  // if current page is final, show button "Play again"
  67.             mChoice1.setVisibility(View.INVISIBLE);  // '.setVisibility' makes the 'View' 'VISIBLE'/'INVISIBLE'/or 'GONE' //Makes 'button 1' invisible
  68.             mChoice2.setText("PLAY AGAIN");     //sets text to Play Again // makes 'button 2' = Play Again button
  69.             mChoice2.setOnClickListener(new View.OnClickListener() { //When Play Again button is clicked, this happens:
  70.                 @Override
  71.                 public void onClick(View v) {
  72.                     finish(); // 'finish()' method finishes the current activity gets rid of it then takes us back to the existing activity aka the start page
  73.                 }
  74.             });
  75.         }
  76.         else{
  77.             mChoice1.setText(mCurrentPage.getChoice1().getText());
  78.             mChoice2.setText(mCurrentPage.getChoice2().getText());
  79.  
  80.         // OnClickListeners for the Choices above ^
  81.             mChoice1.setOnClickListener(new View.OnClickListener() {
  82.                 @Override
  83.                 public void onClick(View v) {
  84.                     int nextPage = mCurrentPage.getChoice1().getNextPage();
  85.                     loadPage(nextPage);
  86.                 }
  87.             });
  88.  
  89.             mChoice2.setOnClickListener(new View.OnClickListener() {
  90.                 @Override
  91.                 public void onClick(View v) {
  92.                     int nextPage = mCurrentPage.getChoice2().getNextPage();
  93.                     loadPage(nextPage);
  94.                 }
  95.             });
  96.         }
  97.     }
  98.  
  99. }
  100.  
  101. //----------------------------------------------------------------------------------------------
  102.  
  103. package teamtreehouse.com.interactivestory.model;
  104.  
  105. import teamtreehouse.com.interactivestory.R;
  106.  
  107. public class Story {
  108.     private Page[] mPages;
  109.  
  110.     public Story() {
  111.         mPages = new Page[7];
  112.  
  113.         mPages[0] = new Page(
  114.                 R.drawable.page0,
  115.                 "On your return trip from studying Saturn's rings, you hear a distress signal that seems to be coming from the surface of Mars. It's strange because there hasn't been a colony there in years. Even stranger, it's calling you by name: \"Help me, %1$s, you're my only hope.\"",
  116.                 new Choice("Stop and investigate", 1),
  117.                 new Choice("Continue home to Earth", 2));
  118.  
  119.         mPages[1] = new Page(
  120.                 R.drawable.page1,
  121.                 "You deftly land your ship near where the distress signal originated. You didn't notice anything strange on your fly-by, but there is a cave in front of you. Behind you is an abandoned rover from the early 21st century.",
  122.                 new Choice("Explore the cave", 3),
  123.                 new Choice("Explore the rover", 4));
  124.  
  125.         mPages[2] = new Page(
  126.                 R.drawable.page2,
  127.                 "You continue your course to Earth. Two days later, you receive a transmission from HQ saying that they have detected some sort of anomaly on the surface of Mars near an abandoned rover. They ask you to investigate, but ultimately the decision is yours because your mission has already run much longer than planned and supplies are low.",
  128.                 new Choice("Head back to Mars to investigate", 4),
  129.                 new Choice("Continue home to Earth", 6));
  130.  
  131.         mPages[3] = new Page(
  132.                 R.drawable.page3,
  133.                 "Your EVA suit is equipped with a headlamp, which you use to navigate the cave. After searching for a while your oxygen levels are starting to get pretty low. You know you should go refill your tank, but there's a very faint light up ahead.",
  134.                 new Choice("Refill at ship and explore the rover", 4),
  135.                 new Choice("Continue towards the faint light", 5));
  136.  
  137.         mPages[4] = new Page(
  138.                 R.drawable.page4,
  139.                 "The rover is covered in dust and most of the solar panels are broken. But you are quite surprised to see the on-board system booted up and running. In fact, there is a message on the screen: \"%1$s, come to 28.543436, -81.369031.\" Those coordinates aren't far, but you don't know if your oxygen will last there and back.",
  140.                 new Choice("Explore the coordinates", 5),
  141.                 new Choice("Return to Earth", 6));
  142.  
  143.         mPages[5] = new Page(
  144.                 R.drawable.page5,
  145.                 "After a long walk slightly uphill, you end up at the top of a small crater. You look around, and are overjoyed to see your favorite android, %1$s-S1124. It had been lost on a previous mission to Mars! You take it back to your ship and fly back to Earth.");
  146.  
  147.         mPages[6] = new Page(
  148.                 R.drawable.page6,
  149.                 "You arrive home on Earth. While your mission was a success, you forever wonder what was sending that signal. Perhaps a future mission will be able to investigate...");
  150.     }
  151.  
  152.     public Page getPage(int pageNumber) {  
  153.         return mPages[pageNumber];
  154.     }
  155. }
  156.  
  157. //----------------------------------------------------------------------------------------------
  158.  
  159. package teamtreehouse.com.interactivestory.ui;
  160.  
  161. import android.app.Activity;
  162. import android.content.Intent;
  163. import android.os.Bundle;
  164. import android.view.View;
  165. import android.widget.Button;
  166. import android.widget.EditText;
  167.  
  168. import teamtreehouse.com.interactivestory.R;
  169.  
  170.  
  171. public class MainActivity extends Activity {
  172.  
  173.     private EditText mNameField;
  174.     private Button mStartButton;
  175.  
  176.     @Override
  177.     protected void onCreate(Bundle savedInstanceState) {
  178.         super.onCreate(savedInstanceState);
  179.         setContentView(R.layout.activity_main);
  180.  
  181.         mNameField = (EditText)findViewById(R.id.nameEditText);
  182.         mStartButton = (Button)findViewById(R.id.startButton);
  183.  
  184.         mStartButton.setOnClickListener(new View.OnClickListener() {
  185.             @Override
  186.             public void onClick(View v) {
  187.                 String name = mNameField.getText().toString();
  188.                 startStory(name);
  189.             }
  190.         });
  191.     }
  192.  
  193.     private void startStory(String name) {
  194.         Intent intent = new Intent(this, StoryActivity.class);
  195.         intent.putExtra(getString(R.string.key_name), name);
  196.         startActivity(intent);
  197.     }
  198.  
  199.     // to make it so when the user hits play again, their name is not stored in the text field.
  200.     @Override  
  201.     protected void onResume() {
  202.         super.onResume();  // calls the OnResume of the parent activity class, we always want to do this be default. // just like we have ths super version of OnCreate, we have the super version of OnResume
  203.         // mNameField.setText(""); //resets the name field EditText // ("") = pass in the blank string. simple
  204.     }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement