diff --git a/engine/core/include/debug.hpp b/engine/core/include/debug.hpp index 6595e9b..5313598 100644 --- a/engine/core/include/debug.hpp +++ b/engine/core/include/debug.hpp @@ -2,6 +2,7 @@ #include +void draw_console(); void draw_debug_ui(); void load_debug_options(); diff --git a/engine/core/include/engine.hpp b/engine/core/include/engine.hpp index 47bd8c1..30fa471 100755 --- a/engine/core/include/engine.hpp +++ b/engine/core/include/engine.hpp @@ -210,6 +210,12 @@ namespace prism { */ void process_mouse_down(int button, prism::Offset offset); + /** + * Called when text has been inputted. This is only emitted when text input is enabled thru input system + * @param string + */ + void process_text_input(const std::string_view string); + /** Adds a timer to the list of timers. @param timer The timer to add. @note The timer instance is passed by reference. Use this to keep track of your timers without having to query it's state back. @@ -282,6 +288,8 @@ namespace prism { bool debug_enabled = false; #endif + bool console_enabled = true; + private: void setup_scene(Scene& scene); diff --git a/engine/core/include/imgui_backend.hpp b/engine/core/include/imgui_backend.hpp index 79966f4..5b302da 100644 --- a/engine/core/include/imgui_backend.hpp +++ b/engine/core/include/imgui_backend.hpp @@ -1,5 +1,7 @@ #pragma once +#include + namespace prism { class imgui_backend { public: @@ -14,6 +16,8 @@ namespace prism { void process_key_down(unsigned int key_code); void process_key_up(unsigned int key_code); + void process_text_input(const std::string_view string); + private: bool mouse_buttons[3] = {}; }; diff --git a/engine/core/include/input.hpp b/engine/core/include/input.hpp index ed1fe54..e564447 100755 --- a/engine/core/include/input.hpp +++ b/engine/core/include/input.hpp @@ -81,8 +81,16 @@ namespace prism { /// Returns all of the bindings registered with this Input system. [[nodiscard]] std::vector get_bindings() const; + void begin_text_input(); + void end_text_input(); + bool is_text_input() const; + + bool get_allowable_text_button(unsigned int keycode) const; + private: std::vector _input_bindings; std::tuple _last_cursor_position; + + bool _in_text_input = false; }; } \ No newline at end of file diff --git a/engine/core/src/debug.cpp b/engine/core/src/debug.cpp index 2470156..7925b27 100644 --- a/engine/core/src/debug.cpp +++ b/engine/core/src/debug.cpp @@ -12,6 +12,7 @@ #include "scene.hpp" #include "renderer.hpp" #include "file.hpp" +#include "console.hpp" struct Options { std::string shader_source_path; @@ -244,3 +245,15 @@ void save_debug_options() { std::string_view get_shader_source_directory() { return options.shader_source_path; } + +void draw_console() { + ImGui::Text("%s", prism::log_output.c_str()); + + static std::string console_input; + ImGui::InputText("##console_input", &console_input); + + if(ImGui::Button("Input")) { + prism::console::parse_and_invoke_command(console_input); + console_input.clear(); + } +} \ No newline at end of file diff --git a/engine/core/src/engine.cpp b/engine/core/src/engine.cpp index dd5948e..98ca1f9 100755 --- a/engine/core/src/engine.cpp +++ b/engine/core/src/engine.cpp @@ -424,11 +424,17 @@ void engine::resize(const int identifier, const prism::Extent extent) { void engine::process_key_down(const unsigned int keyCode) { Expects(keyCode >= 0); - + + if(input->is_text_input() && !input->get_allowable_text_button(keyCode)) + return; + imgui->process_key_down(keyCode); if(keyCode == platform::get_keycode(debug_button) && !ImGui::GetIO().WantTextInput) debug_enabled = !debug_enabled; + + if(keyCode == platform::get_keycode(InputButton::C)) + console_enabled = !console_enabled; } void engine::process_key_up(const unsigned int keyCode) { @@ -604,6 +610,9 @@ void engine::begin_frame(const float delta_time) { if(debug_enabled) draw_debug_ui(); + if(console_enabled) + draw_console(); + if(app != nullptr) app->begin_frame(); } @@ -798,3 +807,7 @@ void engine::setup_scene(Scene& scene) { scene.reset_shadows(); scene.reset_environment(); } + +void prism::engine::process_text_input(const std::string_view string) { + imgui->process_text_input(string); +} diff --git a/engine/core/src/imgui_backend.cpp b/engine/core/src/imgui_backend.cpp index ea93270..05ad732 100644 --- a/engine/core/src/imgui_backend.cpp +++ b/engine/core/src/imgui_backend.cpp @@ -6,6 +6,7 @@ #include "engine.hpp" #include "platform.hpp" #include "assertions.hpp" +#include "input.hpp" using prism::imgui_backend; @@ -159,6 +160,17 @@ imgui_backend::imgui_backend() { void imgui_backend::begin_frame(const float delta_time) { ImGuiIO& io = ImGui::GetIO(); + + const bool imgui_wants_text = io.WantTextInput; + const bool input_is_text = ::engine->get_input()->is_text_input(); + + if(imgui_wants_text != input_is_text) { + if(imgui_wants_text) { + ::engine->get_input()->begin_text_input(); + } else { + ::engine->get_input()->end_text_input(); + } + } const auto [width, height] = platform::get_window_size(0); const auto [dw, dh] = platform::get_window_drawable_size(0); @@ -205,10 +217,9 @@ void imgui_backend::render(int index) { void imgui_backend::process_key_down(unsigned int key_code) { Expects(key_code >= 0); - + ImGuiIO& io = ImGui::GetIO(); - io.AddInputCharactersUTF8(platform::translate_keycode(key_code)); - + io.KeysDown[key_code] = true; } @@ -222,4 +233,9 @@ void imgui_backend::process_key_up(unsigned int key_code) { void imgui_backend::process_mouse_down(int button) { mouse_buttons[button] = true; -} \ No newline at end of file +} + +void prism::imgui_backend::process_text_input(const std::string_view string) { + ImGuiIO& io = ImGui::GetIO(); + io.AddInputCharactersUTF8(string.data()); +} diff --git a/engine/core/src/input.cpp b/engine/core/src/input.cpp index 7397b03..5ee2844 100755 --- a/engine/core/src/input.cpp +++ b/engine/core/src/input.cpp @@ -155,3 +155,24 @@ bool input_system::is_repeating(const std::string& name) { std::vector input_system::get_bindings() const { return _input_bindings; } + +void input_system::begin_text_input() { + _in_text_input = true; + platform::begin_text_input(); +} + +void input_system::end_text_input() { + _in_text_input = false; + platform::end_text_input(); +} + +bool input_system::is_text_input() const { + return _in_text_input; +} + +bool input_system::get_allowable_text_button(unsigned int keycode) const { + if(platform::get_keycode(InputButton::Backspace) == keycode) + return true; + + return false; +} \ No newline at end of file diff --git a/engine/log/include/log.hpp b/engine/log/include/log.hpp index 6ceacce..51ebb4c 100755 --- a/engine/log/include/log.hpp +++ b/engine/log/include/log.hpp @@ -3,8 +3,12 @@ #include namespace prism { + inline std::string log_output; + inline void vlog(fmt::string_view format, fmt::format_args args) { - fmt::vprint(format, args); + auto str = fmt::vformat(format, args); + fmt::print("{}", str); + log_output += str + "\n"; } template diff --git a/engine/platform/include/platform.hpp b/engine/platform/include/platform.hpp index 9bb0eaa..0c35556 100755 --- a/engine/platform/include/platform.hpp +++ b/engine/platform/include/platform.hpp @@ -159,6 +159,10 @@ namespace platform { /// On platforms that support moue capture, this will lock the mouse cursor to the window and hide it. void capture_mouse(const bool capture); + // TODO: right now the OS intercepting and saying "We dont want text input anymore" ala software keyboards is NOT supported yet + void begin_text_input(); + void end_text_input(); + /**Opens a file dialog to select a file. This will not block. @param existing Whether or not to limit to existing files. @param returnFunction The callback function when a file is selected or the dialog is cancelled. An empy string is returned when cancelled. diff --git a/extern/magic_enum/include/magic_enum.hpp b/extern/magic_enum/include/magic_enum.hpp old mode 100755 new mode 100644 index ae49308..f2c9dea --- a/extern/magic_enum/include/magic_enum.hpp +++ b/extern/magic_enum/include/magic_enum.hpp @@ -5,11 +5,11 @@ // | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_| // |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____| // __/ | https://github.com/Neargye/magic_enum -// |___/ version 0.6.5 +// |___/ version 0.7.3 // // Licensed under the MIT License . // SPDX-License-Identifier: MIT -// Copyright (c) 2019 Daniil Goncharov . +// Copyright (c) 2019 - 2021 Daniil Goncharov . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -32,29 +32,57 @@ #ifndef NEARGYE_MAGIC_ENUM_HPP #define NEARGYE_MAGIC_ENUM_HPP +#define MAGIC_ENUM_VERSION_MAJOR 0 +#define MAGIC_ENUM_VERSION_MINOR 7 +#define MAGIC_ENUM_VERSION_PATCH 3 + #include #include #include #include #include #include -#include -#include #include #include -#if defined(_MSC_VER) +#if defined(MAGIC_ENUM_CONFIG_FILE) +#include MAGIC_ENUM_CONFIG_FILE +#endif + +#if !defined(MAGIC_ENUM_USING_ALIAS_OPTIONAL) +#include +#endif +#if !defined(MAGIC_ENUM_USING_ALIAS_STRING) +#include +#endif +#if !defined(MAGIC_ENUM_USING_ALIAS_STRING_VIEW) +#include +#endif + +#if defined(__clang__) +# pragma clang diagnostic push +#elif defined(__GNUC__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // May be used uninitialized 'return {};'. +#elif defined(_MSC_VER) # pragma warning(push) -# pragma warning(disable : 26495) // Variable 'magic_enum::detail::static_string::chars' is uninitialized. -# pragma warning(disable : 26451) // Arithmetic overflow: 'indexes[static_cast(value) - min_v]' using operator '-' on a 4 byte value and then casting the result to a 8 byte value. +# pragma warning(disable : 26495) // Variable 'static_string::chars_' is uninitialized. +# pragma warning(disable : 28020) // Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value. +# pragma warning(disable : 26451) // The expression '0<=_Param_(1)&&_Param_(1)<=1-1' is not true at this call. #endif // Checks magic_enum compiler compatibility. -#if defined(__clang__) || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) -# undef MAGIC_ENUM_SUPPORTED +#if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1910 +# undef MAGIC_ENUM_SUPPORTED # define MAGIC_ENUM_SUPPORTED 1 #endif +// Checks magic_enum compiler aliases compatibility. +#if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1920 +# undef MAGIC_ENUM_SUPPORTED_ALIASES +# define MAGIC_ENUM_SUPPORTED_ALIASES 1 +#endif + // Enum value must be greater or equals than MAGIC_ENUM_RANGE_MIN. By default MAGIC_ENUM_RANGE_MIN = -128. // If need another min range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN. #if !defined(MAGIC_ENUM_RANGE_MIN) @@ -68,544 +96,1043 @@ #endif namespace magic_enum { - - // Enum value must be in range [MAGIC_ENUM_RANGE_MIN, MAGIC_ENUM_RANGE_MAX]. By default MAGIC_ENUM_RANGE_MIN = -128, MAGIC_ENUM_RANGE_MAX = 128. - // If need another range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN and MAGIC_ENUM_RANGE_MAX. - // If need another range for specific enum type, add specialization enum_range for necessary enum type. - template - struct enum_range { - static_assert(std::is_enum_v, "magic_enum::enum_range requires enum type."); - inline static constexpr int min = MAGIC_ENUM_RANGE_MIN; - inline static constexpr int max = MAGIC_ENUM_RANGE_MAX; - static_assert(max > min, "magic_enum::enum_range requires max > min."); - }; - - static_assert(MAGIC_ENUM_RANGE_MIN <= 0, "MAGIC_ENUM_RANGE_MIN must be less or equals than 0."); - static_assert(MAGIC_ENUM_RANGE_MIN > (std::numeric_limits::min)(), "MAGIC_ENUM_RANGE_MIN must be greater than INT16_MIN."); - - static_assert(MAGIC_ENUM_RANGE_MAX > 0, "MAGIC_ENUM_RANGE_MAX must be greater than 0."); - static_assert(MAGIC_ENUM_RANGE_MAX < (std::numeric_limits::max)(), "MAGIC_ENUM_RANGE_MAX must be less than INT16_MAX."); - - static_assert(MAGIC_ENUM_RANGE_MAX > MAGIC_ENUM_RANGE_MIN, "MAGIC_ENUM_RANGE_MAX must be greater than MAGIC_ENUM_RANGE_MIN."); - - namespace detail { - - template - struct supported -#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED || defined(MAGIC_ENUM_NO_CHECK_SUPPORT) - : std::true_type {}; + +// If need another optional type, define the macro MAGIC_ENUM_USING_ALIAS_OPTIONAL. +#if defined(MAGIC_ENUM_USING_ALIAS_OPTIONAL) +MAGIC_ENUM_USING_ALIAS_OPTIONAL #else - : std::false_type {}; +using std::optional; #endif - - template - inline constexpr bool is_enum_v = std::is_enum_v && std::is_same_v>; - - template - struct static_string { - constexpr explicit static_string(std::string_view str) noexcept : static_string{str, std::make_index_sequence{}} { - assert(str.size() == N); - } - - constexpr const char* data() const noexcept { return chars.data(); } - - constexpr std::size_t size() const noexcept { return chars.size(); } - - constexpr operator std::string_view() const noexcept { return {data(), size()}; } - - private: - template - constexpr static_string(std::string_view str, std::index_sequence) noexcept : chars{{str[I]...}} {} - - const std::array chars; - }; - - template <> - struct static_string<0> { - constexpr explicit static_string(std::string_view) noexcept {} - - constexpr const char* data() const noexcept { return nullptr; } - - constexpr std::size_t size() const noexcept { return 0; } - - constexpr operator std::string_view() const noexcept { return {}; } - }; - - constexpr std::string_view pretty_name(std::string_view name) noexcept { - for (std::size_t i = name.size(); i > 0; --i) { - if (!((name[i - 1] >= '0' && name[i - 1] <= '9') || - (name[i - 1] >= 'a' && name[i - 1] <= 'z') || - (name[i - 1] >= 'A' && name[i - 1] <= 'Z') || - (name[i - 1] == '_'))) { - name.remove_prefix(i); - break; - } - } - - if (name.size() > 0 && ((name.front() >= 'a' && name.front() <= 'z') || - (name.front() >= 'A' && name.front() <= 'Z') || - (name.front() == '_'))) { - return name; - } - - return {}; // Invalid name. - } - - template - constexpr bool mixed_sign_less(L lhs, R rhs) noexcept { - static_assert(std::is_integral_v && std::is_integral_v, "magic_enum::detail::mixed_sign_less requires integral type."); - - if constexpr (std::is_signed_v && std::is_unsigned_v) { - // If 'left' is negative, then result is 'true', otherwise cast & compare. - return lhs < 0 || static_cast>(lhs) < rhs; - } else if constexpr (std::is_unsigned_v && std::is_signed_v) { - // If 'right' is negative, then result is 'false', otherwise cast & compare. - return rhs >= 0 && lhs < static_cast>(rhs); - } else { - // If same signedness (both signed or both unsigned). - return lhs < rhs; - } - } - - template - constexpr auto n() noexcept { - static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); + +// If need another string_view type, define the macro MAGIC_ENUM_USING_ALIAS_STRING_VIEW. +#if defined(MAGIC_ENUM_USING_ALIAS_STRING_VIEW) +MAGIC_ENUM_USING_ALIAS_STRING_VIEW +#else +using std::string_view; +#endif + +// If need another string type, define the macro MAGIC_ENUM_USING_ALIAS_STRING. +#if defined(MAGIC_ENUM_USING_ALIAS_STRING) +MAGIC_ENUM_USING_ALIAS_STRING +#else +using std::string; +#endif + +namespace customize { + +// Enum value must be in range [MAGIC_ENUM_RANGE_MIN, MAGIC_ENUM_RANGE_MAX]. By default MAGIC_ENUM_RANGE_MIN = -128, MAGIC_ENUM_RANGE_MAX = 128. +// If need another range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN and MAGIC_ENUM_RANGE_MAX. +// If need another range for specific enum type, add specialization enum_range for necessary enum type. +template +struct enum_range { + static_assert(std::is_enum_v, "magic_enum::customize::enum_range requires enum type."); + inline static constexpr int min = MAGIC_ENUM_RANGE_MIN; + inline static constexpr int max = MAGIC_ENUM_RANGE_MAX; + static_assert(max > min, "magic_enum::customize::enum_range requires max > min."); +}; + +static_assert(MAGIC_ENUM_RANGE_MIN <= 0, "MAGIC_ENUM_RANGE_MIN must be less or equals than 0."); +static_assert(MAGIC_ENUM_RANGE_MIN > (std::numeric_limits::min)(), "MAGIC_ENUM_RANGE_MIN must be greater than INT16_MIN."); + +static_assert(MAGIC_ENUM_RANGE_MAX > 0, "MAGIC_ENUM_RANGE_MAX must be greater than 0."); +static_assert(MAGIC_ENUM_RANGE_MAX < (std::numeric_limits::max)(), "MAGIC_ENUM_RANGE_MAX must be less than INT16_MAX."); + +static_assert(MAGIC_ENUM_RANGE_MAX > MAGIC_ENUM_RANGE_MIN, "MAGIC_ENUM_RANGE_MAX must be greater than MAGIC_ENUM_RANGE_MIN."); + +// If need custom names for enum, add specialization enum_name for necessary enum type. +template +constexpr string_view enum_name(E) noexcept { + static_assert(std::is_enum_v, "magic_enum::customize::enum_name requires enum type."); + + return {}; +} + +} // namespace magic_enum::customize + +namespace detail { + +template +struct supported +#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED || defined(MAGIC_ENUM_NO_CHECK_SUPPORT) + : std::true_type {}; +#else + : std::false_type {}; +#endif + +struct char_equal_to { + constexpr bool operator()(char lhs, char rhs) const noexcept { + return lhs == rhs; + } +}; + +template +class static_string { + public: + constexpr explicit static_string(string_view str) noexcept : static_string{str, std::make_index_sequence{}} { + assert(str.size() == N); + } + + constexpr const char* data() const noexcept { return chars_; } + + constexpr std::size_t size() const noexcept { return N; } + + constexpr operator string_view() const noexcept { return {data(), size()}; } + + private: + template + constexpr static_string(string_view str, std::index_sequence) noexcept : chars_{str[I]..., '\0'} {} + + char chars_[N + 1]; +}; + +template <> +class static_string<0> { + public: + constexpr explicit static_string(string_view) noexcept {} + + constexpr const char* data() const noexcept { return nullptr; } + + constexpr std::size_t size() const noexcept { return 0; } + + constexpr operator string_view() const noexcept { return {}; } +}; + +constexpr string_view pretty_name(string_view name) noexcept { + for (std::size_t i = name.size(); i > 0; --i) { + if (!((name[i - 1] >= '0' && name[i - 1] <= '9') || + (name[i - 1] >= 'a' && name[i - 1] <= 'z') || + (name[i - 1] >= 'A' && name[i - 1] <= 'Z') || + (name[i - 1] == '_'))) { + name.remove_prefix(i); + break; + } + } + + if (name.size() > 0 && ((name.front() >= 'a' && name.front() <= 'z') || + (name.front() >= 'A' && name.front() <= 'Z') || + (name.front() == '_'))) { + return name; + } + + return {}; // Invalid name. +} + +constexpr std::size_t find(string_view str, char c) noexcept { +#if defined(__clang__) && __clang_major__ < 9 && defined(__GLIBCXX__) || defined(_MSC_VER) && _MSC_VER < 1920 && !defined(__clang__) +// https://stackoverflow.com/questions/56484834/constexpr-stdstring-viewfind-last-of-doesnt-work-on-clang-8-with-libstdc +// https://developercommunity.visualstudio.com/content/problem/360432/vs20178-regression-c-failed-in-test.html + constexpr bool workaround = true; +#else + constexpr bool workaround = false; +#endif + if constexpr (workaround) { + for (std::size_t i = 0; i < str.size(); ++i) { + if (str[i] == c) { + return i; + } + } + + return string_view::npos; + } else { + return str.find_first_of(c); + } +} + +template +constexpr std::array, N> to_array(T (&a)[N], std::index_sequence) { + return {{a[I]...}}; +} + +template +constexpr bool cmp_equal(string_view lhs, string_view rhs, BinaryPredicate&& p) noexcept(std::is_nothrow_invocable_r_v) { +#if defined(_MSC_VER) && _MSC_VER < 1920 && !defined(__clang__) + // https://developercommunity.visualstudio.com/content/problem/360432/vs20178-regression-c-failed-in-test.html + // https://developercommunity.visualstudio.com/content/problem/232218/c-constexpr-string-view.html + constexpr bool workaround = true; +#else + constexpr bool workaround = false; +#endif + constexpr bool default_predicate = std::is_same_v, char_equal_to>; + + if constexpr (default_predicate && !workaround) { + static_cast(p); + return lhs == rhs; + } else { + if (lhs.size() != rhs.size()) { + return false; + } + + const auto size = lhs.size(); + for (std::size_t i = 0; i < size; ++i) { + if (!p(lhs[i], rhs[i])) { + return false; + } + } + + return true; + } +} + +template +constexpr bool cmp_less(L lhs, R rhs) noexcept { + static_assert(std::is_integral_v && std::is_integral_v, "magic_enum::detail::cmp_less requires integral type."); + + if constexpr (std::is_signed_v == std::is_signed_v) { + // If same signedness (both signed or both unsigned). + return lhs < rhs; + } else if constexpr (std::is_signed_v) { + // If 'right' is negative, then result is 'false', otherwise cast & compare. + return rhs > 0 && lhs < static_cast>(rhs); + } else { + // If 'left' is negative, then result is 'true', otherwise cast & compare. + return lhs < 0 || static_cast>(lhs) < rhs; + } +} + +template +constexpr I log2(I value) noexcept { + static_assert(std::is_integral_v, "magic_enum::detail::log2 requires integral type."); + + auto ret = I{0}; + for (; value > I{1}; value >>= I{1}, ++ret) {} + + return ret; +} + +template +constexpr bool is_pow2(I x) noexcept { + static_assert(std::is_integral_v, "magic_enum::detail::is_pow2 requires integral type."); + + return x != 0 && (x & (x - 1)) == 0; +} + +template +inline constexpr bool is_enum_v = std::is_enum_v && std::is_same_v>; + +template +constexpr auto n() noexcept { + static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); #if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED # if defined(__clang__) - constexpr std::string_view name{__PRETTY_FUNCTION__ + 34, sizeof(__PRETTY_FUNCTION__) - 36}; + constexpr string_view name{__PRETTY_FUNCTION__ + 34, sizeof(__PRETTY_FUNCTION__) - 36}; # elif defined(__GNUC__) - constexpr std::string_view name{__PRETTY_FUNCTION__ + 49, sizeof(__PRETTY_FUNCTION__) - 51}; + constexpr string_view name{__PRETTY_FUNCTION__ + 49, sizeof(__PRETTY_FUNCTION__) - 51}; # elif defined(_MSC_VER) - constexpr std::string_view name{__FUNCSIG__ + 40, sizeof(__FUNCSIG__) - 57}; + constexpr string_view name{__FUNCSIG__ + 40, sizeof(__FUNCSIG__) - 57}; # endif - return static_string{name}; + return static_string{name}; #else - static_assert(supported::value, "magic_enum: Unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); - return std::string_view{}; // Unsupported compiler. + return string_view{}; // Unsupported compiler. #endif - } - - template - inline constexpr auto type_name_v = n(); - - template - constexpr auto n() noexcept { - static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); +} + +template +inline constexpr auto type_name_v = n(); + +template +constexpr auto n() noexcept { + static_assert(is_enum_v, "magic_enum::detail::n requires enum type."); + constexpr auto custom_name = customize::enum_name(V); + + if constexpr (custom_name.empty()) { + static_cast(custom_name); #if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED # if defined(__clang__) || defined(__GNUC__) - constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}); + constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2}); # elif defined(_MSC_VER) - constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17}); + constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17}); # endif - return static_string{name}; + return static_string{name}; #else - static_assert(supported::value, "magic_enum: Unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); - return std::string_view{}; // Unsupported compiler. + return string_view{}; // Unsupported compiler. #endif - } - - template - inline constexpr auto name_v = n(); - - template - constexpr int reflected_min() noexcept { - static_assert(is_enum_v, "magic_enum::detail::reflected_min requires enum type."); - constexpr auto lhs = enum_range::min; - static_assert(lhs > (std::numeric_limits::min)(), "magic_enum::enum_range requires min must be greater than INT16_MIN."); - constexpr auto rhs = (std::numeric_limits>::min)(); - - return mixed_sign_less(lhs, rhs) ? rhs : lhs; - } - - template - constexpr int reflected_max() noexcept { - static_assert(is_enum_v, "magic_enum::detail::reflected_max requires enum type."); - constexpr auto lhs = enum_range::max; - static_assert(lhs < (std::numeric_limits::max)(), "magic_enum::enum_range requires max must be less than INT16_MAX."); - constexpr auto rhs = (std::numeric_limits>::max)(); - - return mixed_sign_less(lhs, rhs) ? lhs : rhs; - } - - template - inline constexpr int reflected_min_v = reflected_min(); - - template - inline constexpr int reflected_max_v = reflected_max(); - - template - constexpr std::size_t reflected_size() noexcept { - static_assert(is_enum_v, "magic_enum::detail::reflected_size requires enum type."); - static_assert(reflected_max_v > reflected_min_v, "magic_enum::enum_range requires max > min."); - constexpr auto size = reflected_max_v - reflected_min_v + 1; - static_assert(size > 0, "magic_enum::enum_range requires valid size."); - static_assert(size < (std::numeric_limits::max)(), "magic_enum::enum_range requires valid size."); - - return static_cast(size); - } - - template - constexpr auto values(std::integer_sequence) noexcept { - static_assert(is_enum_v, "magic_enum::detail::values requires enum type."); - constexpr std::array valid{{(n(I + reflected_min_v)>().size() != 0)...}}; - constexpr int count = ((valid[I] ? 1 : 0) + ...); - - std::array values{}; - for (int i = 0, v = 0; v < count; ++i) { - if (valid[i]) { - values[v++] = static_cast(i + reflected_min_v); - } - } - - return values; - } - - template - inline constexpr auto values_v = values(std::make_integer_sequence()>{}); - - template - inline constexpr std::size_t count_v = values_v.size(); - - template - inline constexpr int min_v = values_v.empty() ? 0 : static_cast(values_v.front()); - - template - inline constexpr int max_v = values_v.empty() ? 0 : static_cast(values_v.back()); - - template - constexpr std::size_t range_size() noexcept { - static_assert(is_enum_v, "magic_enum::detail::range_size requires enum type."); - constexpr auto size = max_v - min_v + 1; - static_assert(size > 0, "magic_enum::enum_range requires valid size."); - static_assert(size < (std::numeric_limits::max)(), "magic_enum::enum_range requires valid size."); - - return static_cast(size); - } - - template - inline constexpr std::size_t range_size_v = range_size(); - - template - using index_t = std::conditional_t < (std::numeric_limits::max)(), std::uint8_t, std::uint16_t>; - - template - inline constexpr auto invalid_index_v = (std::numeric_limits>::max)(); - - template - constexpr auto indexes(std::integer_sequence) noexcept { - static_assert(is_enum_v, "magic_enum::detail::indexes requires enum type."); - index_t i = 0; - - return std::array, sizeof...(I)>{{((n(I + min_v)>().size() != 0) ? i++ : invalid_index_v)...}}; - } - - template - constexpr auto names(std::index_sequence) noexcept { - static_assert(is_enum_v, "magic_enum::detail::names requires enum type."); - - return std::array{{name_v[I]>...}}; - } - - template - constexpr auto entries(std::index_sequence) noexcept { - static_assert(is_enum_v, "magic_enum::detail::entries requires enum type."); - - return std::array, sizeof...(I)>{{{values_v[I], name_v[I]>}...}}; - } - - template - using enable_if_enum_t = std::enable_if_t>, R>; - - template >>> - using enum_concept = T; - - template > - struct is_scoped_enum : std::false_type {}; - - template - struct is_scoped_enum : std::bool_constant>> {}; - - template > - struct is_unscoped_enum : std::false_type {}; - - template - struct is_unscoped_enum : std::bool_constant>> {}; - - template >> - struct underlying_type {}; - - template - struct underlying_type : std::underlying_type> {}; - - template - struct enum_traits {}; - - template - struct enum_traits>> { - using type = E; - using underlying_type = typename detail::underlying_type::type; - - inline static constexpr std::string_view type_name = detail::type_name_v; - - inline static constexpr bool is_unscoped = detail::is_unscoped_enum::value; - inline static constexpr bool is_scoped = detail::is_scoped_enum::value; - inline static constexpr bool is_dense = detail::range_size_v == detail::count_v; - inline static constexpr bool is_sparse = detail::range_size_v != detail::count_v; - - inline static constexpr std::size_t count = detail::count_v; - inline static constexpr std::array values = detail::values_v; - inline static constexpr std::array names = detail::names(std::make_index_sequence>{}); - inline static constexpr std::array, count> entries = detail::entries(std::make_index_sequence>{}); - - [[nodiscard]] static constexpr bool reflected(E value) noexcept { - return static_cast(value) >= static_cast(reflected_min_v) && static_cast(value) <= static_cast(reflected_max_v); - } - - [[nodiscard]] static constexpr int index(E value) noexcept { - if (static_cast(value) >= static_cast(min_v) && static_cast(value) <= static_cast(max_v)) { - if constexpr (is_sparse) { - if (const auto i = indexes[static_cast(value) - min_v]; i != invalid_index_v) { - return i; - } - } else { - return static_cast(value) - min_v; - } - } - - return -1; // Value out of range. - } - - [[nodiscard]] static constexpr E value(std::size_t index) noexcept { - if constexpr (is_sparse) { - return assert(index < count), values[index]; - } else { - return assert(index < count), static_cast(index + min_v); - } - } - - [[nodiscard]] static constexpr std::string_view name(E value) noexcept { - if (const auto i = index(value); i != -1) { - return names[i]; - } - - return {}; // Value out of range. - } - - private: - static_assert(is_enum_v, "magic_enum::enum_traits requires enum type."); - static_assert(count > 0, "magic_enum::enum_range requires enum implementation or valid max and min."); - using U = underlying_type; - inline static constexpr auto indexes = detail::indexes(std::make_integer_sequence>{}); - }; - - } // namespace magic_enum::detail - - // Checks is magic_enum supported compiler. - inline constexpr bool is_magic_enum_supported = detail::supported::value; - - template - using Enum = detail::enum_concept; - - // Checks whether T is an Unscoped enumeration type. - // Provides the member constant value which is equal to true, if T is an [Unscoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Unscoped_enumeration) type. Otherwise, value is equal to false. - template - struct is_unscoped_enum : detail::is_unscoped_enum {}; - - template - inline constexpr bool is_unscoped_enum_v = is_unscoped_enum::value; - - // Checks whether T is an Scoped enumeration type. - // Provides the member constant value which is equal to true, if T is an [Scoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations) type. Otherwise, value is equal to false. - template - struct is_scoped_enum : detail::is_scoped_enum {}; - - template - inline constexpr bool is_scoped_enum_v = is_scoped_enum::value; - - // If T is a complete enumeration type, provides a member typedef type that names the underlying type of T. - // Otherwise, if T is not an enumeration type, there is no member type. Otherwise (T is an incomplete enumeration type), the program is ill-formed. - template - struct underlying_type : detail::underlying_type {}; - - template - using underlying_type_t = typename underlying_type::type; - - // Enum traits defines a compile-time template-based interface to query the properties of enum. - template - using enum_traits = detail::enum_traits>; - - // Obtains enum value from enum string name. - // Returns std::optional with enum value. - template - [[nodiscard]] constexpr auto enum_cast(std::string_view value) noexcept -> detail::enable_if_enum_t>> { - using D = std::decay_t; - - if constexpr (detail::range_size_v > detail::count_v * 2) { - for (std::size_t i = 0; i < enum_traits::count; ++i) { - if (value == enum_traits::names[i]) { - return enum_traits::values[i]; - } - } - } else { - for (auto i = detail::min_v; i <= detail::max_v; ++i) { - if (value == enum_traits::name(static_cast(i))) { - return static_cast(i); - } - } - } - - return std::nullopt; // Invalid value or out of range. + } else { + return static_string{custom_name}; + } +} + +template +inline constexpr auto enum_name_v = n(); + +template +constexpr bool is_valid() noexcept { + static_assert(is_enum_v, "magic_enum::detail::is_valid requires enum type."); + + return n(V)>().size() != 0; +} + +template > +constexpr E value(std::size_t i) noexcept { + static_assert(is_enum_v, "magic_enum::detail::value requires enum type."); + + if constexpr (IsFlags) { + return static_cast(U{1} << static_cast(static_cast(i) + O)); + } else { + return static_cast(static_cast(i) + O); + } +} + +template > +constexpr int reflected_min() noexcept { + static_assert(is_enum_v, "magic_enum::detail::reflected_min requires enum type."); + + if constexpr (IsFlags) { + return 0; + } else { + constexpr auto lhs = customize::enum_range::min; + static_assert(lhs > (std::numeric_limits::min)(), "magic_enum::enum_range requires min must be greater than INT16_MIN."); + constexpr auto rhs = (std::numeric_limits::min)(); + + if constexpr (cmp_less(lhs, rhs)) { + return rhs; + } else { + static_assert(!is_valid(0)>(), "magic_enum::enum_range detects enum value smaller than min range size."); + return lhs; } - - // Obtains enum value from integer value. - // Returns std::optional with enum value. - template - [[nodiscard]] constexpr auto enum_cast(underlying_type_t value) noexcept -> detail::enable_if_enum_t>> { - using D = std::decay_t; - - if (enum_traits::index(static_cast(value)) != -1) { - return static_cast(value); - } - - return std::nullopt; // Invalid value or out of range. + } +} + +template > +constexpr int reflected_max() noexcept { + static_assert(is_enum_v, "magic_enum::detail::reflected_max requires enum type."); + + if constexpr (IsFlags) { + return std::numeric_limits::digits - 1; + } else { + constexpr auto lhs = customize::enum_range::max; + static_assert(lhs < (std::numeric_limits::max)(), "magic_enum::enum_range requires max must be less than INT16_MAX."); + constexpr auto rhs = (std::numeric_limits::max)(); + + if constexpr (cmp_less(lhs, rhs)) { + static_assert(!is_valid(0)>(), "magic_enum::enum_range detects enum value larger than max range size."); + return lhs; + } else { + return rhs; } - - // Returns integer value from enum value. - template - [[nodiscard]] constexpr auto enum_integer(E value) noexcept -> detail::enable_if_enum_t> { - return static_cast>(value); + } +} + +template +inline constexpr auto reflected_min_v = reflected_min(); + +template +inline constexpr auto reflected_max_v = reflected_max(); + +template +constexpr std::size_t values_count(const bool (&valid)[N]) noexcept { + auto count = std::size_t{0}; + for (std::size_t i = 0; i < N; ++i) { + if (valid[i]) { + ++count; } - - // Obtains index in enum value sequence from enum value. - // Returns std::optional with index. - template - [[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_enum_t> { - if (const auto i = enum_traits::index(value); i != -1) { - return i; - } - - return std::nullopt; // Value out of range. + } + + return count; +} + +template +constexpr auto values(std::index_sequence) noexcept { + static_assert(is_enum_v, "magic_enum::detail::values requires enum type."); + constexpr bool valid[sizeof...(I)] = {is_valid(I)>()...}; + constexpr std::size_t count = values_count(valid); + + if constexpr (count > 0) { + E values[count] = {}; + for (std::size_t i = 0, v = 0; v < count; ++i) { + if (valid[i]) { + values[v++] = value(i); + } } - - // Returns enum value at specified index. - // No bounds checking is performed: the behavior is undefined if index >= number of enum values. - template - [[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_enum_t> { - return enum_traits::value(index); + + return to_array(values, std::make_index_sequence{}); + } else { + return std::array{}; + } +} + +template > +constexpr auto values() noexcept { + static_assert(is_enum_v, "magic_enum::detail::values requires enum type."); + constexpr auto min = reflected_min_v; + constexpr auto max = reflected_max_v; + constexpr auto range_size = max - min + 1; + static_assert(range_size > 0, "magic_enum::enum_range requires valid size."); + static_assert(range_size < (std::numeric_limits::max)(), "magic_enum::enum_range requires valid size."); + + return values>(std::make_index_sequence{}); +} + +template +inline constexpr auto values_v = values(); + +template > +using values_t = decltype((values_v)); + +template +inline constexpr auto count_v = values_v.size(); + +template > +inline constexpr auto min_v = (count_v > 0) ? static_cast(values_v.front()) : U{0}; + +template > +inline constexpr auto max_v = (count_v > 0) ? static_cast(values_v.back()) : U{0}; + +template > +constexpr std::size_t range_size() noexcept { + static_assert(is_enum_v, "magic_enum::detail::range_size requires enum type."); + constexpr auto max = IsFlags ? log2(max_v) : max_v; + constexpr auto min = IsFlags ? log2(min_v) : min_v; + constexpr auto range_size = max - min + U{1}; + static_assert(range_size > 0, "magic_enum::enum_range requires valid size."); + static_assert(range_size < (std::numeric_limits::max)(), "magic_enum::enum_range requires valid size."); + + return static_cast(range_size); +} + +template +inline constexpr auto range_size_v = range_size(); + +template +using index_t = std::conditional_t < (std::numeric_limits::max)(), std::uint8_t, std::uint16_t>; + +template +inline constexpr auto invalid_index_v = (std::numeric_limits>::max)(); + +template +constexpr auto indexes(std::index_sequence) noexcept { + static_assert(is_enum_v, "magic_enum::detail::indexes requires enum type."); + constexpr auto min = IsFlags ? log2(min_v) : min_v; + [[maybe_unused]] auto i = index_t{0}; + + return std::array{{(is_valid(I)>() ? i++ : invalid_index_v)...}}; +} + +template +inline constexpr auto indexes_v = indexes(std::make_index_sequence>{}); + +template +constexpr auto names(std::index_sequence) noexcept { + static_assert(is_enum_v, "magic_enum::detail::names requires enum type."); + + return std::array{{enum_name_v[I]>...}}; +} + +template +inline constexpr auto names_v = names(std::make_index_sequence>{}); + +template > +using names_t = decltype((names_v)); + +template +constexpr auto entries(std::index_sequence) noexcept { + static_assert(is_enum_v, "magic_enum::detail::entries requires enum type."); + + return std::array, sizeof...(I)>{{{values_v[I], enum_name_v[I]>}...}}; +} + +template +inline constexpr auto entries_v = entries(std::make_index_sequence>{}); + +template > +using entries_t = decltype((entries_v)); + +template > +constexpr bool is_sparse() noexcept { + static_assert(is_enum_v, "magic_enum::detail::is_sparse requires enum type."); + + return range_size_v != count_v; +} + +template +inline constexpr bool is_sparse_v = is_sparse(); + +template > +constexpr std::size_t undex(U value) noexcept { + static_assert(is_enum_v, "magic_enum::detail::undex requires enum type."); + + if (const auto i = static_cast(value - min_v); value >= min_v && value <= max_v) { + if constexpr (is_sparse_v) { + if (const auto idx = indexes_v[i]; idx != invalid_index_v) { + return idx; + } + } else { + return i; } - - // Obtains value enum sequence. - // Returns std::array with enum values, sorted by enum value. - template - [[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_enum_t::values)&> { - return enum_traits::values; + } + + return invalid_index_v; // Value out of range. +} + +template > +constexpr std::size_t endex(E value) noexcept { + static_assert(is_enum_v, "magic_enum::detail::endex requires enum type."); + + return undex(static_cast(value)); +} + +template > +constexpr U value_ors() noexcept { + static_assert(is_enum_v, "magic_enum::detail::endex requires enum type."); + + auto value = U{0}; + for (std::size_t i = 0; i < count_v; ++i) { + value |= static_cast(values_v[i]); + } + + return value; +} + +template +struct enable_if_enum {}; + +template +struct enable_if_enum { + using type = R; + using D = std::decay_t; + static_assert(supported::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility)."); +}; + +template +using enable_if_enum_t = std::enable_if_t>, R>; + +template >>> +using enum_concept = T; + +template > +struct is_scoped_enum : std::false_type {}; + +template +struct is_scoped_enum : std::bool_constant>> {}; + +template > +struct is_unscoped_enum : std::false_type {}; + +template +struct is_unscoped_enum : std::bool_constant>> {}; + +template >> +struct underlying_type {}; + +template +struct underlying_type : std::underlying_type> {}; + +} // namespace magic_enum::detail + +// Checks is magic_enum supported compiler. +inline constexpr bool is_magic_enum_supported = detail::supported::value; + +template +using Enum = detail::enum_concept; + +// Checks whether T is an Unscoped enumeration type. +// Provides the member constant value which is equal to true, if T is an [Unscoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Unscoped_enumeration) type. Otherwise, value is equal to false. +template +struct is_unscoped_enum : detail::is_unscoped_enum {}; + +template +inline constexpr bool is_unscoped_enum_v = is_unscoped_enum::value; + +// Checks whether T is an Scoped enumeration type. +// Provides the member constant value which is equal to true, if T is an [Scoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations) type. Otherwise, value is equal to false. +template +struct is_scoped_enum : detail::is_scoped_enum {}; + +template +inline constexpr bool is_scoped_enum_v = is_scoped_enum::value; + +// If T is a complete enumeration type, provides a member typedef type that names the underlying type of T. +// Otherwise, if T is not an enumeration type, there is no member type. Otherwise (T is an incomplete enumeration type), the program is ill-formed. +template +struct underlying_type : detail::underlying_type {}; + +template +using underlying_type_t = typename underlying_type::type; + +// Returns type name of enum. +template +[[nodiscard]] constexpr auto enum_type_name() noexcept -> detail::enable_if_enum_t { + using D = std::decay_t; + constexpr string_view name = detail::type_name_v; + static_assert(name.size() > 0, "Enum type does not have a name."); + + return name; +} + +// Returns number of enum values. +template +[[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_enum_t { + using D = std::decay_t; + + return detail::count_v; +} + +// Returns enum value at specified index. +// No bounds checking is performed: the behavior is undefined if index >= number of enum values. +template +[[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_enum_t> { + using D = std::decay_t; + static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); + + if constexpr (detail::is_sparse_v) { + return assert((index < detail::count_v)), detail::values_v[index]; + } else { + return assert((index < detail::count_v)), detail::value>(index); + } +} + +// Returns std::array with enum values, sorted by enum value. +template +[[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_enum_t> { + using D = std::decay_t; + static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); + + return detail::values_v; +} + +// Returns name from static storage enum variable. +// This version is much lighter on the compile times and is not restricted to the enum_range limitation. +template +[[nodiscard]] constexpr auto enum_name() noexcept -> detail::enable_if_enum_t { + using D = std::decay_t; + constexpr string_view name = detail::enum_name_v; + static_assert(name.size() > 0, "Enum value does not have a name."); + + return name; +} + +// Returns name from enum value. +// If enum value does not have name or value out of range, returns empty string. +template +[[nodiscard]] constexpr auto enum_name(E value) noexcept -> detail::enable_if_enum_t { + using D = std::decay_t; + + if (const auto i = detail::endex(value); i != detail::invalid_index_v) { + return detail::names_v[i]; + } + + return {}; // Invalid value or out of range. +} + +// Returns std::array with names, sorted by enum value. +template +[[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_enum_t> { + using D = std::decay_t; + static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); + + return detail::names_v; +} + +// Returns std::array with pairs (value, name), sorted by enum value. +template +[[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_enum_t> { + using D = std::decay_t; + static_assert(detail::count_v > 0, "magic_enum requires enum implementation and valid max and min."); + + return detail::entries_v; +} + +// Obtains enum value from integer value. +// Returns optional with enum value. +template +[[nodiscard]] constexpr auto enum_cast(underlying_type_t value) noexcept -> detail::enable_if_enum_t>> { + using D = std::decay_t; + + if (detail::undex(value) != detail::invalid_index_v) { + return static_cast(value); + } + + return {}; // Invalid value or out of range. +} + +// Obtains enum value from name. +// Returns optional with enum value. +template +[[nodiscard]] constexpr auto enum_cast(string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v) -> detail::enable_if_enum_t>> { + static_assert(std::is_invocable_r_v, "magic_enum::enum_cast requires bool(char, char) invocable predicate."); + using D = std::decay_t; + + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (detail::cmp_equal(value, detail::names_v[i], p)) { + return enum_value(i); } - - // Returns number of enum values. - template - [[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_enum_t { - return enum_traits::count; + } + + return {}; // Invalid value or out of range. +} + +// Obtains enum value from name. +// Returns optional with enum value. +template +[[nodiscard]] constexpr auto enum_cast(string_view value) noexcept -> detail::enable_if_enum_t>> { + using D = std::decay_t; + + return enum_cast(value, detail::char_equal_to{}); +} + +// Returns integer value from enum value. +template +[[nodiscard]] constexpr auto enum_integer(E value) noexcept -> detail::enable_if_enum_t> { + return static_cast>(value); +} + +// Obtains index in enum values from enum value. +// Returns optional with index. +template +[[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_enum_t> { + using D = std::decay_t; + + if (const auto i = detail::endex(value); i != detail::invalid_index_v) { + return i; + } + + return {}; // Invalid value or out of range. +} + +// Checks whether enum contains enumerator with such enum value. +template +[[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_enum_t { + using D = std::decay_t; + + return detail::endex(value) != detail::invalid_index_v; +} + +// Checks whether enum contains enumerator with such integer value. +template +[[nodiscard]] constexpr auto enum_contains(underlying_type_t value) noexcept -> detail::enable_if_enum_t { + using D = std::decay_t; + + return detail::undex(value) != detail::invalid_index_v; +} + +// Checks whether enum contains enumerator with such name. +template +[[nodiscard]] constexpr auto enum_contains(string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v) -> detail::enable_if_enum_t { + static_assert(std::is_invocable_r_v, "magic_enum::enum_contains requires bool(char, char) invocable predicate."); + using D = std::decay_t; + + return enum_cast(value, std::move_if_noexcept(p)).has_value(); +} + +// Checks whether enum contains enumerator with such name. +template +[[nodiscard]] constexpr auto enum_contains(string_view value) noexcept -> detail::enable_if_enum_t { + using D = std::decay_t; + + return enum_cast(value).has_value(); +} + +namespace ostream_operators { + +template , int> = 0> +std::basic_ostream& operator<<(std::basic_ostream& os, E value) { + using D = std::decay_t; + using U = underlying_type_t; +#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED + if (const auto name = magic_enum::enum_name(value); !name.empty()) { + for (const auto c : name) { + os.put(c); } - - // Returns string enum name from static storage enum variable. - // This version is much lighter on the compile times and is not restricted to the enum_range limitation. - template - [[nodiscard]] constexpr auto enum_name() noexcept -> detail::enable_if_enum_t { - constexpr std::string_view name = detail::name_v, V>; - static_assert(name.size() > 0, "Enum value does not have a name."); - - return name; + return os; + } +#endif + return (os << static_cast(value)); +} + +template , int> = 0> +std::basic_ostream& operator<<(std::basic_ostream& os, optional value) { + return value.has_value() ? (os << value.value()) : os; +} + +} // namespace magic_enum::ostream_operators + +namespace bitwise_operators { + +template , int> = 0> +constexpr E operator~(E rhs) noexcept { + return static_cast(~static_cast>(rhs)); +} + +template , int> = 0> +constexpr E operator|(E lhs, E rhs) noexcept { + return static_cast(static_cast>(lhs) | static_cast>(rhs)); +} + +template , int> = 0> +constexpr E operator&(E lhs, E rhs) noexcept { + return static_cast(static_cast>(lhs) & static_cast>(rhs)); +} + +template , int> = 0> +constexpr E operator^(E lhs, E rhs) noexcept { + return static_cast(static_cast>(lhs) ^ static_cast>(rhs)); +} + +template , int> = 0> +constexpr E& operator|=(E& lhs, E rhs) noexcept { + return lhs = (lhs | rhs); +} + +template , int> = 0> +constexpr E& operator&=(E& lhs, E rhs) noexcept { + return lhs = (lhs & rhs); +} + +template , int> = 0> +constexpr E& operator^=(E& lhs, E rhs) noexcept { + return lhs = (lhs ^ rhs); +} + +} // namespace magic_enum::bitwise_operators + +namespace flags { + +// Returns type name of enum. +using magic_enum::enum_type_name; + +// Returns number of enum-flags values. +template +[[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_enum_t { + using D = std::decay_t; + + return detail::count_v; +} + +// Returns enum-flags value at specified index. +// No bounds checking is performed: the behavior is undefined if index >= number of enum-flags values. +template +[[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_enum_t> { + using D = std::decay_t; + static_assert(detail::count_v > 0, "magic_enum::flags requires enum-flags implementation."); + + if constexpr (detail::is_sparse_v) { + return assert((index < detail::count_v)), detail::values_v[index]; + } else { + constexpr auto min = detail::log2(detail::min_v); + + return assert((index < detail::count_v)), detail::value(index); + } +} + +// Returns std::array with enum-flags values, sorted by enum-flags value. +template +[[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_enum_t> { + using D = std::decay_t; + static_assert(detail::count_v > 0, "magic_enum::flags requires enum-flags implementation."); + + return detail::values_v; +} + +// Returns name from enum-flags value. +// If enum-flags value does not have name or value out of range, returns empty string. +template +[[nodiscard]] auto enum_name(E value) -> detail::enable_if_enum_t { + using D = std::decay_t; + using U = underlying_type_t; + + string name; + auto check_value = U{0}; + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (const auto v = static_cast(enum_value(i)); (static_cast(value) & v) != 0) { + check_value |= v; + const auto n = detail::names_v[i]; + if (!name.empty()) { + name.append(1, '|'); + } + name.append(n.data(), n.size()); } - - // Returns string enum name from enum value. - // If enum value does not have name or value out of range, returns empty string. - template - [[nodiscard]] constexpr auto enum_name(E value) noexcept -> detail::enable_if_enum_t { - return enum_traits::name(value); + } + + if (check_value != 0 && check_value == static_cast(value)) { + return name; + } + + return {}; // Invalid value or out of range. +} + +// Returns std::array with string names, sorted by enum-flags value. +template +[[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_enum_t> { + using D = std::decay_t; + static_assert(detail::count_v > 0, "magic_enum::flags requires enum-flags implementation."); + + return detail::names_v; +} + +// Returns std::array with pairs (value, name), sorted by enum-flags value. +template +[[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_enum_t> { + using D = std::decay_t; + static_assert(detail::count_v > 0, "magic_enum::flags requires enum-flags implementation."); + + return detail::entries_v; +} + +// Obtains enum-flags value from integer value. +// Returns optional with enum-flags value. +template +[[nodiscard]] constexpr auto enum_cast(underlying_type_t value) noexcept -> detail::enable_if_enum_t>> { + using D = std::decay_t; + using U = underlying_type_t; + + if constexpr (detail::is_sparse_v) { + auto check_value = U{0}; + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (const auto v = static_cast(enum_value(i)); (value & v) != 0) { + check_value |= v; + } } - - // Obtains string enum name sequence. - // Returns std::array with string enum names, sorted by enum value. - template - [[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_enum_t::names)&> { - return enum_traits::names; + + if (check_value != 0 && check_value == value) { + return static_cast(value); } - - // Obtains pair (value enum, string enum name) sequence. - // Returns std::array with std::pair (value enum, string enum name), sorted by enum value. - template - [[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_enum_t::entries)&> { - return enum_traits::entries; + } else { + constexpr auto min = detail::min_v; + constexpr auto max = detail::value_ors(); + + if (value >= min && value <= max) { + return static_cast(value); } - - namespace ostream_operators { - - template - auto operator<<(std::basic_ostream& os, E value) -> detail::enable_if_enum_t&> { - if (const auto name = enum_name(value); !name.empty()) { - for (const auto c : name) { - os.put(c); - } - } else { - os << enum_integer(value); - } - - return os; - } - - template - auto operator<<(std::basic_ostream& os, std::optional value) -> detail::enable_if_enum_t&> { - if (value.has_value()) { - os << value.value(); - } - - return os; - } - - } // namespace magic_enum::ostream_operators - - namespace bitwise_operators { - - template - constexpr auto operator~(E rhs) noexcept -> detail::enable_if_enum_t { - return static_cast(~static_cast>(rhs)); - } - - template - constexpr auto operator|(E lhs, E rhs) noexcept -> detail::enable_if_enum_t { - return static_cast(static_cast>(lhs) | static_cast>(rhs)); - } - - template - constexpr auto operator&(E lhs, E rhs) noexcept -> detail::enable_if_enum_t { - return static_cast(static_cast>(lhs) & static_cast>(rhs)); - } - - template - constexpr auto operator^(E lhs, E rhs) noexcept -> detail::enable_if_enum_t { - return static_cast(static_cast>(lhs) ^ static_cast>(rhs)); - } - - template - constexpr auto operator|=(E& lhs, E rhs) noexcept -> detail::enable_if_enum_t { - return lhs = lhs | rhs; - } - - template - constexpr auto operator&=(E& lhs, E rhs) noexcept -> detail::enable_if_enum_t { - return lhs = lhs & rhs; - } - - template - constexpr auto operator^=(E& lhs, E rhs) noexcept -> detail::enable_if_enum_t { - return lhs = lhs ^ rhs; - } - - } // namespace magic_enum::bitwise_operators - + } + + return {}; // Invalid value or out of range. +} + +// Obtains enum-flags value from name. +// Returns optional with enum-flags value. +template +[[nodiscard]] constexpr auto enum_cast(string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v) -> detail::enable_if_enum_t>> { + static_assert(std::is_invocable_r_v, "magic_enum::flags::enum_cast requires bool(char, char) invocable predicate."); + using D = std::decay_t; + using U = underlying_type_t; + + auto result = U{0}; + while (!value.empty()) { + const auto d = detail::find(value, '|'); + const auto s = (d == string_view::npos) ? value : value.substr(0, d); + auto f = U{0}; + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (detail::cmp_equal(s, detail::names_v[i], p)) { + f = static_cast(enum_value(i)); + result |= f; + break; + } + } + if (f == U{0}) { + return {}; // Invalid value or out of range. + } + value.remove_prefix((d == string_view::npos) ? value.size() : d + 1); + } + + if (result == U{0}) { + return {}; // Invalid value or out of range. + } else { + return static_cast(result); + } +} + +// Obtains enum-flags value from name. +// Returns optional with enum-flags value. +template +[[nodiscard]] constexpr auto enum_cast(string_view value) noexcept -> detail::enable_if_enum_t>> { + using D = std::decay_t; + + return enum_cast(value, detail::char_equal_to{}); +} + +// Returns integer value from enum value. +using magic_enum::enum_integer; + +// Obtains index in enum-flags values from enum-flags value. +// Returns optional with index. +template +[[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_enum_t> { + using D = std::decay_t; + using U = underlying_type_t; + + if (detail::is_pow2(static_cast(value))) { + for (std::size_t i = 0; i < detail::count_v; ++i) { + if (enum_value(i) == value) { + return i; + } + } + } + + return {}; // Invalid value or out of range. +} + +// Checks whether enum-flags contains enumerator with such enum-flags value. +template +[[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_enum_t { + using D = std::decay_t; + using U = underlying_type_t; + + return enum_cast(static_cast(value)).has_value(); +} + +// Checks whether enum-flags contains enumerator with such integer value. +template +[[nodiscard]] constexpr auto enum_contains(underlying_type_t value) noexcept -> detail::enable_if_enum_t { + using D = std::decay_t; + + return enum_cast(value).has_value(); +} + +// Checks whether enum-flags contains enumerator with such name. +template +[[nodiscard]] constexpr auto enum_contains(string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v) -> detail::enable_if_enum_t { + static_assert(std::is_invocable_r_v, "magic_enum::flags::enum_contains requires bool(char, char) invocable predicate."); + using D = std::decay_t; + + return enum_cast(value, std::move_if_noexcept(p)).has_value(); +} + +// Checks whether enum-flags contains enumerator with such name. +template +[[nodiscard]] constexpr auto enum_contains(string_view value) noexcept -> detail::enable_if_enum_t { + using D = std::decay_t; + + return enum_cast(value).has_value(); +} + +} // namespace magic_enum::flags + +namespace flags::ostream_operators { + +template , int> = 0> +std::basic_ostream& operator<<(std::basic_ostream& os, E value) { + using D = std::decay_t; + using U = underlying_type_t; +#if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED + if (const auto name = magic_enum::flags::enum_name(value); !name.empty()) { + for (const auto c : name) { + os.put(c); + } + return os; + } +#endif + return (os << static_cast(value)); +} + +template , int> = 0> +std::basic_ostream& operator<<(std::basic_ostream& os, optional value) { + return value.has_value() ? (os << value.value()) : os; +} + +} // namespace magic_enum::flags::ostream_operators + +namespace flags::bitwise_operators { + +using namespace magic_enum::bitwise_operators; + +} // namespace magic_enum::flags::bitwise_operators + } // namespace magic_enum -#if defined(_MSC_VER) +#if defined(__clang__) +# pragma clang diagnostic pop +#elif defined(__GNUC__) +# pragma GCC diagnostic pop +#elif defined(_MSC_VER) # pragma warning(pop) #endif diff --git a/platforms/sdl/main.cpp.in b/platforms/sdl/main.cpp.in index d570935..cf38c66 100644 --- a/platforms/sdl/main.cpp.in +++ b/platforms/sdl/main.cpp.in @@ -264,7 +264,15 @@ void platform::save_dialog(std::function returnFunction) { } char* platform::translate_keycode(const unsigned int keycode) { - return const_cast(SDL_GetKeyName(keycode)); + return const_cast(SDL_GetKeyName(SDL_GetKeyFromScancode((SDL_Scancode)keycode))); +} + +void platform::begin_text_input() { + SDL_StartTextInput(); +} + +void platform::end_text_input() { + SDL_StopTextInput(); } void platform::mute_output() {} @@ -320,7 +328,13 @@ int main(int argc, char* argv[]) { engine->process_key_down(event.key.keysym.scancode); } break; + case SDL_KEYUP: + { + engine->process_key_up(event.key.keysym.scancode); + } + break; case SDL_WINDOWEVENT: + { if (event.window.event == SDL_WINDOWEVENT_RESIZED) { auto window = get_window_by_sdl_id(event.window.windowID); if(window != nullptr) @@ -328,7 +342,13 @@ int main(int argc, char* argv[]) { } else if(event.window.event == SDL_WINDOWEVENT_CLOSE) { engine->quit(); } + } break; + case SDL_TEXTINPUT: + { + engine->process_text_input(event.text.text); + } + break; } }