axmol/extensions/ImGuiEXT/imgui/misc/cpp/imgui_stdlib.cpp

94 lines
3.6 KiB
C++
Raw Normal View History

2020-09-04 17:19:51 +08:00
// dear imgui: wrappers for C++ standard library (STL) types (std::string, etc.)
// This is also an example of how you may wrap your own similar types.
// Compatibility:
// - std::string support is only guaranteed to work from C++11.
// If you try to use it pre-C++11, please share your findings (w/ info about compiler/architecture)
// Changelog:
// - v0.10: Initial version. Added InputText() / InputTextMultiline() calls with std::string
#include "imgui.h"
#include "imgui_stdlib.h"
struct InputTextCallback_UserData
{
2021-12-25 10:04:45 +08:00
std::string* Str;
ImGuiInputTextCallback ChainCallback;
void* ChainCallbackUserData;
2020-09-04 17:19:51 +08:00
};
static int InputTextCallback(ImGuiInputTextCallbackData* data)
{
InputTextCallback_UserData* user_data = (InputTextCallback_UserData*)data->UserData;
if (data->EventFlag == ImGuiInputTextFlags_CallbackResize)
{
// Resize string callback
2021-12-25 10:04:45 +08:00
// If for some reason we refuse the new length (BufTextLen) and/or capacity (BufSize) we need to set them back
// to what we want.
2020-09-04 17:19:51 +08:00
std::string* str = user_data->Str;
IM_ASSERT(data->Buf == str->c_str());
str->resize(data->BufTextLen);
data->Buf = (char*)str->c_str();
}
else if (user_data->ChainCallback)
{
// Forward to user callback, if any
data->UserData = user_data->ChainCallbackUserData;
return user_data->ChainCallback(data);
}
return 0;
}
2021-12-25 10:04:45 +08:00
bool ImGui::InputText(const char* label,
std::string* str,
ImGuiInputTextFlags flags,
ImGuiInputTextCallback callback,
void* user_data)
2020-09-04 17:19:51 +08:00
{
IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
flags |= ImGuiInputTextFlags_CallbackResize;
InputTextCallback_UserData cb_user_data;
2021-12-25 10:04:45 +08:00
cb_user_data.Str = str;
cb_user_data.ChainCallback = callback;
2020-09-04 17:19:51 +08:00
cb_user_data.ChainCallbackUserData = user_data;
return InputText(label, (char*)str->c_str(), str->capacity() + 1, flags, InputTextCallback, &cb_user_data);
}
2021-12-25 10:04:45 +08:00
bool ImGui::InputTextMultiline(const char* label,
std::string* str,
const ImVec2& size,
ImGuiInputTextFlags flags,
ImGuiInputTextCallback callback,
void* user_data)
2020-09-04 17:19:51 +08:00
{
IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
flags |= ImGuiInputTextFlags_CallbackResize;
InputTextCallback_UserData cb_user_data;
2021-12-25 10:04:45 +08:00
cb_user_data.Str = str;
cb_user_data.ChainCallback = callback;
2020-09-04 17:19:51 +08:00
cb_user_data.ChainCallbackUserData = user_data;
2021-12-25 10:04:45 +08:00
return InputTextMultiline(label, (char*)str->c_str(), str->capacity() + 1, size, flags, InputTextCallback,
&cb_user_data);
2020-09-04 17:19:51 +08:00
}
2021-12-25 10:04:45 +08:00
bool ImGui::InputTextWithHint(const char* label,
const char* hint,
std::string* str,
ImGuiInputTextFlags flags,
ImGuiInputTextCallback callback,
void* user_data)
2020-09-04 17:19:51 +08:00
{
IM_ASSERT((flags & ImGuiInputTextFlags_CallbackResize) == 0);
flags |= ImGuiInputTextFlags_CallbackResize;
InputTextCallback_UserData cb_user_data;
2021-12-25 10:04:45 +08:00
cb_user_data.Str = str;
cb_user_data.ChainCallback = callback;
2020-09-04 17:19:51 +08:00
cb_user_data.ChainCallbackUserData = user_data;
2021-12-25 10:04:45 +08:00
return InputTextWithHint(label, hint, (char*)str->c_str(), str->capacity() + 1, flags, InputTextCallback,
&cb_user_data);
2020-09-04 17:19:51 +08:00
}