forked from stride3d/stride
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.cs
More file actions
218 lines (193 loc) · 7.53 KB
/
Button.cs
File metadata and controls
218 lines (193 loc) · 7.53 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & 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.ComponentModel;
using System.Diagnostics;
using Stride.Core;
using Stride.Core.Mathematics;
using Stride.Engine;
using Stride.Graphics;
using Stride.UI.Attributes;
namespace Stride.UI.Controls
{
/// <summary>
/// Represents a Windows button control, which reacts to the Click event.
/// </summary>
[DataContract(nameof(Button))]
[DataContractMetadataType(typeof(ButtonMetadata))]
[DebuggerDisplay("Button - Name={Name}")]
public class Button : ButtonBase
{
private StretchType imageStretchType = StretchType.Uniform;
private StretchDirection imageStretchDirection = StretchDirection.Both;
private ISpriteProvider pressedImage;
private ISpriteProvider notPressedImage;
private ISpriteProvider mouseOverImage;
private bool sizeToContent = true;
public Button()
{
DrawLayerNumber += 1; // (button design image)
Padding = new Thickness(10, 5, 10, 7); // Warning: this must also match in ButtonMetadata
MouseOverStateChanged += (sender, args) => InvalidateButtonImage();
}
/// <inheritdoc/>
public override bool IsPressed
{
get { return base.IsPressed; }
protected set
{
if (value == IsPressed)
return;
base.IsPressed = value;
InvalidateMeasure();
}
}
/// <summary>
/// Gets or sets the image displayed when the button is pressed.
/// </summary>
/// <userdoc>Image displayed when the button is pressed.</userdoc>
[DataMember]
[Display(category: AppearanceCategory)]
[DefaultValue(null)]
public ISpriteProvider PressedImage
{
get { return pressedImage; }
set
{
if (pressedImage == value)
return;
pressedImage = value;
OnAspectImageInvalidated();
}
}
/// <summary>
/// Gets or sets the image displayed when the button is not pressed.
/// </summary>
/// <userdoc>Image displayed when the button is not pressed.</userdoc>
[DataMember]
[Display(category: AppearanceCategory)]
[DefaultValue(null)]
public ISpriteProvider NotPressedImage
{
get { return notPressedImage; }
set
{
if (notPressedImage == value)
return;
notPressedImage = value;
OnAspectImageInvalidated();
}
}
/// <summary>
/// Gets or sets the image displayed when the mouse hovers over the button.
/// </summary>
/// <userdoc>Image displayed when the mouse hovers over the button.</userdoc>
[DataMember]
[Display(category: AppearanceCategory)]
[DefaultValue(null)]
public ISpriteProvider MouseOverImage
{
get { return mouseOverImage; }
set
{
if (mouseOverImage == value)
return;
mouseOverImage = value;
OnAspectImageInvalidated();
}
}
/// <summary>
/// Gets or set the color used to tint the image. Default value is White.
/// </summary>
/// <remarks>The initial image color is multiplied by this color.</remarks>
/// <userdoc>The color used to tint the image. The default value is white.</userdoc>
[DataMember]
[Display(category: AppearanceCategory)]
public Color Color { get; set; } = Color.White;
/// <summary>
/// Gets or sets a value that describes how the button image should be stretched to fill the destination rectangle.
/// </summary>
/// <remarks>This property has no effect is <see cref="SizeToContent"/> is <c>true</c>.</remarks>
/// <userdoc>Describes how the button image should be stretched to fill the destination rectangle.</userdoc>
[DataMember]
[Display(category: LayoutCategory)]
[DefaultValue(StretchType.Uniform)]
public StretchType ImageStretchType
{
get { return imageStretchType; }
set
{
imageStretchType = value;
InvalidateMeasure();
}
}
/// <summary>
/// Gets or sets a value that indicates how the button image is scaled.
/// </summary>
/// <remarks>This property has no effect is <see cref="SizeToContent"/> is <c>true</c>.</remarks>
/// <userdoc>Indicates how the button image is scaled.</userdoc>
[DataMember]
[Display(category: LayoutCategory)]
[DefaultValue(StretchDirection.Both)]
public StretchDirection ImageStretchDirection
{
get { return imageStretchDirection; }
set
{
imageStretchDirection = value;
InvalidateMeasure();
}
}
/// <summary>
/// Gets or sets whether the size depends on the Content. The default is <c>true</c>.
/// </summary>
/// <userdoc>True if this button's size depends of its content, false otherwise.</userdoc>
[DataMember]
[Display(category: LayoutCategory)]
[DefaultValue(true)]
public bool SizeToContent
{
get { return sizeToContent; }
set
{
if (sizeToContent == value)
return;
sizeToContent = value;
InvalidateMeasure();
}
}
internal ISpriteProvider ButtonImageProvider => IsPressed ? PressedImage : (MouseOverState == MouseOverState.MouseOverElement && MouseOverImage != null ? MouseOverImage : NotPressedImage);
internal Sprite ButtonImage => ButtonImageProvider?.GetSprite();
/// <inheritdoc/>
protected override Vector3 ArrangeOverride(Vector3 finalSizeWithoutMargins)
{
return sizeToContent
? base.ArrangeOverride(finalSizeWithoutMargins)
: ImageSizeHelper.CalculateImageSizeFromAvailable(ButtonImage, finalSizeWithoutMargins, ImageStretchType, ImageStretchDirection, false);
}
/// <inheritdoc/>
protected override Vector3 MeasureOverride(Vector3 availableSizeWithoutMargins)
{
return sizeToContent
? base.MeasureOverride(availableSizeWithoutMargins)
: ImageSizeHelper.CalculateImageSizeFromAvailable(ButtonImage, availableSizeWithoutMargins, ImageStretchType, ImageStretchDirection, true);
}
/// <summary>
/// Function triggered when one of the <see cref="PressedImage"/> and <see cref="NotPressedImage"/> images are invalidated.
/// This function can be overridden in inherited classes.
/// </summary>
protected virtual void OnAspectImageInvalidated()
{
InvalidateButtonImage();
}
private void InvalidateButtonImage()
{
if (!sizeToContent)
InvalidateMeasure();
}
private class ButtonMetadata
{
[DefaultThicknessValue(10, 5, 10, 7)]
public Thickness Padding { get; }
}
}
}