Skip to content

Commit

Permalink
imgui bindings: ad to/from_json to ImVec2, ImVec4, ImColor
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed May 12, 2024
1 parent dad5bd1 commit 09b985a
Showing 1 changed file with 55 additions and 1 deletion.
56 changes: 55 additions & 1 deletion external/imgui/bindings/pybind_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6499,7 +6499,15 @@ void py_init_module_imgui_main(py::module& m)
// </litgen_pydef> // Autogenerated code end
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! AUTOGENERATED CODE END !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

// ****************************************************************************************************************
//
// MANUAL PATCHES BELOW
//
// ****************************************************************************************************************

//
// Patches to ImVec2
//
pyClassImVec2.def("__str__", [](const ImVec2& self) -> std::string {
char r[100];
snprintf(r, 100, "ImVec2(%f, %f)", self.x, self.y);
Expand Down Expand Up @@ -6536,7 +6544,20 @@ void py_init_module_imgui_main(py::module& m)
return ImVec2(imv.x, imv.y);
}), py::arg("xy"));

// Json serialization for ImVec2
pyClassImVec2.def("to_json", [](const ImVec2& self) {
py::dict d;
d["x"] = self.x;
d["y"] = self.y;
return d;
});
pyClassImVec2.def_static("from_json", [](py::dict data) {
return ImVec2(data["x"].cast<float>(), data["y"].cast<float>());
});

//
// Patches to ImVec4
//
pyClassImVec4.def("__str__", [](const ImVec4& self) -> std::string {
char r[100];
snprintf(r, 100, "ImVec4(%f, %f, %f, %f)", self.x, self.y, self.z, self.w);
Expand Down Expand Up @@ -6592,7 +6613,22 @@ void py_init_module_imgui_main(py::module& m)
return ImVec4(imv.x, imv.y, imv.z, imv.w);
}), py::arg("xyzw"));

// Json serialization for ImVec4
pyClassImVec4.def("to_json", [](const ImVec4& self) {
py::dict d;
d["x"] = self.x;
d["y"] = self.y;
d["z"] = self.z;
d["w"] = self.w;
return d;
});
pyClassImVec4.def_static("from_json", [](py::dict data) {
return ImVec4(data["x"].cast<float>(), data["y"].cast<float>(), data["z"].cast<float>(), data["w"].cast<float>());
});

//
// Patches to ImColor
//
pyClassImColor.def("__str__", [](const ImColor& self) -> std::string {
char r[100];
snprintf(r, 100, "ImColor(%f, %f, %f, %f)", self.Value.x, self.Value.y, self.Value.z, self.Value.w);
Expand Down Expand Up @@ -6652,13 +6688,31 @@ void py_init_module_imgui_main(py::module& m)
pyClassImColor.def(py::init([](ImColor imc) {
return ImColor(imc.Value.x, imc.Value.y, imc.Value.z, imc.Value.w);
}), py::arg("rgba"));
// Json serialization for ImColor
pyClassImColor.def("to_json", [](const ImColor& self) {
py::dict d;
d["x"] = self.Value.x;
d["y"] = self.Value.y;
d["z"] = self.Value.z;
d["w"] = self.Value.w;
return d;
});
pyClassImColor.def_static("from_json", [](py::dict data) {
ImVec4 v = ImVec4(data["x"].cast<float>(), data["y"].cast<float>(), data["z"].cast<float>(), data["w"].cast<float>());
return ImColor(v);
});


//
// Patches to ImFontAtlas
//
// make imgui.font_atlas_get_tex_data_as_rgba32() also accessible
// as imgui.get_io().fonts.get_tex_data_as_rgba32(), even if autocomplete
// doesn't find the latter.
pyClassImFontAtlas.def("get_tex_data_as_rgba32", font_atlas_get_tex_data_as_rgba32);

//
// Other patches
//

// VERTEX_SIZE, VERTEX_BUFFER_POS_OFFSET, VERTEX_BUFFER_UV_OFFSET, etc.
// Utilities to facilitate rendering in python backends: they provide buffer offsets info
Expand Down

0 comments on commit 09b985a

Please sign in to comment.