control_editor_plugin.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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_string_names.h"
  33. #include "editor/editor_undo_redo_manager.h"
  34. #include "editor/plugins/canvas_item_editor_plugin.h"
  35. #include "editor/themes/editor_scale.h"
  36. #include "scene/gui/check_button.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(SceneStringName(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. const 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(SceneStringName(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(SceneStringName(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. StringName wide_preset_icon = SNAME("ControlAlignHCenterWide");
  307. StringName begin_preset_icon = SNAME("ControlAlignCenterLeft");
  308. StringName 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(SceneStringName(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(SceneStringName(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_category(Object *p_object, const String &p_category) {
  353. inside_control_category = p_category == "Control";
  354. }
  355. void EditorInspectorPluginControl::parse_group(Object *p_object, const String &p_group) {
  356. if (!inside_control_category) {
  357. return;
  358. }
  359. Control *control = Object::cast_to<Control>(p_object);
  360. if (!control || p_group != "Layout") {
  361. return;
  362. }
  363. ControlPositioningWarning *pos_warning = memnew(ControlPositioningWarning);
  364. pos_warning->set_control(control);
  365. add_custom_control(pos_warning);
  366. }
  367. 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) {
  368. Control *control = Object::cast_to<Control>(p_object);
  369. if (!control) {
  370. return false;
  371. }
  372. if (p_path == "anchors_preset") {
  373. EditorPropertyAnchorsPreset *prop_editor = memnew(EditorPropertyAnchorsPreset);
  374. Vector<String> options = p_hint_text.split(",");
  375. prop_editor->setup(options);
  376. add_property_editor(p_path, prop_editor);
  377. return true;
  378. }
  379. if (p_path == "size_flags_horizontal" || p_path == "size_flags_vertical") {
  380. EditorPropertySizeFlags *prop_editor = memnew(EditorPropertySizeFlags);
  381. Vector<String> options;
  382. if (!p_hint_text.is_empty()) {
  383. options = p_hint_text.split(",");
  384. }
  385. prop_editor->setup(options, p_path == "size_flags_vertical");
  386. add_property_editor(p_path, prop_editor);
  387. return true;
  388. }
  389. return false;
  390. }
  391. // Toolbars controls.
  392. Size2 ControlEditorPopupButton::get_minimum_size() const {
  393. Vector2 base_size = Vector2(26, 26) * EDSCALE;
  394. if (arrow_icon.is_null()) {
  395. return base_size;
  396. }
  397. Vector2 final_size;
  398. final_size.x = base_size.x + arrow_icon->get_width();
  399. final_size.y = MAX(base_size.y, arrow_icon->get_height());
  400. return final_size;
  401. }
  402. void ControlEditorPopupButton::toggled(bool p_pressed) {
  403. if (!p_pressed) {
  404. return;
  405. }
  406. Size2 size = get_size() * get_viewport()->get_canvas_transform().get_scale();
  407. popup_panel->set_size(Size2(size.width, 0));
  408. Point2 gp = get_screen_position();
  409. gp.y += size.y;
  410. if (is_layout_rtl()) {
  411. gp.x += size.width - popup_panel->get_size().width;
  412. }
  413. popup_panel->set_position(gp);
  414. popup_panel->popup();
  415. }
  416. void ControlEditorPopupButton::_popup_visibility_changed(bool p_visible) {
  417. set_pressed(p_visible);
  418. }
  419. void ControlEditorPopupButton::_notification(int p_what) {
  420. switch (p_what) {
  421. case NOTIFICATION_ENTER_TREE:
  422. case NOTIFICATION_THEME_CHANGED: {
  423. arrow_icon = get_theme_icon("select_arrow", "Tree");
  424. } break;
  425. case NOTIFICATION_DRAW: {
  426. if (arrow_icon.is_valid()) {
  427. Vector2 arrow_pos = Point2(26, 0) * EDSCALE;
  428. if (is_layout_rtl()) {
  429. arrow_pos.x = get_size().x - arrow_pos.x - arrow_icon->get_width();
  430. }
  431. arrow_pos.y = get_size().y / 2 - arrow_icon->get_height() / 2;
  432. draw_texture(arrow_icon, arrow_pos);
  433. }
  434. } break;
  435. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
  436. popup_panel->set_layout_direction((Window::LayoutDirection)get_layout_direction());
  437. } break;
  438. case NOTIFICATION_VISIBILITY_CHANGED: {
  439. if (!is_visible_in_tree()) {
  440. popup_panel->hide();
  441. }
  442. } break;
  443. }
  444. }
  445. ControlEditorPopupButton::ControlEditorPopupButton() {
  446. set_theme_type_variation("FlatButton");
  447. set_toggle_mode(true);
  448. set_focus_mode(FOCUS_NONE);
  449. popup_panel = memnew(PopupPanel);
  450. add_child(popup_panel);
  451. popup_panel->connect("about_to_popup", callable_mp(this, &ControlEditorPopupButton::_popup_visibility_changed).bind(true));
  452. popup_panel->connect("popup_hide", callable_mp(this, &ControlEditorPopupButton::_popup_visibility_changed).bind(false));
  453. popup_vbox = memnew(VBoxContainer);
  454. popup_panel->add_child(popup_vbox);
  455. }
  456. void ControlEditorPresetPicker::_add_row_button(HBoxContainer *p_row, const int p_preset, const String &p_name) {
  457. ERR_FAIL_COND(preset_buttons.has(p_preset));
  458. Button *b = memnew(Button);
  459. b->set_custom_minimum_size(Size2i(36, 36) * EDSCALE);
  460. b->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  461. b->set_tooltip_text(p_name);
  462. b->set_flat(true);
  463. p_row->add_child(b);
  464. b->connect(SceneStringName(pressed), callable_mp(this, &ControlEditorPresetPicker::_preset_button_pressed).bind(p_preset));
  465. preset_buttons[p_preset] = b;
  466. }
  467. void ControlEditorPresetPicker::_add_separator(BoxContainer *p_box, Separator *p_separator) {
  468. p_separator->add_theme_constant_override("separation", grid_separation);
  469. p_separator->set_custom_minimum_size(Size2i(1, 1));
  470. p_box->add_child(p_separator);
  471. }
  472. void AnchorPresetPicker::_preset_button_pressed(const int p_preset) {
  473. emit_signal("anchors_preset_selected", p_preset);
  474. }
  475. void AnchorPresetPicker::_notification(int p_notification) {
  476. switch (p_notification) {
  477. case NOTIFICATION_ENTER_TREE:
  478. case NOTIFICATION_THEME_CHANGED: {
  479. preset_buttons[PRESET_TOP_LEFT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignTopLeft")));
  480. preset_buttons[PRESET_CENTER_TOP]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterTop")));
  481. preset_buttons[PRESET_TOP_RIGHT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignTopRight")));
  482. preset_buttons[PRESET_CENTER_LEFT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterLeft")));
  483. preset_buttons[PRESET_CENTER]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenter")));
  484. preset_buttons[PRESET_CENTER_RIGHT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterRight")));
  485. preset_buttons[PRESET_BOTTOM_LEFT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignBottomLeft")));
  486. preset_buttons[PRESET_CENTER_BOTTOM]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterBottom")));
  487. preset_buttons[PRESET_BOTTOM_RIGHT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignBottomRight")));
  488. preset_buttons[PRESET_TOP_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignTopWide")));
  489. preset_buttons[PRESET_HCENTER_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignHCenterWide")));
  490. preset_buttons[PRESET_BOTTOM_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignBottomWide")));
  491. preset_buttons[PRESET_LEFT_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignLeftWide")));
  492. preset_buttons[PRESET_VCENTER_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignVCenterWide")));
  493. preset_buttons[PRESET_RIGHT_WIDE]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignRightWide")));
  494. preset_buttons[PRESET_FULL_RECT]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignFullRect")));
  495. } break;
  496. }
  497. }
  498. void AnchorPresetPicker::_bind_methods() {
  499. ADD_SIGNAL(MethodInfo("anchors_preset_selected", PropertyInfo(Variant::INT, "preset")));
  500. }
  501. AnchorPresetPicker::AnchorPresetPicker() {
  502. VBoxContainer *main_vb = memnew(VBoxContainer);
  503. main_vb->add_theme_constant_override("separation", grid_separation);
  504. add_child(main_vb);
  505. HBoxContainer *top_row = memnew(HBoxContainer);
  506. top_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  507. top_row->add_theme_constant_override("separation", grid_separation);
  508. main_vb->add_child(top_row);
  509. _add_row_button(top_row, PRESET_TOP_LEFT, TTR("Top Left"));
  510. _add_row_button(top_row, PRESET_CENTER_TOP, TTR("Center Top"));
  511. _add_row_button(top_row, PRESET_TOP_RIGHT, TTR("Top Right"));
  512. _add_separator(top_row, memnew(VSeparator));
  513. _add_row_button(top_row, PRESET_TOP_WIDE, TTR("Top Wide"));
  514. HBoxContainer *mid_row = memnew(HBoxContainer);
  515. mid_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  516. mid_row->add_theme_constant_override("separation", grid_separation);
  517. main_vb->add_child(mid_row);
  518. _add_row_button(mid_row, PRESET_CENTER_LEFT, TTR("Center Left"));
  519. _add_row_button(mid_row, PRESET_CENTER, TTR("Center"));
  520. _add_row_button(mid_row, PRESET_CENTER_RIGHT, TTR("Center Right"));
  521. _add_separator(mid_row, memnew(VSeparator));
  522. _add_row_button(mid_row, PRESET_HCENTER_WIDE, TTR("HCenter Wide"));
  523. HBoxContainer *bot_row = memnew(HBoxContainer);
  524. bot_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  525. bot_row->add_theme_constant_override("separation", grid_separation);
  526. main_vb->add_child(bot_row);
  527. _add_row_button(bot_row, PRESET_BOTTOM_LEFT, TTR("Bottom Left"));
  528. _add_row_button(bot_row, PRESET_CENTER_BOTTOM, TTR("Center Bottom"));
  529. _add_row_button(bot_row, PRESET_BOTTOM_RIGHT, TTR("Bottom Right"));
  530. _add_separator(bot_row, memnew(VSeparator));
  531. _add_row_button(bot_row, PRESET_BOTTOM_WIDE, TTR("Bottom Wide"));
  532. _add_separator(main_vb, memnew(HSeparator));
  533. HBoxContainer *extra_row = memnew(HBoxContainer);
  534. extra_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  535. extra_row->add_theme_constant_override("separation", grid_separation);
  536. main_vb->add_child(extra_row);
  537. _add_row_button(extra_row, PRESET_LEFT_WIDE, TTR("Left Wide"));
  538. _add_row_button(extra_row, PRESET_VCENTER_WIDE, TTR("VCenter Wide"));
  539. _add_row_button(extra_row, PRESET_RIGHT_WIDE, TTR("Right Wide"));
  540. _add_separator(extra_row, memnew(VSeparator));
  541. _add_row_button(extra_row, PRESET_FULL_RECT, TTR("Full Rect"));
  542. }
  543. void SizeFlagPresetPicker::_preset_button_pressed(const int p_preset) {
  544. int flags = (SizeFlags)p_preset;
  545. if (expand_button->is_pressed()) {
  546. flags |= SIZE_EXPAND;
  547. }
  548. emit_signal("size_flags_selected", flags);
  549. }
  550. void SizeFlagPresetPicker::_expand_button_pressed() {
  551. emit_signal("expand_flag_toggled", expand_button->is_pressed());
  552. }
  553. void SizeFlagPresetPicker::set_allowed_flags(Vector<SizeFlags> &p_flags) {
  554. preset_buttons[SIZE_SHRINK_BEGIN]->set_disabled(!p_flags.has(SIZE_SHRINK_BEGIN));
  555. preset_buttons[SIZE_SHRINK_CENTER]->set_disabled(!p_flags.has(SIZE_SHRINK_CENTER));
  556. preset_buttons[SIZE_SHRINK_END]->set_disabled(!p_flags.has(SIZE_SHRINK_END));
  557. preset_buttons[SIZE_FILL]->set_disabled(!p_flags.has(SIZE_FILL));
  558. expand_button->set_disabled(!p_flags.has(SIZE_EXPAND));
  559. if (p_flags.has(SIZE_EXPAND)) {
  560. expand_button->set_tooltip_text(TTR("Enable to also set the Expand flag.\nDisable to only set Shrink/Fill flags."));
  561. } else {
  562. expand_button->set_pressed(false);
  563. expand_button->set_tooltip_text(TTR("Some parents of the selected nodes do not support the Expand flag."));
  564. }
  565. }
  566. void SizeFlagPresetPicker::set_expand_flag(bool p_expand) {
  567. expand_button->set_pressed(p_expand);
  568. }
  569. void SizeFlagPresetPicker::_notification(int p_notification) {
  570. switch (p_notification) {
  571. case NOTIFICATION_ENTER_TREE:
  572. case NOTIFICATION_THEME_CHANGED: {
  573. if (vertical) {
  574. preset_buttons[SIZE_SHRINK_BEGIN]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterTop")));
  575. preset_buttons[SIZE_SHRINK_CENTER]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenter")));
  576. preset_buttons[SIZE_SHRINK_END]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterBottom")));
  577. preset_buttons[SIZE_FILL]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignVCenterWide")));
  578. } else {
  579. preset_buttons[SIZE_SHRINK_BEGIN]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterLeft")));
  580. preset_buttons[SIZE_SHRINK_CENTER]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenter")));
  581. preset_buttons[SIZE_SHRINK_END]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignCenterRight")));
  582. preset_buttons[SIZE_FILL]->set_button_icon(get_editor_theme_icon(SNAME("ControlAlignHCenterWide")));
  583. }
  584. } break;
  585. }
  586. }
  587. void SizeFlagPresetPicker::_bind_methods() {
  588. ADD_SIGNAL(MethodInfo("size_flags_selected", PropertyInfo(Variant::INT, "size_flags")));
  589. ADD_SIGNAL(MethodInfo("expand_flag_toggled", PropertyInfo(Variant::BOOL, "expand_flag")));
  590. }
  591. SizeFlagPresetPicker::SizeFlagPresetPicker(bool p_vertical) {
  592. vertical = p_vertical;
  593. VBoxContainer *main_vb = memnew(VBoxContainer);
  594. add_child(main_vb);
  595. HBoxContainer *main_row = memnew(HBoxContainer);
  596. main_row->set_alignment(BoxContainer::ALIGNMENT_CENTER);
  597. main_row->add_theme_constant_override("separation", grid_separation);
  598. main_vb->add_child(main_row);
  599. _add_row_button(main_row, SIZE_SHRINK_BEGIN, TTR("Shrink Begin"));
  600. _add_row_button(main_row, SIZE_SHRINK_CENTER, TTR("Shrink Center"));
  601. _add_row_button(main_row, SIZE_SHRINK_END, TTR("Shrink End"));
  602. _add_separator(main_row, memnew(VSeparator));
  603. _add_row_button(main_row, SIZE_FILL, TTR("Fill"));
  604. expand_button = memnew(CheckButton);
  605. expand_button->set_flat(true);
  606. expand_button->set_text(TTR("Expand"));
  607. expand_button->set_tooltip_text(TTR("Enable to also set the Expand flag.\nDisable to only set Shrink/Fill flags."));
  608. expand_button->connect(SceneStringName(pressed), callable_mp(this, &SizeFlagPresetPicker::_expand_button_pressed));
  609. main_vb->add_child(expand_button);
  610. }
  611. // Toolbar.
  612. void ControlEditorToolbar::_anchors_preset_selected(int p_preset) {
  613. LayoutPreset preset = (LayoutPreset)p_preset;
  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, Grow Direction"));
  617. for (Node *E : selection) {
  618. Control *control = Object::cast_to<Control>(E);
  619. if (control) {
  620. undo_redo->add_do_property(control, "layout_mode", LayoutMode::LAYOUT_MODE_ANCHORS);
  621. undo_redo->add_do_property(control, "anchors_preset", preset);
  622. undo_redo->add_undo_method(control, "_edit_set_state", control->_edit_get_state());
  623. }
  624. }
  625. undo_redo->commit_action();
  626. anchors_mode = false;
  627. anchor_mode_button->set_pressed(anchors_mode);
  628. }
  629. void ControlEditorToolbar::_anchors_to_current_ratio() {
  630. List<Node *> selection = editor_selection->get_selected_node_list();
  631. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  632. undo_redo->create_action(TTR("Change Anchors, Offsets (Keep Ratio)"));
  633. for (Node *E : selection) {
  634. Control *control = Object::cast_to<Control>(E);
  635. if (control) {
  636. Point2 top_left_anchor = _position_to_anchor(control, Point2());
  637. Point2 bottom_right_anchor = _position_to_anchor(control, control->get_size());
  638. undo_redo->add_do_method(control, "set_anchor", SIDE_LEFT, top_left_anchor.x, false, true);
  639. undo_redo->add_do_method(control, "set_anchor", SIDE_RIGHT, bottom_right_anchor.x, false, true);
  640. undo_redo->add_do_method(control, "set_anchor", SIDE_TOP, top_left_anchor.y, false, true);
  641. undo_redo->add_do_method(control, "set_anchor", SIDE_BOTTOM, bottom_right_anchor.y, false, true);
  642. undo_redo->add_do_method(control, "set_meta", "_edit_use_anchors_", true);
  643. const bool use_anchors = control->get_meta("_edit_use_anchors_", false);
  644. undo_redo->add_undo_method(control, "_edit_set_state", control->_edit_get_state());
  645. if (use_anchors) {
  646. undo_redo->add_undo_method(control, "set_meta", "_edit_use_anchors_", true);
  647. } else {
  648. undo_redo->add_undo_method(control, "remove_meta", "_edit_use_anchors_");
  649. }
  650. anchors_mode = true;
  651. anchor_mode_button->set_pressed(anchors_mode);
  652. }
  653. }
  654. undo_redo->commit_action();
  655. }
  656. void ControlEditorToolbar::_anchor_mode_toggled(bool p_status) {
  657. List<Control *> selection = _get_edited_controls();
  658. for (Control *E : selection) {
  659. if (Object::cast_to<Container>(E->get_parent())) {
  660. continue;
  661. }
  662. if (p_status) {
  663. E->set_meta("_edit_use_anchors_", true);
  664. } else {
  665. E->remove_meta("_edit_use_anchors_");
  666. }
  667. }
  668. anchors_mode = p_status;
  669. CanvasItemEditor::get_singleton()->update_viewport();
  670. }
  671. void ControlEditorToolbar::_container_flags_selected(int p_flags, bool p_vertical) {
  672. List<Node *> selection = editor_selection->get_selected_node_list();
  673. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  674. if (p_vertical) {
  675. undo_redo->create_action(TTR("Change Vertical Size Flags"));
  676. } else {
  677. undo_redo->create_action(TTR("Change Horizontal Size Flags"));
  678. }
  679. for (Node *E : selection) {
  680. Control *control = Object::cast_to<Control>(E);
  681. if (control) {
  682. int old_flags = p_vertical ? control->get_v_size_flags() : control->get_h_size_flags();
  683. if (p_vertical) {
  684. undo_redo->add_do_method(control, "set_v_size_flags", p_flags);
  685. undo_redo->add_undo_method(control, "set_v_size_flags", old_flags);
  686. } else {
  687. undo_redo->add_do_method(control, "set_h_size_flags", p_flags);
  688. undo_redo->add_undo_method(control, "set_h_size_flags", old_flags);
  689. }
  690. }
  691. }
  692. undo_redo->commit_action();
  693. }
  694. void ControlEditorToolbar::_expand_flag_toggled(bool p_expand, bool p_vertical) {
  695. List<Node *> selection = editor_selection->get_selected_node_list();
  696. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  697. if (p_vertical) {
  698. undo_redo->create_action(TTR("Change Vertical Expand Flag"));
  699. } else {
  700. undo_redo->create_action(TTR("Change Horizontal Expand Flag"));
  701. }
  702. for (Node *E : selection) {
  703. Control *control = Object::cast_to<Control>(E);
  704. if (control) {
  705. int old_flags = p_vertical ? control->get_v_size_flags() : control->get_h_size_flags();
  706. int new_flags = old_flags;
  707. if (p_expand) {
  708. new_flags |= Control::SIZE_EXPAND;
  709. } else {
  710. new_flags &= ~Control::SIZE_EXPAND;
  711. }
  712. if (p_vertical) {
  713. undo_redo->add_do_method(control, "set_v_size_flags", new_flags);
  714. undo_redo->add_undo_method(control, "set_v_size_flags", old_flags);
  715. } else {
  716. undo_redo->add_do_method(control, "set_h_size_flags", new_flags);
  717. undo_redo->add_undo_method(control, "set_h_size_flags", old_flags);
  718. }
  719. }
  720. }
  721. undo_redo->commit_action();
  722. }
  723. Vector2 ControlEditorToolbar::_position_to_anchor(const Control *p_control, Vector2 position) {
  724. ERR_FAIL_NULL_V(p_control, Vector2());
  725. Rect2 parent_rect = p_control->get_parent_anchorable_rect();
  726. Vector2 output;
  727. if (p_control->is_layout_rtl()) {
  728. 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;
  729. } else {
  730. output.x = (parent_rect.size.x == 0) ? 0.0 : (p_control->get_transform().xform(position).x - parent_rect.position.x) / parent_rect.size.x;
  731. }
  732. output.y = (parent_rect.size.y == 0) ? 0.0 : (p_control->get_transform().xform(position).y - parent_rect.position.y) / parent_rect.size.y;
  733. return output;
  734. }
  735. bool ControlEditorToolbar::_is_node_locked(const Node *p_node) {
  736. return p_node->get_meta("_edit_lock_", false);
  737. }
  738. List<Control *> ControlEditorToolbar::_get_edited_controls() {
  739. List<Control *> selection;
  740. for (const KeyValue<Node *, Object *> &E : editor_selection->get_selection()) {
  741. Control *control = Object::cast_to<Control>(E.key);
  742. if (control && control->is_visible_in_tree() && control->get_viewport() == EditorNode::get_singleton()->get_scene_root() && !_is_node_locked(control)) {
  743. selection.push_back(control);
  744. }
  745. }
  746. return selection;
  747. }
  748. void ControlEditorToolbar::_selection_changed() {
  749. // Update toolbar visibility.
  750. bool has_controls = false;
  751. bool has_control_parents = false;
  752. bool has_container_parents = false;
  753. // Also update which size flags can be configured for the selected nodes.
  754. Vector<SizeFlags> allowed_h_flags = {
  755. SIZE_SHRINK_BEGIN,
  756. SIZE_SHRINK_CENTER,
  757. SIZE_SHRINK_END,
  758. SIZE_FILL,
  759. SIZE_EXPAND,
  760. };
  761. Vector<SizeFlags> allowed_v_flags = {
  762. SIZE_SHRINK_BEGIN,
  763. SIZE_SHRINK_CENTER,
  764. SIZE_SHRINK_END,
  765. SIZE_FILL,
  766. SIZE_EXPAND,
  767. };
  768. for (const KeyValue<Node *, Object *> &E : editor_selection->get_selection()) {
  769. Control *control = Object::cast_to<Control>(E.key);
  770. if (!control) {
  771. continue;
  772. }
  773. has_controls = true;
  774. if (Object::cast_to<Control>(control->get_parent())) {
  775. has_control_parents = true;
  776. }
  777. if (Object::cast_to<Container>(control->get_parent())) {
  778. has_container_parents = true;
  779. Container *parent_container = Object::cast_to<Container>(control->get_parent());
  780. Vector<int> container_h_flags = parent_container->get_allowed_size_flags_horizontal();
  781. Vector<SizeFlags> tmp_flags = allowed_h_flags.duplicate();
  782. for (int i = 0; i < allowed_h_flags.size(); i++) {
  783. if (!container_h_flags.has((int)allowed_h_flags[i])) {
  784. tmp_flags.erase(allowed_h_flags[i]);
  785. }
  786. }
  787. allowed_h_flags = tmp_flags;
  788. Vector<int> container_v_flags = parent_container->get_allowed_size_flags_vertical();
  789. tmp_flags = allowed_v_flags.duplicate();
  790. for (int i = 0; i < allowed_v_flags.size(); i++) {
  791. if (!container_v_flags.has((int)allowed_v_flags[i])) {
  792. tmp_flags.erase(allowed_v_flags[i]);
  793. }
  794. }
  795. allowed_v_flags = tmp_flags;
  796. }
  797. }
  798. // Set general toolbar visibility.
  799. set_visible(has_controls);
  800. // Set anchor tools visibility.
  801. if (has_controls && (!has_control_parents || !has_container_parents)) {
  802. anchors_button->set_visible(true);
  803. anchor_mode_button->set_visible(true);
  804. // Update anchor mode.
  805. int nb_valid_controls = 0;
  806. int nb_anchors_mode = 0;
  807. List<Node *> selection = editor_selection->get_selected_node_list();
  808. for (Node *E : selection) {
  809. Control *control = Object::cast_to<Control>(E);
  810. if (!control) {
  811. continue;
  812. }
  813. if (Object::cast_to<Container>(control->get_parent())) {
  814. continue;
  815. }
  816. nb_valid_controls++;
  817. if (control->get_meta("_edit_use_anchors_", false)) {
  818. nb_anchors_mode++;
  819. }
  820. }
  821. anchors_mode = (nb_valid_controls == nb_anchors_mode);
  822. anchor_mode_button->set_pressed(anchors_mode);
  823. } else {
  824. anchors_button->set_visible(false);
  825. anchor_mode_button->set_visible(false);
  826. anchor_mode_button->set_pressed(false);
  827. }
  828. // Set container tools visibility.
  829. if (has_controls && (!has_control_parents || has_container_parents)) {
  830. containers_button->set_visible(true);
  831. // Update allowed size flags.
  832. if (has_container_parents) {
  833. container_h_picker->set_allowed_flags(allowed_h_flags);
  834. container_v_picker->set_allowed_flags(allowed_v_flags);
  835. } else {
  836. Vector<SizeFlags> allowed_all_flags = {
  837. SIZE_SHRINK_BEGIN,
  838. SIZE_SHRINK_CENTER,
  839. SIZE_SHRINK_END,
  840. SIZE_FILL,
  841. SIZE_EXPAND,
  842. };
  843. container_h_picker->set_allowed_flags(allowed_all_flags);
  844. container_v_picker->set_allowed_flags(allowed_all_flags);
  845. }
  846. // Update expand toggles.
  847. int nb_valid_controls = 0;
  848. int nb_h_expand = 0;
  849. int nb_v_expand = 0;
  850. List<Node *> selection = editor_selection->get_selected_node_list();
  851. for (Node *E : selection) {
  852. Control *control = Object::cast_to<Control>(E);
  853. if (!control) {
  854. continue;
  855. }
  856. nb_valid_controls++;
  857. if (control->get_h_size_flags() & Control::SIZE_EXPAND) {
  858. nb_h_expand++;
  859. }
  860. if (control->get_v_size_flags() & Control::SIZE_EXPAND) {
  861. nb_v_expand++;
  862. }
  863. }
  864. container_h_picker->set_expand_flag(nb_valid_controls == nb_h_expand);
  865. container_v_picker->set_expand_flag(nb_valid_controls == nb_v_expand);
  866. } else {
  867. containers_button->set_visible(false);
  868. }
  869. }
  870. void ControlEditorToolbar::_notification(int p_what) {
  871. switch (p_what) {
  872. case NOTIFICATION_ENTER_TREE:
  873. case NOTIFICATION_THEME_CHANGED: {
  874. anchors_button->set_button_icon(get_editor_theme_icon(SNAME("ControlLayout")));
  875. anchor_mode_button->set_button_icon(get_editor_theme_icon(SNAME("Anchor")));
  876. containers_button->set_button_icon(get_editor_theme_icon(SNAME("ContainerLayout")));
  877. } break;
  878. }
  879. }
  880. ControlEditorToolbar::ControlEditorToolbar() {
  881. // Anchor and offset tools.
  882. anchors_button = memnew(ControlEditorPopupButton);
  883. anchors_button->set_tooltip_text(TTR("Presets for the anchor and offset values of a Control node."));
  884. add_child(anchors_button);
  885. Label *anchors_label = memnew(Label);
  886. anchors_label->set_text(TTR("Anchor preset"));
  887. anchors_button->get_popup_hbox()->add_child(anchors_label);
  888. AnchorPresetPicker *anchors_picker = memnew(AnchorPresetPicker);
  889. anchors_picker->set_h_size_flags(SIZE_SHRINK_CENTER);
  890. anchors_button->get_popup_hbox()->add_child(anchors_picker);
  891. anchors_picker->connect("anchors_preset_selected", callable_mp(this, &ControlEditorToolbar::_anchors_preset_selected));
  892. anchors_button->get_popup_hbox()->add_child(memnew(HSeparator));
  893. Button *keep_ratio_button = memnew(Button);
  894. keep_ratio_button->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  895. keep_ratio_button->set_text(TTR("Set to Current Ratio"));
  896. keep_ratio_button->set_tooltip_text(TTR("Adjust anchors and offsets to match the current rect size."));
  897. anchors_button->get_popup_hbox()->add_child(keep_ratio_button);
  898. keep_ratio_button->connect(SceneStringName(pressed), callable_mp(this, &ControlEditorToolbar::_anchors_to_current_ratio));
  899. anchor_mode_button = memnew(Button);
  900. anchor_mode_button->set_theme_type_variation("FlatButton");
  901. anchor_mode_button->set_toggle_mode(true);
  902. anchor_mode_button->set_tooltip_text(TTR("When active, moving Control nodes changes their anchors instead of their offsets."));
  903. add_child(anchor_mode_button);
  904. anchor_mode_button->connect(SceneStringName(toggled), callable_mp(this, &ControlEditorToolbar::_anchor_mode_toggled));
  905. // Container tools.
  906. containers_button = memnew(ControlEditorPopupButton);
  907. containers_button->set_tooltip_text(TTR("Sizing settings for children of a Container node."));
  908. add_child(containers_button);
  909. Label *container_h_label = memnew(Label);
  910. container_h_label->set_text(TTR("Horizontal alignment"));
  911. containers_button->get_popup_hbox()->add_child(container_h_label);
  912. container_h_picker = memnew(SizeFlagPresetPicker(false));
  913. containers_button->get_popup_hbox()->add_child(container_h_picker);
  914. container_h_picker->connect("size_flags_selected", callable_mp(this, &ControlEditorToolbar::_container_flags_selected).bind(false));
  915. container_h_picker->connect("expand_flag_toggled", callable_mp(this, &ControlEditorToolbar::_expand_flag_toggled).bind(false));
  916. containers_button->get_popup_hbox()->add_child(memnew(HSeparator));
  917. Label *container_v_label = memnew(Label);
  918. container_v_label->set_text(TTR("Vertical alignment"));
  919. containers_button->get_popup_hbox()->add_child(container_v_label);
  920. container_v_picker = memnew(SizeFlagPresetPicker(true));
  921. containers_button->get_popup_hbox()->add_child(container_v_picker);
  922. container_v_picker->connect("size_flags_selected", callable_mp(this, &ControlEditorToolbar::_container_flags_selected).bind(true));
  923. container_v_picker->connect("expand_flag_toggled", callable_mp(this, &ControlEditorToolbar::_expand_flag_toggled).bind(true));
  924. // Editor connections.
  925. editor_selection = EditorNode::get_singleton()->get_editor_selection();
  926. editor_selection->add_editor_plugin(this);
  927. editor_selection->connect("selection_changed", callable_mp(this, &ControlEditorToolbar::_selection_changed));
  928. singleton = this;
  929. }
  930. ControlEditorToolbar *ControlEditorToolbar::singleton = nullptr;
  931. // Editor plugin.
  932. ControlEditorPlugin::ControlEditorPlugin() {
  933. toolbar = memnew(ControlEditorToolbar);
  934. toolbar->hide();
  935. add_control_to_container(CONTAINER_CANVAS_EDITOR_MENU, toolbar);
  936. Ref<EditorInspectorPluginControl> plugin;
  937. plugin.instantiate();
  938. add_inspector_plugin(plugin);
  939. }