├── ImGuiDiscordWebHook
├── ImGuiDiscordWebhook.filters
├── ImGuiDiscordWebhook.sln
├── ImGuiDiscordWebhook.user
├── ImGuiDiscordWebhook.vcxproj
├── imgui
│ ├── imconfig.h
│ ├── imgui.cpp
│ ├── imgui.h
│ ├── imgui_demo.cpp
│ ├── imgui_draw.cpp
│ ├── imgui_impl_dx9.cpp
│ ├── imgui_impl_dx9.h
│ ├── imgui_impl_win32.cpp
│ ├── imgui_impl_win32.h
│ ├── imgui_internal.h
│ ├── imgui_tables.cpp
│ ├── imgui_widgets.cpp
│ ├── imstb_rectpack.h
│ ├── imstb_textedit.h
│ └── imstb_truetype.h
├── main.cpp
└── main.h
└── README.md
/ImGuiDiscordWebHook/ImGuiDiscordWebhook.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {1b1594fa-6701-4fee-9ef1-aa6aceab743e}
14 |
15 |
16 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
17 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
18 |
19 |
20 |
21 |
22 | imgui
23 |
24 |
25 | imgui
26 |
27 |
28 | imgui
29 |
30 |
31 | imgui
32 |
33 |
34 | imgui
35 |
36 |
37 | imgui
38 |
39 |
40 | imgui
41 |
42 |
43 | imgui
44 |
45 |
46 | Üst Bilgi Dosyaları
47 |
48 |
49 |
50 |
51 | imgui
52 |
53 |
54 | imgui
55 |
56 |
57 | imgui
58 |
59 |
60 | imgui
61 |
62 |
63 | imgui
64 |
65 |
66 | imgui
67 |
68 |
69 | imgui
70 |
71 |
72 | Kaynak Dosyalar
73 |
74 |
75 |
--------------------------------------------------------------------------------
/ImGuiDiscordWebHook/ImGuiDiscordWebhook.sln:
--------------------------------------------------------------------------------
1 | Microsoft Visual Studio Solution File, Format Version 12.00
2 | # Visual Studio Version 16
3 | VisualStudioVersion = 16.0.31313.79
4 | MinimumVisualStudioVersion = 10.0.40219.1
5 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ImGuiDiscordWebhook", "ImGuiDiscordWebhook.vcxproj", "{C9516CA0-D9EC-4678-9A55-E1B0B23F4B1A}"
6 | EndProject
7 | Global
8 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
9 | Debug|x64 = Debug|x64
10 | Debug|x86 = Debug|x86
11 | Release|x64 = Release|x64
12 | Release|x86 = Release|x86
13 | Description = ImGuiDiscordWebhook
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {C9516CA0-D9EC-4678-9A55-E1B0B23F4B1A}.Debug|x64.ActiveCfg = Debug|x64
17 | {C9516CA0-D9EC-4678-9A55-E1B0B23F4B1A}.Debug|x64.Build.0 = Debug|x64
18 | {C9516CA0-D9EC-4678-9A55-E1B0B23F4B1A}.Debug|x86.ActiveCfg = Debug|Win32
19 | {C9516CA0-D9EC-4678-9A55-E1B0B23F4B1A}.Debug|x86.Build.0 = Debug|Win32
20 | {C9516CA0-D9EC-4678-9A55-E1B0B23F4B1A}.Release|x64.ActiveCfg = Release|x64
21 | {C9516CA0-D9EC-4678-9A55-E1B0B23F4B1A}.Release|x64.Build.0 = Release|x64
22 | {C9516CA0-D9EC-4678-9A55-E1B0B23F4B1A}.Release|x86.ActiveCfg = Release|Win32
23 | {C9516CA0-D9EC-4678-9A55-E1B0B23F4B1A}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {C5397B24-97BA-432E-BECC-94A1D0B6BEA8}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/ImGuiDiscordWebHook/ImGuiDiscordWebhook.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/ImGuiDiscordWebHook/ImGuiDiscordWebhook.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 16.0
23 | Win32Proj
24 | {c9516ca0-d9ec-4678-9a55-e1b0b23f4b1a}
25 | ImGuiDiscordWebhook
26 | 10.0
27 | ImGuiDiscordWebhook
28 |
29 |
30 |
31 | Application
32 | true
33 | v142
34 | Unicode
35 |
36 |
37 | Application
38 | false
39 | v142
40 | true
41 | MultiByte
42 |
43 |
44 | Application
45 | true
46 | v142
47 | Unicode
48 |
49 |
50 | Application
51 | false
52 | v142
53 | true
54 | MultiByte
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 | true
76 |
77 |
78 | false
79 |
80 |
81 | true
82 |
83 |
84 | false
85 |
86 |
87 |
88 | Level3
89 | true
90 | WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
91 | true
92 |
93 |
94 | Console
95 | true
96 |
97 |
98 |
99 |
100 | Level3
101 | true
102 | true
103 | true
104 | WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
105 | true
106 |
107 |
108 | Windows
109 | true
110 | true
111 | true
112 |
113 |
114 |
115 |
116 | Level3
117 | true
118 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
119 | true
120 |
121 |
122 | Console
123 | true
124 |
125 |
126 |
127 |
128 | Level3
129 | true
130 | true
131 | true
132 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
133 | true
134 |
135 |
136 | Windows
137 | true
138 | true
139 | true
140 | RequireAdministrator
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
--------------------------------------------------------------------------------
/ImGuiDiscordWebHook/imgui/imconfig.h:
--------------------------------------------------------------------------------
1 | //-----------------------------------------------------------------------------
2 | // COMPILE-TIME OPTIONS FOR DEAR IMGUI
3 | // Runtime options (clipboard callbacks, enabling various features, etc.) can generally be set via the ImGuiIO structure.
4 | // You can use ImGui::SetAllocatorFunctions() before calling ImGui::CreateContext() to rewire memory allocation functions.
5 | //-----------------------------------------------------------------------------
6 | // A) You may edit imconfig.h (and not overwrite it when updating Dear ImGui, or maintain a patch/rebased branch with your modifications to it)
7 | // B) or '#define IMGUI_USER_CONFIG "my_imgui_config.h"' in your project and then add directives in your own file without touching this template.
8 | //-----------------------------------------------------------------------------
9 | // You need to make sure that configuration settings are defined consistently _everywhere_ Dear ImGui is used, which include the imgui*.cpp
10 | // files but also _any_ of your code that uses Dear ImGui. This is because some compile-time options have an affect on data structures.
11 | // Defining those options in imconfig.h will ensure every compilation unit gets to see the same data structure layouts.
12 | // Call IMGUI_CHECKVERSION() from your .cpp files to verify that the data structures your files are using are matching the ones imgui.cpp is using.
13 | //-----------------------------------------------------------------------------
14 |
15 | #pragma once
16 |
17 | //---- Define assertion handler. Defaults to calling assert().
18 | // If your macro uses multiple statements, make sure is enclosed in a 'do { .. } while (0)' block so it can be used as a single statement.
19 | //#define IM_ASSERT(_EXPR) MyAssert(_EXPR)
20 | //#define IM_ASSERT(_EXPR) ((void)(_EXPR)) // Disable asserts
21 |
22 | //---- Define attributes of all API symbols declarations, e.g. for DLL under Windows
23 | // Using dear imgui via a shared library is not recommended, because of function call overhead and because we don't guarantee backward nor forward ABI compatibility.
24 | //#define IMGUI_API __declspec( dllexport )
25 | //#define IMGUI_API __declspec( dllimport )
26 |
27 | //---- Don't define obsolete functions/enums/behaviors. Consider enabling from time to time after updating to avoid using soon-to-be obsolete function/names.
28 | //#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS
29 |
30 | //---- Disable all of Dear ImGui or don't implement standard windows.
31 | // It is very strongly recommended to NOT disable the demo windows during development. Please read comments in imgui_demo.cpp.
32 | //#define IMGUI_DISABLE // Disable everything: all headers and source files will be empty.
33 | //#define IMGUI_DISABLE_DEMO_WINDOWS // Disable demo windows: ShowDemoWindow()/ShowStyleEditor() will be empty. Not recommended.
34 | //#define IMGUI_DISABLE_METRICS_WINDOW // Disable debug/metrics window: ShowMetricsWindow() will be empty.
35 |
36 | //---- Don't implement some functions to reduce linkage requirements.
37 | //#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc.
38 | //#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] Don't implement default IME handler. Won't use and link with ImmGetContext/ImmSetCompositionWindow.
39 | //#define IMGUI_DISABLE_WIN32_FUNCTIONS // [Win32] Won't use and link with any Win32 function (clipboard, ime).
40 | //#define IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS // [OSX] Implement default OSX clipboard handler (need to link with '-framework ApplicationServices', this is why this is not the default).
41 | //#define IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself (e.g. if you don't want to link with vsnprintf)
42 | //#define IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS // Don't implement ImFabs/ImSqrt/ImPow/ImFmod/ImCos/ImSin/ImAcos/ImAtan2 so you can implement them yourself.
43 | //#define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite so you can implement them yourself if you don't want to link with fopen/fclose/fread/fwrite. This will also disable the LogToTTY() function.
44 | //#define IMGUI_DISABLE_DEFAULT_ALLOCATORS // Don't implement default allocators calling malloc()/free() to avoid linking with them. You will need to call ImGui::SetAllocatorFunctions().
45 |
46 | //---- Include imgui_user.h at the end of imgui.h as a convenience
47 | //#define IMGUI_INCLUDE_IMGUI_USER_H
48 |
49 | //---- Pack colors to BGRA8 instead of RGBA8 (to avoid converting from one to another)
50 | //#define IMGUI_USE_BGRA_PACKED_COLOR
51 |
52 | //---- Use 32-bit for ImWchar (default is 16-bit) to support unicode planes 1-16. (e.g. point beyond 0xFFFF like emoticons, dingbats, symbols, shapes, ancient languages, etc...)
53 | //#define IMGUI_USE_WCHAR32
54 |
55 | //---- Avoid multiple STB libraries implementations, or redefine path/filenames to prioritize another version
56 | // By default the embedded implementations are declared static and not available outside of imgui cpp files.
57 | //#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h"
58 | //#define IMGUI_STB_RECT_PACK_FILENAME "my_folder/stb_rect_pack.h"
59 | //#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION
60 | //#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION
61 |
62 | //---- Unless IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS is defined, use the much faster STB sprintf library implementation of vsnprintf instead of the one from the default C library.
63 | // Note that stb_sprintf.h is meant to be provided by the user and available in the include path at compile time. Also, the compatibility checks of the arguments and formats done by clang and GCC will be disabled in order to support the extra formats provided by STB sprintf.
64 | // #define IMGUI_USE_STB_SPRINTF
65 |
66 | //---- Define constructor and implicit cast operators to convert back<>forth between your math types and ImVec2/ImVec4.
67 | // This will be inlined as part of ImVec2 and ImVec4 class declarations.
68 | /*
69 | #define IM_VEC2_CLASS_EXTRA \
70 | ImVec2(const MyVec2& f) { x = f.x; y = f.y; } \
71 | operator MyVec2() const { return MyVec2(x,y); }
72 |
73 | #define IM_VEC4_CLASS_EXTRA \
74 | ImVec4(const MyVec4& f) { x = f.x; y = f.y; z = f.z; w = f.w; } \
75 | operator MyVec4() const { return MyVec4(x,y,z,w); }
76 | */
77 |
78 | //---- Use 32-bit vertex indices (default is 16-bit) is one way to allow large meshes with more than 64K vertices.
79 | // Your renderer backend will need to support it (most example renderer backends support both 16/32-bit indices).
80 | // Another way to allow large meshes while keeping 16-bit indices is to handle ImDrawCmd::VtxOffset in your renderer.
81 | // Read about ImGuiBackendFlags_RendererHasVtxOffset for details.
82 | //#define ImDrawIdx unsigned int
83 |
84 | //---- Override ImDrawCallback signature (will need to modify renderer backends accordingly)
85 | //struct ImDrawList;
86 | //struct ImDrawCmd;
87 | //typedef void (*MyImDrawCallback)(const ImDrawList* draw_list, const ImDrawCmd* cmd, void* my_renderer_user_data);
88 | //#define ImDrawCallback MyImDrawCallback
89 |
90 | //---- Debug Tools: Macro to break in Debugger
91 | // (use 'Metrics->Tools->Item Picker' to pick widgets with the mouse and break into them for easy debugging.)
92 | //#define IM_DEBUG_BREAK IM_ASSERT(0)
93 | //#define IM_DEBUG_BREAK __debugbreak()
94 |
95 | //---- Debug Tools: Have the Item Picker break in the ItemAdd() function instead of ItemHoverable(),
96 | // (which comes earlier in the code, will catch a few extra items, allow picking items other than Hovered one.)
97 | // This adds a small runtime cost which is why it is not enabled by default.
98 | //#define IMGUI_DEBUG_TOOL_ITEM_PICKER_EX
99 |
100 | //---- Debug Tools: Enable slower asserts
101 | //#define IMGUI_DEBUG_PARANOID
102 |
103 | //---- Tip: You can add extra functions within the ImGui:: namespace, here or in your own headers files.
104 | /*
105 | namespace ImGui
106 | {
107 | void MyFunction(const char* name, const MyMatrix44& v);
108 | }
109 | */
110 |
--------------------------------------------------------------------------------
/ImGuiDiscordWebHook/imgui/imgui_impl_dx9.cpp:
--------------------------------------------------------------------------------
1 | // dear imgui: Renderer Backend for DirectX9
2 | // This needs to be used along with a Platform Backend (e.g. Win32)
3 |
4 | // Implemented features:
5 | // [X] Renderer: User texture binding. Use 'LPDIRECT3DTEXTURE9' as ImTextureID. Read the FAQ about ImTextureID!
6 | // [X] Renderer: Multi-viewport support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
7 | // [X] Renderer: Support for large meshes (64k+ vertices) with 16-bit indices.
8 |
9 | // You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
10 | // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
11 | // Read online: https://github.com/ocornut/imgui/tree/master/docs
12 |
13 | // CHANGELOG
14 | // (minor and older changes stripped away, please see git history for details)
15 | // 2021-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
16 | // 2021-04-23: DirectX9: Explicitly setting up more graphics states to increase compatibility with unusual non-default states.
17 | // 2021-03-18: DirectX9: Calling IDirect3DStateBlock9::Capture() after CreateStateBlock() as a workaround for state restoring issues (see #3857).
18 | // 2021-03-03: DirectX9: Added support for IMGUI_USE_BGRA_PACKED_COLOR in user's imconfig file.
19 | // 2021-02-18: DirectX9: Change blending equation to preserve alpha in output buffer.
20 | // 2019-05-29: DirectX9: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.
21 | // 2019-04-30: DirectX9: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
22 | // 2019-03-29: Misc: Fixed erroneous assert in ImGui_ImplDX9_InvalidateDeviceObjects().
23 | // 2019-01-16: Misc: Disabled fog before drawing UI's. Fixes issue #2288.
24 | // 2018-11-30: Misc: Setting up io.BackendRendererName so it can be displayed in the About Window.
25 | // 2018-06-08: Misc: Extracted imgui_impl_dx9.cpp/.h away from the old combined DX9+Win32 example.
26 | // 2018-06-08: DirectX9: Use draw_data->DisplayPos and draw_data->DisplaySize to setup projection matrix and clipping rectangle.
27 | // 2018-05-07: Render: Saving/restoring Transform because they don't seem to be included in the StateBlock. Setting shading mode to Gouraud.
28 | // 2018-02-16: Misc: Obsoleted the io.RenderDrawListsFn callback and exposed ImGui_ImplDX9_RenderDrawData() in the .h file so you can call it yourself.
29 | // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
30 |
31 | #include "imgui.h"
32 | #include "imgui_impl_dx9.h"
33 |
34 | // DirectX
35 | #include
36 |
37 | // DirectX data
38 | static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
39 | static LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL;
40 | static LPDIRECT3DINDEXBUFFER9 g_pIB = NULL;
41 | static LPDIRECT3DTEXTURE9 g_FontTexture = NULL;
42 | static int g_VertexBufferSize = 5000, g_IndexBufferSize = 10000;
43 |
44 | struct CUSTOMVERTEX
45 | {
46 | float pos[3];
47 | D3DCOLOR col;
48 | float uv[2];
49 | };
50 | #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
51 |
52 | #ifdef IMGUI_USE_BGRA_PACKED_COLOR
53 | #define IMGUI_COL_TO_DX9_ARGB(_COL) (_COL)
54 | #else
55 | #define IMGUI_COL_TO_DX9_ARGB(_COL) (((_COL) & 0xFF00FF00) | (((_COL) & 0xFF0000) >> 16) | (((_COL) & 0xFF) << 16))
56 | #endif
57 |
58 | // Forward Declarations
59 | static void ImGui_ImplDX9_InitPlatformInterface();
60 | static void ImGui_ImplDX9_ShutdownPlatformInterface();
61 | static void ImGui_ImplDX9_CreateDeviceObjectsForPlatformWindows();
62 | static void ImGui_ImplDX9_InvalidateDeviceObjectsForPlatformWindows();
63 |
64 | static void ImGui_ImplDX9_SetupRenderState(ImDrawData* draw_data)
65 | {
66 | // Setup viewport
67 | D3DVIEWPORT9 vp;
68 | vp.X = vp.Y = 0;
69 | vp.Width = (DWORD)draw_data->DisplaySize.x;
70 | vp.Height = (DWORD)draw_data->DisplaySize.y;
71 | vp.MinZ = 0.0f;
72 | vp.MaxZ = 1.0f;
73 | g_pd3dDevice->SetViewport(&vp);
74 |
75 | // Setup render state: fixed-pipeline, alpha-blending, no face culling, no depth testing, shade mode (for gradient)
76 | g_pd3dDevice->SetPixelShader(NULL);
77 | g_pd3dDevice->SetVertexShader(NULL);
78 | g_pd3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
79 | g_pd3dDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
80 | g_pd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
81 | g_pd3dDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
82 | g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
83 | g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
84 | g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
85 | g_pd3dDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
86 | g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
87 | g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
88 | g_pd3dDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
89 | g_pd3dDevice->SetRenderState(D3DRS_SRCBLENDALPHA, D3DBLEND_ONE);
90 | g_pd3dDevice->SetRenderState(D3DRS_DESTBLENDALPHA, D3DBLEND_INVSRCALPHA);
91 | g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
92 | g_pd3dDevice->SetRenderState(D3DRS_FOGENABLE, FALSE);
93 | g_pd3dDevice->SetRenderState(D3DRS_RANGEFOGENABLE, FALSE);
94 | g_pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, FALSE);
95 | g_pd3dDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
96 | g_pd3dDevice->SetRenderState(D3DRS_CLIPPING, TRUE);
97 | g_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
98 | g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
99 | g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
100 | g_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
101 | g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
102 | g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
103 | g_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
104 | g_pd3dDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
105 | g_pd3dDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
106 |
107 | // Setup orthographic projection matrix
108 | // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayPos is (0,0) for single viewport apps.
109 | // Being agnostic of whether or can be used, we aren't relying on D3DXMatrixIdentity()/D3DXMatrixOrthoOffCenterLH() or DirectX::XMMatrixIdentity()/DirectX::XMMatrixOrthographicOffCenterLH()
110 | {
111 | float L = draw_data->DisplayPos.x + 0.5f;
112 | float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x + 0.5f;
113 | float T = draw_data->DisplayPos.y + 0.5f;
114 | float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y + 0.5f;
115 | D3DMATRIX mat_identity = { { { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } } };
116 | D3DMATRIX mat_projection =
117 | { { {
118 | 2.0f/(R-L), 0.0f, 0.0f, 0.0f,
119 | 0.0f, 2.0f/(T-B), 0.0f, 0.0f,
120 | 0.0f, 0.0f, 0.5f, 0.0f,
121 | (L+R)/(L-R), (T+B)/(B-T), 0.5f, 1.0f
122 | } } };
123 | g_pd3dDevice->SetTransform(D3DTS_WORLD, &mat_identity);
124 | g_pd3dDevice->SetTransform(D3DTS_VIEW, &mat_identity);
125 | g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &mat_projection);
126 | }
127 | }
128 |
129 | // Render function.
130 | void ImGui_ImplDX9_RenderDrawData(ImDrawData* draw_data)
131 | {
132 | // Avoid rendering when minimized
133 | if (draw_data->DisplaySize.x <= 0.0f || draw_data->DisplaySize.y <= 0.0f)
134 | return;
135 |
136 | // Create and grow buffers if needed
137 | if (!g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount)
138 | {
139 | if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
140 | g_VertexBufferSize = draw_data->TotalVtxCount + 5000;
141 | if (g_pd3dDevice->CreateVertexBuffer(g_VertexBufferSize * sizeof(CUSTOMVERTEX), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL) < 0)
142 | return;
143 | }
144 | if (!g_pIB || g_IndexBufferSize < draw_data->TotalIdxCount)
145 | {
146 | if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
147 | g_IndexBufferSize = draw_data->TotalIdxCount + 10000;
148 | if (g_pd3dDevice->CreateIndexBuffer(g_IndexBufferSize * sizeof(ImDrawIdx), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, sizeof(ImDrawIdx) == 2 ? D3DFMT_INDEX16 : D3DFMT_INDEX32, D3DPOOL_DEFAULT, &g_pIB, NULL) < 0)
149 | return;
150 | }
151 |
152 | // Backup the DX9 state
153 | IDirect3DStateBlock9* d3d9_state_block = NULL;
154 | if (g_pd3dDevice->CreateStateBlock(D3DSBT_ALL, &d3d9_state_block) < 0)
155 | return;
156 | if (d3d9_state_block->Capture() < 0)
157 | {
158 | d3d9_state_block->Release();
159 | return;
160 | }
161 |
162 | // Backup the DX9 transform (DX9 documentation suggests that it is included in the StateBlock but it doesn't appear to)
163 | D3DMATRIX last_world, last_view, last_projection;
164 | g_pd3dDevice->GetTransform(D3DTS_WORLD, &last_world);
165 | g_pd3dDevice->GetTransform(D3DTS_VIEW, &last_view);
166 | g_pd3dDevice->GetTransform(D3DTS_PROJECTION, &last_projection);
167 |
168 | // Copy and convert all vertices into a single contiguous buffer, convert colors to DX9 default format.
169 | // FIXME-OPT: This is a minor waste of resource, the ideal is to use imconfig.h and
170 | // 1) to avoid repacking colors: #define IMGUI_USE_BGRA_PACKED_COLOR
171 | // 2) to avoid repacking vertices: #define IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT struct ImDrawVert { ImVec2 pos; float z; ImU32 col; ImVec2 uv; }
172 | CUSTOMVERTEX* vtx_dst;
173 | ImDrawIdx* idx_dst;
174 | if (g_pVB->Lock(0, (UINT)(draw_data->TotalVtxCount * sizeof(CUSTOMVERTEX)), (void**)&vtx_dst, D3DLOCK_DISCARD) < 0)
175 | return;
176 | if (g_pIB->Lock(0, (UINT)(draw_data->TotalIdxCount * sizeof(ImDrawIdx)), (void**)&idx_dst, D3DLOCK_DISCARD) < 0)
177 | return;
178 | for (int n = 0; n < draw_data->CmdListsCount; n++)
179 | {
180 | const ImDrawList* cmd_list = draw_data->CmdLists[n];
181 | const ImDrawVert* vtx_src = cmd_list->VtxBuffer.Data;
182 | for (int i = 0; i < cmd_list->VtxBuffer.Size; i++)
183 | {
184 | vtx_dst->pos[0] = vtx_src->pos.x;
185 | vtx_dst->pos[1] = vtx_src->pos.y;
186 | vtx_dst->pos[2] = 0.0f;
187 | vtx_dst->col = IMGUI_COL_TO_DX9_ARGB(vtx_src->col);
188 | vtx_dst->uv[0] = vtx_src->uv.x;
189 | vtx_dst->uv[1] = vtx_src->uv.y;
190 | vtx_dst++;
191 | vtx_src++;
192 | }
193 | memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx));
194 | idx_dst += cmd_list->IdxBuffer.Size;
195 | }
196 | g_pVB->Unlock();
197 | g_pIB->Unlock();
198 | g_pd3dDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX));
199 | g_pd3dDevice->SetIndices(g_pIB);
200 | g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
201 |
202 | // Setup desired DX state
203 | ImGui_ImplDX9_SetupRenderState(draw_data);
204 |
205 | // Render command lists
206 | // (Because we merged all buffers into a single one, we maintain our own offset into them)
207 | int global_vtx_offset = 0;
208 | int global_idx_offset = 0;
209 | ImVec2 clip_off = draw_data->DisplayPos;
210 | for (int n = 0; n < draw_data->CmdListsCount; n++)
211 | {
212 | const ImDrawList* cmd_list = draw_data->CmdLists[n];
213 | for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
214 | {
215 | const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
216 | if (pcmd->UserCallback != NULL)
217 | {
218 | // User callback, registered via ImDrawList::AddCallback()
219 | // (ImDrawCallback_ResetRenderState is a special callback value used by the user to request the renderer to reset render state.)
220 | if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
221 | ImGui_ImplDX9_SetupRenderState(draw_data);
222 | else
223 | pcmd->UserCallback(cmd_list, pcmd);
224 | }
225 | else
226 | {
227 | const RECT r = { (LONG)(pcmd->ClipRect.x - clip_off.x), (LONG)(pcmd->ClipRect.y - clip_off.y), (LONG)(pcmd->ClipRect.z - clip_off.x), (LONG)(pcmd->ClipRect.w - clip_off.y) };
228 | const LPDIRECT3DTEXTURE9 texture = (LPDIRECT3DTEXTURE9)pcmd->TextureId;
229 | g_pd3dDevice->SetTexture(0, texture);
230 | g_pd3dDevice->SetScissorRect(&r);
231 | g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, pcmd->VtxOffset + global_vtx_offset, 0, (UINT)cmd_list->VtxBuffer.Size, pcmd->IdxOffset + global_idx_offset, pcmd->ElemCount / 3);
232 | }
233 | }
234 | global_idx_offset += cmd_list->IdxBuffer.Size;
235 | global_vtx_offset += cmd_list->VtxBuffer.Size;
236 | }
237 |
238 | // When using multi-viewports, it appears that there's an odd logic in DirectX9 which prevent subsequent windows
239 | // from rendering until the first window submits at least one draw call, even once. That's our workaround. (see #2560)
240 | if (global_vtx_offset == 0)
241 | g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 0, 0, 0);
242 |
243 | // Restore the DX9 transform
244 | g_pd3dDevice->SetTransform(D3DTS_WORLD, &last_world);
245 | g_pd3dDevice->SetTransform(D3DTS_VIEW, &last_view);
246 | g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &last_projection);
247 |
248 | // Restore the DX9 state
249 | d3d9_state_block->Apply();
250 | d3d9_state_block->Release();
251 | }
252 |
253 | bool ImGui_ImplDX9_Init(IDirect3DDevice9* device)
254 | {
255 | // Setup backend capabilities flags
256 | ImGuiIO& io = ImGui::GetIO();
257 | io.BackendRendererName = "imgui_impl_dx9";
258 | io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes.
259 | io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional)
260 |
261 | g_pd3dDevice = device;
262 | g_pd3dDevice->AddRef();
263 |
264 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
265 | ImGui_ImplDX9_InitPlatformInterface();
266 |
267 | return true;
268 | }
269 |
270 | void ImGui_ImplDX9_Shutdown()
271 | {
272 | ImGui_ImplDX9_ShutdownPlatformInterface();
273 | ImGui_ImplDX9_InvalidateDeviceObjects();
274 | if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; }
275 | }
276 |
277 | static bool ImGui_ImplDX9_CreateFontsTexture()
278 | {
279 | // Build texture atlas
280 | ImGuiIO& io = ImGui::GetIO();
281 | unsigned char* pixels;
282 | int width, height, bytes_per_pixel;
283 | io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height, &bytes_per_pixel);
284 |
285 | // Convert RGBA32 to BGRA32 (because RGBA32 is not well supported by DX9 devices)
286 | #ifndef IMGUI_USE_BGRA_PACKED_COLOR
287 | if (io.Fonts->TexPixelsUseColors)
288 | {
289 | ImU32* dst_start = (ImU32*)ImGui::MemAlloc(width * height * bytes_per_pixel);
290 | for (ImU32* src = (ImU32*)pixels, *dst = dst_start, *dst_end = dst_start + width * height; dst < dst_end; src++, dst++)
291 | *dst = IMGUI_COL_TO_DX9_ARGB(*src);
292 | pixels = (unsigned char*)dst_start;
293 | }
294 | #endif
295 |
296 | // Upload texture to graphics system
297 | g_FontTexture = NULL;
298 | if (g_pd3dDevice->CreateTexture(width, height, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_FontTexture, NULL) < 0)
299 | return false;
300 | D3DLOCKED_RECT tex_locked_rect;
301 | if (g_FontTexture->LockRect(0, &tex_locked_rect, NULL, 0) != D3D_OK)
302 | return false;
303 | for (int y = 0; y < height; y++)
304 | memcpy((unsigned char*)tex_locked_rect.pBits + tex_locked_rect.Pitch * y, pixels + (width * bytes_per_pixel) * y, (width * bytes_per_pixel));
305 | g_FontTexture->UnlockRect(0);
306 |
307 | // Store our identifier
308 | io.Fonts->SetTexID((ImTextureID)g_FontTexture);
309 |
310 | #ifndef IMGUI_USE_BGRA_PACKED_COLOR
311 | if (io.Fonts->TexPixelsUseColors)
312 | ImGui::MemFree(pixels);
313 | #endif
314 |
315 | return true;
316 | }
317 |
318 | bool ImGui_ImplDX9_CreateDeviceObjects()
319 | {
320 | if (!g_pd3dDevice)
321 | return false;
322 | if (!ImGui_ImplDX9_CreateFontsTexture())
323 | return false;
324 | ImGui_ImplDX9_CreateDeviceObjectsForPlatformWindows();
325 | return true;
326 | }
327 |
328 | void ImGui_ImplDX9_InvalidateDeviceObjects()
329 | {
330 | if (!g_pd3dDevice)
331 | return;
332 | if (g_pVB) { g_pVB->Release(); g_pVB = NULL; }
333 | if (g_pIB) { g_pIB->Release(); g_pIB = NULL; }
334 | if (g_FontTexture) { g_FontTexture->Release(); g_FontTexture = NULL; ImGui::GetIO().Fonts->SetTexID(NULL); } // We copied g_pFontTextureView to io.Fonts->TexID so let's clear that as well.
335 | ImGui_ImplDX9_InvalidateDeviceObjectsForPlatformWindows();
336 | }
337 |
338 | void ImGui_ImplDX9_NewFrame()
339 | {
340 | if (!g_FontTexture)
341 | ImGui_ImplDX9_CreateDeviceObjects();
342 | }
343 |
344 | //--------------------------------------------------------------------------------------------------------
345 | // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
346 | // This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
347 | // If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..
348 | //--------------------------------------------------------------------------------------------------------
349 |
350 | // Helper structure we store in the void* RenderUserData field of each ImGuiViewport to easily retrieve our backend data.
351 | struct ImGuiViewportDataDx9
352 | {
353 | IDirect3DSwapChain9* SwapChain;
354 | D3DPRESENT_PARAMETERS d3dpp;
355 |
356 | ImGuiViewportDataDx9() { SwapChain = NULL; ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS)); }
357 | ~ImGuiViewportDataDx9() { IM_ASSERT(SwapChain == NULL); }
358 | };
359 |
360 | static void ImGui_ImplDX9_CreateWindow(ImGuiViewport* viewport)
361 | {
362 | ImGuiViewportDataDx9* data = IM_NEW(ImGuiViewportDataDx9)();
363 | viewport->RendererUserData = data;
364 |
365 | // PlatformHandleRaw should always be a HWND, whereas PlatformHandle might be a higher-level handle (e.g. GLFWWindow*, SDL_Window*).
366 | // Some backends will leave PlatformHandleRaw NULL, in which case we assume PlatformHandle will contain the HWND.
367 | HWND hwnd = viewport->PlatformHandleRaw ? (HWND)viewport->PlatformHandleRaw : (HWND)viewport->PlatformHandle;
368 | IM_ASSERT(hwnd != 0);
369 |
370 | ZeroMemory(&data->d3dpp, sizeof(D3DPRESENT_PARAMETERS));
371 | data->d3dpp.Windowed = TRUE;
372 | data->d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
373 | data->d3dpp.BackBufferWidth = (UINT)viewport->Size.x;
374 | data->d3dpp.BackBufferHeight = (UINT)viewport->Size.y;
375 | data->d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
376 | data->d3dpp.hDeviceWindow = hwnd;
377 | data->d3dpp.EnableAutoDepthStencil = FALSE;
378 | data->d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
379 | data->d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; // Present without vsync
380 |
381 | HRESULT hr = g_pd3dDevice->CreateAdditionalSwapChain(&data->d3dpp, &data->SwapChain); IM_UNUSED(hr);
382 | IM_ASSERT(hr == D3D_OK);
383 | IM_ASSERT(data->SwapChain != NULL);
384 | }
385 |
386 | static void ImGui_ImplDX9_DestroyWindow(ImGuiViewport* viewport)
387 | {
388 | // The main viewport (owned by the application) will always have RendererUserData == NULL since we didn't create the data for it.
389 | if (ImGuiViewportDataDx9* data = (ImGuiViewportDataDx9*)viewport->RendererUserData)
390 | {
391 | if (data->SwapChain)
392 | data->SwapChain->Release();
393 | data->SwapChain = NULL;
394 | ZeroMemory(&data->d3dpp, sizeof(D3DPRESENT_PARAMETERS));
395 | IM_DELETE(data);
396 | }
397 | viewport->RendererUserData = NULL;
398 | }
399 |
400 | static void ImGui_ImplDX9_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
401 | {
402 | ImGuiViewportDataDx9* data = (ImGuiViewportDataDx9*)viewport->RendererUserData;
403 | if (data->SwapChain)
404 | {
405 | data->SwapChain->Release();
406 | data->SwapChain = NULL;
407 | data->d3dpp.BackBufferWidth = (UINT)size.x;
408 | data->d3dpp.BackBufferHeight = (UINT)size.y;
409 | HRESULT hr = g_pd3dDevice->CreateAdditionalSwapChain(&data->d3dpp, &data->SwapChain); IM_UNUSED(hr);
410 | IM_ASSERT(hr == D3D_OK);
411 | }
412 | }
413 |
414 | static void ImGui_ImplDX9_RenderWindow(ImGuiViewport* viewport, void*)
415 | {
416 | ImGuiViewportDataDx9* data = (ImGuiViewportDataDx9*)viewport->RendererUserData;
417 | ImVec4 clear_color = ImVec4(0.0f, 0.0f, 0.0f, 1.0f);
418 |
419 | LPDIRECT3DSURFACE9 render_target = NULL;
420 | LPDIRECT3DSURFACE9 last_render_target = NULL;
421 | LPDIRECT3DSURFACE9 last_depth_stencil = NULL;
422 | data->SwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &render_target);
423 | g_pd3dDevice->GetRenderTarget(0, &last_render_target);
424 | g_pd3dDevice->GetDepthStencilSurface(&last_depth_stencil);
425 | g_pd3dDevice->SetRenderTarget(0, render_target);
426 | g_pd3dDevice->SetDepthStencilSurface(NULL);
427 |
428 | if (!(viewport->Flags & ImGuiViewportFlags_NoRendererClear))
429 | {
430 | D3DCOLOR clear_col_dx = D3DCOLOR_RGBA((int)(clear_color.x*255.0f), (int)(clear_color.y*255.0f), (int)(clear_color.z*255.0f), (int)(clear_color.w*255.0f));
431 | g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, clear_col_dx, 1.0f, 0);
432 | }
433 |
434 | ImGui_ImplDX9_RenderDrawData(viewport->DrawData);
435 |
436 | // Restore render target
437 | g_pd3dDevice->SetRenderTarget(0, last_render_target);
438 | g_pd3dDevice->SetDepthStencilSurface(last_depth_stencil);
439 | render_target->Release();
440 | last_render_target->Release();
441 | if (last_depth_stencil) last_depth_stencil->Release();
442 | }
443 |
444 | static void ImGui_ImplDX9_SwapBuffers(ImGuiViewport* viewport, void*)
445 | {
446 | ImGuiViewportDataDx9* data = (ImGuiViewportDataDx9*)viewport->RendererUserData;
447 | HRESULT hr = data->SwapChain->Present(NULL, NULL, data->d3dpp.hDeviceWindow, NULL, 0);
448 | // Let main application handle D3DERR_DEVICELOST by resetting the device.
449 | IM_ASSERT(hr == D3D_OK || hr == D3DERR_DEVICELOST);
450 | }
451 |
452 | static void ImGui_ImplDX9_InitPlatformInterface()
453 | {
454 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
455 | platform_io.Renderer_CreateWindow = ImGui_ImplDX9_CreateWindow;
456 | platform_io.Renderer_DestroyWindow = ImGui_ImplDX9_DestroyWindow;
457 | platform_io.Renderer_SetWindowSize = ImGui_ImplDX9_SetWindowSize;
458 | platform_io.Renderer_RenderWindow = ImGui_ImplDX9_RenderWindow;
459 | platform_io.Renderer_SwapBuffers = ImGui_ImplDX9_SwapBuffers;
460 | }
461 |
462 | static void ImGui_ImplDX9_ShutdownPlatformInterface()
463 | {
464 | ImGui::DestroyPlatformWindows();
465 | }
466 |
467 | static void ImGui_ImplDX9_CreateDeviceObjectsForPlatformWindows()
468 | {
469 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
470 | for (int i = 1; i < platform_io.Viewports.Size; i++)
471 | if (!platform_io.Viewports[i]->RendererUserData)
472 | ImGui_ImplDX9_CreateWindow(platform_io.Viewports[i]);
473 | }
474 |
475 | static void ImGui_ImplDX9_InvalidateDeviceObjectsForPlatformWindows()
476 | {
477 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
478 | for (int i = 1; i < platform_io.Viewports.Size; i++)
479 | if (platform_io.Viewports[i]->RendererUserData)
480 | ImGui_ImplDX9_DestroyWindow(platform_io.Viewports[i]);
481 | }
482 |
--------------------------------------------------------------------------------
/ImGuiDiscordWebHook/imgui/imgui_impl_dx9.h:
--------------------------------------------------------------------------------
1 | // dear imgui: Renderer Backend for DirectX9
2 | // This needs to be used along with a Platform Backend (e.g. Win32)
3 |
4 | // Implemented features:
5 | // [X] Renderer: User texture binding. Use 'LPDIRECT3DTEXTURE9' as ImTextureID. Read the FAQ about ImTextureID!
6 | // [X] Renderer: Multi-viewport support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
7 | // [X] Renderer: Support for large meshes (64k+ vertices) with 16-bit indices.
8 |
9 | // You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
10 | // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
11 | // Read online: https://github.com/ocornut/imgui/tree/master/docs
12 |
13 | #pragma once
14 | #include "imgui.h" // IMGUI_IMPL_API
15 |
16 | struct IDirect3DDevice9;
17 |
18 | IMGUI_IMPL_API bool ImGui_ImplDX9_Init(IDirect3DDevice9* device);
19 | IMGUI_IMPL_API void ImGui_ImplDX9_Shutdown();
20 | IMGUI_IMPL_API void ImGui_ImplDX9_NewFrame();
21 | IMGUI_IMPL_API void ImGui_ImplDX9_RenderDrawData(ImDrawData* draw_data);
22 |
23 | // Use if you want to reset your rendering device without losing Dear ImGui state.
24 | IMGUI_IMPL_API bool ImGui_ImplDX9_CreateDeviceObjects();
25 | IMGUI_IMPL_API void ImGui_ImplDX9_InvalidateDeviceObjects();
26 |
--------------------------------------------------------------------------------
/ImGuiDiscordWebHook/imgui/imgui_impl_win32.cpp:
--------------------------------------------------------------------------------
1 | // dear imgui: Platform Backend for Windows (standard windows API for 32 and 64 bits applications)
2 | // This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
3 |
4 | // Implemented features:
5 | // [X] Platform: Clipboard support (for Win32 this is actually part of core dear imgui)
6 | // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
7 | // [X] Platform: Keyboard arrays indexed using VK_* Virtual Key Codes, e.g. ImGui::IsKeyPressed(VK_SPACE).
8 | // [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
9 | // [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
10 |
11 | // You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
12 | // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
13 | // Read online: https://github.com/ocornut/imgui/tree/master/docs
14 |
15 | #include "imgui.h"
16 | #include "imgui_impl_win32.h"
17 | #ifndef WIN32_LEAN_AND_MEAN
18 | #define WIN32_LEAN_AND_MEAN
19 | #endif
20 | #include
21 | #include
22 | #include
23 |
24 | // Configuration flags to add in your imconfig.h file:
25 | //#define IMGUI_IMPL_WIN32_DISABLE_GAMEPAD // Disable gamepad support (this used to be meaningful before <1.81) but we know load XInput dynamically so the option is less relevant now.
26 |
27 | // Using XInput for gamepad (will load DLL dynamically)
28 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
29 | #include
30 | typedef DWORD (WINAPI *PFN_XInputGetCapabilities)(DWORD, DWORD, XINPUT_CAPABILITIES*);
31 | typedef DWORD (WINAPI *PFN_XInputGetState)(DWORD, XINPUT_STATE*);
32 | #endif
33 |
34 | // CHANGELOG
35 | // (minor and older changes stripped away, please see git history for details)
36 | // 2021-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
37 | // 2021-03-23: Inputs: Clearing keyboard down array when losing focus (WM_KILLFOCUS).
38 | // 2021-02-18: Added ImGui_ImplWin32_EnableAlphaCompositing(). Non Visual Studio users will need to link with dwmapi.lib (MinGW/gcc: use -ldwmapi).
39 | // 2021-02-17: Fixed ImGui_ImplWin32_EnableDpiAwareness() attempting to get SetProcessDpiAwareness from shcore.dll on Windows 8 whereas it is only supported on Windows 8.1.
40 | // 2021-01-25: Inputs: Dynamically loading XInput DLL.
41 | // 2020-12-04: Misc: Fixed setting of io.DisplaySize to invalid/uninitialized data when after hwnd has been closed.
42 | // 2020-03-03: Inputs: Calling AddInputCharacterUTF16() to support surrogate pairs leading to codepoint >= 0x10000 (for more complete CJK inputs)
43 | // 2020-02-17: Added ImGui_ImplWin32_EnableDpiAwareness(), ImGui_ImplWin32_GetDpiScaleForHwnd(), ImGui_ImplWin32_GetDpiScaleForMonitor() helper functions.
44 | // 2020-01-14: Inputs: Added support for #define IMGUI_IMPL_WIN32_DISABLE_GAMEPAD/IMGUI_IMPL_WIN32_DISABLE_LINKING_XINPUT.
45 | // 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor.
46 | // 2019-05-11: Inputs: Don't filter value from WM_CHAR before calling AddInputCharacter().
47 | // 2019-01-17: Misc: Using GetForegroundWindow()+IsChild() instead of GetActiveWindow() to be compatible with windows created in a different thread or parent.
48 | // 2019-01-17: Inputs: Added support for mouse buttons 4 and 5 via WM_XBUTTON* messages.
49 | // 2019-01-15: Inputs: Added support for XInput gamepads (if ImGuiConfigFlags_NavEnableGamepad is set by user application).
50 | // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
51 | // 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor.
52 | // 2018-06-10: Inputs: Fixed handling of mouse wheel messages to support fine position messages (typically sent by track-pads).
53 | // 2018-06-08: Misc: Extracted imgui_impl_win32.cpp/.h away from the old combined DX9/DX10/DX11/DX12 examples.
54 | // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors and ImGuiBackendFlags_HasSetMousePos flags + honor ImGuiConfigFlags_NoMouseCursorChange flag.
55 | // 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value and WM_SETCURSOR message handling).
56 | // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
57 | // 2018-02-06: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set).
58 | // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
59 | // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
60 | // 2018-01-08: Inputs: Added mapping for ImGuiKey_Insert.
61 | // 2018-01-05: Inputs: Added WM_LBUTTONDBLCLK double-click handlers for window classes with the CS_DBLCLKS flag.
62 | // 2017-10-23: Inputs: Added WM_SYSKEYDOWN / WM_SYSKEYUP handlers so e.g. the VK_MENU key can be read.
63 | // 2017-10-23: Inputs: Using Win32 ::SetCapture/::GetCapture() to retrieve mouse positions outside the client area when dragging.
64 | // 2016-11-12: Inputs: Only call Win32 ::SetCursor(NULL) when io.MouseDrawCursor is set.
65 |
66 | // Win32 Data
67 | static HWND g_hWnd = NULL;
68 | static INT64 g_Time = 0;
69 | static INT64 g_TicksPerSecond = 0;
70 | static ImGuiMouseCursor g_LastMouseCursor = ImGuiMouseCursor_COUNT;
71 | static bool g_HasGamepad = false;
72 | static bool g_WantUpdateHasGamepad = true;
73 | static bool g_WantUpdateMonitors = true;
74 |
75 | // Forward Declarations
76 | static void ImGui_ImplWin32_InitPlatformInterface();
77 | static void ImGui_ImplWin32_ShutdownPlatformInterface();
78 | static void ImGui_ImplWin32_UpdateMonitors();
79 |
80 | // XInput DLL and functions
81 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
82 | static HMODULE g_XInputDLL = NULL;
83 | static PFN_XInputGetCapabilities g_XInputGetCapabilities = NULL;
84 | static PFN_XInputGetState g_XInputGetState = NULL;
85 | #endif
86 |
87 | // Functions
88 | bool ImGui_ImplWin32_Init(void* hwnd)
89 | {
90 | if (!::QueryPerformanceFrequency((LARGE_INTEGER*)&g_TicksPerSecond))
91 | return false;
92 | if (!::QueryPerformanceCounter((LARGE_INTEGER*)&g_Time))
93 | return false;
94 |
95 | // Setup backend capabilities flags
96 | ImGuiIO& io = ImGui::GetIO();
97 | io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
98 | io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
99 | io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
100 | io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy)
101 | io.BackendPlatformName = "imgui_impl_win32";
102 |
103 | // Our mouse update function expect PlatformHandle to be filled for the main viewport
104 | g_hWnd = (HWND)hwnd;
105 | ImGuiViewport* main_viewport = ImGui::GetMainViewport();
106 | main_viewport->PlatformHandle = main_viewport->PlatformHandleRaw = (void*)g_hWnd;
107 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
108 | ImGui_ImplWin32_InitPlatformInterface();
109 |
110 | // Keyboard mapping. Dear ImGui will use those indices to peek into the io.KeysDown[] array that we will update during the application lifetime.
111 | io.KeyMap[ImGuiKey_Tab] = VK_TAB;
112 | io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
113 | io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
114 | io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
115 | io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;
116 | io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR;
117 | io.KeyMap[ImGuiKey_PageDown] = VK_NEXT;
118 | io.KeyMap[ImGuiKey_Home] = VK_HOME;
119 | io.KeyMap[ImGuiKey_End] = VK_END;
120 | io.KeyMap[ImGuiKey_Insert] = VK_INSERT;
121 | io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
122 | io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
123 | io.KeyMap[ImGuiKey_Space] = VK_SPACE;
124 | io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
125 | io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
126 | io.KeyMap[ImGuiKey_KeyPadEnter] = VK_RETURN;
127 | io.KeyMap[ImGuiKey_A] = 'A';
128 | io.KeyMap[ImGuiKey_C] = 'C';
129 | io.KeyMap[ImGuiKey_V] = 'V';
130 | io.KeyMap[ImGuiKey_X] = 'X';
131 | io.KeyMap[ImGuiKey_Y] = 'Y';
132 | io.KeyMap[ImGuiKey_Z] = 'Z';
133 |
134 | // Dynamically load XInput library
135 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
136 | const char* xinput_dll_names[] =
137 | {
138 | "xinput1_4.dll", // Windows 8+
139 | "xinput1_3.dll", // DirectX SDK
140 | "xinput9_1_0.dll", // Windows Vista, Windows 7
141 | "xinput1_2.dll", // DirectX SDK
142 | "xinput1_1.dll" // DirectX SDK
143 | };
144 | for (int n = 0; n < IM_ARRAYSIZE(xinput_dll_names); n++)
145 | if (HMODULE dll = ::LoadLibraryA(xinput_dll_names[n]))
146 | {
147 | g_XInputDLL = dll;
148 | g_XInputGetCapabilities = (PFN_XInputGetCapabilities)::GetProcAddress(dll, "XInputGetCapabilities");
149 | g_XInputGetState = (PFN_XInputGetState)::GetProcAddress(dll, "XInputGetState");
150 | break;
151 | }
152 | #endif // IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
153 |
154 | return true;
155 | }
156 |
157 | void ImGui_ImplWin32_Shutdown()
158 | {
159 | ImGui_ImplWin32_ShutdownPlatformInterface();
160 |
161 | // Unload XInput library
162 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
163 | if (g_XInputDLL)
164 | ::FreeLibrary(g_XInputDLL);
165 | g_XInputDLL = NULL;
166 | g_XInputGetCapabilities = NULL;
167 | g_XInputGetState = NULL;
168 | #endif // IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
169 |
170 | g_hWnd = NULL;
171 | g_Time = 0;
172 | g_TicksPerSecond = 0;
173 | g_LastMouseCursor = ImGuiMouseCursor_COUNT;
174 | g_HasGamepad = false;
175 | g_WantUpdateHasGamepad = true;
176 | }
177 |
178 | static bool ImGui_ImplWin32_UpdateMouseCursor()
179 | {
180 | ImGuiIO& io = ImGui::GetIO();
181 | if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
182 | return false;
183 |
184 | ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
185 | if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor)
186 | {
187 | // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
188 | ::SetCursor(NULL);
189 | }
190 | else
191 | {
192 | // Show OS mouse cursor
193 | LPTSTR win32_cursor = IDC_ARROW;
194 | switch (imgui_cursor)
195 | {
196 | case ImGuiMouseCursor_Arrow: win32_cursor = IDC_ARROW; break;
197 | case ImGuiMouseCursor_TextInput: win32_cursor = IDC_IBEAM; break;
198 | case ImGuiMouseCursor_ResizeAll: win32_cursor = IDC_SIZEALL; break;
199 | case ImGuiMouseCursor_ResizeEW: win32_cursor = IDC_SIZEWE; break;
200 | case ImGuiMouseCursor_ResizeNS: win32_cursor = IDC_SIZENS; break;
201 | case ImGuiMouseCursor_ResizeNESW: win32_cursor = IDC_SIZENESW; break;
202 | case ImGuiMouseCursor_ResizeNWSE: win32_cursor = IDC_SIZENWSE; break;
203 | case ImGuiMouseCursor_Hand: win32_cursor = IDC_HAND; break;
204 | case ImGuiMouseCursor_NotAllowed: win32_cursor = IDC_NO; break;
205 | }
206 | ::SetCursor(::LoadCursor(NULL, win32_cursor));
207 | }
208 | return true;
209 | }
210 |
211 | // This code supports multi-viewports (multiple OS Windows mapped into different Dear ImGui viewports)
212 | // Because of that, it is a little more complicated than your typical single-viewport binding code!
213 | static void ImGui_ImplWin32_UpdateMousePos()
214 | {
215 | ImGuiIO& io = ImGui::GetIO();
216 | IM_ASSERT(g_hWnd != 0);
217 |
218 | // Set OS mouse position if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
219 | // (When multi-viewports are enabled, all imgui positions are same as OS positions)
220 | if (io.WantSetMousePos)
221 | {
222 | POINT pos = { (int)io.MousePos.x, (int)io.MousePos.y };
223 | if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) == 0)
224 | ::ClientToScreen(g_hWnd, &pos);
225 | ::SetCursorPos(pos.x, pos.y);
226 | }
227 |
228 | io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
229 | io.MouseHoveredViewport = 0;
230 |
231 | // Set imgui mouse position
232 | POINT mouse_screen_pos;
233 | if (!::GetCursorPos(&mouse_screen_pos))
234 | return;
235 | if (HWND focused_hwnd = ::GetForegroundWindow())
236 | {
237 | if (::IsChild(focused_hwnd, g_hWnd))
238 | focused_hwnd = g_hWnd;
239 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
240 | {
241 | // Multi-viewport mode: mouse position in OS absolute coordinates (io.MousePos is (0,0) when the mouse is on the upper-left of the primary monitor)
242 | // This is the position you can get with GetCursorPos(). In theory adding viewport->Pos is also the reverse operation of doing ScreenToClient().
243 | if (ImGui::FindViewportByPlatformHandle((void*)focused_hwnd) != NULL)
244 | io.MousePos = ImVec2((float)mouse_screen_pos.x, (float)mouse_screen_pos.y);
245 | }
246 | else
247 | {
248 | // Single viewport mode: mouse position in client window coordinates (io.MousePos is (0,0) when the mouse is on the upper-left corner of the app window.)
249 | // This is the position you can get with GetCursorPos() + ScreenToClient() or from WM_MOUSEMOVE.
250 | if (focused_hwnd == g_hWnd)
251 | {
252 | POINT mouse_client_pos = mouse_screen_pos;
253 | ::ScreenToClient(focused_hwnd, &mouse_client_pos);
254 | io.MousePos = ImVec2((float)mouse_client_pos.x, (float)mouse_client_pos.y);
255 | }
256 | }
257 | }
258 |
259 | // (Optional) When using multiple viewports: set io.MouseHoveredViewport to the viewport the OS mouse cursor is hovering.
260 | // Important: this information is not easy to provide and many high-level windowing library won't be able to provide it correctly, because
261 | // - This is _ignoring_ viewports with the ImGuiViewportFlags_NoInputs flag (pass-through windows).
262 | // - This is _regardless_ of whether another viewport is focused or being dragged from.
263 | // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the backend, imgui will ignore this field and infer the information by relying on the
264 | // rectangles and last focused time of every viewports it knows about. It will be unaware of foreign windows that may be sitting between or over your windows.
265 | if (HWND hovered_hwnd = ::WindowFromPoint(mouse_screen_pos))
266 | if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hovered_hwnd))
267 | if ((viewport->Flags & ImGuiViewportFlags_NoInputs) == 0) // FIXME: We still get our NoInputs window with WM_NCHITTEST/HTTRANSPARENT code when decorated?
268 | io.MouseHoveredViewport = viewport->ID;
269 | }
270 |
271 | // Gamepad navigation mapping
272 | static void ImGui_ImplWin32_UpdateGamepads()
273 | {
274 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
275 | ImGuiIO& io = ImGui::GetIO();
276 | memset(io.NavInputs, 0, sizeof(io.NavInputs));
277 | if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)
278 | return;
279 |
280 | // Calling XInputGetState() every frame on disconnected gamepads is unfortunately too slow.
281 | // Instead we refresh gamepad availability by calling XInputGetCapabilities() _only_ after receiving WM_DEVICECHANGE.
282 | if (g_WantUpdateHasGamepad)
283 | {
284 | XINPUT_CAPABILITIES caps;
285 | g_HasGamepad = g_XInputGetCapabilities ? (g_XInputGetCapabilities(0, XINPUT_FLAG_GAMEPAD, &caps) == ERROR_SUCCESS) : false;
286 | g_WantUpdateHasGamepad = false;
287 | }
288 |
289 | io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
290 | XINPUT_STATE xinput_state;
291 | if (g_HasGamepad && g_XInputGetState && g_XInputGetState(0, &xinput_state) == ERROR_SUCCESS)
292 | {
293 | const XINPUT_GAMEPAD& gamepad = xinput_state.Gamepad;
294 | io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
295 |
296 | #define MAP_BUTTON(NAV_NO, BUTTON_ENUM) { io.NavInputs[NAV_NO] = (gamepad.wButtons & BUTTON_ENUM) ? 1.0f : 0.0f; }
297 | #define MAP_ANALOG(NAV_NO, VALUE, V0, V1) { float vn = (float)(VALUE - V0) / (float)(V1 - V0); if (vn > 1.0f) vn = 1.0f; if (vn > 0.0f && io.NavInputs[NAV_NO] < vn) io.NavInputs[NAV_NO] = vn; }
298 | MAP_BUTTON(ImGuiNavInput_Activate, XINPUT_GAMEPAD_A); // Cross / A
299 | MAP_BUTTON(ImGuiNavInput_Cancel, XINPUT_GAMEPAD_B); // Circle / B
300 | MAP_BUTTON(ImGuiNavInput_Menu, XINPUT_GAMEPAD_X); // Square / X
301 | MAP_BUTTON(ImGuiNavInput_Input, XINPUT_GAMEPAD_Y); // Triangle / Y
302 | MAP_BUTTON(ImGuiNavInput_DpadLeft, XINPUT_GAMEPAD_DPAD_LEFT); // D-Pad Left
303 | MAP_BUTTON(ImGuiNavInput_DpadRight, XINPUT_GAMEPAD_DPAD_RIGHT); // D-Pad Right
304 | MAP_BUTTON(ImGuiNavInput_DpadUp, XINPUT_GAMEPAD_DPAD_UP); // D-Pad Up
305 | MAP_BUTTON(ImGuiNavInput_DpadDown, XINPUT_GAMEPAD_DPAD_DOWN); // D-Pad Down
306 | MAP_BUTTON(ImGuiNavInput_FocusPrev, XINPUT_GAMEPAD_LEFT_SHOULDER); // L1 / LB
307 | MAP_BUTTON(ImGuiNavInput_FocusNext, XINPUT_GAMEPAD_RIGHT_SHOULDER); // R1 / RB
308 | MAP_BUTTON(ImGuiNavInput_TweakSlow, XINPUT_GAMEPAD_LEFT_SHOULDER); // L1 / LB
309 | MAP_BUTTON(ImGuiNavInput_TweakFast, XINPUT_GAMEPAD_RIGHT_SHOULDER); // R1 / RB
310 | MAP_ANALOG(ImGuiNavInput_LStickLeft, gamepad.sThumbLX, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768);
311 | MAP_ANALOG(ImGuiNavInput_LStickRight, gamepad.sThumbLX, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);
312 | MAP_ANALOG(ImGuiNavInput_LStickUp, gamepad.sThumbLY, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);
313 | MAP_ANALOG(ImGuiNavInput_LStickDown, gamepad.sThumbLY, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32767);
314 | #undef MAP_BUTTON
315 | #undef MAP_ANALOG
316 | }
317 | #endif // #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
318 | }
319 |
320 | static BOOL CALLBACK ImGui_ImplWin32_UpdateMonitors_EnumFunc(HMONITOR monitor, HDC, LPRECT, LPARAM)
321 | {
322 | MONITORINFO info = {};
323 | info.cbSize = sizeof(MONITORINFO);
324 | if (!::GetMonitorInfo(monitor, &info))
325 | return TRUE;
326 | ImGuiPlatformMonitor imgui_monitor;
327 | imgui_monitor.MainPos = ImVec2((float)info.rcMonitor.left, (float)info.rcMonitor.top);
328 | imgui_monitor.MainSize = ImVec2((float)(info.rcMonitor.right - info.rcMonitor.left), (float)(info.rcMonitor.bottom - info.rcMonitor.top));
329 | imgui_monitor.WorkPos = ImVec2((float)info.rcWork.left, (float)info.rcWork.top);
330 | imgui_monitor.WorkSize = ImVec2((float)(info.rcWork.right - info.rcWork.left), (float)(info.rcWork.bottom - info.rcWork.top));
331 | imgui_monitor.DpiScale = ImGui_ImplWin32_GetDpiScaleForMonitor(monitor);
332 | ImGuiPlatformIO& io = ImGui::GetPlatformIO();
333 | if (info.dwFlags & MONITORINFOF_PRIMARY)
334 | io.Monitors.push_front(imgui_monitor);
335 | else
336 | io.Monitors.push_back(imgui_monitor);
337 | return TRUE;
338 | }
339 |
340 | static void ImGui_ImplWin32_UpdateMonitors()
341 | {
342 | ImGui::GetPlatformIO().Monitors.resize(0);
343 | ::EnumDisplayMonitors(NULL, NULL, ImGui_ImplWin32_UpdateMonitors_EnumFunc, 0);
344 | g_WantUpdateMonitors = false;
345 | }
346 |
347 | void ImGui_ImplWin32_NewFrame()
348 | {
349 | ImGuiIO& io = ImGui::GetIO();
350 | IM_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built! It is generally built by the renderer backend. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame().");
351 |
352 | // Setup display size (every frame to accommodate for window resizing)
353 | RECT rect = { 0, 0, 0, 0 };
354 | ::GetClientRect(g_hWnd, &rect);
355 | io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
356 | if (g_WantUpdateMonitors)
357 | ImGui_ImplWin32_UpdateMonitors();
358 |
359 | // Setup time step
360 | INT64 current_time = 0;
361 | ::QueryPerformanceCounter((LARGE_INTEGER*)¤t_time);
362 | io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond;
363 | g_Time = current_time;
364 |
365 | // Read keyboard modifiers inputs
366 | io.KeyCtrl = (::GetKeyState(VK_CONTROL) & 0x8000) != 0;
367 | io.KeyShift = (::GetKeyState(VK_SHIFT) & 0x8000) != 0;
368 | io.KeyAlt = (::GetKeyState(VK_MENU) & 0x8000) != 0;
369 | io.KeySuper = false;
370 | // io.KeysDown[], io.MousePos, io.MouseDown[], io.MouseWheel: filled by the WndProc handler below.
371 |
372 | // Update OS mouse position
373 | ImGui_ImplWin32_UpdateMousePos();
374 |
375 | // Update OS mouse cursor with the cursor requested by imgui
376 | ImGuiMouseCursor mouse_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor();
377 | if (g_LastMouseCursor != mouse_cursor)
378 | {
379 | g_LastMouseCursor = mouse_cursor;
380 | ImGui_ImplWin32_UpdateMouseCursor();
381 | }
382 |
383 | // Update game controllers (if enabled and available)
384 | ImGui_ImplWin32_UpdateGamepads();
385 | }
386 |
387 | // Allow compilation with old Windows SDK. MinGW doesn't have default _WIN32_WINNT/WINVER versions.
388 | #ifndef WM_MOUSEHWHEEL
389 | #define WM_MOUSEHWHEEL 0x020E
390 | #endif
391 | #ifndef DBT_DEVNODES_CHANGED
392 | #define DBT_DEVNODES_CHANGED 0x0007
393 | #endif
394 |
395 | // Win32 message handler (process Win32 mouse/keyboard inputs, etc.)
396 | // Call from your application's message handler.
397 | // When implementing your own backend, you can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if Dear ImGui wants to use your inputs.
398 | // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
399 | // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
400 | // Generally you may always pass all inputs to Dear ImGui, and hide them from your application based on those two flags.
401 | // PS: In this Win32 handler, we use the capture API (GetCapture/SetCapture/ReleaseCapture) to be able to read mouse coordinates when dragging mouse outside of our window bounds.
402 | // PS: We treat DBLCLK messages as regular mouse down messages, so this code will work on windows classes that have the CS_DBLCLKS flag set. Our own example app code doesn't set this flag.
403 | #if 0
404 | // Copy this line into your .cpp file to forward declare the function.
405 | extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
406 | #endif
407 | IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
408 | {
409 | if (ImGui::GetCurrentContext() == NULL)
410 | return 0;
411 |
412 | ImGuiIO& io = ImGui::GetIO();
413 | switch (msg)
414 | {
415 | case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK:
416 | case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK:
417 | case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:
418 | case WM_XBUTTONDOWN: case WM_XBUTTONDBLCLK:
419 | {
420 | int button = 0;
421 | if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) { button = 0; }
422 | if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) { button = 1; }
423 | if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) { button = 2; }
424 | if (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONDBLCLK) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; }
425 | if (!ImGui::IsAnyMouseDown() && ::GetCapture() == NULL)
426 | ::SetCapture(hwnd);
427 | io.MouseDown[button] = true;
428 | return 0;
429 | }
430 | case WM_LBUTTONUP:
431 | case WM_RBUTTONUP:
432 | case WM_MBUTTONUP:
433 | case WM_XBUTTONUP:
434 | {
435 | int button = 0;
436 | if (msg == WM_LBUTTONUP) { button = 0; }
437 | if (msg == WM_RBUTTONUP) { button = 1; }
438 | if (msg == WM_MBUTTONUP) { button = 2; }
439 | if (msg == WM_XBUTTONUP) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; }
440 | io.MouseDown[button] = false;
441 | if (!ImGui::IsAnyMouseDown() && ::GetCapture() == hwnd)
442 | ::ReleaseCapture();
443 | return 0;
444 | }
445 | case WM_MOUSEWHEEL:
446 | io.MouseWheel += (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA;
447 | return 0;
448 | case WM_MOUSEHWHEEL:
449 | io.MouseWheelH += (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA;
450 | return 0;
451 | case WM_KEYDOWN:
452 | case WM_SYSKEYDOWN:
453 | if (wParam < 256)
454 | io.KeysDown[wParam] = 1;
455 | return 0;
456 | case WM_KEYUP:
457 | case WM_SYSKEYUP:
458 | if (wParam < 256)
459 | io.KeysDown[wParam] = 0;
460 | return 0;
461 | case WM_KILLFOCUS:
462 | memset(io.KeysDown, 0, sizeof(io.KeysDown));
463 | return 0;
464 | case WM_CHAR:
465 | // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
466 | if (wParam > 0 && wParam < 0x10000)
467 | io.AddInputCharacterUTF16((unsigned short)wParam);
468 | return 0;
469 | case WM_SETCURSOR:
470 | if (LOWORD(lParam) == HTCLIENT && ImGui_ImplWin32_UpdateMouseCursor())
471 | return 1;
472 | return 0;
473 | case WM_DEVICECHANGE:
474 | if ((UINT)wParam == DBT_DEVNODES_CHANGED)
475 | g_WantUpdateHasGamepad = true;
476 | return 0;
477 | case WM_DISPLAYCHANGE:
478 | g_WantUpdateMonitors = true;
479 | return 0;
480 | }
481 | return 0;
482 | }
483 |
484 |
485 | //--------------------------------------------------------------------------------------------------------
486 | // DPI-related helpers (optional)
487 | //--------------------------------------------------------------------------------------------------------
488 | // - Use to enable DPI awareness without having to create an application manifest.
489 | // - Your own app may already do this via a manifest or explicit calls. This is mostly useful for our examples/ apps.
490 | // - In theory we could call simple functions from Windows SDK such as SetProcessDPIAware(), SetProcessDpiAwareness(), etc.
491 | // but most of the functions provided by Microsoft require Windows 8.1/10+ SDK at compile time and Windows 8/10+ at runtime,
492 | // neither we want to require the user to have. So we dynamically select and load those functions to avoid dependencies.
493 | //---------------------------------------------------------------------------------------------------------
494 | // This is the scheme successfully used by GLFW (from which we borrowed some of the code) and other apps aiming to be highly portable.
495 | // ImGui_ImplWin32_EnableDpiAwareness() is just a helper called by main.cpp, we don't call it automatically.
496 | // If you are trying to implement your own backend for your own engine, you may ignore that noise.
497 | //---------------------------------------------------------------------------------------------------------
498 |
499 | // Implement some of the functions and types normally declared in recent Windows SDK.
500 | #if !defined(_versionhelpers_H_INCLUDED_) && !defined(_INC_VERSIONHELPERS)
501 | static BOOL IsWindowsVersionOrGreater(WORD major, WORD minor, WORD sp)
502 | {
503 | OSVERSIONINFOEXW osvi = { sizeof(osvi), major, minor, 0, 0, { 0 }, sp, 0, 0, 0, 0 };
504 | DWORD mask = VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR;
505 | ULONGLONG cond = ::VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL);
506 | cond = ::VerSetConditionMask(cond, VER_MINORVERSION, VER_GREATER_EQUAL);
507 | cond = ::VerSetConditionMask(cond, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
508 | return ::VerifyVersionInfoW(&osvi, mask, cond);
509 | }
510 | #define IsWindowsVistaOrGreater() IsWindowsVersionOrGreater(HIBYTE(0x0600), LOBYTE(0x0600), 0) // _WIN32_WINNT_VISTA
511 | #define IsWindows8OrGreater() IsWindowsVersionOrGreater(HIBYTE(0x0602), LOBYTE(0x0602), 0) // _WIN32_WINNT_WIN8
512 | #define IsWindows8Point1OrGreater() IsWindowsVersionOrGreater(HIBYTE(0x0603), LOBYTE(0x0603), 0) // _WIN32_WINNT_WINBLUE
513 | #endif
514 |
515 | #ifndef DPI_ENUMS_DECLARED
516 | typedef enum { PROCESS_DPI_UNAWARE = 0, PROCESS_SYSTEM_DPI_AWARE = 1, PROCESS_PER_MONITOR_DPI_AWARE = 2 } PROCESS_DPI_AWARENESS;
517 | typedef enum { MDT_EFFECTIVE_DPI = 0, MDT_ANGULAR_DPI = 1, MDT_RAW_DPI = 2, MDT_DEFAULT = MDT_EFFECTIVE_DPI } MONITOR_DPI_TYPE;
518 | #endif
519 | #ifndef _DPI_AWARENESS_CONTEXTS_
520 | DECLARE_HANDLE(DPI_AWARENESS_CONTEXT);
521 | #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE (DPI_AWARENESS_CONTEXT)-3
522 | #endif
523 | #ifndef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
524 | #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 (DPI_AWARENESS_CONTEXT)-4
525 | #endif
526 | typedef HRESULT(WINAPI* PFN_SetProcessDpiAwareness)(PROCESS_DPI_AWARENESS); // Shcore.lib + dll, Windows 8.1+
527 | typedef HRESULT(WINAPI* PFN_GetDpiForMonitor)(HMONITOR, MONITOR_DPI_TYPE, UINT*, UINT*); // Shcore.lib + dll, Windows 8.1+
528 | typedef DPI_AWARENESS_CONTEXT(WINAPI* PFN_SetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT); // User32.lib + dll, Windows 10 v1607+ (Creators Update)
529 |
530 | // Helper function to enable DPI awareness without setting up a manifest
531 | void ImGui_ImplWin32_EnableDpiAwareness()
532 | {
533 | // Make sure monitors will be updated with latest correct scaling
534 | g_WantUpdateMonitors = true;
535 |
536 | // if (IsWindows10OrGreater()) // This needs a manifest to succeed. Instead we try to grab the function pointer!
537 | {
538 | static HINSTANCE user32_dll = ::LoadLibraryA("user32.dll"); // Reference counted per-process
539 | if (PFN_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContextFn = (PFN_SetThreadDpiAwarenessContext)::GetProcAddress(user32_dll, "SetThreadDpiAwarenessContext"))
540 | {
541 | SetThreadDpiAwarenessContextFn(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
542 | return;
543 | }
544 | }
545 | if (IsWindows8Point1OrGreater())
546 | {
547 | static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); // Reference counted per-process
548 | if (PFN_SetProcessDpiAwareness SetProcessDpiAwarenessFn = (PFN_SetProcessDpiAwareness)::GetProcAddress(shcore_dll, "SetProcessDpiAwareness"))
549 | {
550 | SetProcessDpiAwarenessFn(PROCESS_PER_MONITOR_DPI_AWARE);
551 | return;
552 | }
553 | }
554 | #if _WIN32_WINNT >= 0x0600
555 | ::SetProcessDPIAware();
556 | #endif
557 | }
558 |
559 | #if defined(_MSC_VER) && !defined(NOGDI)
560 | #pragma comment(lib, "gdi32") // Link with gdi32.lib for GetDeviceCaps(). MinGW will require linking with '-lgdi32'
561 | #endif
562 |
563 | float ImGui_ImplWin32_GetDpiScaleForMonitor(void* monitor)
564 | {
565 | UINT xdpi = 96, ydpi = 96;
566 | static BOOL bIsWindows8Point1OrGreater = IsWindows8Point1OrGreater();
567 | if (bIsWindows8Point1OrGreater)
568 | {
569 | static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); // Reference counted per-process
570 | if (PFN_GetDpiForMonitor GetDpiForMonitorFn = (PFN_GetDpiForMonitor)::GetProcAddress(shcore_dll, "GetDpiForMonitor"))
571 | GetDpiForMonitorFn((HMONITOR)monitor, MDT_EFFECTIVE_DPI, &xdpi, &ydpi);
572 | }
573 | #ifndef NOGDI
574 | else
575 | {
576 | const HDC dc = ::GetDC(NULL);
577 | xdpi = ::GetDeviceCaps(dc, LOGPIXELSX);
578 | ydpi = ::GetDeviceCaps(dc, LOGPIXELSY);
579 | ::ReleaseDC(NULL, dc);
580 | }
581 | #endif
582 | IM_ASSERT(xdpi == ydpi); // Please contact me if you hit this assert!
583 | return xdpi / 96.0f;
584 | }
585 |
586 | float ImGui_ImplWin32_GetDpiScaleForHwnd(void* hwnd)
587 | {
588 | HMONITOR monitor = ::MonitorFromWindow((HWND)hwnd, MONITOR_DEFAULTTONEAREST);
589 | return ImGui_ImplWin32_GetDpiScaleForMonitor(monitor);
590 | }
591 |
592 |
593 | //--------------------------------------------------------------------------------------------------------
594 | // IME (Input Method Editor) basic support for e.g. Asian language users
595 | //--------------------------------------------------------------------------------------------------------
596 |
597 | #if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP) // UWP doesn't have Win32 functions
598 | #define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS
599 | #endif
600 |
601 | #if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS)
602 | #define HAS_WIN32_IME 1
603 | #include
604 | #ifdef _MSC_VER
605 | #pragma comment(lib, "imm32")
606 | #endif
607 | static void ImGui_ImplWin32_SetImeInputPos(ImGuiViewport* viewport, ImVec2 pos)
608 | {
609 | COMPOSITIONFORM cf = { CFS_FORCE_POSITION,{ (LONG)(pos.x - viewport->Pos.x), (LONG)(pos.y - viewport->Pos.y) },{ 0, 0, 0, 0 } };
610 | if (HWND hwnd = (HWND)viewport->PlatformHandle)
611 | if (HIMC himc = ::ImmGetContext(hwnd))
612 | {
613 | ::ImmSetCompositionWindow(himc, &cf);
614 | ::ImmReleaseContext(hwnd, himc);
615 | }
616 | }
617 | #else
618 | #define HAS_WIN32_IME 0
619 | #endif
620 |
621 | //--------------------------------------------------------------------------------------------------------
622 | // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
623 | // This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
624 | // If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..
625 | //--------------------------------------------------------------------------------------------------------
626 |
627 | // Helper structure we store in the void* RenderUserData field of each ImGuiViewport to easily retrieve our backend data.
628 | struct ImGuiViewportDataWin32
629 | {
630 | HWND Hwnd;
631 | bool HwndOwned;
632 | DWORD DwStyle;
633 | DWORD DwExStyle;
634 |
635 | ImGuiViewportDataWin32() { Hwnd = NULL; HwndOwned = false; DwStyle = DwExStyle = 0; }
636 | ~ImGuiViewportDataWin32() { IM_ASSERT(Hwnd == NULL); }
637 | };
638 |
639 | static void ImGui_ImplWin32_GetWin32StyleFromViewportFlags(ImGuiViewportFlags flags, DWORD* out_style, DWORD* out_ex_style)
640 | {
641 | if (flags & ImGuiViewportFlags_NoDecoration)
642 | *out_style = WS_POPUP;
643 | else
644 | *out_style = WS_OVERLAPPEDWINDOW;
645 |
646 | if (flags & ImGuiViewportFlags_NoTaskBarIcon)
647 | *out_ex_style = WS_EX_TOOLWINDOW;
648 | else
649 | *out_ex_style = WS_EX_APPWINDOW;
650 |
651 | if (flags & ImGuiViewportFlags_TopMost)
652 | *out_ex_style |= WS_EX_TOPMOST;
653 | }
654 |
655 | static void ImGui_ImplWin32_CreateWindow(ImGuiViewport* viewport)
656 | {
657 | ImGuiViewportDataWin32* data = IM_NEW(ImGuiViewportDataWin32)();
658 | viewport->PlatformUserData = data;
659 |
660 | // Select style and parent window
661 | ImGui_ImplWin32_GetWin32StyleFromViewportFlags(viewport->Flags, &data->DwStyle, &data->DwExStyle);
662 | HWND parent_window = NULL;
663 | if (viewport->ParentViewportId != 0)
664 | if (ImGuiViewport* parent_viewport = ImGui::FindViewportByID(viewport->ParentViewportId))
665 | parent_window = (HWND)parent_viewport->PlatformHandle;
666 |
667 | // Create window
668 | RECT rect = { (LONG)viewport->Pos.x, (LONG)viewport->Pos.y, (LONG)(viewport->Pos.x + viewport->Size.x), (LONG)(viewport->Pos.y + viewport->Size.y) };
669 | ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle);
670 | data->Hwnd = ::CreateWindowEx(
671 | data->DwExStyle, _T("ImGui Platform"), _T("Untitled"), data->DwStyle, // Style, class name, window name
672 | rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, // Window area
673 | parent_window, NULL, ::GetModuleHandle(NULL), NULL); // Parent window, Menu, Instance, Param
674 | data->HwndOwned = true;
675 | viewport->PlatformRequestResize = false;
676 | viewport->PlatformHandle = viewport->PlatformHandleRaw = data->Hwnd;
677 | }
678 |
679 | static void ImGui_ImplWin32_DestroyWindow(ImGuiViewport* viewport)
680 | {
681 | if (ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData)
682 | {
683 | if (::GetCapture() == data->Hwnd)
684 | {
685 | // Transfer capture so if we started dragging from a window that later disappears, we'll still receive the MOUSEUP event.
686 | ::ReleaseCapture();
687 | ::SetCapture(g_hWnd);
688 | }
689 | if (data->Hwnd && data->HwndOwned)
690 | ::DestroyWindow(data->Hwnd);
691 | data->Hwnd = NULL;
692 | IM_DELETE(data);
693 | }
694 | viewport->PlatformUserData = viewport->PlatformHandle = NULL;
695 | }
696 |
697 | static void ImGui_ImplWin32_ShowWindow(ImGuiViewport* viewport)
698 | {
699 | ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
700 | IM_ASSERT(data->Hwnd != 0);
701 | if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)
702 | ::ShowWindow(data->Hwnd, SW_SHOWNA);
703 | else
704 | ::ShowWindow(data->Hwnd, SW_SHOW);
705 | }
706 |
707 | static void ImGui_ImplWin32_UpdateWindow(ImGuiViewport* viewport)
708 | {
709 | // (Optional) Update Win32 style if it changed _after_ creation.
710 | // Generally they won't change unless configuration flags are changed, but advanced uses (such as manually rewriting viewport flags) make this useful.
711 | ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
712 | IM_ASSERT(data->Hwnd != 0);
713 | DWORD new_style;
714 | DWORD new_ex_style;
715 | ImGui_ImplWin32_GetWin32StyleFromViewportFlags(viewport->Flags, &new_style, &new_ex_style);
716 |
717 | // Only reapply the flags that have been changed from our point of view (as other flags are being modified by Windows)
718 | if (data->DwStyle != new_style || data->DwExStyle != new_ex_style)
719 | {
720 | // (Optional) Update TopMost state if it changed _after_ creation
721 | bool top_most_changed = (data->DwExStyle & WS_EX_TOPMOST) != (new_ex_style & WS_EX_TOPMOST);
722 | HWND insert_after = top_most_changed ? ((viewport->Flags & ImGuiViewportFlags_TopMost) ? HWND_TOPMOST : HWND_NOTOPMOST) : 0;
723 | UINT swp_flag = top_most_changed ? 0 : SWP_NOZORDER;
724 |
725 | // Apply flags and position (since it is affected by flags)
726 | data->DwStyle = new_style;
727 | data->DwExStyle = new_ex_style;
728 | ::SetWindowLong(data->Hwnd, GWL_STYLE, data->DwStyle);
729 | ::SetWindowLong(data->Hwnd, GWL_EXSTYLE, data->DwExStyle);
730 | RECT rect = { (LONG)viewport->Pos.x, (LONG)viewport->Pos.y, (LONG)(viewport->Pos.x + viewport->Size.x), (LONG)(viewport->Pos.y + viewport->Size.y) };
731 | ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle); // Client to Screen
732 | ::SetWindowPos(data->Hwnd, insert_after, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, swp_flag | SWP_NOACTIVATE | SWP_FRAMECHANGED);
733 | ::ShowWindow(data->Hwnd, SW_SHOWNA); // This is necessary when we alter the style
734 | viewport->PlatformRequestMove = viewport->PlatformRequestResize = true;
735 | }
736 | }
737 |
738 | static ImVec2 ImGui_ImplWin32_GetWindowPos(ImGuiViewport* viewport)
739 | {
740 | ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
741 | IM_ASSERT(data->Hwnd != 0);
742 | POINT pos = { 0, 0 };
743 | ::ClientToScreen(data->Hwnd, &pos);
744 | return ImVec2((float)pos.x, (float)pos.y);
745 | }
746 |
747 | static void ImGui_ImplWin32_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
748 | {
749 | ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
750 | IM_ASSERT(data->Hwnd != 0);
751 | RECT rect = { (LONG)pos.x, (LONG)pos.y, (LONG)pos.x, (LONG)pos.y };
752 | ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle);
753 | ::SetWindowPos(data->Hwnd, NULL, rect.left, rect.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
754 | }
755 |
756 | static ImVec2 ImGui_ImplWin32_GetWindowSize(ImGuiViewport* viewport)
757 | {
758 | ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
759 | IM_ASSERT(data->Hwnd != 0);
760 | RECT rect;
761 | ::GetClientRect(data->Hwnd, &rect);
762 | return ImVec2(float(rect.right - rect.left), float(rect.bottom - rect.top));
763 | }
764 |
765 | static void ImGui_ImplWin32_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
766 | {
767 | ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
768 | IM_ASSERT(data->Hwnd != 0);
769 | RECT rect = { 0, 0, (LONG)size.x, (LONG)size.y };
770 | ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle); // Client to Screen
771 | ::SetWindowPos(data->Hwnd, NULL, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
772 | }
773 |
774 | static void ImGui_ImplWin32_SetWindowFocus(ImGuiViewport* viewport)
775 | {
776 | ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
777 | IM_ASSERT(data->Hwnd != 0);
778 | ::BringWindowToTop(data->Hwnd);
779 | ::SetForegroundWindow(data->Hwnd);
780 | ::SetFocus(data->Hwnd);
781 | }
782 |
783 | static bool ImGui_ImplWin32_GetWindowFocus(ImGuiViewport* viewport)
784 | {
785 | ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
786 | IM_ASSERT(data->Hwnd != 0);
787 | return ::GetForegroundWindow() == data->Hwnd;
788 | }
789 |
790 | static bool ImGui_ImplWin32_GetWindowMinimized(ImGuiViewport* viewport)
791 | {
792 | ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
793 | IM_ASSERT(data->Hwnd != 0);
794 | return ::IsIconic(data->Hwnd) != 0;
795 | }
796 |
797 | static void ImGui_ImplWin32_SetWindowTitle(ImGuiViewport* viewport, const char* title)
798 | {
799 | // ::SetWindowTextA() doesn't properly handle UTF-8 so we explicitely convert our string.
800 | ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
801 | IM_ASSERT(data->Hwnd != 0);
802 | int n = ::MultiByteToWideChar(CP_UTF8, 0, title, -1, NULL, 0);
803 | ImVector title_w;
804 | title_w.resize(n);
805 | ::MultiByteToWideChar(CP_UTF8, 0, title, -1, title_w.Data, n);
806 | ::SetWindowTextW(data->Hwnd, title_w.Data);
807 | }
808 |
809 | static void ImGui_ImplWin32_SetWindowAlpha(ImGuiViewport* viewport, float alpha)
810 | {
811 | ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
812 | IM_ASSERT(data->Hwnd != 0);
813 | IM_ASSERT(alpha >= 0.0f && alpha <= 1.0f);
814 | if (alpha < 1.0f)
815 | {
816 | DWORD style = ::GetWindowLongW(data->Hwnd, GWL_EXSTYLE) | WS_EX_LAYERED;
817 | ::SetWindowLongW(data->Hwnd, GWL_EXSTYLE, style);
818 | ::SetLayeredWindowAttributes(data->Hwnd, 0, (BYTE)(255 * alpha), LWA_ALPHA);
819 | }
820 | else
821 | {
822 | DWORD style = ::GetWindowLongW(data->Hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED;
823 | ::SetWindowLongW(data->Hwnd, GWL_EXSTYLE, style);
824 | }
825 | }
826 |
827 | static float ImGui_ImplWin32_GetWindowDpiScale(ImGuiViewport* viewport)
828 | {
829 | ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
830 | IM_ASSERT(data->Hwnd != 0);
831 | return ImGui_ImplWin32_GetDpiScaleForHwnd(data->Hwnd);
832 | }
833 |
834 | // FIXME-DPI: Testing DPI related ideas
835 | static void ImGui_ImplWin32_OnChangedViewport(ImGuiViewport* viewport)
836 | {
837 | (void)viewport;
838 | #if 0
839 | ImGuiStyle default_style;
840 | //default_style.WindowPadding = ImVec2(0, 0);
841 | //default_style.WindowBorderSize = 0.0f;
842 | //default_style.ItemSpacing.y = 3.0f;
843 | //default_style.FramePadding = ImVec2(0, 0);
844 | default_style.ScaleAllSizes(viewport->DpiScale);
845 | ImGuiStyle& style = ImGui::GetStyle();
846 | style = default_style;
847 | #endif
848 | }
849 |
850 | static LRESULT CALLBACK ImGui_ImplWin32_WndProcHandler_PlatformWindow(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
851 | {
852 | if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
853 | return true;
854 |
855 | if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hWnd))
856 | {
857 | switch (msg)
858 | {
859 | case WM_CLOSE:
860 | viewport->PlatformRequestClose = true;
861 | return 0;
862 | case WM_MOVE:
863 | viewport->PlatformRequestMove = true;
864 | break;
865 | case WM_SIZE:
866 | viewport->PlatformRequestResize = true;
867 | break;
868 | case WM_MOUSEACTIVATE:
869 | if (viewport->Flags & ImGuiViewportFlags_NoFocusOnClick)
870 | return MA_NOACTIVATE;
871 | break;
872 | case WM_NCHITTEST:
873 | // Let mouse pass-through the window. This will allow the backend to set io.MouseHoveredViewport properly (which is OPTIONAL).
874 | // The ImGuiViewportFlags_NoInputs flag is set while dragging a viewport, as want to detect the window behind the one we are dragging.
875 | // If you cannot easily access those viewport flags from your windowing/event code: you may manually synchronize its state e.g. in
876 | // your main loop after calling UpdatePlatformWindows(). Iterate all viewports/platform windows and pass the flag to your windowing system.
877 | if (viewport->Flags & ImGuiViewportFlags_NoInputs)
878 | return HTTRANSPARENT;
879 | break;
880 | }
881 | }
882 |
883 | return DefWindowProc(hWnd, msg, wParam, lParam);
884 | }
885 |
886 | static void ImGui_ImplWin32_InitPlatformInterface()
887 | {
888 | WNDCLASSEX wcex;
889 | wcex.cbSize = sizeof(WNDCLASSEX);
890 | wcex.style = CS_HREDRAW | CS_VREDRAW;
891 | wcex.lpfnWndProc = ImGui_ImplWin32_WndProcHandler_PlatformWindow;
892 | wcex.cbClsExtra = 0;
893 | wcex.cbWndExtra = 0;
894 | wcex.hInstance = ::GetModuleHandle(NULL);
895 | wcex.hIcon = NULL;
896 | wcex.hCursor = NULL;
897 | wcex.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
898 | wcex.lpszMenuName = NULL;
899 | wcex.lpszClassName = _T("ImGui Platform");
900 | wcex.hIconSm = NULL;
901 | ::RegisterClassEx(&wcex);
902 |
903 | ImGui_ImplWin32_UpdateMonitors();
904 |
905 | // Register platform interface (will be coupled with a renderer interface)
906 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
907 | platform_io.Platform_CreateWindow = ImGui_ImplWin32_CreateWindow;
908 | platform_io.Platform_DestroyWindow = ImGui_ImplWin32_DestroyWindow;
909 | platform_io.Platform_ShowWindow = ImGui_ImplWin32_ShowWindow;
910 | platform_io.Platform_SetWindowPos = ImGui_ImplWin32_SetWindowPos;
911 | platform_io.Platform_GetWindowPos = ImGui_ImplWin32_GetWindowPos;
912 | platform_io.Platform_SetWindowSize = ImGui_ImplWin32_SetWindowSize;
913 | platform_io.Platform_GetWindowSize = ImGui_ImplWin32_GetWindowSize;
914 | platform_io.Platform_SetWindowFocus = ImGui_ImplWin32_SetWindowFocus;
915 | platform_io.Platform_GetWindowFocus = ImGui_ImplWin32_GetWindowFocus;
916 | platform_io.Platform_GetWindowMinimized = ImGui_ImplWin32_GetWindowMinimized;
917 | platform_io.Platform_SetWindowTitle = ImGui_ImplWin32_SetWindowTitle;
918 | platform_io.Platform_SetWindowAlpha = ImGui_ImplWin32_SetWindowAlpha;
919 | platform_io.Platform_UpdateWindow = ImGui_ImplWin32_UpdateWindow;
920 | platform_io.Platform_GetWindowDpiScale = ImGui_ImplWin32_GetWindowDpiScale; // FIXME-DPI
921 | platform_io.Platform_OnChangedViewport = ImGui_ImplWin32_OnChangedViewport; // FIXME-DPI
922 | #if HAS_WIN32_IME
923 | platform_io.Platform_SetImeInputPos = ImGui_ImplWin32_SetImeInputPos;
924 | #endif
925 |
926 | // Register main window handle (which is owned by the main application, not by us)
927 | // This is mostly for simplicity and consistency, so that our code (e.g. mouse handling etc.) can use same logic for main and secondary viewports.
928 | ImGuiViewport* main_viewport = ImGui::GetMainViewport();
929 | ImGuiViewportDataWin32* data = IM_NEW(ImGuiViewportDataWin32)();
930 | data->Hwnd = g_hWnd;
931 | data->HwndOwned = false;
932 | main_viewport->PlatformUserData = data;
933 | main_viewport->PlatformHandle = (void*)g_hWnd;
934 | }
935 |
936 | static void ImGui_ImplWin32_ShutdownPlatformInterface()
937 | {
938 | ::UnregisterClass(_T("ImGui Platform"), ::GetModuleHandle(NULL));
939 | }
940 |
941 | //---------------------------------------------------------------------------------------------------------
942 | // Transparency related helpers (optional)
943 | //--------------------------------------------------------------------------------------------------------
944 |
945 | #if defined(_MSC_VER)
946 | #pragma comment(lib, "dwmapi") // Link with dwmapi.lib. MinGW will require linking with '-ldwmapi'
947 | #endif
948 |
949 | // [experimental]
950 | // Borrowed from GLFW's function updateFramebufferTransparency() in src/win32_window.c
951 | // (the Dwm* functions are Vista era functions but we are borrowing logic from GLFW)
952 | void ImGui_ImplWin32_EnableAlphaCompositing(void* hwnd)
953 | {
954 | if (!IsWindowsVistaOrGreater())
955 | return;
956 |
957 | BOOL composition;
958 | if (FAILED(::DwmIsCompositionEnabled(&composition)) || !composition)
959 | return;
960 |
961 | BOOL opaque;
962 | DWORD color;
963 | if (IsWindows8OrGreater() || (SUCCEEDED(::DwmGetColorizationColor(&color, &opaque)) && !opaque))
964 | {
965 | HRGN region = ::CreateRectRgn(0, 0, -1, -1);
966 | DWM_BLURBEHIND bb = {};
967 | bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION;
968 | bb.hRgnBlur = region;
969 | bb.fEnable = TRUE;
970 | ::DwmEnableBlurBehindWindow((HWND)hwnd, &bb);
971 | ::DeleteObject(region);
972 | }
973 | else
974 | {
975 | DWM_BLURBEHIND bb = {};
976 | bb.dwFlags = DWM_BB_ENABLE;
977 | ::DwmEnableBlurBehindWindow((HWND)hwnd, &bb);
978 | }
979 | }
980 |
981 | //---------------------------------------------------------------------------------------------------------
982 |
--------------------------------------------------------------------------------
/ImGuiDiscordWebHook/imgui/imgui_impl_win32.h:
--------------------------------------------------------------------------------
1 | // dear imgui: Platform Backend for Windows (standard windows API for 32 and 64 bits applications)
2 | // This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
3 |
4 | // Implemented features:
5 | // [X] Platform: Clipboard support (for Win32 this is actually part of core dear imgui)
6 | // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
7 | // [X] Platform: Keyboard arrays indexed using VK_* Virtual Key Codes, e.g. ImGui::IsKeyPressed(VK_SPACE).
8 | // [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
9 | // [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
10 |
11 | // You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
12 | // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
13 | // Read online: https://github.com/ocornut/imgui/tree/master/docs
14 |
15 | #pragma once
16 | #include "imgui.h" // IMGUI_IMPL_API
17 |
18 | IMGUI_IMPL_API bool ImGui_ImplWin32_Init(void* hwnd);
19 | IMGUI_IMPL_API void ImGui_ImplWin32_Shutdown();
20 | IMGUI_IMPL_API void ImGui_ImplWin32_NewFrame();
21 |
22 | // Win32 message handler your application need to call.
23 | // - Intentionally commented out in a '#if 0' block to avoid dragging dependencies on from this helper.
24 | // - You should COPY the line below into your .cpp code to forward declare the function and then you can call it.
25 | #if 0
26 | extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
27 | #endif
28 |
29 | // DPI-related helpers (optional)
30 | // - Use to enable DPI awareness without having to create an application manifest.
31 | // - Your own app may already do this via a manifest or explicit calls. This is mostly useful for our examples/ apps.
32 | // - In theory we could call simple functions from Windows SDK such as SetProcessDPIAware(), SetProcessDpiAwareness(), etc.
33 | // but most of the functions provided by Microsoft require Windows 8.1/10+ SDK at compile time and Windows 8/10+ at runtime,
34 | // neither we want to require the user to have. So we dynamically select and load those functions to avoid dependencies.
35 | IMGUI_IMPL_API void ImGui_ImplWin32_EnableDpiAwareness();
36 | IMGUI_IMPL_API float ImGui_ImplWin32_GetDpiScaleForHwnd(void* hwnd); // HWND hwnd
37 | IMGUI_IMPL_API float ImGui_ImplWin32_GetDpiScaleForMonitor(void* monitor); // HMONITOR monitor
38 |
39 | // Transparency related helpers (optional) [experimental]
40 | // - Use to enable alpha compositing transparency with the desktop.
41 | // - Use together with e.g. clearing your framebuffer with zero-alpha.
42 | IMGUI_IMPL_API void ImGui_ImplWin32_EnableAlphaCompositing(void* hwnd); // HWND hwnd
43 |
--------------------------------------------------------------------------------
/ImGuiDiscordWebHook/imgui/imstb_rectpack.h:
--------------------------------------------------------------------------------
1 | // [DEAR IMGUI]
2 | // This is a slightly modified version of stb_rect_pack.h 1.00.
3 | // Those changes would need to be pushed into nothings/stb:
4 | // - Added STBRP__CDECL
5 | // Grep for [DEAR IMGUI] to find the changes.
6 |
7 | // stb_rect_pack.h - v1.00 - public domain - rectangle packing
8 | // Sean Barrett 2014
9 | //
10 | // Useful for e.g. packing rectangular textures into an atlas.
11 | // Does not do rotation.
12 | //
13 | // Not necessarily the awesomest packing method, but better than
14 | // the totally naive one in stb_truetype (which is primarily what
15 | // this is meant to replace).
16 | //
17 | // Has only had a few tests run, may have issues.
18 | //
19 | // More docs to come.
20 | //
21 | // No memory allocations; uses qsort() and assert() from stdlib.
22 | // Can override those by defining STBRP_SORT and STBRP_ASSERT.
23 | //
24 | // This library currently uses the Skyline Bottom-Left algorithm.
25 | //
26 | // Please note: better rectangle packers are welcome! Please
27 | // implement them to the same API, but with a different init
28 | // function.
29 | //
30 | // Credits
31 | //
32 | // Library
33 | // Sean Barrett
34 | // Minor features
35 | // Martins Mozeiko
36 | // github:IntellectualKitty
37 | //
38 | // Bugfixes / warning fixes
39 | // Jeremy Jaussaud
40 | // Fabian Giesen
41 | //
42 | // Version history:
43 | //
44 | // 1.00 (2019-02-25) avoid small space waste; gracefully fail too-wide rectangles
45 | // 0.99 (2019-02-07) warning fixes
46 | // 0.11 (2017-03-03) return packing success/fail result
47 | // 0.10 (2016-10-25) remove cast-away-const to avoid warnings
48 | // 0.09 (2016-08-27) fix compiler warnings
49 | // 0.08 (2015-09-13) really fix bug with empty rects (w=0 or h=0)
50 | // 0.07 (2015-09-13) fix bug with empty rects (w=0 or h=0)
51 | // 0.06 (2015-04-15) added STBRP_SORT to allow replacing qsort
52 | // 0.05: added STBRP_ASSERT to allow replacing assert
53 | // 0.04: fixed minor bug in STBRP_LARGE_RECTS support
54 | // 0.01: initial release
55 | //
56 | // LICENSE
57 | //
58 | // See end of file for license information.
59 |
60 | //////////////////////////////////////////////////////////////////////////////
61 | //
62 | // INCLUDE SECTION
63 | //
64 |
65 | #ifndef STB_INCLUDE_STB_RECT_PACK_H
66 | #define STB_INCLUDE_STB_RECT_PACK_H
67 |
68 | #define STB_RECT_PACK_VERSION 1
69 |
70 | #ifdef STBRP_STATIC
71 | #define STBRP_DEF static
72 | #else
73 | #define STBRP_DEF extern
74 | #endif
75 |
76 | #ifdef __cplusplus
77 | extern "C" {
78 | #endif
79 |
80 | typedef struct stbrp_context stbrp_context;
81 | typedef struct stbrp_node stbrp_node;
82 | typedef struct stbrp_rect stbrp_rect;
83 |
84 | #ifdef STBRP_LARGE_RECTS
85 | typedef int stbrp_coord;
86 | #else
87 | typedef unsigned short stbrp_coord;
88 | #endif
89 |
90 | STBRP_DEF int stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects);
91 | // Assign packed locations to rectangles. The rectangles are of type
92 | // 'stbrp_rect' defined below, stored in the array 'rects', and there
93 | // are 'num_rects' many of them.
94 | //
95 | // Rectangles which are successfully packed have the 'was_packed' flag
96 | // set to a non-zero value and 'x' and 'y' store the minimum location
97 | // on each axis (i.e. bottom-left in cartesian coordinates, top-left
98 | // if you imagine y increasing downwards). Rectangles which do not fit
99 | // have the 'was_packed' flag set to 0.
100 | //
101 | // You should not try to access the 'rects' array from another thread
102 | // while this function is running, as the function temporarily reorders
103 | // the array while it executes.
104 | //
105 | // To pack into another rectangle, you need to call stbrp_init_target
106 | // again. To continue packing into the same rectangle, you can call
107 | // this function again. Calling this multiple times with multiple rect
108 | // arrays will probably produce worse packing results than calling it
109 | // a single time with the full rectangle array, but the option is
110 | // available.
111 | //
112 | // The function returns 1 if all of the rectangles were successfully
113 | // packed and 0 otherwise.
114 |
115 | struct stbrp_rect
116 | {
117 | // reserved for your use:
118 | int id;
119 |
120 | // input:
121 | stbrp_coord w, h;
122 |
123 | // output:
124 | stbrp_coord x, y;
125 | int was_packed; // non-zero if valid packing
126 |
127 | }; // 16 bytes, nominally
128 |
129 |
130 | STBRP_DEF void stbrp_init_target (stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes);
131 | // Initialize a rectangle packer to:
132 | // pack a rectangle that is 'width' by 'height' in dimensions
133 | // using temporary storage provided by the array 'nodes', which is 'num_nodes' long
134 | //
135 | // You must call this function every time you start packing into a new target.
136 | //
137 | // There is no "shutdown" function. The 'nodes' memory must stay valid for
138 | // the following stbrp_pack_rects() call (or calls), but can be freed after
139 | // the call (or calls) finish.
140 | //
141 | // Note: to guarantee best results, either:
142 | // 1. make sure 'num_nodes' >= 'width'
143 | // or 2. call stbrp_allow_out_of_mem() defined below with 'allow_out_of_mem = 1'
144 | //
145 | // If you don't do either of the above things, widths will be quantized to multiples
146 | // of small integers to guarantee the algorithm doesn't run out of temporary storage.
147 | //
148 | // If you do #2, then the non-quantized algorithm will be used, but the algorithm
149 | // may run out of temporary storage and be unable to pack some rectangles.
150 |
151 | STBRP_DEF void stbrp_setup_allow_out_of_mem (stbrp_context *context, int allow_out_of_mem);
152 | // Optionally call this function after init but before doing any packing to
153 | // change the handling of the out-of-temp-memory scenario, described above.
154 | // If you call init again, this will be reset to the default (false).
155 |
156 |
157 | STBRP_DEF void stbrp_setup_heuristic (stbrp_context *context, int heuristic);
158 | // Optionally select which packing heuristic the library should use. Different
159 | // heuristics will produce better/worse results for different data sets.
160 | // If you call init again, this will be reset to the default.
161 |
162 | enum
163 | {
164 | STBRP_HEURISTIC_Skyline_default=0,
165 | STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default,
166 | STBRP_HEURISTIC_Skyline_BF_sortHeight
167 | };
168 |
169 |
170 | //////////////////////////////////////////////////////////////////////////////
171 | //
172 | // the details of the following structures don't matter to you, but they must
173 | // be visible so you can handle the memory allocations for them
174 |
175 | struct stbrp_node
176 | {
177 | stbrp_coord x,y;
178 | stbrp_node *next;
179 | };
180 |
181 | struct stbrp_context
182 | {
183 | int width;
184 | int height;
185 | int align;
186 | int init_mode;
187 | int heuristic;
188 | int num_nodes;
189 | stbrp_node *active_head;
190 | stbrp_node *free_head;
191 | stbrp_node extra[2]; // we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2'
192 | };
193 |
194 | #ifdef __cplusplus
195 | }
196 | #endif
197 |
198 | #endif
199 |
200 | //////////////////////////////////////////////////////////////////////////////
201 | //
202 | // IMPLEMENTATION SECTION
203 | //
204 |
205 | #ifdef STB_RECT_PACK_IMPLEMENTATION
206 | #ifndef STBRP_SORT
207 | #include
208 | #define STBRP_SORT qsort
209 | #endif
210 |
211 | #ifndef STBRP_ASSERT
212 | #include
213 | #define STBRP_ASSERT assert
214 | #endif
215 |
216 | // [DEAR IMGUI] Added STBRP__CDECL
217 | #ifdef _MSC_VER
218 | #define STBRP__NOTUSED(v) (void)(v)
219 | #define STBRP__CDECL __cdecl
220 | #else
221 | #define STBRP__NOTUSED(v) (void)sizeof(v)
222 | #define STBRP__CDECL
223 | #endif
224 |
225 | enum
226 | {
227 | STBRP__INIT_skyline = 1
228 | };
229 |
230 | STBRP_DEF void stbrp_setup_heuristic(stbrp_context *context, int heuristic)
231 | {
232 | switch (context->init_mode) {
233 | case STBRP__INIT_skyline:
234 | STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight);
235 | context->heuristic = heuristic;
236 | break;
237 | default:
238 | STBRP_ASSERT(0);
239 | }
240 | }
241 |
242 | STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_out_of_mem)
243 | {
244 | if (allow_out_of_mem)
245 | // if it's ok to run out of memory, then don't bother aligning them;
246 | // this gives better packing, but may fail due to OOM (even though
247 | // the rectangles easily fit). @TODO a smarter approach would be to only
248 | // quantize once we've hit OOM, then we could get rid of this parameter.
249 | context->align = 1;
250 | else {
251 | // if it's not ok to run out of memory, then quantize the widths
252 | // so that num_nodes is always enough nodes.
253 | //
254 | // I.e. num_nodes * align >= width
255 | // align >= width / num_nodes
256 | // align = ceil(width/num_nodes)
257 |
258 | context->align = (context->width + context->num_nodes-1) / context->num_nodes;
259 | }
260 | }
261 |
262 | STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes)
263 | {
264 | int i;
265 | #ifndef STBRP_LARGE_RECTS
266 | STBRP_ASSERT(width <= 0xffff && height <= 0xffff);
267 | #endif
268 |
269 | for (i=0; i < num_nodes-1; ++i)
270 | nodes[i].next = &nodes[i+1];
271 | nodes[i].next = NULL;
272 | context->init_mode = STBRP__INIT_skyline;
273 | context->heuristic = STBRP_HEURISTIC_Skyline_default;
274 | context->free_head = &nodes[0];
275 | context->active_head = &context->extra[0];
276 | context->width = width;
277 | context->height = height;
278 | context->num_nodes = num_nodes;
279 | stbrp_setup_allow_out_of_mem(context, 0);
280 |
281 | // node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly)
282 | context->extra[0].x = 0;
283 | context->extra[0].y = 0;
284 | context->extra[0].next = &context->extra[1];
285 | context->extra[1].x = (stbrp_coord) width;
286 | #ifdef STBRP_LARGE_RECTS
287 | context->extra[1].y = (1<<30);
288 | #else
289 | context->extra[1].y = 65535;
290 | #endif
291 | context->extra[1].next = NULL;
292 | }
293 |
294 | // find minimum y position if it starts at x1
295 | static int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0, int width, int *pwaste)
296 | {
297 | stbrp_node *node = first;
298 | int x1 = x0 + width;
299 | int min_y, visited_width, waste_area;
300 |
301 | STBRP__NOTUSED(c);
302 |
303 | STBRP_ASSERT(first->x <= x0);
304 |
305 | #if 0
306 | // skip in case we're past the node
307 | while (node->next->x <= x0)
308 | ++node;
309 | #else
310 | STBRP_ASSERT(node->next->x > x0); // we ended up handling this in the caller for efficiency
311 | #endif
312 |
313 | STBRP_ASSERT(node->x <= x0);
314 |
315 | min_y = 0;
316 | waste_area = 0;
317 | visited_width = 0;
318 | while (node->x < x1) {
319 | if (node->y > min_y) {
320 | // raise min_y higher.
321 | // we've accounted for all waste up to min_y,
322 | // but we'll now add more waste for everything we've visted
323 | waste_area += visited_width * (node->y - min_y);
324 | min_y = node->y;
325 | // the first time through, visited_width might be reduced
326 | if (node->x < x0)
327 | visited_width += node->next->x - x0;
328 | else
329 | visited_width += node->next->x - node->x;
330 | } else {
331 | // add waste area
332 | int under_width = node->next->x - node->x;
333 | if (under_width + visited_width > width)
334 | under_width = width - visited_width;
335 | waste_area += under_width * (min_y - node->y);
336 | visited_width += under_width;
337 | }
338 | node = node->next;
339 | }
340 |
341 | *pwaste = waste_area;
342 | return min_y;
343 | }
344 |
345 | typedef struct
346 | {
347 | int x,y;
348 | stbrp_node **prev_link;
349 | } stbrp__findresult;
350 |
351 | static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height)
352 | {
353 | int best_waste = (1<<30), best_x, best_y = (1 << 30);
354 | stbrp__findresult fr;
355 | stbrp_node **prev, *node, *tail, **best = NULL;
356 |
357 | // align to multiple of c->align
358 | width = (width + c->align - 1);
359 | width -= width % c->align;
360 | STBRP_ASSERT(width % c->align == 0);
361 |
362 | // if it can't possibly fit, bail immediately
363 | if (width > c->width || height > c->height) {
364 | fr.prev_link = NULL;
365 | fr.x = fr.y = 0;
366 | return fr;
367 | }
368 |
369 | node = c->active_head;
370 | prev = &c->active_head;
371 | while (node->x + width <= c->width) {
372 | int y,waste;
373 | y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste);
374 | if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { // actually just want to test BL
375 | // bottom left
376 | if (y < best_y) {
377 | best_y = y;
378 | best = prev;
379 | }
380 | } else {
381 | // best-fit
382 | if (y + height <= c->height) {
383 | // can only use it if it first vertically
384 | if (y < best_y || (y == best_y && waste < best_waste)) {
385 | best_y = y;
386 | best_waste = waste;
387 | best = prev;
388 | }
389 | }
390 | }
391 | prev = &node->next;
392 | node = node->next;
393 | }
394 |
395 | best_x = (best == NULL) ? 0 : (*best)->x;
396 |
397 | // if doing best-fit (BF), we also have to try aligning right edge to each node position
398 | //
399 | // e.g, if fitting
400 | //
401 | // ____________________
402 | // |____________________|
403 | //
404 | // into
405 | //
406 | // | |
407 | // | ____________|
408 | // |____________|
409 | //
410 | // then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned
411 | //
412 | // This makes BF take about 2x the time
413 |
414 | if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) {
415 | tail = c->active_head;
416 | node = c->active_head;
417 | prev = &c->active_head;
418 | // find first node that's admissible
419 | while (tail->x < width)
420 | tail = tail->next;
421 | while (tail) {
422 | int xpos = tail->x - width;
423 | int y,waste;
424 | STBRP_ASSERT(xpos >= 0);
425 | // find the left position that matches this
426 | while (node->next->x <= xpos) {
427 | prev = &node->next;
428 | node = node->next;
429 | }
430 | STBRP_ASSERT(node->next->x > xpos && node->x <= xpos);
431 | y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);
432 | if (y + height <= c->height) {
433 | if (y <= best_y) {
434 | if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
435 | best_x = xpos;
436 | STBRP_ASSERT(y <= best_y);
437 | best_y = y;
438 | best_waste = waste;
439 | best = prev;
440 | }
441 | }
442 | }
443 | tail = tail->next;
444 | }
445 | }
446 |
447 | fr.prev_link = best;
448 | fr.x = best_x;
449 | fr.y = best_y;
450 | return fr;
451 | }
452 |
453 | static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height)
454 | {
455 | // find best position according to heuristic
456 | stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height);
457 | stbrp_node *node, *cur;
458 |
459 | // bail if:
460 | // 1. it failed
461 | // 2. the best node doesn't fit (we don't always check this)
462 | // 3. we're out of memory
463 | if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) {
464 | res.prev_link = NULL;
465 | return res;
466 | }
467 |
468 | // on success, create new node
469 | node = context->free_head;
470 | node->x = (stbrp_coord) res.x;
471 | node->y = (stbrp_coord) (res.y + height);
472 |
473 | context->free_head = node->next;
474 |
475 | // insert the new node into the right starting point, and
476 | // let 'cur' point to the remaining nodes needing to be
477 | // stiched back in
478 |
479 | cur = *res.prev_link;
480 | if (cur->x < res.x) {
481 | // preserve the existing one, so start testing with the next one
482 | stbrp_node *next = cur->next;
483 | cur->next = node;
484 | cur = next;
485 | } else {
486 | *res.prev_link = node;
487 | }
488 |
489 | // from here, traverse cur and free the nodes, until we get to one
490 | // that shouldn't be freed
491 | while (cur->next && cur->next->x <= res.x + width) {
492 | stbrp_node *next = cur->next;
493 | // move the current node to the free list
494 | cur->next = context->free_head;
495 | context->free_head = cur;
496 | cur = next;
497 | }
498 |
499 | // stitch the list back in
500 | node->next = cur;
501 |
502 | if (cur->x < res.x + width)
503 | cur->x = (stbrp_coord) (res.x + width);
504 |
505 | #ifdef _DEBUG
506 | cur = context->active_head;
507 | while (cur->x < context->width) {
508 | STBRP_ASSERT(cur->x < cur->next->x);
509 | cur = cur->next;
510 | }
511 | STBRP_ASSERT(cur->next == NULL);
512 |
513 | {
514 | int count=0;
515 | cur = context->active_head;
516 | while (cur) {
517 | cur = cur->next;
518 | ++count;
519 | }
520 | cur = context->free_head;
521 | while (cur) {
522 | cur = cur->next;
523 | ++count;
524 | }
525 | STBRP_ASSERT(count == context->num_nodes+2);
526 | }
527 | #endif
528 |
529 | return res;
530 | }
531 |
532 | // [DEAR IMGUI] Added STBRP__CDECL
533 | static int STBRP__CDECL rect_height_compare(const void *a, const void *b)
534 | {
535 | const stbrp_rect *p = (const stbrp_rect *) a;
536 | const stbrp_rect *q = (const stbrp_rect *) b;
537 | if (p->h > q->h)
538 | return -1;
539 | if (p->h < q->h)
540 | return 1;
541 | return (p->w > q->w) ? -1 : (p->w < q->w);
542 | }
543 |
544 | // [DEAR IMGUI] Added STBRP__CDECL
545 | static int STBRP__CDECL rect_original_order(const void *a, const void *b)
546 | {
547 | const stbrp_rect *p = (const stbrp_rect *) a;
548 | const stbrp_rect *q = (const stbrp_rect *) b;
549 | return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
550 | }
551 |
552 | #ifdef STBRP_LARGE_RECTS
553 | #define STBRP__MAXVAL 0xffffffff
554 | #else
555 | #define STBRP__MAXVAL 0xffff
556 | #endif
557 |
558 | STBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects)
559 | {
560 | int i, all_rects_packed = 1;
561 |
562 | // we use the 'was_packed' field internally to allow sorting/unsorting
563 | for (i=0; i < num_rects; ++i) {
564 | rects[i].was_packed = i;
565 | }
566 |
567 | // sort according to heuristic
568 | STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare);
569 |
570 | for (i=0; i < num_rects; ++i) {
571 | if (rects[i].w == 0 || rects[i].h == 0) {
572 | rects[i].x = rects[i].y = 0; // empty rect needs no space
573 | } else {
574 | stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
575 | if (fr.prev_link) {
576 | rects[i].x = (stbrp_coord) fr.x;
577 | rects[i].y = (stbrp_coord) fr.y;
578 | } else {
579 | rects[i].x = rects[i].y = STBRP__MAXVAL;
580 | }
581 | }
582 | }
583 |
584 | // unsort
585 | STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order);
586 |
587 | // set was_packed flags and all_rects_packed status
588 | for (i=0; i < num_rects; ++i) {
589 | rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL);
590 | if (!rects[i].was_packed)
591 | all_rects_packed = 0;
592 | }
593 |
594 | // return the all_rects_packed status
595 | return all_rects_packed;
596 | }
597 | #endif
598 |
599 | /*
600 | ------------------------------------------------------------------------------
601 | This software is available under 2 licenses -- choose whichever you prefer.
602 | ------------------------------------------------------------------------------
603 | ALTERNATIVE A - MIT License
604 | Copyright (c) 2017 Sean Barrett
605 | Permission is hereby granted, free of charge, to any person obtaining a copy of
606 | this software and associated documentation files (the "Software"), to deal in
607 | the Software without restriction, including without limitation the rights to
608 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
609 | of the Software, and to permit persons to whom the Software is furnished to do
610 | so, subject to the following conditions:
611 | The above copyright notice and this permission notice shall be included in all
612 | copies or substantial portions of the Software.
613 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
614 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
615 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
616 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
617 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
618 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
619 | SOFTWARE.
620 | ------------------------------------------------------------------------------
621 | ALTERNATIVE B - Public Domain (www.unlicense.org)
622 | This is free and unencumbered software released into the public domain.
623 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
624 | software, either in source code form or as a compiled binary, for any purpose,
625 | commercial or non-commercial, and by any means.
626 | In jurisdictions that recognize copyright laws, the author or authors of this
627 | software dedicate any and all copyright interest in the software to the public
628 | domain. We make this dedication for the benefit of the public at large and to
629 | the detriment of our heirs and successors. We intend this dedication to be an
630 | overt act of relinquishment in perpetuity of all present and future rights to
631 | this software under copyright law.
632 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
633 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
634 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
635 | AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
636 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
637 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
638 | ------------------------------------------------------------------------------
639 | */
640 |
--------------------------------------------------------------------------------
/ImGuiDiscordWebHook/imgui/imstb_textedit.h:
--------------------------------------------------------------------------------
1 | // [DEAR IMGUI]
2 | // This is a slightly modified version of stb_textedit.h 1.13.
3 | // Those changes would need to be pushed into nothings/stb:
4 | // - Fix in stb_textedit_discard_redo (see https://github.com/nothings/stb/issues/321)
5 | // Grep for [DEAR IMGUI] to find the changes.
6 |
7 | // stb_textedit.h - v1.13 - public domain - Sean Barrett
8 | // Development of this library was sponsored by RAD Game Tools
9 | //
10 | // This C header file implements the guts of a multi-line text-editing
11 | // widget; you implement display, word-wrapping, and low-level string
12 | // insertion/deletion, and stb_textedit will map user inputs into
13 | // insertions & deletions, plus updates to the cursor position,
14 | // selection state, and undo state.
15 | //
16 | // It is intended for use in games and other systems that need to build
17 | // their own custom widgets and which do not have heavy text-editing
18 | // requirements (this library is not recommended for use for editing large
19 | // texts, as its performance does not scale and it has limited undo).
20 | //
21 | // Non-trivial behaviors are modelled after Windows text controls.
22 | //
23 | //
24 | // LICENSE
25 | //
26 | // See end of file for license information.
27 | //
28 | //
29 | // DEPENDENCIES
30 | //
31 | // Uses the C runtime function 'memmove', which you can override
32 | // by defining STB_TEXTEDIT_memmove before the implementation.
33 | // Uses no other functions. Performs no runtime allocations.
34 | //
35 | //
36 | // VERSION HISTORY
37 | //
38 | // 1.13 (2019-02-07) fix bug in undo size management
39 | // 1.12 (2018-01-29) user can change STB_TEXTEDIT_KEYTYPE, fix redo to avoid crash
40 | // 1.11 (2017-03-03) fix HOME on last line, dragging off single-line textfield
41 | // 1.10 (2016-10-25) supress warnings about casting away const with -Wcast-qual
42 | // 1.9 (2016-08-27) customizable move-by-word
43 | // 1.8 (2016-04-02) better keyboard handling when mouse button is down
44 | // 1.7 (2015-09-13) change y range handling in case baseline is non-0
45 | // 1.6 (2015-04-15) allow STB_TEXTEDIT_memmove
46 | // 1.5 (2014-09-10) add support for secondary keys for OS X
47 | // 1.4 (2014-08-17) fix signed/unsigned warnings
48 | // 1.3 (2014-06-19) fix mouse clicking to round to nearest char boundary
49 | // 1.2 (2014-05-27) fix some RAD types that had crept into the new code
50 | // 1.1 (2013-12-15) move-by-word (requires STB_TEXTEDIT_IS_SPACE )
51 | // 1.0 (2012-07-26) improve documentation, initial public release
52 | // 0.3 (2012-02-24) bugfixes, single-line mode; insert mode
53 | // 0.2 (2011-11-28) fixes to undo/redo
54 | // 0.1 (2010-07-08) initial version
55 | //
56 | // ADDITIONAL CONTRIBUTORS
57 | //
58 | // Ulf Winklemann: move-by-word in 1.1
59 | // Fabian Giesen: secondary key inputs in 1.5
60 | // Martins Mozeiko: STB_TEXTEDIT_memmove in 1.6
61 | //
62 | // Bugfixes:
63 | // Scott Graham
64 | // Daniel Keller
65 | // Omar Cornut
66 | // Dan Thompson
67 | //
68 | // USAGE
69 | //
70 | // This file behaves differently depending on what symbols you define
71 | // before including it.
72 | //
73 | //
74 | // Header-file mode:
75 | //
76 | // If you do not define STB_TEXTEDIT_IMPLEMENTATION before including this,
77 | // it will operate in "header file" mode. In this mode, it declares a
78 | // single public symbol, STB_TexteditState, which encapsulates the current
79 | // state of a text widget (except for the string, which you will store
80 | // separately).
81 | //
82 | // To compile in this mode, you must define STB_TEXTEDIT_CHARTYPE to a
83 | // primitive type that defines a single character (e.g. char, wchar_t, etc).
84 | //
85 | // To save space or increase undo-ability, you can optionally define the
86 | // following things that are used by the undo system:
87 | //
88 | // STB_TEXTEDIT_POSITIONTYPE small int type encoding a valid cursor position
89 | // STB_TEXTEDIT_UNDOSTATECOUNT the number of undo states to allow
90 | // STB_TEXTEDIT_UNDOCHARCOUNT the number of characters to store in the undo buffer
91 | //
92 | // If you don't define these, they are set to permissive types and
93 | // moderate sizes. The undo system does no memory allocations, so
94 | // it grows STB_TexteditState by the worst-case storage which is (in bytes):
95 | //
96 | // [4 + 3 * sizeof(STB_TEXTEDIT_POSITIONTYPE)] * STB_TEXTEDIT_UNDOSTATE_COUNT
97 | // + sizeof(STB_TEXTEDIT_CHARTYPE) * STB_TEXTEDIT_UNDOCHAR_COUNT
98 | //
99 | //
100 | // Implementation mode:
101 | //
102 | // If you define STB_TEXTEDIT_IMPLEMENTATION before including this, it
103 | // will compile the implementation of the text edit widget, depending
104 | // on a large number of symbols which must be defined before the include.
105 | //
106 | // The implementation is defined only as static functions. You will then
107 | // need to provide your own APIs in the same file which will access the
108 | // static functions.
109 | //
110 | // The basic concept is that you provide a "string" object which
111 | // behaves like an array of characters. stb_textedit uses indices to
112 | // refer to positions in the string, implicitly representing positions
113 | // in the displayed textedit. This is true for both plain text and
114 | // rich text; even with rich text stb_truetype interacts with your
115 | // code as if there was an array of all the displayed characters.
116 | //
117 | // Symbols that must be the same in header-file and implementation mode:
118 | //
119 | // STB_TEXTEDIT_CHARTYPE the character type
120 | // STB_TEXTEDIT_POSITIONTYPE small type that is a valid cursor position
121 | // STB_TEXTEDIT_UNDOSTATECOUNT the number of undo states to allow
122 | // STB_TEXTEDIT_UNDOCHARCOUNT the number of characters to store in the undo buffer
123 | //
124 | // Symbols you must define for implementation mode:
125 | //
126 | // STB_TEXTEDIT_STRING the type of object representing a string being edited,
127 | // typically this is a wrapper object with other data you need
128 | //
129 | // STB_TEXTEDIT_STRINGLEN(obj) the length of the string (ideally O(1))
130 | // STB_TEXTEDIT_LAYOUTROW(&r,obj,n) returns the results of laying out a line of characters
131 | // starting from character #n (see discussion below)
132 | // STB_TEXTEDIT_GETWIDTH(obj,n,i) returns the pixel delta from the xpos of the i'th character
133 | // to the xpos of the i+1'th char for a line of characters
134 | // starting at character #n (i.e. accounts for kerning
135 | // with previous char)
136 | // STB_TEXTEDIT_KEYTOTEXT(k) maps a keyboard input to an insertable character
137 | // (return type is int, -1 means not valid to insert)
138 | // STB_TEXTEDIT_GETCHAR(obj,i) returns the i'th character of obj, 0-based
139 | // STB_TEXTEDIT_NEWLINE the character returned by _GETCHAR() we recognize
140 | // as manually wordwrapping for end-of-line positioning
141 | //
142 | // STB_TEXTEDIT_DELETECHARS(obj,i,n) delete n characters starting at i
143 | // STB_TEXTEDIT_INSERTCHARS(obj,i,c*,n) insert n characters at i (pointed to by STB_TEXTEDIT_CHARTYPE*)
144 | //
145 | // STB_TEXTEDIT_K_SHIFT a power of two that is or'd in to a keyboard input to represent the shift key
146 | //
147 | // STB_TEXTEDIT_K_LEFT keyboard input to move cursor left
148 | // STB_TEXTEDIT_K_RIGHT keyboard input to move cursor right
149 | // STB_TEXTEDIT_K_UP keyboard input to move cursor up
150 | // STB_TEXTEDIT_K_DOWN keyboard input to move cursor down
151 | // STB_TEXTEDIT_K_PGUP keyboard input to move cursor up a page
152 | // STB_TEXTEDIT_K_PGDOWN keyboard input to move cursor down a page
153 | // STB_TEXTEDIT_K_LINESTART keyboard input to move cursor to start of line // e.g. HOME
154 | // STB_TEXTEDIT_K_LINEEND keyboard input to move cursor to end of line // e.g. END
155 | // STB_TEXTEDIT_K_TEXTSTART keyboard input to move cursor to start of text // e.g. ctrl-HOME
156 | // STB_TEXTEDIT_K_TEXTEND keyboard input to move cursor to end of text // e.g. ctrl-END
157 | // STB_TEXTEDIT_K_DELETE keyboard input to delete selection or character under cursor
158 | // STB_TEXTEDIT_K_BACKSPACE keyboard input to delete selection or character left of cursor
159 | // STB_TEXTEDIT_K_UNDO keyboard input to perform undo
160 | // STB_TEXTEDIT_K_REDO keyboard input to perform redo
161 | //
162 | // Optional:
163 | // STB_TEXTEDIT_K_INSERT keyboard input to toggle insert mode
164 | // STB_TEXTEDIT_IS_SPACE(ch) true if character is whitespace (e.g. 'isspace'),
165 | // required for default WORDLEFT/WORDRIGHT handlers
166 | // STB_TEXTEDIT_MOVEWORDLEFT(obj,i) custom handler for WORDLEFT, returns index to move cursor to
167 | // STB_TEXTEDIT_MOVEWORDRIGHT(obj,i) custom handler for WORDRIGHT, returns index to move cursor to
168 | // STB_TEXTEDIT_K_WORDLEFT keyboard input to move cursor left one word // e.g. ctrl-LEFT
169 | // STB_TEXTEDIT_K_WORDRIGHT keyboard input to move cursor right one word // e.g. ctrl-RIGHT
170 | // STB_TEXTEDIT_K_LINESTART2 secondary keyboard input to move cursor to start of line
171 | // STB_TEXTEDIT_K_LINEEND2 secondary keyboard input to move cursor to end of line
172 | // STB_TEXTEDIT_K_TEXTSTART2 secondary keyboard input to move cursor to start of text
173 | // STB_TEXTEDIT_K_TEXTEND2 secondary keyboard input to move cursor to end of text
174 | //
175 | // Keyboard input must be encoded as a single integer value; e.g. a character code
176 | // and some bitflags that represent shift states. to simplify the interface, SHIFT must
177 | // be a bitflag, so we can test the shifted state of cursor movements to allow selection,
178 | // i.e. (STB_TEXTEDIT_K_RIGHT|STB_TEXTEDIT_K_SHIFT) should be shifted right-arrow.
179 | //
180 | // You can encode other things, such as CONTROL or ALT, in additional bits, and
181 | // then test for their presence in e.g. STB_TEXTEDIT_K_WORDLEFT. For example,
182 | // my Windows implementations add an additional CONTROL bit, and an additional KEYDOWN
183 | // bit. Then all of the STB_TEXTEDIT_K_ values bitwise-or in the KEYDOWN bit,
184 | // and I pass both WM_KEYDOWN and WM_CHAR events to the "key" function in the
185 | // API below. The control keys will only match WM_KEYDOWN events because of the
186 | // keydown bit I add, and STB_TEXTEDIT_KEYTOTEXT only tests for the KEYDOWN
187 | // bit so it only decodes WM_CHAR events.
188 | //
189 | // STB_TEXTEDIT_LAYOUTROW returns information about the shape of one displayed
190 | // row of characters assuming they start on the i'th character--the width and
191 | // the height and the number of characters consumed. This allows this library
192 | // to traverse the entire layout incrementally. You need to compute word-wrapping
193 | // here.
194 | //
195 | // Each textfield keeps its own insert mode state, which is not how normal
196 | // applications work. To keep an app-wide insert mode, update/copy the
197 | // "insert_mode" field of STB_TexteditState before/after calling API functions.
198 | //
199 | // API
200 | //
201 | // void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line)
202 | //
203 | // void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)
204 | // void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)
205 | // int stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)
206 | // int stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len)
207 | // void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXEDIT_KEYTYPE key)
208 | //
209 | // Each of these functions potentially updates the string and updates the
210 | // state.
211 | //
212 | // initialize_state:
213 | // set the textedit state to a known good default state when initially
214 | // constructing the textedit.
215 | //
216 | // click:
217 | // call this with the mouse x,y on a mouse down; it will update the cursor
218 | // and reset the selection start/end to the cursor point. the x,y must
219 | // be relative to the text widget, with (0,0) being the top left.
220 | //
221 | // drag:
222 | // call this with the mouse x,y on a mouse drag/up; it will update the
223 | // cursor and the selection end point
224 | //
225 | // cut:
226 | // call this to delete the current selection; returns true if there was
227 | // one. you should FIRST copy the current selection to the system paste buffer.
228 | // (To copy, just copy the current selection out of the string yourself.)
229 | //
230 | // paste:
231 | // call this to paste text at the current cursor point or over the current
232 | // selection if there is one.
233 | //
234 | // key:
235 | // call this for keyboard inputs sent to the textfield. you can use it
236 | // for "key down" events or for "translated" key events. if you need to
237 | // do both (as in Win32), or distinguish Unicode characters from control
238 | // inputs, set a high bit to distinguish the two; then you can define the
239 | // various definitions like STB_TEXTEDIT_K_LEFT have the is-key-event bit
240 | // set, and make STB_TEXTEDIT_KEYTOCHAR check that the is-key-event bit is
241 | // clear. STB_TEXTEDIT_KEYTYPE defaults to int, but you can #define it to
242 | // anything other type you wante before including.
243 | //
244 | //
245 | // When rendering, you can read the cursor position and selection state from
246 | // the STB_TexteditState.
247 | //
248 | //
249 | // Notes:
250 | //
251 | // This is designed to be usable in IMGUI, so it allows for the possibility of
252 | // running in an IMGUI that has NOT cached the multi-line layout. For this
253 | // reason, it provides an interface that is compatible with computing the
254 | // layout incrementally--we try to make sure we make as few passes through
255 | // as possible. (For example, to locate the mouse pointer in the text, we
256 | // could define functions that return the X and Y positions of characters
257 | // and binary search Y and then X, but if we're doing dynamic layout this
258 | // will run the layout algorithm many times, so instead we manually search
259 | // forward in one pass. Similar logic applies to e.g. up-arrow and
260 | // down-arrow movement.)
261 | //
262 | // If it's run in a widget that *has* cached the layout, then this is less
263 | // efficient, but it's not horrible on modern computers. But you wouldn't
264 | // want to edit million-line files with it.
265 |
266 |
267 | ////////////////////////////////////////////////////////////////////////////
268 | ////////////////////////////////////////////////////////////////////////////
269 | ////
270 | //// Header-file mode
271 | ////
272 | ////
273 |
274 | #ifndef INCLUDE_STB_TEXTEDIT_H
275 | #define INCLUDE_STB_TEXTEDIT_H
276 |
277 | ////////////////////////////////////////////////////////////////////////
278 | //
279 | // STB_TexteditState
280 | //
281 | // Definition of STB_TexteditState which you should store
282 | // per-textfield; it includes cursor position, selection state,
283 | // and undo state.
284 | //
285 |
286 | #ifndef STB_TEXTEDIT_UNDOSTATECOUNT
287 | #define STB_TEXTEDIT_UNDOSTATECOUNT 99
288 | #endif
289 | #ifndef STB_TEXTEDIT_UNDOCHARCOUNT
290 | #define STB_TEXTEDIT_UNDOCHARCOUNT 999
291 | #endif
292 | #ifndef STB_TEXTEDIT_CHARTYPE
293 | #define STB_TEXTEDIT_CHARTYPE int
294 | #endif
295 | #ifndef STB_TEXTEDIT_POSITIONTYPE
296 | #define STB_TEXTEDIT_POSITIONTYPE int
297 | #endif
298 |
299 | typedef struct
300 | {
301 | // private data
302 | STB_TEXTEDIT_POSITIONTYPE where;
303 | STB_TEXTEDIT_POSITIONTYPE insert_length;
304 | STB_TEXTEDIT_POSITIONTYPE delete_length;
305 | int char_storage;
306 | } StbUndoRecord;
307 |
308 | typedef struct
309 | {
310 | // private data
311 | StbUndoRecord undo_rec [STB_TEXTEDIT_UNDOSTATECOUNT];
312 | STB_TEXTEDIT_CHARTYPE undo_char[STB_TEXTEDIT_UNDOCHARCOUNT];
313 | short undo_point, redo_point;
314 | int undo_char_point, redo_char_point;
315 | } StbUndoState;
316 |
317 | typedef struct
318 | {
319 | /////////////////////
320 | //
321 | // public data
322 | //
323 |
324 | int cursor;
325 | // position of the text cursor within the string
326 |
327 | int select_start; // selection start point
328 | int select_end;
329 | // selection start and end point in characters; if equal, no selection.
330 | // note that start may be less than or greater than end (e.g. when
331 | // dragging the mouse, start is where the initial click was, and you
332 | // can drag in either direction)
333 |
334 | unsigned char insert_mode;
335 | // each textfield keeps its own insert mode state. to keep an app-wide
336 | // insert mode, copy this value in/out of the app state
337 |
338 | int row_count_per_page;
339 | // page size in number of row.
340 | // this value MUST be set to >0 for pageup or pagedown in multilines documents.
341 |
342 | /////////////////////
343 | //
344 | // private data
345 | //
346 | unsigned char cursor_at_end_of_line; // not implemented yet
347 | unsigned char initialized;
348 | unsigned char has_preferred_x;
349 | unsigned char single_line;
350 | unsigned char padding1, padding2, padding3;
351 | float preferred_x; // this determines where the cursor up/down tries to seek to along x
352 | StbUndoState undostate;
353 | } STB_TexteditState;
354 |
355 |
356 | ////////////////////////////////////////////////////////////////////////
357 | //
358 | // StbTexteditRow
359 | //
360 | // Result of layout query, used by stb_textedit to determine where
361 | // the text in each row is.
362 |
363 | // result of layout query
364 | typedef struct
365 | {
366 | float x0,x1; // starting x location, end x location (allows for align=right, etc)
367 | float baseline_y_delta; // position of baseline relative to previous row's baseline
368 | float ymin,ymax; // height of row above and below baseline
369 | int num_chars;
370 | } StbTexteditRow;
371 | #endif //INCLUDE_STB_TEXTEDIT_H
372 |
373 |
374 | ////////////////////////////////////////////////////////////////////////////
375 | ////////////////////////////////////////////////////////////////////////////
376 | ////
377 | //// Implementation mode
378 | ////
379 | ////
380 |
381 |
382 | // implementation isn't include-guarded, since it might have indirectly
383 | // included just the "header" portion
384 | #ifdef STB_TEXTEDIT_IMPLEMENTATION
385 |
386 | #ifndef STB_TEXTEDIT_memmove
387 | #include
388 | #define STB_TEXTEDIT_memmove memmove
389 | #endif
390 |
391 |
392 | /////////////////////////////////////////////////////////////////////////////
393 | //
394 | // Mouse input handling
395 | //
396 |
397 | // traverse the layout to locate the nearest character to a display position
398 | static int stb_text_locate_coord(STB_TEXTEDIT_STRING *str, float x, float y)
399 | {
400 | StbTexteditRow r;
401 | int n = STB_TEXTEDIT_STRINGLEN(str);
402 | float base_y = 0, prev_x;
403 | int i=0, k;
404 |
405 | r.x0 = r.x1 = 0;
406 | r.ymin = r.ymax = 0;
407 | r.num_chars = 0;
408 |
409 | // search rows to find one that straddles 'y'
410 | while (i < n) {
411 | STB_TEXTEDIT_LAYOUTROW(&r, str, i);
412 | if (r.num_chars <= 0)
413 | return n;
414 |
415 | if (i==0 && y < base_y + r.ymin)
416 | return 0;
417 |
418 | if (y < base_y + r.ymax)
419 | break;
420 |
421 | i += r.num_chars;
422 | base_y += r.baseline_y_delta;
423 | }
424 |
425 | // below all text, return 'after' last character
426 | if (i >= n)
427 | return n;
428 |
429 | // check if it's before the beginning of the line
430 | if (x < r.x0)
431 | return i;
432 |
433 | // check if it's before the end of the line
434 | if (x < r.x1) {
435 | // search characters in row for one that straddles 'x'
436 | prev_x = r.x0;
437 | for (k=0; k < r.num_chars; ++k) {
438 | float w = STB_TEXTEDIT_GETWIDTH(str, i, k);
439 | if (x < prev_x+w) {
440 | if (x < prev_x+w/2)
441 | return k+i;
442 | else
443 | return k+i+1;
444 | }
445 | prev_x += w;
446 | }
447 | // shouldn't happen, but if it does, fall through to end-of-line case
448 | }
449 |
450 | // if the last character is a newline, return that. otherwise return 'after' the last character
451 | if (STB_TEXTEDIT_GETCHAR(str, i+r.num_chars-1) == STB_TEXTEDIT_NEWLINE)
452 | return i+r.num_chars-1;
453 | else
454 | return i+r.num_chars;
455 | }
456 |
457 | // API click: on mouse down, move the cursor to the clicked location, and reset the selection
458 | static void stb_textedit_click(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)
459 | {
460 | // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse
461 | // goes off the top or bottom of the text
462 | if( state->single_line )
463 | {
464 | StbTexteditRow r;
465 | STB_TEXTEDIT_LAYOUTROW(&r, str, 0);
466 | y = r.ymin;
467 | }
468 |
469 | state->cursor = stb_text_locate_coord(str, x, y);
470 | state->select_start = state->cursor;
471 | state->select_end = state->cursor;
472 | state->has_preferred_x = 0;
473 | }
474 |
475 | // API drag: on mouse drag, move the cursor and selection endpoint to the clicked location
476 | static void stb_textedit_drag(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, float x, float y)
477 | {
478 | int p = 0;
479 |
480 | // In single-line mode, just always make y = 0. This lets the drag keep working if the mouse
481 | // goes off the top or bottom of the text
482 | if( state->single_line )
483 | {
484 | StbTexteditRow r;
485 | STB_TEXTEDIT_LAYOUTROW(&r, str, 0);
486 | y = r.ymin;
487 | }
488 |
489 | if (state->select_start == state->select_end)
490 | state->select_start = state->cursor;
491 |
492 | p = stb_text_locate_coord(str, x, y);
493 | state->cursor = state->select_end = p;
494 | }
495 |
496 | /////////////////////////////////////////////////////////////////////////////
497 | //
498 | // Keyboard input handling
499 | //
500 |
501 | // forward declarations
502 | static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state);
503 | static void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state);
504 | static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length);
505 | static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length);
506 | static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length);
507 |
508 | typedef struct
509 | {
510 | float x,y; // position of n'th character
511 | float height; // height of line
512 | int first_char, length; // first char of row, and length
513 | int prev_first; // first char of previous row
514 | } StbFindState;
515 |
516 | // find the x/y location of a character, and remember info about the previous row in
517 | // case we get a move-up event (for page up, we'll have to rescan)
518 | static void stb_textedit_find_charpos(StbFindState *find, STB_TEXTEDIT_STRING *str, int n, int single_line)
519 | {
520 | StbTexteditRow r;
521 | int prev_start = 0;
522 | int z = STB_TEXTEDIT_STRINGLEN(str);
523 | int i=0, first;
524 |
525 | if (n == z) {
526 | // if it's at the end, then find the last line -- simpler than trying to
527 | // explicitly handle this case in the regular code
528 | if (single_line) {
529 | STB_TEXTEDIT_LAYOUTROW(&r, str, 0);
530 | find->y = 0;
531 | find->first_char = 0;
532 | find->length = z;
533 | find->height = r.ymax - r.ymin;
534 | find->x = r.x1;
535 | } else {
536 | find->y = 0;
537 | find->x = 0;
538 | find->height = 1;
539 | while (i < z) {
540 | STB_TEXTEDIT_LAYOUTROW(&r, str, i);
541 | prev_start = i;
542 | i += r.num_chars;
543 | }
544 | find->first_char = i;
545 | find->length = 0;
546 | find->prev_first = prev_start;
547 | }
548 | return;
549 | }
550 |
551 | // search rows to find the one that straddles character n
552 | find->y = 0;
553 |
554 | for(;;) {
555 | STB_TEXTEDIT_LAYOUTROW(&r, str, i);
556 | if (n < i + r.num_chars)
557 | break;
558 | prev_start = i;
559 | i += r.num_chars;
560 | find->y += r.baseline_y_delta;
561 | }
562 |
563 | find->first_char = first = i;
564 | find->length = r.num_chars;
565 | find->height = r.ymax - r.ymin;
566 | find->prev_first = prev_start;
567 |
568 | // now scan to find xpos
569 | find->x = r.x0;
570 | for (i=0; first+i < n; ++i)
571 | find->x += STB_TEXTEDIT_GETWIDTH(str, first, i);
572 | }
573 |
574 | #define STB_TEXT_HAS_SELECTION(s) ((s)->select_start != (s)->select_end)
575 |
576 | // make the selection/cursor state valid if client altered the string
577 | static void stb_textedit_clamp(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)
578 | {
579 | int n = STB_TEXTEDIT_STRINGLEN(str);
580 | if (STB_TEXT_HAS_SELECTION(state)) {
581 | if (state->select_start > n) state->select_start = n;
582 | if (state->select_end > n) state->select_end = n;
583 | // if clamping forced them to be equal, move the cursor to match
584 | if (state->select_start == state->select_end)
585 | state->cursor = state->select_start;
586 | }
587 | if (state->cursor > n) state->cursor = n;
588 | }
589 |
590 | // delete characters while updating undo
591 | static void stb_textedit_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int len)
592 | {
593 | stb_text_makeundo_delete(str, state, where, len);
594 | STB_TEXTEDIT_DELETECHARS(str, where, len);
595 | state->has_preferred_x = 0;
596 | }
597 |
598 | // delete the section
599 | static void stb_textedit_delete_selection(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)
600 | {
601 | stb_textedit_clamp(str, state);
602 | if (STB_TEXT_HAS_SELECTION(state)) {
603 | if (state->select_start < state->select_end) {
604 | stb_textedit_delete(str, state, state->select_start, state->select_end - state->select_start);
605 | state->select_end = state->cursor = state->select_start;
606 | } else {
607 | stb_textedit_delete(str, state, state->select_end, state->select_start - state->select_end);
608 | state->select_start = state->cursor = state->select_end;
609 | }
610 | state->has_preferred_x = 0;
611 | }
612 | }
613 |
614 | // canoncialize the selection so start <= end
615 | static void stb_textedit_sortselection(STB_TexteditState *state)
616 | {
617 | if (state->select_end < state->select_start) {
618 | int temp = state->select_end;
619 | state->select_end = state->select_start;
620 | state->select_start = temp;
621 | }
622 | }
623 |
624 | // move cursor to first character of selection
625 | static void stb_textedit_move_to_first(STB_TexteditState *state)
626 | {
627 | if (STB_TEXT_HAS_SELECTION(state)) {
628 | stb_textedit_sortselection(state);
629 | state->cursor = state->select_start;
630 | state->select_end = state->select_start;
631 | state->has_preferred_x = 0;
632 | }
633 | }
634 |
635 | // move cursor to last character of selection
636 | static void stb_textedit_move_to_last(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)
637 | {
638 | if (STB_TEXT_HAS_SELECTION(state)) {
639 | stb_textedit_sortselection(state);
640 | stb_textedit_clamp(str, state);
641 | state->cursor = state->select_end;
642 | state->select_start = state->select_end;
643 | state->has_preferred_x = 0;
644 | }
645 | }
646 |
647 | #ifdef STB_TEXTEDIT_IS_SPACE
648 | static int is_word_boundary( STB_TEXTEDIT_STRING *str, int idx )
649 | {
650 | return idx > 0 ? (STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str,idx-1) ) && !STB_TEXTEDIT_IS_SPACE( STB_TEXTEDIT_GETCHAR(str, idx) ) ) : 1;
651 | }
652 |
653 | #ifndef STB_TEXTEDIT_MOVEWORDLEFT
654 | static int stb_textedit_move_to_word_previous( STB_TEXTEDIT_STRING *str, int c )
655 | {
656 | --c; // always move at least one character
657 | while( c >= 0 && !is_word_boundary( str, c ) )
658 | --c;
659 |
660 | if( c < 0 )
661 | c = 0;
662 |
663 | return c;
664 | }
665 | #define STB_TEXTEDIT_MOVEWORDLEFT stb_textedit_move_to_word_previous
666 | #endif
667 |
668 | #ifndef STB_TEXTEDIT_MOVEWORDRIGHT
669 | static int stb_textedit_move_to_word_next( STB_TEXTEDIT_STRING *str, int c )
670 | {
671 | const int len = STB_TEXTEDIT_STRINGLEN(str);
672 | ++c; // always move at least one character
673 | while( c < len && !is_word_boundary( str, c ) )
674 | ++c;
675 |
676 | if( c > len )
677 | c = len;
678 |
679 | return c;
680 | }
681 | #define STB_TEXTEDIT_MOVEWORDRIGHT stb_textedit_move_to_word_next
682 | #endif
683 |
684 | #endif
685 |
686 | // update selection and cursor to match each other
687 | static void stb_textedit_prep_selection_at_cursor(STB_TexteditState *state)
688 | {
689 | if (!STB_TEXT_HAS_SELECTION(state))
690 | state->select_start = state->select_end = state->cursor;
691 | else
692 | state->cursor = state->select_end;
693 | }
694 |
695 | // API cut: delete selection
696 | static int stb_textedit_cut(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)
697 | {
698 | if (STB_TEXT_HAS_SELECTION(state)) {
699 | stb_textedit_delete_selection(str,state); // implicitly clamps
700 | state->has_preferred_x = 0;
701 | return 1;
702 | }
703 | return 0;
704 | }
705 |
706 | // API paste: replace existing selection with passed-in text
707 | static int stb_textedit_paste_internal(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE *text, int len)
708 | {
709 | // if there's a selection, the paste should delete it
710 | stb_textedit_clamp(str, state);
711 | stb_textedit_delete_selection(str,state);
712 | // try to insert the characters
713 | if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, len)) {
714 | stb_text_makeundo_insert(state, state->cursor, len);
715 | state->cursor += len;
716 | state->has_preferred_x = 0;
717 | return 1;
718 | }
719 | // remove the undo since we didn't actually insert the characters
720 | if (state->undostate.undo_point)
721 | --state->undostate.undo_point;
722 | return 0;
723 | }
724 |
725 | #ifndef STB_TEXTEDIT_KEYTYPE
726 | #define STB_TEXTEDIT_KEYTYPE int
727 | #endif
728 |
729 | // API key: process a keyboard input
730 | static void stb_textedit_key(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_KEYTYPE key)
731 | {
732 | retry:
733 | switch (key) {
734 | default: {
735 | int c = STB_TEXTEDIT_KEYTOTEXT(key);
736 | if (c > 0) {
737 | STB_TEXTEDIT_CHARTYPE ch = (STB_TEXTEDIT_CHARTYPE) c;
738 |
739 | // can't add newline in single-line mode
740 | if (c == '\n' && state->single_line)
741 | break;
742 |
743 | if (state->insert_mode && !STB_TEXT_HAS_SELECTION(state) && state->cursor < STB_TEXTEDIT_STRINGLEN(str)) {
744 | stb_text_makeundo_replace(str, state, state->cursor, 1, 1);
745 | STB_TEXTEDIT_DELETECHARS(str, state->cursor, 1);
746 | if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) {
747 | ++state->cursor;
748 | state->has_preferred_x = 0;
749 | }
750 | } else {
751 | stb_textedit_delete_selection(str,state); // implicitly clamps
752 | if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, &ch, 1)) {
753 | stb_text_makeundo_insert(state, state->cursor, 1);
754 | ++state->cursor;
755 | state->has_preferred_x = 0;
756 | }
757 | }
758 | }
759 | break;
760 | }
761 |
762 | #ifdef STB_TEXTEDIT_K_INSERT
763 | case STB_TEXTEDIT_K_INSERT:
764 | state->insert_mode = !state->insert_mode;
765 | break;
766 | #endif
767 |
768 | case STB_TEXTEDIT_K_UNDO:
769 | stb_text_undo(str, state);
770 | state->has_preferred_x = 0;
771 | break;
772 |
773 | case STB_TEXTEDIT_K_REDO:
774 | stb_text_redo(str, state);
775 | state->has_preferred_x = 0;
776 | break;
777 |
778 | case STB_TEXTEDIT_K_LEFT:
779 | // if currently there's a selection, move cursor to start of selection
780 | if (STB_TEXT_HAS_SELECTION(state))
781 | stb_textedit_move_to_first(state);
782 | else
783 | if (state->cursor > 0)
784 | --state->cursor;
785 | state->has_preferred_x = 0;
786 | break;
787 |
788 | case STB_TEXTEDIT_K_RIGHT:
789 | // if currently there's a selection, move cursor to end of selection
790 | if (STB_TEXT_HAS_SELECTION(state))
791 | stb_textedit_move_to_last(str, state);
792 | else
793 | ++state->cursor;
794 | stb_textedit_clamp(str, state);
795 | state->has_preferred_x = 0;
796 | break;
797 |
798 | case STB_TEXTEDIT_K_LEFT | STB_TEXTEDIT_K_SHIFT:
799 | stb_textedit_clamp(str, state);
800 | stb_textedit_prep_selection_at_cursor(state);
801 | // move selection left
802 | if (state->select_end > 0)
803 | --state->select_end;
804 | state->cursor = state->select_end;
805 | state->has_preferred_x = 0;
806 | break;
807 |
808 | #ifdef STB_TEXTEDIT_MOVEWORDLEFT
809 | case STB_TEXTEDIT_K_WORDLEFT:
810 | if (STB_TEXT_HAS_SELECTION(state))
811 | stb_textedit_move_to_first(state);
812 | else {
813 | state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor);
814 | stb_textedit_clamp( str, state );
815 | }
816 | break;
817 |
818 | case STB_TEXTEDIT_K_WORDLEFT | STB_TEXTEDIT_K_SHIFT:
819 | if( !STB_TEXT_HAS_SELECTION( state ) )
820 | stb_textedit_prep_selection_at_cursor(state);
821 |
822 | state->cursor = STB_TEXTEDIT_MOVEWORDLEFT(str, state->cursor);
823 | state->select_end = state->cursor;
824 |
825 | stb_textedit_clamp( str, state );
826 | break;
827 | #endif
828 |
829 | #ifdef STB_TEXTEDIT_MOVEWORDRIGHT
830 | case STB_TEXTEDIT_K_WORDRIGHT:
831 | if (STB_TEXT_HAS_SELECTION(state))
832 | stb_textedit_move_to_last(str, state);
833 | else {
834 | state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor);
835 | stb_textedit_clamp( str, state );
836 | }
837 | break;
838 |
839 | case STB_TEXTEDIT_K_WORDRIGHT | STB_TEXTEDIT_K_SHIFT:
840 | if( !STB_TEXT_HAS_SELECTION( state ) )
841 | stb_textedit_prep_selection_at_cursor(state);
842 |
843 | state->cursor = STB_TEXTEDIT_MOVEWORDRIGHT(str, state->cursor);
844 | state->select_end = state->cursor;
845 |
846 | stb_textedit_clamp( str, state );
847 | break;
848 | #endif
849 |
850 | case STB_TEXTEDIT_K_RIGHT | STB_TEXTEDIT_K_SHIFT:
851 | stb_textedit_prep_selection_at_cursor(state);
852 | // move selection right
853 | ++state->select_end;
854 | stb_textedit_clamp(str, state);
855 | state->cursor = state->select_end;
856 | state->has_preferred_x = 0;
857 | break;
858 |
859 | case STB_TEXTEDIT_K_DOWN:
860 | case STB_TEXTEDIT_K_DOWN | STB_TEXTEDIT_K_SHIFT:
861 | case STB_TEXTEDIT_K_PGDOWN:
862 | case STB_TEXTEDIT_K_PGDOWN | STB_TEXTEDIT_K_SHIFT: {
863 | StbFindState find;
864 | StbTexteditRow row;
865 | int i, j, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;
866 | int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGDOWN;
867 | int row_count = is_page ? state->row_count_per_page : 1;
868 |
869 | if (!is_page && state->single_line) {
870 | // on windows, up&down in single-line behave like left&right
871 | key = STB_TEXTEDIT_K_RIGHT | (key & STB_TEXTEDIT_K_SHIFT);
872 | goto retry;
873 | }
874 |
875 | if (sel)
876 | stb_textedit_prep_selection_at_cursor(state);
877 | else if (STB_TEXT_HAS_SELECTION(state))
878 | stb_textedit_move_to_last(str, state);
879 |
880 | // compute current position of cursor point
881 | stb_textedit_clamp(str, state);
882 | stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);
883 |
884 | for (j = 0; j < row_count; ++j) {
885 | float x, goal_x = state->has_preferred_x ? state->preferred_x : find.x;
886 | int start = find.first_char + find.length;
887 |
888 | if (find.length == 0)
889 | break;
890 |
891 | // [DEAR IMGUI]
892 | // going down while being on the last line shouldn't bring us to that line end
893 | if (STB_TEXTEDIT_GETCHAR(str, find.first_char + find.length - 1) != STB_TEXTEDIT_NEWLINE)
894 | break;
895 |
896 | // now find character position down a row
897 | state->cursor = start;
898 | STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);
899 | x = row.x0;
900 | for (i=0; i < row.num_chars; ++i) {
901 | float dx = STB_TEXTEDIT_GETWIDTH(str, start, i);
902 | #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE
903 | if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE)
904 | break;
905 | #endif
906 | x += dx;
907 | if (x > goal_x)
908 | break;
909 | ++state->cursor;
910 | }
911 | stb_textedit_clamp(str, state);
912 |
913 | state->has_preferred_x = 1;
914 | state->preferred_x = goal_x;
915 |
916 | if (sel)
917 | state->select_end = state->cursor;
918 |
919 | // go to next line
920 | find.first_char = find.first_char + find.length;
921 | find.length = row.num_chars;
922 | }
923 | break;
924 | }
925 |
926 | case STB_TEXTEDIT_K_UP:
927 | case STB_TEXTEDIT_K_UP | STB_TEXTEDIT_K_SHIFT:
928 | case STB_TEXTEDIT_K_PGUP:
929 | case STB_TEXTEDIT_K_PGUP | STB_TEXTEDIT_K_SHIFT: {
930 | StbFindState find;
931 | StbTexteditRow row;
932 | int i, j, prev_scan, sel = (key & STB_TEXTEDIT_K_SHIFT) != 0;
933 | int is_page = (key & ~STB_TEXTEDIT_K_SHIFT) == STB_TEXTEDIT_K_PGUP;
934 | int row_count = is_page ? state->row_count_per_page : 1;
935 |
936 | if (!is_page && state->single_line) {
937 | // on windows, up&down become left&right
938 | key = STB_TEXTEDIT_K_LEFT | (key & STB_TEXTEDIT_K_SHIFT);
939 | goto retry;
940 | }
941 |
942 | if (sel)
943 | stb_textedit_prep_selection_at_cursor(state);
944 | else if (STB_TEXT_HAS_SELECTION(state))
945 | stb_textedit_move_to_first(state);
946 |
947 | // compute current position of cursor point
948 | stb_textedit_clamp(str, state);
949 | stb_textedit_find_charpos(&find, str, state->cursor, state->single_line);
950 |
951 | for (j = 0; j < row_count; ++j) {
952 | float x, goal_x = state->has_preferred_x ? state->preferred_x : find.x;
953 |
954 | // can only go up if there's a previous row
955 | if (find.prev_first == find.first_char)
956 | break;
957 |
958 | // now find character position up a row
959 | state->cursor = find.prev_first;
960 | STB_TEXTEDIT_LAYOUTROW(&row, str, state->cursor);
961 | x = row.x0;
962 | for (i=0; i < row.num_chars; ++i) {
963 | float dx = STB_TEXTEDIT_GETWIDTH(str, find.prev_first, i);
964 | #ifdef STB_TEXTEDIT_GETWIDTH_NEWLINE
965 | if (dx == STB_TEXTEDIT_GETWIDTH_NEWLINE)
966 | break;
967 | #endif
968 | x += dx;
969 | if (x > goal_x)
970 | break;
971 | ++state->cursor;
972 | }
973 | stb_textedit_clamp(str, state);
974 |
975 | state->has_preferred_x = 1;
976 | state->preferred_x = goal_x;
977 |
978 | if (sel)
979 | state->select_end = state->cursor;
980 |
981 | // go to previous line
982 | // (we need to scan previous line the hard way. maybe we could expose this as a new API function?)
983 | prev_scan = find.prev_first > 0 ? find.prev_first - 1 : 0;
984 | while (prev_scan > 0 && STB_TEXTEDIT_GETCHAR(str, prev_scan - 1) != STB_TEXTEDIT_NEWLINE)
985 | --prev_scan;
986 | find.first_char = find.prev_first;
987 | find.prev_first = prev_scan;
988 | }
989 | break;
990 | }
991 |
992 | case STB_TEXTEDIT_K_DELETE:
993 | case STB_TEXTEDIT_K_DELETE | STB_TEXTEDIT_K_SHIFT:
994 | if (STB_TEXT_HAS_SELECTION(state))
995 | stb_textedit_delete_selection(str, state);
996 | else {
997 | int n = STB_TEXTEDIT_STRINGLEN(str);
998 | if (state->cursor < n)
999 | stb_textedit_delete(str, state, state->cursor, 1);
1000 | }
1001 | state->has_preferred_x = 0;
1002 | break;
1003 |
1004 | case STB_TEXTEDIT_K_BACKSPACE:
1005 | case STB_TEXTEDIT_K_BACKSPACE | STB_TEXTEDIT_K_SHIFT:
1006 | if (STB_TEXT_HAS_SELECTION(state))
1007 | stb_textedit_delete_selection(str, state);
1008 | else {
1009 | stb_textedit_clamp(str, state);
1010 | if (state->cursor > 0) {
1011 | stb_textedit_delete(str, state, state->cursor-1, 1);
1012 | --state->cursor;
1013 | }
1014 | }
1015 | state->has_preferred_x = 0;
1016 | break;
1017 |
1018 | #ifdef STB_TEXTEDIT_K_TEXTSTART2
1019 | case STB_TEXTEDIT_K_TEXTSTART2:
1020 | #endif
1021 | case STB_TEXTEDIT_K_TEXTSTART:
1022 | state->cursor = state->select_start = state->select_end = 0;
1023 | state->has_preferred_x = 0;
1024 | break;
1025 |
1026 | #ifdef STB_TEXTEDIT_K_TEXTEND2
1027 | case STB_TEXTEDIT_K_TEXTEND2:
1028 | #endif
1029 | case STB_TEXTEDIT_K_TEXTEND:
1030 | state->cursor = STB_TEXTEDIT_STRINGLEN(str);
1031 | state->select_start = state->select_end = 0;
1032 | state->has_preferred_x = 0;
1033 | break;
1034 |
1035 | #ifdef STB_TEXTEDIT_K_TEXTSTART2
1036 | case STB_TEXTEDIT_K_TEXTSTART2 | STB_TEXTEDIT_K_SHIFT:
1037 | #endif
1038 | case STB_TEXTEDIT_K_TEXTSTART | STB_TEXTEDIT_K_SHIFT:
1039 | stb_textedit_prep_selection_at_cursor(state);
1040 | state->cursor = state->select_end = 0;
1041 | state->has_preferred_x = 0;
1042 | break;
1043 |
1044 | #ifdef STB_TEXTEDIT_K_TEXTEND2
1045 | case STB_TEXTEDIT_K_TEXTEND2 | STB_TEXTEDIT_K_SHIFT:
1046 | #endif
1047 | case STB_TEXTEDIT_K_TEXTEND | STB_TEXTEDIT_K_SHIFT:
1048 | stb_textedit_prep_selection_at_cursor(state);
1049 | state->cursor = state->select_end = STB_TEXTEDIT_STRINGLEN(str);
1050 | state->has_preferred_x = 0;
1051 | break;
1052 |
1053 |
1054 | #ifdef STB_TEXTEDIT_K_LINESTART2
1055 | case STB_TEXTEDIT_K_LINESTART2:
1056 | #endif
1057 | case STB_TEXTEDIT_K_LINESTART:
1058 | stb_textedit_clamp(str, state);
1059 | stb_textedit_move_to_first(state);
1060 | if (state->single_line)
1061 | state->cursor = 0;
1062 | else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE)
1063 | --state->cursor;
1064 | state->has_preferred_x = 0;
1065 | break;
1066 |
1067 | #ifdef STB_TEXTEDIT_K_LINEEND2
1068 | case STB_TEXTEDIT_K_LINEEND2:
1069 | #endif
1070 | case STB_TEXTEDIT_K_LINEEND: {
1071 | int n = STB_TEXTEDIT_STRINGLEN(str);
1072 | stb_textedit_clamp(str, state);
1073 | stb_textedit_move_to_first(state);
1074 | if (state->single_line)
1075 | state->cursor = n;
1076 | else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE)
1077 | ++state->cursor;
1078 | state->has_preferred_x = 0;
1079 | break;
1080 | }
1081 |
1082 | #ifdef STB_TEXTEDIT_K_LINESTART2
1083 | case STB_TEXTEDIT_K_LINESTART2 | STB_TEXTEDIT_K_SHIFT:
1084 | #endif
1085 | case STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_SHIFT:
1086 | stb_textedit_clamp(str, state);
1087 | stb_textedit_prep_selection_at_cursor(state);
1088 | if (state->single_line)
1089 | state->cursor = 0;
1090 | else while (state->cursor > 0 && STB_TEXTEDIT_GETCHAR(str, state->cursor-1) != STB_TEXTEDIT_NEWLINE)
1091 | --state->cursor;
1092 | state->select_end = state->cursor;
1093 | state->has_preferred_x = 0;
1094 | break;
1095 |
1096 | #ifdef STB_TEXTEDIT_K_LINEEND2
1097 | case STB_TEXTEDIT_K_LINEEND2 | STB_TEXTEDIT_K_SHIFT:
1098 | #endif
1099 | case STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_SHIFT: {
1100 | int n = STB_TEXTEDIT_STRINGLEN(str);
1101 | stb_textedit_clamp(str, state);
1102 | stb_textedit_prep_selection_at_cursor(state);
1103 | if (state->single_line)
1104 | state->cursor = n;
1105 | else while (state->cursor < n && STB_TEXTEDIT_GETCHAR(str, state->cursor) != STB_TEXTEDIT_NEWLINE)
1106 | ++state->cursor;
1107 | state->select_end = state->cursor;
1108 | state->has_preferred_x = 0;
1109 | break;
1110 | }
1111 | }
1112 | }
1113 |
1114 | /////////////////////////////////////////////////////////////////////////////
1115 | //
1116 | // Undo processing
1117 | //
1118 | // @OPTIMIZE: the undo/redo buffer should be circular
1119 |
1120 | static void stb_textedit_flush_redo(StbUndoState *state)
1121 | {
1122 | state->redo_point = STB_TEXTEDIT_UNDOSTATECOUNT;
1123 | state->redo_char_point = STB_TEXTEDIT_UNDOCHARCOUNT;
1124 | }
1125 |
1126 | // discard the oldest entry in the undo list
1127 | static void stb_textedit_discard_undo(StbUndoState *state)
1128 | {
1129 | if (state->undo_point > 0) {
1130 | // if the 0th undo state has characters, clean those up
1131 | if (state->undo_rec[0].char_storage >= 0) {
1132 | int n = state->undo_rec[0].insert_length, i;
1133 | // delete n characters from all other records
1134 | state->undo_char_point -= n;
1135 | STB_TEXTEDIT_memmove(state->undo_char, state->undo_char + n, (size_t) (state->undo_char_point*sizeof(STB_TEXTEDIT_CHARTYPE)));
1136 | for (i=0; i < state->undo_point; ++i)
1137 | if (state->undo_rec[i].char_storage >= 0)
1138 | state->undo_rec[i].char_storage -= n; // @OPTIMIZE: get rid of char_storage and infer it
1139 | }
1140 | --state->undo_point;
1141 | STB_TEXTEDIT_memmove(state->undo_rec, state->undo_rec+1, (size_t) (state->undo_point*sizeof(state->undo_rec[0])));
1142 | }
1143 | }
1144 |
1145 | // discard the oldest entry in the redo list--it's bad if this
1146 | // ever happens, but because undo & redo have to store the actual
1147 | // characters in different cases, the redo character buffer can
1148 | // fill up even though the undo buffer didn't
1149 | static void stb_textedit_discard_redo(StbUndoState *state)
1150 | {
1151 | int k = STB_TEXTEDIT_UNDOSTATECOUNT-1;
1152 |
1153 | if (state->redo_point <= k) {
1154 | // if the k'th undo state has characters, clean those up
1155 | if (state->undo_rec[k].char_storage >= 0) {
1156 | int n = state->undo_rec[k].insert_length, i;
1157 | // move the remaining redo character data to the end of the buffer
1158 | state->redo_char_point += n;
1159 | STB_TEXTEDIT_memmove(state->undo_char + state->redo_char_point, state->undo_char + state->redo_char_point-n, (size_t) ((STB_TEXTEDIT_UNDOCHARCOUNT - state->redo_char_point)*sizeof(STB_TEXTEDIT_CHARTYPE)));
1160 | // adjust the position of all the other records to account for above memmove
1161 | for (i=state->redo_point; i < k; ++i)
1162 | if (state->undo_rec[i].char_storage >= 0)
1163 | state->undo_rec[i].char_storage += n;
1164 | }
1165 | // now move all the redo records towards the end of the buffer; the first one is at 'redo_point'
1166 | // [DEAR IMGUI]
1167 | size_t move_size = (size_t)((STB_TEXTEDIT_UNDOSTATECOUNT - state->redo_point - 1) * sizeof(state->undo_rec[0]));
1168 | const char* buf_begin = (char*)state->undo_rec; (void)buf_begin;
1169 | const char* buf_end = (char*)state->undo_rec + sizeof(state->undo_rec); (void)buf_end;
1170 | IM_ASSERT(((char*)(state->undo_rec + state->redo_point)) >= buf_begin);
1171 | IM_ASSERT(((char*)(state->undo_rec + state->redo_point + 1) + move_size) <= buf_end);
1172 | STB_TEXTEDIT_memmove(state->undo_rec + state->redo_point+1, state->undo_rec + state->redo_point, move_size);
1173 |
1174 | // now move redo_point to point to the new one
1175 | ++state->redo_point;
1176 | }
1177 | }
1178 |
1179 | static StbUndoRecord *stb_text_create_undo_record(StbUndoState *state, int numchars)
1180 | {
1181 | // any time we create a new undo record, we discard redo
1182 | stb_textedit_flush_redo(state);
1183 |
1184 | // if we have no free records, we have to make room, by sliding the
1185 | // existing records down
1186 | if (state->undo_point == STB_TEXTEDIT_UNDOSTATECOUNT)
1187 | stb_textedit_discard_undo(state);
1188 |
1189 | // if the characters to store won't possibly fit in the buffer, we can't undo
1190 | if (numchars > STB_TEXTEDIT_UNDOCHARCOUNT) {
1191 | state->undo_point = 0;
1192 | state->undo_char_point = 0;
1193 | return NULL;
1194 | }
1195 |
1196 | // if we don't have enough free characters in the buffer, we have to make room
1197 | while (state->undo_char_point + numchars > STB_TEXTEDIT_UNDOCHARCOUNT)
1198 | stb_textedit_discard_undo(state);
1199 |
1200 | return &state->undo_rec[state->undo_point++];
1201 | }
1202 |
1203 | static STB_TEXTEDIT_CHARTYPE *stb_text_createundo(StbUndoState *state, int pos, int insert_len, int delete_len)
1204 | {
1205 | StbUndoRecord *r = stb_text_create_undo_record(state, insert_len);
1206 | if (r == NULL)
1207 | return NULL;
1208 |
1209 | r->where = pos;
1210 | r->insert_length = (STB_TEXTEDIT_POSITIONTYPE) insert_len;
1211 | r->delete_length = (STB_TEXTEDIT_POSITIONTYPE) delete_len;
1212 |
1213 | if (insert_len == 0) {
1214 | r->char_storage = -1;
1215 | return NULL;
1216 | } else {
1217 | r->char_storage = state->undo_char_point;
1218 | state->undo_char_point += insert_len;
1219 | return &state->undo_char[r->char_storage];
1220 | }
1221 | }
1222 |
1223 | static void stb_text_undo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)
1224 | {
1225 | StbUndoState *s = &state->undostate;
1226 | StbUndoRecord u, *r;
1227 | if (s->undo_point == 0)
1228 | return;
1229 |
1230 | // we need to do two things: apply the undo record, and create a redo record
1231 | u = s->undo_rec[s->undo_point-1];
1232 | r = &s->undo_rec[s->redo_point-1];
1233 | r->char_storage = -1;
1234 |
1235 | r->insert_length = u.delete_length;
1236 | r->delete_length = u.insert_length;
1237 | r->where = u.where;
1238 |
1239 | if (u.delete_length) {
1240 | // if the undo record says to delete characters, then the redo record will
1241 | // need to re-insert the characters that get deleted, so we need to store
1242 | // them.
1243 |
1244 | // there are three cases:
1245 | // there's enough room to store the characters
1246 | // characters stored for *redoing* don't leave room for redo
1247 | // characters stored for *undoing* don't leave room for redo
1248 | // if the last is true, we have to bail
1249 |
1250 | if (s->undo_char_point + u.delete_length >= STB_TEXTEDIT_UNDOCHARCOUNT) {
1251 | // the undo records take up too much character space; there's no space to store the redo characters
1252 | r->insert_length = 0;
1253 | } else {
1254 | int i;
1255 |
1256 | // there's definitely room to store the characters eventually
1257 | while (s->undo_char_point + u.delete_length > s->redo_char_point) {
1258 | // should never happen:
1259 | if (s->redo_point == STB_TEXTEDIT_UNDOSTATECOUNT)
1260 | return;
1261 | // there's currently not enough room, so discard a redo record
1262 | stb_textedit_discard_redo(s);
1263 | }
1264 | r = &s->undo_rec[s->redo_point-1];
1265 |
1266 | r->char_storage = s->redo_char_point - u.delete_length;
1267 | s->redo_char_point = s->redo_char_point - u.delete_length;
1268 |
1269 | // now save the characters
1270 | for (i=0; i < u.delete_length; ++i)
1271 | s->undo_char[r->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u.where + i);
1272 | }
1273 |
1274 | // now we can carry out the deletion
1275 | STB_TEXTEDIT_DELETECHARS(str, u.where, u.delete_length);
1276 | }
1277 |
1278 | // check type of recorded action:
1279 | if (u.insert_length) {
1280 | // easy case: was a deletion, so we need to insert n characters
1281 | STB_TEXTEDIT_INSERTCHARS(str, u.where, &s->undo_char[u.char_storage], u.insert_length);
1282 | s->undo_char_point -= u.insert_length;
1283 | }
1284 |
1285 | state->cursor = u.where + u.insert_length;
1286 |
1287 | s->undo_point--;
1288 | s->redo_point--;
1289 | }
1290 |
1291 | static void stb_text_redo(STB_TEXTEDIT_STRING *str, STB_TexteditState *state)
1292 | {
1293 | StbUndoState *s = &state->undostate;
1294 | StbUndoRecord *u, r;
1295 | if (s->redo_point == STB_TEXTEDIT_UNDOSTATECOUNT)
1296 | return;
1297 |
1298 | // we need to do two things: apply the redo record, and create an undo record
1299 | u = &s->undo_rec[s->undo_point];
1300 | r = s->undo_rec[s->redo_point];
1301 |
1302 | // we KNOW there must be room for the undo record, because the redo record
1303 | // was derived from an undo record
1304 |
1305 | u->delete_length = r.insert_length;
1306 | u->insert_length = r.delete_length;
1307 | u->where = r.where;
1308 | u->char_storage = -1;
1309 |
1310 | if (r.delete_length) {
1311 | // the redo record requires us to delete characters, so the undo record
1312 | // needs to store the characters
1313 |
1314 | if (s->undo_char_point + u->insert_length > s->redo_char_point) {
1315 | u->insert_length = 0;
1316 | u->delete_length = 0;
1317 | } else {
1318 | int i;
1319 | u->char_storage = s->undo_char_point;
1320 | s->undo_char_point = s->undo_char_point + u->insert_length;
1321 |
1322 | // now save the characters
1323 | for (i=0; i < u->insert_length; ++i)
1324 | s->undo_char[u->char_storage + i] = STB_TEXTEDIT_GETCHAR(str, u->where + i);
1325 | }
1326 |
1327 | STB_TEXTEDIT_DELETECHARS(str, r.where, r.delete_length);
1328 | }
1329 |
1330 | if (r.insert_length) {
1331 | // easy case: need to insert n characters
1332 | STB_TEXTEDIT_INSERTCHARS(str, r.where, &s->undo_char[r.char_storage], r.insert_length);
1333 | s->redo_char_point += r.insert_length;
1334 | }
1335 |
1336 | state->cursor = r.where + r.insert_length;
1337 |
1338 | s->undo_point++;
1339 | s->redo_point++;
1340 | }
1341 |
1342 | static void stb_text_makeundo_insert(STB_TexteditState *state, int where, int length)
1343 | {
1344 | stb_text_createundo(&state->undostate, where, 0, length);
1345 | }
1346 |
1347 | static void stb_text_makeundo_delete(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int length)
1348 | {
1349 | int i;
1350 | STB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, length, 0);
1351 | if (p) {
1352 | for (i=0; i < length; ++i)
1353 | p[i] = STB_TEXTEDIT_GETCHAR(str, where+i);
1354 | }
1355 | }
1356 |
1357 | static void stb_text_makeundo_replace(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, int where, int old_length, int new_length)
1358 | {
1359 | int i;
1360 | STB_TEXTEDIT_CHARTYPE *p = stb_text_createundo(&state->undostate, where, old_length, new_length);
1361 | if (p) {
1362 | for (i=0; i < old_length; ++i)
1363 | p[i] = STB_TEXTEDIT_GETCHAR(str, where+i);
1364 | }
1365 | }
1366 |
1367 | // reset the state to default
1368 | static void stb_textedit_clear_state(STB_TexteditState *state, int is_single_line)
1369 | {
1370 | state->undostate.undo_point = 0;
1371 | state->undostate.undo_char_point = 0;
1372 | state->undostate.redo_point = STB_TEXTEDIT_UNDOSTATECOUNT;
1373 | state->undostate.redo_char_point = STB_TEXTEDIT_UNDOCHARCOUNT;
1374 | state->select_end = state->select_start = 0;
1375 | state->cursor = 0;
1376 | state->has_preferred_x = 0;
1377 | state->preferred_x = 0;
1378 | state->cursor_at_end_of_line = 0;
1379 | state->initialized = 1;
1380 | state->single_line = (unsigned char) is_single_line;
1381 | state->insert_mode = 0;
1382 | state->row_count_per_page = 0;
1383 | }
1384 |
1385 | // API initialize
1386 | static void stb_textedit_initialize_state(STB_TexteditState *state, int is_single_line)
1387 | {
1388 | stb_textedit_clear_state(state, is_single_line);
1389 | }
1390 |
1391 | #if defined(__GNUC__) || defined(__clang__)
1392 | #pragma GCC diagnostic push
1393 | #pragma GCC diagnostic ignored "-Wcast-qual"
1394 | #endif
1395 |
1396 | static int stb_textedit_paste(STB_TEXTEDIT_STRING *str, STB_TexteditState *state, STB_TEXTEDIT_CHARTYPE const *ctext, int len)
1397 | {
1398 | return stb_textedit_paste_internal(str, state, (STB_TEXTEDIT_CHARTYPE *) ctext, len);
1399 | }
1400 |
1401 | #if defined(__GNUC__) || defined(__clang__)
1402 | #pragma GCC diagnostic pop
1403 | #endif
1404 |
1405 | #endif//STB_TEXTEDIT_IMPLEMENTATION
1406 |
1407 | /*
1408 | ------------------------------------------------------------------------------
1409 | This software is available under 2 licenses -- choose whichever you prefer.
1410 | ------------------------------------------------------------------------------
1411 | ALTERNATIVE A - MIT License
1412 | Copyright (c) 2017 Sean Barrett
1413 | Permission is hereby granted, free of charge, to any person obtaining a copy of
1414 | this software and associated documentation files (the "Software"), to deal in
1415 | the Software without restriction, including without limitation the rights to
1416 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
1417 | of the Software, and to permit persons to whom the Software is furnished to do
1418 | so, subject to the following conditions:
1419 | The above copyright notice and this permission notice shall be included in all
1420 | copies or substantial portions of the Software.
1421 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1422 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1423 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1424 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1425 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1426 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1427 | SOFTWARE.
1428 | ------------------------------------------------------------------------------
1429 | ALTERNATIVE B - Public Domain (www.unlicense.org)
1430 | This is free and unencumbered software released into the public domain.
1431 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
1432 | software, either in source code form or as a compiled binary, for any purpose,
1433 | commercial or non-commercial, and by any means.
1434 | In jurisdictions that recognize copyright laws, the author or authors of this
1435 | software dedicate any and all copyright interest in the software to the public
1436 | domain. We make this dedication for the benefit of the public at large and to
1437 | the detriment of our heirs and successors. We intend this dedication to be an
1438 | overt act of relinquishment in perpetuity of all present and future rights to
1439 | this software under copyright law.
1440 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1441 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1442 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1443 | AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
1444 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1445 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1446 | ------------------------------------------------------------------------------
1447 | */
1448 |
--------------------------------------------------------------------------------
/ImGuiDiscordWebHook/main.cpp:
--------------------------------------------------------------------------------
1 | #include "Main.h"
2 |
3 | #include
4 | #include
5 |
6 | #include
7 | #include
8 | #include
9 | #include
10 | #include
11 | #include
12 | #include
13 |
14 |
15 | #pragma comment(lib,"Wininet.lib")
16 |
17 | #pragma comment(lib, "winmm.lib")
18 |
19 | #define _WIN32_WINNT 0x0500
20 |
21 |
22 | using namespace std;
23 |
24 |
25 |
26 | const string cmd1p1 = "curl -i -H \"Accept: application/json\" -H \"Content-Type:application/json\" -X POST --data \"{\\\"content\\\": \\\"";
27 | const string cmd1p2 = "\\\"}\" ";
28 |
29 |
30 | using namespace std;
31 |
32 | void SendText(string text, string webhook_url) {
33 |
34 | string cmd = cmd1p1 + text + cmd1p2 + webhook_url;
35 |
36 | WinExec(cmd.c_str(), SW_HIDE);
37 |
38 | }
39 |
40 | int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
41 | {
42 | WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, LOADER_BRAND, NULL };
43 | RegisterClassEx(&wc);
44 | main_hwnd = CreateWindow(wc.lpszClassName, LOADER_BRAND, WS_POPUP, 0, 0, 5, 5, NULL, NULL, wc.hInstance, NULL);
45 |
46 |
47 | if (!CreateDeviceD3D(main_hwnd)) {
48 | CleanupDeviceD3D();
49 | UnregisterClass(wc.lpszClassName, wc.hInstance);
50 | return 1;
51 | }
52 |
53 |
54 | ShowWindow(main_hwnd, SW_HIDE);
55 | UpdateWindow(main_hwnd);
56 |
57 |
58 | ImGui::CreateContext();
59 |
60 | ImGuiIO& io = ImGui::GetIO();
61 | io.IniFilename = nullptr;
62 | io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
63 |
64 | ImGui::StyleColorsDark();
65 |
66 |
67 |
68 | ImGuiStyle& style = ImGui::GetStyle();
69 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
70 | {
71 | void Theme(); {
72 |
73 |
74 |
75 | ImGuiStyle* style = &ImGui::GetStyle();
76 | ImVec4* colors = style->Colors;
77 | colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
78 | colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f);
79 | colors[ImGuiCol_WindowBg] = ImVec4(0.10f, 0.10f, 0.10f, 0.50f);
80 | colors[ImGuiCol_ChildBg] = ImVec4(0.03f, 0.03f, 0.03f, 0.34f);
81 | colors[ImGuiCol_PopupBg] = ImVec4(0.19f, 0.19f, 0.19f, 0.92f);
82 | colors[ImGuiCol_Border] = ImVec4(0.19f, 0.19f, 0.19f, 0.29f);
83 | colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.24f);
84 | colors[ImGuiCol_FrameBg] = ImVec4(0.05f, 0.05f, 0.05f, 0.54f);
85 | colors[ImGuiCol_FrameBgHovered] = ImVec4(0.19f, 0.19f, 0.19f, 0.54f);
86 | colors[ImGuiCol_FrameBgActive] = ImVec4(0.20f, 0.22f, 0.23f, 1.00f);
87 | colors[ImGuiCol_TitleBg] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
88 | colors[ImGuiCol_TitleBgActive] = ImVec4(0.06f, 0.06f, 0.06f, 1.00f);
89 | colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
90 | colors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f);
91 | colors[ImGuiCol_ScrollbarBg] = ImVec4(0.05f, 0.05f, 0.05f, 0.54f);
92 | colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.34f, 0.34f, 0.34f, 0.54f);
93 | colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.40f, 0.40f, 0.40f, 0.54f);
94 | colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.56f, 0.56f, 0.56f, 0.54f);
95 | colors[ImGuiCol_CheckMark] = ImVec4(0.33f, 0.67f, 0.86f, 1.00f);
96 | colors[ImGuiCol_SliderGrab] = ImVec4(0.34f, 0.34f, 0.34f, 0.54f);
97 | colors[ImGuiCol_SliderGrabActive] = ImVec4(0.56f, 0.56f, 0.56f, 0.54f);
98 | colors[ImGuiCol_Button] = ImVec4(0.05f, 0.05f, 0.05f, 0.54f);
99 | colors[ImGuiCol_ButtonHovered] = ImVec4(0.19f, 0.19f, 0.19f, 0.54f);
100 | colors[ImGuiCol_ButtonActive] = ImVec4(0.20f, 0.22f, 0.23f, 1.00f);
101 | colors[ImGuiCol_Header] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
102 | colors[ImGuiCol_HeaderHovered] = ImVec4(0.00f, 0.00f, 0.00f, 0.36f);
103 | colors[ImGuiCol_HeaderActive] = ImVec4(0.20f, 0.22f, 0.23f, 0.33f);
104 | colors[ImGuiCol_Separator] = ImColor(77, 136, 247);
105 | colors[ImGuiCol_SeparatorHovered] = ImVec4(0.44f, 0.44f, 0.44f, 0.29f);
106 | colors[ImGuiCol_SeparatorActive] = ImVec4(0.40f, 0.44f, 0.47f, 1.00f);
107 | colors[ImGuiCol_ResizeGrip] = ImVec4(0.28f, 0.28f, 0.28f, 0.29f);
108 | colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.44f, 0.44f, 0.44f, 0.29f);
109 | colors[ImGuiCol_ResizeGripActive] = ImVec4(0.40f, 0.44f, 0.47f, 1.00f);
110 | colors[ImGuiCol_Tab] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
111 | colors[ImGuiCol_TabHovered] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f);
112 | colors[ImGuiCol_TabActive] = ImVec4(0.20f, 0.20f, 0.20f, 0.36f);
113 | colors[ImGuiCol_TabUnfocused] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
114 | colors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f);
115 | colors[ImGuiCol_PlotLines] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f);
116 | colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f);
117 | colors[ImGuiCol_PlotHistogram] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f);
118 | colors[ImGuiCol_PlotHistogramHovered] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f);
119 | colors[ImGuiCol_TextSelectedBg] = ImVec4(0.20f, 0.22f, 0.23f, 1.00f);
120 | colors[ImGuiCol_DragDropTarget] = ImVec4(0.33f, 0.67f, 0.86f, 1.00f);
121 | colors[ImGuiCol_NavHighlight] = ImVec4(0.10f, 0.10f, 0.10f, 0.80f);
122 | colors[ImGuiCol_NavWindowingHighlight] = ImVec4(0.10f, 0.10f, 0.10f, 0.80f);
123 | colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.10f, 0.10f, 0.10f, 0.80f);
124 | colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.10f, 0.10f, 0.10f, 0.80f);
125 |
126 | style->ItemSpacing = ImVec2(10, 2);
127 | style->IndentSpacing = 12;
128 | style->ScrollbarSize = 10;
129 |
130 | style->WindowRounding = 3;
131 | style->FrameRounding = 3;
132 | style->PopupRounding = 3;
133 | style->ScrollbarRounding = 3;
134 | style->GrabRounding = 3;
135 | style->TabRounding = 3;
136 |
137 | style->WindowBorderSize = 0;
138 | style->WindowTitleAlign = ImVec2(0.5, 0.5);
139 | style->WindowMinSize = ImVec2(900, 430);
140 |
141 | style->FramePadding = ImVec2(8, 6);
142 |
143 | }
144 | }
145 |
146 |
147 | ImGui_ImplWin32_Init(main_hwnd);
148 | ImGui_ImplDX9_Init(g_pd3dDevice);
149 |
150 |
151 |
152 | DWORD window_flags = ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar;
153 |
154 | RECT screen_rect;
155 | GetWindowRect(GetDesktopWindow(), &screen_rect);
156 | auto x = float(screen_rect.right - WINDOW_WIDTH) / 2.f;
157 | auto y = float(screen_rect.bottom - WINDOW_HEIGHT) / 2.f;
158 |
159 | MSG msg;
160 | ZeroMemory(&msg, sizeof(msg));
161 |
162 | char message[500] = "";
163 | char webhook[500] = "";
164 |
165 | while (msg.message != WM_QUIT)
166 | {
167 | if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
168 | {
169 | TranslateMessage(&msg);
170 | DispatchMessage(&msg);
171 | continue;
172 | }
173 |
174 |
175 |
176 |
177 |
178 | ImGui_ImplDX9_NewFrame();
179 | ImGui_ImplWin32_NewFrame();
180 | ImGui::NewFrame();
181 | {
182 | ImGui::SetNextWindowPos(ImVec2(x, y), ImGuiCond_Once);
183 | ImGui::SetNextWindowSize(ImVec2(WINDOW_WIDTH, WINDOW_HEIGHT));
184 | ImGui::SetNextWindowBgAlpha(1.0f);
185 |
186 | ImGui::Begin(LOADER_BRAND, &loader_active, window_flags);
187 | {
188 |
189 | ImGui::Text(" ");
190 | ImGui::Text(" ");
191 | ImGui::Text(" ");
192 | ImGui::Text(" ");
193 | ImGui::Separator();
194 | ImGui::Text(" ");
195 | ImGui::Text(" ");
196 | ImGui::SameLine(20);
197 | ImGui::PushItemWidth(790);
198 | ImGui::InputText(("Message"), message, IM_ARRAYSIZE(message));
199 | ImGui::Text(" ");
200 | ImGui::SameLine(20);
201 | ImGui::PushItemWidth(790);
202 | ImGui::InputText(("Webhook"), webhook, IM_ARRAYSIZE(webhook));
203 | ImGui::Text(" ");
204 | ImGui::Separator();
205 | ImGui::Text(" ");
206 | ImGui::SetCursorPos({ 140.f,200.f });
207 | if (ImGui::Button((" Send ")))
208 | {
209 | SendText((message), webhook);
210 | }
211 | ImGui::Text(" ");
212 | ImGui::Text(" ");
213 | ImGui::Separator();
214 | ImGui::TextColored(ImVec4(0.2f, 1.0f, 0.0f, 1.0f), "How to use?");
215 | ImGui::SameLine();
216 | ImGui::TextColored(ImVec4(0.18f, 0.18f, 0.18f, 1.0f), "Developed by Shoven#5688");
217 | ImGui::Text("# Enter your discord webhook address and specify a message and press send");
218 | }
219 | ImGui::End();
220 | }
221 | ImGui::EndFrame();
222 |
223 | g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0, 1.0f, 0);
224 | if (g_pd3dDevice->BeginScene() >= 0)
225 | {
226 | ImGui::Render();
227 | ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
228 | g_pd3dDevice->EndScene();
229 | }
230 |
231 |
232 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
233 | {
234 | ImGui::UpdatePlatformWindows();
235 | ImGui::RenderPlatformWindowsDefault();
236 | }
237 |
238 | HRESULT result = g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
239 |
240 |
241 | if (result == D3DERR_DEVICELOST && g_pd3dDevice->TestCooperativeLevel() == D3DERR_DEVICENOTRESET) {
242 | ResetDevice();
243 | }
244 | if (!loader_active) {
245 | msg.message = WM_QUIT;
246 | }
247 | }
248 |
249 | ImGui_ImplDX9_Shutdown();
250 | ImGui_ImplWin32_Shutdown();
251 | ImGui::DestroyContext();
252 |
253 | CleanupDeviceD3D();
254 | DestroyWindow(main_hwnd);
255 | UnregisterClass(wc.lpszClassName, wc.hInstance);
256 |
257 | return 0;
258 | }
259 |
260 | LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
261 | if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
262 | return true;
263 |
264 | switch (msg)
265 | {
266 | case WM_SIZE:
267 | if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED)
268 | {
269 | g_d3dpp.BackBufferWidth = LOWORD(lParam);
270 | g_d3dpp.BackBufferHeight = HIWORD(lParam);
271 | ResetDevice();
272 | }
273 | return 0;
274 | case WM_SYSCOMMAND:
275 | if ((wParam & 0xfff0) == SC_KEYMENU)
276 | return 0;
277 | break;
278 | case WM_DESTROY:
279 | PostQuitMessage(0);
280 | return 0;
281 | }
282 | return DefWindowProc(hWnd, msg, wParam, lParam);
283 | }
284 |
--------------------------------------------------------------------------------
/ImGuiDiscordWebHook/main.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 | #include
4 | #include
5 |
6 | #include
7 | #pragma comment(lib,"d3d9.lib")
8 |
9 | #include "imgui/imgui.h"
10 | #include "imgui/imgui_impl_dx9.h"
11 | #include "imgui/imgui_impl_win32.h"
12 |
13 | #define LOADER_BRAND "ImGui Discord Webhook"
14 | #define WINDOW_WIDTH 400
15 | #define WINDOW_HEIGHT 200
16 |
17 | bool loader_active = true;
18 | HWND main_hwnd = nullptr;
19 |
20 | LPDIRECT3DDEVICE9 g_pd3dDevice;
21 | D3DPRESENT_PARAMETERS g_d3dpp;
22 | LPDIRECT3D9 g_pD3D;
23 |
24 | extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
25 | LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
26 |
27 | bool CreateDeviceD3D(HWND hWnd)
28 | {
29 | if ((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
30 | return false;
31 |
32 | // Create the D3DDevice
33 | ZeroMemory(&g_d3dpp, sizeof(g_d3dpp));
34 | g_d3dpp.Windowed = TRUE;
35 | g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
36 | g_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
37 | g_d3dpp.EnableAutoDepthStencil = TRUE;
38 | g_d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
39 | g_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
40 |
41 | if (g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &g_d3dpp, &g_pd3dDevice) < 0)
42 | return false;
43 | return true;
44 | }
45 |
46 | void CleanupDeviceD3D()
47 | {
48 | if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; }
49 | if (g_pD3D) { g_pD3D->Release(); g_pD3D = NULL; }
50 |
51 | }
52 |
53 | void ResetDevice()
54 | {
55 | ImGui_ImplDX9_InvalidateDeviceObjects();
56 | HRESULT hr = g_pd3dDevice->Reset(&g_d3dpp);
57 | if (hr == D3DERR_INVALIDCALL)
58 | IM_ASSERT(0);
59 | ImGui_ImplDX9_CreateDeviceObjects();
60 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ImGuiDiscordWebHook
2 | Allows you to send messages via Discord webhook
3 | 
4 |
--------------------------------------------------------------------------------