Android Open Source - Do-not-get-annoyed Field






From Project

Back to project page Do-not-get-annoyed.

License

The source code is released under:

Apache License

If you think the Android project Do-not-get-annoyed 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 mn100013d.pmu.models;
/*  w ww  . j ava2s  . c om*/
import java.io.Serializable;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

/**
 * @author Milutinovic
 * 
 */
public abstract class Field implements Serializable{
  public static final int INITIAL_STATE = 1;
  public static final int HIGHLIGHTED = 2;
  public static final int OCCUPIED = 3;
  public static final int ATTACKED = 4;

  private View view;
  private Context context;
  private Board board;
  private int state = INITIAL_STATE;
  protected Pawn onFieldPawn;

  protected Field(Board board, View view, Context context) {
    this.board = board;
    this.view = view;
    this.context = context;
  }

  public int getState() {
    return state;
  }

  /**
   * int color only if state is OCCUPIED, else return 0 or -1
   * */
  protected void setState(int state, int color) {
    this.state = state;
    switch (state) {
    case INITIAL_STATE:
      ((ImageView) view).setImageResource(getInitialStateImageId(color));
      view.setAnimation(null);
      break;
    case HIGHLIGHTED:
      ((ImageView) view)
          .setImageResource(getHighlightedStateImageId(color));
      ScaleAnimation animation = new ScaleAnimation((float) 1,
          (float) (1.1), (float) 1, (float) 1.1,
          Animation.RELATIVE_TO_SELF, (float) 0.5,
          Animation.RELATIVE_TO_SELF, (float) 0.5);
      animation.setDuration(200);
      animation.setInterpolator(context,
          android.R.anim.accelerate_decelerate_interpolator);
      animation.setRepeatMode(Animation.REVERSE);
      animation.setRepeatCount(Animation.INFINITE);
      view.setAnimation(animation);
      break;
    case OCCUPIED:
      ((ImageView) view).setImageResource(getOccupiedStateImageId(color));
      view.setAnimation(null);
      break;
    case ATTACKED:
      ((ImageView) view).setImageResource(getAttackedStateImageId(color));
      ScaleAnimation animation1 = new ScaleAnimation((float) 1,
          (float) (1.1), (float) 1, (float) 1.1,
          Animation.RELATIVE_TO_SELF, (float) 0.5,
          Animation.RELATIVE_TO_SELF, (float) 0.5);
      animation1.setDuration(200);
      animation1.setInterpolator(context,
          android.R.anim.accelerate_decelerate_interpolator);
      animation1.setRepeatMode(Animation.REVERSE);
      animation1.setRepeatCount(Animation.INFINITE);
      view.setAnimation(animation1);
      break;
    }

  }

  public void setPawn(Pawn pawn) {
    this.onFieldPawn = pawn;
    if (pawn == null)
      setState(Field.INITIAL_STATE, -1);
    else
      setState(Field.OCCUPIED, pawn.getColor());
  }

  public Pawn getPawn() {
    return onFieldPawn;
  }

  public void empty() {
    setPawn(null);
    setState(Field.INITIAL_STATE, -1);
  }

  public void registerOnClickListener(final Pawn pawn) {
    final Field field = this;
    view.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View arg0) {
        board.fieldClicked(field, pawn);
        
      }
    });
  }

  public void removeOnClickListener() {
    view.setOnClickListener(null);
  }

  public void refresh() {
    removeOnClickListener();
    if (onFieldPawn == null) {
      setState(Field.INITIAL_STATE, -1);
    } else {
      setState(Field.OCCUPIED, onFieldPawn.getColor());

    }
  }

  protected abstract int getInitialStateImageId(int color);

  protected abstract int getHighlightedStateImageId(int color);

  protected abstract int getOccupiedStateImageId(int color);

  protected abstract int getAttackedStateImageId(int color);

  public void performClick() {
    view.performClick();
  }
}




Java Source Code List

mn100013d.pmu.BeginingActivity.java
mn100013d.pmu.GameTypeFragment.java
mn100013d.pmu.NewGameActivity.java
mn100013d.pmu.NewGameFragment.java
mn100013d.pmu.PauseFragment.java
mn100013d.pmu.PauseGameFragment.java
mn100013d.pmu.ScoresFragment.java
mn100013d.pmu.SettingsFragment.java
mn100013d.pmu.StartActivity.java
mn100013d.pmu.StartGameFragment.java
mn100013d.pmu.controllers.GameController.java
mn100013d.pmu.data.GameDataDbHelper.java
mn100013d.pmu.data.GameSettingsEditor.java
mn100013d.pmu.data.GameTableContract.java
mn100013d.pmu.exceptions.ContextNotSetException.java
mn100013d.pmu.exceptions.GameExceptions.java
mn100013d.pmu.exceptions.PlayerNotRegisteredException.java
mn100013d.pmu.models.Board.java
mn100013d.pmu.models.CPUGamePlayer.java
mn100013d.pmu.models.Color.java
mn100013d.pmu.models.Dice.java
mn100013d.pmu.models.Field.java
mn100013d.pmu.models.FinishField.java
mn100013d.pmu.models.GamePlayer.java
mn100013d.pmu.models.HomeField.java
mn100013d.pmu.models.HumanGamePlayer.java
mn100013d.pmu.models.PathField.java
mn100013d.pmu.models.Pawn.java
mn100013d.pmu.models.Result.java
mn100013d.pmu.services.FragmentProvider.java
mn100013d.pmu.services.GamePlayerFactory.java
mn100013d.pmu.services.PopupService.java
mn100013d.pmu.services.Randomizer.java
mn100013d.pmu.services.ShakeDetector.java
mn100013d.pmu.services.SoundService.java