Project

General

Profile

« Previous | Next » 

Revision 1e7603bb

Added by Leszek Koltunski over 5 years ago

Refactor the 'Generic' app.

View differences:

src/main/java/org/distorted/examples/generic/GenericActivity2.java
26 26
import android.opengl.GLSurfaceView;
27 27
import android.os.Bundle;
28 28
import android.support.design.widget.TabLayout;
29
import android.support.v4.view.ViewPager;
30 29
import android.support.v7.app.AppCompatActivity;
31 30
import android.view.View;
32 31
import android.widget.CheckBox;
......
41 40
import org.distorted.library.mesh.MeshBase;
42 41
import org.distorted.library.mesh.MeshQuad;
43 42
import org.distorted.library.mesh.MeshSphere;
43
import org.distorted.library.type.Static3D;
44 44
import org.distorted.library.type.Static4D;
45 45

  
46 46
import java.io.IOException;
......
50 50

  
51 51
public class GenericActivity2 extends AppCompatActivity
52 52
  {
53
  public static final int NUM_TABS = 4;
54

  
55 53
  private DistortedTexture mTexture;
56 54
  private MeshBase mMesh;
57 55
  private Bitmap mBitmap;
58

  
59
  private float mCenterX, mCenterY, mCenterZ;
60
  private float mRegionX, mRegionY, mRegionZ, mRegionR;
61 56
  private DistortedEffects mEffects;
62
  private ViewPager mViewPager;
57
  private GenericViewPager mViewPager;
63 58

  
64
  private boolean mShowCenter;
65
  private boolean mShowRegion;
66
  private boolean mShowNormal;
67
  private boolean mUseOIT    ;
68
  private EffectType mCurrentType;
69

  
70
  private GenericTab[] mTab;
59
  private boolean mShowCenter, mShowRegion, mShowNormal, mUseOIT;
71 60

  
72 61
///////////////////////////////////////////////////////////////////////////////////////////////////
73 62

  
......
78 67

  
79 68
    setTheme(R.style.CustomActivityThemeNoActionBar);
80 69

  
81
    mTab = new GenericTab[NUM_TABS];
82

  
83 70
    Bundle b = getIntent().getExtras();
84 71

  
85 72
    int objectType = b.getInt("type");
......
94 81
    mShowNormal = false;
95 82
    mUseOIT     = false;
96 83

  
97
    mCurrentType = EffectType.MATRIX;
98

  
99 84
    int maxsize = numCols > numRows ? (numCols>numSlic ? numCols:numSlic) : (numRows>numSlic ? numRows:numSlic) ;
100 85

  
101 86
    createBitmap(maxsize, bitmapID);
......
131 116

  
132 117
    setContentView(view);
133 118

  
134
    mViewPager = findViewById(R.id.generic_viewpager);
135
    mViewPager.setOffscreenPageLimit( NUM_TABS-1 );
136
    GenericTabViewPager pager = new GenericTabViewPager(this, getSupportFragmentManager() );
137
    mViewPager.setAdapter(pager);
119
    mViewPager = new GenericViewPager(this,R.id.generic_viewpager);
138 120
    TabLayout tabLayout = findViewById(R.id.generic_sliding_tabs);
139
    tabLayout.setupWithViewPager(mViewPager);
140

  
141
    resetData();
121
    tabLayout.setupWithViewPager(mViewPager.getPager());
142 122
    }
143 123

  
144 124
///////////////////////////////////////////////////////////////////////////////////////////////////
......
207 187
    return mBitmap;
208 188
    }
209 189

  
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

  
212
  void setRegion(float x, float y, float z, float r)
213
    {
214
    mRegionX = x;
215
    mRegionY =-y;
216
    mRegionZ = z;
217
    mRegionR = r;
218

  
219
    setRendererRegion();
220
    }
221

  
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

  
224
  void setRendererRegion()
225
    {
226
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
227
    view.getRenderer().setRegion(mRegionX, mRegionY, mRegionZ, mRegionR);
228
    }
229

  
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

  
232
  void setCenter(float x, float y, float z)
233
    {
234
    mCenterX = x;
235
    mCenterY = y;
236
    mCenterZ = z;
237

  
238
    setRendererCenter();
239
    }
240

  
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

  
243
  void setRendererCenter()
