[olympic/studio] Add new ImGui util functions

This commit is contained in:
Gary Talent 2024-04-21 10:24:53 -05:00
parent 453f2750dd
commit eeb2a5a151

View File

@ -8,6 +8,8 @@
#include <imgui.h> #include <imgui.h>
#include <ox/std/bit.hpp>
#include <turbine/context.hpp> #include <turbine/context.hpp>
#include <studio/context.hpp> #include <studio/context.hpp>
@ -15,6 +17,91 @@ namespace studio::ig {
inline constexpr auto BtnSz = ImVec2{52, 22}; inline constexpr auto BtnSz = ImVec2{52, 22};
constexpr ImTextureID toImTextureID(ox::Unsigned_c auto id) noexcept
requires(sizeof(id) <= sizeof(ox::Uint<sizeof(ImTextureID)*8>)) {
return std::bit_cast<ImTextureID>(ox::Uint<sizeof(ImTextureID)*8>{id});
}
template<typename T>
ox::Result<T> getDragDropPayload(ox::CStringView name) noexcept {
auto const payload = ImGui::AcceptDragDropPayload(name.c_str());
if (!payload) {
return OxError(1, "No drag/drop payload");
}
return ox::readClaw<T>(
reinterpret_cast<char const*>(payload->Data),
static_cast<size_t>(payload->DataSize));
}
ox::Error setDragDropPayload(ox::CStringView name, auto const &obj) noexcept {
oxRequire(buff, ox::writeClaw(obj, ox::ClawFormat::Metal));
ImGui::SetDragDropPayload(name.c_str(), buff.data(), buff.size());
return {};
}
class DragDropSource {
private:
bool const m_active{};
public:
inline DragDropSource() noexcept:
m_active(ImGui::BeginDragDropSource()) {
}
inline ~DragDropSource() noexcept {
if (m_active) {
ImGui::EndDragDropSource();
}
}
constexpr operator bool() const noexcept {
return m_active;
}
};
inline auto dragDropSource(auto const&cb) noexcept {
if constexpr(ox::is_same_v<decltype(cb()), ox::Error>) {
if (ig::DragDropSource const tgt; tgt) [[unlikely]] {
return cb();
}
} else {
if (ig::DragDropSource const tgt; tgt) [[unlikely]] {
cb();
}
}
}
class DragDropTarget {
private:
bool const m_active{};
public:
inline DragDropTarget() noexcept:
m_active(ImGui::BeginDragDropTarget()) {
}
inline ~DragDropTarget() noexcept {
if (m_active) {
ImGui::EndDragDropTarget();
}
}
constexpr operator bool() const noexcept {
return m_active;
}
};
inline auto dragDropTarget(auto const&cb) noexcept {
if constexpr(ox::is_same_v<decltype(cb()), ox::Error>) {
if (ig::DragDropTarget const tgt; tgt) [[unlikely]] {
return cb();
}
return ox::Error{};
} else {
if (ig::DragDropTarget const tgt; tgt) [[unlikely]] {
cb();
}
}
}
class ChildStackItem { class ChildStackItem {
public: public:
explicit ChildStackItem(ox::CStringView id, ImVec2 const&sz = {}) noexcept; explicit ChildStackItem(ox::CStringView id, ImVec2 const&sz = {}) noexcept;