Skip to content

Commit 0d18125

Browse files
Refac
1 parent ab45055 commit 0d18125

File tree

2 files changed

+67
-79
lines changed

2 files changed

+67
-79
lines changed

pacman-ui/src/main/java/de/amr/pacmanfx/ui/_2d/PopupLayer.java renamed to pacman-ui/src/main/java/de/amr/pacmanfx/ui/_2d/HelpLayer.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@
1212

1313
import static de.amr.pacmanfx.uilib.Ufx.colorWithOpacity;
1414

15-
/**
16-
* @author Armin Reichert
17-
*/
18-
public class PopupLayer extends Pane {
15+
public class HelpLayer extends Pane {
1916

2017
private final FadingPane helpPopUp = new FadingPane();
2118

22-
public PopupLayer(CrudeCanvasContainer canvasContainer) {
19+
public HelpLayer(CrudeCanvasContainer canvasContainer) {
2320
getChildren().addAll(helpPopUp);
2421

2522
minHeightProperty().bind(canvasContainer.minHeightProperty());

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

Lines changed: 65 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import de.amr.pacmanfx.ui.*;
1313
import de.amr.pacmanfx.ui._2d.CrudeCanvasContainer;
1414
import de.amr.pacmanfx.ui._2d.GameScene2D;
15-
import de.amr.pacmanfx.ui._2d.PopupLayer;
15+
import de.amr.pacmanfx.ui._2d.HelpLayer;
1616
import de.amr.pacmanfx.ui._3d.PlayScene3D;
1717
import de.amr.pacmanfx.ui.dashboard.Dashboard;
1818
import de.amr.pacmanfx.ui.dashboard.DashboardID;
@@ -22,7 +22,6 @@
2222
import javafx.scene.Scene;
2323
import javafx.scene.SubScene;
2424
import javafx.scene.canvas.Canvas;
25-
import javafx.scene.canvas.GraphicsContext;
2625
import javafx.scene.control.ContextMenu;
2726
import javafx.scene.control.MenuItem;
2827
import javafx.scene.input.ContextMenuEvent;
@@ -67,48 +66,46 @@ private static byte identifySceneSwitchType(GameScene sceneBefore, GameScene sce
6766
};
6867
}
6968

70-
private final ActionBindingManager actionBindings;
71-
69+
private final ActionBindingManager actionBindings = new DefaultActionBindingManager();
7270
private final GameUI ui;
7371
private final Scene parentScene;
74-
75-
private BorderPane canvasLayer;
76-
private PopupLayer popupLayer; // help, signature
77-
private BorderPane dashboardLayer;
78-
7972
private final Dashboard dashboard;
80-
private final Canvas commonCanvas = new Canvas();
81-
private final CrudeCanvasContainer canvasContainer = new CrudeCanvasContainer(commonCanvas);
82-
private final MiniGameView miniGameView;
73+
private final Canvas canvas = new Canvas();
74+
private final CrudeCanvasContainer canvasContainer = new CrudeCanvasContainer(canvas);
75+
private final MiniGameView miniView = new MiniGameView();
8376
private final ContextMenu contextMenu = new ContextMenu();
8477

78+
private final BorderPane canvasLayer = new BorderPane();
79+
private final BorderPane dashboardAndMiniViewLayer = new BorderPane();
80+
private HelpLayer helpLayer; // help
81+
8582
public PlayView(GameUI ui, Scene parentScene) {
8683
this.ui = requireNonNull(ui);
8784
this.parentScene = requireNonNull(parentScene);
88-
this.miniGameView = new MiniGameView();
89-
this.dashboard = new Dashboard(ui);
90-
91-
this.actionBindings = new DefaultActionBindingManager();
9285

93-
configureMiniGameView();
86+
dashboard = new Dashboard(ui);
87+
dashboard.setVisible(false);
88+
89+
configureMiniView();
9490
configureCanvasContainer();
9591
createLayout();
9692
configurePropertyBindings();
9793

98-
//TODO what is the cleanest solution to hide the context menu in all needed cases?
9994
setOnContextMenuRequested(this::handleContextMenuRequest);
100-
// game scene changes: hide it
101-
GameUI.PROPERTY_CURRENT_GAME_SCENE.addListener(
102-
(obs, oldGameScene, newGameScene) -> handleGameSceneChange(parentScene, newGameScene));
103-
// any other mouse button clicked: hide it
95+
//TODO what is the recommended way to hide the context menu?
10496
parentScene.addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
105-
if (e.getButton() == MouseButton.PRIMARY) {
97+
if (e.getButton() != MouseButton.SECONDARY) {
10698
contextMenu.hide();
10799
}
108100
});
109101

110-
parentScene.widthProperty() .addListener((py, ov, width) -> canvasContainer.resizeTo(width.doubleValue(), parentScene.getHeight()));
111-
parentScene.heightProperty().addListener((py, ov, height) -> canvasContainer.resizeTo(parentScene.getWidth(), height.doubleValue()));
102+
GameUI.PROPERTY_CURRENT_GAME_SCENE.addListener((py, ov, gameScene) -> {
103+
contextMenu.hide();
104+
if (gameScene != null) embedGameScene(parentScene, gameScene);
105+
});
106+
107+
parentScene.widthProperty() .addListener((py, ov, w) -> canvasContainer.resizeTo(w.doubleValue(), parentScene.getHeight()));
108+
parentScene.heightProperty().addListener((py, ov, h) -> canvasContainer.resizeTo(parentScene.getWidth(), h.doubleValue()));
112109

113110
actionBindings.use(ACTION_BOOT_SHOW_PLAY_VIEW, DEFAULT_ACTION_BINDINGS);
114111
actionBindings.use(ACTION_ENTER_FULLSCREEN, DEFAULT_ACTION_BINDINGS);
@@ -129,11 +126,6 @@ public PlayView(GameUI ui, Scene parentScene) {
129126
actionBindings.use(ACTION_TOGGLE_PLAY_SCENE_2D_3D, DEFAULT_ACTION_BINDINGS);
130127
}
131128

132-
private void handleGameSceneChange(Scene parentScene, GameScene newScene) {
133-
if (newScene != null) embedGameScene(parentScene, newScene);
134-
contextMenu.hide();
135-
}
136-
137129
private void handleContextMenuRequest(ContextMenuEvent contextMenuEvent) {
138130
contextMenu.getItems().clear();
139131
ui.currentGameScene().ifPresent(gameScene -> {
@@ -158,7 +150,7 @@ private void handleContextMenuRequest(ContextMenuEvent contextMenuEvent) {
158150
}
159151

160152
public void showHelp(GameUI ui) {
161-
popupLayer.showHelp(ui, canvasContainer.scaling());
153+
helpLayer.showHelp(ui, canvasContainer.scaling());
162154
}
163155

164156
// -----------------------------------------------------------------------------------------------------------------
@@ -182,12 +174,12 @@ public void draw() {
182174
}
183175
});
184176

185-
if (miniGameView.isVisible() && ui.isCurrentGameSceneID(SCENE_ID_PLAY_SCENE_3D) && ui.theGameContext().optGameLevel().isPresent()) {
186-
miniGameView.draw(ui, ui.theGameContext().theGameLevel());
177+
if (miniView.isVisible() && ui.isCurrentGameSceneID(SCENE_ID_PLAY_SCENE_3D) && ui.theGameContext().optGameLevel().isPresent()) {
178+
miniView.draw(ui, ui.theGameContext().theGameLevel());
187179
}
188180

189181
// Dashboard updates must be called from permanent clock task too!
190-
if (dashboardLayer.isVisible()) {
182+
if (dashboardAndMiniViewLayer.isVisible()) {
191183
dashboard.infoBoxes().filter(InfoBox::isExpanded).forEach(InfoBox::update);
192184
}
193185
}
@@ -219,14 +211,14 @@ public void onGameEvent(GameEvent gameEvent) {
219211
ActorAnimationMap ghostAnimationMap = config.createGhostAnimations(ghost);
220212
ghost.setAnimations(ghostAnimationMap);
221213
});
222-
miniGameView.onLevelCreated(ui, gameLevel);
214+
miniView.onLevelCreated(ui, gameLevel);
223215

224216
// size of game scene might have changed, so re-embed
225217
ui.currentGameScene().ifPresent(gameScene -> embedGameScene(parentScene, gameScene));
226218
}
227219
case GAME_STATE_CHANGED -> {
228220
if (ui.theGameContext().theGameState() == GameState.LEVEL_COMPLETE) {
229-
miniGameView.onLevelCompleted();
221+
miniView.onLevelCompleted();
230222
}
231223
}
232224
}
@@ -293,36 +285,37 @@ private void embedGameScene(Scene parentScene, GameScene gameScene) {
293285
getChildren().set(0, subScene);
294286
}
295287
else if (gameScene instanceof GameScene2D gameScene2D) {
296-
embedGameScene2DDirectly(gameScene2D);
297-
gameScene2D.backgroundColorProperty().bind(GameUI.PROPERTY_CANVAS_BACKGROUND_COLOR);
298-
gameScene2D.clear();
288+
embedGameScene2DWithoutSubScene(gameScene2D);
289+
getChildren().set(0, canvasLayer);
299290
}
300291
else {
301292
Logger.error("Cannot embed play scene of class {}", gameScene.getClass().getName());
302293
}
303294
}
304295

305-
// Game scenes without camera are drawn into the canvas provided by this play view
306-
private void embedGameScene2DDirectly(GameScene2D gameScene2D) {
307-
gameScene2D.setCanvas(commonCanvas);
308-
gameScene2D.setGameRenderer(ui.theConfiguration().createGameRenderer(commonCanvas));
296+
// 2D game scenes without sub-scene/camera (Arcade play scene, cut scenes) are drawn into the canvas provided by this play view
297+
private void embedGameScene2DWithoutSubScene(GameScene2D gameScene2D) {
298+
gameScene2D.setCanvas(canvas);
299+
gameScene2D.setGameRenderer(ui.theConfiguration().createGameRenderer(canvas));
300+
gameScene2D.clear();
301+
gameScene2D.backgroundColorProperty().bind(GameUI.PROPERTY_CANVAS_BACKGROUND_COLOR);
309302
gameScene2D.scalingProperty().bind(canvasContainer.scalingProperty().map(
310303
scaling -> Math.min(scaling.doubleValue(), ui.theUIPrefs().getFloat("scene2d.max_scaling"))));
311-
Vector2f sizePx = gameScene2D.sizeInPx();
312-
canvasContainer.setUnscaledCanvasSize(sizePx.x(), sizePx.y());
304+
305+
Vector2f gameSceneSizePx = gameScene2D.sizeInPx();
306+
canvasContainer.setUnscaledCanvasSize(gameSceneSizePx.x(), gameSceneSizePx.y());
313307
canvasContainer.resizeTo(parentScene.getWidth(), parentScene.getHeight());
314308
canvasContainer.backgroundProperty().bind(GameUI.PROPERTY_CANVAS_BACKGROUND_COLOR.map(Ufx::colorBackground));
315-
getChildren().set(0, canvasLayer);
316309
}
317310

318311
// -----------------------------------------------------------------------------------------------------------------
319312

320-
private void configureMiniGameView() {
321-
miniGameView.backgroundColorProperty().bind(GameUI.PROPERTY_CANVAS_BACKGROUND_COLOR);
322-
miniGameView.debugProperty().bind(GameUI.PROPERTY_DEBUG_INFO_VISIBLE);
323-
miniGameView.canvasHeightProperty().bind(GameUI.PROPERTY_MINI_VIEW_HEIGHT);
324-
miniGameView.opacityProperty().bind(GameUI.PROPERTY_MINI_VIEW_OPACITY_PERCENT.divide(100.0));
325-
miniGameView.visibleProperty().bind(Bindings.createObjectBinding(
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(
326319
() -> GameUI.PROPERTY_MINI_VIEW_ON.get() && ui.isCurrentGameSceneID(SCENE_ID_PLAY_SCENE_3D),
327320
GameUI.PROPERTY_MINI_VIEW_ON, GameUI.PROPERTY_CURRENT_GAME_SCENE
328321
));
@@ -331,42 +324,42 @@ private void configureMiniGameView() {
331324
private void configureCanvasContainer() {
332325
canvasContainer.setMinScaling(0.5);
333326
// 28*TS x 36*TS = Arcade map size in pixels
334-
canvasContainer.setUnscaledCanvasSize(28 *TS, 36 * TS);
327+
canvasContainer.setUnscaledCanvasSize(28 * TS, 36 * TS);
335328
canvasContainer.setBorderColor(Color.rgb(222, 222, 255));
336-
337-
//canvasContainer.roundedBorderProperty().addListener((py, ov, nv) -> ui.currentGameScene().ifPresent(this::embedGameScene));
338329
}
339330

340331
private void configurePropertyBindings() {
341-
GraphicsContext ctx = commonCanvas.getGraphicsContext2D();
342-
GameUI.PROPERTY_CANVAS_FONT_SMOOTHING.addListener((py, ov, on) -> ctx.setFontSmoothingType(on ? FontSmoothingType.LCD : FontSmoothingType.GRAY));
343-
GameUI.PROPERTY_CANVAS_IMAGE_SMOOTHING.addListener((py, ov, on) -> ctx.setImageSmoothing(on));
344-
GameUI.PROPERTY_DEBUG_INFO_VISIBLE.addListener((py, ov, debug) -> {
345-
canvasLayer.setBackground(debug? colorBackground(Color.TEAL) : null);
346-
canvasLayer.setBorder(debug? border(Color.LIGHTGREEN, 1) : null);
347-
});
332+
GameUI.PROPERTY_CANVAS_FONT_SMOOTHING.addListener((py, ov, smooth)
333+
-> canvas.getGraphicsContext2D().setFontSmoothingType(smooth ? FontSmoothingType.LCD : FontSmoothingType.GRAY));
334+
335+
GameUI.PROPERTY_CANVAS_IMAGE_SMOOTHING.addListener((py, ov, smooth)
336+
-> canvas.getGraphicsContext2D().setImageSmoothing(smooth));
337+
338+
GameUI.PROPERTY_DEBUG_INFO_VISIBLE.addListener((py, ov, debug)
339+
-> {
340+
canvasLayer.setBackground(debug ? colorBackground(Color.TEAL) : null);
341+
canvasLayer.setBorder(debug ? border(Color.LIGHTGREEN, 1) : null);
342+
});
348343
}
349344

350345
private void createLayout() {
351-
canvasLayer = new BorderPane(canvasContainer);
346+
canvasLayer.setCenter(canvasContainer);
352347

353-
dashboardLayer = new BorderPane();
354-
dashboardLayer.visibleProperty().bind(Bindings.createObjectBinding(
348+
dashboardAndMiniViewLayer.setLeft(dashboard);
349+
dashboardAndMiniViewLayer.setRight(miniView);
350+
dashboardAndMiniViewLayer.visibleProperty().bind(Bindings.createObjectBinding(
355351
() -> dashboard.isVisible() || GameUI.PROPERTY_MINI_VIEW_ON.get(),
356352
dashboard.visibleProperty(), GameUI.PROPERTY_MINI_VIEW_ON
357353
));
358-
dashboardLayer.setLeft(dashboard);
359-
dashboardLayer.setRight(miniGameView);
360-
dashboard.setVisible(false);
361354

362355
//TODO reconsider help functionality
363-
popupLayer = new PopupLayer(canvasContainer);
364-
popupLayer.setMouseTransparent(true);
356+
helpLayer = new HelpLayer(canvasContainer);
357+
helpLayer.setMouseTransparent(true);
365358

366-
getChildren().addAll(canvasLayer, dashboardLayer, popupLayer);
359+
getChildren().addAll(canvasLayer, dashboardAndMiniViewLayer, helpLayer);
367360
}
368361

369-
// Dashboard
362+
// Dashboard access
370363

371364
public void configureDashboard(List<DashboardID> dashboardIDs) {
372365
dashboard.configure(dashboardIDs);
@@ -383,6 +376,4 @@ public void showDashboard(boolean show) {
383376
public void toggleDashboard() {
384377
dashboard.toggleVisibility();
385378
}
386-
387-
388379
}

0 commit comments

Comments
 (0)