11package pl .baftek .spitfire .screens ;
22
33import com .badlogic .gdx .graphics .Texture ;
4+ import com .badlogic .gdx .graphics .g2d .TextureRegion ;
45import com .badlogic .gdx .scenes .scene2d .InputEvent ;
5- import com .badlogic .gdx .scenes .scene2d .ui .*;
6+ import com .badlogic .gdx .scenes .scene2d .ui .HorizontalGroup ;
7+ import com .badlogic .gdx .scenes .scene2d .ui .Image ;
8+ import com .badlogic .gdx .scenes .scene2d .ui .Label ;
9+ import com .badlogic .gdx .scenes .scene2d .ui .Table ;
610import com .badlogic .gdx .scenes .scene2d .utils .ClickListener ;
11+ import com .badlogic .gdx .scenes .scene2d .utils .TextureRegionDrawable ;
712import com .badlogic .gdx .utils .Align ;
813import com .badlogic .gdx .utils .Timer ;
914import pl .baftek .spitfire .enums .PlayerType ;
@@ -18,18 +23,74 @@ public class HangarScreen extends AbstractScreen {
1823 private Timer timer ;
1924
2025 private Table table ;
21- private Image moneyImage ;
22- private Label upgradesTitle ;
23- private Label moneyLabel ;
24- private MyTextButton exitButton ;
2526
26- private HorizontalGroup upGroup ;
27- private VerticalGroup mainVerticalGroup ;
28- private VerticalGroup contentVG ;
29-
30- private HorizontalGroup scrollerHG ;
27+ private Label currentPlaneLabel ;
28+ private Label availabilityLabel ;
3129 private MyTextButton actionButton ;
30+ private Image moneyImageInActionButton ;
3231 private Image planeImage ;
32+ private Label descLabel ;
33+
34+ private State state ;
35+
36+ private static class State {
37+ private final PlayerType currentPlayerType ;
38+ private final String availabilityString ;
39+ private final String currentPlaneString ;
40+ private final String action ;
41+ private final String desc ;
42+ private final boolean bought ;
43+ private final Texture texture ;
44+
45+ public State (PlayerType playerType , SpitfireGame game ) {
46+ this .currentPlayerType = playerType ;
47+
48+ if (game .isBought (playerType )) {
49+ game .setCurrentPlayerType (playerType );
50+ }
51+
52+ if (playerType == PlayerType .SPITFIRE ) {
53+ texture = SpitfireGame .ResHelper .spitfire ;
54+ currentPlaneString = StringHelper .SPITIFRE ;
55+
56+ action = StringHelper .UPGRADE ;
57+ desc = StringHelper .SPITFIRE_DESC ;
58+ bought = true ;
59+ } else if (playerType == PlayerType .MUSTANG ) {
60+ texture = SpitfireGame .ResHelper .mustang ;
61+ currentPlaneString = StringHelper .MUSTANG ;
62+ desc = StringHelper .MUSTANG_DESC ;
63+
64+ if (game .isBought (PlayerType .MUSTANG )) {
65+ action = StringHelper .UPGRADE ;
66+ bought = true ;
67+ } else {
68+ action = StringHelper .BUY + game .getPlanePrice (PlayerType .MUSTANG );
69+ bought = false ;
70+ }
71+ } else if (playerType == PlayerType .SZTURMOVIK ) {
72+ texture = SpitfireGame .ResHelper .szturmovik ;
73+ currentPlaneString = StringHelper .IL2 ;
74+ desc = StringHelper .IL2_DESC ;
75+
76+ if (game .isBought (PlayerType .SZTURMOVIK )) {
77+ action = StringHelper .UPGRADE ;
78+ bought = true ;
79+ } else {
80+ action = StringHelper .BUY + game .getPlanePrice (PlayerType .SZTURMOVIK );
81+ bought = false ;
82+ }
83+ } else {
84+ desc = null ;
85+ action = null ;
86+ texture = null ;
87+ currentPlaneString = null ;
88+ bought = false ;
89+ }
90+
91+ availabilityString = Integer .toString (game .getCurrentPlaneAvailabilityLevel (playerType ));
92+ }
93+ }
3394
3495 HangarScreen (SpitfireGame game ) {
3596 super (game );
@@ -38,108 +99,35 @@ public class HangarScreen extends AbstractScreen {
3899 @ Override
39100 protected void init () {
40101 timer = new Timer ();
102+ state = new State (game .getCurrentPlayerType (), game );
41103 }
42104
43105 @ Override
44106 protected void buildUI () {
45- upGroup = new HorizontalGroup ();
46- upGroup .padTop (10 );
47- upGroup .padBottom (10 );
48- upGroup .space (10 );
49-
50- mainVerticalGroup = new VerticalGroup ();
51- mainVerticalGroup .padTop (20 );
52- mainVerticalGroup .padBottom (20 );
53- mainVerticalGroup .space (20 );
107+ table = new Table ();
108+ table .setFillParent (true );
109+ table .top ();
54110
111+ initUpGroup ();
55112 initTitle ();
113+ initContentGroup ();
56114 initExitButton ();
57- initUpGroup ();
58- initContentGroup (game .getCurrentPlayerType ());
59115
60- table = new Table ();
61- table .add (upGroup ).row ();
62- table .add (upgradesTitle ).row ();
63- table .add (mainVerticalGroup ).height (950 ).row ();
64- table .add (exitButton ).row ();
65- table .top (); //sets table in upper part of the screen, not middle
66- table .setFillParent (true );
67116 stage .addActor (table );
68117 }
69118
70- private void initContentGroup (final PlayerType playerType ) {
71- //init section
72- String availabilityString ;
73- String currentPlaneString ;
74- String action ;
75- String desc ;
76- boolean bought ;
77-
78- Texture texture ;
79-
80- contentVG = new VerticalGroup ();
81- contentVG .space (10 );
82-
83- scrollerHG = new HorizontalGroup ();
84- scrollerHG .space (30 );
85-
86- System .out .println ("playerType:" + playerType .toString ());
87-
88- if (game .isBought (playerType )) {
89- game .setCurrentPlayerType (playerType );
90- }
119+ private void initContentGroup () {
120+ planeImage = new Image ();
91121
92- if ( playerType == PlayerType . SPITFIRE ) {
93- texture = SpitfireGame .ResHelper .spitfire ;
94- currentPlaneString = StringHelper . SPITIFRE ;
122+ actionButton = new MyTextButton ( null , FONT_SIZE_3 );
123+ moneyImageInActionButton = new Image ( SpitfireGame .ResHelper .smallMoney ) ;
124+ actionButton . add ( moneyImageInActionButton ). right () ;
95125
96- action = StringHelper .UPGRADE ;
97- desc = StringHelper .SPITFIRE_DESC ;
98- bought = true ;
99- } else if (playerType == PlayerType .MUSTANG ) {
100- texture = SpitfireGame .ResHelper .mustang ;
101- currentPlaneString = StringHelper .MUSTANG ;
102- desc = StringHelper .MUSTANG_DESC ;
103-
104- if (game .isBought (PlayerType .MUSTANG )) {
105- action = StringHelper .UPGRADE ;
106- bought = true ;
107- } else {
108- action = StringHelper .BUY + game .getPlanePrice (PlayerType .MUSTANG );
109- bought = false ;
110- }
111- } else if (playerType == PlayerType .SZTURMOVIK ) {
112- texture = SpitfireGame .ResHelper .szturmovik ;
113- currentPlaneString = StringHelper .IL2 ;
114- desc = StringHelper .IL2_DESC ;
115-
116- if (game .isBought (PlayerType .SZTURMOVIK )) {
117- action = StringHelper .UPGRADE ;
118- bought = true ;
119- } else {
120- action = StringHelper .BUY + game .getPlanePrice (PlayerType .SZTURMOVIK );
121- bought = false ;
122- }
123- } else {
124- desc = null ;
125- action = null ;
126- texture = null ;
127- currentPlaneString = null ;
128- bought = false ;
129- }
130-
131- availabilityString = Integer .toString (game .getCurrentPlaneAvailabilityLevel (playerType ));
132-
133- planeImage = new Image (texture );
134-
135- actionButton = new MyTextButton (action , FONT_SIZE_3 );
136- if (!bought ) {
137- Image moneyImage = new Image (SpitfireGame .ResHelper .smallMoney );
138- actionButton .add (moneyImage ).right ();
139- }
140126 actionButton .addListener (new ClickListener () {
141127 @ Override
142128 public void clicked (InputEvent event , float x , float y ) {
129+ PlayerType playerType = state .currentPlayerType ;
130+
143131 //spitfire
144132 if (playerType == PlayerType .SPITFIRE ) {
145133 game .setScreen (new UpgradesScreen (game ));
@@ -149,7 +137,7 @@ public void clicked(InputEvent event, float x, float y) {
149137 if (playerType == PlayerType .MUSTANG && !game .isBought (PlayerType .MUSTANG )) {
150138 game .buyPlane (PlayerType .MUSTANG , stage );
151139 game .setCurrentPlayerType (PlayerType .MUSTANG );
152- refreshContentGroup (PlayerType .MUSTANG );
140+ updateContentGroup (PlayerType .MUSTANG );
153141 } else if (playerType == PlayerType .MUSTANG && game .isBought (PlayerType .MUSTANG )) {
154142 game .setScreen (new UpgradesScreen (game ));
155143 }
@@ -158,7 +146,7 @@ public void clicked(InputEvent event, float x, float y) {
158146 if (playerType == PlayerType .SZTURMOVIK && !game .isBought (PlayerType .SZTURMOVIK )) {
159147 game .buyPlane (PlayerType .SZTURMOVIK , stage );
160148 game .setCurrentPlayerType (PlayerType .SZTURMOVIK );
161- refreshContentGroup (PlayerType .SZTURMOVIK );
149+ updateContentGroup (PlayerType .SZTURMOVIK );
162150 } else if (playerType == PlayerType .SZTURMOVIK && game .isBought (PlayerType .SZTURMOVIK )) {
163151 game .setScreen (new UpgradesScreen (game ));
164152 }
@@ -171,12 +159,14 @@ public void clicked(InputEvent event, float x, float y) {
171159 left .addListener (new ClickListener () {
172160 @ Override
173161 public void clicked (InputEvent event , float x , float y ) {
162+ PlayerType playerType = state .currentPlayerType ;
163+
174164 if (playerType == PlayerType .SPITFIRE ) {
175- refreshContentGroup (PlayerType .MUSTANG );
165+ updateContentGroup (PlayerType .MUSTANG );
176166 } else if (playerType == PlayerType .MUSTANG ) {
177- refreshContentGroup (PlayerType .SZTURMOVIK );
167+ updateContentGroup (PlayerType .SZTURMOVIK );
178168 } else if (playerType == PlayerType .SZTURMOVIK ) {
179- refreshContentGroup (PlayerType .SPITFIRE );
169+ updateContentGroup (PlayerType .SPITFIRE );
180170 }
181171
182172 super .clicked (event , x , y );
@@ -187,52 +177,76 @@ public void clicked(InputEvent event, float x, float y) {
187177 right .addListener (new ClickListener () {
188178 @ Override
189179 public void clicked (InputEvent event , float x , float y ) {
180+ PlayerType playerType = state .currentPlayerType ;
181+
190182 if (playerType == PlayerType .SPITFIRE ) {
191- refreshContentGroup (PlayerType .SZTURMOVIK );
183+ updateContentGroup (PlayerType .SZTURMOVIK );
192184 } else if (playerType == PlayerType .SZTURMOVIK ) {
193- refreshContentGroup (PlayerType .MUSTANG );
185+ updateContentGroup (PlayerType .MUSTANG );
194186 } else if (playerType == PlayerType .MUSTANG ) {
195- refreshContentGroup (PlayerType .SPITFIRE );
187+ updateContentGroup (PlayerType .SPITFIRE );
196188 }
197189
198190 super .clicked (event , x , y );
199191 }
200192 });
201193
202- Label currentPlaneLabel = new Label (currentPlaneString , whiteLabelStyle );
194+ currentPlaneLabel = new Label (null , whiteLabelStyle );
203195 currentPlaneLabel .setFontScale (FONT_SIZE_4 );
204196
205- Label availabilityLabel = new Label (StringHelper . AVAILABLE_FROM_LEVEL + availabilityString , orangeLabelStyle );
197+ availabilityLabel = new Label (null , orangeLabelStyle );
206198 availabilityLabel .setFontScale (FONT_SIZE_2 );
207199
208- Label descLabel = new Label (desc , whiteLabelStyle );
200+ descLabel = new Label (null , whiteLabelStyle );
209201 descLabel .setFontScale (FONT_SIZE_1 );
210202 descLabel .setAlignment (Align .center );
211203
212- scrollerHG .addActor (left );
213- scrollerHG .addActor (currentPlaneLabel );
214- scrollerHG .addActor (right );
204+ table .add (left ).padLeft (16 ).padRight (16 );
205+ table .add (currentPlaneLabel ).expandX ();
206+ table .add (right ).pad (16 ).padRight (16 );
207+ table .row ();
208+
209+ table .add (availabilityLabel ).colspan (3 );
210+ table .row ();
211+
212+ table .add (actionButton ).pad (32 ).colspan (3 );
213+ table .row ();
214+
215+ table .add (planeImage ).colspan (3 );
216+ table .row ();
215217
216- contentVG .addActor (availabilityLabel );
217- contentVG .addActor (scrollerHG );
218- contentVG .addActor (actionButton );
219- contentVG .addActor (planeImage );
220- contentVG .addActor (descLabel );
218+ table .add (descLabel ).padTop (16 ).colspan (3 );
219+ table .row ();
221220
222- mainVerticalGroup .addActor (contentVG );
221+ // Set initial values
222+ updateContentGroup (game .getCurrentPlayerType ());
223223 }
224224
225- private void refreshContentGroup (PlayerType playerType ) {
226- mainVerticalGroup .removeActor (contentVG );
227- initContentGroup (playerType );
225+ private void updateContentGroup (PlayerType playerType ) {
226+ if (game .isBought (playerType )) {
227+ game .setCurrentPlayerType (playerType );
228+ }
229+
230+ state = new State (playerType , game );
231+ currentPlaneLabel .setText (state .currentPlaneString );
232+ availabilityLabel .setText (StringHelper .AVAILABLE_FROM_LEVEL + state .availabilityString );
233+ actionButton .setText (state .action );
234+ moneyImageInActionButton .setVisible (!state .bought );
235+ if (state .bought ) {
236+ moneyImageInActionButton .setDrawable (null );
237+ } else {
238+ moneyImageInActionButton .setDrawable (new TextureRegionDrawable (new TextureRegion (SpitfireGame .ResHelper .smallMoney )));
239+ }
240+ planeImage .setDrawable (new TextureRegionDrawable (new TextureRegion (state .texture )));
241+ descLabel .setText (state .desc );
228242 }
229243
230244 private void initUpGroup () {
231245 HorizontalGroup moneyHG = new HorizontalGroup ();
232246
233- moneyImage = new Image (SpitfireGame .ResHelper .smallMoney );
247+ Image moneyImage = new Image (SpitfireGame .ResHelper .smallMoney );
234248
235- moneyLabel = new Label (Integer .toString (game .playerManager .getMoney ()), whiteLabelStyle );
249+ Label moneyLabel = new Label (Integer .toString (game .playerManager .getMoney ()), whiteLabelStyle );
236250 moneyLabel .setFontScale (FONT_SIZE_3 );
237251
238252 //refreshing money
@@ -246,23 +260,29 @@ public void run() {
246260 moneyHG .addActor (moneyLabel );
247261 moneyHG .addActor (moneyImage );
248262
249- upGroup .addActor (moneyHG );
263+ table .add (moneyHG ).colspan (3 );
264+ table .row ();
250265 }
251266
252267 private void initTitle () {
253- upgradesTitle = new Label (StringHelper .HANGAR , whiteLabelStyle );
268+ Label upgradesTitle = new Label (StringHelper .HANGAR , whiteLabelStyle );
254269 upgradesTitle .setFontScale (FONT_SIZE_6 );
270+
271+ table .add (upgradesTitle ).colspan (3 );
272+ table .row ();
255273 }
256274
257275 private void initExitButton () {
258- exitButton = new MyTextButton (StringHelper .GO_TO_MENU , FONT_SIZE_3 );
276+ MyTextButton exitButton = new MyTextButton (StringHelper .GO_TO_MENU , FONT_SIZE_3 );
259277 exitButton .addListener (new ClickListener () {
260278 @ Override
261279 public void clicked (InputEvent event , float x , float y ) {
262280 game .setScreen (new MenuScreen (game ));
263281 super .clicked (event , x , y );
264282 }
265283 });
284+
285+ table .add (exitButton ).expand ().padBottom (32 ).align (Align .bottom ).colspan (3 );
266286 }
267287
268288 @ Override
0 commit comments