editor_sectioned_inspector.cpp 10 KB

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