2
0

editor_properties_array_dict.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  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) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_node.h"
  34. #include "editor/editor_scale.h"
  35. #include "editor_properties.h"
  36. bool EditorPropertyArrayObject::_set(const StringName &p_name, const Variant &p_value) {
  37. String name = p_name;
  38. if (name.begins_with("indices")) {
  39. int index = name.get_slicec('/', 1).to_int();
  40. array.set(index, p_value);
  41. return true;
  42. }
  43. return false;
  44. }
  45. bool EditorPropertyArrayObject::_get(const StringName &p_name, Variant &r_ret) const {
  46. String name = p_name;
  47. if (name.begins_with("indices")) {
  48. int index = name.get_slicec('/', 1).to_int();
  49. bool valid;
  50. r_ret = array.get(index, &valid);
  51. if (r_ret.get_type() == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(r_ret)) {
  52. r_ret = Object::cast_to<EncodedObjectAsID>(r_ret)->get_object_id();
  53. }
  54. return valid;
  55. }
  56. return false;
  57. }
  58. void EditorPropertyArrayObject::set_array(const Variant &p_array) {
  59. array = p_array;
  60. }
  61. Variant EditorPropertyArrayObject::get_array() {
  62. return array;
  63. }
  64. EditorPropertyArrayObject::EditorPropertyArrayObject() {
  65. }
  66. ///////////////////
  67. bool EditorPropertyDictionaryObject::_set(const StringName &p_name, const Variant &p_value) {
  68. String name = p_name;
  69. if (name == "new_item_key") {
  70. new_item_key = p_value;
  71. return true;
  72. }
  73. if (name == "new_item_value") {
  74. new_item_value = p_value;
  75. return true;
  76. }
  77. if (name.begins_with("indices")) {
  78. int index = name.get_slicec('/', 1).to_int();
  79. Variant key = dict.get_key_at_index(index);
  80. dict[key] = p_value;
  81. return true;
  82. }
  83. return false;
  84. }
  85. bool EditorPropertyDictionaryObject::_get(const StringName &p_name, Variant &r_ret) const {
  86. String name = p_name;
  87. if (name == "new_item_key") {
  88. r_ret = new_item_key;
  89. return true;
  90. }
  91. if (name == "new_item_value") {
  92. r_ret = new_item_value;
  93. return true;
  94. }
  95. if (name.begins_with("indices")) {
  96. int index = name.get_slicec('/', 1).to_int();
  97. Variant key = dict.get_key_at_index(index);
  98. r_ret = dict[key];
  99. if (r_ret.get_type() == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(r_ret)) {
  100. r_ret = Object::cast_to<EncodedObjectAsID>(r_ret)->get_object_id();
  101. }
  102. return true;
  103. }
  104. return false;
  105. }
  106. void EditorPropertyDictionaryObject::set_dict(const Dictionary &p_dict) {
  107. dict = p_dict;
  108. }
  109. Dictionary EditorPropertyDictionaryObject::get_dict() {
  110. return dict;
  111. }
  112. void EditorPropertyDictionaryObject::set_new_item_key(const Variant &p_new_item) {
  113. new_item_key = p_new_item;
  114. }
  115. Variant EditorPropertyDictionaryObject::get_new_item_key() {
  116. return new_item_key;
  117. }
  118. void EditorPropertyDictionaryObject::set_new_item_value(const Variant &p_new_item) {
  119. new_item_value = p_new_item;
  120. }
  121. Variant EditorPropertyDictionaryObject::get_new_item_value() {
  122. return new_item_value;
  123. }
  124. EditorPropertyDictionaryObject::EditorPropertyDictionaryObject() {
  125. }
  126. ///////////////////// ARRAY ///////////////////////////
  127. void EditorPropertyArray::_property_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing) {
  128. if (p_property.begins_with("indices")) {
  129. int index = p_property.get_slice("/", 1).to_int();
  130. Variant array = object->get_array();
  131. array.set(index, p_value);
  132. emit_changed(get_edited_property(), array, "", true);
  133. if (array.get_type() == Variant::ARRAY) {
  134. array = array.call("duplicate"); // Duplicate, so undo/redo works better.
  135. }
  136. object->set_array(array);
  137. }
  138. }
  139. void EditorPropertyArray::_change_type(Object *p_button, int p_index) {
  140. Button *button = Object::cast_to<Button>(p_button);
  141. changing_type_index = p_index;
  142. Rect2 rect = button->get_screen_rect();
  143. change_type->set_as_minsize();
  144. change_type->set_position(rect.get_end() - Vector2(change_type->get_contents_minimum_size().x, 0));
  145. change_type->popup();
  146. }
  147. void EditorPropertyArray::_change_type_menu(int p_index) {
  148. if (p_index == Variant::VARIANT_MAX) {
  149. _remove_pressed(changing_type_index);
  150. return;
  151. }
  152. Variant value;
  153. Callable::CallError ce;
  154. Variant::construct(Variant::Type(p_index), value, nullptr, 0, ce);
  155. Variant array = object->get_array();
  156. array.set(changing_type_index, value);
  157. emit_changed(get_edited_property(), array, "", true);
  158. if (array.get_type() == Variant::ARRAY) {
  159. array = array.call("duplicate"); // Duplicate, so undo/redo works better.
  160. }
  161. object->set_array(array);
  162. update_property();
  163. }
  164. void EditorPropertyArray::_object_id_selected(const StringName &p_property, ObjectID p_id) {
  165. emit_signal(SNAME("object_id_selected"), p_property, p_id);
  166. }
  167. void EditorPropertyArray::update_property() {
  168. Variant array = get_edited_object()->get(get_edited_property());
  169. String arrtype = "";
  170. switch (array_type) {
  171. case Variant::ARRAY: {
  172. arrtype = "Array";
  173. } break;
  174. // Arrays.
  175. case Variant::PACKED_BYTE_ARRAY: {
  176. arrtype = "PackedByteArray";
  177. } break;
  178. case Variant::PACKED_INT32_ARRAY: {
  179. arrtype = "PackedInt32Array";
  180. } break;
  181. case Variant::PACKED_FLOAT32_ARRAY: {
  182. arrtype = "PackedFloat32Array";
  183. } break;
  184. case Variant::PACKED_INT64_ARRAY: {
  185. arrtype = "PackedInt64Array";
  186. } break;
  187. case Variant::PACKED_FLOAT64_ARRAY: {
  188. arrtype = "PackedFloat64Array";
  189. } break;
  190. case Variant::PACKED_STRING_ARRAY: {
  191. arrtype = "PackedStringArray";
  192. } break;
  193. case Variant::PACKED_VECTOR2_ARRAY: {
  194. arrtype = "PackedVector2Array";
  195. } break;
  196. case Variant::PACKED_VECTOR3_ARRAY: {
  197. arrtype = "PackedVector3Array";
  198. } break;
  199. case Variant::PACKED_COLOR_ARRAY: {
  200. arrtype = "PackedColorArray";
  201. } break;
  202. default: {
  203. }
  204. }
  205. if (array.get_type() == Variant::NIL) {
  206. edit->set_text(String("(Nil) ") + arrtype);
  207. edit->set_pressed(false);
  208. if (vbox) {
  209. set_bottom_editor(nullptr);
  210. memdelete(vbox);
  211. vbox = nullptr;
  212. }
  213. return;
  214. }
  215. int size = array.call("size");
  216. int pages = MAX(0, size - 1) / page_length + 1;
  217. page_index = MIN(page_index, pages - 1);
  218. int offset = page_index * page_length;
  219. edit->set_text(arrtype + " (size " + itos(size) + ")");
  220. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  221. if (edit->is_pressed() != unfolded) {
  222. edit->set_pressed(unfolded);
  223. }
  224. if (unfolded) {
  225. updating = true;
  226. if (!vbox) {
  227. vbox = memnew(VBoxContainer);
  228. add_child(vbox);
  229. set_bottom_editor(vbox);
  230. HBoxContainer *hbox = memnew(HBoxContainer);
  231. vbox->add_child(hbox);
  232. Label *label = memnew(Label(TTR("Size: ")));
  233. label->set_h_size_flags(SIZE_EXPAND_FILL);
  234. hbox->add_child(label);
  235. size_slider = memnew(EditorSpinSlider);
  236. size_slider->set_step(1);
  237. size_slider->set_max(1000000);
  238. size_slider->set_h_size_flags(SIZE_EXPAND_FILL);
  239. size_slider->connect("value_changed", callable_mp(this, &EditorPropertyArray::_length_changed));
  240. hbox->add_child(size_slider);
  241. page_hbox = memnew(HBoxContainer);
  242. vbox->add_child(page_hbox);
  243. label = memnew(Label(TTR("Page: ")));
  244. label->set_h_size_flags(SIZE_EXPAND_FILL);
  245. page_hbox->add_child(label);
  246. page_slider = memnew(EditorSpinSlider);
  247. page_slider->set_step(1);
  248. page_slider->set_h_size_flags(SIZE_EXPAND_FILL);
  249. page_slider->connect("value_changed", callable_mp(this, &EditorPropertyArray::_page_changed));
  250. page_hbox->add_child(page_slider);
  251. } else {
  252. // Bye bye children of the box.
  253. for (int i = vbox->get_child_count() - 1; i >= 2; i--) {
  254. Node *child = vbox->get_child(i);
  255. if (child == reorder_selected_element_hbox) {
  256. continue; // Don't remove the property that the user is moving.
  257. }
  258. child->queue_delete(); // Button still needed after pressed is called.
  259. vbox->remove_child(child);
  260. }
  261. }
  262. size_slider->set_value(size);
  263. page_slider->set_max(pages);
  264. page_slider->set_value(page_index);
  265. page_hbox->set_visible(pages > 1);
  266. if (array.get_type() == Variant::ARRAY) {
  267. array = array.call("duplicate");
  268. }
  269. object->set_array(array);
  270. int amount = MIN(size - offset, page_length);
  271. for (int i = 0; i < amount; i++) {
  272. bool reorder_is_from_current_page = reorder_from_index / page_length == page_index;
  273. if (reorder_is_from_current_page && i == reorder_from_index % page_length) {
  274. // Don't duplicate the property that the user is moving.
  275. continue;
  276. }
  277. if (!reorder_is_from_current_page && i == reorder_to_index % page_length) {
  278. // Don't create the property the moving property will take the place of,
  279. // e.g. (if page_length == 20) don't create element 20 if dragging an item from
  280. // the first page to the second page because element 20 would become element 19.
  281. continue;
  282. }
  283. HBoxContainer *hbox = memnew(HBoxContainer);
  284. vbox->add_child(hbox);
  285. Button *reorder_button = memnew(Button);
  286. reorder_button->set_icon(get_theme_icon(SNAME("TripleBar"), SNAME("EditorIcons")));
  287. reorder_button->set_default_cursor_shape(Control::CURSOR_MOVE);
  288. reorder_button->connect("gui_input", callable_mp(this, &EditorPropertyArray::_reorder_button_gui_input));
  289. reorder_button->connect("button_down", callable_mp(this, &EditorPropertyArray::_reorder_button_down), varray(i + offset));
  290. reorder_button->connect("button_up", callable_mp(this, &EditorPropertyArray::_reorder_button_up));
  291. hbox->add_child(reorder_button);
  292. String prop_name = "indices/" + itos(i + offset);
  293. EditorProperty *prop = nullptr;
  294. Variant value = array.get(i + offset);
  295. Variant::Type value_type = value.get_type();
  296. if (value_type == Variant::NIL && subtype != Variant::NIL) {
  297. value_type = subtype;
  298. }
  299. if (value_type == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(value)) {
  300. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  301. editor->setup("Object");
  302. prop = editor;
  303. } else {
  304. prop = EditorInspector::instantiate_property_editor(nullptr, value_type, "", subtype_hint, subtype_hint_string, PROPERTY_USAGE_NONE);
  305. }
  306. prop->set_object_and_property(object.ptr(), prop_name);
  307. prop->set_label(itos(i + offset));
  308. prop->set_selectable(false);
  309. prop->connect("property_changed", callable_mp(this, &EditorPropertyArray::_property_changed));
  310. prop->connect("object_id_selected", callable_mp(this, &EditorPropertyArray::_object_id_selected));
  311. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  312. hbox->add_child(prop);
  313. bool is_untyped_array = array.get_type() == Variant::ARRAY && subtype == Variant::NIL;
  314. if (is_untyped_array) {
  315. Button *edit = memnew(Button);
  316. edit->set_icon(get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")));
  317. hbox->add_child(edit);
  318. edit->connect("pressed", callable_mp(this, &EditorPropertyArray::_change_type), varray(edit, i + offset));
  319. } else {
  320. Button *remove = memnew(Button);
  321. remove->set_icon(get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")));
  322. remove->connect("pressed", callable_mp(this, &EditorPropertyArray::_remove_pressed), varray(i + offset));
  323. hbox->add_child(remove);
  324. }
  325. prop->update_property();
  326. }
  327. if (reorder_to_index % page_length > 0) {
  328. vbox->move_child(vbox->get_child(2), reorder_to_index % page_length + 2);
  329. }
  330. updating = false;
  331. } else {
  332. if (vbox) {
  333. set_bottom_editor(nullptr);
  334. memdelete(vbox);
  335. vbox = nullptr;
  336. }
  337. }
  338. }
  339. void EditorPropertyArray::_remove_pressed(int p_index) {
  340. Variant array = object->get_array();
  341. array.call("remove", p_index);
  342. emit_changed(get_edited_property(), array, "", false);
  343. update_property();
  344. }
  345. void EditorPropertyArray::_button_draw() {
  346. if (dropping) {
  347. Color color = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  348. edit->draw_rect(Rect2(Point2(), edit->get_size()), color, false);
  349. }
  350. }
  351. bool EditorPropertyArray::_is_drop_valid(const Dictionary &p_drag_data) const {
  352. String allowed_type = Variant::get_type_name(subtype);
  353. // When the subtype is of type Object, an additional subtype may be specified in the hint string
  354. // (e.g. Resource, Texture2D, ShaderMaterial, etc). We want the allowed type to be that, not just "Object".
  355. if (subtype == Variant::OBJECT && subtype_hint_string != "") {
  356. allowed_type = subtype_hint_string;
  357. }
  358. Dictionary drag_data = p_drag_data;
  359. if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  360. Vector<String> files = drag_data["files"];
  361. for (int i = 0; i < files.size(); i++) {
  362. String file = files[i];
  363. String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
  364. for (int j = 0; j < allowed_type.get_slice_count(","); j++) {
  365. String at = allowed_type.get_slice(",", j).strip_edges();
  366. // Fail if one of the files is not of allowed type.
  367. if (!ClassDB::is_parent_class(ftype, at)) {
  368. return false;
  369. }
  370. }
  371. }
  372. // If no files fail, drop is valid.
  373. return true;
  374. }
  375. return false;
  376. }
  377. bool EditorPropertyArray::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  378. return _is_drop_valid(p_data);
  379. }
  380. void EditorPropertyArray::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  381. ERR_FAIL_COND(!_is_drop_valid(p_data));
  382. Dictionary drag_data = p_data;
  383. if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  384. Vector<String> files = drag_data["files"];
  385. Variant array = object->get_array();
  386. // Handle the case where array is not initialised yet.
  387. if (!array.is_array()) {
  388. Callable::CallError ce;
  389. Variant::construct(array_type, array, nullptr, 0, ce);
  390. }
  391. // Loop the file array and add to existing array.
  392. for (int i = 0; i < files.size(); i++) {
  393. String file = files[i];
  394. RES res = ResourceLoader::load(file);
  395. if (res.is_valid()) {
  396. array.call("push_back", res);
  397. }
  398. }
  399. if (array.get_type() == Variant::ARRAY) {
  400. array = array.call("duplicate");
  401. }
  402. emit_changed(get_edited_property(), array, "", false);
  403. object->set_array(array);
  404. update_property();
  405. }
  406. }
  407. void EditorPropertyArray::_notification(int p_what) {
  408. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  409. if (is_visible_in_tree()) {
  410. if (_is_drop_valid(get_viewport()->gui_get_drag_data())) {
  411. dropping = true;
  412. edit->update();
  413. }
  414. }
  415. }
  416. if (p_what == NOTIFICATION_DRAG_END) {
  417. if (dropping) {
  418. dropping = false;
  419. edit->update();
  420. }
  421. }
  422. }
  423. void EditorPropertyArray::_edit_pressed() {
  424. Variant array = get_edited_object()->get(get_edited_property());
  425. if (!array.is_array()) {
  426. Callable::CallError ce;
  427. Variant::construct(array_type, array, nullptr, 0, ce);
  428. get_edited_object()->set(get_edited_property(), array);
  429. }
  430. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  431. update_property();
  432. }
  433. void EditorPropertyArray::_page_changed(double p_page) {
  434. if (updating) {
  435. return;
  436. }
  437. page_index = p_page;
  438. update_property();
  439. }
  440. void EditorPropertyArray::_length_changed(double p_page) {
  441. if (updating) {
  442. return;
  443. }
  444. Variant array = object->get_array();
  445. int previous_size = array.call("size");
  446. array.call("resize", int(p_page));
  447. if (array.get_type() == Variant::ARRAY) {
  448. if (subtype != Variant::NIL) {
  449. int size = array.call("size");
  450. for (int i = previous_size; i < size; i++) {
  451. if (array.get(i).get_type() == Variant::NIL) {
  452. Callable::CallError ce;
  453. Variant r;
  454. Variant::construct(subtype, r, nullptr, 0, ce);
  455. array.set(i, r);
  456. }
  457. }
  458. }
  459. array = array.call("duplicate"); // Duplicate, so undo/redo works better.
  460. } else {
  461. int size = array.call("size");
  462. // Pool*Array don't initialize their elements, have to do it manually.
  463. for (int i = previous_size; i < size; i++) {
  464. Callable::CallError ce;
  465. Variant r;
  466. Variant::construct(array.get(i).get_type(), r, nullptr, 0, ce);
  467. array.set(i, r);
  468. }
  469. }
  470. emit_changed(get_edited_property(), array, "", false);
  471. object->set_array(array);
  472. update_property();
  473. }
  474. void EditorPropertyArray::setup(Variant::Type p_array_type, const String &p_hint_string) {
  475. array_type = p_array_type;
  476. // The format of p_hint_string is:
  477. // subType/subTypeHint:nextSubtype ... etc.
  478. if (array_type == Variant::ARRAY && !p_hint_string.is_empty()) {
  479. int hint_subtype_separator = p_hint_string.find(":");
  480. if (hint_subtype_separator >= 0) {
  481. String subtype_string = p_hint_string.substr(0, hint_subtype_separator);
  482. int slash_pos = subtype_string.find("/");
  483. if (slash_pos >= 0) {
  484. subtype_hint = PropertyHint(subtype_string.substr(slash_pos + 1, subtype_string.size() - slash_pos - 1).to_int());
  485. subtype_string = subtype_string.substr(0, slash_pos);
  486. }
  487. subtype_hint_string = p_hint_string.substr(hint_subtype_separator + 1, p_hint_string.size() - hint_subtype_separator - 1);
  488. subtype = Variant::Type(subtype_string.to_int());
  489. }
  490. }
  491. }
  492. void EditorPropertyArray::_reorder_button_gui_input(const Ref<InputEvent> &p_event) {
  493. if (reorder_from_index < 0) {
  494. return;
  495. }
  496. Ref<InputEventMouseMotion> mm = p_event;
  497. if (mm.is_valid()) {
  498. Variant array = object->get_array();
  499. int size = array.call("size");
  500. if ((reorder_to_index == 0 && mm->get_relative().y < 0.0f) || (reorder_to_index == size - 1 && mm->get_relative().y > 0.0f)) {
  501. return;
  502. }
  503. reorder_mouse_y_delta += mm->get_relative().y;
  504. float required_y_distance = 20.0f * EDSCALE;
  505. if (ABS(reorder_mouse_y_delta) > required_y_distance) {
  506. int direction = reorder_mouse_y_delta > 0.0f ? 1 : -1;
  507. reorder_mouse_y_delta -= required_y_distance * direction;
  508. reorder_to_index += direction;
  509. if ((direction < 0 && reorder_to_index % page_length == page_length - 1) || (direction > 0 && reorder_to_index % page_length == 0)) {
  510. // Automatically move to the next/previous page.
  511. page_slider->set_value(page_index + direction);
  512. }
  513. vbox->move_child(reorder_selected_element_hbox, reorder_to_index % page_length + 2);
  514. // Ensure the moving element is visible.
  515. EditorNode::get_singleton()->get_inspector()->ensure_control_visible(reorder_selected_element_hbox);
  516. }
  517. }
  518. }
  519. void EditorPropertyArray::_reorder_button_down(int p_index) {
  520. reorder_from_index = p_index;
  521. reorder_to_index = p_index;
  522. reorder_selected_element_hbox = Object::cast_to<HBoxContainer>(vbox->get_child(p_index % page_length + 2));
  523. reorder_selected_button = Object::cast_to<Button>(reorder_selected_element_hbox->get_child(0));
  524. // Ideally it'd to be able to show the mouse but I had issues with
  525. // Control's `mouse_exit()`/`mouse_entered()` signals not getting called.
  526. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  527. }
  528. void EditorPropertyArray::_reorder_button_up() {
  529. if (reorder_from_index != reorder_to_index) {
  530. // Move the element.
  531. Variant array = object->get_array();
  532. Variant value_to_move = array.get(reorder_from_index);
  533. array.call("remove", reorder_from_index);
  534. array.call("insert", reorder_to_index, value_to_move);
  535. emit_changed(get_edited_property(), array, "", false);
  536. object->set_array(array);
  537. update_property();
  538. }
  539. reorder_from_index = -1;
  540. reorder_to_index = -1;
  541. reorder_mouse_y_delta = 0.0f;
  542. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  543. reorder_selected_button->warp_mouse(reorder_selected_button->get_size() / 2.0f);
  544. reorder_selected_element_hbox = nullptr;
  545. reorder_selected_button = nullptr;
  546. }
  547. void EditorPropertyArray::_bind_methods() {
  548. ClassDB::bind_method(D_METHOD("_can_drop_data_fw"), &EditorPropertyArray::can_drop_data_fw);
  549. ClassDB::bind_method(D_METHOD("_drop_data_fw"), &EditorPropertyArray::drop_data_fw);
  550. }
  551. EditorPropertyArray::EditorPropertyArray() {
  552. object.instantiate();
  553. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  554. edit = memnew(Button);
  555. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  556. edit->set_clip_text(true);
  557. edit->connect("pressed", callable_mp(this, &EditorPropertyArray::_edit_pressed));
  558. edit->set_toggle_mode(true);
  559. edit->set_drag_forwarding(this);
  560. edit->connect("draw", callable_mp(this, &EditorPropertyArray::_button_draw));
  561. add_child(edit);
  562. add_focusable(edit);
  563. vbox = nullptr;
  564. page_slider = nullptr;
  565. size_slider = nullptr;
  566. updating = false;
  567. change_type = memnew(PopupMenu);
  568. add_child(change_type);
  569. change_type->connect("id_pressed", callable_mp(this, &EditorPropertyArray::_change_type_menu));
  570. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  571. String type = Variant::get_type_name(Variant::Type(i));
  572. change_type->add_item(type, i);
  573. }
  574. change_type->add_separator();
  575. change_type->add_item(TTR("Remove Item"), Variant::VARIANT_MAX);
  576. changing_type_index = -1;
  577. subtype = Variant::NIL;
  578. subtype_hint = PROPERTY_HINT_NONE;
  579. subtype_hint_string = "";
  580. dropping = false;
  581. }
  582. ///////////////////// DICTIONARY ///////////////////////////
  583. void EditorPropertyDictionary::_property_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing) {
  584. if (p_property == "new_item_key") {
  585. object->set_new_item_key(p_value);
  586. } else if (p_property == "new_item_value") {
  587. object->set_new_item_value(p_value);
  588. } else if (p_property.begins_with("indices")) {
  589. int index = p_property.get_slice("/", 1).to_int();
  590. Dictionary dict = object->get_dict();
  591. Variant key = dict.get_key_at_index(index);
  592. dict[key] = p_value;
  593. emit_changed(get_edited_property(), dict, "", true);
  594. dict = dict.duplicate(); // Duplicate, so undo/redo works better\.
  595. object->set_dict(dict);
  596. }
  597. }
  598. void EditorPropertyDictionary::_change_type(Object *p_button, int p_index) {
  599. Button *button = Object::cast_to<Button>(p_button);
  600. Rect2 rect = button->get_screen_rect();
  601. change_type->set_as_minsize();
  602. change_type->set_position(rect.get_end() - Vector2(change_type->get_contents_minimum_size().x, 0));
  603. change_type->popup();
  604. changing_type_index = p_index;
  605. }
  606. void EditorPropertyDictionary::_add_key_value() {
  607. // Do not allow nil as valid key. I experienced errors with this
  608. if (object->get_new_item_key().get_type() == Variant::NIL) {
  609. return;
  610. }
  611. Dictionary dict = object->get_dict();
  612. dict[object->get_new_item_key()] = object->get_new_item_value();
  613. object->set_new_item_key(Variant());
  614. object->set_new_item_value(Variant());
  615. emit_changed(get_edited_property(), dict, "", false);
  616. dict = dict.duplicate(); // Duplicate, so undo/redo works better.
  617. object->set_dict(dict);
  618. update_property();
  619. }
  620. void EditorPropertyDictionary::_change_type_menu(int p_index) {
  621. if (changing_type_index < 0) {
  622. Variant value;
  623. Callable::CallError ce;
  624. Variant::construct(Variant::Type(p_index), value, nullptr, 0, ce);
  625. if (changing_type_index == -1) {
  626. object->set_new_item_key(value);
  627. } else {
  628. object->set_new_item_value(value);
  629. }
  630. update_property();
  631. return;
  632. }
  633. Dictionary dict = object->get_dict();
  634. if (p_index < Variant::VARIANT_MAX) {
  635. Variant value;
  636. Callable::CallError ce;
  637. Variant::construct(Variant::Type(p_index), value, nullptr, 0, ce);
  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. dict = dict.duplicate(); // Duplicate, so undo/redo works better\.
  646. object->set_dict(dict);
  647. update_property();
  648. }
  649. void EditorPropertyDictionary::update_property() {
  650. Variant updated_val = get_edited_object()->get(get_edited_property());
  651. if (updated_val.get_type() == Variant::NIL) {
  652. edit->set_text("Dictionary (Nil)"); // This provides symmetry with the array property.
  653. edit->set_pressed(false);
  654. if (vbox) {
  655. set_bottom_editor(nullptr);
  656. memdelete(vbox);
  657. vbox = nullptr;
  658. }
  659. return;
  660. }
  661. Dictionary dict = updated_val;
  662. edit->set_text("Dictionary (size " + itos(dict.size()) + ")");
  663. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  664. if (edit->is_pressed() != unfolded) {
  665. edit->set_pressed(unfolded);
  666. }
  667. if (unfolded) {
  668. updating = true;
  669. if (!vbox) {
  670. vbox = memnew(VBoxContainer);
  671. add_child(vbox);
  672. set_bottom_editor(vbox);
  673. page_hbox = memnew(HBoxContainer);
  674. vbox->add_child(page_hbox);
  675. Label *label = memnew(Label(TTR("Page: ")));
  676. label->set_h_size_flags(SIZE_EXPAND_FILL);
  677. page_hbox->add_child(label);
  678. page_slider = memnew(EditorSpinSlider);
  679. page_slider->set_step(1);
  680. page_hbox->add_child(page_slider);
  681. page_slider->set_h_size_flags(SIZE_EXPAND_FILL);
  682. page_slider->connect("value_changed", callable_mp(this, &EditorPropertyDictionary::_page_changed));
  683. } else {
  684. // Queue children for deletion, deleting immediately might cause errors.
  685. for (int i = 1; i < vbox->get_child_count(); i++) {
  686. vbox->get_child(i)->queue_delete();
  687. }
  688. }
  689. int size = dict.size();
  690. int pages = MAX(0, size - 1) / page_length + 1;
  691. page_slider->set_max(pages);
  692. page_index = MIN(page_index, pages - 1);
  693. page_slider->set_value(page_index);
  694. page_hbox->set_visible(pages > 1);
  695. int offset = page_index * page_length;
  696. int amount = MIN(size - offset, page_length);
  697. dict = dict.duplicate();
  698. object->set_dict(dict);
  699. VBoxContainer *add_vbox = nullptr;
  700. double default_float_step = EDITOR_GET("interface/inspector/default_float_step");
  701. for (int i = 0; i < amount + 2; 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, 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. prop = memnew(EditorPropertyText);
  737. } break;
  738. // Math types.
  739. case Variant::VECTOR2: {
  740. EditorPropertyVector2 *editor = memnew(EditorPropertyVector2);
  741. editor->setup(-100000, 100000, default_float_step, true);
  742. prop = editor;
  743. } break;
  744. case Variant::VECTOR2I: {
  745. EditorPropertyVector2i *editor = memnew(EditorPropertyVector2i);
  746. editor->setup(-100000, 100000, true);
  747. prop = editor;
  748. } break;
  749. case Variant::RECT2: {
  750. EditorPropertyRect2 *editor = memnew(EditorPropertyRect2);
  751. editor->setup(-100000, 100000, default_float_step, true);
  752. prop = editor;
  753. } break;
  754. case Variant::RECT2I: {
  755. EditorPropertyRect2i *editor = memnew(EditorPropertyRect2i);
  756. editor->setup(-100000, 100000, true);
  757. prop = editor;
  758. } break;
  759. case Variant::VECTOR3: {
  760. EditorPropertyVector3 *editor = memnew(EditorPropertyVector3);
  761. editor->setup(-100000, 100000, default_float_step, true);
  762. prop = editor;
  763. } break;
  764. case Variant::VECTOR3I: {
  765. EditorPropertyVector3i *editor = memnew(EditorPropertyVector3i);
  766. editor->setup(-100000, 100000, true);
  767. prop = editor;
  768. } break;
  769. case Variant::TRANSFORM2D: {
  770. EditorPropertyTransform2D *editor = memnew(EditorPropertyTransform2D);
  771. editor->setup(-100000, 100000, default_float_step, true);
  772. prop = editor;
  773. } break;
  774. case Variant::PLANE: {
  775. EditorPropertyPlane *editor = memnew(EditorPropertyPlane);
  776. editor->setup(-100000, 100000, default_float_step, true);
  777. prop = editor;
  778. } break;
  779. case Variant::QUATERNION: {
  780. EditorPropertyQuaternion *editor = memnew(EditorPropertyQuaternion);
  781. editor->setup(-100000, 100000, default_float_step, true);
  782. prop = editor;
  783. } break;
  784. case Variant::AABB: {
  785. EditorPropertyAABB *editor = memnew(EditorPropertyAABB);
  786. editor->setup(-100000, 100000, default_float_step, true);
  787. prop = editor;
  788. } break;
  789. case Variant::BASIS: {
  790. EditorPropertyBasis *editor = memnew(EditorPropertyBasis);
  791. editor->setup(-100000, 100000, default_float_step, true);
  792. prop = editor;
  793. } break;
  794. case Variant::TRANSFORM3D: {
  795. EditorPropertyTransform3D *editor = memnew(EditorPropertyTransform3D);
  796. editor->setup(-100000, 100000, default_float_step, true);
  797. prop = editor;
  798. } break;
  799. // Miscellaneous types.
  800. case Variant::COLOR: {
  801. prop = memnew(EditorPropertyColor);
  802. } break;
  803. case Variant::STRING_NAME: {
  804. EditorPropertyText *ept = memnew(EditorPropertyText);
  805. ept->set_string_name(true);
  806. prop = ept;
  807. } break;
  808. case Variant::NODE_PATH: {
  809. prop = memnew(EditorPropertyNodePath);
  810. } break;
  811. case Variant::RID: {
  812. prop = memnew(EditorPropertyRID);
  813. } break;
  814. case Variant::OBJECT: {
  815. if (Object::cast_to<EncodedObjectAsID>(value)) {
  816. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  817. editor->setup("Object");
  818. prop = editor;
  819. } else {
  820. EditorPropertyResource *editor = memnew(EditorPropertyResource);
  821. editor->setup(object.ptr(), prop_name, "Resource");
  822. prop = editor;
  823. }
  824. } break;
  825. case Variant::DICTIONARY: {
  826. prop = memnew(EditorPropertyDictionary);
  827. } break;
  828. case Variant::ARRAY: {
  829. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  830. editor->setup(Variant::ARRAY);
  831. prop = editor;
  832. } break;
  833. // Arrays.
  834. case Variant::PACKED_BYTE_ARRAY: {
  835. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  836. editor->setup(Variant::PACKED_BYTE_ARRAY);
  837. prop = editor;
  838. } break;
  839. case Variant::PACKED_INT32_ARRAY: {
  840. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  841. editor->setup(Variant::PACKED_INT32_ARRAY);
  842. prop = editor;
  843. } break;
  844. case Variant::PACKED_FLOAT32_ARRAY: {
  845. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  846. editor->setup(Variant::PACKED_FLOAT32_ARRAY);
  847. prop = editor;
  848. } break;
  849. case Variant::PACKED_INT64_ARRAY: {
  850. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  851. editor->setup(Variant::PACKED_INT64_ARRAY);
  852. prop = editor;
  853. } break;
  854. case Variant::PACKED_FLOAT64_ARRAY: {
  855. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  856. editor->setup(Variant::PACKED_FLOAT64_ARRAY);
  857. prop = editor;
  858. } break;
  859. case Variant::PACKED_STRING_ARRAY: {
  860. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  861. editor->setup(Variant::PACKED_STRING_ARRAY);
  862. prop = editor;
  863. } break;
  864. case Variant::PACKED_VECTOR2_ARRAY: {
  865. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  866. editor->setup(Variant::PACKED_VECTOR2_ARRAY);
  867. prop = editor;
  868. } break;
  869. case Variant::PACKED_VECTOR3_ARRAY: {
  870. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  871. editor->setup(Variant::PACKED_VECTOR3_ARRAY);
  872. prop = editor;
  873. } break;
  874. case Variant::PACKED_COLOR_ARRAY: {
  875. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  876. editor->setup(Variant::PACKED_COLOR_ARRAY);
  877. prop = editor;
  878. } break;
  879. default: {
  880. }
  881. }
  882. if (i == amount) {
  883. PanelContainer *pc = memnew(PanelContainer);
  884. vbox->add_child(pc);
  885. Ref<StyleBoxFlat> flat;
  886. flat.instantiate();
  887. for (int j = 0; j < 4; j++) {
  888. flat->set_default_margin(Side(j), 2 * EDSCALE);
  889. }
  890. flat->set_bg_color(get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  891. pc->add_theme_style_override("panel", flat);
  892. add_vbox = memnew(VBoxContainer);
  893. pc->add_child(add_vbox);
  894. }
  895. prop->set_object_and_property(object.ptr(), prop_name);
  896. int change_index = 0;
  897. if (i < amount) {
  898. String cs = key.get_construct_string();
  899. prop->set_label(key.get_construct_string());
  900. prop->set_tooltip(cs);
  901. change_index = i + offset;
  902. } else if (i == amount) {
  903. prop->set_label(TTR("New Key:"));
  904. change_index = -1;
  905. } else if (i == amount + 1) {
  906. prop->set_label(TTR("New Value:"));
  907. change_index = -2;
  908. }
  909. prop->set_selectable(false);
  910. prop->connect("property_changed", callable_mp(this, &EditorPropertyDictionary::_property_changed));
  911. prop->connect("object_id_selected", callable_mp(this, &EditorPropertyDictionary::_object_id_selected));
  912. HBoxContainer *hbox = memnew(HBoxContainer);
  913. if (add_vbox) {
  914. add_vbox->add_child(hbox);
  915. } else {
  916. vbox->add_child(hbox);
  917. }
  918. hbox->add_child(prop);
  919. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  920. Button *edit = memnew(Button);
  921. edit->set_icon(get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")));
  922. hbox->add_child(edit);
  923. edit->connect("pressed", callable_mp(this, &EditorPropertyDictionary::_change_type), varray(edit, change_index));
  924. prop->update_property();
  925. if (i == amount + 1) {
  926. Button *butt_add_item = memnew(Button);
  927. butt_add_item->set_text(TTR("Add Key/Value Pair"));
  928. butt_add_item->connect("pressed", callable_mp(this, &EditorPropertyDictionary::_add_key_value));
  929. add_vbox->add_child(butt_add_item);
  930. }
  931. }
  932. updating = false;
  933. } else {
  934. if (vbox) {
  935. set_bottom_editor(nullptr);
  936. memdelete(vbox);
  937. vbox = nullptr;
  938. }
  939. }
  940. }
  941. void EditorPropertyDictionary::_object_id_selected(const StringName &p_property, ObjectID p_id) {
  942. emit_signal(SNAME("object_id_selected"), p_property, p_id);
  943. }
  944. void EditorPropertyDictionary::_notification(int p_what) {
  945. }
  946. void EditorPropertyDictionary::_edit_pressed() {
  947. Variant prop_val = get_edited_object()->get(get_edited_property());
  948. if (prop_val.get_type() == Variant::NIL) {
  949. Callable::CallError ce;
  950. Variant::construct(Variant::DICTIONARY, prop_val, nullptr, 0, ce);
  951. get_edited_object()->set(get_edited_property(), prop_val);
  952. }
  953. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  954. update_property();
  955. }
  956. void EditorPropertyDictionary::_page_changed(double p_page) {
  957. if (updating) {
  958. return;
  959. }
  960. page_index = p_page;
  961. update_property();
  962. }
  963. void EditorPropertyDictionary::_bind_methods() {
  964. }
  965. EditorPropertyDictionary::EditorPropertyDictionary() {
  966. object.instantiate();
  967. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  968. edit = memnew(Button);
  969. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  970. edit->set_clip_text(true);
  971. edit->connect("pressed", callable_mp(this, &EditorPropertyDictionary::_edit_pressed));
  972. edit->set_toggle_mode(true);
  973. add_child(edit);
  974. add_focusable(edit);
  975. vbox = nullptr;
  976. page_slider = nullptr;
  977. updating = false;
  978. change_type = memnew(PopupMenu);
  979. add_child(change_type);
  980. change_type->connect("id_pressed", callable_mp(this, &EditorPropertyDictionary::_change_type_menu));
  981. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  982. String type = Variant::get_type_name(Variant::Type(i));
  983. change_type->add_item(type, i);
  984. }
  985. change_type->add_separator();
  986. change_type->add_item(TTR("Remove Item"), Variant::VARIANT_MAX);
  987. changing_type_index = -1;
  988. }