Project

General

Profile

« Previous | Next » 

Revision def32b2c

Added by Leszek Koltunski 4 months ago

Progress with sorting the objects by various criteria.

View differences:

src/main/java/org/distorted/config/ConfigScreen.java
20 20
import android.widget.ScrollView;
21 21
import android.widget.TextView;
22 22

  
23
import org.distorted.helpers.PopupCreator;
23
import org.distorted.helpers.ObjectGridCreator;
24 24

  
25 25
import org.distorted.helpers.TransparentImageButton;
26 26
import org.distorted.main.MainActivity;
......
69 69
    ScrollView view = (ScrollView)inflate( act, R.layout.popup_object_simple, null);
70 70
    GridLayout objectGrid = view.findViewById(R.id.objectGrid);
71 71

  
72
    PopupCreator.createObjectGrid(objectGrid,act,mRowCount,mColCount,numObjects,margin,cubeSize,padding);
72
    ObjectGridCreator.createObjectGridClassic(objectGrid,act,mRowCount,mColCount,numObjects,margin,cubeSize,padding);
73 73

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

  
81 81
      button.setOnClickListener( new View.OnClickListener()
src/main/java/org/distorted/helpers/ObjectGridCreator.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 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.helpers;
11

  
12
import android.app.Activity;
13
import android.content.Context;
14
import android.view.LayoutInflater;
15
import android.view.View;
16
import android.view.ViewGroup;
17
import android.widget.GridLayout;
18
import android.widget.ImageButton;
19
import android.widget.ImageView;
20
import android.widget.LinearLayout;
21
import android.widget.ScrollView;
22

  
23
import org.distorted.main.MainSettingsPopup;
24
import org.distorted.main.R;
25
import org.distorted.objects.RubikObject;
26
import org.distorted.objects.RubikObjectList;
27

  
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

  
30
public class ObjectGridCreator
31
  {
32
  private GridLayout mGrid;
33
  private int mSortMode;
34

  
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

  
37
  public View getChildAt(int index)
38
    {
39
    if( mSortMode==MainSettingsPopup.SORT_CLASSIC )
40
      {
41
      return mGrid.getChildAt(index);
42
      }
43
    else
44
      {
45
      return null;
46
      }
47
    }
48

  
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

  
51
  public void createObjectGrid(ScrollView scrollView, Activity act, int rowCount, int colCount, int numObjects, int margin, int size, int pad, int mode)
52
    {
53
    mSortMode = mode;
54

  
55
    if( mode==MainSettingsPopup.SORT_CLASSIC )
56
      {
57
      mGrid = new GridLayout(act);
58
      ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
59
      mGrid.setLayoutParams(params);
60
      scrollView.addView(mGrid);
61

  
62
      createObjectGridClassic(mGrid,act,rowCount,colCount,numObjects,margin,size,pad);
63
      }
64
    else
65
      {
66
      LinearLayout layout = new LinearLayout(act);
67
      ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
68
      layout.setLayoutParams(params);
69
      layout.setOrientation(LinearLayout.VERTICAL);
70
      scrollView.addView(layout);
71

  
72

  
73

  
74
      }
75
    }
76

  
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

  
79
  public static void createObjectGridClassic(GridLayout grid, Activity act, int rowCount, int colCount, int numObjects, int margin, int size, int pad)
80
    {
81
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[rowCount];
82
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[colCount];
83

  
84
    grid.setColumnCount(colCount);
85
    grid.setRowCount(rowCount);
86

  
87
    int[] nextInRow = new int[rowCount];
88

  
89
    for(int row=0; row<rowCount; row++)
90
      {
91
      rowSpecs[row] = GridLayout.spec(row);
92
      nextInRow[row]= 0;
93
      }
94
    for(int col=0; col<colCount; col++)
95
      {
96
      colSpecs[col] = GridLayout.spec(col);
97
      }
98

  
99
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
100

  
101
    for(int object=0; object<numObjects; object++)
102
      {
103
      View v = createView(act,layoutInflater,object,pad);
104
      int row = object/colCount;
105

  
106
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
107
      params.bottomMargin = margin;
108
      params.topMargin    = margin;
109
      params.leftMargin   = margin;
110
      params.rightMargin  = margin;
111

  
112
      params.width = size;
113
      params.height= size;
114

  
115
      nextInRow[row]++;
116

  
117
      grid.addView(v,params);
118
      }
119
    }
120

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

  
123
  public static ImageButton getButton(RubikObject object, View view)
124
    {
125
    return object!=null && object.isFree() ? (ImageButton)view : view.findViewById(R.id.non_free_button);
126
    }
127

  
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

  
130
  private static View createView(Activity act, LayoutInflater inflater, int ordinal, int pad)
131
    {
132
    final RubikObject obj = RubikObjectList.getObject(ordinal);
133

  
134
    if( obj!=null )
135
      {
136
      if( obj.isFree() )
137
        {
138
        ImageButton button = new ImageButton(act);
139
        obj.setIconTo(act,button);
140
        return button;
141
        }
142
      else
143
        {
144
        final View layout = inflater.inflate(R.layout.non_free_object, null);
145
        ImageButton button= layout.findViewById(R.id.non_free_button);
146
        obj.setIconTo(act,button);
147
        ImageView view= layout.findViewById(R.id.non_free_lock);
148
        view.setPadding(0,pad,0,0);
149
        return layout;
150
        }
151
      }
152

  
153
    return null;
154
    }
155
  }
src/main/java/org/distorted/helpers/PopupCreator.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 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.helpers;
11

  
12
import android.app.Activity;
13
import android.content.Context;
14
import android.view.LayoutInflater;
15
import android.view.View;
16
import android.widget.GridLayout;
17
import android.widget.ImageButton;
18
import android.widget.ImageView;
19

  
20
import org.distorted.main.R;
21
import org.distorted.objects.RubikObject;
22
import org.distorted.objects.RubikObjectList;
23

  
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25

  
26
public class PopupCreator
27
  {
28
  public static void createObjectGrid(GridLayout grid, Activity act, int rowCount, int colCount, int numObjects, int margin, int size, int pad)
29
    {
30
    GridLayout.Spec[] rowSpecs = new GridLayout.Spec[rowCount];
31
    GridLayout.Spec[] colSpecs = new GridLayout.Spec[colCount];
32

  
33
    grid.setColumnCount(colCount);
34
    grid.setRowCount(rowCount);
35

  
36
    int[] nextInRow = new int[rowCount];
37

  
38
    for(int row=0; row<rowCount; row++)
39
      {
40
      rowSpecs[row] = GridLayout.spec(row);
41
      nextInRow[row]= 0;
42
      }
43
    for(int col=0; col<colCount; col++)
44
      {
45
      colSpecs[col] = GridLayout.spec(col);
46
      }
47

  
48
    LayoutInflater layoutInflater = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
49

  
50
    for(int object=0; object<numObjects; object++)
51
      {
52
      View v = createView(act,layoutInflater,object,pad);
53
      int row = object/colCount;
54

  
55
      GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpecs[row],colSpecs[nextInRow[row]]);
56
      params.bottomMargin = margin;
57
      params.topMargin    = margin;
58
      params.leftMargin   = margin;
59
      params.rightMargin  = margin;
60

  
61
      params.width = size;
62
      params.height= size;
63

  
64
      nextInRow[row]++;
65

  
66
      grid.addView(v,params);
67
      }
68
    }
69

  
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

  
72
  public static ImageButton getButton(RubikObject object, View view)
73
    {
74
    return object!=null && object.isFree() ? (ImageButton)view : view.findViewById(R.id.non_free_button);
75
    }
76

  
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

  
79
  private static View createView(Activity act, LayoutInflater inflater, int ordinal, int pad)
80
    {
81
    final RubikObject obj = RubikObjectList.getObject(ordinal);
82

  
83
    if( obj!=null )
84
      {
85
      if( obj.isFree() )
86
        {
87
        ImageButton button = new ImageButton(act);
88
        obj.setIconTo(act,button);
89
        return button;
90
        }
91
      else
92
        {
93
        final View layout = inflater.inflate(R.layout.non_free_object, null);
94
        ImageButton button= layout.findViewById(R.id.non_free_button);
95
        obj.setIconTo(act,button);
96
        ImageView view= layout.findViewById(R.id.non_free_lock);
97
        view.setPadding(0,pad,0,0);
98
        return layout;
99
        }
100
      }
101

  
102
    return null;
103
    }
104
  }
