Project

General

Profile

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

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

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 758303a3 Leszek Koltunski
package org.distorted.examples.effectqueue;
21 427ab7bf Leszek Koltunski
22 e4330c89 Leszek Koltunski
import android.app.ActivityManager;
23 427ab7bf Leszek Koltunski
import android.content.Context;
24 e4330c89 Leszek Koltunski
import android.content.pm.ConfigurationInfo;
25 427ab7bf Leszek Koltunski
import android.opengl.GLSurfaceView;
26
import android.view.MotionEvent;
27
import android.util.AttributeSet;
28
29 8dfa45c4 leszek
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 59759251 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Static1D;
36 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38
import org.distorted.library.type.Dynamic3D;
39 427ab7bf Leszek Koltunski
40 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
41 427ab7bf Leszek Koltunski
42 758303a3 Leszek Koltunski
public class EffectQueueSurfaceView extends GLSurfaceView
43 bc0a685b Leszek Koltunski
  {
44 30c47368 Leszek Koltunski
  private static final int RADIUS = 60;
45
46 758303a3 Leszek Koltunski
  private EffectQueueRenderer mRenderer;
47 3f07bedc Leszek Koltunski
  private int mCurrentEffect;
48
  private int mScrW, mScrH;
49 427ab7bf Leszek Koltunski
    
50 30c47368 Leszek Koltunski
  private Static4D mRegionV;
51 3f07bedc Leszek Koltunski
  private Dynamic1D mInterA, mInterB, mInterC, mInterS;
52
  private Dynamic3D mInterD;
53 f9afbbf3 Leszek Koltunski
54
  private final static Static3D mRED = new Static3D(1,0,0);
55
56 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
57 427ab7bf Leszek Koltunski
    
58 41a81a14 Leszek Koltunski
  public EffectQueueSurfaceView(Context context, AttributeSet attrs)
59 bc0a685b Leszek Koltunski
    {
60 41a81a14 Leszek Koltunski
    super(context, attrs);
61 427ab7bf Leszek Koltunski
      
62 3f07bedc Leszek Koltunski
    int duration = 10000;
63
    float count  = 1.0f;
64 59759251 Leszek Koltunski
    int h = 30;
65
    int r = 20;
66
67 3f07bedc Leszek Koltunski
    mInterD = new Dynamic3D(duration,count);
68 59759251 Leszek Koltunski
69 3f07bedc Leszek Koltunski
    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 59759251 Leszek Koltunski
74 3f07bedc Leszek Koltunski
    mInterA = new Dynamic1D(duration,count);
75 59759251 Leszek Koltunski
    mInterA.add(new Static1D(1));
76
    mInterA.add(new Static1D(0));
77
78 3f07bedc Leszek Koltunski
    mInterS = new Dynamic1D(duration,count);
79 b5cc7760 Leszek Koltunski
    mInterS.add(new Static1D(1.0f));
80
    mInterS.add(new Static1D(0.3f));
81
82 3f07bedc Leszek Koltunski
    mInterC = new Dynamic1D(duration,count);
83 f9afbbf3 Leszek Koltunski
    mInterC.add(new Static1D(1));
84
    mInterC.add(new Static1D(0));
85 59759251 Leszek Koltunski
86 3f07bedc Leszek Koltunski
    mInterB = new Dynamic1D(duration,count);
87 bdfec906 Leszek Koltunski
    mInterB.add(new Static1D(0));
88
    mInterB.add(new Static1D(1));
89 427ab7bf Leszek Koltunski
90 bc0a685b Leszek Koltunski
    if(!isInEditMode())
91
      {
92
      setFocusable(true);
93
      setFocusableInTouchMode(true);
94 758303a3 Leszek Koltunski
      mRenderer = new EffectQueueRenderer(this);
95 e4330c89 Leszek Koltunski
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
96
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
97
      setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
98 bc0a685b Leszek Koltunski
      setRenderer(mRenderer);
99 30c47368 Leszek Koltunski
      mRegionV= new Static4D(0,0,RADIUS,RADIUS);
100 bc0a685b Leszek Koltunski
      setEffect(0);  
101 427ab7bf Leszek Koltunski
      }
102 bc0a685b Leszek Koltunski
    }
103 427ab7bf Leszek Koltunski
104 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
105 427ab7bf Leszek Koltunski
106 758303a3 Leszek Koltunski
  EffectQueueRenderer getRenderer()
107 3f07bedc Leszek Koltunski
    {
108
    return mRenderer;
109
    }
110
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
113
  public void setScreenSize(int width, int height)
114 bc0a685b Leszek Koltunski
    {
115
    mScrW = width;
116
    mScrH = height;
117
    }
118 427ab7bf Leszek Koltunski
119 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
120 427ab7bf Leszek Koltunski
121 3f07bedc Leszek Koltunski
  public void setEffect(int effect)
122 bc0a685b Leszek Koltunski
    {
123
    mCurrentEffect = effect;
124
    }
125 427ab7bf Leszek Koltunski
126 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
127 427ab7bf Leszek Koltunski
    
128 bc0a685b Leszek Koltunski
  @Override public boolean onTouchEvent(MotionEvent event) 
129
    {
130 8dfa45c4 leszek
    int x,y,action = event.getAction();
131
    boolean success;
132 af225332 Leszek Koltunski
133 bc0a685b Leszek Koltunski
    switch(action)
134 427ab7bf Leszek Koltunski
      {
135 3f07bedc Leszek Koltunski
      case MotionEvent.ACTION_DOWN: x = (int)event.getX()* mRenderer.BWID/mScrW;
136
                                    y = (int)event.getY()* mRenderer.BHEI/mScrH;
137 758303a3 Leszek Koltunski
                                    EffectQueueActivity act = (EffectQueueActivity)getContext();
138 59759251 Leszek Koltunski
139 427ab7bf Leszek Koltunski
                                    switch(mCurrentEffect)
140
                                      {
141 30c47368 Leszek Koltunski
                                      case 0: VertexEffectDistort distort = new VertexEffectDistort(mInterD, new Static3D(x,y,0), mRegionV);
142 8dfa45c4 leszek
                                              success = mRenderer.getEffects().apply(distort);
143
                                              act.effectAdded(success,distort);
144 e763f1ee Leszek Koltunski
                                              break;
145 30c47368 Leszek Koltunski
                                      case 1: VertexEffectSink sink = new VertexEffectSink(mInterS, new Static3D(x,y,0), mRegionV);
146 8dfa45c4 leszek
                                              success = mRenderer.getEffects().apply(sink);
147
                                              act.effectAdded(success,sink);
148 e763f1ee Leszek Koltunski
                                              break;
149 30c47368 Leszek Koltunski
                                      case 2: FragmentEffectAlpha alpha = new FragmentEffectAlpha(mInterA, new Static4D(x,y,RADIUS,RADIUS), true);
150 8dfa45c4 leszek
                                              success = mRenderer.getEffects().apply(alpha);
151
                                              act.effectAdded(success,alpha);
152 e763f1ee Leszek Koltunski
                                              break;
153 30c47368 Leszek Koltunski
                                      case 3: FragmentEffectSaturation saturation = new FragmentEffectSaturation(mInterB, new Static4D(x,y,RADIUS,RADIUS), false);
154 8dfa45c4 leszek
                                              success = mRenderer.getEffects().apply(saturation);
155
                                              act.effectAdded(success,saturation);
156 e763f1ee Leszek Koltunski
                                              break;
157 30c47368 Leszek Koltunski
                                      case 4: FragmentEffectChroma chroma = new FragmentEffectChroma(mInterC, mRED, new Static4D(x,y,RADIUS,RADIUS), true);
158 8dfa45c4 leszek
                                              success = mRenderer.getEffects().apply(chroma);
159
                                              act.effectAdded(success,chroma);
160 e763f1ee Leszek Koltunski
                                              break;
161 427ab7bf Leszek Koltunski
                                      }
162 af225332 Leszek Koltunski
163 427ab7bf Leszek Koltunski
                                    break;
164
      }
165 af225332 Leszek Koltunski
166 bc0a685b Leszek Koltunski
    return true;
167
    }
168 af225332 Leszek Koltunski
169 427ab7bf Leszek Koltunski
  }