Project

General

Profile

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

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

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.Float2D;
29
import org.distorted.library.Float3D;
30
import org.distorted.library.Float4D;
31
import org.distorted.library.Interpolator3D;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
public class ScratchpadSurfaceView extends GLSurfaceView
36
  {
37
  private ScratchpadRenderer mRenderer;
38
  private static int mCurrentEffect;
39
  private static int mDuration;
40
  private static float mCount;
41
  private static int mScrW, mScrH;
42
    
43
  private static Float4D region;
44
  private static Float2D  point;
45
    
46
  private static Interpolator3D di;
47
  private static Float3D v0, v1, v2, v3;
48
     
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
    
51
  public ScratchpadSurfaceView(Context c, AttributeSet attrs) 
52
    {
53
    super(c, attrs);
54
      
55
    mDuration = 10000;
56
    mCount    = 1.0f;
57
      
58
    di = new Interpolator3D();
59

    
60
    di.setDuration(mDuration);
61
    di.setCount(mCount);
62
      
63
    if(!isInEditMode())
64
      {
65
      setFocusable(true);
66
      setFocusableInTouchMode(true);
67
       
68
      setEGLContextClientVersion(2);
69
        
70
      if( Build.FINGERPRINT.startsWith("generic") ) // when running on the emulator, insert a magic line that is
71
        {                                           // supposed to cure the 'no config chosen' crash on emulator startup
72
        setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
73
        }
74
        
75
      mRenderer = new ScratchpadRenderer(this);
76
      setRenderer(mRenderer);
77
        
78
      point = new Float2D(0,0);
79
      region= new Float4D(0,0,60,60);
80
            
81
      int h = 30;
82
      int r = 20;
83
        
84
      v0 = new Float3D( 0, r, h );
85
      v1 = new Float3D(-r, 0, h );
86
      v2 = new Float3D( 0,-r, h );
87
      v3 = new Float3D( r, 0, h );
88
        
89
      di.add(v0);
90
      di.add(v1);
91
      di.add(v2);
92
      di.add(v3);
93
        
94
      setEffect(0);  
95
      }
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  public static void setScreenSize(int width, int height)
101
    {
102
    mScrW = width;
103
    mScrH = height;
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
  public static int getEffect()
109
    {
110
    return mCurrentEffect;
111
    }
112
   
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  public static void setEffect(int effect)
116
    {
117
    mCurrentEffect = effect;
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  public static void setDuration(int duration)
123
    {
124
    mDuration = duration;
125
    di.setDuration(duration);
126
    }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
  public static void setCount(float count)
131
    {
132
    mCount = count;
133
    di.setCount(count);
134
    }
135
  
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
    
138
  @Override public boolean onTouchEvent(MotionEvent event) 
139
    {
140
    int action = event.getAction();
141
    int x, y;
142
      
143
    switch(action)
144
      {
145
      case MotionEvent.ACTION_DOWN: x = (int)event.getX()*ScratchpadRenderer.BWID/mScrW;
146
                                    y = (int)event.getY()*ScratchpadRenderer.BHEI/mScrH;
147
                                    point.set(x,y);
148
                                              
149
                                    switch(mCurrentEffect)
150
                                      {
151
                                      case 0: ScratchpadRenderer.mBackground.distort(di, region, point); 
152
                                           break;
153
                                      case 1: ScratchpadRenderer.mBackground.sink(0.3f, region, point, mDuration, mCount); 
154
                                           break;
155
                                      case 2: ScratchpadRenderer.mBackground.alpha(0.0f, region, point, mDuration, mCount); 
156
                                           break;  
157
                                      case 3: ScratchpadRenderer.mBackground.macroblock(10, region, point, mDuration, mCount); 
158
                                           break;
159
                                      case 4: ScratchpadRenderer.mBackground.brightness(0.0f, region, point, mDuration, mCount); 
160
                                           break;      
161
                                      }
162
                                    break;
163
      }
164
            
165
    return true;
166
    }
167
  }
(3-3/3)