editor_sectioned_inspector.cpp 11 KB

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