// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using FlaxEngine;
using FlaxEngine.GUI;
namespace FlaxEditor.Viewport.Widgets
{
///
/// The viewport widget location.
///
[HideInEditor]
public enum ViewportWidgetLocation
{
///
/// The upper left corner of the parent container.
///
UpperLeft,
///
/// The upper right corner of the parent container.
///
UpperRight,
}
///
/// Viewport Widgets Container control
///
///
[HideInEditor]
public class ViewportWidgetsContainer : ContainerControl
{
///
/// The widgets margin.
///
public const float WidgetsMargin = 4;
///
/// The widgets height.
///
public const float WidgetsHeight = 18;
///
/// The widgets icon size.
///
public const float WidgetsIconSize = 16;
///
/// Gets the widget location.
///
public ViewportWidgetLocation WidgetLocation { get; }
///
/// Initializes a new instance of the class.
///
/// The location.
public ViewportWidgetsContainer(ViewportWidgetLocation location)
: base(0, WidgetsMargin, 64, WidgetsHeight + 2)
{
AutoFocus = false;
WidgetLocation = location;
}
///
public override void Draw()
{
// Cache data
var style = Style.Current;
var clientRect = new Rectangle(Float2.Zero, Size);
// Draw background
Render2D.FillRectangle(clientRect, style.LightBackground * (IsMouseOver ? 0.3f : 0.2f));
base.Draw();
// Draw frame
Render2D.DrawRectangle(clientRect, style.BackgroundSelected * (IsMouseOver ? 1.0f : 0.6f));
}
///
public override void OnChildResized(Control control)
{
base.OnChildResized(control);
PerformLayout();
}
///
protected override void PerformLayoutBeforeChildren()
{
base.PerformLayoutBeforeChildren();
float x = 1;
for (int i = 0; i < _children.Count; i++)
{
var c = _children[i];
var w = c.Width;
c.Bounds = new Rectangle(x, 1, w, Height - 2);
x += w;
}
Width = x + 1;
}
///
/// Arranges the widgets of the control.
///
/// The control.
public static void ArrangeWidgets(ContainerControl control)
{
// Arrange viewport widgets
const float margin = ViewportWidgetsContainer.WidgetsMargin;
float left = margin;
float right = control.Width - margin;
for (int i = 0; i < control.ChildrenCount; i++)
{
if (control.Children[i] is ViewportWidgetsContainer widget && widget.Visible)
{
float x;
switch (widget.WidgetLocation)
{
case ViewportWidgetLocation.UpperLeft:
x = left;
left += widget.Width + margin;
break;
case ViewportWidgetLocation.UpperRight:
x = right - widget.Width;
right = x - margin;
break;
default:
x = 0;
break;
}
widget.Location = new Float2(x, margin);
}
}
}
}
}