244
    {
245
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
246
    view.getRenderer().setCenter( mCenterX, mCenterY, mCenterZ );
247
    }
248

  
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

  
251
  public float getCenterX()
252
    {
253
    return mCenterX;
254
    }
255

  
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

  
258
  public float getCenterY()
259
    {
260
    return mCenterY;
261
    }
262

  
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

  
265
  public float getRegionX()
266
    {
267
    return mRegionX;
268
    }
269

  
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

  
272
  public float getRegionY()
273
    {
274
    return mRegionY;
275
    }
276

  
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278

  
279
  public float getRegionZ()
280
    {
281
    return mRegionZ;
282
    }
283

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

  
286
  public float getRegionR()
287
    {
288
    return mRegionR;
289
    }
290

  
291 190
///////////////////////////////////////////////////////////////////////////////////////////////////
292 191

  
293 192
  public int getWidth()
......
319 218

  
320 219
///////////////////////////////////////////////////////////////////////////////////////////////////
321 220

  
322
  void resetData()
221
  void setCenter(Static3D center)
323 222
    {
324
    mCenterX = 0.5f*getWidth();
325
    mCenterY = 0.5f*getHeight();
326
    mRegionX = 0;
327
    mRegionY = 0;
328
    mRegionZ = 0;
329
    mRegionR = getWidth()/2;
330

  
331 223
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
332
    GenericRenderer renderer= view.getRenderer();
333

  
334
    renderer.setCenter( mCenterX, mCenterY, mCenterZ );
335
    renderer.setRegion( mRegionX, mRegionY, mRegionZ, mRegionR );
224
    view.getRenderer().setCenter( center.get1(), center.get2(), center.get3() );
336 225
    }
337 226

  
338 227
///////////////////////////////////////////////////////////////////////////////////////////////////
339 228

  
340
  void resetMatrixEffects()
229
  void setRegion(Static4D region)
341 230
    {
342 231
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
343
    GenericRenderer renderer= view.getRenderer();
344

  
345
    renderer.resetMatrixEffects();
232
    view.getRenderer().setRegion( region.get1(), region.get2(), region.get3(), region.get4() );
346 233
    }
347 234

  
348 235
///////////////////////////////////////////////////////////////////////////////////////////////////
349 236

  
350
  void setCurrentEffectType(EffectType type)
237
  void resetMatrixEffects()
351 238
    {
352
    mCurrentType = type;
353
    }
354

  
355
///////////////////////////////////////////////////////////////////////////////////////////////////
239
    GenericSurfaceView view = findViewById(R.id.genericSurfaceView);
240
    GenericRenderer renderer= view.getRenderer();
356 241

  
357
  public DistortedEffects getEffects()
358
    {
359
    return mEffects;
242
    renderer.resetMatrixEffects();
360 243
    }
361 244

  
362 245
///////////////////////////////////////////////////////////////////////////////////////////////////
363 246

  
364
  public void newEffect(View v)
247
  void setCurrentTab(EffectType type)
365 248
    {
366
    int pos = mViewPager.getCurrentItem();
367

  
368
    if( pos>=0 && pos<NUM_TABS ) mTab[pos].newEffect();
249
    mViewPager.setCurrentTab(type);
369 250
    }
370 251

  
371 252
///////////////////////////////////////////////////////////////////////////////////////////////////
372 253

  
373
  public void removeAll(View v)
254
  void setTab(GenericTab tab, int position)
374 255
    {
375
    int pos = mViewPager.getCurrentItem();
376

  
377
    if( pos>=0 && pos<NUM_TABS ) mTab[pos].removeAll();
256
    mViewPager.setTab(tab,position);
378 257
    }
379 258

  
380 259
///////////////////////////////////////////////////////////////////////////////////////////////////
381 260

  
382
  public void remove(View v)
383
    {
384
    int pos = mViewPager.getCurrentItem();
385

  
386
    if( pos>=0 && pos<NUM_TABS ) mTab[pos].remove(v);
387
    }
388

  
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

  
391
  void setTab(int pos, GenericTab tab)
261
  public DistortedEffects getEffects()
392 262
    {
393
    if( pos>=0 && pos<NUM_TABS ) mTab[pos] = tab;
263
    return mEffects;
394 264
    }
