Project

General

Profile

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

magiccube / src / main / java / org / distorted / bandaged / BandagedRenderer.java @ fe1d7818

1 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 da56b12f Leszek Koltunski
// Copyright 2022 Leszek Koltunski                                                               //
3 9530f6b0 Leszek Koltunski
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6 44fec653 Leszek Koltunski
// 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 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
9
10
package org.distorted.bandaged;
11
12 78478d7e leszek
import org.json.JSONException;
13
14 b4a6e84d Leszek Koltunski
import java.io.File;
15
import java.io.FileNotFoundException;
16
import java.io.IOException;
17 d0ad3964 Leszek Koltunski
import java.io.InputStream;
18 b4a6e84d Leszek Koltunski
import java.nio.ByteBuffer;
19
import java.nio.ByteOrder;
20 e4b7d0f9 leszek
import java.util.Stack;
21 b4a6e84d Leszek Koltunski
22 77efd5ad Leszek Koltunski
import javax.microedition.khronos.egl.EGLConfig;
23
import javax.microedition.khronos.opengles.GL10;
24
25 72e386ef Leszek Koltunski
import android.app.Activity;
26 d0ad3964 Leszek Koltunski
import android.content.res.Resources;
27 7cb8d4b0 Leszek Koltunski
import android.opengl.GLES31;
28 9530f6b0 Leszek Koltunski
import android.opengl.GLSurfaceView;
29 72e386ef Leszek Koltunski
import android.widget.Toast;
30 9530f6b0 Leszek Koltunski
31 6dff7924 Leszek Koltunski
import org.distorted.library.effect.EffectType;
32 1a106eb8 Leszek Koltunski
import org.distorted.library.effect.FragmentEffectBrightness;
33 7cb8d4b0 Leszek Koltunski
import org.distorted.library.effect.PostprocessEffectBorder;
34
import org.distorted.library.main.DistortedEffects;
35
import org.distorted.library.main.DistortedFramebuffer;
36 9530f6b0 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
37 da56b12f Leszek Koltunski
import org.distorted.library.main.DistortedNode;
38 9530f6b0 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
39 20b60ad9 Leszek Koltunski
import org.distorted.library.main.InternalOutputSurface;
40 6dff7924 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
41 7cb8d4b0 Leszek Koltunski
import org.distorted.library.type.Static1D;
42 77efd5ad Leszek Koltunski
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44 e514732c leszek
import org.distorted.objectlib.bandaged.BandagedObject;
45 7da27d2b leszek
import org.distorted.objectlib.bandaged.LocallyBandagedList;
46 72e386ef Leszek Koltunski
import org.distorted.objectlib.json.JsonWriter;
47 402b1325 leszek
import org.distorted.objectlib.main.ObjectControl;
48 72e386ef Leszek Koltunski
import org.distorted.objectlib.main.TwistyObject;
49 8477cf44 leszek
import org.distorted.dialogs.DialogBandagedSave;
50 72e386ef Leszek Koltunski
51 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53 7ee8337b leszek
public class BandagedRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
54 9530f6b0 Leszek Koltunski
{
55 213c15de Leszek Koltunski
   private static final int RESET_DURATION = 1000;
56 f6bf4e77 Leszek Koltunski
   private static final float MAX_SIZE_CHANGE = 1.70f;
57
   private static final float MIN_SIZE_CHANGE = 0.50f;
58 9530f6b0 Leszek Koltunski
59 7ee8337b leszek
   private final BandagedView mView;
60 d0ad3964 Leszek Koltunski
   private final Resources mResources;
61 75173f81 Leszek Koltunski
   private final DistortedScreen mScreen;
62
   private final Static3D mScale;
63 50ec342b Leszek Koltunski
   private final Static4D mQuatT, mQuatA;
64 f404152d Leszek Koltunski
   private final BandagedObject mObject;
65 4dcfb7a9 leszek
   private final float mInitRatio;
66 75173f81 Leszek Koltunski
67 e0b71e6e Leszek Koltunski
   private boolean mInitialPhase;
68
   private long mStartTime;
69 b9d062cf Leszek Koltunski
   private float mQuatX, mQuatY, mQuatZ, mQuatW;
70 e4b7d0f9 leszek
   private boolean mResetQuats, mSetQuatT, mResettingObject, mConnectingCubits;
71
   private boolean mCreatingCubits, mRescaling, mUndoingObject;
72 1d4592a2 Leszek Koltunski
   private int mIndex1, mIndex2;
73 7cb8d4b0 Leszek Koltunski
   private int mSaveIcon;
74
   private DistortedFramebuffer mFramebuffer;
75
   private String mPath;
76 b9d062cf Leszek Koltunski
   private boolean mCubitsCreated;
77 a8aa49dc Leszek Koltunski
   private int mWidth, mHeight;
78 f6bf4e77 Leszek Koltunski
   private float mScaleValue, mObjectScreenRatio;
79 da56b12f Leszek Koltunski
80 e4b7d0f9 leszek
   private final Stack<Integer> mPairs;  // each Integer holds a packed pair in ints
81
82 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
83
84 7ee8337b leszek
   BandagedRenderer(BandagedView v, int ordinal)
85 9530f6b0 Leszek Koltunski
     {
86 d0ad3964 Leszek Koltunski
     mView = v;
87
     mResources = v.getResources();
88
89 50ec342b Leszek Koltunski
     mQuatT = new Static4D(0,0,0,1);
90
     mQuatA = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
91 da56b12f Leszek Koltunski
92 1d4592a2 Leszek Koltunski
     mResetQuats       = false;
93
     mSetQuatT         = false;
94
     mResettingObject  = false;
95
     mConnectingCubits = false;
96 b9d062cf Leszek Koltunski
     mCubitsCreated    = false;
97 227e8c1f Leszek Koltunski
     mCreatingCubits   = false;
98 f6bf4e77 Leszek Koltunski
     mRescaling        = false;
99 e4b7d0f9 leszek
     mUndoingObject    = false;
100 75173f81 Leszek Koltunski
101 7cb8d4b0 Leszek Koltunski
     mSaveIcon = -1;
102
103 9530f6b0 Leszek Koltunski
     mScreen = new DistortedScreen();
104 8c4e4bf4 leszek
105
     BandagedActivity act = (BandagedActivity)v.getContext();
106
     act.setUpBackgroundColor(mScreen);
107
108 550db260 Leszek Koltunski
     mScale = new Static3D(1,1,1);
109 7da27d2b leszek
     mObject= LocallyBandagedList.create(ordinal,mScreen);
110 4dcfb7a9 leszek
111
     mInitRatio = mObject.getScreenRatio();
112
     mObjectScreenRatio= mInitRatio;
113 e4b7d0f9 leszek
114
     mPairs = new Stack<>();
115 e0b71e6e Leszek Koltunski
     }
116
117 9530f6b0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
118
119
   @Override
120
   public void onDrawFrame(GL10 glUnused)
121
     {
122
     long time = System.currentTimeMillis();
123
     mScreen.render(time);
124 75173f81 Leszek Koltunski
125 50ec342b Leszek Koltunski
     if( mSetQuatT )
126 75173f81 Leszek Koltunski
       {
127 50ec342b Leszek Koltunski
       mSetQuatT = false;
128 b9d062cf Leszek Koltunski
       mQuatT.set(mQuatX,mQuatY,mQuatZ,mQuatW);
129 75173f81 Leszek Koltunski
       }
130
131
     if( mResetQuats )
132
       {
133
       mResetQuats = false;
134
135 50ec342b Leszek Koltunski
       float qx = mQuatT.get0();
136
       float qy = mQuatT.get1();
137
       float qz = mQuatT.get2();
138
       float qw = mQuatT.get3();
139 75173f81 Leszek Koltunski
140 50ec342b Leszek Koltunski
       float rx = mQuatA.get0();
141
       float ry = mQuatA.get1();
142
       float rz = mQuatA.get2();
143
       float rw = mQuatA.get3();
144 75173f81 Leszek Koltunski
145
       float tx = rw*qx - rz*qy + ry*qz + rx*qw;
146
       float ty = rw*qy + rz*qx + ry*qw - rx*qz;
147
       float tz = rw*qz + rz*qw - ry*qx + rx*qy;
148
       float tw = rw*qw - rz*qz - ry*qy - rx*qx;
149
150 50ec342b Leszek Koltunski
       mQuatT.set(0f, 0f, 0f, 1f);
151
       mQuatA.set(tx, ty, tz, tw);
152 75173f81 Leszek Koltunski
       }
153 e0b71e6e Leszek Koltunski
154
     if( mResettingObject )
155
       {
156
       boolean done = continueResetting(time);
157 e4b7d0f9 leszek
       if( done )
158
         {
159
         mResettingObject = false;
160
         mPairs.clear();
161
         }
162 e0b71e6e Leszek Koltunski
       }
163 7cb8d4b0 Leszek Koltunski
164 eb6bccbd Leszek Koltunski
     if( mSaveIcon>=0 )
165
       {
166 f203ffa0 Leszek Koltunski
       renderIcon(time); // for some reason we need to call render() twice here, otherwise the
167
       mSaveIcon++;      // icon turns out black. Probably some problem with binding the texture.
168 eb6bccbd Leszek Koltunski
       }
169
     if( mSaveIcon>=2 )
170
       {
171
       saveIcon();
172
       mSaveIcon = -1;
173
       }
174 1d4592a2 Leszek Koltunski
175
     if( mConnectingCubits )
176
       {
177 e4b7d0f9 leszek
       boolean success = mObject.tryConnectingCubits(mIndex1,mIndex2,mScaleValue);
178
       if( success ) mPairs.add( packTwoIntsIntoPair(mIndex1,mIndex2) );
179 e3c970a7 Leszek Koltunski
       mConnectingCubits = false;
180 1d4592a2 Leszek Koltunski
       }
181 e4b7d0f9 leszek
     if( mUndoingObject )
182
       {
183
       int num = mPairs.size();
184
185
       if( num>0 )
186
         {
187
         int pair  = mPairs.pop();
188
         int first = getFirstFromPair(pair);
189
         int second= getSecondFromPair(pair);
190
         mObject.disconnectCubits(first, second, mScaleValue);
191
         }
192
193
       mUndoingObject = false;
194
       }
195 227e8c1f Leszek Koltunski
196
     if( mCreatingCubits )
197
       {
198 f6c211f1 Leszek Koltunski
       mPairs.clear();
199 227e8c1f Leszek Koltunski
       rescaleObject();
200
201
       if( mCubitsCreated )
202
         {
203 f404152d Leszek Koltunski
         mObject.createCubits(mQuatT,mQuatA,mScale);
204
         float[] dist = mObject.getDist3D();
205 7ee8337b leszek
         BandagedTouchControl control = mView.getTouchControl();
206 f404152d Leszek Koltunski
         control.setDist3D(dist);
207 227e8c1f Leszek Koltunski
         mScreen.detachAll();
208
         mView.resetCubits();
209 f404152d Leszek Koltunski
         mObject.attachCubits(mScaleValue);
210 227e8c1f Leszek Koltunski
         }
211 e3c970a7 Leszek Koltunski
212
       mCreatingCubits = false;
213 227e8c1f Leszek Koltunski
       }
214 f6bf4e77 Leszek Koltunski
215
     if( mRescaling )
216
       {
217
       rescaleObject();
218 f404152d Leszek Koltunski
       mObject.scaleCubits(mScaleValue);
219 7ee8337b leszek
       BandagedTouchControl control = mView.getTouchControl();
220 f6bf4e77 Leszek Koltunski
       control.setObjectRatio(mObjectScreenRatio);
221 e3c970a7 Leszek Koltunski
       mRescaling = false;
222 f6bf4e77 Leszek Koltunski
       }
223 9530f6b0 Leszek Koltunski
     }
224
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226
227
   @Override
228
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
229
      {
230 3b8f5220 Leszek Koltunski
      if( width!=mWidth || height!=mHeight )
231
        {
232
        mWidth = width;
233
        mHeight= height;
234
        rescaleObject();
235
        mScreen.detachAll();
236
        int touched = mView.getTouched();
237 f404152d Leszek Koltunski
        mObject.attachAndMarkCubits(mScaleValue,touched);
238 3b8f5220 Leszek Koltunski
        mView.setScreenSize(width,height);
239
        mScreen.resize(width,height);
240
        }
241 9530f6b0 Leszek Koltunski
      }
242
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244
245
   @Override
246
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
247
      {
248 7d8ae8db Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX,0);
249 402b1325 leszek
      MeshBase.setMaxEffComponents(ObjectControl.MAX_MOVING_PARTS);
250 1a106eb8 Leszek Koltunski
      FragmentEffectBrightness.enable();
251 d0ad3964 Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(this,1);
252 6dff7924 Leszek Koltunski
      DistortedLibrary.setCull(true);
253 f404152d Leszek Koltunski
      mObject.recreateCubits(mQuatT,mQuatA,mScale);
254 b9d062cf Leszek Koltunski
      mCubitsCreated = true;
255 3b8f5220 Leszek Koltunski
      mWidth = 0;
256
      mHeight= 0;
257 9530f6b0 Leszek Koltunski
      }
