Manual is pretty vague even for regular views, and doesn’t even mention mGLSurfaceView in the UI events overview page.
For starters, all the examples will not work because they rely on the Id for the view (e.g. Button button = (Button)findViewById(R.id.corky);). I’m having a bad time trying to send UI events back and forth. I used the shotgun solution and attached every input event from the mGLSurfaceView to the Activity… But i’m craving for something better.
So, anyone brave enough to conjure a simple example with an Activity, a mGLSurfaceView, and the activity onTouch event saying where was the click relative to the size of the screen? (e.g. w=50%, h=50% for a perfect click on the center of the screen)
public class GlClick extends Activity implements OnTouchListener {
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
mGLSurfaceView = new GLSurfaceView(this);
mGLSurfaceView.setRenderer(new GlRenderer());
setContentView(mGLSurfaceView);
mGLSurfaceView.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
Log.d(TAG, "example output: click was on 234x211 for a 800x600 screen.")
}
}
class GlRenderer implements Renderer {
// here be lots of messy beginner GL code ...
// ... you'd have wished for dragons.
public void onSurfaceChanged(GL10 gl, int w, int h) {
screenWidth = w;
screenHeight = h;
//TODO: send that to the activity
}
}
I’m working on a app ogl on android
I don’t use any touch listener or anything pre made because sooner or later you lose time trying to figure out how to do something lol
what I found to work well is simply
and in the GameHandler I first convert the touch from the screen coordinates to the glworld coordinates.
in my ui elements I have the position and the size and I add a method “touch(…)” or “isTouched()”
by my experiences this works quite well and I recommend you to try it ^^
edit touch convertion
you need on the y to do height-y (ogl and android have different location of the origin)
GAME_DIMENSION_* is the size of the ogl world projection plane (800*600).
then its basic maths (I can’t remember the english name) but its thales formula x1/X1 = x2/X2.
Edit: Vector
You can browse internet for better examples such as http://www.gamedev.net/community/forums/topic.asp?topic_id=302771