UIEditBoxImpl-mac.mm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2012 Jozef Pridavok
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "platform/CCPlatformConfig.h"
  23. #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
  24. #include "ui/UIEditBox/UIEditBoxImpl-mac.h"
  25. #include "base/CCDirector.h"
  26. #include "base/ccUTF8.h"
  27. #include "ui/UIEditBox/UIEditBox.h"
  28. #include "ui/UIEditBox/Mac/CCUIEditBoxMac.h"
  29. NS_CC_BEGIN
  30. namespace ui {
  31. EditBoxImpl* __createSystemEditBox(EditBox* pEditBox)
  32. {
  33. return new EditBoxImplMac(pEditBox);
  34. }
  35. EditBoxImplMac::EditBoxImplMac(EditBox* pEditText)
  36. : EditBoxImplCommon(pEditText)
  37. , _sysEdit(nullptr)
  38. {
  39. //! TODO: Retina on Mac
  40. //! _inRetinaMode = [[CCEAGLView sharedEGLView] contentScaleFactor] == 2.0f ? true : false;
  41. _inRetinaMode = false;
  42. }
  43. EditBoxImplMac::~EditBoxImplMac()
  44. {
  45. [_sysEdit release];
  46. }
  47. void EditBoxImplMac::createNativeControl(const cocos2d::Rect &frame)
  48. {
  49. auto glview = cocos2d::Director::getInstance()->getOpenGLView();
  50. Size size = frame.size;
  51. NSRect rect = NSMakeRect(0, 0,
  52. size.width * glview->getScaleX(),
  53. size.height * glview->getScaleY());
  54. float factor = cocos2d::Director::getInstance()->getContentScaleFactor();
  55. rect.size.width /= factor;
  56. rect.size.height /= factor;
  57. _sysEdit = [[UIEditBoxImplMac alloc] initWithFrame:rect editBox:this];
  58. this->setNativeVisible(false);
  59. }
  60. NSFont* EditBoxImplMac::constructFont(const char *fontName, int fontSize)
  61. {
  62. NSString * fntName = [NSString stringWithUTF8String:fontName];
  63. fntName = [[fntName lastPathComponent] stringByDeletingPathExtension];
  64. float retinaFactor = _inRetinaMode ? 2.0f : 1.0f;
  65. auto glview = cocos2d::Director::getInstance()->getOpenGLView();
  66. float scaleFactor = glview->getScaleX();
  67. if (fontSize == -1)
  68. {
  69. NSRect frameRect = [_sysEdit.textInput frame];
  70. fontSize = frameRect.size.height*2/3;
  71. }
  72. else
  73. {
  74. fontSize = fontSize * scaleFactor / retinaFactor;
  75. }
  76. NSFont *textFont = nil;
  77. if (strlen(fontName) == 0)
  78. {
  79. textFont = [NSFont systemFontOfSize:fontSize];
  80. }
  81. else
  82. {
  83. textFont = [NSFont fontWithName:fntName size:fontSize];
  84. if (textFont == nil) {
  85. textFont = [NSFont systemFontOfSize:fontSize];
  86. }
  87. }
  88. return textFont;
  89. }
  90. void EditBoxImplMac::setNativeFont(const char *pFontName, int fontSize)
  91. {
  92. NSFont* textFont = constructFont(pFontName, fontSize);
  93. [_sysEdit setFont:textFont];
  94. }
  95. void EditBoxImplMac::setNativePlaceholderFont(const char *pFontName, int fontSize)
  96. {
  97. NSFont *textFont = constructFont(pFontName, fontSize);
  98. if (!textFont) {
  99. CCLOGWARN("Font not found: %s", pFontName);
  100. return;
  101. }
  102. [_sysEdit setPlaceholderFont:textFont];
  103. }
  104. void EditBoxImplMac::setNativeFontColor(const cocos2d::Color4B &color)
  105. {
  106. NSColor *newColor = [NSColor colorWithCalibratedRed:color.r / 255.0f
  107. green:color.g / 255.0f
  108. blue:color.b / 255.0f
  109. alpha:color.a / 255.f];
  110. [_sysEdit setTextColor:newColor];
  111. }
  112. void EditBoxImplMac::setNativePlaceholderFontColor(const cocos2d::Color4B &color)
  113. {
  114. NSColor *newColor = [NSColor colorWithCalibratedRed:color.r/255.f
  115. green:color.g / 255.f
  116. blue:color.b / 255.f
  117. alpha:color.a / 255.f];
  118. [_sysEdit setPlaceholderFontColor:newColor];
  119. }
  120. void EditBoxImplMac::setNativeInputMode(EditBox::InputMode inputMode)
  121. {
  122. [_sysEdit setInputMode:inputMode];
  123. auto oldPosition = _editBox->getPosition();
  124. _editBox->setPosition(_editBox->getPosition() + Vec2(10,10));
  125. _editBox->setPosition(oldPosition);
  126. }
  127. void EditBoxImplMac::setNativeMaxLength(int maxLength)
  128. {
  129. [_sysEdit setMaxLength:maxLength];
  130. }
  131. void EditBoxImplMac::setNativeInputFlag(EditBox::InputFlag inputFlag)
  132. {
  133. [_sysEdit setInputFlag:inputFlag];
  134. }
  135. void EditBoxImplMac::setNativeReturnType(EditBox::KeyboardReturnType returnType)
  136. {
  137. [_sysEdit setReturnType:returnType];
  138. }
  139. void EditBoxImplMac::setNativeTextHorizontalAlignment(cocos2d::TextHAlignment alignment)
  140. {
  141. [_sysEdit setTextHorizontalAlignment:alignment];
  142. }
  143. bool EditBoxImplMac::isEditing()
  144. {
  145. return [_sysEdit isEditState] ? true : false;
  146. }
  147. void EditBoxImplMac::setNativeText(const char *pText)
  148. {
  149. NSString *text = [NSString stringWithUTF8String:pText];
  150. [_sysEdit setText:text];
  151. }
  152. void EditBoxImplMac::setNativePlaceHolder(const char* pText)
  153. {
  154. [_sysEdit setPlaceHolder:pText];
  155. }
  156. void EditBoxImplMac::setNativeVisible(bool visible)
  157. {
  158. [_sysEdit setVisible:visible];
  159. }
  160. void EditBoxImplMac::updateNativeFrame(const cocos2d::Rect &rect)
  161. {
  162. GLView* eglView = Director::getInstance()->getOpenGLView();
  163. auto viewPortRect = eglView->getViewPortRect();
  164. // Coordinate System on OSX has its origin at the lower left corner.
  165. // https://developer.apple.com/library/ios/documentation/General/Conceptual/Devpedia-CocoaApp/CoordinateSystem.html
  166. auto screenPosY = viewPortRect.size.height - rect.origin.y - rect.size.height;
  167. [_sysEdit updateFrame:CGRectMake(rect.origin.x,
  168. screenPosY,
  169. rect.size.width, rect.size.height)];
  170. }
  171. const char* EditBoxImplMac::getNativeDefaultFontName()
  172. {
  173. return [[_sysEdit getDefaultFontName] UTF8String];
  174. }
  175. void EditBoxImplMac::nativeOpenKeyboard()
  176. {
  177. [_sysEdit setVisible:YES];
  178. [_sysEdit openKeyboard];
  179. }
  180. void EditBoxImplMac::nativeCloseKeyboard()
  181. {
  182. [_sysEdit closeKeyboard];
  183. }
  184. }
  185. NS_CC_END
  186. #endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)