Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
UIImage.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 
5 using SiliconStudio.Core.Mathematics;
6 using SiliconStudio.Core.Serialization.Converters;
7 using SiliconStudio.Paradox.Graphics;
8 
9 namespace SiliconStudio.Paradox.UI
10 {
11  /// <summary>
12  /// Class holding all the data required to define an UI image.
13  /// </summary>
14  [DataConverter(AutoGenerate = true, ContentReference = true)]
15  public class UIImage : ImageFragment
16  {
17  internal Vector4 BordersInternal;
18 
19  private Vector2 imageIdealSize;
20 
21  /// <summary>
22  /// Create an instance of <see cref="UIImage"/> with a unique random name.
23  /// </summary>
24  public UIImage()
25  :this(Guid.NewGuid().ToString())
26  {
27  }
28 
29  /// <summary>
30  /// Create an empty <see cref="UIImage"/>
31  /// </summary>
32  /// <param name="imageName">The name of the UI image</param>
33  public UIImage(string imageName)
34  : base(imageName)
35  {
36  }
37 
38  /// <summary>
39  /// Create an instance of <see cref="UIImage"/> having a unique name from a single <see cref="Texture2D"/> and initialize the <see cref="Region"/> to the size of the texture.
40  /// </summary>
41  /// <param name="texture">The texture to use as color</param>
42  public UIImage(Texture2D texture)
43  : this(Guid.NewGuid().ToString(), texture, null)
44  {
45  }
46 
47  /// <summary>
48  /// Create an instance of <see cref="UIImage"/> from a single color/alpha <see cref="Texture2D"/> and
49  /// initialize the <see cref="Region"/> to the size of the texture.
50  /// </summary>
51  /// <param name="imageName">The name of the UI image</param>
52  /// <param name="texture">The texture to use as color</param>
53  public UIImage(string imageName, Texture2D texture)
54  : this(imageName, texture, null)
55  {
56  }
57 
58  /// <summary>
59  /// Create an instance of <see cref="UIImage"/> that takes its color components from the <paramref name="color"/> texture and
60  /// its alpha component from the r component of the <paramref name="alpha"/> texture.
61  /// The <see cref="Region"/> is initialized with the size of the textures.
62  /// </summary>
63  /// <param name="imageName">The name of the UI image</param>
64  /// <param name="color">The texture to use as color</param>
65  /// <param name="alpha">The texture to use as alpha</param>
66  /// <exception cref="ArgumentNullException">The provided textures cannot be null</exception>
67  /// <exception cref="ArgumentException">The provided textures must have the same size</exception>
68  public UIImage(string imageName, Texture2D color, Texture2D alpha)
69  : base(imageName, color, alpha)
70  {
71  UpdateIdealSize();
72  }
73 
74  /// <summary>
75  /// Gets or sets size of the unstretchable borders of source image in pixels.
76  /// </summary>
77  /// <remarks>Borders size are ordered as follows X->Left, Y->Right, Z ->Top, W -> Bottom.</remarks>
78  [DataMemberConvert]
79  public Vector4 Borders
80  {
81  get { return BordersInternal; }
82  set
83  {
84  if(value == BordersInternal)
85  return;
86 
87  BordersInternal = value;
88  HasBorders = BordersInternal.Length() > MathUtil.ZeroTolerance;
89 
90  var handler = BorderChanged;
91  if (handler != null)
92  handler(this, EventArgs.Empty);
93  }
94  }
95 
96  /// <summary>
97  /// Gets the value indicating if the image has unstretchable borders.
98  /// </summary>
99  public bool HasBorders { get; private set; }
100 
101  /// <summary>
102  /// Region of the texture (in pixels) defining the image.
103  /// </summary>
104  public override RectangleF Region
105  {
106  set
107  {
108  base.Region = value;
109  UpdateIdealSize();
110  }
111  }
112 
113  /// <summary>
114  /// Gets or sets the rotation to apply to the texture region when rendering the <see cref="UIImage"/>
115  /// </summary>
116  public override ImageOrientation Orientation
117  {
118  set
119  {
120  base.Orientation = value;
121  UpdateIdealSize();
122  }
123  }
124 
125  /// <summary>
126  /// Gets the ideal size of the image in virtual pixel measure.
127  /// </summary>
128  public Vector2 ImageIdealSize
129  {
130  get { return imageIdealSize; }
131  private set
132  {
133  if (value == imageIdealSize)
134  return;
135 
136  imageIdealSize = value;
137 
138  var handler = IdealSizeChanged;
139  if (handler != null)
140  handler(this, EventArgs.Empty);
141  }
142  }
143 
144  private void UpdateIdealSize()
145  {
146  if (Orientation == ImageOrientation.AsIs)
147  ImageIdealSize = new Vector2(RegionInternal.Width, RegionInternal.Height);
148  else
149  ImageIdealSize = new Vector2(RegionInternal.Height, RegionInternal.Width);
150  }
151 
152  internal event EventHandler<EventArgs> BorderChanged;
153  internal event EventHandler<EventArgs> IdealSizeChanged;
154  }
155 }
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
Represents a two dimensional mathematical vector.
Definition: Vector2.cs:42
Orientation
Defines the different orientations that a control or layout can have.
Definition: Orientation.cs:8
Base class for converters to/from a data type.
const float ZeroTolerance
The value for which all absolute numbers smaller than are considered equal to zero.
Definition: MathUtil.cs:38
UIImage(string imageName)
Create an empty UIImage
Definition: UIImage.cs:33
Define a RectangleF. This structure is slightly different from System.Drawing.RectangleF as it is int...
Definition: RectangleF.cs:36
UIImage(string imageName, Texture2D color, Texture2D alpha)
Create an instance of UIImage that takes its color components from the color texture and its alpha c...
Definition: UIImage.cs:68
Represents a four dimensional mathematical vector.
Definition: Vector4.cs:42
UIImage(Texture2D texture)
Create an instance of UIImage having a unique name from a single Texture2D and initialize the Region ...
Definition: UIImage.cs:42
A Texture 2D frontend to SharpDX.Direct3D11.Texture2D.
Definition: Texture2D.cs:37
UIImage()
Create an instance of UIImage with a unique random name.
Definition: UIImage.cs:24
Class holding all the data required to define an UI image.
Definition: UIImage.cs:15
UIImage(string imageName, Texture2D texture)
Create an instance of UIImage from a single color/alpha Texture2D and initialize the Region to the si...
Definition: UIImage.cs:53
ImageOrientation
Defines the possible rotations to apply on image regions.