Androidworld
Androidworld

GLSurfaceView besturen met buttons

#1 01-12-2011 13:20

Smek
Smek
Ingeschreven: 30-11-2011
Berichten: 4
Reputatie
Reputatie omhoog 0 Reputatie omlaag
Profiel    Stuur e-mail   
Hallo allemaal,

Ik heb een GLSurfaceView opgenomen in een layout xml bestand en nu wil ik bepaalde functionaliteit aansturen door middel van knoppen. Ik heb de kubus van les 7 uit deze pagina opgenomen in de view. Deze kun je besturen door de kubus met je vinger te draaien. Aan de bovenkant van het scherm kun je de kubus schalen. Dit schalen wil ik door middel van knoppen doen.

de layout:


<?xml version=“1.0” encoding=“utf-8”?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
androidrientation=”vertical“
android:layout_width=”fill_parent“
android:layout_height=”fill_parent“>

<!– Here is where we put the SurfaceView, in a frame so that we can
stack other views on top of it. –>
<FrameLayout
android:layout_width=”fill_parent“
android:layout_height=”0px“
android:layout_weight=”1“>

<com.nea.nehe.lesson07.Lesson07
android:id=”@+id/glsurfaceview“
android:layout_width=”fill_parent“
android:layout_height=”wrap_content“ />

<LinearLayout android:id=”@+id/hidecontainer“
androidrientation=”vertical“
android:visibility=”gone“
android:gravity=”center“
android:layout_width=”fill_parent“
android:layout_height=”fill_parent“>

<Button android:id=”@+id/hideme1“
android:layout_width=”wrap_content“
android:layout_height=”wrap_content“
android:layout_gravity=”center“
android:visibility=”gone“
android:text=”hide me“/>

<Button android:id=”@+id/hideme2“
android:layout_width=”wrap_content“
android:layout_height=”wrap_content“
android:layout_gravity=”center“
android:visibility=”gone“
android:text=”hide me“/>

</LinearLayout>

</FrameLayout>

<LinearLayout
androidrientation=”horizontal“
android:layout_width=”wrap_content“
android:layout_height=”wrap_content“
android:layout_gravity=”center“>

<Button android:id=”@+id/plus“
android:layout_width=”wrap_content“
android:layout_height=”wrap_content“
android:text=”+“/>

<Button android:id=”@+id/min“
android:layout_width=”wrap_content“
android:layout_height=”wrap_content“
android:text=”-"/>

</LinearLayout>

</LinearLayout>

de activity class:
import android.app.Activity;
import android.os.Bundle;

public class Run extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
}

/**
* Remember to resume our Lesson
*/
@Override
protected void onResume() {
super.onResume();
}

/**
* Also pause our Lesson
*/
@Override
protected void onPause() {
super.onPause();
}
}

de GLSurfaceView:
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class Lesson07 extends GLSurfaceView implements Renderer {

/** Cube instance */
private Cube cube;

/* Rotation values */
private float xrot; //X Rotation
private float yrot; //Y Rotation

/* Rotation speed values */
private float xspeed; //X Rotation Speed ( NEW )
private float yspeed; //Y Rotation Speed ( NEW )

private float z = -5.0f; //Depth Into The Screen ( NEW )

private int filter = 0; //Which texture filter? ( NEW )

/*
* These variables store the previous X and Y
* values as well as a fix touch scale factor.
* These are necessary for the rotation transformation
* added to this lesson, based on the screen touches. ( NEW )
*/
private float oldX;
private float oldY;
private final float TOUCH_SCALE = 0.2f; //Proved to be good for normal rotation ( NEW )

/** The Activity Context */
private Context context;

public Lesson07(Context context, AttributeSet attr) {
super(context, attr);

//Set this as Renderer
this.setRenderer(this);
//Request focus, otherwise buttons won't react
this.requestFocus();
this.setFocusableInTouchMode(true);

this.context = context;

cube = new Cube();
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {

//Settings
gl.glDisable(GL10.GL_DITHER); //Disable dithering ( NEW )
gl.glEnable(GL10.GL_TEXTURE_2D); //Enable Texture Mapping
gl.glShadeModel(GL10.GL_SMOOTH); //Enable Smooth Shading
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); //Black Background
gl.glClearDepthf(1.0f); //Depth Buffer Setup
gl.glEnable(GL10.GL_DEPTH_TEST); //Enables Depth Testing
gl.glDepthFunc(GL10.GL_LEQUAL); //The Type Of Depth Testing To Do

gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}

/**
* Here we do our drawing
*/
public void onDrawFrame(GL10 gl) {
//Clear Screen And Depth Buffer
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity(); //Reset The Current Modelview Matrix

gl.glTranslatef(0.0f, 0.0f, z); //Move z units into the screen
gl.glScalef(0.8f, 0.8f, 0.8f); //Scale the Cube to 80 percent, otherwise it would be too large for the screen

//Rotate around the axis based on the rotation matrix (rotation, x, y, z)
gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f); //X
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f); //Y

cube.draw(gl, filter); //Draw the Cube

//Change rotation factors
xrot += xspeed;
yrot += yspeed;
}

/**
* If the surface changes, reset the view
*/
public void onSurfaceChanged(GL10 gl, int width, int height) {
if(height == 0) { //Prevent A Divide By Zero By
height = 1; //Making Height Equal One
}

gl.glViewport(0, 0, width, height); //Reset The Current Viewport
gl.glMatrixMode(GL10.GL_PROJECTION); //Select The Projection Matrix
gl.glLoadIdentity(); //Reset The Projection Matrix

//Calculate The Aspect Ratio Of The Window
GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);

gl.glMatrixMode(GL10.GL_MODELVIEW); //Select The Modelview Matrix
gl.glLoadIdentity(); //Reset The Modelview Matrix
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
//
if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
yspeed -= 0.1f;

} else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
yspeed += 0.1f;

} else if(keyCode == KeyEvent.KEYCODE_DPAD_UP) {
xspeed -= 0.1f;

} else if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
xspeed += 0.1f;

} else if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
filter += 1;
if(filter > 2) {
filter = 0;
}
}

//We handled the event
return true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
//
float x = event.getX();
float y = event.getY();

//If a touch is moved on the screen
if(event.getAction() == MotionEvent.ACTION_MOVE) {
//Calculate the change
float dx = x - oldX;
float dy = y - oldY;
//Define an upper area of 10% on the screen
int upperArea = this.getHeight() / 10;

//Zoom in/out if the touch move has been made in the upper
if(y < upperArea) {
z -= dx * TOUCH_SCALE / 2;

//Rotate around the axis otherwise
} else {
xrot += dy * TOUCH_SCALE;
yrot += dx * TOUCH_SCALE;
}
}

//Remember the values
oldX = x;
oldY = y;

//We handled the event
return true;
}
}

Dit stukje draait het eigenlijk om die wil ik kunnen bedienen met knoppen.
//Define an upper area of 10% on the screen
int upperArea = this.getHeight() / 10;

//Zoom in/out if the touch move has been made in the upper
if(y < upperArea) {
z -= dx * TOUCH_SCALE / 2;

Iemand enig idee hoe ik dat zou moeten doen?

Vast bedankt!

Bijgewerkt (24-04-2013 13:47)