258
259 550db260 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
260
261
   public DistortedScreen getScreen()
262
     {
263
     return mScreen;
264
     }
265
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267
268 1d4592a2 Leszek Koltunski
   void setConnecting(int index1, int index2)
269
     {
270
     mIndex1 = index1;
271
     mIndex2 = index2;
272
     mConnectingCubits = true;
273
     }
274
275 75173f81 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
276
277 50ec342b Leszek Koltunski
   public Static4D getQuatAccu()
278
     {
279
     return mQuatA;
280
     }
281 75173f81 Leszek Koltunski
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283
284 50ec342b Leszek Koltunski
   public void setQuatTemp(float x, float y, float z, float w)
285
     {
286 2683f0c4 Leszek Koltunski
     mSetQuatT = false;
287
288 b9d062cf Leszek Koltunski
     mQuatX = x;
289
     mQuatY = y;
290
     mQuatZ = z;
291
     mQuatW = w;
292 75173f81 Leszek Koltunski
293 50ec342b Leszek Koltunski
     mSetQuatT = true;
294
     }
295 75173f81 Leszek Koltunski
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297
298 50ec342b Leszek Koltunski
   public void resetQuats()
299
     {
300
     mResetQuats = true;
301
     }
302
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304
305 e0b71e6e Leszek Koltunski
   public boolean isBusy()
