Project

General

Profile

« Previous | Next » 

Revision 72e386ef

Added by Leszek Koltunski over 2 years ago

Bandaged 3x3: progress with saving.

View differences:

src/main/java/org/distorted/bandaged/BandagedCreatorRenderer.java
22 22
import javax.microedition.khronos.egl.EGLConfig;
23 23
import javax.microedition.khronos.opengles.GL10;
24 24

  
25
import android.app.Activity;
25 26
import android.opengl.GLSurfaceView;
27
import android.widget.Toast;
26 28

  
27 29
import org.distorted.dialogs.RubikDialogSaveBandaged;
28 30
import org.distorted.library.main.DistortedLibrary;
......
31 33

  
32 34
import org.distorted.library.type.Static3D;
33 35
import org.distorted.library.type.Static4D;
36
import org.distorted.objectlib.json.JsonWriter;
37
import org.distorted.objectlib.main.TwistyObject;
38
import org.distorted.objectlib.objects.TwistyBandagedGeneric;
39
import org.json.JSONException;
40

  
41
import java.io.File;
42
import java.io.FileNotFoundException;
43
import java.io.IOException;
34 44

  
35 45
///////////////////////////////////////////////////////////////////////////////////////////////////
36 46

  
......
316 326
///////////////////////////////////////////////////////////////////////////////////////////////////
317 327

  
318 328
   public void saveObject()
329
     {
330
     int len = POSITIONS.length;
331
     int numAttached=0;
332

  
333
     for(int i=0; i<len; i++)
334
       if( mCubits[i].isAttached() ) numAttached++;
335

  
336
     float[][] pos = new float[numAttached][];
337
     int attached=0;
338

  
339
     for(int i=0; i<len; i++)
340
       if( mCubits[i].isAttached() )
341
         {
342
         pos[attached++] = mCubits[i].getPosition();
343
         }
344

  
345
     TwistyBandagedGeneric.setPositions(pos);
346
     TwistyObject obj = new TwistyBandagedGeneric();
347
     BandagedCreatorActivity act = (BandagedCreatorActivity) mView.getContext();
348

  
349
     createObjectJson(obj,act);
350
     }
351

  
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

  
354
   private void createObjectJson(TwistyObject object, Activity act)
355
     {
356
     final String name = object.getShortName()+"_object.json";
357
     File file = new File(act.getFilesDir(), name);
358
     String filename = file.getAbsolutePath();
359

  
360
     try
361
       {
362
       JsonWriter writer = JsonWriter.getInstance();
363
       String json = writer.createObjectString(object,24);
364
       writer.write(filename,json);
365

  
366
       act.runOnUiThread(new Runnable()
367
         {
368
         public void run()
369
           {
370
           String message = "Saving to \n\n"+filename+"\n\nsuccessful";
371
           Toast.makeText(act,message,Toast.LENGTH_LONG).show();
372
           }
373
         });
374
       }
375
     catch(JSONException ex)
376
       {
377
       act.runOnUiThread(new Runnable()
378
         {
379
         public void run()
380
           {
381
           String message = "JSON 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
       }
386
     catch(FileNotFoundException ex)
387
       {
388
       act.runOnUiThread(new Runnable()
389
         {
390
         public void run()
391
           {
392
           String message = "FileNotFound exception saving to \n\n"+filename+"\n\n failed:\n\n"+ex.getMessage();
393
           Toast.makeText(act,message,Toast.LENGTH_LONG).show();
394
           }
395
         });
396
       }
397
     catch(IOException ex)
398
       {
399
       act.runOnUiThread(new Runnable()
400
         {
401
         public void run()
402
           {
403
           String message = "IO exception saving to \n\n"+filename+"\n\n failed:\n\n"+ex.getMessage();
404
           Toast.makeText(act,message,Toast.LENGTH_LONG).show();
405
           }
406
         });
407
       }
408
     }
409

  
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411
/*
412
   private void createIcon(TwistyObject object)
413
     {
414
     if( !mIconActive )
415
       {
416
       mIconActive = true;
417

  
418
       ObjectControl control = getControl();
419
       DistortedEffects effects = object.getObjectEffects();
420

  
421
       Static4D defRot = ShapeHexahedron.DEFAULT_ROT;
422
       Static1D halo = new Static1D(5);
423
       control.rotateNow(defRot);
424
       float scale = 1.15f;
425
       control.scaleNow(scale);
426
       Static4D color = new Static4D(0,0,0,1);
427
       PostprocessEffectBorder border = new PostprocessEffectBorder(halo,color);
428
       border.setHaloDepth(false);
429
       effects.apply(border);
430
       String objectName = object.getShortName();
431
       final String name = STORAGE+objectName+".png";
432
       renderer.setSaveIcon(name);
433
       }
434
     }
435
*/
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437

  
438
   public void displaySavingDialog()
319 439
     {
320 440
     BandagedCreatorActivity act = (BandagedCreatorActivity)mView.getContext();
321 441
     RubikDialogSaveBandaged saveDiag = new RubikDialogSaveBandaged();
src/main/java/org/distorted/bandaged/BandagedCreatorScreen.java
73 73
      public void onClick(View v)
74 74
        {
75 75
        BandagedCreatorRenderer renderer = act.getRenderer();
76
        if( !renderer.isBusy() ) renderer.saveObject();
76
        if( !renderer.isBusy() ) renderer.displaySavingDialog();
77 77
        }
78 78
      });
79 79
    }
src/main/java/org/distorted/dialogs/RubikDialogSaveBandaged.java
36 36
import androidx.appcompat.app.AppCompatDialogFragment;
37 37
import androidx.fragment.app.FragmentActivity;
38 38

  
39
import org.distorted.bandaged.BandagedCreatorActivity;
40
import org.distorted.bandaged.BandagedCreatorRenderer;
39 41
import org.distorted.main.R;
40 42
import org.distorted.main.RubikActivity;
41 43

  
......
70 72
      @Override
71 73
      public void onClick(DialogInterface dialog, int which)
72 74
        {
75
        BandagedCreatorActivity bact = (BandagedCreatorActivity)getContext();
73 76

  
77
        if( bact!=null )
78
          {
79
          BandagedCreatorRenderer rend = bact.getRenderer();
80
          rend.saveObject();
81
          }
74 82
        }
75 83
      });
76 84

  
......
79 87
      @Override
80 88
      public void onClick(DialogInterface dialog, int which)
81 89
        {
82

  
90
        // empty
83 91
        }
84 92
      });
85 93

  

Also available in: Unified diff