Skip to content

Commit 2303423

Browse files
merlinNDnmwsharp
andauthored
ImGui bindings: fix optional strings (#64)
* ImGui: fix optional `const char *` arguments * ManagedBuffer: import missing type caster For the return value of `get_texture_size()`. --------- Co-authored-by: Nicholas Sharp <nmwsharp@gmail.com>
1 parent 155a632 commit 2303423

9 files changed

Lines changed: 59 additions & 32 deletions

src/cpp/imgui/imgui_api_columns_legacy.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "imgui_utils.h"
88

99
#include <nanobind/stl/array.h>
10+
#include <nanobind/stl/optional.h>
11+
#include <nanobind/stl/string.h>
1012

1113
// clang-format off
1214
void bind_imgui_api_columns_legacy(nb::module_& m) {
@@ -15,7 +17,9 @@ void bind_imgui_api_columns_legacy(nb::module_& m) {
1517
// IMGUI_API void Columns(int count = 1, const char* id = NULL, bool borders = true);
1618
m.def(
1719
"Columns",
18-
[](int count, const char* id, bool borders) { ImGui::Columns(count, id, borders); },
20+
[](int count, std::optional<std::string> id, bool borders) {
21+
ImGui::Columns(count, to_char_ptr(id), borders);
22+
},
1923
nb::arg("count") = 1,
2024
nb::arg("id") = nb::none(),
2125
nb::arg("borders") = true);

src/cpp/imgui/imgui_api_data_plotting.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <nanobind/stl/array.h>
1010
#include <nanobind/eigen/dense.h>
11+
#include <nanobind/stl/optional.h>
12+
#include <nanobind/stl/string.h>
1113

1214
// clang-format off
1315
void bind_imgui_api_data_plotting(nb::module_& m) {
@@ -16,33 +18,39 @@ void bind_imgui_api_data_plotting(nb::module_& m) {
1618
// IMGUI_API void PlotLines(const char* label, const float* values, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0), int stride = sizeof(float));
1719
m.def(
1820
"PlotLines",
19-
[](const char* label, const Eigen::Ref<const Eigen::VectorXf>& values, int values_offset, const char* overlay_text, float scale_min, float scale_max, const Vec2T& graph_size) {
20-
ImGui::PlotLines(label, values.data(), static_cast<int>(values.size()), values_offset, overlay_text, scale_min, scale_max, to_vec2(graph_size));
21+
[](const char* label, const Eigen::Ref<const Eigen::VectorXf>& values, int values_offset,
22+
std::optional<std::string> overlay_text, float scale_min, float scale_max, const Vec2T& graph_size) {
23+
ImGui::PlotLines(label, values.data(), static_cast<int>(values.size()),
24+
values_offset, to_char_ptr(overlay_text), scale_min, scale_max, to_vec2(graph_size));
2125
},
2226
nb::arg("label"),
2327
nb::arg("values"),
2428
nb::arg("values_offset") = 0,
2529
nb::arg("overlay_text") = nb::none(),
2630
nb::arg("scale_min") = FLT_MAX,
2731
nb::arg("scale_max") = FLT_MAX,
28-
nb::arg("graph_size") = Vec2T(0.f, 0.f));
32+
nb::arg("graph_size") = Vec2T(0.f, 0.f)
33+
);
2934

3035
// IMGUI_API void PlotLines(const char* label, float(*values_getter)(void* data, int idx), void* data, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0));
3136
// TODO: Callback version not bound
3237

3338
// IMGUI_API void PlotHistogram(const char* label, const float* values, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0), int stride = sizeof(float));
3439
m.def(
3540
"PlotHistogram",
36-
[](const char* label, const Eigen::Ref<const Eigen::VectorXf>& values, int values_offset, const char* overlay_text, float scale_min, float scale_max, const Vec2T& graph_size) {
37-
ImGui::PlotHistogram(label, values.data(), static_cast<int>(values.size()), values_offset, overlay_text, scale_min, scale_max, to_vec2(graph_size));
41+
[](const char* label, const Eigen::Ref<const Eigen::VectorXf>& values, int values_offset,
42+
std::optional<std::string> overlay_text, float scale_min, float scale_max, const Vec2T& graph_size) {
43+
ImGui::PlotHistogram(label, values.data(), static_cast<int>(values.size()),
44+
values_offset, to_char_ptr(overlay_text), scale_min, scale_max, to_vec2(graph_size));
3845
},
3946
nb::arg("label"),
4047
nb::arg("values"),
4148
nb::arg("values_offset") = 0,
4249
nb::arg("overlay_text") = nb::none(),
4350
nb::arg("scale_min") = FLT_MAX,
4451
nb::arg("scale_max") = FLT_MAX,
45-
nb::arg("graph_size") = Vec2T(0.f, 0.f));
52+
nb::arg("graph_size") = Vec2T(0.f, 0.f)
53+
);
4654

4755
// IMGUI_API void PlotHistogram(const char* label, float (*values_getter)(void* data, int idx), void* data, int values_count, int values_offset = 0, const char* overlay_text = NULL, float scale_min = FLT_MAX, float scale_max = FLT_MAX, ImVec2 graph_size = ImVec2(0, 0));
4856
// TODO: Callback version not bound
@@ -71,7 +79,7 @@ void bind_imgui_api_data_plotting(nb::module_& m) {
7179
// IMGUI_API void Value(const char* prefix, float v, const char* float_format = NULL);
7280
m.def(
7381
"Value",
74-
[](const char* prefix, float v, const char* float_format) { ImGui::Value(prefix, v, float_format); },
82+
[](const char* prefix, float v, std::optional<std::string> float_format) { ImGui::Value(prefix, v, to_char_ptr(float_format)); },
7583
nb::arg("prefix"),
7684
nb::arg("v"),
7785
nb::arg("float_format") = nb::none());

src/cpp/imgui/imgui_api_logging.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "imgui_utils.h"
88

99
#include <nanobind/stl/array.h>
10+
#include <nanobind/stl/optional.h>
11+
#include <nanobind/stl/string.h>
1012

1113
// clang-format off
1214
void bind_imgui_api_logging(nb::module_& m) {
@@ -21,7 +23,9 @@ void bind_imgui_api_logging(nb::module_& m) {
2123
// IMGUI_API void LogToFile(int auto_open_depth = -1, const char* filename = NULL);
2224
m.def(
2325
"LogToFile",
24-
[](int auto_open_depth, const char* filename) { ImGui::LogToFile(auto_open_depth, filename); },
26+
[](int auto_open_depth, std::optional<std::string> filename) {
27+
ImGui::LogToFile(auto_open_depth, to_char_ptr(filename));
28+
},
2529
nb::arg("auto_open_depth") = -1,
2630
nb::arg("filename") = nb::none());
2731

src/cpp/imgui/imgui_api_menus.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "imgui_utils.h"
88

99
#include <nanobind/stl/array.h>
10+
#include <nanobind/stl/optional.h>
11+
#include <nanobind/stl/string.h>
1012

1113
// clang-format off
1214
void bind_imgui_api_menus(nb::module_& m) {
@@ -37,8 +39,8 @@ void bind_imgui_api_menus(nb::module_& m) {
3739
// IMGUI_API bool MenuItem(const char* label, const char* shortcut = NULL, bool selected = false, bool enabled = true);
3840
m.def(
3941
"MenuItem",
40-
[](const char* label, const char* shortcut, bool selected, bool enabled) {
41-
return ImGui::MenuItem(label, shortcut, selected, enabled);
42+
[](const char* label, std::optional<std::string> shortcut, bool selected, bool enabled) {
43+
return ImGui::MenuItem(label, to_char_ptr(shortcut), selected, enabled);
4244
},
4345
nb::arg("label"),
4446
nb::arg("shortcut") = nb::none(),

src/cpp/imgui/imgui_api_popups.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "imgui_utils.h"
88

99
#include <nanobind/stl/array.h>
10+
#include <nanobind/stl/optional.h>
11+
#include <nanobind/stl/string.h>
1012

1113
// clang-format off
1214
void bind_imgui_api_popups(nb::module_& m) {
@@ -59,7 +61,7 @@ void bind_imgui_api_popups(nb::module_& m) {
5961
// IMGUI_API void OpenPopupOnItemClick(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1);
6062
m.def(
6163
"OpenPopupOnItemClick",
62-
[](const char* str_id, ImGuiPopupFlags popup_flags) { ImGui::OpenPopupOnItemClick(str_id, popup_flags); },
64+
[](std::optional<std::string> str_id, ImGuiPopupFlags popup_flags) { ImGui::OpenPopupOnItemClick(to_char_ptr(str_id), popup_flags); },
6365
nb::arg("str_id") = nb::none(),
6466
nb::arg("popup_flags") = 1);
6567

@@ -70,26 +72,26 @@ void bind_imgui_api_popups(nb::module_& m) {
7072
// IMGUI_API bool BeginPopupContextItem(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1);
7173
m.def(
7274
"BeginPopupContextItem",
73-
[](const char* str_id, ImGuiPopupFlags popup_flags) {
74-
return ImGui::BeginPopupContextItem(str_id, popup_flags);
75+
[](std::optional<std::string> str_id, ImGuiPopupFlags popup_flags) {
76+
return ImGui::BeginPopupContextItem(to_char_ptr(str_id), popup_flags);
7577
},
7678
nb::arg("str_id") = nb::none(),
7779
nb::arg("popup_flags") = 1);
7880

7981
// IMGUI_API bool BeginPopupContextWindow(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1);
8082
m.def(
8183
"BeginPopupContextWindow",
82-
[](const char* str_id, ImGuiPopupFlags popup_flags) {
83-
return ImGui::BeginPopupContextWindow(str_id, popup_flags);
84+
[](std::optional<std::string> str_id, ImGuiPopupFlags popup_flags) {
85+
return ImGui::BeginPopupContextWindow(to_char_ptr(str_id), popup_flags);
8486
},
8587
nb::arg("str_id") = nb::none(),
8688
nb::arg("popup_flags") = 1);
8789

8890
// IMGUI_API bool BeginPopupContextVoid(const char* str_id = NULL, ImGuiPopupFlags popup_flags = 1);
8991
m.def(
9092
"BeginPopupContextVoid",
91-
[](const char* str_id, ImGuiPopupFlags popup_flags) {
92-
return ImGui::BeginPopupContextVoid(str_id, popup_flags);
93+
[](std::optional<std::string> str_id, ImGuiPopupFlags popup_flags) {
94+
return ImGui::BeginPopupContextVoid(to_char_ptr(str_id), popup_flags);
9395
},
9496
nb::arg("str_id") = nb::none(),
9597
nb::arg("popup_flags") = 1);

src/cpp/imgui/imgui_api_widgets_drag.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "imgui_utils.h"
88

99
#include <nanobind/stl/array.h>
10+
#include <nanobind/stl/optional.h>
11+
#include <nanobind/stl/string.h>
1012

1113
// clang-format off
1214
void bind_imgui_api_widgets_drag(nb::module_& m) {
@@ -76,11 +78,7 @@ void bind_imgui_api_widgets_drag(nb::module_& m) {
7678
m.def(
7779
"DragFloatRange2",
7880
[](const char* label, float v_current_min, float v_current_max, float v_speed, float v_min, float v_max, std::string format, std::optional<std::string> format_max, ImGuiSliderFlags flags) {
79-
const char* format_max_ptr = nullptr;
80-
if(format_max.has_value()) {
81-
format_max_ptr = format_max->c_str();
82-
}
83-
bool result = ImGui::DragFloatRange2(label, &v_current_min, &v_current_max, v_speed, v_min, v_max, format.c_str(), format_max_ptr, flags);
81+
bool result = ImGui::DragFloatRange2(label, &v_current_min, &v_current_max, v_speed, v_min, v_max, format.c_str(), to_char_ptr(format_max), flags);
8482
return std::make_tuple(result, v_current_min, v_current_max);
8583
},
8684
nb::arg("label"),
@@ -156,8 +154,8 @@ void bind_imgui_api_widgets_drag(nb::module_& m) {
156154
// IMGUI_API bool DragIntRange2(const char* label, int* v_current_min, int* v_current_max, float v_speed = 1.0f, int v_min = 0, int v_max = 0, const char* format = "%d", const char* format_max = NULL, ImGuiSliderFlags flags = 0);
157155
m.def(
158156
"DragIntRange2",
159-
[](const char* label, int v_current_min, int v_current_max, float v_speed, int v_min, int v_max, const char* format, const char* format_max, ImGuiSliderFlags flags) {
160-
bool result = ImGui::DragIntRange2(label, &v_current_min, &v_current_max, v_speed, v_min, v_max, format, format_max, flags);
157+
[](const char* label, int v_current_min, int v_current_max, float v_speed, int v_min, int v_max, const char* format, std::optional<std::string> format_max, ImGuiSliderFlags flags) {
158+
bool result = ImGui::DragIntRange2(label, &v_current_min, &v_current_max, v_speed, v_min, v_max, format, to_char_ptr(format_max), flags);
161159
return std::make_tuple(result, v_current_min, v_current_max);
162160
},
163161
nb::arg("label"),

src/cpp/imgui/imgui_api_widgets_main.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ void bind_imgui_api_widgets_main(nb::module_& m) {
9595
// IMGUI_API void ProgressBar(float fraction, const ImVec2& size_arg = ImVec2(-FLT_MIN, 0), const char* overlay = NULL);
9696
m.def(
9797
"ProgressBar",
98-
[](float fraction, const Vec2T& size_arg, std::string overlay) {
99-
ImGui::ProgressBar(fraction, to_vec2(size_arg), overlay.empty() ? nullptr : overlay.c_str());
98+
[](float fraction, const Vec2T& size_arg, std::optional<std::string> overlay) {
99+
ImGui::ProgressBar(fraction, to_vec2(size_arg), to_char_ptr(overlay));
100100
},
101101
nb::arg("fraction"),
102102
nb::arg("size_arg") = Vec2T(-FLT_MIN, 0.f),
103-
nb::arg("overlay") = "");
103+
nb::arg("overlay") = nb::none());
104104

105105
// IMGUI_API void Bullet();
106106
m.def("Bullet", []() { ImGui::Bullet(); });
@@ -114,11 +114,11 @@ void bind_imgui_api_widgets_main(nb::module_& m) {
114114
// IMGUI_API void TextLinkOpenURL(const char* label, const char* url = NULL);
115115
m.def(
116116
"TextLinkOpenURL",
117-
[](std::string label, std::string url) {
118-
return ImGui::TextLinkOpenURL(label.c_str(), url.empty() ? nullptr : url.c_str());
117+
[](std::string label, std::optional<std::string> url) {
118+
return ImGui::TextLinkOpenURL(label.c_str(), to_char_ptr(url));
119119
},
120120
nb::arg("label"),
121-
nb::arg("url") = "");
121+
nb::arg("url") = nb::none());
122122

123123
}
124124
// clang-format on

src/cpp/imgui/imgui_utils.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,11 @@ inline std::vector<const char*> convert_string_items(const std::vector<std::stri
4848
_items.push_back(item.data());
4949
}
5050
return _items;
51-
}
51+
}
52+
53+
inline const char *to_char_ptr(const std::optional<std::string> &s) {
54+
if (s.has_value()) {
55+
return s->c_str();
56+
}
57+
return nullptr;
58+
}

src/cpp/managed_buffer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include "utils.h"
99

10+
#include <nanobind/stl/array.h>
11+
1012
template <typename T>
1113
nb::class_<ps::render::ManagedBuffer<T>> bind_managed_buffer_T(nb::module_& m, ps::ManagedBufferType t) {
1214

0 commit comments

Comments
 (0)