Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ThumbnailCompilerContext.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.IO;
5 using System.Threading;
6 
7 using SiliconStudio.Core.Mathematics;
8 using SiliconStudio.Core.Storage;
9 
10 namespace SiliconStudio.Assets.Compiler
11 {
12  /// <summary>
13  /// The context used when building the thumbnail of an asset in a Package.
14  /// </summary>
16  {
17  private readonly object thumbnailCounterLock = new object();
18 
19  /// <summary>
20  /// Initializes a new instance of the <see cref="ThumbnailCompilerContext"/> class.
21  /// </summary>
23  {
24  ThumbnailResolution = 128 * Int2.One;
25  }
26 
27  /// <summary>
28  /// Gets the desired resolution for thumbnails.
29  /// </summary>
30  public Int2 ThumbnailResolution { get; private set; }
31 
32  /// <summary>
33  /// Indicate whether the fact that a thumbnail has been built should be notified with <see cref="NotifyThumbnailBuilt"/>
34  /// </summary>
35  /// <remarks>Use this property to avoid doing unnecessary stream operation when <see cref="ThumbnailBuilt"/> event has no subscriber.</remarks>
36  public bool ShouldNotifyThumbnailBuilt { get { return ThumbnailBuilt != null; } }
37 
38  /// <summary>
39  /// The array of data representing the thumbnail to display when a thumbnail build failed.
40  /// </summary>
41  public Byte[] BuildFailedThumbnail;
42 
43  /// <summary>
44  /// The event raised when a thumbnail has finished to build.
45  /// </summary>
46  public event EventHandler<ThumbnailBuiltEventArgs> ThumbnailBuilt;
47 
48  /// <summary>
49  /// Notify that the thumbnail has just been built. This method will raise the <see cref="ThumbnailBuilt"/> event.
50  /// </summary>
51  /// <param name="assetItem">The asset item whose thumbnail has been built.</param>
52  /// <param name="result">A <see cref="ThumbnailBuildResult"/> value indicating whether the build was successful, failed or cancelled.</param>
53  /// <param name="changed">A boolean indicating whether the thumbnal has changed since the last generation.</param>
54  /// <param name="thumbnailStream">A stream to the thumbnail image.</param>
55  /// <param name="thumbnailHash"></param>
56  internal void NotifyThumbnailBuilt(AssetItem assetItem, ThumbnailBuildResult result, bool changed, Stream thumbnailStream, ObjectId thumbnailHash)
57  {
58  try
59  {
60  Monitor.Enter(thumbnailCounterLock);
61  var handler = ThumbnailBuilt;
62  if (handler != null)
63  {
64  // create the thumbnail build event arguments
65  var thumbnailBuiltArgs = new ThumbnailBuiltEventArgs
66  {
67  AssetId = assetItem.Id,
68  Url = assetItem.Location,
69  Result = result,
70  ThumbnailChanged = changed
71  };
72 
73  Monitor.Exit(thumbnailCounterLock);
74 
75  // Open the image data stream if the build succeeded
76  if (thumbnailBuiltArgs.Result == ThumbnailBuildResult.Succeeded)
77  {
78  thumbnailBuiltArgs.ThumbnailStream = thumbnailStream;
79  thumbnailBuiltArgs.ThumbnailId = thumbnailHash;
80  }
81  else if (BuildFailedThumbnail != null)
82  {
83  thumbnailBuiltArgs.ThumbnailStream = new MemoryStream(BuildFailedThumbnail);
84  }
85  handler(assetItem, thumbnailBuiltArgs);
86  }
87  }
88  finally
89  {
90  if (Monitor.IsEntered(thumbnailCounterLock))
91  Monitor.Exit(thumbnailCounterLock);
92  }
93  }
94  }
95 }
EventHandler< ThumbnailBuiltEventArgs > ThumbnailBuilt
The event raised when a thumbnail has finished to build.
An asset item part of a Package accessible through SiliconStudio.Assets.Package.Assets.
Definition: AssetItem.cs:17
Byte[] BuildFailedThumbnail
The array of data representing the thumbnail to display when a thumbnail build failed.
The context used when compiling an asset in a Package.
ThumbnailCompilerContext()
Initializes a new instance of the ThumbnailCompilerContext class.
A hash to uniquely identify data.
Definition: ObjectId.cs:13
Represents a three dimensional mathematical vector.
Definition: Int2.cs:41
An event arguments class containing information about a thumbnail creation.
ThumbnailBuildResult
This enum describes the result of a thumbnail build operation.
The context used when building the thumbnail of an asset in a Package.