Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPlay.java @ 6b64f9f8

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