Home | History | Annotate | Download | only in jetboy
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 // Android JET demonstration code:
     18 // See the JetBoyView.java file for examples on the use of the JetPlayer class.
     19 
     20 package com.example.android.jetboy;
     21 
     22 import com.example.android.jetboy.JetBoyView.JetBoyThread;
     23 
     24 import android.app.Activity;
     25 import android.os.Bundle;
     26 import android.util.Log;
     27 import android.view.KeyEvent;
     28 import android.view.View;
     29 import android.widget.Button;
     30 import android.widget.TextView;
     31 
     32 public class JetBoy extends Activity implements View.OnClickListener {
     33 
     34     /** A handle to the thread that's actually running the animation. */
     35     private JetBoyThread mJetBoyThread;
     36 
     37     /** A handle to the View in which the game is running. */
     38     private JetBoyView mJetBoyView;
     39 
     40     // the play start button
     41     private Button mButton;
     42 
     43     // used to hit retry
     44     private Button mButtonRetry;
     45 
     46     // the window for instructions and such
     47     private TextView mTextView;
     48 
     49     // game window timer
     50     private TextView mTimerView;
     51 
     52     /**
     53      * Required method from parent class
     54      *
     55      * @param savedInstanceState - The previous instance of this app
     56      */
     57     public void onCreate(Bundle savedInstanceState) {
     58         super.onCreate(savedInstanceState);
     59 
     60         setContentView(R.layout.main);
     61 
     62         // get handles to the JetView from XML and the JET thread.
     63         mJetBoyView = (JetBoyView)findViewById(R.id.JetBoyView);
     64         mJetBoyThread = mJetBoyView.getThread();
     65 
     66         // look up the happy shiny button
     67         mButton = (Button)findViewById(R.id.Button01);
     68         mButton.setOnClickListener(this);
     69 
     70         mButtonRetry = (Button)findViewById(R.id.Button02);
     71         mButtonRetry.setOnClickListener(this);
     72 
     73         // set up handles for instruction text and game timer text
     74         mTextView = (TextView)findViewById(R.id.text);
     75         mTimerView = (TextView)findViewById(R.id.timer);
     76 
     77         mJetBoyView.setTimerView(mTimerView);
     78 
     79         mJetBoyView.SetButtonView(mButtonRetry);
     80 
     81         mJetBoyView.SetTextView(mTextView);
     82     }
     83 
     84 
     85     /**
     86      * Handles component interaction
     87      *
     88      * @param v The object which has been clicked
     89      */
     90     public void onClick(View v) {
     91         // this is the first screen
     92         if (mJetBoyThread.getGameState() == JetBoyThread.STATE_START) {
     93             mButton.setText("PLAY!");
     94             mTextView.setVisibility(View.VISIBLE);
     95 
     96             mTextView.setText(R.string.helpText);
     97             mJetBoyThread.setGameState(JetBoyThread.STATE_PLAY);
     98 
     99         }
    100         // we have entered game play, now we about to start running
    101         else if (mJetBoyThread.getGameState() == JetBoyThread.STATE_PLAY) {
    102             mButton.setVisibility(View.INVISIBLE);
    103             mTextView.setVisibility(View.INVISIBLE);
    104             mTimerView.setVisibility(View.VISIBLE);
    105             mJetBoyThread.setGameState(JetBoyThread.STATE_RUNNING);
    106 
    107         }
    108         // this is a retry button
    109         else if (mButtonRetry.equals(v)) {
    110 
    111             mTextView.setText(R.string.helpText);
    112 
    113             mButton.setText("PLAY!");
    114             mButtonRetry.setVisibility(View.INVISIBLE);
    115             // mButtonRestart.setVisibility(View.INVISIBLE);
    116 
    117             mTextView.setVisibility(View.VISIBLE);
    118             mButton.setText("PLAY!");
    119             mButton.setVisibility(View.VISIBLE);
    120 
    121             mJetBoyThread.setGameState(JetBoyThread.STATE_PLAY);
    122 
    123         } else {
    124             Log.d("JB VIEW", "unknown click " + v.getId());
    125 
    126             Log.d("JB VIEW", "state is  " + mJetBoyThread.mState);
    127 
    128         }
    129     }
    130 
    131     /**
    132      * Standard override to get key-press events.
    133      */
    134     @Override
    135     public boolean onKeyDown(int keyCode, KeyEvent msg) {
    136         if (keyCode == KeyEvent.KEYCODE_BACK) {
    137             return super.onKeyDown(keyCode, msg);
    138         } else {
    139             return mJetBoyThread.doKeyDown(keyCode, msg);
    140         }
    141     }
    142 
    143     /**
    144      * Standard override for key-up.
    145      */
    146     @Override
    147     public boolean onKeyUp(int keyCode, KeyEvent msg) {
    148         if (keyCode == KeyEvent.KEYCODE_BACK) {
    149             return super.onKeyUp(keyCode, msg);
    150         } else {
    151             return mJetBoyThread.doKeyUp(keyCode, msg);
    152         }
    153     }
    154 }
    155