control_editor_plugin.cpp 37 KB

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