Skip to content

Commit 986aa53

Browse files
moheng233SpaiR
andauthored
[API] Added callback binding for InputText (#156)
* Added callback binding for InputText * Add JavaDoc * Change * Add Javadoc * Fix Import * Teaks and improvements - Extended example; - Added input text callback ImGuiInputTextFlags#CallbackEdit; - Minor code improvements. --------- Co-authored-by: SpaiR <[email protected]>
1 parent f3c971c commit 986aa53

File tree

6 files changed

+296
-6
lines changed

6 files changed

+296
-6
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import imgui.ImGui;
2+
import imgui.ImGuiInputTextCallbackData;
3+
import imgui.callback.ImGuiInputTextCallback;
4+
import imgui.flag.ImGuiCond;
5+
import imgui.flag.ImGuiInputTextFlags;
6+
import imgui.type.ImBoolean;
7+
import imgui.type.ImString;
8+
9+
import java.time.LocalDateTime;
10+
import java.time.format.DateTimeFormatter;
11+
12+
public class ExampleInputTextCallback {
13+
private static final ImString STR = new ImString();
14+
private static final StringBuilder OUTPUT = new StringBuilder();
15+
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
16+
17+
private static final ImGuiInputTextCallback CALLBACK = new ImGuiInputTextCallback() {
18+
@Override
19+
public void accept(final ImGuiInputTextCallbackData data) {
20+
final char c = (char) data.getEventChar();
21+
if (c == 'h' || c == 'H') {
22+
data.setEventChar('!');
23+
OUTPUT.append(DATE_FORMAT.format(LocalDateTime.now())).append(" :: Replaced!\n");
24+
} else if (c == 'w' || c == 'W') {
25+
data.setEventChar(0);
26+
OUTPUT.append(DATE_FORMAT.format(LocalDateTime.now())).append(" :: Discarded!\n");
27+
} else {
28+
OUTPUT.append(DATE_FORMAT.format(LocalDateTime.now())).append(" :: Typed: ").append(c).append('\n');
29+
}
30+
}
31+
};
32+
33+
public static void show(final ImBoolean showInputTextCallback) {
34+
ImGui.setNextWindowSize(400, 300, ImGuiCond.Once);
35+
if (ImGui.begin("Input Text Callback Demo", showInputTextCallback)) {
36+
ImGui.alignTextToFramePadding();
37+
ImGui.text("Try to input \"Hello World!\":");
38+
ImGui.sameLine();
39+
ImGui.inputText("##input", STR, ImGuiInputTextFlags.CallbackCharFilter, CALLBACK);
40+
ImGui.text(OUTPUT.toString());
41+
}
42+
ImGui.end();
43+
}
44+
}

example/src/main/java/Extra.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class Extra {
1313
private static final ImBoolean SHOW_IMGUI_FILE_DIALOG_WINDOW = new ImBoolean(false);
1414
private static final ImBoolean SHOW_IMGUI_MEMORY_EDITOR_WINDOW = new ImBoolean(false);
1515
private static final ImBoolean SHOW_IMGUI_CANVAS_EDITOR_WINDOW = new ImBoolean(false);
16+
private static final ImBoolean SHOW_IMGUI_INPUT_CALLBACK_WINDOW = new ImBoolean(false);
1617

1718
private static final Graph GRAPH = new Graph();
1819

@@ -28,6 +29,7 @@ public static void show(final Application app) {
2829
ImGui.checkbox("Show ImGuiFileDialog Demo Window", SHOW_IMGUI_FILE_DIALOG_WINDOW);
2930
ImGui.checkbox("Show ImGui MemoryEditor Demo Window", SHOW_IMGUI_MEMORY_EDITOR_WINDOW);
3031
ImGui.checkbox("Show ImGui Canvas Demo Window", SHOW_IMGUI_CANVAS_EDITOR_WINDOW);
32+
ImGui.checkbox("Show Imgui InputText Callback Window", SHOW_IMGUI_INPUT_CALLBACK_WINDOW);
3133

3234
if (SHOW_DEMO_WINDOW.get()) {
3335
ImGui.showDemoWindow(SHOW_DEMO_WINDOW);
@@ -68,5 +70,9 @@ public static void show(final Application app) {
6870
if (SHOW_IMGUI_CANVAS_EDITOR_WINDOW.get()) {
6971
ExampleCanvasEditor.show(SHOW_IMGUI_CANVAS_EDITOR_WINDOW);
7072
}
73+
74+
if (SHOW_IMGUI_INPUT_CALLBACK_WINDOW.get()) {
75+
ExampleInputTextCallback.show(SHOW_IMGUI_INPUT_CALLBACK_WINDOW);
76+
}
7177
}
7278
}

imgui-binding/src/main/java/imgui/ImGui.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package imgui;
22

33
import imgui.assertion.ImAssertCallback;
4+
import imgui.callback.ImGuiInputTextCallback;
45
import imgui.flag.ImGuiCond;
56
import imgui.flag.ImGuiDragDropFlags;
67
import imgui.flag.ImGuiInputTextFlags;
@@ -2988,11 +2989,13 @@ public static boolean vSliderScalar(String label, float sizeX, float sizeY, int
29882989

29892990
/*JNI
29902991
jmethodID jImStringResizeInternalMID;
2992+
jmethodID jInputTextCallbackMID;
29912993
29922994
jfieldID inputDataSizeID;
29932995
jfieldID inputDataIsDirtyID;
29942996
jfieldID inputDataIsResizedID;
29952997
2998+
29962999
struct InputTextCallbackUserData {
29973000
JNIEnv* env;
29983001
jobject* imString;
@@ -3001,6 +3004,7 @@ public static boolean vSliderScalar(String label, float sizeX, float sizeY, int
30013004
char* resizedBuf;
30023005
jobject* textInputData;
30033006
char* allowedChars;
3007+
jobject* handler;
30043008
};
30053009
30063010
static int TextEditCallbackStub(ImGuiInputTextCallbackData* data) {
@@ -3033,6 +3037,11 @@ static int TextEditCallbackStub(ImGuiInputTextCallbackData* data) {
30333037
}
30343038
}
30353039
3040+
if (userData->handler != NULL) {
3041+
JNIEnv* env = userData->env;
3042+
env->CallObjectMethod(*userData->handler, jInputTextCallbackMID, data);
3043+
}
3044+
30363045
return 0;
30373046
}
30383047
*/
@@ -3045,41 +3054,68 @@ static int TextEditCallbackStub(ImGuiInputTextCallbackData* data) {
30453054
30463055
jclass jImString = env->FindClass("imgui/type/ImString");
30473056
jImStringResizeInternalMID = env->GetMethodID(jImString, "resizeInternal", "(I)[B");
3057+
3058+
jclass jCallback = env->FindClass("imgui/callback/ImGuiInputTextCallback");
3059+
jInputTextCallbackMID = env->GetMethodID(jCallback, "accept", "(J)V");
30483060
*/
30493061

30503062
public static boolean inputText(String label, ImString text) {
3051-
return preInputText(false, label, null, text, 0, 0, ImGuiInputTextFlags.None);
3063+
return preInputText(false, label, null, text);
30523064
}
30533065

30543066
public static boolean inputText(String label, ImString text, int imGuiInputTextFlags) {
30553067
return preInputText(false, label, null, text, 0, 0, imGuiInputTextFlags);
30563068
}
30573069

3070+
public static boolean inputText(String label, ImString text, int imGuiInputTextFlags, ImGuiInputTextCallback callback) {
3071+
return preInputText(false, label, null, text, 0, 0, imGuiInputTextFlags, callback);
3072+
}
3073+
30583074
public static boolean inputTextMultiline(String label, ImString text) {
3059-
return preInputText(true, label, null, text, 0, 0, ImGuiInputTextFlags.None);
3075+
return preInputText(true, label, null, text);
30603076
}
30613077

30623078
public static boolean inputTextMultiline(String label, ImString text, float width, float height) {
3063-
return preInputText(true, label, null, text, width, height, ImGuiInputTextFlags.None);
3079+
return preInputText(true, label, null, text, width, height);
30643080
}
30653081

30663082
public static boolean inputTextMultiline(String label, ImString text, int imGuiInputTextFlags) {
30673083
return preInputText(true, label, null, text, 0, 0, imGuiInputTextFlags);
30683084
}
30693085

3086+
public static boolean inputTextMultiline(String label, ImString text, int imGuiInputTextFlags, ImGuiInputTextCallback callback) {
3087+
return preInputText(true, label, null, text, 0, 0, imGuiInputTextFlags, callback);
3088+
}
3089+
30703090
public static boolean inputTextMultiline(String label, ImString text, float width, float height, int imGuiInputTextFlags) {
30713091
return preInputText(true, label, null, text, width, height, imGuiInputTextFlags);
30723092
}
30733093

3094+
public static boolean inputTextMultiline(String label, ImString text, float width, float height, int imGuiInputTextFlags, ImGuiInputTextCallback callback) {
3095+
return preInputText(true, label, null, text, width, height, imGuiInputTextFlags, callback);
3096+
}
3097+
30743098
public static boolean inputTextWithHint(String label, String hint, ImString text) {
3075-
return preInputText(false, label, hint, text, 0, 0, ImGuiInputTextFlags.None);
3099+
return preInputText(false, label, hint, text);
30763100
}
30773101

30783102
public static boolean inputTextWithHint(String label, String hint, ImString text, int imGuiInputTextFlags) {
30793103
return preInputText(false, label, hint, text, 0, 0, imGuiInputTextFlags);
30803104
}
30813105

3106+
private static boolean preInputText(boolean multiline, String label, String hint, ImString text) {
3107+
return preInputText(multiline, label, hint, text, 0, 0);
3108+
}
3109+
3110+
private static boolean preInputText(boolean multiline, String label, String hint, ImString text, float width, float height) {
3111+
return preInputText(multiline, label, hint, text, width, height, ImGuiInputTextFlags.None);
3112+
}
3113+
30823114
private static boolean preInputText(boolean multiline, String label, String hint, ImString text, float width, float height, int flags) {
3115+
return preInputText(multiline, label, hint, text, width, height, flags, null);
3116+
}
3117+
3118+
private static boolean preInputText(boolean multiline, String label, String hint, ImString text, float width, float height, int flags, ImGuiInputTextCallback callback) {
30833119
final ImString.InputData inputData = text.inputData;
30843120

30853121
if (inputData.isResizable) {
@@ -3095,10 +3131,10 @@ private static boolean preInputText(boolean multiline, String label, String hint
30953131
hintLabel = "";
30963132
}
30973133

3098-
return nInputText(multiline, hint != null, label, hintLabel, text, text.getData(), text.getData().length, width, height, flags, inputData, inputData.allowedChars);
3134+
return nInputText(multiline, hint != null, label, hintLabel, text, text.getData(), text.getData().length, width, height, flags, inputData, inputData.allowedChars, callback);
30993135
}
31003136

3101-
private static native boolean nInputText(boolean multiline, boolean hint, String label, String hintLabel, ImString imString, byte[] buf, int maxSize, float width, float height, int flags, ImString.InputData textInputData, String allowedChars); /*
3137+
private static native boolean nInputText(boolean multiline, boolean hint, String label, String hintLabel, ImString imString, byte[] buf, int maxSize, float width, float height, int flags, ImString.InputData textInputData, String allowedChars, ImGuiInputTextCallback callback); /*
31023138
InputTextCallbackUserData userData;
31033139
userData.imString = &imString;
31043140
userData.maxSize = maxSize;
@@ -3107,6 +3143,7 @@ private static boolean preInputText(boolean multiline, String label, String hint
31073143
userData.textInputData = &textInputData;
31083144
userData.env = env;
31093145
userData.allowedChars = allowedChars;
3146+
userData.handler = &callback;
31103147
31113148
bool valueChanged;
31123149
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package imgui;
2+
3+
import imgui.binding.ImGuiStruct;
4+
5+
/**
6+
* Shared state of InputText(), passed as an argument to your callback when a ImGuiInputTextFlags_Callback* flag is used.<p>
7+
* The callback function should return 0 by default.<p>
8+
* Callbacks (follow a flag name and see comments in ImGuiInputTextFlags_ declarations for more details)<p>
9+
* - ImGuiInputTextFlags_CallbackEdit: Callback on buffer edit (note that InputText() already returns true on edit, the callback is useful mainly to manipulate the underlying buffer while focus is active)<p>
10+
* - ImGuiInputTextFlags_CallbackAlways: Callback on each iteration<p>
11+
* - ImGuiInputTextFlags_CallbackCompletion: Callback on pressing TAB<p>
12+
* - ImGuiInputTextFlags_CallbackHistory: Callback on pressing Up/Down arrows<p>
13+
* - ImGuiInputTextFlags_CallbackCharFilter: Callback on character inputs to replace or discard them. Modify 'EventChar' to replace or discard, or return 1 in callback to discard.<p>
14+
* - ImGuiInputTextFlags_CallbackResize: Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow.<p>
15+
*/
16+
public class ImGuiInputTextCallbackData extends ImGuiStruct {
17+
public ImGuiInputTextCallbackData(final long ptr) {
18+
super(ptr);
19+
}
20+
21+
/*JNI
22+
#include "_common.h"
23+
24+
#define IMGUI_CALLBACK_DATA ((ImGuiInputTextCallbackData*)STRUCT_PTR)
25+
*/
26+
27+
/**
28+
* One ImGuiInputTextFlags_Callback*
29+
*
30+
* @return ImGuiInputTextFlags
31+
*/
32+
public native int getEventFlag(); /*
33+
return IMGUI_CALLBACK_DATA->EventFlag;
34+
*/
35+
36+
/**
37+
* What user passed to InputText()
38+
*
39+
* @return ImGuiInputTextFlags
40+
*/
41+
public native int getFlags(); /*
42+
return IMGUI_CALLBACK_DATA->Flags;
43+
*/
44+
45+
/**
46+
* [CharFilter] Character input;
47+
*
48+
* @return Character input
49+
*/
50+
public native int getEventChar(); /*
51+
return IMGUI_CALLBACK_DATA->EventChar;
52+
*/
53+
54+
/**
55+
* [CharFilter] Replace character with another one, or set to zero to drop. return 1 is equivalent to setting EventChar=0;
56+
*
57+
* @param c Replaced characters
58+
*/
59+
public void setEventChar(final char c) {
60+
setEventChar((int) c);
61+
}
62+
63+
/**
64+
* [CharFilter] Replace character with another one, or set to zero to drop. return 1 is equivalent to setting EventChar=0;
65+
*
66+
* @param c Replaced characters
67+
*/
68+
public native void setEventChar(int c); /*
69+
IMGUI_CALLBACK_DATA->EventChar = c;
70+
*/
71+
72+
/**
73+
* [Completion,History]
74+
*
75+
* @return Key pressed (Up/Down/TAB)
76+
*/
77+
public native int getEventKey(); /*
78+
return IMGUI_CALLBACK_DATA->EventKey;
79+
*/
80+
81+
/**
82+
* [Resize] Can replace pointer <p>
83+
* [Completion,History,Always] Only write to pointed data, don't replace the actual pointer!
84+
*
85+
* @return Buf
86+
*/
87+
public native String getBuf(); /*
88+
return env->NewStringUTF(IMGUI_CALLBACK_DATA->Buf);
89+
*/
90+
91+
/**
92+
* Set if you modify Buf/BufTextLen!
93+
*
94+
* @return Dirty
95+
*/
96+
public native boolean getBufDirty(); /*
97+
return IMGUI_CALLBACK_DATA->BufDirty;
98+
*/
99+
100+
/**
101+
* Set if you modify Buf/BufTextLen!
102+
*
103+
* @param dirty Dirty
104+
*/
105+
public native void setBufDirty(boolean dirty); /*
106+
IMGUI_CALLBACK_DATA->BufDirty = dirty;
107+
*/
108+
109+
/**
110+
* Current cursor position
111+
*
112+
* @return Current cursor position
113+
*/
114+
public native int getCursorPos(); /*
115+
return IMGUI_CALLBACK_DATA->CursorPos;
116+
*/
117+
118+
/**
119+
* Set the current cursor position
120+
*
121+
* @param pos Set the current cursor position
122+
*/
123+
public native void setCursorPos(int pos); /*
124+
IMGUI_CALLBACK_DATA->CursorPos = pos;
125+
*/
126+
127+
/**
128+
* Selection Start
129+
*
130+
* @return Selection Start
131+
*/
132+
public native int getSelectionStart(); /*
133+
return IMGUI_CALLBACK_DATA->SelectionStart;
134+
*/
135+
136+
/**
137+
* Set Selection Start
138+
*
139+
* @param pos Selection Start
140+
*/
141+
public native void setSelectionStart(int pos); /*
142+
IMGUI_CALLBACK_DATA->SelectionStart = pos;
143+
*/
144+
145+
/**
146+
* Selection End
147+
*
148+
* @return Selection End
149+
*/
150+
public native int getSelectionEnd(); /*
151+
return IMGUI_CALLBACK_DATA->SelectionEnd;
152+
*/
153+
154+
/**
155+
* Set Selection End
156+
*
157+
* @param pos Selection End
158+
*/
159+
public native void setSelectionEnd(int pos); /*
160+
IMGUI_CALLBACK_DATA->SelectionEnd = pos;
161+
*/
162+
163+
/**
164+
* Delete Chars
165+
*
166+
* @param pos Start Delete Pos
167+
* @param bytesCount Delete Char Count
168+
*/
169+
public native void deleteChars(int pos, int bytesCount); /*
170+
IMGUI_CALLBACK_DATA->DeleteChars(pos, bytesCount);
171+
*/
172+
173+
/**
174+
* Insert Chars
175+
*
176+
* @param pos insert Psos
177+
* @param str insert String
178+
*/
179+
public native void insertChars(int pos, String str); /*
180+
IMGUI_CALLBACK_DATA->InsertChars(pos, str);
181+
*/
182+
}

0 commit comments

Comments
 (0)