Project

General

Profile

Download (13.2 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ cc8ab8e3

1 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 fcd5b990 Leszek Koltunski
package org.distorted.screens;
21 211b48f2 Leszek Koltunski
22 179f7189 Leszek Koltunski
import java.lang.ref.WeakReference;
23
24
import android.app.Activity;
25 211b48f2 Leszek Koltunski
import android.content.SharedPreferences;
26 e3c74c0f Leszek Koltunski
import android.util.TypedValue;
27 211b48f2 Leszek Koltunski
import android.view.LayoutInflater;
28
import android.view.View;
29 85038b84 Leszek Koltunski
import android.view.ViewGroup;
30 e07c48a2 Leszek Koltunski
import android.widget.GridLayout;
31 211b48f2 Leszek Koltunski
import android.widget.ImageButton;
32
import android.widget.LinearLayout;
33 d6e7c7fb Leszek Koltunski
import android.widget.RelativeLayout;
34 85038b84 Leszek Koltunski
import android.widget.ScrollView;
35 10373dc7 Leszek Koltunski
import android.widget.TextView;
36 211b48f2 Leszek Koltunski
37 5f3801d3 Leszek Koltunski
import org.distorted.dialogs.RubikDialogUpdates;
38 acabdd83 Leszek Koltunski
import org.distorted.external.RubikNetwork;
39
import org.distorted.external.RubikUpdates;
40 3f7a4363 Leszek Koltunski
41
import org.distorted.main.R;
42
import org.distorted.main.RubikActivity;
43 e03e0352 Leszek Koltunski
import org.distorted.dialogs.RubikDialogAbout;
44 55e6be1d Leszek Koltunski
import org.distorted.helpers.TransparentImageButton;
45 d433b50e Leszek Koltunski
import org.distorted.objects.RubikObject;
46
import org.distorted.objects.RubikObjectList;
47 211b48f2 Leszek Koltunski
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50 85038b84 Leszek Koltunski
public class RubikScreenPlay extends RubikScreenAbstract implements RubikNetwork.Updatee
51 211b48f2 Leszek Koltunski
  {
52 6e3fcb91 Leszek Koltunski
  public static final int NUM_COLUMNS  = 5;
53 00af5060 Leszek Koltunski
  public static final int LEVELS_SHOWN = 10;
54 211b48f2 Leszek Koltunski
55 113d9d67 Leszek Koltunski
  private TransparentImageButton mAboutButton, mExitButton;
56 10373dc7 Leszek Koltunski
  private TextView mBubbleUpdates;
57 85038b84 Leszek Koltunski
  private int mCurrentBubbleNumber;
58 85b09df4 Leszek Koltunski
  private int mLevelValue;
59 cc8ab8e3 Leszek Koltunski
  private int mColCount, mRowCount;
60
  private float mScreenWidth;
61 179f7189 Leszek Koltunski
  private WeakReference<RubikActivity> mWeakAct;
62 255492a0 Leszek Koltunski
63 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65 f5da732a Leszek Koltunski
  void leaveScreen(RubikActivity act)
66 211b48f2 Leszek Koltunski
    {
67 85038b84 Leszek Koltunski
    act.switchRenderingOn();
68
    LinearLayout hiddenView = act.findViewById(R.id.hiddenView);
69
    hiddenView.removeAllViews();
70 211b48f2 Leszek Koltunski
    }
71
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74 f5da732a Leszek Koltunski
  void enterScreen(final RubikActivity act)
75 211b48f2 Leszek Koltunski
    {
76 85038b84 Leszek Koltunski
    act.switchRenderingOff();
77 179f7189 Leszek Koltunski
    mWeakAct = new WeakReference<>(act);
78 d433b50e Leszek Koltunski
    int numObjects = RubikObjectList.getNumObjects();
79 9c39179e Leszek Koltunski
    mScreenWidth = act.getScreenWidthInPixels();
80 cc8ab8e3 Leszek Koltunski
    int screenHeight= act.getScreenHeightInPixels();
81
    int upperBarHeight = act.getHeightUpperBar();
82
    int lowerBarHeight = act.getHeightLowerBar();
83 e3c74c0f Leszek Koltunski
84 d433b50e Leszek Koltunski
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
85 287e91a6 Leszek Koltunski
    mColCount = NUM_COLUMNS;
86 e07c48a2 Leszek Koltunski
87 85038b84 Leszek Koltunski
    float titleSize = mScreenWidth*RubikActivity.TITLE_TEXT_SIZE;
88
    LayoutInflater inflater = act.getLayoutInflater();
89
90 211b48f2 Leszek Koltunski
    // TOP ////////////////////////////
91 7289fd6c Leszek Koltunski
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
92 211b48f2 Leszek Koltunski
    layoutTop.removeAllViews();
93 85038b84 Leszek Koltunski
    TextView label = (TextView)inflater.inflate(R.layout.upper_text, null);
94
    label.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
95 876803e6 Leszek Koltunski
    label.setText(R.string.app_name);
96 85038b84 Leszek Koltunski
    layoutTop.addView(label);
97
98
    // BOTTOM /////////////////////////
99
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
100
    layoutBot.removeAllViews();
101
102 113d9d67 Leszek Koltunski
    float D = 0.20f;
103
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams( (int)(mScreenWidth*     D ),LinearLayout.LayoutParams.MATCH_PARENT);
104
    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams( (int)(mScreenWidth*(1-2*D)),LinearLayout.LayoutParams.MATCH_PARENT);
105 85038b84 Leszek Koltunski
106 113d9d67 Leszek Koltunski
    LinearLayout layoutLeft = new LinearLayout(act);
107
108
    layoutLeft.setLayoutParams(params1);
109
    RelativeLayout layoutMid = (RelativeLayout)inflater.inflate(R.layout.play_bottom_bar, null);
110
    layoutMid.setLayoutParams(params2);
111
    LinearLayout layoutRight = new LinearLayout(act);
112
    layoutRight.setLayoutParams(params1);
113
114
    setupAboutButton(act);
115
    setupUpdatesButtonAndBubble(act,layoutMid);
116
    setupExitButton(act);
117
118
    layoutLeft.addView(mAboutButton);
119
    layoutRight.addView(mExitButton);
120
121
    layoutBot.addView(layoutLeft);
122
    layoutBot.addView(layoutMid);
123
    layoutBot.addView(layoutRight);
124 85038b84 Leszek Koltunski
125
    // POPUP //////////////////////////
126
    LinearLayout hiddenView = act.findViewById(R.id.hiddenView);
127
    hiddenView.removeAllViews();
128 85b09df4 Leszek Koltunski
129 85038b84 Leszek Koltunski
    ViewGroup.LayoutParams paramsMid = hiddenView.getLayoutParams();
130 cc8ab8e3 Leszek Koltunski
    paramsMid.height = (int)(screenHeight-upperBarHeight-lowerBarHeight);
131 85038b84 Leszek Koltunski
    hiddenView.setLayoutParams(paramsMid);
132 0254cfd7 Leszek Koltunski
133 85038b84 Leszek Koltunski
    ScrollView scroll = (ScrollView)inflater.inflate(R.layout.popup_object_simple, null);
134
    hiddenView.addView(scroll);
135 211b48f2 Leszek Koltunski
136 cc8ab8e3 Leszek Koltunski
    setupScrollingObjects(act,scroll,mScreenWidth);
137 769d7b9f Leszek Koltunski
    }
138 211b48f2 Leszek Koltunski
139 8ab435b9 Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////////
140 4c0cd600 Leszek Koltunski
141 113d9d67 Leszek Koltunski
  private void setupAboutButton(final RubikActivity act)
142 85038b84 Leszek Koltunski
    {
143
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
144 113d9d67 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
145
    mAboutButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
146 85038b84 Leszek Koltunski
147
    mAboutButton.setOnClickListener( new View.OnClickListener()
148
      {
149
      @Override
150
      public void onClick(View v)
151
        {
152
        RubikDialogAbout aDiag = new RubikDialogAbout();
153
        aDiag.show(act.getSupportFragmentManager(), null);
154
        }
155
      });
156
    }
157
158
//////////////////////////////////////////////////////////////////////////////////////////////////
159
160 113d9d67 Leszek Koltunski
  private void setupExitButton(final RubikActivity act)
161 85038b84 Leszek Koltunski
    {
162 113d9d67 Leszek Koltunski
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_exit,R.drawable.ui_medium_exit, R.drawable.ui_big_exit, R.drawable.ui_huge_exit);
163
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
164
    mExitButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
165 85038b84 Leszek Koltunski
166 113d9d67 Leszek Koltunski
    mExitButton.setOnClickListener( new View.OnClickListener()
167 85038b84 Leszek Koltunski
      {
168
      @Override
169
      public void onClick(View v)
170
        {
171 113d9d67 Leszek Koltunski
        act.finish();
172 85038b84 Leszek Koltunski
        }
173
      });
174
    }
175
176
//////////////////////////////////////////////////////////////////////////////////////////////////
177
178 113d9d67 Leszek Koltunski
  private void setupUpdatesButtonAndBubble(final RubikActivity act, RelativeLayout relLayout)
179 85038b84 Leszek Koltunski
    {
180 113d9d67 Leszek Koltunski
    TypedValue value = new TypedValue();
181
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, value, true);
182
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
183 cc8ab8e3 Leszek Koltunski
    ImageButton updatesButton = relLayout.findViewById(R.id.buttonUpdates);
184
    updatesButton.setImageResource(icon);
185
    updatesButton.setBackgroundResource(value.resourceId);
186 85038b84 Leszek Koltunski
187 cc8ab8e3 Leszek Koltunski
    updatesButton.setOnClickListener( new View.OnClickListener()
188 85038b84 Leszek Koltunski
      {
189
      @Override
190
      public void onClick(View v)
191
        {
192 113d9d67 Leszek Koltunski
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
193
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
194 85038b84 Leszek Koltunski
        }
195
      });
196 113d9d67 Leszek Koltunski
197
    mBubbleUpdates = relLayout.findViewById(R.id.bubbleUpdates);
198
    mBubbleUpdates.setVisibility(View.INVISIBLE);
199
200
    RubikNetwork network = RubikNetwork.getInstance();
201
    network.signUpForUpdates(this);
202 85038b84 Leszek Koltunski
    }
