Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects2d / Effects2DActivity.java @ e763f1ee

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 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.effects2d;
21

    
22
import org.distorted.library.Distorted;
23
import org.distorted.examples.R;
24
import org.distorted.library.EffectNames;
25
import org.distorted.library.EffectTypes;
26

    
27
import android.app.Activity;
28
import android.opengl.GLSurfaceView;
29
import android.os.Bundle;
30
import android.view.View;
31
import android.widget.AdapterView;
32
import android.widget.ArrayAdapter;
33
import android.widget.Spinner;
34
import android.widget.TableLayout;
35
import android.widget.TableRow;
36
import android.widget.TextView;
37

    
38
import java.util.ArrayList;
39
import java.util.HashMap;
40

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

    
43
public class Effects2DActivity extends Activity implements AdapterView.OnItemSelectedListener
44
  {
45
  private Spinner mID, mName, mType;
46
  private static ArrayAdapter<String> mAdapterName, mAdapterType;
47
  private static ArrayAdapter<Long> mAdapterID;
48

    
49
  private int mPosID, mPosName, mPosType;
50
  private TableLayout mLayoutList;
51

    
52
  private HashMap<Long,TableRow> mMap = new HashMap<>();
53

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

    
56
  @Override
57
  protected void onCreate(Bundle savedInstanceState) 
58
    {
59
    super.onCreate(savedInstanceState);
60
 
61
    setContentView(R.layout.effects2dlayout);
62

    
63
    mPosID   = 0;
64
    mPosName = 0;
65
    mPosType = 0;
66

    
67
    mID   = (Spinner)findViewById(R.id.effects2d_spinnerID  );
68
    mName = (Spinner)findViewById(R.id.effects2d_spinnerName);
69
    mType = (Spinner)findViewById(R.id.effects2d_spinnerType);
70

    
71
    mID.setOnItemSelectedListener(this);
72
    mName.setOnItemSelectedListener(this);
73
    mType.setOnItemSelectedListener(this);
74

    
75
    ArrayList<Long> itemsID  = new ArrayList<>();
76

    
77
    String[] itemsName = new String[] { getText(R.string.distort     ).toString(),
78
                                        getText(R.string.sink        ).toString(),
79
                                        getText(R.string.transparency).toString(),
80
                                        getText(R.string.macroblock  ).toString(),
81
                                        getText(R.string.chroma      ).toString()};
82

    
83
    String[] itemsType = new String[] {"VERTEX", "FRAGMENT"};
84

    
85
    mAdapterID = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, itemsID);
86
    mAdapterID.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
87
    mID.setAdapter(mAdapterID);
88

    
89
    mAdapterName = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, itemsName);
90
    mAdapterName.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
91
    mName.setAdapter(mAdapterName);
92

    
93
    mAdapterType = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, itemsType);
94
    mAdapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
95
    mType.setAdapter(mAdapterType);
96

    
97
    mLayoutList = (TableLayout)findViewById(R.id.effects2dTableList);
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
  @Override
103
  protected void onResume() 
104
    {
105
    super.onResume();
106
      
107
    GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.effects2dSurfaceView);
108
    mView.onResume();
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
  @Override
114
  protected void onPause() 
115
    {
116
    GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.effects2dSurfaceView);
117
    mView.onPause();
118
      
119
    super.onPause();
120
    }
121
    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  @Override
125
  public void onStop()
