Android: first attempt at using the density call from java
authorXeli
Tue, 05 Jun 2012 19:36:07 +0200
changeset 7185 92a045156255
parent 7184 211ab9e2cb15
child 7186 013deb83086b
child 7187 aff30d80bd7b
Android: first attempt at using the density call from java
hedgewars/uTouch.pas
project_files/Android-build/SDL-android-project/jni/SDL/src/core/android/SDL_android.cpp
project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/SDLActivity.java
--- a/hedgewars/uTouch.pas	Tue Jun 05 19:33:49 2012 +0200
+++ b/hedgewars/uTouch.pas	Tue Jun 05 19:36:07 2012 +0200
@@ -62,8 +62,11 @@
 const
     clickTime = 200;
     nilFingerId = High(TSDL_FingerId);
+    baseRectSize = 96;
 
 var
+    rectSize, halfRectSize: LongInt;
+
     pointerCount : Longword;
     fingers: array of TTouch_Data;
     moveCursor : boolean;
@@ -551,8 +554,10 @@
     x := 0;//avoid compiler hint
     y := 0;
     convertToFingerCoord(x, y, CrosshairX, CrosshairY);
-  isOnCrosshair:= sqrt(sqr(finger.x-x) + sqr(finger.y-y)) < 50;
-//    isOnCrosshair:= isOnRect(x-24, y-24, 48, 48, finger);
+    isOnCrosshair:= isOnRect((x-HalfRectSize), (y-HalfRectSize), RectSize, RectSize, finger);
+    printFinger(finger);
+    WriteLnToConsole(inttostr(finger.x) + '   ' + inttostr(x));
+    WriteLnToConsole(inttostr(x) + '  ' + inttostr(y) + '   ' + inttostr(round(Android_JNI_getDensity() * 10)));
 end;
 
 function isOnCurrentHog(finger: TTouch_Data): boolean;
@@ -562,7 +567,7 @@
     x := 0;
     y := 0;
     convertToFingerCoord(x,y, hwRound(CurrentHedgehog^.Gear^.X), hwRound(CurrentHedgehog^.Gear^.Y));
-    isOnCurrentHog := sqrt(sqr(finger.X-x) + sqr(finger.Y-y)) < 50;
+    isOnCurrentHog:= isOnRect((x-HalfRectSize), (y-HalfRectSize), RectSize, RectSize, finger);
 end;
 
 procedure convertToFingerCoord(var x,y : LongInt; oldX, oldY: LongInt);
@@ -627,12 +632,22 @@
 var
     index: Longword;
     //uRenderCoordScaleX, uRenderCoordScaleY: Longword;
+    density: Single;
 begin
     buttonsDown:= 0;
 
     setLength(fingers, 4);
     for index := 0 to High(fingers) do 
         fingers[index].id := nilFingerId;
+
+{$IFDEF ANDROID}
+    density:= Android_JNI_getDensity();
+{$ELSE}
+    density:= 1.0;
+{$ENDIF}
+
+    rectSize:= round(baseRectSize * density);
+    halfRectSize:= rectSize shl 1;
 end;
 
 begin
--- a/project_files/Android-build/SDL-android-project/jni/SDL/src/core/android/SDL_android.cpp	Tue Jun 05 19:33:49 2012 +0200
+++ b/project_files/Android-build/SDL-android-project/jni/SDL/src/core/android/SDL_android.cpp	Tue Jun 05 19:36:07 2012 +0200
@@ -638,16 +638,12 @@
     return Android_JNI_FileClose(ctx, true);
 }
 
-/*******************************************************************************
-             Functions called by the hwengine into Java
-*******************************************************************************/
-
-extern "C" float Android_JNI_getDensity(){
+extern "C" int Android_JNI_getDensity(){
     jmethodID mid;
-    jfloat density;
+    jint density;
     //SDLActivity.getDensity()
-    mid = mEnv->GetStaticMethodID(mActivityClass, "getDensity", "()F");
-        if(!mid) return 1.5f;
+    mid = mEnv->GetStaticMethodID(mActivityClass, "getDensity", "()I");
+        if(!mid) return 160;
     density = mEnv->CallStaticFloatMethod(mActivityClass, mid);
     return density;
 
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/SDLActivity.java	Tue Jun 05 19:33:49 2012 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/SDLActivity.java	Tue Jun 05 19:36:07 2012 +0200
@@ -403,9 +403,9 @@
 		}
 	}
 	
-	public static float getDensity(){
+	public static int getDensity(){
 		DisplayMetrics dm = SDLActivity.getContext().getResources().getDisplayMetrics();
-		return dm.density;
+		return dm.densityDpi;
 	}
 }