Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialog / RubikDialogPatternView.java @ c715128d

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
package org.distorted.dialog;
21
22
import android.content.Context;
23
import android.support.v4.app.FragmentActivity;
24
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
import org.distorted.magic.R;
36
import org.distorted.magic.RubikActivity;
37 044529c1 Leszek Koltunski
import org.distorted.object.RubikObjectList;
38 3a9d19ed Leszek Koltunski
import org.distorted.patterns.RubikPattern;
39 e108b57e Leszek Koltunski
import org.distorted.uistate.RubikState;
40 3a9d19ed Leszek Koltunski
import org.distorted.uistate.RubikStatePattern;
41 e108b57e Leszek Koltunski
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44 3a9d19ed Leszek Koltunski
public class RubikDialogPatternView extends FrameLayout implements AdapterView.OnItemSelectedListener
45 e108b57e Leszek Koltunski
  {
46
  private LinearLayout mLayout;
47 c715128d Leszek Koltunski
  private ScrollView mScroll;
48 e108b57e Leszek Koltunski
  private RubikDialogPattern mDialog;
49 044529c1 Leszek Koltunski
  private int mTab, mPos;
50 e108b57e Leszek Koltunski
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53
  public RubikDialogPatternView(Context context, AttributeSet attrs, int defStyle)
54
    {
55
    super(context, attrs, defStyle);
56
    }
57
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60
  public RubikDialogPatternView(Context context, AttributeSet attrs)
61
    {
62
    super(context, attrs);
63
    }
64
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67 3a9d19ed Leszek Koltunski
  public RubikDialogPatternView(FragmentActivity act, RubikDialogPattern dialog, int position)
68 e108b57e Leszek Koltunski
    {
69
    super(act);
70
71
    mDialog = dialog;
72 044529c1 Leszek Koltunski
    mTab = position;
73 e108b57e Leszek Koltunski
    View tab = inflate( act, R.layout.dialog_pattern_tab, null);
74
    mLayout = tab.findViewById(R.id.tabLayout);
75 c715128d Leszek Koltunski
    mScroll = tab.findViewById(R.id.tabScrollView);
76 3a9d19ed Leszek Koltunski
77
    String[] categories = createCategories();
78
79
    Spinner spinner = tab.findViewById(R.id.pattern_category_spinner);
80
    spinner.setOnItemSelectedListener(this);
81
82
    ArrayAdapter<String> adapter = new ArrayAdapter<>(act, android.R.layout.simple_spinner_item, categories);
83
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
84
    spinner.setAdapter(adapter);
85
86 044529c1 Leszek Koltunski
    RubikPattern pattern = RubikPattern.getInstance();
87
    spinner.setSelection(pattern.recallCategory(mTab));
88 e108b57e Leszek Koltunski
89
    addView(tab);
90
    }
91
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
94 3a9d19ed Leszek Koltunski
  private String[] createCategories()
95
    {
96
    RubikPattern pattern = RubikPattern.getInstance();
97 044529c1 Leszek Koltunski
    int numCat = pattern.getNumCategories(mTab);
98 3a9d19ed Leszek Koltunski
99
    String[] ret = new String[numCat];
100
101
    for(int i=0; i<numCat; i++)
102
      {
103 044529c1 Leszek Koltunski
      ret[i] = pattern.getCategoryName(mTab,i);
104 3a9d19ed Leszek Koltunski
      }
105
106
    return ret;
107
    }
108
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
111
  private String[] createPatterns(int category)
112 e108b57e Leszek Koltunski
    {
113 3a9d19ed Leszek Koltunski
    RubikPattern pattern = RubikPattern.getInstance();
114 044529c1 Leszek Koltunski
    int numPat = pattern.getNumPatterns(mTab, category);
115 3a9d19ed Leszek Koltunski
116
    String[] ret = new String[numPat];
117
118
    for(int i=0; i<numPat; i++)
119
      {
120 044529c1 Leszek Koltunski
      ret[i] = pattern.getPatternName(mTab, category, i);
121 3a9d19ed Leszek Koltunski
      }
122
123
    return ret;
124
    }
125
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
128 e0e84674 Leszek Koltunski
  private void fillPatterns(final int category)
129 3a9d19ed Leszek Koltunski
    {
130
    final RubikActivity act = (RubikActivity)getContext();
131
132 e108b57e Leszek Koltunski
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
133
    final float scale = metrics.density;
134
    int margin = (int)(3*scale + 0.5f);
135
    LinearLayout.LayoutParams bParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
136
    bParams.setMargins(margin, margin, margin, margin);
137
138 3a9d19ed Leszek Koltunski
    final String[] patterns = createPatterns(category);
139
    int len = patterns.length;
140 c715128d Leszek Koltunski
    final RubikPattern pattern = RubikPattern.getInstance();
141 e108b57e Leszek Koltunski
142 3a9d19ed Leszek Koltunski
    mLayout.removeAllViews();
143
144
    for(int i=0; i<len; i++)
145 e108b57e Leszek Koltunski
      {
146 3a9d19ed Leszek Koltunski
      final int ii = i;
147 e108b57e Leszek Koltunski
      Button button = new Button(act);
148
      button.setLayoutParams(bParams);
149 3a9d19ed Leszek Koltunski
      button.setText(patterns[i]);
150 e108b57e Leszek Koltunski
151
      button.setOnClickListener( new View.OnClickListener()
152
        {
153
        @Override
154
        public void onClick(View view)
155
          {
156 044529c1 Leszek Koltunski
          int[] sizes = RubikObjectList.CUBE.getSizes();
157 c715128d Leszek Koltunski
          int[][] moves = pattern.reInitialize(mTab, category, ii);
158 aa171dee Leszek Koltunski
          act.changeObject(RubikObjectList.CUBE,sizes[mTab],moves);
159 6f2a942e Leszek Koltunski
          RubikStatePattern state = (RubikStatePattern) RubikState.PATT.getStateClass();
160
          state.setPattern(act, mTab, category, ii);
161 c715128d Leszek Koltunski
          mDialog.rememberState();
162 e108b57e Leszek Koltunski
          mDialog.dismiss();
163
          }
164
        });
165
166
      mLayout.addView(button);
167
      }
168
    }
169 3a9d19ed Leszek Koltunski
170 044529c1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
171
172
  int getCurrentCategory()
173
    {
174
    return mPos;
175
    }
176
177 c715128d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
178
179
  int getCurrentScrollPos()
180
    {
181
    return mScroll.getScrollY();
182
    }
183
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
186
  @Override
187
  protected void onLayout(boolean changed, int left, int top, int right, int bottom)
188
    {
189
    super.onLayout(changed,left,top,right,bottom);
190
191
    if( !changed )
192
      {
193
      final RubikPattern pattern = RubikPattern.getInstance();
194
      mScroll.setScrollY( pattern.recallScrollPos(mTab) );
195
      }
196
    }
197
198 3a9d19ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
199
200
  @Override
201
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
202
    {
203
    if( parent.getId() == R.id.pattern_category_spinner )
204
      {
205 044529c1 Leszek Koltunski
      mPos = pos;
206 3a9d19ed Leszek Koltunski
      fillPatterns(pos);
207
      }
208
    }
209
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212
  @Override
213
  public void onNothingSelected(AdapterView<?> parent)
214
    {
215
216
    }
217 e108b57e Leszek Koltunski
  }