Faut faire que c'est threadsafe parceque la c'est pas le cas :x

main
Allan POINT 3 years ago
parent 6c03d93d86
commit ada13da2b0

@ -49,5 +49,8 @@ public abstract class ObservableBDJ {
Platform.runLater(()->observeur.update());
}
*/
for (ObserverBDJ observeur : observeurs) {
observeur.update();
}
}
}

@ -113,6 +113,9 @@ public class PlateauView extends View implements ObserverCV {
public void addPosCell(Cellule cellule){
colors.put(cellule.getPosition(), cellule.getActiveColor());
}
public void delPosCell(Cellule cellule){
colors.remove(cellule.getPosition());
}
public void setColones(int value) throws IllegalArgumentException{
if(value < 0){

@ -6,6 +6,7 @@ import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -13,24 +14,17 @@ import androidx.fragment.app.Fragment;
import projet.iut.jeu_de_la_vie.R;
import projet.iut.jeu_de_la_vie.model.Manager;
import projet.iut.jeu_de_la_vie.model.cellule.Cellule;
import projet.iut.jeu_de_la_vie.model.cellulesVivantes.observer.ObserverCV;
import projet.iut.jeu_de_la_vie.view.PlateauView;
public class FragmentPlateau extends Fragment {
/*
private Plateau plateau;
public class FragmentPlateau extends Fragment implements ObserverCV {
public Plateau getPlateau() {
return plateau;
}
public void setPlateau(Plateau plateau) {
this.plateau = plateau;
}
*/
private Manager manager;
public FragmentPlateau(){
super(R.layout.fragment_plateau); //<-- lui passer la future vue du jeu
manager = new Manager();
manager.getActualiseurCellule().getArbitre().getPlateau().getCellulesVivantes().attacher(this);
}
@Override
@ -47,24 +41,61 @@ public class FragmentPlateau extends Fragment {
PlateauView plateauView = view.findViewById(R.id.plateauView);
plateauView.setLignes(manager.getNumberOfLines());
plateauView.setColones(manager.getNomberOfColumns());
plateauView.setOnTouchListener((view1, motionEvent) -> {
int x = (int) (( (float) manager.getNomberOfColumns() / view1.getWidth()) * motionEvent.getX()-5/100);
int y = (int) (( (float) manager.getNomberOfColumns() / view1.getWidth()) * motionEvent.getY()-5/100);
Log.d("D", "x=" + x);
Log.d("D", "y=" + y);
try {
manager.updateOnCellBeforeStart(x, y);
((PlateauView) view1).addPosCell(manager.getCell(x, y));
view1.postInvalidate();
}catch (IllegalArgumentException e){
Log.w("WARNING", e.getMessage());
plateauView.setOnTouchListener(this::loadRect);
Button button = view.findViewById(R.id.startnstopButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(manager.isJeuLance())
{
((Button) view).setText("Start");
manager.stoperJeu();
}else {
((Button) view).setText("Stop");
manager.lancerJeu();
}
}
return false;
});
}
private boolean loadRect(View view1, MotionEvent motionEvent){
int x = (int) (( (float) manager.getNomberOfColumns() / view1.getWidth()) * motionEvent.getX()-5/100);
int y = (int) (( (float) manager.getNomberOfColumns() / view1.getWidth()) * motionEvent.getY()-5/100);
return loadRect(view1, x, y);
}
private boolean loadRect(View view, int x, int y){
Log.d("D", "x=" + x);
Log.d("D", "y=" + y);
try {
manager.updateOnCellBeforeStart(x, y);
Cellule cellule = manager.getCell(x, y);
if(cellule.isAlive()) {
((PlateauView) view).addPosCell(cellule);
}else {
((PlateauView) view).delPosCell(cellule);
}
view.postInvalidate();
}catch (IllegalArgumentException e){
Log.w("WARNING", e.getMessage());
}
return false;
}
@Override
public void onInflate(@NonNull Context context, @NonNull AttributeSet attrs, @Nullable Bundle savedInstanceState) {
super.onInflate(context, attrs, savedInstanceState);
}
@Override
public void update(Cellule changingCell) {
View plateauView = getView().findViewById(R.id.plateauView);
if(plateauView != null) {
plateauView.post(new Runnable() {
@Override
public void run() {
loadRect(plateauView, changingCell.getPosition().getX(), changingCell.getPosition().getY());
}
});
}
}
}

@ -1,7 +1,6 @@
package projet.iut.jeu_de_la_vie.view.fragment;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

@ -1,10 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<projet.iut.jeu_de_la_vie.view.PlateauView
android:id="@+id/plateauView"
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
android:id="@+id/containerPlateauView">
<projet.iut.jeu_de_la_vie.view.PlateauView
android:id="@+id/plateauView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="@+id/startnstopButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/startnstopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/start_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

@ -8,13 +8,13 @@
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerViewId"
android:name="projet.iut.jeu_de_la_vie.view.fragment.MenuFragment"
android:layout_width="415dp"
android:layout_height="539dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.988" />
app:layout_constraintVertical_bias="1.0" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -1,3 +1,4 @@
<resources>
<string name="app_name">Jeu_de_la_vie</string>
<string name="start_text">Start</string>
</resources>
Loading…
Cancel
Save