Project

General

Profile

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

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

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.uistate;
21

    
22
import android.content.SharedPreferences;
23
import android.os.Bundle;
24
import android.support.v4.app.FragmentManager;
25
import android.util.DisplayMetrics;
26
import android.view.Gravity;
27
import android.view.LayoutInflater;
28
import android.view.View;
29
import android.widget.Button;
30
import android.widget.ImageButton;
31
import android.widget.LinearLayout;
32
import android.widget.TextView;
33

    
34
import org.distorted.dialog.RubikDialogPattern;
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

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
public class RubikStatePattern extends RubikStateAbstract
43
  {
44
  // 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
  private TextView mText;
48
  private Button mBackButton;
49
  private ImageButton mPrevButton, mNextButton;
50
  private TextView mMovesText;
51
  private int mSize;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
  RubikStatePattern()
56
    {
57

    
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  void leaveState(RubikActivity act)
63
    {
64
    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
    }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
  void enterState(final RubikActivity act)
78
    {
79
    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

    
93
    FragmentManager mana = act.getSupportFragmentManager();
94
    RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
95

    
96
    if( diag==null ) showDialog(mana);
97

    
98
    LayoutInflater inflater = act.getLayoutInflater();
99

    
100
    // TOP ////////////////////////////
101
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
102
    layoutTop.removeAllViews();
103
    mText = (TextView)inflater.inflate(R.layout.upper_text, null);
104
    mText.setText(R.string.patterns);
105
    layoutTop.addView(mText);
106

    
107
    // BOT ////////////////////////////
108
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
109
    final float scale = metrics.density;
110

    
111
    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
    if( mBackButton==null ) setupBackButton(act,scale);
122

    
123
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
124
    layoutRight.removeAllViews();
125
    layoutRight.addView(mBackButton);
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
  private void showDialog(FragmentManager manager)
131
    {
132
    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
    }
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
      {
155
      @Override
156
      public void onClick(View v)
157
        {
158
        FragmentManager mana = act.getSupportFragmentManager();
159
        RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
160

    
161
        if( diag==null )
162
          {
163
          mText.setTextSize(DEFAULT_TEXT_SIZE);
164
          mText.setText(R.string.patterns);
165
          showDialog(mana);
166
          }
167
        else
168
          {
169
          diag.rememberCategories();
170
          diag.dismiss();
171
          RubikState.goBack(act);
172
          }
173
        }
174
      });
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  private void setupPrevButton(final RubikActivity act, final float scale)
180
    {
181
    int padding = (int)(3*scale + 0.5f);
182
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
183
    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
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
204
    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
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
225

    
226
    mMovesText = new TextView(act);
227
    mMovesText.setLayoutParams(params);
228
    mMovesText.setPadding(padding,0,padding,0);
229
    mMovesText.setGravity(Gravity.CENTER);
230

    
231
    mMovesText.setText("aaa");
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  public void setPattern(int sizeIndex, int category, int pattern)
237
    {
238
    mSize = RubikObjectList.CUBE.getSizes()[sizeIndex];
239

    
240
    RubikPattern patt = RubikPattern.getInstance();
241
    String patternName = patt.getPatternName(sizeIndex,category,pattern);
242
    mText.setText(patternName);
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
  public void savePreferences(SharedPreferences.Editor editor)
248
    {
249
    mBackButton= null;
250
    mPrevButton= null;
251
    mNextButton= null;
252
    mMovesText = null;
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  public void restorePreferences(SharedPreferences preferences)
258
    {
259

    
260
    }
261
  }
(4-4/6)