Project

General

Profile

« Previous | Next » 

Revision e07c48a2

Added by Leszek Koltunski about 4 years ago

Make the ObjectPopup a 2D grid.

View differences:

src/main/java/org/distorted/objects/RubikObjectList.java
74 74
  private final RubikMovement mObjectMovementClass;
75 75
  private static final RubikObjectList[] objects;
76 76
  private static int mNumAll;
77
  private static int[] mIndices;
77 78

  
78 79
  static
79 80
    {
......
84 85
    int maxLevel= Integer.MIN_VALUE;
85 86
    int maxSize = Integer.MIN_VALUE;
86 87

  
88
    mIndices = new int[] {0,1,2};
89

  
87 90
    for(RubikObjectList object: RubikObjectList.values())
88 91
      {
89 92
      objects[i] = object;
......
104 107
    MAX_OBJECT_SIZE = maxSize;
105 108
    }
106 109

  
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
// TODO
112
  public static int getColumnCount()
113
    {
114
    return 3;
115
    }
116

  
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
// TODO
119
  public static int getRowCount()
120
    {
121
    return 4;
122
    }
123

  
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
// TODO
126
  public static int[] getIndices()
127
    {
128
    return mIndices;
129
    }
130

  
107 131
///////////////////////////////////////////////////////////////////////////////////////////////////
108 132

  
109 133
  public static RubikObjectList getObject(int ordinal)
src/main/java/org/distorted/states/RubikStatePlay.java
33 33
import android.widget.AdapterView;
34 34
import android.widget.ArrayAdapter;
35 35
import android.widget.Button;
36
import android.widget.GridLayout;
36 37
import android.widget.ImageButton;
37 38
import android.widget.LinearLayout;
38 39
import android.widget.PopupWindow;
......
69 70
  private PopupWindow mObjectPopup, mMenuPopup;
70 71
  private int mObject = DEF_OBJECT;
71 72
  private int mSize   = DEF_SIZE;
72
  private int mObjectLayoutWidth, mMenuLayoutWidth, mMenuLayoutHeight;
73
  private LinearLayout mObjectLayout, mMenuLayout;
73
  private int mObjectSize, mMenuLayoutWidth, mMenuLayoutHeight;
74
  private GridLayout mObjectGrid;
74 75
  private AppCompatSpinner mLevelSpinner;
75 76
  private ArrayAdapter<String> mSpinnerAdapter;
76 77
  private int mLevelValue;
77 78
  private int mBarHeight;
78
  private float mButtonSize, mTitleSize, mMenuItemSize, mMenuTextSize;
79
  private float mButtonSize, mMenuItemSize, mMenuTextSize;
80
  private int mColCount, mRowCount;
79 81

  
80 82
  private ArrayList<Move> mMoves;
81 83
  private boolean mCanPrevMove;
......
106 108
    float width = act.getScreenWidthInPixels();
107 109
    mMenuTextSize = width*RubikActivity.MENU_MEDIUM_TEXT_SIZE;
108 110
    mButtonSize   = width*RubikActivity.BUTTON_TEXT_SIZE;
109
    mTitleSize    = width*RubikActivity.TITLE_TEXT_SIZE;
110 111
    mMenuItemSize = width*RubikActivity.MENU_ITEM_SIZE;
111 112

  
112 113
    mCanPrevMove = true;
......
114 115
    if( mMoves==null ) mMoves = new ArrayList<>();
115 116
    else               mMoves.clear();
116 117

  
118
    mRowCount = RubikObjectList.getRowCount();
119
    mColCount = RubikObjectList.getColumnCount();
120

  
117 121
    // TOP ////////////////////////////
118 122
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
119 123
    layoutTop.removeAllViews();
......
147 151

  
148 152
  private void setupObjectButton(final RubikActivity act, final float width)
149 153
    {
150
    int padding = (int)(width*RubikActivity.PADDING);
151
    int margin  = (int)(width*RubikActivity.MARGIN);
152
    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);
154
    final int padding = (int)(width*RubikActivity.PADDING);
155
    final int margin  = (int)(width*RubikActivity.MARGIN);
156
    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);
153 157

  
154 158
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT, 1.2f);
155 159
    params.topMargin    = margin;
