Skip to content

Commit

Permalink
[API] Bindings for ImGuiColorTextEdit (#79)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
SocketByte and SpaiR committed Sep 12, 2021
1 parent 4d679fe commit 4e56056
Show file tree
Hide file tree
Showing 11 changed files with 786 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
[submodule "include/implot"]
path = include/implot
url = https://github.com/epezent/implot.git
[submodule "include/ImGuiColorTextEdit"]
path = include/ImGuiColorTextEdit
url = https://github.com/BalazsJako/ImGuiColorTextEdit
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ dependencies {
Immediate mode 3D gizmo for scene editing and other controls based on Dear Imgui.
- [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>
Advanced 2D Plotting for Dear ImGui.
- [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>
Syntax highlighting text editor for ImGui.

## Freetype
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.
Expand Down
3 changes: 2 additions & 1 deletion buildSrc/src/main/groovy/tool/generator/GenerateLibs.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class GenerateLibs extends DefaultTask {

// Copy ImGui h/cpp files
project.copy { CopySpec spec ->
['include/imgui', 'include/imnodes', 'include/imgui-node-editor', 'include/imguizmo', 'include/implot'].each {
['include/imgui', 'include/imnodes', 'include/imgui-node-editor',
'include/imguizmo', 'include/implot', 'include/ImGuiColorTextEdit'].each {
spec.from(project.rootProject.file(it)) { CopySpec s -> s.include('*.h', '*.cpp', '*.inl') }
}
spec.from(project.rootProject.file('imgui-binding/src/main/native'))
Expand Down
138 changes: 138 additions & 0 deletions example/src/main/java/ExampleImGuiColorTextEdit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import imgui.ImGui;
import imgui.extension.texteditor.TextEditor;
import imgui.extension.texteditor.TextEditorLanguageDefinition;
import imgui.flag.ImGuiWindowFlags;
import imgui.type.ImBoolean;

import java.util.HashMap;
import java.util.Map;

public class ExampleImGuiColorTextEdit {
private static final TextEditor EDITOR = new TextEditor();

static {
TextEditorLanguageDefinition lang = TextEditorLanguageDefinition.c();

String[] ppnames = {
"NULL", "PM_REMOVE",
"ZeroMemory", "DXGI_SWAP_EFFECT_DISCARD", "D3D_FEATURE_LEVEL", "D3D_DRIVER_TYPE_HARDWARE", "WINAPI", "D3D11_SDK_VERSION", "assert"};
String[] ppvalues = {
"#define NULL ((void*)0)",
"#define PM_REMOVE (0x0001)",
"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); ",
"enum DXGI_SWAP_EFFECT::DXGI_SWAP_EFFECT_DISCARD = 0",
"enum D3D_FEATURE_LEVEL",
"enum D3D_DRIVER_TYPE::D3D_DRIVER_TYPE_HARDWARE = ( D3D_DRIVER_TYPE_UNKNOWN + 1 )",
"#define WINAPI __stdcall",
"#define D3D11_SDK_VERSION (7)",
" #define assert(expression) (void)( \n" +
" (!!(expression)) || \n" +
" (_wassert(_CRT_WIDE(#expression), _CRT_WIDE(__FILE__), (unsigned)(__LINE__)), 0) \n" +
" )"
};

// Adding custom preproc identifiers
Map<String, String> preprocIdentifierMap = new HashMap<>();
for (int i = 0; i < ppnames.length; ++i) {
preprocIdentifierMap.put(ppnames[i], ppvalues[i]);
}
lang.setPreprocIdentifiers(preprocIdentifierMap);

String[] identifiers = {
"HWND", "HRESULT", "LPRESULT","D3D11_RENDER_TARGET_VIEW_DESC", "DXGI_SWAP_CHAIN_DESC","MSG","LRESULT","WPARAM", "LPARAM","UINT","LPVOID",
"ID3D11Device", "ID3D11DeviceContext", "ID3D11Buffer", "ID3D11Buffer", "ID3D10Blob", "ID3D11VertexShader", "ID3D11InputLayout", "ID3D11Buffer",
"ID3D10Blob", "ID3D11PixelShader", "ID3D11SamplerState", "ID3D11ShaderResourceView", "ID3D11RasterizerState", "ID3D11BlendState", "ID3D11DepthStencilState",
"IDXGISwapChain", "ID3D11RenderTargetView", "ID3D11Texture2D", "TextEditor" };
String[] idecls = {
"typedef HWND_* HWND", "typedef long HRESULT", "typedef long* LPRESULT", "struct D3D11_RENDER_TARGET_VIEW_DESC", "struct DXGI_SWAP_CHAIN_DESC",
"typedef tagMSG MSG\n * Message structure","typedef LONG_PTR LRESULT","WPARAM", "LPARAM","UINT","LPVOID",
"ID3D11Device", "ID3D11DeviceContext", "ID3D11Buffer", "ID3D11Buffer", "ID3D10Blob", "ID3D11VertexShader", "ID3D11InputLayout", "ID3D11Buffer",
"ID3D10Blob", "ID3D11PixelShader", "ID3D11SamplerState", "ID3D11ShaderResourceView", "ID3D11RasterizerState", "ID3D11BlendState", "ID3D11DepthStencilState",
"IDXGISwapChain", "ID3D11RenderTargetView", "ID3D11Texture2D", "class TextEditor" };

// Adding custom identifiers
Map<String, String> identifierMap = new HashMap<>();
for (int i = 0; i < ppnames.length; ++i) {
identifierMap.put(identifiers[i], idecls[i]);
}
lang.setIdentifiers(identifierMap);

EDITOR.setLanguageDefinition(lang);

// Adding error markers
Map<Integer, String> errorMarkers = new HashMap<>();
errorMarkers.put(1, "Expected '>'");
EDITOR.setErrorMarkers(errorMarkers);

EDITOR.setTextLines(new String[]{
"#include <iostream",
"",
"int main() {",
" std::cout << \"Hello, World!\" << std::endl;",
"}"
});
}

public static void show(final ImBoolean showImColorTextEditWindow) {
ImGui.setNextWindowSize(500, 400);
if (ImGui.begin("Text Editor", showImColorTextEditWindow,
ImGuiWindowFlags.HorizontalScrollbar | ImGuiWindowFlags.MenuBar)) {
if (ImGui.beginMenuBar()) {
if (ImGui.beginMenu("File")) {
if (ImGui.menuItem("Save")) {
String textToSave = EDITOR.getText();
/// save text....
}

ImGui.endMenu();
}
if (ImGui.beginMenu("Edit")) {
final boolean ro = EDITOR.isReadOnly();
if (ImGui.menuItem("Read-only mode", "", ro)) {
EDITOR.setReadOnly(!ro);
}

ImGui.separator();

if (ImGui.menuItem("Undo", "ALT-Backspace", !ro && EDITOR.canUndo())) {
EDITOR.undo(1);
}
if (ImGui.menuItem("Redo", "Ctrl-Y", !ro && EDITOR.canRedo())) {
EDITOR.redo(1);
}

ImGui.separator();

if (ImGui.menuItem("Copy", "Ctrl-C", EDITOR.hasSelection())) {
EDITOR.copy();
}
if (ImGui.menuItem("Cut", "Ctrl-X", !ro && EDITOR.hasSelection())) {
EDITOR.cut();
}
if (ImGui.menuItem("Delete", "Del", !ro && EDITOR.hasSelection())) {
EDITOR.delete();
}
if (ImGui.menuItem("Paste", "Ctrl-V", !ro && ImGui.getClipboardText() != null)) {
EDITOR.paste();
}

ImGui.endMenu();
}

ImGui.endMenuBar();
}

int cposX = EDITOR.getCursorPositionLine();
int cposY = EDITOR.getCursorPositionColumn();

String overwrite = EDITOR.isOverwrite() ? "Ovr" : "Ins";
String canUndo = EDITOR.canUndo() ? "*" : " ";

ImGui.text(cposX + "/" + cposY + " " + EDITOR.getTotalLines() + " lines | " + overwrite + " | " + canUndo);

EDITOR.render("TextEditor");

ImGui.end();
}
}
}
6 changes: 6 additions & 0 deletions example/src/main/java/Extra.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Extra {
private static final ImBoolean SHOW_DRAG_N_DROP_WINDOW = new ImBoolean(false);
private static final ImBoolean SHOW_IMPLOT_DEMO_WINDOW = new ImBoolean(false);
private static final ImBoolean SHOW_IMGUIZMO_DEMO = new ImBoolean(false);
private static final ImBoolean SHOW_IMGUI_COLOR_TEXT_EDIT_WINDOW = new ImBoolean(false);

private static final Graph GRAPH = new Graph();

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

if (SHOW_DEMO_WINDOW.get()) {
ImGui.showDemoWindow(SHOW_DEMO_WINDOW);
Expand All @@ -44,5 +46,9 @@ public static void show(final Application app) {
if (SHOW_IMPLOT_DEMO_WINDOW.get()) {
ExampleImPlot.show(SHOW_IMPLOT_DEMO_WINDOW);
}

if (SHOW_IMGUI_COLOR_TEXT_EDIT_WINDOW.get()) {
ExampleImGuiColorTextEdit.show(SHOW_IMGUI_COLOR_TEXT_EDIT_WINDOW);
}
}
}
Loading

0 comments on commit 4e56056

Please sign in to comment.