306 50ec342b Leszek Koltunski
     {
307 e3c970a7 Leszek Koltunski
     return (mResettingObject || mCreatingCubits || mConnectingCubits);
308 e0b71e6e Leszek Koltunski
     }
309
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311
312
   public void saveObject()
313 72e386ef Leszek Koltunski
     {
314 f404152d Leszek Koltunski
     TwistyObject obj = mObject.createObject(TwistyObject.MODE_NORM, 1.0f );
315 bfb59352 Leszek Koltunski
     String name = obj.getShortName();
316 7ee8337b leszek
     BandagedActivity act = (BandagedActivity) mView.getContext();
317 72e386ef Leszek Koltunski
318 bfb59352 Leszek Koltunski
     if( act.objectDoesntExist(name) && createObjectJson(obj,act) )
319 e48ad1af Leszek Koltunski
       {
320 f404152d Leszek Koltunski
       setupIconCreation(act);
321 e48ad1af Leszek Koltunski
       act.addObject(obj.getShortName());
322 e1637420 leszek
       act.doNotShowDialogAnymore();
323 e48ad1af Leszek Koltunski
       }
324 72e386ef Leszek Koltunski
     }
325
326 e4b7d0f9 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
327
328
   private int packTwoIntsIntoPair(int index1, int index2)
