From da78032542537c88c6141772d7ecb719f2f7d797 Mon Sep 17 00:00:00 2001 From: Macarse Date: Sun, 10 Apr 2011 17:57:17 -0300 Subject: [PATCH 1/2] Added the possibility of inflating Cocos2dxGLSurfaceView from an xml --- .../gen/org/cocos2dx/tests/R.java | 35 ++++++++++--------- tests/test.android/res/layout/test_demo.xml | 10 ++++++ .../cocos2dx/lib/Cocos2dxGLSurfaceView.java | 28 ++++++++++----- .../src/org/cocos2dx/tests/TestsDemo.java | 7 ++-- 4 files changed, 50 insertions(+), 30 deletions(-) create mode 100644 tests/test.android/res/layout/test_demo.xml diff --git a/tests/test.android/gen/org/cocos2dx/tests/R.java b/tests/test.android/gen/org/cocos2dx/tests/R.java index 44aef6fd17..b927797d6f 100644 --- a/tests/test.android/gen/org/cocos2dx/tests/R.java +++ b/tests/test.android/gen/org/cocos2dx/tests/R.java @@ -1,16 +1,19 @@ -/* AUTO-GENERATED FILE. DO NOT MODIFY. - * - * This class was automatically generated by the - * aapt tool from the resource data it found. It - * should not be modified by hand. - */ - -package org.cocos2dx.tests; - -public final class R { - public static final class attr { - } - public static final class string { - public static final int app_name=0x7f020000; - } -} +/* AUTO-GENERATED FILE. DO NOT MODIFY. + * + * This class was automatically generated by the + * aapt tool from the resource data it found. It + * should not be modified by hand. + */ + +package org.cocos2dx.tests; + +public final class R { + public static final class attr { + } + public static final class layout { + public static final int test_demo=0x7f020000; + } + public static final class string { + public static final int app_name=0x7f030000; + } +} diff --git a/tests/test.android/res/layout/test_demo.xml b/tests/test.android/res/layout/test_demo.xml new file mode 100644 index 0000000000..3879e59741 --- /dev/null +++ b/tests/test.android/res/layout/test_demo.xml @@ -0,0 +1,10 @@ + + + + + + diff --git a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java index a50181603b..f7b64beb08 100644 --- a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java +++ b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java @@ -2,11 +2,12 @@ package org.cocos2dx.lib; import android.content.Context; import android.opengl.GLSurfaceView; +import android.util.AttributeSet; import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; -public class Cocos2dxGLSurfaceView extends GLSurfaceView{ +public class Cocos2dxGLSurfaceView extends GLSurfaceView { private static final String TAG = Cocos2dxGLSurfaceView.class .getCanonicalName(); private Cocos2dxRenderer mRenderer; @@ -14,6 +15,15 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView{ public Cocos2dxGLSurfaceView(Context context) { super(context); + initView(); + } + + public Cocos2dxGLSurfaceView(Context context, AttributeSet attrs) { + super(context, attrs); + initView(); + } + + private void initView() { mRenderer = new Cocos2dxRenderer(); setFocusableInTouchMode(true); setRenderer(mRenderer); @@ -29,12 +39,12 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView{ for (int i = 0; i < pointerNumber; i++) { ids[i] = event.getPointerId(i); xs[i] = event.getX(i); - ys[i] = event.getY(i); + ys[i] = event.getY(i); } switch (event.getAction() & MotionEvent.ACTION_MASK) { - case MotionEvent.ACTION_POINTER_DOWN: - final int idPointerDown = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT; + case MotionEvent.ACTION_POINTER_DOWN: + final int idPointerDown = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT; final float xPointerDown = event.getX(idPointerDown); final float yPointerDown = event.getY(idPointerDown); @@ -46,9 +56,9 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView{ }); break; - case MotionEvent.ACTION_DOWN: + case MotionEvent.ACTION_DOWN: // there are only one finger on the screen - final int idDown = event.getPointerId(0); + final int idDown = event.getPointerId(0); final float xDown = event.getX(idDown); final float yDown = event.getY(idDown); @@ -60,7 +70,7 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView{ }); break; - case MotionEvent.ACTION_MOVE: + case MotionEvent.ACTION_MOVE: queueEvent(new Runnable() { @Override public void run() { @@ -69,8 +79,8 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView{ }); break; - case MotionEvent.ACTION_POINTER_UP: - final int idPointerUp = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT; + case MotionEvent.ACTION_POINTER_UP: + final int idPointerUp = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT; final float xPointerUp = event.getX(idPointerUp); final float yPointerUp = event.getY(idPointerUp); diff --git a/tests/test.android/src/org/cocos2dx/tests/TestsDemo.java b/tests/test.android/src/org/cocos2dx/tests/TestsDemo.java index 84924905b3..2796593b14 100644 --- a/tests/test.android/src/org/cocos2dx/tests/TestsDemo.java +++ b/tests/test.android/src/org/cocos2dx/tests/TestsDemo.java @@ -15,8 +15,7 @@ public class TestsDemo extends Cocos2dxActivity{ String packageName = getApplication().getPackageName(); super.setPackgeName(packageName); - mGLView = new Cocos2dxGLSurfaceView(this); - setContentView(mGLView); + setContentView(R.layout.test_demo); } @Override @@ -35,9 +34,7 @@ public class TestsDemo extends Cocos2dxActivity{ android.os.Process.killProcess(android.os.Process.myPid()); } - - private GLSurfaceView mGLView; - + static { System.loadLibrary("cocosdenshion"); System.loadLibrary("chipmunk"); From 0203141aafca9194c47d986355c7e8a3085085c3 Mon Sep 17 00:00:00 2001 From: Macarse Date: Mon, 11 Apr 2011 00:28:12 -0300 Subject: [PATCH 2/2] Now the size depends on the GLView size --- .../gen/org/cocos2dx/tests/R.java | 3 +++ tests/test.android/res/layout/test_demo.xml | 8 ++++++- .../src/org/cocos2dx/tests/TestsDemo.java | 21 ++++++++++++++----- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/tests/test.android/gen/org/cocos2dx/tests/R.java b/tests/test.android/gen/org/cocos2dx/tests/R.java index b927797d6f..217a1922f6 100644 --- a/tests/test.android/gen/org/cocos2dx/tests/R.java +++ b/tests/test.android/gen/org/cocos2dx/tests/R.java @@ -10,6 +10,9 @@ package org.cocos2dx.tests; public final class R { public static final class attr { } + public static final class id { + public static final int test_demo_gl_surfaceview=0x7f040000; + } public static final class layout { public static final int test_demo=0x7f020000; } diff --git a/tests/test.android/res/layout/test_demo.xml b/tests/test.android/res/layout/test_demo.xml index 3879e59741..f44fe3b140 100644 --- a/tests/test.android/res/layout/test_demo.xml +++ b/tests/test.android/res/layout/test_demo.xml @@ -1,9 +1,15 @@ + android:layout_height="fill_parent" + android:orientation="vertical"> + + diff --git a/tests/test.android/src/org/cocos2dx/tests/TestsDemo.java b/tests/test.android/src/org/cocos2dx/tests/TestsDemo.java index 2796593b14..a3702a182c 100644 --- a/tests/test.android/src/org/cocos2dx/tests/TestsDemo.java +++ b/tests/test.android/src/org/cocos2dx/tests/TestsDemo.java @@ -3,12 +3,12 @@ package org.cocos2dx.tests; import org.cocos2dx.lib.Cocos2dxActivity; import org.cocos2dx.lib.Cocos2dxGLSurfaceView; - -import android.opengl.GLSurfaceView; import android.os.Bundle; public class TestsDemo extends Cocos2dxActivity{ - protected void onCreate(Bundle savedInstanceState){ + private Cocos2dxGLSurfaceView mGLView; + + protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); // get the packageName,it's used to set the resource path @@ -16,8 +16,19 @@ public class TestsDemo extends Cocos2dxActivity{ super.setPackgeName(packageName); setContentView(R.layout.test_demo); - } - + mGLView = (Cocos2dxGLSurfaceView) findViewById(R.id.test_demo_gl_surfaceview); + + // Get the size of the mGLView after the layout happens + mGLView.post(new Runnable() { + + @Override + public void run() { + Cocos2dxActivity.screenHeight = mGLView.getHeight(); + Cocos2dxActivity.screenWidth = mGLView.getWidth(); + } + }); + } + @Override protected void onPause() { super.onPause();