Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DTab.java @ fe7fe83e

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.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
import java.lang.ref.WeakReference;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
public class Effects3DTab extends Fragment implements AdapterView.OnItemSelectedListener
43
  {
44
  private WeakReference<Effects3DActivity> mAct;
45
  private EffectType mType;
46
  private ArrayList<Effects3DEffect> mList;
47
  private int mEffectAdd;
48
  private EffectName[] mEffectNames;
49
  private DistortedEffects mEffects;
50
  private String[] mEffectStrings;
51
  private int mTab, mLayout, mSpinner;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

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

    
63
  void setParams(EffectType type, WeakReference<Effects3DActivity> act, int tab, int layout, int spinner)
64
    {
65
    mTab     = tab;
66
    mLayout  = layout;
67
    mSpinner = spinner;
68
    mType    = type;
69
    mAct     = act;
70
    mEffects = act.get().getEffects();
71

    
72
    android.util.Log.e("tab", "setparams: "+type.name() );
73

    
74
    createEffectNames(mType);
75
    mEffectStrings = new String[mEffectNames.length];
76

    
77
    for (int i = 0; i < mEffectNames.length; i++)
78
      {
79
      mEffectStrings[i] = mEffectNames[i].name();
80

    
81
      android.util.Log.d("tab", "  effect: "+mEffectStrings[i]);
82
      }
83
    }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  @Override
88
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
89
    {
90
    View rootView = inflater.inflate( mTab, container, false);
91
    return rootView;
92
    }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
  @Override
97
  public void onStart()
98
    {
99
    super.onStart();
100

    
101
    android.util.Log.e("tab", "onStart, type="+mType.name());
102

    
103
    Effects3DActivity act = mAct.get();
104

    
105
    ArrayAdapter<String> adapterEffect = new ArrayAdapter<>( act, android.R.layout.simple_spinner_item, mEffectStrings);
106
    adapterEffect.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
107

    
108
    Spinner effectSpinner = act.findViewById(mSpinner);
109
    effectSpinner.setOnItemSelectedListener(this);
110
    effectSpinner.setAdapter(adapterEffect);
111

    
112
    resetData();
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
118
    {
119
    if( parent.getId() == mSpinner )
120
      {
121
      mEffectAdd = pos;
122
      }
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  public void onNothingSelected(AdapterView<?> parent)
128
    {
129
    }
130

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

    
133
  private void resetData()
134
    {
135
    Effects3DSurfaceView view = mAct.get().findViewById(R.id.effects3dSurfaceView);
136
    view.getRenderer().showRegionAndCenter(false,false);
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  void newEffect()
142
    {
143
    Effects3DEffect eff = new Effects3DEffect(mEffectNames[mEffectAdd], mAct.get() );
144
    mList.add(eff);
145

    
146
    LinearLayout layout = mAct.get().findViewById(mLayout);
147
    View view = eff.createView();
148
    layout.addView(view);
149

    
150
    if( mEffectNames[mEffectAdd].supportsCenter() )
151
      {
152
      View center = eff.createCenter();
153
      layout.addView(center);
154
      }
155

    
156
    if( mEffectNames[mEffectAdd].supportsRegion() )
157
      {
158
      View region = eff.createRegion();
159
      layout.addView(region);
160
      }
161

    
162
    eff.apply(mEffects);
163

    
164
    Effects3DActivity act = mAct.get();
165
    boolean region = act.getShowRegion();
166
    boolean center = act.getShowCenter();
167

    
168
    boolean show = (mEffectNames[mEffectAdd].getType()==EffectType.VERTEX);
169
    Effects3DSurfaceView sv = act.findViewById(R.id.effects3dSurfaceView);
170
    sv.getRenderer().showRegionAndCenter( (show && region) , (show && center) );
171
    }
172

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

    
175
  private void createEffectNames(EffectType type)
176
    {
177
    EffectName[] names = EffectName.values();
178

    
179
    int numEffects=0;
180

    
181
    for(int i=0; i<names.length; i++)
182
      if( names[i].getType() == type ) numEffects++;
183

    
184
    mEffectNames = new EffectName[numEffects];
185

    
186
    numEffects=0;
187

    
188
    for(int i=0; i<names.length; i++)
189
      if( names[i].getType() == type )
190
        {
191
        mEffectNames[numEffects++] = names[i];
192
        }
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
  void removeAll()
198
    {
199
    Effects3DActivity act = mAct.get();
200

    
201
    mList.clear();
202
    LinearLayout layout = act.findViewById(mLayout);
203
    layout.removeAllViews();
204
    mEffects.abortByType(mType);
205

    
206
    resetData();
207
    act.resetData();
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  void remove(View v)
213
    {
214
    for(Effects3DEffect effect: mList)
215
      {
216
      if( effect.thisView(v) )
217
        {
218
        LinearLayout layout = mAct.get().findViewById(mLayout);
219
        View view;
220

    
221
        view = effect.getEffect();
222
        if( view!=null ) layout.removeView(view);
223
        view = effect.getCenter();
224
        if( view!=null ) layout.removeView(view);
225
        view = effect.getRegion();
226
        if( view!=null ) layout.removeView(view);
227

    
228
        mEffects.abortById(effect.getId());
229
        mList.remove(effect);
230

    
231
        resetData();
232

    
233
        break;
234
        }
235
      }
236
    }
237

    
238
  }
(5-5/6)