Skip to content

Commit

Permalink
imgui bindings: add to_dict & from_dict 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 09b985a commit f4b1b0c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 38 deletions.
40 changes: 40 additions & 0 deletions bindings/imgui_bundle/imgui/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations
import sys
from typing import (
Dict,
List,
Any,
Optional,
Expand Down Expand Up @@ -483,6 +484,19 @@ class ImVec2:
def __getitem__(self, idx: int) -> float:
"""(private API)"""
pass
# #ifdef IMGUI_BUNDLE_PYTHON_API
#
# std::map<std::string, float> to_dict() const { return {{"x", x}, {"y", y}}; } /* original C++ signature */
def to_dict(self) -> Dict[str, float]:
"""(private API)"""
pass
# static ImVec2 from_dict(const std::map<std::string, float>& d) { IM_ASSERT((d.find("x") != d.end()) && (d.find("y") != d.end()) && "ImVec2::from_dict dict should contain x and y keys"); return ImVec2(d.at("x"), d.at("y")); } /* original C++ signature */
@staticmethod
def from_dict(d: Dict[str, float]) -> ImVec2:
"""(private API)"""
pass
# #endif
#

class ImVec4:
"""ImVec4: 4D vector used to store clipping rectangles, colors etc. [Compile-time configurable type]"""
Expand All @@ -503,6 +517,19 @@ class ImVec4:
@overload
def __init__(self, _x: float, _y: float, _z: float, _w: float) -> None:
pass
# #ifdef IMGUI_BUNDLE_PYTHON_API
#
# std::map<std::string, float> to_dict() const { return {{"x", x}, {"y", y}, {"z", z}, {"w", w}}; } /* original C++ signature */
def to_dict(self) -> Dict[str, float]:
"""(private API)"""
pass
# static ImVec4 from_dict(const std::map<std::string, float>& d) { IM_ASSERT((d.find("x") != d.end()) && (d.find("y") != d.end()) && (d.find("z") != d.end()) && (d.find("w") != d.end()) && "ImVec4::from_dict dict should contain x, y, z and w keys"); return ImVec4(d.at("x"), d.at("y"), d.at("z"), d.at("w")); } /* original C++ signature */
@staticmethod
def from_dict(d: Dict[str, float]) -> ImVec4:
"""(private API)"""
pass
# #endif
#

# -----------------------------------------------------------------------------
# [SECTION] Dear ImGui end-user API functions
Expand Down Expand Up @@ -8752,6 +8779,19 @@ class ImColor:
def hsv(h: float, s: float, v: float, a: float = 1.0) -> ImColor:
"""(private API)"""
pass
# #ifdef IMGUI_BUNDLE_PYTHON_API
#
# std::map<std::string, float> to_dict() const { return {{"x", Value.x}, {"y", Value.x}, {"z", Value.x}, {"w", Value.x}}; } /* original C++ signature */
def to_dict(self) -> Dict[str, float]:
"""(private API)"""
pass
# static ImVec4 from_dict(const std::map<std::string, float>& d) { IM_ASSERT((d.find("x") != d.end()) && (d.find("y") != d.end()) && (d.find("z") != d.end()) && (d.find("w") != d.end()) && "ImVec4.from_dict() requires a dictionary with keys 'x', 'y', 'z', 'w'"); ImVec4 v = ImVec4(d.at("x"), d.at("y"), d.at("z"), d.at("w")); return ImColor(v); } /* original C++ signature */
@staticmethod
def from_dict(d: Dict[str, float]) -> ImVec4:
"""(private API)"""
pass
# #endif
#

# -----------------------------------------------------------------------------
# [SECTION] Drawing API (ImDrawCmd, ImDrawIdx, ImDrawVert, ImDrawChannel, ImDrawListSplitter, ImDrawListFlags, ImDrawList, ImDrawData)
Expand Down
67 changes: 30 additions & 37 deletions external/imgui/bindings/pybind_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ void py_init_module_imgui_main(py::module& m)
py::overload_cast<size_t>(&ImVec2::operator[]),
py::arg("idx"),
"(private API)")
// #ifdef IMGUI_BUNDLE_PYTHON_API
//
.def("to_dict",
&ImVec2::to_dict, "(private API)")
.def_static("from_dict",
&ImVec2::from_dict,
py::arg("d"),
"(private API)")
// #endif
//
.def("__copy__", [](const ImVec2 &self) {
return ImVec2(self);
}) ;
Expand All @@ -187,6 +197,16 @@ void py_init_module_imgui_main(py::module& m)
.def(py::init<>())
.def(py::init<float, float, float, float>(),
py::arg("_x"), py::arg("_y"), py::arg("_z"), py::arg("_w"))
// #ifdef IMGUI_BUNDLE_PYTHON_API
//
.def("to_dict",
&ImVec4::to_dict, "(private API)")
.def_static("from_dict",
&ImVec4::from_dict,
py::arg("d"),
"(private API)")
// #endif
//
.def("__copy__", [](const ImVec4 &self) {
return ImVec4(self);
}) ;
Expand Down Expand Up @@ -5658,6 +5678,16 @@ void py_init_module_imgui_main(py::module& m)
&ImColor::HSV,
py::arg("h"), py::arg("s"), py::arg("v"), py::arg("a") = 1.0f,
"(private API)")
// #ifdef IMGUI_BUNDLE_PYTHON_API
//
.def("to_dict",
&ImColor::to_dict, "(private API)")
.def_static("from_dict",
&ImColor::from_dict,
py::arg("d"),
"(private API)")
// #endif
//
.def("__copy__", [](const ImColor &self) {
return ImColor(self);
}) ;
Expand Down Expand Up @@ -6544,17 +6574,6 @@ 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
//
Expand Down Expand Up @@ -6613,19 +6632,6 @@ 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
//
Expand Down Expand Up @@ -6688,19 +6694,6 @@ 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
Expand Down
2 changes: 1 addition & 1 deletion external/imgui/imgui
Submodule imgui updated 1 files
+15 −0 imgui.h

0 comments on commit f4b1b0c

Please sign in to comment.