libgdx input分析

来源:互联网 发布:linux 打包一个文件夹 编辑:程序博客网 时间:2024/06/05 07:38
AndroidApplication 里面:
public void initialize (ApplicationListener listener, AndroidApplicationConfiguration config) 
input = AndroidInputFactory.newAndroidInput(this, this, graphics.view, config);
代码为:
if (sdkVersion >= 12) {
clazz = Class.forName("com.badlogic.gdx.backends.android.AndroidInputThreePlus");
} else {
clazz = Class.forName("com.badlogic.gdx.backends.android.AndroidInput");
}
Constructor<?> constructor = clazz.getConstructor(Application.class, Context.class, Object.class,
AndroidApplicationConfiguration.class);
input = (AndroidInput)constructor.newInstance(activity, context, view, config);


AndroidInput 我们来看这个:
AndroidInput implements Input, OnKeyListener, OnTouchListener
接收按键 和触屏事件。
AndroidInput (Application activity, Context context, Object view, AndroidApplicationConfiguration config)
构造里面接收:activity view 和配置
v.setOnKeyListener(this);
v.setOnTouchListener(this);
如此即可将按键和触屏在此处接收了。

下来我们再看看
onKey 和onTouch 接口:
两个分别保存当前的按键和触屏消息即可。
真正的处理是在processEvents接口里面:
void processEvents () {
synchronized (this) {
justTouched = false;


if (processor != null) {
final InputProcessor processor = this.processor;


int len = keyEvents.size();
for (int i = 0; i < len; i++) {
KeyEvent e = keyEvents.get(i);
currentEventTimeStamp = e.timeStamp;
switch (e.type) {
case KeyEvent.KEY_DOWN:
processor.keyDown(e.keyCode);
break;
case KeyEvent.KEY_UP:
processor.keyUp(e.keyCode);
break;
case KeyEvent.KEY_TYPED:
processor.keyTyped(e.keyChar);
}
usedKeyEvents.free(e);
}


len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent e = touchEvents.get(i);
currentEventTimeStamp = e.timeStamp;
switch (e.type) {
case TouchEvent.TOUCH_DOWN:
processor.touchDown(e.x, e.y, e.pointer, Buttons.LEFT);
justTouched = true;
break;
case TouchEvent.TOUCH_UP:
processor.touchUp(e.x, e.y, e.pointer, Buttons.LEFT);
break;
case TouchEvent.TOUCH_DRAGGED:
processor.touchDragged(e.x, e.y, e.pointer);
}
usedTouchEvents.free(e);
}
} else {
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent e = touchEvents.get(i);
if (e.type == TouchEvent.TOUCH_DOWN) justTouched = true;
usedTouchEvents.free(e);
}


len = keyEvents.size();
for (int i = 0; i < len; i++) {
usedKeyEvents.free(keyEvents.get(i));
}
}


if (touchEvents.size() == 0) {
for (int i = 0; i < deltaX.length; i++) {
deltaX[0] = 0;
deltaY[0] = 0;
}
}


keyEvents.clear();
touchEvents.clear();
}
}
依据类别,将消息传递给InputProcessor去处理
我们需要处理输入事件时,使用代码:
Gdx.input.setInputProcessor(this);
    来将事件接收过来。
    下来我们看下 processEvents 接口在哪里触发的。
我们知道系统一直在触发ApplicationListener 里面 的render 接口,此接口是在AndroidGraphics里面onDrawFrame进行调用的。
((AndroidInput)app.getInput()).processEvents(); 此接口则会去分派输入消息。

关于onDrawFrame的流程,我们需要分析  GLSurfaceView类。

摘自网络的一段分析:

Android提供了两个基本的类让我们使用OpenGL ES API来创建和操纵图形:GLSurfaceView和 GLSurfaceView.Renderer。因此我们首先需要了解这两个类。


1.      GLSurfaceView:


这是一个视图类,你可以调用OpenGL API在上面绘制图形和操纵物体,功能和SurfaceView相似。我们可以创建一个GLSurfaceView类的实例,并添加自己的渲染器。如果我们要自己实现一些触摸屏的操作,我们必须扩展这个类来实现触摸监听器。


 


