control_editor_plugin.cpp 40 KB

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