Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericTab.java @ 77e66c58

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 androidx.annotation.NonNull;
24
import androidx.fragment.app.Fragment;
25
import android.view.LayoutInflater;
26
import android.view.View;
27
import android.view.ViewGroup;
28
import android.widget.AdapterView;
29
import android.widget.ArrayAdapter;
30
import android.widget.Button;
31
import android.widget.LinearLayout;
32
import android.widget.Spinner;
33

    
34
import org.distorted.examples.R;
35
import org.distorted.library.effect.EffectName;
36
import org.distorted.library.effect.EffectType;
37
import org.distorted.library.main.DistortedEffects;
38

    
39
import java.util.ArrayList;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
public class GenericTab extends Fragment implements AdapterView.OnItemSelectedListener, View.OnClickListener
44
  {
45
  private EffectType mType;
46
  private ArrayList<GenericEffect> mList;
47
  private int mEffectAdd;
48
  private EffectName[] mEffectNames;
49
  private DistortedEffects mEffects;
50
  private String[] mEffectStrings;
51
  private int mLayout, mSpinner;
52
  private int mChildren;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

    
70
    int position = args.getInt("position");
71

    
72
    int tab  = GenericViewPager.TABS[position][0];
73
    mLayout  = GenericViewPager.TABS[position][1];
74
    mSpinner = GenericViewPager.TABS[position][2];
75
    mType    = EffectType.getType(GenericViewPager.TABS[position][3]);
76

    
77
    GenericActivity2 act = (GenericActivity2)getActivity();
78

    
79
    if( act!=null ) mEffects = act.getEffects();
80

    
81
    createEffectNames(mType);
82
    mEffectStrings = new String[mEffectNames.length];
83

    
84
    for (int i = 0; i < mEffectNames.length; i++)
85
      {
86
      mEffectStrings[i] = mEffectNames[i].name();
87
      }
88

    
89
    View view = inflater.inflate( tab, container, false);
90

    
91
    Button add = view.findViewById(R.id.effects3dButtonAdd);
92
    add.setOnClickListener(this);
93
    Button rem = view.findViewById(R.id.effects3dButtonRemove);
94
    rem.setOnClickListener(this);
95

    
96
    return view;
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  @Override
102
  public void onStart()
103
    {
104
    super.onStart();
105

    
106
    GenericActivity2 act = (GenericActivity2)getActivity();
107

    
108
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>( act, android.R.layout.simple_spinner_item, mEffectStrings);
109
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
110

    
111
    Spinner effectSpinner = act.findViewById(mSpinner);
112

    
113
    effectSpinner.setOnItemSelectedListener(this);
114
    effectSpinner.setAdapter(adapterEffect);
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  @Override
120
  public void onClick(View v)
121
    {
122
    int id = v.getId();
123

    
124
    if( id == R.id.effects3dButtonAdd    ) newEffect();
125
    if( id == R.id.effects3dButtonRemove ) removeAll();
126
    if( id == R.id.button0dRemove ||
127
        id == R.id.button1dRemove ||
128
        id == R.id.button2dRemove ||
129
        id == R.id.button3dRemove ||
130
        id == R.id.button4dRemove ||
131
        id == R.id.button5dRemove  ) remove(v);
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
   @Override
137
   public void setUserVisibleHint(boolean isVisibleToUser)
138
     {
139
     super.setUserVisibleHint(isVisibleToUser);
140

    
141
     if (isVisibleToUser)
142
       {
143
       if( mType!=null )
144
         {
145
         GenericActivity2 act = (GenericActivity2)getActivity();
146

    
147
         if( act!=null )
148
           {
149
           GenericViewPager pager = act.getPager();
150
           pager.setCurrentTab(mType);
151
           act.showRegionAndCenter(null);
152
           }
153
         }
154
       }
155
     }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

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

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

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

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

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

    
181
    mChildren++;
182

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

    
187
    int dimension = eff.getDimension();
188
    Button butt;
189

    
190
    switch(dimension)
191
      {
192
      case 0: butt = view.findViewById(R.id.button0dRemove);
193
              butt.setOnClickListener(this);
194
              break;
195
      case 1: butt = view.findViewById(R.id.button1dRemove);
196
              butt.setOnClickListener(this);
197
              break;
198
      case 2: butt = view.findViewById(R.id.button2dRemove);
199
              butt.setOnClickListener(this);
200
              break;
201
      case 3: butt = view.findViewById(R.id.button3dRemove);
202
              butt.setOnClickListener(this);
203
              break;
204
      case 4: butt = view.findViewById(R.id.button4dRemove);
205
              butt.setOnClickListener(this);
206
              break;
207
      case 5: butt = view.findViewById(R.id.button5dRemove);
208
              butt.setOnClickListener(this);
209
              break;
210
      }
211

    
212
    if( mEffectNames[mEffectAdd].getCenterDimension() > 0 )
213
      {
214
      View center = eff.createCenter(mChildren);
215
      layout.addView(center);
216
      }
217

    
218
    if( mEffectNames[mEffectAdd].getRegionDimension() > 0 )
219
      {
220
      View region = eff.createRegion(mChildren);
221
      layout.addView(region);
222
      }
223

    
224
    eff.apply(mEffects);
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
  private void removeAll()
230
    {
231
    GenericActivity2 act = (GenericActivity2)getActivity();
232

    
233
    mChildren = 0;
234

    
235
    mList.clear();
236
    LinearLayout layout = act.findViewById(mLayout);
237
    layout.removeAllViews();
238
    mEffects.abortByType(mType);
239

    
240
    if( mType==EffectType.MATRIX )
241
      {
242
      GenericSurfaceView view = act.findViewById(R.id.genericSurfaceView);
243
      GenericRenderer renderer= view.getRenderer();
244
      renderer.resetMatrixEffects();
245
      }
246
    if( mType==EffectType.VERTEX )
247
      {
248
      GenericSurfaceView view = act.findViewById(R.id.genericSurfaceView);
249
      GenericRenderer renderer= view.getRenderer();
250
      renderer.resetVertexEffects();
251
      }
252

    
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

    
257
  private void remove(View v)
258
    {
259
    for(GenericEffect effect: mList)
260
      {
261
      if( effect.thisView(v) )
262
        {
263
        GenericActivity2 act = (GenericActivity2)getActivity();
264
        LinearLayout layout = act.findViewById(mLayout);
265
        View view;
266

    
267
        view = effect.getEffect();
268
        if( view!=null ) layout.removeView(view);
269
        view = effect.getCenter();
270
        if( view!=null ) layout.removeView(view);
271
        view = effect.getRegion();
272
        if( view!=null ) layout.removeView(view);
273

    
274
        mEffects.abortById(effect.getId());
275

    
276
        int index = mList.indexOf(effect);
277
        int capac = mList.size();
278

    
279
        for(int i=index+1; i<capac; i++)
280
          {
281
          mList.get(i).setBackground(i-1);
282
          }
283

    
284
        mList.remove(effect);
285
        mChildren--;
286

    
287
        break;
288
        }
289
      }
290
    }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
  private void createEffectNames(EffectType type)
295
    {
296
    EffectName[] names = EffectName.values();
297

    
298
    int numEffects=0;
299

    
300
    for( EffectName name: names )
301
      if( name.getType() == type ) numEffects++;
302

    
303
    mEffectNames = new EffectName[numEffects];
304

    
305
    numEffects=0;
306

    
307
    for( EffectName name: names )
308
      if( name.getType() == type )
309
        {
310
        mEffectNames[numEffects++] = name;
311
        }
312
    }
313
  }
(7-7/8)