Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericTab.java @ 6cc818f6

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.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 GenericTab extends Fragment implements AdapterView.OnItemSelectedListener
42
  {
43
  private EffectType mType;
44
  private ArrayList<GenericEffect> mList;
45
  private int mEffectAdd;
46
  private EffectName[] mEffectNames;
47
  private DistortedEffects mEffects;
48
  private String[] mEffectStrings;
49
  private int mTab, mLayout, mSpinner;
50
  private int mChildren;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  public GenericTab()
55
    {
56
    mList = new ArrayList<>();
57
    mEffectAdd = 0;
58
    mChildren  = 0;
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  @Override
64
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
65
    {
66
    Bundle args = getArguments();
67

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

    
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
      }
93

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

    
96
    if( act!=null )
97
      {
98
      act.setTab(position, this);
99
      mEffects = act.getEffects();
100
      }
101

    
102
    createEffectNames(mType);
103
    mEffectStrings = new String[mEffectNames.length];
104

    
105
    for (int i = 0; i < mEffectNames.length; i++)
106
      {
107
      mEffectStrings[i] = mEffectNames[i].name();
108
      }
109

    
110
    return inflater.inflate( mTab, container, false);
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  @Override
116
  public void onStart()
117
    {
118
    super.onStart();
119

    
120
    GenericActivity2 act = (GenericActivity2)getActivity();
121

    
122
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>( act, android.R.layout.simple_spinner_item, mEffectStrings);
123
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
124

    
125
    Spinner effectSpinner = act.findViewById(mSpinner);
126

    
127
    effectSpinner.setOnItemSelectedListener(this);
128
    effectSpinner.setAdapter(adapterEffect);
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
   @Override
134
   public void setUserVisibleHint(boolean isVisibleToUser)
135
     {
136
     super.setUserVisibleHint(isVisibleToUser);
137

    
138
     if (isVisibleToUser)
139
       {
140
       if( mType!=null )
141
         {
142
         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

    
151
         act.setCurrentEffectType(mType);
152
         }
153
       }
154
     }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
159
    {
160
    if( parent.getId() == mSpinner )
161
      {
162
      mEffectAdd = pos;
163
      }
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
  public void onNothingSelected(AdapterView<?> parent)
169
    {
170
    }
171

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

    
174
  void newEffect()
175
    {
176
    GenericActivity2 act = (GenericActivity2)getActivity();
177
    GenericEffect eff = new GenericEffect(mEffectNames[mEffectAdd], act );
178
    mList.add(eff);
179

    
180
    mChildren++;
181

    
182
    LinearLayout layout = act.findViewById(mLayout);
183
    View view = eff.createView(mChildren);
184
    layout.addView(view);
185

    
186
    if( mEffectNames[mEffectAdd].getCenterDimension() > 0 )
187
      {
188
      View center = eff.createCenter(mChildren);
189
      layout.addView(center);
190
      }
191

    
192
    if( mEffectNames[mEffectAdd].getRegionDimension() > 0 )
193
      {
194
      View region = eff.createRegion(mChildren);
195
      layout.addView(region);
196
      }
197

    
198
    eff.apply(mEffects);
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  private void createEffectNames(EffectType type)
204
    {
205
    EffectName[] names = EffectName.values();
206

    
207
    int numEffects=0;
208

    
209
    for(int i=0; i<names.length; i++)
210
      if( names[i].getType() == type ) numEffects++;
211

    
212
    mEffectNames = new EffectName[numEffects];
213

    
214
    numEffects=0;
215

    
216
    for(int i=0; i<names.length; i++)
217
      if( names[i].getType() == type )
218
        {
219
        mEffectNames[numEffects++] = names[i];
220
        }
221
    }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
  void removeAll()
226
    {
227
    GenericActivity2 act = (GenericActivity2)getActivity();
228

    
229
    mChildren = 0;
230

    
231
    mList.clear();
232
    LinearLayout layout = act.findViewById(mLayout);
233
    layout.removeAllViews();
234
    mEffects.abortByType(mType);
235

    
236
    act.resetData();
237

    
238
    if( mType==EffectType.MATRIX )
239
      {
240
      act.resetMatrixEffects();
241
      }
242
    }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
  void remove(View v)
247
    {
248
    for(GenericEffect effect: mList)
249
      {
250
      if( effect.thisView(v) )
251
        {
252
        GenericActivity2 act = (GenericActivity2)getActivity();
253
        LinearLayout layout = act.findViewById(mLayout);
254
        View view;
255

    
256
        view = effect.getEffect();
257
        if( view!=null ) layout.removeView(view);
258
        view = effect.getCenter();
259
        if( view!=null ) layout.removeView(view);
260
        view = effect.getRegion();
261
        if( view!=null ) layout.removeView(view);
262

    
263
        mEffects.abortById(effect.getId());
264

    
265
        int index = mList.indexOf(effect);
266
        int capac = mList.size();
267

    
268
        for(int i=index+1; i<capac; i++)
269
          {
270
          mList.get(i).setBackground(i-1);
271
          }
272

    
273
        mList.remove(effect);
274
        mChildren--;
275

    
276
        break;
277
        }
278
      }
279
    }
280

    
281
  }
(6-6/7)