// Copyright (c) 2012-2020 Wojciech Figat. All rights reserved. using System; using FlaxEditor.Options; 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 = 4; /// /// Event fired when button gets clicked. /// public Action ButtonClicked; /// /// 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 + itemScale * DefaultMarginV; private float itemScale; /// class. /// public ToolStrip() { itemScale = Style.Current.IconSizeExtra; AutoFocus = false; AnchorPreset = AnchorPresets.HorizontalStretchTop; BackgroundColor = Style.Current.LightBackground; } /// /// 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); } /// 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(); } } }