......
169 173
        {
170 174
        if( act.getPreRender().canPlay() )
171 175
          {
172
          int total = RubikObjectList.getTotal();
173 176
          boolean vertical = act.isVertical();
174
          mObjectLayout.setOrientation(vertical ? LinearLayout.VERTICAL:LinearLayout.HORIZONTAL);
175

  
176
          int width  = view.getWidth();
177
          int layhei = mObjectLayoutWidth * (vertical? total:1);
178
          int laywid = mObjectLayoutWidth * (vertical? 1:total);
179

  
180
          mObjectPopup.showAsDropDown(view, (width-laywid)/2, 0, Gravity.LEFT);
177
          mObjectGrid.setOrientation(vertical ? GridLayout.VERTICAL:GridLayout.HORIZONTAL);
178
          mObjectPopup.showAsDropDown(view, 0, margin, Gravity.LEFT);
181 179

  
182 180
          if( android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1 )
183 181
            {
182
            int layhei = mObjectSize * (vertical? mRowCount:mColCount);
183
            int laywid = mObjectSize * (vertical? mColCount:mRowCount);
184 184
            mObjectPopup.update(view, laywid, layhei);
185 185
            }
186 186
          }
......
373 373
    {
374 374
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
375 375
    final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
376
    mObjectLayout = layout.findViewById(R.id.popup);
376
    mObjectGrid = layout.findViewById(R.id.objectGrid);
377

  
378
    int[] indices = RubikObjectList.getIndices();
379

  
380
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[mRowCount];
381
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[mColCount];
382

  
383
    mObjectGrid.setColumnCount(mColCount);
384
    mObjectGrid.setRowCount(mRowCount);
385

  
386
    for(int row=0; row<mRowCount; row++)
387
      {
388
      rowSpecs[row] = GridLayout.spec(row);
389
      }
390
    for(int col=0; col<mColCount; col++)
391
      {
392
      colSpecs[col] = GridLayout.spec(col);
393
      }
377 394

  
378 395
    mObjectPopup = new PopupWindow(act);
379 396
    mObjectPopup.setContentView(layout);
380 397
    mObjectPopup.setFocusable(true);
381
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
382 398
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_cube2,R.drawable.ui_medium_cube2, R.drawable.ui_big_cube2, R.drawable.ui_huge_cube2);
383 399

  
384 400
    BitmapDrawable bd = (BitmapDrawable) act.getResources().getDrawable(icon);
385
    int cubeWidth  = bd.getIntrinsicWidth();
386
    mObjectLayoutWidth = (int)(cubeWidth + 2*margin + 0.5f);
387

  
388
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
389
    p.setMargins(margin, margin, margin, margin);
401
    int cubeWidth = bd.getIntrinsicWidth();
402
    int margin = (int)(width*RubikActivity.LARGE_MARGIN);
403
    mObjectSize = (int)(cubeWidth + 2*margin + 0.5f);
390 404

  
391 405
    for(int object=0; object<RubikObjectList.NUM_OBJECTS; object++)
392 406
      {
......
398 412

  
399 413
      for(int i=0; i<len; i++)
400 414
        {
401
        final int size = i;
415
        final int index = i;
402 416

  
403 417
        ImageButton button = new ImageButton(act);
404
        button.setLayoutParams(p);
405

  
406 418
        button.setBackgroundResource(icons[i]);
407 419
        button.setOnClickListener( new View.OnClickListener()
408 420
          {
......
412 424
            if( act.getPreRender().canPlay() && RubikState.getCurrentState()==RubikState.PLAY )
413 425
              {
414 426
              mObject = obj;
415
              mSize   = sizes[size];
416
              act.changeObject(list,sizes[size], true);
427
              mSize   = sizes[index];
428
              act.changeObject(list,sizes[index], true);
417 429
              adjustSpinner(act);
418 430
              mMoves.clear();
419 431
              }
......
422 434
            }
423 435
          });
424 436

  
425
        mObjectLayout.addView(button);
437
        GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[index],colSpecs[indices[object]]);
438
        params.bottomMargin = margin;
439
        params.topMargin    = margin;
440
        params.leftMargin   = margin;
441
        params.rightMargin  = margin;
442

  
443
        mObjectGrid.addView(button, params);
426 444
        }
427 445
      }
428 446
    }
......
432 450
  private void setupMenuWindow(final RubikActivity act, final float width)
433 451
    {
434 452
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
435
    final View layout = layoutInflater.inflate(R.layout.popup_objects, null);
436
    mMenuLayout = layout.findViewById(R.id.popup);
453
    final View layout = layoutInflater.inflate(R.layout.popup_menu, null);
454
    LinearLayout menuLayout = layout.findViewById(R.id.menuGrid);
437 455

  
438 456
    mMenuPopup = new PopupWindow(act);
439 457
    mMenuPopup.setContentView(layout);
......
464 482
          }
465 483
        });
466 484

  
467
      mMenuLayout.addView(button);
485
      menuLayout.addView(button);
468 486
      }
469 487

  
470 488
    mMenuLayoutWidth = (int)(width/2);
src/main/res/layout/popup_menu.xml
1
<?xml version="1.0" encoding="utf-8"?>
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
   android:id="@+id/menuGrid"
4
   android:layout_width="wrap_content"
5
   android:layout_height="wrap_content"
6
   android:orientation="vertical">
7
</LinearLayout>
src/main/res/layout/popup_objects.xml
1 1
<?xml version="1.0" encoding="utf-8"?>
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
   android:id="@+id/objectGrid"
3 4
   android:layout_width="wrap_content"
4
   android:id="@+id/popup"
5
   android:layout_height="wrap_content"
6
   android:orientation="vertical" >
7
</LinearLayout>
5
   android:layout_height="wrap_content">
6
</GridLayout>

Also available in: Unified diff