Project

General

Profile

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

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

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 b88cdd91 Leszek Koltunski
import android.graphics.Bitmap;
29 4c0cd600 Leszek Koltunski
import android.graphics.drawable.BitmapDrawable;
30 255492a0 Leszek Koltunski
import android.os.Build;
31 e03e0352 Leszek Koltunski
import android.os.Bundle;
32 e3c74c0f Leszek Koltunski
import android.util.TypedValue;
33 255492a0 Leszek Koltunski
import android.view.Gravity;
34 211b48f2 Leszek Koltunski
import android.view.LayoutInflater;
35
import android.view.View;
36
import android.widget.Button;
37 e07c48a2 Leszek Koltunski
import android.widget.GridLayout;
38 211b48f2 Leszek Koltunski
import android.widget.ImageButton;
39
import android.widget.LinearLayout;
40 4c0cd600 Leszek Koltunski
import android.widget.PopupWindow;
41 d6e7c7fb Leszek Koltunski
import android.widget.RelativeLayout;
42 10373dc7 Leszek Koltunski
import android.widget.TextView;
43 211b48f2 Leszek Koltunski
44 5f3801d3 Leszek Koltunski
import org.distorted.dialogs.RubikDialogUpdates;
45 63dd19c4 Leszek Koltunski
import org.distorted.network.RubikNetwork;
46
import org.distorted.network.RubikUpdates;
47 2afc6754 Leszek Koltunski
import org.distorted.objectlib.main.ObjectControl;
48 3f7a4363 Leszek Koltunski
49
import org.distorted.main.R;
50
import org.distorted.main.RubikActivity;
51 e03e0352 Leszek Koltunski
import org.distorted.dialogs.RubikDialogAbout;
52 a8576d91 Leszek Koltunski
import org.distorted.dialogs.RubikDialogPattern;
53 e03e0352 Leszek Koltunski
import org.distorted.dialogs.RubikDialogScores;
54 234a7582 Leszek Koltunski
import org.distorted.dialogs.RubikDialogTutorial;
55 55e6be1d Leszek Koltunski
import org.distorted.helpers.TransparentButton;
56
import org.distorted.helpers.TransparentImageButton;
57 6a083c6a Leszek Koltunski
import org.distorted.network.RubikScores;
58 d433b50e Leszek Koltunski
import org.distorted.objects.RubikObject;
59
import org.distorted.objects.RubikObjectList;
60 211b48f2 Leszek Koltunski
61 b600ccd9 Leszek Koltunski
import static android.view.View.inflate;
62 88d28110 Leszek Koltunski
63 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65 63dd19c4 Leszek Koltunski
public class RubikScreenPlay extends RubikScreenBase implements RubikNetwork.Updatee
66 211b48f2 Leszek Koltunski
  {
67 6e3fcb91 Leszek Koltunski
  public static final int NUM_COLUMNS  = 5;
68 00af5060 Leszek Koltunski
  public static final int LEVELS_SHOWN = 10;
69 211b48f2 Leszek Koltunski
70 7cf2637d Leszek Koltunski
  private static final int[] BUTTON_LABELS = { R.string.scores,
71
                                               R.string.patterns,
72
                                               R.string.solver,
73
                                               R.string.tutorials,
74
                                               R.string.about };
75
76 e03e0352 Leszek Koltunski
  private static final int NUM_BUTTONS = BUTTON_LABELS.length;
77 de62316a Leszek Koltunski
  private static final float LAST_BUTTON = 1.5f;
78 dc78f395 Leszek Koltunski
  private static final int[] mLocation = new int[2];
79 e03e0352 Leszek Koltunski
80 0873a75a Leszek Koltunski
  private TransparentImageButton mObjButton, mMenuButton, mSolveButton, mScrambleButton;
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 dd874ae8 Leszek Koltunski
    setupSolveButton(act);
132
    setupScrambleButton(act);
133
    createBottomPane(act,mSolveButton,mScrambleButton);
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 2da68298 Leszek Koltunski
         if( mPlayPopup==null )
183
          {
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 0a7aa15b Leszek Koltunski
          final int levelsShown = Math.min(dbLevel,LEVELS_SHOWN);
198 d90c55cc Leszek Koltunski
          final int popupHeight = (int)(levelsShown*(mMenuItemSize+margin)+3*margin+mMenuItemSize*(LAST_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 6e3fcb91 Leszek Koltunski
    int cubeWidth = (int)(width/9);
241 598de3ee Leszek Koltunski
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
242
    mObjectSize = (int)(cubeWidth + 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 d433b50e Leszek Koltunski
      final RubikObject robject = RubikObjectList.getObject(object);
278 6e3fcb91 Leszek Koltunski
      int icons = robject==null ? 0 : robject.getIconID();
279 287e91a6 Leszek Koltunski
      int row = object/NUM_COLUMNS;
280 400ff34d Leszek Koltunski
      final int ordinal = robject==null ? 0 : robject.getOrdinal();
281 769d7b9f Leszek Koltunski
282 7ac0ee88 Leszek Koltunski
      ImageButton button = new ImageButton(act);
283
      button.setBackgroundResource(icons);
284
      button.setOnClickListener( new View.OnClickListener()
285 769d7b9f Leszek Koltunski
        {
286 7ac0ee88 Leszek Koltunski
        @Override
287
        public void onClick(View v)
288 769d7b9f Leszek Koltunski
          {
289 2afc6754 Leszek Koltunski
          if( act.getControl().isUINotBlocked() && ScreenList.getCurrentScreen()== ScreenList.PLAY )
290 769d7b9f Leszek Koltunski
            {
291 400ff34d Leszek Koltunski
            RubikObjectList.setCurrObject(act,ordinal);
292
            act.changeObject(ordinal,true);
293 4c9947bd Leszek Koltunski
            if( mPlayLayout!=null ) adjustLevels(act);
294 dd1a65c1 Leszek Koltunski
            mMovesController.clearMoves(act);
295 769d7b9f Leszek Koltunski
            }
296
297 7ac0ee88 Leszek Koltunski
          mObjectPopup.dismiss();
298
          }
299
        });
300 e07c48a2 Leszek Koltunski
301 7ac0ee88 Leszek Koltunski
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
302
      params.bottomMargin = margin;
303
      params.topMargin    = margin;
304
      params.leftMargin   = margin;
305
      params.rightMargin  = margin;
306 fa679111 Leszek Koltunski
307 6e3fcb91 Leszek Koltunski
      params.width = cubeWidth;
308
      params.height= cubeWidth;
309
310 7ac0ee88 Leszek Koltunski
      nextInRow[row]++;
311
312
      objectGrid.addView(button, params);
313 769d7b9f Leszek Koltunski
      }
314 4c0cd600 Leszek Koltunski
    }
315
316 8d1da3f1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
317
318 d6e7c7fb Leszek Koltunski
  private void setupBottomLayout(final RubikActivity act, final RelativeLayout layout)
319 8d1da3f1 Leszek Koltunski
    {
320
    int iconT = RubikActivity.getDrawable(R.drawable.ui_small_tutorial,R.drawable.ui_medium_tutorial, R.drawable.ui_big_tutorial, R.drawable.ui_huge_tutorial);
321 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);
322
    int iconI = RubikActivity.getDrawable(R.drawable.ui_small_info,R.drawable.ui_medium_info, R.drawable.ui_big_info, R.drawable.ui_huge_info);
323 8d1da3f1 Leszek Koltunski
324
    ImageButton buttonTut = layout.findViewById(R.id.buttonTut);
325 d6e7c7fb Leszek Koltunski
    ImageButton buttonDow = layout.findViewById(R.id.buttonDow);
326
    ImageButton buttonInf = layout.findViewById(R.id.buttonInf);
327 8d1da3f1 Leszek Koltunski
328
    buttonTut.setImageResource(iconT);
329 d6e7c7fb Leszek Koltunski
    buttonDow.setImageResource(iconD);
330
    buttonInf.setImageResource(iconI);
331 8d1da3f1 Leszek Koltunski
332 6e3fcb91 Leszek Koltunski
    Resources res = act.getResources();
333 d6e7c7fb Leszek Koltunski
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(iconI);
334 6e3fcb91 Leszek Koltunski
    mBottomHeight = bd.getIntrinsicHeight();
335
336 8d1da3f1 Leszek Koltunski
    TypedValue outValue = new TypedValue();
337
    act.getTheme().resolveAttribute(android.R.attr.selectableItemBackgroundBorderless, outValue, true);
338
    buttonTut.setBackgroundResource(outValue.resourceId);
339 d6e7c7fb Leszek Koltunski
    buttonDow.setBackgroundResource(outValue.resourceId);
340
    buttonInf.setBackgroundResource(outValue.resourceId);
341 8d1da3f1 Leszek Koltunski
342
    buttonTut.setOnClickListener( new View.OnClickListener()
343
      {
344
      @Override
345
      public void onClick(View v)
346
        {
347
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
348
        RubikDialogTutorial tDiag = new RubikDialogTutorial();
349
        tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
350
        }
351
      });
352
353 d6e7c7fb Leszek Koltunski
    buttonDow.setOnClickListener( new View.OnClickListener()
354
      {
355
      @Override
356
      public void onClick(View v)
357
        {
358 5f3801d3 Leszek Koltunski
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
359
        RubikDialogUpdates uDiag = new RubikDialogUpdates();
360
        uDiag.show( act.getSupportFragmentManager(), RubikDialogUpdates.getDialogTag() );
361 d6e7c7fb Leszek Koltunski
        }
362
      });
363
364
    buttonInf.setOnClickListener( new View.OnClickListener()
365 8d1da3f1 Leszek Koltunski
      {
366
      @Override
367
      public void onClick(View v)
368
        {
369 f12e4de9 Leszek Koltunski
        if( mObjectPopup!=null ) mObjectPopup.dismiss();
370 400ff34d Leszek Koltunski
        int currObject = RubikObjectList.getCurrObject();
371
        act.switchConfig(currObject);
372 8d1da3f1 Leszek Koltunski
        }
373
      });
374 d6e7c7fb Leszek Koltunski
375 10373dc7 Leszek Koltunski
    mBubbleUpdates = layout.findViewById(R.id.bubbleUpdates);
376
    mBubbleUpdates.setVisibility(View.INVISIBLE);
377
378 d6e7c7fb Leszek Koltunski
    RubikNetwork network = RubikNetwork.getInstance();
379
    network.signUpForUpdates(this);
380 8d1da3f1 Leszek Koltunski
    }
381
382 e03e0352 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
383
384 ad0c8e0e Leszek Koltunski
  private void setupMenuWindow(final RubikActivity act, final float width)
385 e03e0352 Leszek Koltunski
    {
386
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
387 e07c48a2 Leszek Koltunski
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
388
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
389 e03e0352 Leszek Koltunski
390
    mMenuPopup = new PopupWindow(act);
391
    mMenuPopup.setContentView(layout);
392
    mMenuPopup.setFocusable(true);
393 c651024f Leszek Koltunski
    int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
394 ad0c8e0e Leszek Koltunski
    int padding = (int)(width*RubikActivity.PADDING);
395 e03e0352 Leszek Koltunski
396 0254cfd7 Leszek Koltunski
    mMenuLayoutWidth = (int)(width/2);
397
    mMenuLayoutHeight= (int)(2*margin + NUM_BUTTONS*(mMenuItemSize+margin));
398
399 43162dfb Leszek Koltunski
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams( mMenuLayoutWidth - 2*padding, (int)mMenuItemSize);
400 e03e0352 Leszek Koltunski
401
    for(int i=0; i<NUM_BUTTONS; i++)
402
      {
403
      final int but = i;
404
      Button button = new Button(act);
405 43162dfb Leszek Koltunski
      button.setLayoutParams(p);
406 e03e0352 Leszek Koltunski
      button.setText(BUTTON_LABELS[i]);
407 88fb92ba Leszek Koltunski
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
408 e03e0352 Leszek Koltunski
409
      button.setOnClickListener( new View.OnClickListener()
410
        {
411
        @Override
412
        public void onClick(View v)
413
          {
414
          mMenuPopup.dismiss();
415 0254cfd7 Leszek Koltunski
          MenuAction(act,but);
416 e03e0352 Leszek Koltunski
          }
417
        });
418
419 e07c48a2 Leszek Koltunski
      menuLayout.addView(button);
420 e03e0352 Leszek Koltunski
      }
421 0254cfd7 Leszek Koltunski
    }
422 e03e0352 Leszek Koltunski
423 0254cfd7 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
424
425
  private void setupPlayWindow(final RubikActivity act, final float width)
426
    {
427
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
428
    final View layout = layoutInflater.inflate(R.layout.popup_play, null);
429
    mPlayLayout = layout.findViewById(R.id.playGrid);
430
431
    mPlayLayoutWidth = (int)(width*0.4f);
432
433
    mPlayPopup = new PopupWindow(act);
434
    mPlayPopup.setContentView(layout);
435
    mPlayPopup.setFocusable(true);
436
437
    adjustLevels(act);
438 e03e0352 Leszek Koltunski
    }
439
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441
442 0254cfd7 Leszek Koltunski
  private void MenuAction(RubikActivity act, int button)
443 e03e0352 Leszek Koltunski
    {
444
    switch(button)
445
      {
446 8ab435b9 Leszek Koltunski
      case 0: Bundle sBundle = new Bundle();
447 400ff34d Leszek Koltunski
              int currObject = RubikObjectList.getCurrObject();
448
              sBundle.putInt("tab", currObject );
449 a8576d91 Leszek Koltunski
              sBundle.putBoolean("submitting", false);
450 e03e0352 Leszek Koltunski
              RubikDialogScores scores = new RubikDialogScores();
451 a8576d91 Leszek Koltunski
              scores.setArguments(sBundle);
452 e03e0352 Leszek Koltunski
              scores.show(act.getSupportFragmentManager(), null);
453
              break;
454 234a7582 Leszek Koltunski
      case 1: RubikDialogPattern pDiag = new RubikDialogPattern();
455
              pDiag.show( act.getSupportFragmentManager(), RubikDialogPattern.getDialogTag() );
456 e03e0352 Leszek Koltunski
              break;
457 b2a92941 Leszek Koltunski
      case 2: ScreenList.switchScreen(act, ScreenList.SVER);
458 e03e0352 Leszek Koltunski
              break;
459 b2a92941 Leszek Koltunski
      case 3: RubikDialogTutorial tDiag = new RubikDialogTutorial();
460 234a7582 Leszek Koltunski
              tDiag.show( act.getSupportFragmentManager(), RubikDialogTutorial.getDialogTag() );
461 2971588c Leszek Koltunski
              break;
462 b2a92941 Leszek Koltunski
      case 4: 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 dd874ae8 Leszek Koltunski
  void setupSolveButton(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
    mSolveButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_END,params);
475 46405bb4 Leszek Koltunski
476 a8576d91 Leszek Koltunski
    mSolveButton.setOnClickListener( new View.OnClickListener()
477 46405bb4 Leszek Koltunski
      {
478 a8576d91 Leszek Koltunski
      @Override
479
      public void onClick(View v)
480
        {
481 2afc6754 Leszek Koltunski
        act.getControl().solveObject();
482 dd1a65c1 Leszek Koltunski
        mMovesController.clearMoves(act);
483 a8576d91 Leszek Koltunski
        }
484
      });
485 46405bb4 Leszek Koltunski
    }
486
487 dd874ae8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
488
489
  private void setupScrambleButton(final RubikActivity act)
490
    {
491
    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);
492 b600ccd9 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
493
    mScrambleButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_START, params);
494 dd874ae8 Leszek Koltunski
495
    mScrambleButton.setOnClickListener( new View.OnClickListener()
496
      {
497
      @Override
498
      public void onClick(View v)
499
        {
500 400ff34d Leszek Koltunski
        int currObject = RubikObjectList.getCurrObject();
501
        RubikObject object = RubikObjectList.getObject(currObject);
502 d433b50e Leszek Koltunski
        int numScrambles = object==null ? 0 : object.getNumScramble();
503 dd874ae8 Leszek Koltunski
        mShouldReactToEndOfScrambling = false;
504
        act.getControl().scrambleObject(numScrambles);
505
        }
506
      });
507
    }
508
509 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
510 3398a606 Leszek Koltunski
// This is necessary! Otherwise the ObjectPopup will not be re-created next time and we will still
511
// hold a reference to the old instance of the RubikActivity class (because setupObjectWindow is not
512
// going to be called)
513
// An reference to the old instance of RubikActivity will cause all sorts of strange issues.
514 211b48f2 Leszek Koltunski
515
  public void savePreferences(SharedPreferences.Editor editor)
516
    {
517 d38a302b Leszek Koltunski
    editor.putInt("play_LevelValue", mLevelValue );
518
519 3398a606 Leszek Koltunski
    if( mObjectPopup!=null )
520
      {
521
      mObjectPopup.dismiss();
522
      mObjectPopup = null;
523
      }
524 87d86e5f Leszek Koltunski
525 3398a606 Leszek Koltunski
    if( mMenuPopup!=null )
526
      {
527
      mMenuPopup.dismiss();
528
      mMenuPopup = null;
529
      }
530
531
    if( mPlayPopup!=null )
532
      {
533
      mPlayPopup.dismiss();
534
      mPlayPopup = null;
535
      }
536 211b48f2 Leszek Koltunski
    }
537
538
///////////////////////////////////////////////////////////////////////////////////////////////////
539
540
  public void restorePreferences(SharedPreferences preferences)
541
    {
542 d38a302b Leszek Koltunski
    mLevelValue = preferences.getInt("play_LevelValue", 0);
543 211b48f2 Leszek Koltunski
    }
544
545 53f23b64 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
546
547 400ff34d Leszek Koltunski
  public void setCurrObject(RubikActivity act)
548 53f23b64 Leszek Koltunski
    {
549 400ff34d Leszek Koltunski
    if( mPlayLayout!=null ) adjustLevels(act);
550 7b7d65ce Leszek Koltunski
    }
551
552
///////////////////////////////////////////////////////////////////////////////////////////////////
553 255492a0 Leszek Koltunski
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
554
555
  private void displayPopup(RubikActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
556
    {
557
    View topLayout = act.findViewById(R.id.relativeLayout);
558 7ac0ee88 Leszek Koltunski
    boolean isFullScreen;
559 00aa398a Leszek Koltunski
560
    if( topLayout!=null )
561
      {
562
      topLayout.getLocationOnScreen(mLocation);
563 7ac0ee88 Leszek Koltunski
      isFullScreen = (mLocation[1]==0);
564 00aa398a Leszek Koltunski
      }
565
    else
566
      {
567 7ac0ee88 Leszek Koltunski
      isFullScreen = true;
568 00aa398a Leszek Koltunski
      }
569 255492a0 Leszek Koltunski
570 69a271f3 Leszek Koltunski
    try
571 255492a0 Leszek Koltunski
      {
572 69a271f3 Leszek Koltunski
      // if on Android 11 or we are fullscreen
573
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
574
        {
575
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
576
        window.update(view, w, h);
577
        }
578
      else  // Android 10 or below in pop-up mode or split-screen mode
579
        {
580
        view.getLocationOnScreen(mLocation);
581
        int width  = view.getWidth();
582
        int height = view.getHeight();
583
        int x = mLocation[0]+(width-w)/2;
584
        int y = mLocation[1]+height+yoff;
585
586
        window.showAsDropDown(view);
587
        window.update(x,y,w,h);
588
        }
589 255492a0 Leszek Koltunski
      }
590 69a271f3 Leszek Koltunski
    catch( IllegalArgumentException iae )
591 255492a0 Leszek Koltunski
      {
592 69a271f3 Leszek Koltunski
      // ignore, this means window is 'not attached to window manager' -
593
      // which most probably is because we are already exiting the app.
594 255492a0 Leszek Koltunski
      }
595
    }
596
597 ae77f661 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
598
599 d38a302b Leszek Koltunski
  private void adjustSolvedIcons()
600 ae77f661 Leszek Koltunski
    {
601 59cc16ae Leszek Koltunski
    if( mPlayLayout!=null )
602 ae77f661 Leszek Koltunski
      {
603 59cc16ae Leszek Koltunski
      int currObject = RubikObjectList.getCurrObject();
604
      int dbLevel = RubikObjectList.getDBLevel(currObject);
605
      int numLevel= Math.min(dbLevel, LEVELS_SHOWN);
606
      RubikScores scores = RubikScores.getInstance();
607
608
      for(int i=0; i<numLevel; i++)
609
        {
610
        int level = i<numLevel-1 ? i+1 : dbLevel;
611
        Button button = (Button)mPlayLayout.getChildAt(i);
612
        int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
613
        button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
614
        }
615 ae77f661 Leszek Koltunski
      }
616
    }
617
618 255492a0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
619 7b7d65ce Leszek Koltunski
620 0254cfd7 Leszek Koltunski
  private void adjustLevels(final RubikActivity act)
621 7b7d65ce Leszek Koltunski
    {
622 400ff34d Leszek Koltunski
    int currObject = RubikObjectList.getCurrObject();
623
    int dbLevel = RubikObjectList.getDBLevel(currObject);
624
    RubikObject object = RubikObjectList.getObject(currObject);
625 d433b50e Leszek Koltunski
    int numScrambles = object==null ? 0 : object.getNumScramble();
626 0a7aa15b Leszek Koltunski
    int numLevel = Math.min(dbLevel, LEVELS_SHOWN);
627 00af5060 Leszek Koltunski
    String[] levels = new String[numLevel];
628 7b7d65ce Leszek Koltunski
629 00af5060 Leszek Koltunski
    for(int i=0; i<numLevel-1; i++)
630 53f23b64 Leszek Koltunski
      {
631 7b7d65ce Leszek Koltunski
      levels[i] = act.getString(R.string.lv_placeholder,i+1);
632 53f23b64 Leszek Koltunski
      }
633
634 4fc7b1e6 Leszek Koltunski
    if( numLevel>0 )
635
      {
636
      levels[numLevel-1] = act.getString(R.string.level_full);
637
      }
638 00af5060 Leszek Koltunski
639 0a7aa15b Leszek Koltunski
    if( mLevelValue>dbLevel || mLevelValue<1 ||
640
       (mLevelValue<dbLevel || mLevelValue>LEVELS_SHOWN ) )
641 00af5060 Leszek Koltunski
      {
642
      mLevelValue=1;
643
      }
644 0254cfd7 Leszek Koltunski
645 de62316a Leszek Koltunski
    float width  = act.getScreenWidthInPixels();
646 c651024f Leszek Koltunski
    int margin   = (int)(width*RubikActivity.SMALL_MARGIN);
647 de62316a Leszek Koltunski
    int padding  = (int)(width*RubikActivity.PADDING);
648
    int butWidth = mPlayLayoutWidth - 2*padding;
649
    int butHeight= (int)mMenuItemSize;
650
    int lastButH = (int)(mMenuItemSize*LAST_BUTTON) ;
651 0254cfd7 Leszek Koltunski
652 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pM = new LinearLayout.LayoutParams( butWidth, butHeight );
653 0254cfd7 Leszek Koltunski
    pM.setMargins(margin, 0, margin, margin);
654 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pT = new LinearLayout.LayoutParams( butWidth, butHeight );
655 0254cfd7 Leszek Koltunski
    pT.setMargins(margin, margin, margin, margin);
656 de62316a Leszek Koltunski
    LinearLayout.LayoutParams pB = new LinearLayout.LayoutParams( butWidth, lastButH  );
657 0254cfd7 Leszek Koltunski
    pB.setMargins(margin, margin, margin, 2*margin);
658
659
    mPlayLayout.removeAllViews();
660
661 d7e539d0 Leszek Koltunski
    RubikScores scores = RubikScores.getInstance();
662
663 00af5060 Leszek Koltunski
    for(int i=0; i<numLevel; i++)
664 011fcfe0 Leszek Koltunski
      {
665 0a7aa15b Leszek Koltunski
      final int level     = i<numLevel-1 ? i+1 : dbLevel;
666
      final int scrambles = i<numLevel-1 ? i+1 : numScrambles;
667 0254cfd7 Leszek Koltunski
      Button button = new Button(act);
668 00af5060 Leszek Koltunski
      button.setLayoutParams(i==0 ? pT : (i==numLevel-1 ? pB : pM));
669 0254cfd7 Leszek Koltunski
      button.setText(levels[i]);
670
      button.setTextSize(TypedValue.COMPLEX_UNIT_PX, mMenuTextSize);
671
672 400ff34d Leszek Koltunski
      int icon = scores.isSolved(currObject, level-1) ? R.drawable.ui_solved : R.drawable.ui_notsolved;
673 d7e539d0 Leszek Koltunski
      button.setCompoundDrawablesWithIntrinsicBounds(icon,0,0,0);
674
675 0254cfd7 Leszek Koltunski
      button.setOnClickListener( new View.OnClickListener()
676 011fcfe0 Leszek Koltunski
        {
677 0254cfd7 Leszek Koltunski
        @Override
678
        public void onClick(View v)
679
          {
680 2afc6754 Leszek Koltunski
          ObjectControl control = act.getControl();
681 6fad862b Leszek Koltunski
682 2afc6754 Leszek Koltunski
          if(control.isUINotBlocked())
683 6fad862b Leszek Koltunski
            {
684 598de3ee Leszek Koltunski
            if( mPlayPopup!=null ) mPlayPopup.dismiss();
685 0a7aa15b Leszek Koltunski
            mLevelValue = level;
686 dd874ae8 Leszek Koltunski
            mShouldReactToEndOfScrambling = true;
687 2afc6754 Leszek Koltunski
            control.scrambleObject(scrambles);
688 6fad862b Leszek Koltunski
            }
689 0254cfd7 Leszek Koltunski
          }
690
        });
691 011fcfe0 Leszek Koltunski
692 0254cfd7 Leszek Koltunski
      mPlayLayout.addView(button);
693
      }
694 53f23b64 Leszek Koltunski
    }
695
696 211b48f2 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
697
698 dca3888a Leszek Koltunski
  public int getLevel()
699 211b48f2 Leszek Koltunski
    {
700 85b09df4 Leszek Koltunski
    return mLevelValue;
701 211b48f2 Leszek Koltunski
    }
702
703 dd874ae8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
704
705
  public boolean shouldReactToEndOfScrambling()
706
    {
707
    return mShouldReactToEndOfScrambling;
708
    }
709 63dd19c4 Leszek Koltunski
710
///////////////////////////////////////////////////////////////////////////////////////////////////
711
712
  public void receiveUpdate(RubikUpdates updates)
713
    {
714 179f7189 Leszek Koltunski
    Activity act = mWeakAct.get();
715 10373dc7 Leszek Koltunski
716 179f7189 Leszek Koltunski
    if( act!=null )
717 903c7bbc Leszek Koltunski
      {
718 179f7189 Leszek Koltunski
      act.runOnUiThread(new Runnable()
719
        {
720
        @Override
721
        public void run()
722
          {
723 2c9ab085 Leszek Koltunski
          int num = updates.getCompletedNumber();
724 179f7189 Leszek Koltunski
725
          if( num>0 )
726
            {
727
            String shownNum = String.valueOf(num);
728
            mBubbleUpdates.setText(shownNum);
729
            mBubbleUpdates.setVisibility(View.VISIBLE);
730
            int height = (int)(0.05f*mScreenWidth);
731
            mBubbleUpdates.setTextSize(TypedValue.COMPLEX_UNIT_PX,height);
732
            }
733
         else
734
            {
735
            mBubbleUpdates.setVisibility(View.INVISIBLE);
736
            }
737
          }
738
        });
739 903c7bbc Leszek Koltunski
      }
740 63dd19c4 Leszek Koltunski
    }
741
742
///////////////////////////////////////////////////////////////////////////////////////////////////
743
744
  public void errorUpdate()
745
    {
746 c99db493 Leszek Koltunski
    android.util.Log.e("D", "Screen: Error receiving update");
747 63dd19c4 Leszek Koltunski
    }
748 ffe7e55d Leszek Koltunski
749
///////////////////////////////////////////////////////////////////////////////////////////////////
750
751 b88cdd91 Leszek Koltunski
  public void iconDownloaded(int ordinal, Bitmap bitmap)
752 ffe7e55d Leszek Koltunski
    {
753
    // empty
754
    }
755 211b48f2 Leszek Koltunski
  }