Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DTab.java @ 3b1e9c7e

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
  private int mChildren;
51

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

    
54
  public Effects3DTab()
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.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
    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
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
114

    
115
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>( act, android.R.layout.simple_spinner_item, mEffectStrings);
116
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
117

    
118
    Spinner effectSpinner = act.findViewById(mSpinner);
119

    
120
    effectSpinner.setOnItemSelectedListener(this);
121
    effectSpinner.setAdapter(adapterEffect);
122

    
123
    resetData();
124
    }
125

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

    
128
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
129
    {
130
    if( parent.getId() == mSpinner )
131
      {
132
      mEffectAdd = pos;
133
      }
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  public void onNothingSelected(AdapterView<?> parent)
139
    {
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
  private void resetData()
145
    {
146
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
147
    Effects3DSurfaceView view = act.findViewById(R.id.effects3dSurfaceView);
148
    view.getRenderer().showRegionAndCenter(false,false);
149
    }
150

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

    
153
  void newEffect()
154
    {
155
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
156
    Effects3DEffect eff = new Effects3DEffect(mEffectNames[mEffectAdd], act );
157
    mList.add(eff);
158

    
159
    mChildren++;
160

    
161
    LinearLayout layout = act.findViewById(mLayout);
162
    View view = eff.createView(mChildren);
163
    layout.addView(view);
164

    
165
    if( mEffectNames[mEffectAdd].supportsCenter() )
166
      {
167
      View center = eff.createCenter(mChildren);
168
      layout.addView(center);
169
      }
170

    
171
    if( mEffectNames[mEffectAdd].supportsRegion() )
172
      {
173
      View region = eff.createRegion(mChildren);
174
      layout.addView(region);
175
      }
176

    
177
    eff.apply(mEffects);
178

    
179
    boolean region = act.getShowRegion();
180
    boolean center = act.getShowCenter();
181

    
182
    boolean show = (mEffectNames[mEffectAdd].getType()==EffectType.VERTEX);
183
    Effects3DSurfaceView sv = act.findViewById(R.id.effects3dSurfaceView);
184
    sv.getRenderer().showRegionAndCenter( (show && region) , (show && center) );
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  private void createEffectNames(EffectType type)
190
    {
191
    EffectName[] names = EffectName.values();
192

    
193
    int numEffects=0;
194

    
195
    for(int i=0; i<names.length; i++)
196
      if( names[i].getType() == type ) numEffects++;
197

    
198
    mEffectNames = new EffectName[numEffects];
199

    
200
    numEffects=0;
201

    
202
    for(int i=0; i<names.length; i++)
203
      if( names[i].getType() == type )
204
        {
205
        mEffectNames[numEffects++] = names[i];
206
        }
207
    }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
  void removeAll()
212
    {
213
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
214

    
215
    mChildren = 0;
216

    
217
    mList.clear();
218
    LinearLayout layout = act.findViewById(mLayout);
219
    layout.removeAllViews();
220
    mEffects.abortByType(mType);
221

    
222
    resetData();
223
    act.resetData();
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  void remove(View v)
229
    {
230
    for(Effects3DEffect effect: mList)
231
      {
232
      if( effect.thisView(v) )
233
        {
234
        Effects3DActivity2 act = (Effects3DActivity2)getActivity();
235
        LinearLayout layout = act.findViewById(mLayout);
236
        View view;
237

    
238
        view = effect.getEffect();
239
        if( view!=null ) layout.removeView(view);
240
        view = effect.getCenter();
241
        if( view!=null ) layout.removeView(view);
242
        view = effect.getRegion();
243
        if( view!=null ) layout.removeView(view);
244

    
245
        mEffects.abortById(effect.getId());
246

    
247
        int index = mList.indexOf(effect);
248
        int capac = mList.size();
249

    
250
        for(int i=index+1; i<capac; i++)
251
          {
252
          mList.get(i).setBackground(i-1);
253
          }
254

    
255
        mList.remove(effect);
256
        mChildren--;
257

    
258
        resetData();
259

    
260
        break;
261
        }
262
      }
263
    }
264

    
265
  }
(6-6/7)