329
     {
330
     return (index1<<16) + index2;
331
     }
332
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334
335
   private int getFirstFromPair(int pair)
336
     {
337
     return pair>>16;
338
     }
339
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341
342
   private int getSecondFromPair(int pair)
343
     {
344
     return pair&0xffff;
345
     }
346
347 72e386ef Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
348
349 e48ad1af Leszek Koltunski
   private boolean createObjectJson(TwistyObject object, Activity act)
350 72e386ef Leszek Koltunski
     {
351
     final String name = object.getShortName()+"_object.json";
352
     File file = new File(act.getFilesDir(), name);
353
     String filename = file.getAbsolutePath();
354
355
     try
356
       {
357
       JsonWriter writer = JsonWriter.getInstance();
358 ff4a2a13 leszek
       String json = writer.createObjectString(object);
359 72e386ef Leszek Koltunski
       writer.write(filename,json);
360 e48ad1af Leszek Koltunski
       return true;
361 72e386ef Leszek Koltunski
       }
362
     catch(JSONException ex)
363
       {
364
       act.runOnUiThread(new Runnable()
365
         {
366
         public void run()
367
           {
368
           String message = "JSON Exception saving to \n\n"+filename+"\n\n failed:\n\n"+ex.getMessage();
369
           Toast.makeText(act,message,Toast.LENGTH_LONG).show();
370
           }
371
         });
372 e48ad1af Leszek Koltunski
373
       return false;
374 72e386ef Leszek Koltunski
       }
375
     catch(FileNotFoundException ex)
376
       {
377
       act.runOnUiThread(new Runnable()
378
         {
379
         public void run()
380
           {
381
           String message = "FileNotFound exception saving to \n\n"+filename+"\n\n failed:\n\n"+ex.getMessage();
382
           Toast.makeText(act,message,Toast.LENGTH_LONG).show();
383
           }
384
         });
385 e48ad1af Leszek Koltunski
386
       return false;
387 72e386ef Leszek Koltunski
       }
388
     catch(IOException ex)
389
       {
390
       act.runOnUiThread(new Runnable()
391
         {
392
         public void run()
393
           {
394
           String message = "IO exception saving to \n\n"+filename+"\n\n failed:\n\n"+ex.getMessage();
395
           Toast.makeText(act,message,Toast.LENGTH_LONG).show();
396
           }
397
         });
398 e48ad1af Leszek Koltunski
399
       return false;
400 72e386ef Leszek Koltunski
       }
401
     }
