use java coding style

This commit is contained in:
minggo 2014-12-10 18:07:46 +08:00
parent c067338c17
commit 77ed7a47e2
13 changed files with 201 additions and 191 deletions

View File

@ -55,8 +55,8 @@ public class Cocos2dxAccelerometer implements SensorEventListener {
// Constructors
// ===========================================================
public Cocos2dxAccelerometer(final Context pContext) {
this.mContext = pContext;
public Cocos2dxAccelerometer(final Context context) {
this.mContext = context;
this.mSensorManager = (SensorManager) this.mContext.getSystemService(Context.SENSOR_SERVICE);
this.mAccelerometer = this.mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
@ -92,14 +92,14 @@ public class Cocos2dxAccelerometer implements SensorEventListener {
// ===========================================================
@Override
public void onSensorChanged(final SensorEvent pSensorEvent) {
if (pSensorEvent.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
public void onSensorChanged(final SensorEvent sensorEvent) {
if (sensorEvent.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
return;
}
float x = pSensorEvent.values[0];
float y = pSensorEvent.values[1];
final float z = pSensorEvent.values[2];
float x = sensorEvent.values[0];
float y = sensorEvent.values[1];
final float z = sensorEvent.values[2];
/*
* Because the axes are not swapped when the device's screen orientation
@ -118,17 +118,17 @@ public class Cocos2dxAccelerometer implements SensorEventListener {
y = -tmp;
}
Cocos2dxGLSurfaceView.queueAccelerometer(x,y,z,pSensorEvent.timestamp);
Cocos2dxGLSurfaceView.queueAccelerometer(x,y,z,sensorEvent.timestamp);
/*
if(BuildConfig.DEBUG) {
Log.d(TAG, "x = " + pSensorEvent.values[0] + " y = " + pSensorEvent.values[1] + " z = " + pSensorEvent.values[2]);
Log.d(TAG, "x = " + sensorEvent.values[0] + " y = " + sensorEvent.values[1] + " z = " + pSensorEvent.values[2]);
}
*/
}
@Override
public void onAccuracyChanged(final Sensor pSensor, final int pAccuracy) {
public void onAccuracyChanged(final Sensor sensor, final int accuracy) {
}
// ===========================================================
@ -136,7 +136,7 @@ public class Cocos2dxAccelerometer implements SensorEventListener {
// Native method called from Cocos2dxGLSurfaceView (To be in the same thread)
// ===========================================================
public static native void onSensorChanged(final float pX, final float pY, final float pZ, final long pTimestamp);
public static native void onSensorChanged(final float x, final float y, final float z, final long timestamp);
// ===========================================================
// Inner and Anonymous Classes

View File

@ -56,9 +56,9 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe
// Fields
// ===========================================================
private Cocos2dxGLSurfaceView mGLSurfaceView;
private int[] glContextAttrs;
private Cocos2dxHandler mHandler;
private Cocos2dxGLSurfaceView mGLSurfaceView = null;
private int[] mGLContextAttrs = null;
private Cocos2dxHandler mHandler = null;
private static Cocos2dxActivity sContext = null;
private Cocos2dxVideoHelper mVideoHelper = null;
private Cocos2dxWebViewHelper mWebViewHelper = null;
@ -103,7 +103,7 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe
Cocos2dxHelper.init(this);
this.glContextAttrs = getGLContextAttrs();
this.mGLContextAttrs = getGLContextAttrs();
this.init();
if (mVideoHelper == null) {
@ -222,7 +222,7 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe
public Cocos2dxGLSurfaceView onCreateView() {
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
//this line is need on some device if we specify an alpha bits
if(this.glContextAttrs[3] > 0) glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
if(this.mGLContextAttrs[3] > 0) glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
class cocos2dEGLConfigChooser implements GLSurfaceView.EGLConfigChooser
{
@ -344,7 +344,7 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe
}
}
cocos2dEGLConfigChooser chooser = new cocos2dEGLConfigChooser(this.glContextAttrs);
cocos2dEGLConfigChooser chooser = new cocos2dEGLConfigChooser(this.mGLContextAttrs);
glSurfaceView.setEGLConfigChooser(chooser);
return glSurfaceView;

View File

@ -58,7 +58,7 @@ public class Cocos2dxBitmap {
// Fields
// ===========================================================
private static Context _context;
private static Context sContext;
// ===========================================================
// Constructors
@ -69,7 +69,7 @@ public class Cocos2dxBitmap {
// ===========================================================
public static void setContext(final Context context) {
Cocos2dxBitmap._context = context;
Cocos2dxBitmap.sContext = context;
}
// ===========================================================
@ -84,20 +84,19 @@ public class Cocos2dxBitmap {
final int height, final byte[] pixels);
/**
* @param pWidth
* @param width
* the width to draw, it can be 0
* @param pHeight
* @param height
* the height to draw, it can be 0
*/
public static void createTextBitmap(String string, final String fontName,
final int fontSize, final int alignment, final int width,
final int height) {
//
createTextBitmapShadowStroke( string, fontName, fontSize, 1.0f, 1.0f, 1.0f, // text font and color
alignment, width, height, // alignment and size
false, 0.0f, 0.0f, 0.0f, 0.0f, // no shadow
false, 1.0f, 1.0f, 1.0f, 1.0f); // no stroke
false, 0.0f, 0.0f, 0.0f, 0.0f, // no shadow
false, 1.0f, 1.0f, 1.0f, 1.0f); // no stroke
}
@ -113,13 +112,12 @@ public class Cocos2dxBitmap {
string = Cocos2dxBitmap.refactorString(string);
final Paint paint = Cocos2dxBitmap.newPaint(fontName, fontSize, horizontalAlignment);
/**
* if the first word width less than designed width,It means no words to show
*/
// if the first word width less than designed width, it means no words to show
if(0 != width)
{
final int firstWordWidth = (int) Math.ceil(paint.measureText(string, 0,1));
if ( firstWordWidth > width)
if (firstWordWidth > width)
{
Log.w("createTextBitmapShadowStroke warning:","the input width is less than the width of the pString's first word\n");
return false;
@ -151,7 +149,7 @@ public class Cocos2dxBitmap {
final Canvas canvas = new Canvas(bitmap);
/* Draw string. */
// Draw string.
final FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();
// draw again with stroke on if needed
@ -204,17 +202,17 @@ public class Cocos2dxBitmap {
paint.setTextSize(fontSize);
paint.setAntiAlias(true);
/* Set type face for paint, now it support .ttf file. */
// Set type face for paint, now it support .ttf file.
if (fontName.endsWith(".ttf")) {
try {
final Typeface typeFace = Cocos2dxTypefaces.get(
Cocos2dxBitmap._context, fontName);
Cocos2dxBitmap.sContext, fontName);
paint.setTypeface(typeFace);
} catch (final Exception e) {
Log.e("Cocos2dxBitmap", "error to create ttf type face: "
+ fontName);
/* The file may not find, use system font. */
// The file may not find, use system font.
paint.setTypeface(Typeface.create(fontName, Typeface.NORMAL));
}
} else {
@ -249,7 +247,7 @@ public class Cocos2dxBitmap {
if (width != 0) {
maxContentWidth = width;
} else {
/* Compute the max width. */
// Compute the max width.
int temp = 0;
for (final String line : lines) {
temp = (int) Math.ceil(paint.measureText(line, 0,
@ -335,13 +333,13 @@ public class Cocos2dxBitmap {
strList.add(line);
}
/* Should not exceed the max height. */
// Should not exceed the max height.
if (maxLines > 0 && strList.size() >= maxLines) {
break;
}
}
/* Remove exceeding lines. */
// Remove exceeding lines.
if (maxLines > 0 && strList.size() > maxLines) {
while (strList.size() > maxLines) {
strList.removeLast();
@ -366,13 +364,13 @@ public class Cocos2dxBitmap {
}
private static LinkedList<String> divideStringWithMaxWidth(
final String string, final int maxWidth, final Paint paint) {
final String string, final int maxWidth, final Paint paint) {
final int charLength = string.length();
int start = 0;
int tempWidth = 0;
final LinkedList<String> strList = new LinkedList<String>();
/* Break a String into String[] by the width & should wrap the word. */
// 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(string, start,
i));
@ -381,21 +379,21 @@ public class Cocos2dxBitmap {
.lastIndexOf(" ");
if (lastIndexOfSpace != -1 && lastIndexOfSpace > start) {
/* Should wrap the word. */
// Should wrap the word.
strList.add(string.substring(start, lastIndexOfSpace));
i = lastIndexOfSpace + 1; // skip space
} else {
/* Should not exceed the width. */
// Should not exceed the width.
if (tempWidth > maxWidth) {
strList.add(string.substring(start, i - 1));
/* Compute from previous char. */
// Compute from previous char.
--i;
} else {
strList.add(string.substring(start, i));
}
}
/* Remove spaces at the beginning of a new line. */
// Remove spaces at the beginning of a new line.
while (i < charLength && string.charAt(i) == ' ') {
++i;
}
@ -404,7 +402,7 @@ public class Cocos2dxBitmap {
}
}
/* Add the last chars. */
// Add the last chars.
if (start < charLength) {
strList.add(string.substring(start));
}
@ -413,7 +411,7 @@ public class Cocos2dxBitmap {
}
private static String refactorString(final String string) {
/* Avoid error when content is "". */
// Avoid error when content is "".
if (string.compareTo("") == 0) {
return " ";
}

View File

@ -275,7 +275,7 @@ public class Cocos2dxEditBoxDialog extends Dialog {
this.mInputEditText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
/* If user didn't set keyboard type, this callback will be invoked twice with 'KeyEvent.ACTION_DOWN' and 'KeyEvent.ACTION_UP'. */
// If user didn't set keyboard type, this callback will be invoked twice with 'KeyEvent.ACTION_DOWN' and 'KeyEvent.ACTION_UP'.
if (actionId != EditorInfo.IME_NULL || (actionId == EditorInfo.IME_NULL && event != null && event.getAction() == KeyEvent.ACTION_DOWN)) {
Cocos2dxHelper.setEditTextDialogResult(Cocos2dxEditBoxDialog.this.mInputEditText.getText().toString());
Cocos2dxEditBoxDialog.this.closeKeyboard();

View File

@ -72,7 +72,7 @@ public class Cocos2dxEditText extends EditText {
public boolean onKeyDown(final int pKeyCode, final KeyEvent pKeyEvent) {
super.onKeyDown(pKeyCode, pKeyEvent);
/* Let GlSurfaceView get focus if back key is input. */
// Let GlSurfaceView get focus if back key is input.
if (pKeyCode == KeyEvent.KEYCODE_BACK) {
this.mCocos2dxGLSurfaceView.requestFocus();
}

View File

@ -47,17 +47,17 @@ public class Cocos2dxMusic {
private MediaPlayer mBackgroundMediaPlayer;
private float mLeftVolume;
private float mRightVolume;
private boolean mPaused;// whether music is paused state.
private boolean mPaused; // whether music is paused state.
private boolean mIsLoop = false;
private boolean mManualPaused = false;// whether music is paused manually before the program is switched to the background.
private boolean mManualPaused = false; // whether music is paused manually before the program is switched to the background.
private String mCurrentPath;
// ===========================================================
// Constructors
// ===========================================================
public Cocos2dxMusic(final Context pContext) {
this.mContext = pContext;
public Cocos2dxMusic(final Context context) {
this.mContext = context;
this.initData();
}
@ -74,8 +74,8 @@ public class Cocos2dxMusic {
// Methods
// ===========================================================
public void preloadBackgroundMusic(final String pPath) {
if ((this.mCurrentPath == null) || (!this.mCurrentPath.equals(pPath))) {
public void preloadBackgroundMusic(final String path) {
if ((this.mCurrentPath == null) || (!this.mCurrentPath.equals(path))) {
// preload new background music
// release old resource and create a new one
@ -83,10 +83,10 @@ public class Cocos2dxMusic {
this.mBackgroundMediaPlayer.release();
}
this.mBackgroundMediaPlayer = this.createMediaplayer(pPath);
this.mBackgroundMediaPlayer = this.createMediaplayer(path);
// record the path
this.mCurrentPath = pPath;
this.mCurrentPath = path;
}
}
@ -136,8 +136,11 @@ public class Cocos2dxMusic {
if (this.mBackgroundMediaPlayer != null) {
mBackgroundMediaPlayer.release();
mBackgroundMediaPlayer = createMediaplayer(mCurrentPath);
// should set the state, if not, the following sequence will be error
// play -> pause -> stop -> resume
/**
* should set the state, if not, the following sequence will be error
* play -> pause -> stop -> resume
*/
this.mPaused = false;
}
}
@ -192,16 +195,16 @@ public class Cocos2dxMusic {
}
}
public void setBackgroundVolume(float pVolume) {
if (pVolume < 0.0f) {
pVolume = 0.0f;
public void setBackgroundVolume(float volume) {
if (volume < 0.0f) {
volume = 0.0f;
}
if (pVolume > 1.0f) {
pVolume = 1.0f;
if (volume > 1.0f) {
volume = 1.0f;
}
this.mLeftVolume = this.mRightVolume = pVolume;
this.mLeftVolume = this.mRightVolume = volume;
if (this.mBackgroundMediaPlayer != null) {
this.mBackgroundMediaPlayer.setVolume(this.mLeftVolume, this.mRightVolume);
}
@ -238,16 +241,16 @@ public class Cocos2dxMusic {
* the pPath relative to assets
* @return
*/
private MediaPlayer createMediaplayer(final String pPath) {
private MediaPlayer createMediaplayer(final String path) {
MediaPlayer mediaPlayer = new MediaPlayer();
try {
if (pPath.startsWith("/")) {
final FileInputStream fis = new FileInputStream(pPath);
if (path.startsWith("/")) {
final FileInputStream fis = new FileInputStream(path);
mediaPlayer.setDataSource(fis.getFD());
fis.close();
} else {
final AssetFileDescriptor assetFileDescritor = this.mContext.getAssets().openFd(pPath);
final AssetFileDescriptor assetFileDescritor = this.mContext.getAssets().openFd(path);
mediaPlayer.setDataSource(assetFileDescritor.getFileDescriptor(), assetFileDescritor.getStartOffset(), assetFileDescritor.getLength());
}

View File

@ -56,13 +56,13 @@ public class Cocos2dxRenderer implements GLSurfaceView.Renderer {
// Getter & Setter
// ===========================================================
public static void setAnimationInterval(final double pAnimationInterval) {
Cocos2dxRenderer.sAnimationInterval = (long) (pAnimationInterval * Cocos2dxRenderer.NANOSECONDSPERSECOND);
public static void setAnimationInterval(final double animationInterval) {
Cocos2dxRenderer.sAnimationInterval = (long) (animationInterval * Cocos2dxRenderer.NANOSECONDSPERSECOND);
}
public void setScreenWidthAndHeight(final int pSurfaceWidth, final int pSurfaceHeight) {
this.mScreenWidth = pSurfaceWidth;
this.mScreenHeight = pSurfaceHeight;
public void setScreenWidthAndHeight(final int surfaceWidth, final int surfaceHeight) {
this.mScreenWidth = surfaceWidth;
this.mScreenHeight = surfaceHeight;
}
// ===========================================================
@ -70,15 +70,15 @@ public class Cocos2dxRenderer implements GLSurfaceView.Renderer {
// ===========================================================
@Override
public void onSurfaceCreated(final GL10 pGL10, final EGLConfig pEGLConfig) {
public void onSurfaceCreated(final GL10 GL10, final EGLConfig EGLConfig) {
Cocos2dxRenderer.nativeInit(this.mScreenWidth, this.mScreenHeight);
this.mLastTickInNanoSeconds = System.nanoTime();
mNativeInitCompleted = true;
}
@Override
public void onSurfaceChanged(final GL10 pGL10, final int pWidth, final int pHeight) {
Cocos2dxRenderer.nativeOnSurfaceChanged(pWidth, pHeight);
public void onSurfaceChanged(final GL10 GL10, final int width, final int height) {
Cocos2dxRenderer.nativeOnSurfaceChanged(width, height);
}
@Override
@ -111,42 +111,44 @@ public class Cocos2dxRenderer implements GLSurfaceView.Renderer {
// Methods
// ===========================================================
private static native void nativeTouchesBegin(final int pID, final float pX, final float pY);
private static native void nativeTouchesEnd(final int pID, final float pX, final float pY);
private static native void nativeTouchesMove(final int[] pIDs, final float[] pXs, final float[] pYs);
private static native void nativeTouchesCancel(final int[] pIDs, final float[] pXs, final float[] pYs);
private static native boolean nativeKeyDown(final int pKeyCode);
private static native void nativeTouchesBegin(final int id, final float x, final float y);
private static native void nativeTouchesEnd(final int id, final float x, final float y);
private static native void nativeTouchesMove(final int[] ids, final float[] xs, final float[] ys);
private static native void nativeTouchesCancel(final int[] ids, final float[] xs, final float[] ys);
private static native boolean nativeKeyDown(final int keyCode);
private static native void nativeRender();
private static native void nativeInit(final int pWidth, final int pHeight);
private static native void nativeOnSurfaceChanged(final int pWidth, final int pHeight);
private static native void nativeInit(final int width, final int height);
private static native void nativeOnSurfaceChanged(final int width, final int height);
private static native void nativeOnPause();
private static native void nativeOnResume();
public void handleActionDown(final int pID, final float pX, final float pY) {
Cocos2dxRenderer.nativeTouchesBegin(pID, pX, pY);
public void handleActionDown(final int id, final float x, final float y) {
Cocos2dxRenderer.nativeTouchesBegin(id, x, y);
}
public void handleActionUp(final int pID, final float pX, final float pY) {
Cocos2dxRenderer.nativeTouchesEnd(pID, pX, pY);
public void handleActionUp(final int id, final float x, final float y) {
Cocos2dxRenderer.nativeTouchesEnd(id, x, y);
}
public void handleActionCancel(final int[] pIDs, final float[] pXs, final float[] pYs) {
Cocos2dxRenderer.nativeTouchesCancel(pIDs, pXs, pYs);
public void handleActionCancel(final int[] ids, final float[] xs, final float[] ys) {
Cocos2dxRenderer.nativeTouchesCancel(ids, xs, ys);
}
public void handleActionMove(final int[] pIDs, final float[] pXs, final float[] pYs) {
Cocos2dxRenderer.nativeTouchesMove(pIDs, pXs, pYs);
public void handleActionMove(final int[] ids, final float[] xs, final float[] ys) {
Cocos2dxRenderer.nativeTouchesMove(ids, xs, ys);
}
public void handleKeyDown(final int pKeyCode) {
Cocos2dxRenderer.nativeKeyDown(pKeyCode);
public void handleKeyDown(final int keyCode) {
Cocos2dxRenderer.nativeKeyDown(keyCode);
}
public void handleOnPause() {
// onPause may be invoked before onSurfaceCreated
// and engine will be initialized correctly after
// onSurfaceCreated is invoked, can not invoke any
// native methed before onSurfaceCreated is invoked
/**
* onPause may be invoked before onSurfaceCreated,
* and engine will be initialized correctly after
* onSurfaceCreated is invoked. Can not invoke any
* native method before onSurfaceCreated is invoked
*/
if (! mNativeInitCompleted)
return;
@ -159,12 +161,12 @@ public class Cocos2dxRenderer implements GLSurfaceView.Renderer {
Cocos2dxRenderer.nativeOnResume();
}
private static native void nativeInsertText(final String pText);
private static native void nativeInsertText(final String text);
private static native void nativeDeleteBackward();
private static native String nativeGetContentText();
public void handleInsertText(final String pText) {
Cocos2dxRenderer.nativeInsertText(pText);
public void handleInsertText(final String text) {
Cocos2dxRenderer.nativeInsertText(text);
}
public void handleDeleteBackward() {

View File

@ -77,8 +77,8 @@ public class Cocos2dxSound {
// Constructors
// ===========================================================
public Cocos2dxSound(final Context pContext) {
this.mContext = pContext;
public Cocos2dxSound(final Context context) {
this.mContext = context;
this.initData();
}
@ -111,50 +111,50 @@ public class Cocos2dxSound {
// Methods
// ===========================================================
public int preloadEffect(final String pPath) {
Integer soundID = this.mPathSoundIDMap.get(pPath);
public int preloadEffect(final String path) {
Integer soundID = this.mPathSoundIDMap.get(path);
if (soundID == null) {
soundID = this.createSoundIDFromAsset(pPath);
soundID = this.createSoundIDFromAsset(path);
// save value just in case if file is really loaded
if (soundID != Cocos2dxSound.INVALID_SOUND_ID) {
this.mPathSoundIDMap.put(pPath, soundID);
this.mPathSoundIDMap.put(path, soundID);
}
}
return soundID;
}
public void unloadEffect(final String pPath) {
public void unloadEffect(final String path) {
// stop effects
final ArrayList<Integer> streamIDs = this.mPathStreamIDsMap.get(pPath);
final ArrayList<Integer> streamIDs = this.mPathStreamIDsMap.get(path);
if (streamIDs != null) {
for (final Integer steamID : streamIDs) {
this.mSoundPool.stop(steamID);
}
}
this.mPathStreamIDsMap.remove(pPath);
this.mPathStreamIDsMap.remove(path);
// unload effect
final Integer soundID = this.mPathSoundIDMap.get(pPath);
final Integer soundID = this.mPathSoundIDMap.get(path);
if(soundID != null){
this.mSoundPool.unload(soundID);
this.mPathSoundIDMap.remove(pPath);
this.mPathSoundIDMap.remove(path);
}
}
public int playEffect(final String pPath, final boolean pLoop, float pitch, float pan, float gain){
Integer soundID = this.mPathSoundIDMap.get(pPath);
public int playEffect(final String path, final boolean loop, float pitch, float pan, float gain){
Integer soundID = this.mPathSoundIDMap.get(path);
int streamID = Cocos2dxSound.INVALID_STREAM_ID;
if (soundID != null) {
// parameters; pan = -1 for left channel, 1 for right channel, 0 for both channels
// play sound
streamID = this.doPlayEffect(pPath, soundID.intValue(), pLoop, pitch, pan, gain);
streamID = this.doPlayEffect(path, soundID.intValue(), loop, pitch, pan, gain);
} else {
// the effect is not prepared
soundID = this.preloadEffect(pPath);
soundID = this.preloadEffect(path);
if (soundID == Cocos2dxSound.INVALID_SOUND_ID) {
// can not preload effect
return Cocos2dxSound.INVALID_SOUND_ID;
@ -163,7 +163,7 @@ public class Cocos2dxSound {
// only allow one playEffect at a time, or the semaphore will not work correctly
synchronized(this.mSoundPool) {
// add this effect into mEffecToPlayWhenLoadedArray, and it will be played when loaded completely
mEffecToPlayWhenLoadedArray.add(new SoundInfoForLoadedCompleted(pPath, soundID.intValue(), pLoop,
mEffecToPlayWhenLoadedArray.add(new SoundInfoForLoadedCompleted(path, soundID.intValue(), loop,
pitch, pan, gain));
try {
@ -247,16 +247,16 @@ public class Cocos2dxSound {
return (this.mLeftVolume + this.mRightVolume) / 2;
}
public void setEffectsVolume(float pVolume) {
// pVolume should be in [0, 1.0]
if (pVolume < 0) {
pVolume = 0;
public void setEffectsVolume(float volume) {
// volume should be in [0, 1.0]
if (volume < 0) {
volume = 0;
}
if (pVolume > 1) {
pVolume = 1;
if (volume > 1) {
volume = 1;
}
this.mLeftVolume = this.mRightVolume = pVolume;
this.mLeftVolume = this.mRightVolume = volume;
// change the volume of playing sounds
if (!this.mPathStreamIDsMap.isEmpty()) {
@ -282,14 +282,14 @@ public class Cocos2dxSound {
this.initData();
}
public int createSoundIDFromAsset(final String pPath) {
public int createSoundIDFromAsset(final String path) {
int soundID = Cocos2dxSound.INVALID_SOUND_ID;
try {
if (pPath.startsWith("/")) {
soundID = this.mSoundPool.load(pPath, 0);
if (path.startsWith("/")) {
soundID = this.mSoundPool.load(path, 0);
} else {
soundID = this.mSoundPool.load(this.mContext.getAssets().openFd(pPath), 0);
soundID = this.mSoundPool.load(this.mContext.getAssets().openFd(path), 0);
}
} catch (final Exception e) {
soundID = Cocos2dxSound.INVALID_SOUND_ID;
@ -308,19 +308,19 @@ public class Cocos2dxSound {
return Math.max(min, (Math.min(value, max)));
}
private int doPlayEffect(final String pPath, final int soundId, final boolean pLoop, float pitch, float pan, float gain) {
private int doPlayEffect(final String path, final int soundId, final boolean loop, float pitch, float pan, float gain) {
float leftVolume = this.mLeftVolume * gain * (1.0f - this.clamp(pan, 0.0f, 1.0f));
float rightVolume = this.mRightVolume * gain * (1.0f - this.clamp(-pan, 0.0f, 1.0f));
float soundRate = this.clamp(SOUND_RATE * pitch, 0.5f, 2.0f);
// play sound
int streamID = this.mSoundPool.play(soundId, this.clamp(leftVolume, 0.0f, 1.0f), this.clamp(rightVolume, 0.0f, 1.0f), Cocos2dxSound.SOUND_PRIORITY, pLoop ? -1 : 0, soundRate);
int streamID = this.mSoundPool.play(soundId, this.clamp(leftVolume, 0.0f, 1.0f), this.clamp(rightVolume, 0.0f, 1.0f), Cocos2dxSound.SOUND_PRIORITY, loop ? -1 : 0, soundRate);
// record stream id
ArrayList<Integer> streamIDs = this.mPathStreamIDsMap.get(pPath);
ArrayList<Integer> streamIDs = this.mPathStreamIDsMap.get(path);
if (streamIDs == null) {
streamIDs = new ArrayList<Integer>();
this.mPathStreamIDsMap.put(pPath, streamIDs);
this.mPathStreamIDsMap.put(path, streamIDs);
}
streamIDs.add(streamID);
@ -370,8 +370,10 @@ public class Cocos2dxSound {
// set the stream id which will be returned by playEffect()
mStreamIdSyn = doPlayEffect(info.path, info.soundID, info.isLoop, info.pitch, info.pan, info.gain);
// remove it from array, because we will break here
// so it is safe to do
/*
* Remove it from array, because we will break here.
* So it is safe to do.
*/
mEffecToPlayWhenLoadedArray.remove(info);
break;

View File

@ -56,21 +56,21 @@ public class Cocos2dxTypefaces {
// Methods
// ===========================================================
public static synchronized Typeface get(final Context pContext, final String pAssetName) {
if (!Cocos2dxTypefaces.sTypefaceCache.containsKey(pAssetName)) {
public static synchronized Typeface get(final Context context, final String assetName) {
if (!Cocos2dxTypefaces.sTypefaceCache.containsKey(assetName)) {
Typeface typeface = null;
if (pAssetName.startsWith("/"))
if (assetName.startsWith("/"))
{
typeface = Typeface.createFromFile(pAssetName);
typeface = Typeface.createFromFile(assetName);
}
else
{
typeface = Typeface.createFromAsset(pContext.getAssets(), pAssetName);
typeface = Typeface.createFromAsset(context.getAssets(), assetName);
}
Cocos2dxTypefaces.sTypefaceCache.put(pAssetName, typeface);
Cocos2dxTypefaces.sTypefaceCache.put(assetName, typeface);
}
return Cocos2dxTypefaces.sTypefaceCache.get(pAssetName);
return Cocos2dxTypefaces.sTypefaceCache.get(assetName);
}
// ===========================================================

View File

@ -232,7 +232,7 @@ public class Cocos2dxVideoHelper {
}
}
public static void setVideoUrl(int index,int videoSource,String videoUrl) {
public static void setVideoUrl(int index, int videoSource, String videoUrl) {
Message msg = new Message();
msg.what = VideoTaskSetSource;
msg.arg1 = index;
@ -241,7 +241,7 @@ public class Cocos2dxVideoHelper {
mVideoHandler.sendMessage(msg);
}
private void _setVideoURL(int index,int videoSource,String videoUrl) {
private void _setVideoURL(int index, int videoSource, String videoUrl) {
Cocos2dxVideoView videoView = sVideoViews.get(index);
if (videoView != null) {
switch (videoSource) {
@ -257,7 +257,7 @@ public class Cocos2dxVideoHelper {
}
}
public static void setVideoRect(int index,int left,int top,int maxWidth,int maxHeight) {
public static void setVideoRect(int index, int left, int top, int maxWidth, int maxHeight) {
Message msg = new Message();
msg.what = VideoTaskSetRect;
msg.arg1 = index;
@ -265,14 +265,14 @@ public class Cocos2dxVideoHelper {
mVideoHandler.sendMessage(msg);
}
private void _setVideoRect(int index,int left,int top,int maxWidth,int maxHeight) {
private void _setVideoRect(int index, int left, int top, int maxWidth, int maxHeight) {
Cocos2dxVideoView videoView = sVideoViews.get(index);
if (videoView != null) {
videoView.setVideoRect(left,top,maxWidth,maxHeight);
}
}
public static void setFullScreenEnabled(int index,boolean enabled, int width,int height) {
public static void setFullScreenEnabled(int index, boolean enabled, int width, int height) {
Message msg = new Message();
msg.what = VideoTaskFullScreen;
msg.arg1 = index;

View File

@ -52,11 +52,13 @@ public class Cocos2dxVideoView extends SurfaceView implements MediaPlayerControl
private static final int STATE_PAUSED = 4;
private static final int STATE_PLAYBACK_COMPLETED = 5;
// mCurrentState is a VideoView object's current state.
// mTargetState is the state that a method caller intends to reach.
// For instance, regardless the VideoView object's current state,
// calling pause() intends to bring the object to a target state
// of STATE_PAUSED.
/**
* mCurrentState is a VideoView object's current state.
* mTargetState is the state that a method caller intends to reach.
* For instance, regardless the VideoView object's current state,
* calling pause() intends to bring the object to a target state
* of STATE_PAUSED.
*/
private int mCurrentState = STATE_IDLE;
private int mTargetState = STATE_IDLE;
@ -113,7 +115,7 @@ public class Cocos2dxVideoView extends SurfaceView implements MediaPlayerControl
}
public void setVideoRect(int left,int top,int maxWidth,int maxHeight) {
public void setVideoRect(int left, int top, int maxWidth, int maxHeight) {
mViewLeft = left;
mViewTop = top;
mViewWidth = maxWidth;
@ -296,8 +298,10 @@ public class Cocos2dxVideoView extends SurfaceView implements MediaPlayerControl
}
mMediaPlayer.prepareAsync();
// we don't set the target state here either, but preserve the
// target state that was there before.
/**
* Don't set the target state here either, but preserve the target state that was there before.
*/
mCurrentState = STATE_PREPARING;
} catch (IOException ex) {
Log.w(TAG, "Unable to open content: " + mUri, ex);
@ -329,7 +333,7 @@ public class Cocos2dxVideoView extends SurfaceView implements MediaPlayerControl
}
}
public void fixSize(int left,int top,int width,int height) {
public void fixSize(int left, int top, int width, int height) {
if (width != 0 && height != 0) {
if (mKeepRatio) {
if ( mVideoWidth * height > width * mVideoHeight ) {
@ -389,7 +393,8 @@ public class Cocos2dxVideoView extends SurfaceView implements MediaPlayerControl
mVideoWidth = mp.getVideoWidth();
mVideoHeight = mp.getVideoHeight();
int seekToPosition = mSeekWhenPrepared; // mSeekWhenPrepared may be changed after seekTo() call
// mSeekWhenPrepared may be changed after seekTo() call
int seekToPosition = mSeekWhenPrepared;
if (seekToPosition != 0) {
seekTo(seekToPosition);
}
@ -451,10 +456,10 @@ public class Cocos2dxVideoView extends SurfaceView implements MediaPlayerControl
int messageId;
if (framework_err == MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) {
//messageId = com.android.internal.R.string.VideoView_error_text_invalid_progressive_playback;
// messageId = com.android.internal.R.string.VideoView_error_text_invalid_progressive_playback;
messageId = r.getIdentifier("VideoView_error_text_invalid_progressive_playback", "string", "android");
} else {
//messageId = com.android.internal.R.string.VideoView_error_text_unknown;
// messageId = com.android.internal.R.string.VideoView_error_text_unknown;
messageId = r.getIdentifier("VideoView_error_text_unknown", "string", "android");
}
@ -502,7 +507,7 @@ public class Cocos2dxVideoView extends SurfaceView implements MediaPlayerControl
/**
* Register a callback to be invoked when the end of a media file
* has been reached during playback.
* has been reached during play back.
*
* @param l The callback that will be run
*/
@ -513,7 +518,7 @@ public class Cocos2dxVideoView extends SurfaceView implements MediaPlayerControl
/**
* Register a callback to be invoked when an error occurs
* during playback or setup. If no listener is specified,
* during play back or setup. If no listener is specified,
* or if the listener returned false, VideoView will inform
* the user of any errors.
*

View File

@ -15,8 +15,8 @@ import android.widget.FrameLayout;
public class Cocos2dxWebView extends WebView {
private static final String TAG = Cocos2dxWebViewHelper.class.getSimpleName();
private int viewTag;
private String jsScheme;
private int mViewTag;
private String mJSScheme;
public Cocos2dxWebView(Context context) {
this(context, -1);
@ -25,8 +25,8 @@ public class Cocos2dxWebView extends WebView {
@SuppressLint("SetJavaScriptEnabled")
public Cocos2dxWebView(Context context, int viewTag) {
super(context);
this.viewTag = viewTag;
this.jsScheme = "";
this.mViewTag = viewTag;
this.mJSScheme = "";
this.setFocusable(true);
this.setFocusableInTouchMode(true);
@ -48,7 +48,7 @@ public class Cocos2dxWebView extends WebView {
}
public void setJavascriptInterfaceScheme(String scheme) {
this.jsScheme = scheme != null ? scheme : "";
this.mJSScheme = scheme != null ? scheme : "";
}
public void setScalesPageToFit(boolean scalesPageToFit) {
@ -59,23 +59,23 @@ public class Cocos2dxWebView extends WebView {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlString) {
URI uri = URI.create(urlString);
if (uri != null && uri.getScheme().equals(jsScheme)) {
Cocos2dxWebViewHelper._onJsCallback(viewTag, urlString);
if (uri != null && uri.getScheme().equals(mJSScheme)) {
Cocos2dxWebViewHelper._onJsCallback(mViewTag, urlString);
return true;
}
return Cocos2dxWebViewHelper._shouldStartLoading(viewTag, urlString);
return Cocos2dxWebViewHelper._shouldStartLoading(mViewTag, urlString);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Cocos2dxWebViewHelper._didFinishLoading(viewTag, url);
Cocos2dxWebViewHelper._didFinishLoading(mViewTag, url);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Cocos2dxWebViewHelper._didFailLoading(viewTag, failingUrl);
Cocos2dxWebViewHelper._didFailLoading(mViewTag, failingUrl);
}
}

View File

@ -13,18 +13,18 @@ import java.util.concurrent.FutureTask;
public class Cocos2dxWebViewHelper {
private static final String TAG = Cocos2dxWebViewHelper.class.getSimpleName();
private static Handler handler;
private static Cocos2dxActivity cocos2dxActivity;
private static FrameLayout layout;
private static Handler sHandler;
private static Cocos2dxActivity sCocos2dxActivity;
private static FrameLayout sLayout;
private static SparseArray<Cocos2dxWebView> webViews;
private static int viewTag = 0;
public Cocos2dxWebViewHelper(FrameLayout layout) {
Cocos2dxWebViewHelper.layout = layout;
Cocos2dxWebViewHelper.handler = new Handler(Looper.myLooper());
Cocos2dxWebViewHelper.sLayout = layout;
Cocos2dxWebViewHelper.sHandler = new Handler(Looper.myLooper());
Cocos2dxWebViewHelper.cocos2dxActivity = (Cocos2dxActivity) Cocos2dxActivity.getContext();
Cocos2dxWebViewHelper.sCocos2dxActivity = (Cocos2dxActivity) Cocos2dxActivity.getContext();
Cocos2dxWebViewHelper.webViews = new SparseArray<Cocos2dxWebView>();
}
@ -55,14 +55,14 @@ public class Cocos2dxWebViewHelper {
@SuppressWarnings("unused")
public static int createWebView() {
final int index = viewTag;
cocos2dxActivity.runOnUiThread(new Runnable() {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = new Cocos2dxWebView(cocos2dxActivity, index);
Cocos2dxWebView webView = new Cocos2dxWebView(sCocos2dxActivity, index);
FrameLayout.LayoutParams lParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
layout.addView(webView, lParams);
sLayout.addView(webView, lParams);
webViews.put(index, webView);
}
@ -72,13 +72,13 @@ public class Cocos2dxWebViewHelper {
@SuppressWarnings("unused")
public static void removeWebView(final int index) {
cocos2dxActivity.runOnUiThread(new Runnable() {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);
if (webView != null) {
webViews.remove(index);
layout.removeView(webView);
sLayout.removeView(webView);
}
}
});
@ -86,7 +86,7 @@ public class Cocos2dxWebViewHelper {
@SuppressWarnings("unused")
public static void setVisible(final int index, final boolean visible) {
cocos2dxActivity.runOnUiThread(new Runnable() {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);
@ -99,7 +99,7 @@ public class Cocos2dxWebViewHelper {
@SuppressWarnings("unused")
public static void setWebViewRect(final int index, final int left, final int top, final int maxWidth, final int maxHeight) {
cocos2dxActivity.runOnUiThread(new Runnable() {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);
@ -112,7 +112,7 @@ public class Cocos2dxWebViewHelper {
@SuppressWarnings("unused")
public static void setJavascriptInterfaceScheme(final int index, final String scheme) {
cocos2dxActivity.runOnUiThread(new Runnable() {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);
@ -125,7 +125,7 @@ public class Cocos2dxWebViewHelper {
@SuppressWarnings("unused")
public static void loadData(final int index, final String data, final String mimeType, final String encoding, final String baseURL) {
cocos2dxActivity.runOnUiThread(new Runnable() {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);
@ -138,7 +138,7 @@ public class Cocos2dxWebViewHelper {
@SuppressWarnings("unused")
public static void loadHTMLString(final int index, final String htmlString, final String mimeType, final String encoding) {
cocos2dxActivity.runOnUiThread(new Runnable() {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);
@ -151,7 +151,7 @@ public class Cocos2dxWebViewHelper {
@SuppressWarnings("unused")
public static void loadUrl(final int index, final String url) {
cocos2dxActivity.runOnUiThread(new Runnable() {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);
@ -164,7 +164,7 @@ public class Cocos2dxWebViewHelper {
@SuppressWarnings("unused")
public static void loadFile(final int index, final String filePath) {
cocos2dxActivity.runOnUiThread(new Runnable() {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);
@ -176,7 +176,7 @@ public class Cocos2dxWebViewHelper {
}
public static void stopLoading(final int index) {
cocos2dxActivity.runOnUiThread(new Runnable() {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);
@ -189,7 +189,7 @@ public class Cocos2dxWebViewHelper {
}
public static void reload(final int index) {
cocos2dxActivity.runOnUiThread(new Runnable() {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);
@ -202,7 +202,7 @@ public class Cocos2dxWebViewHelper {
public static <T> T callInMainThread(Callable<T> call) throws ExecutionException, InterruptedException {
FutureTask<T> task = new FutureTask<T>(call);
handler.post(task);
sHandler.post(task);
return task.get();
}
@ -244,7 +244,7 @@ public class Cocos2dxWebViewHelper {
@SuppressWarnings("unused")
public static void goBack(final int index) {
cocos2dxActivity.runOnUiThread(new Runnable() {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);
@ -257,7 +257,7 @@ public class Cocos2dxWebViewHelper {
@SuppressWarnings("unused")
public static void goForward(final int index) {
cocos2dxActivity.runOnUiThread(new Runnable() {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);
@ -270,7 +270,7 @@ public class Cocos2dxWebViewHelper {
@SuppressWarnings("unused")
public static void evaluateJS(final int index, final String js) {
cocos2dxActivity.runOnUiThread(new Runnable() {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);
@ -283,7 +283,7 @@ public class Cocos2dxWebViewHelper {
@SuppressWarnings("unused")
public static void setScalesPageToFit(final int index, final boolean scalesPageToFit) {
cocos2dxActivity.runOnUiThread(new Runnable() {
sCocos2dxActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Cocos2dxWebView webView = webViews.get(index);