Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 453ee65a

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 4c0cd600 Leszek Koltunski
import android.content.Context;
26 211b48f2 Leszek Koltunski
import android.content.SharedPreferences;
27 b600ccd9 Leszek Koltunski
import android.content.res.Resources;
28 4c0cd600 Leszek Koltunski
import android.graphics.drawable.BitmapDrawable;
29 255492a0 Leszek Koltunski
import android.os.Build;
30 e03e0352 Leszek Koltunski
import android.os.Bundle;
31 e3c74c0f Leszek Koltunski
import android.util.TypedValue;
32 255492a0 Leszek Koltunski
import android.view.Gravity;
33 211b48f2 Leszek Koltunski
import android.view.LayoutInflater;
34
import android.view.View;
35
import android.widget.Button;
36 e07c48a2 Leszek Koltunski
import android.widget.GridLayout;
37 211b48f2 Leszek Koltunski
import android.widget.ImageButton;
38
import android.widget.LinearLayout;
39 4c0cd600 Leszek Koltunski
import android.widget.PopupWindow;
40 d6e7c7fb Leszek Koltunski
import android.widget.RelativeLayout;
41 10373dc7 Leszek Koltunski
import android.widget.TextView;
42 211b48f2 Leszek Koltunski
43 5f3801d3 Leszek Koltunski
import org.distorted.dialogs.RubikDialogUpdates;
44 acabdd83 Leszek Koltunski
import org.distorted.external.RubikNetwork;
45
import org.distorted.external.RubikUpdates;
46 2afc6754 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
47 3f7a4363 Leszek Koltunski
48
import org.distorted.main.R;
49
import org.distorted.main.RubikActivity;
50 e03e0352 Leszek Koltunski
import org.distorted.dialogs.RubikDialogAbout;
51 a8576d91 Leszek Koltunski
import org.distorted.dialogs.RubikDialogPattern;
52 e03e0352 Leszek Koltunski
import org.distorted.dialogs.RubikDialogScores;
53 234a7582 Leszek Koltunski
import org.distorted.dialogs.RubikDialogTutorial;
54 55e6be1d Leszek Koltunski
import org.distorted.helpers.TransparentButton;
55
import org.distorted.helpers.TransparentImageButton;
56 acabdd83 Leszek Koltunski
import org.distorted.external.RubikScores;
57 d433b50e Leszek Koltunski
import org.distorted.objects.RubikObject;
58
import org.distorted.objects.RubikObjectList;
59 211b48f2 Leszek Koltunski
60 b600ccd9 Leszek Koltunski
import static android.view.View.inflate;
61 88d28110 Leszek Koltunski
62 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64 63dd19c4 Leszek Koltunski
public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Updatee
65 211b48f2 Leszek Koltunski
  {
66 6e3fcb91 Leszek Koltunski
  public static final int NUM_COLUMNS  = 5;
67 adbd16d9 Leszek Koltunski
  public static final int LEVELS_SHOWN = 8;
68 211b48f2 Leszek Koltunski
69 5d5ed376 Leszek Koltunski
  private static final int[] BUTTON_LABELS = { R.string.scores,
70
                                               R.string.patterns,
71
                                               R.string.solver,
72
                                               R.string.tutorials,
73
                                               R.string.bandaged,
74
                                               R.string.about };
75
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
76 d6c79ac9 Leszek Koltunski
77 adbd16d9 Leszek Koltunski
  private static final float BIG_BUTTON = 1.5f;
78 dc78f395 Leszek Koltunski
  private static final int[] mLocation = new int[2];
79 e03e0352 Leszek Koltunski
80 453ee65a Leszek Koltunski
  private TransparentImageButton mObjButton, mMenuButton, mExitButton;
81 033100af Leszek Koltunski
  private TransparentButton mPlayButton;
82 0254cfd7 Leszek Koltunski
  private PopupWindow mObjectPopup, mMenuPopup, mPlayPopup;
83 400ff34d Leszek Koltunski
  private LinearLayout mPlayLayout;
84 10373dc7 Leszek Koltunski
  private TextView mBubbleUpdates;
85 0254cfd7 Leszek Koltunski
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight, mPlayLayoutWidth;
86 85b09df4 Leszek Koltunski
  private int mLevelValue;
87 e07c48a2 Leszek Koltunski
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
88 598de3ee Leszek Koltunski
  private int mColCount, mRowCount, mMaxRowCount;
89 255492a0 Leszek Koltunski
  private int mUpperBarHeight;
90 dd874ae8 Leszek Koltunski
  private boolean mShouldReactToEndOfScrambling;
91 6e3fcb91 Leszek Koltunski
  private int mBottomHeight;
92 9c39179e Leszek Koltunski
  private float mScreenWidth;
93 179f7189 Leszek Koltunski
  private WeakReference<RubikActivity> mWeakAct;
94 255492a0 Leszek Koltunski
95 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
96
97 f5da732a Leszek Koltunski
  void leaveScreen(RubikActivity act)
98 211b48f2 Leszek Koltunski
    {
99 85b09df4 Leszek Koltunski
100 211b48f2 Leszek Koltunski
    }
101
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104 f5da732a Leszek Koltunski
  void enterScreen(final RubikActivity act)
105 211b48f2 Leszek Koltunski
    {
106 179f7189 Leszek Koltunski
    mWeakAct = new WeakReference<>(act);
107 d433b50e Leszek Koltunski
    int numObjects = RubikObjectList.getNumObjects();
108 9c39179e Leszek Koltunski
    mScreenWidth = act.getScreenWidthInPixels();
109 255492a0 Leszek Koltunski
    mUpperBarHeight = act.getHeightUpperBar();
110 a8576d91 Leszek Koltunski
111 9c39179e Leszek Koltunski
    mMenuTextSize = mScreenWidth*RubikActivity.MENU_MED_TEXT_SIZE;
112
    mButtonSize   = mScreenWidth*RubikActivity.BUTTON_TEXT_SIZE;
113
    mMenuItemSize = mScreenWidth*RubikActivity.MENU_ITEM_SIZE;
114 e3c74c0f Leszek Koltunski
115 d433b50e Leszek Koltunski
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
116 287e91a6 Leszek Koltunski
    mColCount = NUM_COLUMNS;
117 e07c48a2 Leszek Koltunski
118 211b48f2 Leszek Koltunski
    // TOP ////////////////////////////
119 7289fd6c Leszek Koltunski
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
120 211b48f2 Leszek Koltunski
    layoutTop.removeAllViews();
121 85b09df4 Leszek Koltunski
122 9c39179e Leszek Koltunski
    setupObjectButton(act,mScreenWidth);
123 85b09df4 Leszek Koltunski
    layoutTop.addView(mObjButton);
124 0254cfd7 Leszek Koltunski
125 9c39179e Leszek Koltunski
    setupMenuButton(act,mScreenWidth);
126 0254cfd7 Leszek Koltunski
    layoutTop.addView(mMenuButton);
127
128 9c39179e Leszek Koltunski
    setupPlayButton(act,mScreenWidth);
129 85b09df4 Leszek Koltunski
    layoutTop.addView(mPlayButton);
130 211b48f2 Leszek Koltunski
131 453ee65a Leszek Koltunski
    setupExitButton(act);
132
    createBottomPane(act,mExitButton,null);
133 769d7b9f Leszek Koltunski
    }
134 211b48f2 Leszek Koltunski
135 8ab435b9 Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////////
136 4c0cd600 Leszek Koltunski
137 ad0c8e0e Leszek Koltunski
  private void setupObjectButton(final RubikActivity act, final float width)
138 769d7b9f Leszek Koltunski
    {
139 c651024f Leszek Koltunski
    final int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
140 6e3fcb91 Leszek Koltunski
    final int lMargin = (int)(width*RubikActivity.LARGE_MARGIN);
141 e07c48a2 Leszek Koltunski
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_menu,R.drawable.ui_medium_cube_menu, R.drawable.ui_big_cube_menu, R.drawable.ui_huge_cube_menu);
142 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
143
    mObjButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
144 769d7b9f Leszek Koltunski
145
    mObjButton.setOnClickListener( new View.OnClickListener()
146
      {
147
      @Override
148
      public void onClick(View view)
149 4c0cd600 Leszek Koltunski
        {
150 2da68298 Leszek Koltunski
        if( mObjectPopup==null )
151
          {
152
          float width = act.getScreenWidthInPixels();
153
          float height= act.getScreenHeightInPixels();
154
          setupObjectWindow(act,width,height);
155
          }
156
157
        if( act.getControl().isUINotBlocked())
158 a42e25a6 Leszek Koltunski
          {
159 598de3ee Leszek Koltunski
          int rowCount = Math.min(mMaxRowCount,mRowCount);
160 c5b4af4a Leszek Koltunski
          View popupView = mObjectPopup.getContentView();
161
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
162 1071fb69 Leszek Koltunski
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+mBottomHeight+2*lMargin+5*margin,margin,margin);
163 c5b4af4a Leszek Koltunski
          }
164 769d7b9f Leszek Koltunski
        }
165
      });