402
403 2d88fcc3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
404
405 f404152d Leszek Koltunski
   private void setupIconCreation(Activity act)
406 72e386ef Leszek Koltunski
     {
407 f203ffa0 Leszek Koltunski
     final float R=1.0f;
408 ba52835a Leszek Koltunski
     final int FBO_WIDTH  = (int)(R*720);
409
     final int FBO_HEIGHT = (int)(R*1280);
410 d06018a9 leszek
     final float OBJECT_SIZE = R*0.60f;
411 f203ffa0 Leszek Koltunski
412 f404152d Leszek Koltunski
     TwistyObject obj = mObject.createObject(TwistyObject.MODE_ICON, OBJECT_SIZE );
413 16663e27 Leszek Koltunski
     DistortedEffects effects = obj.getObjectEffects();
414
     DistortedNode node = obj.getNode();
415 eb6bccbd Leszek Koltunski
416 20b60ad9 Leszek Koltunski
     if( mFramebuffer==null )
417
       {
418 f203ffa0 Leszek Koltunski
       mFramebuffer = new DistortedFramebuffer(FBO_WIDTH,FBO_HEIGHT,1, InternalOutputSurface.DEPTH_NO_STENCIL);
419 8ce05bed leszek
       mFramebuffer.glClearColor(0,0,0,0);
420 20b60ad9 Leszek Koltunski
       }
421 7cb8d4b0 Leszek Koltunski
422 f404152d Leszek Koltunski
     mFramebuffer.setProjection( mObject.computeProjectionAngle() ,0.1f);
423 20b60ad9 Leszek Koltunski
     mFramebuffer.detachAll();
424
     mFramebuffer.attach(node);
425 7cb8d4b0 Leszek Koltunski
426
     Static1D halo = new Static1D(5);
427
     Static4D color = new Static4D(0,0,0,1);
428
     PostprocessEffectBorder border = new PostprocessEffectBorder(halo,color);
429
     border.setHaloDepth(false);
430
     effects.apply(border);
431
432 16663e27 Leszek Koltunski
     final String name = obj.getShortName()+".png";
433 7cb8d4b0 Leszek Koltunski
     File file = new File(act.getFilesDir(), name);
434
     String filename = file.getAbsolutePath();
435
436
     mSaveIcon = 0;
437
     mPath = filename;
438 72e386ef Leszek Koltunski
     }
