editor_sectioned_inspector.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*************************************************************************/
  2. /* editor_sectioned_inspector.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "editor_sectioned_inspector.h"
  31. #include "editor_scale.h"
  32. class SectionedInspectorFilter : public Object {
  33. GDCLASS(SectionedInspectorFilter, Object);
  34. Object *edited = nullptr;
  35. String section;
  36. bool allow_sub = false;
  37. bool _set(const StringName &p_name, const Variant &p_value) {
  38. if (!edited) {
  39. return false;
  40. }
  41. String name = p_name;
  42. if (section != "") {
  43. name = section + "/" + name;
  44. }
  45. bool valid;
  46. edited->set(name, p_value, &valid);
  47. return valid;
  48. }
  49. bool _get(const StringName &p_name, Variant &r_ret) const {
  50. if (!edited) {
  51. return false;
  52. }
  53. String name = p_name;
  54. if (section != "") {
  55. name = section + "/" + name;
  56. }
  57. bool valid = false;
  58. r_ret = edited->get(name, &valid);
  59. return valid;
  60. }
  61. void _get_property_list(List<PropertyInfo> *p_list) const {
  62. if (!edited) {
  63. return;
  64. }
  65. List<PropertyInfo> pinfo;
  66. edited->get_property_list(&pinfo);
  67. for (PropertyInfo &pi : pinfo) {
  68. int sp = pi.name.find("/");
  69. if (pi.name == "resource_path" || pi.name == "resource_name" || pi.name == "resource_local_to_scene" || pi.name.begins_with("script/") || pi.name.begins_with("_global_script")) { //skip resource stuff
  70. continue;
  71. }
  72. if (sp == -1) {
  73. pi.name = "global/" + pi.name;
  74. }
  75. if (pi.name.begins_with(section + "/")) {
  76. pi.name = pi.name.replace_first(section + "/", "");
  77. if (!allow_sub && pi.name.find("/") != -1) {
  78. continue;
  79. }
  80. p_list->push_back(pi);
  81. }
  82. }
  83. }
  84. bool property_can_revert(const String &p_name) {
  85. return edited->call("property_can_revert", section + "/" + p_name);
  86. }
  87. Variant property_get_revert(const String &p_name) {
  88. return edited->call("property_get_revert", section + "/" + p_name);
  89. }
  90. protected:
  91. static void _bind_methods() {
  92. ClassDB::bind_method("property_can_revert", &SectionedInspectorFilter::property_can_revert);
  93. ClassDB::bind_method("property_get_revert", &SectionedInspectorFilter::property_get_revert);
  94. }
  95. public:
  96. void set_section(const String &p_section, bool p_allow_sub) {
  97. section = p_section;
  98. allow_sub = p_allow_sub;
  99. notify_property_list_changed();
  100. }
  101. void set_edited(Object *p_edited) {
  102. edited = p_edited;
  103. notify_property_list_changed();
  104. }
  105. };
  106. void SectionedInspector::_bind_methods() {
  107. ClassDB::bind_method("update_category_list", &SectionedInspector::update_category_list);
  108. }
  109. void SectionedInspector::_section_selected() {
  110. if (!sections->get_selected()) {
  111. return;
  112. }
  113. selected_category = sections->get_selected()->get_metadata(0);
  114. filter->set_section(selected_category, sections->get_selected()->get_first_child() == nullptr);
  115. inspector->set_property_prefix(selected_category + "/");
  116. }
  117. void SectionedInspector::set_current_section(const String &p_section) {
  118. if (section_map.has(p_section)) {
  119. section_map[p_section]->select(0);
  120. }
  121. }
  122. String SectionedInspector::get_current_section() const {
  123. if (sections->get_selected()) {
  124. return sections->get_selected()->get_metadata(0);
  125. } else {
  126. return "";
  127. }
  128. }
  129. String SectionedInspector::get_full_item_path(const String &p_item) {
  130. String base = get_current_section();
  131. if (base != "") {
  132. return base + "/" + p_item;
  133. } else {
  134. return p_item;
  135. }
  136. }
  137. void SectionedInspector::edit(Object *p_object) {
  138. if (!p_object) {
  139. obj = ObjectID();
  140. sections->clear();
  141. filter->set_edited(nullptr);
  142. inspector->edit(nullptr);
  143. return;
  144. }
  145. ObjectID id = p_object->get_instance_id();
  146. inspector->set_object_class(p_object->get_class());
  147. if (obj != id) {
  148. obj = id;
  149. update_category_list();
  150. filter->set_edited(p_object);
  151. inspector->edit(filter);
  152. TreeItem *first_item = sections->get_root();
  153. if (first_item) {
  154. while (first_item->get_first_child()) {
  155. first_item = first_item->get_first_child();
  156. }
  157. first_item->select(0);
  158. selected_category = first_item->get_metadata(0);
  159. }
  160. } else {
  161. update_category_list();
  162. }
  163. }
  164. void SectionedInspector::update_category_list() {
  165. sections->clear();
  166. Object *o = ObjectDB::get_instance(obj);
  167. if (!o) {
  168. return;
  169. }
  170. List<PropertyInfo> pinfo;
  171. o->get_property_list(&pinfo);
  172. section_map.clear();
  173. TreeItem *root = sections->create_item();
  174. section_map[""] = root;
  175. String filter;
  176. if (search_box) {
  177. filter = search_box->get_text();
  178. }
  179. for (PropertyInfo &pi : pinfo) {
  180. if (pi.usage & PROPERTY_USAGE_CATEGORY) {
  181. continue;
  182. } else if (!(pi.usage & PROPERTY_USAGE_EDITOR) || (restrict_to_basic && !(pi.usage & PROPERTY_USAGE_EDITOR_BASIC_SETTING))) {
  183. continue;
  184. }
  185. if (pi.name.find(":") != -1 || pi.name == "script" || pi.name == "resource_name" || pi.name == "resource_path" || pi.name == "resource_local_to_scene" || pi.name.begins_with("_global_script")) {
  186. continue;
  187. }
  188. if (!filter.is_empty() && pi.name.findn(filter) == -1 && pi.name.replace("/", " ").capitalize().findn(filter) == -1) {
  189. continue;
  190. }
  191. int sp = pi.name.find("/");
  192. if (sp == -1) {
  193. pi.name = "global/" + pi.name;
  194. }
  195. Vector<String> sectionarr = pi.name.split("/");
  196. String metasection;
  197. int sc = MIN(2, sectionarr.size() - 1);
  198. for (int i = 0; i < sc; i++) {
  199. TreeItem *parent = section_map[metasection];
  200. //parent->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  201. parent->set_custom_font(0, get_theme_font(SNAME("bold"), SNAME("EditorFonts")));
  202. if (i > 0) {
  203. metasection += "/" + sectionarr[i];
  204. } else {
  205. metasection = sectionarr[i];
  206. }
  207. if (!section_map.has(metasection)) {
  208. TreeItem *ms = sections->create_item(parent);
  209. section_map[metasection] = ms;
  210. ms->set_text(0, sectionarr[i].capitalize());
  211. ms->set_metadata(0, metasection);
  212. ms->set_selectable(0, false);
  213. }
  214. if (i == sc - 1) {
  215. //if it has children, make selectable
  216. section_map[metasection]->set_selectable(0, true);
  217. }
  218. }
  219. }
  220. if (section_map.has(selected_category)) {
  221. section_map[selected_category]->select(0);
  222. }
  223. inspector->update_tree();
  224. }
  225. void SectionedInspector::register_search_box(LineEdit *p_box) {
  226. search_box = p_box;
  227. inspector->register_text_enter(p_box);
  228. search_box->connect("text_changed", callable_mp(this, &SectionedInspector::_search_changed));
  229. }
  230. void SectionedInspector::_search_changed(const String &p_what) {
  231. update_category_list();
  232. }
  233. EditorInspector *SectionedInspector::get_inspector() {
  234. return inspector;
  235. }
  236. void SectionedInspector::set_restrict_to_basic_settings(bool p_restrict) {
  237. restrict_to_basic = p_restrict;
  238. update_category_list();
  239. inspector->set_restrict_to_basic_settings(p_restrict);
  240. }
  241. SectionedInspector::SectionedInspector() :
  242. sections(memnew(Tree)),
  243. filter(memnew(SectionedInspectorFilter)),
  244. inspector(memnew(EditorInspector)) {
  245. add_theme_constant_override("autohide", 1); // Fixes the dragger always showing up
  246. VBoxContainer *left_vb = memnew(VBoxContainer);
  247. left_vb->set_custom_minimum_size(Size2(190, 0) * EDSCALE);
  248. add_child(left_vb);
  249. sections->set_v_size_flags(SIZE_EXPAND_FILL);
  250. sections->set_hide_root(true);
  251. left_vb->add_child(sections, true);
  252. VBoxContainer *right_vb = memnew(VBoxContainer);
  253. right_vb->set_custom_minimum_size(Size2(300, 0) * EDSCALE);
  254. right_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  255. add_child(right_vb);
  256. inspector->set_v_size_flags(SIZE_EXPAND_FILL);
  257. right_vb->add_child(inspector, true);
  258. inspector->set_use_doc_hints(true);
  259. sections->connect("cell_selected", callable_mp(this, &SectionedInspector::_section_selected));
  260. }
  261. SectionedInspector::~SectionedInspector() {
  262. memdelete(filter);
  263. }