editor_properties_array_dict.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  1. /**************************************************************************/
  2. /* editor_properties_array_dict.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 "editor_properties_array_dict.h"
  31. #include "core/input/input.h"
  32. #include "core/io/marshalls.h"
  33. #include "editor/editor_properties.h"
  34. #include "editor/editor_properties_vector.h"
  35. #include "editor/editor_scale.h"
  36. #include "editor/editor_settings.h"
  37. #include "editor/gui/editor_spin_slider.h"
  38. #include "editor/inspector_dock.h"
  39. #include "scene/gui/button.h"
  40. #include "scene/resources/packed_scene.h"
  41. bool EditorPropertyArrayObject::_set(const StringName &p_name, const Variant &p_value) {
  42. String name = p_name;
  43. if (!name.begins_with("indices")) {
  44. return false;
  45. }
  46. int index;
  47. if (name.begins_with("metadata/")) {
  48. index = name.get_slice("/", 2).to_int();
  49. } else {
  50. index = name.get_slice("/", 1).to_int();
  51. }
  52. array.set(index, p_value);
  53. return true;
  54. }
  55. bool EditorPropertyArrayObject::_get(const StringName &p_name, Variant &r_ret) const {
  56. String name = p_name;
  57. if (!name.begins_with("indices")) {
  58. return false;
  59. }
  60. int index;
  61. if (name.begins_with("metadata/")) {
  62. index = name.get_slice("/", 2).to_int();
  63. } else {
  64. index = name.get_slice("/", 1).to_int();
  65. }
  66. bool valid;
  67. r_ret = array.get(index, &valid);
  68. if (r_ret.get_type() == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(r_ret)) {
  69. r_ret = Object::cast_to<EncodedObjectAsID>(r_ret)->get_object_id();
  70. }
  71. return valid;
  72. }
  73. void EditorPropertyArrayObject::set_array(const Variant &p_array) {
  74. array = p_array;
  75. }
  76. Variant EditorPropertyArrayObject::get_array() {
  77. return array;
  78. }
  79. EditorPropertyArrayObject::EditorPropertyArrayObject() {
  80. }
  81. ///////////////////
  82. bool EditorPropertyDictionaryObject::_set(const StringName &p_name, const Variant &p_value) {
  83. String name = p_name;
  84. if (name == "new_item_key") {
  85. new_item_key = p_value;
  86. return true;
  87. }
  88. if (name == "new_item_value") {
  89. new_item_value = p_value;
  90. return true;
  91. }
  92. if (name.begins_with("indices")) {
  93. int index = name.get_slicec('/', 1).to_int();
  94. Variant key = dict.get_key_at_index(index);
  95. dict[key] = p_value;
  96. return true;
  97. }
  98. return false;
  99. }
  100. bool EditorPropertyDictionaryObject::_get(const StringName &p_name, Variant &r_ret) const {
  101. String name = p_name;
  102. if (name == "new_item_key") {
  103. r_ret = new_item_key;
  104. return true;
  105. }
  106. if (name == "new_item_value") {
  107. r_ret = new_item_value;
  108. return true;
  109. }
  110. if (name.begins_with("indices")) {
  111. int index = name.get_slicec('/', 1).to_int();
  112. Variant key = dict.get_key_at_index(index);
  113. r_ret = dict[key];
  114. if (r_ret.get_type() == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(r_ret)) {
  115. r_ret = Object::cast_to<EncodedObjectAsID>(r_ret)->get_object_id();
  116. }
  117. return true;
  118. }
  119. return false;
  120. }
  121. void EditorPropertyDictionaryObject::set_dict(const Dictionary &p_dict) {
  122. dict = p_dict;
  123. }
  124. Dictionary EditorPropertyDictionaryObject::get_dict() {
  125. return dict;
  126. }
  127. void EditorPropertyDictionaryObject::set_new_item_key(const Variant &p_new_item) {
  128. new_item_key = p_new_item;
  129. }
  130. Variant EditorPropertyDictionaryObject::get_new_item_key() {
  131. return new_item_key;
  132. }
  133. void EditorPropertyDictionaryObject::set_new_item_value(const Variant &p_new_item) {
  134. new_item_value = p_new_item;
  135. }
  136. Variant EditorPropertyDictionaryObject::get_new_item_value() {
  137. return new_item_value;
  138. }
  139. EditorPropertyDictionaryObject::EditorPropertyDictionaryObject() {
  140. }
  141. ///////////////////// ARRAY ///////////////////////////
  142. void EditorPropertyArray::initialize_array(Variant &p_array) {
  143. if (array_type == Variant::ARRAY && subtype != Variant::NIL) {
  144. Array array;
  145. StringName subtype_class;
  146. Ref<Script> subtype_script;
  147. if (subtype == Variant::OBJECT && !subtype_hint_string.is_empty()) {
  148. if (ClassDB::class_exists(subtype_hint_string)) {
  149. subtype_class = subtype_hint_string;
  150. }
  151. }
  152. array.set_typed(subtype, subtype_class, subtype_script);
  153. p_array = array;
  154. } else {
  155. VariantInternal::initialize(&p_array, array_type);
  156. }
  157. }
  158. void EditorPropertyArray::_property_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing) {
  159. if (!p_property.begins_with("indices")) {
  160. return;
  161. }
  162. int index;
  163. if (p_property.begins_with("metadata/")) {
  164. index = p_property.get_slice("/", 2).to_int();
  165. } else {
  166. index = p_property.get_slice("/", 1).to_int();
  167. }
  168. Variant array = object->get_array().duplicate();
  169. array.set(index, p_value);
  170. object->set_array(array);
  171. emit_changed(get_edited_property(), array, "", true);
  172. }
  173. void EditorPropertyArray::_change_type(Object *p_button, int p_index) {
  174. Button *button = Object::cast_to<Button>(p_button);
  175. changing_type_index = p_index;
  176. Rect2 rect = button->get_screen_rect();
  177. change_type->reset_size();
  178. change_type->set_position(rect.get_end() - Vector2(change_type->get_contents_minimum_size().x, 0));
  179. change_type->popup();
  180. }
  181. void EditorPropertyArray::_change_type_menu(int p_index) {
  182. if (p_index == Variant::VARIANT_MAX) {
  183. _remove_pressed(changing_type_index);
  184. return;
  185. }
  186. Variant value;
  187. VariantInternal::initialize(&value, Variant::Type(p_index));
  188. Variant array = object->get_array().duplicate();
  189. array.set(changing_type_index, value);
  190. emit_changed(get_edited_property(), array, "", true);
  191. update_property();
  192. }
  193. void EditorPropertyArray::_object_id_selected(const StringName &p_property, ObjectID p_id) {
  194. emit_signal(SNAME("object_id_selected"), p_property, p_id);
  195. }
  196. void EditorPropertyArray::update_property() {
  197. Variant array = get_edited_property_value();
  198. String array_type_name = Variant::get_type_name(array_type);
  199. if (array_type == Variant::ARRAY && subtype != Variant::NIL) {
  200. String type_name;
  201. if (subtype == Variant::OBJECT && (subtype_hint == PROPERTY_HINT_RESOURCE_TYPE || subtype_hint == PROPERTY_HINT_NODE_TYPE)) {
  202. type_name = subtype_hint_string;
  203. } else {
  204. type_name = Variant::get_type_name(subtype);
  205. }
  206. array_type_name = vformat("%s[%s]", array_type_name, type_name);
  207. }
  208. if (!array.is_array()) {
  209. edit->set_text(vformat(TTR("(Nil) %s"), array_type_name));
  210. edit->set_pressed(false);
  211. if (container) {
  212. set_bottom_editor(nullptr);
  213. memdelete(container);
  214. button_add_item = nullptr;
  215. container = nullptr;
  216. }
  217. return;
  218. }
  219. object->set_array(array);
  220. int size = array.call("size");
  221. int max_page = MAX(0, size - 1) / page_length;
  222. page_index = MIN(page_index, max_page);
  223. int offset = page_index * page_length;
  224. edit->set_text(vformat(TTR("%s (size %s)"), array_type_name, itos(size)));
  225. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  226. if (edit->is_pressed() != unfolded) {
  227. edit->set_pressed(unfolded);
  228. }
  229. if (unfolded) {
  230. updating = true;
  231. if (!container) {
  232. container = memnew(MarginContainer);
  233. container->set_theme_type_variation("MarginContainer4px");
  234. container->set_mouse_filter(MOUSE_FILTER_STOP);
  235. add_child(container);
  236. set_bottom_editor(container);
  237. VBoxContainer *vbox = memnew(VBoxContainer);
  238. container->add_child(vbox);
  239. HBoxContainer *hbox = memnew(HBoxContainer);
  240. vbox->add_child(hbox);
  241. Label *size_label = memnew(Label(TTR("Size:")));
  242. size_label->set_h_size_flags(SIZE_EXPAND_FILL);
  243. hbox->add_child(size_label);
  244. size_slider = memnew(EditorSpinSlider);
  245. size_slider->set_step(1);
  246. size_slider->set_max(INT32_MAX);
  247. size_slider->set_h_size_flags(SIZE_EXPAND_FILL);
  248. size_slider->set_read_only(is_read_only());
  249. size_slider->connect("value_changed", callable_mp(this, &EditorPropertyArray::_length_changed));
  250. hbox->add_child(size_slider);
  251. property_vbox = memnew(VBoxContainer);
  252. property_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  253. vbox->add_child(property_vbox);
  254. button_add_item = EditorInspector::create_inspector_action_button(TTR("Add Element"));
  255. button_add_item->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  256. button_add_item->connect(SNAME("pressed"), callable_mp(this, &EditorPropertyArray::_add_element));
  257. button_add_item->set_disabled(is_read_only());
  258. vbox->add_child(button_add_item);
  259. paginator = memnew(EditorPaginator);
  260. paginator->connect("page_changed", callable_mp(this, &EditorPropertyArray::_page_changed));
  261. vbox->add_child(paginator);
  262. } else {
  263. // Bye bye children of the box.
  264. for (int i = property_vbox->get_child_count() - 1; i >= 0; i--) {
  265. Node *child = property_vbox->get_child(i);
  266. if (child == reorder_selected_element_hbox) {
  267. continue; // Don't remove the property that the user is moving.
  268. }
  269. child->queue_free(); // Button still needed after pressed is called.
  270. property_vbox->remove_child(child);
  271. }
  272. }
  273. size_slider->set_value(size);
  274. property_vbox->set_visible(size > 0);
  275. button_add_item->set_visible(page_index == max_page);
  276. paginator->update(page_index, max_page);
  277. paginator->set_visible(max_page > 0);
  278. int amount = MIN(size - offset, page_length);
  279. for (int i = 0; i < amount; i++) {
  280. bool reorder_is_from_current_page = reorder_from_index / page_length == page_index;
  281. if (reorder_is_from_current_page && i == reorder_from_index % page_length) {
  282. // Don't duplicate the property that the user is moving.
  283. continue;
  284. }
  285. if (!reorder_is_from_current_page && i == reorder_to_index % page_length) {
  286. // Don't create the property the moving property will take the place of,
  287. // e.g. (if page_length == 20) don't create element 20 if dragging an item from
  288. // the first page to the second page because element 20 would become element 19.
  289. continue;
  290. }
  291. HBoxContainer *hbox = memnew(HBoxContainer);
  292. property_vbox->add_child(hbox);
  293. Button *reorder_button = memnew(Button);
  294. reorder_button->set_icon(get_theme_icon(SNAME("TripleBar"), SNAME("EditorIcons")));
  295. reorder_button->set_default_cursor_shape(Control::CURSOR_MOVE);
  296. reorder_button->set_disabled(is_read_only());
  297. reorder_button->connect("gui_input", callable_mp(this, &EditorPropertyArray::_reorder_button_gui_input));
  298. reorder_button->connect("button_down", callable_mp(this, &EditorPropertyArray::_reorder_button_down).bind(i + offset));
  299. reorder_button->connect("button_up", callable_mp(this, &EditorPropertyArray::_reorder_button_up));
  300. hbox->add_child(reorder_button);
  301. String prop_name = "indices/" + itos(i + offset);
  302. EditorProperty *prop = nullptr;
  303. Variant value = array.get(i + offset);
  304. Variant::Type value_type = value.get_type();
  305. if (value_type == Variant::NIL && subtype != Variant::NIL) {
  306. value_type = subtype;
  307. }
  308. if (value_type == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(value)) {
  309. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  310. editor->setup("Object");
  311. prop = editor;
  312. } else {
  313. prop = EditorInspector::instantiate_property_editor(nullptr, value_type, "", subtype_hint, subtype_hint_string, PROPERTY_USAGE_NONE);
  314. }
  315. prop->set_object_and_property(object.ptr(), prop_name);
  316. prop->set_label(itos(i + offset));
  317. prop->set_selectable(false);
  318. prop->set_use_folding(is_using_folding());
  319. prop->connect("property_changed", callable_mp(this, &EditorPropertyArray::_property_changed));
  320. prop->connect("object_id_selected", callable_mp(this, &EditorPropertyArray::_object_id_selected));
  321. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  322. prop->set_read_only(is_read_only());
  323. hbox->add_child(prop);
  324. bool is_untyped_array = array.get_type() == Variant::ARRAY && subtype == Variant::NIL;
  325. if (is_untyped_array) {
  326. Button *edit_btn = memnew(Button);
  327. edit_btn->set_icon(get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")));
  328. hbox->add_child(edit_btn);
  329. edit_btn->set_disabled(is_read_only());
  330. edit_btn->connect("pressed", callable_mp(this, &EditorPropertyArray::_change_type).bind(edit_btn, i + offset));
  331. } else {
  332. Button *remove_btn = memnew(Button);
  333. remove_btn->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  334. remove_btn->set_disabled(is_read_only());
  335. remove_btn->connect("pressed", callable_mp(this, &EditorPropertyArray::_remove_pressed).bind(i + offset));
  336. hbox->add_child(remove_btn);
  337. }
  338. prop->update_property();
  339. }
  340. if (reorder_to_index % page_length > 0) {
  341. property_vbox->move_child(property_vbox->get_child(0), reorder_to_index % page_length);
  342. }
  343. updating = false;
  344. } else {
  345. if (container) {
  346. set_bottom_editor(nullptr);
  347. memdelete(container);
  348. button_add_item = nullptr;
  349. container = nullptr;
  350. }
  351. }
  352. }
  353. void EditorPropertyArray::_remove_pressed(int p_index) {
  354. Variant array = object->get_array().duplicate();
  355. array.call("remove_at", p_index);
  356. emit_changed(get_edited_property(), array, "", false);
  357. update_property();
  358. }
  359. void EditorPropertyArray::_button_draw() {
  360. if (dropping) {
  361. Color color = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  362. edit->draw_rect(Rect2(Point2(), edit->get_size()), color, false);
  363. }
  364. }
  365. bool EditorPropertyArray::_is_drop_valid(const Dictionary &p_drag_data) const {
  366. if (is_read_only()) {
  367. return false;
  368. }
  369. String allowed_type = Variant::get_type_name(subtype);
  370. // When the subtype is of type Object, an additional subtype may be specified in the hint string
  371. // (e.g. Resource, Texture2D, ShaderMaterial, etc). We want the allowed type to be that, not just "Object".
  372. if (subtype == Variant::OBJECT && !subtype_hint_string.is_empty()) {
  373. allowed_type = subtype_hint_string;
  374. }
  375. Dictionary drag_data = p_drag_data;
  376. if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  377. Vector<String> files = drag_data["files"];
  378. for (int i = 0; i < files.size(); i++) {
  379. String file = files[i];
  380. String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
  381. for (int j = 0; j < allowed_type.get_slice_count(","); j++) {
  382. String at = allowed_type.get_slice(",", j).strip_edges();
  383. // Fail if one of the files is not of allowed type.
  384. if (!ClassDB::is_parent_class(ftype, at)) {
  385. return false;
  386. }
  387. }
  388. }
  389. // If no files fail, drop is valid.
  390. return true;
  391. }
  392. return false;
  393. }
  394. bool EditorPropertyArray::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  395. return _is_drop_valid(p_data);
  396. }
  397. void EditorPropertyArray::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  398. ERR_FAIL_COND(!_is_drop_valid(p_data));
  399. Dictionary drag_data = p_data;
  400. if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  401. Vector<String> files = drag_data["files"];
  402. Variant array = object->get_array();
  403. // Handle the case where array is not initialized yet.
  404. if (!array.is_array()) {
  405. initialize_array(array);
  406. } else {
  407. array = array.duplicate();
  408. }
  409. // Loop the file array and add to existing array.
  410. for (int i = 0; i < files.size(); i++) {
  411. String file = files[i];
  412. Ref<Resource> res = ResourceLoader::load(file);
  413. if (res.is_valid()) {
  414. array.call("push_back", res);
  415. }
  416. }
  417. emit_changed(get_edited_property(), array, "", false);
  418. update_property();
  419. }
  420. }
  421. void EditorPropertyArray::_notification(int p_what) {
  422. switch (p_what) {
  423. case NOTIFICATION_THEME_CHANGED:
  424. case NOTIFICATION_ENTER_TREE: {
  425. change_type->clear();
  426. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  427. if (i == Variant::CALLABLE || i == Variant::SIGNAL || i == Variant::RID) {
  428. // These types can't be constructed or serialized properly, so skip them.
  429. continue;
  430. }
  431. String type = Variant::get_type_name(Variant::Type(i));
  432. change_type->add_icon_item(get_theme_icon(type, SNAME("EditorIcons")), type, i);
  433. }
  434. change_type->add_separator();
  435. change_type->add_icon_item(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), TTR("Remove Item"), Variant::VARIANT_MAX);
  436. if (button_add_item) {
  437. button_add_item->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  438. }
  439. } break;
  440. case NOTIFICATION_DRAG_BEGIN: {
  441. if (is_visible_in_tree()) {
  442. if (_is_drop_valid(get_viewport()->gui_get_drag_data())) {
  443. dropping = true;
  444. edit->queue_redraw();
  445. }
  446. }
  447. } break;
  448. case NOTIFICATION_DRAG_END: {
  449. if (dropping) {
  450. dropping = false;
  451. edit->queue_redraw();
  452. }
  453. } break;
  454. }
  455. }
  456. void EditorPropertyArray::_edit_pressed() {
  457. Variant array = get_edited_property_value();
  458. if (!array.is_array() && edit->is_pressed()) {
  459. initialize_array(array);
  460. get_edited_object()->set(get_edited_property(), array);
  461. }
  462. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  463. update_property();
  464. }
  465. void EditorPropertyArray::_page_changed(int p_page) {
  466. if (updating) {
  467. return;
  468. }
  469. page_index = p_page;
  470. update_property();
  471. }
  472. void EditorPropertyArray::_length_changed(double p_page) {
  473. if (updating) {
  474. return;
  475. }
  476. Variant array = object->get_array().duplicate();
  477. array.call("resize", int(p_page));
  478. emit_changed(get_edited_property(), array, "", false);
  479. update_property();
  480. }
  481. void EditorPropertyArray::_add_element() {
  482. _length_changed(double(object->get_array().call("size")) + 1.0);
  483. }
  484. void EditorPropertyArray::setup(Variant::Type p_array_type, const String &p_hint_string) {
  485. array_type = p_array_type;
  486. // The format of p_hint_string is:
  487. // subType/subTypeHint:nextSubtype ... etc.
  488. if (!p_hint_string.is_empty()) {
  489. int hint_subtype_separator = p_hint_string.find(":");
  490. if (hint_subtype_separator >= 0) {
  491. String subtype_string = p_hint_string.substr(0, hint_subtype_separator);
  492. int slash_pos = subtype_string.find("/");
  493. if (slash_pos >= 0) {
  494. subtype_hint = PropertyHint(subtype_string.substr(slash_pos + 1, subtype_string.size() - slash_pos - 1).to_int());
  495. subtype_string = subtype_string.substr(0, slash_pos);
  496. }
  497. subtype_hint_string = p_hint_string.substr(hint_subtype_separator + 1, p_hint_string.size() - hint_subtype_separator - 1);
  498. subtype = Variant::Type(subtype_string.to_int());
  499. }
  500. }
  501. }
  502. void EditorPropertyArray::_reorder_button_gui_input(const Ref<InputEvent> &p_event) {
  503. if (reorder_from_index < 0 || is_read_only()) {
  504. return;
  505. }
  506. Ref<InputEventMouseMotion> mm = p_event;
  507. if (mm.is_valid()) {
  508. Variant array = object->get_array();
  509. int size = array.call("size");
  510. // Cumulate the mouse delta, many small changes (dragging slowly) should result in reordering at some point.
  511. reorder_mouse_y_delta += mm->get_relative().y;
  512. // Reordering is done by moving the dragged element by +1/-1 index at a time based on the cumulated mouse delta so if
  513. // already at the array bounds make sure to ignore the remaining out of bounds drag (by resetting the cumulated delta).
  514. if ((reorder_to_index == 0 && reorder_mouse_y_delta < 0.0f) || (reorder_to_index == size - 1 && reorder_mouse_y_delta > 0.0f)) {
  515. reorder_mouse_y_delta = 0.0f;
  516. return;
  517. }
  518. float required_y_distance = 20.0f * EDSCALE;
  519. if (ABS(reorder_mouse_y_delta) > required_y_distance) {
  520. int direction = reorder_mouse_y_delta > 0.0f ? 1 : -1;
  521. reorder_mouse_y_delta -= required_y_distance * direction;
  522. reorder_to_index += direction;
  523. if ((direction < 0 && reorder_to_index % page_length == page_length - 1) || (direction > 0 && reorder_to_index % page_length == 0)) {
  524. // Automatically move to the next/previous page.
  525. _page_changed(page_index + direction);
  526. }
  527. property_vbox->move_child(reorder_selected_element_hbox, reorder_to_index % page_length);
  528. // Ensure the moving element is visible.
  529. InspectorDock::get_inspector_singleton()->ensure_control_visible(reorder_selected_element_hbox);
  530. }
  531. }
  532. }
  533. void EditorPropertyArray::_reorder_button_down(int p_index) {
  534. if (is_read_only()) {
  535. return;
  536. }
  537. reorder_from_index = p_index;
  538. reorder_to_index = p_index;
  539. reorder_selected_element_hbox = Object::cast_to<HBoxContainer>(property_vbox->get_child(p_index % page_length));
  540. reorder_selected_button = Object::cast_to<Button>(reorder_selected_element_hbox->get_child(0));
  541. // Ideally it'd to be able to show the mouse but I had issues with
  542. // Control's `mouse_exit()`/`mouse_entered()` signals not getting called.
  543. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  544. }
  545. void EditorPropertyArray::_reorder_button_up() {
  546. if (is_read_only()) {
  547. return;
  548. }
  549. if (reorder_from_index != reorder_to_index) {
  550. // Move the element.
  551. Variant array = object->get_array().duplicate();
  552. Variant value_to_move = array.get(reorder_from_index);
  553. array.call("remove_at", reorder_from_index);
  554. array.call("insert", reorder_to_index, value_to_move);
  555. emit_changed(get_edited_property(), array, "", false);
  556. update_property();
  557. }
  558. reorder_from_index = -1;
  559. reorder_to_index = -1;
  560. reorder_mouse_y_delta = 0.0f;
  561. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  562. ERR_FAIL_NULL(reorder_selected_button);
  563. reorder_selected_button->warp_mouse(reorder_selected_button->get_size() / 2.0f);
  564. reorder_selected_element_hbox = nullptr;
  565. reorder_selected_button = nullptr;
  566. }
  567. void EditorPropertyArray::_bind_methods() {
  568. }
  569. EditorPropertyArray::EditorPropertyArray() {
  570. object.instantiate();
  571. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  572. edit = memnew(Button);
  573. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  574. edit->set_clip_text(true);
  575. edit->connect("pressed", callable_mp(this, &EditorPropertyArray::_edit_pressed));
  576. edit->set_toggle_mode(true);
  577. SET_DRAG_FORWARDING_CD(edit, EditorPropertyArray);
  578. edit->connect("draw", callable_mp(this, &EditorPropertyArray::_button_draw));
  579. add_child(edit);
  580. add_focusable(edit);
  581. change_type = memnew(PopupMenu);
  582. add_child(change_type);
  583. change_type->connect("id_pressed", callable_mp(this, &EditorPropertyArray::_change_type_menu));
  584. changing_type_index = -1;
  585. subtype = Variant::NIL;
  586. subtype_hint = PROPERTY_HINT_NONE;
  587. subtype_hint_string = "";
  588. }
  589. ///////////////////// DICTIONARY ///////////////////////////
  590. void EditorPropertyDictionary::_property_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing) {
  591. if (p_property == "new_item_key") {
  592. object->set_new_item_key(p_value);
  593. } else if (p_property == "new_item_value") {
  594. object->set_new_item_value(p_value);
  595. } else if (p_property.begins_with("indices")) {
  596. int index = p_property.get_slice("/", 1).to_int();
  597. Dictionary dict = object->get_dict().duplicate();
  598. Variant key = dict.get_key_at_index(index);
  599. dict[key] = p_value;
  600. object->set_dict(dict);
  601. emit_changed(get_edited_property(), dict, "", true);
  602. }
  603. }
  604. void EditorPropertyDictionary::_change_type(Object *p_button, int p_index) {
  605. Button *button = Object::cast_to<Button>(p_button);
  606. Rect2 rect = button->get_screen_rect();
  607. change_type->set_item_disabled(change_type->get_item_index(Variant::VARIANT_MAX), p_index < 0);
  608. change_type->reset_size();
  609. change_type->set_position(rect.get_end() - Vector2(change_type->get_contents_minimum_size().x, 0));
  610. change_type->popup();
  611. changing_type_index = p_index;
  612. }
  613. void EditorPropertyDictionary::_add_key_value() {
  614. // Do not allow nil as valid key. I experienced errors with this
  615. if (object->get_new_item_key().get_type() == Variant::NIL) {
  616. return;
  617. }
  618. Dictionary dict = object->get_dict().duplicate();
  619. dict[object->get_new_item_key()] = object->get_new_item_value();
  620. object->set_new_item_key(Variant());
  621. object->set_new_item_value(Variant());
  622. emit_changed(get_edited_property(), dict, "", false);
  623. update_property();
  624. }
  625. void EditorPropertyDictionary::_change_type_menu(int p_index) {
  626. if (changing_type_index < 0) {
  627. Variant value;
  628. VariantInternal::initialize(&value, Variant::Type(p_index));
  629. if (changing_type_index == -1) {
  630. object->set_new_item_key(value);
  631. } else {
  632. object->set_new_item_value(value);
  633. }
  634. update_property();
  635. return;
  636. }
  637. Dictionary dict = object->get_dict().duplicate();
  638. if (p_index < Variant::VARIANT_MAX) {
  639. Variant value;
  640. VariantInternal::initialize(&value, Variant::Type(p_index));
  641. Variant key = dict.get_key_at_index(changing_type_index);
  642. dict[key] = value;
  643. } else {
  644. Variant key = dict.get_key_at_index(changing_type_index);
  645. dict.erase(key);
  646. }
  647. emit_changed(get_edited_property(), dict, "", false);
  648. update_property();
  649. }
  650. void EditorPropertyDictionary::setup(PropertyHint p_hint) {
  651. property_hint = p_hint;
  652. }
  653. void EditorPropertyDictionary::update_property() {
  654. Variant updated_val = get_edited_property_value();
  655. if (updated_val.get_type() == Variant::NIL) {
  656. edit->set_text(TTR("Dictionary (Nil)")); // This provides symmetry with the array property.
  657. edit->set_pressed(false);
  658. if (container) {
  659. set_bottom_editor(nullptr);
  660. memdelete(container);
  661. button_add_item = nullptr;
  662. container = nullptr;
  663. }
  664. return;
  665. }
  666. Dictionary dict = updated_val;
  667. object->set_dict(updated_val);
  668. edit->set_text(vformat(TTR("Dictionary (size %d)"), dict.size()));
  669. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  670. if (edit->is_pressed() != unfolded) {
  671. edit->set_pressed(unfolded);
  672. }
  673. if (unfolded) {
  674. updating = true;
  675. if (!container) {
  676. container = memnew(MarginContainer);
  677. container->set_theme_type_variation("MarginContainer4px");
  678. add_child(container);
  679. set_bottom_editor(container);
  680. VBoxContainer *vbox = memnew(VBoxContainer);
  681. container->add_child(vbox);
  682. property_vbox = memnew(VBoxContainer);
  683. property_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  684. vbox->add_child(property_vbox);
  685. paginator = memnew(EditorPaginator);
  686. paginator->connect("page_changed", callable_mp(this, &EditorPropertyDictionary::_page_changed));
  687. vbox->add_child(paginator);
  688. } else {
  689. // Queue children for deletion, deleting immediately might cause errors.
  690. for (int i = property_vbox->get_child_count() - 1; i >= 0; i--) {
  691. property_vbox->get_child(i)->queue_free();
  692. }
  693. }
  694. int size = dict.size();
  695. int max_page = MAX(0, size - 1) / page_length;
  696. page_index = MIN(page_index, max_page);
  697. paginator->update(page_index, max_page);
  698. paginator->set_visible(max_page > 0);
  699. int offset = page_index * page_length;
  700. int amount = MIN(size - offset, page_length);
  701. int total_amount = page_index == max_page ? amount + 2 : amount; // For the "Add Key/Value Pair" box on last page.
  702. VBoxContainer *add_vbox = nullptr;
  703. double default_float_step = EDITOR_GET("interface/inspector/default_float_step");
  704. for (int i = 0; i < total_amount; i++) {
  705. String prop_name;
  706. Variant key;
  707. Variant value;
  708. if (i < amount) {
  709. prop_name = "indices/" + itos(i + offset);
  710. key = dict.get_key_at_index(i + offset);
  711. value = dict.get_value_at_index(i + offset);
  712. } else if (i == amount) {
  713. prop_name = "new_item_key";
  714. value = object->get_new_item_key();
  715. } else if (i == amount + 1) {
  716. prop_name = "new_item_value";
  717. value = object->get_new_item_value();
  718. }
  719. EditorProperty *prop = nullptr;
  720. switch (value.get_type()) {
  721. case Variant::NIL: {
  722. prop = memnew(EditorPropertyNil);
  723. } break;
  724. // Atomic types.
  725. case Variant::BOOL: {
  726. prop = memnew(EditorPropertyCheck);
  727. } break;
  728. case Variant::INT: {
  729. EditorPropertyInteger *editor = memnew(EditorPropertyInteger);
  730. editor->setup(-100000, 100000, 1, false, true, true);
  731. prop = editor;
  732. } break;
  733. case Variant::FLOAT: {
  734. EditorPropertyFloat *editor = memnew(EditorPropertyFloat);
  735. editor->setup(-100000, 100000, default_float_step, true, false, true, true);
  736. prop = editor;
  737. } break;
  738. case Variant::STRING: {
  739. if (i != amount && property_hint == PROPERTY_HINT_MULTILINE_TEXT) {
  740. // If this is NOT the new key field and there's a multiline hint,
  741. // show the field as multiline
  742. prop = memnew(EditorPropertyMultilineText);
  743. } else {
  744. prop = memnew(EditorPropertyText);
  745. }
  746. } break;
  747. // Math types.
  748. case Variant::VECTOR2: {
  749. EditorPropertyVector2 *editor = memnew(EditorPropertyVector2);
  750. editor->setup(-100000, 100000, default_float_step, true);
  751. prop = editor;
  752. } break;
  753. case Variant::VECTOR2I: {
  754. EditorPropertyVector2i *editor = memnew(EditorPropertyVector2i);
  755. editor->setup(-100000, 100000);
  756. prop = editor;
  757. } break;
  758. case Variant::RECT2: {
  759. EditorPropertyRect2 *editor = memnew(EditorPropertyRect2);
  760. editor->setup(-100000, 100000, default_float_step, true);
  761. prop = editor;
  762. } break;
  763. case Variant::RECT2I: {
  764. EditorPropertyRect2i *editor = memnew(EditorPropertyRect2i);
  765. editor->setup(-100000, 100000);
  766. prop = editor;
  767. } break;
  768. case Variant::VECTOR3: {
  769. EditorPropertyVector3 *editor = memnew(EditorPropertyVector3);
  770. editor->setup(-100000, 100000, default_float_step, true);
  771. prop = editor;
  772. } break;
  773. case Variant::VECTOR3I: {
  774. EditorPropertyVector3i *editor = memnew(EditorPropertyVector3i);
  775. editor->setup(-100000, 100000);
  776. prop = editor;
  777. } break;
  778. case Variant::VECTOR4: {
  779. EditorPropertyVector4 *editor = memnew(EditorPropertyVector4);
  780. editor->setup(-100000, 100000, default_float_step, true);
  781. prop = editor;
  782. } break;
  783. case Variant::VECTOR4I: {
  784. EditorPropertyVector4i *editor = memnew(EditorPropertyVector4i);
  785. editor->setup(-100000, 100000);
  786. prop = editor;
  787. } break;
  788. case Variant::TRANSFORM2D: {
  789. EditorPropertyTransform2D *editor = memnew(EditorPropertyTransform2D);
  790. editor->setup(-100000, 100000, default_float_step, true);
  791. prop = editor;
  792. } break;
  793. case Variant::PLANE: {
  794. EditorPropertyPlane *editor = memnew(EditorPropertyPlane);
  795. editor->setup(-100000, 100000, default_float_step, true);
  796. prop = editor;
  797. } break;
  798. case Variant::QUATERNION: {
  799. EditorPropertyQuaternion *editor = memnew(EditorPropertyQuaternion);
  800. editor->setup(-100000, 100000, default_float_step, true);
  801. prop = editor;
  802. } break;
  803. case Variant::AABB: {
  804. EditorPropertyAABB *editor = memnew(EditorPropertyAABB);
  805. editor->setup(-100000, 100000, default_float_step, true);
  806. prop = editor;
  807. } break;
  808. case Variant::BASIS: {
  809. EditorPropertyBasis *editor = memnew(EditorPropertyBasis);
  810. editor->setup(-100000, 100000, default_float_step, true);
  811. prop = editor;
  812. } break;
  813. case Variant::TRANSFORM3D: {
  814. EditorPropertyTransform3D *editor = memnew(EditorPropertyTransform3D);
  815. editor->setup(-100000, 100000, default_float_step, true);
  816. prop = editor;
  817. } break;
  818. case Variant::PROJECTION: {
  819. EditorPropertyProjection *editor = memnew(EditorPropertyProjection);
  820. editor->setup(-100000, 100000, default_float_step, true);
  821. prop = editor;
  822. } break;
  823. // Miscellaneous types.
  824. case Variant::COLOR: {
  825. prop = memnew(EditorPropertyColor);
  826. } break;
  827. case Variant::STRING_NAME: {
  828. EditorPropertyText *ept = memnew(EditorPropertyText);
  829. ept->set_string_name(true);
  830. prop = ept;
  831. } break;
  832. case Variant::NODE_PATH: {
  833. prop = memnew(EditorPropertyNodePath);
  834. } break;
  835. case Variant::RID: {
  836. prop = memnew(EditorPropertyRID);
  837. } break;
  838. case Variant::SIGNAL: {
  839. prop = memnew(EditorPropertySignal);
  840. } break;
  841. case Variant::CALLABLE: {
  842. prop = memnew(EditorPropertyCallable);
  843. } break;
  844. case Variant::OBJECT: {
  845. if (Object::cast_to<EncodedObjectAsID>(value)) {
  846. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  847. editor->setup("Object");
  848. prop = editor;
  849. } else {
  850. EditorPropertyResource *editor = memnew(EditorPropertyResource);
  851. editor->setup(object.ptr(), prop_name, "Resource");
  852. editor->set_use_folding(is_using_folding());
  853. prop = editor;
  854. }
  855. } break;
  856. case Variant::DICTIONARY: {
  857. prop = memnew(EditorPropertyDictionary);
  858. } break;
  859. case Variant::ARRAY: {
  860. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  861. editor->setup(Variant::ARRAY);
  862. prop = editor;
  863. } break;
  864. // Arrays.
  865. case Variant::PACKED_BYTE_ARRAY: {
  866. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  867. editor->setup(Variant::PACKED_BYTE_ARRAY);
  868. prop = editor;
  869. } break;
  870. case Variant::PACKED_INT32_ARRAY: {
  871. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  872. editor->setup(Variant::PACKED_INT32_ARRAY);
  873. prop = editor;
  874. } break;
  875. case Variant::PACKED_FLOAT32_ARRAY: {
  876. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  877. editor->setup(Variant::PACKED_FLOAT32_ARRAY);
  878. prop = editor;
  879. } break;
  880. case Variant::PACKED_INT64_ARRAY: {
  881. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  882. editor->setup(Variant::PACKED_INT64_ARRAY);
  883. prop = editor;
  884. } break;
  885. case Variant::PACKED_FLOAT64_ARRAY: {
  886. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  887. editor->setup(Variant::PACKED_FLOAT64_ARRAY);
  888. prop = editor;
  889. } break;
  890. case Variant::PACKED_STRING_ARRAY: {
  891. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  892. editor->setup(Variant::PACKED_STRING_ARRAY);
  893. prop = editor;
  894. } break;
  895. case Variant::PACKED_VECTOR2_ARRAY: {
  896. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  897. editor->setup(Variant::PACKED_VECTOR2_ARRAY);
  898. prop = editor;
  899. } break;
  900. case Variant::PACKED_VECTOR3_ARRAY: {
  901. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  902. editor->setup(Variant::PACKED_VECTOR3_ARRAY);
  903. prop = editor;
  904. } break;
  905. case Variant::PACKED_COLOR_ARRAY: {
  906. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  907. editor->setup(Variant::PACKED_COLOR_ARRAY);
  908. prop = editor;
  909. } break;
  910. default: {
  911. }
  912. }
  913. ERR_FAIL_COND(!prop);
  914. prop->set_read_only(is_read_only());
  915. if (i == amount) {
  916. PanelContainer *pc = memnew(PanelContainer);
  917. property_vbox->add_child(pc);
  918. pc->add_theme_style_override(SNAME("panel"), get_theme_stylebox(SNAME("DictionaryAddItem"), SNAME("EditorStyles")));
  919. add_vbox = memnew(VBoxContainer);
  920. pc->add_child(add_vbox);
  921. }
  922. prop->set_object_and_property(object.ptr(), prop_name);
  923. int change_index = 0;
  924. if (i < amount) {
  925. String cs = key.get_construct_string();
  926. prop->set_label(key.get_construct_string());
  927. prop->set_tooltip_text(cs);
  928. change_index = i + offset;
  929. } else if (i == amount) {
  930. prop->set_label(TTR("New Key:"));
  931. change_index = -1;
  932. } else if (i == amount + 1) {
  933. prop->set_label(TTR("New Value:"));
  934. change_index = -2;
  935. }
  936. prop->set_selectable(false);
  937. prop->connect("property_changed", callable_mp(this, &EditorPropertyDictionary::_property_changed));
  938. prop->connect("object_id_selected", callable_mp(this, &EditorPropertyDictionary::_object_id_selected));
  939. HBoxContainer *hbox = memnew(HBoxContainer);
  940. if (add_vbox) {
  941. add_vbox->add_child(hbox);
  942. } else {
  943. property_vbox->add_child(hbox);
  944. }
  945. hbox->add_child(prop);
  946. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  947. Button *edit_btn = memnew(Button);
  948. edit_btn->set_icon(get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")));
  949. edit_btn->set_disabled(is_read_only());
  950. hbox->add_child(edit_btn);
  951. edit_btn->connect("pressed", callable_mp(this, &EditorPropertyDictionary::_change_type).bind(edit_btn, change_index));
  952. prop->update_property();
  953. if (i == amount + 1) {
  954. button_add_item = EditorInspector::create_inspector_action_button(TTR("Add Key/Value Pair"));
  955. button_add_item->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  956. button_add_item->set_disabled(is_read_only());
  957. button_add_item->connect("pressed", callable_mp(this, &EditorPropertyDictionary::_add_key_value));
  958. add_vbox->add_child(button_add_item);
  959. }
  960. }
  961. updating = false;
  962. } else {
  963. if (container) {
  964. set_bottom_editor(nullptr);
  965. memdelete(container);
  966. button_add_item = nullptr;
  967. container = nullptr;
  968. }
  969. }
  970. }
  971. void EditorPropertyDictionary::_object_id_selected(const StringName &p_property, ObjectID p_id) {
  972. emit_signal(SNAME("object_id_selected"), p_property, p_id);
  973. }
  974. void EditorPropertyDictionary::_notification(int p_what) {
  975. switch (p_what) {
  976. case NOTIFICATION_THEME_CHANGED:
  977. case NOTIFICATION_ENTER_TREE: {
  978. change_type->clear();
  979. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  980. if (i == Variant::CALLABLE || i == Variant::SIGNAL || i == Variant::RID) {
  981. // These types can't be constructed or serialized properly, so skip them.
  982. continue;
  983. }
  984. String type = Variant::get_type_name(Variant::Type(i));
  985. change_type->add_icon_item(get_theme_icon(type, SNAME("EditorIcons")), type, i);
  986. }
  987. change_type->add_separator();
  988. change_type->add_icon_item(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), TTR("Remove Item"), Variant::VARIANT_MAX);
  989. if (button_add_item) {
  990. button_add_item->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  991. }
  992. } break;
  993. }
  994. }
  995. void EditorPropertyDictionary::_edit_pressed() {
  996. Variant prop_val = get_edited_property_value();
  997. if (prop_val.get_type() == Variant::NIL && edit->is_pressed()) {
  998. VariantInternal::initialize(&prop_val, Variant::DICTIONARY);
  999. get_edited_object()->set(get_edited_property(), prop_val);
  1000. }
  1001. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  1002. update_property();
  1003. }
  1004. void EditorPropertyDictionary::_page_changed(int p_page) {
  1005. if (updating) {
  1006. return;
  1007. }
  1008. page_index = p_page;
  1009. update_property();
  1010. }
  1011. void EditorPropertyDictionary::_bind_methods() {
  1012. }
  1013. EditorPropertyDictionary::EditorPropertyDictionary() {
  1014. object.instantiate();
  1015. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  1016. edit = memnew(Button);
  1017. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  1018. edit->set_clip_text(true);
  1019. edit->connect("pressed", callable_mp(this, &EditorPropertyDictionary::_edit_pressed));
  1020. edit->set_toggle_mode(true);
  1021. add_child(edit);
  1022. add_focusable(edit);
  1023. container = nullptr;
  1024. button_add_item = nullptr;
  1025. paginator = nullptr;
  1026. change_type = memnew(PopupMenu);
  1027. add_child(change_type);
  1028. change_type->connect("id_pressed", callable_mp(this, &EditorPropertyDictionary::_change_type_menu));
  1029. changing_type_index = -1;
  1030. }
  1031. ///////////////////// LOCALIZABLE STRING ///////////////////////////
  1032. void EditorPropertyLocalizableString::_property_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing) {
  1033. if (p_property.begins_with("indices")) {
  1034. int index = p_property.get_slice("/", 1).to_int();
  1035. Dictionary dict = object->get_dict().duplicate();
  1036. Variant key = dict.get_key_at_index(index);
  1037. dict[key] = p_value;
  1038. object->set_dict(dict);
  1039. emit_changed(get_edited_property(), dict, "", true);
  1040. }
  1041. }
  1042. void EditorPropertyLocalizableString::_add_locale_popup() {
  1043. locale_select->popup_locale_dialog();
  1044. }
  1045. void EditorPropertyLocalizableString::_add_locale(const String &p_locale) {
  1046. Dictionary dict = object->get_dict().duplicate();
  1047. object->set_new_item_key(p_locale);
  1048. object->set_new_item_value(String());
  1049. dict[object->get_new_item_key()] = object->get_new_item_value();
  1050. emit_changed(get_edited_property(), dict, "", false);
  1051. update_property();
  1052. }
  1053. void EditorPropertyLocalizableString::_remove_item(Object *p_button, int p_index) {
  1054. Dictionary dict = object->get_dict().duplicate();
  1055. Variant key = dict.get_key_at_index(p_index);
  1056. dict.erase(key);
  1057. emit_changed(get_edited_property(), dict, "", false);
  1058. update_property();
  1059. }
  1060. void EditorPropertyLocalizableString::update_property() {
  1061. Variant updated_val = get_edited_property_value();
  1062. if (updated_val.get_type() == Variant::NIL) {
  1063. edit->set_text(TTR("Localizable String (Nil)")); // This provides symmetry with the array property.
  1064. edit->set_pressed(false);
  1065. if (container) {
  1066. set_bottom_editor(nullptr);
  1067. memdelete(container);
  1068. button_add_item = nullptr;
  1069. container = nullptr;
  1070. }
  1071. return;
  1072. }
  1073. Dictionary dict = updated_val;
  1074. object->set_dict(dict);
  1075. edit->set_text(vformat(TTR("Localizable String (size %d)"), dict.size()));
  1076. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  1077. if (edit->is_pressed() != unfolded) {
  1078. edit->set_pressed(unfolded);
  1079. }
  1080. if (unfolded) {
  1081. updating = true;
  1082. if (!container) {
  1083. container = memnew(MarginContainer);
  1084. container->set_theme_type_variation("MarginContainer4px");
  1085. add_child(container);
  1086. set_bottom_editor(container);
  1087. VBoxContainer *vbox = memnew(VBoxContainer);
  1088. container->add_child(vbox);
  1089. property_vbox = memnew(VBoxContainer);
  1090. property_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  1091. vbox->add_child(property_vbox);
  1092. paginator = memnew(EditorPaginator);
  1093. paginator->connect("page_changed", callable_mp(this, &EditorPropertyLocalizableString::_page_changed));
  1094. vbox->add_child(paginator);
  1095. } else {
  1096. // Queue children for deletion, deleting immediately might cause errors.
  1097. for (int i = property_vbox->get_child_count() - 1; i >= 0; i--) {
  1098. property_vbox->get_child(i)->queue_free();
  1099. }
  1100. }
  1101. int size = dict.size();
  1102. int max_page = MAX(0, size - 1) / page_length;
  1103. page_index = MIN(page_index, max_page);
  1104. paginator->update(page_index, max_page);
  1105. paginator->set_visible(max_page > 0);
  1106. int offset = page_index * page_length;
  1107. int amount = MIN(size - offset, page_length);
  1108. for (int i = 0; i < amount; i++) {
  1109. String prop_name;
  1110. Variant key;
  1111. Variant value;
  1112. prop_name = "indices/" + itos(i + offset);
  1113. key = dict.get_key_at_index(i + offset);
  1114. value = dict.get_value_at_index(i + offset);
  1115. EditorProperty *prop = memnew(EditorPropertyText);
  1116. prop->set_object_and_property(object.ptr(), prop_name);
  1117. int remove_index = 0;
  1118. String cs = key.get_construct_string();
  1119. prop->set_label(cs);
  1120. prop->set_tooltip_text(cs);
  1121. remove_index = i + offset;
  1122. prop->set_selectable(false);
  1123. prop->connect("property_changed", callable_mp(this, &EditorPropertyLocalizableString::_property_changed));
  1124. prop->connect("object_id_selected", callable_mp(this, &EditorPropertyLocalizableString::_object_id_selected));
  1125. HBoxContainer *hbox = memnew(HBoxContainer);
  1126. property_vbox->add_child(hbox);
  1127. hbox->add_child(prop);
  1128. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  1129. Button *edit_btn = memnew(Button);
  1130. edit_btn->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  1131. hbox->add_child(edit_btn);
  1132. edit_btn->connect("pressed", callable_mp(this, &EditorPropertyLocalizableString::_remove_item).bind(edit_btn, remove_index));
  1133. prop->update_property();
  1134. }
  1135. if (page_index == max_page) {
  1136. button_add_item = EditorInspector::create_inspector_action_button(TTR("Add Translation"));
  1137. button_add_item->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  1138. button_add_item->connect("pressed", callable_mp(this, &EditorPropertyLocalizableString::_add_locale_popup));
  1139. property_vbox->add_child(button_add_item);
  1140. }
  1141. updating = false;
  1142. } else {
  1143. if (container) {
  1144. set_bottom_editor(nullptr);
  1145. memdelete(container);
  1146. button_add_item = nullptr;
  1147. container = nullptr;
  1148. }
  1149. }
  1150. }
  1151. void EditorPropertyLocalizableString::_object_id_selected(const StringName &p_property, ObjectID p_id) {
  1152. emit_signal(SNAME("object_id_selected"), p_property, p_id);
  1153. }
  1154. void EditorPropertyLocalizableString::_notification(int p_what) {
  1155. switch (p_what) {
  1156. case NOTIFICATION_THEME_CHANGED:
  1157. case NOTIFICATION_ENTER_TREE: {
  1158. if (button_add_item) {
  1159. button_add_item->set_icon(get_theme_icon(SNAME("Add"), SNAME("EditorIcons")));
  1160. }
  1161. } break;
  1162. }
  1163. }
  1164. void EditorPropertyLocalizableString::_edit_pressed() {
  1165. Variant prop_val = get_edited_property_value();
  1166. if (prop_val.get_type() == Variant::NIL && edit->is_pressed()) {
  1167. VariantInternal::initialize(&prop_val, Variant::DICTIONARY);
  1168. get_edited_object()->set(get_edited_property(), prop_val);
  1169. }
  1170. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  1171. update_property();
  1172. }
  1173. void EditorPropertyLocalizableString::_page_changed(int p_page) {
  1174. if (updating) {
  1175. return;
  1176. }
  1177. page_index = p_page;
  1178. update_property();
  1179. }
  1180. void EditorPropertyLocalizableString::_bind_methods() {
  1181. }
  1182. EditorPropertyLocalizableString::EditorPropertyLocalizableString() {
  1183. object.instantiate();
  1184. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  1185. edit = memnew(Button);
  1186. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  1187. edit->set_clip_text(true);
  1188. edit->connect("pressed", callable_mp(this, &EditorPropertyLocalizableString::_edit_pressed));
  1189. edit->set_toggle_mode(true);
  1190. add_child(edit);
  1191. add_focusable(edit);
  1192. container = nullptr;
  1193. button_add_item = nullptr;
  1194. paginator = nullptr;
  1195. updating = false;
  1196. locale_select = memnew(EditorLocaleDialog);
  1197. locale_select->connect("locale_selected", callable_mp(this, &EditorPropertyLocalizableString::_add_locale));
  1198. add_child(locale_select);
  1199. }