editor_sectioned_inspector.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  68. PropertyInfo pi = E->get();
  69. int sp = pi.name.find("/");
  70. 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
  71. continue;
  72. }
  73. if (sp == -1) {
  74. pi.name = "global/" + pi.name;
  75. }
  76. if (pi.name.begins_with(section + "/")) {
  77. pi.name = pi.name.replace_first(section + "/", "");
  78. if (!allow_sub && pi.name.find("/") != -1) {
  79. continue;
  80. }
  81. p_list->push_back(pi);
  82. }
  83. }
  84. }
  85. bool property_can_revert(const String &p_name) {
  86. return edited->call("property_can_revert", section + "/" + p_name);
  87. }
  88. Variant property_get_revert(const String &p_name) {
  89. return edited->call("property_get_revert", section + "/" + p_name);
  90. }
  91. protected:
  92. static void _bind_methods() {
  93. ClassDB::bind_method("property_can_revert", &SectionedInspectorFilter::property_can_revert);
  94. ClassDB::bind_method("property_get_revert", &SectionedInspectorFilter::property_get_revert);
  95. }
  96. public:
  97. void set_section(const String &p_section, bool p_allow_sub) {
  98. section = p_section;
  99. allow_sub = p_allow_sub;
  100. notify_property_list_changed();
  101. }
  102. void set_edited(Object *p_edited) {
  103. edited = p_edited;
  104. notify_property_list_changed();
  105. }
  106. };
  107. void SectionedInspector::_bind_methods() {
  108. ClassDB::bind_method("update_category_list", &SectionedInspector::update_category_list);
  109. }
  110. void SectionedInspector::_section_selected() {
  111. if (!sections->get_selected()) {
  112. return;
  113. }
  114. selected_category = sections->get_selected()->get_metadata(0);
  115. filter->set_section(selected_category, sections->get_selected()->get_children() == nullptr);
  116. inspector->set_property_prefix(selected_category + "/");
  117. }
  118. void SectionedInspector::set_current_section(const String &p_section) {
  119. if (section_map.has(p_section)) {
  120. section_map[p_section]->select(0);
  121. }
  122. }
  123. String SectionedInspector::get_current_section() const {
  124. if (sections->get_selected()) {
  125. return sections->get_selected()->get_metadata(0);
  126. } else {
  127. return "";
  128. }
  129. }
  130. String SectionedInspector::get_full_item_path(const String &p_item) {
  131. String base = get_current_section();
  132. if (base != "") {
  133. return base + "/" + p_item;
  134. } else {
  135. return p_item;
  136. }
  137. }
  138. void SectionedInspector::edit(Object *p_object) {
  139. if (!p_object) {
  140. obj = ObjectID();
  141. sections->clear();
  142. filter->set_edited(nullptr);
  143. inspector->edit(nullptr);
  144. return;
  145. }
  146. ObjectID id = p_object->get_instance_id();
  147. inspector->set_object_class(p_object->get_class());
  148. if (obj != id) {
  149. obj = id;
  150. update_category_list();
  151. filter->set_edited(p_object);
  152. inspector->edit(filter);
  153. TreeItem *first_item = sections->get_root();
  154. if (first_item) {
  155. while (first_item->get_children()) {
  156. first_item = first_item->get_children();
  157. }
  158. first_item->select(0);
  159. selected_category = first_item->get_metadata(0);
  160. }
  161. } else {
  162. update_category_list();
  163. }
  164. }
  165. void SectionedInspector::update_category_list() {
  166. sections->clear();
  167. Object *o = ObjectDB::get_instance(obj);
  168. if (!o) {
  169. return;
  170. }
  171. List<PropertyInfo> pinfo;
  172. o->get_property_list(&pinfo);
  173. section_map.clear();
  174. TreeItem *root = sections->create_item();
  175. section_map[""] = root;
  176. String filter;
  177. if (search_box) {
  178. filter = search_box->get_text();
  179. }
  180. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  181. PropertyInfo pi = E->get();
  182. if (pi.usage & PROPERTY_USAGE_CATEGORY) {
  183. continue;
  184. } else if (!(pi.usage & PROPERTY_USAGE_EDITOR) || (restrict_to_basic && !(pi.usage & PROPERTY_USAGE_EDITOR_BASIC_SETTING))) {
  185. continue;
  186. }
  187. 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")) {
  188. continue;
  189. }
  190. if (!filter.is_empty() && pi.name.findn(filter) == -1 && pi.name.replace("/", " ").capitalize().findn(filter) == -1) {
  191. continue;
  192. }
  193. int sp = pi.name.find("/");
  194. if (sp == -1) {
  195. pi.name = "global/" + pi.name;
  196. }
  197. Vector<String> sectionarr = pi.name.split("/");
  198. String metasection;
  199. int sc = MIN(2, sectionarr.size() - 1);
  200. for (int i = 0; i < sc; i++) {
  201. TreeItem *parent = section_map[metasection];
  202. parent->set_custom_bg_color(0, get_theme_color("prop_subsection", "Editor"));
  203. if (i > 0) {
  204. metasection += "/" + sectionarr[i];
  205. } else {
  206. metasection = sectionarr[i];
  207. }
  208. if (!section_map.has(metasection)) {
  209. TreeItem *ms = sections->create_item(parent);
  210. section_map[metasection] = ms;
  211. ms->set_text(0, sectionarr[i].capitalize());
  212. ms->set_metadata(0, metasection);
  213. ms->set_selectable(0, false);
  214. }
  215. if (i == sc - 1) {
  216. //if it has children, make selectable
  217. section_map[metasection]->set_selectable(0, true);
  218. }
  219. }
  220. }
  221. if (section_map.has(selected_category)) {
  222. section_map[selected_category]->select(0);
  223. }
  224. inspector->update_tree();
  225. }
  226. void SectionedInspector::register_search_box(LineEdit *p_box) {
  227. search_box = p_box;
  228. inspector->register_text_enter(p_box);
  229. search_box->connect("text_changed", callable_mp(this, &SectionedInspector::_search_changed));
  230. }
  231. void SectionedInspector::_search_changed(const String &p_what) {
  232. update_category_list();
  233. }
  234. EditorInspector *SectionedInspector::get_inspector() {
  235. return inspector;
  236. }
  237. void SectionedInspector::set_restrict_to_basic_settings(bool p_restrict) {
  238. restrict_to_basic = p_restrict;
  239. update_category_list();
  240. inspector->set_restrict_to_basic_settings(p_restrict);
  241. }
  242. SectionedInspector::SectionedInspector() :
  243. sections(memnew(Tree)),
  244. filter(memnew(SectionedInspectorFilter)),
  245. inspector(memnew(EditorInspector)) {
  246. add_theme_constant_override("autohide", 1); // Fixes the dragger always showing up
  247. VBoxContainer *left_vb = memnew(VBoxContainer);
  248. left_vb->set_custom_minimum_size(Size2(190, 0) * EDSCALE);
  249. add_child(left_vb);
  250. sections->set_v_size_flags(SIZE_EXPAND_FILL);
  251. sections->set_hide_root(true);
  252. left_vb->add_child(sections, true);
  253. VBoxContainer *right_vb = memnew(VBoxContainer);
  254. right_vb->set_custom_minimum_size(Size2(300, 0) * EDSCALE);
  255. right_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  256. add_child(right_vb);
  257. inspector->set_v_size_flags(SIZE_EXPAND_FILL);
  258. right_vb->add_child(inspector, true);
  259. inspector->set_use_doc_hints(true);
  260. sections->connect("cell_selected", callable_mp(this, &SectionedInspector::_section_selected));
  261. }
  262. SectionedInspector::~SectionedInspector() {
  263. memdelete(filter);
  264. }