-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
ExampleImGuiColorTextEdit.java
138 lines (115 loc) · 6.15 KB
/
ExampleImGuiColorTextEdit.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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();
}
}
}