-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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 <klimbetoo@gmail.com>
- Loading branch information
Showing
6 changed files
with
296 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import imgui.ImGui; | ||
import imgui.ImGuiInputTextCallbackData; | ||
import imgui.callback.ImGuiInputTextCallback; | ||
import imgui.flag.ImGuiCond; | ||
import imgui.flag.ImGuiInputTextFlags; | ||
import imgui.type.ImBoolean; | ||
import imgui.type.ImString; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class ExampleInputTextCallback { | ||
private static final ImString STR = new ImString(); | ||
private static final StringBuilder OUTPUT = new StringBuilder(); | ||
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"); | ||
|
||
private static final ImGuiInputTextCallback CALLBACK = new ImGuiInputTextCallback() { | ||
@Override | ||
public void accept(final ImGuiInputTextCallbackData data) { | ||
final char c = (char) data.getEventChar(); | ||
if (c == 'h' || c == 'H') { | ||
data.setEventChar('!'); | ||
OUTPUT.append(DATE_FORMAT.format(LocalDateTime.now())).append(" :: Replaced!\n"); | ||
} else if (c == 'w' || c == 'W') { | ||
data.setEventChar(0); | ||
OUTPUT.append(DATE_FORMAT.format(LocalDateTime.now())).append(" :: Discarded!\n"); | ||
} else { | ||
OUTPUT.append(DATE_FORMAT.format(LocalDateTime.now())).append(" :: Typed: ").append(c).append('\n'); | ||
} | ||
} | ||
}; | ||
|
||
public static void show(final ImBoolean showInputTextCallback) { | ||
ImGui.setNextWindowSize(400, 300, ImGuiCond.Once); | ||
if (ImGui.begin("Input Text Callback Demo", showInputTextCallback)) { | ||
ImGui.alignTextToFramePadding(); | ||
ImGui.text("Try to input \"Hello World!\":"); | ||
ImGui.sameLine(); | ||
ImGui.inputText("##input", STR, ImGuiInputTextFlags.CallbackCharFilter, CALLBACK); | ||
ImGui.text(OUTPUT.toString()); | ||
} | ||
ImGui.end(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
imgui-binding/src/main/java/imgui/ImGuiInputTextCallbackData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
package imgui; | ||
|
||
import imgui.binding.ImGuiStruct; | ||
|
||
/** | ||
* Shared state of InputText(), passed as an argument to your callback when a ImGuiInputTextFlags_Callback* flag is used.<p> | ||
* The callback function should return 0 by default.<p> | ||
* Callbacks (follow a flag name and see comments in ImGuiInputTextFlags_ declarations for more details)<p> | ||
* - 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> | ||
* - ImGuiInputTextFlags_CallbackAlways: Callback on each iteration<p> | ||
* - ImGuiInputTextFlags_CallbackCompletion: Callback on pressing TAB<p> | ||
* - ImGuiInputTextFlags_CallbackHistory: Callback on pressing Up/Down arrows<p> | ||
* - 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> | ||
* - ImGuiInputTextFlags_CallbackResize: Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow.<p> | ||
*/ | ||
public class ImGuiInputTextCallbackData extends ImGuiStruct { | ||
public ImGuiInputTextCallbackData(final long ptr) { | ||
super(ptr); | ||
} | ||
|
||
/*JNI | ||
#include "_common.h" | ||
#define IMGUI_CALLBACK_DATA ((ImGuiInputTextCallbackData*)STRUCT_PTR) | ||
*/ | ||
|
||
/** | ||
* One ImGuiInputTextFlags_Callback* | ||
* | ||
* @return ImGuiInputTextFlags | ||
*/ | ||
public native int getEventFlag(); /* | ||
return IMGUI_CALLBACK_DATA->EventFlag; | ||
*/ | ||
|
||
/** | ||
* What user passed to InputText() | ||
* | ||
* @return ImGuiInputTextFlags | ||
*/ | ||
public native int getFlags(); /* | ||
return IMGUI_CALLBACK_DATA->Flags; | ||
*/ | ||
|
||
/** | ||
* [CharFilter] Character input; | ||
* | ||
* @return Character input | ||
*/ | ||
public native int getEventChar(); /* | ||
return IMGUI_CALLBACK_DATA->EventChar; | ||
*/ | ||
|
||
/** | ||
* [CharFilter] Replace character with another one, or set to zero to drop. return 1 is equivalent to setting EventChar=0; | ||
* | ||
* @param c Replaced characters | ||
*/ | ||
public void setEventChar(final char c) { | ||
setEventChar((int) c); | ||
} | ||
|
||
/** | ||
* [CharFilter] Replace character with another one, or set to zero to drop. return 1 is equivalent to setting EventChar=0; | ||
* | ||
* @param c Replaced characters | ||
*/ | ||
public native void setEventChar(int c); /* | ||
IMGUI_CALLBACK_DATA->EventChar = c; | ||
*/ | ||
|
||
/** | ||
* [Completion,History] | ||
* | ||
* @return Key pressed (Up/Down/TAB) | ||
*/ | ||
public native int getEventKey(); /* | ||
return IMGUI_CALLBACK_DATA->EventKey; | ||
*/ | ||
|
||
/** | ||
* [Resize] Can replace pointer <p> | ||
* [Completion,History,Always] Only write to pointed data, don't replace the actual pointer! | ||
* | ||
* @return Buf | ||
*/ | ||
public native String getBuf(); /* | ||
return env->NewStringUTF(IMGUI_CALLBACK_DATA->Buf); | ||
*/ | ||
|
||
/** | ||
* Set if you modify Buf/BufTextLen! | ||
* | ||
* @return Dirty | ||
*/ | ||
public native boolean getBufDirty(); /* | ||
return IMGUI_CALLBACK_DATA->BufDirty; | ||
*/ | ||
|
||
/** | ||
* Set if you modify Buf/BufTextLen! | ||
* | ||
* @param dirty Dirty | ||
*/ | ||
public native void setBufDirty(boolean dirty); /* | ||
IMGUI_CALLBACK_DATA->BufDirty = dirty; | ||
*/ | ||
|
||
/** | ||
* Current cursor position | ||
* | ||
* @return Current cursor position | ||
*/ | ||
public native int getCursorPos(); /* | ||
return IMGUI_CALLBACK_DATA->CursorPos; | ||
*/ | ||
|
||
/** | ||
* Set the current cursor position | ||
* | ||
* @param pos Set the current cursor position | ||
*/ | ||
public native void setCursorPos(int pos); /* | ||
IMGUI_CALLBACK_DATA->CursorPos = pos; | ||
*/ | ||
|
||
/** | ||
* Selection Start | ||
* | ||
* @return Selection Start | ||
*/ | ||
public native int getSelectionStart(); /* | ||
return IMGUI_CALLBACK_DATA->SelectionStart; | ||
*/ | ||
|
||
/** | ||
* Set Selection Start | ||
* | ||
* @param pos Selection Start | ||
*/ | ||
public native void setSelectionStart(int pos); /* | ||
IMGUI_CALLBACK_DATA->SelectionStart = pos; | ||
*/ | ||
|
||
/** | ||
* Selection End | ||
* | ||
* @return Selection End | ||
*/ | ||
public native int getSelectionEnd(); /* | ||
return IMGUI_CALLBACK_DATA->SelectionEnd; | ||
*/ | ||
|
||
/** | ||
* Set Selection End | ||
* | ||
* @param pos Selection End | ||
*/ | ||
public native void setSelectionEnd(int pos); /* | ||
IMGUI_CALLBACK_DATA->SelectionEnd = pos; | ||
*/ | ||
|
||
/** | ||
* Delete Chars | ||
* | ||
* @param pos Start Delete Pos | ||
* @param bytesCount Delete Char Count | ||
*/ | ||
public native void deleteChars(int pos, int bytesCount); /* | ||
IMGUI_CALLBACK_DATA->DeleteChars(pos, bytesCount); | ||
*/ | ||
|
||
/** | ||
* Insert Chars | ||
* | ||
* @param pos insert Psos | ||
* @param str insert String | ||
*/ | ||
public native void insertChars(int pos, String str); /* | ||
IMGUI_CALLBACK_DATA->InsertChars(pos, str); | ||
*/ | ||
} |
Oops, something went wrong.