control_editor_plugin.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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/editor_scale.h"
  33. #include "editor/editor_settings.h"
  34. #include "editor/editor_undo_redo_manager.h"
  35. #include "editor/plugins/canvas_item_editor_plugin.h"
  36. #include "scene/gui/separator.h"
  37. // Inspector controls.
  38. void ControlPositioningWarning::_update_warning() {
  39. if (!control_node) {
  40. title_icon->set_texture(nullptr);
  41. title_label->set_text("");
  42. hint_label->set_text("");
  43. return;
  44. }
  45. Node *parent_node = control_node->get_parent_control();
  46. if (!parent_node) {
  47. title_icon->set_texture(get_theme_icon(SNAME("SubViewport"), SNAME("EditorIcons")));
  48. title_label->set_text(TTR("This node doesn't have a control parent."));
  49. hint_label->set_text(TTR("Use the appropriate layout properties depending on where you are going to put it."));
  50. } else if (Object::cast_to<Container>(parent_node)) {
  51. title_icon->set_texture(get_theme_icon(SNAME("ContainerLayout"), SNAME("EditorIcons")));
  52. title_label->set_text(TTR("This node is a child of a container."));
  53. hint_label->set_text(TTR("Use container properties for positioning."));
  54. } else {
  55. title_icon->set_texture(get_theme_icon(SNAME("ControlLayout"), SNAME("EditorIcons")));
  56. title_label->set_text(TTR("This node is a child of a regular control."));
  57. hint_label->set_text(TTR("Use anchors and the rectangle for positioning."));
  58. }
  59. bg_panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("bg_group_note"), SNAME("EditorProperty")));
  60. }
  61. void ControlPositioningWarning::_update_toggler() {
  62. Ref<Texture2D> arrow;
  63. if (hint_label->is_visible()) {
  64. arrow = get_theme_icon(SNAME("arrow"), SNAME("Tree"));
  65. set_tooltip_text(TTR("Collapse positioning hint."));
  66. } else {
  67. if (is_layout_rtl()) {
  68. arrow = get_theme_icon(SNAME("arrow_collapsed"), SNAME("Tree"));
  69. } else {
  70. arrow = get_theme_icon(SNAME("arrow_collapsed_mirrored"), SNAME("Tree"));
  71. }
  72. set_tooltip_text(TTR("Expand positioning hint."));
  73. }
  74. hint_icon->set_texture(arrow);
  75. }
  76. void ControlPositioningWarning::set_control(Control *p_node) {
  77. control_node = p_node;
  78. _update_warning();
  79. }
  80. void ControlPositioningWarning::gui_input(const Ref<InputEvent> &p_event) {
  81. Ref<InputEventMouseButton> mb = p_event;
  82. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  83. bool state = !hint_label->is_visible();
  84. hint_filler_left->set_visible(state);
  85. hint_label->set_visible(state);
  86. hint_filler_right->set_visible(state);
  87. _update_toggler();
  88. }
  89. }
  90. void ControlPositioningWarning::_notification(int p_notification) {
  91. switch (p_notification) {
  92. case NOTIFICATION_ENTER_TREE:
  93. case NOTIFICATION_THEME_CHANGED:
  94. _update_warning();
  95. _update_toggler();
  96. break;
  97. }
  98. }
  99. ControlPositioningWarning::ControlPositioningWarning() {
  100. set_mouse_filter(MOUSE_FILTER_STOP);
  101. bg_panel = memnew(PanelContainer);
  102. bg_panel->set_mouse_filter(MOUSE_FILTER_IGNORE);
  103. add_child(bg_panel);
  104. grid = memnew(GridContainer);
  105. grid->set_columns(3);
  106. bg_panel->add_child(grid);
  107. title_icon = memnew(TextureRect);
  108. title_icon->set_stretch_mode(TextureRect::StretchMode::STRETCH_KEEP_CENTERED);
  109. grid->add_child(title_icon);
  110. title_label = memnew(Label);
  111. title_label->set_autowrap_mode(TextServer::AutowrapMode::AUTOWRAP_WORD);
  112. title_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  113. title_label->set_vertical_alignment(VerticalAlignment::VERTICAL_ALIGNMENT_CENTER);
  114. grid->add_child(title_label);
  115. hint_icon = memnew(TextureRect);
  116. hint_icon->set_stretch_mode(TextureRect::StretchMode::STRETCH_KEEP_CENTERED);
  117. grid->add_child(hint_icon);
  118. // Filler.
  119. hint_filler_left = memnew(Control);
  120. hint_filler_left->hide();
  121. grid->add_child(hint_filler_left);
  122. hint_label = memnew(Label);
  123. hint_label->set_autowrap_mode(TextServer::AutowrapMode::AUTOWRAP_WORD);
  124. hint_label->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  125. hint_label->set_vertical_alignment(VerticalAlignment::VERTICAL_ALIGNMENT_CENTER);
  126. hint_label->hide();
  127. grid->add_child(hint_label);
  128. // Filler.
  129. hint_filler_right = memnew(Control);
  130. hint_filler_right->hide();
  131. grid->add_child(hint_filler_right);
  132. }
  133. void EditorPropertyAnchorsPreset::_set_read_only(bool p_read_only) {
  134. options->set_disabled(p_read_only);
  135. };
  136. void EditorPropertyAnchorsPreset::_option_selected(int p_which) {
  137. int64_t val = options->get_item_metadata(p_which);
  138. emit_changed(get_edited_property(), val);
  139. }
  140. void EditorPropertyAnchorsPreset::update_property() {
  141. int64_t which = get_edited_object()->get(get_edited_property());
  142. for (int i = 0; i < options->get_item_count(); i++) {
  143. Variant val = options->get_item_metadata(i);
  144. if (val != Variant() && which == (int64_t)val) {
  145. options->select(i);
  146. return;
  147. }
  148. }
  149. }
  150. void EditorPropertyAnchorsPreset::setup(const Vector<String> &p_options) {
  151. options->clear();
  152. Vector<String> split_after;
  153. split_after.append("Custom");
  154. split_after.append("PresetFullRect");
  155. split_after.append("PresetBottomLeft");
  156. split_after.append("PresetCenter");
  157. for (int i = 0, j = 0; i < p_options.size(); i++, j++) {
  158. Vector<String> text_split = p_options[i].split(":");
  159. int64_t current_val = text_split[1].to_int();
  160. String option_name = text_split[0];
  161. if (option_name.begins_with("Preset")) {
  162. String preset_name = option_name.trim_prefix("Preset");
  163. String humanized_name = preset_name.capitalize();
  164. String icon_name = "ControlAlign" + preset_name;
  165. options->add_icon_item(EditorNode::get_singleton()->get_gui_base()->get_theme_icon(icon_name, "EditorIcons"), humanized_name);
  166. } else {
  167. options->add_item(option_name);
  168. }
  169. options->set_item_metadata(j, current_val);
  170. if (split_after.has(option_name)) {
  171. options->add_separator();
  172. j++;
  173. }
  174. }
  175. }
  176. EditorPropertyAnchorsPreset::EditorPropertyAnchorsPreset() {
  177. options = memnew(OptionButton);
  178. options->set_clip_text(true);
  179. options->set_flat(true);
  180. add_child(options);
  181. add_focusable(options);
  182. options->connect("item_selected", callable_mp(this, &EditorPropertyAnchorsPreset::_option_selected));
  183. }
  184. void EditorPropertySizeFlags::_set_read_only(bool p_read_only) {
  185. for (CheckBox *check : flag_checks) {
  186. check->set_disabled(p_read_only);
  187. }
  188. flag_presets->set_disabled(p_read_only);
  189. };
  190. void EditorPropertySizeFlags::_preset_selected(int p_which) {
  191. int preset = flag_presets->get_item_id(p_which);
  192. if (preset == SIZE_FLAGS_PRESET_CUSTOM) {
  193. flag_options->set_visible(true);
  194. return;
  195. }
  196. flag_options->set_visible(false);
  197. uint32_t value = 0;
  198. switch (preset) {
  199. case SIZE_FLAGS_PRESET_FILL:
  200. value = Control::SIZE_FILL;
  201. break;
  202. case SIZE_FLAGS_PRESET_SHRINK_BEGIN:
  203. value = Control::SIZE_SHRINK_BEGIN;
  204. break;
  205. case SIZE_FLAGS_PRESET_SHRINK_CENTER:
  206. value = Control::SIZE_SHRINK_CENTER;
  207. break;
  208. case SIZE_FLAGS_PRESET_SHRINK_END:
  209. value = Control::SIZE_SHRINK_END;
  210. break;
  211. }
  212. bool is_expand = flag_expand->is_visible() && flag_expand->is_pressed();
  213. if (is_expand) {
  214. value |= Control::SIZE_EXPAND;
  215. }
  216. emit_changed(get_edited_property(), value);
  217. }
  218. void EditorPropertySizeFlags::_expand_toggled() {
  219. uint32_t value = get_edited_object()->get(get_edited_property());
  220. if (flag_expand->is_visible() && flag_expand->is_pressed()) {
  221. value |= Control::SIZE_EXPAND;
  222. } else {
  223. value ^= Control::SIZE_EXPAND;
  224. }
  225. // Keep the custom preset selected as we toggle individual flags.
  226. keep_selected_preset = true;
  227. emit_changed(get_edited_property(), value);
  228. }
  229. void EditorPropertySizeFlags::_flag_toggled() {
  230. uint32_t value = 0;
  231. for (int i = 0; i < flag_checks.size(); i++) {
  232. if (flag_checks[i]->is_pressed()) {
  233. int flag_value = flag_checks[i]->get_meta("_value");
  234. value |= flag_value;
  235. }
  236. }
  237. bool is_expand = flag_expand->is_visible() && flag_expand->is_pressed();
  238. if (is_expand) {
  239. value |= Control::SIZE_EXPAND;
  240. }
  241. // Keep the custom preset selected as we toggle individual flags.
  242. keep_selected_preset = true;
  243. emit_changed(get_edited_property(), value);
  244. }
  245. void EditorPropertySizeFlags::update_property() {
  246. uint32_t value = get_edited_object()->get(get_edited_property());
  247. for (int i = 0; i < flag_checks.size(); i++) {
  248. int flag_value = flag_checks[i]->get_meta("_value");
  249. if (value & flag_value) {
  250. flag_checks[i]->set_pressed(true);
  251. } else {
  252. flag_checks[i]->set_pressed(false);
  253. }
  254. }
  255. bool is_expand = value & Control::SIZE_EXPAND;
  256. flag_expand->set_pressed(is_expand);
  257. if (keep_selected_preset) {
  258. keep_selected_preset = false;
  259. return;
  260. }
  261. FlagPreset preset = SIZE_FLAGS_PRESET_CUSTOM;
  262. if (value == Control::SIZE_FILL || value == (Control::SIZE_FILL | Control::SIZE_EXPAND)) {
  263. preset = SIZE_FLAGS_PRESET_FILL;
  264. } else if (value == Control::SIZE_SHRINK_BEGIN || value == (Control::SIZE_SHRINK_BEGIN | Control::SIZE_EXPAND)) {
  265. preset = SIZE_FLAGS_PRESET_SHRINK_BEGIN;
  266. } else if (value == Control::SIZE_SHRINK_CENTER || value == (Control::SIZE_SHRINK_CENTER | Control::SIZE_EXPAND)) {
  267. preset = SIZE_FLAGS_PRESET_SHRINK_CENTER;
  268. } else if (value == Control::SIZE_SHRINK_END || value == (Control::SIZE_SHRINK_END | Control::SIZE_EXPAND)) {
  269. preset = SIZE_FLAGS_PRESET_SHRINK_END;
  270. }
  271. int preset_idx = flag_presets->get_item_index(preset);
  272. if (preset_idx >= 0) {
  273. flag_presets->select(preset_idx);
  274. }
  275. flag_options->set_visible(preset == SIZE_FLAGS_PRESET_CUSTOM);
  276. }
  277. void EditorPropertySizeFlags::setup(const Vector<String> &p_options, bool p_vertical) {
  278. vertical = p_vertical;
  279. if (p_options.size() == 0) {
  280. flag_presets->clear();
  281. flag_presets->add_item(TTR("Container Default"));
  282. flag_presets->set_disabled(true);
  283. flag_expand->set_visible(false);
  284. return;
  285. }
  286. HashMap<int, String> flags;
  287. for (int i = 0, j = 0; i < p_options.size(); i++, j++) {
  288. Vector<String> text_split = p_options[i].split(":");
  289. int64_t current_val = text_split[1].to_int();
  290. flags[current_val] = text_split[0];
  291. if (current_val == SIZE_EXPAND) {
  292. continue;
  293. }
  294. CheckBox *cb = memnew(CheckBox);
  295. cb->set_text(text_split[0]);
  296. cb->set_clip_text(true);
  297. cb->set_meta("_value", current_val);
  298. cb->connect("pressed", callable_mp(this, &EditorPropertySizeFlags::_flag_toggled));
  299. add_focusable(cb);
  300. flag_options->add_child(cb);
  301. flag_checks.append(cb);
  302. }
  303. Control *gui_base = EditorNode::get_singleton()->get_gui_base();
  304. String wide_preset_icon = SNAME("ControlAlignHCenterWide");
  305. String begin_preset_icon = SNAME("ControlAlignCenterLeft");
  306. String end_preset_icon = SNAME("ControlAlignCenterRight");
  307. if (vertical) {
  308. wide_preset_icon = SNAME("ControlAlignVCenterWide");
  309. begin_preset_icon = SNAME("ControlAlignCenterTop");
  310. end_preset_icon = SNAME("ControlAlignCenterBottom");
  311. }
  312. flag_presets->clear();
  313. if (flags.has(SIZE_FILL)) {
  314. flag_presets->add_icon_item(gui_base->get_theme_icon(wide_preset_icon, SNAME("EditorIcons")), TTR("Fill"), SIZE_FLAGS_PRESET_FILL);
  315. }
  316. // Shrink Begin is the same as no flags at all, as such it cannot be disabled.
  317. flag_presets->add_icon_item(gui_base->get_theme_icon(begin_preset_icon, SNAME("EditorIcons")), TTR("Shrink Begin"), SIZE_FLAGS_PRESET_SHRINK_BEGIN);
  318. if (flags.has(SIZE_SHRINK_CENTER)) {
  319. flag_presets->add_icon_item(gui_base->get_theme_icon(SNAME("ControlAlignCenter"), SNAME("EditorIcons")), TTR("Shrink Center"), SIZE_FLAGS_PRESET_SHRINK_CENTER);
  320. }
  321. if (flags.has(SIZE_SHRINK_END)) {
  322. flag_presets->add_icon_item(gui_base->get_theme_icon(end_preset_icon, SNAME("EditorIcons")), TTR("Shrink End"), SIZE_FLAGS_PRESET_SHRINK_END);
  323. }
  324. flag_presets->add_separator();
  325. flag_presets->add_item(TTR("Custom"), SIZE_FLAGS_PRESET_CUSTOM);
  326. flag_expand->set_visible(flags.has(SIZE_EXPAND));
  327. }
  328. EditorPropertySizeFlags::EditorPropertySizeFlags() {
  329. VBoxContainer *vb = memnew(VBoxContainer);
  330. add_child(vb);
  331. flag_presets = memnew(OptionButton);
  332. flag_presets->set_clip_text(true);
  333. flag_presets->set_flat(true);
  334. vb->add_child(flag_presets);
  335. add_focusable(flag_presets);
  336. set_label_reference(flag_presets);
  337. flag_presets->connect("item_selected", callable_mp(this, &EditorPropertySizeFlags::_preset_selected));
  338. flag_options = memnew(VBoxContainer);
  339. flag_options->hide();
  340. vb->add_child(flag_options);
  341. flag_expand = memnew(CheckBox);
  342. flag_expand->set_text(TTR("Expand"));
  343. vb->add_child(flag_expand);
  344. add_focusable(flag_expand);
  345. flag_expand->connect("pressed", callable_mp(this, &EditorPropertySizeFlags::_expand_toggled));
  346. }
  347. bool EditorInspectorPluginControl::can_handle(Object *p_object) {
  348. return Object::cast_to<Control>(p_object) != nullptr;
  349. }
  350. void EditorInspectorPluginControl::parse_group(Object *p_object, const String &p_group) {
  351. Control *control = Object::cast_to<Control>(p_object);
  352. if (!control || p_group != "Layout") {
  353. return;
  354. }
  355. ControlPositioningWarning *pos_warning = memnew(ControlPositioningWarning);
  356. pos_warning->set_control(control);
  357. add_custom_control(pos_warning);
  358. }
  359. 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) {
  360. Control *control = Object::cast_to<Control>(p_object);
  361. if (!control) {
  362. return false;
  363. }
  364. if (p_path == "anchors_preset") {
  365. EditorPropertyAnchorsPreset *prop_editor = memnew(EditorPropertyAnchorsPreset);
  366. Vector<String> options = p_hint_text.split(",");
  367. prop_editor->setup(options);
  368. add_property_editor(p_path, prop_editor);
  369. return true;
  370. }
  371. if (p_path == "size_flags_horizontal" || p_path == "size_flags_vertical") {
  372. EditorPropertySizeFlags *prop_editor = memnew(EditorPropertySizeFlags);
  373. Vector<String> options;
  374. if (!p_hint_text.is_empty()) {
  375. options = p_hint_text.split(",");
  376. }
  377. prop_editor->setup(options, p_path == "size_flags_vertical");
  378. add_property_editor(p_path, prop_editor);
  379. return true;
  380. }
  381. return false;
  382. }
  383. // Toolbars controls.
  384. Size2 ControlEditorPopupButton::get_minimum_size() const {
  385. Vector2 base_size = Vector2(26, 26) * EDSCALE;
  386. if (arrow_icon.is_null()) {
  387. return base_size;
  388. }
  389. Vector2 final_size;
  390. final_size.x = base_size.x + arrow_icon->get_width();
  391. final_size.y = MAX(base_size.y, arrow_icon->get_height());
  392. return final_size;
  393. }
  394. void ControlEditorPopupButton::toggled(bool p_pressed) {
  395. if (!p_pressed) {
  396. return;
  397. }
  398. Size2 size = get_size() * get_viewport()->get_canvas_transform().get_scale();
  399. popup_panel->set_size(Size2(size.width, 0));
  400. Point2 gp = get_screen_position();
  401. gp.y += size.y;
  402. if (is_layout_rtl()) {
  403. gp.x += size.width - popup_panel->get_size().width;
  404. }
  405. popup_panel->set_position(gp);
  406. popup_panel->popup();
  407. }
  408. void ControlEditorPopupButton::_popup_visibility_changed(bool p_visible) {
  409. set_pressed(p_visible);
  410. }
  411. void ControlEditorPopupButton::_notification(int p_what) {
  412. switch (p_what) {
  413. case NOTIFICATION_ENTER_TREE:
  414. case NOTIFICATION_THEME_CHANGED: {
  415. arrow_icon = get_theme_icon("select_arrow", "Tree");
  416. } break;
  417. case NOTIFICATION_DRAW: {
  418. if (arrow_icon.is_valid()) {
  419. Vector2 arrow_pos = Point2(26, 0) * EDSCALE;
  420. arrow_pos.y = get_size().y / 2 - arrow_icon->get_height() / 2;
  421. draw_texture(arrow_icon, arrow_pos);
  422. }
  423. } break;
  424. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  425. popup_panel->set_layout_direction((Window::LayoutDirection)get_layout_direction());
  426. } break;
  427. case NOTIFICATION_VISIBILITY_CHANGED: {
  428. if (!is_visible_in_tree()) {
  429. popup_panel->hide();
  430. }
  431. } break;
  432. }
  433. }
  434. ControlEditorPopupButton::ControlEditorPopupButton() {
  435. set_flat(true);
  436. set_toggle_mode(true);
  437. set_focus_mode(FOCUS_NONE);
  438. popup_panel = memnew(PopupPanel);
  439. popup_panel->set_theme_type_variation("ControlEditorPopupPanel");
  440. add_child(popup_panel);
  441. popup_panel->connect("about_to_popup", callable_mp(this, &ControlEditorPopupButton::_popup_visibility_changed).bind(true));
  442. popup_panel->connect("popup_hide", callable_mp(this, &ControlEditorPopupButton::_popup_visibility_changed).bind(false));
  443. popup_vbox = memnew(VBoxContainer);
  444. popup_panel->add_child(popup_vbox);
  445. }
  446. void ControlEditorPresetPicker::_add_row_button(HBoxContainer *p_row, const int p_preset, const String &p_name) {
  447. ERR_FAIL_COND(preset_buttons.has(p_preset));
  448. Button *b = memnew(Button);
  449. b->set_custom_minimum_size(Size2i(36, 36) * EDSCALE);
  450. b->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  451. b->set_tooltip_text(p_name);
  452. b->set_flat(true);
  453. p_row->add_child(b);
  454. b->connect("pressed", callable_mp(this, &ControlEditorPresetPicker::_preset_button_pressed).bind(p_preset));
  455. preset_buttons[p_preset] = b;
  456. }
  457. void ControlEditorPresetPicker::_add_separator(BoxContainer *p_box, Separator *p_separator) {
  458. p_separator->add_theme_constant_override("separation", grid_separation);
  459. p_separator->set_custom_minimum_size(Size2i(1, 1));
  460. p_box->add_child(p_separator);
  461. }
  462. void AnchorPresetPicker::_preset_button_pressed(const int p_preset) {
  463. emit_signal("anchors_preset_selected", p_preset);
  464. }
  465. void AnchorPresetPicker::_notification(int p_notification) {
  466. switch (p_notification) {
  467. case NOTIFICATION_ENTER_TREE:
  468. case NOTIFICATION_THEME_CHANGED: {
  469. preset_buttons[PRESET_TOP_LEFT]->set_icon(get_theme_icon(SNAME("ControlAlignTopLeft"), SNAME("EditorIcons")));
  470. preset_buttons[PRESET_CENTER_TOP]->set_icon(get_theme_icon(SNAME("ControlAlignCenterTop"), SNAME("EditorIcons")));
  471. preset_buttons[PRESET_TOP_RIGHT]->set_icon(get_theme_icon(SNAME("ControlAlignTopRight"), SNAME("EditorIcons")));
  472. preset_buttons[PRESET_CENTER_LEFT]->set_icon(get_theme_icon(SNAME("ControlAlignCenterLeft"), SNAME("EditorIcons")));
  473. preset_buttons[PRESET_CENTER]->set_icon(get_theme_icon(SNAME("ControlAlignCenter"), SNAME("EditorIcons")));
  474. preset_buttons[PRESET_CENTER_RIGHT]->set_icon(get_theme_icon(SNAME("ControlAlignCenterRight"), SNAME("EditorIcons")));
  475. preset_buttons[PRESET_BOTTOM_LEFT]->set_icon(get_theme_icon(SNAME("ControlAlignBottomLeft"), SNAME("EditorIcons")));
  476. preset_buttons[PRESET_CENTER_BOTTOM]->set_icon(get_theme_icon(SNAME("ControlAlignCenterBottom"), SNAME("EditorIcons")));
  477. preset_buttons[PRESET_BOTTOM_RIGHT]->set_icon(get_theme_icon(SNAME("ControlAlignBottomRight"), SNAME("EditorIcons")));
  478. preset_buttons[PRESET_TOP_WIDE]->set_icon(get_theme_icon(SNAME("ControlAlignTopWide"), SNAME("EditorIcons")));
  479. preset_buttons[PRESET_HCENTER_WIDE]->set_icon(get_theme_icon(SNAME("ControlAlignHCenterWide"), SNAME("EditorIcons")));
  480. preset_buttons[PRESET_BOTTOM_WIDE]->set_icon(get_theme_icon(SNAME("ControlAlignBottomWide"), SNAME("EditorIcons")));
  481. preset_buttons[PRESET_LEFT_WIDE]->set_icon(get_theme_icon(SNAME("ControlAlignLeftWide"), SNAME("EditorIcons")));
  482. preset_buttons[PRESET_VCENTER_WIDE]->set_icon(get_theme_icon(SNAME("ControlAlignVCenterWide"), SNAME("EditorIcons")));
  483. preset_buttons[PRESET_RIGHT_WIDE]->set_icon(get_theme_icon(SNAME("ControlAlignRightWide"), SNAME("EditorIcons")));
  484. preset_buttons[PRESET_FULL_RECT]->set_icon(get_theme_icon(SNAME("ControlAlignFullRect"), SNAME("EditorIcons")));
  485. } break;
  486. }
  487. }
  488. void AnchorPresetPicker::_bind_methods() {
  489. ADD_SIGNAL(MethodInfo("anchors_preset_selected", PropertyInfo(Variant::INT, "preset")));
  490. }
  491. AnchorPresetPicker::AnchorPresetPicker() {
  492. VBoxContainer *main_vb = memnew(VBoxContainer);
  493. main_vb->add_theme_constant_override("separation", grid_separation);
  494. add_child(main_vb);
  495. HBoxContainer *top_row = memnew(HBoxContainer);
  496. top_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  497. top_row->add_theme_constant_override("separation", grid_separation);
  498. main_vb->add_child(top_row);
  499. _add_row_button(top_row, PRESET_TOP_LEFT, TTR("Top Left"));
  500. _add_row_button(top_row, PRESET_CENTER_TOP, TTR("Center Top"));
  501. _add_row_button(top_row, PRESET_TOP_RIGHT, TTR("Top Right"));
  502. _add_separator(top_row, memnew(VSeparator));
  503. _add_row_button(top_row, PRESET_TOP_WIDE, TTR("Top Wide"));
  504. HBoxContainer *mid_row = memnew(HBoxContainer);
  505. mid_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  506. mid_row->add_theme_constant_override("separation", grid_separation);
  507. main_vb->add_child(mid_row);
  508. _add_row_button(mid_row, PRESET_CENTER_LEFT, TTR("Center Left"));
  509. _add_row_button(mid_row, PRESET_CENTER, TTR("Center"));
  510. _add_row_button(mid_row, PRESET_CENTER_RIGHT, TTR("Center Right"));
  511. _add_separator(mid_row, memnew(VSeparator));
  512. _add_row_button(mid_row, PRESET_HCENTER_WIDE, TTR("HCenter Wide"));
  513. HBoxContainer *bot_row = memnew(HBoxContainer);
  514. bot_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  515. bot_row->add_theme_constant_override("separation", grid_separation);
  516. main_vb->add_child(bot_row);
  517. _add_row_button(bot_row, PRESET_BOTTOM_LEFT, TTR("Bottom Left"));
  518. _add_row_button(bot_row, PRESET_CENTER_BOTTOM, TTR("Center Bottom"));
  519. _add_row_button(bot_row, PRESET_BOTTOM_RIGHT, TTR("Bottom Right"));
  520. _add_separator(bot_row, memnew(VSeparator));
  521. _add_row_button(bot_row, PRESET_BOTTOM_WIDE, TTR("Bottom Wide"));
  522. _add_separator(main_vb, memnew(HSeparator));
  523. HBoxContainer *extra_row = memnew(HBoxContainer);
  524. extra_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  525. extra_row->add_theme_constant_override("separation", grid_separation);
  526. main_vb->add_child(extra_row);
  527. _add_row_button(extra_row, PRESET_LEFT_WIDE, TTR("Left Wide"));
  528. _add_row_button(extra_row, PRESET_VCENTER_WIDE, TTR("VCenter Wide"));
  529. _add_row_button(extra_row, PRESET_RIGHT_WIDE, TTR("Right Wide"));
  530. _add_separator(extra_row, memnew(VSeparator));
  531. _add_row_button(extra_row, PRESET_FULL_RECT, TTR("Full Rect"));
  532. }
  533. void SizeFlagPresetPicker::_preset_button_pressed(const int p_preset) {
  534. int flags = (SizeFlags)p_preset;
  535. if (expand_button->is_pressed()) {
  536. flags |= SIZE_EXPAND;
  537. }
  538. emit_signal("size_flags_selected", flags);
  539. }
  540. void SizeFlagPresetPicker::set_allowed_flags(Vector<SizeFlags> &p_flags) {
  541. preset_buttons[SIZE_SHRINK_BEGIN]->set_disabled(!p_flags.has(SIZE_SHRINK_BEGIN));
  542. preset_buttons[SIZE_SHRINK_CENTER]->set_disabled(!p_flags.has(SIZE_SHRINK_CENTER));
  543. preset_buttons[SIZE_SHRINK_END]->set_disabled(!p_flags.has(SIZE_SHRINK_END));
  544. preset_buttons[SIZE_FILL]->set_disabled(!p_flags.has(SIZE_FILL));
  545. expand_button->set_disabled(!p_flags.has(SIZE_EXPAND));
  546. if (p_flags.has(SIZE_EXPAND)) {
  547. expand_button->set_tooltip_text(TTR("Enable to also set the Expand flag.\nDisable to only set Shrink/Fill flags."));
  548. } else {
  549. expand_button->set_pressed(false);
  550. expand_button->set_tooltip_text(TTR("Some parents of the selected nodes do not support the Expand flag."));
  551. }
  552. }
  553. void SizeFlagPresetPicker::_notification(int p_notification) {
  554. switch (p_notification) {
  555. case NOTIFICATION_ENTER_TREE:
  556. case NOTIFICATION_THEME_CHANGED: {
  557. if (vertical) {
  558. preset_buttons[SIZE_SHRINK_BEGIN]->set_icon(get_theme_icon(SNAME("ControlAlignCenterTop"), SNAME("EditorIcons")));
  559. preset_buttons[SIZE_SHRINK_CENTER]->set_icon(get_theme_icon(SNAME("ControlAlignCenter"), SNAME("EditorIcons")));
  560. preset_buttons[SIZE_SHRINK_END]->set_icon(get_theme_icon(SNAME("ControlAlignCenterBottom"), SNAME("EditorIcons")));
  561. preset_buttons[SIZE_FILL]->set_icon(get_theme_icon(SNAME("ControlAlignVCenterWide"), SNAME("EditorIcons")));
  562. } else {
  563. preset_buttons[SIZE_SHRINK_BEGIN]->set_icon(get_theme_icon(SNAME("ControlAlignCenterLeft"), SNAME("EditorIcons")));
  564. preset_buttons[SIZE_SHRINK_CENTER]->set_icon(get_theme_icon(SNAME("ControlAlignCenter"), SNAME("EditorIcons")));
  565. preset_buttons[SIZE_SHRINK_END]->set_icon(get_theme_icon(SNAME("ControlAlignCenterRight"), SNAME("EditorIcons")));
  566. preset_buttons[SIZE_FILL]->set_icon(get_theme_icon(SNAME("ControlAlignHCenterWide"), SNAME("EditorIcons")));
  567. }
  568. } break;
  569. }
  570. }
  571. void SizeFlagPresetPicker::_bind_methods() {
  572. ADD_SIGNAL(MethodInfo("size_flags_selected", PropertyInfo(Variant::INT, "size_flags")));
  573. }
  574. SizeFlagPresetPicker::SizeFlagPresetPicker(bool p_vertical) {
  575. vertical = p_vertical;
  576. VBoxContainer *main_vb = memnew(VBoxContainer);
  577. add_child(main_vb);
  578. HBoxContainer *main_row = memnew(HBoxContainer);
  579. main_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  580. main_row->add_theme_constant_override("separation", grid_separation);
  581. main_vb->add_child(main_row);
  582. _add_row_button(main_row, SIZE_SHRINK_BEGIN, TTR("Shrink Begin"));
  583. _add_row_button(main_row, SIZE_SHRINK_CENTER, TTR("Shrink Center"));
  584. _add_row_button(main_row, SIZE_SHRINK_END, TTR("Shrink End"));
  585. _add_separator(main_row, memnew(VSeparator));
  586. _add_row_button(main_row, SIZE_FILL, TTR("Fill"));
  587. expand_button = memnew(CheckBox);
  588. expand_button->set_flat(true);
  589. expand_button->set_text(TTR("Align with Expand"));
  590. expand_button->set_tooltip_text(TTR("Enable to also set the Expand flag.\nDisable to only set Shrink/Fill flags."));
  591. main_vb->add_child(expand_button);
  592. }
  593. // Toolbar.
  594. void ControlEditorToolbar::_anchors_preset_selected(int p_preset) {
  595. LayoutPreset preset = (LayoutPreset)p_preset;
  596. List<Node *> selection = editor_selection->get_selected_node_list();
  597. undo_redo->create_action(TTR("Change Anchors, Offsets, Grow Direction"));
  598. for (Node *E : selection) {
  599. Control *control = Object::cast_to<Control>(E);
  600. if (control) {
  601. undo_redo->add_do_property(control, "anchors_preset", preset);
  602. undo_redo->add_undo_method(control, "_edit_set_state", control->_edit_get_state());
  603. }
  604. }
  605. undo_redo->commit_action();
  606. anchors_mode = false;
  607. anchor_mode_button->set_pressed(anchors_mode);
  608. }
  609. void ControlEditorToolbar::_anchors_to_current_ratio() {
  610. List<Node *> selection = editor_selection->get_selected_node_list();
  611. undo_redo->create_action(TTR("Change Anchors, Offsets (Keep Ratio)"));
  612. for (Node *E : selection) {
  613. Control *control = Object::cast_to<Control>(E);
  614. if (control) {
  615. Point2 top_left_anchor = _position_to_anchor(control, Point2());
  616. Point2 bottom_right_anchor = _position_to_anchor(control, control->get_size());
  617. undo_redo->add_do_method(control, "set_anchor", SIDE_LEFT, top_left_anchor.x, false, true);
  618. undo_redo->add_do_method(control, "set_anchor", SIDE_RIGHT, bottom_right_anchor.x, false, true);
  619. undo_redo->add_do_method(control, "set_anchor", SIDE_TOP, top_left_anchor.y, false, true);
  620. undo_redo->add_do_method(control, "set_anchor", SIDE_BOTTOM, bottom_right_anchor.y, false, true);
  621. undo_redo->add_do_method(control, "set_meta", "_edit_use_anchors_", true);
  622. const bool use_anchors = control->get_meta("_edit_use_anchors_", false);
  623. undo_redo->add_undo_method(control, "_edit_set_state", control->_edit_get_state());
  624. if (use_anchors) {
  625. undo_redo->add_undo_method(control, "set_meta", "_edit_use_anchors_", true);
  626. } else {
  627. undo_redo->add_undo_method(control, "remove_meta", "_edit_use_anchors_");
  628. }
  629. anchors_mode = true;
  630. anchor_mode_button->set_pressed(anchors_mode);
  631. }
  632. }
  633. undo_redo->commit_action();
  634. }
  635. void ControlEditorToolbar::_anchor_mode_toggled(bool p_status) {
  636. List<Control *> selection = _get_edited_controls();
  637. for (Control *E : selection) {
  638. if (Object::cast_to<Container>(E->get_parent())) {
  639. continue;
  640. }
  641. if (p_status) {
  642. E->set_meta("_edit_use_anchors_", true);
  643. } else {
  644. E->remove_meta("_edit_use_anchors_");
  645. }
  646. }
  647. anchors_mode = p_status;
  648. CanvasItemEditor::get_singleton()->update_viewport();
  649. }
  650. void ControlEditorToolbar::_container_flags_selected(int p_flags, bool p_vertical) {
  651. List<Node *> selection = editor_selection->get_selected_node_list();
  652. if (p_vertical) {
  653. undo_redo->create_action(TTR("Change Vertical Size Flags"));
  654. } else {
  655. undo_redo->create_action(TTR("Change Horizontal Size Flags"));
  656. }
  657. for (Node *E : selection) {
  658. Control *control = Object::cast_to<Control>(E);
  659. if (control) {
  660. if (p_vertical) {
  661. undo_redo->add_do_method(control, "set_v_size_flags", p_flags);
  662. } else {
  663. undo_redo->add_do_method(control, "set_h_size_flags", p_flags);
  664. }
  665. undo_redo->add_undo_method(control, "_edit_set_state", control->_edit_get_state());
  666. }
  667. }
  668. undo_redo->commit_action();
  669. }
  670. Vector2 ControlEditorToolbar::_anchor_to_position(const Control *p_control, Vector2 anchor) {
  671. ERR_FAIL_COND_V(!p_control, Vector2());
  672. Transform2D parent_transform = p_control->get_transform().affine_inverse();
  673. Rect2 parent_rect = p_control->get_parent_anchorable_rect();
  674. if (p_control->is_layout_rtl()) {
  675. return parent_transform.xform(parent_rect.position + Vector2(parent_rect.size.x - parent_rect.size.x * anchor.x, parent_rect.size.y * anchor.y));
  676. } else {
  677. return parent_transform.xform(parent_rect.position + Vector2(parent_rect.size.x * anchor.x, parent_rect.size.y * anchor.y));
  678. }
  679. }
  680. Vector2 ControlEditorToolbar::_position_to_anchor(const Control *p_control, Vector2 position) {
  681. ERR_FAIL_COND_V(!p_control, Vector2());
  682. Rect2 parent_rect = p_control->get_parent_anchorable_rect();
  683. Vector2 output = Vector2();
  684. if (p_control->is_layout_rtl()) {
  685. 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;
  686. } else {
  687. output.x = (parent_rect.size.x == 0) ? 0.0 : (p_control->get_transform().xform(position).x - parent_rect.position.x) / parent_rect.size.x;
  688. }
  689. output.y = (parent_rect.size.y == 0) ? 0.0 : (p_control->get_transform().xform(position).y - parent_rect.position.y) / parent_rect.size.y;
  690. return output;
  691. }
  692. bool ControlEditorToolbar::_is_node_locked(const Node *p_node) {
  693. return p_node->get_meta("_edit_lock_", false);
  694. }
  695. List<Control *> ControlEditorToolbar::_get_edited_controls() {
  696. List<Control *> selection;
  697. for (const KeyValue<Node *, Object *> &E : editor_selection->get_selection()) {
  698. Control *control = Object::cast_to<Control>(E.key);
  699. if (control && control->is_visible_in_tree() && control->get_viewport() == EditorNode::get_singleton()->get_scene_root() && !_is_node_locked(control)) {
  700. selection.push_back(control);
  701. }
  702. }
  703. return selection;
  704. }
  705. void ControlEditorToolbar::_selection_changed() {
  706. // Update toolbar visibility.
  707. bool has_controls = false;
  708. bool has_control_parents = false;
  709. bool has_container_parents = false;
  710. // Also update which size flags can be configured for the selected nodes.
  711. Vector<SizeFlags> allowed_h_flags = {
  712. SIZE_SHRINK_BEGIN,
  713. SIZE_SHRINK_CENTER,
  714. SIZE_SHRINK_END,
  715. SIZE_FILL,
  716. SIZE_EXPAND,
  717. };
  718. Vector<SizeFlags> allowed_v_flags = {
  719. SIZE_SHRINK_BEGIN,
  720. SIZE_SHRINK_CENTER,
  721. SIZE_SHRINK_END,
  722. SIZE_FILL,
  723. SIZE_EXPAND,
  724. };
  725. for (const KeyValue<Node *, Object *> &E : editor_selection->get_selection()) {
  726. Control *control = Object::cast_to<Control>(E.key);
  727. if (!control) {
  728. continue;
  729. }
  730. has_controls = true;
  731. if (Object::cast_to<Control>(control->get_parent())) {
  732. has_control_parents = true;
  733. }
  734. if (Object::cast_to<Container>(control->get_parent())) {
  735. has_container_parents = true;
  736. Container *parent_container = Object::cast_to<Container>(control->get_parent());
  737. Vector<int> container_h_flags = parent_container->get_allowed_size_flags_horizontal();
  738. Vector<SizeFlags> tmp_flags = allowed_h_flags.duplicate();
  739. for (int i = 0; i < allowed_h_flags.size(); i++) {
  740. if (!container_h_flags.has((int)allowed_h_flags[i])) {
  741. tmp_flags.erase(allowed_h_flags[i]);
  742. }
  743. }
  744. allowed_h_flags = tmp_flags;
  745. Vector<int> container_v_flags = parent_container->get_allowed_size_flags_vertical();
  746. tmp_flags = allowed_v_flags.duplicate();
  747. for (int i = 0; i < allowed_v_flags.size(); i++) {
  748. if (!container_v_flags.has((int)allowed_v_flags[i])) {
  749. tmp_flags.erase(allowed_v_flags[i]);
  750. }
  751. }
  752. allowed_v_flags = tmp_flags;
  753. }
  754. }
  755. // Set general toolbar visibility.
  756. set_visible(has_controls);
  757. // Set anchor tools visibility.
  758. if (has_controls && (!has_control_parents || !has_container_parents)) {
  759. anchors_button->set_visible(true);
  760. anchor_mode_button->set_visible(true);
  761. // Update anchor mode.
  762. int nb_valid_controls = 0;
  763. int nb_anchors_mode = 0;
  764. List<Node *> selection = editor_selection->get_selected_node_list();
  765. for (Node *E : selection) {
  766. Control *control = Object::cast_to<Control>(E);
  767. if (!control) {
  768. continue;
  769. }
  770. if (Object::cast_to<Container>(control->get_parent())) {
  771. continue;
  772. }
  773. nb_valid_controls++;
  774. if (control->get_meta("_edit_use_anchors_", false)) {
  775. nb_anchors_mode++;
  776. }
  777. }
  778. anchors_mode = (nb_valid_controls == nb_anchors_mode);
  779. anchor_mode_button->set_pressed(anchors_mode);
  780. } else {
  781. anchors_button->set_visible(false);
  782. anchor_mode_button->set_visible(false);
  783. anchor_mode_button->set_pressed(false);
  784. }
  785. // Set container tools visibility.
  786. if (has_controls && (!has_control_parents || has_container_parents)) {
  787. containers_button->set_visible(true);
  788. // Update allowed size flags.
  789. if (has_container_parents) {
  790. container_h_picker->set_allowed_flags(allowed_h_flags);
  791. container_v_picker->set_allowed_flags(allowed_v_flags);
  792. } else {
  793. Vector<SizeFlags> allowed_all_flags = {
  794. SIZE_SHRINK_BEGIN,
  795. SIZE_SHRINK_CENTER,
  796. SIZE_SHRINK_END,
  797. SIZE_FILL,
  798. SIZE_EXPAND,
  799. };
  800. container_h_picker->set_allowed_flags(allowed_all_flags);
  801. container_v_picker->set_allowed_flags(allowed_all_flags);
  802. }
  803. } else {
  804. containers_button->set_visible(false);
  805. }
  806. }
  807. void ControlEditorToolbar::_notification(int p_what) {
  808. switch (p_what) {
  809. case NOTIFICATION_ENTER_TREE:
  810. case NOTIFICATION_THEME_CHANGED: {
  811. anchors_button->set_icon(get_theme_icon(SNAME("ControlLayout"), SNAME("EditorIcons")));
  812. anchor_mode_button->set_icon(get_theme_icon(SNAME("Anchor"), SNAME("EditorIcons")));
  813. containers_button->set_icon(get_theme_icon(SNAME("ContainerLayout"), SNAME("EditorIcons")));
  814. } break;
  815. }
  816. }
  817. ControlEditorToolbar::ControlEditorToolbar() {
  818. add_child(memnew(VSeparator));
  819. // Anchor and offset tools.
  820. anchors_button = memnew(ControlEditorPopupButton);
  821. anchors_button->set_tooltip_text(TTR("Presets for the anchor and offset values of a Control node."));
  822. add_child(anchors_button);
  823. Label *anchors_label = memnew(Label);
  824. anchors_label->set_text(TTR("Anchor preset"));
  825. anchors_button->get_popup_hbox()->add_child(anchors_label);
  826. AnchorPresetPicker *anchors_picker = memnew(AnchorPresetPicker);
  827. anchors_picker->set_h_size_flags(SIZE_SHRINK_CENTER);
  828. anchors_button->get_popup_hbox()->add_child(anchors_picker);
  829. anchors_picker->connect("anchors_preset_selected", callable_mp(this, &ControlEditorToolbar::_anchors_preset_selected));
  830. anchors_button->get_popup_hbox()->add_child(memnew(HSeparator));
  831. Button *keep_ratio_button = memnew(Button);
  832. keep_ratio_button->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  833. keep_ratio_button->set_text(TTR("Set to Current Ratio"));
  834. keep_ratio_button->set_tooltip_text(TTR("Adjust anchors and offsets to match the current rect size."));
  835. anchors_button->get_popup_hbox()->add_child(keep_ratio_button);
  836. keep_ratio_button->connect("pressed", callable_mp(this, &ControlEditorToolbar::_anchors_to_current_ratio));
  837. anchor_mode_button = memnew(Button);
  838. anchor_mode_button->set_flat(true);
  839. anchor_mode_button->set_toggle_mode(true);
  840. anchor_mode_button->set_tooltip_text(TTR("When active, moving Control nodes changes their anchors instead of their offsets."));
  841. add_child(anchor_mode_button);
  842. anchor_mode_button->connect("toggled", callable_mp(this, &ControlEditorToolbar::_anchor_mode_toggled));
  843. // Container tools.
  844. containers_button = memnew(ControlEditorPopupButton);
  845. containers_button->set_tooltip_text(TTR("Sizing settings for children of a Container node."));
  846. add_child(containers_button);
  847. Label *container_h_label = memnew(Label);
  848. container_h_label->set_text(TTR("Horizontal alignment"));
  849. containers_button->get_popup_hbox()->add_child(container_h_label);
  850. container_h_picker = memnew(SizeFlagPresetPicker(false));
  851. containers_button->get_popup_hbox()->add_child(container_h_picker);
  852. container_h_picker->connect("size_flags_selected", callable_mp(this, &ControlEditorToolbar::_container_flags_selected).bind(false));
  853. containers_button->get_popup_hbox()->add_child(memnew(HSeparator));
  854. Label *container_v_label = memnew(Label);
  855. container_v_label->set_text(TTR("Vertical alignment"));
  856. containers_button->get_popup_hbox()->add_child(container_v_label);
  857. container_v_picker = memnew(SizeFlagPresetPicker(true));
  858. containers_button->get_popup_hbox()->add_child(container_v_picker);
  859. container_v_picker->connect("size_flags_selected", callable_mp(this, &ControlEditorToolbar::_container_flags_selected).bind(true));
  860. // Editor connections.
  861. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  862. editor_selection = EditorNode::get_singleton()->get_editor_selection();
  863. editor_selection->add_editor_plugin(this);
  864. editor_selection->connect("selection_changed", callable_mp(this, &ControlEditorToolbar::_selection_changed));
  865. singleton = this;
  866. }
  867. ControlEditorToolbar *ControlEditorToolbar::singleton = nullptr;
  868. // Editor plugin.
  869. ControlEditorPlugin::ControlEditorPlugin() {
  870. toolbar = memnew(ControlEditorToolbar);
  871. toolbar->hide();
  872. add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar);
  873. Ref<EditorInspectorPluginControl> plugin;
  874. plugin.instantiate();
  875. add_inspector_plugin(plugin);
  876. }