126
    {
127
    super.onStop();
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  @Override
133
  public void onDestroy()
134
    {
135
    Distorted.onDestroy();
136
    super.onDestroy();
137
    }     
138
 
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
    
141
  public void Distort(View v)
142
    {
143
    Effects2DSurfaceView.setEffect(0);
144
    }     
145
    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  public void Sink(View v)
149
    {
150
    Effects2DSurfaceView.setEffect(1);
151
    }       
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
    
155
  public void Transparency(View v)
156
    {
157
    Effects2DSurfaceView.setEffect(2);
158
    }     
159
    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
  public void Macroblock(View v)
163
    {
164
    Effects2DSurfaceView.setEffect(3);
165
    }       
166
 
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  public void Chroma(View v)
170
    {
171
    Effects2DSurfaceView.setEffect(4);
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
177
    {
178
    switch(parent.getId())
179
      {
180
      case R.id.effects2d_spinnerID  : mPosID   = pos; break;
181
      case R.id.effects2d_spinnerName: mPosName = pos; break;
182
      case R.id.effects2d_spinnerType: mPosType = pos; break;
183
      }
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  public void onNothingSelected(AdapterView<?> parent)
189
    {
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  public void removeByID(View view)
195
    {
196
    Long currID = (Long)mID.getItemAtPosition(mPosID);
197

    
198
    Effects2DRenderer.mBackground.abortEffect(currID);
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  public void removeByName(View view)
204
    {
205
    EffectNames name;
206

    
207
    switch(mPosName)
208
      {
209
      case  0: name = EffectNames.DISTORT      ; break;
210
      case  1: name = EffectNames.SINK         ; break;
211
      case  2: name = EffectNames.SMOOTH_ALPHA ; break;
212
      case  3: name = EffectNames.MACROBLOCK   ; break;
213
      case  4: name = EffectNames.SMOOTH_CHROMA; break;
214
      default: name = EffectNames.CONTRAST     ;
215
      }
216

    
217
    Effects2DRenderer.mBackground.abortEffects(name);
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  public void removeByType(View view)
223
    {
224
    EffectTypes type;
225

    
226
    switch(mPosType)
227
      {
228
      case  0: type = EffectTypes.VERTEX  ; break;
229
      case  1: type = EffectTypes.FRAGMENT; break;
230
      default: type = EffectTypes.MATRIX;
231
      }
232

    
233
    Effects2DRenderer.mBackground.abortEffects(type);
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
  public void effectAdded(final long id, final EffectNames name, final EffectTypes type)
239
    {
240
    mAdapterID.add( new Long(id) );
241
    mAdapterID.notifyDataSetChanged();
242

    
243
    TableRow tr = new TableRow(this);
244
    tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
245

    
246
    TextView b1 = new TextView(this);
247
    b1.setText("ID: "+id);
248
    b1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
249
    tr.addView(b1);
250

    
251
    TextView b2 = new TextView(this);
252
    b2.setText(name.name());
253
    b2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
254
    tr.addView(b2);
255

    
256
    TextView b3 = new TextView(this);
257
    b3.setText(type.name());
258
    b3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
259
    tr.addView(b3);
260

    
261
    TextView b4 = new TextView(this);
262
    b4.setText("LIVE");
263
    b4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
264
    tr.addView(b4);
265

    
266
    mMap.put(id,tr);
267

    
268
    mLayoutList.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  public void effectRemoved(final long id)
274
    {
275
    runOnUiThread(new Runnable()
276
      {
277
      public void run()
278
        {
279
        mAdapterID.remove( new Long(id) );
280
        mAdapterID.notifyDataSetChanged();
281

    
282
        TableRow row = mMap.remove(id);
283

    
284
        if( row!=null )
285
          {
286
          mLayoutList.removeView(row);
287
          }
288
        else
289
          {
290
          android.util.Log.e("EFFECTS2D", "Impossible: id="+id+" not in the map!");
291
          }
292
        }
293
      });
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
  public void effectFinished(final long id)
299
    {
300
    runOnUiThread(new Runnable()
301
      {
302
      public void run()
303
        {
304
        TableRow row = mMap.get(id);
305

    
306
        if( row!=null )
307
          {
308
          TextView v = (TextView)row.getVirtualChildAt(3);
309
          v.setText("FINISHED");
310
          }
311
        }
312
      });
313
    }
314
  }
(1-1/3)