Project

General

Profile

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

examples / src / main / java / org / distorted / examples / scratchpad / ScratchpadSurfaceView.java @ 59759251

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.scratchpad;
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.type.Dynamic1D;
29
import org.distorted.library.type.Static1D;
30
import org.distorted.library.type.Static2D;
31
import org.distorted.library.type.Static3D;
32
import org.distorted.library.type.Static4D;
33
import org.distorted.library.type.Dynamic3D;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
public class ScratchpadSurfaceView extends GLSurfaceView
38
  {
39
  private ScratchpadRenderer mRenderer;
40
  private static int mCurrentEffect;
41
  private static int mDuration;
42
  private static float mCount;
43
  private static int mScrW, mScrH;
44
    
45
  private static Static4D region;
46
  private static Static2D point;
47

    
48
  private static Static4D mRegion;
49
  private static Dynamic1D mInterA, mInterM, mInterB;
50

    
51
  private static Dynamic3D mInterD;
52
  private static Static3D v0, v1, v2, v3;
53
     
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
    
56
  public ScratchpadSurfaceView(Context c, AttributeSet attrs) 
57
    {
58
    super(c, attrs);
59
      
60
    mDuration = 10000;
61
    mCount    = 1.0f;
62
      
63
    mInterD = new Dynamic3D();
64
    mInterD.setDuration(mDuration);
65
    mInterD.setCount(mCount);
66

    
67
    int h = 30;
68
    int r = 20;
69

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

    
75
    mInterD.add(v0);
76
    mInterD.add(v1);
77
    mInterD.add(v2);
78
    mInterD.add(v3);
79

    
80
    mInterA = new Dynamic1D();
81
    mInterA.setDuration(mDuration);
82
    mInterA.setCount(mCount);
83
    mInterA.add(new Static1D(1));
84
    mInterA.add(new Static1D(0));
85

    
86
    mInterB = new Dynamic1D();
87
    mInterB.setDuration(mDuration);
88
    mInterB.setCount(mCount);
89
    mInterB.add(new Static1D(1));
90
    mInterB.add(new Static1D(0));
91

    
92
    mInterM = new Dynamic1D();
93
    mInterM.setDuration(mDuration);
94
    mInterM.setCount(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 ScratchpadRenderer(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 int getEffect()
132
    {
133
    return mCurrentEffect;
134
    }
135
   
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  public static void setEffect(int effect)
139
    {
140
    mCurrentEffect = effect;
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  public static void setDuration(int duration)
146
    {
147
    mDuration = duration;
148
    mInterD.setDuration(duration);
149
    mInterA.setDuration(duration);
150
    mInterB.setDuration(duration);
151
    mInterM.setDuration(duration);
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
  public static void setCount(float count)
157
    {
158
    mCount = count;
159
    mInterD.setCount(count);
160
    mInterA.setCount(count);
161
    mInterB.setCount(count);
162
    mInterM.setCount(count);
163
    }
164
  
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
    
167
  @Override public boolean onTouchEvent(MotionEvent event) 
168
    {
169
    int action = event.getAction();
170
    int x, y;
171
      
172
    switch(action)
173
      {
174
      case MotionEvent.ACTION_DOWN: x = (int)event.getX()*ScratchpadRenderer.BWID/mScrW;
175
                                    y = (int)event.getY()*ScratchpadRenderer.BHEI/mScrH;
176
                                    point.set(x,y);
177
                                    mRegion.set(x,y,60,60);
178

    
179
                                    switch(mCurrentEffect)
180
                                      {
181
                                      case 0: ScratchpadRenderer.mBackground.distort(mInterD, region, point);
182
                                           break;
183
                                      case 1: ScratchpadRenderer.mBackground.sink(0.3f, region, point, mDuration, mCount); 
184
                                           break;
185
                                      case 2: ScratchpadRenderer.mBackground.alpha(mInterA, mRegion, false);
186
                                           break;  
187
                                      case 3: ScratchpadRenderer.mBackground.macroblock(mInterM, mRegion);
188
                                           break;
189
                                      case 4: ScratchpadRenderer.mBackground.brightness(mInterB, mRegion, false);
190
                                           break;      
191
                                      }
192
                                    break;
193
      }
194
            
195
    return true;
196
    }
197
  }
(3-3/3)