issue #1712: Adding getting dpi support for iOS and Android.

This commit is contained in:
James Chen 2013-02-16 19:36:21 +08:00
parent a170f3b0d8
commit ac17f67686
11 changed files with 146 additions and 15 deletions

View File

@ -80,6 +80,7 @@ platform/CCThread.cpp \
platform/CCFileUtils.cpp \ platform/CCFileUtils.cpp \
platform/platform.cpp \ platform/platform.cpp \
platform/CCEGLViewProtocol.cpp \ platform/CCEGLViewProtocol.cpp \
platform/android/CCDevice.cpp \
platform/android/CCEGLView.cpp \ platform/android/CCEGLView.cpp \
platform/android/CCAccelerometer.cpp \ platform/android/CCAccelerometer.cpp \
platform/android/CCApplication.cpp \ platform/android/CCApplication.cpp \
@ -93,6 +94,7 @@ platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxAccelerometer.cpp \
platform/android/jni/JniHelper.cpp \ platform/android/jni/JniHelper.cpp \
platform/android/jni/IMEJni.cpp \ platform/android/jni/IMEJni.cpp \
platform/android/jni/TouchesJni.cpp \ platform/android/jni/TouchesJni.cpp \
platform/android/jni/DPIJni.cpp \
script_support/CCScriptSupport.cpp \ script_support/CCScriptSupport.cpp \
shaders/ccShaders.cpp \ shaders/ccShaders.cpp \
shaders/CCGLProgram.cpp \ shaders/CCGLProgram.cpp \

View File

@ -123,7 +123,7 @@ THE SOFTWARE.
#include "particle_nodes/CCParticleSystemQuad.h" #include "particle_nodes/CCParticleSystemQuad.h"
// platform // platform
#include "platform/CCDevice.h"
#include "platform/CCCommon.h" #include "platform/CCCommon.h"
#include "platform/CCFileUtils.h" #include "platform/CCFileUtils.h"
#include "platform/CCImage.h" #include "platform/CCImage.h"

View File

@ -0,0 +1,19 @@
#ifndef __CCDEVICE_H__
#define __CCDEVICE_H__
#include "CCPlatformMacros.h"
NS_CC_BEGIN
class CCDevice
{
private:
CCDevice();
public:
static float getDPI();
};
NS_CC_END
#endif /* __CCDEVICE_H__ */

View File

@ -0,0 +1,11 @@
#include "platform/CCDevice.h"
#include "jni/DPIJni.h"
NS_CC_BEGIN
float CCDevice::getDPI()
{
return getDPIJNI();
}
NS_CC_END

View File

@ -29,7 +29,10 @@ import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.os.Message; import android.os.Message;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout; import android.widget.FrameLayout;
public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelperListener { public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelperListener {
@ -106,6 +109,25 @@ public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelpe
this.mHandler.sendMessage(msg); this.mHandler.sendMessage(msg);
} }
public static float getDPI()
{
if (sContext != null)
{
DisplayMetrics metrics = new DisplayMetrics();
WindowManager wm = ((Activity)sContext).getWindowManager();
if (wm != null)
{
Display d = wm.getDefaultDisplay();
if (d != null)
{
d.getMetrics(metrics);
return (int)(metrics.density*160.0f);
}
}
}
return 0.0f;
}
@Override @Override
public void runOnGLThread(final Runnable pRunnable) { public void runOnGLThread(final Runnable pRunnable) {
this.mGLSurfaceView.queueEvent(pRunnable); this.mGLSurfaceView.queueEvent(pRunnable);

View File

@ -0,0 +1,19 @@
#include "DPIJni.h"
#include "jni/JniHelper.h"
USING_NS_CC;
extern "C" {
float getDPIJNI()
{
JniMethodInfo t;
jfloat ret = 0.0f;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxActivity", "getDPI", "()F")) {
ret = t.env->CallStaticFloatMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
}
return ret;
}
} // extern "C"

View File

@ -0,0 +1,10 @@
#ifndef __DPIJNI_H__
#define __DPIJNI_H__
extern "C" {
float getDPIJNI();
} // extern "C"
#endif /* __DPIJNI_H__ */

View File

@ -0,0 +1,25 @@
#include "CCDevice.h"
#import <UIKit/UIKit.h>
NS_CC_BEGIN
float CCDevice::getDPI()
{
float scale = 1.0f;
float dpi = 0.0f;
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
scale = [[UIScreen mainScreen] scale];
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
dpi = 132 * scale;
} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
dpi = 163 * scale;
} else {
dpi = 160 * scale;
}
return dpi;
}
NS_CC_END

