Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedActivity.java @ a5972f92

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.bandaged;
11

    
12
import static android.view.View.LAYOUT_DIRECTION_RTL;
13

    
14
import java.io.InputStream;
15

    
16
import android.content.Intent;
17
import android.content.SharedPreferences;
18
import android.content.res.Configuration;
19
import android.graphics.Bitmap;
20
import android.os.Bundle;
21
import android.view.ViewGroup;
22
import android.widget.LinearLayout;
23

    
24
import androidx.preference.PreferenceManager;
25

    
26
import org.distorted.dialogs.RubikDialogError;
27
import org.distorted.dialogs.RubikDialogMessage;
28
import org.distorted.external.RubikFiles;
29
import org.distorted.helpers.BaseActivity;
30
import org.distorted.library.main.DistortedLibrary;
31
import org.distorted.main.R;
32
import org.distorted.objectlib.main.InitAssets;
33
import org.distorted.objectlib.main.TwistyJson;
34
import org.distorted.objectlib.main.TwistyObject;
35
import org.distorted.os.OSInterface;
36
import org.distorted.playui.PlayActivity;
37

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

    
40
public class BandagedActivity extends BaseActivity
41
{
42
    public static final float SPINNER_TEXT_SIZE = 0.03f;
43
    public static final float PADDING           = 0.010f;
44
    public static final float RATIO_SCROLL      = 0.30f;
45

    
46
    private static final int ACTIVITY_NUMBER    = 2;
47
    private static final float RATIO_BUT        = 0.07f;
48
    private static final int NUM_SCRAMBLES      = 300;
49

    
50
    private BandagedScreen mScreen;
51
    private boolean mRTL;
52
    private int mObjectOrdinal;
53
    private boolean mDisplayMessageDialog;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
    @Override
58
    protected void onCreate(Bundle savedState)
59
      {
60
      super.onCreate(savedState);
61

    
62
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
63
      setContentView(R.layout.bandaged);
64

    
65
      Bundle b = getIntent().getExtras();
66
      mObjectOrdinal = (b != null) ? b.getInt("obj") : 0;
67
      mDisplayMessageDialog = true;
68

    
69
      final Configuration config = getResources().getConfiguration();
70
      final int layoutDirection = config.getLayoutDirection();
71
      mRTL = layoutDirection==LAYOUT_DIRECTION_RTL;
72

    
73
      computeScreenDimensions();
74
      hideNavigationBar();
75
      cutoutHack();
76
      computeHeights();
77
      }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
// this does not include possible insets
81

    
82
    private void computeHeights()
83
      {
84
      int barHeight    = (int)(mScreenHeight*RATIO_BAR);
85
      int butHeight    = (int)(mScreenHeight*RATIO_BUT);
86
      int viewHeight   = (int)(mScreenHeight*RATIO_SCROLL);
87
      int objectHeight = (int)(mScreenHeight*(1-RATIO_SCROLL+RATIO_BAR));
88
      int padding      = (int)(mScreenHeight*PADDING);
89

    
90
      LinearLayout botLayout = findViewById(R.id.lowerBar);
91
      ViewGroup.LayoutParams paramsL = botLayout.getLayoutParams();
92
      paramsL.height = barHeight;
93
      botLayout.setLayoutParams(paramsL);
94

    
95
      LinearLayout butLayout = findViewById(R.id.bandagedCreatorButtons);
96
      ViewGroup.LayoutParams paramsB = butLayout.getLayoutParams();
97
      paramsB.height = butHeight;
98
      butLayout.setPadding(padding,0,padding,padding);
99
      butLayout.setLayoutParams(paramsB);
100

    
101
      LinearLayout topLayout = findViewById(R.id.bandagedCreatorTopView);
102
      ViewGroup.LayoutParams paramsT = topLayout.getLayoutParams();
103
      paramsT.height = viewHeight;
104
      topLayout.setPadding(padding,padding,padding,padding);
105
      topLayout.setLayoutParams(paramsT);
106

    
107
      BandagedView creator = findViewById(R.id.bandagedCreatorObjectView);
108
      ViewGroup.LayoutParams paramsC = creator.getLayoutParams();
109
      paramsC.height = objectHeight;
110
      creator.setLayoutParams(paramsC);
111
      }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
    
115
    @Override
116
    protected void onPause() 
117
      {
118
      super.onPause();
119
      BandagedView view = findViewById(R.id.bandagedCreatorObjectView);
120
      view.onPause();
121
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
122
      savePreferences();
123
      }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
    
127
    @Override
128
    protected void onResume() 
129
      {
130
      super.onResume();
131

    
132
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
133
      BandagedView view = findViewById(R.id.bandagedCreatorObjectView);
134
      view.onResume();
135

    
136
      if( mScreen==null )
137
        {
138
        int ordinal = getObjectOrdinal();
139
        mScreen = new BandagedScreen(ordinal);
140
        }
141

    
142
      mScreen.onAttachedToWindow(this);
143

    
144
      restorePreferences();
145
      BandagedWorkerThread.create(this);
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
    
150
    @Override
151
    protected void onDestroy() 
152
      {
153
      super.onDestroy();
154
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
155
      }
156

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

    
159
    void OpenGLError()
160
      {
161
      RubikDialogError errDiag = new RubikDialogError();
162
      errDiag.show(getSupportFragmentManager(), null);
163
      }
164

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

    
167
    private void savePreferences()
168
      {
169
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
170
      SharedPreferences.Editor editor = preferences.edit();
171
      mScreen.savePreferences(editor);
172

    
173
      editor.putBoolean("bandageDisplayDialog", mDisplayMessageDialog );
174

    
175
      editor.apply();
176
      }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
    private void restorePreferences()
181
      {
182
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
183
      mScreen.restorePreferences(this,preferences);
184

    
185
      mDisplayMessageDialog = preferences.getBoolean("bandageDisplayDialog",true);
186

    
187
      if( mDisplayMessageDialog )
188
        {
189
        Bundle bundle = new Bundle();
190
        bundle.putString("argument", getString(R.string.bandage_message) );
191
        RubikDialogMessage diag = new RubikDialogMessage();
192
        diag.setArguments(bundle);
193
        diag.show( getSupportFragmentManager(), RubikDialogMessage.getDialogTag() );
194
        }
195
      }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
    void doNotShowDialogAnymore()
200
      {
201
      mDisplayMessageDialog = false;
202
      }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
// PUBLIC API
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
    public void changeObject(int x, int y, int z)
209
      {
210
      BandagedRenderer renderer = getRenderer();
211
      renderer.changeObject(x,y,z);
212
      }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
    public boolean isRTL()
217
      {
218
      return mRTL;
219
      }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
    public BandagedRenderer getRenderer()
224
      {
225
      BandagedView view = findViewById(R.id.bandagedCreatorObjectView);
226
      return view.getRenderer();
227
      }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
    public boolean objectDoesntExist(String name)
232
      {
233
      return mScreen.objectDoesntExist(name);
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    public void addObject(String name)
239
      {
240
      mScreen.addObject(this,name);
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
    public void deleteObject(String name)
246
      {
247
      RubikFiles files = RubikFiles.getInstance();
248
      InputStream jsonStream = files.openFile(this,name+"_object.json");
249
      InitAssets assets = new InitAssets(jsonStream,null,null);
250

    
251
      if( !assets.noJsonStream() )
252
        {
253
        TwistyObject object = new TwistyJson( TwistyObject.MODE_NORM, null, null, 1.0f, assets);
254

    
255
        if( !object.getError() )
256
          {
257
          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
258
          SharedPreferences.Editor editor = preferences.edit();
259
          OSInterface os = new OSInterface(this,null);
260
          os.setEditor(editor);
261
          object.removePreferences(os);
262
          editor.apply();
263
          }
264
        }
265

    
266
      mScreen.deleteObject(this,name);
267
      files.deleteIcon(this,name);
268
      files.deleteJsonObject(this,name);
269
      }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
    public void playObject(String name)
274
      {
275
      Intent intent = new Intent(this, PlayActivity.class);
276
      intent.putExtra("level", -1);
277
      intent.putExtra("name", name);
278
      intent.putExtra("scrambles", NUM_SCRAMBLES);
279
      intent.putExtra("local", true);
280
      intent.putExtra("ordinal", 0);
281
      startActivity(intent);
282
      }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

    
286
    public void iconCreationDone(Bitmap bmp)
287
      {
288
      mScreen.iconCreationDone(this,bmp);
289
      }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
    public int getObjectOrdinal()
294
      {
295
      return mObjectOrdinal;
296
      }
297
}
(1-1/7)