Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldSurfaceViewPicker.java @ fcb09e1f

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.aroundtheworld;
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.util.AttributeSet;
27
import android.view.MotionEvent;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
class AroundTheWorldSurfaceViewPicker extends GLSurfaceView
32
{
33
    private AroundTheWorldRendererPicker mRenderer;
34
    private AroundTheWorldEffectsManager mManager;
35

    
36
    private float mX, mY;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
    public AroundTheWorldSurfaceViewPicker(Context context, AttributeSet attrs)
41
      {
42
      super(context, attrs);
43

    
44
      if(!isInEditMode())
45
        {
46
        final ActivityManager activityManager     = (android.app.ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
47
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
48
        android.util.Log.e("View", "Using OpenGL ES "+configurationInfo.getGlEsVersion());
49
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
50
        mRenderer = new AroundTheWorldRendererPicker();
51
        setRenderer(mRenderer);
52
        }
53
      }
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
    public AroundTheWorldRendererPicker getRenderer()
58
      {
59
      return mRenderer;
60
      }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
    public void setEffectsManager(AroundTheWorldEffectsManager manager)
65
      {
66
      mManager = manager;
67
      }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
    float getx()
72
      {
73
      return mX;
74
      }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
    float gety()
79
      {
80
      return mY;
81
      }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
    void set(float x, float y)
86
      {
87
      mX = x;
88
      mY = y;
89

    
90
      mRenderer.move(mX,mY);
91

    
92
      float distance_from_left =(mX+0.865f)/(3*0.865f);
93
      float distance_from_top  =0.333f*(1-mY)-0.192f*mX;
94

    
95
      mManager.setRace( distance_from_top, distance_from_left );
96
      }
97

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

    
100
    @Override
101
    public boolean onTouchEvent(MotionEvent event)
102
      {
103
      int x = (int)event.getX();
104
      int y = (int)event.getY();
105

    
106
      int h = mRenderer.getHeight();
107
      int w = mRenderer.getWidth();
108

    
109
      mX = (x-w/2)*3.0f/h;
110
      mY =-(y-h/2)*3.0f/h;
111

    
112
      if( mY>-1.5f && mY<1.5f )
113
        {
114
        if( mY>=0 && mY> 1-0.577f*mX )
115
          {
116
          float d = 0.577f*mX + mY;
117
          mY /= d;
118
          mX /= d;
119
          }
120
        else if( mY<=0 && mY< 0.577f*mX-1 )
121
          {
122
          float d = 0.577f*mX - mY;
123
          mY /= d;
124
          mX /= d;
125
          }
126
        else if( mX< -0.865f )
127
          {
128
          mX  =  -0.865f;
129
          }
130

    
131
        set(mX,mY);
132
        }
133

    
134
      return true;
135
      }
136
}
137

    
(6-6/6)