203
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
206 cc8ab8e3 Leszek Koltunski
  private void setupScrollingObjects(final RubikActivity act, final ScrollView view, final float width)
207 85038b84 Leszek Koltunski
    {
208 cc8ab8e3 Leszek Koltunski
    int margin   = (int)(width*RubikActivity.LARGE_MARGIN);
209
    int cubeSize = (int)(width/NUM_COLUMNS) - 2*margin;
210 85038b84 Leszek Koltunski
211
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
212
213
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
214
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
215
216
    objectGrid.setColumnCount(mColCount);
217
    objectGrid.setRowCount(mRowCount);
218
219
    int[] nextInRow = new int[mRowCount];
220
221
    for(int row=0; row<mRowCount; row++)
222
      {
223
      rowSpecs[row] = GridLayout.spec(row);
224
      nextInRow[row]= 0;
225
      }
226
    for(int col=0; col<mColCount; col++)
227
      {
228
      colSpecs[col] = GridLayout.spec(col);
229
      }
230
231
    int numObjects = RubikObjectList.getNumObjects();
232
233
    for(int object=0; object<numObjects; object++)
234
      {
235
      final int ordinal = object;
236
      RubikObject rObject = RubikObjectList.getObject(ordinal);
237
      int row = object/NUM_COLUMNS;
238
      ImageButton button = new ImageButton(act);
239
      if( rObject!=null ) rObject.setIconTo(act,button);
240
241
      button.setOnClickListener( new View.OnClickListener()
242
        {
243
        @Override
244
        public void onClick(View v)
245
          {
246
          // TODO
247
          android.util.Log.e("D", "object "+ordinal+" clicked");
248
          }
249
        });
250
251
      GridLayout.Spec rowS = rowSpecs[row];
252
      GridLayout.Spec colS = colSpecs[nextInRow[row]];
253
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowS,colS);
254
      params.bottomMargin = margin;
255
      params.topMargin    = margin;
256
      params.leftMargin   = margin;
257
      params.rightMargin  = margin;
258
259 cc8ab8e3 Leszek Koltunski
      params.width = cubeSize;
260
      params.height= cubeSize;
261 6e3fcb91 Leszek Koltunski
262 7ac0ee88 Leszek Koltunski
      nextInRow[row]++;
263
264
      objectGrid.addView(button, params);
265 769d7b9f Leszek Koltunski
      }
