fix game view (multiple propertyChange triggering when switching between menu/game screen)
This commit is contained in:
@@ -107,6 +107,7 @@ public class GameScreen extends ScreenAdapter {
|
|||||||
@Override
|
@Override
|
||||||
public void hide() {
|
public void hide() {
|
||||||
this.engine.removeAllEntities();
|
this.engine.removeAllEntities();
|
||||||
|
this.stage.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package io.github.com.quillraven.ui.model;
|
|||||||
|
|
||||||
import io.github.com.quillraven.GdxGame;
|
import io.github.com.quillraven.GdxGame;
|
||||||
|
|
||||||
|
import java.beans.PropertyChangeListener;
|
||||||
import java.beans.PropertyChangeSupport;
|
import java.beans.PropertyChangeSupport;
|
||||||
|
|
||||||
public abstract class ViewModel {
|
public abstract class ViewModel {
|
||||||
@@ -19,6 +20,12 @@ public abstract class ViewModel {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void clearPropertyChanges() {
|
||||||
|
for (PropertyChangeListener listener : this.propertyChangeSupport.getPropertyChangeListeners()) {
|
||||||
|
this.propertyChangeSupport.removePropertyChangeListener(listener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface OnPropertyChange<T> {
|
public interface OnPropertyChange<T> {
|
||||||
void onChange(T value);
|
void onChange(T value);
|
||||||
|
|||||||
@@ -22,7 +22,10 @@ public class GameView extends View<GameViewModel> {
|
|||||||
|
|
||||||
this.lifeGroup = findActor("lifeGroup");
|
this.lifeGroup = findActor("lifeGroup");
|
||||||
updateLife(viewModel.getLifePoints());
|
updateLife(viewModel.getLifePoints());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void setupPropertyChanges() {
|
||||||
viewModel.onPropertyChange(GameViewModel.LIFE_POINTS, Integer.class, this::updateLife);
|
viewModel.onPropertyChange(GameViewModel.LIFE_POINTS, Integer.class, this::updateLife);
|
||||||
viewModel.onPropertyChange(GameViewModel.PLAYER_DAMAGE, Map.Entry.class, this::showDamage);
|
viewModel.onPropertyChange(GameViewModel.PLAYER_DAMAGE, Map.Entry.class, this::showDamage);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,8 +28,21 @@ public abstract class View<T extends ViewModel> extends Table implements EventLi
|
|||||||
setupUI();
|
setupUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void setStage(Stage stage) {
|
||||||
|
super.setStage(stage);
|
||||||
|
if (stage == null) {
|
||||||
|
viewModel.clearPropertyChanges();
|
||||||
|
} else {
|
||||||
|
setupPropertyChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract void setupUI();
|
protected abstract void setupUI();
|
||||||
|
|
||||||
|
protected void setupPropertyChanges() {
|
||||||
|
}
|
||||||
|
|
||||||
public void onLeft() {
|
public void onLeft() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user