diff --git a/src/binding_field.h b/src/binding_field.h index 52b5c8e..04e0513 100644 --- a/src/binding_field.h +++ b/src/binding_field.h @@ -1,6 +1,5 @@ #pragma once -#include "binding.h" -#include "node.h" +#include "binding_property.h" #include "cppgen.h" template diff --git a/src/binding_input.h b/src/binding_input.h index e9166ad..5bb3751 100644 --- a/src/binding_input.h +++ b/src/binding_input.h @@ -6,7 +6,7 @@ #include #include "stx.h" #include "utils.h" -#include "binding.h" +#include "binding_property.h" #include "cppgen.h" #include "ui_new_field.h" #include "ui_binding.h" diff --git a/src/binding.cpp b/src/binding_property.cpp similarity index 56% rename from src/binding.cpp rename to src/binding_property.cpp index 9a028b8..f4dca67 100644 --- a/src/binding.cpp +++ b/src/binding_property.cpp @@ -1,84 +1,7 @@ -#include "binding.h" +#include "binding_property.h" #include "cppgen.h" #include -std::string CodeShortcut(std::string_view sh) -{ - if (sh.empty()) - return ""; - size_t i = -1; - std::string code; - while (true) - { - size_t j = sh.find_first_of("+-", i + 1); - if (j == std::string::npos) - j = sh.size(); - std::string key(sh.substr(i + 1, j - i - 1)); - if (key.empty() && j < sh.size()) //like in ctrl-+ - key = sh[j]; - stx::erase_if(key, [](char c) { return std::isspace(c); }); - if (key.empty()) - break; - std::string lk = key; - stx::for_each(lk, [](char& c) { c = std::tolower(c); }); - //todo - if (lk == "ctrl") - code += "ImGuiMod_Ctrl"; - else if (lk == "alt") - code += "ImGuiMod_Alt"; - else if (lk == "shift") - code += "ImGuiMod_Shift"; - else if (key == "+") - code += "ImGuiKey_Equal"; - else if (key == "-") - code += "ImGuiKey_Minus"; - else if (key == ".") - code += "ImGuiKey_Period"; - else if (key == ",") - code += "ImGuiKey_Comma"; - else if (key == "/") - code += "ImGuiKey_Slash"; - else - code += std::string("ImGuiKey_") - + (char)std::toupper(key[0]) - + key.substr(1); - code += " | "; - i = j; - if (j == sh.size()) - break; - } - if (code.size()) - code.resize(code.size() - 3); - return code; -} - -std::string ParseShortcut(std::string_view line) -{ - std::string sh; - size_t i = 0; - while (true) - { - size_t j1 = line.find("ImGuiKey_", i); - size_t j2 = line.find("ImGuiMod_", i); - if (j1 == std::string::npos && j2 == std::string::npos) - break; - if (j1 < j2) - i = j1 + 9; - else - i = j2 + 9; - for (; i < line.size(); ++i) - { - if (!isalnum(line[i])) - break; - sh += line[i]; - } - sh += "+"; - } - if (sh.size()) - sh.pop_back(); - return sh; -} - float direct_val::eval_px(const UIContext& ctx) const { return val * ctx.zoomFactor; diff --git a/src/binding.h b/src/binding_property.h similarity index 87% rename from src/binding.h rename to src/binding_property.h index 203aa06..68e104e 100644 --- a/src/binding.h +++ b/src/binding_property.h @@ -8,145 +8,8 @@ #include "stx.h" #include "utils.h" #include "cpp_parser.h" +#include "binding_type.h" -struct UIContext; - -std::string CodeShortcut(std::string_view sh); -std::string ParseShortcut(std::string_view line); - -//negative values allowed -struct dimension -{ - float value; - dimension(float v = 0) : value(v) {} - dimension& operator= (float v) { value = v; return *this; } - operator float& () { return value; } - operator const float& () const { return value; } -}; - -//positive or zero dimension (so that neg values represent empty value) -struct pzdimension -{ - float value; - pzdimension(float v = 0) : value(v) {} - pzdimension& operator= (float v) { value = v; return *this; } - operator float& () { return value; } - operator const float& () const { return value; } -}; - -struct pzdimension2 -{ - ImVec2 value; - pzdimension2(ImVec2 v = {}) : value(v) {} - pzdimension2& operator= (ImVec2 v) { value = v; return *this; } - operator ImVec2& () { return value; } - operator const ImVec2& () const { return value; } - float& operator[] (int i) { return value[i]; } - const float operator[] (int i) const { return value[i]; } //no & as in ImVec2 - bool operator== (ImVec2 v) const { return value[0] == v[0] && value[1] == v[1]; } - bool operator!= (ImVec2 v) const { return !(*this == v); } -}; - -struct font_name -{ - std::string value; - font_name(const std::string& v = {}) : value(v) {} - font_name& operator= (const std::string& v) { value = v; return *this; } - operator std::string& () { return value; } - operator const std::string& () const { return value; } - operator std::string_view() const { return value; } - /*friend std::ostream& operator<< (std::ostream& os, const font_name& fn) - { - return os << "\"" << fn.value << "\""; - } - friend std::istream& operator>> (std::istream& is, font_name& fn) - { - if (is.get() != '"') { - is.setstate(std::ios::failbit); - return is; - } - std::getline(is, fn.value); - if (value.empty() || fn.value.back() != '"') { - is.setstate(std::ios::failbit); - return is; - } - value.pop_back(); - return is; - }*/ -}; - -struct shortcut_ -{ - std::string value; - shortcut_(const std::string& v = {}) : value(v) {} - operator const std::string& () { return value; } -}; - -struct color32 -{ - color32(ImU32 c = 0) : c(c) {} //0 means style default color - color32(const ImVec4& v) : c(IM_COL32(255*v.x, 255*v.y, 255*v.z, 255*v.w)) {} - color32& operator= (ImU32 cc) { c = cc; return *this; } - operator ImU32 () const { return c; } - operator ImU32& () { return c; } - bool operator== (const color32& a) const { return c == a.c; } - bool operator!= (const color32& a) const { return c != a.c; } - std::string string() const { - std::ostringstream os; - os << *this; - return os.str(); - } - friend std::ostream& operator<< (std::ostream& os, color32 clr) - { - if (!clr) - return os; - return os << "0x" << std::hex << std::setw(2*4) - << std::setfill('0') << (ImU32)clr; - /*os.fill('0'); - return os << "#" << std::hex - << std::setw(2) << ((clr >> IM_COL32_R_SHIFT) & 0xff) - << std::setw(2) << ((clr >> IM_COL32_G_SHIFT) & 0xff) - << std::setw(2) << ((clr >> IM_COL32_B_SHIFT) & 0xff) - << std::setw(2) << ((clr >> IM_COL32_A_SHIFT) & 0xff);*/ - } - friend std::istream& operator>> (std::istream& is, color32& c) - { - if (is.peek() == EOF) { - c = color32(); - return is; - } - if (is.get() != '0') { - //c = color32(); - is.setstate(std::ios::failbit); - return is; - } - if (is.get() != 'x') { - //c = color32(); - is.setstate(std::ios::failbit); - return is; - } - ImU32 cc = 0; - for (int i = 0; i < 8; ++i) - { - int v = is.get(); - if (v >= 'a' && v <= 'f') - v = v - 'a' + 10; - else if (v >= 'A' && v <= 'F') - v = v - 'A' + 10; - else - v -= '0'; - cc = (cc << 4) | v; - } - c = cc; - return is; - } - - -private: - ImU32 c; -}; - -//------------------------------------------------------------ template struct two_step diff --git a/src/binding_type.h b/src/binding_type.h new file mode 100755 index 0000000..a4c6edc --- /dev/null +++ b/src/binding_type.h @@ -0,0 +1,211 @@ +#pragma once +#include +#include +#include + +//negative values allowed +struct dimension +{ + float value; + dimension(float v = 0) : value(v) {} + dimension& operator= (float v) { value = v; return *this; } + operator float& () { return value; } + operator const float& () const { return value; } +}; + +//positive or zero dimension (so that neg values represent empty value) +struct pzdimension +{ + float value; + pzdimension(float v = 0) : value(v) {} + pzdimension& operator= (float v) { value = v; return *this; } + operator float& () { return value; } + operator const float& () const { return value; } +}; + +struct pzdimension2 +{ + ImVec2 value; + pzdimension2(ImVec2 v = {}) : value(v) {} + pzdimension2& operator= (ImVec2 v) { value = v; return *this; } + operator ImVec2& () { return value; } + operator const ImVec2& () const { return value; } + float& operator[] (int i) { return value[i]; } + const float operator[] (int i) const { return value[i]; } //no & as in ImVec2 + bool operator== (ImVec2 v) const { return value[0] == v[0] && value[1] == v[1]; } + bool operator!= (ImVec2 v) const { return !(*this == v); } +}; + +struct font_name +{ + std::string value; + font_name(const std::string& v = {}) : value(v) {} + font_name& operator= (const std::string& v) { value = v; return *this; } + operator std::string& () { return value; } + operator const std::string& () const { return value; } + operator std::string_view() const { return value; } + /*friend std::ostream& operator<< (std::ostream& os, const font_name& fn) + { + return os << "\"" << fn.value << "\""; + } + friend std::istream& operator>> (std::istream& is, font_name& fn) + { + if (is.get() != '"') { + is.setstate(std::ios::failbit); + return is; + } + std::getline(is, fn.value); + if (value.empty() || fn.value.back() != '"') { + is.setstate(std::ios::failbit); + return is; + } + value.pop_back(); + return is; + }*/ +}; + +struct shortcut_ +{ + std::string value; + shortcut_(const std::string& v = {}) : value(v) {} + operator const std::string& () { return value; } +}; + +struct color32 +{ + color32(ImU32 c = 0) : c(c) {} //0 means style default color + color32(const ImVec4& v) : c(IM_COL32(255 * v.x, 255 * v.y, 255 * v.z, 255 * v.w)) {} + color32& operator= (ImU32 cc) { c = cc; return *this; } + operator ImU32 () const { return c; } + operator ImU32& () { return c; } + bool operator== (const color32& a) const { return c == a.c; } + bool operator!= (const color32& a) const { return c != a.c; } + std::string string() const { + std::ostringstream os; + os << *this; + return os.str(); + } + friend std::ostream& operator<< (std::ostream& os, color32 clr) + { + if (!clr) + return os; + return os << "0x" << std::hex << std::setw(2 * 4) + << std::setfill('0') << (ImU32)clr; + /*os.fill('0'); + return os << "#" << std::hex + << std::setw(2) << ((clr >> IM_COL32_R_SHIFT) & 0xff) + << std::setw(2) << ((clr >> IM_COL32_G_SHIFT) & 0xff) + << std::setw(2) << ((clr >> IM_COL32_B_SHIFT) & 0xff) + << std::setw(2) << ((clr >> IM_COL32_A_SHIFT) & 0xff);*/ + } + friend std::istream& operator>> (std::istream& is, color32& c) + { + if (is.peek() == EOF) { + c = color32(); + return is; + } + if (is.get() != '0') { + //c = color32(); + is.setstate(std::ios::failbit); + return is; + } + if (is.get() != 'x') { + //c = color32(); + is.setstate(std::ios::failbit); + return is; + } + ImU32 cc = 0; + for (int i = 0; i < 8; ++i) + { + int v = is.get(); + if (v >= 'a' && v <= 'f') + v = v - 'a' + 10; + else if (v >= 'A' && v <= 'F') + v = v - 'A' + 10; + else + v -= '0'; + cc = (cc << 4) | v; + } + c = cc; + return is; + } + + +private: + ImU32 c; +}; + +//------------------------------------------------------------------ + +template +struct typeid_func_name; + +template +std::string typeid_name() +{ + if constexpr (std::is_pointer_v) + return typeid_name>() + "*"; + else if constexpr (std::is_lvalue_reference_v) + return typeid_name>() + "&"; + else if constexpr (std::is_rvalue_reference_v) + return typeid_name>() + "&&"; + else if constexpr (std::is_function_v) + return typeid_func_name::str(); + + else if constexpr (std::is_same_v) + return "void"; + else if constexpr (std::is_same_v) + return "std::string"; + else if constexpr (std::is_same_v) + return "color4"; + else if constexpr (std::is_same_v) + return "float"; + else if constexpr (std::is_same_v) + return "int"; + else if constexpr (std::is_same_v) + return "float"; + else if constexpr (std::is_same_v) + return "double"; + else if constexpr (std::is_same_v) + return "size_t"; + else if constexpr (std::is_same_v) + return "bool"; + else if constexpr (std::is_same_v || std::is_same_v) + return "ImVec2"; + else if constexpr (std::is_same_v) + return "ImFont*"; + else if constexpr (std::is_same_v>) + return "std::vector"; + else + { + std::string str = typeid(T).name(); + if (str.size() >= 14 && !str.compare(str.size() - 14, 14, " __cdecl(void)")) + str = str.substr(0, str.size() - 14) + "()"; + auto i = str.find(' '); + if (i != std::string::npos) + str.erase(0, i + 1); + auto it = stx::find_if(str, [](char c) { return isalpha(c); }); + if (it != str.end()) + str.erase(0, it - str.begin()); + return str; + } +} + + +template +struct typeid_func_name +{ + static std::string str() + { + return typeid_name() + "()"; + } +}; + +template +struct typeid_func_name +{ + static std::string str() + { + return typeid_name() + "(" + typeid_name() + ")"; + } +}; diff --git a/src/cppgen.h b/src/cppgen.h index 58e5e97..b45b0da 100644 --- a/src/cppgen.h +++ b/src/cppgen.h @@ -2,7 +2,7 @@ #include #include #include -#include "node2.h" +#include "node_container.h" //------------------------------------------------------ class CppGen diff --git a/src/imrad.cpp b/src/imrad.cpp index 651b743..9d52bb8 100644 --- a/src/imrad.cpp +++ b/src/imrad.cpp @@ -24,7 +24,7 @@ #include // Will drag system OpenGL headers #include -#include "node.h" +#include "node_standard.h" #include "cppgen.h" #include "utils.h" #include "ui_new_field.h" diff --git a/src/node2.cpp b/src/node_container.cpp similarity index 99% rename from src/node2.cpp rename to src/node_container.cpp index ee5469d..79036cf 100644 --- a/src/node2.cpp +++ b/src/node_container.cpp @@ -1,4 +1,4 @@ -#include "node2.h" +#include "node_container.h" #include "stx.h" #include "imrad.h" #include "cppgen.h" diff --git a/src/node2.h b/src/node_container.h similarity index 99% rename from src/node2.h rename to src/node_container.h index 92be2d0..0750150 100644 --- a/src/node2.h +++ b/src/node_container.h @@ -5,7 +5,7 @@ #include #include #include -#include "node.h" +#include "node_standard.h" struct Table : Widget { diff --git a/src/node.cpp b/src/node_standard.cpp similarity index 99% rename from src/node.cpp rename to src/node_standard.cpp index 6044215..d7bcedf 100644 --- a/src/node.cpp +++ b/src/node_standard.cpp @@ -1,4 +1,4 @@ -#include "node.h" +#include "node_standard.h" #include "stx.h" #include "imrad.h" #include "cppgen.h" diff --git a/src/node.h b/src/node_standard.h similarity index 99% rename from src/node.h rename to src/node_standard.h index 6c5df7c..ea1e2be 100644 --- a/src/node.h +++ b/src/node_standard.h @@ -6,7 +6,7 @@ #include #include #include "uicontext.h" -#include "binding.h" +#include "binding_property.h" #include "imrad.h" #include "IconsFontAwesome6.h" diff --git a/src/ui_horiz_layout.cpp b/src/ui_horiz_layout.cpp index 18e7f49..d6b4411 100644 --- a/src/ui_horiz_layout.cpp +++ b/src/ui_horiz_layout.cpp @@ -2,7 +2,7 @@ // visit github.com/tpecholt/imrad #include "ui_horiz_layout.h" -#include "node2.h" +#include "node_container.h" HorizLayout horizLayout; diff --git a/src/ui_horiz_layout.h b/src/ui_horiz_layout.h index fbbb34e..2f2f434 100644 --- a/src/ui_horiz_layout.h +++ b/src/ui_horiz_layout.h @@ -3,7 +3,7 @@ #pragma once #include "imrad.h" -#include "node.h" +#include "node_standard.h" class HorizLayout { diff --git a/src/ui_new_field.h b/src/ui_new_field.h index 8b2fa9c..d110195 100644 --- a/src/ui_new_field.h +++ b/src/ui_new_field.h @@ -1,5 +1,4 @@ #pragma once -#include "binding.h" #include "cppgen.h" #include #include diff --git a/src/ui_table_columns.h b/src/ui_table_columns.h index 2a92b84..23c3152 100644 --- a/src/ui_table_columns.h +++ b/src/ui_table_columns.h @@ -2,7 +2,7 @@ // github.com/xyz #pragma once -#include "node2.h" +#include "node_container.h" #include #include #include diff --git a/src/utils.cpp b/src/utils.cpp index 91dfb39..60ce656 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,5 +1,5 @@ #include "utils.h" -#include "node.h" +#include "node_standard.h" #if WIN32 #define WIN32_LEAN_AND_MEAN #include @@ -24,6 +24,83 @@ int InputTextCharExprFilter(ImGuiInputTextCallbackData* data) return 0; } +std::string CodeShortcut(std::string_view sh) +{ + if (sh.empty()) + return ""; + size_t i = -1; + std::string code; + while (true) + { + size_t j = sh.find_first_of("+-", i + 1); + if (j == std::string::npos) + j = sh.size(); + std::string key(sh.substr(i + 1, j - i - 1)); + if (key.empty() && j < sh.size()) //like in ctrl-+ + key = sh[j]; + stx::erase_if(key, [](char c) { return std::isspace(c); }); + if (key.empty()) + break; + std::string lk = key; + stx::for_each(lk, [](char& c) { c = std::tolower(c); }); + //todo + if (lk == "ctrl") + code += "ImGuiMod_Ctrl"; + else if (lk == "alt") + code += "ImGuiMod_Alt"; + else if (lk == "shift") + code += "ImGuiMod_Shift"; + else if (key == "+") + code += "ImGuiKey_Equal"; + else if (key == "-") + code += "ImGuiKey_Minus"; + else if (key == ".") + code += "ImGuiKey_Period"; + else if (key == ",") + code += "ImGuiKey_Comma"; + else if (key == "/") + code += "ImGuiKey_Slash"; + else + code += std::string("ImGuiKey_") + + (char)std::toupper(key[0]) + + key.substr(1); + code += " | "; + i = j; + if (j == sh.size()) + break; + } + if (code.size()) + code.resize(code.size() - 3); + return code; +} + +std::string ParseShortcut(std::string_view line) +{ + std::string sh; + size_t i = 0; + while (true) + { + size_t j1 = line.find("ImGuiKey_", i); + size_t j2 = line.find("ImGuiMod_", i); + if (j1 == std::string::npos && j2 == std::string::npos) + break; + if (j1 < j2) + i = j1 + 9; + else + i = j2 + 9; + for (; i < line.size(); ++i) + { + if (!isalnum(line[i])) + break; + sh += line[i]; + } + sh += "+"; + } + if (sh.size()) + sh.pop_back(); + return sh; +} + //---------------------------------------------------- child_iterator::iter::iter() diff --git a/src/utils.h b/src/utils.h index 0cc462e..12f7da5 100644 --- a/src/utils.h +++ b/src/utils.h @@ -62,81 +62,8 @@ void ShellExec(const std::string& path); int InputTextCharExprFilter(ImGuiInputTextCallbackData* data); -//---------------------------------------------------------------------- - -template -struct typeid_func_name; - -template -struct typeid_func_name -{ - static std::string str() - { - return typeid_name() + "()"; - } -}; - -template -struct typeid_func_name -{ - static std::string str() - { - return typeid_name() + "(" + typeid_name() + ")"; - } -}; - -template -std::string typeid_name() -{ - if constexpr (std::is_pointer_v) - return typeid_name>() + "*"; - else if constexpr (std::is_lvalue_reference_v) - return typeid_name>() + "&"; - else if constexpr (std::is_rvalue_reference_v) - return typeid_name>() + "&&"; - else if constexpr (std::is_function_v) - return typeid_func_name::str(); - - else if constexpr (std::is_same_v) - return "void"; - else if constexpr (std::is_same_v) - return "std::string"; - else if constexpr (std::is_same_v) - return "color4"; - else if constexpr (std::is_same_v) - return "float"; - else if constexpr (std::is_same_v) - return "int"; - else if constexpr (std::is_same_v) - return "float"; - else if constexpr (std::is_same_v) - return "double"; - else if constexpr (std::is_same_v) - return "size_t"; - else if constexpr (std::is_same_v) - return "bool"; - else if constexpr (std::is_same_v || std::is_same_v) - return "ImVec2"; - else if constexpr (std::is_same_v) - return "ImFont*"; - else if constexpr (std::is_same_v>) - return "std::vector"; - else - { - std::string str = typeid(T).name(); - if (str.size() >= 14 && !str.compare(str.size() - 14, 14, " __cdecl(void)")) - str = str.substr(0, str.size() - 14) + "()"; - auto i = str.find(' '); - if (i != std::string::npos) - str.erase(0, i + 1); - auto it = stx::find_if(str, [](char c) { return isalpha(c); }); - if (it != str.end()) - str.erase(0, it - str.begin()); - return str; - } -} - - +std::string CodeShortcut(std::string_view sh); +std::string ParseShortcut(std::string_view line); //----------------------------------------------------------------------