Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigScreen.java @ 7bb30586

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

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

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

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
public class ConfigScreen
36
{
37
  private static final float TEXT_SIZE = 0.0310f;
38
  private static final float PADDING   = 0.0060f;
39
  private static final float MARGIN    = 0.0024f;
40
  private static final int NUM_COLUMNS = 5;
41
  private static final int[] mLocation = new int[2];
42
  private static final float POPUP_PADDING = 0.028f;
43
  private static final float POPUP_MARGIN  = 0.016f;
44

    
45
  private TransparentImageButton mBackButton, mObjectButton, mPrevButton, mNextButton;
46
  private TextView mMovesText;
47
  private PopupWindow mObjectPopup;
48
  private ConfigScreenPane mPane;
49
  private int mObjectOrdinal;
50
  private int mColCount, mRowCount, mMaxRowCount;
51
  private int mObjectSize;
52
  private int mBarHeight;
53
  private float mButtonSize;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  private void setupObjectWindow(final ConfigActivity act, final float width, final float height)
58
    {
59
    int numObjects= RubikObjectList.getNumObjects();
60
    mRowCount = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
61
    mColCount = NUM_COLUMNS;
62

    
63
    int cubeSize  = (int)( (Math.min(width,(int)(height*0.7f))) / 9 );
64
    int margin    = (int)(height*POPUP_MARGIN);
65
    int padding   = (int)(height*POPUP_PADDING);
66
    mObjectSize   = (int)(cubeSize + 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
    PopupCreator.createObjectGrid(objectGrid,act,mRowCount,mColCount,numObjects,margin,cubeSize,padding);
73

    
74
    for(int child=0; child<numObjects; child++)
75
      {
76
      final RubikObject obj = RubikObjectList.getObject(child);
77
      View v = objectGrid.getChildAt(child);
78
      ImageButton button = PopupCreator.getButton(obj,v);
79
      final int ordinal = child;
80

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

    
94
          mObjectPopup.dismiss();
95
          }
96
        });
97
      }
98

    
99
    mObjectPopup = new PopupWindow(act);
100
    mObjectPopup.setFocusable(true);
101
    mObjectPopup.setContentView(view);
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
  private void setupBackButton(final ConfigActivity act)
107
    {
108
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
109
    mBackButton = new TransparentImageButton(act,R.drawable.ui_smallback,params);
110

    
111
    mBackButton.setOnClickListener( new View.OnClickListener()
112
      {
113
      @Override
114
      public void onClick(View v)
115
        {
116
        act.finish();
117
        }
118
      });
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  private void setupObjectButton(final ConfigActivity act, final int width, final int height)
124
    {
125
    final int margin= (int)(height*MARGIN);
126
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
127
    mObjectButton = new TransparentImageButton(act,R.drawable.ui_cube_menu,params);
128

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

    
139
        int rowCount = Math.min(mMaxRowCount,mRowCount);
140
        View popupView = mObjectPopup.getContentView();
141
        popupView.setSystemUiVisibility(MainActivity.FLAGS);
142
        displayPopup(act,view,mObjectPopup,mObjectSize*mColCount,mObjectSize*rowCount+5*margin,margin,margin);
143
        }
144
      });
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
// work around lame bugs in Android's version <= 10 pop-up and split-screen modes
149

    
150
  private void displayPopup(ConfigActivity act, View view, PopupWindow window, int w, int h, int xoff, int yoff)
