Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigScreen.java @ 1071fb69

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.config;
21

    
22
import android.os.Build;
23
import android.util.TypedValue;
24
import android.view.Gravity;
25
import android.view.View;
26
import android.widget.GridLayout;
27
import android.widget.ImageButton;
28
import android.widget.LinearLayout;
29
import android.widget.PopupWindow;
30
import android.widget.ScrollView;
31
import android.widget.TextView;
32

    
33
import org.distorted.objectlib.main.ObjectControl;
34

    
35
import org.distorted.helpers.TransparentImageButton;
36
import org.distorted.main.R;
37
import org.distorted.main.RubikActivity;
38
import org.distorted.objects.RubikObject;
39
import org.distorted.objects.RubikObjectList;
40

    
41
import static android.view.View.inflate;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class ConfigScreen
46
{
47
  private static final int NUM_COLUMNS = 5;
48
  private static final int[] mLocation = new int[2];
49

    
50
  private TransparentImageButton mBackButton, mObjectButton, mPrevButton, mNextButton;
51
  private TextView mMovesText;
52
  private PopupWindow mObjectPopup;
53
  private ConfigScreenPane mPane;
54
  private int mObjectOrdinal;
55
  private int mColCount, mRowCount, mMaxRowCount;
56
  private int mObjectSize;
57
  private int mBarHeight;
58
  private float mButtonSize;
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  private void setupObjectWindow(final ConfigActivity act, final float width, final float height)
63
    {
64
    int cubeWidth = (int)(width/9);
65
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
66
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
67
    mMaxRowCount = (int)((height-mBarHeight)/mObjectSize);
68

    
69
    ScrollView view = (ScrollView)inflate( act, R.layout.popup_object_simple, null);
70
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
71

    
72
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
73
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
74

    
75
    objectGrid.setColumnCount(mColCount);
76
    objectGrid.setRowCount(mRowCount);
77

    
78
    mObjectPopup = new PopupWindow(act);
79
    mObjectPopup.setFocusable(true);
80
    mObjectPopup.setContentView(view);
81

    
82
    int[] nextInRow = new int[mRowCount];
83

    
84
    for(int row=0; row<mRowCount; row++)
85
      {
86
      rowSpecs[row] = GridLayout.spec(row);
87
      nextInRow[row]= 0;
88
      }
89
    for(int col=0; col<mColCount; col++)
90
      {
91
      colSpecs[col] = GridLayout.spec(col);
92
      }
93

    
94
    int numObjects = RubikObjectList.getNumObjects();
95

    
96
    for(int object=0; object<numObjects; object++)
97
      {
98
      final int ordinal = object;
99
      RubikObject rubikObject = RubikObjectList.getObject(ordinal);
100
      int icons = rubikObject.getIconID();
101
      int row = object/NUM_COLUMNS;
102

    
103
      ImageButton button = new ImageButton(act);
104
      button.setBackgroundResource(icons);
105
      button.setOnClickListener( new View.OnClickListener()
106
        {
107
        @Override
108
        public void onClick(View v)
109
          {
110
          if( act.getControl().isUINotBlocked() && mObjectOrdinal!=ordinal )
111
            {
112
            mObjectOrdinal = ordinal;
113
            act.changeObject(mObjectOrdinal);
114
            mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
115
            mPane.updatePane(act,mObjectOrdinal);
116
            }
117

    
118
          mObjectPopup.dismiss();
119
          }
120
        });
121

    
122
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
123
      params.bottomMargin = margin;
124
      params.topMargin    = margin;
125
      params.leftMargin   = margin;
126
      params.rightMargin  = margin;
127

    
128
      params.width = cubeWidth;
129
      params.height= cubeWidth;
130

    
131
      nextInRow[row]++;
132

    
133
      objectGrid.addView(button, params);
134
      }
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  private void setupBackButton(final ConfigActivity act)
140
    {
141
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_smallback,R.drawable.ui_medium_smallback, R.drawable.ui_big_smallback, R.drawable.ui_huge_smallback);
142
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
143
    mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
144

    
145
    mBackButton.setOnClickListener( new View.OnClickListener()
146
      {
147
      @Override
148
      public void onClick(View v)
149
        {
150
        ObjectControl control = act.getControl();
151
        if( control!=null ) control.unblockEverything();
152
        act.finish();
153
        }
154
      });
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  private void setupObjectButton(final ConfigActivity act, final int width)
160
    {
161
    final int margin= (int)(width*RubikActivity.MARGIN);
162
    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);
163
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
164
    mObjectButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
165

    
166
    mObjectButton.setOnClickListener( new View.OnClickListener()
167
      {
168
      @Override
169
      public void onClick(View view)
170
        {
171
        if( mObjectPopup==null )
172
          {
173
          float height= act.getScreenHeightInPixels();
174
          setupObjectWindow(act,width,height);
175
          }
176

    
177
        if( act.getControl().isUINotBlocked())
178
          {
179
          int rowCount = Math.min(mMaxRowCount,mRowCount);
180
          View popupView = mObjectPopup.getContentView();
181
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
182
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+5*margin,margin,margin);
183
          }
184
        }
185
      });
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
190

    
191
  private void displayPopup(ConfigActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
192
    {
193
    View topLayout = act.findViewById(R.id.mainLayout);
194
    boolean isFullScreen;
195

    
196
    if( topLayout!=null )
197
      {
198
      topLayout.getLocationOnScreen(mLocation);
199
      isFullScreen = (mLocation[1]==0);
200
      }
201
    else
202
      {
203
      isFullScreen = true;
204
      }
205

    
206
    try
207
      {
208
      // if on Android 11 or we are fullscreen
209
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
210
        {
211
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
212
        window.update(view, w, h);
213
        }
214
      else  // Android 10 or below in pop-up mode or split-screen mode
215
        {
216
        view.getLocationOnScreen(mLocation);
217
        int width  = view.getWidth();
218
        int height = view.getHeight();
219
        int x = mLocation[0]+(width-w)/2;
220
        int y = mLocation[1]+height+yoff;
221

    
222
        window.showAsDropDown(view);
223
        window.update(x,y,w,h);
224
        }
225
      }
226
    catch( IllegalArgumentException iae )
227
      {
228
      // ignore, this means window is 'not attached to window manager' -
229
      // which most probably is because we are already exiting the app.
230
      }
231
    }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
  private void prevObject(ConfigActivity act, int numObjects)
236
    {
237
    mObjectOrdinal--;
238
    if( mObjectOrdinal<0 ) mObjectOrdinal=numObjects-1;
239

    
240
    act.changeObject(mObjectOrdinal);
241
    mPane.updatePane(act,mObjectOrdinal);
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  private void nextObject(ConfigActivity act, int numObjects)
247
    {
248
    mObjectOrdinal++;
249
    if( mObjectOrdinal>=numObjects ) mObjectOrdinal=0;
250

    
251
    act.changeObject(mObjectOrdinal);
252
    mPane.updatePane(act,mObjectOrdinal);
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  private void setupPrevButton(final ConfigActivity act)
258
    {
259
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
260
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
261
    mPrevButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
262

    
263
    mPrevButton.setOnClickListener( new View.OnClickListener()
264
      {
265
      @Override
266
      public void onClick(View v)
267
        {
268
        int numObjects = RubikObjectList.getNumObjects();
269
        prevObject(act,numObjects);
270
        mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
271
        }
272
      });
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  private void setupNextButton(final ConfigActivity act)
278
    {
279
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_right,R.drawable.ui_medium_right, R.drawable.ui_big_right, R.drawable.ui_huge_right);
280
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
281
    mNextButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
282

    
283
    mNextButton.setOnClickListener( new View.OnClickListener()
284
      {
285
      @Override
286
      public void onClick(View v)
287
        {
288
        int numObjects = RubikObjectList.getNumObjects();
289
        nextObject(act,numObjects);
290
        mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
291
        }
292
      });
293
    }
294

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296

    
297
  private void setupTextView(final ConfigActivity act, final float width, int numObjects)
298
    {
299
    int padding = (int)(width*RubikActivity.PADDING);
300
    int margin  = (int)(width*RubikActivity.MARGIN);
301
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
302
    params.topMargin    = margin;
303
    params.bottomMargin = margin;
304
    params.leftMargin   = margin;
305
    params.rightMargin  = margin;
306

    
307
    mMovesText = new TextView(act);
308
    mMovesText.setTextSize(20);
309
    mMovesText.setLayoutParams(params);
310
    mMovesText.setPadding(padding,0,padding,0);
311
    mMovesText.setGravity(Gravity.CENTER);
312
    mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
313
    mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
314
    }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

    
318
  void onAttachedToWindow(final ConfigActivity act, final int objectOrdinal)
319
    {
320
    int numObjects = RubikObjectList.getNumObjects();
321
    int width = act.getScreenWidthInPixels();
322
    mBarHeight = act.getHeightBar();
323
    mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
324

    
325
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
326
    mColCount = NUM_COLUMNS;
327

    
328
    mObjectOrdinal = objectOrdinal;
329

    
330
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
331
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams(width/2, LinearLayout.LayoutParams.MATCH_PARENT);
332
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
333

    
334
    LinearLayout layoutLeft = new LinearLayout(act);
335
    layoutLeft.setLayoutParams(paramsL);
336
    LinearLayout layoutMid  = new LinearLayout(act);
337
    layoutMid.setLayoutParams(paramsM);
338
    LinearLayout layoutRight= new LinearLayout(act);
339
    layoutRight.setLayoutParams(paramsR);
340

    
341
    setupObjectButton(act,width);
342
    setupPrevButton(act);
343
    setupNextButton(act);
344
    setupTextView(act,width,numObjects);
345
    setupBackButton(act);
346

    
347
    layoutLeft.addView(mObjectButton);
348
    layoutMid.addView(mPrevButton);
349
    layoutMid.addView(mMovesText);
350
    layoutMid.addView(mNextButton);
351
    layoutRight.addView(mBackButton);
352

    
353
    LinearLayout layout = act.findViewById(R.id.lowerBar);
354
    layout.removeAllViews();
355
    layout.addView(layoutLeft);
356
    layout.addView(layoutMid);
357
    layout.addView(layoutRight);
358

    
359
    mPane = new ConfigScreenPane(act,mObjectOrdinal);
360
    }
361
}
(4-4/6)