property_selector.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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(" ", "_");
  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 = Object::cast_to<Script>(ObjectDB::get_instance(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.replace("*", "");
  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_slice(":", 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_slice(":", 1) + " ";
  218. mi.name = mi.name.get_slice(":", 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 (List<PropertyInfo>::Iterator arg_itr = mi.arguments.begin(); arg_itr != mi.arguments.end(); ++arg_itr) {
  226. if (arg_itr != mi.arguments.begin()) {
  227. desc += ", ";
  228. }
  229. desc += arg_itr->name;
  230. if (arg_itr->type == Variant::NIL) {
  231. desc += ": Variant";
  232. } else if (arg_itr->name.contains_char(':')) {
  233. desc += vformat(": %s", arg_itr->name.get_slice(":", 1));
  234. arg_itr->name = arg_itr->name.get_slice(":", 0);
  235. } else {
  236. desc += vformat(": %s", Variant::get_type_name(arg_itr->type));
  237. }
  238. }
  239. desc += ")";
  240. if (mi.flags & METHOD_FLAG_CONST) {
  241. desc += " const";
  242. }
  243. if (mi.flags & METHOD_FLAG_VIRTUAL) {
  244. desc += " virtual";
  245. }
  246. item->set_text(0, desc);
  247. item->set_metadata(0, name);
  248. item->set_selectable(0, true);
  249. if (!found && !search_box->get_text().is_empty() && name.containsn(search_text)) {
  250. item->select(0);
  251. found = true;
  252. } else if (!found && search_box->get_text().is_empty() && name == selected) {
  253. item->select(0);
  254. found = true;
  255. }
  256. }
  257. if (category && category->get_first_child() == nullptr) {
  258. memdelete(category); //old category was unused
  259. }
  260. if (found) {
  261. // As we call this while adding items, defer until list is completely populated.
  262. callable_mp(search_options, &Tree::scroll_to_item).call_deferred(search_options->get_selected(), true);
  263. }
  264. }
  265. get_ok_button()->set_disabled(search_options->get_selected() == nullptr);
  266. }
  267. void PropertySelector::_confirmed() {
  268. TreeItem *ti = search_options->get_selected();
  269. if (!ti) {
  270. return;
  271. }
  272. emit_signal(SNAME("selected"), ti->get_metadata(0));
  273. hide();
  274. }
  275. void PropertySelector::_item_selected() {
  276. help_bit->set_custom_text(String(), String(), String());
  277. TreeItem *item = search_options->get_selected();
  278. get_ok_button()->set_disabled(item == nullptr);
  279. if (!item) {
  280. return;
  281. }
  282. String name = item->get_metadata(0);
  283. String class_type;
  284. if (type != Variant::NIL) {
  285. class_type = Variant::get_type_name(type);
  286. } else if (!base_type.is_empty()) {
  287. class_type = base_type;
  288. } else if (instance) {
  289. class_type = instance->get_class();
  290. }
  291. String text;
  292. while (!class_type.is_empty()) {
  293. if (properties) {
  294. if (ClassDB::has_property(class_type, name, true)) {
  295. help_bit->parse_symbol("property|" + class_type + "|" + name);
  296. break;
  297. }
  298. } else {
  299. if (ClassDB::has_method(class_type, name, true)) {
  300. help_bit->parse_symbol("method|" + class_type + "|" + name);
  301. break;
  302. }
  303. }
  304. // It may be from a parent class, keep looking.
  305. class_type = ClassDB::get_parent_class(class_type);
  306. }
  307. }
  308. void PropertySelector::_hide_requested() {
  309. _cancel_pressed(); // From AcceptDialog.
  310. }
  311. void PropertySelector::_create_subproperties(TreeItem *p_parent_item, Variant::Type p_type) {
  312. switch (p_type) {
  313. case Variant::VECTOR2: {
  314. _create_subproperty(p_parent_item, "x", Variant::FLOAT);
  315. _create_subproperty(p_parent_item, "y", Variant::FLOAT);
  316. } break;
  317. case Variant::VECTOR2I: {
  318. _create_subproperty(p_parent_item, "x", Variant::INT);
  319. _create_subproperty(p_parent_item, "y", Variant::INT);
  320. } break;
  321. case Variant::RECT2: {
  322. _create_subproperty(p_parent_item, "position", Variant::VECTOR2);
  323. _create_subproperty(p_parent_item, "size", Variant::VECTOR2);
  324. _create_subproperty(p_parent_item, "end", Variant::VECTOR2);
  325. } break;
  326. case Variant::RECT2I: {
  327. _create_subproperty(p_parent_item, "position", Variant::VECTOR2I);
  328. _create_subproperty(p_parent_item, "size", Variant::VECTOR2I);
  329. _create_subproperty(p_parent_item, "end", Variant::VECTOR2I);
  330. } break;
  331. case Variant::VECTOR3: {
  332. _create_subproperty(p_parent_item, "x", Variant::FLOAT);
  333. _create_subproperty(p_parent_item, "y", Variant::FLOAT);
  334. _create_subproperty(p_parent_item, "z", Variant::FLOAT);
  335. } break;
  336. case Variant::VECTOR3I: {
  337. _create_subproperty(p_parent_item, "x", Variant::INT);
  338. _create_subproperty(p_parent_item, "y", Variant::INT);
  339. _create_subproperty(p_parent_item, "z", Variant::INT);
  340. } break;
  341. case Variant::TRANSFORM2D: {
  342. _create_subproperty(p_parent_item, "origin", Variant::VECTOR2);
  343. _create_subproperty(p_parent_item, "x", Variant::VECTOR2);
  344. _create_subproperty(p_parent_item, "y", Variant::VECTOR2);
  345. } break;
  346. case Variant::VECTOR4: {
  347. _create_subproperty(p_parent_item, "x", Variant::FLOAT);
  348. _create_subproperty(p_parent_item, "y", Variant::FLOAT);
  349. _create_subproperty(p_parent_item, "z", Variant::FLOAT);
  350. _create_subproperty(p_parent_item, "w", Variant::FLOAT);
  351. } break;
  352. case Variant::VECTOR4I: {
  353. _create_subproperty(p_parent_item, "x", Variant::INT);
  354. _create_subproperty(p_parent_item, "y", Variant::INT);
  355. _create_subproperty(p_parent_item, "z", Variant::INT);
  356. _create_subproperty(p_parent_item, "w", Variant::INT);
  357. } break;
  358. case Variant::PLANE: {
  359. _create_subproperty(p_parent_item, "x", Variant::FLOAT);
  360. _create_subproperty(p_parent_item, "y", Variant::FLOAT);
  361. _create_subproperty(p_parent_item, "z", Variant::FLOAT);
  362. _create_subproperty(p_parent_item, "normal", Variant::VECTOR3);
  363. _create_subproperty(p_parent_item, "d", Variant::FLOAT);
  364. } break;
  365. case Variant::QUATERNION: {
  366. _create_subproperty(p_parent_item, "x", Variant::FLOAT);
  367. _create_subproperty(p_parent_item, "y", Variant::FLOAT);
  368. _create_subproperty(p_parent_item, "z", Variant::FLOAT);
  369. _create_subproperty(p_parent_item, "w", Variant::FLOAT);
  370. } break;
  371. case Variant::AABB: {
  372. _create_subproperty(p_parent_item, "position", Variant::VECTOR3);
  373. _create_subproperty(p_parent_item, "size", Variant::VECTOR3);
  374. _create_subproperty(p_parent_item, "end", Variant::VECTOR3);
  375. } break;
  376. case Variant::BASIS: {
  377. _create_subproperty(p_parent_item, "x", Variant::VECTOR3);
  378. _create_subproperty(p_parent_item, "y", Variant::VECTOR3);
  379. _create_subproperty(p_parent_item, "z", Variant::VECTOR3);
  380. } break;
  381. case Variant::TRANSFORM3D: {
  382. _create_subproperty(p_parent_item, "basis", Variant::BASIS);
  383. _create_subproperty(p_parent_item, "origin", Variant::VECTOR3);
  384. } break;
  385. case Variant::PROJECTION: {
  386. _create_subproperty(p_parent_item, "x", Variant::VECTOR4);
  387. _create_subproperty(p_parent_item, "y", Variant::VECTOR4);
  388. _create_subproperty(p_parent_item, "z", Variant::VECTOR4);
  389. _create_subproperty(p_parent_item, "w", Variant::VECTOR4);
  390. } break;
  391. case Variant::COLOR: {
  392. _create_subproperty(p_parent_item, "r", Variant::FLOAT);
  393. _create_subproperty(p_parent_item, "g", Variant::FLOAT);
  394. _create_subproperty(p_parent_item, "b", Variant::FLOAT);
  395. _create_subproperty(p_parent_item, "a", Variant::FLOAT);
  396. _create_subproperty(p_parent_item, "r8", Variant::INT);
  397. _create_subproperty(p_parent_item, "g8", Variant::INT);
  398. _create_subproperty(p_parent_item, "b8", Variant::INT);
  399. _create_subproperty(p_parent_item, "a8", Variant::INT);
  400. _create_subproperty(p_parent_item, "h", Variant::FLOAT);
  401. _create_subproperty(p_parent_item, "s", Variant::FLOAT);
  402. _create_subproperty(p_parent_item, "v", Variant::FLOAT);
  403. } break;
  404. default: {
  405. }
  406. }
  407. }
  408. void PropertySelector::_create_subproperty(TreeItem *p_parent_item, const String &p_name, Variant::Type p_type) {
  409. if (!type_filter.is_empty() && !type_filter.has(p_type)) {
  410. return;
  411. }
  412. TreeItem *item = search_options->create_item(p_parent_item);
  413. item->set_text(0, p_name);
  414. item->set_metadata(0, String(p_parent_item->get_metadata(0)) + ":" + p_name);
  415. item->set_icon(0, search_options->get_editor_theme_icon(Variant::get_type_name(p_type)));
  416. _create_subproperties(item, p_type);
  417. }
  418. void PropertySelector::_notification(int p_what) {
  419. switch (p_what) {
  420. case NOTIFICATION_ENTER_TREE: {
  421. connect(SceneStringName(confirmed), callable_mp(this, &PropertySelector::_confirmed));
  422. } break;
  423. case NOTIFICATION_EXIT_TREE: {
  424. disconnect(SceneStringName(confirmed), callable_mp(this, &PropertySelector::_confirmed));
  425. } break;
  426. }
  427. }
  428. void PropertySelector::select_method_from_base_type(const String &p_base, const String &p_current, bool p_virtuals_only) {
  429. base_type = p_base;
  430. selected = p_current;
  431. type = Variant::NIL;
  432. script = ObjectID();
  433. properties = false;
  434. instance = nullptr;
  435. virtuals_only = p_virtuals_only;
  436. popup_centered_ratio(0.6);
  437. search_box->set_text("");
  438. search_box->grab_focus();
  439. _update_search();
  440. }
  441. void PropertySelector::select_method_from_script(const Ref<Script> &p_script, const String &p_current) {
  442. ERR_FAIL_COND(p_script.is_null());
  443. base_type = p_script->get_instance_base_type();
  444. selected = p_current;
  445. type = Variant::NIL;
  446. script = p_script->get_instance_id();
  447. properties = false;
  448. instance = nullptr;
  449. virtuals_only = false;
  450. popup_centered_ratio(0.6);
  451. search_box->set_text("");
  452. search_box->grab_focus();
  453. _update_search();
  454. }
  455. void PropertySelector::select_method_from_basic_type(Variant::Type p_type, const String &p_current) {
  456. ERR_FAIL_COND(p_type == Variant::NIL);
  457. base_type = "";
  458. selected = p_current;
  459. type = p_type;
  460. script = ObjectID();
  461. properties = false;
  462. instance = nullptr;
  463. virtuals_only = false;
  464. popup_centered_ratio(0.6);
  465. search_box->set_text("");
  466. search_box->grab_focus();
  467. _update_search();
  468. }
  469. void PropertySelector::select_method_from_instance(Object *p_instance, const String &p_current) {
  470. base_type = p_instance->get_class();
  471. selected = p_current;
  472. type = Variant::NIL;
  473. script = ObjectID();
  474. {
  475. Ref<Script> scr = p_instance->get_script();
  476. if (scr.is_valid()) {
  477. script = scr->get_instance_id();
  478. }
  479. }
  480. properties = false;
  481. instance = nullptr;
  482. virtuals_only = false;
  483. popup_centered_ratio(0.6);
  484. search_box->set_text("");
  485. search_box->grab_focus();
  486. _update_search();
  487. }
  488. void PropertySelector::select_property_from_base_type(const String &p_base, const String &p_current) {
  489. base_type = p_base;
  490. selected = p_current;
  491. type = Variant::NIL;
  492. script = ObjectID();
  493. properties = true;
  494. instance = nullptr;
  495. virtuals_only = false;
  496. popup_centered_ratio(0.6);
  497. search_box->set_text("");
  498. search_box->grab_focus();
  499. _update_search();
  500. }
  501. void PropertySelector::select_property_from_script(const Ref<Script> &p_script, const String &p_current) {
  502. ERR_FAIL_COND(p_script.is_null());
  503. base_type = p_script->get_instance_base_type();
  504. selected = p_current;
  505. type = Variant::NIL;
  506. script = p_script->get_instance_id();
  507. properties = true;
  508. instance = nullptr;
  509. virtuals_only = false;
  510. popup_centered_ratio(0.6);
  511. search_box->set_text("");
  512. search_box->grab_focus();
  513. _update_search();
  514. }
  515. void PropertySelector::select_property_from_basic_type(Variant::Type p_type, const String &p_current) {
  516. ERR_FAIL_COND(p_type == Variant::NIL);
  517. base_type = "";
  518. selected = p_current;
  519. type = p_type;
  520. script = ObjectID();
  521. properties = true;
  522. instance = nullptr;
  523. virtuals_only = false;
  524. popup_centered_ratio(0.6);
  525. search_box->set_text("");
  526. search_box->grab_focus();
  527. _update_search();
  528. }
  529. void PropertySelector::select_property_from_instance(Object *p_instance, const String &p_current) {
  530. base_type = "";
  531. selected = p_current;
  532. type = Variant::NIL;
  533. script = ObjectID();
  534. properties = true;
  535. instance = p_instance;
  536. virtuals_only = false;
  537. popup_centered_ratio(0.6);
  538. search_box->set_text("");
  539. search_box->grab_focus();
  540. _update_search();
  541. }
  542. void PropertySelector::set_type_filter(const Vector<Variant::Type> &p_type_filter) {
  543. type_filter = p_type_filter;
  544. }
  545. void PropertySelector::_bind_methods() {
  546. ADD_SIGNAL(MethodInfo("selected", PropertyInfo(Variant::STRING, "name")));
  547. }
  548. PropertySelector::PropertySelector() {
  549. VBoxContainer *vbc = memnew(VBoxContainer);
  550. add_child(vbc);
  551. //set_child_rect(vbc);
  552. search_box = memnew(LineEdit);
  553. vbc->add_margin_child(TTR("Search:"), search_box);
  554. search_box->connect(SceneStringName(text_changed), callable_mp(this, &PropertySelector::_text_changed));
  555. search_box->connect(SceneStringName(gui_input), callable_mp(this, &PropertySelector::_sbox_input));
  556. search_options = memnew(Tree);
  557. search_options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  558. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  559. set_ok_button_text(TTR("Open"));
  560. get_ok_button()->set_disabled(true);
  561. register_text_enter(search_box);
  562. set_hide_on_ok(false);
  563. search_options->connect("item_activated", callable_mp(this, &PropertySelector::_confirmed));
  564. search_options->connect("cell_selected", callable_mp(this, &PropertySelector::_item_selected));
  565. search_options->set_hide_root(true);
  566. help_bit = memnew(EditorHelpBit);
  567. help_bit->set_content_height_limits(80 * EDSCALE, 80 * EDSCALE);
  568. help_bit->connect("request_hide", callable_mp(this, &PropertySelector::_hide_requested));
  569. vbc->add_margin_child(TTR("Description:"), help_bit);
  570. }