// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. using System; using FlaxEditor.Windows; using FlaxEngine; using FlaxEngine.GUI; namespace FlaxEditor.GUI { /// /// Tool strip with child items. /// /// public class ToolStrip : ContainerControl { /// /// The default margin vertically. /// public const int DefaultMarginV = 1; /// /// The default margin horizontally. /// public const int DefaultMarginH = 2; /// /// Event fired when button gets clicked with the primary mouse button. /// public Action ButtonClicked; /// /// Event fired when button gets clicked with the secondary mouse button. /// public Action SecondaryButtonClicked; /// /// Tries to get the last button. /// public ToolStripButton LastButton { get { for (int i = _children.Count - 1; i >= 0; i--) { if (_children[i] is ToolStripButton button) return button; } return null; } } /// /// Gets amount of buttons that has been added /// public int ButtonsCount { get { int result = 0; for (int i = 0; i < _children.Count; i++) { if (_children[i] is ToolStripButton) result++; } return result; } } /// /// Gets the height for the items. /// public float ItemsHeight => Height - 2 * DefaultMarginV; /// /// Initializes a new instance of the class. /// /// The toolstrip height. /// The toolstrip Y position. public ToolStrip(float height = 32.0f, float y = 0) { AutoFocus = false; AnchorPreset = AnchorPresets.HorizontalStretchTop; BackgroundColor = Style.Current.LightBackground; Offsets = new Margin(0, 0, y, height * Editor.Instance.Options.Options.Interface.IconsScale); } /// /// Adds the button. /// /// The icon sprite. /// The custom action to call on button clicked. /// The button. public ToolStripButton AddButton(SpriteHandle sprite, Action onClick = null) { var button = new ToolStripButton(ItemsHeight, ref sprite) { Parent = this, }; if (onClick != null) button.Clicked += onClick; return button; } /// /// Adds the button. /// /// The icon sprite. /// The text. /// The custom action to call on button clicked. /// The button. public ToolStripButton AddButton(SpriteHandle sprite, string text, Action onClick = null) { var button = new ToolStripButton(ItemsHeight, ref sprite) { Text = text, Parent = this, }; if (onClick != null) button.Clicked += onClick; return button; } /// /// Adds the button. /// /// The text. /// The custom action to call on button clicked. /// The button. public ToolStripButton AddButton(string text, Action onClick = null) { var button = new ToolStripButton(ItemsHeight, ref SpriteHandle.Invalid) { Text = text, Parent = this, }; if (onClick != null) button.Clicked += onClick; return button; } /// /// Adds the separator. /// /// The separator. public ToolStripSeparator AddSeparator() { return AddChild(new ToolStripSeparator(ItemsHeight)); } internal void OnButtonClicked(ToolStripButton button) { ButtonClicked?.Invoke(button); } internal void OnSecondaryButtonClicked(ToolStripButton button) { SecondaryButtonClicked?.Invoke(button); } /// protected override void PerformLayoutBeforeChildren() { // Arrange controls float x = DefaultMarginH; float h = ItemsHeight; for (int i = 0; i < _children.Count; i++) { var c = _children[i]; if (c.Visible) { var w = c.Width; c.Bounds = new Rectangle(x, DefaultMarginV, w, h); x += w + DefaultMarginH; } } } /// public override void OnChildResized(Control control) { base.OnChildResized(control); PerformLayout(); } /// public override bool OnKeyDown(KeyboardKeys key) { if (base.OnKeyDown(key)) return true; // Fallback to the owning window for shortcuts EditorWindow editorWindow = null; ContainerControl c = Parent; while (c != null && editorWindow == null) { editorWindow = c as EditorWindow; c = c.Parent; } var editor = Editor.Instance; if (editorWindow == null) editorWindow = editor.Windows.EditWin; // Fallback to main editor window return editorWindow.InputActions.Process(editor, this, key); } } }