Skip to content

Commit 7e19f0d

Browse files
committed
Merge pull request #13 from spritebuilder/headless-develop
Android - Textfield fixes, orientation fixes and tablet detection added
2 parents 6f2811d + 9f54b1a commit 7e19f0d

File tree

5 files changed

+53
-40
lines changed

5 files changed

+53
-40
lines changed

cocos2d-tests-android/Headless/tests/CCTextFieldTest.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ - (void) setupTextFieldBasicTest
3434
textField.preferredSize = CGSizeMake(0.5, 50);
3535
textField.positionType = CCPositionTypeNormalized;
3636
textField.position = ccp(0.5f, 0.8f);
37-
textField.padding = 6;
37+
textField.padding = 10;
3838
textField.anchorPoint = ccp(0.5f, 0.5f);
3939
textField.string = @"Hello!";
4040

cocos2d-ui/Platform/Android/CCPlatformTextFieldAndroid.m

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#import "CCDirector.h"
1515
#import "CCEditText.h"
1616

17-
17+
1818
@implementation CCPlatformTextFieldAndroid {
1919
CCEditText *_editText;
2020
}
@@ -50,29 +50,28 @@ - (void) onExitTransitionDidStart
5050
- (void) positionInControl:(CCControl *)control padding:(float)padding {
5151
CGPoint worldPos = [control convertToWorldSpace:CGPointZero];
5252
CGPoint viewPos = [[CCDirector sharedDirector] convertToUI:worldPos];
53-
viewPos.x += padding;
54-
// viewPos.y += padding;
53+
5554

5655
CGFloat scale = [[CCDirector sharedDirector] contentScaleFactor];
5756

5857
viewPos.x *= scale;
5958
viewPos.y *= scale;
6059

6160
CGSize size = control.contentSizeInPoints;
62-
size.width -= padding * 2;
63-
size.height -= padding * 2;
6461
size.width *= scale;
6562
size.height *= scale ;
66-
viewPos.y -= size.height + padding *scale;
67-
63+
viewPos.y -= size.height;
64+
6865

6966
CGRect frame = CGRectZero;
7067
frame.origin = viewPos;
7168
frame.size = size;
69+
int nativePadding = (int)padding*scale;
7270
dispatch_async(dispatch_get_main_queue(), ^{
7371
AndroidViewGroupLayoutParams *oldParams = [_editText layoutParams];
7472
AndroidRelativeLayoutLayoutParams *params = [[AndroidRelativeLayoutLayoutParams alloc] initWithWidth:frame.size.width height:frame.size.height];
7573
[params setMargins:frame.origin.x top:frame.origin.y right:0 bottom:0];
74+
[_editText setPadding:nativePadding top:nativePadding right:nativePadding bottom:nativePadding];
7675
[_editText setLayoutParams:params];
7776
[_editText setImeOptions:AndroidEditorInfoIME_FLAG_NO_EXTRACT_UI];
7877

@@ -82,7 +81,7 @@ - (void) positionInControl:(CCControl *)control padding:(float)padding {
8281
if ([[weakSelf delegate] respondsToSelector:@selector(platformTextFieldDidFinishEditing:)]) {
8382
[[weakSelf delegate] platformTextFieldDidFinishEditing:weakSelf];
8483
}
85-
84+
8685
}];
8786
});
8887
}
@@ -106,12 +105,12 @@ - (void)setFontSize:(float)fontSize {
106105
dispatch_async(dispatch_get_main_queue(), ^{
107106
// AndroidDisplayMetrics *metrics = [[AndroidDisplayMetrics alloc] init];
108107
// [[CCActivity currentActivity].windowManager.defaultDisplay getMetrics:metrics];
109-
108+
110109
[_editText setTextSizeDouble:fontSize];
111-
110+
112111
});
113-
114-
112+
113+
115114
}
116115

