Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStatePattern.java @ 3a9d19ed

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.LayoutInflater;
27
import android.view.View;
28
import android.widget.Button;
29
import android.widget.LinearLayout;
30
import android.widget.TextView;
31

    
32
import org.distorted.dialog.RubikDialogPattern;
33
import org.distorted.magic.R;
34
import org.distorted.magic.RubikActivity;
35
import org.distorted.object.RubikObjectList;
36
import org.distorted.patterns.RubikPattern;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
public class RubikStatePattern extends RubikStateAbstract
41
  {
42
  private TextView mText;
43
  private Button mBackButton;
44
  private int mSize;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
  RubikStatePattern()
49
    {
50

    
51
    }
52

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

    
55
  void leaveState(RubikActivity act)
56
    {
57
    RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
58

    
59
    if( !play.setObjectAndSize(RubikObjectList.CUBE, mSize) )
60
      {
61
      int object= play.getObject();
62
      int size  = play.getSize();
63

    
64
      act.changeObject(RubikObjectList.getObject(object),size);
65
      }
66
    }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  void enterState(final RubikActivity act)
71
    {
72
    RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
73
    int obj  = play.getObject();
74
    int size = play.getSize();
75

    
76
    if( size>=RubikPattern.MIN_CUBE && size<=RubikPattern.MAX_CUBE && obj==RubikObjectList.CUBE.ordinal() )
77
      {
78
      mSize = size;
79
      }
80
    else
81
      {
82
      mSize = RubikStatePlay.DEF_SIZE;
83
      act.changeObject(RubikObjectList.CUBE,mSize);
84
      }
85

    
86
    FragmentManager mana = act.getSupportFragmentManager();
87
    RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
88

    
89
    if( diag==null ) showDialog(mana);
90

    
91
    LayoutInflater inflater = act.getLayoutInflater();
92

    
93
    // TOP ////////////////////////////
94
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
95
    layoutTop.removeAllViews();
96
    mText = (TextView)inflater.inflate(R.layout.upper_text, null);
97
    mText.setText(R.string.patterns);
98
    layoutTop.addView(mText);
99

    
100
    // BOT ////////////////////////////
101
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
102
    final float scale = metrics.density;
103

    
104
    if( mBackButton==null ) setupBackButton(act,scale);
105

    
106
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
107
    layoutRight.removeAllViews();
108
    layoutRight.addView(mBackButton);
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
  private void showDialog(FragmentManager manager)
114
    {
115
    Bundle bundle = new Bundle();
116
    int object = RubikObjectList.CUBE.ordinal();
117
    int sizeIndex = RubikObjectList.getSizeIndex(object,mSize);
118
    bundle.putInt("tab", RubikObjectList.pack(object,sizeIndex) );
119

    
120
    RubikDialogPattern diag = new RubikDialogPattern();
121
    diag.setArguments(bundle);
122
    diag.show( manager, RubikDialogPattern.getDialogTag() );
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  private void setupBackButton(final RubikActivity act, final float scale)
128
    {
129
    int padding = (int)(3*scale + 0.5f);
130
    LinearLayout.LayoutParams backParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
131
    mBackButton = new Button(act);
132
    mBackButton.setLayoutParams(backParams);
133
    mBackButton.setId(BUTTON_ID_BACK);
134
    mBackButton.setPadding(padding,0,padding,0);
135
    mBackButton.setText(R.string.back);
136

    
137
    mBackButton.setOnClickListener( new View.OnClickListener()
138
      {
139
      @Override
140
      public void onClick(View v)
141
        {
142
        FragmentManager mana = act.getSupportFragmentManager();
143
        RubikDialogPattern diag = (RubikDialogPattern) mana.findFragmentByTag(RubikDialogPattern.getDialogTag());
144

    
145
        if( diag==null )
146
          {
147
          showDialog(mana);
148
          }
149
        else
150
          {
151
          diag.dismiss();
152
          RubikState.goBack(act);
153
          }
154
        }
155
      });
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
  public void setPatternName(String name)
161
    {
162
    mText.setText(name);
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  public void savePreferences(SharedPreferences.Editor editor)
168
    {
169
    mBackButton= null;
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
  public void restorePreferences(SharedPreferences preferences)
175
    {
176

    
177
    }
178
  }
(4-4/6)