-
-
Notifications
You must be signed in to change notification settings - Fork 909
/
ImageButton.cs
70 lines (60 loc) · 2.54 KB
/
ImageButton.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Diagnostics;
using Stride.Core;
using Stride.UI.Attributes;
namespace Stride.UI.Controls
{
/// <summary>
/// A <see cref="Button"/> whose <see cref="ContentControl.Content"/> are the <see cref="Button.PressedImage"/> and <see cref="Button.NotPressedImage"/>.
/// </summary>
[DataContractMetadataType(typeof(ImageButtonMetadata))]
[DebuggerDisplay("ImageButton - Name={Name}")]
[Obsolete("Use Button with SizeToContent set to false.")]
public class ImageButton : Button
{
private readonly ImageElement contentImageElement = new ImageElement();
public ImageButton()
{
Padding = Thickness.UniformCuboid(0); // Warning: this must also match in ImageButtonMetadata
base.Content = contentImageElement;
MouseOverStateChanged += (sender, args) => UpdateContentImage();
}
protected override void OnAspectImageInvalidated()
{
UpdateContentImage();
}
private void UpdateContentImage()
{
contentImageElement.Source = ButtonImageProvider;
}
/// <summary>
/// The current content of the <see cref="ImageButton"/>, that is the current image used.
/// </summary>
/// <remarks>The <see cref="Content"/> of a <see cref="ImageButton"/> is determined by its state (pressed/not pressed) and the value of
/// <see cref="Button.PressedImage"/> and <see cref="Button.NotPressedImage"/>.
/// The <see cref="Content"/> cannot be set manually by the user.</remarks>
/// <exception cref="InvalidOperationException">The user tried to modify the <see cref="ImageButton"/> content.</exception>
public override UIElement Content
{
set { throw new InvalidOperationException("The content of an ImageButton cannot be modified by the user."); }
}
public override bool IsPressed
{
get { return base.IsPressed; }
protected set
{
if (value == IsPressed)
return;
base.IsPressed = value;
UpdateContentImage();
}
}
private class ImageButtonMetadata
{
[DefaultThicknessValue(0, 0, 0, 0)]
public Thickness Padding { get; }
}
}
}