コード例 #1
0
  public void setTextView(TextView newView) {
    mStatusText = newView;

    // Set the StatusText to the current score.
    mStatusText.setText(mPuzzle.getScore());
    mStatusText.setVisibility(View.VISIBLE);
  }
コード例 #2
0
 public void UnScramble() {
   mPuzzle.UnScramblePuzzle();
   mBrickGrid = mPuzzle.GetPuzzle();
   enabled = false;
   mStatusText.setText(mPuzzle.getScore());
   mStatusText.setVisibility(View.VISIBLE);
   invalidate();
 }
コード例 #3
0
  public boolean onTouch(View v, MotionEvent event) {
    // Don't do anything until started.
    if (!enabled) return true;

    Index index;

    float x = event.getX();
    float y = event.getY();

    // The touch was outside the grid, ignore it
    index = CoordinateToIndex(x, y);
    if (index.x == 2012 || index.y == 2012) {
      clearSelected();
      return true;
    }

    // The initial touch downward
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:

        // Record the initial down coordinates
        downX = event.getX();
        downY = event.getY();

        break;
      case MotionEvent.ACTION_MOVE:
        // What to do while the finger is moving.

        // Commenting out the break will fall through to ACTION_UP.

        // This will change the tiles while moving.
        // Need to undo this on ACTION_UP
        if (mBrickGrid[index.x][index.y] < 2012) {
          if (mBrickGrid[index.x][index.y] < BLANK) {
            mBrickGrid[index.x][index.y] = mBrickGrid[index.x][index.y] + 15;

            invalidate();
          }
        }
        break;
      case MotionEvent.ACTION_UP:
        clearSelected();

        // Record the up coordinates
        upX = event.getX();
        upY = event.getY();

        // ((TextView)findViewById(R.id.score)).setText("Up");

        Index downIndex;
        Index upIndex;

        downIndex = CoordinateToIndex(downX, downY);
        upIndex = CoordinateToIndex(upX, upY);

        // Check for drag on column
        if ((upIndex.x == downIndex.x) && (Math.abs(upIndex.y - downIndex.y) == 4)) {
          int points = mPuzzle.submit(columnToString(upIndex.x));

          if (points == -2) {
            AlertDialog.Builder alert = new AlertDialog.Builder(v.getContext());

            alert.setMessage("You Win!");
            CharSequence ok = "Ok";
            alert.setPositiveButton(
                ok,
                new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface arg0, int arg1) {
                    arg0.dismiss();
                  }
                });
            alert.show();
            mStatusText.setText(mPuzzle.getScore());
          } else if (points > 0) {
            popUpPoints(v, Integer.toString(points));
            mStatusText.setText(mPuzzle.getScore());
            mStatusText.setVisibility(View.VISIBLE);
            popUpPoints(v, Integer.toString(points));
          } else {
            // Play a sound and vibrate?
          }

          // Break out of the on up to avoid tile press detection.
          break;
        }

        // Check for drag on row
        if ((upIndex.y == downIndex.y) && (Math.abs(upIndex.x - downIndex.x) == 4)) {
          int points = mPuzzle.submit(rowToString(upIndex.y));
          if (points > 0) {
            popUpPoints(v, Integer.toString(points));
            mStatusText.setText(mPuzzle.getScore());
            mStatusText.setVisibility(View.VISIBLE);
          }

          // Break out of the on up to avoid tile press detection.
          break;
        }

        // No drag, then it should be a tile press.
        if (mBrickGrid[index.x][index.y] < 2012) {
          if (mBrickGrid[index.x][index.y] > BLANK) {
            mBrickGrid[index.x][index.y] = mBrickGrid[index.x][index.y] - 15;
            invalidate();
          }
        }
        mPuzzle.ChangePuzzle(index.x, index.y);
        mBrickGrid = mPuzzle.GetPuzzle();
        invalidate();

        break;
      default:
        break;
    }

    return true;
  }