166
    }
167 4c0cd600 Leszek Koltunski
168 85b09df4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
169
170 2da68298 Leszek Koltunski
  private void setupPlayButton(final RubikActivity act, final float width)
171 85b09df4 Leszek Koltunski
    {
172 c651024f Leszek Koltunski
    final int margin = (int)(width*RubikActivity.SMALL_MARGIN);
173 d90c55cc Leszek Koltunski
174 033100af Leszek Koltunski
    mPlayButton = new TransparentButton(act, R.string.play, mButtonSize);
175 85b09df4 Leszek Koltunski
176
    mPlayButton.setOnClickListener( new View.OnClickListener()
177
      {
178
      @Override
179 0254cfd7 Leszek Koltunski
      public void onClick(View view)
180 85b09df4 Leszek Koltunski
        {
181 7e4f3e04 Leszek Koltunski
        if( mPlayPopup==null )
182 2da68298 Leszek Koltunski
          {
183
          float width = act.getScreenWidthInPixels();
184
          setupPlayWindow(act,width);
185
          }
186
187
        if( act.getControl().isUINotBlocked())
188 6e194411 Leszek Koltunski
          {
189 d38a302b Leszek Koltunski
          adjustSolvedIcons();
190 2da68298 Leszek Koltunski
          float height= act.getScreenHeightInPixels();
191
          final int maxHeight= (int)(0.9f*(height-mUpperBarHeight) );
192 c5b4af4a Leszek Koltunski
          View popupView = mPlayPopup.getContentView();
193
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
194 400ff34d Leszek Koltunski
          final int object  = RubikObjectList.getCurrObject();
195
          final int dbLevel = RubikObjectList.getDBLevel(object);
196 adbd16d9 Leszek Koltunski
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN)+1;
197
          final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+4*margin+2*mMenuItemSize*(BIG_BUTTON-1.0f));
198 255492a0 Leszek Koltunski
          final int realHeight = Math.min(popupHeight,maxHeight);
199
          displayPopup(act,view,mPlayPopup,mPlayLayoutWidth,realHeight,margin,margin);
200 6fad862b Leszek Koltunski
          }
201 85b09df4 Leszek Koltunski
        }
202
      });
