Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigScreen.java @ 97a4ae23

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.content.res.Resources;
23
import android.graphics.drawable.BitmapDrawable;
24
import android.os.Build;
25
import android.util.TypedValue;
26
import android.view.Gravity;
27
import android.view.View;
28
import android.widget.GridLayout;
29
import android.widget.ImageButton;
30
import android.widget.LinearLayout;
31
import android.widget.PopupWindow;
32
import android.widget.ScrollView;
33
import android.widget.TextView;
34

    
35
import org.distorted.objectlib.main.ObjectControl;
36
import org.distorted.objectlib.main.ObjectType;
37

    
38
import org.distorted.helpers.TransparentImageButton;
39
import org.distorted.main.R;
40
import org.distorted.main.RubikActivity;
41

    
42
import static android.view.View.inflate;
43
import static org.distorted.objectlib.main.ObjectType.NUM_OBJECTS;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
public class ConfigScreen
48
{
49
  private static final int NUM_COLUMNS = 4;
50
  private static final int[] mLocation = new int[2];
51

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

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  private void setupObjectWindow(final ConfigActivity act, final float width, final float height)
64
    {
65
    int icon = RubikActivity.getDrawable(R.drawable.cube_2s,R.drawable.cube_2m, R.drawable.cube_2b, R.drawable.cube_2h);
66

    
67
    Resources res = act.getResources();
68
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(icon);
69
    int cubeWidth = bd.getIntrinsicWidth();
70
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
71
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
72
    mMaxRowCount = (int)((height-mBarHeight)/mObjectSize);
73

    
74
    ScrollView view = (ScrollView)inflate( act, R.layout.popup_object_simple, null);
75
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
76

    
77
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
78
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
79

    
80
    objectGrid.setColumnCount(mColCount);
81
    objectGrid.setRowCount(mRowCount);
82

    
83
    mObjectPopup = new PopupWindow(act);
84
    mObjectPopup.setFocusable(true);
85
    mObjectPopup.setContentView(view);
86

    
87
    int[] nextInRow = new int[mRowCount];
88

    
89
    for(int row=0; row<mRowCount; row++)
90
      {
91
      rowSpecs[row] = GridLayout.spec(row);
92
      nextInRow[row]= 0;
93
      }
94
    for(int col=0; col<mColCount; col++)
95
      {
96
      colSpecs[col] = GridLayout.spec(col);
97
      }
98

    
99
    for(int object = 0; object< NUM_OBJECTS; object++)
100
      {
101
      final int ordinal = object;
102
      ObjectType type = ObjectType.getObject(ordinal);
103
      int iconSize = RubikActivity.getDrawableSize();
104
      int icons = type.getIconID(iconSize);
105
      int row = object/NUM_COLUMNS;
106

    
107
      ImageButton button = new ImageButton(act);
108
      button.setBackgroundResource(icons);
109
      button.setOnClickListener( new View.OnClickListener()
110
        {
111
        @Override
112
        public void onClick(View v)
113
          {
114
          if( act.getControl().isUINotBlocked() && mObjectOrdinal!=ordinal )
115
            {
116
            mObjectOrdinal = ordinal;
117
            act.changeObject(type);
118
            mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,NUM_OBJECTS));
119
            setupInfo(act);
120
            }
121

    
122
          mObjectPopup.dismiss();
123
          }
124
        });
125

    
126
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
127
      params.bottomMargin = margin;
128
      params.topMargin    = margin;
129
      params.leftMargin   = margin;
130
      params.rightMargin  = margin;
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.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,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)
237
    {
238
    mObjectOrdinal--;
239
    if( mObjectOrdinal<0 ) mObjectOrdinal=NUM_OBJECTS-1;
240

    
241
    ObjectType type = ObjectType.getObject(mObjectOrdinal);
242
    act.changeObject(type);
243

    
244
    setupInfo(act);
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  private void nextObject(ConfigActivity act)
250
    {
251
    mObjectOrdinal++;
252
    if( mObjectOrdinal>=NUM_OBJECTS ) mObjectOrdinal=0;
253

    
254
    ObjectType type = ObjectType.getObject(mObjectOrdinal);
255
    act.changeObject(type);
256

    
257
    setupInfo(act);
258
    }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

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

    
268
    mPrevButton.setOnClickListener( new View.OnClickListener()
269
      {
270
      @Override
271
      public void onClick(View v)
272
        {
273
        prevObject(act);
274
        mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,NUM_OBJECTS));
275
        }
276
      });
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

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

    
287
    mNextButton.setOnClickListener( new View.OnClickListener()
288
      {
289
      @Override
290
      public void onClick(View v)
291
        {
292
        nextObject(act);
293
        mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,NUM_OBJECTS));
294
        }
295
      });
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

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

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

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320
// TODO: info pane
321

    
322
  private void setupInfo(ConfigActivity act)
323
    {
324

    
325
    }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

    
329
  void enterScreen(final ConfigActivity act, final int objectOrdinal)
330
    {
331
    int width = act.getScreenWidthInPixels();
332
    mBarHeight = act.getHeightBar();
333
    mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
334

    
335
    mRowCount = (NUM_OBJECTS + NUM_COLUMNS-1) / NUM_COLUMNS;
336
    mColCount = NUM_COLUMNS;
337

    
338
    mObjectOrdinal = objectOrdinal;
339

    
340
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams((int)(width/4),LinearLayout.LayoutParams.MATCH_PARENT);
341
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams((int)(width/2),LinearLayout.LayoutParams.MATCH_PARENT);
342
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams((int)(width/4),LinearLayout.LayoutParams.MATCH_PARENT);
343

    
344
    LinearLayout layoutLeft = new LinearLayout(act);
345
    layoutLeft.setLayoutParams(paramsL);
346
    LinearLayout layoutMid  = new LinearLayout(act);
347
    layoutMid.setLayoutParams(paramsM);
348
    LinearLayout layoutRight= new LinearLayout(act);
349
    layoutRight.setLayoutParams(paramsR);
350

    
351
    setupObjectButton(act,width);
352
    setupPrevButton(act);
353
    setupNextButton(act);
354
    setupTextView(act,width);
355
    setupBackButton(act);
356

    
357
    layoutLeft.addView(mObjectButton);
358
    layoutMid.addView(mPrevButton);
359
    layoutMid.addView(mMovesText);
360
    layoutMid.addView(mNextButton);
361
    layoutRight.addView(mBackButton);
362

    
363
    LinearLayout layout = act.findViewById(R.id.lowerBar);
364
    layout.removeAllViews();
365
    layout.addView(layoutLeft);
366
    layout.addView(layoutMid);
367
    layout.addView(layoutRight);
368

    
369
    setupInfo(act);
370
    }
371
}
(4-4/5)