Android Open Source - SevenWonders Game Messages Display






From Project

Back to project page SevenWonders.

License

The source code is released under:

Apache License

If you think the Android project SevenWonders listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package skylight1.sevenwonders.view;
/*  w ww .ja  v  a2s .c  o m*/
import skylight1.sevenwonders.PlayActivity;
import skylight1.sevenwonders.R;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

/**
 * Used to show a string text message in 2 TextViews on top of the game view.
 * This can be used to show messages to the users telling the user about the game. 
 * The messages will stay visible for a short amount of time and will then disappear.
 * If a new message is set while the old one is still visible, the old one is replaced.
 * There are two different priorities: Message and Alerts: Messages will be displayed on the bottom
 * of the screen while the more important alerts will be displayed with a larger font right in the middle
 * of the screen.
 * 
 * @author Johannes
 */
public class GameMessagesDisplay {
  private static final int HIDE_GAME_EVENT_MESSAGE = 1;
  private enum Priority {
    MESSAGE, ALERT
  }
  
  public enum GameEvent {  
    END_OF_WORLD_REACHED("A magical barrier bars your way!\nTurn around!", Priority.ALERT),
    ANHK_COLLECTED("Ankh collected!", Priority.MESSAGE),
    HIT_BY_SWORD("Struck by a Sword!", Priority.MESSAGE),
    TIME_BONUS_FOUND("Extra time!", Priority.MESSAGE), 
    RUBY_FOUND("Fly through walls for a while!", Priority.MESSAGE),
    INVINCIBLE("Invincibility is yours for a while!", Priority.MESSAGE),
    NOT_HARMED("Saved by Invincibility!", Priority.MESSAGE),
    AAARGH("Aaargh!", Priority.ALERT),
    COIN_COLLECTED("Riches are yours!", Priority.MESSAGE),
    COLLIDED_WITH_OBSTACLE("You cannot fly through the pyramids yet!", Priority.ALERT),
    ;
    
    public final String messageText;
    private final Priority priority;

    GameEvent(String messageText, Priority priority) {
      this.messageText = messageText;
      this.priority = priority;
    }
  }

  private static final long DISPLAY_TIME = 2000;
  
  TextView gameEventMessageView;
  TextView gameEventAlertView;
  
  private Handler handler;

  private static Handler updateUiHandler = null;
  
  public GameMessagesDisplay(Handler uiHandler, Activity activity) {
    updateUiHandler = uiHandler;
    gameEventMessageView = (TextView) activity.findViewById(R.id.gameEventMessageView);
    gameEventAlertView = (TextView) activity.findViewById(R.id.gameEventAlertView);
    
    handler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
        super.handleMessage(msg);
          GameMessagesDisplay.this.hideMessage((View)msg.obj);
      }
    };
  }

  private TextView getViewForPriority(Priority priority) {
    if (priority == Priority.ALERT) {
      return gameEventAlertView;
    } else {
      return gameEventMessageView;
    }
  }

  public void showMessage(Message msg) {
    GameEvent event = GameEvent.values()[msg.arg1];
    TextView textView = getViewForPriority(event.priority);
    handler.removeMessages(HIDE_GAME_EVENT_MESSAGE, textView);
    textView.setText(event.messageText);
    textView.setVisibility(View.VISIBLE);
    Message hideItAgainMessage = handler.obtainMessage(HIDE_GAME_EVENT_MESSAGE, textView);
    handler.sendMessageDelayed(hideItAgainMessage, DISPLAY_TIME);
  }
  
  public void hideMessage(View view) {
    view.setVisibility(View.GONE);    
  }

  public static void postMessage(GameEvent event) {
    if (updateUiHandler != null) {
      Message msg =
        updateUiHandler.obtainMessage(PlayActivity.SHOW_GAME_EVENT_MESSAGE_MESSAGE, event.ordinal(), 0);
      updateUiHandler.sendMessage(msg);
    }
  }
}




Java Source Code List

com.facebook.android.AsyncFacebookRunner.java
com.facebook.android.DialogError.java
com.facebook.android.FacebookError.java
com.facebook.android.Facebook.java
com.facebook.android.FbDialog.java
com.facebook.android.Util.java
skylight1.sevenwonders.AboutActivity.java
skylight1.sevenwonders.Adverts.java
skylight1.sevenwonders.Analytics.java
skylight1.sevenwonders.GameState.java
skylight1.sevenwonders.LevelChooserActivity.java
skylight1.sevenwonders.MenuActivity.java
skylight1.sevenwonders.PlayActivity.java
skylight1.sevenwonders.ScoreActivity.java
skylight1.sevenwonders.SettingsActivity.java
skylight1.sevenwonders.Settings.java
skylight1.sevenwonders.SevenWondersApplication.java
skylight1.sevenwonders.SoundTracksStoppingExceptionHandler.java
skylight1.sevenwonders.SplashActivity.java
skylight1.sevenwonders.StoryActivity.java
skylight1.sevenwonders.levels.CoinCollisionAction.java
skylight1.sevenwonders.levels.CollisionAction.java
skylight1.sevenwonders.levels.ExtraTimeAction.java
skylight1.sevenwonders.levels.GameLevel.java
skylight1.sevenwonders.levels.GameObjectDescriptor.java
skylight1.sevenwonders.levels.HazardCollisionAction.java
skylight1.sevenwonders.levels.LevelConstructionToolkit.java
skylight1.sevenwonders.levels.ProtectionAction.java
skylight1.sevenwonders.levels.RubyCollisionAction.java
skylight1.sevenwonders.levels.SpellCollisionAction.java
skylight1.sevenwonders.services.SoundTracks.java
skylight1.sevenwonders.social.DialogUtil.java
skylight1.sevenwonders.social.NoNPEWebView.java
skylight1.sevenwonders.social.facebook.FacebookApplicationPost.java
skylight1.sevenwonders.social.facebook.FacebookConfig.java
skylight1.sevenwonders.social.facebook.FacebookScoreActivity.java
skylight1.sevenwonders.social.facebook.FacebookUtil.java
skylight1.sevenwonders.social.facebook.LoginAndGetPermissions.java
skylight1.sevenwonders.social.facebook.SessionStore.java
skylight1.sevenwonders.social.facebook.WallPost.java
skylight1.sevenwonders.social.twitter.AuthRequest.java
skylight1.sevenwonders.social.twitter.AuthStore.java
skylight1.sevenwonders.social.twitter.TwitterUpdater.java
skylight1.sevenwonders.view.Carpet.java
skylight1.sevenwonders.view.CubeBounds.java
skylight1.sevenwonders.view.GameMessagesDisplay.java
skylight1.sevenwonders.view.GameTexture.java
skylight1.sevenwonders.view.Position.java
skylight1.sevenwonders.view.SevenWondersGLRenderer.java
skylight1.sevenwonders.view.SevenWondersGLSurfaceView.java
skylight1.sevenwonders.view.StoryPagesController.java
skylight1.sevenwonders.view.StyledSpannableStringBuilder.java
skylight1.sevenwonders.view.TextStyles.java
skylight1.sevenwonders.view.TiltControl.java
wave.Constants.java
wave.CreateWave.java
wave.RectangleWave.java
wave.TriangleWave.java
wave.UtilsWave.java
wave.VertexWave.java