266 4c0cd600 Leszek Koltunski
    }
267
268 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
269
270
  public void savePreferences(SharedPreferences.Editor editor)
271
    {
272 d38a302b Leszek Koltunski
    editor.putInt("play_LevelValue", mLevelValue );
273 211b48f2 Leszek Koltunski
    }
274
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276
277
  public void restorePreferences(SharedPreferences preferences)
278
    {
279 d38a302b Leszek Koltunski
    mLevelValue = preferences.getInt("play_LevelValue", 0);
280 211b48f2 Leszek Koltunski
    }
281
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283
284 dca3888a Leszek Koltunski
  public int getLevel()
285 211b48f2 Leszek Koltunski
    {
286 85b09df4 Leszek Koltunski
    return mLevelValue;
287 211b48f2 Leszek Koltunski
    }
288
289 84d746d7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
290
291 cc8ab8e3 Leszek Koltunski
  public void recreateListOfObjects()
292 84d746d7 Leszek Koltunski
    {
293 f3f3cb09 Leszek Koltunski
    int numObjects = RubikObjectList.getNumObjects();
294
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
295
    mColCount = NUM_COLUMNS;
296 85038b84 Leszek Koltunski
297
    RubikActivity act = mWeakAct.get();
298
    LayoutInflater inflater = act.getLayoutInflater();
299
300
    act.runOnUiThread(new Runnable()
301
      {
302
      @Override
303
      public void run()
304
        {
305
        LinearLayout hiddenView = act.findViewById(R.id.hiddenView);
306
        hiddenView.removeAllViews();
307
        ScrollView scroll = (ScrollView)inflater.inflate(R.layout.popup_object_simple, null);
308
        hiddenView.addView(scroll);
309 cc8ab8e3 Leszek Koltunski
        setupScrollingObjects(act,scroll,mScreenWidth);
310 85038b84 Leszek Koltunski
311
        mCurrentBubbleNumber--;
312
        updateNumberOfNewObjects();
313
        }
314
      });
315 84d746d7 Leszek Koltunski
    }