203
    }
204
205 e31abc1e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
206
207 ad0c8e0e Leszek Koltunski
  private void setupMenuButton(final RubikActivity act, final float width)
208 e31abc1e Leszek Koltunski
    {
209 c651024f Leszek Koltunski
    final int margin = (int)(width*RubikActivity.SMALL_MARGIN);
210 0254cfd7 Leszek Koltunski
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_menu,R.drawable.ui_medium_menu, R.drawable.ui_big_menu, R.drawable.ui_huge_menu);
211 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
212
    mMenuButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
213 e31abc1e Leszek Koltunski
214 e03e0352 Leszek Koltunski
    mMenuButton.setOnClickListener( new View.OnClickListener()
215 e31abc1e Leszek Koltunski
      {
216
      @Override
217
      public void onClick(View view)
218
        {
219 2da68298 Leszek Koltunski
        if( mMenuPopup==null )
220
          {
221
          float width = act.getScreenWidthInPixels();
222
          setupMenuWindow(act,width);
223
          }
224
225
        if( act.getControl().isUINotBlocked())
226 e03e0352 Leszek Koltunski
          {
227 c5b4af4a Leszek Koltunski
          View popupView = mMenuPopup.getContentView();
228
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
229 255492a0 Leszek Koltunski
          displayPopup(act,view,mMenuPopup,mMenuLayoutWidth,mMenuLayoutHeight,(int)(-width/12),margin);
230 6fad862b Leszek Koltunski
          }
231 e31abc1e Leszek Koltunski
        }
232
      });
233
    }
234
235 4c0cd600 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
236
237 598de3ee Leszek Koltunski
  private void setupObjectWindow(final RubikActivity act, final float width, final float height)
