Skip to content

Commit 4e56056

Browse files
SocketByteSpaiR
andauthored
[API] Bindings for ImGuiColorTextEdit (#79)
* Add ImGuiColorTextEdit submodule * ImGuiColorTextEdit integration * Remove unused Identifier class (oops) * Fix checkstyle * Small tweaks and fixes * Remove empty line before imports Co-authored-by: SpaiR <klimbetoo@gmail.com>
1 parent 4d679fe commit 4e56056

11 files changed

Lines changed: 786 additions & 1 deletion

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313
[submodule "include/implot"]
1414
path = include/implot
1515
url = https://github.com/epezent/implot.git
16+
[submodule "include/ImGuiColorTextEdit"]
17+
path = include/ImGuiColorTextEdit
18+
url = https://github.com/BalazsJako/ImGuiColorTextEdit

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ dependencies {
256256
Immediate mode 3D gizmo for scene editing and other controls based on Dear Imgui.
257257
- [implot](https://github.com/epezent/implot/tree/555ff688a8134bc0c602123149abe9c17d577475) | [Example](https://github.com/SpaiR/imgui-java/blob/v1.84.1.0/example/src/main/java/ExampleImPlot.java) <br>
258258
Advanced 2D Plotting for Dear ImGui.
259+
- [ImGuiColorTextEdit](https://github.com/BalazsJako/ImGuiColorTextEdit/tree/0a88824f7de8d0bd11d8419066caa7d3469395c4) | [Example](https://github.com/SpaiR/imgui-java/blob/v1.84.1.0/example/src/main/java/ExampleImGuiColorTextEdit.java) <br>
260+
Syntax highlighting text editor for ImGui.
259261

260262
## Freetype
261263
By default, Dear ImGui uses stb-truetype to render fonts. Yet there is an option to use FreeType font renderer. Go to the [imgui_freetype](https://github.com/ocornut/imgui/tree/256594575d95d56dda616c544c509740e74906b4/misc/freetype) to read about the difference.

buildSrc/src/main/groovy/tool/generator/GenerateLibs.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ class GenerateLibs extends DefaultTask {
4646

4747
// Copy ImGui h/cpp files
4848
project.copy { CopySpec spec ->
49-
['include/imgui', 'include/imnodes', 'include/imgui-node-editor', 'include/imguizmo', 'include/implot'].each {
49+
['include/imgui', 'include/imnodes', 'include/imgui-node-editor',
50+
'include/imguizmo', 'include/implot', 'include/ImGuiColorTextEdit'].each {
5051
spec.from(project.rootProject.file(it)) { CopySpec s -> s.include('*.h', '*.cpp', '*.inl') }
5152
}
5253
spec.from(project.rootProject.file('imgui-binding/src/main/native'))
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import imgui.ImGui;
2+
import imgui.extension.texteditor.TextEditor;
3+
import imgui.extension.texteditor.TextEditorLanguageDefinition;
4+
import imgui.flag.ImGuiWindowFlags;
5+
import imgui.type.ImBoolean;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
public class ExampleImGuiColorTextEdit {
11+
private static final TextEditor EDITOR = new TextEditor();
12+
13+
static {
14+
TextEditorLanguageDefinition lang = TextEditorLanguageDefinition.c();
15+
16+
String[] ppnames = {
17+
"NULL", "PM_REMOVE",
18+
"ZeroMemory", "DXGI_SWAP_EFFECT_DISCARD", "D3D_FEATURE_LEVEL", "D3D_DRIVER_TYPE_HARDWARE", "WINAPI", "D3D11_SDK_VERSION", "assert"};
19+
String[] ppvalues = {
20+
"#define NULL ((void*)0)",
21+
"#define PM_REMOVE (0x0001)",
22+
"Microsoft's own memory zapper function\n(which is a macro actually)\nvoid ZeroMemory(\n\t[in] PVOID Destination,\n\t[in] SIZE_T Length\n); ",
23+
"enum DXGI_SWAP_EFFECT::DXGI_SWAP_EFFECT_DISCARD = 0",
24+
"enum D3D_FEATURE_LEVEL",
25+
"enum D3D_DRIVER_TYPE::D3D_DRIVER_TYPE_HARDWARE = ( D3D_DRIVER_TYPE_UNKNOWN + 1 )",
26+
"#define WINAPI __stdcall",
27+
"#define D3D11_SDK_VERSION (7)",
28+
" #define assert(expression) (void)( \n" +
29+
" (!!(expression)) || \n" +
30+
" (_wassert(_CRT_WIDE(#expression), _CRT_WIDE(__FILE__), (unsigned)(__LINE__)), 0) \n" +
31+
" )"
32+
};
33+
34+
// Adding custom preproc identifiers
35+
Map<String, String> preprocIdentifierMap = new HashMap<>();
36+
for (int i = 0; i < ppnames.length; ++i) {
37+
preprocIdentifierMap.put(ppnames[i], ppvalues[i]);
38+
}
39+
lang.setPreprocIdentifiers(preprocIdentifierMap);
40+
41+
String[] identifiers = {
42+
"HWND", "HRESULT", "LPRESULT","D3D11_RENDER_TARGET_VIEW_DESC", "DXGI_SWAP_CHAIN_DESC","MSG","LRESULT","WPARAM", "LPARAM","UINT","LPVOID",
43+
"ID3D11Device", "ID3D11DeviceContext", "ID3D11Buffer", "ID3D11Buffer", "ID3D10Blob", "ID3D11VertexShader", "ID3D11InputLayout", "ID3D11Buffer",
44+
"ID3D10Blob", "ID3D11PixelShader", "ID3D11SamplerState", "ID3D11ShaderResourceView", "ID3D11RasterizerState", "ID3D11BlendState", "ID3D11DepthStencilState",
45+
"IDXGISwapChain", "ID3D11RenderTargetView", "ID3D11Texture2D", "TextEditor" };
46+
String[] idecls = {
47+
"typedef HWND_* HWND", "typedef long HRESULT", "typedef long* LPRESULT", "struct D3D11_RENDER_TARGET_VIEW_DESC", "struct DXGI_SWAP_CHAIN_DESC",
48+
"typedef tagMSG MSG\n * Message structure","typedef LONG_PTR LRESULT","WPARAM", "LPARAM","UINT","LPVOID",
49+
"ID3D11Device", "ID3D11DeviceContext", "ID3D11Buffer", "ID3D11Buffer", "ID3D10Blob", "ID3D11VertexShader", "ID3D11InputLayout", "ID3D11Buffer",
50+
"ID3D10Blob", "ID3D11PixelShader", "ID3D11SamplerState", "ID3D11ShaderResourceView", "ID3D11RasterizerState", "ID3D11BlendState", "ID3D11DepthStencilState",
51+
"IDXGISwapChain", "ID3D11RenderTargetView", "ID3D11Texture2D", "class TextEditor" };
52+
53+
// Adding custom identifiers
54+
Map<String, String> identifierMap = new HashMap<>();
55+
for (int i = 0; i < ppnames.length; ++i) {
56+
identifierMap.put(identifiers[i], idecls[i]);
57+
}
58+
lang.setIdentifiers(identifierMap);
59+
60+
EDITOR.setLanguageDefinition(lang);
61+
62+
// Adding error markers
63+
Map<Integer, String> errorMarkers = new HashMap<>();
64+
errorMarkers.put(1, "Expected '>'");
65+
EDITOR.setErrorMarkers(errorMarkers);
66+
67+
EDITOR.setTextLines(new String[]{
68+
"#include <iostream",
69+
"",
70+
"int main() {",
71+
" std::cout << \"Hello, World!\" << std::endl;",
72+
"}"
73+
});
74+
}
75+
76+
public static void show(final ImBoolean showImColorTextEditWindow) {
77+
ImGui.setNextWindowSize(500, 400);
78+
if (ImGui.begin("Text Editor", showImColorTextEditWindow,
79+
ImGuiWindowFlags.HorizontalScrollbar | ImGuiWindowFlags.MenuBar)) {
80+
if (ImGui.beginMenuBar()) {
81+
if (ImGui.beginMenu("File")) {
82+
if (ImGui.menuItem("Save")) {
83+
String textToSave = EDITOR.getText();
84+
/// save text....
85+
}
86+
87+
ImGui.endMenu();
88+
}
89+
if (ImGui.beginMenu("Edit")) {
90+
final boolean ro = EDITOR.isReadOnly();
91+
if (ImGui.menuItem("Read-only mode", "", ro)) {
92+
EDITOR.setReadOnly(!ro);
93+
}
94+
95+
ImGui.separator();
96+
97+
if (ImGui.menuItem("Undo", "ALT-Backspace", !ro && EDITOR.canUndo())) {
98+
EDITOR.undo(1);
99+
}
100+
if (ImGui.menuItem("Redo", "Ctrl-Y", !ro && EDITOR.canRedo())) {
101+
EDITOR.redo(1);
102+
}
103+
104+
ImGui.separator();
105+
106+
if (ImGui.menuItem("Copy", "Ctrl-C", EDITOR.hasSelection())) {
107+
EDITOR.copy();
108+
}
109+
if (ImGui.menuItem("Cut", "Ctrl-X", !ro && EDITOR.hasSelection())) {
110+
EDITOR.cut();
111+
}
112+
if (ImGui.menuItem("Delete", "Del", !ro && EDITOR.hasSelection())) {
113+
EDITOR.delete();
114+
}
115+
if (ImGui.menuItem("Paste", "Ctrl-V", !ro && ImGui.getClipboardText() != null)) {
116+
EDITOR.paste();
117+
}
118+
119+
ImGui.endMenu();
120+
}
121+
122+
ImGui.endMenuBar();
123+
}
124+
125+
int cposX = EDITOR.getCursorPositionLine();
126+
int cposY = EDITOR.getCursorPositionColumn();
127+
128+
String overwrite = EDITOR.isOverwrite() ? "Ovr" : "Ins";
129+
String canUndo = EDITOR.canUndo() ? "*" : " ";
130+
131+
ImGui.text(cposX + "/" + cposY + " " + EDITOR.getTotalLines() + " lines | " + overwrite + " | " + canUndo);
132+
133+
EDITOR.render("TextEditor");
134+
135+
ImGui.end();
136+
}
137+
}
138+
}

example/src/main/java/Extra.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class Extra {
99
private static final ImBoolean SHOW_DRAG_N_DROP_WINDOW = new ImBoolean(false);
1010
private static final ImBoolean SHOW_IMPLOT_DEMO_WINDOW = new ImBoolean(false);
1111
private static final ImBoolean SHOW_IMGUIZMO_DEMO = new ImBoolean(false);
12+
private static final ImBoolean SHOW_IMGUI_COLOR_TEXT_EDIT_WINDOW = new ImBoolean(false);
1213

1314
private static final Graph GRAPH = new Graph();
1415

@@ -20,6 +21,7 @@ public static void show(final Application app) {
2021
ImGui.checkbox("Show Drag'N'Drop Demo Window", SHOW_DRAG_N_DROP_WINDOW);
2122
ImGui.checkbox("Show ImPlot Demo Window", SHOW_IMPLOT_DEMO_WINDOW);
2223
ImGui.checkbox("Show ImGuizmo Demo Window", SHOW_IMGUIZMO_DEMO);
24+
ImGui.checkbox("Show ImGuiColorTextEdit Demo Window", SHOW_IMGUI_COLOR_TEXT_EDIT_WINDOW);
2325

2426
if (SHOW_DEMO_WINDOW.get()) {
2527
ImGui.showDemoWindow(SHOW_DEMO_WINDOW);
@@ -44,5 +46,9 @@ public static void show(final Application app) {
4446
if (SHOW_IMPLOT_DEMO_WINDOW.get()) {
4547
ExampleImPlot.show(SHOW_IMPLOT_DEMO_WINDOW);
4648
}
49+
50+
if (SHOW_IMGUI_COLOR_TEXT_EDIT_WINDOW.get()) {
51+
ExampleImGuiColorTextEdit.show(SHOW_IMGUI_COLOR_TEXT_EDIT_WINDOW);
52+
}
4753
}
4854
}

0 commit comments

Comments
 (0)