axmol/core/base/CCIMEDispatcher.cpp

346 lines
9.5 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
2022-01-04 12:36:20 +08:00
https://adxeproject.github.io/
2019-11-23 20:27:39 +08:00
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.
****************************************************************************/
#include "base/CCIMEDispatcher.h"
#include <list>
NS_CC_BEGIN
//////////////////////////////////////////////////////////////////////////
// add/remove delegate in IMEDelegate Cons/Destructor
//////////////////////////////////////////////////////////////////////////
IMEDelegate::IMEDelegate()
{
IMEDispatcher::sharedDispatcher()->addDelegate(this);
}
IMEDelegate::~IMEDelegate()
{
IMEDispatcher::sharedDispatcher()->removeDelegate(this);
}
bool IMEDelegate::attachWithIME()
{
return IMEDispatcher::sharedDispatcher()->attachDelegateWithIME(this);
}
bool IMEDelegate::detachWithIME()
{
return IMEDispatcher::sharedDispatcher()->detachDelegateWithIME(this);
}
//////////////////////////////////////////////////////////////////////////
2021-12-25 10:04:45 +08:00
typedef std::list<IMEDelegate*> DelegateList;
typedef std::list<IMEDelegate*>::iterator DelegateIter;
2019-11-23 20:27:39 +08:00
//////////////////////////////////////////////////////////////////////////
// Delegate List manage class
//////////////////////////////////////////////////////////////////////////
class IMEDispatcher::Impl
{
public:
2021-12-25 10:04:45 +08:00
Impl() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
~Impl() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void init() { _delegateWithIme = 0; }
2019-11-23 20:27:39 +08:00
DelegateIter findDelegate(IMEDelegate* delegate)
{
DelegateIter end = _delegateList.end();
for (DelegateIter iter = _delegateList.begin(); iter != end; ++iter)
{
if (delegate == *iter)
{
return iter;
}
}
return end;
}
2021-12-25 10:04:45 +08:00
DelegateList _delegateList;
IMEDelegate* _delegateWithIme;
2019-11-23 20:27:39 +08:00
};
//////////////////////////////////////////////////////////////////////////
// Cons/Destructor
//////////////////////////////////////////////////////////////////////////
2021-12-25 10:04:45 +08:00
IMEDispatcher::IMEDispatcher() : _impl(new IMEDispatcher::Impl)
2019-11-23 20:27:39 +08:00
{
_impl->init();
}
IMEDispatcher::~IMEDispatcher()
{
CC_SAFE_DELETE(_impl);
}
//////////////////////////////////////////////////////////////////////////
// Add/Attach/Remove IMEDelegate
//////////////////////////////////////////////////////////////////////////
void IMEDispatcher::addDelegate(IMEDelegate* delegate)
{
2021-12-25 10:04:45 +08:00
if (!delegate || !_impl)
2019-11-23 20:27:39 +08:00
{
return;
}
if (_impl->_delegateList.end() != _impl->findDelegate(delegate))
{
// pDelegate already in list
return;
}
_impl->_delegateList.push_front(delegate);
}
2021-12-25 10:04:45 +08:00
bool IMEDispatcher::attachDelegateWithIME(IMEDelegate* delegate)
2019-11-23 20:27:39 +08:00
{
bool ret = false;
do
{
2021-12-25 10:04:45 +08:00
CC_BREAK_IF(!_impl || !delegate);
2019-11-23 20:27:39 +08:00
DelegateIter end = _impl->_delegateList.end();
DelegateIter iter = _impl->findDelegate(delegate);
// if pDelegate is not in delegate list, return
CC_BREAK_IF(end == iter);
if (_impl->_delegateWithIme)
{
if (_impl->_delegateWithIme != delegate)
{
2021-12-25 10:04:45 +08:00
// if old delegate canDetachWithIME return false
2019-11-23 20:27:39 +08:00
// or pDelegate canAttachWithIME return false,
// do nothing.
2021-12-25 10:04:45 +08:00
CC_BREAK_IF(!_impl->_delegateWithIme->canDetachWithIME() || !delegate->canAttachWithIME());
2019-11-23 20:27:39 +08:00
// detach first
2021-12-25 10:04:45 +08:00
IMEDelegate* oldDelegate = _impl->_delegateWithIme;
_impl->_delegateWithIme = 0;
2019-11-23 20:27:39 +08:00
oldDelegate->didDetachWithIME();
_impl->_delegateWithIme = *iter;
delegate->didAttachWithIME();
}
ret = true;
break;
}
// delegate hasn't attached to IME yet
2021-12-25 10:04:45 +08:00
CC_BREAK_IF(!delegate->canAttachWithIME());
2019-11-23 20:27:39 +08:00
_impl->_delegateWithIme = *iter;
delegate->didAttachWithIME();
ret = true;
} while (0);
return ret;
}
2021-12-25 10:04:45 +08:00
bool IMEDispatcher::detachDelegateWithIME(IMEDelegate* delegate)
2019-11-23 20:27:39 +08:00
{
bool ret = false;
do
{
2021-12-25 10:04:45 +08:00
CC_BREAK_IF(!_impl || !delegate);
2019-11-23 20:27:39 +08:00
// if pDelegate is not the current delegate attached to IME, return
CC_BREAK_IF(_impl->_delegateWithIme != delegate);
2021-12-25 10:04:45 +08:00
CC_BREAK_IF(!delegate->canDetachWithIME());
2019-11-23 20:27:39 +08:00
_impl->_delegateWithIme = 0;
delegate->didDetachWithIME();
ret = true;
} while (0);
return ret;
}
void IMEDispatcher::removeDelegate(IMEDelegate* delegate)
{
2021-12-25 10:04:45 +08:00
do
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
CC_BREAK_IF(!delegate || !_impl);
2019-11-23 20:27:39 +08:00
DelegateIter iter = _impl->findDelegate(delegate);
DelegateIter end = _impl->_delegateList.end();
CC_BREAK_IF(end == iter);
if (_impl->_delegateWithIme)
2021-12-25 10:04:45 +08:00
if (*iter == _impl->_delegateWithIme)
{
_impl->_delegateWithIme = 0;
}
2019-11-23 20:27:39 +08:00
_impl->_delegateList.erase(iter);
} while (0);
}
//////////////////////////////////////////////////////////////////////////
// dispatch text message
//////////////////////////////////////////////////////////////////////////
2021-12-25 10:04:45 +08:00
void IMEDispatcher::dispatchInsertText(const char* text, size_t len)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
do
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
CC_BREAK_IF(!_impl || !text || len <= 0);
2019-11-23 20:27:39 +08:00
// there is no delegate attached to IME
2021-12-25 10:04:45 +08:00
CC_BREAK_IF(!_impl->_delegateWithIme);
2019-11-23 20:27:39 +08:00
_impl->_delegateWithIme->insertText(text, len);
} while (0);
}
void IMEDispatcher::dispatchDeleteBackward()
{
2021-12-25 10:04:45 +08:00
do
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
CC_BREAK_IF(!_impl);
2019-11-23 20:27:39 +08:00
// there is no delegate attached to IME
2021-12-25 10:04:45 +08:00
CC_BREAK_IF(!_impl->_delegateWithIme);
2019-11-23 20:27:39 +08:00
_impl->_delegateWithIme->deleteBackward();
} while (0);
}
void IMEDispatcher::dispatchControlKey(EventKeyboard::KeyCode keyCode)
{
do
{
CC_BREAK_IF(!_impl);
// there is no delegate attached to IME
CC_BREAK_IF(!_impl->_delegateWithIme);
_impl->_delegateWithIme->controlKey(keyCode);
} while (0);
}
std::string_view IMEDispatcher::getContentText()
2019-11-23 20:27:39 +08:00
{
if (_impl && _impl->_delegateWithIme)
{
return _impl->_delegateWithIme->getContentText();
}
return STD_STRING_EMPTY;
}
//////////////////////////////////////////////////////////////////////////
// dispatch keyboard message
//////////////////////////////////////////////////////////////////////////
void IMEDispatcher::dispatchKeyboardWillShow(IMEKeyboardNotificationInfo& info)
{
if (_impl)
{
2021-12-25 10:04:45 +08:00
IMEDelegate* delegate = nullptr;
DelegateIter last = _impl->_delegateList.end();
2019-11-23 20:27:39 +08:00
for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
{
delegate = *(first);
if (delegate)
{
delegate->keyboardWillShow(info);
}
}
}
}
void IMEDispatcher::dispatchKeyboardDidShow(IMEKeyboardNotificationInfo& info)
{
if (_impl)
{
2021-12-25 10:04:45 +08:00
IMEDelegate* delegate = nullptr;
DelegateIter last = _impl->_delegateList.end();
2019-11-23 20:27:39 +08:00
for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
{
delegate = *(first);
if (delegate)
{
delegate->keyboardDidShow(info);
}
}
}
}
void IMEDispatcher::dispatchKeyboardWillHide(IMEKeyboardNotificationInfo& info)
{
if (_impl)
{
2021-12-25 10:04:45 +08:00
IMEDelegate* delegate = nullptr;
DelegateIter last = _impl->_delegateList.end();
2019-11-23 20:27:39 +08:00
for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
{
delegate = *(first);
if (delegate)
{
delegate->keyboardWillHide(info);
}
}
}
}
void IMEDispatcher::dispatchKeyboardDidHide(IMEKeyboardNotificationInfo& info)
{
if (_impl)
{
2021-12-25 10:04:45 +08:00
IMEDelegate* delegate = nullptr;
DelegateIter last = _impl->_delegateList.end();
2019-11-23 20:27:39 +08:00
for (DelegateIter first = _impl->_delegateList.begin(); first != last; ++first)
{
delegate = *(first);
if (delegate)
{
delegate->keyboardDidHide(info);
}
}
}
}
//////////////////////////////////////////////////////////////////////////
// protected member function
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// static member function
//////////////////////////////////////////////////////////////////////////
IMEDispatcher* IMEDispatcher::sharedDispatcher()
{
static IMEDispatcher s_instance;
return &s_instance;
}
NS_CC_END