Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ElementRenderer.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 SiliconStudio.Core;
4 using SiliconStudio.Core.Mathematics;
5 using SiliconStudio.Core.Serialization.Assets;
6 using SiliconStudio.Paradox.Graphics;
7 
8 namespace SiliconStudio.Paradox.UI.Renderers
9 {
10  /// <summary>
11  /// Base class for UI element renderers
12  /// </summary>
13  public class ElementRenderer
14  {
15  internal UISystem UI { get; private set; }
16 
17  private static Color blackColor;
18 
19  /// <summary>
20  /// A reference to the game asset manager.
21  /// </summary>
22  public IAssetManager Asset { get; private set; }
23 
24  private IGraphicsDeviceService GraphicsDeviceService { get; set; }
25 
26  /// <summary>
27  /// A reference to the game graphic device.
28  /// </summary>
30  {
31  get
32  {
33  return GraphicsDeviceService == null? null: GraphicsDeviceService.GraphicsDevice;
34  }
35  }
36 
37  /// <summary>
38  /// Gets a reference to the UI image drawer.
39  /// </summary>
40  public UIBatch Batch
41  {
42  get
43  {
44  return UI.Batch;
45  }
46  }
47 
48  /// <summary>
49  /// A depth stencil state that keep the stencil value in any cases.
50  /// </summary>
51  public DepthStencilState KeepStencilValueState
52  {
53  get
54  {
55  return UI.KeepStencilValueState;
56  }
57  }
58 
59  /// <summary>
60  /// A depth stencil state that increase the stencil value if the stencil test passes.
61  /// </summary>
62  public DepthStencilState IncreaseStencilValueState
63  {
64  get
65  {
66  return UI.IncreaseStencilValueState;
67  }
68  }
69 
70  /// <summary>
71  /// A depth stencil state that decrease the stencil value if the stencil test passes.
72  /// </summary>
73  public DepthStencilState DecreaseStencilValueState
74  {
75  get
76  {
77  return UI.DecreaseStencilValueState;
78  }
79  }
80 
81  /// <summary>
82  /// The projection matrix of the UI.
83  /// </summary>
84  public Matrix ProjectionMatrix
85  {
86  get
87  {
88  return UI.ProjectionMatrix;
89  }
90  }
91 
92  /// <summary>
93  /// The view matrix of the UI.
94  /// </summary>
95  public Matrix ViewMatrix
96  {
97  get
98  {
99  return UI.ViewMatrix;
100  }
101  }
102 
103  /// <summary>
104  /// The view projection matrix of the UI.
105  /// </summary>
106  public Matrix ViewProjectionMatrix
107  {
108  get
109  {
110  return UI.ViewProjectionInternal;
111  }
112  }
113 
114  /// <summary>
115  /// Return the resolution of the UI.
116  /// </summary>
117  public Vector3 Resolution
118  {
119  get
120  {
121  return UI.VirtualResolution;
122  }
123  }
124 
125  /// <summary>
126  /// Create an instance of an UI element renderer.
127  /// </summary>
128  /// <param name="services">The list of registered services</param>
130  {
131  UI = services.GetSafeServiceAs<UISystem>();
132  Asset = services.GetSafeServiceAs<IAssetManager>();
133  GraphicsDeviceService = services.GetSafeServiceAs<IGraphicsDeviceService>();
134  }
135 
136  /// <summary>
137  /// Render the provided <see cref="UIElement"/>.
138  /// </summary>
139  /// <param name="element">The element to render.</param>
140  /// <param name="context">The rendering context containing information how to draw the element.</param>
141  /// <remarks>The render target, the depth stencil buffer and the depth stencil state are already correctly set when entering this function.
142  /// If the user wants to perform some intermediate rendering, it is his responsibility to bind them back correctly before the final rendering.</remarks>
143  public virtual void RenderColor(UIElement element, UIRenderingContext context)
144  {
145  var backgroundColor = element.Opacity * element.BackgroundColor;
146 
147  // optimization: don't draw the background if transparent
148  if (backgroundColor == new Color())
149  return;
150 
151  // Default implementation: render an back-face cube with background color
152  Batch.DrawBackground(ref element.WorldMatrixInternal, ref element.RenderSizeInternal, ref backgroundColor, context.DepthBias);
153 
154  // increase depth bias value so that next elements renders on top of it.
155  context.DepthBias += 1;
156  }
157 
158  /// <summary>
159  /// Render the clipping region of the provided <see cref="UIElement"/>.
160  /// </summary>
161  /// <param name="element">The element to render.</param>
162  /// <param name="context">The rendering context containing information how to draw the element.</param>
163  /// <remarks>The render target, the depth stencil buffer and the depth stencil state are already correctly set when entering this function.
164  /// If the user wants to perform some intermediate rendering, it is his responsibility to bind them back correctly before the final rendering.</remarks>
165  public virtual void RenderClipping(UIElement element, UIRenderingContext context)
166  {
167  // Default implementation: render an back-face cube
168  Batch.DrawBackground(ref element.WorldMatrixInternal, ref element.RenderSizeInternal, ref blackColor, context.DepthBias);
169 
170  // increase the context depth bias for next elements.
171  context.DepthBias += 1;
172  }
173 
174  /// <summary>
175  /// Loads this instance. This method is called when the element renderer is added to the rendering pipeline
176  /// </summary>
177  public virtual void Load()
178  {
179 
180  }
181 
182  /// <summary>
183  /// Unloads this instance. This method is called when the element renderer is removed from the rendering pipeline
184  /// </summary>
185  public virtual void Unload()
186  {
187 
188  }
189  }
190 }
Provides a base class for all the User Interface elements in Paradox applications.
Definition: UIElement.cs:21
Service providing method to access GraphicsDevice life-cycle.
Contains depth-stencil state for the device.
A utility class to batch and draw UI images.
Definition: UIBatch.cs:15
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
A service registry is a IServiceProvider that provides methods to register and unregister services...
Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. See The+GraphicsDevice+class to learn more about the class.
Base class for UI element renderers
SiliconStudio.Core.Mathematics.Color Color
Definition: ColorPicker.cs:14
virtual void RenderClipping(UIElement element, UIRenderingContext context)
Render the clipping region of the provided UIElement.
virtual void Load()
Loads this instance. This method is called when the element renderer is added to the rendering pipeli...
Represents a 32-bit color (4 bytes) in the form of RGBA (in byte order: R, G, B, A).
Definition: Color.cs:16
Interface of the UI system.
Definition: UISystem.cs:20
The UI drawing context. It provides information about how to render UIElements for drawing...
ElementRenderer(IServiceRegistry services)
Create an instance of an UI element renderer.
virtual void RenderColor(UIElement element, UIRenderingContext context)
Render the provided UIElement.
virtual void Unload()
Unloads this instance. This method is called when the element renderer is removed from the rendering ...
Represents a 4x4 mathematical matrix.
Definition: Matrix.cs:47