316
317 85038b84 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
318
319
  private void updateNumberOfNewObjects()
320
    {
321
    if( mCurrentBubbleNumber>0 )
322
      {
323
      String shownNum = String.valueOf(mCurrentBubbleNumber);
324
      mBubbleUpdates.setText(shownNum);
325
      mBubbleUpdates.setVisibility(View.VISIBLE);
326
      int height = (int)(0.05f*mScreenWidth);
327
      mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
328
      }
329
    else
330
      {
331
      mBubbleUpdates.setVisibility(View.INVISIBLE);
332
      }
333
    }
334
335 63dd19c4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
336
337
  public void receiveUpdate(RubikUpdates updates)
338
    {
339 179f7189 Leszek Koltunski
    Activity act = mWeakAct.get();
340 10373dc7 Leszek Koltunski
341 179f7189 Leszek Koltunski
    if( act!=null )
342 903c7bbc Leszek Koltunski
      {
343 85038b84 Leszek Koltunski
      mCurrentBubbleNumber = updates.getCompletedNumber();
344
345 179f7189 Leszek Koltunski
      act.runOnUiThread(new Runnable()
346
        {
347
        @Override
348
        public void run()
349
          {
350 85038b84 Leszek Koltunski
          updateNumberOfNewObjects();
351 179f7189 Leszek Koltunski
          }
352
        });
353 903c7bbc Leszek Koltunski
      }
354 63dd19c4 Leszek Koltunski
    }
355
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357
358
  public void errorUpdate()
359
    {
360 c99db493 Leszek Koltunski
    android.util.Log.e("D", "Screen: Error receiving update");
361 63dd19c4 Leszek Koltunski
    }
362 211b48f2 Leszek Koltunski
  }