Added Game Controller and Listener.

This commit is contained in:
Alexei KADIR 2024-04-03 00:37:11 +02:00
parent 6b3a8bf760
commit 9859945768
2 changed files with 156 additions and 0 deletions

View File

@ -0,0 +1,141 @@
package fr.iutfbleau.sae;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class GameController implements GameListener, View.OnClickListener {
private final Activity activity;
private final GameView view;
private final GameModel model;
private final Button reset;
private final TextView score;
private final TextView moves;
private final View end;
private final TextView endScore;
private final Button back;
private final Button watch;
public GameController(Activity activity, GameView view, GameModel model) {
this.activity = activity;
this.view = view;
this.model = model;
this.reset = (Button) this.activity.findViewById(R.id.reset);
this.score = (TextView) this.activity.findViewById(R.id.score);
this.moves = (TextView) this.activity.findViewById(R.id.moves);
this.end = this.activity.findViewById(R.id.end);
this.endScore = (TextView) this.activity.findViewById(R.id.end_score);
this.back = (Button) this.activity.findViewById(R.id.back);
this.watch = (Button) this.activity.findViewById(R.id.watch);
this.view.setListener(this);
this.reset.setOnClickListener(this);
this.back.setOnClickListener(this);
this.watch.setOnClickListener(this);
this.update();
}
public void update() {
this.score.setText(this.activity.getString(R.string.score, this.model.getScore()));
this.moves.setText(this.activity.getString(R.string.moves, this.model.getValidLineCount()));
if (this.model.isGameFinished()) {
this.end.setVisibility(View.VISIBLE);
this.endScore.setText(this.activity.getString(R.string.end_score, this.model.getScore()));
}
}
public void watch() {
this.end.setVisibility(View.INVISIBLE);
}
public void back() {
this.activity.finish();
}
public void reset() {
this.view.setOffset(0, 0);
this.view.invalidate();
}
@Override
public void onLineStarted(GameView view, Cell start) {
if (this.view != view)
return;
if (this.model.isGameFinished())
return;
this.model.startInputLine(start);
this.view.invalidate();
}
@Override
public void onLineEndMoved(GameView view, Cell end) {
if (this.view != view)
return;
if (this.model.isGameFinished())
return;
if (this.model.moveInputLineEnd(end))
this.view.invalidate();
}
@Override
public void onLineEnded(GameView view, Cell end) {
if (this.view != view)
return;
if (this.model.isGameFinished())
return;
this.model.endInputLine(end);
this.view.invalidate();
this.update();
}
@Override
public void onClick(View view) {
if (this.reset == view)
this.reset();
else if (this.back == view)
this.back();
else if (this.watch == view)
this.watch();
else
throw new IllegalStateException("Unhandled click event source.");
}
}

View File

@ -0,0 +1,15 @@
package fr.iutfbleau.sae;
import android.graphics.Point;
public interface GameListener {
void onLineStarted(GameView view, Cell start);
void onLineEndMoved(GameView view, Cell end);
void onLineEnded(GameView view, Cell end);
}