Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DTab.java @ 0be78976

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
    }
58
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61 cf6fb87f Leszek Koltunski
  @Override
62
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
63 fe7fe83e Leszek Koltunski
    {
64 cf6fb87f Leszek Koltunski
    Bundle args = getArguments();
65
66
    int position = args.getInt("position");
67
68
    switch(position)
69
      {
70
      case 0: mTab     = R.layout.effects3dtab1;
71
              mLayout  = R.id.effects3dlayout1;
72
              mSpinner = R.id.effects3dspinner1;
73
              mType    = EffectType.VERTEX;
74
              break;
75
      case 1: mTab     = R.layout.effects3dtab2;
76
              mLayout  = R.id.effects3dlayout2;
77
              mSpinner = R.id.effects3dspinner2;
78
              mType    = EffectType.FRAGMENT;
79
              break;
80
      case 2: mTab     = R.layout.effects3dtab3;
81
              mLayout  = R.id.effects3dlayout3;
82
              mSpinner = R.id.effects3dspinner3;
83
              mType    = EffectType.POSTPROCESS;
84
              break;
85
      }
86 fe7fe83e Leszek Koltunski
87 348dbeea Leszek Koltunski
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
88 06c636a5 Leszek Koltunski
    act.setTab(position, this);
89
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
    View rootView = inflater.inflate( mTab, container, false);
101
    return rootView;
102
    }
103
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
106
  @Override
107
  public void onStart()
108
    {
109
    super.onStart();
110
111 348dbeea Leszek Koltunski
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
112 fe7fe83e Leszek Koltunski
113
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>( act, android.R.layout.simple_spinner_item, mEffectStrings);
114
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
115
116
    Spinner effectSpinner = act.findViewById(mSpinner);
117 cf6fb87f Leszek Koltunski
118 fe7fe83e Leszek Koltunski
    effectSpinner.setOnItemSelectedListener(this);
119
    effectSpinner.setAdapter(adapterEffect);
120
121
    resetData();
122
    }
123
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
126
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
127
    {
128
    if( parent.getId() == mSpinner )
129
      {
130
      mEffectAdd = pos;
131
      }
132
    }
133
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
136
  public void onNothingSelected(AdapterView<?> parent)
137
    {
138
    }
139
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
142
  private void resetData()
143
    {
144 348dbeea Leszek Koltunski
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
145 cf6fb87f Leszek Koltunski
    Effects3DSurfaceView view = act.findViewById(R.id.effects3dSurfaceView);
146 fe7fe83e Leszek Koltunski
    view.getRenderer().showRegionAndCenter(false,false);
147
    }
148
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
151
  void newEffect()
152
    {
153 348dbeea Leszek Koltunski
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
154 cf6fb87f Leszek Koltunski
    Effects3DEffect eff = new Effects3DEffect(mEffectNames[mEffectAdd], act );
155 fe7fe83e Leszek Koltunski
    mList.add(eff);
156
157 cf6fb87f Leszek Koltunski
    LinearLayout layout = act.findViewById(mLayout);
158 fe7fe83e Leszek Koltunski
    View view = eff.createView();
159
    layout.addView(view);
160
161
    if( mEffectNames[mEffectAdd].supportsCenter() )
162
      {
163
      View center = eff.createCenter();
164
      layout.addView(center);
165
      }
166
167
    if( mEffectNames[mEffectAdd].supportsRegion() )
168
      {
169
      View region = eff.createRegion();
170
      layout.addView(region);
171
      }
172
173
    eff.apply(mEffects);
174
175
    boolean region = act.getShowRegion();
176
    boolean center = act.getShowCenter();
177
178
    boolean show = (mEffectNames[mEffectAdd].getType()==EffectType.VERTEX);
179
    Effects3DSurfaceView sv = act.findViewById(R.id.effects3dSurfaceView);
180
    sv.getRenderer().showRegionAndCenter( (show && region) , (show && center) );
181
    }
182
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
185
  private void createEffectNames(EffectType type)
186
    {
187
    EffectName[] names = EffectName.values();
188
189
    int numEffects=0;
190
191
    for(int i=0; i<names.length; i++)
192
      if( names[i].getType() == type ) numEffects++;
193
194
    mEffectNames = new EffectName[numEffects];
195
196
    numEffects=0;
197
198
    for(int i=0; i<names.length; i++)
199
      if( names[i].getType() == type )
200
        {
201
        mEffectNames[numEffects++] = names[i];
202
        }
203
    }
204
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206
207
  void removeAll()
208
    {
209 348dbeea Leszek Koltunski
    Effects3DActivity2 act = (Effects3DActivity2)getActivity();
210 fe7fe83e Leszek Koltunski
211
    mList.clear();
212
    LinearLayout layout = act.findViewById(mLayout);
213
    layout.removeAllViews();
214
    mEffects.abortByType(mType);
215
216
    resetData();
217
    act.resetData();
218
    }
219
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221
222
  void remove(View v)
223
    {
224
    for(Effects3DEffect effect: mList)
225
      {
226
      if( effect.thisView(v) )
227
        {
228 348dbeea Leszek Koltunski
        Effects3DActivity2 act = (Effects3DActivity2)getActivity();
229 cf6fb87f Leszek Koltunski
        LinearLayout layout = act.findViewById(mLayout);
230 fe7fe83e Leszek Koltunski
        View view;
231
232
        view = effect.getEffect();
233
        if( view!=null ) layout.removeView(view);
234
        view = effect.getCenter();
235
        if( view!=null ) layout.removeView(view);
236
        view = effect.getRegion();
237
        if( view!=null ) layout.removeView(view);
238
239
        mEffects.abortById(effect.getId());
240
        mList.remove(effect);
241
242
        resetData();
243
244
        break;
245
        }
246
      }
247
    }
248
249
  }