editor_properties_array_dict.cpp 46 KB

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