Fix ImString auto-resize when during the copy-paste

This commit is contained in:
SpaiR committed 2020-05-20 00:51:44 +03:00
1 parent 1998f98ccb
commit fe89f8dd81
2 files changed
+41 -24

No files matched your search

+36 -24
View File
@@ -2575,16 +2575,19 @@ public final class ImGui {
// - Most of the ImGuiInputTextFlags flags are only useful for InputText() and not for InputFloatX, InputIntX, InputDouble etc. // - Most of the ImGuiInputTextFlags flags are only useful for InputText() and not for InputFloatX, InputIntX, InputDouble etc.
/*JNI /*JNI
jmethodID jImStringResizeInternalMID;
jfieldID imTextInputDataSizeID; jfieldID imTextInputDataSizeID;
jfieldID imTextInputDataIsDirtyID; jfieldID imTextInputDataIsDirtyID;
jfieldID imTextInputDataIsResizedID; jfieldID imTextInputDataIsResizedID;
int resizeValue;
struct InputTextCallbackUserData { struct InputTextCallbackUserData {
JNIEnv* env; JNIEnv* env;
jobject* imString;
int maxSize;
jbyteArray jResizedBuf;
char* resizedBuf;
jobject* textInputData; jobject* textInputData;
char* buf;
char* allowedChars; char* allowedChars;
}; };
@@ -2603,6 +2606,19 @@ public final class ImGui {
} }
return found ? 0 : 1; return found ? 0 : 1;
} }
} else if (data->EventFlag == ImGuiInputTextFlags_CallbackResize) {
int newSize = data->BufTextLen;
if (newSize >= userData->maxSize) {
JNIEnv* env = userData->env;
jbyteArray newBufArr = (jbyteArray)env->CallObjectMethod(*userData->imString, jImStringResizeInternalMID, newSize);
char* newBuf = (char*)env->GetPrimitiveArrayCritical(newBufArr, 0);
data->Buf = newBuf;
userData->jResizedBuf = newBufArr;
userData->resizedBuf = newBuf;
}
} }
return 0; return 0;
@@ -2614,10 +2630,9 @@ public final class ImGui {
imTextInputDataSizeID = env->GetFieldID(jImInputTextDataClass, "size", "I"); imTextInputDataSizeID = env->GetFieldID(jImInputTextDataClass, "size", "I");
imTextInputDataIsDirtyID = env->GetFieldID(jImInputTextDataClass, "isDirty", "Z"); imTextInputDataIsDirtyID = env->GetFieldID(jImInputTextDataClass, "isDirty", "Z");
imTextInputDataIsResizedID = env->GetFieldID(jImInputTextDataClass, "isResized", "Z"); imTextInputDataIsResizedID = env->GetFieldID(jImInputTextDataClass, "isResized", "Z");
*/
private static native int nGetResizeValue(); /* jclass jImString = env->FindClass("imgui/ImString");
return resizeValue; jImStringResizeInternalMID = env->GetMethodID(jImString, "resizeInternal", "(I)[B");
*/ */
public static boolean inputText(String label, ImString text) { public static boolean inputText(String label, ImString text) {
@@ -2651,28 +2666,23 @@ public final class ImGui {
flags |= ImGuiInputTextFlags.CallbackResize; flags |= ImGuiInputTextFlags.CallbackResize;
} }
final boolean hasInput = nInputText(multiline, label, text.data, text.data.length, width, height, flags, inputData, inputData.allowedChars); if (!inputData.allowedChars.isEmpty()) {
flags |= ImGuiInputTextFlags.CallbackCharFilter;
if (inputData.isResized) {
inputData.isResized = false;
final int resizeValue = nGetResizeValue();
text.resize(resizeValue + inputData.resizeFactor);
inputData.size = resizeValue;
} }
return hasInput; return nInputText(multiline, label, text, text.data, text.data.length, width, height, flags, inputData, inputData.allowedChars);
} }
private static native boolean nInputText(boolean multiline, String label, byte[] buf, int maxSize, float width, float height, int flags, ImGuiInputTextData textInputData, String allowedChars); /* private static native boolean nInputText(boolean multiline, String label, ImString imString, byte[] buf, int maxSize, float width, float height, int flags, ImGuiInputTextData textInputData, String allowedChars); /*
InputTextCallbackUserData userData; InputTextCallbackUserData userData;
userData.imString = &imString;
userData.maxSize = maxSize;
userData.jResizedBuf = NULL;
userData.resizedBuf = NULL;
userData.textInputData = &textInputData; userData.textInputData = &textInputData;
userData.env = env; userData.env = env;
userData.buf = buf;
userData.allowedChars = allowedChars; userData.allowedChars = allowedChars;
if (strlen(allowedChars) > 0)
flags |= ImGuiInputTextFlags_CallbackCharFilter;
bool valueChanged; bool valueChanged;
if (multiline) { if (multiline) {
@@ -2681,14 +2691,16 @@ public final class ImGui {
valueChanged = ImGui::InputText(label, buf, maxSize, flags, &TextEditCallbackStub, &userData); valueChanged = ImGui::InputText(label, buf, maxSize, flags, &TextEditCallbackStub, &userData);
} }
int size = strlen(buf); if (valueChanged) {
int size;
if ((flags & ImGuiInputTextFlags_CallbackResize) && ((size + 1) > maxSize)) { if (userData.jResizedBuf != NULL) {
env->SetBooleanField(textInputData, imTextInputDataIsResizedID, true); size = strlen(userData.resizedBuf);
resizeValue = size; env->ReleasePrimitiveArrayCritical(userData.jResizedBuf, userData.resizedBuf, 0);
} else {
size = strlen(buf);
} }
if (valueChanged) {
env->SetIntField(textInputData, imTextInputDataSizeID, size); env->SetIntField(textInputData, imTextInputDataSizeID, size);
env->SetBooleanField(textInputData, imTextInputDataIsDirtyID, true); env->SetBooleanField(textInputData, imTextInputDataIsDirtyID, true);
} }
@@ -107,6 +107,11 @@ public final class ImString {
data = newBuffer; data = newBuffer;
} }
byte[] resizeInternal(final int newSize) {
resize(newSize + inputData.resizeFactor);
return data;
}
/** /**
* Get the length of the text inside of the data buffer. * Get the length of the text inside of the data buffer.
* @return length of the text inside of the data buffer * @return length of the text inside of the data buffer