Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigScreen.java @ 1fa599b8

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.R;
27
import org.distorted.main.RubikActivity;
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

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

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

    
61
    int cubeSize  = (int)( (Math.min(width,(int)(height*0.7f))) / 9 );
62
    int margin    = (int)(height*RubikActivity.POPUP_MARGIN);
63
    int padding   = (int)(height*RubikActivity.POPUP_PADDING);
64
    mObjectSize   = (int)(cubeSize + 2*margin + 0.5f);
65
    mMaxRowCount  = (int)((height-mBarHeight)/mObjectSize);
66

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

    
70
    PopupCreator.createObjectGrid(objectGrid,act,mRowCount,mColCount,numObjects,margin,cubeSize,padding);
71

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

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

    
92
          mObjectPopup.dismiss();
93
          }
94
        });
95
      }
96

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

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

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

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

    
122
  private void setupObjectButton(final ConfigActivity act, final int width, final int height)
123
    {
124
    final int margin= (int)(height*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
          setupObjectWindow(act,width,height);
137
          }
138

    
139
        int rowCount = Math.min(mMaxRowCount,mRowCount);
140
        View popupView = mObjectPopup.getContentView();
141
        popupView.setSystemUiVisibility(RubikActivity.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
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
219
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
220
    mPrevButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
221

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

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

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

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

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

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

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

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

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

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

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

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

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

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

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