Project

General

Profile

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

magiccube / src / main / java / org / distorted / solverui / SolverActivity.java @ a03fec57

1 cb30e768 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 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.solverui;
11
12
import android.content.SharedPreferences;
13
import android.os.Build;
14
import android.os.Bundle;
15
import android.view.DisplayCutout;
16
import android.view.ViewGroup;
17
import android.widget.LinearLayout;
18
19
import org.distorted.dialogs.RubikDialogError;
20 e1637420 leszek
import org.distorted.dialogs.RubikDialogMessage;
21 a5972f92 leszek
import org.distorted.helpers.BaseActivity;
22 cb30e768 leszek
import org.distorted.library.main.DistortedLibrary;
23
import org.distorted.library.main.DistortedScreen;
24
import org.distorted.main.R;
25
import org.distorted.objectlib.helpers.OperatingSystemInterface;
26
import org.distorted.objectlib.main.InitAssets;
27
import org.distorted.objectlib.main.ObjectControl;
28
import org.distorted.objectlib.main.TwistyObject;
29
import org.distorted.objects.RubikObject;
30
import org.distorted.objects.RubikObjectList;
31
import org.distorted.os.OSInterface;
32
33
import java.io.InputStream;
34
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36
37 a5972f92 leszek
public class SolverActivity extends BaseActivity
38 cb30e768 leszek
{
39 a5972f92 leszek
    public static final float RATIO_UPP       = 0.100f;
40 cb30e768 leszek
    public static final float PADDING         = 0.010f;
41
    public static final float SMALL_MARGIN    = 0.004f;
42
    public static final float BUTTON_TEXT_SIZE= 0.050f;
43
    public static final float TITLE_TEXT_SIZE = 0.060f;
44
45 c9f72ca3 leszek
    private static final int ACTIVITY_NUMBER = 4;
46 cb30e768 leszek
    private static final float RATIO_INSET= 0.09f;
47
48
    private int mObjectOrdinal;
49 e1637420 leszek
    private boolean mDisplayMessageDialog;
50 cb30e768 leszek
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53
    @Override
54
    protected void onCreate(Bundle savedState)
55
      {
56
      super.onCreate(savedState);
57
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
58
      setContentView(R.layout.solver);
59
60
      Bundle b = getIntent().getExtras();
61
      mObjectOrdinal = b!=null ? b.getInt("obj") : 0;
62 e1637420 leszek
      mDisplayMessageDialog = true;
63
64 a5972f92 leszek
      computeScreenDimensions();
65
      hideNavigationBar();
66 cb30e768 leszek
      cutoutHack();
67 a5972f92 leszek
      computeUpperBarHeight(RATIO_UPP);
68
      computeLowerBarHeight(RATIO_BAR);
69 cb30e768 leszek
      }
70
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
73
    @Override
74
    public void onAttachedToWindow()
75
      {
76
      super.onAttachedToWindow();
77
78
      if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.P )
79
        {
80
        DisplayCutout cutout = getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
81
        int insetHeight = cutout!=null ? cutout.getSafeInsetTop() : 0;
82
83
        LinearLayout layoutHid = findViewById(R.id.hiddenBar);
84
        ViewGroup.LayoutParams paramsHid = layoutHid.getLayoutParams();
85
        paramsHid.height = (int)(insetHeight*RATIO_INSET);
86
        layoutHid.setLayoutParams(paramsHid);
87
        }
88
      }
89
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
    
92
    @Override
93
    protected void onPause() 
94
      {
95
      super.onPause();
96
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
97
      view.onPause();
98
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
99
      savePreferences();
100
      }
101
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
    
104
    @Override
105
    protected void onResume() 
106
      {
107
      super.onResume();
108
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
109
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
110
      view.onResume();
111
112
      createObject();
113
114 9881dc03 leszek
      restorePreferences();
115 cb30e768 leszek
      ScreenList.setScreen(this);
116
      }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
    
120
    @Override
121
    protected void onDestroy() 
122
      {
123
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
124
      super.onDestroy();
125
      }
126
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
129
    private void savePreferences()
130
      {
131 9881dc03 leszek
      SharedPreferences.Editor editor = mPreferences.edit();
132 cb30e768 leszek
133
      for( int i=0; i< ScreenList.LENGTH; i++ )
134
        {
135
        ScreenList.getScreen(i).getScreenClass().savePreferences(editor);
136
        }
137
138
      ScreenList.savePreferences(editor);
139
140 e1637420 leszek
      editor.putBoolean("solverDisplayDialog", mDisplayMessageDialog );
141
142 cb30e768 leszek
      boolean success = editor.commit();
143
      if( !success ) android.util.Log.e("D", "Failed to save preferences");
144
      }
145
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
148 9881dc03 leszek
    private void restorePreferences()
149 cb30e768 leszek
      {
150 fb8a0bcd leszek
      for( int i=0; i<ScreenList.LENGTH; i++ )
151 cb30e768 leszek
        {
152 9881dc03 leszek
        ScreenList.getScreen(i).getScreenClass().restorePreferences(mPreferences);
153 cb30e768 leszek
        }
154
155 9881dc03 leszek
      ScreenList.restorePreferences(mPreferences);
156 e1637420 leszek
157 9881dc03 leszek
      mDisplayMessageDialog = mPreferences.getBoolean("solverDisplayDialog",true);
158 e1637420 leszek
159
      if( mDisplayMessageDialog )
160
        {
161
        Bundle bundle = new Bundle();
162
        bundle.putString("argument", getString(R.string.solver_message) );
163
        RubikDialogMessage diag = new RubikDialogMessage();
164
        diag.setArguments(bundle);
165
        diag.show( getSupportFragmentManager(), RubikDialogMessage.getDialogTag() );
166
        }
167
      }
168
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
171
    void doNotShowDialogAnymore()
172
      {
173
      mDisplayMessageDialog = false;
174 cb30e768 leszek
      }
175
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
178
    void OpenGLError()
179
      {
180
      RubikDialogError errDiag = new RubikDialogError();
181
      errDiag.show(getSupportFragmentManager(), null);
182
      }
183
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
// PUBLIC API
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
188
    public TwistyObject getObject()
189
      {
190
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
191
      return view.getObjectControl().getObject();
192
      }
193
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
196
    public DistortedScreen getScreen()
197
      {
198
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
199
      return view.getRenderer().getScreen();
200
      }
201
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203
204
    public ObjectControl getControl()
205
      {
206
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
207
      return view.getObjectControl();
208
      }
209
210 c02235d5 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212
    public int getObjectOrdinal()
213
      {
214
      return mObjectOrdinal;
215
      }
216
217 cb30e768 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
218
219
    public void createObject()
220
      {
221
      SolverSurfaceView view = findViewById(R.id.solverSurfaceView);
222
      ObjectControl control = view.getObjectControl();
223
      RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
224
      InputStream jsonStream = object==null ? null : object.getObjectStream(this);
225
      InputStream meshStream = object==null ? null : object.getMeshStream(this);
226
      String name = object==null ? "NULL" : object.getUpperName();
227
      OSInterface os = view.getInterface();
228
      InitAssets asset = new InitAssets(jsonStream,meshStream,os);
229 fb8a0bcd leszek
      control.changeIfDifferent(mObjectOrdinal,name,TwistyObject.MODE_NORM,asset);
230 cb30e768 leszek
      }
231
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233
234
    public OperatingSystemInterface getInterface()
235
      {
236
      SolverSurfaceView view  = findViewById(R.id.solverSurfaceView);
237
      return view.getInterface();
238
      }
239
}