editor_properties_array_dict.cpp 45 KB

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