src/main/java/org/distorted/main/MainActivity.java
71 71
    private int mNumUpdates;
72 72
    private int mCurrentObject;
73 73
    private MainScrollGrid mGrid;
74
    private int mSortMode;
74 75

  
75 76
///////////////////////////////////////////////////////////////////////////////////////////////////
76 77

  
......
160 161

  
161 162
      getWindowWidth(getResources().getConfiguration());
162 163
      mGrid = new MainScrollGrid();
163
      mGrid.createGrid(this,mScreenWidth);
164
      mGrid.createGrid(this,mScreenWidth,mSortMode);
164 165

  
165 166
      if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.P )
166 167
        {
......
304 305
      SharedPreferences.Editor editor = preferences.edit();
305 306

  
306 307
      editor.putString("appVersion", mCurrVersion );
308
      editor.putInt("sortMode", mSortMode);
307 309

  
308 310
      RubikObjectList.savePreferences(editor);
309 311

  
......
319 321
    private void restorePreferences(SharedPreferences preferences, boolean justStarted)
320 322
      {
321 323
      mOldVersion = preferences.getString("appVersion","");
324
      mSortMode = preferences.getInt("sortMode", MainSettingsPopup.SORT_CATEGORY);
322 325

  
323 326
      RubikObjectList.restorePreferences(this,preferences,justStarted);
324 327
      RubikScores scores = RubikScores.getInstance();
......
522 525
      return 0;
523 526
      }
524 527

  
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529

  
530
    public void sortObjectsBy(int sortMode)
531
      {
532
      mSortMode = sortMode;
533
      mGrid.createGrid(this,mScreenWidth,mSortMode);
534
      }
535

  
525 536
///////////////////////////////////////////////////////////////////////////////////////////////////
526 537

  
527 538
    public void errorUpdate()
src/main/java/org/distorted/main/MainScrollGrid.java
11 11

  
12 12
import android.util.DisplayMetrics;
13 13
import android.view.View;
14
import android.widget.GridLayout;
15 14
import android.widget.ImageButton;
15
import android.widget.ScrollView;
16 16

  
17
import org.distorted.helpers.PopupCreator;
17
import org.distorted.helpers.ObjectGridCreator;
18 18
import org.distorted.objects.RubikObject;
19 19
import org.distorted.objects.RubikObjectList;
20 20

  
......
26 26
  public static final float POPUP_PADDING= 0.035f;
27 27
  public static final float POPUP_MARGIN = 0.024f;
28 28

  
29
  private int mSortMode = -1;
30
  private ObjectGridCreator mCreator;
31

  
29 32
///////////////////////////////////////////////////////////////////////////////////////////////////
30 33

  
31
  void createGrid(final MainActivity act, int windowWidth)
34
  void createGrid(final MainActivity act, int windowWidth, int sortMode)
32 35
    {
36
    if( mCreator==null ) mCreator = new ObjectGridCreator();
37

  
38
    mSortMode = sortMode;
39

  
33 40
    int numObjects = RubikObjectList.getNumObjects();
34 41
    int rowCount   = (numObjects + NUM_COLUMNS-1) / NUM_COLUMNS;
35 42
    int colCount   = NUM_COLUMNS;
......
38 45
    int padding    = (int)(windowWidth*POPUP_PADDING);
39 46
    int cubeSize   = objectSize - 2*margin;
40 47

  
41
    GridLayout objectGrid = act.findViewById(R.id.objectGrid);
42
    objectGrid.removeAllViews();
48
    ScrollView scrollView = act.findViewById(R.id.objectScroll);
49
    scrollView.removeAllViews();
43 50

  
44
    PopupCreator.createObjectGrid(objectGrid,act,rowCount,colCount,numObjects,margin,cubeSize,padding);
51
    mCreator.createObjectGrid(scrollView,act,rowCount,colCount,numObjects,margin,cubeSize,padding,sortMode);
45 52

  
46 53
    DisplayMetrics displaymetrics = new DisplayMetrics();
47 54
    act.getWindowManager().getDefaultDisplay().getRealMetrics(displaymetrics);
......
49 56
    for(int child=0; child<numObjects; child++)
50 57
      {
51 58
      final RubikObject obj = RubikObjectList.getObject(child);
52
      View v = objectGrid.getChildAt(child);
53
      ImageButton button = PopupCreator.getButton(obj,v);
59
      View v = mCreator.getChildAt(child);
60
      ImageButton button = ObjectGridCreator.getButton(obj,v);
54 61
      final int ordinal = child;
55 62

  
56 63
      button.setOnClickListener( new View.OnClickListener()
......
77 84
      @Override
78 85
      public void run()
79 86
        {
80
        createGrid(act,scrW);
87
        if( mSortMode<0 ) mSortMode = MainSettingsPopup.SORT_CATEGORY;
88
        createGrid(act,scrW,mSortMode);
81 89
        }
82 90
      });
83 91
    }
src/main/java/org/distorted/main/MainSettingsPopup.java
22 22
import android.widget.Spinner;
23 23
import android.widget.TextView;
24 24

  
25
import java.lang.ref.WeakReference;
25 26
import java.lang.reflect.Field;
26 27

  
27 28
///////////////////////////////////////////////////////////////////////////////////////////////////
28 29

  
29 30
public class MainSettingsPopup implements AdapterView.OnItemSelectedListener
30 31
  {
32
  public static final int SORT_CLASSIC    = 0;
33
  public static final int SORT_CATEGORY   = 1;
34
  public static final int SORT_DIFFICULTY = 2;
35
  public static final int SORT_AUTHOR     = 3;
36
  public static final int SORT_YEAR       = 4;
37

  
38
  private static final String[] mSortNames = { "Classic" , "Category" , "Difficulty", "Author" , "Year" };
31 39
  private static final float MENU_TITLE_SIZE= 0.070f;
32 40
  private static final float MENU_TEXT_SIZE = 0.060f;
41
  private static final int[] mLocation = new int[2];
33 42

  
34 43
  private final PopupWindow mPopup;
35
  private static final int[] mLocation = new int[2];
44
  private final WeakReference<MainActivity> mAct;
36 45
  private int mCurrMethod;
37 46

  
38 47
///////////////////////////////////////////////////////////////////////////////////////////////////
......
73 82

  
74 83
  MainSettingsPopup(MainActivity act, int width, int height)
75 84
    {
85
    mAct = new WeakReference<>(act);
86

  
76 87
    // due to bugs in Android API <=25, a Spinner inside a PopupWindow will crash once you click on it.
77 88
    // solution: on those APIs, use a special Spinner in dialog mode (this does not crash)
78 89
    int id = android.os.Build.VERSION.SDK_INT <= 25 ? R.layout.settings_popup_android25 : R.layout.settings_popup;
......
97 108

  
98 109
    mCurrMethod = -1;
99 110

  
100
    String[] actNames = { "Classic" , "Category" , "Difficulty", "Author" , "Year" };
101
    ArrayAdapter<String> actAdapter = new ArrayAdapter<>(act, android.R.layout.simple_spinner_item, actNames);
111
    ArrayAdapter<String> actAdapter = new ArrayAdapter<>(act, android.R.layout.simple_spinner_item, mSortNames);
102 112
    actAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
103 113
    actSpinner.setAdapter(actAdapter);
104 114
    }
......
158 168
    if( parent.getId()==R.id.sortMethod && mCurrMethod!=pos )
159 169
      {
160 170
      mCurrMethod = pos;
171
      MainActivity act = mAct.get();
172
      act.sortObjectsBy(pos);
161 173
      }
162 174
    }
163 175

  
src/main/res/layout/main.xml
94 94
        android:layout_height="0dp"
95 95
        android:layout_weight="0.84"
96 96
        android:background="@color/grey">
97

  
98
        <GridLayout
99
            android:id="@+id/objectGrid"
100
            android:layout_width="match_parent"
101
            android:layout_height="wrap_content">
102
        </GridLayout>
103

  
104 97
    </ScrollView>
105 98

  
106 99
    <LinearLayout

Also available in: Unified diff