Begin working on an IconButton class, and demoing the IconButton idea using the scale link icon.
This commit is contained in:
@@ -78,7 +78,8 @@ namespace FlaxEditor.CustomEditors.Editors
|
||||
/// <seealso cref="FlaxEditor.CustomEditors.Editors.Float3Editor" />
|
||||
public class ScaleEditor : Float3Editor
|
||||
{
|
||||
private Image _linkImage;
|
||||
private Button _linkButton;
|
||||
private SpriteBrush _linkBrush;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Initialize(LayoutElementsContainer layout)
|
||||
@@ -87,18 +88,26 @@ namespace FlaxEditor.CustomEditors.Editors
|
||||
|
||||
LinkValues = Editor.Instance.Windows.PropertiesWin.ScaleLinked;
|
||||
|
||||
_linkImage = new Image
|
||||
// Add button with the link icon.
|
||||
_linkBrush = new SpriteBrush(Editor.Instance.Icons.Link32);
|
||||
_linkButton = new Button
|
||||
{
|
||||
Parent = LinkedLabel,
|
||||
Width = 18,
|
||||
Height = 18,
|
||||
Brush = LinkValues ? new SpriteBrush(Editor.Instance.Icons.Link32) : new SpriteBrush(),
|
||||
BackgroundBrush = _linkBrush,
|
||||
AnchorPreset = AnchorPresets.TopLeft,
|
||||
TooltipText = "Scale values are linked together.",
|
||||
};
|
||||
|
||||
_linkButton.Clicked += ToggleLink;
|
||||
_linkButton.BorderColor = Color.Transparent;
|
||||
_linkButton.BorderColorSelected = Color.Transparent;
|
||||
_linkButton.BorderColorHighlighted = Color.Transparent;
|
||||
SetLinkStyle();
|
||||
|
||||
var x = LinkedLabel.Text.Value.Length * 7 + 5;
|
||||
_linkImage.LocalX += x;
|
||||
_linkImage.LocalY += 1;
|
||||
_linkButton.LocalX += x;
|
||||
_linkButton.LocalY += 1;
|
||||
|
||||
LinkedLabel.SetupContextMenu += (label, menu, editor) =>
|
||||
{
|
||||
@@ -127,7 +136,16 @@ namespace FlaxEditor.CustomEditors.Editors
|
||||
{
|
||||
LinkValues = !LinkValues;
|
||||
Editor.Instance.Windows.PropertiesWin.ScaleLinked = LinkValues;
|
||||
_linkImage.Brush = LinkValues ? new SpriteBrush(Editor.Instance.Icons.Link32) : new SpriteBrush();
|
||||
SetLinkStyle();
|
||||
}
|
||||
|
||||
private void SetLinkStyle()
|
||||
{
|
||||
Color backgroundColor = LinkValues ? FlaxEngine.GUI.Style.Current.BackgroundSelected : FlaxEngine.GUI.Style.Current.ForegroundDisabled;
|
||||
_linkButton.BackgroundColor = backgroundColor;
|
||||
_linkButton.BackgroundColorHighlighted = backgroundColor.RGBMultiplied(0.9f);
|
||||
_linkButton.BackgroundColorSelected = backgroundColor.RGBMultiplied(0.8f);
|
||||
_linkButton.TooltipText = (LinkValues ? "Unlink" : "Link") + " values for uniform scaling.";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
167
Source/Engine/UI/GUI/Common/IconButton.cs
Normal file
167
Source/Engine/UI/GUI/Common/IconButton.cs
Normal file
@@ -0,0 +1,167 @@
|
||||
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
|
||||
|
||||
using System;
|
||||
|
||||
namespace FlaxEngine.GUI
|
||||
{
|
||||
/// <summary>
|
||||
/// Button with an icon.
|
||||
/// </summary>
|
||||
public class IconButton : Button
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public override void ClearState()
|
||||
{
|
||||
base.ClearState();
|
||||
|
||||
if (_isPressed)
|
||||
OnPressEnd();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void DrawSelf()
|
||||
{
|
||||
// Cache data
|
||||
Rectangle clientRect = new Rectangle(Float2.Zero, Size);
|
||||
bool enabled = EnabledInHierarchy;
|
||||
Color backgroundColor = BackgroundColor;
|
||||
Color borderColor = BorderColor;
|
||||
Color textColor = TextColor;
|
||||
if (!enabled)
|
||||
{
|
||||
backgroundColor *= 0.5f;
|
||||
borderColor *= 0.5f;
|
||||
textColor *= 0.6f;
|
||||
}
|
||||
else if (_isPressed)
|
||||
{
|
||||
backgroundColor = BackgroundColorSelected;
|
||||
borderColor = BorderColorSelected;
|
||||
}
|
||||
else if (IsMouseOver || IsNavFocused)
|
||||
{
|
||||
backgroundColor = BackgroundColorHighlighted;
|
||||
borderColor = BorderColorHighlighted;
|
||||
}
|
||||
|
||||
// Draw background
|
||||
if (BackgroundBrush != null)
|
||||
BackgroundBrush.Draw(clientRect, backgroundColor);
|
||||
else
|
||||
Render2D.FillRectangle(clientRect, backgroundColor);
|
||||
Render2D.DrawRectangle(clientRect, borderColor);
|
||||
|
||||
// Draw text
|
||||
Render2D.DrawText(_font?.GetFont(), TextMaterial, _text, clientRect, textColor, TextAlignment.Center, TextAlignment.Center);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void OnMouseEnter(Float2 location)
|
||||
{
|
||||
base.OnMouseEnter(location);
|
||||
|
||||
HoverBegin?.Invoke();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void OnMouseLeave()
|
||||
{
|
||||
if (_isPressed)
|
||||
{
|
||||
OnPressEnd();
|
||||
}
|
||||
|
||||
HoverEnd?.Invoke();
|
||||
|
||||
base.OnMouseLeave();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool OnMouseDown(Float2 location, MouseButton button)
|
||||
{
|
||||
if (base.OnMouseDown(location, button))
|
||||
return true;
|
||||
|
||||
if (button == MouseButton.Left && !_isPressed)
|
||||
{
|
||||
OnPressBegin();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool OnMouseUp(Float2 location, MouseButton button)
|
||||
{
|
||||
if (base.OnMouseUp(location, button))
|
||||
return true;
|
||||
|
||||
if (button == MouseButton.Left && _isPressed)
|
||||
{
|
||||
OnPressEnd();
|
||||
OnClick();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool OnTouchDown(Float2 location, int pointerId)
|
||||
{
|
||||
if (base.OnTouchDown(location, pointerId))
|
||||
return true;
|
||||
|
||||
if (!_isPressed)
|
||||
{
|
||||
OnPressBegin();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool OnTouchUp(Float2 location, int pointerId)
|
||||
{
|
||||
if (base.OnTouchUp(location, pointerId))
|
||||
return true;
|
||||
|
||||
if (_isPressed)
|
||||
{
|
||||
OnPressEnd();
|
||||
OnClick();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void OnTouchLeave()
|
||||
{
|
||||
if (_isPressed)
|
||||
{
|
||||
OnPressEnd();
|
||||
}
|
||||
|
||||
base.OnTouchLeave();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void OnLostFocus()
|
||||
{
|
||||
if (_isPressed)
|
||||
{
|
||||
OnPressEnd();
|
||||
}
|
||||
|
||||
base.OnLostFocus();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void OnSubmit()
|
||||
{
|
||||
OnClick();
|
||||
|
||||
base.OnSubmit();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user