238 4c0cd600 Leszek Koltunski
    {
239 adbd16d9 Leszek Koltunski
    int cubeSize = (int)(width/9);
240
    int margin   = (int)(width*RubikActivity.LARGE_MARGIN);
241
    mObjectSize  = (int)(cubeSize + 2*margin + 0.5f);
242 0b162619 Leszek Koltunski
    mMaxRowCount = (int)((height-1.8f*mUpperBarHeight)/mObjectSize);
243 598de3ee Leszek Koltunski
244 8d1da3f1 Leszek Koltunski
    LinearLayout view = (LinearLayout)inflate( act, R.layout.popup_object, null);
245 b600ccd9 Leszek Koltunski
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
246 e07c48a2 Leszek Koltunski
247
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
248
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
249
250 92843d3b Leszek Koltunski
    objectGrid.setColumnCount(mColCount);
251
    objectGrid.setRowCount(mRowCount);
252 e07c48a2 Leszek Koltunski
253 d6e7c7fb Leszek Koltunski
    RelativeLayout bottomLayout = view.findViewById(R.id.bottomLayout);
254
    setupBottomLayout(act,bottomLayout);
255 0b162619 Leszek Koltunski
256 b600ccd9 Leszek Koltunski
    mObjectPopup = new PopupWindow(act);
257
    mObjectPopup.setFocusable(true);
258
    mObjectPopup.setContentView(view);
259
260 0501a4b8 Leszek Koltunski
    int[] nextInRow = new int[mRowCount];
261 fa679111 Leszek Koltunski
262 e07c48a2 Leszek Koltunski
    for(int row=0; row<mRowCount; row++)
263
      {
264
      rowSpecs[row] = GridLayout.spec(row);
265 0501a4b8 Leszek Koltunski
      nextInRow[row]= 0;
266 e07c48a2 Leszek Koltunski
      }
267
    for(int col=0; col<mColCount; col++)
268
      {
269
      colSpecs[col] = GridLayout.spec(col);
270
      }
271 769d7b9f Leszek Koltunski
272 d433b50e Leszek Koltunski
    int numObjects = RubikObjectList.getNumObjects();
273
274
    for(int object=0; object<numObjects; object++)
275 769d7b9f Leszek Koltunski
      {
276 314e9ff0 Leszek Koltunski
      final int ordinal = object;
277
      final RubikObject rObject = RubikObjectList.getObject(object);
278 287e91a6 Leszek Koltunski
      int row = object/NUM_COLUMNS;
279 7ac0ee88 Leszek Koltunski
      ImageButton button = new ImageButton(act);
280 eb985085 Leszek Koltunski
      if( rObject!=null ) rObject.setIconTo(act,button);
281 314e9ff0 Leszek Koltunski
282 7ac0ee88 Leszek Koltunski
      button.setOnClickListener( new View.OnClickListener()
283 769d7b9f Leszek Koltunski
        {
284 7ac0ee88 Leszek Koltunski
        @Override
285
        public void onClick(View v)
286 769d7b9f Leszek Koltunski
          {
287 2afc6754 Leszek Koltunski
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
288 769d7b9f Leszek Koltunski
            {
289 400ff34d Leszek Koltunski
            RubikObjectList.setCurrObject(act,ordinal);
290
            act.changeObject(ordinal,true);
291 4c9947bd Leszek Koltunski
            if( mPlayLayout!=null ) adjustLevels(act);
292 dd1a65c1 Leszek Koltunski
            mMovesController.clearMoves(act);
293 769d7b9f Leszek Koltunski
            }
294
295 1f3bc259 Leszek Koltunski
          if( mObjectPopup!=null ) mObjectPopup.dismiss();
296 7ac0ee88 Leszek Koltunski
          }
297
        });
298 e07c48a2 Leszek Koltunski
299 7ac0ee88 Leszek Koltunski
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
300
      params.bottomMargin = margin;
301
      params.topMargin    = margin;
302
      params.leftMargin   = margin;
303
      params.rightMargin  = margin;
304 fa679111 Leszek Koltunski
305 adbd16d9 Leszek Koltunski
      params.width = cubeSize;
306
      params.height= cubeSize;
307 6e3fcb91 Leszek Koltunski
308 7ac0ee88 Leszek Koltunski
      nextInRow[row]++;
309
310
      objectGrid.addView(button, params);
311 769d7b9f Leszek Koltunski
      }
312 4c0cd600 Leszek Koltunski
    }
