editor_sectioned_inspector.cpp 12 KB

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