Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effectqueue / EffectQueueSurfaceView.java @ 107e4b72

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.effectqueue;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.opengl.GLSurfaceView;
26
import android.view.MotionEvent;
27
import android.util.AttributeSet;
28

    
29
import org.distorted.library.effect.FragmentEffectAlpha;
30
import org.distorted.library.effect.FragmentEffectChroma;
31
import org.distorted.library.effect.FragmentEffectSaturation;
32
import org.distorted.library.effect.VertexEffectDistort;
33
import org.distorted.library.effect.VertexEffectSink;
34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Static1D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38
import org.distorted.library.type.Dynamic3D;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
public class EffectQueueSurfaceView extends GLSurfaceView
43
  {
44
  private static final int RADIUS = 60;
45

    
46
  private EffectQueueRenderer mRenderer;
47
  private int mCurrentEffect;
48
  private int mScrW, mScrH;
49
    
50
  private Static4D mRegionV;
51
  private Dynamic1D mInterA, mInterB, mInterC, mInterS;
52
  private Dynamic3D mInterD;
53

    
54
  private final static Static3D mRED = new Static3D(1,0,0);
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
    
58
  public EffectQueueSurfaceView(Context context, AttributeSet attrs)
59
    {
60
    super(context, attrs);
61
      
62
    int duration = 10000;
63
    float count  = 1.0f;
64
    int h = 30;
65
    int r = 20;
66

    
67
    mInterD = new Dynamic3D(duration,count);
68

    
69
    mInterD.add(new Static3D( 0, r, h ));
70
    mInterD.add(new Static3D(-r, 0, h ));
71
    mInterD.add(new Static3D( 0,-r, h ));
72
    mInterD.add(new Static3D( r, 0, h ));
73

    
74
    mInterA = new Dynamic1D(duration,count);
75
    mInterA.add(new Static1D(1));
76
    mInterA.add(new Static1D(0));
77

    
78
    mInterS = new Dynamic1D(duration,count);
79
    mInterS.add(new Static1D(1.0f));
80
    mInterS.add(new Static1D(0.3f));
81

    
82
    mInterC = new Dynamic1D(duration,count);
83
    mInterC.add(new Static1D(1));
84
    mInterC.add(new Static1D(0));
85

    
86
    mInterB = new Dynamic1D(duration,count);
87
    mInterB.add(new Static1D(0));
88
    mInterB.add(new Static1D(1));
89

    
90
    if(!isInEditMode())
91
      {
92
      setFocusable(true);
93
      setFocusableInTouchMode(true);
94
      mRenderer = new EffectQueueRenderer(this);
95
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
96
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
97
      setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
98
      setRenderer(mRenderer);
99
      mRegionV= new Static4D(0,0,RADIUS,RADIUS);
100
      setEffect(0);  
101
      }
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
  EffectQueueRenderer getRenderer()
107
    {
108
    return mRenderer;
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
  public void setScreenSize(int width, int height)
114
    {
115
    mScrW = width;
116
    mScrH = height;
117
    }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
  public void setEffect(int effect)
122
    {
123
    mCurrentEffect = effect;
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
    
128
  @Override public boolean onTouchEvent(MotionEvent event) 
129
    {
130
    int x,y,action = event.getAction();
131
    boolean success;
132

    
133
    switch(action)
134
      {
135
      case MotionEvent.ACTION_DOWN: x = (int)event.getX()* mRenderer.BWID/mScrW;
136
                                    y = (int)event.getY()* mRenderer.BHEI/mScrH;
137
                                    EffectQueueActivity act = (EffectQueueActivity)getContext();
138

    
139
                                    switch(mCurrentEffect)
140
                                      {
141
                                      case 0: VertexEffectDistort distort = new VertexEffectDistort(mInterD, new Static3D(x,y,0), mRegionV);
142
                                              success = mRenderer.getEffects().apply(distort);
143
                                              act.effectAdded(success,distort);
144
                                              break;
145
                                      case 1: VertexEffectSink sink = new VertexEffectSink(mInterS, new Static3D(x,y,0), mRegionV);
146
                                              success = mRenderer.getEffects().apply(sink);
147
                                              act.effectAdded(success,sink);
148
                                              break;
149
                                      case 2: FragmentEffectAlpha alpha = new FragmentEffectAlpha(mInterA, new Static4D(x,y,RADIUS,RADIUS), true);
150
                                              success = mRenderer.getEffects().apply(alpha);
151
                                              act.effectAdded(success,alpha);
152
                                              break;
153
                                      case 3: FragmentEffectSaturation saturation = new FragmentEffectSaturation(mInterB, new Static4D(x,y,RADIUS,RADIUS), false);
154
                                              success = mRenderer.getEffects().apply(saturation);
155
                                              act.effectAdded(success,saturation);
156
                                              break;
157
                                      case 4: FragmentEffectChroma chroma = new FragmentEffectChroma(mInterC, mRED, new Static4D(x,y,RADIUS,RADIUS), true);
158
                                              success = mRenderer.getEffects().apply(chroma);
159
                                              act.effectAdded(success,chroma);
160
                                              break;
161
                                      }
162

    
163
                                    break;
164
      }
165

    
166
    return true;
167
    }
168

    
169
  }
(3-3/3)