property_info.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**************************************************************************/
  2. /* property_info.hpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include <godot_cpp/core/defs.hpp>
  32. #include <godot_cpp/classes/global_constants.hpp>
  33. #include <godot_cpp/variant/variant.hpp>
  34. #include <godot_cpp/godot.hpp>
  35. #include <gdextension_interface.h>
  36. namespace godot {
  37. struct PropertyInfo {
  38. Variant::Type type = Variant::NIL;
  39. StringName name;
  40. StringName class_name;
  41. uint32_t hint = PROPERTY_HINT_NONE;
  42. String hint_string;
  43. uint32_t usage = PROPERTY_USAGE_DEFAULT;
  44. PropertyInfo() = default;
  45. PropertyInfo(Variant::Type p_type, const StringName &p_name, PropertyHint p_hint = PROPERTY_HINT_NONE, const String &p_hint_string = "", uint32_t p_usage = PROPERTY_USAGE_DEFAULT, const StringName &p_class_name = "") :
  46. type(p_type),
  47. name(p_name),
  48. hint(p_hint),
  49. hint_string(p_hint_string),
  50. usage(p_usage) {
  51. if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
  52. class_name = hint_string;
  53. } else {
  54. class_name = p_class_name;
  55. }
  56. }
  57. PropertyInfo(GDExtensionVariantType p_type, const StringName &p_name, PropertyHint p_hint = PROPERTY_HINT_NONE, const String &p_hint_string = "", uint32_t p_usage = PROPERTY_USAGE_DEFAULT, const StringName &p_class_name = "") :
  58. PropertyInfo((Variant::Type)p_type, p_name, p_hint, p_hint_string, p_usage, p_class_name) {}
  59. PropertyInfo(const GDExtensionPropertyInfo *p_info) :
  60. PropertyInfo(p_info->type, *reinterpret_cast<StringName *>(p_info->name), (PropertyHint)p_info->hint, *reinterpret_cast<String *>(p_info->hint_string), p_info->usage, *reinterpret_cast<StringName *>(p_info->class_name)) {}
  61. operator Dictionary() const {
  62. Dictionary dict;
  63. dict["name"] = name;
  64. dict["class_name"] = class_name;
  65. dict["type"] = type;
  66. dict["hint"] = hint;
  67. dict["hint_string"] = hint_string;
  68. dict["usage"] = usage;
  69. return dict;
  70. }
  71. static PropertyInfo from_dict(const Dictionary &p_dict) {
  72. PropertyInfo pi;
  73. if (p_dict.has("type")) {
  74. pi.type = Variant::Type(int(p_dict["type"]));
  75. }
  76. if (p_dict.has("name")) {
  77. pi.name = p_dict["name"];
  78. }
  79. if (p_dict.has("class_name")) {
  80. pi.class_name = p_dict["class_name"];
  81. }
  82. if (p_dict.has("hint")) {
  83. pi.hint = PropertyHint(int(p_dict["hint"]));
  84. }
  85. if (p_dict.has("hint_string")) {
  86. pi.hint_string = p_dict["hint_string"];
  87. }
  88. if (p_dict.has("usage")) {
  89. pi.usage = p_dict["usage"];
  90. }
  91. return pi;
  92. }
  93. void _update(GDExtensionPropertyInfo *p_info) {
  94. p_info->type = (GDExtensionVariantType)type;
  95. *(reinterpret_cast<StringName *>(p_info->name)) = name;
  96. p_info->hint = hint;
  97. *(reinterpret_cast<String *>(p_info->hint_string)) = hint_string;
  98. p_info->usage = usage;
  99. *(reinterpret_cast<StringName *>(p_info->class_name)) = class_name;
  100. }
  101. GDExtensionPropertyInfo _to_gdextension() const {
  102. return {
  103. (GDExtensionVariantType)type,
  104. name._native_ptr(),
  105. class_name._native_ptr(),
  106. hint,
  107. hint_string._native_ptr(),
  108. usage,
  109. };
  110. }
  111. };
  112. } // namespace godot