property_selector.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /**************************************************************************/
  2. /* property_selector.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 "property_selector.h"
  31. #include "editor/editor_help.h"
  32. #include "editor/editor_node.h"
  33. #include "editor/themes/editor_scale.h"
  34. #include "scene/gui/line_edit.h"
  35. #include "scene/gui/tree.h"
  36. void PropertySelector::_text_changed(const String &p_newtext) {
  37. _update_search();
  38. }
  39. void PropertySelector::_sbox_input(const Ref<InputEvent> &p_event) {
  40. // Redirect navigational key events to the tree.
  41. Ref<InputEventKey> key = p_event;
  42. if (key.is_valid()) {
  43. if (key->is_action("ui_up", true) || key->is_action("ui_down", true) || key->is_action("ui_page_up") || key->is_action("ui_page_down")) {
  44. search_options->gui_input(key);
  45. search_box->accept_event();
  46. TreeItem *root = search_options->get_root();
  47. if (!root->get_first_child()) {
  48. return;
  49. }
  50. TreeItem *current = search_options->get_selected();
  51. TreeItem *item = search_options->get_next_selected(root);
  52. while (item) {
  53. item->deselect(0);
  54. item = search_options->get_next_selected(item);
  55. }
  56. current->select(0);
  57. }
  58. }
  59. }
  60. void PropertySelector::_update_search() {
  61. if (properties) {
  62. set_title(TTR("Select Property"));
  63. } else if (virtuals_only) {
  64. set_title(TTR("Select Virtual Method"));
  65. } else {
  66. set_title(TTR("Select Method"));
  67. }
  68. search_options->clear();
  69. help_bit->set_custom_text(String(), String(), String());
  70. TreeItem *root = search_options->create_item();
  71. // Allow using spaces in place of underscores in the search string (makes the search more fault-tolerant).
  72. const String search_text = search_box->get_text().replace_char(' ', '_');
  73. if (properties) {
  74. List<PropertyInfo> props;
  75. if (instance) {
  76. instance->get_property_list(&props, true);
  77. } else if (type != Variant::NIL) {
  78. Variant v;
  79. Callable::CallError ce;
  80. Variant::construct(type, v, nullptr, 0, ce);
  81. v.get_property_list(&props);
  82. } else {
  83. Object *obj = ObjectDB::get_instance(script);
  84. if (Object::cast_to<Script>(obj)) {
  85. props.push_back(PropertyInfo(Variant::NIL, "Script Variables", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CATEGORY));
  86. Object::cast_to<Script>(obj)->get_script_property_list(&props);
  87. }
  88. StringName base = base_type;
  89. while (base) {
  90. props.push_back(PropertyInfo(Variant::NIL, base, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CATEGORY));
  91. ClassDB::get_property_list(base, &props, true);
  92. base = ClassDB::get_parent_class(base);
  93. }
  94. }
  95. TreeItem *category = nullptr;
  96. bool found = false;
  97. for (const PropertyInfo &E : props) {
  98. if (E.usage == PROPERTY_USAGE_CATEGORY) {
  99. if (category && category->get_first_child() == nullptr) {
  100. memdelete(category); //old category was unused
  101. }
  102. category = search_options->create_item(root);
  103. category->set_text(0, E.name);
  104. category->set_selectable(0, false);
  105. Ref<Texture2D> icon;
  106. if (E.name == "Script Variables") {
  107. icon = search_options->get_editor_theme_icon(SNAME("Script"));
  108. } else {
  109. icon = EditorNode::get_singleton()->get_class_icon(E.name);
  110. }
  111. category->set_icon(0, icon);
  112. continue;
  113. }
  114. if (!(E.usage & PROPERTY_USAGE_EDITOR) && !(E.usage & PROPERTY_USAGE_SCRIPT_VARIABLE)) {
  115. continue;
  116. }
  117. if (!search_box->get_text().is_empty() && !E.name.containsn(search_text)) {
  118. continue;
  119. }
  120. if (!type_filter.is_empty() && !type_filter.has(E.type)) {
  121. continue;
  122. }
  123. TreeItem *item = search_options->create_item(category ? category : root);
  124. item->set_text(0, E.name);
  125. item->set_metadata(0, E.name);
  126. item->set_icon(0, search_options->get_editor_theme_icon(Variant::get_type_name(E.type)));
  127. if (!found && !search_box->get_text().is_empty() && E.name.containsn(search_text)) {
  128. item->select(0);
  129. found = true;
  130. } else if (!found && search_box->get_text().is_empty() && E.name == selected) {
  131. item->select(0);
  132. found = true;
  133. }
  134. item->set_selectable(0, true);
  135. _create_subproperties(item, E.type);
  136. item->set_collapsed(true);
  137. }
  138. if (category && category->get_first_child() == nullptr) {
  139. memdelete(category); //old category was unused
  140. }
  141. if (found) {
  142. // As we call this while adding items, defer until list is completely populated.
  143. callable_mp(search_options, &Tree::scroll_to_item).call_deferred(search_options->get_selected(), true);
  144. }
  145. } else {
  146. List<MethodInfo> methods;
  147. if (type != Variant::NIL) {
  148. Variant v;
  149. Callable::CallError ce;
  150. Variant::construct(type, v, nullptr, 0, ce);
  151. v.get_method_list(&methods);
  152. } else {
  153. Ref<Script> script_ref = ObjectDB::get_ref<Script>(script);
  154. if (script_ref.is_valid()) {
  155. if (script_ref->is_built_in()) {
  156. script_ref->reload(true);
  157. }
  158. List<MethodInfo> script_methods;
  159. script_ref->get_script_method_list(&script_methods);
  160. methods.push_back(MethodInfo("*Script Methods")); // TODO: Split by inheritance.
  161. for (const MethodInfo &mi : script_methods) {
  162. if (mi.name.begins_with("@")) {
  163. // GH-92782. GDScript inline setters/getters are historically present in `get_method_list()`
  164. // and can be called using `Object.call()`. However, these functions are meant to be internal
  165. // and their names are not valid identifiers, so let's hide them from the user.
  166. continue;
  167. }
  168. methods.push_back(mi);
  169. }
  170. }
  171. StringName base = base_type;
  172. while (base) {
  173. methods.push_back(MethodInfo("*" + String(base)));
  174. ClassDB::get_method_list(base, &methods, true, true);
  175. base = ClassDB::get_parent_class(base);
  176. }
  177. }
  178. TreeItem *category = nullptr;
  179. bool found = false;
  180. bool script_methods = false;
  181. for (MethodInfo &mi : methods) {
  182. if (mi.name.begins_with("*")) {
  183. if (category && category->get_first_child() == nullptr) {
  184. memdelete(category); //old category was unused
  185. }
  186. category = search_options->create_item(root);
  187. category->set_text(0, mi.name.replace_first("*", ""));
  188. category->set_selectable(0, false);
  189. Ref<Texture2D> icon;
  190. script_methods = false;
  191. String rep = mi.name.remove_char('*');
  192. if (mi.name == "*Script Methods") {
  193. icon = search_options->get_editor_theme_icon(SNAME("Script"));
  194. script_methods = true;
  195. } else {
  196. icon = EditorNode::get_singleton()->get_class_icon(rep);
  197. }
  198. category->set_icon(0, icon);
  199. continue;
  200. }
  201. String name = mi.name.get_slicec(':', 0);
  202. if (!script_methods && name.begins_with("_") && !(mi.flags & METHOD_FLAG_VIRTUAL)) {
  203. continue;
  204. }
  205. if (virtuals_only && !(mi.flags & METHOD_FLAG_VIRTUAL)) {
  206. continue;
  207. }
  208. if (!virtuals_only && (mi.flags & METHOD_FLAG_VIRTUAL)) {
  209. continue;
  210. }
  211. if (!search_box->get_text().is_empty() && !name.containsn(search_text)) {
  212. continue;
  213. }
  214. TreeItem *item = search_options->create_item(category ? category : root);
  215. String desc;
  216. if (mi.name.contains_char(':')) {
  217. desc = mi.name.get_slicec(':', 1) + " ";
  218. mi.name = mi.name.get_slicec(':', 0);
  219. } else if (mi.return_val.type != Variant::NIL) {
  220. desc = Variant::get_type_name(mi.return_val.type);
  221. } else {
  222. desc = "void";
  223. }
  224. desc += vformat(" %s(", mi.name);
  225. for (int64_t i = 0; i < mi.arguments.size(); ++i) {
  226. PropertyInfo &arg = mi.arguments.write[i];
  227. if (i > 0) {
  228. desc += ", ";
  229. }
  230. desc += arg.name;
  231. if (arg.type == Variant::NIL) {
  232. desc += ": Variant";
  233. } else if (arg.name.contains_char(':')) {
  234. desc += vformat(": %s", arg.name.get_slicec(':', 1));
  235. arg.name = arg.name.get_slicec(':', 0);
  236. } else {
  237. desc += vformat(": %s", Variant::get_type_name(arg.type));
  238. }
  239. }
  240. desc += ")";
  241. if (mi.flags & METHOD_FLAG_CONST) {
  242. desc += " const";
  243. }
  244. if (mi.flags & METHOD_FLAG_VIRTUAL) {
  245. desc += " virtual";
  246. }
  247. item->set_text(0, desc);
  248. item->set_metadata(0, name);
  249. item->set_selectable(0, true);
  250. if (!found && !search_box->get_text().is_empty() && name.containsn(search_text)) {
  251. item->select(0);
  252. found = true;
  253. } else if (!found && search_box->get_text().is_empty() && name == selected) {
  254. item->select(0);
  255. found = true;
  256. }
  257. }
  258. if (category && category->get_first_child() == nullptr) {
  259. memdelete(category); //old category was unused
  260. }
  261. if (found) {
  262. // As we call this while adding items, defer until list is completely populated.
  263. callable_mp(search_options, &Tree::scroll_to_item).call_deferred(search_options->get_selected(), true);
  264. }
  265. }
  266. get_ok_button()->set_disabled(search_options->get_selected() == nullptr);
  267. }
  268. void PropertySelector::_confirmed() {
  269. TreeItem *ti = search_options->get_selected();
  270. if (!ti) {
  271. return;
  272. }
  273. emit_signal(SNAME("selected"), ti->get_metadata(0));
  274. hide();
  275. }
  276. void PropertySelector::_item_selected() {
  277. help_bit->set_custom_text(String(), String(), String());
  278. TreeItem *item = search_options->get_selected();
  279. get_ok_button()->set_disabled(item == nullptr);
  280. if (!item) {
  281. return;
  282. }
  283. String name = item->get_metadata(0);
  284. String class_type;
  285. if (type != Variant::NIL) {
  286. class_type = Variant::get_type_name(type);
  287. } else if (!base_type.is_empty()) {
  288. class_type = base_type;
  289. } else if (instance) {
  290. class_type = instance->get_class();
  291. }
  292. String text;
  293. while (!class_type.is_empty()) {
  294. if (properties) {
  295. if (ClassDB::has_property(class_type, name, true)) {
  296. help_bit->parse_symbol("property|" + class_type + "|" + name);
  297. break;
  298. }
  299. } else {
  300. if (ClassDB::has_method(class_type, name, true)) {
  301. help_bit->parse_symbol("method|" + class_type + "|" + name);
  302. break;
  303. }
  304. }
  305. // It may be from a parent class, keep looking.
  306. class_type = ClassDB::get_parent_class(class_type);
  307. }
  308. }
  309. void PropertySelector::_hide_requested() {
  310. _cancel_pressed(); // From AcceptDialog.
  311. }
  312. void PropertySelector::_create_subproperties(TreeItem *p_parent_item, Variant::Type p_type) {
  313. switch (p_type) {
  314. case Variant::VECTOR2: {
  315. _create_subproperty(p_parent_item, "x", Variant::FLOAT);
  316. _create_subproperty(p_parent_item, "y", Variant::FLOAT);
  317. } break;
  318. case Variant::VECTOR2I: {
  319. _create_subproperty(p_parent_item, "x", Variant::INT);
  320. _create_subproperty(p_parent_item, "y", Variant::INT);
  321. } break;
  322. case Variant::RECT2: {
  323. _create_subproperty(p_parent_item, "position", Variant::VECTOR2);
  324. _create_subproperty(p_parent_item, "size", Variant::VECTOR2);
  325. _create_subproperty(p_parent_item, "end", Variant::VECTOR2);
  326. } break;
  327. case Variant::RECT2I: {
  328. _create_subproperty(p_parent_item, "position", Variant::VECTOR2I);
  329. _create_subproperty(p_parent_item, "size", Variant::VECTOR2I);
  330. _create_subproperty(p_parent_item, "end", Variant::VECTOR2I);
  331. } break;
  332. case Variant::VECTOR3: {
  333. _create_subproperty(p_parent_item, "x", Variant::FLOAT);
  334. _create_subproperty(p_parent_item, "y", Variant::FLOAT);
  335. _create_subproperty(p_parent_item, "z", Variant::FLOAT);
  336. } break;
  337. case Variant::VECTOR3I: {
  338. _create_subproperty(p_parent_item, "x", Variant::INT);
  339. _create_subproperty(p_parent_item, "y", Variant::INT);
  340. _create_subproperty(p_parent_item, "z", Variant::INT);
  341. } break;
  342. case Variant::TRANSFORM2D: {
  343. _create_subproperty(p_parent_item, "origin", Variant::VECTOR2);
  344. _create_subproperty(p_parent_item, "x", Variant::VECTOR2);
  345. _create_subproperty(p_parent_item, "y", Variant::VECTOR2);
  346. } break;
  347. case Variant::VECTOR4: {
  348. _create_subproperty(p_parent_item, "x", Variant::FLOAT);
  349. _create_subproperty(p_parent_item, "y", Variant::FLOAT);
  350. _create_subproperty(p_parent_item, "z", Variant::FLOAT);
  351. _create_subproperty(p_parent_item, "w", Variant::FLOAT);
  352. } break;
  353. case Variant::VECTOR4I: {
  354. _create_subproperty(p_parent_item, "x", Variant::INT);
  355. _create_subproperty(p_parent_item, "y", Variant::INT);
  356. _create_subproperty(p_parent_item, "z", Variant::INT);
  357. _create_subproperty(p_parent_item, "w", Variant::INT);
  358. } break;
  359. case Variant::PLANE: {
  360. _create_subproperty(p_parent_item, "x", Variant::FLOAT);
  361. _create_subproperty(p_parent_item, "y", Variant::FLOAT);
  362. _create_subproperty(p_parent_item, "z", Variant::FLOAT);
  363. _create_subproperty(p_parent_item, "normal", Variant::VECTOR3);
  364. _create_subproperty(p_parent_item, "d", Variant::FLOAT);
  365. } break;
  366. case Variant::QUATERNION: {
  367. _create_subproperty(p_parent_item, "x", Variant::FLOAT);
  368. _create_subproperty(p_parent_item, "y", Variant::FLOAT);
  369. _create_subproperty(p_parent_item, "z", Variant::FLOAT);
  370. _create_subproperty(p_parent_item, "w", Variant::FLOAT);
  371. } break;
  372. case Variant::AABB: {
  373. _create_subproperty(p_parent_item, "position", Variant::VECTOR3);
  374. _create_subproperty(p_parent_item, "size", Variant::VECTOR3);
  375. _create_subproperty(p_parent_item, "end", Variant::VECTOR3);
  376. } break;
  377. case Variant::BASIS: {
  378. _create_subproperty(p_parent_item, "x", Variant::VECTOR3);
  379. _create_subproperty(p_parent_item, "y", Variant::VECTOR3);
  380. _create_subproperty(p_parent_item, "z", Variant::VECTOR3);
  381. } break;
  382. case Variant::TRANSFORM3D: {
  383. _create_subproperty(p_parent_item, "basis", Variant::BASIS);
  384. _create_subproperty(p_parent_item, "origin", Variant::VECTOR3);
  385. } break;
  386. case Variant::PROJECTION: {
  387. _create_subproperty(p_parent_item, "x", Variant::VECTOR4);
  388. _create_subproperty(p_parent_item, "y", Variant::VECTOR4);
  389. _create_subproperty(p_parent_item, "z", Variant::VECTOR4);
  390. _create_subproperty(p_parent_item, "w", Variant::VECTOR4);
  391. } break;
  392. case Variant::COLOR: {
  393. _create_subproperty(p_parent_item, "r", Variant::FLOAT);
  394. _create_subproperty(p_parent_item, "g", Variant::FLOAT);
  395. _create_subproperty(p_parent_item, "b", Variant::FLOAT);
  396. _create_subproperty(p_parent_item, "a", Variant::FLOAT);
  397. _create_subproperty(p_parent_item, "r8", Variant::INT);
  398. _create_subproperty(p_parent_item, "g8", Variant::INT);
  399. _create_subproperty(p_parent_item, "b8", Variant::INT);
  400. _create_subproperty(p_parent_item, "a8", Variant::INT);
  401. _create_subproperty(p_parent_item, "h", Variant::FLOAT);
  402. _create_subproperty(p_parent_item, "s", Variant::FLOAT);
  403. _create_subproperty(p_parent_item, "v", Variant::FLOAT);
  404. } break;
  405. default: {
  406. }
  407. }
  408. }
  409. void PropertySelector::_create_subproperty(TreeItem *p_parent_item, const String &p_name, Variant::Type p_type) {
  410. if (!type_filter.is_empty() && !type_filter.has(p_type)) {
  411. return;
  412. }
  413. TreeItem *item = search_options->create_item(p_parent_item);
  414. item->set_text(0, p_name);
  415. item->set_metadata(0, String(p_parent_item->get_metadata(0)) + ":" + p_name);
  416. item->set_icon(0, search_options->get_editor_theme_icon(Variant::get_type_name(p_type)));
  417. _create_subproperties(item, p_type);
  418. }
  419. void PropertySelector::_notification(int p_what) {
  420. switch (p_what) {
  421. case NOTIFICATION_ENTER_TREE: {
  422. connect(SceneStringName(confirmed), callable_mp(this, &PropertySelector::_confirmed));
  423. } break;
  424. case NOTIFICATION_EXIT_TREE: {
  425. disconnect(SceneStringName(confirmed), callable_mp(this, &PropertySelector::_confirmed));
  426. } break;
  427. }
  428. }
  429. void PropertySelector::select_method_from_base_type(const String &p_base, const String &p_current, bool p_virtuals_only) {
  430. base_type = p_base;
  431. selected = p_current;
  432. type = Variant::NIL;
  433. script = ObjectID();
  434. properties = false;
  435. instance = nullptr;
  436. virtuals_only = p_virtuals_only;
  437. popup_centered_ratio(0.6);
  438. search_box->set_text("");
  439. search_box->grab_focus();
  440. _update_search();
  441. }
  442. void PropertySelector::select_method_from_script(const Ref<Script> &p_script, const String &p_current) {
  443. ERR_FAIL_COND(p_script.is_null());
  444. base_type = p_script->get_instance_base_type();
  445. selected = p_current;
  446. type = Variant::NIL;
  447. script = p_script->get_instance_id();
  448. properties = false;
  449. instance = nullptr;
  450. virtuals_only = false;
  451. popup_centered_ratio(0.6);
  452. search_box->set_text("");
  453. search_box->grab_focus();
  454. _update_search();
  455. }
  456. void PropertySelector::select_method_from_basic_type(Variant::Type p_type, const String &p_current) {
  457. ERR_FAIL_COND(p_type == Variant::NIL);
  458. base_type = "";
  459. selected = p_current;
  460. type = p_type;
  461. script = ObjectID();
  462. properties = false;
  463. instance = nullptr;
  464. virtuals_only = false;
  465. popup_centered_ratio(0.6);
  466. search_box->set_text("");
  467. search_box->grab_focus();
  468. _update_search();
  469. }
  470. void PropertySelector::select_method_from_instance(Object *p_instance, const String &p_current) {
  471. base_type = p_instance->get_class();
  472. selected = p_current;
  473. type = Variant::NIL;
  474. script = ObjectID();
  475. {
  476. Ref<Script> scr = p_instance->get_script();
  477. if (scr.is_valid()) {
  478. script = scr->get_instance_id();
  479. }
  480. }
  481. properties = false;
  482. instance = nullptr;
  483. virtuals_only = false;
  484. popup_centered_ratio(0.6);
  485. search_box->set_text("");
  486. search_box->grab_focus();
  487. _update_search();
  488. }
  489. void PropertySelector::select_property_from_base_type(const String &p_base, const String &p_current) {
  490. base_type = p_base;
  491. selected = p_current;
  492. type = Variant::NIL;
  493. script = ObjectID();
  494. properties = true;
  495. instance = nullptr;
  496. virtuals_only = false;
  497. popup_centered_ratio(0.6);
  498. search_box->set_text("");
  499. search_box->grab_focus();
  500. _update_search();
  501. }
  502. void PropertySelector::select_property_from_script(const Ref<Script> &p_script, const String &p_current) {
  503. ERR_FAIL_COND(p_script.is_null());
  504. base_type = p_script->get_instance_base_type();
  505. selected = p_current;
  506. type = Variant::NIL;
  507. script = p_script->get_instance_id();
  508. properties = true;
  509. instance = nullptr;
  510. virtuals_only = false;
  511. popup_centered_ratio(0.6);
  512. search_box->set_text("");
  513. search_box->grab_focus();
  514. _update_search();
  515. }
  516. void PropertySelector::select_property_from_basic_type(Variant::Type p_type, const String &p_current) {
  517. ERR_FAIL_COND(p_type == Variant::NIL);
  518. base_type = "";
  519. selected = p_current;
  520. type = p_type;
  521. script = ObjectID();
  522. properties = true;
  523. instance = nullptr;
  524. virtuals_only = false;
  525. popup_centered_ratio(0.6);
  526. search_box->set_text("");
  527. search_box->grab_focus();
  528. _update_search();
  529. }
  530. void PropertySelector::select_property_from_instance(Object *p_instance, const String &p_current) {
  531. base_type = "";
  532. selected = p_current;
  533. type = Variant::NIL;
  534. script = ObjectID();
  535. properties = true;
  536. instance = p_instance;
  537. virtuals_only = false;
  538. popup_centered_ratio(0.6);
  539. search_box->set_text("");
  540. search_box->grab_focus();
  541. _update_search();
  542. }
  543. void PropertySelector::set_type_filter(const Vector<Variant::Type> &p_type_filter) {
  544. type_filter = p_type_filter;
  545. }
  546. void PropertySelector::_bind_methods() {
  547. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "name")));
  548. }
  549. PropertySelector::PropertySelector() {
  550. VBoxContainer *vbc = memnew(VBoxContainer);
  551. add_child(vbc);
  552. //set_child_rect(vbc);
  553. search_box = memnew(LineEdit);
  554. vbc->add_margin_child(TTR("Search:"), search_box);
  555. search_box->connect(SceneStringName(text_changed), callable_mp(this, &PropertySelector::_text_changed));
  556. search_box->connect(SceneStringName(gui_input), callable_mp(this, &PropertySelector::_sbox_input));
  557. search_options = memnew(Tree);
  558. search_options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  559. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  560. set_ok_button_text(TTR("Open"));
  561. get_ok_button()->set_disabled(true);
  562. register_text_enter(search_box);
  563. set_hide_on_ok(false);
  564. search_options->connect("item_activated", callable_mp(this, &PropertySelector::_confirmed));
  565. search_options->connect("cell_selected", callable_mp(this, &PropertySelector::_item_selected));
  566. search_options->set_hide_root(true);
  567. help_bit = memnew(EditorHelpBit);
  568. help_bit->set_content_height_limits(80 * EDSCALE, 80 * EDSCALE);
  569. help_bit->connect("request_hide", callable_mp(this, &PropertySelector::_hide_requested));
  570. vbc->add_margin_child(TTR("Description:"), help_bit);
  571. }