Project

General

Profile

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

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

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_old.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
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
107
    mBackButton = new TransparentImageButton(act,R.drawable.ui_smallback,params);
108

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

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

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

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

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

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

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

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

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

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

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

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

    
197
    act.changeObject(mObjectOrdinal);
198
    mPane.updatePane(act,mObjectOrdinal);
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

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

    
208
    act.changeObject(mObjectOrdinal);
209
    mPane.updatePane(act,mObjectOrdinal);
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

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

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

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

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

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

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

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

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

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

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

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

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

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

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

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

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