Project

General

Profile

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

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

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 Button mBackButton;
43
  private int mSize;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
  RubikStatePattern()
48
    {
49

    
50
    }
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

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

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

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

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

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

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

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

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

    
90
    LayoutInflater inflater = act.getLayoutInflater();
91

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

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

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

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

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

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

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

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

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

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

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

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  public void savePreferences(SharedPreferences.Editor editor)
160
    {
161
    mBackButton= null;
162
    }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
  public void restorePreferences(SharedPreferences preferences)
167
    {
168

    
169
    }
170
  }
(4-4/6)