2.      GLSurfaceView.Renderer


这个接口定义了在一个OpenGL的GLSurfaceView中绘制图形所需要的方法。我们必须在一个单独的类中为这些接口提供实现,并使用GLSurfaceView.setRenderer()方法将它依附到GLSurfaceView实例对象上。


我们需要实现GLSurfaceView.Renderer的以下方法:


a)      onSurfaceCreated():系统在创建GLSurfaceView时调用它一次。我们可以使用它来设置OpenGL的环境变量,或是初始化OpenGL的图形物体。


b)      onDrawFrame():系统在每次重绘GLSurfaceView时调用这个方法。这个方法主要完成绘制图形的操作。


c)      onSurfaceChanged():系统在GLSurfaceView的几何属性发生改变时调用该方法,包括大小或是设备屏幕的方向发生变化。例如,系统在屏幕从直立变为水平使调用它。这个方法主要用来对GLSurfaceView容器的变化进行响应。


 


实验步骤


1.      添加新项目


2.      添加新类myGLRenderer,实现GLSurfaceView.Renderer接口


代码如下:


[java] <span style="font-size:16px;">public class myGLRenderer implements Renderer { 
 
    @Override 
    public void onDrawFrame(GL10 gl) { 
        // TODO Auto-generated method stub  
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);//清空缓存  
    } 
 
    @Override 
    public void onSurfaceChanged(GL10 gl, int width, int height) { 
        // TODO Auto-generated method stub  
        gl.glViewport(0, 0, width, height);//设置视口  
    } 
 
    @Override 
    public void onSurfaceCreated(GL10 gl, EGLConfig config) { 
        // TODO Auto-generated method stub  
        gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f); //设置清除色  
    } 
 

</span> 
<span style="font-size:16px;">public class myGLRenderer implements Renderer {


 @Override
 public void onDrawFrame(GL10 gl) {
  // TODO Auto-generated method stub
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);//清空缓存
 }


 @Override
 public void onSurfaceChanged(GL10 gl, int width, int height) {
  // TODO Auto-generated method stub
        gl.glViewport(0, 0, width, height);//设置视口
 }


 @Override
 public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  // TODO Auto-generated method stub
        gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f); //设置清除色
 }


}
</span>


 




3.      添加新的类myGLSurfaceView,父类为GLSurfaceView


代码如下:


 


[java] <span style="font-size:16px;">public class myGLSurfaceView extends GLSurfaceView { 
 
    public myGLSurfaceView(Context context) { 
        super(context); 
        // TODO Auto-generated constructor stub  
        mrender = new myGLRenderer(); 
        setRenderer(mrender); 
    } 
    private myGLRenderer mrender; 

</span> 
<span style="font-size:16px;">public class myGLSurfaceView extends GLSurfaceView {


 public myGLSurfaceView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
  mrender = new myGLRenderer();
  setRenderer(mrender);
 }
 private myGLRenderer mrender;
}
</span>


 




4.      主程序代码如下:


 


[java] <span style="font-size:16px;">public class HelloOpenGLActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        mGLSurfaceView = new myGLSurfaceView(this); 
        setContentView(mGLSurfaceView);//这里我们用mGLSurfaceView来替换以前常用的R.layout.main  
    }    
    private myGLSurfaceView mGLSurfaceView; 

</span> 
<span style="font-size:16px;">public class HelloOpenGLActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGLSurfaceView = new myGLSurfaceView(this);
        setContentView(mGLSurfaceView);//这里我们用mGLSurfaceView来替换以前常用的R.layout.main
    }  
    private myGLSurfaceView mGLSurfaceView;
}
</span>


 




这样,我们便完成了使用OpenGL绘制灰色背景的应用。当然这是最基本的功能了。后续我们会一起来探索如何使用OpenGL绘制简单的几何图形。


 


摘自 北京大学-Google Android实验室


此时input分析完毕。
0 0