Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effectqueue / EffectQueueSurfaceView.java @ 30f337e5

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.Effect;
30
import org.distorted.library.effect.FragmentEffectAlpha;
31
import org.distorted.library.effect.FragmentEffectChroma;
32
import org.distorted.library.effect.FragmentEffectSaturation;
33
import org.distorted.library.effect.VertexEffectDistort;
34
import org.distorted.library.effect.VertexEffectSink;
35
import org.distorted.library.type.Dynamic1D;
36
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39
import org.distorted.library.type.Dynamic3D;
40

    
41
import static org.distorted.examples.effectqueue.EffectQueueRenderer.*;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
public class EffectQueueSurfaceView extends GLSurfaceView
46
  {
47
  private static final float RADIUS = 0.15f;
48

    
49
  private EffectQueueRenderer mRenderer;
50
  private int mCurrentEffect;
51
  private int mScrW, mScrH;
52
    
53
  private final Dynamic1D mInterA;
54
    private final Dynamic1D mInterB;
55
    private final Dynamic1D mInterC;
56
    private final Dynamic1D mInterS;
57
  private final Dynamic3D mInterD;
58

    
59
  private final static Static3D RED      = new Static3D(1,0,0);
60
  private final static Static3D REGION_F = new Static3D(RADIUS,RADIUS,RADIUS);
61
  private final static Static4D REGION_V = new Static4D(0,0,0,RADIUS);
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
    
65
  public EffectQueueSurfaceView(Context context, AttributeSet attrs)
66
    {
67
    super(context, attrs);
68
      
69
    int duration = 10000;
70
    float count  = 1.0f;
71
    float h = 50.0f/BWID;
72
    float r = 20.0f/BWID;
73

    
74
    mInterD = new Dynamic3D(duration,count);
75

    
76
    mInterD.add(new Static3D( 0, r, h ));
77
    mInterD.add(new Static3D(-r, 0, h ));
78
    mInterD.add(new Static3D( 0,-r, h ));
79
    mInterD.add(new Static3D( r, 0, h ));
80

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

    
85
    mInterS = new Dynamic1D(duration,count);
86
    mInterS.add(new Static1D(1.0f));
87
    mInterS.add(new Static1D(0.3f));
88

    
89
    mInterC = new Dynamic1D(duration,count);
90
    mInterC.add(new Static1D(1));
91
    mInterC.add(new Static1D(0));
92

    
93
    mInterB = new Dynamic1D(duration,count);
94
    mInterB.add(new Static1D(0));
95
    mInterB.add(new Static1D(1));
96

    
97
    if(!isInEditMode())
98
      {
99
      setFocusable(true);
100
      setFocusableInTouchMode(true);
101
      mRenderer = new EffectQueueRenderer(this);
102
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
103
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
104
      setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
105
      setRenderer(mRenderer);
106
      setEffect(0);
107
      }
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
  EffectQueueRenderer getRenderer()
113
    {
114
    return mRenderer;
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  public void setScreenSize(int width, int height)
120
    {
121
    mScrW = width;
122
    mScrH = height;
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  public void setEffect(int effect)
128
    {
129
    mCurrentEffect = effect;
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
    
134
  @Override
135
  public boolean onTouchEvent(MotionEvent event)
136
    {
137
    int action = event.getAction();
138
    Effect effect;
139
    boolean success;
140
    float x,y;
141

    
142
    if(action==MotionEvent.ACTION_DOWN)
143
      {
144
      x = event.getX()/mScrW - 0.5f;
145
      y = 0.5f - event.getY()/mScrH;
146
      EffectQueueActivity act = (EffectQueueActivity)getContext();
147
      Static3D center = new Static3D(x,y,0);
148

    
149
      switch(mCurrentEffect)
150
        {
151
        case 0: effect = new VertexEffectDistort     (mInterD,      center, REGION_V);
152
                success= mRenderer.getEffects().apply(effect);
153
                act.effectAdded(success,effect);
154
                break;
155
        case 1: effect = new VertexEffectSink        (mInterS,      center, REGION_V);
156
                success= mRenderer.getEffects().apply(effect);
157
                act.effectAdded(success,effect);
158
                break;
159
        case 2: effect = new FragmentEffectAlpha     (mInterA,      center, REGION_F, true);
160
                success= mRenderer.getEffects().apply(effect);
161
                act.effectAdded(success,effect);
162
                break;
163
        case 3: effect = new FragmentEffectSaturation(mInterB,      center, REGION_F, false);
164
                success= mRenderer.getEffects().apply(effect);
165
                act.effectAdded(success,effect);
166
                break;
167
        case 4: effect = new FragmentEffectChroma    (mInterC, RED, center, REGION_F, true);
168
                success= mRenderer.getEffects().apply(effect);
169
                act.effectAdded(success,effect);
170
                break;
171
        }
172
      }
173

    
174
    return true;
175
    }
176

    
177
  }
(3-3/3)