313
314 8d1da3f1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
315
316 d6e7c7fb Leszek Koltunski
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
317 8d1da3f1 Leszek Koltunski
    {
318
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
319 d6e7c7fb Leszek Koltunski
    int iconD = RubikActivity.getDrawable(R.drawable.ui_small_download,R.drawable.ui_medium_download, R.drawable.ui_big_download, R.drawable.ui_huge_download);
320
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
321 8d1da3f1 Leszek Koltunski
322
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
323 d6e7c7fb Leszek Koltunski
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
324
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
325 8d1da3f1 Leszek Koltunski
326
    buttonTut.setImageResource(iconT);
327 d6e7c7fb Leszek Koltunski
    buttonDow.setImageResource(iconD);
328
    buttonInf.setImageResource(iconI);
329 8d1da3f1 Leszek Koltunski
330 6e3fcb91 Leszek Koltunski
    Resources res = act.getResources();
331 d6e7c7fb Leszek Koltunski
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(iconI);
332 6e3fcb91 Leszek Koltunski
    mBottomHeight = bd.getIntrinsicHeight();
333
334 8d1da3f1 Leszek Koltunski
    TypedValue outValue = new TypedValue();
335
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
336
    buttonTut.setBackgroundResource(outValue.resourceId);
337 d6e7c7fb Leszek Koltunski
    buttonDow.setBackgroundResource(outValue.resourceId);
338
    buttonInf.setBackgroundResource(outValue.resourceId);
339 8d1da3f1 Leszek Koltunski
340
    buttonTut.setOnClickListener( new View.OnClickListener()
341
      {
342
      @Override
343
      public void onClick(View v)
344
        {
345
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
346
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
347
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
348
        }
349
      });
350
351 d6e7c7fb Leszek Koltunski
    buttonDow.setOnClickListener( new View.OnClickListener()
352
      {
353
      @Override
354
      public void onClick(View v)
355
        {
356 5f3801d3 Leszek Koltunski
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
357
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
358
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
359 d6e7c7fb Leszek Koltunski
        }
360
      });
361
362
    buttonInf.setOnClickListener( new View.OnClickListener()
363 8d1da3f1 Leszek Koltunski
      {
364
      @Override
365
      public void onClick(View v)
366
        {
367 f12e4de9 Leszek Koltunski
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
368 400ff34d Leszek Koltunski
        int currObject = RubikObjectList.getCurrObject();
369
        act.switchConfig(currObject);
370 8d1da3f1 Leszek Koltunski
        }
371
      });
372 d6e7c7fb Leszek Koltunski
373 10373dc7 Leszek Koltunski
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
374
    mBubbleUpdates.setVisibility(View.INVISIBLE);
375
376 d6e7c7fb Leszek Koltunski
    RubikNetwork network = RubikNetwork.getInstance();
377
    network.signUpForUpdates(this);
378 8d1da3f1 Leszek Koltunski
    }
379
380 e03e0352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
381
382 ad0c8e0e Leszek Koltunski
  private void setupMenuWindow(final RubikActivity act, final float width)
383 e03e0352 Leszek Koltunski
    {
384
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
385 e07c48a2 Leszek Koltunski
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
386
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
387 e03e0352 Leszek Koltunski
388
    mMenuPopup = new PopupWindow(act);
389
    mMenuPopup.setContentView(layout);
390
    mMenuPopup.setFocusable(true);
391 c651024f Leszek Koltunski
    int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
392 ad0c8e0e Leszek Koltunski
    int padding = (int)(width*RubikActivity.PADDING);
393 e03e0352 Leszek Koltunski
394 0254cfd7 Leszek Koltunski
    mMenuLayoutWidth = (int)(width/2);
395 5d5ed376 Leszek Koltunski
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
396 0254cfd7 Leszek Koltunski
397 43162dfb Leszek Koltunski
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
398 e03e0352 Leszek Koltunski
399 5d5ed376 Leszek Koltunski
    for(int i=0; i<NUM_BUTTONS; i++)
400 e03e0352 Leszek Koltunski
      {
401
      final int but = i;
402
      Button button = new Button(act);
403 43162dfb Leszek Koltunski
      button.setLayoutParams(p);
404 5d5ed376 Leszek Koltunski
      button.setText(BUTTON_LABELS[i]);
405 88fb92ba Leszek Koltunski
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
406 e03e0352 Leszek Koltunski
407
      button.setOnClickListener( new View.OnClickListener()
408
        {
409
        @Override
410
        public void onClick(View v)
411
          {
412
          mMenuPopup.dismiss();
413 0254cfd7 Leszek Koltunski
          MenuAction(act,but);
414 e03e0352 Leszek Koltunski
          }
415
        });
416
417 e07c48a2 Leszek Koltunski
      menuLayout.addView(button);
418 e03e0352 Leszek Koltunski
      }
419 0254cfd7 Leszek Koltunski
    }
420 e03e0352 Leszek Koltunski
421 0254cfd7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
422
423
  private void setupPlayWindow(final RubikActivity act, final float width)
424
    {
425
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
426
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
427
    mPlayLayout = layout.findViewById(R.id.playGrid);
428
429
    mPlayLayoutWidth = (int)(width*0.4f);
430
431
    mPlayPopup = new PopupWindow(act);
432
    mPlayPopup.setContentView(layout);
433
    mPlayPopup.setFocusable(true);
434
435
    adjustLevels(act);
436 e03e0352 Leszek Koltunski
    }
