editor_properties_array_dict.cpp 44 KB

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