Skip to content

Commit 521133c

Browse files
Refac
1 parent 0d18125 commit 521133c

File tree

2 files changed

+46
-43
lines changed

2 files changed

+46
-43
lines changed

pacman-ui/src/main/java/de/amr/pacmanfx/ui/layout/MiniGameView.java

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import javafx.beans.property.*;
1616
import javafx.geometry.Insets;
1717
import javafx.scene.canvas.Canvas;
18-
import javafx.scene.canvas.GraphicsContext;
1918
import javafx.scene.layout.Background;
2019
import javafx.scene.layout.HBox;
2120
import javafx.scene.layout.Priority;
@@ -24,11 +23,12 @@
2423
import javafx.scene.text.Font;
2524
import javafx.scene.text.TextAlignment;
2625
import javafx.util.Duration;
27-
import org.tinylog.Logger;
2826

2927
import java.util.stream.Stream;
3028

3129
import static de.amr.pacmanfx.Globals.*;
30+
import static de.amr.pacmanfx.ui.GameUI_Config.SCENE_ID_PLAY_SCENE_3D;
31+
import static java.util.Objects.requireNonNull;
3232

3333
public class MiniGameView extends VBox {
3434

@@ -43,7 +43,10 @@ public class MiniGameView extends VBox {
4343

4444
private final Canvas canvas;
4545
private final HBox canvasContainer;
46+
47+
private GameUI ui;
4648
private GameRenderer gr;
49+
private GameLevel gameLevel;
4750
private long drawCallCount;
4851

4952
private final Animation moveIntoScreenAnimation;
@@ -85,7 +88,20 @@ public DoubleProperty canvasHeightProperty() {
8588
return canvasHeightProperty;
8689
}
8790

88-
public void onLevelCreated(GameUI ui, GameLevel gameLevel) {
91+
public void setGameUI(GameUI ui) {
92+
this.ui = requireNonNull(ui);
93+
backgroundColorProperty().bind(GameUI.PROPERTY_CANVAS_BACKGROUND_COLOR);
94+
debugProperty().bind(GameUI.PROPERTY_DEBUG_INFO_VISIBLE);
95+
canvasHeightProperty().bind(GameUI.PROPERTY_MINI_VIEW_HEIGHT);
96+
opacityProperty().bind(GameUI.PROPERTY_MINI_VIEW_OPACITY_PERCENT.divide(100.0));
97+
visibleProperty().bind(Bindings.createObjectBinding(
98+
() -> GameUI.PROPERTY_MINI_VIEW_ON.get() && ui.isCurrentGameSceneID(SCENE_ID_PLAY_SCENE_3D),
99+
GameUI.PROPERTY_MINI_VIEW_ON, GameUI.PROPERTY_CURRENT_GAME_SCENE
100+
));
101+
}
102+
103+
public void setGameLevel(GameLevel gameLevel) {
104+
this.gameLevel = requireNonNull(gameLevel);
89105
worldSizeProperty.set(gameLevel.worldSizePx());
90106
gr = ui.theConfiguration().createGameRenderer(canvas);
91107
gr.applyRenderingHints(gameLevel);
@@ -116,39 +132,37 @@ private Animation createMoveOffScreenAnimation() {
116132
return transition;
117133
}
118134

119-
public void draw(GameUI ui, GameLevel gameLevel) {
120-
GraphicsContext ctx = canvas.getGraphicsContext2D();
121-
float scaling = scalingProperty.floatValue();
122-
ctx.setFill(backgroundColorProperty().get());
123-
ctx.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
135+
public void draw() {
136+
drawCallCount += 1;
124137
if (gr == null) {
125-
Logger.warn("Cannot draw game scene without game renderer");
126-
return;
127-
}
128-
if (gameLevel == null) {
129-
Logger.warn("No game level to draw in mini game view");
130138
return;
131139
}
140+
float scaling = scalingProperty.get();
132141
gr.setScaling(scaling);
133-
gr.drawLevel(ui.theGameContext(),
134-
gameLevel,
135-
backgroundColorProperty().get(),
136-
false,
137-
gameLevel.blinking().isOn(),
138-
ui.theGameClock().tickCount());
139-
gameLevel.bonus().ifPresent(gr::drawActor);
140-
gr.drawActor(gameLevel.pac());
141-
Stream.of(ORANGE_GHOST_POKEY, CYAN_GHOST_BASHFUL, PINK_GHOST_SPEEDY, RED_GHOST_SHADOW)
142+
gr.ctx().setFill(backgroundColorProperty().get());
143+
gr.ctx().fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
144+
145+
if (gameLevel != null) {
146+
gr.drawLevel(ui.theGameContext(),
147+
gameLevel,
148+
backgroundColorProperty().get(),
149+
false,
150+
gameLevel.blinking().isOn(),
151+
ui.theGameClock().tickCount());
152+
gameLevel.bonus().ifPresent(gr::drawActor);
153+
gr.drawActor(gameLevel.pac());
154+
Stream.of(ORANGE_GHOST_POKEY, CYAN_GHOST_BASHFUL, PINK_GHOST_SPEEDY, RED_GHOST_SHADOW)
142155
.map(gameLevel::ghost)
143156
.forEach(gr::drawActor);
157+
}
158+
144159
if (debugProperty().get()) {
145-
ctx.save();
146-
ctx.setTextAlign(TextAlignment.CENTER);
147-
ctx.setFill(Color.WHITE);
148-
ctx.setFont(Font.font(14 * scaling));
149-
ctx.fillText("scaling: %.2f, draw calls: %d".formatted(scaling, drawCallCount), canvas.getWidth() * 0.5, 16 * scaling);
150-
ctx.restore();
160+
gr.ctx().save();
161+
gr.ctx().setTextAlign(TextAlignment.CENTER);
162+
gr.ctx().setFill(Color.WHITE);
163+
gr.ctx().setFont(Font.font(14 * scaling));
164+
gr.ctx().fillText("scaling: %.2f, draw calls: %d".formatted(scaling, drawCallCount), canvas.getWidth() * 0.5, 16 * scaling);
165+
gr.ctx().restore();
151166
}
152-
drawCallCount += 1;
153167
}
154168
}

pacman-ui/src/main/java/de/amr/pacmanfx/ui/layout/PlayView.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public PlayView(GameUI ui, Scene parentScene) {
8686
dashboard = new Dashboard(ui);
8787
dashboard.setVisible(false);
8888

89-
configureMiniView();
89+
miniView.setGameUI(ui);
9090
configureCanvasContainer();
9191
createLayout();
9292
configurePropertyBindings();
@@ -175,7 +175,7 @@ public void draw() {
175175
});
176176

177177
if (miniView.isVisible() && ui.isCurrentGameSceneID(SCENE_ID_PLAY_SCENE_3D) && ui.theGameContext().optGameLevel().isPresent()) {
178-
miniView.draw(ui, ui.theGameContext().theGameLevel());
178+
miniView.draw();
179179
}
180180

181181
// Dashboard updates must be called from permanent clock task too!
@@ -211,7 +211,7 @@ public void onGameEvent(GameEvent gameEvent) {
211211
ActorAnimationMap ghostAnimationMap = config.createGhostAnimations(ghost);
212212
ghost.setAnimations(ghostAnimationMap);
213213
});
214-
miniView.onLevelCreated(ui, gameLevel);
214+
miniView.setGameLevel(gameLevel);
215215

216216
// size of game scene might have changed, so re-embed
217217
ui.currentGameScene().ifPresent(gameScene -> embedGameScene(parentScene, gameScene));
@@ -310,17 +310,6 @@ private void embedGameScene2DWithoutSubScene(GameScene2D gameScene2D) {
310310

311311
// -----------------------------------------------------------------------------------------------------------------
312312

313-
private void configureMiniView() {
314-
miniView.backgroundColorProperty().bind(GameUI.PROPERTY_CANVAS_BACKGROUND_COLOR);
315-
miniView.debugProperty().bind(GameUI.PROPERTY_DEBUG_INFO_VISIBLE);
316-
miniView.canvasHeightProperty().bind(GameUI.PROPERTY_MINI_VIEW_HEIGHT);
317-
miniView.opacityProperty().bind(GameUI.PROPERTY_MINI_VIEW_OPACITY_PERCENT.divide(100.0));
318-
miniView.visibleProperty().bind(Bindings.createObjectBinding(
319-
() -> GameUI.PROPERTY_MINI_VIEW_ON.get() && ui.isCurrentGameSceneID(SCENE_ID_PLAY_SCENE_3D),
320-
GameUI.PROPERTY_MINI_VIEW_ON, GameUI.PROPERTY_CURRENT_GAME_SCENE
321-
));
322-
}
323-
324313
private void configureCanvasContainer() {
325314
canvasContainer.setMinScaling(0.5);
326315
// 28*TS x 36*TS = Arcade map size in pixels

0 commit comments

Comments
 (0)