-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyGLSurfaceView.java
More file actions
138 lines (113 loc) · 4.32 KB
/
MyGLSurfaceView.java
File metadata and controls
138 lines (113 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.example.f1;
import android.content.Context;
import android.graphics.Point;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.WindowManager;
import androidx.constraintlayout.widget.ConstraintSet;
public class MyGLSurfaceView extends GLSurfaceView {
public final float SENSIBILITE = 20f;
public final static float SEPARTATIONBOUTTONA = 0.16f;
public final static float SEPARTATIONBOUTTONB = 0.32f;
public final static float SEPARTATIONBOUTTONC = 0.48f;
public final static float SEPARTATIONBOUTTOND = 0.70f;
public final static float SEPARTATIONBOUTTONE = 0.85f;
private final MyGLRenderer renderer;
private Game game;
private int width;
private int height;
//sensor
SensorManager sm;
Sensor sensor;
public MyGLSurfaceView(Context context) {
super(context);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width=size.x;
height=size.y;
Log.i("Triangle moa", "Triangle moa size ("+width+";"+height+")");
//create an OpenGL ES 2.0 context
setEGLContextClientVersion(2);
renderer = new MyGLRenderer();
//set the Renderer for drawing on the GLSurfaceView
setRenderer(renderer);
sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//setRequestedOrientation "Landsacpe"
}
@Override
public boolean onTouchEvent(MotionEvent e) {
//Log.i("Triangle moa", "Triangle moa on TouchEvent debut");
float touchX = (float)(e.getX()/width);
float touchY = (float)(e.getY()/height);
game = renderer.getGame();//besoin de l'initialiser a chaque fois sinon crash
if(e.getAction()==MotionEvent.ACTION_DOWN){
Log.i("Triangle moa", "Triangle moa on TouchEvent down (" + touchX + ";" + touchY);
//game.buttonTouch(touchX, touchY);
if(touchX<SEPARTATIONBOUTTONA){
// btn couleur 1
}else if(touchX<SEPARTATIONBOUTTONB){
//btn couleur 2
}else if(touchX<SEPARTATIONBOUTTONC){
//btn couleur 3
}else if(touchX<SEPARTATIONBOUTTOND){
//btn gauche reculer
game.setPersonnageAcceleration(-1);
}else if(touchX<SEPARTATIONBOUTTONE){
//btn milieu avancer
game.setPersonnageAcceleration(1);
}else{
//btn droite accelerer
game.setPersonnageAcceleration(2);
}
}else if(e.getAction()==MotionEvent.ACTION_UP){
Log.i("Triangle moa", "Triangle moa action up");
//relacher gas
game.setPersonnageAcceleration(0);
}
if(touchX<SEPARTATIONBOUTTONC){
//juste click, pas besoin de action down
return false;
}else {
//pas juste click, besoin de action down
return true;
}
//return false;
}
@Override
public void onResume(){
super.onResume();
sm.registerListener(gyroListener, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onPause(){
super.onPause();
sm.unregisterListener(gyroListener);
}
public SensorEventListener gyroListener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
@Override
public void onSensorChanged(SensorEvent event) {
//float x = event.values[0]*SENSIBILITE;
float y = event.values[1]*SENSIBILITE;
//float z = -event.values[2]*SENSIBILITE;
//Log.i("Triangle moa", "Triangle moa sensor (" + x + ";" + y + ";" + z + ")");
game = renderer.getGame();
if(game!=null){
game.setPersonnageRotation(y);
}
}
};
}