package com pyatnashki app import android os Bundle import android sup

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.pyatnashki.app;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class GameBoardFragment extends Fragment implements View.OnTouchListener{
public static final String LOG_TAG = "GameBoardFragment";
public static final String TAG = "com.pyatnashki.app.GameBoardFragment";
private GameBoard gameBoard = null;
public static final String PROGRESS = "progress";
GameBoardLayout boardLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(LOG_TAG, "onCreateView");
View v = inflater.inflate(R.layout.fragment_game_board, container, false);
LinearLayout linearLayout = (LinearLayout) v.findViewById(R.id.gameBoardLayout);
boardLayout = new GameBoardLayout(getActivity());
boardLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
boardLayout.removeAllViews();
linearLayout.addView(boardLayout);
if(savedInstanceState!=null){
gameBoard = (GameBoard) savedInstanceState.getSerializable(TAG);
playGame();
}
return v;
}
@Override
public void onResume() {
super.onResume();
Log.d(LOG_TAG, "onResume");
if(gameBoard==null){
gameBoard = new GameBoard(this);
gameBoard.create();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(TAG, gameBoard);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
if(event.getAction()==MotionEvent.ACTION_DOWN){
if(gameBoard.motionKost(x, y)){
if(gameBoard.getEtalonPosition().equals(gameBoard.getKostPositions())) {
saveProgress();
}
}
}
return true;
}
public void playGame() {
Log.d(LOG_TAG, "playGame");
for (Kostyashka kost :gameBoard.getKostPositions()) {
if(kost.getNumber().equalsIgnoreCase(GameBoard.SPACE)) continue;
kost.setOnTouchListener(this);
GameBoardLayout rl = (GameBoardLayout) kost.getParent();
if(rl!=null){
rl.removeView(kost);
}
boardLayout.addView(kost);
}
}
public void saveProgress(){
Bundle args = new Bundle();
args.putInt(PROGRESS, gameBoard.getMoves());
SaveProgressFragment saveProgress = new SaveProgressFragment();
saveProgress.setArguments(args);
saveProgress.show(getFragmentManager(), SaveProgressFragment.TAG);
}
}