Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialogs / RubikDialogPatternView.java @ 66e777b0

1 e108b57e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 1f9772f3 Leszek Koltunski
package org.distorted.dialogs;
21 e108b57e Leszek Koltunski
22
import android.content.Context;
23 66e777b0 Leszek Koltunski
import androidx.fragment.app.FragmentActivity;
24 e108b57e Leszek Koltunski
import android.util.AttributeSet;
25
import android.util.DisplayMetrics;
26
import android.view.View;
27 3a9d19ed Leszek Koltunski
import android.widget.AdapterView;
28
import android.widget.ArrayAdapter;
29 e108b57e Leszek Koltunski
import android.widget.Button;
30
import android.widget.FrameLayout;
31
import android.widget.LinearLayout;
32 c715128d Leszek Koltunski
import android.widget.ScrollView;
33 3a9d19ed Leszek Koltunski
import android.widget.Spinner;
34 e108b57e Leszek Koltunski
35 1f9772f3 Leszek Koltunski
import org.distorted.main.R;
36
import org.distorted.main.RubikActivity;
37
import org.distorted.objects.RubikObjectList;
38 3a9d19ed Leszek Koltunski
import org.distorted.patterns.RubikPattern;
39 b498f3f6 Leszek Koltunski
import org.distorted.patterns.RubikPatternList;
40 1f9772f3 Leszek Koltunski
import org.distorted.states.RubikState;
41
import org.distorted.states.RubikStatePattern;
42 e108b57e Leszek Koltunski
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45 3a9d19ed Leszek Koltunski
public class RubikDialogPatternView extends FrameLayout implements AdapterView.OnItemSelectedListener
46 e108b57e Leszek Koltunski
  {
47
  private LinearLayout mLayout;
48 c715128d Leszek Koltunski
  private ScrollView mScroll;
49 e108b57e Leszek Koltunski
  private RubikDialogPattern mDialog;
50 044529c1 Leszek Koltunski
  private int mTab, mPos;
51 e108b57e Leszek Koltunski
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
54
  public RubikDialogPatternView(Context context, AttributeSet attrs, int defStyle)
55
    {
56
    super(context, attrs, defStyle);
57
    }
58
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61
  public RubikDialogPatternView(Context context, AttributeSet attrs)
62
    {
63
    super(context, attrs);
64
    }
65
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68 3a9d19ed Leszek Koltunski
  public RubikDialogPatternView(FragmentActivity act, RubikDialogPattern dialog, int position)
69 e108b57e Leszek Koltunski
    {
70
    super(act);
71
72
    mDialog = dialog;
73 044529c1 Leszek Koltunski
    mTab = position;
74 e108b57e Leszek Koltunski
    View tab = inflate( act, R.layout.dialog_pattern_tab, null);
75
    mLayout = tab.findViewById(R.id.tabLayout);
76 c715128d Leszek Koltunski
    mScroll = tab.findViewById(R.id.tabScrollView);
77 3a9d19ed Leszek Koltunski
78
    String[] categories = createCategories();
79
80
    Spinner spinner = tab.findViewById(R.id.pattern_category_spinner);
81
    spinner.setOnItemSelectedListener(this);
82
83
    ArrayAdapter<String> adapter = new ArrayAdapter<>(act, android.R.layout.simple_spinner_item, categories);
84
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
85
    spinner.setAdapter(adapter);
86
87 044529c1 Leszek Koltunski
    RubikPattern pattern = RubikPattern.getInstance();
88
    spinner.setSelection(pattern.recallCategory(mTab));
89 e108b57e Leszek Koltunski
90
    addView(tab);
91
    }
92
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
95 3a9d19ed Leszek Koltunski
  private String[] createCategories()
96
    {
97
    RubikPattern pattern = RubikPattern.getInstance();
98 044529c1 Leszek Koltunski
    int numCat = pattern.getNumCategories(mTab);
99 3a9d19ed Leszek Koltunski
100
    String[] ret = new String[numCat];
101
102
    for(int i=0; i<numCat; i++)
103
      {
104 044529c1 Leszek Koltunski
      ret[i] = pattern.getCategoryName(mTab,i);
105 3a9d19ed Leszek Koltunski
      }
106
107
    return ret;
108
    }
109
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
112
  private String[] createPatterns(int category)
113 e108b57e Leszek Koltunski
    {
114 3a9d19ed Leszek Koltunski
    RubikPattern pattern = RubikPattern.getInstance();
115 044529c1 Leszek Koltunski
    int numPat = pattern.getNumPatterns(mTab, category);
116 3a9d19ed Leszek Koltunski
117
    String[] ret = new String[numPat];
118
119
    for(int i=0; i<numPat; i++)
120
      {
121 044529c1 Leszek Koltunski
      ret[i] = pattern.getPatternName(mTab, category, i);
122 3a9d19ed Leszek Koltunski
      }
123
124
    return ret;
125
    }
126
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
129 e0e84674 Leszek Koltunski
  private void fillPatterns(final int category)
130 3a9d19ed Leszek Koltunski
    {
131
    final RubikActivity act = (RubikActivity)getContext();
132
133 e108b57e Leszek Koltunski
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
134
    final float scale = metrics.density;
135
    int margin = (int)(3*scale + 0.5f);
136
    LinearLayout.LayoutParams bParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
137
    bParams.setMargins(margin, margin, margin, margin);
138
139 3a9d19ed Leszek Koltunski
    final String[] patterns = createPatterns(category);
140
    int len = patterns.length;
141 c715128d Leszek Koltunski
    final RubikPattern pattern = RubikPattern.getInstance();
142 e108b57e Leszek Koltunski
143 3a9d19ed Leszek Koltunski
    mLayout.removeAllViews();
144
145
    for(int i=0; i<len; i++)
146 e108b57e Leszek Koltunski
      {
147 3a9d19ed Leszek Koltunski
      final int ii = i;
148 e108b57e Leszek Koltunski
      Button button = new Button(act);
149
      button.setLayoutParams(bParams);
150 3a9d19ed Leszek Koltunski
      button.setText(patterns[i]);
151 e108b57e Leszek Koltunski
152
      button.setOnClickListener( new View.OnClickListener()
153
        {
154
        @Override
155
        public void onClick(View view)
156
          {
157 c715128d Leszek Koltunski
          int[][] moves = pattern.reInitialize(mTab, category, ii);
158 b498f3f6 Leszek Koltunski
159
          RubikObjectList list = RubikPatternList.getObject(mTab);
160
          int size             = RubikPatternList.getSize(mTab);
161
162
          act.changeObject( list, size, moves);
163
164 6f2a942e Leszek Koltunski
          RubikStatePattern state = (RubikStatePattern) RubikState.PATT.getStateClass();
165
          state.setPattern(act, mTab, category, ii);
166 c715128d Leszek Koltunski
          mDialog.rememberState();
167 e108b57e Leszek Koltunski
          mDialog.dismiss();
168
          }
169
        });
170
171
      mLayout.addView(button);
172
      }
173
    }
174 3a9d19ed Leszek Koltunski
175 044529c1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
176
177
  int getCurrentCategory()
178
    {
179
    return mPos;
180
    }
181
182 c715128d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
183
184
  int getCurrentScrollPos()
185
    {
186
    return mScroll.getScrollY();
187
    }
188
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
191
  @Override
192
  protected void onLayout(boolean changed, int left, int top, int right, int bottom)
193
    {
194
    super.onLayout(changed,left,top,right,bottom);
195
196
    if( !changed )
197
      {
198
      final RubikPattern pattern = RubikPattern.getInstance();
199
      mScroll.setScrollY( pattern.recallScrollPos(mTab) );
200
      }
201
    }
202
203 3a9d19ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
204
205
  @Override
206
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
207
    {
208
    if( parent.getId() == R.id.pattern_category_spinner )
209
      {
210 044529c1 Leszek Koltunski
      mPos = pos;
211 3a9d19ed Leszek Koltunski
      fillPatterns(pos);
212
      }
213
    }
214
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217
  @Override
218
  public void onNothingSelected(AdapterView<?> parent)
219
    {
220
221
    }
222 e108b57e Leszek Koltunski
  }