395 265

  
396 266
///////////////////////////////////////////////////////////////////////////////////////////////////
397 267

  
398
  boolean getShowCenter()
268
  public void newEffect(View v)
399 269
    {
400
    return mShowCenter;
270
    mViewPager.newEffect();
401 271
    }
402 272

  
403 273
///////////////////////////////////////////////////////////////////////////////////////////////////
404 274

  
405
  boolean getShowRegion()
275
  public void removeAll(View v)
406 276
    {
407
    return mShowRegion;
277
    mViewPager.removeAll();
408 278
    }
409 279

  
410 280
///////////////////////////////////////////////////////////////////////////////////////////////////
411 281

  
412
  public void showCenter(View view)
282
  public void remove(View v)
413 283
    {
414
    CheckBox box = (CheckBox)view;
415
    mShowCenter = box.isChecked();
416

  
417
    boolean showR=supportsRegion(mCurrentType);
418
    boolean showC=supportsCenter(mCurrentType);
419

  
420
    GenericSurfaceView sv = findViewById(R.id.genericSurfaceView);
421
    sv.getRenderer().showRegionAndCenter( (mShowRegion&&showR) , (mShowCenter&&showC) );
284
    mViewPager.remove(v);
422 285
    }
423 286

  
424 287
///////////////////////////////////////////////////////////////////////////////////////////////////
425 288

  
426
  public void showRegion(View view)
289
  public void showRegionAndCenter(View view)
427 290
    {
428
    CheckBox box = (CheckBox)view;
429
    mShowRegion = box.isChecked();
430

  
431
    boolean showR=supportsRegion(mCurrentType);
432
    boolean showC=supportsCenter(mCurrentType);
291
    CheckBox center = findViewById(R.id.genericCheckBoxCenter);
292
    mShowCenter = center.isChecked();
293
    CheckBox region = findViewById(R.id.genericCheckBoxRegion);
294
    mShowRegion = region.isChecked();
433 295

  
434 296
    GenericSurfaceView sv = findViewById(R.id.genericSurfaceView);
435
    sv.getRenderer().showRegionAndCenter( (mShowRegion&&showR) , (mShowCenter&&showC) );
436
    }
437

  
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439

  
440
  boolean supportsCenter(EffectType type)
441
    {
442
    switch(type)
443
      {
444
      case MATRIX     : return true;
445
      case VERTEX     : return true;
446
      case FRAGMENT   : return true;
447
      case POSTPROCESS: return false;
448
      }
449

  
450
    return false;
451
    }
452

  
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454

  
455
  boolean supportsRegion(EffectType type)
456
    {
457
    switch(type)
458
      {
459
      case MATRIX     : return false;
460
      case VERTEX     : return true;
461
      case FRAGMENT   : return false;
462
      case POSTPROCESS: return false;
463
      }
464 297

  
465
    return false;
298
    mViewPager.showRegionAndCenter(mShowRegion, mShowCenter, sv);
466 299
    }
467 300

  
468 301
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/examples/generic/GenericEffect.java
620 620
        seek[i].setProgress(mInterRegion[i]);
621 621
        }
622 622

  
623
      act.setRegion(mRegion4Sta.get1(), mRegion4Sta.get2(), mRegion4Sta.get3(), mRegion4Sta.get4());
623
      act.setRegion(mRegion4Sta);
624 624
      }
625 625
    else
626 626
      {
......
647 647
        seek[i].setProgress(mInterRegion[i]);
648 648
        }
649 649

  
650
      act.setRegion(mRegion3Sta.get1(), mRegion3Sta.get2(), mRegion3Sta.get3(), 1.0f );
650
      act.setRegion( new Static4D( mRegion3Sta.get1(), mRegion3Sta.get2(), mRegion3Sta.get3(), 1.0f) );
651 651
      }
652 652

  
653 653
    return mRegion;
......
682 682
      seek[i].setProgress( mInterCenter[i] );
683 683
      }
684 684

  
685
    act.setCenter(mCenterSta.get1(),mCenterSta.get2(),mCenterSta.get3());
685
    act.setCenter(mCenterSta);
686 686

  
687 687
    return mCenter;
688 688
    }
......
769 769
    if( fromUser )
