editor_properties_array_dict.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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/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->set_use_folding(is_using_folding());
  305. prop->connect("property_changed", this, "_property_changed");
  306. prop->connect("object_id_selected", this, "_object_id_selected");
  307. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  308. hbox->add_child(prop);
  309. bool is_untyped_array = array.get_type() == Variant::ARRAY && subtype == Variant::NIL;
  310. if (is_untyped_array) {
  311. Button *edit = memnew(Button);
  312. edit->set_icon(get_icon("Edit", "EditorIcons"));
  313. hbox->add_child(edit);
  314. edit->connect("pressed", this, "_change_type", varray(edit, i + offset));
  315. } else {
  316. Button *remove = memnew(Button);
  317. remove->set_icon(get_icon("Remove", "EditorIcons"));
  318. remove->connect("pressed", this, "_remove_pressed", varray(i + offset));
  319. hbox->add_child(remove);
  320. }
  321. prop->update_property();
  322. }
  323. if (reorder_to_index % page_length > 0) {
  324. vbox->move_child(vbox->get_child(2), reorder_to_index % page_length + 2);
  325. }
  326. updating = false;
  327. } else {
  328. if (vbox) {
  329. set_bottom_editor(nullptr);
  330. memdelete(vbox);
  331. vbox = nullptr;
  332. }
  333. }
  334. }
  335. void EditorPropertyArray::_remove_pressed(int p_index) {
  336. Variant array = object->get_array();
  337. array.call("remove", p_index);
  338. emit_changed(get_edited_property(), array, "", false);
  339. update_property();
  340. }
  341. void EditorPropertyArray::_button_draw() {
  342. if (dropping) {
  343. Color color = get_color("accent_color", "Editor");
  344. edit->draw_rect(Rect2(Point2(), edit->get_size()), color, false);
  345. }
  346. }
  347. bool EditorPropertyArray::_is_drop_valid(const Dictionary &p_drag_data) const {
  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 != "") {
  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 initialised yet.
  383. if (!array.is_array()) {
  384. Variant::CallError ce;
  385. array = Variant::construct(array_type, nullptr, 0, ce);
  386. }
  387. // Loop the file array and add to existing array.
  388. for (int i = 0; i < files.size(); i++) {
  389. String file = files[i];
  390. RES res = ResourceLoader::load(file);
  391. if (res.is_valid()) {
  392. array.call("push_back", res);
  393. }
  394. }
  395. if (array.get_type() == Variant::ARRAY) {
  396. array = array.call("duplicate");
  397. }
  398. emit_changed(get_edited_property(), array, "", false);
  399. object->set_array(array);
  400. update_property();
  401. }
  402. }
  403. void EditorPropertyArray::_notification(int p_what) {
  404. switch (p_what) {
  405. case NOTIFICATION_ENTER_TREE:
  406. case NOTIFICATION_THEME_CHANGED: {
  407. change_type->clear();
  408. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  409. String type = Variant::get_type_name(Variant::Type(i));
  410. change_type->add_icon_item(get_icon(type, "EditorIcons"), type, i);
  411. }
  412. change_type->add_separator();
  413. change_type->add_icon_item(get_icon("Remove", "EditorIcons"), TTR("Remove Item"), Variant::VARIANT_MAX);
  414. } break;
  415. case NOTIFICATION_DRAG_BEGIN: {
  416. if (is_visible_in_tree()) {
  417. if (_is_drop_valid(get_viewport()->gui_get_drag_data())) {
  418. dropping = true;
  419. edit->update();
  420. }
  421. }
  422. } break;
  423. case NOTIFICATION_DRAG_END: {
  424. if (dropping) {
  425. dropping = false;
  426. edit->update();
  427. }
  428. } break;
  429. }
  430. }
  431. void EditorPropertyArray::_edit_pressed() {
  432. Variant array = get_edited_object()->get(get_edited_property());
  433. if (!array.is_array()) {
  434. Variant::CallError ce;
  435. array = Variant::construct(array_type, nullptr, 0, ce);
  436. get_edited_object()->set(get_edited_property(), array);
  437. }
  438. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  439. update_property();
  440. }
  441. void EditorPropertyArray::_page_changed(double p_page) {
  442. if (updating) {
  443. return;
  444. }
  445. page_index = p_page;
  446. update_property();
  447. }
  448. void EditorPropertyArray::_length_changed(double p_page) {
  449. if (updating) {
  450. return;
  451. }
  452. Variant array = object->get_array();
  453. int previous_size = array.call("size");
  454. array.call("resize", int(p_page));
  455. if (array.get_type() == Variant::ARRAY) {
  456. if (subtype != Variant::NIL) {
  457. int size = array.call("size");
  458. for (int i = previous_size; i < size; i++) {
  459. if (array.get(i).get_type() == Variant::NIL) {
  460. Variant::CallError ce;
  461. array.set(i, Variant::construct(subtype, nullptr, 0, ce));
  462. }
  463. }
  464. }
  465. array = array.call("duplicate"); // Duplicate, so undo/redo works better.
  466. } else {
  467. int size = array.call("size");
  468. // Pool*Array don't initialize their elements, have to do it manually.
  469. for (int i = previous_size; i < size; i++) {
  470. Variant::CallError ce;
  471. array.set(i, Variant::construct(array.get(i).get_type(), nullptr, 0, ce));
  472. }
  473. }
  474. emit_changed(get_edited_property(), array, "", false);
  475. object->set_array(array);
  476. update_property();
  477. }
  478. void EditorPropertyArray::setup(Variant::Type p_array_type, const String &p_hint_string) {
  479. array_type = p_array_type;
  480. // The format of p_hint_string is:
  481. // subType/subTypeHint:nextSubtype ... etc.
  482. if (array_type == Variant::ARRAY && !p_hint_string.empty()) {
  483. int hint_subtype_separator = p_hint_string.find(":");
  484. if (hint_subtype_separator >= 0) {
  485. String subtype_string = p_hint_string.substr(0, hint_subtype_separator);
  486. int slash_pos = subtype_string.find("/");
  487. if (slash_pos >= 0) {
  488. subtype_hint = PropertyHint(subtype_string.substr(slash_pos + 1, subtype_string.size() - slash_pos - 1).to_int());
  489. subtype_string = subtype_string.substr(0, slash_pos);
  490. }
  491. subtype_hint_string = p_hint_string.substr(hint_subtype_separator + 1, p_hint_string.size() - hint_subtype_separator - 1);
  492. subtype = Variant::Type(subtype_string.to_int());
  493. }
  494. }
  495. }
  496. void EditorPropertyArray::_reorder_button_gui_input(const Ref<InputEvent> &p_event) {
  497. if (reorder_from_index < 0) {
  498. return;
  499. }
  500. Ref<InputEventMouseMotion> mm = p_event;
  501. if (mm.is_valid()) {
  502. Variant array = object->get_array();
  503. int size = array.call("size");
  504. // Cumulate the mouse delta, many small changes (dragging slowly) should result in reordering at some point.
  505. reorder_mouse_y_delta += mm->get_relative().y;
  506. // Reordering is done by moving the dragged element by +1/-1 index at a time based on the cumulated mouse delta so if
  507. // already at the array bounds make sure to ignore the remaining out of bounds drag (by resetting the cumulated delta).
  508. if ((reorder_to_index == 0 && reorder_mouse_y_delta < 0.0f) || (reorder_to_index == size - 1 && reorder_mouse_y_delta > 0.0f)) {
  509. reorder_mouse_y_delta = 0.0f;
  510. return;
  511. }
  512. float required_y_distance = 20.0f * EDSCALE;
  513. if (ABS(reorder_mouse_y_delta) > required_y_distance) {
  514. int direction = reorder_mouse_y_delta > 0.0f ? 1 : -1;
  515. reorder_mouse_y_delta -= required_y_distance * direction;
  516. reorder_to_index += direction;
  517. if ((direction < 0 && reorder_to_index % page_length == page_length - 1) || (direction > 0 && reorder_to_index % page_length == 0)) {
  518. // Automatically move to the next/previous page.
  519. page_slider->set_value(page_index + direction);
  520. }
  521. vbox->move_child(reorder_selected_element_hbox, reorder_to_index % page_length + 2);
  522. // Ensure the moving element is visible.
  523. EditorNode::get_singleton()->get_inspector()->ensure_control_visible(reorder_selected_element_hbox);
  524. }
  525. }
  526. }
  527. void EditorPropertyArray::_reorder_button_down(int p_index) {
  528. reorder_from_index = p_index;
  529. reorder_to_index = p_index;
  530. reorder_selected_element_hbox = Object::cast_to<HBoxContainer>(vbox->get_child(p_index % page_length + 2));
  531. reorder_selected_button = Object::cast_to<Button>(reorder_selected_element_hbox->get_child(0));
  532. // Ideally it'd to be able to show the mouse but I had issues with
  533. // Control's `mouse_exit()`/`mouse_entered()` signals not getting called.
  534. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  535. }
  536. void EditorPropertyArray::_reorder_button_up() {
  537. if (reorder_from_index != reorder_to_index) {
  538. // Move the element.
  539. Variant array = object->get_array();
  540. Variant value_to_move = array.get(reorder_from_index);
  541. array.call("remove", reorder_from_index);
  542. array.call("insert", reorder_to_index, value_to_move);
  543. emit_changed(get_edited_property(), array, "", false);
  544. object->set_array(array);
  545. update_property();
  546. }
  547. reorder_from_index = -1;
  548. reorder_to_index = -1;
  549. reorder_mouse_y_delta = 0.0f;
  550. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  551. ERR_FAIL_NULL(reorder_selected_button);
  552. reorder_selected_button->warp_mouse(reorder_selected_button->get_size() / 2.0f);
  553. reorder_selected_element_hbox = nullptr;
  554. reorder_selected_button = nullptr;
  555. }
  556. void EditorPropertyArray::_bind_methods() {
  557. ClassDB::bind_method("_edit_pressed", &EditorPropertyArray::_edit_pressed);
  558. ClassDB::bind_method("_page_changed", &EditorPropertyArray::_page_changed);
  559. ClassDB::bind_method("_length_changed", &EditorPropertyArray::_length_changed);
  560. ClassDB::bind_method("_property_changed", &EditorPropertyArray::_property_changed, DEFVAL(String()), DEFVAL(false));
  561. ClassDB::bind_method("_change_type", &EditorPropertyArray::_change_type);
  562. ClassDB::bind_method("_change_type_menu", &EditorPropertyArray::_change_type_menu);
  563. ClassDB::bind_method("_object_id_selected", &EditorPropertyArray::_object_id_selected);
  564. ClassDB::bind_method("_remove_pressed", &EditorPropertyArray::_remove_pressed);
  565. ClassDB::bind_method("_reorder_button_gui_input", &EditorPropertyArray::_reorder_button_gui_input);
  566. ClassDB::bind_method("_reorder_button_down", &EditorPropertyArray::_reorder_button_down);
  567. ClassDB::bind_method("_reorder_button_up", &EditorPropertyArray::_reorder_button_up);
  568. ClassDB::bind_method("_button_draw", &EditorPropertyArray::_button_draw);
  569. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &EditorPropertyArray::can_drop_data_fw);
  570. ClassDB::bind_method(D_METHOD("drop_data_fw"), &EditorPropertyArray::drop_data_fw);
  571. }
  572. EditorPropertyArray::EditorPropertyArray() {
  573. object.instance();
  574. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  575. edit = memnew(Button);
  576. edit->set_flat(true);
  577. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  578. edit->set_clip_text(true);
  579. edit->connect("pressed", this, "_edit_pressed");
  580. edit->set_toggle_mode(true);
  581. edit->set_drag_forwarding(this);
  582. edit->connect("draw", this, "_button_draw");
  583. add_child(edit);
  584. add_focusable(edit);
  585. vbox = nullptr;
  586. page_slider = nullptr;
  587. size_slider = nullptr;
  588. updating = false;
  589. change_type = memnew(PopupMenu);
  590. add_child(change_type);
  591. change_type->connect("id_pressed", this, "_change_type_menu");
  592. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  593. String type = Variant::get_type_name(Variant::Type(i));
  594. change_type->add_item(type, i);
  595. }
  596. change_type->add_separator();
  597. change_type->add_item(TTR("Remove Item"), Variant::VARIANT_MAX);
  598. changing_type_index = -1;
  599. subtype = Variant::NIL;
  600. subtype_hint = PROPERTY_HINT_NONE;
  601. subtype_hint_string = "";
  602. dropping = false;
  603. }
  604. ///////////////////// DICTIONARY ///////////////////////////
  605. void EditorPropertyDictionary::_property_changed(const String &p_property, Variant p_value, const String &p_name, bool changing) {
  606. if (p_property == "new_item_key") {
  607. object->set_new_item_key(p_value);
  608. } else if (p_property == "new_item_value") {
  609. object->set_new_item_value(p_value);
  610. } else if (p_property.begins_with("indices")) {
  611. int index = p_property.get_slice("/", 1).to_int();
  612. Dictionary dict = object->get_dict();
  613. Variant key = dict.get_key_at_index(index);
  614. dict[key] = p_value;
  615. emit_changed(get_edited_property(), dict, "", true);
  616. dict = dict.duplicate(); // Duplicate, so undo/redo works better\.
  617. object->set_dict(dict);
  618. }
  619. }
  620. void EditorPropertyDictionary::_change_type(Object *p_button, int p_index) {
  621. Button *button = Object::cast_to<Button>(p_button);
  622. Rect2 rect = button->get_global_rect();
  623. change_type->set_as_minsize();
  624. change_type->set_global_position(rect.position + rect.size - Vector2(change_type->get_combined_minimum_size().x, 0));
  625. change_type->popup();
  626. changing_type_index = p_index;
  627. }
  628. void EditorPropertyDictionary::_add_key_value() {
  629. // Do not allow nil as valid key. I experienced errors with this
  630. if (object->get_new_item_key().get_type() == Variant::NIL) {
  631. return;
  632. }
  633. Dictionary dict = object->get_dict();
  634. dict[object->get_new_item_key()] = object->get_new_item_value();
  635. object->set_new_item_key(Variant());
  636. object->set_new_item_value(Variant());
  637. emit_changed(get_edited_property(), dict, "", false);
  638. dict = dict.duplicate(); // Duplicate, so undo/redo works better.
  639. object->set_dict(dict);
  640. update_property();
  641. }
  642. void EditorPropertyDictionary::_change_type_menu(int p_index) {
  643. if (changing_type_index < 0) {
  644. Variant value;
  645. Variant::CallError ce;
  646. value = Variant::construct(Variant::Type(p_index), nullptr, 0, ce);
  647. if (changing_type_index == -1) {
  648. object->set_new_item_key(value);
  649. } else {
  650. object->set_new_item_value(value);
  651. }
  652. update_property();
  653. return;
  654. }
  655. Dictionary dict = object->get_dict();
  656. if (p_index < Variant::VARIANT_MAX) {
  657. Variant value;
  658. Variant::CallError ce;
  659. value = Variant::construct(Variant::Type(p_index), nullptr, 0, ce);
  660. Variant key = dict.get_key_at_index(changing_type_index);
  661. dict[key] = value;
  662. } else {
  663. Variant key = dict.get_key_at_index(changing_type_index);
  664. dict.erase(key);
  665. }
  666. emit_changed(get_edited_property(), dict, "", false);
  667. dict = dict.duplicate(); // Duplicate, so undo/redo works better.
  668. object->set_dict(dict);
  669. update_property();
  670. }
  671. void EditorPropertyDictionary::update_property() {
  672. Variant updated_val = get_edited_object()->get(get_edited_property());
  673. if (updated_val.get_type() == Variant::NIL) {
  674. edit->set_text("Dictionary (Nil)"); // This provides symmetry with the array property.
  675. edit->set_pressed(false);
  676. if (vbox) {
  677. set_bottom_editor(nullptr);
  678. memdelete(vbox);
  679. button_add_item = nullptr;
  680. vbox = nullptr;
  681. }
  682. return;
  683. }
  684. Dictionary dict = updated_val;
  685. edit->set_text("Dictionary (size " + itos(dict.size()) + ")");
  686. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  687. if (edit->is_pressed() != unfolded) {
  688. edit->set_pressed(unfolded);
  689. }
  690. if (unfolded) {
  691. updating = true;
  692. if (!vbox) {
  693. vbox = memnew(VBoxContainer);
  694. add_child(vbox);
  695. set_bottom_editor(vbox);
  696. page_hbox = memnew(HBoxContainer);
  697. vbox->add_child(page_hbox);
  698. Label *label = memnew(Label(TTR("Page:")));
  699. label->set_h_size_flags(SIZE_EXPAND_FILL);
  700. page_hbox->add_child(label);
  701. page_slider = memnew(EditorSpinSlider);
  702. page_slider->set_step(1);
  703. page_hbox->add_child(page_slider);
  704. page_slider->set_h_size_flags(SIZE_EXPAND_FILL);
  705. page_slider->connect("value_changed", this, "_page_changed");
  706. } else {
  707. // Queue children for deletion, deleting immediately might cause errors.
  708. for (int i = 1; i < vbox->get_child_count(); i++) {
  709. vbox->get_child(i)->queue_delete();
  710. }
  711. }
  712. int size = dict.size();
  713. int pages = MAX(0, size - 1) / page_length + 1;
  714. page_slider->set_max(pages);
  715. page_index = MIN(page_index, pages - 1);
  716. page_slider->set_value(page_index);
  717. page_hbox->set_visible(pages > 1);
  718. int offset = page_index * page_length;
  719. int amount = MIN(size - offset, page_length);
  720. dict = dict.duplicate();
  721. object->set_dict(dict);
  722. VBoxContainer *add_vbox = nullptr;
  723. for (int i = 0; i < amount + 2; i++) {
  724. String prop_name;
  725. Variant key;
  726. Variant value;
  727. if (i < amount) {
  728. prop_name = "indices/" + itos(i + offset);
  729. key = dict.get_key_at_index(i + offset);
  730. value = dict.get_value_at_index(i + offset);
  731. } else if (i == amount) {
  732. prop_name = "new_item_key";
  733. value = object->get_new_item_key();
  734. } else if (i == amount + 1) {
  735. prop_name = "new_item_value";
  736. value = object->get_new_item_value();
  737. }
  738. EditorProperty *prop = nullptr;
  739. switch (value.get_type()) {
  740. case Variant::NIL: {
  741. prop = memnew(EditorPropertyNil);
  742. } break;
  743. // Atomic types.
  744. case Variant::BOOL: {
  745. prop = memnew(EditorPropertyCheck);
  746. } break;
  747. case Variant::INT: {
  748. EditorPropertyInteger *editor = memnew(EditorPropertyInteger);
  749. editor->setup(-100000, 100000, 1, true, true);
  750. prop = editor;
  751. } break;
  752. case Variant::REAL: {
  753. EditorPropertyFloat *editor = memnew(EditorPropertyFloat);
  754. editor->setup(-100000, 100000, 0.001, true, false, true, true);
  755. prop = editor;
  756. } break;
  757. case Variant::STRING: {
  758. prop = memnew(EditorPropertyText);
  759. } break;
  760. // Math types.
  761. case Variant::VECTOR2: {
  762. EditorPropertyVector2 *editor = memnew(EditorPropertyVector2);
  763. editor->setup(-100000, 100000, 0.001, true);
  764. prop = editor;
  765. } break;
  766. case Variant::RECT2: {
  767. EditorPropertyRect2 *editor = memnew(EditorPropertyRect2);
  768. editor->setup(-100000, 100000, 0.001, true);
  769. prop = editor;
  770. } break;
  771. case Variant::VECTOR3: {
  772. EditorPropertyVector3 *editor = memnew(EditorPropertyVector3);
  773. editor->setup(-100000, 100000, 0.001, true);
  774. prop = editor;
  775. } break;
  776. case Variant::TRANSFORM2D: {
  777. EditorPropertyTransform2D *editor = memnew(EditorPropertyTransform2D);
  778. editor->setup(-100000, 100000, 0.001, true);
  779. prop = editor;
  780. } break;
  781. case Variant::PLANE: {
  782. EditorPropertyPlane *editor = memnew(EditorPropertyPlane);
  783. editor->setup(-100000, 100000, 0.001, true);
  784. prop = editor;
  785. } break;
  786. case Variant::QUAT: {
  787. EditorPropertyQuat *editor = memnew(EditorPropertyQuat);
  788. editor->setup(-100000, 100000, 0.001, true);
  789. prop = editor;
  790. } break;
  791. case Variant::AABB: {
  792. EditorPropertyAABB *editor = memnew(EditorPropertyAABB);
  793. editor->setup(-100000, 100000, 0.001, true);
  794. prop = editor;
  795. } break;
  796. case Variant::BASIS: {
  797. EditorPropertyBasis *editor = memnew(EditorPropertyBasis);
  798. editor->setup(-100000, 100000, 0.001, true);
  799. prop = editor;
  800. } break;
  801. case Variant::TRANSFORM: {
  802. EditorPropertyTransform *editor = memnew(EditorPropertyTransform);
  803. editor->setup(-100000, 100000, 0.001, true);
  804. prop = editor;
  805. } break;
  806. // Miscellaneous types.
  807. case Variant::COLOR: {
  808. prop = memnew(EditorPropertyColor);
  809. } break;
  810. case Variant::NODE_PATH: {
  811. prop = memnew(EditorPropertyNodePath);
  812. } break;
  813. case Variant::_RID: {
  814. prop = memnew(EditorPropertyRID);
  815. } break;
  816. case Variant::OBJECT: {
  817. if (Object::cast_to<EncodedObjectAsID>(value)) {
  818. EditorPropertyObjectID *editor = memnew(EditorPropertyObjectID);
  819. editor->setup("Object");
  820. prop = editor;
  821. } else {
  822. EditorPropertyResource *editor = memnew(EditorPropertyResource);
  823. editor->setup(object.ptr(), prop_name, "Resource");
  824. editor->set_use_folding(is_using_folding());
  825. prop = editor;
  826. }
  827. } break;
  828. case Variant::DICTIONARY: {
  829. prop = memnew(EditorPropertyDictionary);
  830. } break;
  831. case Variant::ARRAY: {
  832. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  833. editor->setup(Variant::ARRAY);
  834. prop = editor;
  835. } break;
  836. // Arrays.
  837. case Variant::POOL_BYTE_ARRAY: {
  838. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  839. editor->setup(Variant::POOL_BYTE_ARRAY);
  840. prop = editor;
  841. } break;
  842. case Variant::POOL_INT_ARRAY: {
  843. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  844. editor->setup(Variant::POOL_INT_ARRAY);
  845. prop = editor;
  846. } break;
  847. case Variant::POOL_REAL_ARRAY: {
  848. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  849. editor->setup(Variant::POOL_REAL_ARRAY);
  850. prop = editor;
  851. } break;
  852. case Variant::POOL_STRING_ARRAY: {
  853. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  854. editor->setup(Variant::POOL_STRING_ARRAY);
  855. prop = editor;
  856. } break;
  857. case Variant::POOL_VECTOR2_ARRAY: {
  858. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  859. editor->setup(Variant::POOL_VECTOR2_ARRAY);
  860. prop = editor;
  861. } break;
  862. case Variant::POOL_VECTOR3_ARRAY: {
  863. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  864. editor->setup(Variant::POOL_VECTOR3_ARRAY);
  865. prop = editor;
  866. } break;
  867. case Variant::POOL_COLOR_ARRAY: {
  868. EditorPropertyArray *editor = memnew(EditorPropertyArray);
  869. editor->setup(Variant::POOL_COLOR_ARRAY);
  870. prop = editor;
  871. } break;
  872. default: {
  873. }
  874. }
  875. if (i == amount) {
  876. PanelContainer *pc = memnew(PanelContainer);
  877. vbox->add_child(pc);
  878. Ref<StyleBoxFlat> flat;
  879. flat.instance();
  880. for (int j = 0; j < 4; j++) {
  881. flat->set_default_margin(Margin(j), 2 * EDSCALE);
  882. }
  883. flat->set_bg_color(get_color("prop_subsection", "Editor"));
  884. pc->add_style_override("panel", flat);
  885. add_vbox = memnew(VBoxContainer);
  886. pc->add_child(add_vbox);
  887. }
  888. prop->set_object_and_property(object.ptr(), prop_name);
  889. int change_index = 0;
  890. if (i < amount) {
  891. String cs = key.get_construct_string();
  892. prop->set_label(key.get_construct_string());
  893. prop->set_tooltip(cs);
  894. change_index = i + offset;
  895. } else if (i == amount) {
  896. prop->set_label(TTR("New Key:"));
  897. change_index = -1;
  898. } else if (i == amount + 1) {
  899. prop->set_label(TTR("New Value:"));
  900. change_index = -2;
  901. }
  902. prop->set_selectable(false);
  903. prop->connect("property_changed", this, "_property_changed");
  904. prop->connect("object_id_selected", this, "_object_id_selected");
  905. HBoxContainer *hbox = memnew(HBoxContainer);
  906. if (add_vbox) {
  907. add_vbox->add_child(hbox);
  908. } else {
  909. vbox->add_child(hbox);
  910. }
  911. hbox->add_child(prop);
  912. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  913. Button *edit = memnew(Button);
  914. edit->set_icon(get_icon("Edit", "EditorIcons"));
  915. hbox->add_child(edit);
  916. edit->connect("pressed", this, "_change_type", varray(edit, change_index));
  917. prop->update_property();
  918. if (i == amount + 1) {
  919. button_add_item = memnew(Button);
  920. button_add_item->set_text(TTR("Add Key/Value Pair"));
  921. button_add_item->set_icon(get_icon("Add", "EditorIcons"));
  922. button_add_item->connect("pressed", this, "_add_key_value");
  923. add_vbox->add_child(button_add_item);
  924. }
  925. }
  926. updating = false;
  927. } else {
  928. if (vbox) {
  929. set_bottom_editor(nullptr);
  930. memdelete(vbox);
  931. button_add_item = nullptr;
  932. vbox = nullptr;
  933. }
  934. }
  935. }
  936. void EditorPropertyDictionary::_object_id_selected(const String &p_property, ObjectID p_id) {
  937. emit_signal("object_id_selected", p_property, p_id);
  938. }
  939. void EditorPropertyDictionary::_notification(int p_what) {
  940. switch (p_what) {
  941. case NOTIFICATION_ENTER_TREE:
  942. case NOTIFICATION_THEME_CHANGED: {
  943. change_type->clear();
  944. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  945. String type = Variant::get_type_name(Variant::Type(i));
  946. change_type->add_icon_item(get_icon(type, "EditorIcons"), type, i);
  947. }
  948. change_type->add_separator();
  949. change_type->add_icon_item(get_icon("Remove", "EditorIcons"), TTR("Remove Item"), Variant::VARIANT_MAX);
  950. if (Object::cast_to<Button>(button_add_item)) {
  951. button_add_item->set_icon(get_icon("Add", "EditorIcons"));
  952. }
  953. } break;
  954. }
  955. }
  956. void EditorPropertyDictionary::_edit_pressed() {
  957. Variant prop_val = get_edited_object()->get(get_edited_property());
  958. if (prop_val.get_type() == Variant::NIL) {
  959. Variant::CallError ce;
  960. prop_val = Variant::construct(Variant::DICTIONARY, nullptr, 0, ce);
  961. get_edited_object()->set(get_edited_property(), prop_val);
  962. }
  963. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  964. update_property();
  965. }
  966. void EditorPropertyDictionary::_page_changed(double p_page) {
  967. if (updating) {
  968. return;
  969. }
  970. page_index = p_page;
  971. update_property();
  972. }
  973. void EditorPropertyDictionary::_bind_methods() {
  974. ClassDB::bind_method("_edit_pressed", &EditorPropertyDictionary::_edit_pressed);
  975. ClassDB::bind_method("_page_changed", &EditorPropertyDictionary::_page_changed);
  976. ClassDB::bind_method("_property_changed", &EditorPropertyDictionary::_property_changed, DEFVAL(String()), DEFVAL(false));
  977. ClassDB::bind_method("_change_type", &EditorPropertyDictionary::_change_type);
  978. ClassDB::bind_method("_change_type_menu", &EditorPropertyDictionary::_change_type_menu);
  979. ClassDB::bind_method("_add_key_value", &EditorPropertyDictionary::_add_key_value);
  980. ClassDB::bind_method("_object_id_selected", &EditorPropertyDictionary::_object_id_selected);
  981. }
  982. EditorPropertyDictionary::EditorPropertyDictionary() {
  983. object.instance();
  984. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  985. edit = memnew(Button);
  986. edit->set_flat(true);
  987. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  988. edit->set_clip_text(true);
  989. edit->connect("pressed", this, "_edit_pressed");
  990. edit->set_toggle_mode(true);
  991. add_child(edit);
  992. add_focusable(edit);
  993. vbox = nullptr;
  994. page_slider = nullptr;
  995. button_add_item = nullptr;
  996. updating = false;
  997. change_type = memnew(PopupMenu);
  998. add_child(change_type);
  999. change_type->connect("id_pressed", this, "_change_type_menu");
  1000. changing_type_index = -1;
  1001. }