// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. using System; namespace FlaxEngine.GUI { /// /// The basic GUI image control. Shows texture, sprite or render target. /// /// [ActorToolbox("GUI")] public class Image : ContainerControl { /// /// Gets or sets the image source. /// [EditorOrder(10), Tooltip("The image to draw.")] public IBrush Brush { get; set; } /// /// Gets or sets the margin for the image. /// [EditorOrder(20), Tooltip("Margins of the image area.")] public Margin Margin { get; set; } /// /// Gets or sets the color used to multiply the image pixels. /// [EditorDisplay("Image Style"), EditorOrder(2010), ExpandGroups] public Color Color { get; set; } = Color.White; /// /// Gets or sets the color used to multiply the image pixels when mouse is over the image. /// [EditorDisplay("Image Style"), EditorOrder(2011)] public Color MouseOverColor { get; set; } = Color.White; /// /// Gets or sets the color used to multiply the image pixels when control is disabled. /// [EditorDisplay("Image Style"), EditorOrder(2012)] public Color DisabledTint { get; set; } = Color.Gray; /// /// Gets or sets a value indicating whether keep aspect ratio when drawing the image. /// [EditorOrder(60), Tooltip("If checked, control will keep aspect ratio of the image.")] public bool KeepAspectRatio { get; set; } = true; /// /// Occurs when mouse clicks on the image. /// public event Action Clicked; /// public Image() : base(0, 0, 64, 64) { AutoFocus = false; } /// public Image(float x, float y, float width, float height) : base(x, y, width, height) { AutoFocus = false; } /// public Image(Float2 location, Float2 size) : base(location, size) { AutoFocus = false; } /// public Image(Rectangle bounds) : base(bounds) { AutoFocus = false; } /// public override void DrawSelf() { base.DrawSelf(); if (Brush == null) return; Rectangle rect; if (KeepAspectRatio) { // Figure out the ratio var size = Size; var imageSize = Brush.Size; if (imageSize.LengthSquared < 1) return; var ratio = size / imageSize; var aspectRatio = ratio.MinValue; // Get the new height and width var newSize = imageSize * aspectRatio; // Calculate the X,Y position of the upper-left corner // (one of these will always be zero) var newPos = (size - newSize) / 2; rect = new Rectangle(newPos, newSize); } else { rect = new Rectangle(Float2.Zero, Size); } Margin.ShrinkRectangle(ref rect); var color = IsMouseOver || IsNavFocused ? MouseOverColor : Color; if (!Enabled) color *= DisabledTint; Brush.Draw(rect, color); } /// public override bool OnMouseUp(Float2 location, MouseButton button) { if (base.OnMouseUp(location, button)) return true; if (Clicked != null) { Clicked.Invoke(this, button); return true; } return false; } /// public override void OnSubmit() { base.OnSubmit(); // Execute default user interaction via mouse click Clicked?.Invoke(this, MouseButton.Left); } } }