439 7cb8d4b0 Leszek Koltunski
440
///////////////////////////////////////////////////////////////////////////////////////////////////
441
442 eb6bccbd Leszek Koltunski
   private void renderIcon(long time)
443
     {
444
     mFramebuffer.render(time);
445
     }
446 7cb8d4b0 Leszek Koltunski
447 eb6bccbd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
448 7cb8d4b0 Leszek Koltunski
449 eb6bccbd Leszek Koltunski
   private void saveIcon()
450
     {
451
     int fW = mFramebuffer.getWidth();
452
     int fH = mFramebuffer.getHeight();
453 7cb8d4b0 Leszek Koltunski
454 eb6bccbd Leszek Koltunski
     ByteBuffer buf = ByteBuffer.allocateDirect(fW*fH*4);
455
     buf.order(ByteOrder.LITTLE_ENDIAN);
456
457
     mFramebuffer.setAsReadFramebuffer(0);
458
     GLES31.glReadBuffer(GLES31.GL_COLOR_ATTACHMENT0);
459
     GLES31.glReadPixels( 0, 0, fW, fH, GLES31.GL_RGBA, GLES31.GL_UNSIGNED_BYTE, buf);
460 7ee8337b leszek
     BandagedWorkerThread.newBuffer(buf,fW,fH,6,mPath);
461 eb6bccbd Leszek Koltunski
     GLES31.glBindFramebuffer(GLES31.GL_READ_FRAMEBUFFER, 0);
462
463
     mSaveIcon = -1;
464
     }
465 7cb8d4b0 Leszek Koltunski
466 f6bf4e77 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
467
468
   void mulObjectRatio(float ratio)
469
     {
470
     mObjectScreenRatio *= ratio;
471
472 4dcfb7a9 leszek
     if( mObjectScreenRatio>MAX_SIZE_CHANGE*mInitRatio) mObjectScreenRatio = MAX_SIZE_CHANGE*mInitRatio;
473
     if( mObjectScreenRatio<MIN_SIZE_CHANGE*mInitRatio) mObjectScreenRatio = MIN_SIZE_CHANGE*mInitRatio;
474 f6bf4e77 Leszek Koltunski
475
     mRescaling = true;
476
     }
477
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479
480
   float getObjectRatio()
481
     {
482
     return mObjectScreenRatio;
483
     }
484
485 a8aa49dc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
486
487
   private void rescaleObject()
488
     {
489 afd7e804 Leszek Koltunski
     float size = mObject.getMaxSize();
490 f6bf4e77 Leszek Koltunski
     final float Q = mObjectScreenRatio/size;
491 a8aa49dc Leszek Koltunski
     mScaleValue = mWidth<mHeight ? Q*mWidth : Q*mHeight;
492
     mScale.set( mScaleValue,mScaleValue,mScaleValue );
493
     }
