Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects2d / Effects2DSurfaceView.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 android.content.Context;
23
import android.opengl.GLSurfaceView;
24
import android.os.Build;
25
import android.view.MotionEvent;
26
import android.util.AttributeSet;
27

    
28
import org.distorted.library.EffectNames;
29
import org.distorted.library.EffectTypes;
30
import org.distorted.library.type.Dynamic1D;
31
import org.distorted.library.type.Static1D;
32
import org.distorted.library.type.Static2D;
33
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static4D;
35
import org.distorted.library.type.Dynamic3D;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
public class Effects2DSurfaceView extends GLSurfaceView
40
  {
41
  private Effects2DRenderer mRenderer;
42
  private static int mCurrentEffect;
43
  private static int mDuration;
44
  private static float mCount;
45
  private static int mScrW, mScrH;
46
    
47
  private static Static4D region;
48
  private static Static2D point;
49

    
50
  private static Static4D mRegion;
51
  private static Dynamic1D mInterA, mInterM, mInterC, mInterS;
52

    
53
  private static Dynamic3D mInterD;
54
  private static Static3D v0, v1, v2, v3;
55

    
56
  private final static Static3D mRED = new Static3D(1,0,0);
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
    
60
  public Effects2DSurfaceView(Context c, AttributeSet attrs)
61
    {
62
    super(c, attrs);
63
      
64
    mDuration = 10000;
65
    mCount    = 1.0f;
66
      
67
    mInterD = new Dynamic3D(mDuration,mCount);
68

    
69
    int h = 30;
70
    int r = 20;
71

    
72
    v0 = new Static3D( 0, r, h );
73
    v1 = new Static3D(-r, 0, h );
74
    v2 = new Static3D( 0,-r, h );
75
    v3 = new Static3D( r, 0, h );
76

    
77
    mInterD.add(v0);
78
    mInterD.add(v1);
79
    mInterD.add(v2);
80
    mInterD.add(v3);
81

    
82
    mInterA = new Dynamic1D(mDuration,mCount);
83
    mInterA.add(new Static1D(1));
84
    mInterA.add(new Static1D(0));
85

    
86
    mInterS = new Dynamic1D(mDuration,mCount);
87
    mInterS.add(new Static1D(1.0f));
88
    mInterS.add(new Static1D(0.3f));
89

    
90
    mInterC = new Dynamic1D(mDuration,mCount);
91
    mInterC.add(new Static1D(1));
92
    mInterC.add(new Static1D(0));
93

    
94
    mInterM = new Dynamic1D(mDuration,mCount);
95
    mInterM.add(new Static1D(1));
96
    mInterM.add(new Static1D(10));
97

    
98
    if(!isInEditMode())
99
      {
100
      setFocusable(true);
101
      setFocusableInTouchMode(true);
102
       
103
      setEGLContextClientVersion(2);
104
        
105
      if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
106
        {                                           // supposed to cure the 'no config chosen' crash on emulator startup
107
        setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
108
        }
109
        
110
      mRenderer = new Effects2DRenderer(this);
111
      setRenderer(mRenderer);
112

    
113
      point = new Static2D(0,0);
114
      region= new Static4D(0,0,60,60);
115
      mRegion = new Static4D(0,0,60,60);
116
        
117
      setEffect(0);  
118
      }
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  public static void setScreenSize(int width, int height)
124
    {
125
    mScrW = width;
126
    mScrH = height;
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
  public static void setEffect(int effect)
132
    {
133
    mCurrentEffect = effect;
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
    
138
  @Override public boolean onTouchEvent(MotionEvent event) 
139
    {
140
    int action = event.getAction();
141
    int x, y;
142
    long id = -1;
143

    
144
    switch(action)
145
      {
146
      case MotionEvent.ACTION_DOWN: x = (int)event.getX()* Effects2DRenderer.BWID/mScrW;
147
                                    y = (int)event.getY()* Effects2DRenderer.BHEI/mScrH;
148
                                    point.set(x,y);
149
                                    mRegion.set(x,y,60,60);
150
                                    Effects2DActivity act = (Effects2DActivity)getContext();
151

    
152
                                    switch(mCurrentEffect)
153
                                      {
154
                                      case 0: id = Effects2DRenderer.mBackground.distort(mInterD, point, region);
155
                                              act.effectAdded(id, EffectNames.DISTORT, EffectTypes.VERTEX);
156
                                              break;
157
                                      case 1: id = Effects2DRenderer.mBackground.sink(mInterS, point, region);
158
                                              act.effectAdded(id, EffectNames.SINK, EffectTypes.VERTEX);
159
                                              break;
160
                                      case 2: id = Effects2DRenderer.mBackground.alpha(mInterA, mRegion, true);
161
                                              act.effectAdded(id, EffectNames.ALPHA, EffectTypes.FRAGMENT);
162
                                              break;
163
                                      case 3: id = Effects2DRenderer.mBackground.macroblock(mInterM, mRegion);
164
                                              act.effectAdded(id, EffectNames.MACROBLOCK, EffectTypes.FRAGMENT);
165
                                              break;
166
                                      case 4: id = Effects2DRenderer.mBackground.chroma(mInterC, mRED, mRegion, true);
167
                                              act.effectAdded(id, EffectNames.CHROMA, EffectTypes.FRAGMENT);
168
                                              break;
169
                                      }
170

    
171
                                    break;
172
      }
173

    
174
    return true;
175
    }
176

    
177
  }
(3-3/3)