Begin working on an IconButton class, and demoing the IconButton idea using the scale link icon.

This commit is contained in:
Menotdan
2023-05-07 22:58:57 -04:00
parent e7249d9d94
commit 141555377b
2 changed files with 192 additions and 7 deletions

View 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();
}
}
}