151
    {
152
    View topLayout = act.findViewById(R.id.mainLayout);
153
    boolean isFullScreen;
154

    
155
    if( topLayout!=null )
156
      {
157
      topLayout.getLocationOnScreen(mLocation);
158
      isFullScreen = (mLocation[1]==0);
159
      }
160
    else
161
      {
162
      isFullScreen = true;
163
      }
164

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

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

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  private void prevObject(ConfigActivity act, int numObjects)
195
    {
196
    mObjectOrdinal--;
197
    if( mObjectOrdinal<0 ) mObjectOrdinal=numObjects-1;
198

    
199
    act.changeObject(mObjectOrdinal);
200
    mPane.updatePane(act,mObjectOrdinal);
201
    }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
  private void nextObject(ConfigActivity act, int numObjects)
206
    {
207
    mObjectOrdinal++;
208
    if( mObjectOrdinal>=numObjects ) mObjectOrdinal=0;
209

    
210
    act.changeObject(mObjectOrdinal);
211
    mPane.updatePane(act,mObjectOrdinal);
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  private void setupPrevButton(final ConfigActivity act)
217
    {
218
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
219
    mPrevButton = new TransparentImageButton(act,R.drawable.ui_left,params);
220

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

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

    
235
  private void setupNextButton(final ConfigActivity act)
236
    {
237
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
238
    mNextButton = new TransparentImageButton(act,R.drawable.ui_right,params);
239

    
240
    mNextButton.setOnClickListener( new View.OnClickListener()
241
      {
242
      @Override
243
      public void onClick(View v)
244
        {
245
        int numObjects = RubikObjectList.getNumObjects();
246
        nextObject(act,numObjects);
247
        mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
248
        }
249
      });
250
    }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
  private void setupTextView(final ConfigActivity act, final float height, int numObjects)
255
    {
256
    int padding = (int)(height*PADDING);
257
    int margin  = (int)(height*MARGIN);
258
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
259
    params.topMargin    = margin;
260
    params.bottomMargin = margin;
261
    params.leftMargin   = margin;
262
    params.rightMargin  = margin;
263

    
264
    mMovesText = new TextView(act);
265
    mMovesText.setTextSize(20);
266
    mMovesText.setLayoutParams(params);
267
    mMovesText.setPadding(padding,0,padding,0);
268
    mMovesText.setGravity(Gravity.CENTER);
269
    mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
270
    mMovesText.setText(act.getString(R.string.mo_placeholder,mObjectOrdinal+1,numObjects));
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

    
275
  void onAttachedToWindow(final ConfigActivity act, final int objectOrdinal)
276
    {
277
    int numObjects = RubikObjectList.getNumObjects();
278
    int width = act.getScreenWidthInPixels();
279
    int height= act.getScreenHeightInPixels();
280
    mBarHeight = act.getHeightBar();
281
    mButtonSize = height*TEXT_SIZE;
282
    mObjectOrdinal = objectOrdinal;
283

    
284
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
285
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams(width/2, LinearLayout.LayoutParams.MATCH_PARENT);
286
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams(width/4, LinearLayout.LayoutParams.MATCH_PARENT);
287

    
288
    LinearLayout layoutLeft = new LinearLayout(act);
289
    layoutLeft.setLayoutParams(paramsL);
290
    LinearLayout layoutMid  = new LinearLayout(act);
291
    layoutMid.setLayoutParams(paramsM);
292
    LinearLayout layoutRight= new LinearLayout(act);
293
    layoutRight.setLayoutParams(paramsR);
294

    
295
    setupObjectButton(act,width,height);
296
    setupPrevButton(act);
297
    setupNextButton(act);
298
    setupTextView(act,height,numObjects);
299
    setupBackButton(act);
300

    
301
    layoutLeft.addView(mObjectButton);
302
    layoutMid.addView(mPrevButton);
303
    layoutMid.addView(mMovesText);
304
    layoutMid.addView(mNextButton);
305
    layoutRight.addView(mBackButton);
306

    
307
    LinearLayout layout = act.findViewById(R.id.lowerBar);
308
    layout.removeAllViews();
309
    layout.addView(layoutLeft);
310
    layout.addView(layoutMid);
311
    layout.addView(layoutRight);
312

    
313
    mPane = new ConfigScreenPane(act,mObjectOrdinal);
314
    }
315
}
(4-4/6)