Project

General

Profile

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

magiccube / src / main / java / org / distorted / screens / RubikScreenPattern.java @ c5fca790

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

    
22
import android.content.SharedPreferences;
23
import androidx.fragment.app.FragmentManager;
24
import android.util.TypedValue;
25
import android.view.Gravity;
26
import android.view.LayoutInflater;
27
import android.view.View;
28
import android.widget.LinearLayout;
29
import android.widget.TextView;
30

    
31
import org.distorted.objectlib.main.ObjectControl;
32

    
33
import org.distorted.main.R;
34
import org.distorted.dialogs.RubikDialogPattern;
35
import org.distorted.helpers.TransparentImageButton;
36
import org.distorted.main.RubikActivity;
37
import org.distorted.objects.RubikObjectList;
38
import org.distorted.patterns.RubikPattern;
39
import org.distorted.patterns.RubikPatternList;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
public class RubikScreenPattern extends RubikScreenAbstract
44
  {
45
  private TextView mText;
46
  private TransparentImageButton mPrevButton, mNextButton, mBackButton;
47
  private TextView mMovesText;
48
  private int mNumMoves;
49
  private int mPatternOrdinal, mCategory, mPattern;
50
  private float mButtonSize;
51

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

    
54
  RubikScreenPattern()
55
    {
56

    
57
    }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
  void leaveScreen(RubikActivity act)
62
    {
63
    int object = RubikPatternList.getObject(mPatternOrdinal);
64

    
65
    if( !RubikObjectList.setCurrObject(object) )
66
      {
67
      act.changeObject(object,false);
68
      }
69
    }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  void enterScreen(final RubikActivity act)
74
    {
75
    float width = act.getScreenWidthInPixels();
76
    mButtonSize = width*RubikActivity.BUTTON_TEXT_SIZE;
77
    float titleSize  = width*RubikActivity.TITLE_TEXT_SIZE;
78
    LayoutInflater inflater = act.getLayoutInflater();
79

    
80
    // TOP ////////////////////////////
81
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
82
    layoutTop.removeAllViews();
83
    mText = (TextView)inflater.inflate(R.layout.upper_pattern_text, null);
84
    mText.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
85
    mText.setText(R.string.patterns);
86
    layoutTop.addView(mText);
87

    
88
    // BOT ////////////////////////////
89
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
90
    layoutBot.removeAllViews();
91

    
92
    setupPrevButton(act);
93
    setupNextButton(act);
94
    setupTextView(act,width);
95

    
96
    setTrioState(false);
97

    
98
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams((int)(width/2),LinearLayout.LayoutParams.MATCH_PARENT);
99
    LinearLayout.LayoutParams paramsM = new LinearLayout.LayoutParams((int)(width/6),LinearLayout.LayoutParams.MATCH_PARENT);
100
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams((int)(width/3),LinearLayout.LayoutParams.MATCH_PARENT);
101

    
102
    LinearLayout layoutLeft = new LinearLayout(act);
103
    layoutLeft.setLayoutParams(paramsL);
104
    LinearLayout layoutMid = new LinearLayout(act);
105
    layoutMid.setLayoutParams(paramsM);
106
    LinearLayout layoutRight = new LinearLayout(act);
107
    layoutRight.setLayoutParams(paramsR);
108

    
109
    layoutLeft.addView(mPrevButton);
110
    layoutLeft.addView(mMovesText);
111
    layoutLeft.addView(mNextButton);
112

    
113
    setupBackButton(act);
114

    
115
    layoutRight.addView(mBackButton);
116

    
117
    layoutBot.addView(layoutLeft);
118
    layoutBot.addView(layoutMid);
119
    layoutBot.addView(layoutRight);
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  private void showDialog(FragmentManager manager)
125
    {
126
    RubikDialogPattern diag = new RubikDialogPattern();
127
    diag.show( manager, RubikDialogPattern.getDialogTag() );
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  private void setTrioState(boolean enable)
133
    {
134
    int state = enable ? View.VISIBLE : View.INVISIBLE;
135

    
136
    if( mPrevButton!=null ) mPrevButton.setVisibility(state);
137
    if( mNextButton!=null ) mNextButton.setVisibility(state);
138
    if( mMovesText !=null ) mMovesText.setVisibility(state);
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
  private void setupBackButton(final RubikActivity act)
144
    {
145
    final int icon = RubikActivity.getDrawable(R.drawable.ui_small_back,R.drawable.ui_medium_back, R.drawable.ui_big_back, R.drawable.ui_huge_back);
146
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
147
    mBackButton = new TransparentImageButton(act, icon, TransparentImageButton.GRAVITY_MIDDLE, params);
148

    
149
    mBackButton.setOnClickListener( new View.OnClickListener()
150
      {
151
      @Override
152
      public void onClick(View v)
153
        {
154
        FragmentManager mana = act.getSupportFragmentManager();
155
        ScreenList.goBack(act);
156
        showDialog(mana);
157
        }
158
      });
159
    }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  private void setupPrevButton(final RubikActivity act)
164
    {
165
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_left,R.drawable.ui_medium_left, R.drawable.ui_big_left, R.drawable.ui_huge_left);
166
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
167
    mPrevButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
168

    
169
    mPrevButton.setOnClickListener( new View.OnClickListener()
170
      {
171
      @Override
172
      public void onClick(View v)
173
        {
174
        RubikPattern pattern = RubikPattern.getInstance();
175
        ObjectControl control = act.getControl();
176
        pattern.backMove( control, mPatternOrdinal, mCategory, mPattern);
177
        int currMove = pattern.getCurMove(mPatternOrdinal, mCategory, mPattern);
178
        mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
179
        }
180
      });
181
    }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
  private void setupNextButton(final RubikActivity act)
186
    {
187
    int icon = RubikActivity.getDrawable(R.drawable.ui_small_right,R.drawable.ui_medium_right, R.drawable.ui_big_right, R.drawable.ui_huge_right);
188
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1.0f);
189
    mNextButton = new TransparentImageButton(act,icon,TransparentImageButton.GRAVITY_MIDDLE,params);
190

    
191
    mNextButton.setOnClickListener( new View.OnClickListener()
192
      {
193
      @Override
194
      public void onClick(View v)
195
        {
196
        RubikPattern pattern = RubikPattern.getInstance();
197
        ObjectControl control = act.getControl();
198
        pattern.makeMove( control, mPatternOrdinal, mCategory, mPattern);
199
        int currMove = pattern.getCurMove(mPatternOrdinal, mCategory, mPattern);
200
        mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
201
        }
202
      });
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  private void setupTextView(final RubikActivity act, final float width)
208
    {
209
    int padding = (int)(width*RubikActivity.PADDING);
210
    int margin  = (int)(width*RubikActivity.SMALL_MARGIN);
211

    
212
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,2.0f);
213
    params.topMargin    = margin;
214
    params.bottomMargin = margin;
215
    params.leftMargin   = margin;
216
    params.rightMargin  = margin;
217

    
218
    mMovesText = new TextView(act);
219
    mMovesText.setTextSize(20);
220
    mMovesText.setLayoutParams(params);
221
    mMovesText.setPadding(padding,0,padding,0);
222
    mMovesText.setGravity(Gravity.CENTER);
223
    mMovesText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mButtonSize);
224
    mMovesText.setText(act.getString(R.string.mo_placeholder,0,0));
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
  public void setPattern(final RubikActivity act, int ordinal, int category, int pattern)
230
    {
231
    mPatternOrdinal = ordinal;
232
    mCategory       = category;
233
    mPattern        = pattern;
234

    
235
    setTrioState(true);
236

    
237
    RubikPattern patt = RubikPattern.getInstance();
238
    String patternName = patt.getPatternName(ordinal,category,pattern);
239
    mText.setText(patternName);
240

    
241
    mNumMoves   = patt.getNumMoves(ordinal,category,pattern);
242
    int currMove= patt.getCurMove(ordinal,category,pattern);
243

    
244
    mMovesText.setText(act.getString(R.string.mo_placeholder,currMove,mNumMoves));
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  public void savePreferences(SharedPreferences.Editor editor)
250
    {
251

    
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  public void restorePreferences(SharedPreferences preferences)
257
    {
258

    
259
    }
260
  }
(5-5/11)