494
495 b9d062cf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
496
497
   public void changeObject(int x, int y, int z)
498
     {
499 f404152d Leszek Koltunski
     if( mObject.tryChangeObject(x,y,z) ) mCreatingCubits = true;
500 b9d062cf Leszek Koltunski
     }
501
502 72e386ef Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
503
504
   public void displaySavingDialog()
505 e0b71e6e Leszek Koltunski
     {
506 7ee8337b leszek
     BandagedActivity act = (BandagedActivity)mView.getContext();
507 8477cf44 leszek
     DialogBandagedSave saveDiag = new DialogBandagedSave();
508 61a7b812 Leszek Koltunski
     saveDiag.show(act.getSupportFragmentManager(), null);
509 e0b71e6e Leszek Koltunski
     }
510
511
///////////////////////////////////////////////////////////////////////////////////////////////////
512
513
   public void setupReset()
514
     {
515
     mResettingObject = true;
516
     mInitialPhase    = true;
517
     mStartTime       = System.currentTimeMillis();
518
     }
519
520 e4b7d0f9 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
521
522
   public void setupUndo()
523
     {
524
     mUndoingObject = true;
525
     }
526
527 e0b71e6e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
528
529
   public boolean continueResetting(long time)
530
     {
531
     long diff = time-mStartTime;
532 213c15de Leszek Koltunski
     float quotient = ((float)diff)/RESET_DURATION;
533 e0b71e6e Leszek Koltunski
534
     if( mInitialPhase && quotient>0.5f )
535
       {
536
       mInitialPhase=false;
537 f404152d Leszek Koltunski
       mView.resetCubits();
538
       mObject.resetObject(mScaleValue);
539 e0b71e6e Leszek Koltunski
       }
540
541
     double angle = 2*Math.PI*quotient*quotient*(3-2*quotient);
542
543
     float sinA = (float)Math.sin(angle);
544
     float cosA = (float)Math.cos(angle);
545
546
     mQuatT.set(0, -sinA, 0, cosA);
547 50ec342b Leszek Koltunski
548 e0b71e6e Leszek Koltunski
     return quotient>1.0f;
549 50ec342b Leszek Koltunski
     }
550 6f3af598 Leszek Koltunski
551
///////////////////////////////////////////////////////////////////////////////////////////////////
552
553
  public void touchCubit(int index)
554
    {
555 f404152d Leszek Koltunski
    mObject.touchCubit(index);
556 6f3af598 Leszek Koltunski
    }
557
558
///////////////////////////////////////////////////////////////////////////////////////////////////
559
560
  public void untouchCubit(int index)
561
    {
562 f404152d Leszek Koltunski
    mObject.untouchCubit(index);
563
    }
564
565
///////////////////////////////////////////////////////////////////////////////////////////////////
566
567 7ee8337b leszek
  public BandagedTouchControl createTouchControl()
568 f404152d Leszek Koltunski
    {
569 7ee8337b leszek
    return new BandagedTouchControl( getObjectRatio() , mScreen.getFOV(), mObject );
570 6f3af598 Leszek Koltunski
    }
571 d0ad3964 Leszek Koltunski
572
///////////////////////////////////////////////////////////////////////////////////////////////////
573
574
  public void distortedException(Exception ex)
575
    {
576
    android.util.Log.e("BandagedCreator", "unexpected exception: "+ex.getMessage() );
577
    }
578
579
///////////////////////////////////////////////////////////////////////////////////////////////////
580
581
  public InputStream localFile(int fileID)
582
    {
583
    return mResources.openRawResource(fileID);
584
    }
585
586
///////////////////////////////////////////////////////////////////////////////////////////////////
587
588
  public void logMessage(String message)
589
    {
590
    android.util.Log.e("BandagedCreator", message );
591
    }
592 9530f6b0 Leszek Koltunski
}