Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStatePattern.java @ 11877284

1 4f470e00 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.uistate;
21
22
import android.content.SharedPreferences;
23 e108b57e Leszek Koltunski
import android.os.Bundle;
24
import android.support.v4.app.FragmentManager;
25 4f470e00 Leszek Koltunski
import android.util.DisplayMetrics;
26 11877284 Leszek Koltunski
import android.view.Gravity;
27 4f470e00 Leszek Koltunski
import android.view.LayoutInflater;
28 a6d3b158 Leszek Koltunski
import android.view.View;
29 4f470e00 Leszek Koltunski
import android.widget.Button;
30 0a57000c Leszek Koltunski
import android.widget.ImageButton;
31 4f470e00 Leszek Koltunski
import android.widget.LinearLayout;
32
import android.widget.TextView;
33
34 e108b57e Leszek Koltunski
import org.distorted.dialog.RubikDialogPattern;
35 4f470e00 Leszek Koltunski
import org.distorted.magic.R;
36
import org.distorted.magic.RubikActivity;
37 53f23b64 Leszek Koltunski
import org.distorted.object.RubikObjectList;
38
import org.distorted.patterns.RubikPattern;
39 4f470e00 Leszek Koltunski
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
42
public class RubikStatePattern extends RubikStateAbstract
43
  {
44 e0e84674 Leszek Koltunski
  // TODO: read this from upper_text.xml;  this is the height of the TextView there.
45
  private static final int DEFAULT_TEXT_SIZE = 30;
46
47 3a9d19ed Leszek Koltunski
  private TextView mText;
48 53f23b64 Leszek Koltunski
  private Button mBackButton;
49 0a57000c Leszek Koltunski
  private ImageButton mPrevButton, mNextButton;
50
  private TextView mMovesText;
51 53f23b64 Leszek Koltunski
  private int mSize;
52 4f470e00 Leszek Koltunski
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55
  RubikStatePattern()
56
    {
57
58
    }
59
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62
  void leaveState(RubikActivity act)
63
    {
64 53f23b64 Leszek Koltunski
    RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
65
66
    if( !play.setObjectAndSize(RubikObjectList.CUBE, mSize) )
67
      {
68
      int object= play.getObject();
69
      int size  = play.getSize();
70
71
      act.changeObject(RubikObjectList.getObject(object),size);
72
      }
73 4f470e00 Leszek Koltunski
    }
74
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
77 a6d3b158 Leszek Koltunski
  void enterState(final RubikActivity act)
78 4f470e00 Leszek Koltunski
    {
79 53f23b64 Leszek Koltunski
    RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
80
    int obj  = play.getObject();
81
    int size = play.getSize();
82
83
    if( size>=RubikPattern.MIN_CUBE && size<=RubikPattern.MAX_CUBE && obj==RubikObjectList.CUBE.ordinal() )
84
      {
85
      mSize = size;
86
      }
87
    else
88
      {
89
      mSize = RubikStatePlay.DEF_SIZE;
90
      act.changeObject(RubikObjectList.CUBE,mSize);
91
      }
92 a6d3b158 Leszek Koltunski
93 e108b57e Leszek Koltunski
    FragmentManager mana = act.getSupportFragmentManager();
94
    RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
95
96
    if( diag==null ) showDialog(mana);
97
98 4f470e00 Leszek Koltunski
    LayoutInflater inflater = act.getLayoutInflater();
99
100
    // TOP ////////////////////////////
101
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
102
    layoutTop.removeAllViews();
103 3a9d19ed Leszek Koltunski
    mText = (TextView)inflater.inflate(R.layout.upper_text, null);
104
    mText.setText(R.string.patterns);
105
    layoutTop.addView(mText);
106 4f470e00 Leszek Koltunski
107
    // BOT ////////////////////////////
108 53f23b64 Leszek Koltunski
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
109
    final float scale = metrics.density;
110
111 0a57000c Leszek Koltunski
    if( mPrevButton==null ) setupPrevButton(act,scale);
112
    if( mNextButton==null ) setupNextButton(act,scale);
113
    if( mMovesText ==null ) setupTextView(act,scale);
114
115
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
116
    layoutLeft.removeAllViews();
117
    layoutLeft.addView(mPrevButton);
118
    layoutLeft.addView(mMovesText);
119
    layoutLeft.addView(mNextButton);
120
121 53f23b64 Leszek Koltunski
    if( mBackButton==null ) setupBackButton(act,scale);
122
123 4f470e00 Leszek Koltunski
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
124
    layoutRight.removeAllViews();
125 53f23b64 Leszek Koltunski
    layoutRight.addView(mBackButton);
126
    }
127 4f470e00 Leszek Koltunski
128 53f23b64 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
129 a6d3b158 Leszek Koltunski
130 e108b57e Leszek Koltunski
  private void showDialog(FragmentManager manager)
131 53f23b64 Leszek Koltunski
    {
132 e108b57e Leszek Koltunski
    Bundle bundle = new Bundle();
133
    int object = RubikObjectList.CUBE.ordinal();
134
    int sizeIndex = RubikObjectList.getSizeIndex(object,mSize);
135
    bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) );
