control_editor_plugin.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. /*************************************************************************/
  2. /* control_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "control_editor_plugin.h"
  31. #include "editor/editor_node.h"
  32. #include "editor/plugins/canvas_item_editor_plugin.h"
  33. void ControlPositioningWarning::_update_warning() {
  34. if (!control_node) {
  35. title_icon->set_texture(nullptr);
  36. title_label->set_text("");
  37. hint_label->set_text("");
  38. return;
  39. }
  40. Node *parent_node = control_node->get_parent_control();
  41. if (!parent_node) {
  42. title_icon->set_texture(get_theme_icon(SNAME("SubViewport"), SNAME("EditorIcons")));
  43. title_label->set_text(TTR("This node doesn't have a control parent."));
  44. hint_label->set_text(TTR("Use the appropriate layout properties depending on where you are going to put it."));
  45. } else if (Object::cast_to<Container>(parent_node)) {
  46. title_icon->set_texture(get_theme_icon(SNAME("Container"), SNAME("EditorIcons")));
  47. title_label->set_text(TTR("This node is a child of a container."));
  48. hint_label->set_text(TTR("Use container properties for positioning."));
  49. } else {
  50. title_icon->set_texture(get_theme_icon(SNAME("ControlLayout"), SNAME("EditorIcons")));
  51. title_label->set_text(TTR("This node is a child of a regular control."));
  52. hint_label->set_text(TTR("Use anchors and the rectangle for positioning."));
  53. }
  54. bg_panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("bg_group_note"), SNAME("EditorProperty")));
  55. }
  56. void ControlPositioningWarning::_update_toggler() {
  57. Ref<Texture2D> arrow;
  58. if (hint_label->is_visible()) {
  59. arrow = get_theme_icon(SNAME("arrow"), SNAME("Tree"));
  60. set_tooltip(TTR("Collapse positioning hint."));
  61. } else {
  62. if (is_layout_rtl()) {
  63. arrow = get_theme_icon(SNAME("arrow_collapsed"), SNAME("Tree"));
  64. } else {
  65. arrow = get_theme_icon(SNAME("arrow_collapsed_mirrored"), SNAME("Tree"));
  66. }
  67. set_tooltip(TTR("Expand positioning hint."));
  68. }
  69. hint_icon->set_texture(arrow);
  70. }
  71. void ControlPositioningWarning::set_control(Control *p_node) {
  72. control_node = p_node;
  73. _update_warning();
  74. }
  75. void ControlPositioningWarning::gui_input(const Ref<InputEvent> &p_event) {
  76. Ref<InputEventMouseButton> mb = p_event;
  77. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  78. bool state = !hint_label->is_visible();
  79. hint_filler_left->set_visible(state);
  80. hint_label->set_visible(state);
  81. hint_filler_right->set_visible(state);
  82. _update_toggler();
  83. }
  84. }
  85. void ControlPositioningWarning::_notification(int p_notification) {
  86. switch (p_notification) {
  87. case NOTIFICATION_ENTER_TREE:
  88. case NOTIFICATION_THEME_CHANGED:
  89. _update_warning();
  90. _update_toggler();
  91. break;
  92. }
  93. }
  94. ControlPositioningWarning::ControlPositioningWarning() {
  95. set_mouse_filter(MOUSE_FILTER_STOP);
  96. bg_panel = memnew(PanelContainer);
  97. bg_panel->set_mouse_filter(MOUSE_FILTER_IGNORE);
  98. add_child(bg_panel);
  99. grid = memnew(GridContainer);
  100. grid->set_columns(3);
  101. bg_panel->add_child(grid);
  102. title_icon = memnew(TextureRect);
  103. title_icon->set_stretch_mode(TextureRect::StretchMode::STRETCH_KEEP_CENTERED);
  104. grid->add_child(title_icon);
  105. title_label = memnew(Label);
  106. title_label->set_autowrap_mode(TextServer::AutowrapMode::AUTOWRAP_WORD);
  107. title_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  108. title_label->set_vertical_alignment(VerticalAlignment::VERTICAL_ALIGNMENT_CENTER);
  109. grid->add_child(title_label);
  110. hint_icon = memnew(TextureRect);
  111. hint_icon->set_stretch_mode(TextureRect::StretchMode::STRETCH_KEEP_CENTERED);
  112. grid->add_child(hint_icon);
  113. // Filler.
  114. hint_filler_left = memnew(Control);
  115. hint_filler_left->hide();
  116. grid->add_child(hint_filler_left);
  117. hint_label = memnew(Label);
  118. hint_label->set_autowrap_mode(TextServer::AutowrapMode::AUTOWRAP_WORD);
  119. hint_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  120. hint_label->set_vertical_alignment(VerticalAlignment::VERTICAL_ALIGNMENT_CENTER);
  121. hint_label->hide();
  122. grid->add_child(hint_label);
  123. // Filler.
  124. hint_filler_right = memnew(Control);
  125. hint_filler_right->hide();
  126. grid->add_child(hint_filler_right);
  127. }
  128. void EditorPropertyAnchorsPreset::_set_read_only(bool p_read_only) {
  129. options->set_disabled(p_read_only);
  130. };
  131. void EditorPropertyAnchorsPreset::_option_selected(int p_which) {
  132. int64_t val = options->get_item_metadata(p_which);
  133. emit_changed(get_edited_property(), val);
  134. }
  135. void EditorPropertyAnchorsPreset::update_property() {
  136. int64_t which = get_edited_object()->get(get_edited_property());
  137. for (int i = 0; i < options->get_item_count(); i++) {
  138. Variant val = options->get_item_metadata(i);
  139. if (val != Variant() && which == (int64_t)val) {
  140. options->select(i);
  141. return;
  142. }
  143. }
  144. }
  145. void EditorPropertyAnchorsPreset::setup(const Vector<String> &p_options) {
  146. options->clear();
  147. Vector<String> split_after;
  148. split_after.append("Custom");
  149. split_after.append("PresetFullRect");
  150. split_after.append("PresetBottomLeft");
  151. split_after.append("PresetCenter");
  152. for (int i = 0, j = 0; i < p_options.size(); i++, j++) {
  153. Vector<String> text_split = p_options[i].split(":");
  154. int64_t current_val = text_split[1].to_int();
  155. String option_name = text_split[0];
  156. if (option_name.begins_with("Preset")) {
  157. String preset_name = option_name.trim_prefix("Preset");
  158. String humanized_name = preset_name.capitalize();
  159. String icon_name = "ControlAlign" + preset_name;
  160. options->add_icon_item(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(icon_name, "EditorIcons"), humanized_name);
  161. } else {
  162. options->add_item(option_name);
  163. }
  164. options->set_item_metadata(j, current_val);
  165. if (split_after.has(option_name)) {
  166. options->add_separator();
  167. j++;
  168. }
  169. }
  170. }
  171. EditorPropertyAnchorsPreset::EditorPropertyAnchorsPreset() {
  172. options = memnew(OptionButton);
  173. options->set_clip_text(true);
  174. options->set_flat(true);
  175. add_child(options);
  176. add_focusable(options);
  177. options->connect("item_selected", callable_mp(this, &EditorPropertyAnchorsPreset::_option_selected));
  178. }
  179. void EditorPropertySizeFlags::_set_read_only(bool p_read_only) {
  180. for (CheckBox *check : flag_checks) {
  181. check->set_disabled(p_read_only);
  182. }
  183. flag_presets->set_disabled(p_read_only);
  184. };
  185. void EditorPropertySizeFlags::_preset_selected(int p_which) {
  186. int preset = flag_presets->get_item_id(p_which);
  187. if (preset == SIZE_FLAGS_PRESET_CUSTOM) {
  188. flag_options->set_visible(true);
  189. return;
  190. }
  191. flag_options->set_visible(false);
  192. uint32_t value = 0;
  193. switch (preset) {
  194. case SIZE_FLAGS_PRESET_FILL:
  195. value = Control::SIZE_FILL;
  196. break;
  197. case SIZE_FLAGS_PRESET_SHRINK_BEGIN:
  198. value = Control::SIZE_SHRINK_BEGIN;
  199. break;
  200. case SIZE_FLAGS_PRESET_SHRINK_CENTER:
  201. value = Control::SIZE_SHRINK_CENTER;
  202. break;
  203. case SIZE_FLAGS_PRESET_SHRINK_END:
  204. value = Control::SIZE_SHRINK_END;
  205. break;
  206. }
  207. bool is_expand = flag_expand->is_visible() && flag_expand->is_pressed();
  208. if (is_expand) {
  209. value |= Control::SIZE_EXPAND;
  210. }
  211. emit_changed(get_edited_property(), value);
  212. }
  213. void EditorPropertySizeFlags::_expand_toggled() {
  214. uint32_t value = get_edited_object()->get(get_edited_property());
  215. if (flag_expand->is_visible() && flag_expand->is_pressed()) {
  216. value |= Control::SIZE_EXPAND;
  217. } else {
  218. value ^= Control::SIZE_EXPAND;
  219. }
  220. // Keep the custom preset selected as we toggle individual flags.
  221. keep_selected_preset = true;
  222. emit_changed(get_edited_property(), value);
  223. }
  224. void EditorPropertySizeFlags::_flag_toggled() {
  225. uint32_t value = 0;
  226. for (int i = 0; i < flag_checks.size(); i++) {
  227. if (flag_checks[i]->is_pressed()) {
  228. int flag_value = flag_checks[i]->get_meta("_value");
  229. value |= flag_value;
  230. }
  231. }
  232. bool is_expand = flag_expand->is_visible() && flag_expand->is_pressed();
  233. if (is_expand) {
  234. value |= Control::SIZE_EXPAND;
  235. }
  236. // Keep the custom preset selected as we toggle individual flags.
  237. keep_selected_preset = true;
  238. emit_changed(get_edited_property(), value);
  239. }
  240. void EditorPropertySizeFlags::update_property() {
  241. uint32_t value = get_edited_object()->get(get_edited_property());
  242. for (int i = 0; i < flag_checks.size(); i++) {
  243. int flag_value = flag_checks[i]->get_meta("_value");
  244. if (value & flag_value) {
  245. flag_checks[i]->set_pressed(true);
  246. } else {
  247. flag_checks[i]->set_pressed(false);
  248. }
  249. }
  250. bool is_expand = value & Control::SIZE_EXPAND;
  251. flag_expand->set_pressed(is_expand);
  252. if (keep_selected_preset) {
  253. keep_selected_preset = false;
  254. return;
  255. }
  256. FlagPreset preset = SIZE_FLAGS_PRESET_CUSTOM;
  257. if (value == Control::SIZE_FILL || value == (Control::SIZE_FILL | Control::SIZE_EXPAND)) {
  258. preset = SIZE_FLAGS_PRESET_FILL;
  259. } else if (value == Control::SIZE_SHRINK_BEGIN || value == (Control::SIZE_SHRINK_BEGIN | Control::SIZE_EXPAND)) {
  260. preset = SIZE_FLAGS_PRESET_SHRINK_BEGIN;
  261. } else if (value == Control::SIZE_SHRINK_CENTER || value == (Control::SIZE_SHRINK_CENTER | Control::SIZE_EXPAND)) {
  262. preset = SIZE_FLAGS_PRESET_SHRINK_CENTER;
  263. } else if (value == Control::SIZE_SHRINK_END || value == (Control::SIZE_SHRINK_END | Control::SIZE_EXPAND)) {
  264. preset = SIZE_FLAGS_PRESET_SHRINK_END;
  265. }
  266. int preset_idx = flag_presets->get_item_index(preset);
  267. if (preset_idx >= 0) {
  268. flag_presets->select(preset_idx);
  269. }
  270. flag_options->set_visible(preset == SIZE_FLAGS_PRESET_CUSTOM);
  271. }
  272. void EditorPropertySizeFlags::setup(const Vector<String> &p_options, bool p_vertical) {
  273. vertical = p_vertical;
  274. if (p_options.size() == 0) {
  275. flag_presets->clear();
  276. flag_presets->add_item(TTR("Container Default"));
  277. flag_presets->set_disabled(true);
  278. flag_expand->set_visible(false);
  279. return;
  280. }
  281. HashMap<int, String> flags;
  282. for (int i = 0, j = 0; i < p_options.size(); i++, j++) {
  283. Vector<String> text_split = p_options[i].split(":");
  284. int64_t current_val = text_split[1].to_int();
  285. flags[current_val] = text_split[0];
  286. if (current_val == SIZE_EXPAND) {
  287. continue;
  288. }
  289. CheckBox *cb = memnew(CheckBox);
  290. cb->set_text(text_split[0]);
  291. cb->set_clip_text(true);
  292. cb->set_meta("_value", current_val);
  293. cb->connect("pressed", callable_mp(this, &EditorPropertySizeFlags::_flag_toggled));
  294. add_focusable(cb);
  295. flag_options->add_child(cb);
  296. flag_checks.append(cb);
  297. }
  298. Control *gui_base = EditorNode::get_singleton()->get_gui_base();
  299. String wide_preset_icon = SNAME("ControlAlignHCenterWide");
  300. String begin_preset_icon = SNAME("ControlAlignCenterLeft");
  301. String end_preset_icon = SNAME("ControlAlignCenterRight");
  302. if (vertical) {
  303. wide_preset_icon = SNAME("ControlAlignVCenterWide");
  304. begin_preset_icon = SNAME("ControlAlignCenterTop");
  305. end_preset_icon = SNAME("ControlAlignCenterBottom");
  306. }
  307. flag_presets->clear();
  308. if (flags.has(SIZE_FILL)) {
  309. flag_presets->add_icon_item(gui_base->get_theme_icon(wide_preset_icon, SNAME("EditorIcons")), TTR("Fill"), SIZE_FLAGS_PRESET_FILL);
  310. }
  311. // Shrink Begin is the same as no flags at all, as such it cannot be disabled.
  312. flag_presets->add_icon_item(gui_base->get_theme_icon(begin_preset_icon, SNAME("EditorIcons")), TTR("Shrink Begin"), SIZE_FLAGS_PRESET_SHRINK_BEGIN);
  313. if (flags.has(SIZE_SHRINK_CENTER)) {
  314. flag_presets->add_icon_item(gui_base->get_theme_icon(SNAME("ControlAlignCenter"), SNAME("EditorIcons")), TTR("Shrink Center"), SIZE_FLAGS_PRESET_SHRINK_CENTER);
  315. }
  316. if (flags.has(SIZE_SHRINK_END)) {
  317. flag_presets->add_icon_item(gui_base->get_theme_icon(end_preset_icon, SNAME("EditorIcons")), TTR("Shrink End"), SIZE_FLAGS_PRESET_SHRINK_END);
  318. }
  319. flag_presets->add_separator();
  320. flag_presets->add_item(TTR("Custom"), SIZE_FLAGS_PRESET_CUSTOM);
  321. flag_expand->set_visible(flags.has(SIZE_EXPAND));
  322. }
  323. EditorPropertySizeFlags::EditorPropertySizeFlags() {
  324. VBoxContainer *vb = memnew(VBoxContainer);
  325. add_child(vb);
  326. flag_presets = memnew(OptionButton);
  327. flag_presets->set_clip_text(true);
  328. flag_presets->set_flat(true);
  329. vb->add_child(flag_presets);
  330. add_focusable(flag_presets);
  331. set_label_reference(flag_presets);
  332. flag_presets->connect("item_selected", callable_mp(this, &EditorPropertySizeFlags::_preset_selected));
  333. flag_options = memnew(VBoxContainer);
  334. flag_options->hide();
  335. vb->add_child(flag_options);
  336. flag_expand = memnew(CheckBox);
  337. flag_expand->set_text(TTR("Expand"));
  338. vb->add_child(flag_expand);
  339. add_focusable(flag_expand);
  340. flag_expand->connect("pressed", callable_mp(this, &EditorPropertySizeFlags::_expand_toggled));
  341. }
  342. bool EditorInspectorPluginControl::can_handle(Object *p_object) {
  343. return Object::cast_to<Control>(p_object) != nullptr;
  344. }
  345. void EditorInspectorPluginControl::parse_group(Object *p_object, const String &p_group) {
  346. Control *control = Object::cast_to<Control>(p_object);
  347. if (!control || p_group != "Layout") {
  348. return;
  349. }
  350. ControlPositioningWarning *pos_warning = memnew(ControlPositioningWarning);
  351. pos_warning->set_control(control);
  352. add_custom_control(pos_warning);
  353. }
  354. bool EditorInspectorPluginControl::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const uint32_t p_usage, const bool p_wide) {
  355. Control *control = Object::cast_to<Control>(p_object);
  356. if (!control) {
  357. return false;
  358. }
  359. if (p_path == "anchors_preset") {
  360. EditorPropertyAnchorsPreset *prop_editor = memnew(EditorPropertyAnchorsPreset);
  361. Vector<String> options = p_hint_text.split(",");
  362. prop_editor->setup(options);
  363. add_property_editor(p_path, prop_editor);
  364. return true;
  365. }
  366. if (p_path == "size_flags_horizontal" || p_path == "size_flags_vertical") {
  367. EditorPropertySizeFlags *prop_editor = memnew(EditorPropertySizeFlags);
  368. Vector<String> options;
  369. if (!p_hint_text.is_empty()) {
  370. options = p_hint_text.split(",");
  371. }
  372. prop_editor->setup(options, p_path == "size_flags_vertical");
  373. add_property_editor(p_path, prop_editor);
  374. return true;
  375. }
  376. return false;
  377. }
  378. void ControlEditorToolbar::_set_anchors_and_offsets_preset(Control::LayoutPreset p_preset) {
  379. List<Node *> selection = editor_selection->get_selected_node_list();
  380. undo_redo->create_action(TTR("Change Anchors and Offsets"));
  381. for (Node *E : selection) {
  382. Control *control = Object::cast_to<Control>(E);
  383. if (control) {
  384. undo_redo->add_do_method(control, "set_anchors_preset", p_preset);
  385. switch (p_preset) {
  386. case PRESET_TOP_LEFT:
  387. case PRESET_TOP_RIGHT:
  388. case PRESET_BOTTOM_LEFT:
  389. case PRESET_BOTTOM_RIGHT:
  390. case PRESET_CENTER_LEFT:
  391. case PRESET_CENTER_TOP:
  392. case PRESET_CENTER_RIGHT:
  393. case PRESET_CENTER_BOTTOM:
  394. case PRESET_CENTER:
  395. undo_redo->add_do_method(control, "set_offsets_preset", p_preset, Control::PRESET_MODE_KEEP_SIZE);
  396. break;
  397. case PRESET_LEFT_WIDE:
  398. case PRESET_TOP_WIDE:
  399. case PRESET_RIGHT_WIDE:
  400. case PRESET_BOTTOM_WIDE:
  401. case PRESET_VCENTER_WIDE:
  402. case PRESET_HCENTER_WIDE:
  403. case PRESET_FULL_RECT:
  404. undo_redo->add_do_method(control, "set_offsets_preset", p_preset, Control::PRESET_MODE_MINSIZE);
  405. break;
  406. }
  407. undo_redo->add_undo_method(control, "_edit_set_state", control->_edit_get_state());
  408. }
  409. }
  410. undo_redo->commit_action();
  411. anchors_mode = false;
  412. anchor_mode_button->set_pressed(anchors_mode);
  413. }
  414. void ControlEditorToolbar::_set_anchors_and_offsets_to_keep_ratio() {
  415. List<Node *> selection = editor_selection->get_selected_node_list();
  416. undo_redo->create_action(TTR("Change Anchors and Offsets"));
  417. for (Node *E : selection) {
  418. Control *control = Object::cast_to<Control>(E);
  419. if (control) {
  420. Point2 top_left_anchor = _position_to_anchor(control, Point2());
  421. Point2 bottom_right_anchor = _position_to_anchor(control, control->get_size());
  422. undo_redo->add_do_method(control, "set_anchor", SIDE_LEFT, top_left_anchor.x, false, true);
  423. undo_redo->add_do_method(control, "set_anchor", SIDE_RIGHT, bottom_right_anchor.x, false, true);
  424. undo_redo->add_do_method(control, "set_anchor", SIDE_TOP, top_left_anchor.y, false, true);
  425. undo_redo->add_do_method(control, "set_anchor", SIDE_BOTTOM, bottom_right_anchor.y, false, true);
  426. undo_redo->add_do_method(control, "set_meta", "_edit_use_anchors_", true);
  427. const bool use_anchors = control->get_meta("_edit_use_anchors_", false);
  428. undo_redo->add_undo_method(control, "_edit_set_state", control->_edit_get_state());
  429. if (use_anchors) {
  430. undo_redo->add_undo_method(control, "set_meta", "_edit_use_anchors_", true);
  431. } else {
  432. undo_redo->add_undo_method(control, "remove_meta", "_edit_use_anchors_");
  433. }
  434. anchors_mode = true;
  435. anchor_mode_button->set_pressed(anchors_mode);
  436. }
  437. }
  438. undo_redo->commit_action();
  439. }
  440. void ControlEditorToolbar::_set_anchors_preset(Control::LayoutPreset p_preset) {
  441. List<Node *> selection = editor_selection->get_selected_node_list();
  442. undo_redo->create_action(TTR("Change Anchors"));
  443. for (Node *E : selection) {
  444. Control *control = Object::cast_to<Control>(E);
  445. if (control) {
  446. undo_redo->add_do_method(control, "set_anchors_preset", p_preset);
  447. undo_redo->add_undo_method(control, "_edit_set_state", control->_edit_get_state());
  448. }
  449. }
  450. undo_redo->commit_action();
  451. }
  452. void ControlEditorToolbar::_set_container_h_preset(Control::SizeFlags p_preset) {
  453. List<Node *> selection = editor_selection->get_selected_node_list();
  454. undo_redo->create_action(TTR("Change Horizontal Size Flags"));
  455. for (Node *E : selection) {
  456. Control *control = Object::cast_to<Control>(E);
  457. if (control) {
  458. undo_redo->add_do_method(control, "set_h_size_flags", p_preset);
  459. undo_redo->add_undo_method(control, "_edit_set_state", control->_edit_get_state());
  460. }
  461. }
  462. undo_redo->commit_action();
  463. }
  464. void ControlEditorToolbar::_set_container_v_preset(Control::SizeFlags p_preset) {
  465. List<Node *> selection = editor_selection->get_selected_node_list();
  466. undo_redo->create_action(TTR("Change Horizontal Size Flags"));
  467. for (Node *E : selection) {
  468. Control *control = Object::cast_to<Control>(E);
  469. if (control) {
  470. undo_redo->add_do_method(control, "set_v_size_flags", p_preset);
  471. undo_redo->add_undo_method(control, "_edit_set_state", control->_edit_get_state());
  472. }
  473. }
  474. undo_redo->commit_action();
  475. }
  476. Vector2 ControlEditorToolbar::_anchor_to_position(const Control *p_control, Vector2 anchor) {
  477. ERR_FAIL_COND_V(!p_control, Vector2());
  478. Transform2D parent_transform = p_control->get_transform().affine_inverse();
  479. Rect2 parent_rect = p_control->get_parent_anchorable_rect();
  480. if (p_control->is_layout_rtl()) {
  481. return parent_transform.xform(parent_rect.position + Vector2(parent_rect.size.x - parent_rect.size.x * anchor.x, parent_rect.size.y * anchor.y));
  482. } else {
  483. return parent_transform.xform(parent_rect.position + Vector2(parent_rect.size.x * anchor.x, parent_rect.size.y * anchor.y));
  484. }
  485. }
  486. Vector2 ControlEditorToolbar::_position_to_anchor(const Control *p_control, Vector2 position) {
  487. ERR_FAIL_COND_V(!p_control, Vector2());
  488. Rect2 parent_rect = p_control->get_parent_anchorable_rect();
  489. Vector2 output = Vector2();
  490. if (p_control->is_layout_rtl()) {
  491. output.x = (parent_rect.size.x == 0) ? 0.0 : (parent_rect.size.x - p_control->get_transform().xform(position).x - parent_rect.position.x) / parent_rect.size.x;
  492. } else {
  493. output.x = (parent_rect.size.x == 0) ? 0.0 : (p_control->get_transform().xform(position).x - parent_rect.position.x) / parent_rect.size.x;
  494. }
  495. output.y = (parent_rect.size.y == 0) ? 0.0 : (p_control->get_transform().xform(position).y - parent_rect.position.y) / parent_rect.size.y;
  496. return output;
  497. }
  498. void ControlEditorToolbar::_button_toggle_anchor_mode(bool p_status) {
  499. List<Control *> selection = _get_edited_controls(false, false);
  500. for (Control *E : selection) {
  501. if (Object::cast_to<Container>(E->get_parent())) {
  502. continue;
  503. }
  504. if (p_status) {
  505. E->set_meta("_edit_use_anchors_", true);
  506. } else {
  507. E->remove_meta("_edit_use_anchors_");
  508. }
  509. }
  510. anchors_mode = p_status;
  511. CanvasItemEditor::get_singleton()->update_viewport();
  512. }
  513. bool ControlEditorToolbar::_is_node_locked(const Node *p_node) {
  514. return p_node->get_meta("_edit_lock_", false);
  515. }
  516. List<Control *> ControlEditorToolbar::_get_edited_controls(bool retrieve_locked, bool remove_controls_if_parent_in_selection) {
  517. List<Control *> selection;
  518. for (const KeyValue<Node *, Object *> &E : editor_selection->get_selection()) {
  519. Control *control = Object::cast_to<Control>(E.key);
  520. if (control && control->is_visible_in_tree() && control->get_viewport() == EditorNode::get_singleton()->get_scene_root() && (retrieve_locked || !_is_node_locked(control))) {
  521. selection.push_back(control);
  522. }
  523. }
  524. if (remove_controls_if_parent_in_selection) {
  525. List<Control *> filtered_selection;
  526. for (Control *E : selection) {
  527. if (!selection.find(E->get_parent())) {
  528. filtered_selection.push_back(E);
  529. }
  530. }
  531. return filtered_selection;
  532. }
  533. return selection;
  534. }
  535. void ControlEditorToolbar::_popup_callback(int p_op) {
  536. switch (p_op) {
  537. case ANCHORS_AND_OFFSETS_PRESET_TOP_LEFT: {
  538. _set_anchors_and_offsets_preset(PRESET_TOP_LEFT);
  539. } break;
  540. case ANCHORS_AND_OFFSETS_PRESET_TOP_RIGHT: {
  541. _set_anchors_and_offsets_preset(PRESET_TOP_RIGHT);
  542. } break;
  543. case ANCHORS_AND_OFFSETS_PRESET_BOTTOM_LEFT: {
  544. _set_anchors_and_offsets_preset(PRESET_BOTTOM_LEFT);
  545. } break;
  546. case ANCHORS_AND_OFFSETS_PRESET_BOTTOM_RIGHT: {
  547. _set_anchors_and_offsets_preset(PRESET_BOTTOM_RIGHT);
  548. } break;
  549. case ANCHORS_AND_OFFSETS_PRESET_CENTER_LEFT: {
  550. _set_anchors_and_offsets_preset(PRESET_CENTER_LEFT);
  551. } break;
  552. case ANCHORS_AND_OFFSETS_PRESET_CENTER_RIGHT: {
  553. _set_anchors_and_offsets_preset(PRESET_CENTER_RIGHT);
  554. } break;
  555. case ANCHORS_AND_OFFSETS_PRESET_CENTER_TOP: {
  556. _set_anchors_and_offsets_preset(PRESET_CENTER_TOP);
  557. } break;
  558. case ANCHORS_AND_OFFSETS_PRESET_CENTER_BOTTOM: {
  559. _set_anchors_and_offsets_preset(PRESET_CENTER_BOTTOM);
  560. } break;
  561. case ANCHORS_AND_OFFSETS_PRESET_CENTER: {
  562. _set_anchors_and_offsets_preset(PRESET_CENTER);
  563. } break;
  564. case ANCHORS_AND_OFFSETS_PRESET_TOP_WIDE: {
  565. _set_anchors_and_offsets_preset(PRESET_TOP_WIDE);
  566. } break;
  567. case ANCHORS_AND_OFFSETS_PRESET_LEFT_WIDE: {
  568. _set_anchors_and_offsets_preset(PRESET_LEFT_WIDE);
  569. } break;
  570. case ANCHORS_AND_OFFSETS_PRESET_RIGHT_WIDE: {
  571. _set_anchors_and_offsets_preset(PRESET_RIGHT_WIDE);
  572. } break;
  573. case ANCHORS_AND_OFFSETS_PRESET_BOTTOM_WIDE: {
  574. _set_anchors_and_offsets_preset(PRESET_BOTTOM_WIDE);
  575. } break;
  576. case ANCHORS_AND_OFFSETS_PRESET_VCENTER_WIDE: {
  577. _set_anchors_and_offsets_preset(PRESET_VCENTER_WIDE);
  578. } break;
  579. case ANCHORS_AND_OFFSETS_PRESET_HCENTER_WIDE: {
  580. _set_anchors_and_offsets_preset(PRESET_HCENTER_WIDE);
  581. } break;
  582. case ANCHORS_AND_OFFSETS_PRESET_FULL_RECT: {
  583. _set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  584. } break;
  585. case ANCHORS_AND_OFFSETS_PRESET_KEEP_RATIO: {
  586. _set_anchors_and_offsets_to_keep_ratio();
  587. } break;
  588. case ANCHORS_PRESET_TOP_LEFT: {
  589. _set_anchors_preset(PRESET_TOP_LEFT);
  590. } break;
  591. case ANCHORS_PRESET_TOP_RIGHT: {
  592. _set_anchors_preset(PRESET_TOP_RIGHT);
  593. } break;
  594. case ANCHORS_PRESET_BOTTOM_LEFT: {
  595. _set_anchors_preset(PRESET_BOTTOM_LEFT);
  596. } break;
  597. case ANCHORS_PRESET_BOTTOM_RIGHT: {
  598. _set_anchors_preset(PRESET_BOTTOM_RIGHT);
  599. } break;
  600. case ANCHORS_PRESET_CENTER_LEFT: {
  601. _set_anchors_preset(PRESET_CENTER_LEFT);
  602. } break;
  603. case ANCHORS_PRESET_CENTER_RIGHT: {
  604. _set_anchors_preset(PRESET_CENTER_RIGHT);
  605. } break;
  606. case ANCHORS_PRESET_CENTER_TOP: {
  607. _set_anchors_preset(PRESET_CENTER_TOP);
  608. } break;
  609. case ANCHORS_PRESET_CENTER_BOTTOM: {
  610. _set_anchors_preset(PRESET_CENTER_BOTTOM);
  611. } break;
  612. case ANCHORS_PRESET_CENTER: {
  613. _set_anchors_preset(PRESET_CENTER);
  614. } break;
  615. case ANCHORS_PRESET_TOP_WIDE: {
  616. _set_anchors_preset(PRESET_TOP_WIDE);
  617. } break;
  618. case ANCHORS_PRESET_LEFT_WIDE: {
  619. _set_anchors_preset(PRESET_LEFT_WIDE);
  620. } break;
  621. case ANCHORS_PRESET_RIGHT_WIDE: {
  622. _set_anchors_preset(PRESET_RIGHT_WIDE);
  623. } break;
  624. case ANCHORS_PRESET_BOTTOM_WIDE: {
  625. _set_anchors_preset(PRESET_BOTTOM_WIDE);
  626. } break;
  627. case ANCHORS_PRESET_VCENTER_WIDE: {
  628. _set_anchors_preset(PRESET_VCENTER_WIDE);
  629. } break;
  630. case ANCHORS_PRESET_HCENTER_WIDE: {
  631. _set_anchors_preset(PRESET_HCENTER_WIDE);
  632. } break;
  633. case ANCHORS_PRESET_FULL_RECT: {
  634. _set_anchors_preset(Control::PRESET_FULL_RECT);
  635. } break;
  636. case CONTAINERS_H_PRESET_FILL: {
  637. _set_container_h_preset(Control::SIZE_FILL);
  638. } break;
  639. case CONTAINERS_H_PRESET_FILL_EXPAND: {
  640. _set_container_h_preset(Control::SIZE_EXPAND_FILL);
  641. } break;
  642. case CONTAINERS_H_PRESET_SHRINK_BEGIN: {
  643. _set_container_h_preset(Control::SIZE_SHRINK_BEGIN);
  644. } break;
  645. case CONTAINERS_H_PRESET_SHRINK_CENTER: {
  646. _set_container_h_preset(Control::SIZE_SHRINK_CENTER);
  647. } break;
  648. case CONTAINERS_H_PRESET_SHRINK_END: {
  649. _set_container_h_preset(Control::SIZE_SHRINK_END);
  650. } break;
  651. case CONTAINERS_V_PRESET_FILL: {
  652. _set_container_v_preset(Control::SIZE_FILL);
  653. } break;
  654. case CONTAINERS_V_PRESET_FILL_EXPAND: {
  655. _set_container_v_preset(Control::SIZE_EXPAND_FILL);
  656. } break;
  657. case CONTAINERS_V_PRESET_SHRINK_BEGIN: {
  658. _set_container_v_preset(Control::SIZE_SHRINK_BEGIN);
  659. } break;
  660. case CONTAINERS_V_PRESET_SHRINK_CENTER: {
  661. _set_container_v_preset(Control::SIZE_SHRINK_CENTER);
  662. } break;
  663. case CONTAINERS_V_PRESET_SHRINK_END: {
  664. _set_container_v_preset(Control::SIZE_SHRINK_END);
  665. } break;
  666. }
  667. }
  668. void ControlEditorToolbar::_selection_changed() {
  669. // Update the anchors_mode.
  670. int nb_controls = 0;
  671. int nb_valid_controls = 0;
  672. int nb_anchors_mode = 0;
  673. List<Node *> selection = editor_selection->get_selected_node_list();
  674. for (Node *E : selection) {
  675. Control *control = Object::cast_to<Control>(E);
  676. if (!control) {
  677. continue;
  678. }
  679. nb_controls++;
  680. if (Object::cast_to<Container>(control->get_parent())) {
  681. continue;
  682. }
  683. nb_valid_controls++;
  684. if (control->get_meta("_edit_use_anchors_", false)) {
  685. nb_anchors_mode++;
  686. }
  687. }
  688. anchors_mode = (nb_valid_controls == nb_anchors_mode);
  689. anchor_mode_button->set_pressed(anchors_mode);
  690. if (nb_controls > 0) {
  691. set_physics_process(true);
  692. } else {
  693. set_physics_process(false);
  694. set_visible(false);
  695. }
  696. }
  697. void ControlEditorToolbar::_notification(int p_what) {
  698. switch (p_what) {
  699. case NOTIFICATION_ENTER_TREE:
  700. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  701. anchor_presets_menu->set_icon(get_theme_icon(SNAME("ControlLayout"), SNAME("EditorIcons")));
  702. PopupMenu *p = anchor_presets_menu->get_popup();
  703. p->clear();
  704. p->add_icon_item(get_theme_icon(SNAME("ControlAlignTopLeft"), SNAME("EditorIcons")), TTR("Top Left"), ANCHORS_AND_OFFSETS_PRESET_TOP_LEFT);
  705. p->add_icon_item(get_theme_icon(SNAME("ControlAlignTopRight"), SNAME("EditorIcons")), TTR("Top Right"), ANCHORS_AND_OFFSETS_PRESET_TOP_RIGHT);
  706. p->add_icon_item(get_theme_icon(SNAME("ControlAlignBottomRight"), SNAME("EditorIcons")), TTR("Bottom Right"), ANCHORS_AND_OFFSETS_PRESET_BOTTOM_RIGHT);
  707. p->add_icon_item(get_theme_icon(SNAME("ControlAlignBottomLeft"), SNAME("EditorIcons")), TTR("Bottom Left"), ANCHORS_AND_OFFSETS_PRESET_BOTTOM_LEFT);
  708. p->add_separator();
  709. p->add_icon_item(get_theme_icon(SNAME("ControlAlignCenterLeft"), SNAME("EditorIcons")), TTR("Center Left"), ANCHORS_AND_OFFSETS_PRESET_CENTER_LEFT);
  710. p->add_icon_item(get_theme_icon(SNAME("ControlAlignCenterTop"), SNAME("EditorIcons")), TTR("Center Top"), ANCHORS_AND_OFFSETS_PRESET_CENTER_TOP);
  711. p->add_icon_item(get_theme_icon(SNAME("ControlAlignCenterRight"), SNAME("EditorIcons")), TTR("Center Right"), ANCHORS_AND_OFFSETS_PRESET_CENTER_RIGHT);
  712. p->add_icon_item(get_theme_icon(SNAME("ControlAlignCenterBottom"), SNAME("EditorIcons")), TTR("Center Bottom"), ANCHORS_AND_OFFSETS_PRESET_CENTER_BOTTOM);
  713. p->add_icon_item(get_theme_icon(SNAME("ControlAlignCenter"), SNAME("EditorIcons")), TTR("Center"), ANCHORS_AND_OFFSETS_PRESET_CENTER);
  714. p->add_separator();
  715. p->add_icon_item(get_theme_icon(SNAME("ControlAlignLeftWide"), SNAME("EditorIcons")), TTR("Left Wide"), ANCHORS_AND_OFFSETS_PRESET_LEFT_WIDE);
  716. p->add_icon_item(get_theme_icon(SNAME("ControlAlignTopWide"), SNAME("EditorIcons")), TTR("Top Wide"), ANCHORS_AND_OFFSETS_PRESET_TOP_WIDE);
  717. p->add_icon_item(get_theme_icon(SNAME("ControlAlignRightWide"), SNAME("EditorIcons")), TTR("Right Wide"), ANCHORS_AND_OFFSETS_PRESET_RIGHT_WIDE);
  718. p->add_icon_item(get_theme_icon(SNAME("ControlAlignBottomWide"), SNAME("EditorIcons")), TTR("Bottom Wide"), ANCHORS_AND_OFFSETS_PRESET_BOTTOM_WIDE);
  719. p->add_icon_item(get_theme_icon(SNAME("ControlAlignVCenterWide"), SNAME("EditorIcons")), TTR("VCenter Wide"), ANCHORS_AND_OFFSETS_PRESET_VCENTER_WIDE);
  720. p->add_icon_item(get_theme_icon(SNAME("ControlAlignHCenterWide"), SNAME("EditorIcons")), TTR("HCenter Wide"), ANCHORS_AND_OFFSETS_PRESET_HCENTER_WIDE);
  721. p->add_separator();
  722. p->add_icon_item(get_theme_icon(SNAME("ControlAlignFullRect"), SNAME("EditorIcons")), TTR("Full Rect"), ANCHORS_AND_OFFSETS_PRESET_FULL_RECT);
  723. p->add_icon_item(get_theme_icon(SNAME("Anchor"), SNAME("EditorIcons")), TTR("Keep Current Ratio"), ANCHORS_AND_OFFSETS_PRESET_KEEP_RATIO);
  724. p->set_item_tooltip(19, TTR("Adjust anchors and offsets to match the current rect size."));
  725. p->add_separator();
  726. p->add_submenu_item(TTR("Anchors only"), "Anchors");
  727. p->set_item_icon(21, get_theme_icon(SNAME("Anchor"), SNAME("EditorIcons")));
  728. anchors_popup->clear();
  729. anchors_popup->add_icon_item(get_theme_icon(SNAME("ControlAlignTopLeft"), SNAME("EditorIcons")), TTR("Top Left"), ANCHORS_PRESET_TOP_LEFT);
  730. anchors_popup->add_icon_item(get_theme_icon(SNAME("ControlAlignTopRight"), SNAME("EditorIcons")), TTR("Top Right"), ANCHORS_PRESET_TOP_RIGHT);
  731. anchors_popup->add_icon_item(get_theme_icon(SNAME("ControlAlignBottomRight"), SNAME("EditorIcons")), TTR("Bottom Right"), ANCHORS_PRESET_BOTTOM_RIGHT);
  732. anchors_popup->add_icon_item(get_theme_icon(SNAME("ControlAlignBottomLeft"), SNAME("EditorIcons")), TTR("Bottom Left"), ANCHORS_PRESET_BOTTOM_LEFT);
  733. anchors_popup->add_separator();
  734. anchors_popup->add_icon_item(get_theme_icon(SNAME("ControlAlignCenterLeft"), SNAME("EditorIcons")), TTR("Center Left"), ANCHORS_PRESET_CENTER_LEFT);
  735. anchors_popup->add_icon_item(get_theme_icon(SNAME("ControlAlignCenterTop"), SNAME("EditorIcons")), TTR("Center Top"), ANCHORS_PRESET_CENTER_TOP);
  736. anchors_popup->add_icon_item(get_theme_icon(SNAME("ControlAlignCenterRight"), SNAME("EditorIcons")), TTR("Center Right"), ANCHORS_PRESET_CENTER_RIGHT);
  737. anchors_popup->add_icon_item(get_theme_icon(SNAME("ControlAlignCenterBottom"), SNAME("EditorIcons")), TTR("Center Bottom"), ANCHORS_PRESET_CENTER_BOTTOM);
  738. anchors_popup->add_icon_item(get_theme_icon(SNAME("ControlAlignCenter"), SNAME("EditorIcons")), TTR("Center"), ANCHORS_PRESET_CENTER);
  739. anchors_popup->add_separator();
  740. anchors_popup->add_icon_item(get_theme_icon(SNAME("ControlAlignLeftWide"), SNAME("EditorIcons")), TTR("Left Wide"), ANCHORS_PRESET_LEFT_WIDE);
  741. anchors_popup->add_icon_item(get_theme_icon(SNAME("ControlAlignTopWide"), SNAME("EditorIcons")), TTR("Top Wide"), ANCHORS_PRESET_TOP_WIDE);
  742. anchors_popup->add_icon_item(get_theme_icon(SNAME("ControlAlignRightWide"), SNAME("EditorIcons")), TTR("Right Wide"), ANCHORS_PRESET_RIGHT_WIDE);
  743. anchors_popup->add_icon_item(get_theme_icon(SNAME("ControlAlignBottomWide"), SNAME("EditorIcons")), TTR("Bottom Wide"), ANCHORS_PRESET_BOTTOM_WIDE);
  744. anchors_popup->add_icon_item(get_theme_icon(SNAME("ControlAlignVCenterWide"), SNAME("EditorIcons")), TTR("VCenter Wide"), ANCHORS_PRESET_VCENTER_WIDE);
  745. anchors_popup->add_icon_item(get_theme_icon(SNAME("ControlAlignHCenterWide"), SNAME("EditorIcons")), TTR("HCenter Wide"), ANCHORS_PRESET_HCENTER_WIDE);
  746. anchors_popup->add_separator();
  747. anchors_popup->add_icon_item(get_theme_icon(SNAME("ControlAlignFullRect"), SNAME("EditorIcons")), TTR("Full Rect"), ANCHORS_PRESET_FULL_RECT);
  748. anchor_mode_button->set_icon(get_theme_icon(SNAME("Anchor"), SNAME("EditorIcons")));
  749. container_h_presets_menu->set_icon(get_theme_icon(SNAME("Container"), SNAME("EditorIcons")));
  750. container_v_presets_menu->set_icon(get_theme_icon(SNAME("Container"), SNAME("EditorIcons")));
  751. p = container_h_presets_menu->get_popup();
  752. p->clear();
  753. p->add_icon_item(get_theme_icon(SNAME("ControlAlignHCenterWide"), SNAME("EditorIcons")), TTR("Fill"), CONTAINERS_H_PRESET_FILL);
  754. p->add_icon_item(get_theme_icon(SNAME("ControlAlignHCenterWide"), SNAME("EditorIcons")), TTR("Fill & Expand"), CONTAINERS_H_PRESET_FILL_EXPAND);
  755. p->add_icon_item(get_theme_icon(SNAME("ControlAlignCenterLeft"), SNAME("EditorIcons")), TTR("Shrink Begin"), CONTAINERS_H_PRESET_SHRINK_BEGIN);
  756. p->add_icon_item(get_theme_icon(SNAME("ControlAlignCenter"), SNAME("EditorIcons")), TTR("Shrink Center"), CONTAINERS_H_PRESET_SHRINK_CENTER);
  757. p->add_icon_item(get_theme_icon(SNAME("ControlAlignCenterRight"), SNAME("EditorIcons")), TTR("Shrink End"), CONTAINERS_H_PRESET_SHRINK_END);
  758. p = container_v_presets_menu->get_popup();
  759. p->clear();
  760. p->add_icon_item(get_theme_icon(SNAME("ControlAlignVCenterWide"), SNAME("EditorIcons")), TTR("Fill"), CONTAINERS_V_PRESET_FILL);
  761. p->add_icon_item(get_theme_icon(SNAME("ControlAlignVCenterWide"), SNAME("EditorIcons")), TTR("Fill & Expand"), CONTAINERS_V_PRESET_FILL_EXPAND);
  762. p->add_icon_item(get_theme_icon(SNAME("ControlAlignCenterTop"), SNAME("EditorIcons")), TTR("Shrink Begin"), CONTAINERS_V_PRESET_SHRINK_BEGIN);
  763. p->add_icon_item(get_theme_icon(SNAME("ControlAlignCenter"), SNAME("EditorIcons")), TTR("Shrink Center"), CONTAINERS_V_PRESET_SHRINK_CENTER);
  764. p->add_icon_item(get_theme_icon(SNAME("ControlAlignCenterBottom"), SNAME("EditorIcons")), TTR("Shrink End"), CONTAINERS_V_PRESET_SHRINK_END);
  765. } break;
  766. case NOTIFICATION_PHYSICS_PROCESS: {
  767. bool has_control_parents = false;
  768. bool has_container_parents = false;
  769. // Update the viewport if the canvas_item changes
  770. List<Control *> selection = _get_edited_controls(true);
  771. for (Control *control : selection) {
  772. if (Object::cast_to<Control>(control->get_parent())) {
  773. has_control_parents = true;
  774. }
  775. if (Object::cast_to<Container>(control->get_parent())) {
  776. has_container_parents = true;
  777. }
  778. }
  779. // Show / Hide the control layout buttons.
  780. if (selection.size() > 0) {
  781. set_visible(true);
  782. // Toggle anchor and container layout buttons depending on parents of the selected nodes.
  783. // - If there are no control parents, enable everything.
  784. // - If there are container parents, then enable only container buttons.
  785. // - If there are NO container parents, then enable only anchor buttons.
  786. bool enable_anchors = false;
  787. bool enable_containers = false;
  788. if (!has_control_parents) {
  789. enable_anchors = true;
  790. enable_containers = true;
  791. } else if (has_container_parents) {
  792. enable_containers = true;
  793. } else {
  794. enable_anchors = true;
  795. }
  796. if (enable_anchors) {
  797. anchor_presets_menu->set_visible(true);
  798. anchor_mode_button->set_visible(true);
  799. } else {
  800. anchor_presets_menu->set_visible(false);
  801. anchor_mode_button->set_visible(false);
  802. }
  803. if (enable_containers) {
  804. container_h_presets_menu->set_visible(true);
  805. container_v_presets_menu->set_visible(true);
  806. } else {
  807. container_h_presets_menu->set_visible(false);
  808. container_v_presets_menu->set_visible(false);
  809. }
  810. } else {
  811. set_visible(false);
  812. }
  813. } break;
  814. }
  815. }
  816. ControlEditorToolbar::ControlEditorToolbar() {
  817. anchor_presets_menu = memnew(MenuButton);
  818. anchor_presets_menu->set_shortcut_context(this);
  819. anchor_presets_menu->set_text(TTR("Anchors"));
  820. anchor_presets_menu->set_tooltip(TTR("Presets for the anchor and offset values of a Control node."));
  821. add_child(anchor_presets_menu);
  822. anchor_presets_menu->set_switch_on_hover(true);
  823. PopupMenu *p = anchor_presets_menu->get_popup();
  824. p->connect("id_pressed", callable_mp(this, &ControlEditorToolbar::_popup_callback));
  825. anchors_popup = memnew(PopupMenu);
  826. p->add_child(anchors_popup);
  827. anchors_popup->set_name("Anchors");
  828. anchors_popup->connect("id_pressed", callable_mp(this, &ControlEditorToolbar::_popup_callback));
  829. anchor_mode_button = memnew(Button);
  830. anchor_mode_button->set_flat(true);
  831. anchor_mode_button->set_toggle_mode(true);
  832. anchor_mode_button->set_tooltip(TTR("When active, moving Control nodes changes their anchors instead of their offsets."));
  833. add_child(anchor_mode_button);
  834. anchor_mode_button->connect("toggled", callable_mp(this, &ControlEditorToolbar::_button_toggle_anchor_mode));
  835. add_child(memnew(VSeparator));
  836. container_h_presets_menu = memnew(MenuButton);
  837. container_h_presets_menu->set_shortcut_context(this);
  838. container_h_presets_menu->set_text(TTR("Horizontal"));
  839. container_h_presets_menu->set_tooltip(TTR("Horizontal sizing setting for children of a Container node."));
  840. add_child(container_h_presets_menu);
  841. container_h_presets_menu->set_switch_on_hover(true);
  842. p = container_h_presets_menu->get_popup();
  843. p->connect("id_pressed", callable_mp(this, &ControlEditorToolbar::_popup_callback));
  844. container_v_presets_menu = memnew(MenuButton);
  845. container_v_presets_menu->set_shortcut_context(this);
  846. container_v_presets_menu->set_text(TTR("Vertical"));
  847. container_v_presets_menu->set_tooltip(TTR("Vertical sizing setting for children of a Container node."));
  848. add_child(container_v_presets_menu);
  849. container_v_presets_menu->set_switch_on_hover(true);
  850. p = container_v_presets_menu->get_popup();
  851. p->connect("id_pressed", callable_mp(this, &ControlEditorToolbar::_popup_callback));
  852. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  853. editor_selection = EditorNode::get_singleton()->get_editor_selection();
  854. editor_selection->add_editor_plugin(this);
  855. editor_selection->connect("selection_changed", callable_mp(this, &ControlEditorToolbar::_selection_changed));
  856. singleton = this;
  857. }
  858. ControlEditorToolbar *ControlEditorToolbar::singleton = nullptr;
  859. ControlEditorPlugin::ControlEditorPlugin() {
  860. toolbar = memnew(ControlEditorToolbar);
  861. toolbar->hide();
  862. add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar);
  863. Ref<EditorInspectorPluginControl> plugin;
  864. plugin.instantiate();
  865. add_inspector_plugin(plugin);
  866. }