Improved text input support (fixes #58)

Now languages such as Vietnamese can also be typed on Telex format.
This commit is contained in:
Duy Tran Khanh committed 2021-02-24 17:08:38 +07:00
1 parent 884b8437a5
commit 8f28cb71a6
1 file changed
+98 -8
+98 -8
View File
@@ -10,6 +10,9 @@
#include "GLES2/gl2.h"
#include "GLES2/gl2ext.h"
// Debugging purposes
// #define DEBUG_VISIBLE_TEXT_FIELD
#define ADD_BUTTON(NAME, KEY, RECT) \
UIButton *button_##KEY = [UIButton buttonWithType:UIButtonTypeRoundedRect]; \
button_##KEY.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth; \
@@ -42,11 +45,19 @@
#define BTN_RECT 80.0, 30.0
#define BTN_SQUARE 50.0, 50.0
#define INPUT_SPACE_CHAR @" "
#define INPUT_SPACE_LENGTH 20
int togglableVisibleButtonIndex = -1;
UIButton* togglableVisibleButtons[100];
UIView *touchView;
#ifdef DEBUG_VISIBLE_TEXT_FIELD
UILabel *inputLengthView;
#endif
UITextField *inputView;
int inputTextLength;
BOOL shouldTriggerClick = NO;
int notchOffset;
@@ -94,10 +105,19 @@ int notchOffset;
[touchView addGestureRecognizer:longpressGesture];
[self.view addSubview:touchView];
#ifndef DEBUG_VISIBLE_TEXT_FIELD
inputView = [[UITextField alloc] initWithFrame:CGRectMake(5 * 3 + 80 * 2, 5, BTN_RECT)];
inputView.delegate = self;
inputView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.0f];
#else
inputView = [[UITextField alloc] initWithFrame:CGRectMake(5 * 3 + 80 * 2, 5 * 2 + 30, 200, 30)];
inputView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.5f];
inputLengthView = [[UILabel alloc] initWithFrame:CGRectMake(5 * 2 + 80, 5 * 2 + 30, BTN_RECT)];
inputLengthView.text = @"length=?";
inputLengthView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0.6f];
[self.view addSubview:inputLengthView];
#endif
inputView.delegate = self;
[inputView addTarget:self action:@selector(inputViewDidChange) forControlEvents:UIControlEventEditingChanged];
[inputView addTarget:self action:@selector(inputViewDidClick) forControlEvents:UIControlEventTouchDown];
@@ -170,6 +190,9 @@ int notchOffset;
int hotbarItem = callback_SurfaceViewController_touchHotbar(location.x * screenScale, location.y * screenScale);
if (hotbarItem == -1) {
inputView.text = INPUT_SPACE_CHAR;
inputTextLength = 0;
Java_org_lwjgl_glfw_CallbackBridge_nativeSendMouseButton(NULL, NULL,
isGrabbing == JNI_TRUE ? GLFW_MOUSE_BUTTON_RIGHT : GLFW_MOUSE_BUTTON_LEFT, 1, 0);
Java_org_lwjgl_glfw_CallbackBridge_nativeSendMouseButton(NULL, NULL,
@@ -192,6 +215,9 @@ int notchOffset;
int hotbarItem = callback_SurfaceViewController_touchHotbar(location.x * screenScale, location.y * screenScale);
if (sender.state == UIGestureRecognizerStateBegan) {
if (hotbarItem == -1) {
inputView.text = INPUT_SPACE_CHAR;
inputTextLength = 0;
Java_org_lwjgl_glfw_CallbackBridge_nativeSendMouseButton(NULL, NULL, GLFW_MOUSE_BUTTON_LEFT, 1, 0);
} else {
Java_org_lwjgl_glfw_CallbackBridge_nativeSendKey(NULL, NULL, GLFW_KEY_Q, 0, 1, 0);
@@ -206,6 +232,9 @@ int notchOffset;
|| sender.state == UIGestureRecognizerStateEnded)
{
if (hotbarItem == -1) {
inputView.text = INPUT_SPACE_CHAR;
inputTextLength = 0;
Java_org_lwjgl_glfw_CallbackBridge_nativeSendMouseButton(NULL, NULL, GLFW_MOUSE_BUTTON_LEFT, 0, 0);
} else {
Java_org_lwjgl_glfw_CallbackBridge_nativeSendKey(NULL, NULL, GLFW_KEY_Q, 0, 0, 0);
@@ -214,27 +243,86 @@ int notchOffset;
}
}
NSString* inputStringBefore;
-(void)inputViewDidChange {
if ([inputView.text length] <= 1) {
if ([inputView.text length] < INPUT_SPACE_LENGTH) {
Java_org_lwjgl_glfw_CallbackBridge_nativeSendKey(NULL, NULL, GLFW_KEY_BACKSPACE, 0, 1, 0);
Java_org_lwjgl_glfw_CallbackBridge_nativeSendKey(NULL, NULL, GLFW_KEY_BACKSPACE, 0, 0, 0);
} else {
NSString *newText = [inputView.text substringFromIndex:2];
inputView.text = [@" " stringByAppendingString:inputView.text];
if (inputTextLength > 0) {
--inputTextLength;
}
#ifdef DEBUG_VISIBLE_TEXT_FIELD
inputLengthView.text = [@"length=" stringByAppendingFormat:@"%i",
inputTextLength];
#endif
} else if ([inputView.text length] > INPUT_SPACE_LENGTH) {
NSString *newText = [inputView.text substringFromIndex:INPUT_SPACE_LENGTH];
int charLength = [newText length];
for (int i = 0; i < charLength; i++) {
// 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);
inputView.text = [inputView.text substringFromIndex:1];
if (inputTextLength < INPUT_SPACE_LENGTH) {
++inputTextLength;
}
#ifdef DEBUG_VISIBLE_TEXT_FIELD
inputLengthView.text = [@"length=" stringByAppendingFormat:@"%i",
inputTextLength];
#endif
}
inputStringBefore = inputView.text;
// [inputView.text substringFromIndex:inputTextLength - 1];
} else {
#ifdef DEBUG_VISIBLE_TEXT_FIELD
NSLog(@"Compare \"%@\" vs \"%@\"", inputView.text, inputStringBefore);
#endif
for (int i = 0; i < INPUT_SPACE_LENGTH; i++) {
if ([inputView.text characterAtIndex:i] != [inputStringBefore characterAtIndex:i]) {
NSString *inputStringNow = [inputView.text substringFromIndex:i];
/*
inputView.text = [inputView.text substringToIndex:i];
// self notify
[self inputViewDidChange];
inputView.text = [inputView.text stringByAppendingString:inputStringNow];
// self notify
[self inputViewDidChange];
*/
for (int i2 = 0; i2 < [inputStringNow length]; i2++) {
Java_org_lwjgl_glfw_CallbackBridge_nativeSendKey(NULL, NULL, GLFW_KEY_BACKSPACE, 0, 1, 0);
Java_org_lwjgl_glfw_CallbackBridge_nativeSendKey(NULL, NULL, GLFW_KEY_BACKSPACE, 0, 0, 0);
}
for (int i2 = 0; i2 < [inputStringNow length]; i2++) {
Java_org_lwjgl_glfw_CallbackBridge_nativeSendCharMods(NULL, NULL, (jchar) [inputStringNow characterAtIndex:i2], /* mods */ 0);
}
break;
}
}
#ifdef DEBUG_VISIBLE_TEXT_FIELD
inputLengthView.text = @"length =";
#endif
inputStringBefore = inputView.text;
// [inputView.text substringFromIndex:inputTextLength - 1];
}
// Reset to default value
inputView.text = @" ";
// inputView.text = INPUT_SPACE_CHAR;
}
-(void)inputViewDidClick {
// Zero the input field so user will no longer able to select text inside.
#ifndef DEBUG_VISIBLE_TEXT_FIELD
inputView.alpha = 0.0f;
inputView.text = @" ";
#endif
inputView.text = INPUT_SPACE_CHAR;
inputTextLength = 0;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
@@ -250,8 +338,10 @@ ADD_BUTTON_DEF(special_togglebtn) {
for (int i = 0; i < togglableVisibleButtonIndex + 1; i++) {
togglableVisibleButtons[i].hidden = currentVisibility;
}
#ifndef DEBUG_VISIBLE_TEXT_FIELD
inputView.hidden = currentVisibility;
#endif
}
}