UIEditBoxImpl-winrt.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /****************************************************************************
  2. Copyright (c) 2014 cocos2d-x.org
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __UIEditBoxIMPLWINRT_H__
  22. #define __UIEditBoxIMPLWINRT_H__
  23. #include "platform/CCPlatformConfig.h"
  24. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  25. #include "ui/UIEditBox/UIEditBoxImpl-common.h"
  26. using namespace Windows::UI::Xaml;
  27. using namespace Windows::UI::Xaml::Controls;
  28. using namespace Windows::UI::Xaml::Input;
  29. NS_CC_BEGIN
  30. namespace ui {
  31. ref class EditBoxWinRT sealed
  32. {
  33. public:
  34. EditBoxWinRT(Windows::Foundation::EventHandler<Platform::String^>^ beginHandler,
  35. Windows::Foundation::EventHandler<Platform::String^>^ changeHandler,
  36. Windows::Foundation::EventHandler<cocos2d::EndEventArgs^>^ endHandler);
  37. void closeKeyboard();
  38. bool isEditing();
  39. void openKeyboard();
  40. void setFontColor(Windows::UI::Color color);
  41. void setFontFamily(Platform::String^ fontFamily);
  42. void setFontSize(int fontSize);
  43. void setInputFlag(int inputFlags);
  44. void setInputMode(int inputMode);
  45. void setTextHorizontalAlignment(int alignment);
  46. void setMaxLength(int maxLength);
  47. void setPosition(Windows::Foundation::Rect rect);
  48. void setSize(Windows::Foundation::Size size);
  49. void setText(Platform::String^ text);
  50. void setVisible(bool visible);
  51. private:
  52. void EditBoxWinRT::onPasswordChanged(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ args);
  53. void EditBoxWinRT::onTextChanged(Platform::Object ^sender, Windows::UI::Xaml::Controls::TextChangedEventArgs ^e);
  54. void EditBoxWinRT::onKeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ args);
  55. void EditBoxWinRT::onGotFocus(Platform::Object ^sender, Windows::UI::Xaml::RoutedEventArgs ^args);
  56. void EditBoxWinRT::onLostFocus(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs ^args);
  57. Windows::UI::Xaml::Controls::Control^ createPasswordBox();
  58. Windows::UI::Xaml::Controls::Control^ createTextBox();
  59. void removeTextBox();
  60. void setInputScope(TextBox^ textBox);
  61. void _setTextHorizontalAlignment(TextBox^ textBox);
  62. void _setTexVerticalAlignment(Windows::UI::Xaml::Controls::Control^ textBox);
  63. void _setPadding(Windows::UI::Xaml::Controls::Control^ editBox);
  64. // Properties
  65. Windows::Foundation::Rect _rect;
  66. Windows::Foundation::Size _size;
  67. Windows::UI::Color _color;
  68. Platform::String^ _initialText;
  69. int _fontSize;
  70. Platform::String^ _fontFamily;
  71. int _alignment;
  72. int _inputMode;
  73. int _inputFlag;
  74. bool _password;
  75. bool _isEditing;
  76. bool _visible;
  77. bool _multiline;
  78. int _maxLength;
  79. // The actual edit box, however, could be a TextBox, PasswordBox, or a SearchBox (not yet implemented)
  80. Windows::UI::Xaml::Controls::Control^ _textBox = nullptr;
  81. Platform::Agile<Windows::UI::Core::CoreDispatcher> m_dispatcher = nullptr;
  82. Platform::Agile<Windows::UI::Xaml::Controls::Panel> m_panel = nullptr;
  83. Concurrency::critical_section _critical_section;
  84. Windows::Foundation::EventHandler<Platform::String^>^ _beginHandler = nullptr;
  85. Windows::Foundation::EventHandler<Platform::String^>^ _changeHandler = nullptr;
  86. Windows::Foundation::EventHandler<EndEventArgs^>^ _endHandler = nullptr;
  87. Windows::Foundation::EventRegistrationToken _unfocusToken;
  88. Windows::Foundation::EventRegistrationToken _changeToken;
  89. Windows::Foundation::EventRegistrationToken _focusToken;
  90. Windows::Foundation::EventRegistrationToken _keydownToken;
  91. };
  92. class CC_GUI_DLL UIEditBoxImplWinrt : public EditBoxImplCommon
  93. {
  94. public:
  95. /**
  96. * @js NA
  97. */
  98. UIEditBoxImplWinrt(EditBox* pEditText);
  99. /**
  100. * @js NA
  101. * @lua NA
  102. */
  103. virtual ~UIEditBoxImplWinrt() { };
  104. virtual bool isEditing() override { return _system_control->isEditing(); }
  105. virtual void createNativeControl(const Rect& frame) override { }
  106. virtual void setNativeFont(const char* pFontName, int fontSize) override;
  107. virtual void setNativeFontColor(const Color4B& color) override;
  108. virtual void setNativePlaceholderFont(const char* pFontName, int fontSize) override { CCLOG("Warning! You can't change WinRT placeholder font"); }
  109. virtual void setNativePlaceholderFontColor(const Color4B& color) override { CCLOG("Warning! You can't change WinRT placeholder font color"); }
  110. virtual void setNativeInputMode(EditBox::InputMode inputMode) override;
  111. virtual void setNativeInputFlag(EditBox::InputFlag inputFlag) override;
  112. virtual void setNativeReturnType(EditBox::KeyboardReturnType returnType) override { CCLOG("Warning! You can't change WinRT return type"); }
  113. virtual void setNativeTextHorizontalAlignment(cocos2d::TextHAlignment alignment);
  114. virtual void setNativeText(const char* pText) override;
  115. virtual void setNativePlaceHolder(const char* pText) override { CCLOG("Warning! You can't change WinRT placeholder text"); }
  116. virtual void setNativeVisible(bool visible) override;
  117. virtual void updateNativeFrame(const Rect& rect) override; // TODO
  118. virtual const char* getNativeDefaultFontName() override { return "Segoe UI"; }
  119. virtual void nativeOpenKeyboard();
  120. virtual void nativeCloseKeyboard() override;
  121. virtual void setNativeMaxLength(int maxLength) override;
  122. private:
  123. cocos2d::Vec2 convertDesignCoordToXamlCoord(const cocos2d::Vec2& designCoord);
  124. virtual void doAnimationWhenKeyboardMove(float duration, float distance) override { CCLOG("Warning! doAnimationWhenKeyboardMove not supported on WinRT"); }
  125. EditBoxWinRT^ _system_control;
  126. int _fontSize;
  127. };
  128. } // namespace ui
  129. NS_CC_END
  130. #endif // CC_PLATFORM_WINRT
  131. #endif // #ifdef __UIEditBoxIMPLWINRT_H__