Skip to content

Commit

Permalink
Sync the text box input between NME and android so that if the box is…
Browse files Browse the repository at this point in the history
… ever seen, it looks correct (android will show this box if it thinks the display region is too small). This also allows for using the native text box in the future. Only show the text box for TextFields, and allow 'Dumb' keys otherwise.
  • Loading branch information
hughsando committed May 22, 2016
1 parent 936264e commit 8666ed6
Show file tree
Hide file tree
Showing 15 changed files with 237 additions and 73 deletions.
13 changes: 12 additions & 1 deletion project/include/Display.h
Expand Up @@ -339,6 +339,14 @@ class SimpleButton : public DisplayObjectContainer

double GetTimeStamp();

enum PopupKeyboardMode
{
pkmOff = 0x0000,
pkmDumb = 0x0001,
pkmSmart = 0x0002,
pkmNative = 0x0003,
};

class Stage : public DisplayObjectContainer
{
public:
Expand Down Expand Up @@ -367,7 +375,8 @@ class Stage : public DisplayObjectContainer
virtual void SetScreenMode(ScreenMode mode) { }
virtual void ShowCursor(bool inShow) { };
virtual void SetCursor(Cursor inCursor)=0;
virtual void EnablePopupKeyboard(bool inEnable) { }
virtual void PopupKeyboard(PopupKeyboardMode inMode, WString *inValue=0) { };
virtual void SetPopupTextSelection(int inSel0, int inSel1) { }
double GetNextWake() { return mNextWake; }
virtual void SetNextWakeDelay(double inNextWake);

Expand All @@ -378,6 +387,8 @@ class Stage : public DisplayObjectContainer
virtual uint32 getBackgroundMask() { return 0xffffffff; }

virtual const char *getJoystickName(int id) { return NULL; }
virtual void onTextFieldText(const std::string &inText, int inPos0, int inPos1);


Matrix GetFullMatrix(bool inStageScaling);
bool FinishEditOnEnter();
Expand Down
3 changes: 3 additions & 0 deletions project/include/TextField.h
Expand Up @@ -157,6 +157,7 @@ class TextField : public DisplayObject
void EndDrag(Event &inEvent);
void OnKey(Event &inEvent);
void OnScrollWheel(int inDirection);
void onTextUpdate(const std::string &inText, int inPos0, int inPos1);
void DeleteSelection();
void ClearSelection();
void CopySelection();
Expand All @@ -169,6 +170,8 @@ class TextField : public DisplayObject

bool CaretOn();
bool IsCacheDirty();
void SyncSelection();
void Focus();



Expand Down
36 changes: 33 additions & 3 deletions project/src/android/AndroidFrame.cpp
Expand Up @@ -336,6 +336,7 @@ class AndroidStage : public Stage
}
}


void OnJoy(int inDeviceId, int inCode, bool inDown)
{
//__android_log_print(ANDROID_LOG_INFO, "NME", "OnJoy %d %d %d", inDeviceId, inCode, inDown);
Expand Down Expand Up @@ -451,15 +452,32 @@ class AndroidStage : public Stage
//__android_log_print(ANDROID_LOG_INFO, "NME", "Accelerometer %f %f %f", inX, inY, inZ);
}

void EnablePopupKeyboard(bool inEnable)
void PopupKeyboard(PopupKeyboardMode inMode,WString *inValue)
{
JNIEnv *env = GetEnv();
jclass cls = FindClass("org/haxe/nme/GameActivity");
jmethodID mid = env->GetStaticMethodID(cls, "showKeyboard", "(Z)V");
jstring str = 0;
if (inValue)
{
std::string cstr = WideToUTF8(*inValue);
str = env->NewStringUTF( cstr.c_str() );
}
jmethodID mid = env->GetStaticMethodID(cls, "popupKeyboard", "(ILjava/lang/String;)V");
if (mid == 0)
return;

env->CallStaticVoidMethod(cls, mid, (jboolean) inEnable);
env->CallStaticVoidMethod(cls, mid, (int)inMode, str);
}

void SetPopupTextSelection(int inSel0, int inSel1)
{
JNIEnv *env = GetEnv();
jclass cls = FindClass("org/haxe/nme/GameActivity");
jmethodID mid = env->GetStaticMethodID(cls, "setPopupSelection", "(II)V");
if (mid == 0)
return;

env->CallStaticVoidMethod(cls, mid, inSel0, inSel1);
}

bool getMultitouchSupported() { return true; }
Expand Down Expand Up @@ -833,6 +851,18 @@ JAVA_EXPORT int JNICALL Java_org_haxe_nme_NME_onKeyChange(JNIEnv * env, jobject
return nme::GetResult();
}


