editor_sectioned_inspector.cpp 11 KB

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