Add basic support of Java<->Native callbacks
Implement them for ImGuiIO::GetClipboardTextFn and ImGuiIO::SetClipboardTextFn
This commit is contained in:
1 parent
168c7602dd
commit
1c037e826a
9 files changed
+131
-3
No files matched your search
@@ -18,7 +18,7 @@ public final class ImGui {
|
||||
|
||||
static {
|
||||
System.loadLibrary(System.getProperty(LIB_NAME_PROP, LIB_NAME_DEFAULT));
|
||||
initJniCommon();
|
||||
initJni();
|
||||
ImDrawList.nInit();
|
||||
ImDrawData.nInit();
|
||||
nInitInputTextData();
|
||||
@@ -31,10 +31,12 @@ public final class ImGui {
|
||||
#include <stdint.h>
|
||||
#include <imgui.h>
|
||||
#include "jni_common.h"
|
||||
#include "jni_callbacks.h"
|
||||
*/
|
||||
|
||||
private static native void initJniCommon(); /*
|
||||
private static native void initJni(); /*
|
||||
Jni::InitCommon(env);
|
||||
Jni::InitCallbacks(env);
|
||||
*/
|
||||
|
||||
// Context creation and access
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package imgui;
|
||||
|
||||
import imgui.callbacks.ImStrConsumer;
|
||||
import imgui.callbacks.ImStrSupplier;
|
||||
|
||||
/**
|
||||
* Communicate most settings and inputs/outputs to Dear ImGui using this structure.
|
||||
* Access via ImGui::GetIO(). Read 'Programmer guide' section in .cpp file for general usage.
|
||||
@@ -11,6 +14,7 @@ public final class ImGuiIO {
|
||||
/*JNI
|
||||
#include <imgui.h>
|
||||
#include "jni_common.h"
|
||||
#include "jni_callbacks.h"
|
||||
*/
|
||||
|
||||
//------------------------------------------------------------------
|
||||
@@ -253,7 +257,41 @@ public final class ImGuiIO {
|
||||
ImGui::GetIO().BackendRendererName = obj_backendRendererName == NULL ? NULL : (char*)env->GetStringUTFChars(obj_backendRendererName, JNI_FALSE);
|
||||
*/
|
||||
|
||||
// TODO clipboard callback
|
||||
// Optional: Access OS clipboard
|
||||
// (default to use native Win32 clipboard on Windows, otherwise uses a private clipboard. Override to access OS clipboard on other architectures)
|
||||
|
||||
/*JNI
|
||||
jobject _setClipboardTextCallback = nullptr;
|
||||
jobject _getClipboardTextCallback = nullptr;
|
||||
|
||||
void setClipboardTextStub(void* userData, const char* text) {
|
||||
Jni::CallImStrConsumer(Jni::GetEnv(), _setClipboardTextCallback, text);
|
||||
}
|
||||
|
||||
const char* getClipboardTextStub(void* user_data) {
|
||||
JNIEnv* env = Jni::GetEnv();
|
||||
jstring jstr = Jni::CallImStrSupplier(env, _getClipboardTextCallback);
|
||||
return env->GetStringUTFChars(jstr, 0);
|
||||
}
|
||||
*/
|
||||
|
||||
public native void setSetClipboardTextFn(ImStrConsumer setClipboardTextCallback); /*
|
||||
if (_setClipboardTextCallback != nullptr) {
|
||||
env->DeleteGlobalRef(_setClipboardTextCallback);
|
||||
}
|
||||
|
||||
_setClipboardTextCallback = env->NewGlobalRef(setClipboardTextCallback);
|
||||
ImGui::GetIO().SetClipboardTextFn = setClipboardTextStub;
|
||||
*/
|
||||
|
||||
public native void setGetClipboardTextFn(ImStrSupplier getClipboardTextCallback); /*
|
||||
if (_getClipboardTextCallback != nullptr) {
|
||||
env->DeleteGlobalRef(_getClipboardTextCallback);
|
||||
}
|
||||
|
||||
_getClipboardTextCallback = env->NewGlobalRef(getClipboardTextCallback);
|
||||
ImGui::GetIO().GetClipboardTextFn = getClipboardTextStub;
|
||||
*/
|
||||
|
||||
//------------------------------------------------------------------
|
||||
// Input - Fill before calling NewFrame()
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package imgui.callbacks;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Callback to supply Java string form native code.
|
||||
*/
|
||||
public abstract class ImStrConsumer implements Consumer<String> {
|
||||
@Override
|
||||
public abstract void accept(String str);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package imgui.callbacks;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Callback to get Java string from native code.
|
||||
*/
|
||||
public abstract class ImStrSupplier implements Supplier<String> {
|
||||
@Override
|
||||
public abstract String get();
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#include "jni_callbacks.h"
|
||||
|
||||
jmethodID jImStrConsumerAcceptMID;
|
||||
jmethodID jImStrSupplierGetMID;
|
||||
|
||||
namespace Jni
|
||||
{
|
||||
void InitCallbacks(JNIEnv* env) {
|
||||
jclass jImStrConsumer = env->FindClass("imgui/callbacks/ImStrConsumer");
|
||||
jImStrConsumerAcceptMID = env->GetMethodID(jImStrConsumer, "accept", "(Ljava/lang/String;)V");
|
||||
|
||||
jclass jImStrSupplier = env->FindClass("imgui/callbacks/ImStrSupplier");
|
||||
jImStrSupplierGetMID = env->GetMethodID(jImStrSupplier, "get", "()Ljava/lang/String;");
|
||||
}
|
||||
|
||||
void CallImStrConsumer(JNIEnv* env, jobject consumer, const char* str) {
|
||||
env->CallVoidMethod(consumer, jImStrConsumerAcceptMID, env->NewStringUTF(str));
|
||||
}
|
||||
|
||||
jstring CallImStrSupplier(JNIEnv* env, jobject supplier) {
|
||||
return (jstring)env->CallObjectMethod(supplier, jImStrSupplierGetMID);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#include <jni.h>
|
||||
|
||||
#ifndef JNI_CALLBACK_H
|
||||
#define JNI_CALLBACK_H
|
||||
|
||||
namespace Jni
|
||||
{
|
||||
void InitCallbacks(JNIEnv* env);
|
||||
|
||||
void CallImStrConsumer(JNIEnv* env, jobject consumer, const char* str);
|
||||
jstring CallImStrSupplier(JNIEnv* env, jobject supplier);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -8,9 +8,13 @@ jfieldID imVec4YID;
|
||||
jfieldID imVec4ZID;
|
||||
jfieldID imVec4WID;
|
||||
|
||||
static JavaVM* jvm;
|
||||
|
||||
namespace Jni
|
||||
{
|
||||
void InitCommon(JNIEnv* env) {
|
||||
env->GetJavaVM(&jvm);
|
||||
|
||||
jclass jImVec2Class = env->FindClass("imgui/ImVec2");
|
||||
imVec2XID = env->GetFieldID(jImVec2Class, "x", "F");
|
||||
imVec2YID = env->GetFieldID(jImVec2Class, "y", "F");
|
||||
@@ -22,6 +26,13 @@ void InitCommon(JNIEnv* env) {
|
||||
imVec4WID = env->GetFieldID(jImVec4Class, "w", "F");
|
||||
}
|
||||
|
||||
JNIEnv* GetEnv() {
|
||||
JNIEnv* env;
|
||||
jint res = jvm->GetEnv((void**)(&env), JNI_VERSION_1_8);
|
||||
assert(res == JNI_OK);
|
||||
return env;
|
||||
}
|
||||
|
||||
void ImVec2Cpy(JNIEnv* env, ImVec2* src, jobject dst) {
|
||||
env->SetFloatField(dst, imVec2XID, src->x);
|
||||
env->SetFloatField(dst, imVec2YID, src->y);
|
||||
|
||||
@@ -8,6 +8,8 @@ namespace Jni
|
||||
{
|
||||
void InitCommon(JNIEnv* env);
|
||||
|
||||
JNIEnv* GetEnv();
|
||||
|
||||
void ImVec2Cpy(JNIEnv* env, ImVec2* src, jobject dst);
|
||||
void ImVec2Cpy(JNIEnv* env, ImVec2 src, jobject dst);
|
||||
|
||||
|
||||
Reference in new issue
Block a user