437
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439
440 0254cfd7 Leszek Koltunski
  private void MenuAction(RubikActivity act, int button)
441 e03e0352 Leszek Koltunski
    {
442
    switch(button)
443
      {
444 8ab435b9 Leszek Koltunski
      case 0: Bundle sBundle = new Bundle();
445 400ff34d Leszek Koltunski
              int currObject = RubikObjectList.getCurrObject();
446
              sBundle.putInt("tab", currObject );
447 a8576d91 Leszek Koltunski
              sBundle.putBoolean("submitting", false);
448 e03e0352 Leszek Koltunski
              RubikDialogScores scores = new RubikDialogScores();
449 a8576d91 Leszek Koltunski
              scores.setArguments(sBundle);
450 e03e0352 Leszek Koltunski
              scores.show(act.getSupportFragmentManager(), null);
451
              break;
452 234a7582 Leszek Koltunski
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
453
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
454 e03e0352 Leszek Koltunski
              break;
455 b2a92941 Leszek Koltunski
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
456 e03e0352 Leszek Koltunski
              break;
457 b2a92941 Leszek Koltunski
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
458 234a7582 Leszek Koltunski
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
459 2971588c Leszek Koltunski
              break;
460 5d5ed376 Leszek Koltunski
      case 4: act.switchToBandagedCreator();
461
              break;
462 9530f6b0 Leszek Koltunski
      case 5: RubikDialogAbout aDiag = new RubikDialogAbout();
463 a8576d91 Leszek Koltunski
              aDiag.show(act.getSupportFragmentManager(), null);
464 e03e0352 Leszek Koltunski
              break;
465
      }
466
    }
467
468 46405bb4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
469
470 453ee65a Leszek Koltunski
  void setupExitButton(final RubikActivity act)
471 46405bb4 Leszek Koltunski
    {
472 dd874ae8 Leszek Koltunski
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube_solve_new,R.drawable.ui_medium_cube_solve_new, R.drawable.ui_big_cube_solve_new, R.drawable.ui_huge_cube_solve_new);
473 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
474 453ee65a Leszek Koltunski
    mExitButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE,params);
475 46405bb4 Leszek Koltunski
476 453ee65a Leszek Koltunski
    mExitButton.setOnClickListener( new View.OnClickListener()
477 46405bb4 Leszek Koltunski
      {
478 a8576d91 Leszek Koltunski
      @Override
479
      public void onClick(View v)
480
        {
481 453ee65a Leszek Koltunski
        act.finish();
482 dd874ae8 Leszek Koltunski
        }
483
      });
484
    }
485
486 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
487 3398a606 Leszek Koltunski
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
488
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
489
// going to be called)
490
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
491 211b48f2 Leszek Koltunski
492
  public void savePreferences(SharedPreferences.Editor editor)
493
    {
494 d38a302b Leszek Koltunski
    editor.putInt("play_LevelValue", mLevelValue );
495
496 3398a606 Leszek Koltunski
    if( mObjectPopup!=null )
497
      {
498
      mObjectPopup.dismiss();
499
      mObjectPopup = null;
500
      }
501 87d86e5f Leszek Koltunski
502 3398a606 Leszek Koltunski
    if( mMenuPopup!=null )
503
      {
504
      mMenuPopup.dismiss();
505
      mMenuPopup = null;
506
      }
507
508
    if( mPlayPopup!=null )
509
      {
510
      mPlayPopup.dismiss();
511
      mPlayPopup = null;
512
      }
513 211b48f2 Leszek Koltunski
    }
514
515
///////////////////////////////////////////////////////////////////////////////////////////////////
516
517
  public void restorePreferences(SharedPreferences preferences)
518
    {
519 d38a302b Leszek Koltunski
    mLevelValue = preferences.getInt("play_LevelValue", 0);
520 211b48f2 Leszek Koltunski
    }
521
522 53f23b64 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
523
524 400ff34d Leszek Koltunski
  public void setCurrObject(RubikActivity act)
525 53f23b64 Leszek Koltunski
    {
526 400ff34d Leszek Koltunski
    if( mPlayLayout!=null ) adjustLevels(act);
527 7b7d65ce Leszek Koltunski
    }
