Project

General

Profile

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

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

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