JAVA_EXPORT int JNICALL Java_org_haxe_nme_NME_onText(JNIEnv * env, jobject obj, jstring inText, int inReplacePos, int inReplaceLen)
{
AutoHaxe haxe("onText");
if (nme::sStage)
{
std::string text = JStringToStdString(env, inText, false);
nme::sStage->onTextFieldText(text,inReplacePos,inReplaceLen);
}
return nme::GetResult();
}

JAVA_EXPORT int JNICALL Java_org_haxe_nme_NME_onJoyChange(JNIEnv * env, jobject obj, int deviceId, int code, bool down)
{
AutoHaxe haxe("onJoy");
Expand Down
4 changes: 2 additions & 2 deletions project/src/common/Display.cpp
Expand Up @@ -674,7 +674,7 @@ void DisplayObject::Focus()
{
Stage *stage = getStage();
if (stage)
stage->EnablePopupKeyboard(true);
stage->PopupKeyboard(pkmDumb);
}
#endif
}
Expand All @@ -686,7 +686,7 @@ void DisplayObject::Unfocus()
{
Stage *stage = getStage();
if (stage)
stage->EnablePopupKeyboard(false);
stage->PopupKeyboard(pkmOff);
}
#endif
}
Expand Down
4 changes: 2 additions & 2 deletions project/src/common/ExternalInterface.cpp
Expand Up @@ -2296,7 +2296,7 @@ value nme_display_object_request_soft_keyboard(value inObj)
if (stage)
{
// TODO: return whether it pops up
stage->EnablePopupKeyboard(true);
stage->PopupKeyboard(pkmDumb);
return alloc_bool(true);
}
}
Expand All @@ -2315,7 +2315,7 @@ value nme_display_object_dismiss_soft_keyboard(value inObj)
if (stage)
{
// TODO: return whether it pops up
stage->EnablePopupKeyboard(false);
stage->PopupKeyboard(pkmOff);
return alloc_bool(true);
}
}
Expand Down
14 changes: 12 additions & 2 deletions project/src/common/Stage.cpp
Expand Up @@ -283,7 +283,7 @@ void Stage::HandleEvent(Event &inEvent)
#if defined(IPHONE) || defined(ANDROID) || defined(WEBOS) || defined(TIZEN)
else
{
EnablePopupKeyboard(false);
PopupKeyboard(pkmOff);
SetFocusObject(0,fsMouse);
}
#endif
Expand All @@ -308,7 +308,7 @@ void Stage::HandleEvent(Event &inEvent)
else if (inEvent.type==etMouseClick || inEvent.type==etMouseDown ||
(inEvent.type==etTouchBegin && (inEvent.flags & efPrimaryTouch) ))
{
EnablePopupKeyboard(false);
PopupKeyboard(pkmOff);
SetFocusObject(0);
}
#endif
Expand All @@ -318,6 +318,16 @@ void Stage::HandleEvent(Event &inEvent)
hit_obj->DecRef();
}

void Stage::onTextFieldText(const std::string &inText, int inPos0, int inPos1)
{
if (mFocusObject)
{
TextField *field = dynamic_cast<TextField *>(mFocusObject);
if (field)
field->onTextUpdate(inText, inPos0, inPos1);
}
}

void Stage::setOpaqueBackground(uint32 inBG)
{
opaqueBackground = inBG | 0xff000000;
Expand Down
64 changes: 61 additions & 3 deletions project/src/common/TextField.cpp
Expand Up @@ -542,6 +542,33 @@ void TextField::setSelection(int inStartIndex, int inEndIndex)
mCaretDirty = true;
mGfxDirty = true;
DirtyCache();

SyncSelection();
}

void TextField::SyncSelection()
{
Stage *stage = getStage();
if (stage && stage->GetFocusObject()==this)
{
if (mSelectMin<mSelectMax)
stage->SetPopupTextSelection(mSelectMin, mSelectMax);
else
stage->SetPopupTextSelection(caretIndex,caretIndex);
}
}


void TextField::Focus()
{
#if defined(IPHONE) || defined (ANDROID) || defined(WEBOS) || defined(BLACKBERRY) || defined(TIZEN)
if (needsSoftKeyboard)
{
WString value = getText();
getStage()->PopupKeyboard(pkmSmart,&value);
SyncSelection();
}
#endif
}


Expand All @@ -564,9 +591,6 @@ bool TextField::CaptureDown(Event &inEvent)
{
if (selectable || isInput)
{
if (selectable && isInput)
getStage()->EnablePopupKeyboard(true);

UserPoint point = GlobalToLocal(UserPoint( inEvent.x, inEvent.y));
int pos = PointToChar(point);
caretIndex = pos;
Expand All @@ -579,6 +603,14 @@ bool TextField::CaptureDown(Event &inEvent)
mGfxDirty = true;
DirtyCache();
}

if (selectable && isInput)
{
WString value = getText();
getStage()->PopupKeyboard(pkmSmart,&value);
SyncSelection();
}

}
return true;
}
Expand Down Expand Up @@ -620,6 +652,7 @@ void TextField::Drag(Event &inEvent)
mTilesDirty = true;
mCaretDirty = true;
DirtyCache();
SyncSelection();
}
}

