Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Update packages #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,22 @@ public void onOrientationChanged(int physOrientation) {
if (physOrientation != OrientationEventListener.ORIENTATION_UNKNOWN) {

Activity activity = rctContext.getCurrentActivity();
int uiRotation = (activity == null)
? Surface.ROTATION_0
int uiRotation = (activity == null) ? Surface.ROTATION_0
: activity.getWindowManager().getDefaultDisplay().getRotation();
int uiDegrees = 0;
switch (uiRotation) {
case Surface.ROTATION_0: uiDegrees = 0; break;
case Surface.ROTATION_90: uiDegrees = 90; break;
case Surface.ROTATION_180: uiDegrees = 180; break;
case Surface.ROTATION_270: uiDegrees = 270; break;
case Surface.ROTATION_0:
uiDegrees = 0;
break;
case Surface.ROTATION_90:
uiDegrees = 90;
break;
case Surface.ROTATION_180:
uiDegrees = 180;
break;
case Surface.ROTATION_270:
uiDegrees = 270;
break;
}

int relOrientation = (physOrientation + uiDegrees) % 360;
Expand All @@ -112,15 +119,16 @@ public void onOrientationChanged(int physOrientation) {
} else if (relOrientation < 125 && relOrientation >= 55) {
nextDeviceOrientation = CC_CAMERA_ORIENTATION_LANDSCAPE_RIGHT;
} else if (relOrientation < 215 && relOrientation >= 145) {
nextDeviceOrientation = CC_CAMERA_ORIENTATION_PORTRAIT_UPSIDEDOWN;
nextDeviceOrientation = CC_CAMERA_ORIENTATION_PORTRAIT_UPSIDEDOWN;
}

// System.out.println("[OrientationModule] Activity null? " + (activity == null ? "YES" : "NO"));
// System.out.println("[OrientationModule] UI degrees: " + uiDegrees + ", Phys degrees: " + physOrientation + ", FINAL: " + relOrientation);
}

if (nextDeviceOrientation != mDeviceOrientation) {
System.out.println("CCCameraOrientationChange: " + mDeviceOrientation + " -> " + nextDeviceOrientation);
System.out.println(
"CCCameraOrientationChange: " + mDeviceOrientation + " -> " + nextDeviceOrientation);
mDeviceOrientation = nextDeviceOrientation;

WritableMap params = Arguments.createMap();
Expand Down Expand Up @@ -154,6 +162,13 @@ public void getOrientation(Callback callback) {
}
}

@ReactMethod
public void getOrientationAsCCInt(Callback callback) {
final int orientationInt = getReactApplicationContext().getResources().getConfiguration().orientation;
int ccOrientationInt = this.getCCOrientationInt(orientationInt);
callback.invoke(null, ccOrientationInt);
}

@ReactMethod
public void lockToPortrait() {
final Activity activity = getCurrentActivity();
Expand Down Expand Up @@ -204,6 +219,8 @@ public void unlockAllOrientations() {
HashMap<String, Object> constants = new HashMap<String, Object>();
int orientationInt = getReactApplicationContext().getResources().getConfiguration().orientation;

constants.put("initialOrientationInt", this.getCCOrientationInt(orientationInt));

String orientation = this.getOrientationString(orientationInt);
if (orientation == "null") {
constants.put("initialOrientation", null);
Expand Down Expand Up @@ -234,6 +251,21 @@ private String getOrientationString(int orientation) {
}
}

private int getCCOrientationInt(int orientation) {
int ccOrientationInt;
switch (orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
ccOrientationInt = CC_CAMERA_ORIENTATION_LANDSCAPE_LEFT;
break;
case Configuration.ORIENTATION_PORTRAIT:
ccOrientationInt = CC_CAMERA_ORIENTATION_PORTRAIT;
break;
default:
ccOrientationInt = -1;
}
return ccOrientationInt;
}

@Override
public void onHostResume() {
final Activity activity = getCurrentActivity();
Expand Down
33 changes: 32 additions & 1 deletion iOS/RCTOrientation/Orientation.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#endif

@implementation Orientation
@synthesize bridge = _bridge;

static UIInterfaceOrientationMask _orientation = UIInterfaceOrientationMaskAllButUpsideDown;
+ (void)setOrientation: (UIInterfaceOrientationMask)orientation {
Expand Down Expand Up @@ -146,6 +145,29 @@ - (void)deviceOrientationDidChange:(NSNotification *)notification
}
}

- (NSNumber *)getCCOrientationInt: (UIDeviceOrientation)orientation {
NSNumber *ccOrientationInt;
switch (orientation) {
case UIDeviceOrientationPortrait:
ccOrientationInt = @(CCCameraOrientationPortrait);
break;
case UIDeviceOrientationLandscapeLeft:
ccOrientationInt = @(CCCameraOrientationLandscapeLeft);
break;
case UIDeviceOrientationLandscapeRight:
ccOrientationInt = @(CCCameraOrientationLandscapeRight);
break;
case UIDeviceOrientationPortraitUpsideDown:
ccOrientationInt = @(CCCameraOrientationPortraitUpsideDown);
break;
default:
// use last known orientation (if FaceUp or FaceDown, or unknown)
ccOrientationInt = @(self.lastOrientation);
break;
}
return ccOrientationInt;
}

- (NSString *)getOrientationStr: (UIDeviceOrientation)orientation {
NSString *orientationStr;
switch (orientation) {
Expand Down Expand Up @@ -247,6 +269,13 @@ - (NSString *)getSpecificOrientationStr: (UIDeviceOrientation)orientation {
callback(@[[NSNull null], orientationStr]);
}

RCT_EXPORT_METHOD(getOrientationAsCCInt:(RCTResponseSenderBlock)callback)
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
NSNumber *ccOrientationInt = [self getCCOrientationInt:orientation];
callback(@[[NSNull null], ccOrientationInt]);
}

RCT_EXPORT_METHOD(lockToPortrait)
{
#if DEBUG
Expand Down Expand Up @@ -322,8 +351,10 @@ - (NSDictionary *)constantsToExport

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
NSString *orientationStr = [self getOrientationStr:orientation];
NSNumber *ccOrientationInt = [self getCCOrientationInt:orientation];

return @{
@"initialOrientationInt": ccOrientationInt,
@"initialOrientation": orientationStr,
@"orientationEnum": @{
@"portrait": @(CCCameraOrientationPortrait),
Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ module.exports = {
});
},

getOrientationAsCCInt(cb) {
Orientation.getOrientationAsCCInt((error, orientation) => {
cb(error, orientation);
});
},

getSpecificOrientation(cb) {
Orientation.getSpecificOrientation((error, orientation) => {
cb(error, orientation);
Expand Down Expand Up @@ -150,6 +156,10 @@ module.exports = {
return Orientation.initialOrientation;
},

getInitialOrientationInt() {
return Orientation.initialOrientationInt;
},

getOrientations() {
return Orientation.orientationEnum;
}
Expand Down