Project

General

Profile

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

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

1 7ba38011 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
package org.distorted.examples.aroundtheworld;
21
22 41a81a14 Leszek Koltunski
import android.app.ActivityManager;
23 7ba38011 Leszek Koltunski
import android.content.Context;
24 41a81a14 Leszek Koltunski
import android.content.pm.ConfigurationInfo;
25 248a2782 Leszek Koltunski
import android.opengl.GLSurfaceView;
26 7ba38011 Leszek Koltunski
import android.util.AttributeSet;
27
import android.view.MotionEvent;
28
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30
31 248a2782 Leszek Koltunski
class AroundTheWorldSurfaceViewPicker extends GLSurfaceView
32 7ba38011 Leszek Koltunski
{
33 248a2782 Leszek Koltunski
    private AroundTheWorldRendererPicker mRenderer;
34 2f20ae3a Leszek Koltunski
    private AroundTheWorldEffectsManager mManager;
35 7ba38011 Leszek Koltunski
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37
38 41a81a14 Leszek Koltunski
    public AroundTheWorldSurfaceViewPicker(Context context, AttributeSet attrs)
39 7ba38011 Leszek Koltunski
      {
40 41a81a14 Leszek Koltunski
      super(context, attrs);
41 7ba38011 Leszek Koltunski
42 248a2782 Leszek Koltunski
      if(!isInEditMode())
43 7ba38011 Leszek Koltunski
        {
44 41a81a14 Leszek Koltunski
        final ActivityManager activityManager     = (android.app.ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
45
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
46
        android.util.Log.e("View", "Using OpenGL ES "+configurationInfo.getGlEsVersion());
47
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
48 248a2782 Leszek Koltunski
        mRenderer = new AroundTheWorldRendererPicker();
49
        setRenderer(mRenderer);
50 7ba38011 Leszek Koltunski
        }
51
      }
52 2f20ae3a Leszek Koltunski
53 7ba38011 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55 248a2782 Leszek Koltunski
    public AroundTheWorldRendererPicker getRenderer()
56 7ba38011 Leszek Koltunski
      {
57 248a2782 Leszek Koltunski
      return mRenderer;
58 7ba38011 Leszek Koltunski
      }
59
60 2f20ae3a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62
    public void setEffectsManager(AroundTheWorldEffectsManager manager)
63
      {
64
      mManager = manager;
65
      }
66
67 7ba38011 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69 248a2782 Leszek Koltunski
    @Override public boolean onTouchEvent(MotionEvent event)
70 7ba38011 Leszek Koltunski
      {
71 248a2782 Leszek Koltunski
      int x = (int)event.getX();
72
      int y = (int)event.getY();
73 7ba38011 Leszek Koltunski
74 951c4af9 Leszek Koltunski
      int h = mRenderer.getHeight();
75
      int w = mRenderer.getWidth();
76
77
      float xf = (x-w/2)*3.0f/h;
78
      float yf =-(y-h/2)*3.0f/h;
79
80
      if( yf>-1.5f && yf<1.5f )
81
        {
82
        if( yf>=0 && yf> 1-0.577f*xf )
83
          {
84
          float d = 0.577f*xf + yf;
85
          yf /= d;
86
          xf /= d;
87
          }
88
        else if( yf<=0 && yf< 0.577f*xf-1 )
89
          {
90
          float d = 0.577f*xf - yf;
91
          yf /= d;
92
          xf /= d;
93
          }
94
        else if( xf< -0.865f )
95
          {
96
          xf  =  -0.865f;
97
          }
98
99
        mRenderer.move(xf,yf);
100 e6995f3b Leszek Koltunski
101
        float distance_from_left =(xf+0.865f)/(3*0.865f);
102
        float distance_from_top  =0.333f*(1-yf)-0.192f*xf;
103
104
        mManager.setRace( distance_from_top, distance_from_left );
105 951c4af9 Leszek Koltunski
        }
106 7ba38011 Leszek Koltunski
107
      return true;
108
      }
109
}