770 770
      {
771 771
      GenericActivity2 act = mAct.get();
772
      act.setCenter(mCenterSta.get1(),mCenterSta.get2(),mCenterSta.get3());
773
      act.setRegion(mRegion4Sta.get1(),mRegion4Sta.get2(),mRegion4Sta.get3(), mRegion4Sta.get4());
772
      act.setCenter(mCenterSta);
773
      act.setRegion(mRegion4Sta);
774 774
      }
775 775
    }
776 776

  
src/main/java/org/distorted/examples/generic/GenericRenderer.java
62 62
    private Static3D mRotateCen, mMoveObject, mScaleObject, mMoveCenter, mScaleCenter, mMoveRegion, mMoveBackground, mScaleBackground;
63 63
    private boolean mShowingCenter=false;
64 64
    private boolean mShowingRegion=false;
65
    private float mFactorObj, mFactorReg;
65
    private float mFactorObj;
66 66
    private int mWidth;
67 67
    private float mFactor;
68 68

  
......
91 91
      mCenterTexture     = new DistortedTexture(100,100);
92 92
      mRegionTexture     = new DistortedTexture(100,100);
93 93

  
94
      mFactorObj = 1.0f;
95

  
94 96
      DistortedEffects objectEffects     = act.getEffects();
95 97
      DistortedEffects backgroundEffects = new DistortedEffects();
96 98
      DistortedEffects centerEffects     = new DistortedEffects();
......
174 176
      {
175 177
      if( mShowingCenter!=showCenter  )
176 178
        {
177
        if( showCenter ) mScreen.attach(mCenterNode);
178
        else             mScreen.detach(mCenterNode);
179
        if( showCenter )
180
          {
181
          if( mShowingRegion ) mScreen.detach(mRegionNode);
182
          mScreen.attach(mCenterNode);
183
          if( mShowingRegion ) mScreen.attach(mRegionNode);
184
          }
185
        else
186
          {
187
          mScreen.detach(mCenterNode);
188
          }
179 189

  
180 190
        mShowingCenter = showCenter;
181 191
        }
......
207 217

  
208 218
    void setRegion(float x, float y, float z, float r)
209 219
      {
210
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
211
      mRegionPoint.set(mFactorObj*x,-mFactorObj*y, mFactorObj*z);
212
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
220
      float factorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
221

  
222
      mRegionPoint.set(mFactorObj*x,mFactorObj*y, mFactorObj*z);
223
      mRegionScalePoint.set(factorReg,factorReg,factorReg);
213 224
      }
214 225

  
215 226
///////////////////////////////////////////////////////////////////////////////////////////////////
......
235 246

  
236 247
      float factorCen;
237 248
      int centerSize = mCenterTexture.getWidth();
238
      int regionSize = mRegionTexture.getWidth();
249
      float oldFactorObj = mFactorObj;
239 250

  
240 251
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
241 252
        {
......
248 259
        factorCen  = (0.08f  *width)/centerSize;
249 260
        }
250 261

  
251
      GenericActivity2 act = (GenericActivity2)mView.getContext();
252
      mCenterPoint.set(mFactorObj*act.getCenterX(),+mFactorObj*act.getCenterY(),0);
253
      mRegionPoint.set(mFactorObj*act.getRegionX(),-mFactorObj*act.getRegionY(),0);
254
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
255
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
262
      float adjustFactor = mFactorObj/oldFactorObj;
263

  
264
      mCenterPoint.set1( mCenterPoint.get1()*adjustFactor );
265
      mCenterPoint.set2( mCenterPoint.get2()*adjustFactor );
266
      mCenterPoint.set3( mCenterPoint.get3()*adjustFactor );
267

  
268
      mRegionPoint.set1( mRegionPoint.get1()*adjustFactor );
269
      mRegionPoint.set2( mRegionPoint.get2()*adjustFactor );
270
      mRegionPoint.set3( mRegionPoint.get3()*adjustFactor );
271

  
272
      mRegionScalePoint.set1(mRegionScalePoint.get1()*adjustFactor);
273
      mRegionScalePoint.set2(mRegionScalePoint.get2()*adjustFactor);
274
      mRegionScalePoint.set3(mRegionScalePoint.get3()*adjustFactor);
275

  
256 276
      mMoveObject.set( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , -mFactorObj*mObjDepth );
257
      mRotateCen.set(width/2,height/2, 0);
277
      mRotateCen.set(width*0.5f,height*0.5f, 0);
258 278
      mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
259 279
      mMoveCenter.set( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
260 280
                       (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , 15 );
......
266 286
      float factorBackY = ((float)height)/backgroundSize;
267 287

  
268 288
      // quite tricky: move the background exactly to the FAR plane! (see DistortedOutputSurface.setProjection() )
269
      mMoveBackground.set( -width/2, -height/2, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360)) );
