1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
| #include "EditBox.h" #include "cocos/bindings/jswrapper/SeApi.h" #include "cocos/bindings/manual/jsb_global.h" #include "engine/EngineEvents.h" #import <UIKit/UIKit.h>
#define ITEM_MARGIN_WIDTH 0 #define ITEM_MARGIN_HEIGHT 10 #define TEXT_LINE_HEIGHT 40 #define TEXT_VIEW_MAX_LINE_SHOWN 1.5
const bool INPUTBOX_HIDDEN = true;
@interface EditboxManager : NSObject + (instancetype)sharedInstance; - (void)show:(const cc::EditBox::ShowInfo*)showInfo; - (void)hide; - (UIView*)getCurrentViewInUse; - (NSString*)getCurrentText; @end
@interface TextFieldDelegate : NSObject <UITextFieldDelegate> - (id)initWithPairs:(UITextField*)inputOnView and:(UITextField*)inputOnToolbar; - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; - (void)textFieldDidChange:(UITextField *)textField; - (BOOL)textFieldShouldReturn:(UITextField *)textField; @end
@interface TextViewDelegate : NSObject <UITextViewDelegate> - (id)initWithPairs:(UITextView*)inputOnView and:(UITextView*)inputOnToolbar; - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text; - (void)textViewDidChange:(UITextView *)textView; @end
namespace { static bool g_isMultiline{false}; static int g_maxLength{INT_MAX}; se::Value textInputCallback;
void getTextInputCallback() { if (!textInputCallback.isUndefined()) { return; }
auto global = se::ScriptEngine::getInstance()->getGlobalObject(); se::Value jsbVal; if (global->getProperty("jsb", &jsbVal) && jsbVal.isObject()) { jsbVal.toObject()->getProperty("onTextInput", &textInputCallback); se::ScriptEngine::getInstance()->addBeforeCleanupHook([]() { textInputCallback.setUndefined(); }); } }
void callJSFunc(const ccstd::string &type, const ccstd::string &text) { getTextInputCallback();
if (textInputCallback.isUndefined() || !textInputCallback.isObject()) { return; }
se::AutoHandleScope scope; se::ValueArray args; args.push_back(se::Value(type)); args.push_back(se::Value(text)); textInputCallback.toObject()->call(args, nullptr); }
int getTextInputHeight(bool isMultiLine) { if (isMultiLine) { return TEXT_LINE_HEIGHT * TEXT_VIEW_MAX_LINE_SHOWN; } return TEXT_LINE_HEIGHT; }
void setTextFieldKeyboardType(UITextField *textField, const ccstd::string &inputType) { if (0 == inputType.compare("password")) { textField.secureTextEntry = YES; textField.keyboardType = UIKeyboardTypeDefault; } else { textField.secureTextEntry = NO; if (0 == inputType.compare("email")) { textField.keyboardType = UIKeyboardTypeEmailAddress; } else if (0 == inputType.compare("number")) { textField.keyboardType = UIKeyboardTypeDecimalPad; } else if (0 == inputType.compare("url")) { textField.keyboardType = UIKeyboardTypeURL; } else { textField.keyboardType = UIKeyboardTypeDefault; } } }
void setTextFieldReturnType(UITextField *textField, const ccstd::string &returnType) { if (0 == returnType.compare("done")) { textField.returnKeyType = UIReturnKeyDone; } else if (0 == returnType.compare("next")) { textField.returnKeyType = UIReturnKeyNext; } else if (0 == returnType.compare("search")) { textField.returnKeyType = UIReturnKeySearch; } else if (0 == returnType.compare("go")) { textField.returnKeyType = UIReturnKeyGo; } else if (0 == returnType.compare("send")) { textField.returnKeyType = UIReturnKeySend; } else { textField.returnKeyType = UIReturnKeyDone; } }
CGRect getSafeAreaRect() { UIView *view = UIApplication.sharedApplication.delegate.window.rootViewController.view; CGRect viewRect = view.frame;
if (@available(iOS 11.0, *)) { auto safeAreaInsets = view.safeAreaInsets;
UIInterfaceOrientation orient = [UIApplication sharedApplication].statusBarOrientation; if (UIInterfaceOrientationLandscapeLeft == orient) { viewRect.origin.x = 0; viewRect.size.width -= safeAreaInsets.right; } else { viewRect.origin.x += safeAreaInsets.left; viewRect.size.width -= safeAreaInsets.left; } }
return viewRect; }
NSString* getTextInputType(const cc::EditBox::ShowInfo* showInfo) { return showInfo->isMultiline ? @"textView" : @"textField"; } }
@implementation TextViewDelegate { UITextView* tViewOnView; UITextView* tViewOnToolbar; }
- (id)initWithPairs:(UITextView*)inputOnView and:(UITextView*)inputOnToolbar { if (self = [super init]) { tViewOnView = inputOnView; tViewOnToolbar = inputOnToolbar; } return self; }
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { return YES; }
- (void)textViewDidChange:(UITextView *)textView { if (textView.markedTextRange != nil) { return; }
if (textView.text.length > g_maxLength) { NSRange rangeIndex = [textView.text rangeOfComposedCharacterSequenceAtIndex:g_maxLength]; textView.text = [textView.text substringToIndex:rangeIndex.location]; }
tViewOnView.text = textView.text; tViewOnToolbar.text = textView.text; callJSFunc("input", [textView.text UTF8String]); }
@end
@implementation TextFieldDelegate { UITextField* textFieldOnView; UITextField* textFieldOnToolbar; }
- (id)initWithPairs:(UITextField*)inputOnView and:(UITextField*)inputOnToolbar { if (self = [super init]) { textFieldOnView = inputOnView; textFieldOnToolbar = inputOnToolbar; } return self; }
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { return YES; }
- (void)textFieldDidChange:(UITextField *)textField { if (textField.markedTextRange != nil) { return; }
if (textField.text.length > g_maxLength) { NSRange rangeIndex = [textField.text rangeOfComposedCharacterSequenceAtIndex:g_maxLength]; textField.text = [textField.text substringToIndex:rangeIndex.location]; }
textFieldOnView.text = textField.text; textFieldOnToolbar.text = textField.text; callJSFunc("input", [textField.text UTF8String]); }
- (BOOL)textFieldShouldReturn:(UITextField *)textField { cc::EditBox::complete(); return YES; }
@end
@interface InputBoxPair : NSObject @property(readwrite, retain) id inputOnView; @property(readwrite, retain) id inputOnToolbar; @property(readwrite, retain) id inputDelegate; @end
@implementation InputBoxPair - (void)dealloc { [_inputOnView release]; [_inputOnToolbar release]; [_inputDelegate release]; [super dealloc]; } @end
@implementation EditboxManager { NSMutableDictionary<NSString*, InputBoxPair*>* textInputDictionnary; InputBoxPair* curView;
cc::events::Resize::Listener resizeListener; cc::events::Touch::Listener touchListener; cc::events::Close::Listener closeListener; }
static EditboxManager *instance = nil;
+ (instancetype)sharedInstance { static dispatch_once_t pred = 0; dispatch_once(&pred, ^{ instance = [[super allocWithZone:NULL] init]; if (instance == nil) { CC_LOG_ERROR("Editbox manager init failed, plz check if you have enough space left"); } }); return instance; }
+ (id)allocWithZone:(struct _NSZone*)zone { return [EditboxManager sharedInstance]; }
- (id)copyWithZone:(struct _NSZone*)zone { return [EditboxManager sharedInstance]; }
- (void)onOrientationChanged { cc::EditBox::complete(); }
- (id)init { if (self = [super init]) { textInputDictionnary = [NSMutableDictionary new]; if (textInputDictionnary == nil) { [self release]; return nil; }
resizeListener.bind([&](int , int , uint32_t ) { [[EditboxManager sharedInstance] onOrientationChanged]; });
touchListener.bind([&](const cc::TouchEvent& event) { if (event.type == cc::TouchEvent::Type::BEGAN) { cc::EditBox::complete(); } });
closeListener.bind([&]() { [[EditboxManager sharedInstance] dealloc]; }); } return self; }
- (void)dealloc { [textInputDictionnary release]; [super dealloc]; }
- (UIView *)createAccessoryContainerWithHeight:(CGFloat)height { CGRect safeRect = getSafeAreaRect(); UIView *container = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, safeRect.size.width, height)] autorelease]; container.backgroundColor = [UIColor darkGrayColor]; return container; }
- (void)addInputAccessoryViewForTextView:(InputBoxPair*)inputbox with:(const cc::EditBox::ShowInfo*)showInfo { CGFloat height = getTextInputHeight(g_isMultiline) + ITEM_MARGIN_HEIGHT; UIView *container = [self createAccessoryContainerWithHeight:height]; CGFloat width = container.frame.size.width;
UITextView* textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, width, getTextInputHeight(g_isMultiline))]; textView.textColor = [UIColor blackColor]; textView.backgroundColor = [UIColor whiteColor]; textView.layer.cornerRadius = 0.0; textView.clipsToBounds = YES; textView.textContainerInset = UIEdgeInsetsMake(8, 8, 8, 8); textView.text = [NSString stringWithUTF8String:showInfo->defaultValue.c_str()];
TextViewDelegate* delegate = [[TextViewDelegate alloc] initWithPairs:[inputbox inputOnView] and:textView]; inputbox.inputDelegate = delegate; textView.delegate = delegate;
[container addSubview:textView]; ((UITextView*)[inputbox inputOnView]).inputAccessoryView = container; [inputbox setInputOnToolbar:textView];
[textView release]; }
- (void)addInputAccessoryViewForTextField:(InputBoxPair*)inputbox with:(const cc::EditBox::ShowInfo*)showInfo { CGFloat height = TEXT_LINE_HEIGHT + ITEM_MARGIN_HEIGHT; UIView *container = [self createAccessoryContainerWithHeight:height]; CGFloat width = container.frame.size.width;
UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, width, TEXT_LINE_HEIGHT)]; textField.borderStyle = UITextBorderStyleNone; textField.textColor = [UIColor blackColor]; textField.backgroundColor = [UIColor whiteColor]; textField.clearButtonMode = UITextFieldViewModeWhileEditing; textField.leftView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 1)] autorelease]; textField.leftViewMode = UITextFieldViewModeAlways; textField.text = [NSString stringWithUTF8String:showInfo->defaultValue.c_str()];
TextFieldDelegate* delegate = [[TextFieldDelegate alloc] initWithPairs:[inputbox inputOnView] and:textField]; textField.delegate = delegate; inputbox.inputDelegate = delegate; [textField addTarget:delegate action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
[container addSubview:textField]; ((UITextField*)[inputbox inputOnView]).inputAccessoryView = container; [inputbox setInputOnToolbar:textField];
[textField release]; }
- (id)createTextView:(const cc::EditBox::ShowInfo *)showInfo { InputBoxPair* ret; CGRect viewRect = UIApplication.sharedApplication.delegate.window.rootViewController.view.frame; NSString* inputType = getTextInputType(showInfo);
if ((ret = [textInputDictionnary objectForKey:inputType])) { [[ret inputOnView] setFrame:CGRectMake(showInfo->x, viewRect.size.height - showInfo->y - showInfo->height, showInfo->width, showInfo->height)];
UIView *accessoryView = [[ret inputOnView] inputAccessoryView]; CGRect safeRect = getSafeAreaRect(); accessoryView.frame = CGRectMake(0, 0, safeRect.size.width, getTextInputHeight(g_isMultiline) + ITEM_MARGIN_HEIGHT); ((UITextView*)[ret inputOnToolbar]).frame = CGRectMake(0, 0, safeRect.size.width, getTextInputHeight(g_isMultiline)); } else { ret = [[InputBoxPair alloc] init]; [ret setInputOnView:[[UITextView alloc] initWithFrame:CGRectMake(showInfo->x, viewRect.size.height - showInfo->y - showInfo->height, showInfo->width, showInfo->height)]]; [textInputDictionnary setValue:ret forKey:inputType]; [self addInputAccessoryViewForTextView:ret with:showInfo]; }
((UITextView*)[ret inputOnToolbar]).text = [NSString stringWithUTF8String:showInfo->defaultValue.c_str()]; ((UITextView*)[ret inputOnView]).text = [NSString stringWithUTF8String:showInfo->defaultValue.c_str()]; return ret; }
- (id)createTextField:(const cc::EditBox::ShowInfo*)showInfo { InputBoxPair* ret; CGRect viewRect = UIApplication.sharedApplication.delegate.window.rootViewController.view.frame; NSString* inputType = getTextInputType(showInfo);
if ((ret = [textInputDictionnary objectForKey:inputType])) { [[ret inputOnView] setFrame:CGRectMake(showInfo->x, viewRect.size.height - showInfo->y - showInfo->height, showInfo->width, showInfo->height)];
UIView *accessoryView = [[ret inputOnView] inputAccessoryView]; CGRect safeRect = getSafeAreaRect(); accessoryView.frame = CGRectMake(0, 0, safeRect.size.width, TEXT_LINE_HEIGHT + ITEM_MARGIN_HEIGHT); ((UITextField*)[ret inputOnToolbar]).frame = CGRectMake(0, 0, safeRect.size.width, TEXT_LINE_HEIGHT); } else { ret = [[InputBoxPair alloc] init]; [ret setInputOnView:[[UITextField alloc] initWithFrame:CGRectMake(showInfo->x, viewRect.size.height - showInfo->y - showInfo->height, showInfo->width, showInfo->height)]]; [textInputDictionnary setValue:ret forKey:inputType]; [self addInputAccessoryViewForTextField:ret with:showInfo]; }
((UITextField*)[ret inputOnToolbar]).text = [NSString stringWithUTF8String:showInfo->defaultValue.c_str()]; ((UITextField*)[ret inputOnView]).text = [NSString stringWithUTF8String:showInfo->defaultValue.c_str()]; setTextFieldReturnType((UITextField*)[ret inputOnToolbar], showInfo->confirmType); setTextFieldKeyboardType((UITextField*)[ret inputOnToolbar], showInfo->inputType);
return ret; }
- (void)show:(const cc::EditBox::ShowInfo*)showInfo { g_maxLength = showInfo->maxLength; g_isMultiline = showInfo->isMultiline;
if (g_isMultiline) { curView = [self createTextView:showInfo]; } else { curView = [self createTextField:showInfo]; }
[[curView inputOnView] setHidden:INPUTBOX_HIDDEN]; UIView *view = UIApplication.sharedApplication.delegate.window.rootViewController.view;
[view addSubview:[curView inputOnView]]; [[curView inputOnView] becomeFirstResponder]; [[curView inputOnToolbar] becomeFirstResponder]; }
- (void)hide { [[curView inputOnView] becomeFirstResponder]; [[curView inputOnView] removeFromSuperview]; [[curView inputOnToolbar] resignFirstResponder]; [[curView inputOnView] resignFirstResponder]; }
- (InputBoxPair*)getCurrentViewInUse { return curView; }
- (NSString*)getCurrentText { if (g_isMultiline) { return [(UITextView*)[curView inputOnToolbar] text]; } return [(UITextField*)[curView inputOnToolbar] text]; }
@end
namespace cc {
bool EditBox::_isShown = false;
void EditBox::show(const cc::EditBox::ShowInfo &showInfo) { [[EditboxManager sharedInstance] show:&showInfo]; EditBox::_isShown = true; }
void EditBox::hide() { [[EditboxManager sharedInstance] hide]; EditBox::_isShown = false; }
bool EditBox::complete() { if (!EditBox::_isShown) { return true; } NSString *text = [[EditboxManager sharedInstance] getCurrentText]; callJSFunc("complete", [text UTF8String]); EditBox::hide(); return true; }
}
|