Project

General

Profile

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

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

1 7ba38011 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 7ba38011 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 7ba38011 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 7ba38011 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 7ba38011 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 643844c9 leszek
    private float mX, mY;
37
38 7ba38011 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
39
40 41a81a14 Leszek Koltunski
    public AroundTheWorldSurfaceViewPicker(Context context, AttributeSet attrs)
41 7ba38011 Leszek Koltunski
      {
42 41a81a14 Leszek Koltunski
      super(context, attrs);
43 7ba38011 Leszek Koltunski
44 248a2782 Leszek Koltunski
      if(!isInEditMode())
45 7ba38011 Leszek Koltunski
        {
46 41a81a14 Leszek Koltunski
        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 248a2782 Leszek Koltunski
        mRenderer = new AroundTheWorldRendererPicker();
51
        setRenderer(mRenderer);
52 7ba38011 Leszek Koltunski
        }
53
      }
54 2f20ae3a Leszek Koltunski
55 7ba38011 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
56
57 248a2782 Leszek Koltunski
    public AroundTheWorldRendererPicker getRenderer()
58 7ba38011 Leszek Koltunski
      {
59 248a2782 Leszek Koltunski
      return mRenderer;
60 7ba38011 Leszek Koltunski
      }
61
62 2f20ae3a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64
    public void setEffectsManager(AroundTheWorldEffectsManager manager)
65
      {
66
      mManager = manager;
67
      }
68
69 7ba38011 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
70
71 643844c9 leszek
    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 a4d59c0b Leszek Koltunski
    @Override
101
    public boolean onTouchEvent(MotionEvent event)
102 7ba38011 Leszek Koltunski
      {
103 248a2782 Leszek Koltunski
      int x = (int)event.getX();
104
      int y = (int)event.getY();
105 7ba38011 Leszek Koltunski
106 951c4af9 Leszek Koltunski
      int h = mRenderer.getHeight();
107
      int w = mRenderer.getWidth();
108
109 643844c9 leszek
      mX = (x-w/2)*3.0f/h;
110
      mY =-(y-h/2)*3.0f/h;
111 951c4af9 Leszek Koltunski
112 643844c9 leszek
      if( mY>-1.5f && mY<1.5f )
113 951c4af9 Leszek Koltunski
        {
114 643844c9 leszek
        if( mY>=0 && mY> 1-0.577f*mX )
115 951c4af9 Leszek Koltunski
          {
116 643844c9 leszek
          float d = 0.577f*mX + mY;
117
          mY /= d;
118
          mX /= d;
119 951c4af9 Leszek Koltunski
          }
120 643844c9 leszek
        else if( mY<=0 && mY< 0.577f*mX-1 )
121 951c4af9 Leszek Koltunski
          {
122 643844c9 leszek
          float d = 0.577f*mX - mY;
123
          mY /= d;
124
          mX /= d;
125 951c4af9 Leszek Koltunski
          }
126 643844c9 leszek
        else if( mX< -0.865f )
127 951c4af9 Leszek Koltunski
          {
128 643844c9 leszek
          mX  =  -0.865f;
129 951c4af9 Leszek Koltunski
          }
130
131 643844c9 leszek
        set(mX,mY);
132 951c4af9 Leszek Koltunski
        }
133 7ba38011 Leszek Koltunski
134
      return true;
135
      }
136
}