Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigScreen.java @ 01e57154

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 rObject = RubikObjectList.getObject(ordinal);
100
      int row = object/NUM_COLUMNS;
101
      ImageButton button = new ImageButton(act);
102
      if( rObject!=null ) rObject.setIconTo(act,button);
103

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

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

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

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

    
132
      nextInRow[row]++;
133

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

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

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

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

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

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

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

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

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

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

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

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

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

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

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

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

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

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

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

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

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

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

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

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

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

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

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

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

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

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

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

    
329
    mObjectOrdinal = objectOrdinal;
330

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

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

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

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

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

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