From b1ed1a8a517705980c3f4c86e26465c7ffc8d8d4 Mon Sep 17 00:00:00 2001 From: hanxi Date: Sat, 6 Jun 2015 14:20:16 +0800 Subject: [PATCH] add UIEditBox support linux platform. --- CMakeLists.txt | 1 + build/install-deps-linux.sh | 1 + cmake/Modules/FindGTK3.cmake | 56 ++++++++ cocos/CMakeLists.txt | 2 +- cocos/ui/CMakeLists.txt | 3 +- cocos/ui/UIEditBox/UIEditBoxImpl-linux.cpp | 120 ++++++++++++++++++ cocos/ui/UIEditBox/UIEditBoxImpl-linux.h | 90 +++++++++++++ .../src/TextInputTest/TextInputTest.lua | 37 ++++++ tests/lua-tests/src/mainMenu.lua | 3 +- 9 files changed, 310 insertions(+), 3 deletions(-) create mode 100644 cmake/Modules/FindGTK3.cmake create mode 100644 cocos/ui/UIEditBox/UIEditBoxImpl-linux.cpp create mode 100644 cocos/ui/UIEditBox/UIEditBoxImpl-linux.h create mode 100644 tests/lua-tests/src/TextInputTest/TextInputTest.lua diff --git a/CMakeLists.txt b/CMakeLists.txt index b7f9fcc2f948..925b47e95146 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -202,6 +202,7 @@ if(LINUX OR MACOSX OR WINDOWS) cocos_find_package(FMODEX FMODEX REQUIRED) cocos_find_package(Fontconfig FONTCONFIG REQUIRED) + cocos_find_package(GTK3 GTK3 REQUIRED) endif() if(WINDOWS) diff --git a/build/install-deps-linux.sh b/build/install-deps-linux.sh index b58b8ca923fb..3e19ef24d6a8 100755 --- a/build/install-deps-linux.sh +++ b/build/install-deps-linux.sh @@ -28,6 +28,7 @@ DEPENDS+=' libfontconfig1-dev' DEPENDS+=' libsqlite3-dev' DEPENDS+=' libglew-dev' DEPENDS+=' libssl-dev' +DEPENDS+=' libgtk-3-dev' MISSING= echo "Checking for missing packages ..." diff --git a/cmake/Modules/FindGTK3.cmake b/cmake/Modules/FindGTK3.cmake new file mode 100644 index 000000000000..11e8429611a4 --- /dev/null +++ b/cmake/Modules/FindGTK3.cmake @@ -0,0 +1,56 @@ +# - Try to find GTK3 +# +# $Id: FindGTK3.cmake 39970 2011-11-21 15:39:25Z jmayer $ +# +# Once done this will define +# +# GTK3_FOUND - System has GTK3 +# GTK3_INCLUDE_DIRS - The GTK3 include directory +# GTK3_LIBRARIES - The libraries needed to use GTK3 +# GTK3_DEFINITIONS - Compiler switches required for using GTK3 +#============================================================================= +# Copyright 2011 Duncan Mac-Vicar P. +# +# Distributed under the OSI-approved BSD License (the "License"); +# see accompanying file COPYING-CMAKE-SCRIPTS for details. +# +# This software is distributed WITHOUT ANY WARRANTY; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +# See the License for more information. +#============================================================================= +# (To distribute this file outside of CMake, substitute the full +# License text for the above reference.) + +# use pkg-config to get the directories and then use these values +# in the FIND_PATH() and FIND_LIBRARY() calls +find_package(PkgConfig) +pkg_check_modules(PC_GTK3 gtk+-3.0 QUIET) + +# MESSAGE(STATUS "PC_GTK3_LIBRARIES: ${PC_GTK3_LIBRARIES}") +# MESSAGE(STATUS "PC_GTK3_LIBRARY_DIRS: ${PC_GTK3_LIBRARY_DIRS}") +# MESSAGE(STATUS "PC_GTK3_LDFLAGS: ${PC_GTK3_LDFLAGS}") +# MESSAGE(STATUS "PC_GTK3_LDFLAGS_OTHER: ${PC_GTK3_LDFLAGS_OTHER}") + +set(GTK3_DEFINITIONS ${PC_GTK3_CFLAGS_OTHER}) + +#FIND_PATH(GTK3_INCLUDE_DIR NAMES "gtk/gtk.h" +# HINTS +# ${PC_GTK3_INCLUDEDIR} +# ${PC_GTK3_INCLUDE_DIRS} +# PATH_SUFFIXES "gtk-3.0" +# ) +set(GTK3_INCLUDE_DIRS ${PC_GTK3_INCLUDE_DIRS}) + +#FIND_LIBRARY(GTK3_LIBRARIES NAMES gtk-3 gtk3 +# HINTS +# ${PC_GTK3_LIBDIR} +# ${PC_GTK3_LIBRARY_DIRS} +# ) +set(GTK3_LIBRARIES ${PC_GTK3_LIBRARIES}) + +# handle the QUIETLY and REQUIRED arguments and set GTK3_FOUND to TRUE if +# all listed variables are TRUE +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(GTK3 DEFAULT_MSG GTK3_LIBRARIES GTK3_INCLUDE_DIRS) + +mark_as_advanced(GTK3_INCLUDE_DIRS GTK3_LIBRARIES) diff --git a/cocos/CMakeLists.txt b/cocos/CMakeLists.txt index 311fe2728dd2..6cff5b9a26ce 100644 --- a/cocos/CMakeLists.txt +++ b/cocos/CMakeLists.txt @@ -96,7 +96,7 @@ if(WINDOWS) endforeach() list(APPEND PLATFORM_SPECIFIC_LIBS ws2_32 winmm) elseif(LINUX) - foreach(_pkg OPENGL GLEW GLFW3 FMODEX FONTCONFIG THREADS) + foreach(_pkg OPENGL GLEW GLFW3 FMODEX FONTCONFIG THREADS GTK3) cocos_use_pkg(cocos2d ${_pkg}) endforeach() elseif(MACOSX OR APPLE) diff --git a/cocos/ui/CMakeLists.txt b/cocos/ui/CMakeLists.txt index 4ab3940db942..0fb18b8496af 100644 --- a/cocos/ui/CMakeLists.txt +++ b/cocos/ui/CMakeLists.txt @@ -13,7 +13,8 @@ elseif(MACOSX) ) elseif(LINUX) set(COCOS_UI_SPECIFIC_SRC - ui/UIEditBox/UIEditBoxImpl-stub.cpp + ui/UIEditBox/UIEditBoxImpl-common.cpp + ui/UIEditBox/UIEditBoxImpl-linux.cpp ) endif() diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-linux.cpp b/cocos/ui/UIEditBox/UIEditBoxImpl-linux.cpp new file mode 100644 index 000000000000..210b277ed3bd --- /dev/null +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-linux.cpp @@ -0,0 +1,120 @@ +/**************************************************************************** + Copyright (c) 2010-2012 cocos2d-x.org + Copyright (c) 2015 hanxi + + 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 "UIEditBoxImpl-linux.h" + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) + +#include "UIEditBox.h" +#include "2d/CCLabel.h" +#include "base/ccUTF8.h" +#include + +// desoty dialog when lost focus +static void dialogFocusOutCallback(GtkWidget* widget, gpointer user_data) +{ + gtk_widget_destroy(widget); +} + +bool LinuxInputBox(std::string &entryLine) +{ + bool didChange = false; + GtkWidget *dialog; + GtkWidget *entry; + GtkWidget *contentArea; + + gtk_init(0, NULL); + dialog = gtk_dialog_new(); + entry = gtk_entry_new(); + contentArea = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); + + gtk_container_add(GTK_CONTAINER(contentArea), entry); + gtk_dialog_add_button(GTK_DIALOG(dialog), "OK", 0); + gtk_entry_set_text(GTK_ENTRY(entry), entryLine.c_str()); + + g_signal_connect(dialog, "focus-out-event", G_CALLBACK(dialogFocusOutCallback), NULL); + gtk_window_set_keep_above(GTK_WINDOW(dialog), true); + gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_MENU); + gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER); + gtk_widget_show_all(dialog); + + gint result = gtk_dialog_run(GTK_DIALOG(dialog)); + switch(result) + { + case 0: + entryLine = gtk_entry_get_text(GTK_ENTRY(entry)); + didChange = true; + break; + default: + // CCLOG("Undefined. Perhaps dialog was closed"); + break; + } + + gtk_widget_destroy(dialog); + while (g_main_context_iteration(NULL, false)); + return didChange; +} + +NS_CC_BEGIN + +namespace ui { + +EditBoxImpl* __createSystemEditBox(EditBox* pEditBox) +{ + return new EditBoxImplLinux(pEditBox); +} + +EditBoxImplLinux::EditBoxImplLinux(EditBox* pEditText) +: EditBoxImplCommon(pEditText) +{ + +} + +EditBoxImplLinux::~EditBoxImplLinux() +{ + +} + +bool EditBoxImplLinux::isEditing() +{ + return false; +} + +void EditBoxImplLinux::nativeOpenKeyboard() +{ + std::string text = this->getText(); + bool didChange = LinuxInputBox(text); + if (didChange) + { + this->editBoxEditingDidEnd(text); + } +} + +} + +NS_CC_END + +#endif /* #if (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) */ + diff --git a/cocos/ui/UIEditBox/UIEditBoxImpl-linux.h b/cocos/ui/UIEditBox/UIEditBoxImpl-linux.h new file mode 100644 index 000000000000..5a0726632c0b --- /dev/null +++ b/cocos/ui/UIEditBox/UIEditBoxImpl-linux.h @@ -0,0 +1,90 @@ +/**************************************************************************** + Copyright (c) 2010-2012 cocos2d-x.org + Copyright (c) 2015 hanxi + + 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. + ****************************************************************************/ + +#ifndef __UIEDITBOXIMPLLINUX_H__ +#define __UIEDITBOXIMPLLINUX_H__ + +#include "platform/CCPlatformConfig.h" + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) + +#include "UIEditBoxImpl-common.h" + +NS_CC_BEGIN + +class Label; + +namespace ui { + +class EditBox; + +class EditBoxImplLinux: public EditBoxImplCommon +{ +public: + /** + * @js NA + */ + EditBoxImplLinux(EditBox* pEditText); + /** + * @js NA + * @lua NA + */ + virtual ~EditBoxImplLinux(); + + + virtual bool isEditing() override; + virtual void createNativeControl(const Rect& frame) override {}; + virtual void setNativeFont(const char* pFontName, int fontSize) override {}; + virtual void setNativeFontColor(const Color4B& color) override {}; + virtual void setNativePlaceholderFont(const char* pFontName, int fontSize) override {}; + virtual void setNativePlaceholderFontColor(const Color4B& color) override {}; + virtual void setNativeInputMode(EditBox::InputMode inputMode) override {}; + virtual void setNativeInputFlag(EditBox::InputFlag inputFlag) override {}; + virtual void setNativeReturnType(EditBox::KeyboardReturnType returnType)override {}; + virtual void setNativeText(const char* pText) override {}; + virtual void setNativePlaceHolder(const char* pText) override {}; + virtual void setNativeVisible(bool visible) override {}; + virtual void updateNativeFrame(const Rect& rect) override {}; + virtual void setNativeContentSize(const Size& size) override {}; + virtual const char* getNativeDefaultFontName() override {}; + virtual void nativeOpenKeyboard() override; + virtual void nativeCloseKeyboard() override {}; + virtual void setNativeMaxLength(int maxLength) {}; + + +private: + virtual void doAnimationWhenKeyboardMove(float duration, float distance)override {} +}; + + +} + + +NS_CC_END + +#endif /* #if (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) */ + +#endif /* __UIEDITBOXIMPLLINUX_H__ */ + diff --git a/tests/lua-tests/src/TextInputTest/TextInputTest.lua b/tests/lua-tests/src/TextInputTest/TextInputTest.lua new file mode 100644 index 000000000000..8159f88119ba --- /dev/null +++ b/tests/lua-tests/src/TextInputTest/TextInputTest.lua @@ -0,0 +1,37 @@ + +local TextInput = {} + +function TextInput.create() + cclog("TextInput.create") + local layer = cc.Layer:create() + Helper.initWithLayer(layer) + TextInput.layer = layer + + local res = "Images/CyanSquare.png" + local width = 200 + local height = 40 + local back = cc.Scale9Sprite:create(res) + local edit = ccui.EditBox:create(cc.size(width,height),res) + layer:addChild(edit) + edit:setPosition( cc.p(250,200) ) + edit:setPlaceHolder("click to input text") + + local editPasswd = ccui.EditBox:create(cc.size(width,height),res) + editPasswd:setInputFlag(cc.EDITBOX_INPUT_FLAG_PASSWORD) + layer:addChild(editPasswd) + editPasswd:setPosition( cc.p(250,100) ) + editPasswd:setPlaceHolder("click to input password") + return layer +end + +function TextInputTestMain() + cclog("TextInputTestMain") + local scene = cc.Scene:create() + Helper.createFunctionTable = { + TextInput.create, + } + scene:addChild(TextInput.create()) + scene:addChild(CreateBackMenuItem()) + return scene +end + diff --git a/tests/lua-tests/src/mainMenu.lua b/tests/lua-tests/src/mainMenu.lua index 1af3f9308102..57c6c78cbbeb 100644 --- a/tests/lua-tests/src/mainMenu.lua +++ b/tests/lua-tests/src/mainMenu.lua @@ -43,6 +43,7 @@ require "SpriteTest/SpriteTest" require "SceneTest/SceneTest" require "SpineTest/SpineTest" require "TerrainTest/TerrainTest" +require "TextInputTest/TextInputTest" require "Texture2dTest/Texture2dTest" require "TileMapTest/TileMapTest" require "TouchesTest/TouchesTest" @@ -136,7 +137,7 @@ local _allTests = { { isSupported = true, name = "TerrainTest" , create_func = TerrainTest }, { isSupported = true, name = "SpriteTest" , create_func = SpriteTest }, { isSupported = true, name = "SpritePolygonTest" , create_func = SpritePolygonTest }, - { isSupported = false, name = "TextInputTest" , create_func= TextInputTestMain }, + { isSupported = true, name = "TextInputTest" , create_func= TextInputTestMain }, { isSupported = true, name = "Texture2DTest" , create_func = Texture2dTestMain }, { isSupported = false, name = "TextureCacheTest" , create_func= TextureCacheTestMain }, { isSupported = true, name = "TileMapTest" , create_func = TileMapTestMain },