Skip to content

Commit

Permalink
add UIEditBox support linux platform.
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxi authored and hanxi committed Sep 18, 2015
1 parent 731373b commit b1ed1a8
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 3 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions build/install-deps-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ..."
Expand Down
56 changes: 56 additions & 0 deletions cmake/Modules/FindGTK3.cmake
Original file line number Diff line number Diff line change
@@ -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. <[email protected]>
#
# 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)
2 changes: 1 addition & 1 deletion cocos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion cocos/ui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
120 changes: 120 additions & 0 deletions cocos/ui/UIEditBox/UIEditBoxImpl-linux.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtk/gtk.h>

// 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) */

90 changes: 90 additions & 0 deletions cocos/ui/UIEditBox/UIEditBoxImpl-linux.h
Original file line number Diff line number Diff line change
@@ -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__ */

37 changes: 37 additions & 0 deletions tests/lua-tests/src/TextInputTest/TextInputTest.lua
Original file line number Diff line number Diff line change
@@ -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

3 changes: 2 additions & 1 deletion tests/lua-tests/src/mainMenu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 },
Expand Down

0 comments on commit b1ed1a8

Please sign in to comment.