2023-03-11 22:10:18 +08:00
|
|
|
///****************************************************************************
|
|
|
|
//Copyright (c) 2014 cocos2d-x.org
|
|
|
|
//Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
//
|
2023-03-12 01:44:55 +08:00
|
|
|
//https://axmolengine.github.io/
|
2023-03-11 22:10:18 +08:00
|
|
|
//
|
|
|
|
//* Portions Copyright (c) Microsoft Open Technologies, Inc.
|
|
|
|
//* All Rights Reserved
|
|
|
|
//
|
|
|
|
//Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
//of this software and associated documentation files (the "Software"), to deal
|
|
|
|
//in the Software without restriction, including without limitation the rights
|
|
|
|
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
//copies of the Software, and to permit persons to whom the Software is
|
|
|
|
//furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
//The above copyright notice and this permission notice shall be included in
|
|
|
|
//all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
//THE SOFTWARE.
|
|
|
|
//****************************************************************************/
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "platform/PlatformConfig.h"
|
2023-03-11 22:10:18 +08:00
|
|
|
#if (AX_TARGET_PLATFORM == AX_PLATFORM_WINRT)
|
|
|
|
#include "ui/UIEditBox/UIEditBoxImpl-winrt.h"
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "platform/winrt/WinRTUtils.h"
|
|
|
|
#include "platform/winrt/GLViewImpl-winrt.h"
|
|
|
|
#include "2d/FontFreeType.h"
|
2023-03-11 22:10:18 +08:00
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
#include <winrt/Windows.UI.Xaml.Input.h>
|
|
|
|
#include <winrt/Windows.UI.ViewManagement.h>
|
|
|
|
#include <winrt/Windows.Foundation.Collections.h>
|
|
|
|
#include <winrt/Windows.UI.Xaml.Media.h>
|
|
|
|
|
2023-03-11 22:10:18 +08:00
|
|
|
#if defined(WINAPI_FAMILY) && WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
|
|
|
|
#define XAML_TOP_PADDING 10.0f
|
|
|
|
#else
|
|
|
|
#define XAML_TOP_PADDING 0.0f
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define EDIT_BOX_PADDING 5.0f
|
|
|
|
|
|
|
|
namespace ax {
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
static const winrt::hstring EDIT_BOX_XAML_NAME = L"axmol_editbox";
|
|
|
|
static const winrt::hstring CANVAS_XAML_NAME = L"axmol_canvas";
|
2023-03-11 22:10:18 +08:00
|
|
|
|
|
|
|
EditBoxImpl* __createSystemEditBox(EditBox* pEditBox)
|
|
|
|
{
|
|
|
|
return new UIEditBoxImplWinrt(pEditBox);
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
EditBoxWinRT::EditBoxWinRT(
|
|
|
|
winrt::delegate<Windows::Foundation::IInspectable const&, winrt::hstring const&> const& beginHandler,
|
|
|
|
winrt::delegate<Windows::Foundation::IInspectable const&, winrt::hstring const&> const& changeHandler,
|
|
|
|
winrt::delegate<Windows::Foundation::IInspectable const&, ax::EndEventArgs const&> const& endHandler)
|
|
|
|
:
|
2023-03-11 22:10:18 +08:00
|
|
|
_beginHandler(beginHandler),
|
|
|
|
_changeHandler(changeHandler),
|
|
|
|
_endHandler(endHandler),
|
|
|
|
_alignment(),
|
|
|
|
_initialText(L""),
|
|
|
|
_fontFamily(L"Segoe UI"),
|
|
|
|
_fontSize(12),
|
|
|
|
_password(false),
|
|
|
|
_isEditing(false),
|
|
|
|
_multiline(false),
|
|
|
|
_maxLength(0 /* unlimited */)
|
|
|
|
{
|
|
|
|
m_dispatcher = ax::GLViewImpl::sharedOpenGLView()->getDispatcher();
|
|
|
|
m_panel = ax::GLViewImpl::sharedOpenGLView()->getPanel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxWinRT::closeKeyboard()
|
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
m_dispatcher.get().RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [this]() {
|
2023-03-11 22:10:18 +08:00
|
|
|
removeTextBox();
|
|
|
|
_textBox = nullptr;
|
2023-09-26 00:11:33 +08:00
|
|
|
auto canvas = findXamlElement(m_panel.get(), CANVAS_XAML_NAME).as<Canvas>();
|
|
|
|
canvas.Visibility(Visibility::Collapsed);
|
|
|
|
});
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
Windows::UI::Xaml::Controls::Control EditBoxWinRT::createPasswordBox()
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
auto passwordBox = PasswordBox();
|
|
|
|
passwordBox.BorderThickness(Windows::UI::Xaml::Thickness{0});
|
|
|
|
passwordBox.Name(EDIT_BOX_XAML_NAME);
|
|
|
|
passwordBox.Width(_size.Width);
|
|
|
|
passwordBox.Height(_size.Height);
|
|
|
|
passwordBox.Foreground(Media::SolidColorBrush(_color));
|
|
|
|
passwordBox.FontSize(_fontSize);
|
|
|
|
passwordBox.FontFamily(Media::FontFamily(_fontFamily));
|
|
|
|
passwordBox.MaxLength(_maxLength);
|
|
|
|
passwordBox.Password(_initialText);
|
|
|
|
_changeToken = passwordBox.PasswordChanged({this, &ax::ui::EditBoxWinRT::onPasswordChanged});
|
2023-03-11 22:10:18 +08:00
|
|
|
return passwordBox;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
Windows::UI::Xaml::Controls::Control EditBoxWinRT::createTextBox()
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
auto textBox = TextBox();
|
|
|
|
textBox.BorderThickness(Windows::UI::Xaml::Thickness{0});
|
|
|
|
textBox.Name(EDIT_BOX_XAML_NAME);
|
|
|
|
textBox.Width(_size.Width);
|
|
|
|
textBox.Height(_size.Height);
|
|
|
|
textBox.Foreground(Media::SolidColorBrush(_color));
|
|
|
|
textBox.FontSize(_fontSize);
|
|
|
|
textBox.FontFamily(Media::FontFamily(_fontFamily));
|
|
|
|
textBox.MaxLength(_maxLength);
|
|
|
|
textBox.AcceptsReturn(_multiline);
|
|
|
|
textBox.TextWrapping(_multiline ? TextWrapping::Wrap : TextWrapping::NoWrap);
|
|
|
|
textBox.Text(_initialText);
|
2023-03-11 22:10:18 +08:00
|
|
|
setInputScope(textBox);
|
|
|
|
_setTextHorizontalAlignment(textBox);
|
2023-09-26 00:11:33 +08:00
|
|
|
_changeToken = textBox.TextChanged({this, &ax::ui::EditBoxWinRT::onTextChanged});
|
2023-03-11 22:10:18 +08:00
|
|
|
return textBox;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
void EditBoxWinRT::onPasswordChanged(Windows::Foundation::IInspectable const& sender,
|
|
|
|
Windows::UI::Xaml::RoutedEventArgs const& args)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
onTextChanged(sender, nullptr);
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
void EditBoxWinRT::onTextChanged(Windows::Foundation::IInspectable const& sender,
|
|
|
|
Windows::UI::Xaml::Controls::TextChangedEventArgs const& e)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
winrt::hstring text = L"";
|
2023-03-11 22:10:18 +08:00
|
|
|
if (_password) {
|
2023-09-26 00:11:33 +08:00
|
|
|
text = _textBox.as<PasswordBox>().Password();
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
else {
|
2023-09-26 00:11:33 +08:00
|
|
|
text = _textBox.as<TextBox>().Text();
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
2023-09-26 00:11:33 +08:00
|
|
|
std::shared_ptr<ax::InputEvent> inputEvent(new UIEditBoxEvent(*this, text, _changeHandler));
|
2023-03-11 22:10:18 +08:00
|
|
|
ax::GLViewImpl::sharedOpenGLView()->QueueEvent(inputEvent);
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
void EditBoxWinRT::onKeyDown(Windows::Foundation::IInspectable const& sender,
|
|
|
|
Windows::UI::Xaml::Input::KeyRoutedEventArgs const& args)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
if (args.Key() == Windows::System::VirtualKey::Enter && !_multiline)
|
|
|
|
{
|
2023-03-11 22:10:18 +08:00
|
|
|
onLostFocus(nullptr, args);
|
|
|
|
}
|
2023-09-26 00:11:33 +08:00
|
|
|
else if (args.Key() == Windows::System::VirtualKey::Tab)
|
|
|
|
{
|
2023-03-11 22:10:18 +08:00
|
|
|
onLostFocus(nullptr, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
void EditBoxWinRT::onGotFocus(Windows::Foundation::IInspectable const& sender,
|
|
|
|
Windows::UI::Xaml::RoutedEventArgs const& args)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
Concurrency::critical_section::scoped_lock lock(_critical_section);
|
2023-09-26 00:11:33 +08:00
|
|
|
std::shared_ptr<ax::InputEvent> inputEvent(new UIEditBoxEvent(*this, winrt::hstring{}, _beginHandler));
|
2023-03-11 22:10:18 +08:00
|
|
|
ax::GLViewImpl::sharedOpenGLView()->QueueEvent(inputEvent);
|
|
|
|
_isEditing = true;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
void EditBoxWinRT::onLostFocus(Windows::Foundation::IInspectable const& sender,
|
|
|
|
Windows::UI::Xaml::RoutedEventArgs const& args)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
EditBoxDelegate::EditBoxEndAction action = EditBoxDelegate::EditBoxEndAction::UNKNOWN;
|
2023-09-26 00:11:33 +08:00
|
|
|
Windows::UI::Xaml::Input::KeyRoutedEventArgs keyArgs = args.try_as<Windows::UI::Xaml::Input::KeyRoutedEventArgs>();
|
2023-03-11 22:10:18 +08:00
|
|
|
if (keyArgs) {
|
2023-09-26 00:11:33 +08:00
|
|
|
if (keyArgs.Key() == Windows::System::VirtualKey::Enter && !_multiline)
|
|
|
|
{
|
2023-03-11 22:10:18 +08:00
|
|
|
action = EditBoxDelegate::EditBoxEndAction::RETURN;
|
|
|
|
}
|
2023-09-26 00:11:33 +08:00
|
|
|
else if (keyArgs.Key() == Windows::System::VirtualKey::Tab)
|
|
|
|
{
|
2023-03-11 22:10:18 +08:00
|
|
|
action = EditBoxDelegate::EditBoxEndAction::TAB_TO_NEXT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_isEditing = false;
|
|
|
|
Concurrency::critical_section::scoped_lock lock(_critical_section);
|
2023-09-26 00:11:33 +08:00
|
|
|
winrt::hstring text = L"";
|
2023-03-11 22:10:18 +08:00
|
|
|
if (_password) {
|
2023-09-26 00:11:33 +08:00
|
|
|
text = _textBox.as<PasswordBox>().Password();
|
|
|
|
_textBox.as <PasswordBox>().PasswordChanged(_changeToken);
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
else {
|
2023-09-26 00:11:33 +08:00
|
|
|
text = _textBox.as<TextBox>().Text();
|
|
|
|
_textBox.as<TextBox>().TextChanged(_changeToken);
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
std::shared_ptr<ax::InputEvent> inputEvent(new UIEditBoxEndEvent(*this, text, static_cast<int>(action), _endHandler));
|
2023-03-11 22:10:18 +08:00
|
|
|
ax::GLViewImpl::sharedOpenGLView()->QueueEvent(inputEvent);
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
_textBox.LostFocus(_unfocusToken);
|
|
|
|
_textBox.GotFocus(_focusToken);
|
|
|
|
_textBox.KeyDown(_keydownToken);
|
2023-03-11 22:10:18 +08:00
|
|
|
closeKeyboard();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditBoxWinRT::isEditing() {
|
|
|
|
return _isEditing;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxWinRT::openKeyboard()
|
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
m_dispatcher.get().RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [this]() {
|
2023-03-11 22:10:18 +08:00
|
|
|
removeTextBox();
|
2023-09-26 00:11:33 +08:00
|
|
|
Canvas canvas = findXamlElement(m_panel.get(), CANVAS_XAML_NAME).as<Canvas>();
|
2023-03-11 22:10:18 +08:00
|
|
|
|
|
|
|
if (_password) {
|
|
|
|
_textBox = createPasswordBox();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_textBox = createTextBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Position the text box
|
2023-09-26 00:11:33 +08:00
|
|
|
canvas.SetLeft(_textBox, _rect.X);
|
|
|
|
canvas.SetTop(_textBox, _rect.Y - XAML_TOP_PADDING);
|
2023-03-11 22:10:18 +08:00
|
|
|
|
|
|
|
_setTexVerticalAlignment(_textBox);
|
|
|
|
_setPadding(_textBox);
|
|
|
|
|
|
|
|
// Finally, insert it into the XAML scene hierarchy and make the containing canvas visible
|
2023-09-26 00:11:33 +08:00
|
|
|
canvas.Children().InsertAt(0, _textBox);
|
|
|
|
canvas.Background(Media::SolidColorBrush());
|
|
|
|
canvas.Visibility(Visibility::Visible);
|
|
|
|
_keydownToken = _textBox.KeyDown({this, &ax::ui::EditBoxWinRT::onKeyDown});
|
|
|
|
_focusToken = _textBox.GotFocus({this, &ax::ui::EditBoxWinRT::onGotFocus});
|
|
|
|
_unfocusToken = _textBox.LostFocus({this, &ax::ui::EditBoxWinRT::onLostFocus});
|
|
|
|
|
|
|
|
_textBox.Focus(FocusState::Programmatic);
|
2023-03-11 22:10:18 +08:00
|
|
|
if (_password) {
|
2023-09-26 00:11:33 +08:00
|
|
|
_textBox.as<PasswordBox>().SelectAll();
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
else {
|
2023-09-26 00:11:33 +08:00
|
|
|
_textBox.as<TextBox>().Select(_initialText.size(), 0);
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
auto inputPane = Windows::UI::ViewManagement::InputPane::GetForCurrentView();
|
2023-09-26 00:11:33 +08:00
|
|
|
});
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
void EditBoxWinRT::setFontColor(Windows::UI::Color const& color)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
_color = color;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
void EditBoxWinRT::setFontFamily(winrt::hstring const& fontFamily)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
_fontFamily = fontFamily;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxWinRT::setFontSize(int fontSize)
|
|
|
|
{
|
|
|
|
_fontSize = fontSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxWinRT::removeTextBox()
|
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
auto canvas = findXamlElement(m_panel.get(), CANVAS_XAML_NAME);
|
2023-03-11 22:10:18 +08:00
|
|
|
auto box = findXamlElement(canvas, EDIT_BOX_XAML_NAME);
|
|
|
|
removeXamlElement(canvas, box);
|
|
|
|
_isEditing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxWinRT::setInputFlag(int inputFlags) {
|
|
|
|
_password = false;
|
|
|
|
switch ((EditBox::InputFlag)inputFlags) {
|
|
|
|
case EditBox::InputFlag::PASSWORD:
|
|
|
|
_password = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
AXLOG("Warning: cannot set INITIAL_CAPS_* input flags for WinRT edit boxes");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxWinRT::setInputMode(int inputMode) {
|
|
|
|
_multiline = (EditBox::InputMode)inputMode == EditBox::InputMode::ANY;
|
|
|
|
_inputMode = inputMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxWinRT::setTextHorizontalAlignment(int alignment) {
|
|
|
|
_alignment = alignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxWinRT::setMaxLength(int maxLength) {
|
|
|
|
_maxLength = maxLength;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
void EditBoxWinRT::_setTextHorizontalAlignment(TextBox textBox)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
switch (_alignment) {
|
|
|
|
default:
|
|
|
|
case 0:
|
2023-09-26 00:11:33 +08:00
|
|
|
textBox.TextAlignment(TextAlignment::Left);
|
2023-03-11 22:10:18 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
2023-09-26 00:11:33 +08:00
|
|
|
textBox.TextAlignment(TextAlignment::Center);
|
2023-03-11 22:10:18 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
2023-09-26 00:11:33 +08:00
|
|
|
textBox.TextAlignment(TextAlignment::Right);
|
2023-03-11 22:10:18 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
void EditBoxWinRT::_setTexVerticalAlignment(Windows::UI::Xaml::Controls::Control textBox) {
|
|
|
|
textBox.VerticalAlignment(_multiline ? VerticalAlignment::Top : VerticalAlignment::Center);
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
void EditBoxWinRT::_setPadding(Windows::UI::Xaml::Controls::Control editBox)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
double padding = EDIT_BOX_PADDING*ax::Director::getInstance()->getOpenGLView()->getScaleX();
|
2023-03-11 22:10:18 +08:00
|
|
|
if (_multiline) {
|
2023-09-26 00:11:33 +08:00
|
|
|
editBox.Padding(Thickness{padding, padding, 0.0, 0.0});
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
else {
|
2023-09-26 00:11:33 +08:00
|
|
|
editBox.Padding(Thickness{padding, 0.0, 0.0, 0.0});
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
void EditBoxWinRT::setInputScope(TextBox textBox)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
InputScope inputScope = InputScope();
|
|
|
|
InputScopeName name = InputScopeName();
|
2023-03-11 22:10:18 +08:00
|
|
|
|
|
|
|
switch ((EditBox::InputMode)_inputMode) {
|
|
|
|
case EditBox::InputMode::SINGLE_LINE:
|
|
|
|
case EditBox::InputMode::ANY:
|
2023-09-26 00:11:33 +08:00
|
|
|
name.NameValue(InputScopeNameValue::Default);
|
2023-03-11 22:10:18 +08:00
|
|
|
break;
|
|
|
|
case EditBox::InputMode::EMAIL_ADDRESS:
|
2023-09-26 00:11:33 +08:00
|
|
|
name.NameValue(InputScopeNameValue::EmailSmtpAddress);
|
2023-03-11 22:10:18 +08:00
|
|
|
break;
|
|
|
|
case EditBox::InputMode::DECIMAL:
|
|
|
|
case EditBox::InputMode::NUMERIC:
|
2023-09-26 00:11:33 +08:00
|
|
|
name.NameValue(InputScopeNameValue::Number);
|
2023-03-11 22:10:18 +08:00
|
|
|
break;
|
|
|
|
case EditBox::InputMode::PHONE_NUMBER:
|
2023-09-26 00:11:33 +08:00
|
|
|
name.NameValue(InputScopeNameValue::TelephoneNumber);
|
2023-03-11 22:10:18 +08:00
|
|
|
break;
|
|
|
|
case EditBox::InputMode::URL:
|
2023-09-26 00:11:33 +08:00
|
|
|
name.NameValue(InputScopeNameValue::Url);
|
2023-03-11 22:10:18 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
textBox.InputScope(nullptr);
|
|
|
|
inputScope.Names().Append(name);
|
|
|
|
textBox.InputScope(inputScope);
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
void EditBoxWinRT::setPosition(Windows::Foundation::Rect const& rect)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
_rect = rect;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
void EditBoxWinRT::setSize(Windows::Foundation::Size const& size)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
_size = size;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
void EditBoxWinRT::setText(winrt::hstring const& text)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
_initialText = text;
|
|
|
|
// If already editing
|
|
|
|
if (_isEditing) {
|
2023-09-26 00:11:33 +08:00
|
|
|
m_dispatcher.get().RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [this]() {
|
2023-03-11 22:10:18 +08:00
|
|
|
if (!_password) {
|
2023-09-26 00:11:33 +08:00
|
|
|
auto textBox = _textBox.as<TextBox>();
|
|
|
|
unsigned int currentStart = textBox.SelectionStart();
|
|
|
|
bool cursorAtEnd = currentStart == textBox.Text().size();
|
|
|
|
textBox.Text(_initialText);
|
|
|
|
if (cursorAtEnd || currentStart > textBox.Text().size())
|
|
|
|
{
|
|
|
|
currentStart = textBox.Text().size();
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
2023-09-26 00:11:33 +08:00
|
|
|
textBox.Select(currentStart, 0);
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
2023-09-26 00:11:33 +08:00
|
|
|
});
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxWinRT::setVisible(bool visible)
|
|
|
|
{
|
|
|
|
_visible = visible;
|
|
|
|
// If already editing
|
2023-09-26 00:11:33 +08:00
|
|
|
m_dispatcher.get().RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, [this]() {
|
|
|
|
Canvas canvas = findXamlElement(m_panel.get(), CANVAS_XAML_NAME).as<Canvas>();
|
|
|
|
canvas.Visibility(_visible ? Visibility::Visible : Visibility::Collapsed);
|
|
|
|
});
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UIEditBoxImplWinrt::UIEditBoxImplWinrt(EditBox* pEditText) : EditBoxImplCommon(pEditText)
|
|
|
|
{
|
2023-09-26 00:11:33 +08:00
|
|
|
auto beginHandler = ([this](Windows::Foundation::IInspectable const& sender, winrt::hstring const& arg) {
|
2023-03-11 22:10:18 +08:00
|
|
|
this->editBoxEditingDidBegin();
|
|
|
|
});
|
2023-09-26 00:11:33 +08:00
|
|
|
auto changeHandler = ([this](Windows::Foundation::IInspectable const& sender, winrt::hstring const& arg) {
|
2023-03-11 22:10:18 +08:00
|
|
|
auto text = PlatformStringToString(arg);
|
|
|
|
this->editBoxEditingChanged(text);
|
|
|
|
});
|
2023-09-26 00:11:33 +08:00
|
|
|
auto endHandler = ([this](Windows::Foundation::IInspectable const& sender, ax::EndEventArgs const& arg) {
|
|
|
|
auto text = PlatformStringToString(arg.GetText());
|
|
|
|
auto action = arg.GetAction();
|
2023-03-11 22:10:18 +08:00
|
|
|
this->editBoxEditingDidEnd(text, static_cast<ax::ui::EditBoxDelegate::EditBoxEndAction>(action));
|
|
|
|
this->onEndEditing(text);
|
|
|
|
});
|
2023-09-26 00:11:33 +08:00
|
|
|
|
|
|
|
_system_control_agile =
|
|
|
|
winrt::make_agile(winrt::make<EditBoxWinRT>(beginHandler, changeHandler, endHandler));
|
|
|
|
_system_control = _system_control_agile.get().as<EditBoxWinRT>();
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UIEditBoxImplWinrt::setNativeFont(const char* pFontName, int fontSize)
|
|
|
|
{
|
|
|
|
// fontSize
|
|
|
|
_fontSize = fontSize;
|
|
|
|
auto transform = _editBox->getNodeToWorldTransform();
|
|
|
|
ax::Vec3 scale;
|
|
|
|
transform.getScale(&scale);
|
2023-09-26 00:11:33 +08:00
|
|
|
_system_control->setFontSize(
|
|
|
|
_fontSize *
|
|
|
|
ax::Director::getInstance()->getOpenGLView()->getScaleY() /** scale.y*/);
|
2023-03-11 22:10:18 +08:00
|
|
|
|
|
|
|
// fontFamily
|
2023-09-26 00:11:33 +08:00
|
|
|
auto font = ax::FontFreeType::create(pFontName, fontSize, ax::GlyphCollection::DYNAMIC, ""sv);
|
2023-03-11 22:10:18 +08:00
|
|
|
if (font != nullptr) {
|
2023-09-26 00:11:33 +08:00
|
|
|
std::string fontName = "ms-appx:///Content/" + std::string(pFontName) +'#' + font->getFontFamily();
|
2023-03-11 22:10:18 +08:00
|
|
|
_system_control->setFontFamily(PlatformStringFromString(fontName));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIEditBoxImplWinrt::setNativeFontColor(const Color4B& color)
|
|
|
|
{
|
|
|
|
Windows::UI::Color win_color = { 0xFF, color.r, color.g, color.b };
|
|
|
|
_system_control->setFontColor(win_color);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIEditBoxImplWinrt::setNativeInputMode(EditBox::InputMode inputMode)
|
|
|
|
{
|
|
|
|
_system_control->setInputMode((int)inputMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIEditBoxImplWinrt::setNativeInputFlag(EditBox::InputFlag inputFlag)
|
|
|
|
{
|
|
|
|
_system_control->setInputFlag((int)inputFlag);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIEditBoxImplWinrt::setNativeTextHorizontalAlignment(ax::TextHAlignment alignment)
|
|
|
|
{
|
|
|
|
_system_control->setTextHorizontalAlignment((int)alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIEditBoxImplWinrt::setNativeText(const char* pText)
|
|
|
|
{
|
|
|
|
_system_control->setText(PlatformStringFromString(pText));
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIEditBoxImplWinrt::setNativeVisible(bool visible)
|
|
|
|
{
|
|
|
|
_system_control->setVisible(visible);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIEditBoxImplWinrt::updateNativeFrame(const Rect& rect)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIEditBoxImplWinrt::nativeOpenKeyboard()
|
|
|
|
{
|
|
|
|
// Update the text
|
|
|
|
_system_control->setText(PlatformStringFromString(getText()));
|
|
|
|
// Size
|
|
|
|
auto glView = ax::Director::getInstance()->getOpenGLView();
|
|
|
|
auto transform = _editBox->getNodeToWorldTransform();
|
|
|
|
ax::Vec3 scale;
|
|
|
|
transform.getScale(&scale);
|
|
|
|
Windows::Foundation::Size xamlSize = { _editBox->getContentSize().width * glView->getScaleX() * scale.x, _editBox->getContentSize().height * glView->getScaleY() * scale.y };
|
|
|
|
_system_control->setSize(xamlSize);
|
|
|
|
_system_control->setFontSize(_fontSize * ax::Director::getInstance()->getOpenGLView()->getScaleY() /** scale.y*/);
|
|
|
|
// Position
|
|
|
|
auto directorInstance = ax::Director::getInstance();
|
|
|
|
auto frameSize = glView->getFrameSize();
|
|
|
|
auto winSize = directorInstance->getWinSize();
|
|
|
|
auto leftBottom = _editBox->convertToWorldSpace(ax::Point::ZERO);
|
|
|
|
auto rightTop = _editBox->convertToWorldSpace(ax::Point(_editBox->getContentSize().width, _editBox->getContentSize().height));
|
|
|
|
Windows::Foundation::Rect rect;
|
|
|
|
rect.X = frameSize.width / 2 + (leftBottom.x - winSize.width / 2) * glView->getScaleX();
|
|
|
|
rect.Y = frameSize.height / 2 - (rightTop.y - winSize.height / 2) * glView->getScaleY();
|
|
|
|
rect.Width = (rightTop.x - leftBottom.x) * glView->getScaleX();
|
|
|
|
rect.Height = (rightTop.y - leftBottom.y) * glView->getScaleY();
|
|
|
|
_system_control->setPosition(rect);
|
|
|
|
// .. and open
|
|
|
|
_system_control->openKeyboard();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIEditBoxImplWinrt::nativeCloseKeyboard()
|
|
|
|
{
|
|
|
|
_system_control->closeKeyboard();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UIEditBoxImplWinrt::setNativeMaxLength(int maxLength)
|
|
|
|
{
|
|
|
|
_system_control->setMaxLength(maxLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
ax::Vec2 UIEditBoxImplWinrt::convertDesignCoordToXamlCoord(const ax::Vec2& designCoord)
|
|
|
|
{
|
|
|
|
auto glView = ax::Director::getInstance()->getOpenGLView();
|
|
|
|
float viewH = glView->getFrameSize().height;
|
|
|
|
Vec2 visiblePos = Vec2(designCoord.x * glView->getScaleX(), designCoord.y * glView->getScaleY());
|
|
|
|
Vec2 screenGLPos = visiblePos + glView->getViewPortRect().origin;
|
|
|
|
Vec2 xamlPos(screenGLPos.x, viewH - screenGLPos.y);
|
|
|
|
return xamlPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ui
|
|
|
|
|
|
|
|
} // namespace cocos2d
|
|
|
|
|
|
|
|
#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|