289
      mMoveBackground.set( -width*0.5f, -height*0.5f, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360)) );
270 290
      mScaleBackground.set( 2*factorBackX, 2*factorBackY, 1.0f );
271 291

  
272 292
      mScreen.resize(width, height);
src/main/java/org/distorted/examples/generic/GenericTab.java
29 29
import android.widget.LinearLayout;
30 30
import android.widget.Spinner;
31 31

  
32
import org.distorted.examples.R;
33 32
import org.distorted.library.effect.EffectName;
34 33
import org.distorted.library.effect.EffectType;
35 34
import org.distorted.library.main.DistortedEffects;
......
46 45
  private EffectName[] mEffectNames;
47 46
  private DistortedEffects mEffects;
48 47
  private String[] mEffectStrings;
49
  private int mTab, mLayout, mSpinner;
48
  private int mLayout, mSpinner;
50 49
  private int mChildren;
51 50

  
52 51
///////////////////////////////////////////////////////////////////////////////////////////////////
......
67 66

  
68 67
    int position = args.getInt("position");
69 68

  
70
    switch(position)
71
      {
72
      case 0: mTab     = R.layout.effects3dtab0;
73
              mLayout  = R.id.effects3dlayout0;
74
              mSpinner = R.id.effects3dspinner0;
75
              mType    = EffectType.MATRIX;
76
              break;
77
      case 1: mTab     = R.layout.effects3dtab1;
78
              mLayout  = R.id.effects3dlayout1;
79
              mSpinner = R.id.effects3dspinner1;
80
              mType    = EffectType.VERTEX;
81
              break;
82
      case 2: mTab     = R.layout.effects3dtab2;
83
              mLayout  = R.id.effects3dlayout2;
84
              mSpinner = R.id.effects3dspinner2;
85
              mType    = EffectType.FRAGMENT;
86
              break;
87
      case 3: mTab     = R.layout.effects3dtab3;
88
              mLayout  = R.id.effects3dlayout3;
89
              mSpinner = R.id.effects3dspinner3;
90
              mType    = EffectType.POSTPROCESS;
91
              break;
92
      }
69
    int tab  = GenericViewPager.TABS[GenericViewPager.NUM*position    ];
70
    mLayout  = GenericViewPager.TABS[GenericViewPager.NUM*position + 1];
71
    mSpinner = GenericViewPager.TABS[GenericViewPager.NUM*position + 2];
72
    mType    = EffectType.getType(GenericViewPager.TABS[GenericViewPager.NUM*position + 3]);
93 73

  
94 74
    GenericActivity2 act = (GenericActivity2)getActivity();
95 75

  
96 76
    if( act!=null )
97 77
      {
98
      act.setTab(position, this);
78
      act.setTab(this,position);
99 79
      mEffects = act.getEffects();
100 80
      }
101 81

  
......
107 87
      mEffectStrings[i] = mEffectNames[i].name();
108 88
      }
109 89

  
110
    return inflater.inflate( mTab, container, false);
90
    return inflater.inflate( tab, container, false);
111 91
    }
112 92

  
113 93
///////////////////////////////////////////////////////////////////////////////////////////////////
......
140 120
       if( mType!=null )
141 121
         {
142 122
         GenericActivity2 act = (GenericActivity2)getActivity();
143
         boolean region = act.getShowRegion();
144
         boolean center = act.getShowCenter();
145
         boolean showR  = act.supportsRegion(mType);
146
         boolean showC  = act.supportsCenter(mType);
147

  
148
         GenericSurfaceView sv = act.findViewById(R.id.genericSurfaceView);
149
         sv.getRenderer().showRegionAndCenter( (showR && region) , (showC && center) );
150 123

  
151
         act.setCurrentEffectType(mType);
124
         if( act!=null )
125
           {
126
           act.setCurrentTab(mType);
127
           act.showRegionAndCenter(null);
128
           }
152 129
         }
