editor_sectioned_inspector.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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;
  35. String section;
  36. bool allow_sub;
  37. bool _set(const StringName &p_name, const Variant &p_value) {
  38. if (!edited)
  39. return false;
  40. String name = p_name;
  41. if (section != "") {
  42. name = section + "/" + name;
  43. }
  44. bool valid;
  45. edited->set(name, p_value, &valid);
  46. return valid;
  47. }
  48. bool _get(const StringName &p_name, Variant &r_ret) const {
  49. if (!edited)
  50. return false;
  51. String name = p_name;
  52. if (section != "") {
  53. name = section + "/" + name;
  54. }
  55. bool valid = false;
  56. r_ret = edited->get(name, &valid);
  57. return valid;
  58. }
  59. void _get_property_list(List<PropertyInfo> *p_list) const {
  60. if (!edited)
  61. return;
  62. List<PropertyInfo> pinfo;
  63. edited->get_property_list(&pinfo);
  64. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  65. PropertyInfo pi = E->get();
  66. int sp = pi.name.find("/");
  67. if (pi.name == "resource_path" || pi.name == "resource_name" || pi.name == "resource_local_to_scene" || pi.name.begins_with("script/")) //skip resource stuff
  68. continue;
  69. if (sp == -1) {
  70. pi.name = "global/" + pi.name;
  71. }
  72. if (pi.name.begins_with(section + "/")) {
  73. pi.name = pi.name.replace_first(section + "/", "");
  74. if (!allow_sub && pi.name.find("/") != -1)
  75. continue;
  76. p_list->push_back(pi);
  77. }
  78. }
  79. }
  80. bool property_can_revert(const String &p_name) {
  81. return edited->call("property_can_revert", section + "/" + p_name);
  82. }
  83. Variant property_get_revert(const String &p_name) {
  84. return edited->call("property_get_revert", section + "/" + p_name);
  85. }
  86. protected:
  87. static void _bind_methods() {
  88. ClassDB::bind_method("property_can_revert", &SectionedInspectorFilter::property_can_revert);
  89. ClassDB::bind_method("property_get_revert", &SectionedInspectorFilter::property_get_revert);
  90. }
  91. public:
  92. void set_section(const String &p_section, bool p_allow_sub) {
  93. section = p_section;
  94. allow_sub = p_allow_sub;
  95. _change_notify();
  96. }
  97. void set_edited(Object *p_edited) {
  98. edited = p_edited;
  99. _change_notify();
  100. }
  101. SectionedInspectorFilter() {
  102. edited = NULL;
  103. }
  104. };
  105. void SectionedInspector::_bind_methods() {
  106. ClassDB::bind_method("_section_selected", &SectionedInspector::_section_selected);
  107. ClassDB::bind_method("_search_changed", &SectionedInspector::_search_changed);
  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. filter->set_section(sections->get_selected()->get_metadata(0), sections->get_selected()->get_children() == NULL);
  114. inspector->set_property_prefix(String(sections->get_selected()->get_metadata(0)) + "/");
  115. }
  116. void SectionedInspector::set_current_section(const String &p_section) {
  117. if (section_map.has(p_section)) {
  118. section_map[p_section]->select(0);
  119. }
  120. }
  121. String SectionedInspector::get_current_section() const {
  122. if (sections->get_selected())
  123. return sections->get_selected()->get_metadata(0);
  124. else
  125. return "";
  126. }
  127. String SectionedInspector::get_full_item_path(const String &p_item) {
  128. String base = get_current_section();
  129. if (base != "")
  130. return base + "/" + p_item;
  131. else
  132. return p_item;
  133. }
  134. void SectionedInspector::edit(Object *p_object) {
  135. if (!p_object) {
  136. obj = -1;
  137. sections->clear();
  138. filter->set_edited(NULL);
  139. inspector->edit(NULL);
  140. return;
  141. }
  142. ObjectID id = p_object->get_instance_id();
  143. inspector->set_object_class(p_object->get_class());
  144. if (obj != id) {
  145. obj = id;
  146. update_category_list();
  147. filter->set_edited(p_object);
  148. inspector->edit(filter);
  149. if (sections->get_root()->get_children()) {
  150. sections->get_root()->get_children()->select(0);
  151. }
  152. } else {
  153. update_category_list();
  154. }
  155. }
  156. void SectionedInspector::update_category_list() {
  157. String selected_category = get_current_section();
  158. sections->clear();
  159. Object *o = ObjectDB::get_instance(obj);
  160. if (!o)
  161. return;
  162. List<PropertyInfo> pinfo;
  163. o->get_property_list(&pinfo);
  164. section_map.clear();
  165. TreeItem *root = sections->create_item();
  166. section_map[""] = root;
  167. for (List<PropertyInfo>::Element *E = pinfo.front(); E; E = E->next()) {
  168. PropertyInfo pi = E->get();
  169. if (pi.usage & PROPERTY_USAGE_CATEGORY)
  170. continue;
  171. else if (!(pi.usage & PROPERTY_USAGE_EDITOR))
  172. continue;
  173. if (pi.name.find(":") != -1 || pi.name == "script" || pi.name == "resource_name" || pi.name == "resource_path" || pi.name == "resource_local_to_scene")
  174. continue;
  175. if (search_box && search_box->get_text() != String() && pi.name.findn(search_box->get_text()) == -1)
  176. continue;
  177. int sp = pi.name.find("/");
  178. if (sp == -1)
  179. pi.name = "Global/" + pi.name;
  180. Vector<String> sectionarr = pi.name.split("/");
  181. String metasection;
  182. int sc = MIN(2, sectionarr.size() - 1);
  183. for (int i = 0; i < sc; i++) {
  184. TreeItem *parent = section_map[metasection];
  185. parent->set_custom_bg_color(0, get_color("prop_subsection", "Editor"));
  186. if (i > 0) {
  187. metasection += "/" + sectionarr[i];
  188. } else {
  189. metasection = sectionarr[i];
  190. }
  191. if (!section_map.has(metasection)) {
  192. TreeItem *ms = sections->create_item(parent);
  193. section_map[metasection] = ms;
  194. ms->set_text(0, sectionarr[i].capitalize());
  195. ms->set_metadata(0, metasection);
  196. ms->set_selectable(0, false);
  197. }
  198. if (i == sc - 1) {
  199. //if it has children, make selectable
  200. section_map[metasection]->set_selectable(0, true);
  201. }
  202. }
  203. }
  204. if (section_map.has(selected_category)) {
  205. section_map[selected_category]->select(0);
  206. }
  207. inspector->update_tree();
  208. }
  209. void SectionedInspector::register_search_box(LineEdit *p_box) {
  210. search_box = p_box;
  211. inspector->register_text_enter(p_box);
  212. search_box->connect("text_changed", this, "_search_changed");
  213. }
  214. void SectionedInspector::_search_changed(const String &p_what) {
  215. update_category_list();
  216. }
  217. EditorInspector *SectionedInspector::get_inspector() {
  218. return inspector;
  219. }
  220. SectionedInspector::SectionedInspector() :
  221. obj(-1),
  222. sections(memnew(Tree)),
  223. filter(memnew(SectionedInspectorFilter)),
  224. inspector(memnew(EditorInspector)),
  225. search_box(NULL) {
  226. add_constant_override("autohide", 1); // Fixes the dragger always showing up
  227. VBoxContainer *left_vb = memnew(VBoxContainer);
  228. left_vb->set_custom_minimum_size(Size2(170, 0) * EDSCALE);
  229. add_child(left_vb);
  230. sections->set_v_size_flags(SIZE_EXPAND_FILL);
  231. sections->set_hide_root(true);
  232. left_vb->add_child(sections, true);
  233. VBoxContainer *right_vb = memnew(VBoxContainer);
  234. right_vb->set_custom_minimum_size(Size2(300, 0) * EDSCALE);
  235. right_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  236. add_child(right_vb);
  237. inspector->set_v_size_flags(SIZE_EXPAND_FILL);
  238. right_vb->add_child(inspector, true);
  239. inspector->set_use_doc_hints(true);
  240. sections->connect("cell_selected", this, "_section_selected");
  241. }
  242. SectionedInspector::~SectionedInspector() {
  243. memdelete(filter);
  244. }