Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ImageButton.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Diagnostics;
5 
6 namespace SiliconStudio.Paradox.UI.Controls
7 {
8  /// <summary>
9  /// A <see cref="Button"/> whose <see cref="ContentControl.Content"/> are the <see cref="Button.PressedImage"/> and <see cref="Button.NotPressedImage"/>.
10  /// </summary>
11  [DebuggerDisplay("ImageButton - Name={Name}")]
12  public class ImageButton : Button
13  {
14  private readonly ImageElement contentImageElement = new ImageElement();
15 
16  public ImageButton()
17  {
18  Padding = Thickness.UniformCuboid(0);
19  base.Content = contentImageElement;
20 
21  MouseOverStateChanged += (sender, args) => UpdateContentImage();
22  }
23 
24  protected override void OnAspectImageInvalidated()
25  {
26  UpdateContentImage();
27  }
28 
29  private void UpdateContentImage()
30  {
31  contentImageElement.Source = IsPressed ? PressedImage : MouseOverState == MouseOverState.MouseOverElement? MouseOverImage: NotPressedImage;
32  }
33 
34  /// <summary>
35  /// The current content of the <see cref="ImageButton"/>, that is the current image used.
36  /// </summary>
37  /// <remarks>The <see cref="Content"/> of a <see cref="ImageButton"/> is determined by its state (pressed/not pressed) and the value of
38  /// <see cref="Button.PressedImage"/> and <see cref="Button.NotPressedImage"/>.
39  /// The <see cref="Content"/> cannot be set manually by the user.</remarks>
40  /// <exception cref="InvalidOperationException">The user tried to modify the <see cref="ImageButton"/> content.</exception>
41  public override UIElement Content
42  {
43  set { throw new InvalidOperationException("The content of an ImageButton cannot be modified by the user."); }
44  }
45 
46  public override bool IsPressed
47  {
48  get { return base.IsPressed; }
49  protected set
50  {
51  if(value == IsPressed)
52  return;
53 
54  base.IsPressed = value;
55 
56  UpdateContentImage();
57  }
58  }
59  }
60 }
MouseOverState
Describe the possible states of the mouse over an UI element.
Provides a base class for all the User Interface elements in Paradox applications.
Definition: UIElement.cs:21
override void OnAspectImageInvalidated()
Function triggered when one of the PressedImage and NotPressedImage images are invalidated. This function can be overridden in inherited classes.
Definition: ImageButton.cs:24
Represents a Windows button control, which reacts to the Click event.
Definition: Button.cs:13
Represents a control that displays an image.
Definition: ImageElement.cs:14
A Button whose ContentControl.Content are the Button.PressedImage and Button.NotPressedImage.
Definition: ImageButton.cs:12