2013-01-11 06:59:21 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013 Jozef Pridavok
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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 "CCEditBoxImplWin.h"
|
2013-01-31 14:04:49 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
|
|
|
|
|
2013-01-11 06:59:21 +08:00
|
|
|
#include "CCEditBox.h"
|
|
|
|
#include "proj.win32/Win32InputBox.h"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
EditBoxImpl* __createSystemEditBox(EditBox* pEditBox)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
return new EditBoxImplWin(pEditBox);
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
EditBoxImplWin::EditBoxImplWin(EditBox* pEditText)
|
|
|
|
: EditBoxImpl(pEditText)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _label(NULL)
|
|
|
|
, _labelPlaceHolder(NULL)
|
2013-07-26 17:45:01 +08:00
|
|
|
, _editBoxInputMode(EditBox::InputMode::SINGLE_LINE)
|
|
|
|
, _editBoxInputFlag(EditBox::InputFlag::INTIAL_CAPS_ALL_CHARACTERS)
|
2013-07-26 21:36:32 +08:00
|
|
|
, _keyboardReturnType(EditBox::KeyboardReturnType::DEFAULT)
|
2013-07-08 18:11:32 +08:00
|
|
|
, _colText(Color3B::WHITE)
|
|
|
|
, _colPlaceHolder(Color3B::GRAY)
|
2013-06-15 14:03:30 +08:00
|
|
|
, _maxLength(-1)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
EditBoxImplWin::~EditBoxImplWin()
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplWin::doAnimationWhenKeyboardMove(float duration, float distance)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool EditBoxImplWin::initWithSize(const Size& size)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
|
|
|
//! int fontSize = getFontSizeAccordingHeightJni(size.height-12);
|
2013-06-20 14:15:53 +08:00
|
|
|
_label = LabelTTF::create("", "", size.height-12);
|
2013-02-25 22:50:15 +08:00
|
|
|
// align the text vertically center
|
2013-07-12 18:04:32 +08:00
|
|
|
_label->setAnchorPoint(Point(0, 0.5f));
|
|
|
|
_label->setPosition(Point(5, size.height / 2.0f));
|
2013-06-15 14:03:30 +08:00
|
|
|
_label->setColor(_colText);
|
|
|
|
_editBox->addChild(_label);
|
2013-01-11 06:59:21 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
_labelPlaceHolder = LabelTTF::create("", "", size.height-12);
|
2013-02-25 22:50:15 +08:00
|
|
|
// align the text vertically center
|
2013-07-12 18:04:32 +08:00
|
|
|
_labelPlaceHolder->setAnchorPoint(Point(0, 0.5f));
|
|
|
|
_labelPlaceHolder->setPosition(Point(5, size.height / 2.0f));
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelPlaceHolder->setVisible(false);
|
|
|
|
_labelPlaceHolder->setColor(_colPlaceHolder);
|
|
|
|
_editBox->addChild(_labelPlaceHolder);
|
2013-01-11 06:59:21 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_editSize = size;
|
2013-01-11 06:59:21 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplWin::setFont(const char* pFontName, int fontSize)
|
2013-02-25 22:50:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_label != NULL) {
|
|
|
|
_label->setFontName(pFontName);
|
|
|
|
_label->setFontSize(fontSize);
|
2013-02-25 22:50:15 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_labelPlaceHolder != NULL) {
|
|
|
|
_labelPlaceHolder->setFontName(pFontName);
|
|
|
|
_labelPlaceHolder->setFontSize(fontSize);
|
2013-02-25 22:50:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void EditBoxImplWin::setFontColor(const Color3B& color)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_colText = color;
|
|
|
|
_label->setColor(color);
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplWin::setPlaceholderFont(const char* pFontName, int fontSize)
|
2013-02-25 22:50:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_labelPlaceHolder != NULL) {
|
|
|
|
_labelPlaceHolder->setFontName(pFontName);
|
|
|
|
_labelPlaceHolder->setFontSize(fontSize);
|
2013-02-25 22:50:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void EditBoxImplWin::setPlaceholderFontColor(const Color3B& color)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_colPlaceHolder = color;
|
|
|
|
_labelPlaceHolder->setColor(color);
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 17:45:01 +08:00
|
|
|
void EditBoxImplWin::setInputMode(EditBox::InputMode inputMode)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_editBoxInputMode = inputMode;
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplWin::setMaxLength(int maxLength)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_maxLength = maxLength;
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
int EditBoxImplWin::getMaxLength()
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _maxLength;
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 17:45:01 +08:00
|
|
|
void EditBoxImplWin::setInputFlag(EditBox::InputFlag inputFlag)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_editBoxInputFlag = inputFlag;
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 17:45:01 +08:00
|
|
|
void EditBoxImplWin::setReturnType(EditBox::KeyboardReturnType returnType)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_keyboardReturnType = returnType;
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool EditBoxImplWin::isEditing()
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplWin::setText(const char* pText)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
|
|
|
if (pText != NULL)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_text = pText;
|
2013-01-11 06:59:21 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_text.length() > 0)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelPlaceHolder->setVisible(false);
|
2013-01-11 06:59:21 +08:00
|
|
|
|
|
|
|
std::string strToShow;
|
|
|
|
|
2013-07-26 22:56:37 +08:00
|
|
|
if (EditBox::InputFlag::PASSWORD == _editBoxInputFlag)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
long length = cc_utf8_strlen(_text.c_str(), -1);
|
2013-01-11 06:59:21 +08:00
|
|
|
for (long i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
strToShow.append("*");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
strToShow = _text;
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
//! std::string strWithEllipsis = getStringWithEllipsisJni(strToShow.c_str(), _editSize.width, _editSize.height-12);
|
|
|
|
//! _label->setString(strWithEllipsis.c_str());
|
|
|
|
_label->setString(strToShow.c_str());
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelPlaceHolder->setVisible(true);
|
|
|
|
_label->setString("");
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
const char* EditBoxImplWin::getText(void)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _text.c_str();
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplWin::setPlaceHolder(const char* pText)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
|
|
|
if (pText != NULL)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_placeHolder = pText;
|
|
|
|
if (_placeHolder.length() > 0 && _text.length() == 0)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelPlaceHolder->setVisible(true);
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_labelPlaceHolder->setString(_placeHolder.c_str());
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplWin::setPosition(const Point& pos)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
//_label->setPosition(pos);
|
|
|
|
//_labelPlaceHolder->setPosition(pos);
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplWin::setVisible(bool visible)
|
2013-03-03 20:13:22 +08:00
|
|
|
{ // don't need to be implemented on win32 platform.
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplWin::setContentSize(const Size& size)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplWin::setAnchorPoint(const Point& anchorPoint)
|
2013-02-25 22:50:15 +08:00
|
|
|
{ // don't need to be implemented on win32 platform.
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplWin::visit(void)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void editBoxCallbackFunc(const char* pText, void* ctx)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
EditBoxImplWin* thiz = (EditBoxImplWin*)ctx;
|
2013-01-11 06:59:21 +08:00
|
|
|
thiz->setText(pText);
|
|
|
|
|
|
|
|
if (thiz->getDelegate() != NULL)
|
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
thiz->getDelegate()->editBoxTextChanged(thiz->getEditBox(), thiz->getText());
|
|
|
|
thiz->getDelegate()->editBoxEditingDidEnd(thiz->getEditBox());
|
|
|
|
thiz->getDelegate()->editBoxReturn(thiz->getEditBox());
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
2013-05-24 11:42:27 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
EditBox* pEditBox = thiz->getEditBox();
|
2013-05-24 11:42:27 +08:00
|
|
|
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
|
|
|
|
{
|
2013-07-04 15:44:42 +08:00
|
|
|
CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "changed",pEditBox);
|
2013-07-02 15:23:51 +08:00
|
|
|
ScriptEvent event(kCommonEvent,(void*)&data);
|
2013-07-22 17:24:54 +08:00
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
|
2013-07-02 15:23:51 +08:00
|
|
|
memset(data.eventName,0,64*sizeof(char));
|
|
|
|
strncpy(data.eventName,"ended",64);
|
|
|
|
event.data = (void*)&data;
|
2013-07-22 17:24:54 +08:00
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
|
2013-07-02 15:23:51 +08:00
|
|
|
memset(data.eventName,0,64*sizeof(char));
|
|
|
|
strncpy(data.eventName,"return",64);
|
|
|
|
event.data = (void*)&data;
|
2013-07-22 17:24:54 +08:00
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
|
2013-05-24 11:42:27 +08:00
|
|
|
}
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplWin::openKeyboard()
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_delegate != NULL)
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_delegate->editBoxEditingDidBegin(_editBox);
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
2013-05-24 11:42:27 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
EditBox* pEditBox = this->getEditBox();
|
2013-05-24 11:42:27 +08:00
|
|
|
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
|
|
|
|
{
|
2013-07-04 15:44:42 +08:00
|
|
|
CommonScriptData data(pEditBox->getScriptEditBoxHandler(), "began",pEditBox);
|
2013-07-02 15:23:51 +08:00
|
|
|
ScriptEvent event(kCommonEvent,(void*)&data);
|
2013-07-22 17:24:54 +08:00
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
|
2013-05-24 11:42:27 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string placeHolder = _labelPlaceHolder->getString();
|
2013-01-11 06:59:21 +08:00
|
|
|
if (placeHolder.length() == 0)
|
|
|
|
placeHolder = "Enter value";
|
|
|
|
|
|
|
|
char pText[100]= {0};
|
|
|
|
std::string text = getText();
|
|
|
|
if (text.length())
|
|
|
|
strncpy(pText, text.c_str(), 100);
|
|
|
|
bool didChange = CWin32InputBox::InputBox("Input", placeHolder.c_str(), pText, 100, false) == IDOK;
|
|
|
|
|
|
|
|
if (didChange)
|
|
|
|
setText(pText);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_delegate != NULL) {
|
2013-01-11 06:59:21 +08:00
|
|
|
if (didChange)
|
2013-06-15 14:03:30 +08:00
|
|
|
_delegate->editBoxTextChanged(_editBox, getText());
|
|
|
|
_delegate->editBoxEditingDidEnd(_editBox);
|
|
|
|
_delegate->editBoxReturn(_editBox);
|
2013-01-11 06:59:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplWin::closeKeyboard()
|
2013-01-11 06:59:21 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void EditBoxImplWin::onEnter(void)
|
2013-03-14 14:39:59 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-01-11 06:59:21 +08:00
|
|
|
NS_CC_EXT_END
|
|
|
|
|
2013-01-31 14:04:49 +08:00
|
|
|
#endif /* (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) */
|