Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DTab.java @ 06c636a5

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.effects3d;
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 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

    
58
    android.util.Log.e("tab", "TAB CONSTRUCTOR");
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.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

    
89
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
90
    act.setTab(position, this);
91

    
92
    mEffects = act.getEffects();
93

    
94
    createEffectNames(mType);
95
    mEffectStrings = new String[mEffectNames.length];
96

    
97
    for (int i = 0; i < mEffectNames.length; i++)
98
      {
99
      mEffectStrings[i] = mEffectNames[i].name();
100
      }
101

    
102
    android.util.Log.e("tab", "onCreateView, this is fragment position "+position+" type="+mType.name());
103

    
104
    View rootView = inflater.inflate( mTab, container, false);
105
    return rootView;
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
  @Override
111
  public void onStart()
112
    {
113
    super.onStart();
114

    
115
    android.util.Log.e("tab", "onStart, type="+mType.name());
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
  @Override
121
  public void onResume()
122
    {
123
    super.onResume();
124

    
125
    android.util.Log.e("tab", "onResume, type="+mType.name());
126

    
127
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
128

    
129
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>( act, android.R.layout.simple_spinner_item, mEffectStrings);
130
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
131

    
132
    Spinner effectSpinner = act.findViewById(mSpinner);
133

    
134
    if( mSpinner == R.id.effects3dspinner1 ) android.util.Log.e("tab", "onStart, mSpinner correct");
135
    if( effectSpinner==null ) android.util.Log.e("tab", "onStart, effectSpinner null");
136

    
137
    effectSpinner.setOnItemSelectedListener(this);
138
    effectSpinner.setAdapter(adapterEffect);
139

    
140
    resetData();
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
146
    {
147
    if( parent.getId() == mSpinner )
148
      {
149
      mEffectAdd = pos;
150
      }
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  public void onNothingSelected(AdapterView<?> parent)
156
    {
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  private void resetData()
162
    {
163
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
164
    Effects3DSurfaceView view = act.findViewById(R.id.effects3dSurfaceView);
165
    view.getRenderer().showRegionAndCenter(false,false);
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  void newEffect()
171
    {
172
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
173
    Effects3DEffect eff = new Effects3DEffect(mEffectNames[mEffectAdd], act );
174
    mList.add(eff);
175

    
176
    LinearLayout layout = act.findViewById(mLayout);
177
    View view = eff.createView();
178
    layout.addView(view);
179

    
180
    if( mEffectNames[mEffectAdd].supportsCenter() )
181
      {
182
      View center = eff.createCenter();
183
      layout.addView(center);
184
      }
185

    
186
    if( mEffectNames[mEffectAdd].supportsRegion() )
187
      {
188
      View region = eff.createRegion();
189
      layout.addView(region);
190
      }
191

    
192
    eff.apply(mEffects);
193

    
194
    boolean region = act.getShowRegion();
195
    boolean center = act.getShowCenter();
196

    
197
    boolean show = (mEffectNames[mEffectAdd].getType()==EffectType.VERTEX);
198
    Effects3DSurfaceView sv = act.findViewById(R.id.effects3dSurfaceView);
199
    sv.getRenderer().showRegionAndCenter( (show && region) , (show && center) );
200
    }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

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

    
208
    int numEffects=0;
209

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

    
213
    mEffectNames = new EffectName[numEffects];
214

    
215
    numEffects=0;
216

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

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
  void removeAll()
227
    {
228
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
229

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

    
235
    resetData();
236
    act.resetData();
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
  void remove(View v)
242
    {
243
    for(Effects3DEffect effect: mList)
244
      {
245
      if( effect.thisView(v) )
246
        {
247
        Effects3DActivity2 act = (Effects3DActivity2)getActivity();
248
        LinearLayout layout = act.findViewById(mLayout);
249
        View view;
250

    
251
        view = effect.getEffect();
252
        if( view!=null ) layout.removeView(view);
253
        view = effect.getCenter();
254
        if( view!=null ) layout.removeView(view);
255
        view = effect.getRegion();
256
        if( view!=null ) layout.removeView(view);
257

    
258
        mEffects.abortById(effect.getId());
259
        mList.remove(effect);
260

    
261
        resetData();
262

    
263
        break;
264
        }
265
      }
266
    }
267

    
268
  }
(6-6/7)