editor_properties_array_dict.cpp 46 KB

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