15
15
import javafx .beans .property .*;
16
16
import javafx .geometry .Insets ;
17
17
import javafx .scene .canvas .Canvas ;
18
- import javafx .scene .canvas .GraphicsContext ;
19
18
import javafx .scene .layout .Background ;
20
19
import javafx .scene .layout .HBox ;
21
20
import javafx .scene .layout .Priority ;
24
23
import javafx .scene .text .Font ;
25
24
import javafx .scene .text .TextAlignment ;
26
25
import javafx .util .Duration ;
27
- import org .tinylog .Logger ;
28
26
29
27
import java .util .stream .Stream ;
30
28
31
29
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 ;
32
32
33
33
public class MiniGameView extends VBox {
34
34
@@ -43,7 +43,10 @@ public class MiniGameView extends VBox {
43
43
44
44
private final Canvas canvas ;
45
45
private final HBox canvasContainer ;
46
+
47
+ private GameUI ui ;
46
48
private GameRenderer gr ;
49
+ private GameLevel gameLevel ;
47
50
private long drawCallCount ;
48
51
49
52
private final Animation moveIntoScreenAnimation ;
@@ -85,7 +88,20 @@ public DoubleProperty canvasHeightProperty() {
85
88
return canvasHeightProperty ;
86
89
}
87
90
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 );
89
105
worldSizeProperty .set (gameLevel .worldSizePx ());
90
106
gr = ui .theConfiguration ().createGameRenderer (canvas );
91
107
gr .applyRenderingHints (gameLevel );
@@ -116,39 +132,37 @@ private Animation createMoveOffScreenAnimation() {
116
132
return transition ;
117
133
}
118
134
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 ;
124
137
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" );
130
138
return ;
131
139
}
140
+ float scaling = scalingProperty .get ();
132
141
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 )
142
155
.map (gameLevel ::ghost )
143
156
.forEach (gr ::drawActor );
157
+ }
158
+
144
159
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 ();
151
166
}
152
- drawCallCount += 1 ;
153
167
}
154
168
}
0 commit comments