Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects2d / Effects2DActivity.java @ 50ac40a6

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
import android.widget.Toast;
38

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

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

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

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

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

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

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

    
68
    mAdd  = (Spinner)findViewById(R.id.effects2d_spinnerAdd );
69
    mID   = (Spinner)findViewById(R.id.effects2d_spinnerID  );
70
    mName = (Spinner)findViewById(R.id.effects2d_spinnerName);
71
    mType = (Spinner)findViewById(R.id.effects2d_spinnerType);
72

    
73
    mAdd.setOnItemSelectedListener(this);
74
    mID.setOnItemSelectedListener(this);
75
    mName.setOnItemSelectedListener(this);
76
    mType.setOnItemSelectedListener(this);
77

    
78
    ArrayList<Long> itemsID  = new ArrayList<>();
79

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

    
86
    String[] itemsType = new String[] {"VERTEX", "FRAGMENT"};
87

    
88
    mAdapterAdd = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, itemsName);
89
    mAdapterAdd.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
90
    mAdd.setAdapter(mAdapterAdd);
91

    
92
    mAdapterID = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, itemsID);
93
    mAdapterID.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
94
    mID.setAdapter(mAdapterID);
95

    
96
    mAdapterName = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, itemsName);
97
    mAdapterName.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
98
    mName.setAdapter(mAdapterName);
99

    
100
    mAdapterType = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, itemsType);
101
    mAdapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
102
    mType.setAdapter(mAdapterType);
103

    
104
    mLayoutList = (TableLayout)findViewById(R.id.effects2dTableList);
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
  @Override
110
  protected void onResume() 
111
    {
112
    super.onResume();
113
      
114
    GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.effects2dSurfaceView);
115
    mView.onResume();
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
  @Override
121
  protected void onPause() 
122
    {
123
    GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.effects2dSurfaceView);
124
    mView.onPause();
125
      
126
    super.onPause();
127
    }
128
    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
  @Override
132
  public void onStop()
133
    {
134
    super.onStop();
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  @Override
140
  public void onDestroy()
141
    {
142
    Distorted.onDestroy();
143
    super.onDestroy();
144
    }     
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
149
    {
150
    switch(parent.getId())
151
      {
152
      case R.id.effects2d_spinnerAdd : Effects2DSurfaceView.setEffect(pos); break;
153
      case R.id.effects2d_spinnerID  : mPosID   = pos; break;
154
      case R.id.effects2d_spinnerName: mPosName = pos; break;
155
      case R.id.effects2d_spinnerType: mPosType = pos; break;
156
      }
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  public void onNothingSelected(AdapterView<?> parent)
162
    {
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
  public void removeByID(View view)
168
    {
169
    Long currID = (Long)mID.getItemAtPosition(mPosID);
170

    
171
    Effects2DRenderer.mBackground.abortEffect(currID);
172
    }
173

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

    
176
  public void removeByName(View view)
177
    {
178
    EffectNames name;
179

    
180
    switch(mPosName)
181
      {
182
      case  0: name = EffectNames.DISTORT      ; break;
183
      case  1: name = EffectNames.SINK         ; break;
184
      case  2: name = EffectNames.SMOOTH_ALPHA ; break;
185
      case  3: name = EffectNames.MACROBLOCK   ; break;
186
      case  4: name = EffectNames.SMOOTH_CHROMA; break;
187
      default: name = EffectNames.CONTRAST     ;
188
      }
189

    
190
    Effects2DRenderer.mBackground.abortEffects(name);
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  public void removeByType(View view)
196
    {
197
    EffectTypes type;
198

    
199
    switch(mPosType)
200
      {
201
      case  0: type = EffectTypes.VERTEX  ; break;
202
      case  1: type = EffectTypes.FRAGMENT; break;
203
      default: type = EffectTypes.MATRIX;
204
      }
205

    
206
    Effects2DRenderer.mBackground.abortEffects(type);
207
    }
208

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

    
211
  public void effectAdded(final long id, final EffectNames name, final EffectTypes type)
212
    {
213
    if( id>=0 )  // we really added a new effect
214
      {
215
      mAdapterID.add(id);
216
      mAdapterID.notifyDataSetChanged();
217

    
218
      TableRow tr = new TableRow(this);
219
      tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
220

    
221
      TextView b1 = new TextView(this);
222
      b1.setText("ID: "+id);
223
      b1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
224
      tr.addView(b1);
225

    
226
      TextView b2 = new TextView(this);
227
      b2.setText(name.name());
228
      b2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
229
      tr.addView(b2);
230

    
231
      TextView b3 = new TextView(this);
232
      b3.setText(type.name());
233
      b3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
234
      tr.addView(b3);
235

    
236
      TextView b4 = new TextView(this);
237
      b4.setText("LIVE");
238
      b4.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
239
      tr.addView(b4);
240

    
241
      mMap.put(id,tr);
242

    
243
      mLayoutList.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
244
      }
245
    else // id=-1, i.e. we failed to add new effect due to too many effects already
246
      {
247
      Toast.makeText(this, R.string.example_effects2d_toast , Toast.LENGTH_LONG).show();
248
      }
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
  public void effectRemoved(final long id)
254
    {
255
    runOnUiThread(new Runnable()
256
      {
257
      public void run()
258
        {
259
        mAdapterID.remove(id);
260
        mAdapterID.notifyDataSetChanged();
261

    
262
        TableRow row = mMap.remove(id);
263

    
264
        if( row!=null )
265
          {
266
          mLayoutList.removeView(row);
267
          }
268
        else
269
          {
270
          android.util.Log.e("EFFECTS2D", "Impossible: id="+id+" not in the map!");
271
          }
272
        }
273
      });
274
    }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

    
278
  public void effectFinished(final long id)
279
    {
280
    runOnUiThread(new Runnable()
281
      {
282
      public void run()
283
        {
284
        TableRow row = mMap.get(id);
285

    
286
        if( row!=null )
287
          {
288
          TextView v = (TextView)row.getVirtualChildAt(3);
289
          v.setText("FINISHED");
290
          }
291
        }
292
      });
293
    }
294
  }
(1-1/3)