master
ブランチ master
ShaderPlayground / deps / imgui / imgui / misc / cpp / imgui_stdlib.cpp
3.8 KB、100行 生 Blame 履歴
1// dear imgui: wrappers for C++ standard library (STL) types (std::string, etc.)
2
3// This is also an example of how you may wrap your own similar types.
4// TL;DR; this is using the ImGuiInputTextFlags_CallbackResize facility,
5// which also demonstrated in 'Dear ImGui Demo->Widgets->Text Input->Resize Callback'.
6
7// Changelog:
8// - v0.10: Initial version. Added InputText() / InputTextMultiline() calls with std::string
9
10// Usage:
11// {
12// #include "misc/cpp/imgui_stdlib.h"
13// #include "misc/cpp/imgui_stdlib.cpp" // <-- If you want to include implementation without messing with your project/build.
14// [...]
15// std::string my_string;
16// ImGui::InputText("my string", &my_string);
17// }
18
19// See more C++ related extension (fmt, RAII, syntactic sugar) on Wiki:
20// https://github.com/ocornut/imgui/wiki/Useful-Extensions#cness
21
22#include "imgui.h"
23#ifndef IMGUI_DISABLE
24#include "imgui_stdlib.h"
25
26// Clang warnings with -Weverything
27#if defined(__clang__)
28#pragma clang diagnostic push
29#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
30#endif
31
32struct InputTextCallback_UserData
33{
34 std::string* Str;
35 ImGuiInputTextCallback ChainCallback;
36 void* ChainCallbackUserData;
37};
38
39static int InputTextCallback(ImGuiInputTextCallbackData* data)
40{
41 InputTextCallback_UserData* user_data = (InputTextCallback_UserData*)data->UserData;
42 if (data->EventFlag == ImGuiInputTextFlags_CallbackResize)
43 {
44 // Resize string callback
45 // If for some reason we refuse the new length (BufTextLen) and/or capacity (BufSize) we need to set them back to what we want.
46 std::string* str = user_data->Str;
47 IM_ASSERT(data->Buf == str->c_str());
48 str->resize(data->BufTextLen);
49 data->Buf = (char*)str->c_str();
50 }
51 else if (user_data->ChainCallback)
52 {
53 // Forward to user callback, if any
54 data->UserData = user_data->ChainCallbackUserData;
55 return user_data->ChainCallback(data);
56 }
57 return 0;
58}
59
60bool ImGui::InputText(const char* label, std::string* str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
61{
62 IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
63 flags |= ImGuiInputTextFlags_CallbackResize;
64
65 InputTextCallback_UserData cb_user_data;
66 cb_user_data.Str = str;
67 cb_user_data.ChainCallback = callback;
68 cb_user_data.ChainCallbackUserData = user_data;
69 return InputText(label, (char*)str->c_str(), str->capacity() + 1, flags, InputTextCallback, &cb_user_data);
70}
71
72bool ImGui::InputTextMultiline(const char* label, std::string* str, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
73{
74 IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
75 flags |= ImGuiInputTextFlags_CallbackResize;
76
77 InputTextCallback_UserData cb_user_data;
78 cb_user_data.Str = str;
79 cb_user_data.ChainCallback = callback;
80 cb_user_data.ChainCallbackUserData = user_data;
81 return InputTextMultiline(label, (char*)str->c_str(), str->capacity() + 1, size, flags, InputTextCallback, &cb_user_data);
82}
83
84bool ImGui::InputTextWithHint(const char* label, const char* hint, std::string* str, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data)
85{
86 IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
87 flags |= ImGuiInputTextFlags_CallbackResize;
88
89 InputTextCallback_UserData cb_user_data;
90 cb_user_data.Str = str;
91 cb_user_data.ChainCallback = callback;
92 cb_user_data.ChainCallbackUserData = user_data;
93 return InputTextWithHint(label, hint, (char*)str->c_str(), str->capacity() + 1, flags, InputTextCallback, &cb_user_data);
94}
95
96#if defined(__clang__)
97#pragma clang diagnostic pop
98#endif
99
100#endif // #ifndef IMGUI_DISABLE