Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigScreen.java @ 71cda061

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.config;
11

    
12
import android.os.Build;
13
import android.util.TypedValue;
14
import android.view.Gravity;
15
import android.view.View;
16
import android.widget.GridLayout;
17
import android.widget.ImageButton;
18
import android.widget.LinearLayout;
19
import android.widget.PopupWindow;
20
import android.widget.ScrollView;
21
import android.widget.TextView;
22

    
23
import org.distorted.helpers.PopupCreator;
24
import org.distorted.objectlib.main.ObjectControl;
25

    
26
import org.distorted.helpers.TransparentImageButton;
27
import org.distorted.main.R;
28
import org.distorted.main.RubikActivity;
29
import org.distorted.objects.RubikObject;
30
import org.distorted.objects.RubikObjectList;
31

    
32
import static android.view.View.inflate;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
public class ConfigScreen
37
{
38
  private static final int NUM_COLUMNS = 5;
39
  private static final int[] mLocation = new int[2];
40

    
41
  private TransparentImageButton mBackButton, mObjectButton, mPrevButton, mNextButton;
42
  private TextView mMovesText;
43
  private PopupWindow mObjectPopup;
44
  private ConfigScreenPane mPane;
45
  private int mObjectOrdinal;
46
  private int mColCount, mRowCount, mMaxRowCount;
47
  private int mObjectSize;
48
  private int mBarHeight;
49
  private float mButtonSize;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  private void setupObjectWindow(final ConfigActivity act, final float width, final float height)
54
    {
55
    int numObjects= RubikObjectList.getNumObjects();
56
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
57
    mColCount = NUM_COLUMNS;
58

    
59
    int cubeSize  = (int)(width/9);
60
    int margin    = (int)(width*RubikActivity.LARGE_MARGIN);
61
    int padding   = (int)(width*RubikActivity.POPUP_PADDING);
62
    mObjectSize   = (int)(cubeSize + 2*margin + 0.5f);
63
    mMaxRowCount  = (int)((height-mBarHeight)/mObjectSize);
64

    
65
    ScrollView view = (ScrollView)inflate( act, R.layout.popup_object_simple, null);
66
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
67

    
68
    PopupCreator.createObjectGrid(objectGrid,act,mRowCount,mColCount,numObjects,margin,cubeSize,padding);
69

    
70
    for(int child=0; child<numObjects; child++)
71
      {
72
      final RubikObject obj = RubikObjectList.getObject(child);
73
      View v = objectGrid.getChildAt(child);
74
      ImageButton button = PopupCreator.getButton(obj,v);
75
      final int ordinal = child;
76

    
77
      button.setOnClickListener( new View.OnClickListener()
78
        {
79
        @Override
80
        public void onClick(View v)
81
          {
82
          if( act.getControl().isUINotBlocked() && mObjectOrdinal!=ordinal )
83
            {
84
            mObjectOrdinal = ordinal;
85
            act.changeObject(mObjectOrdinal);
86
            mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
87
            mPane.updatePane(act,mObjectOrdinal);
88
            }
89

    
90
          mObjectPopup.dismiss();
91
          }
92
        });
93
      }
94

    
95
    mObjectPopup = new PopupWindow(act);
96
    mObjectPopup.setFocusable(true);
97
    mObjectPopup.setContentView(view);
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
  private void setupBackButton(final ConfigActivity act)
103
    {
104
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_smallback,R.drawable.ui_medium_smallback, R.drawable.ui_big_smallback, R.drawable.ui_huge_smallback);
105
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
106
    mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
107

    
108
    mBackButton.setOnClickListener( new View.OnClickListener()
109
      {
110
      @Override
111
      public void onClick(View v)
112
        {
113
        ObjectControl control = act.getControl();
114
        if( control!=null ) control.unblockEverything();
115
        act.finish();
116
        }
117
      });
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  private void setupObjectButton(final ConfigActivity act, final int width)
123
    {
124
    final int margin= (int)(width*RubikActivity.SMALL_MARGIN);
125
    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);
126
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
127
    mObjectButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
128

    
129
    mObjectButton.setOnClickListener( new View.OnClickListener()
130
      {
131
      @Override
132
      public void onClick(View view)
133
        {
134
        if( mObjectPopup==null )
135
          {
136
          float height= act.getScreenHeightInPixels();
137
          setupObjectWindow(act,width,height);
138
          }
139

    
140
        if( act.getControl().isUINotBlocked())
141
          {
142
          int rowCount = Math.min(mMaxRowCount,mRowCount);
143
          View popupView = mObjectPopup.getContentView();
144
          popupView.setSystemUiVisibility(RubikActivity.FLAGS);
145
          displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+5*margin,margin,margin);
146
          }
147
        }
148
      });
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
153

    
154
  private void displayPopup(ConfigActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
155
    {
156
    View topLayout = act.findViewById(R.id.mainLayout);
157
    boolean isFullScreen;
158

    
159
    if( topLayout!=null )
160
      {
161
      topLayout.getLocationOnScreen(mLocation);
162
      isFullScreen = (mLocation[1]==0);
163
      }
164
    else
165
      {
166
      isFullScreen = true;
167
      }
168

    
169
    try
170
      {
171
      // if on Android 11 or we are fullscreen
172
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R || isFullScreen )
173
        {
174
        window.showAsDropDown(view, xoff, yoff, Gravity.CENTER);
175
        window.update(view, w, h);
176
        }
177
      else  // Android 10 or below in pop-up mode or split-screen mode
178
        {
179
        view.getLocationOnScreen(mLocation);
180
        int width  = view.getWidth();
181
        int height = view.getHeight();
182
        int x = mLocation[0]+(width-w)/2;
183
        int y = mLocation[1]+height+yoff;
184

    
185
        window.showAsDropDown(view);
186
        window.update(x,y,w,h);
187
        }
188
      }
189
    catch( IllegalArgumentException iae )
190
      {
191
      // ignore, this means window is 'not attached to window manager' -
192
      // which most probably is because we are already exiting the app.
193
      }
194
    }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
  private void prevObject(ConfigActivity act, int numObjects)
199
    {
200
    mObjectOrdinal--;
201
    if( mObjectOrdinal<0 ) mObjectOrdinal=numObjects-1;
202

    
203
    act.changeObject(mObjectOrdinal);
204
    mPane.updatePane(act,mObjectOrdinal);
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
  private void nextObject(ConfigActivity act, int numObjects)
210
    {
211
    mObjectOrdinal++;
212
    if( mObjectOrdinal>=numObjects ) mObjectOrdinal=0;
213

    
214
    act.changeObject(mObjectOrdinal);
215
    mPane.updatePane(act,mObjectOrdinal);
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  private void setupPrevButton(final ConfigActivity act)
221
    {
222
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
223
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
224
    mPrevButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
225

    
226
    mPrevButton.setOnClickListener( new View.OnClickListener()
227
      {
228
      @Override
229
      public void onClick(View v)
230
        {
231
        int numObjects = RubikObjectList.getNumObjects();
232
        prevObject(act,numObjects);
233
        mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
234
        }
235
      });
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
  private void setupNextButton(final ConfigActivity act)
241
    {
242
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_right,R.drawable.ui_medium_right, R.drawable.ui_big_right, R.drawable.ui_huge_right);
243
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
244
    mNextButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
245

    
246
    mNextButton.setOnClickListener( new View.OnClickListener()
247
      {
248
      @Override
249
      public void onClick(View v)
250
        {
251
        int numObjects = RubikObjectList.getNumObjects();
252
        nextObject(act,numObjects);
253
        mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
254
        }
255
      });
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  private void setupTextView(final ConfigActivity act, final float width, int numObjects)
261
    {
262
    int padding = (int)(width*RubikActivity.PADDING);
263
    int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
264
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
265
    params.topMargin    = margin;
266
    params.bottomMargin = margin;
267
    params.leftMargin   = margin;
268
    params.rightMargin  = margin;
269

    
270
    mMovesText = new TextView(act);
271
    mMovesText.setTextSize(20);
272
    mMovesText.setLayoutParams(params);
273
    mMovesText.setPadding(padding,0,padding,0);
274
    mMovesText.setGravity(Gravity.CENTER);
275
    mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
276
    mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
277
    }
278

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

    
281
  void onAttachedToWindow(final ConfigActivity act, final int objectOrdinal)
282
    {
283
    int numObjects = RubikObjectList.getNumObjects();
284
    int width = act.getScreenWidthInPixels();
285
    mBarHeight = act.getHeightBar();
286
    mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
287
    mObjectOrdinal = objectOrdinal;
288

    
289
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
290
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams(width/2, LinearLayout.LayoutParams.MATCH_PARENT);
291
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
292

    
293
    LinearLayout layoutLeft = new LinearLayout(act);
294
    layoutLeft.setLayoutParams(paramsL);
295
    LinearLayout layoutMid  = new LinearLayout(act);
296
    layoutMid.setLayoutParams(paramsM);
297
    LinearLayout layoutRight= new LinearLayout(act);
298
    layoutRight.setLayoutParams(paramsR);
299

    
300
    setupObjectButton(act,width);
301
    setupPrevButton(act);
302
    setupNextButton(act);
303
    setupTextView(act,width,numObjects);
304
    setupBackButton(act);
305

    
306
    layoutLeft.addView(mObjectButton);
307
    layoutMid.addView(mPrevButton);
308
    layoutMid.addView(mMovesText);
309
    layoutMid.addView(mNextButton);
310
    layoutRight.addView(mBackButton);
311

    
312
    LinearLayout layout = act.findViewById(R.id.lowerBar);
313
    layout.removeAllViews();
314
    layout.addView(layoutLeft);
315
    layout.addView(layoutMid);
316
    layout.addView(layoutRight);
317

    
318
    mPane = new ConfigScreenPane(act,mObjectOrdinal);
319
    }
320
}
(4-4/6)