Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigActivity.java @ 9881dc03

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.config;
11

    
12
import android.os.Bundle;
13
import android.content.SharedPreferences;
14

    
15
import org.distorted.dialogs.RubikDialogError;
16
import org.distorted.dialogs.RubikDialogMessage;
17
import org.distorted.helpers.BaseActivity;
18
import org.distorted.library.main.DistortedLibrary;
19
import org.distorted.main.R;
20
import org.distorted.objectlib.main.InitAssets;
21
import org.distorted.objectlib.main.ObjectControl;
22
import org.distorted.objectlib.main.TwistyObject;
23
import org.distorted.objects.RubikObject;
24
import org.distorted.objects.RubikObjectList;
25
import org.distorted.os.OSInterface;
26

    
27
import java.io.InputStream;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
public class ConfigActivity extends BaseActivity
32
{
33
    private static final int ACTIVITY_NUMBER = 7;
34
    private ConfigScreen mScreen;
35
    private ConfigScreenPane mPane;
36
    private int mObjectOrdinal;
37
    private boolean mSupportsAdjColors;
38
    private boolean mDisplayMessageDialog;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
    @Override
43
    protected void onCreate(Bundle savedState)
44
      {
45
      super.onCreate(savedState);
46
      DistortedLibrary.onCreate(ACTIVITY_NUMBER);
47
      setContentView(R.layout.config);
48

    
49
      Bundle b = getIntent().getExtras();
50
      if(b != null) mObjectOrdinal = b.getInt("obj");
51

    
52
      mDisplayMessageDialog = true;
53

    
54
      computeScreenDimensions();
55
      hideNavigationBar();
56
      cutoutHack();
57
      computeLowerBarHeight(RATIO_BAR);
58
      }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
    
62
    @Override
63
    protected void onPause() 
64
      {
65
      super.onPause();
66
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
67
      view.onPause();
68
      savePreferences();
69
      DistortedLibrary.onPause(ACTIVITY_NUMBER);
70
      }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
    
74
    @Override
75
    protected void onResume() 
76
      {
77
      super.onResume();
78
      DistortedLibrary.onResume(ACTIVITY_NUMBER);
79
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
80
      view.onResume();
81
      String key ="";
82
      mSupportsAdjColors = false;
83

    
84
      if( mObjectOrdinal>=0 && mObjectOrdinal< RubikObjectList.getNumObjects() )
85
        {
86
        RubikObject object = RubikObjectList.getObject(mObjectOrdinal);
87
        changeIfDifferent(object,mObjectOrdinal,view.getObjectControl());
88

    
89
        if( object!=null )
90
          {
91
          key = object.getUpperName();
92
          mSupportsAdjColors = object.supportsAdjustableColors();
93
          }
94
        }
95

    
96
      OSInterface os = view.getInterface();
97
      os.setPreferences(mPreferences);
98
      restorePreferences();
99

    
100
      if( mScreen==null ) mScreen = new ConfigScreen();
101
      mScreen.onAttachedToWindow(this,mObjectOrdinal);
102

    
103
      mPane = new ConfigScreenPane(this,key,os,mSupportsAdjColors);
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
    void repaintPuzzleFace(int cubit, int face)
109
      {
110
      if( mPane!=null && mSupportsAdjColors )
111
        {
112
        int color  = mPane.getCurrentColor();
113
        ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
114
        ObjectControl control = view.getObjectControl();
115
        control.repaintPuzzleFace(cubit,face,color);
116
        mDisplayMessageDialog = false;
117
        }
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
    @Override
123
    protected void onDestroy() 
124
      {
125
      super.onDestroy();
126
      DistortedLibrary.onDestroy(ACTIVITY_NUMBER);
127
      }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
    void OpenGLError()
132
      {
133
      RubikDialogError errDiag = new RubikDialogError();
134
      errDiag.show(getSupportFragmentManager(), null);
135
      }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
    private void changeIfDifferent(RubikObject object,int ordinal,ObjectControl control)
140
      {
141
      if( object!=null )
142
        {
143
        int iconMode           = TwistyObject.MODE_NORM;
144
        InputStream jsonStream = object.getObjectStream(this);
145
        InputStream meshStream = object.getMeshStream(this);
146
        String name            = object.getUpperName();
147
        InitAssets asset       = new InitAssets(jsonStream,meshStream,null);
148
        control.changeIfDifferent(ordinal,name,iconMode,asset);
149
        }
150
      }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
    private void savePreferences()
155
      {
156
      SharedPreferences.Editor editor = mPreferences.edit();
157

    
158
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
159
      OSInterface os = view.getInterface();
160
      os.setEditor(editor);
161
      view.getObjectControl().savePreferences();
162

    
163
      editor.putBoolean("configDisplayDialog", mDisplayMessageDialog );
164

    
165
      editor.apply();
166
      }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
    private void restorePreferences()
171
      {
172
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
173
      view.getObjectControl().restoreStickers();
174

    
175
      mDisplayMessageDialog = mPreferences.getBoolean("configDisplayDialog",true);
176

    
177
      if( mDisplayMessageDialog )
178
        {
179
        Bundle bundle = new Bundle();
180
        bundle.putString("argument", getString(R.string.config_message) );
181
        RubikDialogMessage diag = new RubikDialogMessage();
182
        diag.setArguments(bundle);
183
        diag.show( getSupportFragmentManager(), RubikDialogMessage.getDialogTag() );
184
        }
185
      }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
// PUBLIC API
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
    public ConfigRenderer getRenderer()
192
      {
193
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
194
      return view.getRenderer();
195
      }
196

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

    
199
    public void recreateColors()
200
      {
201
      ObjectControl control = getControl();
202
      control.recreateColors();
203
      }
204

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

    
207
    public void recreateStickers(int borders, int corners)
208
      {
209
      ObjectControl control = getControl();
210

    
211
      float border_step = ConfigScreenPane.BORDER_STEPS[borders];
212
      float corner_step = ConfigScreenPane.CORNER_STEPS[corners];
213

    
214
      control.recreateSticker(border_step,corner_step);
215
      }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
    public void resetUI()
220
      {
221
      mPane.resetUI(this);
222
      }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
    public ObjectControl getControl()
227
      {
228
      ConfigSurfaceView view = findViewById(R.id.configSurfaceView);
229
      return view.getObjectControl();
230
      }
231
}
(1-1/6)