528
529
///////////////////////////////////////////////////////////////////////////////////////////////////
530 255492a0 Leszek Koltunski
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
531
532
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
533
    {
534
    View topLayout = act.findViewById(R.id.relativeLayout);
535 7ac0ee88 Leszek Koltunski
    boolean isFullScreen;
536 00aa398a Leszek Koltunski
537
    if( topLayout!=null )
538
      {
539
      topLayout.getLocationOnScreen(mLocation);
540 7ac0ee88 Leszek Koltunski
      isFullScreen = (mLocation[1]==0);
541 00aa398a Leszek Koltunski
      }
542
    else
543
      {
544 7ac0ee88 Leszek Koltunski
      isFullScreen = true;
545 00aa398a Leszek Koltunski
      }
546 255492a0 Leszek Koltunski
547 69a271f3 Leszek Koltunski
    try
548 255492a0 Leszek Koltunski
      {
549 69a271f3 Leszek Koltunski
      // if on Android 11 or we are fullscreen
550
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
551
        {
552
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
553
        window.update(view, w, h);
554
        }
555
      else  // Android 10 or below in pop-up mode or split-screen mode
556
        {
557
        view.getLocationOnScreen(mLocation);
558
        int width  = view.getWidth();
559
        int height = view.getHeight();
560
        int x = mLocation[0]+(width-w)/2;
561
        int y = mLocation[1]+height+yoff;
562
563
        window.showAsDropDown(view);
564
        window.update(x,y,w,h);
565
        }
566 255492a0 Leszek Koltunski
      }
567 69a271f3 Leszek Koltunski
    catch( IllegalArgumentException iae )
568 255492a0 Leszek Koltunski
      {
569 69a271f3 Leszek Koltunski
      // ignore, this means window is 'not attached to window manager' -
570
      // which most probably is because we are already exiting the app.
571 255492a0 Leszek Koltunski
      }
572
    }
573
574 ae77f661 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
575
576 d38a302b Leszek Koltunski
  private void adjustSolvedIcons()
577 ae77f661 Leszek Koltunski
    {
578 59cc16ae Leszek Koltunski
    if( mPlayLayout!=null )
579 ae77f661 Leszek Koltunski
      {
580 59cc16ae Leszek Koltunski
      int currObject = RubikObjectList.getCurrObject();
581
      int dbLevel = RubikObjectList.getDBLevel(currObject);
582 adbd16d9 Leszek Koltunski
      int numLevel= Math.min(dbLevel, LEVELS_SHOWN)+1;
583 59cc16ae Leszek Koltunski
      RubikScores scores = RubikScores.getInstance();
584
585
      for(int i=0; i<numLevel; i++)
586
        {
587 adbd16d9 Leszek Koltunski
        int level = i<numLevel-1 ? i : dbLevel;
588 59cc16ae Leszek Koltunski
        Button button = (Button)mPlayLayout.getChildAt(i);
589
        int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
590
        button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
591
        }
592 ae77f661 Leszek Koltunski
      }
593
    }
594
595 255492a0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
596 7b7d65ce Leszek Koltunski
597 0254cfd7 Leszek Koltunski
  private void adjustLevels(final RubikActivity act)
598 7b7d65ce Leszek Koltunski
    {
599 400ff34d Leszek Koltunski
    int currObject = RubikObjectList.getCurrObject();
600
    int dbLevel = RubikObjectList.getDBLevel(currObject);
601
    RubikObject object = RubikObjectList.getObject(currObject);
602 d433b50e Leszek Koltunski
    int numScrambles = object==null ? 0 : object.getNumScramble();
603 adbd16d9 Leszek Koltunski
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN)+1;
604 00af5060 Leszek Koltunski
    String[] levels = new String[numLevel];
605 7b7d65ce Leszek Koltunski
606 adbd16d9 Leszek Koltunski
    levels[0] = act.getString(R.string.free_play);
607
    for(int i=1; i<numLevel-1; i++) levels[i] = act.getString(R.string.lv_placeholder,i);
608
    levels[numLevel-1] = act.getString(R.string.level_full);
609 00af5060 Leszek Koltunski
610 0a7aa15b Leszek Koltunski
    if( mLevelValue>dbLevel || mLevelValue<1 ||
611 adbd16d9 Leszek Koltunski
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) ) mLevelValue=1;
612 0254cfd7 Leszek Koltunski
613 de62316a Leszek Koltunski
    float width  = act.getScreenWidthInPixels();
614 c651024f Leszek Koltunski
    int margin   = (int)(width*RubikActivity.SMALL_MARGIN);
615 de62316a Leszek Koltunski
    int padding  = (int)(width*RubikActivity.PADDING);
616
    int butWidth = mPlayLayoutWidth - 2*padding;
617
    int butHeight= (int)mMenuItemSize;
618 adbd16d9 Leszek Koltunski
    int bigButH  = (int)(mMenuItemSize*BIG_BUTTON) ;
