Project

General

Profile

Download (8.88 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / effects3d / Effects3DTab.java @ c2c45d0a

1 fe7fe83e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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.effects3d;
21
22
import android.os.Bundle;
23 c2c45d0a Leszek Koltunski
import android.support.v4.app.Fragment;
24 fe7fe83e Leszek Koltunski
import android.view.LayoutInflater;
25
import android.view.View;
26
import android.view.ViewGroup;
27
import android.widget.AdapterView;
28
import android.widget.ArrayAdapter;
29
import android.widget.LinearLayout;
30
import android.widget.Spinner;
31
32
import org.distorted.examples.R;
33
import org.distorted.library.effect.EffectName;
34
import org.distorted.library.effect.EffectType;
35
import org.distorted.library.main.DistortedEffects;
36
37
import java.util.ArrayList;
38
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41
public class Effects3DTab extends Fragment implements AdapterView.OnItemSelectedListener
42
  {
43
  private EffectType mType;
44
  private ArrayList<Effects3DEffect> mList;
45
  private int mEffectAdd;
46
  private EffectName[] mEffectNames;
47
  private DistortedEffects mEffects;
48
  private String[] mEffectStrings;
49
  private int mTab, mLayout, mSpinner;
50
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53
  public Effects3DTab()
54
    {
55
    mList = new ArrayList<>();
56
    mEffectAdd = 0;
57 cf6fb87f Leszek Koltunski
58
    android.util.Log.e("tab", "TAB CONSTRUCTOR");
59 fe7fe83e Leszek Koltunski
    }
60
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63 cf6fb87f Leszek Koltunski
  @Override
64
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
65 fe7fe83e Leszek Koltunski
    {
66 cf6fb87f Leszek Koltunski
    Bundle args = getArguments();
67
68
    int position = args.getInt("position");
69
70
    switch(position)
71
      {
72
      case 0: mTab     = R.layout.effects3dtab1;
73
              mLayout  = R.id.effects3dlayout1;
74
              mSpinner = R.id.effects3dspinner1;
75
              mType    = EffectType.VERTEX;
76
              break;
77
      case 1: mTab     = R.layout.effects3dtab2;
78
              mLayout  = R.id.effects3dlayout2;
79
              mSpinner = R.id.effects3dspinner2;
80
              mType    = EffectType.FRAGMENT;
81
              break;
82
      case 2: mTab     = R.layout.effects3dtab3;
83
              mLayout  = R.id.effects3dlayout3;
84
              mSpinner = R.id.effects3dspinner3;
85
              mType    = EffectType.POSTPROCESS;
86
              break;
87
      }
88 fe7fe83e Leszek Koltunski
89 348dbeea Leszek Koltunski
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
90 cf6fb87f Leszek Koltunski
    mEffects = act.getEffects();
91 fe7fe83e Leszek Koltunski
92
    createEffectNames(mType);
93
    mEffectStrings = new String[mEffectNames.length];
94
95
    for (int i = 0; i < mEffectNames.length; i++)
96
      {
97
      mEffectStrings[i] = mEffectNames[i].name();
98
      }
99
100 cf6fb87f Leszek Koltunski
    android.util.Log.e("tab", "onCreateView, this is fragment position "+position+" type="+mType.name());
101 fe7fe83e Leszek Koltunski
102
    View rootView = inflater.inflate( mTab, container, false);
103
    return rootView;
104
    }
105
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
108
  @Override
109
  public void onStart()
110
    {
111
    super.onStart();
112
113
    android.util.Log.e("tab", "onStart, type="+mType.name());
114 cf6fb87f Leszek Koltunski
    }
115
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117 fe7fe83e Leszek Koltunski
118 cf6fb87f Leszek Koltunski
  @Override
119
  public void onResume()
120
    {
121
    super.onResume();
122
123
    android.util.Log.e("tab", "onResume, type="+mType.name());
124
125 348dbeea Leszek Koltunski
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
126 fe7fe83e Leszek Koltunski
127
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>( act, android.R.layout.simple_spinner_item, mEffectStrings);
128
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
129
130
    Spinner effectSpinner = act.findViewById(mSpinner);
131 cf6fb87f Leszek Koltunski
132
    if( mSpinner == R.id.effects3dspinner1 ) android.util.Log.e("tab", "onStart, mSpinner correct");
133
    if( effectSpinner==null ) android.util.Log.e("tab", "onStart, effectSpinner null");
134
135 fe7fe83e Leszek Koltunski
    effectSpinner.setOnItemSelectedListener(this);
136
    effectSpinner.setAdapter(adapterEffect);
137
138
    resetData();
139
    }
140
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
143
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
144
    {
145
    if( parent.getId() == mSpinner )
146
      {
147
      mEffectAdd = pos;
148
      }
149
    }
150
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
153
  public void onNothingSelected(AdapterView<?> parent)
154
    {
155
    }
156
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
159
  private void resetData()
160
    {
161 348dbeea Leszek Koltunski
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
162 cf6fb87f Leszek Koltunski
    Effects3DSurfaceView view = act.findViewById(R.id.effects3dSurfaceView);
163 fe7fe83e Leszek Koltunski
    view.getRenderer().showRegionAndCenter(false,false);
164
    }
165
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
168
  void newEffect()
169
    {
170 348dbeea Leszek Koltunski
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
171 cf6fb87f Leszek Koltunski
    Effects3DEffect eff = new Effects3DEffect(mEffectNames[mEffectAdd], act );
172 fe7fe83e Leszek Koltunski
    mList.add(eff);
173
174 cf6fb87f Leszek Koltunski
    LinearLayout layout = act.findViewById(mLayout);
175 fe7fe83e Leszek Koltunski
    View view = eff.createView();
176
    layout.addView(view);
177
178
    if( mEffectNames[mEffectAdd].supportsCenter() )
179
      {
180
      View center = eff.createCenter();
181
      layout.addView(center);
182
      }
183
184
    if( mEffectNames[mEffectAdd].supportsRegion() )
185
      {
186
      View region = eff.createRegion();
187
      layout.addView(region);
188
      }
189
190
    eff.apply(mEffects);
191
192
    boolean region = act.getShowRegion();
193
    boolean center = act.getShowCenter();
194
195
    boolean show = (mEffectNames[mEffectAdd].getType()==EffectType.VERTEX);
196
    Effects3DSurfaceView sv = act.findViewById(R.id.effects3dSurfaceView);
197
    sv.getRenderer().showRegionAndCenter( (show && region) , (show && center) );
198
    }
199
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201
202
  private void createEffectNames(EffectType type)
203
    {
204
    EffectName[] names = EffectName.values();
205
206
    int numEffects=0;
207
208
    for(int i=0; i<names.length; i++)
209
      if( names[i].getType() == type ) numEffects++;
210
211
    mEffectNames = new EffectName[numEffects];
212
213
    numEffects=0;
214
215
    for(int i=0; i<names.length; i++)
216
      if( names[i].getType() == type )
217
        {
218
        mEffectNames[numEffects++] = names[i];
219
        }
220
    }
221
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223
224
  void removeAll()
225
    {
226 348dbeea Leszek Koltunski
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
227 fe7fe83e Leszek Koltunski
228
    mList.clear();
229
    LinearLayout layout = act.findViewById(mLayout);
230
    layout.removeAllViews();
231
    mEffects.abortByType(mType);
232
233
    resetData();
234
    act.resetData();
235
    }
236
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
239
  void remove(View v)
240
    {
241
    for(Effects3DEffect effect: mList)
242
      {
243
      if( effect.thisView(v) )
244
        {
245 348dbeea Leszek Koltunski
        Effects3DActivity2 act = (Effects3DActivity2)getActivity();
246 cf6fb87f Leszek Koltunski
        LinearLayout layout = act.findViewById(mLayout);
247 fe7fe83e Leszek Koltunski
        View view;
248
249
        view = effect.getEffect();
250
        if( view!=null ) layout.removeView(view);
251
        view = effect.getCenter();
252
        if( view!=null ) layout.removeView(view);
253
        view = effect.getRegion();
254
        if( view!=null ) layout.removeView(view);
255
256
        mEffects.abortById(effect.getId());
257
        mList.remove(effect);
258
259
        resetData();
260
261
        break;
262
        }
263
      }
264
    }
265
266
  }