Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import android.widget.AdapterView;
28
import android.widget.ArrayAdapter;
29
import android.widget.Button;
30
import android.widget.FrameLayout;
31
import android.widget.LinearLayout;
32
import android.widget.Spinner;
33

    
34
import org.distorted.magic.R;
35
import org.distorted.magic.RubikActivity;
36
import org.distorted.object.RubikObjectList;
37
import org.distorted.patterns.RubikPattern;
38
import org.distorted.uistate.RubikState;
39
import org.distorted.uistate.RubikStatePattern;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
public class RubikDialogPatternView extends FrameLayout implements AdapterView.OnItemSelectedListener
44
  {
45
  private LinearLayout mLayout;
46
  private RubikDialogPattern mDialog;
47
  private int mTab, mPos;
48

    
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
  public RubikDialogPatternView(FragmentActivity act, RubikDialogPattern dialog, int position)
66
    {
67
    super(act);
68

    
69
    mDialog = dialog;
70
    mTab = position;
71
    View tab = inflate( act, R.layout.dialog_pattern_tab, null);
72
    mLayout = tab.findViewById(R.id.tabLayout);
73

    
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
    RubikPattern pattern = RubikPattern.getInstance();
84
    spinner.setSelection(pattern.recallCategory(mTab));
85

    
86
    addView(tab);
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  private String[] createCategories()
92
    {
93
    RubikPattern pattern = RubikPattern.getInstance();
94
    int numCat = pattern.getNumCategories(mTab);
95

    
96
    String[] ret = new String[numCat];
97

    
98
    for(int i=0; i<numCat; i++)
99
      {
100
      ret[i] = pattern.getCategoryName(mTab,i);
101
      }
102

    
103
    return ret;
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
  private String[] createPatterns(int category)
109
    {
110
    RubikPattern pattern = RubikPattern.getInstance();
111
    int numPat = pattern.getNumPatterns(mTab, category);
112

    
113
    String[] ret = new String[numPat];
114

    
115
    for(int i=0; i<numPat; i++)
116
      {
117
      ret[i] = pattern.getPatternName(mTab, category, i);
118
      }
119

    
120
    return ret;
121
    }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
  private void fillPatterns(final int category)
126
    {
127
    final RubikActivity act = (RubikActivity)getContext();
128

    
129
    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
    final String[] patterns = createPatterns(category);
136
    int len = patterns.length;
137

    
138
    mLayout.removeAllViews();
139

    
140
    for(int i=0; i<len; i++)
141
      {
142
      final int ii = i;
143
      Button button = new Button(act);
144
      button.setLayoutParams(bParams);
145
      button.setText(patterns[i]);
146

    
147
      button.setOnClickListener( new View.OnClickListener()
148
        {
149
        @Override
150
        public void onClick(View view)
151
          {
152
          RubikStatePattern state = (RubikStatePattern) RubikState.PATT.getStateClass();
153
          state.setPattern(act, mTab, category, ii);
154
          int[] sizes = RubikObjectList.CUBE.getSizes();
155
          RubikPattern pattern = RubikPattern.getInstance();
156
          int[][] moves = pattern.getMoves(mTab, category, ii);
157
          act.changeObject(RubikObjectList.CUBE,sizes[mTab],moves);
158
          mDialog.rememberCategories();
159
          mDialog.dismiss();
160
          }
161
        });
162

    
163
      mLayout.addView(button);
164
      }
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  int getCurrentCategory()
170
    {
171
    return mPos;
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
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
      mPos = pos;
182
      fillPatterns(pos);
183
      }
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  @Override
189
  public void onNothingSelected(AdapterView<?> parent)
190
    {
191

    
192
    }
193
  }
(7-7/12)