619 0254cfd7 Leszek Koltunski
620 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
621 0254cfd7 Leszek Koltunski
    pM.setMargins(margin, 0, margin, margin);
622 adbd16d9 Leszek Koltunski
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, bigButH   );
623 0254cfd7 Leszek Koltunski
    pB.setMargins(margin, margin, margin, 2*margin);
624
625
    mPlayLayout.removeAllViews();
626
627 00af5060 Leszek Koltunski
    for(int i=0; i<numLevel; i++)
628 011fcfe0 Leszek Koltunski
      {
629 adbd16d9 Leszek Koltunski
      final int level     = i<numLevel-1 ? i : dbLevel;
630
      final int scrambles = i<numLevel-1 ? i : numScrambles;
631 0254cfd7 Leszek Koltunski
      Button button = new Button(act);
632 adbd16d9 Leszek Koltunski
      button.setLayoutParams(i==0 ? pB : (i==numLevel-1 ? pB : pM));
633 0254cfd7 Leszek Koltunski
      button.setText(levels[i]);
634
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
635
636
      button.setOnClickListener( new View.OnClickListener()
637 011fcfe0 Leszek Koltunski
        {
638 0254cfd7 Leszek Koltunski
        @Override
639
        public void onClick(View v)
640
          {
641 2afc6754 Leszek Koltunski
          ObjectControl control = act.getControl();
642 6fad862b Leszek Koltunski
643 2afc6754 Leszek Koltunski
          if(control.isUINotBlocked())
644 6fad862b Leszek Koltunski
            {
645 598de3ee Leszek Koltunski
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
646 adbd16d9 Leszek Koltunski
647
            if( level>0 )
648
              {
649
              mLevelValue = level;
650
              mShouldReactToEndOfScrambling = true;
651
              control.scrambleObject(scrambles);
652
              }
653
             else
654
              {
655
              ScreenList.switchScreen(act, ScreenList.FREE);
656
              }
657 6fad862b Leszek Koltunski
            }
658 0254cfd7 Leszek Koltunski
          }
659
        });
660 011fcfe0 Leszek Koltunski
661 0254cfd7 Leszek Koltunski
      mPlayLayout.addView(button);
662
      }
663 53f23b64 Leszek Koltunski
    }
664
665 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
666
667 dca3888a Leszek Koltunski
  public int getLevel()
668 211b48f2 Leszek Koltunski
    {
669 85b09df4 Leszek Koltunski
    return mLevelValue;
670 211b48f2 Leszek Koltunski
    }
671
672 84d746d7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
673
674
  public void recreatePopup()
675
    {
676
    mObjectPopup = null;
677 f3f3cb09 Leszek Koltunski
678
    int numObjects = RubikObjectList.getNumObjects();
679
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
680
    mColCount = NUM_COLUMNS;
681 84d746d7 Leszek Koltunski
    }
682
683 dd874ae8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
684
685
  public boolean shouldReactToEndOfScrambling()
686
    {
687
    return mShouldReactToEndOfScrambling;
688
    }
689 63dd19c4 Leszek Koltunski
690
///////////////////////////////////////////////////////////////////////////////////////////////////
691
692
  public void receiveUpdate(RubikUpdates updates)
693
    {
694 179f7189 Leszek Koltunski
    Activity act = mWeakAct.get();
695 10373dc7 Leszek Koltunski
696 179f7189 Leszek Koltunski
    if( act!=null )
697 903c7bbc Leszek Koltunski
      {
698 179f7189 Leszek Koltunski
      act.runOnUiThread(new Runnable()
699
        {
700
        @Override
701
        public void run()
702
          {
703 2c9ab085 Leszek Koltunski
          int num = updates.getCompletedNumber();
704 179f7189 Leszek Koltunski
705
          if( num>0 )
706
            {
707
            String shownNum = String.valueOf(num);
708
            mBubbleUpdates.setText(shownNum);
709
            mBubbleUpdates.setVisibility(View.VISIBLE);
710
            int height = (int)(0.05f*mScreenWidth);
711
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
712
            }
713
         else
714
            {
715
            mBubbleUpdates.setVisibility(View.INVISIBLE);
716
            }
717
          }
718
        });
719 903c7bbc Leszek Koltunski
      }
720 63dd19c4 Leszek Koltunski
    }
721
722
///////////////////////////////////////////////////////////////////////////////////////////////////
723
724
  public void errorUpdate()
725
    {
726 c99db493 Leszek Koltunski
    android.util.Log.e("D", "Screen: Error receiving update");
727 63dd19c4 Leszek Koltunski
    }
728 211b48f2 Leszek Koltunski
  }