mirror of https://github.com/axmolengine/axmol.git
Tests : Use cocos2dandroid library
* Remove code and resources that are now in a library * Add library as a dependency for HelloWorld * Use cocos2dx_default_ resources
This commit is contained in:
parent
4a4d051cee
commit
d86e088e9f
|
@ -9,3 +9,5 @@
|
|||
|
||||
# Project target.
|
||||
target=android-8
|
||||
|
||||
android.library.reference.1=../../cocos2dx/platform/android/java
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<org.cocos2dx.lib.Cocos2dxEditText
|
||||
android:id="@+id/textField"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="fill_parent"
|
||||
android:background="@null"/>
|
||||
|
||||
<org.cocos2dx.lib.Cocos2dxGLSurfaceView
|
||||
android:id="@+id/test_demo_gl_surfaceview"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"/>
|
||||
|
||||
</FrameLayout>
|
|
@ -1,107 +0,0 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2010-2011 cocos2d-x.org
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
package org.cocos2dx.lib;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Configuration;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
import android.view.Display;
|
||||
import android.view.Surface;
|
||||
import android.view.WindowManager;
|
||||
|
||||
/**
|
||||
*
|
||||
* This class is used for controlling the Accelerometer
|
||||
*
|
||||
*/
|
||||
public class Cocos2dxAccelerometer implements SensorEventListener {
|
||||
|
||||
private static final String TAG = "Cocos2dxAccelerometer";
|
||||
private Context mContext;
|
||||
private SensorManager mSensorManager;
|
||||
private Sensor mAccelerometer;
|
||||
private int mNaturalOrientation;
|
||||
|
||||
public Cocos2dxAccelerometer(Context context){
|
||||
mContext = context;
|
||||
|
||||
//Get an instance of the SensorManager
|
||||
mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
|
||||
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
|
||||
|
||||
Display display = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
|
||||
mNaturalOrientation = display.getOrientation();
|
||||
}
|
||||
|
||||
public void enable() {
|
||||
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_GAME);
|
||||
}
|
||||
|
||||
public void disable () {
|
||||
mSensorManager.unregisterListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
|
||||
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER){
|
||||
return;
|
||||
}
|
||||
|
||||
float x = event.values[0];
|
||||
float y = event.values[1];
|
||||
float z = event.values[2];
|
||||
|
||||
/*
|
||||
* Because the axes are not swapped when the device's screen orientation changes.
|
||||
* So we should swap it here.
|
||||
* In tablets such as Motorola Xoom, the default orientation is landscape, so should
|
||||
* consider this.
|
||||
*/
|
||||
int orientation = mContext.getResources().getConfiguration().orientation;
|
||||
if ((orientation == Configuration.ORIENTATION_LANDSCAPE) && (mNaturalOrientation != Surface.ROTATION_0)){
|
||||
float tmp = x;
|
||||
x = -y;
|
||||
y = tmp;
|
||||
}
|
||||
else if ((orientation == Configuration.ORIENTATION_PORTRAIT) && (mNaturalOrientation != Surface.ROTATION_0))
|
||||
{
|
||||
float tmp = x;
|
||||
x = y;
|
||||
y = -tmp;
|
||||
}
|
||||
|
||||
onSensorChanged(x, y, z, event.timestamp);
|
||||
// Log.d(TAG, "x = " + event.values[0] + " y = " + event.values[1] + " z = " + event.values[2]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
}
|
||||
|
||||
private static native void onSensorChanged(float x, float y, float z, long timeStamp);
|
||||
}
|
|
@ -1,253 +0,0 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2010-2011 cocos2d-x.org
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
package org.cocos2dx.lib;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.Log;
|
||||
|
||||
public class Cocos2dxActivity extends Activity{
|
||||
private static Cocos2dxMusic backgroundMusicPlayer;
|
||||
private static Cocos2dxSound soundPlayer;
|
||||
private static Cocos2dxAccelerometer accelerometer;
|
||||
private static boolean accelerometerEnabled = false;
|
||||
private static Handler handler;
|
||||
private final static int HANDLER_SHOW_DIALOG = 1;
|
||||
private static String packageName;
|
||||
|
||||
private static native void nativeSetPaths(String apkPath);
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// get frame size
|
||||
DisplayMetrics dm = new DisplayMetrics();
|
||||
getWindowManager().getDefaultDisplay().getMetrics(dm);
|
||||
accelerometer = new Cocos2dxAccelerometer(this);
|
||||
|
||||
// init media player and sound player
|
||||
backgroundMusicPlayer = new Cocos2dxMusic(this);
|
||||
soundPlayer = new Cocos2dxSound(this);
|
||||
|
||||
// init bitmap context
|
||||
Cocos2dxBitmap.setContext(this);
|
||||
|
||||
handler = new Handler(){
|
||||
public void handleMessage(Message msg){
|
||||
switch(msg.what){
|
||||
case HANDLER_SHOW_DIALOG:
|
||||
showDialog(((DialogMessage)msg.obj).title, ((DialogMessage)msg.obj).message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public static String getCurrentLanguage() {
|
||||
String languageName = java.util.Locale.getDefault().getLanguage();
|
||||
return languageName;
|
||||
}
|
||||
|
||||
public static void showMessageBox(String title, String message){
|
||||
Message msg = new Message();
|
||||
msg.what = HANDLER_SHOW_DIALOG;
|
||||
msg.obj = new DialogMessage(title, message);
|
||||
|
||||
handler.sendMessage(msg);
|
||||
}
|
||||
|
||||
public static void enableAccelerometer() {
|
||||
accelerometerEnabled = true;
|
||||
accelerometer.enable();
|
||||
}
|
||||
|
||||
public static void disableAccelerometer() {
|
||||
accelerometerEnabled = false;
|
||||
accelerometer.disable();
|
||||
}
|
||||
|
||||
public static void preloadBackgroundMusic(String path){
|
||||
backgroundMusicPlayer.preloadBackgroundMusic(path);
|
||||
}
|
||||
|
||||
public static void playBackgroundMusic(String path, boolean isLoop){
|
||||
backgroundMusicPlayer.playBackgroundMusic(path, isLoop);
|
||||
}
|
||||
|
||||
public static void stopBackgroundMusic(){
|
||||
backgroundMusicPlayer.stopBackgroundMusic();
|
||||
}
|
||||
|
||||
public static void pauseBackgroundMusic(){
|
||||
backgroundMusicPlayer.pauseBackgroundMusic();
|
||||
}
|
||||
|
||||
public static void resumeBackgroundMusic(){
|
||||
backgroundMusicPlayer.resumeBackgroundMusic();
|
||||
}
|
||||
|
||||
public static void rewindBackgroundMusic(){
|
||||
backgroundMusicPlayer.rewindBackgroundMusic();
|
||||
}
|
||||
|
||||
public static boolean isBackgroundMusicPlaying(){
|
||||
return backgroundMusicPlayer.isBackgroundMusicPlaying();
|
||||
}
|
||||
|
||||
public static float getBackgroundMusicVolume(){
|
||||
return backgroundMusicPlayer.getBackgroundVolume();
|
||||
}
|
||||
|
||||
public static void setBackgroundMusicVolume(float volume){
|
||||
backgroundMusicPlayer.setBackgroundVolume(volume);
|
||||
}
|
||||
|
||||
public static int playEffect(String path, boolean isLoop){
|
||||
return soundPlayer.playEffect(path, isLoop);
|
||||
}
|
||||
|
||||
public static void stopEffect(int soundId){
|
||||
soundPlayer.stopEffect(soundId);
|
||||
}
|
||||
|
||||
public static void pauseEffect(int soundId){
|
||||
soundPlayer.pauseEffect(soundId);
|
||||
}
|
||||
|
||||
public static void resumeEffect(int soundId){
|
||||
soundPlayer.resumeEffect(soundId);
|
||||
}
|
||||
|
||||
public static float getEffectsVolume(){
|
||||
return soundPlayer.getEffectsVolume();
|
||||
}
|
||||
|
||||
public static void setEffectsVolume(float volume){
|
||||
soundPlayer.setEffectsVolume(volume);
|
||||
}
|
||||
|
||||
public static void preloadEffect(String path){
|
||||
soundPlayer.preloadEffect(path);
|
||||
}
|
||||
|
||||
public static void unloadEffect(String path){
|
||||
soundPlayer.unloadEffect(path);
|
||||
}
|
||||
|
||||
public static void stopAllEffects(){
|
||||
soundPlayer.stopAllEffects();
|
||||
}
|
||||
|
||||
public static void pauseAllEffects(){
|
||||
soundPlayer.pauseAllEffects();
|
||||
}
|
||||
|
||||
public static void resumeAllEffects(){
|
||||
soundPlayer.resumeAllEffects();
|
||||
}
|
||||
|
||||
public static void end(){
|
||||
backgroundMusicPlayer.end();
|
||||
soundPlayer.end();
|
||||
}
|
||||
|
||||
public static String getCocos2dxPackageName(){
|
||||
return packageName;
|
||||
}
|
||||
|
||||
public static void terminateProcess(){
|
||||
android.os.Process.killProcess(android.os.Process.myPid());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
if (accelerometerEnabled) {
|
||||
accelerometer.enable();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
if (accelerometerEnabled) {
|
||||
accelerometer.disable();
|
||||
}
|
||||
}
|
||||
|
||||
protected void setPackageName(String packageName) {
|
||||
Cocos2dxActivity.packageName = packageName;
|
||||
|
||||
String apkFilePath = "";
|
||||
ApplicationInfo appInfo = null;
|
||||
PackageManager packMgmr = getApplication().getPackageManager();
|
||||
try {
|
||||
appInfo = packMgmr.getApplicationInfo(packageName, 0);
|
||||
} catch (NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("Unable to locate assets, aborting...");
|
||||
}
|
||||
apkFilePath = appInfo.sourceDir;
|
||||
Log.w("apk path", apkFilePath);
|
||||
|
||||
// add this link at the renderer class
|
||||
nativeSetPaths(apkFilePath);
|
||||
}
|
||||
|
||||
private void showDialog(String title, String message){
|
||||
Dialog dialog = new AlertDialog.Builder(this)
|
||||
.setTitle(title)
|
||||
.setMessage(message)
|
||||
.setPositiveButton("Ok",
|
||||
new DialogInterface.OnClickListener()
|
||||
{
|
||||
public void onClick(DialogInterface dialog, int whichButton){
|
||||
|
||||
}
|
||||
}).create();
|
||||
|
||||
dialog.show();
|
||||
}
|
||||
}
|
||||
|
||||
class DialogMessage {
|
||||
public String title;
|
||||
public String message;
|
||||
|
||||
public DialogMessage(String title, String message){
|
||||
this.message = message;
|
||||
this.title = title;
|
||||
}
|
||||
}
|
|
@ -1,413 +0,0 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2010-2011 cocos2d-x.org
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
package org.cocos2dx.lib;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Typeface;
|
||||
import android.graphics.Paint.Align;
|
||||
import android.graphics.Paint.FontMetricsInt;
|
||||
import android.util.Log;
|
||||
|
||||
public class Cocos2dxBitmap{
|
||||
/*
|
||||
* The values are the same as cocos2dx/platform/CCImage.h.
|
||||
*/
|
||||
private static final int HALIGNCENTER = 3;
|
||||
private static final int HALIGNLEFT = 1;
|
||||
private static final int HALIGNRIGHT = 2;
|
||||
// vertical alignment
|
||||
private static final int VALIGNTOP = 1;
|
||||
private static final int VALIGNBOTTOM = 2;
|
||||
private static final int VALIGNCENTER = 3;
|
||||
|
||||
private static Context context;
|
||||
|
||||
public static void setContext(Context context){
|
||||
Cocos2dxBitmap.context = context;
|
||||
}
|
||||
|
||||
/*
|
||||
* @width: the width to draw, it can be 0
|
||||
* @height: the height to draw, it can be 0
|
||||
*/
|
||||
public static void createTextBitmap(String content, String fontName,
|
||||
int fontSize, int alignment, int width, int height){
|
||||
|
||||
content = refactorString(content);
|
||||
Paint paint = newPaint(fontName, fontSize, alignment);
|
||||
|
||||
TextProperty textProperty = computeTextProperty(content, paint, width, height);
|
||||
|
||||
int bitmapTotalHeight = (height == 0 ? textProperty.totalHeight:height);
|
||||
|
||||
// Draw text to bitmap
|
||||
Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth,
|
||||
bitmapTotalHeight, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
|
||||
// Draw string
|
||||
FontMetricsInt fm = paint.getFontMetricsInt();
|
||||
int x = 0;
|
||||
int y = computeY(fm, height, textProperty.totalHeight, alignment);
|
||||
String[] lines = textProperty.lines;
|
||||
for (String line : lines){
|
||||
x = computeX(paint, line, textProperty.maxWidth, alignment);
|
||||
canvas.drawText(line, x, y, paint);
|
||||
y += textProperty.heightPerLine;
|
||||
}
|
||||
|
||||
initNativeObject(bitmap);
|
||||
}
|
||||
|
||||
private static int computeX(Paint paint, String content, int w, int alignment){
|
||||
int ret = 0;
|
||||
int hAlignment = alignment & 0x0F;
|
||||
|
||||
switch (hAlignment){
|
||||
case HALIGNCENTER:
|
||||
ret = w / 2;
|
||||
break;
|
||||
|
||||
// ret = 0
|
||||
case HALIGNLEFT:
|
||||
break;
|
||||
|
||||
case HALIGNRIGHT:
|
||||
ret = w;
|
||||
break;
|
||||
|
||||
/*
|
||||
* Default is align left.
|
||||
* Should be same as newPaint().
|
||||
*/
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private static int computeY(FontMetricsInt fm, int constrainHeight, int totalHeight, int alignment) {
|
||||
int y = -fm.top;
|
||||
|
||||
if (constrainHeight > totalHeight) {
|
||||
int vAlignment = (alignment >> 4) & 0x0F;
|
||||
|
||||
switch (vAlignment) {
|
||||
case VALIGNTOP:
|
||||
y = -fm.top;
|
||||
break;
|
||||
case VALIGNCENTER:
|
||||
y = -fm.top + (constrainHeight - totalHeight)/2;
|
||||
break;
|
||||
case VALIGNBOTTOM:
|
||||
y = -fm.top + (constrainHeight - totalHeight);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
private static class TextProperty{
|
||||
// The max width of lines
|
||||
int maxWidth;
|
||||
// The height of all lines
|
||||
int totalHeight;
|
||||
int heightPerLine;
|
||||
String[] lines;
|
||||
|
||||
TextProperty(int w, int h, String[] lines){
|
||||
this.maxWidth = w;
|
||||
this.heightPerLine = h;
|
||||
this.totalHeight = h * lines.length;
|
||||
this.lines = lines;
|
||||
}
|
||||
}
|
||||
|
||||
private static TextProperty computeTextProperty(String content, Paint paint,
|
||||
int maxWidth, int maxHeight){
|
||||
FontMetricsInt fm = paint.getFontMetricsInt();
|
||||
int h = (int)Math.ceil(fm.bottom - fm.top);
|
||||
int maxContentWidth = 0;
|
||||
|
||||
String[] lines = splitString(content, maxHeight, maxWidth, paint);
|
||||
|
||||
if (maxWidth != 0){
|
||||
maxContentWidth = maxWidth;
|
||||
}
|
||||
else {
|
||||
/*
|
||||
* Compute the max width
|
||||
*/
|
||||
int temp = 0;
|
||||
for (String line : lines){
|
||||
temp = (int)Math.ceil(paint.measureText(line, 0, line.length()));
|
||||
if (temp > maxContentWidth){
|
||||
maxContentWidth = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new TextProperty(maxContentWidth, h, lines);
|
||||
}
|
||||
|
||||
/*
|
||||
* If maxWidth or maxHeight is not 0,
|
||||
* split the string to fix the maxWidth and maxHeight.
|
||||
*/
|
||||
private static String[] splitString(String content, int maxHeight, int maxWidth,
|
||||
Paint paint){
|
||||
String[] lines = content.split("\\n");
|
||||
String[] ret = null;
|
||||
FontMetricsInt fm = paint.getFontMetricsInt();
|
||||
int heightPerLine = (int)Math.ceil(fm.bottom - fm.top);
|
||||
int maxLines = maxHeight / heightPerLine;
|
||||
|
||||
if (maxWidth != 0){
|
||||
LinkedList<String> strList = new LinkedList<String>();
|
||||
for (String line : lines){
|
||||
/*
|
||||
* The width of line is exceed maxWidth, should divide it into
|
||||
* two or more lines.
|
||||
*/
|
||||
int lineWidth = (int)Math.ceil(paint.measureText(line));
|
||||
if (lineWidth > maxWidth){
|
||||
strList.addAll(divideStringWithMaxWidth(paint, line, maxWidth));
|
||||
}
|
||||
else{
|
||||
strList.add(line);
|
||||
}
|
||||
|
||||
/*
|
||||
* Should not exceed the max height;
|
||||
*/
|
||||
if (maxLines > 0 && strList.size() >= maxLines){
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Remove exceeding lines
|
||||
*/
|
||||
if (maxLines > 0 && strList.size() > maxLines){
|
||||
while (strList.size() > maxLines){
|
||||
strList.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
ret = new String[strList.size()];
|
||||
strList.toArray(ret);
|
||||
} else
|
||||
if (maxHeight != 0 && lines.length > maxLines) {
|
||||
/*
|
||||
* Remove exceeding lines
|
||||
*/
|
||||
LinkedList<String> strList = new LinkedList<String>();
|
||||
for (int i = 0; i < maxLines; i++){
|
||||
strList.add(lines[i]);
|
||||
}
|
||||
ret = new String[strList.size()];
|
||||
strList.toArray(ret);
|
||||
}
|
||||
else {
|
||||
ret = lines;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private static LinkedList<String> divideStringWithMaxWidth(Paint paint, String content,
|
||||
int width){
|
||||
int charLength = content.length();
|
||||
int start = 0;
|
||||
int tempWidth = 0;
|
||||
LinkedList<String> strList = new LinkedList<String>();
|
||||
|
||||
/*
|
||||
* Break a String into String[] by the width & should wrap the word
|
||||
*/
|
||||
for (int i = 1; i <= charLength; ++i){
|
||||
tempWidth = (int)Math.ceil(paint.measureText(content, start, i));
|
||||
if (tempWidth >= width){
|
||||
int lastIndexOfSpace = content.substring(0, i).lastIndexOf(" ");
|
||||
|
||||
if (lastIndexOfSpace != -1 && lastIndexOfSpace > start){
|
||||
/**
|
||||
* Should wrap the word
|
||||
*/
|
||||
strList.add(content.substring(start, lastIndexOfSpace));
|
||||
i = lastIndexOfSpace;
|
||||
}
|
||||
else {
|
||||
/*
|
||||
* Should not exceed the width
|
||||
*/
|
||||
if (tempWidth > width){
|
||||
strList.add(content.substring(start, i - 1));
|
||||
/*
|
||||
* compute from previous char
|
||||
*/
|
||||
--i;
|
||||
}
|
||||
else {
|
||||
strList.add(content.substring(start, i));
|
||||
}
|
||||
}
|
||||
|
||||
// remove spaces at the beginning of a new line
|
||||
while(content.indexOf(i++) == ' ') {
|
||||
}
|
||||
|
||||
start = i;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Add the last chars
|
||||
*/
|
||||
if (start < charLength){
|
||||
strList.add(content.substring(start));
|
||||
}
|
||||
|
||||
return strList;
|
||||
}
|
||||
|
||||
private static Paint newPaint(String fontName, int fontSize, int alignment){
|
||||
Paint paint = new Paint();
|
||||
paint.setColor(Color.WHITE);
|
||||
paint.setTextSize(fontSize);
|
||||
paint.setAntiAlias(true);
|
||||
|
||||
/*
|
||||
* Set type face for paint, now it support .ttf file.
|
||||
*/
|
||||
if (fontName.endsWith(".ttf")){
|
||||
try {
|
||||
//Typeface typeFace = Typeface.createFromAsset(context.getAssets(), fontName);
|
||||
Typeface typeFace = Cocos2dxTypefaces.get(context, fontName);
|
||||
paint.setTypeface(typeFace);
|
||||
} catch (Exception e){
|
||||
Log.e("Cocos2dxBitmap",
|
||||
"error to create ttf type face: " + fontName);
|
||||
|
||||
/*
|
||||
* The file may not find, use system font
|
||||
*/
|
||||
paint.setTypeface(Typeface.create(fontName, Typeface.NORMAL));
|
||||
}
|
||||
}
|
||||
else {
|
||||
paint.setTypeface(Typeface.create(fontName, Typeface.NORMAL));
|
||||
}
|
||||
|
||||
int hAlignment = alignment & 0x0F;
|
||||
switch (hAlignment){
|
||||
case HALIGNCENTER:
|
||||
paint.setTextAlign(Align.CENTER);
|
||||
break;
|
||||
|
||||
case HALIGNLEFT:
|
||||
paint.setTextAlign(Align.LEFT);
|
||||
break;
|
||||
|
||||
case HALIGNRIGHT:
|
||||
paint.setTextAlign(Align.RIGHT);
|
||||
break;
|
||||
|
||||
default:
|
||||
paint.setTextAlign(Align.LEFT);
|
||||
break;
|
||||
}
|
||||
|
||||
return paint;
|
||||
}
|
||||
|
||||
private static String refactorString(String str){
|
||||
// Avoid error when content is ""
|
||||
if (str.compareTo("") == 0){
|
||||
return " ";
|
||||
}
|
||||
|
||||
/*
|
||||
* If the font of "\n" is "" or "\n", insert " " in front of it.
|
||||
*
|
||||
* For example:
|
||||
* "\nabc" -> " \nabc"
|
||||
* "\nabc\n\n" -> " \nabc\n \n"
|
||||
*/
|
||||
StringBuilder strBuilder = new StringBuilder(str);
|
||||
int start = 0;
|
||||
int index = strBuilder.indexOf("\n");
|
||||
while (index != -1){
|
||||
if (index == 0 || strBuilder.charAt(index -1) == '\n'){
|
||||
strBuilder.insert(start, " ");
|
||||
start = index + 2;
|
||||
} else {
|
||||
start = index + 1;
|
||||
}
|
||||
|
||||
if (start > strBuilder.length() || index == strBuilder.length()){
|
||||
break;
|
||||
}
|
||||
|
||||
index = strBuilder.indexOf("\n", start);
|
||||
}
|
||||
|
||||
return strBuilder.toString();
|
||||
}
|
||||
|
||||
private static void initNativeObject(Bitmap bitmap){
|
||||
byte[] pixels = getPixels(bitmap);
|
||||
if (pixels == null){
|
||||
return;
|
||||
}
|
||||
|
||||
nativeInitBitmapDC(bitmap.getWidth(), bitmap.getHeight(), pixels);
|
||||
}
|
||||
|
||||
private static byte[] getPixels(Bitmap bitmap){
|
||||
if (bitmap != null){
|
||||
byte[] pixels = new byte[bitmap.getWidth() * bitmap.getHeight() * 4];
|
||||
ByteBuffer buf = ByteBuffer.wrap(pixels);
|
||||
buf.order(ByteOrder.nativeOrder());
|
||||
bitmap.copyPixelsToBuffer(buf);
|
||||
return pixels;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static native void nativeInitBitmapDC(int width, int height, byte[] pixels);
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2012 cocos2d-x.org
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
package org.cocos2dx.lib;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.KeyEvent;
|
||||
import android.widget.EditText;
|
||||
|
||||
public class Cocos2dxEditText extends EditText {
|
||||
|
||||
private Cocos2dxGLSurfaceView mView;
|
||||
|
||||
public Cocos2dxEditText(Context context) {
|
||||
super(context);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public Cocos2dxEditText(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
public Cocos2dxEditText(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
}
|
||||
|
||||
public void setMainView(Cocos2dxGLSurfaceView glSurfaceView) {
|
||||
mView = glSurfaceView;
|
||||
}
|
||||
|
||||
/*
|
||||
* Let GlSurfaceView get focus if back key is input
|
||||
*/
|
||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||
super.onKeyDown(keyCode, event);
|
||||
|
||||
if (keyCode == KeyEvent.KEYCODE_BACK) {
|
||||
mView.requestFocus();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -1,417 +0,0 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2010-2011 cocos2d-x.org
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
package org.cocos2dx.lib;
|
||||
|
||||
import android.content.Context;
|
||||
import android.opengl.GLSurfaceView;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.text.Editable;
|
||||
import android.text.TextWatcher;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Log;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.TextView;
|
||||
import android.widget.TextView.OnEditorActionListener;
|
||||
|
||||
class TextInputWraper implements TextWatcher, OnEditorActionListener {
|
||||
|
||||
private static final Boolean debug = false;
|
||||
private void LogD(String msg) {
|
||||
if (debug) Log.d("TextInputFilter", msg);
|
||||
}
|
||||
|
||||
private Cocos2dxGLSurfaceView mMainView;
|
||||
private String mText;
|
||||
private String mOriginText;
|
||||
|
||||
private Boolean isFullScreenEdit() {
|
||||
InputMethodManager imm = (InputMethodManager)mMainView.getTextField().getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
return imm.isFullscreenMode();
|
||||
}
|
||||
|
||||
public TextInputWraper(Cocos2dxGLSurfaceView view) {
|
||||
mMainView = view;
|
||||
}
|
||||
|
||||
public void setOriginText(String text) {
|
||||
mOriginText = text;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
if (isFullScreenEdit()) {
|
||||
return;
|
||||
}
|
||||
|
||||
LogD("afterTextChanged: " + s);
|
||||
int nModified = s.length() - mText.length();
|
||||
if (nModified > 0) {
|
||||
final String insertText = s.subSequence(mText.length(), s.length()).toString();
|
||||
mMainView.insertText(insertText);
|
||||
LogD("insertText(" + insertText + ")");
|
||||
}
|
||||
else {
|
||||
for (; nModified < 0; ++nModified) {
|
||||
mMainView.deleteBackward();
|
||||
LogD("deleteBackward");
|
||||
}
|
||||
}
|
||||
mText = s.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count,
|
||||
int after) {
|
||||
LogD("beforeTextChanged(" + s + ")start: " + start + ",count: " + count + ",after: " + after);
|
||||
mText = s.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
||||
if (mMainView.getTextField() == v && isFullScreenEdit()) {
|
||||
// user press the action button, delete all old text and insert new text
|
||||
for (int i = mOriginText.length(); i > 0; --i) {
|
||||
mMainView.deleteBackward();
|
||||
LogD("deleteBackward");
|
||||
}
|
||||
String text = v.getText().toString();
|
||||
|
||||
/*
|
||||
* If user input nothing, translate "\n" to engine.
|
||||
*/
|
||||
if (text.compareTo("") == 0){
|
||||
text = "\n";
|
||||
}
|
||||
|
||||
if ('\n' != text.charAt(text.length() - 1)) {
|
||||
text += '\n';
|
||||
}
|
||||
|
||||
final String insertText = text;
|
||||
mMainView.insertText(insertText);
|
||||
LogD("insertText(" + insertText + ")");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class Cocos2dxGLSurfaceView extends GLSurfaceView {
|
||||
|
||||
static private Cocos2dxGLSurfaceView mainView;
|
||||
|
||||
private static final String TAG = Cocos2dxGLSurfaceView.class.getCanonicalName();
|
||||
private Cocos2dxRenderer mRenderer;
|
||||
|
||||
private static final boolean debug = false;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// for initialize
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
public Cocos2dxGLSurfaceView(Context context) {
|
||||
super(context);
|
||||
initView();
|
||||
}
|
||||
|
||||
public Cocos2dxGLSurfaceView(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
initView();
|
||||
}
|
||||
|
||||
public void setCocos2dxRenderer(Cocos2dxRenderer renderer){
|
||||
mRenderer = renderer;
|
||||
setRenderer(mRenderer);
|
||||
}
|
||||
|
||||
protected void initView() {
|
||||
setFocusableInTouchMode(true);
|
||||
|
||||
textInputWraper = new TextInputWraper(this);
|
||||
|
||||
handler = new Handler(){
|
||||
public void handleMessage(Message msg){
|
||||
switch(msg.what){
|
||||
case HANDLER_OPEN_IME_KEYBOARD:
|
||||
if (null != mTextField && mTextField.requestFocus()) {
|
||||
mTextField.removeTextChangedListener(textInputWraper);
|
||||
mTextField.setText("");
|
||||
String text = (String)msg.obj;
|
||||
mTextField.append(text);
|
||||
textInputWraper.setOriginText(text);
|
||||
mTextField.addTextChangedListener(textInputWraper);
|
||||
InputMethodManager imm = (InputMethodManager)mainView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
imm.showSoftInput(mTextField, 0);
|
||||
Log.d("GLSurfaceView", "showSoftInput");
|
||||
}
|
||||
break;
|
||||
|
||||
case HANDLER_CLOSE_IME_KEYBOARD:
|
||||
if (null != mTextField) {
|
||||
mTextField.removeTextChangedListener(textInputWraper);
|
||||
InputMethodManager imm = (InputMethodManager)mainView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
imm.hideSoftInputFromWindow(mTextField.getWindowToken(), 0);
|
||||
Log.d("GLSurfaceView", "HideSoftInput");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
mainView = this;
|
||||
}
|
||||
|
||||
public void onPause(){
|
||||
queueEvent(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mRenderer.handleOnPause();
|
||||
}
|
||||
});
|
||||
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
public void onResume(){
|
||||
super.onResume();
|
||||
|
||||
queueEvent(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mRenderer.handleOnResume();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// for text input
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
private final static int HANDLER_OPEN_IME_KEYBOARD = 2;
|
||||
private final static int HANDLER_CLOSE_IME_KEYBOARD = 3;
|
||||
private static Handler handler;
|
||||
private static TextInputWraper textInputWraper;
|
||||
private Cocos2dxEditText mTextField;
|
||||
|
||||
public TextView getTextField() {
|
||||
return mTextField;
|
||||
}
|
||||
|
||||
public void setTextField(Cocos2dxEditText view) {
|
||||
mTextField = view;
|
||||
if (null != mTextField && null != textInputWraper) {
|
||||
mTextField.setOnEditorActionListener(textInputWraper);
|
||||
mTextField.setMainView(this);
|
||||
this.requestFocus();
|
||||
}
|
||||
}
|
||||
|
||||
public static void openIMEKeyboard() {
|
||||
Message msg = new Message();
|
||||
msg.what = HANDLER_OPEN_IME_KEYBOARD;
|
||||
msg.obj = mainView.getContentText();
|
||||
handler.sendMessage(msg);
|
||||
|
||||
}
|
||||
|
||||
private String getContentText() {
|
||||
return mRenderer.getContentText();
|
||||
}
|
||||
|
||||
public static void closeIMEKeyboard() {
|
||||
Message msg = new Message();
|
||||
msg.what = HANDLER_CLOSE_IME_KEYBOARD;
|
||||
handler.sendMessage(msg);
|
||||
}
|
||||
|
||||
public void insertText(final String text) {
|
||||
queueEvent(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mRenderer.handleInsertText(text);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void deleteBackward() {
|
||||
queueEvent(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mRenderer.handleDeleteBackward();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// for touch event
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public boolean onTouchEvent(final MotionEvent event) {
|
||||
// these data are used in ACTION_MOVE and ACTION_CANCEL
|
||||
final int pointerNumber = event.getPointerCount();
|
||||
final int[] ids = new int[pointerNumber];
|
||||
final float[] xs = new float[pointerNumber];
|
||||
final float[] ys = new float[pointerNumber];
|
||||
|
||||
for (int i = 0; i < pointerNumber; i++) {
|
||||
ids[i] = event.getPointerId(i);
|
||||
xs[i] = event.getX(i);
|
||||
ys[i] = event.getY(i);
|
||||
}
|
||||
|
||||
switch (event.getAction() & MotionEvent.ACTION_MASK) {
|
||||
case MotionEvent.ACTION_POINTER_DOWN:
|
||||
final int indexPointerDown = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT;
|
||||
final int idPointerDown = event.getPointerId(indexPointerDown);
|
||||
final float xPointerDown = event.getX(indexPointerDown);
|
||||
final float yPointerDown = event.getY(indexPointerDown);
|
||||
|
||||
queueEvent(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mRenderer.handleActionDown(idPointerDown, xPointerDown, yPointerDown);
|
||||
}
|
||||
});
|
||||
break;
|
||||
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
// there are only one finger on the screen
|
||||
final int idDown = event.getPointerId(0);
|
||||
final float xDown = xs[0];
|
||||
final float yDown = ys[0];
|
||||
|
||||
queueEvent(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mRenderer.handleActionDown(idDown, xDown, yDown);
|
||||
}
|
||||
});
|
||||
break;
|
||||
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
queueEvent(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mRenderer.handleActionMove(ids, xs, ys);
|
||||
}
|
||||
});
|
||||
break;
|
||||
|
||||
case MotionEvent.ACTION_POINTER_UP:
|
||||
final int indexPointUp = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT;
|
||||
final int idPointerUp = event.getPointerId(indexPointUp);
|
||||
final float xPointerUp = event.getX(indexPointUp);
|
||||
final float yPointerUp = event.getY(indexPointUp);
|
||||
|
||||
queueEvent(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mRenderer.handleActionUp(idPointerUp, xPointerUp, yPointerUp);
|
||||
}
|
||||
});
|
||||
break;
|
||||
|
||||
case MotionEvent.ACTION_UP:
|
||||
// there are only one finger on the screen
|
||||
final int idUp = event.getPointerId(0);
|
||||
final float xUp = xs[0];
|
||||
final float yUp = ys[0];
|
||||
|
||||
queueEvent(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mRenderer.handleActionUp(idUp, xUp, yUp);
|
||||
}
|
||||
});
|
||||
break;
|
||||
|
||||
case MotionEvent.ACTION_CANCEL:
|
||||
queueEvent(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mRenderer.handleActionCancel(ids, xs, ys);
|
||||
}
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
if (debug){
|
||||
dumpEvent(event);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* This function is called before Cocos2dxRenderer.nativeInit(), so the width and height is correct.
|
||||
*/
|
||||
protected void onSizeChanged(int w, int h, int oldw, int oldh){
|
||||
this.mRenderer.setScreenWidthAndHeight(w, h);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||
final int kc = keyCode;
|
||||
if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU) {
|
||||
queueEvent(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mRenderer.handleKeyDown(kc);
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
return super.onKeyDown(keyCode, event);
|
||||
}
|
||||
|
||||
// Show an event in the LogCat view, for debugging
|
||||
private void dumpEvent(MotionEvent event) {
|
||||
String names[] = { "DOWN" , "UP" , "MOVE" , "CANCEL" , "OUTSIDE" ,
|
||||
"POINTER_DOWN" , "POINTER_UP" , "7?" , "8?" , "9?" };
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int action = event.getAction();
|
||||
int actionCode = action & MotionEvent.ACTION_MASK;
|
||||
sb.append("event ACTION_" ).append(names[actionCode]);
|
||||
if (actionCode == MotionEvent.ACTION_POINTER_DOWN
|
||||
|| actionCode == MotionEvent.ACTION_POINTER_UP) {
|
||||
sb.append("(pid " ).append(
|
||||
action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
|
||||
sb.append(")" );
|
||||
}
|
||||
sb.append("[" );
|
||||
for (int i = 0; i < event.getPointerCount(); i++) {
|
||||
sb.append("#" ).append(i);
|
||||
sb.append("(pid " ).append(event.getPointerId(i));
|
||||
sb.append(")=" ).append((int) event.getX(i));
|
||||
sb.append("," ).append((int) event.getY(i));
|
||||
if (i + 1 < event.getPointerCount())
|
||||
sb.append(";" );
|
||||
}
|
||||
sb.append("]" );
|
||||
Log.d(TAG, sb.toString());
|
||||
}
|
||||
}
|
|
@ -1,228 +0,0 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2010-2011 cocos2d-x.org
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
package org.cocos2dx.lib;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.AssetFileDescriptor;
|
||||
import android.media.MediaPlayer;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
*
|
||||
* This class is used for controlling background music
|
||||
*
|
||||
*/
|
||||
public class Cocos2dxMusic {
|
||||
|
||||
private static final String TAG = "Cocos2dxMusic";
|
||||
private float mLeftVolume;
|
||||
private float mRightVolume;
|
||||
private Context mContext;
|
||||
private MediaPlayer mBackgroundMediaPlayer;
|
||||
private boolean mIsPaused;
|
||||
private String mCurrentPath;
|
||||
|
||||
public Cocos2dxMusic(Context context){
|
||||
this.mContext = context;
|
||||
initData();
|
||||
}
|
||||
|
||||
public void preloadBackgroundMusic(String path){
|
||||
if ((mCurrentPath == null) || (! mCurrentPath.equals(path))){
|
||||
// preload new background music
|
||||
|
||||
// release old resource and create a new one
|
||||
if (mBackgroundMediaPlayer != null){
|
||||
mBackgroundMediaPlayer.release();
|
||||
}
|
||||
|
||||
mBackgroundMediaPlayer = createMediaplayerFromAssets(path);
|
||||
|
||||
// record the path
|
||||
mCurrentPath = path;
|
||||
}
|
||||
}
|
||||
|
||||
public void playBackgroundMusic(String path, boolean isLoop){
|
||||
if (mCurrentPath == null){
|
||||
// it is the first time to play background music
|
||||
// or end() was called
|
||||
mBackgroundMediaPlayer = createMediaplayerFromAssets(path);
|
||||
mCurrentPath = path;
|
||||
}
|
||||
else {
|
||||
if (! mCurrentPath.equals(path)){
|
||||
// play new background music
|
||||
|
||||
// release old resource and create a new one
|
||||
if (mBackgroundMediaPlayer != null){
|
||||
mBackgroundMediaPlayer.release();
|
||||
}
|
||||
mBackgroundMediaPlayer = createMediaplayerFromAssets(path);
|
||||
|
||||
// record the path
|
||||
mCurrentPath = path;
|
||||
}
|
||||
}
|
||||
|
||||
if (mBackgroundMediaPlayer == null){
|
||||
Log.e(TAG, "playBackgroundMusic: background media player is null");
|
||||
} else {
|
||||
// if the music is playing or paused, stop it
|
||||
mBackgroundMediaPlayer.stop();
|
||||
|
||||
mBackgroundMediaPlayer.setLooping(isLoop);
|
||||
|
||||
try {
|
||||
mBackgroundMediaPlayer.prepare();
|
||||
mBackgroundMediaPlayer.seekTo(0);
|
||||
mBackgroundMediaPlayer.start();
|
||||
|
||||
this.mIsPaused = false;
|
||||
} catch (Exception e){
|
||||
Log.e(TAG, "playBackgroundMusic: error state");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void stopBackgroundMusic(){
|
||||
if (mBackgroundMediaPlayer != null){
|
||||
mBackgroundMediaPlayer.stop();
|
||||
|
||||
// should set the state, if not , the following sequence will be error
|
||||
// play -> pause -> stop -> resume
|
||||
this.mIsPaused = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void pauseBackgroundMusic(){
|
||||
if (mBackgroundMediaPlayer != null && mBackgroundMediaPlayer.isPlaying()){
|
||||
mBackgroundMediaPlayer.pause();
|
||||
this.mIsPaused = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void resumeBackgroundMusic(){
|
||||
if (mBackgroundMediaPlayer != null && this.mIsPaused){
|
||||
mBackgroundMediaPlayer.start();
|
||||
this.mIsPaused = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void rewindBackgroundMusic(){
|
||||
if (mBackgroundMediaPlayer != null){
|
||||
mBackgroundMediaPlayer.stop();
|
||||
|
||||
try {
|
||||
mBackgroundMediaPlayer.prepare();
|
||||
mBackgroundMediaPlayer.seekTo(0);
|
||||
mBackgroundMediaPlayer.start();
|
||||
|
||||
this.mIsPaused = false;
|
||||
} catch (Exception e){
|
||||
Log.e(TAG, "rewindBackgroundMusic: error state");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isBackgroundMusicPlaying(){
|
||||
boolean ret = false;
|
||||
|
||||
if (mBackgroundMediaPlayer == null){
|
||||
ret = false;
|
||||
} else {
|
||||
ret = mBackgroundMediaPlayer.isPlaying();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void end(){
|
||||
if (mBackgroundMediaPlayer != null){
|
||||
mBackgroundMediaPlayer.release();
|
||||
}
|
||||
|
||||
initData();
|
||||
}
|
||||
|
||||
public float getBackgroundVolume(){
|
||||
if (this.mBackgroundMediaPlayer != null){
|
||||
return (this.mLeftVolume + this.mRightVolume) / 2;
|
||||
} else {
|
||||
return 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
public void setBackgroundVolume(float volume){
|
||||
if (volume < 0.0f){
|
||||
volume = 0.0f;
|
||||
}
|
||||
|
||||
if (volume > 1.0f){
|
||||
volume = 1.0f;
|
||||
}
|
||||
|
||||
this.mLeftVolume = this.mRightVolume = volume;
|
||||
if (this.mBackgroundMediaPlayer != null){
|
||||
this.mBackgroundMediaPlayer.setVolume(this.mLeftVolume, this.mRightVolume);
|
||||
}
|
||||
}
|
||||
|
||||
private void initData(){
|
||||
mLeftVolume =0.5f;
|
||||
mRightVolume = 0.5f;
|
||||
mBackgroundMediaPlayer = null;
|
||||
mIsPaused = false;
|
||||
mCurrentPath = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* create mediaplayer for music
|
||||
* @param path the path relative to assets
|
||||
* @return
|
||||
*/
|
||||
private MediaPlayer createMediaplayerFromAssets(String path){
|
||||
MediaPlayer mediaPlayer = new MediaPlayer();
|
||||
|
||||
try{
|
||||
if (path.startsWith("/")) {
|
||||
mediaPlayer.setDataSource(path);
|
||||
}
|
||||
else {
|
||||
AssetFileDescriptor assetFileDescritor = mContext.getAssets().openFd(path);
|
||||
mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(),
|
||||
assetFileDescritor.getStartOffset(), assetFileDescritor.getLength());
|
||||
}
|
||||
|
||||
mediaPlayer.prepare();
|
||||
|
||||
mediaPlayer.setVolume(mLeftVolume, mRightVolume);
|
||||
}catch (Exception e) {
|
||||
mediaPlayer = null;
|
||||
Log.e(TAG, "error: " + e.getMessage(), e);
|
||||
}
|
||||
|
||||
return mediaPlayer;
|
||||
}
|
||||
}
|
|
@ -1,137 +0,0 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2010-2011 cocos2d-x.org
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
package org.cocos2dx.lib;
|
||||
|
||||
import javax.microedition.khronos.egl.EGLConfig;
|
||||
import javax.microedition.khronos.opengles.GL10;
|
||||
|
||||
import android.opengl.GLSurfaceView;
|
||||
|
||||
public class Cocos2dxRenderer implements GLSurfaceView.Renderer {
|
||||
private final static long NANOSECONDSPERSECOND = 1000000000L;
|
||||
private final static long NANOSECONDSPERMINISECOND = 1000000;
|
||||
private static long animationInterval = (long)(1.0 / 60 * NANOSECONDSPERSECOND);
|
||||
private long last;
|
||||
private int screenWidth;
|
||||
private int screenHeight;
|
||||
|
||||
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
|
||||
nativeInit(screenWidth, screenHeight);
|
||||
last = System.nanoTime();
|
||||
}
|
||||
|
||||
public void setScreenWidthAndHeight(int w, int h){
|
||||
this.screenWidth = w;
|
||||
this.screenHeight = h;
|
||||
}
|
||||
|
||||
public void onSurfaceChanged(GL10 gl, int w, int h) {
|
||||
}
|
||||
|
||||
public void onDrawFrame(GL10 gl) {
|
||||
|
||||
long now = System.nanoTime();
|
||||
long interval = now - last;
|
||||
|
||||
// should render a frame when onDrawFrame() is called
|
||||
// or there is a "ghost"
|
||||
nativeRender();
|
||||
|
||||
// fps controlling
|
||||
if (interval < animationInterval){
|
||||
try {
|
||||
// because we render it before, so we should sleep twice time interval
|
||||
Thread.sleep((animationInterval - interval) * 2 / NANOSECONDSPERMINISECOND);
|
||||
} catch (Exception e){}
|
||||
}
|
||||
|
||||
last = now;
|
||||
}
|
||||
|
||||
public void handleActionDown(int id, float x, float y)
|
||||
{
|
||||
nativeTouchesBegin(id, x, y);
|
||||
}
|
||||
|
||||
public void handleActionUp(int id, float x, float y)
|
||||
{
|
||||
nativeTouchesEnd(id, x, y);
|
||||
}
|
||||
|
||||
public void handleActionCancel(int[] id, float[] x, float[] y)
|
||||
{
|
||||
nativeTouchesCancel(id, x, y);
|
||||
}
|
||||
|
||||
public void handleActionMove(int[] id, float[] x, float[] y)
|
||||
{
|
||||
nativeTouchesMove(id, x, y);
|
||||
}
|
||||
|
||||
public void handleKeyDown(int keyCode)
|
||||
{
|
||||
nativeKeyDown(keyCode);
|
||||
}
|
||||
|
||||
public void handleOnPause(){
|
||||
nativeOnPause();
|
||||
}
|
||||
|
||||
public void handleOnResume(){
|
||||
nativeOnResume();
|
||||
}
|
||||
|
||||
public static void setAnimationInterval(double interval){
|
||||
animationInterval = (long)(interval * NANOSECONDSPERSECOND);
|
||||
}
|
||||
private static native void nativeTouchesBegin(int id, float x, float y);
|
||||
private static native void nativeTouchesEnd(int id, float x, float y);
|
||||
private static native void nativeTouchesMove(int[] id, float[] x, float[] y);
|
||||
private static native void nativeTouchesCancel(int[] id, float[] x, float[] y);
|
||||
private static native boolean nativeKeyDown(int keyCode);
|
||||
private static native void nativeRender();
|
||||
private static native void nativeInit(int w, int h);
|
||||
private static native void nativeOnPause();
|
||||
private static native void nativeOnResume();
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
// handle input method edit message
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public void handleInsertText(final String text) {
|
||||
nativeInsertText(text);
|
||||
}
|
||||
|
||||
public void handleDeleteBackward() {
|
||||
nativeDeleteBackward();
|
||||
}
|
||||
|
||||
public String getContentText() {
|
||||
return nativeGetContentText();
|
||||
}
|
||||
|
||||
private static native void nativeInsertText(String text);
|
||||
private static native void nativeDeleteBackward();
|
||||
private static native String nativeGetContentText();
|
||||
}
|
|
@ -1,245 +0,0 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2010-2011 cocos2d-x.org
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
package org.cocos2dx.lib;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import android.content.Context;
|
||||
import android.media.AudioManager;
|
||||
import android.media.SoundPool;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
*
|
||||
* This class is used for controlling effect
|
||||
*
|
||||
*/
|
||||
|
||||
public class Cocos2dxSound {
|
||||
private Context mContext;
|
||||
private SoundPool mSoundPool;
|
||||
private float mLeftVolume;
|
||||
private float mRightVolume;
|
||||
|
||||
// sound path and stream ids map
|
||||
// a file may be played many times at the same time
|
||||
// so there is an array map to a file path
|
||||
private HashMap<String,ArrayList<Integer>> mPathStreamIDsMap;
|
||||
|
||||
private HashMap<String, Integer> mPathSoundIdMap;
|
||||
|
||||
private static final String TAG = "Cocos2dxSound";
|
||||
private static final int MAX_SIMULTANEOUS_STREAMS_DEFAULT = 5;
|
||||
private static final float SOUND_RATE = 1.0f;
|
||||
private static final int SOUND_PRIORITY = 1;
|
||||
private static final int SOUND_QUALITY = 5;
|
||||
|
||||
private final static int INVALID_SOUND_ID = -1;
|
||||
private final static int INVALID_STREAM_ID = -1;
|
||||
|
||||
public Cocos2dxSound(Context context){
|
||||
this.mContext = context;
|
||||
initData();
|
||||
}
|
||||
|
||||
public int preloadEffect(String path){
|
||||
Integer soundID = this.mPathSoundIdMap.get(path);
|
||||
|
||||
if (soundID == null) {
|
||||
soundID = createSoundIdFromAsset(path);
|
||||
this.mPathSoundIdMap.put(path, soundID);
|
||||
}
|
||||
|
||||
return soundID;
|
||||
}
|
||||
|
||||
public void unloadEffect(String path){
|
||||
// stop effects
|
||||
ArrayList<Integer> streamIDs = this.mPathStreamIDsMap.get(path);
|
||||
if (streamIDs != null) {
|
||||
for (Integer streamID : streamIDs) {
|
||||
this.mSoundPool.stop(streamID);
|
||||
}
|
||||
}
|
||||
this.mPathStreamIDsMap.remove(path);
|
||||
|
||||
// unload effect
|
||||
Integer soundID = this.mPathSoundIdMap.get(path);
|
||||
this.mSoundPool.unload(soundID);
|
||||
this.mPathSoundIdMap.remove(path);
|
||||
}
|
||||
|
||||
public int playEffect(String path, boolean isLoop){
|
||||
Integer soundId = this.mPathSoundIdMap.get(path);
|
||||
int streamId = INVALID_STREAM_ID;
|
||||
|
||||
if (soundId != null){
|
||||
// play sound
|
||||
streamId = this.mSoundPool.play(soundId.intValue(), this.mLeftVolume,
|
||||
this.mRightVolume, SOUND_PRIORITY, isLoop ? -1 : 0, SOUND_RATE);
|
||||
|
||||
// record stream id
|
||||
ArrayList<Integer> streamIds = this.mPathStreamIDsMap.get(path);
|
||||
if (streamIds == null) {
|
||||
streamIds = new ArrayList<Integer>();
|
||||
this.mPathStreamIDsMap.put(path, streamIds);
|
||||
}
|
||||
streamIds.add(streamId);
|
||||
} else {
|
||||
// the effect is not prepared
|
||||
soundId = preloadEffect(path);
|
||||
if (soundId == INVALID_SOUND_ID){
|
||||
// can not preload effect
|
||||
return INVALID_SOUND_ID;
|
||||
}
|
||||
|
||||
/*
|
||||
* Someone reports that, it can not play effect for the
|
||||
* first time. If you are lucky to meet it. There are two
|
||||
* ways to resolve it.
|
||||
* 1. Add some delay here. I don't know how long it is, so
|
||||
* I don't add it here.
|
||||
* 2. If you use 2.2(API level 8), you can call
|
||||
* SoundPool.setOnLoadCompleteListener() to play the effect.
|
||||
* Because the method is supported from 2.2, so I can't use
|
||||
* it here.
|
||||
*/
|
||||
playEffect(path, isLoop);
|
||||
}
|
||||
|
||||
return streamId;
|
||||
}
|
||||
|
||||
public void stopEffect(int streamID){
|
||||
this.mSoundPool.stop(streamID);
|
||||
|
||||
// remove record
|
||||
for (String path : this.mPathStreamIDsMap.keySet()) {
|
||||
if (this.mPathStreamIDsMap.get(path).contains(streamID)) {
|
||||
this.mPathStreamIDsMap.get(path).remove(this.mPathStreamIDsMap.get(path).indexOf(streamID));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void pauseEffect(int streamID){
|
||||
this.mSoundPool.pause(streamID);
|
||||
}
|
||||
|
||||
public void resumeEffect(int streamID){
|
||||
this.mSoundPool.resume(streamID);
|
||||
}
|
||||
|
||||
public void pauseAllEffects(){
|
||||
this.mSoundPool.autoPause();
|
||||
}
|
||||
|
||||
public void resumeAllEffects(){
|
||||
// autoPause() is available since level 8
|
||||
this.mSoundPool.autoResume();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void stopAllEffects(){
|
||||
// stop effects
|
||||
if (! this.mPathStreamIDsMap.isEmpty()) {
|
||||
Iterator<?> iter = this.mPathStreamIDsMap.entrySet().iterator();
|
||||
while (iter.hasNext()){
|
||||
Map.Entry<String, ArrayList<Integer>> entry = (Map.Entry<String, ArrayList<Integer>>)iter.next();
|
||||
for (int streamID : entry.getValue()) {
|
||||
this.mSoundPool.stop(streamID);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// remove records
|
||||
this.mPathStreamIDsMap.clear();
|
||||
}
|
||||
|
||||
public float getEffectsVolume(){
|
||||
return (this.mLeftVolume + this.mRightVolume) / 2;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setEffectsVolume(float volume){
|
||||
// volume should be in [0, 1.0]
|
||||
if (volume < 0){
|
||||
volume = 0;
|
||||
}
|
||||
if (volume > 1){
|
||||
volume = 1;
|
||||
}
|
||||
|
||||
this.mLeftVolume = this.mRightVolume = volume;
|
||||
|
||||
// change the volume of playing sounds
|
||||
if (! this.mPathStreamIDsMap.isEmpty()) {
|
||||
Iterator<?> iter = this.mPathStreamIDsMap.entrySet().iterator();
|
||||
while (iter.hasNext()){
|
||||
Map.Entry<String, ArrayList<Integer>> entry = (Map.Entry<String, ArrayList<Integer>>)iter.next();
|
||||
for (int streamID : entry.getValue()) {
|
||||
this.mSoundPool.setVolume(streamID, mLeftVolume, mRightVolume);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void end(){
|
||||
this.mSoundPool.release();
|
||||
this.mPathStreamIDsMap.clear();
|
||||
this.mPathSoundIdMap.clear();
|
||||
|
||||
initData();
|
||||
}
|
||||
|
||||
public int createSoundIdFromAsset(String path){
|
||||
int soundId = INVALID_SOUND_ID;
|
||||
|
||||
try {
|
||||
if (path.startsWith("/")){
|
||||
soundId = mSoundPool.load(path, 0);
|
||||
}
|
||||
else {
|
||||
soundId = mSoundPool.load(mContext.getAssets().openFd(path), 0);
|
||||
}
|
||||
} catch(Exception e){
|
||||
soundId = INVALID_SOUND_ID;
|
||||
Log.e(TAG, "error: " + e.getMessage(), e);
|
||||
}
|
||||
|
||||
return soundId;
|
||||
}
|
||||
|
||||
private void initData(){
|
||||
this.mPathStreamIDsMap = new HashMap<String,ArrayList<Integer>>();
|
||||
this.mPathSoundIdMap = new HashMap<String, Integer>();
|
||||
mSoundPool = new SoundPool(MAX_SIMULTANEOUS_STREAMS_DEFAULT, AudioManager.STREAM_MUSIC, SOUND_QUALITY);
|
||||
|
||||
this.mLeftVolume = 0.5f;
|
||||
this.mRightVolume = 0.5f;
|
||||
}
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
/****************************************************************************
|
||||
Copyright (c) 2010-2011 cocos2d-x.org
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
package org.cocos2dx.lib;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Typeface;
|
||||
|
||||
public class Cocos2dxTypefaces {
|
||||
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
|
||||
|
||||
public static Typeface get(Context context, String name){
|
||||
synchronized(cache){
|
||||
if (! cache.containsKey(name)){
|
||||
Typeface t = Typeface.createFromAsset(context.getAssets(), name);
|
||||
cache.put(name, t);
|
||||
}
|
||||
|
||||
return cache.get(name);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -45,10 +45,10 @@ public class TestsDemo extends Cocos2dxActivity{
|
|||
String packageName = getApplication().getPackageName();
|
||||
super.setPackageName(packageName);
|
||||
|
||||
setContentView(R.layout.test_demo);
|
||||
setContentView(R.layout.cocos2dx_default_screen_layout);
|
||||
|
||||
mGLView = (Cocos2dxGLSurfaceView) findViewById(R.id.test_demo_gl_surfaceview);
|
||||
mGLView.setTextField((Cocos2dxEditText)findViewById(R.id.textField));
|
||||
mGLView = (Cocos2dxGLSurfaceView) findViewById(R.id.cocos2dx_default_gl_surfaceview);
|
||||
mGLView.setTextField((Cocos2dxEditText)findViewById(R.id.cocos2dx_default_textField));
|
||||
mGLView.setEGLContextClientVersion(2);
|
||||
mGLView.setCocos2dxRenderer(new Cocos2dxRenderer());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue