|
| 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 | +} |
0 commit comments