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()); 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){ public void addPosCell(Cellule cellule){
colors.put(cellule.getPosition(), cellule.getActiveColor()); colors.put(cellule.getPosition(), cellule.getActiveColor());
} }
public void delPosCell(Cellule cellule){
colors.remove(cellule.getPosition());
}
public void setColones(int value) throws IllegalArgumentException{ public void setColones(int value) throws IllegalArgumentException{
if(value < 0){ if(value < 0){

@ -6,6 +6,7 @@ import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.widget.Button;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; 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.R;
import projet.iut.jeu_de_la_vie.model.Manager; 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; import projet.iut.jeu_de_la_vie.view.PlateauView;
public class FragmentPlateau extends Fragment { public class FragmentPlateau extends Fragment implements ObserverCV {
/*
private Plateau plateau;
public Plateau getPlateau() {
return plateau;
}
public void setPlateau(Plateau plateau) {
this.plateau = plateau;
}
*/
private Manager manager; private Manager manager;
public FragmentPlateau(){ public FragmentPlateau(){
super(R.layout.fragment_plateau); //<-- lui passer la future vue du jeu super(R.layout.fragment_plateau); //<-- lui passer la future vue du jeu
manager = new Manager(); manager = new Manager();
manager.getActualiseurCellule().getArbitre().getPlateau().getCellulesVivantes().attacher(this);
} }
@Override @Override
@ -47,24 +41,61 @@ public class FragmentPlateau extends Fragment {
PlateauView plateauView = view.findViewById(R.id.plateauView); PlateauView plateauView = view.findViewById(R.id.plateauView);
plateauView.setLignes(manager.getNumberOfLines()); plateauView.setLignes(manager.getNumberOfLines());
plateauView.setColones(manager.getNomberOfColumns()); plateauView.setColones(manager.getNomberOfColumns());
plateauView.setOnTouchListener((view1, motionEvent) -> { 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();
}
}
});
}
private boolean loadRect(View view1, MotionEvent motionEvent){
int x = (int) (( (float) manager.getNomberOfColumns() / view1.getWidth()) * motionEvent.getX()-5/100); int x = (int) (( (float) manager.getNomberOfColumns() / view1.getWidth()) * motionEvent.getX()-5/100);
int y = (int) (( (float) manager.getNomberOfColumns() / view1.getWidth()) * motionEvent.getY()-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", "x=" + x);
Log.d("D", "y=" + y); Log.d("D", "y=" + y);
try { try {
manager.updateOnCellBeforeStart(x, y); manager.updateOnCellBeforeStart(x, y);
((PlateauView) view1).addPosCell(manager.getCell(x, y)); Cellule cellule = manager.getCell(x, y);
view1.postInvalidate(); if(cellule.isAlive()) {
((PlateauView) view).addPosCell(cellule);
}else {
((PlateauView) view).delPosCell(cellule);
}
view.postInvalidate();
}catch (IllegalArgumentException e){ }catch (IllegalArgumentException e){
Log.w("WARNING", e.getMessage()); Log.w("WARNING", e.getMessage());
} }
return false; return false;
});
} }
@Override @Override
public void onInflate(@NonNull Context context, @NonNull AttributeSet attrs, @Nullable Bundle savedInstanceState) { public void onInflate(@NonNull Context context, @NonNull AttributeSet attrs, @Nullable Bundle savedInstanceState) {
super.onInflate(context, attrs, 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; package projet.iut.jeu_de_la_vie.view.fragment;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.EditText; import android.widget.EditText;

@ -1,10 +1,31 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <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_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/containerPlateauView">
<projet.iut.jeu_de_la_vie.view.PlateauView <projet.iut.jeu_de_la_vie.view.PlateauView
android:id="@+id/plateauView" android:id="@+id/plateauView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="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> </FrameLayout>

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

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