Merge pull request #3445 from jlewin/design_tools
Revise IContentProvider GetThumbnail signature
This commit is contained in:
commit
5bde6edbf0
7 changed files with 30 additions and 45 deletions
|
|
@ -70,10 +70,9 @@ namespace MatterHackers.MatterControl
|
|||
return null;
|
||||
}
|
||||
|
||||
public Task GetThumbnail(ILibraryItem item, int width, int height, ThumbnailSetter imageCallback)
|
||||
public Task<ImageBuffer> GetThumbnail(ILibraryItem item, int width, int height)
|
||||
{
|
||||
imageCallback(thumbnailImage, false);
|
||||
return Task.CompletedTask;
|
||||
return Task.FromResult(thumbnailImage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ namespace MatterHackers.MatterControl.Library
|
|||
|
||||
public interface IContentProvider
|
||||
{
|
||||
Task GetThumbnail(ILibraryItem item, int width, int height, ThumbnailSetter imageCallback);
|
||||
Task<ImageBuffer> GetThumbnail(ILibraryItem item, int width, int height);
|
||||
ImageBuffer DefaultImage { get; }
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -96,10 +96,9 @@ namespace MatterHackers.MatterControl.DesignTools
|
|||
return null;
|
||||
}
|
||||
|
||||
public async Task GetThumbnail(ILibraryItem item, int width, int height, ThumbnailSetter imageCallback)
|
||||
public async Task<ImageBuffer> GetThumbnail(ILibraryItem item, int width, int height)
|
||||
{
|
||||
var imageBuffer = await LoadImage(item);
|
||||
imageCallback(imageBuffer, raytracedImage: true);
|
||||
return await LoadImage(item);
|
||||
}
|
||||
|
||||
public ImageBuffer DefaultImage => AggContext.StaticData.LoadIcon("140.png");
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ namespace MatterHackers.MatterControl
|
|||
loadedItem = Object3D.Load(contentStream.Stream, Path.GetExtension(streamInterface.FileName), CancellationToken.None, null /*itemCache*/, progressReporter);
|
||||
|
||||
// Set MeshPath for non-mcx content. Avoid on mcx to ensure serialization of children
|
||||
if (item is FileSystemFileItem fileItem
|
||||
if (item is FileSystemFileItem fileItem
|
||||
&& !string.Equals(Path.GetExtension(fileItem.FileName), ".mcx", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
loadedItem.MeshPath = fileItem.Path;
|
||||
|
|
@ -101,7 +101,7 @@ namespace MatterHackers.MatterControl
|
|||
}
|
||||
|
||||
|
||||
public async Task GetThumbnail(ILibraryItem libraryItem, int width, int height, ThumbnailSetter imageCallback)
|
||||
public async Task<ImageBuffer> GetThumbnail(ILibraryItem libraryItem, int width, int height)
|
||||
{
|
||||
IObject3D object3D = null;
|
||||
|
||||
|
|
@ -122,15 +122,14 @@ namespace MatterHackers.MatterControl
|
|||
|
||||
string thumbnailId = libraryItem.ID;
|
||||
|
||||
var thumbnail = GetThumbnail(object3D, thumbnailId, width, height, false);
|
||||
imageCallback?.Invoke(thumbnail, true);
|
||||
return GetThumbnail(object3D, thumbnailId, width, height, false);
|
||||
}
|
||||
|
||||
public ImageBuffer GetThumbnail(IObject3D item, string thumbnailId, int width, int height, bool onlyUseCache)
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
return DefaultImage.CreateScaledImage(width, height);
|
||||
return DefaultImage;
|
||||
}
|
||||
|
||||
var image = LoadCachedImage(thumbnailId, width, height);
|
||||
|
|
@ -148,7 +147,7 @@ namespace MatterHackers.MatterControl
|
|||
|
||||
if(onlyUseCache)
|
||||
{
|
||||
return DefaultImage.CreateScaledImage(width, height);
|
||||
return DefaultImage;
|
||||
}
|
||||
|
||||
int estimatedMemorySize = item.EstimatedMemory();
|
||||
|
|
@ -175,7 +174,7 @@ namespace MatterHackers.MatterControl
|
|||
if (thumbnail != null)
|
||||
{
|
||||
// Cache at requested size
|
||||
string cachePath = ApplicationController.Instance.ThumbnailCachePath(thumbnailId, width, height);
|
||||
string cachePath = ApplicationController.Instance.ThumbnailCachePath(item.MeshRenderId().ToString(), width, height);
|
||||
|
||||
// TODO: Lookup best large image and downscale if required
|
||||
if (false)
|
||||
|
|
@ -184,15 +183,9 @@ namespace MatterHackers.MatterControl
|
|||
}
|
||||
|
||||
AggContext.ImageIO.SaveImageData(cachePath, thumbnail);
|
||||
var meshCachePath = ApplicationController.Instance.ThumbnailCachePath(item.MeshRenderId().ToString(), width, height);
|
||||
if (meshCachePath != cachePath)
|
||||
{
|
||||
// also save it to the mesh cache
|
||||
AggContext.ImageIO.SaveImageData(meshCachePath, thumbnail);
|
||||
}
|
||||
}
|
||||
|
||||
return thumbnail ?? DefaultImage.CreateScaledImage(width, height);
|
||||
return thumbnail ?? DefaultImage;
|
||||
}
|
||||
|
||||
internal static ImageBuffer LoadCachedImage(string cacheId, int width, int height)
|
||||
|
|
@ -206,7 +199,7 @@ namespace MatterHackers.MatterControl
|
|||
if (width < 100
|
||||
&& height < 100)
|
||||
{
|
||||
// check for a 100x100 image
|
||||
// check for a 100x100 image
|
||||
var cachedAt100x100 = LoadImage(ApplicationController.Instance.ThumbnailCachePath(cacheId, 100, 100));
|
||||
if (cachedAt100x100 != null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -262,7 +262,7 @@ namespace MatterHackers.MatterControl.CustomWidgets
|
|||
container.AddChild(text);
|
||||
}
|
||||
|
||||
this.SetItemThumbnail(loadingImage, raytracedImage: false);
|
||||
this.SetItemThumbnail(loadingImage);
|
||||
}
|
||||
|
||||
public override string ToolTipText
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ namespace MatterHackers.MatterControl.CustomWidgets
|
|||
var thumbnail = MeshContentProvider.LoadCachedImage(thumbnailId, thumbWidth, thumbHeight);
|
||||
if (thumbnail != null)
|
||||
{
|
||||
this.SetItemThumbnail(thumbnail, raytracedImage: false);
|
||||
this.SetItemThumbnail(thumbnail);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -111,14 +111,10 @@ namespace MatterHackers.MatterControl.CustomWidgets
|
|||
else
|
||||
{
|
||||
// Show processing image
|
||||
this.SetItemThumbnail(generatingThumbnailIcon, raytracedImage: false);
|
||||
this.SetItemThumbnail(generatingThumbnailIcon);
|
||||
|
||||
// Ask the provider for a content specific thumbnail
|
||||
await contentProvider.GetThumbnail(
|
||||
libraryItem,
|
||||
thumbWidth,
|
||||
thumbHeight,
|
||||
this.SetItemThumbnail);
|
||||
thumbnail = await contentProvider.GetThumbnail(libraryItem, thumbWidth, thumbHeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -129,7 +125,7 @@ namespace MatterHackers.MatterControl.CustomWidgets
|
|||
thumbnail = ((libraryItem is ILibraryContainerLink) ? defaultFolderIcon : defaultItemIcon).AlphaToPrimaryAccent();
|
||||
}
|
||||
|
||||
this.SetItemThumbnail(thumbnail, raytracedImage: false);
|
||||
this.SetItemThumbnail(thumbnail);
|
||||
}
|
||||
|
||||
private void ScheduleRaytraceOperation()
|
||||
|
|
@ -141,7 +137,7 @@ namespace MatterHackers.MatterControl.CustomWidgets
|
|||
|
||||
ApplicationController.Instance.QueueForGeneration(async () =>
|
||||
{
|
||||
// When this widget is dequeued for generation, validate before processing. Off-screen widgets should be skipped and will requeue next time they become visible
|
||||
// When dequeued for generation, ensure visible before raytracing. Off-screen widgets are dequeue and will reschedule if redrawn
|
||||
if (!this.ActuallyVisibleOnScreen())
|
||||
{
|
||||
// Skip raytracing operation, requeue on next draw
|
||||
|
|
@ -155,14 +151,17 @@ namespace MatterHackers.MatterControl.CustomWidgets
|
|||
requeueRaytraceOnDraw = false;
|
||||
|
||||
// Show processing image
|
||||
this.SetItemThumbnail(generatingThumbnailIcon, raytracedImage: false);
|
||||
this.SetItemThumbnail(generatingThumbnailIcon);
|
||||
|
||||
// Ask the MeshContentProvider to RayTrace the image
|
||||
await meshContentProvider.GetThumbnail(
|
||||
listViewItem.Model,
|
||||
thumbWidth,
|
||||
thumbHeight,
|
||||
this.SetItemThumbnail);
|
||||
var thumbnail = await meshContentProvider.GetThumbnail(listViewItem.Model, thumbWidth, thumbHeight);
|
||||
if (thumbnail != null)
|
||||
{
|
||||
requeueRaytraceOnDraw = false;
|
||||
raytracePending = false;
|
||||
|
||||
this.SetItemThumbnail(thumbnail);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -219,7 +218,7 @@ namespace MatterHackers.MatterControl.CustomWidgets
|
|||
|
||||
public event EventHandler ImageSet;
|
||||
|
||||
protected void SetItemThumbnail(ImageBuffer thumbnail, bool raytracedImage)
|
||||
protected void SetItemThumbnail(ImageBuffer thumbnail)
|
||||
{
|
||||
if (thumbnail != null)
|
||||
{
|
||||
|
|
@ -233,11 +232,6 @@ namespace MatterHackers.MatterControl.CustomWidgets
|
|||
thumbnail = LibraryProviderHelpers.ResizeImage(thumbnail, thumbWidth, thumbHeight);
|
||||
}
|
||||
|
||||
if (raytracedImage)
|
||||
{
|
||||
this.raytracePending = false;
|
||||
}
|
||||
|
||||
if (GuiWidget.DeviceScale != 1)
|
||||
{
|
||||
thumbnail = thumbnail.CreateScaledImage(GuiWidget.DeviceScale);
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ namespace MatterHackers.MatterControl.Tests.Automation
|
|||
|
||||
// Generate thumbnail
|
||||
var stopWatch = Stopwatch.StartNew();
|
||||
await provider.GetThumbnail(item, 400, 400, (imageBuffer, _) => { });
|
||||
await provider.GetThumbnail(item, 400, 400);
|
||||
|
||||
Assert.Less(stopWatch.ElapsedMilliseconds, 2000, "Elapsed thumbnail generation for Rook.amf should be less than 2 seconds for expected orthographic mode");
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue