Project

General

Profile

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

magiccube / src / main / java / org / distorted / dialog / RubikDialogPatternView.java @ 6f2a942e

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