add enums to property drawer

This commit is contained in:
2019-12-10 03:18:51 +07:00
parent 09edf8c514
commit cc038493eb
3 changed files with 51 additions and 18 deletions

View File

@@ -52,6 +52,38 @@ namespace e2d::imgex
bool show_input_text(str_view label, str* value);
bool show_input_text_multiline(str_view label, str* value);
template < typename E >
bool show_enum(str_view label, E* value) {
char* label_cstr = static_cast<char*>(E2D_CLEAR_ALLOCA(label.size() + 1));
std::memcpy(label_cstr, label.data(), label.size());
std::optional<str_view> preview = enum_hpp::to_string(*value);
char* preview_cstr = static_cast<char*>(E2D_CLEAR_ALLOCA((preview ? preview->size() : 0) + 1));
if ( preview ) {
std::memcpy(preview_cstr, preview->data(), preview->size());
}
if ( ImGui::BeginCombo(label_cstr, preview_cstr) ) {
E2D_DEFER([](){ ImGui::EndCombo(); });
for ( std::size_t i = 0; i < enum_hpp::size<E>(); ++i ) {
str_view item_name = enum_hpp::names<E>()[i];
char* item_name_cstr = static_cast<char*>(E2D_CLEAR_ALLOCA(item_name.size() + 1));
std::memcpy(item_name_cstr, item_name.data(), item_name.size());
bool selected = *value == enum_hpp::values<E>()[i];
if ( ImGui::Selectable(item_name_cstr, selected) ) {
*value = enum_hpp::values<E>()[i];
return true;
} else if ( selected ) {
ImGui::SetItemDefaultFocus();
}
}
}
return false;
}
template < typename... Args >
void show_text(str_view text, Args&&... args) {
if constexpr ( sizeof... (Args) ) {

View File

@@ -78,8 +78,7 @@ namespace e2d::dbgui_widgets
}
bool property_drawer::operator()(str_view l, property<v2f>& p) const {
v2f v = *p;
if ( (*this)(l, v) ) {
if ( v2f v = *p; (*this)(l, v) ) {
p = v;
return true;
}
@@ -135,8 +134,7 @@ namespace e2d::dbgui_widgets
}
bool property_drawer::operator()(str_view l, property<v3f>& p) const {
v3f v = *p;
if ( (*this)(l, v) ) {
if ( v3f v = *p; (*this)(l, v) ) {
p = v;
return true;
}
@@ -192,8 +190,7 @@ namespace e2d::dbgui_widgets
}
bool property_drawer::operator()(str_view l, property<v4f>& p) const {
v4f v = *p;
if ( (*this)(l, v) ) {
if ( v4f v = *p; (*this)(l, v) ) {
p = v;
return true;
}
@@ -247,8 +244,7 @@ namespace e2d::dbgui_widgets
}
bool property_drawer::operator()(str_view l, property<str>& p) const {
str v = *p;
if ( (*this)(l, v) ) {
if ( str v = *p; (*this)(l, v) ) {
p = std::move(v);
return true;
}
@@ -283,8 +279,7 @@ namespace e2d::dbgui_widgets
}
bool property_drawer::operator()(str_view l, property<degf>& p) const {
degf v = *p;
if ( (*this)(l, v) ) {
if ( degf v = *p; (*this)(l, v) ) {
p = v;
return true;
}
@@ -340,8 +335,7 @@ namespace e2d::dbgui_widgets
}
bool property_drawer::operator()(str_view l, property<radf>& p) const {
radf v = *p;
if ( (*this)(l, v) ) {
if ( radf v = *p; (*this)(l, v) ) {
p = v;
return true;
}
@@ -397,8 +391,7 @@ namespace e2d::dbgui_widgets
}
bool property_drawer::operator()(str_view l, property<color>& p) const {
color v = *p;
if ( (*this)(l, v) ) {
if ( color v = *p; (*this)(l, v) ) {
p = v;
return true;
}
@@ -410,8 +403,7 @@ namespace e2d::dbgui_widgets
//
bool property_drawer::operator()(str_view l, color32& p) const {
color v = color(p);
if ( (*this)(l, v) ) {
if ( color v = color(p); (*this)(l, v) ) {
p = color32(v);
return true;
}
@@ -419,8 +411,7 @@ namespace e2d::dbgui_widgets
}
bool property_drawer::operator()(str_view l, property<color32>& p) const {
color v = color(*p);
if ( (*this)(l, v) ) {
if ( color v = color(*p); (*this)(l, v) ) {
p = color32(v);
return true;
}

View File

@@ -55,5 +55,15 @@ namespace e2d::dbgui_widgets
bool operator()(str_view l, color32& p) const;
bool operator()(str_view l, property<color32>& p) const;
template < typename E >
std::enable_if_t<std::is_enum_v<E>, bool>
operator()(str_view l, property<E>& p) const {
if ( E v = *p; imgex::show_enum(l, &v) ) {
p = v;
return true;
}
return false;
}
};
}