property_selector.cpp 22 KB

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