Skip to content

Commit f9eef5c

Browse files
committed
Merge pull request #13636 from zilongshanren/refactorEditBox
refactor EditBox iOS&Android implementation
2 parents 2dfd22d + c0f40ad commit f9eef5c

File tree

14 files changed

+833
-708
lines changed

14 files changed

+833
-708
lines changed

build/cocos2d_libs.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,8 @@
12241224
299754F5193EC95400A54AC3 /* ObjectFactory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 299754F2193EC95400A54AC3 /* ObjectFactory.cpp */; };
12251225
299754F6193EC95400A54AC3 /* ObjectFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = 299754F3193EC95400A54AC3 /* ObjectFactory.h */; };
12261226
299754F7193EC95400A54AC3 /* ObjectFactory.h in Headers */ = {isa = PBXBuildFile; fileRef = 299754F3193EC95400A54AC3 /* ObjectFactory.h */; };
1227+
299B15AC1B93FC530008E196 /* UIEditBoxImpl-common.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 299B15AA1B93FC530008E196 /* UIEditBoxImpl-common.cpp */; };
1228+
299B15AD1B93FC530008E196 /* UIEditBoxImpl-common.h in Headers */ = {isa = PBXBuildFile; fileRef = 299B15AB1B93FC530008E196 /* UIEditBoxImpl-common.h */; };
12271229
299CF1FB19A434BC00C378C1 /* ccRandom.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 299CF1F919A434BC00C378C1 /* ccRandom.cpp */; };
12281230
299CF1FC19A434BC00C378C1 /* ccRandom.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 299CF1F919A434BC00C378C1 /* ccRandom.cpp */; };
12291231
299CF1FD19A434BC00C378C1 /* ccRandom.h in Headers */ = {isa = PBXBuildFile; fileRef = 299CF1FA19A434BC00C378C1 /* ccRandom.h */; };
@@ -4108,6 +4110,8 @@
41084110
2986667918B1B079000E39CA /* CCTweenFunction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCTweenFunction.h; sourceTree = "<group>"; };
41094111
299754F2193EC95400A54AC3 /* ObjectFactory.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ObjectFactory.cpp; path = ../base/ObjectFactory.cpp; sourceTree = "<group>"; };
41104112
299754F3193EC95400A54AC3 /* ObjectFactory.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ObjectFactory.h; path = ../base/ObjectFactory.h; sourceTree = "<group>"; };
4113+
299B15AA1B93FC530008E196 /* UIEditBoxImpl-common.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "UIEditBoxImpl-common.cpp"; sourceTree = "<group>"; };
4114+
299B15AB1B93FC530008E196 /* UIEditBoxImpl-common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIEditBoxImpl-common.h"; sourceTree = "<group>"; };
41114115
299CF1F919A434BC00C378C1 /* ccRandom.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ccRandom.cpp; path = ../base/ccRandom.cpp; sourceTree = "<group>"; };
41124116
299CF1FA19A434BC00C378C1 /* ccRandom.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ccRandom.h; path = ../base/ccRandom.h; sourceTree = "<group>"; };
41134117
29BDBA52195D597A003225C9 /* UIDeprecated.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = UIDeprecated.cpp; sourceTree = "<group>"; };
@@ -6816,6 +6820,8 @@
68166820
292DB12E19B4574100A80320 /* UIEditBox */ = {
68176821
isa = PBXGroup;
68186822
children = (
6823+
299B15AA1B93FC530008E196 /* UIEditBoxImpl-common.cpp */,
6824+
299B15AB1B93FC530008E196 /* UIEditBoxImpl-common.h */,
68196825
50ED2BDC19BEAF7900A0AB90 /* UIEditBoxImpl-win32.h */,
68206826
50ED2BDE19BEAF7900A0AB90 /* UIEditBoxImpl-win32.cpp */,
68216827
292DB12F19B4574100A80320 /* UIEditBox.cpp */,
@@ -10540,6 +10546,7 @@
1054010546
15AE1BEB19AAE01E00C27E9E /* CCControlButton.h in Headers */,
1054110547
B6CAB1F81AF9AA1A00B9B856 /* btDbvt.h in Headers */,
1054210548
B665E35D1AA80A6500DDB1C5 /* CCPUOnRandomObserver.h in Headers */,
10549+
299B15AD1B93FC530008E196 /* UIEditBoxImpl-common.h in Headers */,
1054310550
1A570281180BCC900088DEC7 /* CCSprite.h in Headers */,
1054410551
B6DD2FD21B04825B00E47F5F /* DetourNode.h in Headers */,
1054510552
1A570285180BCC900088DEC7 /* CCSpriteBatchNode.h in Headers */,
@@ -12226,6 +12233,7 @@
1222612233
B6DD2FC41B04825B00E47F5F /* DetourNavMesh.cpp in Sources */,
1222712234
B6CAAFFB1AF9A9E100B9B856 /* CCPhysics3DWorld.cpp in Sources */,
1222812235
B665E3471AA80A6500DDB1C5 /* CCPUOnExpireObserverTranslator.cpp in Sources */,
12236+
299B15AC1B93FC530008E196 /* UIEditBoxImpl-common.cpp in Sources */,
1222912237
B6CAB2A41AF9AA1A00B9B856 /* btConvexHullShape.cpp in Sources */,
1223012238
15AE1AA519AAD40300C27E9E /* b2Fixture.cpp in Sources */,
1223112239
B29A7DE219EE1B7700872B35 /* MeshAttachment.c in Sources */,

cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBox.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ of this software and associated documentation files (the "Software"), to deal
2626

2727
import android.content.Context;
2828
import android.graphics.Color;
29+
import android.graphics.Typeface;
2930
import android.text.InputFilter;
3031
import android.text.InputType;
32+
import android.text.method.PasswordTransformationMethod;
33+
import android.util.Log;
3134
import android.view.Gravity;
3235
import android.view.KeyEvent;
3336
import android.view.View;
@@ -106,6 +109,12 @@ public class Cocos2dxEditBox extends EditText {
106109
private int mInputModeContraints;
107110
private int mMaxLength;
108111

112+
//OpenGL view scaleX
113+
private float mScaleX;
114+
115+
116+
117+
109118
public Cocos2dxEditBox(Context context){
110119
super(context);
111120
}
@@ -121,6 +130,15 @@ public void setEditBoxViewRect(int left, int top, int maxWidth, int maxHeight) {
121130
this.setLayoutParams(layoutParams);
122131
}
123132

133+
public float getOpenGLViewScaleX() {
134+
return mScaleX;
135+
}
136+
137+
public void setOpenGLViewScaleX(float mScaleX) {
138+
this.mScaleX = mScaleX;
139+
}
140+
141+
124142
public void setMaxLength(int maxLength){
125143
this.mMaxLength = maxLength;
126144

@@ -210,6 +228,8 @@ public void setInputFlag(int inputFlag) {
210228
switch (inputFlag) {
211229
case kEditBoxInputFlagPassword:
212230
this.mInputFlagConstraints = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
231+
this.setTypeface(Typeface.DEFAULT);
232+
this.setTransformationMethod(new PasswordTransformationMethod());
213233
break;
214234
case kEditBoxInputFlagSensitive:
215235
this.mInputFlagConstraints = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;

cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxEditBoxHelper.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,17 @@ public Cocos2dxEditBoxHelper(ResizeLayout layout) {
7575
Cocos2dxEditBoxHelper.mEditBoxArray = new SparseArray<Cocos2dxEditBox>();
7676
}
7777

78+
public static int convertToSP(float point){
79+
Resources r = mCocos2dxActivity.getResources();
7880

79-
public static int createEditBox(final int left, final int top, final int width, final int height) {
81+
int convertedValue = (int)TypedValue.applyDimension(
82+
TypedValue.COMPLEX_UNIT_SP, point, r.getDisplayMetrics());
83+
84+
return convertedValue;
85+
86+
}
87+
88+
public static int createEditBox(final int left, final int top, final int width, final int height, final float scaleX) {
8089
final int index = mViewTag;
8190
mCocos2dxActivity.runOnUiThread(new Runnable() {
8291
@Override
@@ -92,6 +101,17 @@ public void run() {
92101
editBox.setBackgroundColor(Color.TRANSPARENT);
93102
editBox.setTextColor(Color.WHITE);
94103
editBox.setSingleLine();
104+
editBox.setOpenGLViewScaleX(scaleX);
105+
Resources r = mCocos2dxActivity.getResources();
106+
float density = r.getDisplayMetrics().density;
107+
int paddingBottom = (int)(height * 0.33f / density);
108+
paddingBottom = convertToSP(paddingBottom - 5 * scaleX / density);
109+
paddingBottom = paddingBottom / 2;
110+
int paddingTop = paddingBottom;
111+
int paddingLeft = (int)(5 * scaleX / density);
112+
paddingLeft = convertToSP(paddingLeft);
113+
114+
editBox.setPadding(paddingLeft,paddingTop, 0, paddingBottom);
95115

96116

97117
FrameLayout.LayoutParams lParams = new FrameLayout.LayoutParams(
@@ -101,7 +121,7 @@ public void run() {
101121
lParams.leftMargin = left;
102122
lParams.topMargin = top;
103123
lParams.width = width;
104-
lParams.height = height+20;
124+
lParams.height = height;
105125
lParams.gravity = Gravity.TOP | Gravity.LEFT;
106126

107127
mFrameLayout.addView(editBox, lParams);
@@ -222,7 +242,10 @@ public void run() {
222242
}
223243
//TODO: The font size is not the same across all the anroid devices...
224244
if (fontSize >= 0){
225-
editBox.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize);
245+
float density = mCocos2dxActivity.getResources().getDisplayMetrics().density;
246+
// Log.e("XXX", "density is " + density);
247+
editBox.setTextSize(TypedValue.COMPLEX_UNIT_SP,
248+
fontSize / density );
226249
}
227250
editBox.setTypeface(tf);
228251
}

cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,12 @@ void deleteValueForKeyJNI(const char* key)
390390
}
391391
}
392392

393-
int addEditBoxJNI(int left, int top, int width, int height){
393+
int addEditBoxJNI(int left, int top, int width, int height, float scaleX){
394394
JniMethodInfo t;
395395

396396
int ret = -1;
397-
if (JniHelper::getStaticMethodInfo(t, EDITBOX_CLASS_NAME, "createEditBox", "(IIII)I")) {
398-
ret = t.env->CallStaticIntMethod(t.classID, t.methodID, left, top, width, height);
397+
if (JniHelper::getStaticMethodInfo(t, EDITBOX_CLASS_NAME, "createEditBox", "(IIIIF)I")) {
398+
ret = t.env->CallStaticIntMethod(t.classID, t.methodID, left, top, width, height, scaleX);
399399
t.env->DeleteLocalRef(t.classID);
400400
}
401401
return ret;

cocos/platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ extern void setStringForKeyJNI(const char* key, const char* value);
5555
extern void deleteValueForKeyJNI(const char* key);
5656
extern void conversionEncodingJNI(const char* src, int byteSize, const char* fromCharset, char* dst, const char* newCharset);
5757
//Added for new Android EditBox
58-
extern int addEditBoxJNI(int left, int top, int width, int height);
58+
extern int addEditBoxJNI(int left, int top, int width, int height, float scaleX);
5959
extern void removeEditBoxJNI(int index);
6060
extern void setEditBoxViewRectJNI(int index, int left, int top, int width, int height);
6161
extern void setMaxLengthJNI(int index, int maxLength);

cocos/ui/Android.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ UIWebView.cpp \
4242
UIWebViewImpl-android.cpp \
4343
UIEditBox/UIEditBox.cpp \
4444
UIEditBox/UIEditBoxImpl-android.cpp \
45+
UIEditBox/UIEditBoxImpl-common.cpp \
4546
UILayoutComponent.cpp \
4647

4748
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../editor-support

0 commit comments

Comments
 (0)