Project

General

Profile

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

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

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.ScrollView;
33
import android.widget.Spinner;
34

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

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

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

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

    
71
    mDialog = dialog;
72
    mTab = position;
73
    View tab = inflate( act, R.layout.dialog_pattern_tab, null);
74
    mLayout = tab.findViewById(R.id.tabLayout);
75
    mScroll = tab.findViewById(R.id.tabScrollView);
76

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

    
89
    addView(tab);
90
    }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
  private String[] createCategories()
95
    {
96
    RubikPattern pattern = RubikPattern.getInstance();
97
    int numCat = pattern.getNumCategories(mTab);
98

    
99
    String[] ret = new String[numCat];
100

    
101
    for(int i=0; i<numCat; i++)
102
      {
103
      ret[i] = pattern.getCategoryName(mTab,i);
104
      }
105

    
106
    return ret;
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  private String[] createPatterns(int category)
112
    {
113
    RubikPattern pattern = RubikPattern.getInstance();
114
    int numPat = pattern.getNumPatterns(mTab, category);
115

    
116
    String[] ret = new String[numPat];
117

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

    
123
    return ret;
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
  private void fillPatterns(final int category)
129
    {
130
    final RubikActivity act = (RubikActivity)getContext();
131

    
132
    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
    final String[] patterns = createPatterns(category);
139
    int len = patterns.length;
140
    final RubikPattern pattern = RubikPattern.getInstance();
141

    
142
    mLayout.removeAllViews();
143

    
144
    for(int i=0; i<len; i++)
145
      {
146
      final int ii = i;
147
      Button button = new Button(act);
148
      button.setLayoutParams(bParams);
149
      button.setText(patterns[i]);
150

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

    
166
      mLayout.addView(button);
167
      }
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  int getCurrentCategory()
173
    {
174
    return mPos;
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
      mPos = pos;
206
      fillPatterns(pos);
207
      }
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  @Override
213
  public void onNothingSelected(AdapterView<?> parent)
214
    {
215

    
216
    }
217
  }
(7-7/12)