control_editor_plugin.cpp 38 KB

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