fix game view (multiple propertyChange triggering when switching between menu/game screen)

This commit is contained in:
Quillraven
2025-06-23 20:25:43 +02:00
parent a0295db5fd
commit 083453edb0
4 changed files with 24 additions and 0 deletions

View File

@@ -107,6 +107,7 @@ public class GameScreen extends ScreenAdapter {
@Override
public void hide() {
this.engine.removeAllEntities();
this.stage.clear();
}
@Override

View File

@@ -2,6 +2,7 @@ package io.github.com.quillraven.ui.model;
import io.github.com.quillraven.GdxGame;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
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
public interface OnPropertyChange<T> {
void onChange(T value);

View File

@@ -22,7 +22,10 @@ public class GameView extends View<GameViewModel> {
this.lifeGroup = findActor("lifeGroup");
updateLife(viewModel.getLifePoints());
}
@Override
protected void setupPropertyChanges() {
viewModel.onPropertyChange(GameViewModel.LIFE_POINTS, Integer.class, this::updateLife);
viewModel.onPropertyChange(GameViewModel.PLAYER_DAMAGE, Map.Entry.class, this::showDamage);
}

View File

@@ -28,8 +28,21 @@ public abstract class View<T extends ViewModel> extends Table implements EventLi
setupUI();
}
@Override
protected void setStage(Stage stage) {
super.setStage(stage);
if (stage == null) {
viewModel.clearPropertyChanges();
} else {
setupPropertyChanges();
}
}
protected abstract void setupUI();
protected void setupPropertyChanges() {
}
public void onLeft() {
}