136
137
    RubikDialogPattern diag = new RubikDialogPattern();
138
    diag.setArguments(bundle);
139
    diag.show( manager, RubikDialogPattern.getDialogTag() );
140 53f23b64 Leszek Koltunski
    }
141
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
144
  private void setupBackButton(final RubikActivity act, final float scale)
145
    {
146
    int padding = (int)(3*scale + 0.5f);
147
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
148
    mBackButton = new Button(act);
149
    mBackButton.setLayoutParams(backParams);
150
    mBackButton.setPadding(padding,0,padding,0);
151
    mBackButton.setText(R.string.back);
152
153
    mBackButton.setOnClickListener( new View.OnClickListener()
154 a6d3b158 Leszek Koltunski
      {
155
      @Override
156
      public void onClick(View v)
157
        {
158 e108b57e Leszek Koltunski
        FragmentManager mana = act.getSupportFragmentManager();
159
        RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
160 53f23b64 Leszek Koltunski
161 e108b57e Leszek Koltunski
        if( diag==null )
162 53f23b64 Leszek Koltunski
          {
163 e0e84674 Leszek Koltunski
          mText.setTextSize(DEFAULT_TEXT_SIZE);
164
          mText.setText(R.string.patterns);
165 e108b57e Leszek Koltunski
          showDialog(mana);
166 53f23b64 Leszek Koltunski
          }
167 e108b57e Leszek Koltunski
        else
168
          {
169 044529c1 Leszek Koltunski
          diag.rememberCategories();
170 e108b57e Leszek Koltunski
          diag.dismiss();
171
          RubikState.goBack(act);
172
          }
173
        }
174
      });
175 4f470e00 Leszek Koltunski
    }
176
177 0a57000c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
178
179
  private void setupPrevButton(final RubikActivity act, final float scale)
180
    {
181
    int padding = (int)(3*scale + 0.5f);
182 11877284 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
183 0a57000c Leszek Koltunski
    mPrevButton = new ImageButton(act);
184
    mPrevButton.setLayoutParams(params);
185
    mPrevButton.setPadding(padding,0,padding,0);
186
    mPrevButton.setImageResource(R.drawable.left);
187
188
    mPrevButton.setOnClickListener( new View.OnClickListener()
189
      {
190
      @Override
191
      public void onClick(View v)
192
        {
193
        android.util.Log.e("patt", "prev button clicked!");
194
        }
195
      });
196
    }
197
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199
200
  private void setupNextButton(final RubikActivity act, final float scale)
201
    {
202
    int padding = (int)( 3*scale + 0.5f);
203 11877284 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
204 0a57000c Leszek Koltunski
    mNextButton = new ImageButton(act);
205
    mNextButton.setLayoutParams(params);
206
    mNextButton.setPadding(padding,0,padding,0);
207
    mNextButton.setImageResource(R.drawable.right);
208
209
    mNextButton.setOnClickListener( new View.OnClickListener()
210
      {
211
      @Override
212
      public void onClick(View v)
213
        {
214
        android.util.Log.e("patt", "next button clicked!");
215
        }
216
      });
217
    }
218
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220
221
  private void setupTextView(final RubikActivity act, final float scale)
222
    {
223
    int padding = (int)( 3*scale + 0.5f);
224 11877284 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
225 0a57000c Leszek Koltunski
226
    mMovesText = new TextView(act);
227
    mMovesText.setLayoutParams(params);
228
    mMovesText.setPadding(padding,0,padding,0);
229 11877284 Leszek Koltunski
    mMovesText.setGravity(Gravity.CENTER);
230 0a57000c Leszek Koltunski
231
    mMovesText.setText("aaa");
232
    }
233
234 3a9d19ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
235
236 e0e84674 Leszek Koltunski
  public void setPattern(int sizeIndex, int category, int pattern)
237 3a9d19ed Leszek Koltunski
    {
238 044529c1 Leszek Koltunski
    mSize = RubikObjectList.CUBE.getSizes()[sizeIndex];
239
240 e0e84674 Leszek Koltunski
    RubikPattern patt = RubikPattern.getInstance();
241
    String patternName = patt.getPatternName(sizeIndex,category,pattern);
242
    mText.setText(patternName);
243 3a9d19ed Leszek Koltunski
    }
244
245 4f470e00 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
246
247
  public void savePreferences(SharedPreferences.Editor editor)
248
    {
249 53f23b64 Leszek Koltunski
    mBackButton= null;
250 0a57000c Leszek Koltunski
    mPrevButton= null;
251
    mNextButton= null;
252
    mMovesText = null;
253 4f470e00 Leszek Koltunski
    }
254
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256
257
  public void restorePreferences(SharedPreferences preferences)
258
    {
259
260
    }
261
  }