Expand Down Expand Up @@ -675,6 +708,31 @@ void TextField::PasteSelection()
InsertString(UTF8ToWide(GetClipboardText()));
}

void TextField::onTextUpdate(const std::string &inText, int inPos0, int inPos1)
{
if (inPos1>inPos0)
{
mSelectMin = inPos0;
mSelectMax = inPos1;
DeleteSelection();
}
else
caretIndex = inPos0;

Stage *stage = getStage();
if (stage)
{
Event onText(etChar);
onText.utf8Text = inText.c_str();
onText.utf8Length = inText.size();
onText.id = getID();
stage->HandleEvent(onText);
}

InsertString(UTF8ToWide(inText));
}



void TextField::OnKey(Event &inEvent)
{
Expand Down
10 changes: 5 additions & 5 deletions project/src/iPhone/UIStageView.mm
Expand Up @@ -164,7 +164,7 @@ - (void) mainLoop:(id) sender;
bool isOpenGL() const { return nmeView->mOGLContext; }
Surface *GetPrimarySurface() { return nmeView->mHardwareSurface; }
void SetCursor(nme::Cursor) { /* No cursors on iPhone ! */ }
void EnablePopupKeyboard(bool inEnable);
void PopupKeyboard(PopupKeyboardMode inEnable,WString *);
double getDPIScale() { return nmeView->dpiScale; }

int getWindowFrameBufferId() { return nmeView->defaultFramebuffer; };
Expand Down Expand Up @@ -1836,10 +1836,10 @@ -(void)removeMovieNotificationHandlers
[nmeView tearDown];
}

void NMEStage::EnablePopupKeyboard(bool inEnable)
void NMEStage::PopupKeyboard(PopupKeyboardMode inMode, WString *)
{
popupEnabled = inEnable;
[ nmeView enableKeyboard:inEnable];
popupEnabled = inEnable!=pkmOff;
[ nmeView enableKeyboard:popupEnabled];
}


Expand Down Expand Up @@ -2139,7 +2139,7 @@ - (void) dealloc

void EnableKeyboard(bool inEnable)
{
sgNmeStage->EnablePopupKeyboard(inEnable);
sgNmeStage->PopupKeyboard(inEnable ? pkmDumb : pkmOff);
}


Expand Down
6 changes: 3 additions & 3 deletions project/src/sdl/SDLStage.cpp
Expand Up @@ -628,13 +628,13 @@ class SDLStage : public Stage
SDL_WarpMouse( inX, inY );
}

void EnablePopupKeyboard (bool enabled) {
void PopupKeyboard(PopupKeyboardMode inMode, WString *) {

#ifdef WEBOS

if (PDL_GetPDKVersion () >= 300) {

if (enabled) {
if (inMode) {

PDL_SetKeyboardState (PDL_TRUE);

Expand All @@ -650,7 +650,7 @@ class SDLStage : public Stage

#ifdef BLACKBERRY

if (enabled) {
if (inMode) {

virtualkeyboard_show();

Expand Down
5 changes: 0 additions & 5 deletions project/src/sdl2/SDL2Stage.cpp
Expand Up @@ -680,11 +680,6 @@ class SDLStage : public Stage
}


void EnablePopupKeyboard(bool enabled)
{

}


bool getMultitouchSupported()
{
Expand Down
5 changes: 0 additions & 5 deletions project/src/winrt/WinRTStage.cpp
Expand Up @@ -407,11 +407,6 @@ class WinRTStage : public Stage, public Direct3DBase
}


void EnablePopupKeyboard(bool enabled)
{
}


bool getMultitouchSupported()
{
return true;
Expand Down
1 change: 0 additions & 1 deletion samples/02-Text/Sample.nmml
Expand Up @@ -18,7 +18,6 @@
<window
width="${WIDTH}"
height="${HEIGHT}"
orientation="landscape"
fps="60"
background="0xffffff"
resizable="true"
Expand Down
Expand Up @@ -26,6 +26,7 @@ public class NME {
public static native int onTrackball(float x,float y);
public static native int onJoyChange(int inDeviceID, int inCode, boolean inIsDown);
public static native int onKeyChange(int inKeyCode, int inCharCode, boolean inIsDown, boolean isChar);
public static native int onText(String inNewText, int inReplacePos, int inReplaceLength);
public static native int onRender();
public static native int onPoll();
public static native double getNextWake();
Expand Down

0 comments on commit 8666ed6

Please sign in to comment.