Skip to content

Commit

Permalink
[Keyboard] Correct the return button behavior
Browse files Browse the repository at this point in the history
- Pressing return will send the Enter key to game, but not dismiss the
keyboard since some input boxes in game could be multi-line.
- Press Keyboard (hide keyboard button) no longer trigger Enter key.
  • Loading branch information
khanhduytran0 committed Feb 12, 2021
1 parent de3d9a9 commit 111e8f9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Natives/LoginViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#define TYPE_MOJANG 2
#define TYPE_OFFLINE 3

void loginAccountInput(UINavigationController *controller, int type, char* username_c, char* password_c) {
void loginAccountInput(UINavigationController *controller, int type, const char* username_c, const char* password_c) {
JNIEnv *env;
(*runtimeJavaVMPtr)->AttachCurrentThread(runtimeJavaVMPtr, &env, NULL);

Expand Down
32 changes: 13 additions & 19 deletions Natives/SurfaceViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ - (void)executebtn_##KEY:(int)held

// TODO: key modifiers impl

@interface SurfaceViewController () {
@interface SurfaceViewController ()<UITextFieldDelegate> {
}

@property (strong, nonatomic) MGLContext *context;
Expand Down Expand Up @@ -89,7 +89,7 @@ - (void)viewDidLoad
[self.view addSubview:touchView];

inputView = [[UITextField alloc] initWithFrame:CGRectMake(5 * 3 + 80 * 2, 5, BTN_RECT)];

inputView.delegate = self;
inputView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.0f];
[inputView addTarget:self action:@selector(inputViewDidChange) forControlEvents:UIControlEventEditingChanged];
[inputView addTarget:self action:@selector(inputViewDidClick) forControlEvents:UIControlEventTouchDown];
Expand Down Expand Up @@ -210,15 +210,9 @@ -(void)inputViewDidChange {
} else {
NSString *newText = [inputView.text substringFromIndex:2];
int charLength = [newText length];
//char16_t *charText = [newText UTF16String];
for (int i = 0; i < charLength; i++) {
unichar currChar = [newText characterAtIndex:i];
if (currChar == '\n') {
Java_org_lwjgl_glfw_CallbackBridge_nativeSendKey(NULL, NULL, GLFW_KEY_ENTER, 0, 1, 0);
Java_org_lwjgl_glfw_CallbackBridge_nativeSendKey(NULL, NULL, GLFW_KEY_ENTER, 0, 0, 0);
} else {
Java_org_lwjgl_glfw_CallbackBridge_nativeSendCharMods(NULL, NULL, (jchar) currChar /* charText[i] */, /* mods */ 0);
}
// Directly convert unichar to jchar which both are in UTF-16 encoding.
Java_org_lwjgl_glfw_CallbackBridge_nativeSendCharMods(NULL, NULL, (jchar) [newText characterAtIndex:i] /* charText[i] */, /* mods */ 0);
}
}

Expand All @@ -232,13 +226,21 @@ -(void)inputViewDidClick {
inputView.text = @" ";
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
Java_org_lwjgl_glfw_CallbackBridge_nativeSendKey(NULL, NULL, GLFW_KEY_ENTER, 0, 1, 0);
Java_org_lwjgl_glfw_CallbackBridge_nativeSendKey(NULL, NULL, GLFW_KEY_ENTER, 0, 0, 0);
return YES;
}

int currentVisibility = 1;
ADD_BUTTON_DEF(special_togglebtn) {
if (held == 0) {
currentVisibility = !currentVisibility;
for (int i = 0; i < togglableVisibleButtonIndex + 1; i++) {
togglableVisibleButtons[i].hidden = currentVisibility;
}

inputView.hidden = currentVisibility;
}
}

Expand Down Expand Up @@ -277,13 +279,6 @@ -(void)inputViewDidClick {
ADD_BUTTON_DEF_KEY(space, GLFW_KEY_SPACE)
ADD_BUTTON_DEF_KEY(escape, GLFW_KEY_ESCAPE)

/*
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
*/

- (void)dealloc
{
if ([MGLContext currentContext] == self.context) {
Expand Down Expand Up @@ -369,5 +364,4 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
[self sendTouchEvent: touches withEvent: ACTION_UP];
}

// #pragma mark - GLKView and GLKViewController delegate methods
@end
@end
2 changes: 1 addition & 1 deletion Natives/input_bridge_v3.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void JNI_OnUnload(JavaVM* vm, void* reserved) {
#define ADD_CALLBACK_WWIN(NAME) \
GLFW_invoke_##NAME##_func* GLFW_invoke_##NAME; \
JNIEXPORT jlong JNICALL Java_org_lwjgl_glfw_GLFW_nglfwSet##NAME##Callback(JNIEnv * env, jclass cls, jlong window, jlong callbackptr) { \
void** oldCallback = &GLFW_invoke_##NAME; \
void** oldCallback = (void**) &GLFW_invoke_##NAME; \
GLFW_invoke_##NAME = (GLFW_invoke_##NAME##_func*) (uintptr_t) callbackptr; \
return (jlong) (uintptr_t) *oldCallback; \
}
Expand Down

0 comments on commit 111e8f9

Please sign in to comment.