Added Game Activity.

This commit is contained in:
Alexei KADIR 2024-04-02 22:27:03 +02:00
parent d9e3dab7d4
commit 6b3a8bf760
2 changed files with 179 additions and 0 deletions

View File

@ -0,0 +1,88 @@
package fr.iutfbleau.sae;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import java.util.HashSet;
import java.util.Set;
public class GameActivity extends AppCompatActivity {
public static final String SIZE_KEY = "SIZE";
public static final int DEFAULT_SIZE = 10;
public static final String HARD_MODE_KEY = "HARD_MODE";
public static final boolean DEFAULT_HARD_MODE = false;
private static final String LINES_KEY = "LINES";
private static final String OFFSET_X_KEY = "OFFSET_X";
private static final String OFFSET_Y_KEY = "OFFSET_Y";
private GameView view;
private GameModel model;
private GameController controller;
@Override
protected void onCreate(Bundle savedState) {
super.onCreate(savedState);
this.setContentView(R.layout.activity_game);
Intent intent = this.getIntent();
this.view = (GameView) this.findViewById(R.id.game);
this.model = new GameModel(intent.getIntExtra(SIZE_KEY, DEFAULT_SIZE),
intent.getBooleanExtra(HARD_MODE_KEY, DEFAULT_HARD_MODE));
this.view.setModel(this.model);
this.controller = new GameController(this, this.view, this.model);
}
@Override
protected void onSaveInstanceState(@NonNull Bundle state) {
super.onSaveInstanceState(state);
state.putSerializable(LINES_KEY, new HashSet<Line>(this.model.getLines()));
Point offset = this.view.getOffset();
state.putInt(OFFSET_X_KEY, offset.x);
state.putInt(OFFSET_Y_KEY, offset.y);
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedState) {
super.onSaveInstanceState(savedState);
Intent intent = this.getIntent();
this.model.reset(intent.getIntExtra(SIZE_KEY, DEFAULT_SIZE), (Set<Line>) savedState.getSerializable(LINES_KEY));
this.controller.update();
this.view.setOffset(savedState.getInt(OFFSET_X_KEY, 0), savedState.getInt(OFFSET_Y_KEY, 0));
}
}

View File

@ -0,0 +1,91 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GameActivity">
<fr.iutfbleau.sae.GameView
android:id="@+id/game"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:padding="12dp"
android:textSize="24sp" />
<TextView
android:id="@+id/moves"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/score"
android:layout_alignParentEnd="true"
android:padding="12dp"
android:textSize="24sp" />
<fr.iutfbleau.sae.ResetButton
android:id="@+id/reset"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="12dp" />
<LinearLayout
android:id="@+id/end"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="24dp"
android:background="@color/grid"
android:orientation="vertical">
<TextView
android:id="@+id/end_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="12dp"
android:textAlignment="center"
android:textSize="32sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/watch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginVertical="24dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="12dp"
android:layout_weight="1"
android:background="@color/black"
android:padding="24dp"
android:text="@string/watch"
android:textSize="24sp" />
<Button
android:id="@+id/back"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginVertical="24dp"
android:layout_marginStart="12dp"
android:layout_marginEnd="24dp"
android:layout_weight="1"
android:background="@color/black"
android:padding="24dp"
android:text="@string/back"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>