117116
- (void)setString:(NSString *)string {

cocos2d/CCConfiguration.m

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,34 @@ -(NSInteger) runningDevice
171171
NSInteger ret=-1;
172172

173173
#if __CC_PLATFORM_ANDROID
174-
if([CCDirector sharedDirector].contentScaleFactor > 1.0)
175-
{
176-
ret = CCDeviceiPhoneRetinaDisplay;
177-
}
178-
else
179-
{
180-
ret = CCDeviceiPhone;
174+
175+
176+
AndroidDisplayMetrics *metrics = [[AndroidDisplayMetrics alloc] init];
177+
[[CCActivity currentActivity].windowManager.defaultDisplay getMetrics:metrics];
178+
double yInches= metrics.heightPixels/metrics.ydpi;
179+
double xInches= metrics.widthPixels/metrics.xdpi;
180+
double diagonalInches = sqrt(xInches*xInches + yInches*yInches);
181+
if (diagonalInches<=6){
182+
183+
184+
if([CCDirector sharedDirector].contentScaleFactor > 1.0)
185+
{
186+
ret = CCDeviceiPhoneRetinaDisplay;
187+
}
188+
else
189+
{
190+
ret = CCDeviceiPhone;
191+
}
192+
} else {
193+
if([CCDirector sharedDirector].contentScaleFactor > 1.0)
194+
{
195+
ret = CCDeviceiPadRetinaDisplay;
196+
}
197+
else
198+
{
199+
ret = CCDeviceiPad;
200+
}
201+
181202
}
182203
#elif __CC_PLATFORM_IOS
183204

cocos2d/Platforms/Android/CCActivity.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@ - (void)run
106106
screenMode = CCScreenScaledAspectFitEmulationMode;
107107
}
108108

109+
if([_cocos2dSetupConfig[CCSetupScreenOrientation] isEqual:CCScreenOrientationPortrait])
110+
{
111+
self.requestedOrientation = AndroidActivityInfoScreenOrientationSensorPortrait;
112+
}
113+
else if([_cocos2dSetupConfig[CCSetupScreenOrientation] isEqual:CCScreenOrientationLandscape])
114+
{
115+
self.requestedOrientation = AndroidActivityInfoScreenOrientationSensorLandscape;
116+
}
117+
else
118+
{
119+
self.requestedOrientation = AndroidActivityInfoScreenOrientationUnspecified;
120+
}
121+
109122
_glView = [[CCGLView alloc] initWithContext:self screenMode:screenMode scaleFactor:metrics.density];
110123
[metrics release];
111124
[_glView.holder addCallback:self];

external/ObjectAL/OALSimpleAudio.m

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -659,31 +659,11 @@ - (void) unloadAllEffects
659659
return nil;
660660
}
661661

662-
#if 0 // __CC_PLATFORM_ANDROID
663-
// TODO: we are creating a player and throwing it away each time, and not taking into account if it was preloaded.
664-
// think about checking for preload and implement the player's asyncPrepare finish listener
665-
AndroidAssetFileDescriptor *assetFd = [[[CCActivity currentActivity] assets] openFdWithFileName:filePath];
666-
667-
if (assetFd.fileDescriptor && [assetFd.fileDescriptor valid]) {
668-
AndroidMediaPlayer *player = [[AndroidMediaPlayer alloc] init];
669-
[player reset];
670-
[player setDataSource:assetFd.fileDescriptor offset:assetFd.startOffset length:assetFd.length];
671-
#warning TODO: note sure how to set on finished listener for player and call the player's release() (not objc release) in the callback to close fd
672-
[player prepare];
673-
[player start];
674-
}
675-
676-
if (assetFd) {
677-
[assetFd close];
678-
}
679-
// TODO: convert to ALSoundSource and return
680-
#else
681662
ALBuffer* buffer = [self internalPreloadEffect:filePath reduceToMono:NO];
682663
if(nil != buffer)
683664
{
684665
return [channel play:buffer gain:volume pitch:pitch pan:pan loop:loop];
685666
}
686-
#endif
687667
return nil;
688668
}
689669

0 commit comments

Comments
 (0)