153 130
       }
154 131
     }
......
233 210
    layout.removeAllViews();
234 211
    mEffects.abortByType(mType);
235 212

  
236
    act.resetData();
237

  
238 213
    if( mType==EffectType.MATRIX )
239 214
      {
240 215
      act.resetMatrixEffects();
......
277 252
        }
278 253
      }
279 254
    }
280

  
281 255
  }
src/main/java/org/distorted/examples/generic/GenericTabViewPager.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
package org.distorted.examples.generic;
21

  
22
import android.support.v4.app.Fragment;
23
import android.support.v4.app.FragmentManager;
24
import android.support.v4.app.FragmentPagerAdapter;
25
import android.os.Bundle;
26

  
27
import org.distorted.examples.R;
28

  
29
import java.lang.ref.WeakReference;
30

  
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

  
33
public class GenericTabViewPager extends FragmentPagerAdapter
34
  {
35
  private WeakReference<GenericActivity2> mAct;
36

  
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

  
39
  GenericTabViewPager(GenericActivity2 act, FragmentManager fm)
40
    {
41
    super(fm);
42
    mAct = new WeakReference<>(act);
43
    }
44

  
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

  
47
  @Override
48
  public Fragment getItem(int position)
49
    {
50
    if( position>=0 && position< GenericActivity2.NUM_TABS )
51
      {
52
      GenericTab tab = new GenericTab();
53

  
54
      Bundle bundle = new Bundle();
55
      bundle.putInt("position", position);
56
      tab.setArguments(bundle);
57

  
58
      return tab;
59
      }
60

  
61
    return null;
62
    }
63

  
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

  
66
  @Override
67
  public int getCount()
68
    {
69
    return GenericActivity2.NUM_TABS;
70
    }
71

  
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

  
74
  @Override
75
  public CharSequence getPageTitle(int position)
76
    {
77
    switch (position)
78
      {
79
      case 0 : return mAct.get().getString(R.string.type_matrix);
80
      case 1 : return mAct.get().getString(R.string.type_vertex);
81
      case 2 : return mAct.get().getString(R.string.type_fragment);
82
      case 3 : return mAct.get().getString(R.string.type_postprocess);
83
      default: return null;
84
      }
85
    }
86
  }
