Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects2d / Effects2DSurfaceView.java @ 84dd0d8f

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e5424523 Leszek Koltunski
package org.distorted.examples.effects2d;
21 427ab7bf Leszek Koltunski
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 e763f1ee Leszek Koltunski
import org.distorted.library.EffectNames;
29
import org.distorted.library.EffectTypes;
30 59759251 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
31
import org.distorted.library.type.Static1D;
32 7589635e Leszek Koltunski
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 427ab7bf Leszek Koltunski
37 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
38 427ab7bf Leszek Koltunski
39 e5424523 Leszek Koltunski
public class Effects2DSurfaceView extends GLSurfaceView
40 bc0a685b Leszek Koltunski
  {
41 e5424523 Leszek Koltunski
  private Effects2DRenderer mRenderer;
42 bc0a685b Leszek Koltunski
  private static int mCurrentEffect;
43
  private static int mDuration;
44
  private static float mCount;
45
  private static int mScrW, mScrH;
46 427ab7bf Leszek Koltunski
    
47 7589635e Leszek Koltunski
  private static Static4D region;
48
  private static Static2D point;
49 59759251 Leszek Koltunski
50
  private static Static4D mRegion;
51 f9afbbf3 Leszek Koltunski
  private static Dynamic1D mInterA, mInterM, mInterC, mInterS;
52 59759251 Leszek Koltunski
53
  private static Dynamic3D mInterD;
54 7589635e Leszek Koltunski
  private static Static3D v0, v1, v2, v3;
55 f9afbbf3 Leszek Koltunski
56
  private final static Static3D mRED = new Static3D(1,0,0);
57
58 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
59 427ab7bf Leszek Koltunski
    
60 e5424523 Leszek Koltunski
  public Effects2DSurfaceView(Context c, AttributeSet attrs)
61 bc0a685b Leszek Koltunski
    {
62
    super(c, attrs);
63 427ab7bf Leszek Koltunski
      
64 bc0a685b Leszek Koltunski
    mDuration = 10000;
65
    mCount    = 1.0f;
66 427ab7bf Leszek Koltunski
      
67 f988589e Leszek Koltunski
    mInterD = new Dynamic3D(mDuration,mCount);
68 59759251 Leszek Koltunski
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 f988589e Leszek Koltunski
    mInterA = new Dynamic1D(mDuration,mCount);
83 59759251 Leszek Koltunski
    mInterA.add(new Static1D(1));
84
    mInterA.add(new Static1D(0));
85
86 f988589e Leszek Koltunski
    mInterS = new Dynamic1D(mDuration,mCount);
87 b5cc7760 Leszek Koltunski
    mInterS.add(new Static1D(1.0f));
88
    mInterS.add(new Static1D(0.3f));
89
90 f9afbbf3 Leszek Koltunski
    mInterC = new Dynamic1D(mDuration,mCount);
91
    mInterC.add(new Static1D(1));
92
    mInterC.add(new Static1D(0));
93 59759251 Leszek Koltunski
94 f988589e Leszek Koltunski
    mInterM = new Dynamic1D(mDuration,mCount);
95 59759251 Leszek Koltunski
    mInterM.add(new Static1D(1));
96
    mInterM.add(new Static1D(10));
97 427ab7bf Leszek Koltunski
98 bc0a685b Leszek Koltunski
    if(!isInEditMode())
99
      {
100
      setFocusable(true);
101
      setFocusableInTouchMode(true);
102
       
103
      setEGLContextClientVersion(2);
104 427ab7bf Leszek Koltunski
        
105 bc0a685b Leszek Koltunski
      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 427ab7bf Leszek Koltunski
        
110 e5424523 Leszek Koltunski
      mRenderer = new Effects2DRenderer(this);
111 bc0a685b Leszek Koltunski
      setRenderer(mRenderer);
112 af225332 Leszek Koltunski
113 7589635e Leszek Koltunski
      point = new Static2D(0,0);
114
      region= new Static4D(0,0,60,60);
115 59759251 Leszek Koltunski
      mRegion = new Static4D(0,0,60,60);
116 427ab7bf Leszek Koltunski
        
117 bc0a685b Leszek Koltunski
      setEffect(0);  
118 427ab7bf Leszek Koltunski
      }
119 bc0a685b Leszek Koltunski
    }
120 427ab7bf Leszek Koltunski
121 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
122 427ab7bf Leszek Koltunski
123 bc0a685b Leszek Koltunski
  public static void setScreenSize(int width, int height)
124
    {
125
    mScrW = width;
126
    mScrH = height;
127
    }
128 427ab7bf Leszek Koltunski
129 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
130 427ab7bf Leszek Koltunski
131 bc0a685b Leszek Koltunski
  public static void setEffect(int effect)
132
    {
133
    mCurrentEffect = effect;
134
    }
135 427ab7bf Leszek Koltunski
136 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
137 427ab7bf Leszek Koltunski
    
138 bc0a685b Leszek Koltunski
  @Override public boolean onTouchEvent(MotionEvent event) 
139
    {
140
    int action = event.getAction();
141
    int x, y;
142 84dd0d8f Leszek Koltunski
    long id;
143 af225332 Leszek Koltunski
144 bc0a685b Leszek Koltunski
    switch(action)
145 427ab7bf Leszek Koltunski
      {
146 e5424523 Leszek Koltunski
      case MotionEvent.ACTION_DOWN: x = (int)event.getX()* Effects2DRenderer.BWID/mScrW;
147
                                    y = (int)event.getY()* Effects2DRenderer.BHEI/mScrH;
148 427ab7bf Leszek Koltunski
                                    point.set(x,y);
149 59759251 Leszek Koltunski
                                    mRegion.set(x,y,60,60);
150 e763f1ee Leszek Koltunski
                                    Effects2DActivity act = (Effects2DActivity)getContext();
151 59759251 Leszek Koltunski
152 427ab7bf Leszek Koltunski
                                    switch(mCurrentEffect)
153
                                      {
154 af225332 Leszek Koltunski
                                      case 0: id = Effects2DRenderer.mBackground.distort(mInterD, point, region);
155 e763f1ee Leszek Koltunski
                                              act.effectAdded(id, EffectNames.DISTORT, EffectTypes.VERTEX);
156
                                              break;
157 af225332 Leszek Koltunski
                                      case 1: id = Effects2DRenderer.mBackground.sink(mInterS, point, region);
158 e763f1ee Leszek Koltunski
                                              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 af225332 Leszek Koltunski
                                      case 3: id = Effects2DRenderer.mBackground.macroblock(mInterM, mRegion);
164 e763f1ee Leszek Koltunski
                                              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 427ab7bf Leszek Koltunski
                                      }
170 af225332 Leszek Koltunski
171 427ab7bf Leszek Koltunski
                                    break;
172
      }
173 af225332 Leszek Koltunski
174 bc0a685b Leszek Koltunski
    return true;
175
    }
176 af225332 Leszek Koltunski
177 427ab7bf Leszek Koltunski
  }