editor_properties_array_dict.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. /*************************************************************************/
  2. /* editor_properties_array_dict.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "editor_properties_array_dict.h"
  31. #include "core/io/marshalls.h"
  32. #include "core/os/input.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_scale.h"
  35. #include "editor_properties.h"
  36. #include "scene/main/viewport.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::_property_changed(const String &p_property, Variant p_value, const String &p_name, bool p_changing) {
  129. if (p_property.begins_with("indices")) {
  130. int index = p_property.get_slice("/", 1).to_int();
  131. Variant array = object->get_array();
  132. array.set(index, p_value);
  133. emit_changed(get_edited_property(), array, "", true);
  134. if (array.get_type() == Variant::ARRAY) {
  135. array = array.call("duplicate"); // Duplicate, so undo/redo works better.
  136. }
  137. object->set_array(array);
  138. }
  139. }
  140. void EditorPropertyArray::_change_type(Object *p_button, int p_index) {
  141. Button *button = Object::cast_to<Button>(p_button);
  142. changing_type_index = p_index;
  143. Rect2 rect = button->get_global_rect();
  144. change_type->set_as_minsize();
  145. change_type->set_global_position(rect.position + rect.size - Vector2(change_type->get_combined_minimum_size().x, 0));
  146. change_type->popup();
  147. }
  148. void EditorPropertyArray::_change_type_menu(int p_index) {
  149. if (p_index == Variant::VARIANT_MAX) {
  150. _remove_pressed(changing_type_index);
  151. return;
  152. }
  153. Variant value;
  154. Variant::CallError ce;
  155. value = Variant::construct(Variant::Type(p_index), nullptr, 0, ce);
  156. Variant array = object->get_array();
  157. array.set(changing_type_index, value);
  158. emit_changed(get_edited_property(), array, "", true);
  159. if (array.get_type() == Variant::ARRAY) {
  160. array = array.call("duplicate"); // Duplicate, so undo/redo works better.
  161. }
  162. object->set_array(array);
  163. update_property();
  164. }
  165. void EditorPropertyArray::_object_id_selected(const String &p_property, ObjectID p_id) {
  166. emit_signal("object_id_selected", p_property, p_id);
  167. }
  168. void EditorPropertyArray::update_property() {
  169. Variant array = get_edited_object()->get(get_edited_property());
  170. String arrtype = "";
  171. switch (array_type) {
  172. case Variant::ARRAY: {
  173. arrtype = "Array";
  174. } break;
  175. // Arrays.
  176. case Variant::POOL_BYTE_ARRAY: {
  177. arrtype = "PoolByteArray";
  178. } break;
  179. case Variant::POOL_INT_ARRAY: {
  180. arrtype = "PoolIntArray";
  181. } break;
  182. case Variant::POOL_REAL_ARRAY: {
  183. arrtype = "PoolFloatArray";
  184. } break;
  185. case Variant::POOL_STRING_ARRAY: {
  186. arrtype = "PoolStringArray";
  187. } break;
  188. case Variant::POOL_VECTOR2_ARRAY: {
  189. arrtype = "PoolVector2Array";
  190. } break;
  191. case Variant::POOL_VECTOR3_ARRAY: {
  192. arrtype = "PoolVector3Array";
  193. } break;
  194. case Variant::POOL_COLOR_ARRAY: {
  195. arrtype = "PoolColorArray";
  196. } break;
  197. default: {
  198. }
  199. }
  200. if (array.get_type() == Variant::NIL) {
  201. edit->set_text(String("(Nil) ") + arrtype);
  202. edit->set_pressed(false);
  203. if (vbox) {
  204. set_bottom_editor(nullptr);
  205. memdelete(vbox);
  206. vbox = nullptr;
  207. }
  208. return;
  209. }
  210. int size = array.call("size");
  211. int pages = MAX(0, size - 1) / page_length + 1;
  212. page_index = MIN(page_index, pages - 1);
  213. int offset = page_index * page_length;
  214. edit->set_text(arrtype + " (size " + itos(size) + ")");
  215. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  216. if (edit->is_pressed() != unfolded) {
  217. edit->set_pressed(unfolded);
  218. }
  219. if (unfolded) {
  220. updating = true;
  221. if (!vbox) {
  222. vbox = memnew(VBoxContainer);
  223. add_child(vbox);
  224. set_bottom_editor(vbox);
  225. HBoxContainer *hbox = memnew(HBoxContainer);
  226. vbox->add_child(hbox);
  227. Label *label = memnew(Label(TTR("Size: ")));
  228. label->set_h_size_flags(SIZE_EXPAND_FILL);
  229. hbox->add_child(label);
  230. size_slider = memnew(EditorSpinSlider);
  231. size_slider->set_step(1);
  232. size_slider->set_max(1000000);
  233. size_slider->set_h_size_flags(SIZE_EXPAND_FILL);
  234. size_slider->connect("value_changed", this, "_length_changed");
  235. hbox->add_child(size_slider);
  236. page_hbox = memnew(HBoxContainer);
  237. vbox->add_child(page_hbox);
  238. label = memnew(Label(TTR("Page: ")));
  239. label->set_h_size_flags(SIZE_EXPAND_FILL);
  240. page_hbox->add_child(label);
  241. page_slider = memnew(EditorSpinSlider);
  242. page_slider->set_step(1);
  243. page_slider->set_h_size_flags(SIZE_EXPAND_FILL);
  244. page_slider->connect("value_changed", this, "_page_changed");
  245. page_hbox->add_child(page_slider);
  246. } else {
  247. // Bye bye children of the box.
  248. for (int i = vbox->get_child_count() - 1; i >= 2; i--) {
  249. Node *child = vbox->get_child(i);
  250. if (child == reorder_selected_element_hbox) {
  251. continue; // Don't remove the property that the user is moving.
  252. }
  253. child->queue_delete(); // Button still needed after pressed is called.
  254. vbox->remove_child(child);
  255. }
  256. }
  257. size_slider->set_value(size);
  258. page_slider->set_max(pages);
  259. page_slider->set_value(page_index);
  260. page_hbox->set_visible(pages > 1);
  261. if (array.get_type() == Variant::ARRAY) {
  262. array = array.call("duplicate");
  263. }
  264. object->set_array(array);
  265. int amount = MIN(size - offset, page_length);
  266. for (int i = 0; i < amount; i++) {
  267. bool reorder_is_from_current_page = reorder_from_index / page_length == page_index;
  268. if (reorder_is_from_current_page && i == reorder_from_index % page_length) {
  269. // Don't duplicate the property that the user is moving.
  270. continue;
  271. }
  272. if (!reorder_is_from_current_page && i == reorder_to_index % page_length) {
  273. // Don't create the property the moving property will take the place of,
  274. // e.g. (if page_length == 20) don't create element 20 if dragging an item from
  275. // the first page to the second page because element 20 would become element 19.
  276. continue;
  277. }
  278. HBoxContainer *hbox = memnew(HBoxContainer);
  279. vbox->add_child(hbox);
  280. Button *reorder_button = memnew(Button);
  281. reorder_button->set_icon(get_icon("TripleBar", "EditorIcons"));
  282. reorder_button->set_default_cursor_shape(Control::CURSOR_MOVE);
  283. reorder_button->connect("gui_input", this, "_reorder_button_gui_input");
  284. reorder_button->connect("button_down", this, "_reorder_button_down", varray(i + offset));
  285. reorder_button->connect("button_up", this, "_reorder_button_up");
  286. hbox->add_child(reorder_button);
  287. String prop_name = "indices/" + itos(i + offset);
  288. EditorProperty *prop = nullptr;
  289. Variant value = array.get(i + offset);
  290. Variant::Type value_type = value.get_type();
  291. if (value_type == Variant::NIL && subtype != Variant::NIL) {
  292. value_type = subtype;
  293. }
  294. if (value_type == Variant::OBJECT && Object::cast_to<EncodedObjectAsID>(value)) {
  295. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  296. editor->setup("Object");
  297. prop = editor;
  298. } else {
  299. prop = EditorInspector::instantiate_property_editor(nullptr, value_type, "", subtype_hint, subtype_hint_string, 0);
  300. }
  301. prop->set_object_and_property(object.ptr(), prop_name);
  302. prop->set_label(itos(i + offset));
  303. prop->set_selectable(false);
  304. prop->connect("property_changed", this, "_property_changed");
  305. prop->connect("object_id_selected", this, "_object_id_selected");
  306. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  307. hbox->add_child(prop);
  308. bool is_untyped_array = array.get_type() == Variant::ARRAY && subtype == Variant::NIL;
  309. if (is_untyped_array) {
  310. Button *edit = memnew(Button);
  311. edit->set_icon(get_icon("Edit", "EditorIcons"));
  312. hbox->add_child(edit);
  313. edit->connect("pressed", this, "_change_type", varray(edit, i + offset));
  314. } else {
  315. Button *remove = memnew(Button);
  316. remove->set_icon(get_icon("Remove", "EditorIcons"));
  317. remove->connect("pressed", this, "_remove_pressed", varray(i + offset));
  318. hbox->add_child(remove);
  319. }
  320. prop->update_property();
  321. }
  322. if (reorder_to_index % page_length > 0) {
  323. vbox->move_child(vbox->get_child(2), reorder_to_index % page_length + 2);
  324. }
  325. updating = false;
  326. } else {
  327. if (vbox) {
  328. set_bottom_editor(nullptr);
  329. memdelete(vbox);
  330. vbox = nullptr;
  331. }
  332. }
  333. }
  334. void EditorPropertyArray::_remove_pressed(int p_index) {
  335. Variant array = object->get_array();
  336. array.call("remove", p_index);
  337. emit_changed(get_edited_property(), array, "", false);
  338. update_property();
  339. }
  340. void EditorPropertyArray::_button_draw() {
  341. if (dropping) {
  342. Color color = get_color("accent_color", "Editor");
  343. edit->draw_rect(Rect2(Point2(), edit->get_size()), color, false);
  344. }
  345. }
  346. bool EditorPropertyArray::_is_drop_valid(const Dictionary &p_drag_data) const {
  347. String allowed_type = Variant::get_type_name(subtype);
  348. // When the subtype is of type Object, an additional subtype may be specified in the hint string
  349. // (e.g. Resource, Texture2D, ShaderMaterial, etc). We want the allowed type to be that, not just "Object".
  350. if (subtype == Variant::OBJECT && subtype_hint_string != "") {
  351. allowed_type = subtype_hint_string;
  352. }
  353. Dictionary drag_data = p_drag_data;
  354. if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  355. Vector<String> files = drag_data["files"];
  356. for (int i = 0; i < files.size(); i++) {
  357. String file = files[i];
  358. String ftype = EditorFileSystem::get_singleton()->get_file_type(file);
  359. for (int j = 0; j < allowed_type.get_slice_count(","); j++) {
  360. String at = allowed_type.get_slice(",", j).strip_edges();
  361. // Fail if one of the files is not of allowed type
  362. if (!ClassDB::is_parent_class(ftype, at)) {
  363. return false;
  364. }
  365. }
  366. }
  367. // If no files fail, drop is valid
  368. return true;
  369. }
  370. return false;
  371. }
  372. bool EditorPropertyArray::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  373. return _is_drop_valid(p_data);
  374. }
  375. void EditorPropertyArray::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  376. ERR_FAIL_COND(!_is_drop_valid(p_data));
  377. Dictionary drag_data = p_data;
  378. if (drag_data.has("type") && String(drag_data["type"]) == "files") {
  379. Vector<String> files = drag_data["files"];
  380. Variant array = object->get_array();
  381. // Handle the case where array is not initialised yet.
  382. if (!array.is_array()) {
  383. Variant::CallError ce;
  384. array = Variant::construct(array_type, nullptr, 0, ce);
  385. }
  386. // Loop the file array and add to existing array.
  387. for (int i = 0; i < files.size(); i++) {
  388. String file = files[i];
  389. RES res = ResourceLoader::load(file);
  390. if (res.is_valid()) {
  391. array.call("push_back", res);
  392. }
  393. }
  394. if (array.get_type() == Variant::ARRAY) {
  395. array = array.call("duplicate");
  396. }
  397. emit_changed(get_edited_property(), array, "", false);
  398. object->set_array(array);
  399. update_property();
  400. }
  401. }
  402. void EditorPropertyArray::_notification(int p_what) {
  403. if (p_what == NOTIFICATION_DRAG_BEGIN) {
  404. if (is_visible_in_tree()) {
  405. if (_is_drop_valid(get_viewport()->gui_get_drag_data())) {
  406. dropping = true;
  407. edit->update();
  408. }
  409. }
  410. }
  411. if (p_what == NOTIFICATION_DRAG_END) {
  412. if (dropping) {
  413. dropping = false;
  414. edit->update();
  415. }
  416. }
  417. }
  418. void EditorPropertyArray::_edit_pressed() {
  419. Variant array = get_edited_object()->get(get_edited_property());
  420. if (!array.is_array()) {
  421. Variant::CallError ce;
  422. array = Variant::construct(array_type, nullptr, 0, ce);
  423. get_edited_object()->set(get_edited_property(), array);
  424. }
  425. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  426. update_property();
  427. }
  428. void EditorPropertyArray::_page_changed(double p_page) {
  429. if (updating) {
  430. return;
  431. }
  432. page_index = p_page;
  433. update_property();
  434. }
  435. void EditorPropertyArray::_length_changed(double p_page) {
  436. if (updating) {
  437. return;
  438. }
  439. Variant array = object->get_array();
  440. int previous_size = array.call("size");
  441. array.call("resize", int(p_page));
  442. if (array.get_type() == Variant::ARRAY) {
  443. if (subtype != Variant::NIL) {
  444. int size = array.call("size");
  445. for (int i = previous_size; i < size; i++) {
  446. if (array.get(i).get_type() == Variant::NIL) {
  447. Variant::CallError ce;
  448. array.set(i, Variant::construct(subtype, nullptr, 0, ce));
  449. }
  450. }
  451. }
  452. array = array.call("duplicate"); // Duplicate, so undo/redo works better.
  453. } else {
  454. int size = array.call("size");
  455. // Pool*Array don't initialize their elements, have to do it manually.
  456. for (int i = previous_size; i < size; i++) {
  457. Variant::CallError ce;
  458. array.set(i, Variant::construct(array.get(i).get_type(), nullptr, 0, ce));
  459. }
  460. }
  461. emit_changed(get_edited_property(), array, "", false);
  462. object->set_array(array);
  463. update_property();
  464. }
  465. void EditorPropertyArray::setup(Variant::Type p_array_type, const String &p_hint_string) {
  466. array_type = p_array_type;
  467. // The format of p_hint_string is:
  468. // subType/subTypeHint:nextSubtype ... etc.
  469. if (array_type == Variant::ARRAY && !p_hint_string.empty()) {
  470. int hint_subtype_separator = p_hint_string.find(":");
  471. if (hint_subtype_separator >= 0) {
  472. String subtype_string = p_hint_string.substr(0, hint_subtype_separator);
  473. int slash_pos = subtype_string.find("/");
  474. if (slash_pos >= 0) {
  475. subtype_hint = PropertyHint(subtype_string.substr(slash_pos + 1, subtype_string.size() - slash_pos - 1).to_int());
  476. subtype_string = subtype_string.substr(0, slash_pos);
  477. }
  478. subtype_hint_string = p_hint_string.substr(hint_subtype_separator + 1, p_hint_string.size() - hint_subtype_separator - 1);
  479. subtype = Variant::Type(subtype_string.to_int());
  480. }
  481. }
  482. }
  483. void EditorPropertyArray::_reorder_button_gui_input(const Ref<InputEvent> &p_event) {
  484. if (reorder_from_index < 0) {
  485. return;
  486. }
  487. Ref<InputEventMouseMotion> mm = p_event;
  488. if (mm.is_valid()) {
  489. Variant array = object->get_array();
  490. int size = array.call("size");
  491. if ((reorder_to_index == 0 && mm->get_relative().y < 0.0f) || (reorder_to_index == size - 1 && mm->get_relative().y > 0.0f)) {
  492. return;
  493. }
  494. reorder_mouse_y_delta += mm->get_relative().y;
  495. float required_y_distance = 20.0f * EDSCALE;
  496. if (ABS(reorder_mouse_y_delta) > required_y_distance) {
  497. int direction = reorder_mouse_y_delta > 0.0f ? 1 : -1;
  498. reorder_mouse_y_delta -= required_y_distance * direction;
  499. reorder_to_index += direction;
  500. if ((direction < 0 && reorder_to_index % page_length == page_length - 1) || (direction > 0 && reorder_to_index % page_length == 0)) {
  501. // Automatically move to the next/previous page.
  502. page_slider->set_value(page_index + direction);
  503. }
  504. vbox->move_child(reorder_selected_element_hbox, reorder_to_index % page_length + 2);
  505. // Ensure the moving element is visible.
  506. EditorNode::get_singleton()->get_inspector()->ensure_control_visible(reorder_selected_element_hbox);
  507. }
  508. }
  509. }
  510. void EditorPropertyArray::_reorder_button_down(int p_index) {
  511. reorder_from_index = p_index;
  512. reorder_to_index = p_index;
  513. reorder_selected_element_hbox = Object::cast_to<HBoxContainer>(vbox->get_child(p_index % page_length + 2));
  514. reorder_selected_button = Object::cast_to<Button>(reorder_selected_element_hbox->get_child(0));
  515. // Ideally it'd to be able to show the mouse but I had issues with
  516. // Control's `mouse_exit()`/`mouse_entered()` signals not getting called.
  517. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  518. }
  519. void EditorPropertyArray::_reorder_button_up() {
  520. if (reorder_from_index != reorder_to_index) {
  521. // Move the element.
  522. Variant array = object->get_array();
  523. Variant value_to_move = array.get(reorder_from_index);
  524. array.call("remove", reorder_from_index);
  525. array.call("insert", reorder_to_index, value_to_move);
  526. emit_changed(get_edited_property(), array, "", false);
  527. object->set_array(array);
  528. update_property();
  529. }
  530. reorder_from_index = -1;
  531. reorder_to_index = -1;
  532. reorder_mouse_y_delta = 0.0f;
  533. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  534. reorder_selected_button->warp_mouse(reorder_selected_button->get_size() / 2.0f);
  535. reorder_selected_element_hbox = nullptr;
  536. reorder_selected_button = nullptr;
  537. }
  538. void EditorPropertyArray::_bind_methods() {
  539. ClassDB::bind_method("_edit_pressed", &EditorPropertyArray::_edit_pressed);
  540. ClassDB::bind_method("_page_changed", &EditorPropertyArray::_page_changed);
  541. ClassDB::bind_method("_length_changed", &EditorPropertyArray::_length_changed);
  542. ClassDB::bind_method("_property_changed", &EditorPropertyArray::_property_changed, DEFVAL(String()), DEFVAL(false));
  543. ClassDB::bind_method("_change_type", &EditorPropertyArray::_change_type);
  544. ClassDB::bind_method("_change_type_menu", &EditorPropertyArray::_change_type_menu);
  545. ClassDB::bind_method("_object_id_selected", &EditorPropertyArray::_object_id_selected);
  546. ClassDB::bind_method("_remove_pressed", &EditorPropertyArray::_remove_pressed);
  547. ClassDB::bind_method("_reorder_button_gui_input", &EditorPropertyArray::_reorder_button_gui_input);
  548. ClassDB::bind_method("_reorder_button_down", &EditorPropertyArray::_reorder_button_down);
  549. ClassDB::bind_method("_reorder_button_up", &EditorPropertyArray::_reorder_button_up);
  550. ClassDB::bind_method("_button_draw", &EditorPropertyArray::_button_draw);
  551. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &EditorPropertyArray::can_drop_data_fw);
  552. ClassDB::bind_method(D_METHOD("drop_data_fw"), &EditorPropertyArray::drop_data_fw);
  553. }
  554. EditorPropertyArray::EditorPropertyArray() {
  555. object.instance();
  556. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  557. edit = memnew(Button);
  558. edit->set_flat(true);
  559. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  560. edit->set_clip_text(true);
  561. edit->connect("pressed", this, "_edit_pressed");
  562. edit->set_toggle_mode(true);
  563. edit->set_drag_forwarding(this);
  564. edit->connect("draw", this, "_button_draw");
  565. add_child(edit);
  566. add_focusable(edit);
  567. vbox = nullptr;
  568. page_slider = nullptr;
  569. size_slider = nullptr;
  570. updating = false;
  571. change_type = memnew(PopupMenu);
  572. add_child(change_type);
  573. change_type->connect("id_pressed", this, "_change_type_menu");
  574. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  575. String type = Variant::get_type_name(Variant::Type(i));
  576. change_type->add_item(type, i);
  577. }
  578. change_type->add_separator();
  579. change_type->add_item(TTR("Remove Item"), Variant::VARIANT_MAX);
  580. changing_type_index = -1;
  581. subtype = Variant::NIL;
  582. subtype_hint = PROPERTY_HINT_NONE;
  583. subtype_hint_string = "";
  584. dropping = false;
  585. }
  586. ///////////////////// DICTIONARY ///////////////////////////
  587. void EditorPropertyDictionary::_property_changed(const String &p_property, Variant p_value, const String &p_name, bool changing) {
  588. if (p_property == "new_item_key") {
  589. object->set_new_item_key(p_value);
  590. } else if (p_property == "new_item_value") {
  591. object->set_new_item_value(p_value);
  592. } else if (p_property.begins_with("indices")) {
  593. int index = p_property.get_slice("/", 1).to_int();
  594. Dictionary dict = object->get_dict();
  595. Variant key = dict.get_key_at_index(index);
  596. dict[key] = p_value;
  597. emit_changed(get_edited_property(), dict, "", true);
  598. dict = dict.duplicate(); // Duplicate, so undo/redo works better\.
  599. object->set_dict(dict);
  600. }
  601. }
  602. void EditorPropertyDictionary::_change_type(Object *p_button, int p_index) {
  603. Button *button = Object::cast_to<Button>(p_button);
  604. Rect2 rect = button->get_global_rect();
  605. change_type->set_as_minsize();
  606. change_type->set_global_position(rect.position + rect.size - Vector2(change_type->get_combined_minimum_size().x, 0));
  607. change_type->popup();
  608. changing_type_index = p_index;
  609. }
  610. void EditorPropertyDictionary::_add_key_value() {
  611. // Do not allow nil as valid key. I experienced errors with this
  612. if (object->get_new_item_key().get_type() == Variant::NIL) {
  613. return;
  614. }
  615. Dictionary dict = object->get_dict();
  616. dict[object->get_new_item_key()] = object->get_new_item_value();
  617. object->set_new_item_key(Variant());
  618. object->set_new_item_value(Variant());
  619. emit_changed(get_edited_property(), dict, "", false);
  620. dict = dict.duplicate(); // Duplicate, so undo/redo works better.
  621. object->set_dict(dict);
  622. update_property();
  623. }
  624. void EditorPropertyDictionary::_change_type_menu(int p_index) {
  625. if (changing_type_index < 0) {
  626. Variant value;
  627. Variant::CallError ce;
  628. value = Variant::construct(Variant::Type(p_index), nullptr, 0, ce);
  629. if (changing_type_index == -1) {
  630. object->set_new_item_key(value);
  631. } else {
  632. object->set_new_item_value(value);
  633. }
  634. update_property();
  635. return;
  636. }
  637. Dictionary dict = object->get_dict();
  638. if (p_index < Variant::VARIANT_MAX) {
  639. Variant value;
  640. Variant::CallError ce;
  641. value = Variant::construct(Variant::Type(p_index), nullptr, 0, ce);
  642. Variant key = dict.get_key_at_index(changing_type_index);
  643. dict[key] = value;
  644. } else {
  645. Variant key = dict.get_key_at_index(changing_type_index);
  646. dict.erase(key);
  647. }
  648. emit_changed(get_edited_property(), dict, "", false);
  649. dict = dict.duplicate(); // Duplicate, so undo/redo works better.
  650. object->set_dict(dict);
  651. update_property();
  652. }
  653. void EditorPropertyDictionary::update_property() {
  654. Variant updated_val = get_edited_object()->get(get_edited_property());
  655. if (updated_val.get_type() == Variant::NIL) {
  656. edit->set_text("Dictionary (Nil)"); // This provides symmetry with the array property.
  657. edit->set_pressed(false);
  658. if (vbox) {
  659. set_bottom_editor(nullptr);
  660. memdelete(vbox);
  661. vbox = nullptr;
  662. }
  663. return;
  664. }
  665. Dictionary dict = updated_val;
  666. edit->set_text("Dictionary (size " + itos(dict.size()) + ")");
  667. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  668. if (edit->is_pressed() != unfolded) {
  669. edit->set_pressed(unfolded);
  670. }
  671. if (unfolded) {
  672. updating = true;
  673. if (!vbox) {
  674. vbox = memnew(VBoxContainer);
  675. add_child(vbox);
  676. set_bottom_editor(vbox);
  677. page_hbox = memnew(HBoxContainer);
  678. vbox->add_child(page_hbox);
  679. Label *label = memnew(Label(TTR("Page: ")));
  680. label->set_h_size_flags(SIZE_EXPAND_FILL);
  681. page_hbox->add_child(label);
  682. page_slider = memnew(EditorSpinSlider);
  683. page_slider->set_step(1);
  684. page_hbox->add_child(page_slider);
  685. page_slider->set_h_size_flags(SIZE_EXPAND_FILL);
  686. page_slider->connect("value_changed", this, "_page_changed");
  687. } else {
  688. // Queue children for deletion, deleting immediately might cause errors.
  689. for (int i = 1; i < vbox->get_child_count(); i++) {
  690. vbox->get_child(i)->queue_delete();
  691. }
  692. }
  693. int size = dict.size();
  694. int pages = MAX(0, size - 1) / page_length + 1;
  695. page_slider->set_max(pages);
  696. page_index = MIN(page_index, pages - 1);
  697. page_slider->set_value(page_index);
  698. page_hbox->set_visible(pages > 1);
  699. int offset = page_index * page_length;
  700. int amount = MIN(size - offset, page_length);
  701. dict = dict.duplicate();
  702. object->set_dict(dict);
  703. VBoxContainer *add_vbox = nullptr;
  704. for (int i = 0; i < amount + 2; i++) {
  705. String prop_name;
  706. Variant key;
  707. Variant value;
  708. if (i < amount) {
  709. prop_name = "indices/" + itos(i + offset);
  710. key = dict.get_key_at_index(i + offset);
  711. value = dict.get_value_at_index(i + offset);
  712. } else if (i == amount) {
  713. prop_name = "new_item_key";
  714. value = object->get_new_item_key();
  715. } else if (i == amount + 1) {
  716. prop_name = "new_item_value";
  717. value = object->get_new_item_value();
  718. }
  719. EditorProperty *prop = nullptr;
  720. switch (value.get_type()) {
  721. case Variant::NIL: {
  722. prop = memnew(EditorPropertyNil);
  723. } break;
  724. // Atomic types.
  725. case Variant::BOOL: {
  726. prop = memnew(EditorPropertyCheck);
  727. } break;
  728. case Variant::INT: {
  729. EditorPropertyInteger *editor = memnew(EditorPropertyInteger);
  730. editor->setup(-100000, 100000, 1, true, true);
  731. prop = editor;
  732. } break;
  733. case Variant::REAL: {
  734. EditorPropertyFloat *editor = memnew(EditorPropertyFloat);
  735. editor->setup(-100000, 100000, 0.001, true, false, true, true);
  736. prop = editor;
  737. } break;
  738. case Variant::STRING: {
  739. prop = memnew(EditorPropertyText);
  740. } break;
  741. // Math types.
  742. case Variant::VECTOR2: {
  743. EditorPropertyVector2 *editor = memnew(EditorPropertyVector2);
  744. editor->setup(-100000, 100000, 0.001, true);
  745. prop = editor;
  746. } break;
  747. case Variant::RECT2: {
  748. EditorPropertyRect2 *editor = memnew(EditorPropertyRect2);
  749. editor->setup(-100000, 100000, 0.001, true);
  750. prop = editor;
  751. } break;
  752. case Variant::VECTOR3: {
  753. EditorPropertyVector3 *editor = memnew(EditorPropertyVector3);
  754. editor->setup(-100000, 100000, 0.001, true);
  755. prop = editor;
  756. } break;
  757. case Variant::TRANSFORM2D: {
  758. EditorPropertyTransform2D *editor = memnew(EditorPropertyTransform2D);
  759. editor->setup(-100000, 100000, 0.001, true);
  760. prop = editor;
  761. } break;
  762. case Variant::PLANE: {
  763. EditorPropertyPlane *editor = memnew(EditorPropertyPlane);
  764. editor->setup(-100000, 100000, 0.001, true);
  765. prop = editor;
  766. } break;
  767. case Variant::QUAT: {
  768. EditorPropertyQuat *editor = memnew(EditorPropertyQuat);
  769. editor->setup(-100000, 100000, 0.001, true);
  770. prop = editor;
  771. } break;
  772. case Variant::AABB: {
  773. EditorPropertyAABB *editor = memnew(EditorPropertyAABB);
  774. editor->setup(-100000, 100000, 0.001, true);
  775. prop = editor;
  776. } break;
  777. case Variant::BASIS: {
  778. EditorPropertyBasis *editor = memnew(EditorPropertyBasis);
  779. editor->setup(-100000, 100000, 0.001, true);
  780. prop = editor;
  781. } break;
  782. case Variant::TRANSFORM: {
  783. EditorPropertyTransform *editor = memnew(EditorPropertyTransform);
  784. editor->setup(-100000, 100000, 0.001, true);
  785. prop = editor;
  786. } break;
  787. // Miscellaneous types.
  788. case Variant::COLOR: {
  789. prop = memnew(EditorPropertyColor);
  790. } break;
  791. case Variant::NODE_PATH: {
  792. prop = memnew(EditorPropertyNodePath);
  793. } break;
  794. case Variant::_RID: {
  795. prop = memnew(EditorPropertyRID);
  796. } break;
  797. case Variant::OBJECT: {
  798. if (Object::cast_to<EncodedObjectAsID>(value)) {
  799. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  800. editor->setup("Object");
  801. prop = editor;
  802. } else {
  803. EditorPropertyResource *editor = memnew(EditorPropertyResource);
  804. editor->setup(object.ptr(), prop_name, "Resource");
  805. prop = editor;
  806. }
  807. } break;
  808. case Variant::DICTIONARY: {
  809. prop = memnew(EditorPropertyDictionary);
  810. } break;
  811. case Variant::ARRAY: {
  812. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  813. editor->setup(Variant::ARRAY);
  814. prop = editor;
  815. } break;
  816. // Arrays.
  817. case Variant::POOL_BYTE_ARRAY: {
  818. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  819. editor->setup(Variant::POOL_BYTE_ARRAY);
  820. prop = editor;
  821. } break;
  822. case Variant::POOL_INT_ARRAY: {
  823. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  824. editor->setup(Variant::POOL_INT_ARRAY);
  825. prop = editor;
  826. } break;
  827. case Variant::POOL_REAL_ARRAY: {
  828. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  829. editor->setup(Variant::POOL_REAL_ARRAY);
  830. prop = editor;
  831. } break;
  832. case Variant::POOL_STRING_ARRAY: {
  833. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  834. editor->setup(Variant::POOL_STRING_ARRAY);
  835. prop = editor;
  836. } break;
  837. case Variant::POOL_VECTOR2_ARRAY: {
  838. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  839. editor->setup(Variant::POOL_VECTOR2_ARRAY);
  840. prop = editor;
  841. } break;
  842. case Variant::POOL_VECTOR3_ARRAY: {
  843. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  844. editor->setup(Variant::POOL_VECTOR3_ARRAY);
  845. prop = editor;
  846. } break;
  847. case Variant::POOL_COLOR_ARRAY: {
  848. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  849. editor->setup(Variant::POOL_COLOR_ARRAY);
  850. prop = editor;
  851. } break;
  852. default: {
  853. }
  854. }
  855. if (i == amount) {
  856. PanelContainer *pc = memnew(PanelContainer);
  857. vbox->add_child(pc);
  858. Ref<StyleBoxFlat> flat;
  859. flat.instance();
  860. for (int j = 0; j < 4; j++) {
  861. flat->set_default_margin(Margin(j), 2 * EDSCALE);
  862. }
  863. flat->set_bg_color(get_color("prop_subsection", "Editor"));
  864. pc->add_style_override("panel", flat);
  865. add_vbox = memnew(VBoxContainer);
  866. pc->add_child(add_vbox);
  867. }
  868. prop->set_object_and_property(object.ptr(), prop_name);
  869. int change_index = 0;
  870. if (i < amount) {
  871. String cs = key.get_construct_string();
  872. prop->set_label(key.get_construct_string());
  873. prop->set_tooltip(cs);
  874. change_index = i + offset;
  875. } else if (i == amount) {
  876. prop->set_label(TTR("New Key:"));
  877. change_index = -1;
  878. } else if (i == amount + 1) {
  879. prop->set_label(TTR("New Value:"));
  880. change_index = -2;
  881. }
  882. prop->set_selectable(false);
  883. prop->connect("property_changed", this, "_property_changed");
  884. prop->connect("object_id_selected", this, "_object_id_selected");
  885. HBoxContainer *hbox = memnew(HBoxContainer);
  886. if (add_vbox) {
  887. add_vbox->add_child(hbox);
  888. } else {
  889. vbox->add_child(hbox);
  890. }
  891. hbox->add_child(prop);
  892. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  893. Button *edit = memnew(Button);
  894. edit->set_icon(get_icon("Edit", "EditorIcons"));
  895. hbox->add_child(edit);
  896. edit->connect("pressed", this, "_change_type", varray(edit, change_index));
  897. prop->update_property();
  898. if (i == amount + 1) {
  899. Button *butt_add_item = memnew(Button);
  900. butt_add_item->set_text(TTR("Add Key/Value Pair"));
  901. butt_add_item->connect("pressed", this, "_add_key_value");
  902. add_vbox->add_child(butt_add_item);
  903. }
  904. }
  905. updating = false;
  906. } else {
  907. if (vbox) {
  908. set_bottom_editor(nullptr);
  909. memdelete(vbox);
  910. vbox = nullptr;
  911. }
  912. }
  913. }
  914. void EditorPropertyDictionary::_object_id_selected(const String &p_property, ObjectID p_id) {
  915. emit_signal("object_id_selected", p_property, p_id);
  916. }
  917. void EditorPropertyDictionary::_notification(int p_what) {
  918. }
  919. void EditorPropertyDictionary::_edit_pressed() {
  920. Variant prop_val = get_edited_object()->get(get_edited_property());
  921. if (prop_val.get_type() == Variant::NIL) {
  922. Variant::CallError ce;
  923. prop_val = Variant::construct(Variant::DICTIONARY, nullptr, 0, ce);
  924. get_edited_object()->set(get_edited_property(), prop_val);
  925. }
  926. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  927. update_property();
  928. }
  929. void EditorPropertyDictionary::_page_changed(double p_page) {
  930. if (updating) {
  931. return;
  932. }
  933. page_index = p_page;
  934. update_property();
  935. }
  936. void EditorPropertyDictionary::_bind_methods() {
  937. ClassDB::bind_method("_edit_pressed", &EditorPropertyDictionary::_edit_pressed);
  938. ClassDB::bind_method("_page_changed", &EditorPropertyDictionary::_page_changed);
  939. ClassDB::bind_method("_property_changed", &EditorPropertyDictionary::_property_changed, DEFVAL(String()), DEFVAL(false));
  940. ClassDB::bind_method("_change_type", &EditorPropertyDictionary::_change_type);
  941. ClassDB::bind_method("_change_type_menu", &EditorPropertyDictionary::_change_type_menu);
  942. ClassDB::bind_method("_add_key_value", &EditorPropertyDictionary::_add_key_value);
  943. ClassDB::bind_method("_object_id_selected", &EditorPropertyDictionary::_object_id_selected);
  944. }
  945. EditorPropertyDictionary::EditorPropertyDictionary() {
  946. object.instance();
  947. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  948. edit = memnew(Button);
  949. edit->set_flat(true);
  950. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  951. edit->set_clip_text(true);
  952. edit->connect("pressed", this, "_edit_pressed");
  953. edit->set_toggle_mode(true);
  954. add_child(edit);
  955. add_focusable(edit);
  956. vbox = nullptr;
  957. page_slider = nullptr;
  958. updating = false;
  959. change_type = memnew(PopupMenu);
  960. add_child(change_type);
  961. change_type->connect("id_pressed", this, "_change_type_menu");
  962. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  963. String type = Variant::get_type_name(Variant::Type(i));
  964. change_type->add_item(type, i);
  965. }
  966. change_type->add_separator();
  967. change_type->add_item(TTR("Remove Item"), Variant::VARIANT_MAX);
  968. changing_type_index = -1;
  969. }