src/main/java/org/distorted/examples/generic/GenericViewPager.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2018 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
package org.distorted.examples.generic;
21

  
22
import android.os.Bundle;
23
import android.support.v4.app.Fragment;
24
import android.support.v4.app.FragmentManager;
25
import android.support.v4.app.FragmentPagerAdapter;
26
import android.support.v4.view.ViewPager;
27
import android.support.v7.app.AppCompatActivity;
28
import android.view.View;
29

  
30
import org.distorted.examples.R;
31
import org.distorted.library.effect.EffectType;
32

  
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

  
35
class GenericViewPager
36
  {
37
  // Add here a new row to create another tab serving effects of a given type (you would also need to create
38
  // the three layouts to fill the first 3 values of the row )
39
  // Comment out a row to remove the tab.
40
  //
41
  static final int[] TABS =
42

  
43
      // LAYOUT ////////////  INNER LAYOUT ////////  SPINNER //////////////  EffectType /////////////////////  SHOW CENTER // SHOW REGION
44
    { R.layout.effects3dtab0, R.id.effects3dlayout0, R.id.effects3dspinner0, EffectType.MATRIX     .ordinal() ,             1,          0 ,
45
      R.layout.effects3dtab1, R.id.effects3dlayout1, R.id.effects3dspinner1, EffectType.VERTEX     .ordinal() ,             1,          1 ,
46
      R.layout.effects3dtab2, R.id.effects3dlayout2, R.id.effects3dspinner2, EffectType.FRAGMENT   .ordinal() ,             1,          0 ,
47
      R.layout.effects3dtab3, R.id.effects3dlayout3, R.id.effects3dspinner3, EffectType.POSTPROCESS.ordinal() ,             0,          0 };
48

  
49
  static final int NUM = 6;  // 6 ints required to describe 1 tab
50
  private static final int NUM_TABS = TABS.length/NUM;
51

  
52
  private ViewPager mViewPager;
53
  private GenericTab[] mTab;
54
  private int mCurrentTab;
55

  
56
  private class GenericTabViewPager extends FragmentPagerAdapter
57
    {
58
    GenericTabViewPager(FragmentManager fm)
59
      {
60
      super(fm);
61
      }
62

  
63
    @Override
64
    public Fragment getItem(int position)
65
      {
66
      if( position>=0 && position<NUM_TABS )
67
        {
68
        GenericTab tab = new GenericTab();  // no we cannot make tab equal to mTab[position]
69
                                            // tabs have to be set through the setTab() method
70
        Bundle bundle = new Bundle();       // otherwise it doesn't work on recreation (rotate!)
71
        bundle.putInt("position", position);
72
        tab.setArguments(bundle);
73

  
74
        return tab;
75
        }
76

  
77
      return null;
78
      }
79

  
80
    @Override
81
    public int getCount()
82
      {
83
      return NUM_TABS;
84
      }
85

  
86
    @Override
87
    public CharSequence getPageTitle(int position)
88
      {
89
      int ordinal = TABS[position*NUM + 3];
90
      return EffectType.getType(ordinal).name();
91
      }
92
    }
93

  
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

  
96
  GenericViewPager(AppCompatActivity act, int resourceID)
97
    {
98
    mViewPager = act.findViewById(resourceID);
99
    mViewPager.setOffscreenPageLimit( NUM_TABS-1 );
100
    GenericTabViewPager pager = new GenericTabViewPager( act.getSupportFragmentManager() );
101
    mViewPager.setAdapter(pager);
102

  
103
    mTab = new GenericTab[NUM_TABS];
104
    mCurrentTab = 0;
105
    }
106

  
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

  
109
  void showRegionAndCenter(boolean showR, boolean showC, GenericSurfaceView sv)
110
    {
111
    boolean localShowC = showCenter(mCurrentTab);
112
    boolean localShowR = showRegion(mCurrentTab);
113

  
114
    sv.getRenderer().showRegionAndCenter( (showR&&localShowR) , (showC&&localShowC) );
115
    }
116

  
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

  
119
  void newEffect()
120
    {
121
    int pos = mViewPager.getCurrentItem();
122

  
123
    if( pos>=0 && pos<NUM_TABS ) mTab[pos].newEffect();
124
    }
125

  
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

  
128
  void removeAll()
129
    {
130
    int pos = mViewPager.getCurrentItem();
131

  
132
    if( pos>=0 && pos<NUM_TABS ) mTab[pos].removeAll();
133
    }
134

  
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

  
137
  void remove(View v)
138
    {
139
    int pos = mViewPager.getCurrentItem();
140

  
141
    if( pos>=0 && pos<NUM_TABS ) mTab[pos].remove(v);
142
    }
143

  
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

  
146
  ViewPager getPager()
147
    {
148
    return mViewPager;
149
    }
150

  
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

  
153
  void setCurrentTab(EffectType type)
154
    {
155
    for(int i=0; i<NUM_TABS; i++)
156
      {
157
      if( TABS[i*NUM + 3] == type.ordinal() )
158
        {
159
        mCurrentTab = i;
160
        break;
161
        }
162
      }
163
    }
164

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

  
167
  void setTab(GenericTab tab, int position)
168
    {
169
    if( position>=0 && position<NUM_TABS ) mTab[position] = tab;
170
    }
171

  
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

  
174
  private static boolean showCenter(int position)
175
    {
176
    return TABS[NUM*position + 4] == 1;
177
    }
178

  
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

  
181
  private static boolean showRegion(int position)
182
    {
183
    return TABS[NUM*position + 5] == 1;
184
    }
185
  }
src/main/res/layout/genericlayout.xml
22 22
            android:layout_height="wrap_content"
23 23
            android:layout_weight="1"
24 24
            android:checked="false"
25
            android:onClick="showCenter"
25
            android:onClick="showRegionAndCenter"
26 26
            android:text="@string/show_center"
27 27
            android:textSize="12sp"/>
28 28

  
......
32 32
            android:layout_height="wrap_content"
33 33
            android:layout_weight="1"
34 34
            android:checked="false"
35
            android:onClick="showRegion"
35
            android:onClick="showRegionAndCenter"
36 36
            android:text="@string/show_region"
37 37
            android:textSize="12sp"/>
38 38

  

Also available in: Unified diff