View File

@ -1 +1 @@
81abe6adef9de3477eb86bdb9c31bd542aa0eef8 faa02223cf3b80808988ca41838ac143d3001427

View File

@ -24,16 +24,6 @@
****************************************************************************/ ****************************************************************************/
#include "CCScrollView.h" #include "CCScrollView.h"
#include "actions/CCActionInterval.h"
#include "actions/CCActionTween.h"
#include "actions/CCActionInstant.h"
#include "support/CCPointExtension.h"
#include "touch_dispatcher/CCTouchDispatcher.h"
#include "effects/CCGrid.h"
#include "CCDirector.h"
#include "kazmath/GL/matrix.h"
#include "touch_dispatcher/CCTouch.h"
#include "CCEGLView.h"
NS_CC_EXT_BEGIN NS_CC_EXT_BEGIN
@ -41,6 +31,13 @@ NS_CC_EXT_BEGIN
#define SCROLL_DEACCEL_DIST 1.0f #define SCROLL_DEACCEL_DIST 1.0f
#define BOUNCE_DURATION 0.15f #define BOUNCE_DURATION 0.15f
#define INSET_RATIO 0.2f #define INSET_RATIO 0.2f
#define MOVE_INCH 7.0f/160.0f
static float convertDistanceFromPointToInch(float pointDis)
{
float factor = ( CCEGLView::sharedOpenGLView()->getScaleX() + CCEGLView::sharedOpenGLView()->getScaleY() ) / 2;
return pointDis * factor / CCDevice::getDPI();
}
CCScrollView::CCScrollView() CCScrollView::CCScrollView()
@ -629,12 +626,38 @@ void CCScrollView::ccTouchMoved(CCTouch* touch, CCEvent* event)
CCRect frame; CCRect frame;
float newX, newY; float newX, newY;
m_bTouchMoved = true;
frame = getViewRect(); frame = getViewRect();
newPoint = this->convertTouchToNodeSpace((CCTouch*)m_pTouches->objectAtIndex(0)); newPoint = this->convertTouchToNodeSpace((CCTouch*)m_pTouches->objectAtIndex(0));
moveDistance = ccpSub(newPoint, m_tTouchPoint); moveDistance = ccpSub(newPoint, m_tTouchPoint);
m_tTouchPoint = newPoint;
float dis = 0.0f;
if (m_eDirection == kCCScrollViewDirectionVertical)
{
dis = moveDistance.y;
}
else if (m_eDirection == kCCScrollViewDirectionHorizontal)
{
dis = moveDistance.x;
}
else
{
dis = sqrtf(moveDistance.x*moveDistance.x + moveDistance.y*moveDistance.y);
}
float disInch = 0.0f;
if (!m_bTouchMoved && ( disInch = fabs(convertDistanceFromPointToInch(dis))) < MOVE_INCH)
{
CCLOG("Invalid movement, distance = [%f, %f], disInch = %f", moveDistance.x, moveDistance.y, disInch);
return;
}
if (!m_bTouchMoved)
{
moveDistance = CCPointZero;
}
m_tTouchPoint = newPoint;
m_bTouchMoved = true;
if (frame.containsPoint(this->convertToWorldSpace(newPoint))) if (frame.containsPoint(this->convertToWorldSpace(newPoint)))
{ {

View File

@ -26,7 +26,7 @@
#ifndef __CCSCROLLVIEW_H__ #ifndef __CCSCROLLVIEW_H__
#define __CCSCROLLVIEW_H__ #define __CCSCROLLVIEW_H__
#include "layers_scenes_transitions_nodes/CCLayer.h" #include "cocos2d.h"
#include "ExtensionMacros.h" #include "ExtensionMacros.h"
NS_CC_EXT_BEGIN NS_CC_EXT_BEGIN