editor_properties_array_dict.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  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_button_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_button_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_button_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_button_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. change_type->add_icon_item(get_editor_theme_icon(SNAME("Remove")), TTR("Remove Item"), Variant::VARIANT_MAX);
  533. change_type->add_separator();
  534. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  535. if (i == Variant::CALLABLE || i == Variant::SIGNAL || i == Variant::RID) {
  536. // These types can't be constructed or serialized properly, so skip them.
  537. continue;
  538. }
  539. String type = Variant::get_type_name(Variant::Type(i));
  540. change_type->add_icon_item(get_editor_theme_icon(type), type, i);
  541. }
  542. if (button_add_item) {
  543. button_add_item->set_button_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_char(':');
  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_char('/');
  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. EditorPropertyArray::EditorPropertyArray() {
  693. object.instantiate();
  694. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  695. edit = memnew(Button);
  696. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  697. edit->set_clip_text(true);
  698. edit->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyArray::_edit_pressed));
  699. edit->set_toggle_mode(true);
  700. SET_DRAG_FORWARDING_CD(edit, EditorPropertyArray);
  701. edit->connect(SceneStringName(draw), callable_mp(this, &EditorPropertyArray::_button_draw));
  702. add_child(edit);
  703. add_focusable(edit);
  704. change_type = memnew(PopupMenu);
  705. add_child(change_type);
  706. change_type->connect(SceneStringName(id_pressed), callable_mp(this, &EditorPropertyArray::_change_type_menu));
  707. changing_type_index = -1;
  708. subtype = Variant::NIL;
  709. subtype_hint = PROPERTY_HINT_NONE;
  710. subtype_hint_string = "";
  711. has_borders = true;
  712. }
  713. ///////////////////// DICTIONARY ///////////////////////////
  714. void EditorPropertyDictionary::initialize_dictionary(Variant &p_dictionary) {
  715. if (key_subtype != Variant::NIL || value_subtype != Variant::NIL) {
  716. Dictionary dict;
  717. StringName key_subtype_class;
  718. Ref<Script> key_subtype_script;
  719. if (key_subtype == Variant::OBJECT && !key_subtype_hint_string.is_empty() && ClassDB::class_exists(key_subtype_hint_string)) {
  720. key_subtype_class = key_subtype_hint_string;
  721. }
  722. StringName value_subtype_class;
  723. Ref<Script> value_subtype_script;
  724. if (value_subtype == Variant::OBJECT && !value_subtype_hint_string.is_empty() && ClassDB::class_exists(value_subtype_hint_string)) {
  725. value_subtype_class = value_subtype_hint_string;
  726. }
  727. dict.set_typed(key_subtype, key_subtype_class, key_subtype_script, value_subtype, value_subtype_class, value_subtype_script);
  728. p_dictionary = dict;
  729. } else {
  730. VariantInternal::initialize(&p_dictionary, Variant::DICTIONARY);
  731. }
  732. }
  733. void EditorPropertyDictionary::_property_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing) {
  734. if (p_value.get_type() == Variant::OBJECT && p_value.is_null()) {
  735. p_value = Variant(); // `EditorResourcePicker` resets to `Ref<Resource>()`. See GH-82716.
  736. }
  737. object->set(p_property, p_value);
  738. emit_changed(get_edited_property(), object->get_dict(), p_name, p_changing);
  739. }
  740. void EditorPropertyDictionary::_change_type(Object *p_button, int p_slot_index) {
  741. Button *button = Object::cast_to<Button>(p_button);
  742. int index = slots[p_slot_index].index;
  743. Rect2 rect = button->get_screen_rect();
  744. change_type->set_item_disabled(change_type->get_item_index(Variant::VARIANT_MAX), index < 0);
  745. change_type->reset_size();
  746. change_type->set_position(rect.get_end() - Vector2(change_type->get_contents_minimum_size().x, 0));
  747. change_type->popup();
  748. changing_type_index = index;
  749. }
  750. void EditorPropertyDictionary::_add_key_value() {
  751. // Do not allow nil as valid key. I experienced errors with this
  752. if (object->get_new_item_key().get_type() == Variant::NIL) {
  753. return;
  754. }
  755. Dictionary dict = object->get_dict().duplicate();
  756. Variant new_key = object->get_new_item_key();
  757. Variant new_value = object->get_new_item_value();
  758. dict[new_key] = new_value;
  759. Variant::Type type = new_key.get_type();
  760. new_key.zero();
  761. VariantInternal::initialize(&new_key, type);
  762. object->set_new_item_key(new_key);
  763. type = new_value.get_type();
  764. new_value.zero();
  765. VariantInternal::initialize(&new_value, type);
  766. object->set_new_item_value(new_value);
  767. object->set_dict(dict);
  768. slots[(dict.size() - 1) % page_length].update_prop_or_index();
  769. emit_changed(get_edited_property(), dict);
  770. }
  771. void EditorPropertyDictionary::_create_new_property_slot(int p_idx) {
  772. HBoxContainer *hbox = memnew(HBoxContainer);
  773. EditorProperty *prop = memnew(EditorPropertyNil);
  774. hbox->add_child(prop);
  775. bool use_key = p_idx == EditorPropertyDictionaryObject::NEW_KEY_INDEX;
  776. bool is_untyped_dict = (use_key ? key_subtype : value_subtype) == Variant::NIL;
  777. if (is_untyped_dict) {
  778. Button *edit_btn = memnew(Button);
  779. edit_btn->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
  780. edit_btn->set_disabled(is_read_only());
  781. edit_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyDictionary::_change_type).bind(edit_btn, slots.size()));
  782. hbox->add_child(edit_btn);
  783. } else if (p_idx >= 0) {
  784. Button *remove_btn = memnew(Button);
  785. remove_btn->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  786. remove_btn->set_disabled(is_read_only());
  787. remove_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyDictionary::_remove_pressed).bind(slots.size()));
  788. hbox->add_child(remove_btn);
  789. }
  790. if (add_panel) {
  791. add_panel->get_child(0)->add_child(hbox);
  792. } else {
  793. property_vbox->add_child(hbox);
  794. }
  795. Slot slot;
  796. slot.prop = prop;
  797. slot.object = object;
  798. slot.container = hbox;
  799. int index = p_idx + (p_idx >= 0 ? page_index * page_length : 0);
  800. slot.set_index(index);
  801. slots.push_back(slot);
  802. }
  803. void EditorPropertyDictionary::_change_type_menu(int p_index) {
  804. ERR_FAIL_COND_MSG(
  805. changing_type_index == EditorPropertyDictionaryObject::NOT_CHANGING_TYPE,
  806. "Tried to change the type of a dict key or value, but nothing was selected.");
  807. Variant value;
  808. switch (changing_type_index) {
  809. case EditorPropertyDictionaryObject::NEW_KEY_INDEX:
  810. case EditorPropertyDictionaryObject::NEW_VALUE_INDEX:
  811. VariantInternal::initialize(&value, Variant::Type(p_index));
  812. if (changing_type_index == EditorPropertyDictionaryObject::NEW_KEY_INDEX) {
  813. object->set_new_item_key(value);
  814. } else {
  815. object->set_new_item_value(value);
  816. }
  817. update_property();
  818. break;
  819. default:
  820. Dictionary dict = object->get_dict().duplicate();
  821. Variant key = dict.get_key_at_index(changing_type_index);
  822. if (p_index < Variant::VARIANT_MAX) {
  823. VariantInternal::initialize(&value, Variant::Type(p_index));
  824. dict[key] = value;
  825. } else {
  826. dict.erase(key);
  827. object->set_dict(dict);
  828. for (Slot &slot : slots) {
  829. slot.update_prop_or_index();
  830. }
  831. }
  832. emit_changed(get_edited_property(), dict);
  833. }
  834. }
  835. void EditorPropertyDictionary::setup(PropertyHint p_hint, const String &p_hint_string) {
  836. PackedStringArray types = p_hint_string.split(";");
  837. if (types.size() > 0 && !types[0].is_empty()) {
  838. String key = types[0];
  839. int hint_key_subtype_separator = key.find_char(':');
  840. if (hint_key_subtype_separator >= 0) {
  841. String key_subtype_string = key.substr(0, hint_key_subtype_separator);
  842. int slash_pos = key_subtype_string.find_char('/');
  843. if (slash_pos >= 0) {
  844. key_subtype_hint = PropertyHint(key_subtype_string.substr(slash_pos + 1, key_subtype_string.size() - slash_pos - 1).to_int());
  845. key_subtype_string = key_subtype_string.substr(0, slash_pos);
  846. }
  847. key_subtype_hint_string = key.substr(hint_key_subtype_separator + 1, key.size() - hint_key_subtype_separator - 1);
  848. key_subtype = Variant::Type(key_subtype_string.to_int());
  849. Variant new_key = object->get_new_item_key();
  850. VariantInternal::initialize(&new_key, key_subtype);
  851. object->set_new_item_key(new_key);
  852. }
  853. }
  854. if (types.size() > 1 && !types[1].is_empty()) {
  855. String value = types[1];
  856. int hint_value_subtype_separator = value.find_char(':');
  857. if (hint_value_subtype_separator >= 0) {
  858. String value_subtype_string = value.substr(0, hint_value_subtype_separator);
  859. int slash_pos = value_subtype_string.find_char('/');
  860. if (slash_pos >= 0) {
  861. value_subtype_hint = PropertyHint(value_subtype_string.substr(slash_pos + 1, value_subtype_string.size() - slash_pos - 1).to_int());
  862. value_subtype_string = value_subtype_string.substr(0, slash_pos);
  863. }
  864. value_subtype_hint_string = value.substr(hint_value_subtype_separator + 1, value.size() - hint_value_subtype_separator - 1);
  865. value_subtype = Variant::Type(value_subtype_string.to_int());
  866. Variant new_value = object->get_new_item_value();
  867. VariantInternal::initialize(&new_value, value_subtype);
  868. object->set_new_item_value(new_value);
  869. }
  870. }
  871. }
  872. void EditorPropertyDictionary::update_property() {
  873. Variant updated_val = get_edited_property_value();
  874. String dict_type_name = "Dictionary";
  875. if (key_subtype != Variant::NIL || value_subtype != Variant::NIL) {
  876. String key_subtype_name = "Variant";
  877. if (key_subtype == Variant::OBJECT && (key_subtype_hint == PROPERTY_HINT_RESOURCE_TYPE || key_subtype_hint == PROPERTY_HINT_NODE_TYPE)) {
  878. key_subtype_name = key_subtype_hint_string;
  879. } else if (key_subtype != Variant::NIL) {
  880. key_subtype_name = Variant::get_type_name(key_subtype);
  881. }
  882. String value_subtype_name = "Variant";
  883. if (value_subtype == Variant::OBJECT && (value_subtype_hint == PROPERTY_HINT_RESOURCE_TYPE || value_subtype_hint == PROPERTY_HINT_NODE_TYPE)) {
  884. value_subtype_name = value_subtype_hint_string;
  885. } else if (value_subtype != Variant::NIL) {
  886. value_subtype_name = Variant::get_type_name(value_subtype);
  887. }
  888. dict_type_name += vformat("[%s, %s]", key_subtype_name, value_subtype_name);
  889. }
  890. if (updated_val.get_type() != Variant::DICTIONARY) {
  891. edit->set_text(vformat(TTR("(Nil) %s"), dict_type_name)); // This provides symmetry with the array property.
  892. edit->set_pressed(false);
  893. if (container) {
  894. set_bottom_editor(nullptr);
  895. memdelete(container);
  896. button_add_item = nullptr;
  897. container = nullptr;
  898. add_panel = nullptr;
  899. slots.clear();
  900. }
  901. return;
  902. }
  903. Dictionary dict = updated_val;
  904. object->set_dict(updated_val);
  905. edit->set_text(vformat(TTR("%s (size %d)"), dict_type_name, dict.size()));
  906. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  907. if (edit->is_pressed() != unfolded) {
  908. edit->set_pressed(unfolded);
  909. }
  910. if (unfolded) {
  911. updating = true;
  912. if (!container) {
  913. container = memnew(PanelContainer);
  914. container->set_mouse_filter(MOUSE_FILTER_STOP);
  915. add_child(container);
  916. set_bottom_editor(container);
  917. VBoxContainer *vbox = memnew(VBoxContainer);
  918. container->add_child(vbox);
  919. property_vbox = memnew(VBoxContainer);
  920. property_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  921. vbox->add_child(property_vbox);
  922. paginator = memnew(EditorPaginator);
  923. paginator->connect("page_changed", callable_mp(this, &EditorPropertyDictionary::_page_changed));
  924. vbox->add_child(paginator);
  925. for (int i = 0; i < page_length; i++) {
  926. _create_new_property_slot(slots.size());
  927. }
  928. add_panel = memnew(PanelContainer);
  929. property_vbox->add_child(add_panel);
  930. add_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("DictionaryAddItem")));
  931. VBoxContainer *add_vbox = memnew(VBoxContainer);
  932. add_panel->add_child(add_vbox);
  933. _create_new_property_slot(EditorPropertyDictionaryObject::NEW_KEY_INDEX);
  934. _create_new_property_slot(EditorPropertyDictionaryObject::NEW_VALUE_INDEX);
  935. button_add_item = EditorInspector::create_inspector_action_button(TTR("Add Key/Value Pair"));
  936. button_add_item->set_button_icon(get_theme_icon(SNAME("Add"), EditorStringName(EditorIcons)));
  937. button_add_item->set_disabled(is_read_only());
  938. button_add_item->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyDictionary::_add_key_value));
  939. add_vbox->add_child(button_add_item);
  940. }
  941. int size = dict.size();
  942. int max_page = MAX(0, size - 1) / page_length;
  943. if (page_index > max_page) {
  944. _page_changed(max_page);
  945. }
  946. paginator->update(page_index, max_page);
  947. paginator->set_visible(max_page > 0);
  948. add_panel->set_visible(page_index == max_page);
  949. for (Slot &slot : slots) {
  950. bool slot_visible = slot.index < size;
  951. slot.container->set_visible(slot_visible);
  952. // If not visible no need to update it.
  953. if (!slot_visible) {
  954. continue;
  955. }
  956. Variant value;
  957. object->get_by_property_name(slot.prop_name, value);
  958. Variant::Type value_type = value.get_type();
  959. // Check if the editor property needs to be updated.
  960. bool value_as_id = Object::cast_to<EncodedObjectAsID>(value);
  961. if (value_type != slot.type || (value_type == Variant::OBJECT && value_as_id != slot.as_id)) {
  962. slot.as_id = value_as_id;
  963. slot.type = value_type;
  964. EditorProperty *new_prop = nullptr;
  965. if (value_type == Variant::OBJECT && value_as_id) {
  966. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  967. editor->setup("Object");
  968. new_prop = editor;
  969. } else {
  970. bool use_key = slot.index == EditorPropertyDictionaryObject::NEW_KEY_INDEX;
  971. new_prop = EditorInspector::instantiate_property_editor(this, value_type, "", use_key ? key_subtype_hint : value_subtype_hint,
  972. use_key ? key_subtype_hint_string : value_subtype_hint_string, PROPERTY_USAGE_NONE);
  973. }
  974. new_prop->set_selectable(false);
  975. new_prop->set_use_folding(is_using_folding());
  976. new_prop->connect(SNAME("property_changed"), callable_mp(this, &EditorPropertyDictionary::_property_changed));
  977. new_prop->connect(SNAME("object_id_selected"), callable_mp(this, &EditorPropertyDictionary::_object_id_selected));
  978. new_prop->set_h_size_flags(SIZE_EXPAND_FILL);
  979. new_prop->set_read_only(is_read_only());
  980. slot.set_prop(new_prop);
  981. } else if (slot.index != EditorPropertyDictionaryObject::NEW_KEY_INDEX && slot.index != EditorPropertyDictionaryObject::NEW_VALUE_INDEX) {
  982. Variant key = dict.get_key_at_index(slot.index);
  983. String cs = key.get_construct_string();
  984. slot.prop->set_label(cs);
  985. slot.prop->set_tooltip_text(cs);
  986. }
  987. // We need to grab the focus of the property that is being changed, even if the type didn't actually changed.
  988. // Otherwise, focus will stay on the change type button, which is not very user friendly.
  989. if (changing_type_index == slot.index) {
  990. callable_mp(slot.prop, &EditorProperty::grab_focus).call_deferred(0);
  991. changing_type_index = EditorPropertyDictionaryObject::NOT_CHANGING_TYPE; // Reset to avoid grabbing focus again.
  992. }
  993. slot.prop->update_property();
  994. }
  995. updating = false;
  996. } else {
  997. if (container) {
  998. set_bottom_editor(nullptr);
  999. memdelete(container);
  1000. button_add_item = nullptr;
  1001. container = nullptr;
  1002. add_panel = nullptr;
  1003. slots.clear();
  1004. }
  1005. }
  1006. }
  1007. void EditorPropertyDictionary::_remove_pressed(int p_slot_index) {
  1008. Dictionary dict = object->get_dict().duplicate();
  1009. int index = slots[p_slot_index].index;
  1010. dict.erase(dict.get_key_at_index(index));
  1011. emit_changed(get_edited_property(), dict);
  1012. }
  1013. void EditorPropertyDictionary::_object_id_selected(const StringName &p_property, ObjectID p_id) {
  1014. emit_signal(SNAME("object_id_selected"), p_property, p_id);
  1015. }
  1016. void EditorPropertyDictionary::_notification(int p_what) {
  1017. switch (p_what) {
  1018. case NOTIFICATION_THEME_CHANGED:
  1019. case NOTIFICATION_ENTER_TREE: {
  1020. change_type->clear();
  1021. change_type->add_icon_item(get_editor_theme_icon(SNAME("Remove")), TTR("Remove Item"), Variant::VARIANT_MAX);
  1022. change_type->add_separator();
  1023. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  1024. if (i == Variant::CALLABLE || i == Variant::SIGNAL || i == Variant::RID) {
  1025. // These types can't be constructed or serialized properly, so skip them.
  1026. continue;
  1027. }
  1028. String type = Variant::get_type_name(Variant::Type(i));
  1029. change_type->add_icon_item(get_editor_theme_icon(type), type, i);
  1030. }
  1031. if (button_add_item) {
  1032. button_add_item->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  1033. add_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("DictionaryAddItem")));
  1034. }
  1035. } break;
  1036. }
  1037. }
  1038. void EditorPropertyDictionary::_edit_pressed() {
  1039. Variant prop_val = get_edited_property_value();
  1040. if (prop_val.get_type() == Variant::NIL && edit->is_pressed()) {
  1041. initialize_dictionary(prop_val);
  1042. emit_changed(get_edited_property(), prop_val);
  1043. }
  1044. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  1045. update_property();
  1046. }
  1047. void EditorPropertyDictionary::_page_changed(int p_page) {
  1048. page_index = p_page;
  1049. int i = p_page * page_length;
  1050. for (Slot &slot : slots) {
  1051. if (slot.index > -1) {
  1052. slot.set_index(i);
  1053. i++;
  1054. }
  1055. }
  1056. if (updating) {
  1057. return;
  1058. }
  1059. update_property();
  1060. }
  1061. bool EditorPropertyDictionary::is_colored(ColorationMode p_mode) {
  1062. return p_mode == COLORATION_CONTAINER_RESOURCE;
  1063. }
  1064. EditorPropertyDictionary::EditorPropertyDictionary() {
  1065. object.instantiate();
  1066. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  1067. edit = memnew(Button);
  1068. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  1069. edit->set_clip_text(true);
  1070. edit->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyDictionary::_edit_pressed));
  1071. edit->set_toggle_mode(true);
  1072. add_child(edit);
  1073. add_focusable(edit);
  1074. container = nullptr;
  1075. button_add_item = nullptr;
  1076. paginator = nullptr;
  1077. change_type = memnew(PopupMenu);
  1078. add_child(change_type);
  1079. change_type->connect(SceneStringName(id_pressed), callable_mp(this, &EditorPropertyDictionary::_change_type_menu));
  1080. changing_type_index = -1;
  1081. has_borders = true;
  1082. key_subtype = Variant::NIL;
  1083. key_subtype_hint = PROPERTY_HINT_NONE;
  1084. key_subtype_hint_string = "";
  1085. value_subtype = Variant::NIL;
  1086. value_subtype_hint = PROPERTY_HINT_NONE;
  1087. value_subtype_hint_string = "";
  1088. }
  1089. ///////////////////// LOCALIZABLE STRING ///////////////////////////
  1090. void EditorPropertyLocalizableString::_property_changed(const String &p_property, const Variant &p_value, const String &p_name, bool p_changing) {
  1091. if (p_property.begins_with("indices")) {
  1092. int index = p_property.get_slice("/", 1).to_int();
  1093. Dictionary dict = object->get_dict().duplicate();
  1094. Variant key = dict.get_key_at_index(index);
  1095. dict[key] = p_value;
  1096. object->set_dict(dict);
  1097. emit_changed(get_edited_property(), dict, "", true);
  1098. }
  1099. }
  1100. void EditorPropertyLocalizableString::_add_locale_popup() {
  1101. locale_select->popup_locale_dialog();
  1102. }
  1103. void EditorPropertyLocalizableString::_add_locale(const String &p_locale) {
  1104. Dictionary dict = object->get_dict().duplicate();
  1105. object->set_new_item_key(p_locale);
  1106. object->set_new_item_value(String());
  1107. dict[object->get_new_item_key()] = object->get_new_item_value();
  1108. emit_changed(get_edited_property(), dict, "", false);
  1109. update_property();
  1110. }
  1111. void EditorPropertyLocalizableString::_remove_item(Object *p_button, int p_index) {
  1112. Dictionary dict = object->get_dict().duplicate();
  1113. Variant key = dict.get_key_at_index(p_index);
  1114. dict.erase(key);
  1115. emit_changed(get_edited_property(), dict, "", false);
  1116. update_property();
  1117. }
  1118. void EditorPropertyLocalizableString::update_property() {
  1119. Variant updated_val = get_edited_property_value();
  1120. if (updated_val.get_type() == Variant::NIL) {
  1121. edit->set_text(TTR("Localizable String (Nil)")); // This provides symmetry with the array property.
  1122. edit->set_pressed(false);
  1123. if (container) {
  1124. set_bottom_editor(nullptr);
  1125. memdelete(container);
  1126. button_add_item = nullptr;
  1127. container = nullptr;
  1128. }
  1129. return;
  1130. }
  1131. Dictionary dict = updated_val;
  1132. object->set_dict(dict);
  1133. edit->set_text(vformat(TTR("Localizable String (size %d)"), dict.size()));
  1134. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  1135. if (edit->is_pressed() != unfolded) {
  1136. edit->set_pressed(unfolded);
  1137. }
  1138. if (unfolded) {
  1139. updating = true;
  1140. if (!container) {
  1141. container = memnew(MarginContainer);
  1142. container->set_theme_type_variation("MarginContainer4px");
  1143. add_child(container);
  1144. set_bottom_editor(container);
  1145. VBoxContainer *vbox = memnew(VBoxContainer);
  1146. container->add_child(vbox);
  1147. property_vbox = memnew(VBoxContainer);
  1148. property_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  1149. vbox->add_child(property_vbox);
  1150. paginator = memnew(EditorPaginator);
  1151. paginator->connect("page_changed", callable_mp(this, &EditorPropertyLocalizableString::_page_changed));
  1152. vbox->add_child(paginator);
  1153. } else {
  1154. // Queue children for deletion, deleting immediately might cause errors.
  1155. for (int i = property_vbox->get_child_count() - 1; i >= 0; i--) {
  1156. property_vbox->get_child(i)->queue_free();
  1157. }
  1158. }
  1159. int size = dict.size();
  1160. int max_page = MAX(0, size - 1) / page_length;
  1161. page_index = MIN(page_index, max_page);
  1162. paginator->update(page_index, max_page);
  1163. paginator->set_visible(max_page > 0);
  1164. int offset = page_index * page_length;
  1165. int amount = MIN(size - offset, page_length);
  1166. for (int i = 0; i < amount; i++) {
  1167. String prop_name;
  1168. Variant key;
  1169. Variant value;
  1170. prop_name = "indices/" + itos(i + offset);
  1171. key = dict.get_key_at_index(i + offset);
  1172. value = dict.get_value_at_index(i + offset);
  1173. EditorProperty *prop = memnew(EditorPropertyText);
  1174. prop->set_object_and_property(object.ptr(), prop_name);
  1175. int remove_index = 0;
  1176. String cs = key.get_construct_string();
  1177. prop->set_label(cs);
  1178. prop->set_tooltip_text(cs);
  1179. remove_index = i + offset;
  1180. prop->set_selectable(false);
  1181. prop->connect("property_changed", callable_mp(this, &EditorPropertyLocalizableString::_property_changed));
  1182. prop->connect("object_id_selected", callable_mp(this, &EditorPropertyLocalizableString::_object_id_selected));
  1183. HBoxContainer *hbox = memnew(HBoxContainer);
  1184. property_vbox->add_child(hbox);
  1185. hbox->add_child(prop);
  1186. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  1187. Button *edit_btn = memnew(Button);
  1188. edit_btn->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  1189. hbox->add_child(edit_btn);
  1190. edit_btn->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyLocalizableString::_remove_item).bind(edit_btn, remove_index));
  1191. prop->update_property();
  1192. }
  1193. if (page_index == max_page) {
  1194. button_add_item = EditorInspector::create_inspector_action_button(TTR("Add Translation"));
  1195. button_add_item->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  1196. button_add_item->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyLocalizableString::_add_locale_popup));
  1197. property_vbox->add_child(button_add_item);
  1198. }
  1199. updating = false;
  1200. } else {
  1201. if (container) {
  1202. set_bottom_editor(nullptr);
  1203. memdelete(container);
  1204. button_add_item = nullptr;
  1205. container = nullptr;
  1206. }
  1207. }
  1208. }
  1209. void EditorPropertyLocalizableString::_object_id_selected(const StringName &p_property, ObjectID p_id) {
  1210. emit_signal(SNAME("object_id_selected"), p_property, p_id);
  1211. }
  1212. void EditorPropertyLocalizableString::_notification(int p_what) {
  1213. switch (p_what) {
  1214. case NOTIFICATION_THEME_CHANGED:
  1215. case NOTIFICATION_ENTER_TREE: {
  1216. if (button_add_item) {
  1217. button_add_item->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  1218. }
  1219. } break;
  1220. }
  1221. }
  1222. void EditorPropertyLocalizableString::_edit_pressed() {
  1223. Variant prop_val = get_edited_property_value();
  1224. if (prop_val.get_type() == Variant::NIL && edit->is_pressed()) {
  1225. VariantInternal::initialize(&prop_val, Variant::DICTIONARY);
  1226. get_edited_object()->set(get_edited_property(), prop_val);
  1227. }
  1228. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  1229. update_property();
  1230. }
  1231. void EditorPropertyLocalizableString::_page_changed(int p_page) {
  1232. if (updating) {
  1233. return;
  1234. }
  1235. page_index = p_page;
  1236. update_property();
  1237. }
  1238. EditorPropertyLocalizableString::EditorPropertyLocalizableString() {
  1239. object.instantiate();
  1240. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  1241. edit = memnew(Button);
  1242. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  1243. edit->set_clip_text(true);
  1244. edit->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyLocalizableString::_edit_pressed));
  1245. edit->set_toggle_mode(true);
  1246. add_child(edit);
  1247. add_focusable(edit);
  1248. container = nullptr;
  1249. button_add_item = nullptr;
  1250. paginator = nullptr;
  1251. updating = false;
  1252. locale_select = memnew(EditorLocaleDialog);
  1253. locale_select->connect("locale_selected", callable_mp(this, &EditorPropertyLocalizableString::_add_locale));
  1254. add_child(locale_select);
  1255. }