control_editor_plugin.cpp 38 KB

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