2018-03-19 18:29:32 -07:00
|
|
|
|
/*
|
|
|
|
|
|
Copyright (c) 2017, Lars Brubaker, John Lewin
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
|
list of conditions and the following disclaimer.
|
|
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
|
|
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
|
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
The views and conclusions contained in the software and documentation are those
|
|
|
|
|
|
of the authors and should not be interpreted as representing official policies,
|
|
|
|
|
|
either expressed or implied, of the FreeBSD Project.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2021-07-25 18:22:40 -07:00
|
|
|
|
using System.ComponentModel;
|
2018-03-19 18:29:32 -07:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Threading;
|
2019-02-02 16:46:54 -08:00
|
|
|
|
using System.Threading.Tasks;
|
2018-04-26 14:58:38 -07:00
|
|
|
|
using MatterHackers.Agg;
|
2018-03-19 18:29:32 -07:00
|
|
|
|
using MatterHackers.Agg.Image;
|
|
|
|
|
|
using MatterHackers.Agg.ImageProcessing;
|
|
|
|
|
|
using MatterHackers.Agg.Platform;
|
2021-08-01 21:24:33 -07:00
|
|
|
|
using MatterHackers.Agg.UI;
|
2018-03-19 18:29:32 -07:00
|
|
|
|
using MatterHackers.DataConverters3D;
|
2018-06-07 18:41:02 -07:00
|
|
|
|
using MatterHackers.Localizations;
|
2021-08-01 21:24:33 -07:00
|
|
|
|
using MatterHackers.MatterControl.DataStorage;
|
2020-09-12 19:44:18 -07:00
|
|
|
|
using MatterHackers.MatterControl.PartPreviewWindow;
|
2018-03-19 18:29:32 -07:00
|
|
|
|
using MatterHackers.PolygonMesh;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MatterHackers.MatterControl.DesignTools
|
|
|
|
|
|
{
|
2021-08-17 11:31:40 -07:00
|
|
|
|
public interface IEditorWidgetModifier
|
|
|
|
|
|
{
|
|
|
|
|
|
void ModifyEditorWidget(GuiWidget widget, ThemeConfig theme, Action requestWidgetUpdate);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-25 18:22:40 -07:00
|
|
|
|
public interface IImageProvider
|
|
|
|
|
|
{
|
|
|
|
|
|
ImageBuffer Image { get; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HideMeterialAndColor]
|
2021-08-17 11:31:40 -07:00
|
|
|
|
public class ImageObject3D : AssetObject3D, IImageProvider, IObject3DControlsProvider, IEditorWidgetModifier
|
2018-03-19 18:29:32 -07:00
|
|
|
|
{
|
|
|
|
|
|
private const double DefaultSizeMm = 60;
|
|
|
|
|
|
|
|
|
|
|
|
private string _assetPath;
|
|
|
|
|
|
|
|
|
|
|
|
private ImageBuffer _image;
|
|
|
|
|
|
|
|
|
|
|
|
private bool _invert;
|
|
|
|
|
|
|
|
|
|
|
|
public ImageObject3D()
|
|
|
|
|
|
{
|
2018-06-07 18:41:02 -07:00
|
|
|
|
Name = "Image".Localize();
|
2018-03-19 18:29:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-22 15:43:50 -08:00
|
|
|
|
public override bool CanApply => false;
|
2020-09-26 18:46:08 -07:00
|
|
|
|
|
2021-07-25 18:22:40 -07:00
|
|
|
|
[GoogleSearch]
|
|
|
|
|
|
public string ImageSearch { get; set; } = "";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[DisplayName("")]
|
2018-03-19 18:29:32 -07:00
|
|
|
|
[JsonIgnore]
|
2021-08-22 22:07:43 -07:00
|
|
|
|
[ImageDisplay(Margin = new int[] { 9, 3, 9, 3 }, MaxXSize = 400, Stretch = true)]
|
2018-03-19 18:29:32 -07:00
|
|
|
|
public ImageBuffer Image
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_image == null)
|
|
|
|
|
|
{
|
2020-07-15 18:06:28 -07:00
|
|
|
|
// set a temp image so we don't have any problems with threading
|
|
|
|
|
|
var image = this.LoadImage();
|
2018-03-19 18:29:32 -07:00
|
|
|
|
|
2020-07-15 18:06:28 -07:00
|
|
|
|
if (image != null)
|
2018-03-20 16:37:13 -07:00
|
|
|
|
{
|
2018-03-19 18:29:32 -07:00
|
|
|
|
if (this.Invert)
|
|
|
|
|
|
{
|
2020-08-18 20:21:43 -07:00
|
|
|
|
image = InvertLightness.DoInvertLightness(image);
|
2018-03-19 18:29:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-05-02 11:50:40 -07:00
|
|
|
|
else // bad load
|
|
|
|
|
|
{
|
2020-07-15 18:06:28 -07:00
|
|
|
|
image = new ImageBuffer(200, 100);
|
|
|
|
|
|
var graphics2D = image.NewGraphics2D();
|
2018-05-02 11:50:40 -07:00
|
|
|
|
graphics2D.Clear(Color.White);
|
2020-07-15 18:06:28 -07:00
|
|
|
|
graphics2D.DrawString("Image Missing".Localize(), image.Width / 2, image.Height / 2, 20, Agg.Font.Justification.Center, Agg.Font.Baseline.BoundsCenter);
|
2018-05-02 11:50:40 -07:00
|
|
|
|
}
|
2018-06-06 15:46:30 -07:00
|
|
|
|
|
2018-07-12 22:49:39 -07:00
|
|
|
|
// we don't want to invalidate on the mesh change
|
2018-06-20 08:09:35 -07:00
|
|
|
|
using (RebuildLock())
|
|
|
|
|
|
{
|
2020-07-15 18:06:28 -07:00
|
|
|
|
base.Mesh = this.InitMesh(image) ?? PlatonicSolids.CreateCube(100, 100, 0.2);
|
2018-06-20 08:09:35 -07:00
|
|
|
|
}
|
2018-06-06 15:46:30 -07:00
|
|
|
|
|
2020-07-15 18:06:28 -07:00
|
|
|
|
_image = image;
|
|
|
|
|
|
|
2018-06-06 15:46:30 -07:00
|
|
|
|
// send the invalidate on image change
|
2021-12-05 22:01:50 -08:00
|
|
|
|
this.CancelAllParentBuilding();
|
2019-02-13 15:45:33 -08:00
|
|
|
|
Parent?.Invalidate(new InvalidateArgs(this, InvalidateType.Image));
|
2021-07-25 18:22:40 -07:00
|
|
|
|
Invalidate(InvalidateType.DisplayValues);
|
2018-03-19 18:29:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return _image;
|
|
|
|
|
|
}
|
2021-07-25 18:22:40 -07:00
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
2018-03-19 18:29:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool Invert
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _invert;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_invert != value)
|
|
|
|
|
|
{
|
|
|
|
|
|
_invert = value;
|
|
|
|
|
|
_image = null;
|
2021-07-25 18:22:40 -07:00
|
|
|
|
var _ = Image;
|
2018-03-19 18:29:32 -07:00
|
|
|
|
|
2019-01-28 17:44:00 -08:00
|
|
|
|
Invalidate(InvalidateType.Image);
|
2018-03-19 18:29:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-25 18:22:40 -07:00
|
|
|
|
|
|
|
|
|
|
[DisplayName("Open new image")]
|
|
|
|
|
|
[Description("Open")]
|
|
|
|
|
|
public override string AssetPath
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _assetPath;
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_assetPath != value)
|
|
|
|
|
|
{
|
|
|
|
|
|
_assetPath = value;
|
|
|
|
|
|
_image = null;
|
|
|
|
|
|
|
|
|
|
|
|
InitMesh(this.Image);
|
|
|
|
|
|
|
|
|
|
|
|
this.Invalidate(InvalidateType.DisplayValues);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-16 22:44:28 -07:00
|
|
|
|
public void AddObject3DControls(Object3DControlsLayer object3DControlsLayer)
|
2020-09-12 19:44:18 -07:00
|
|
|
|
{
|
2020-10-11 14:53:46 -07:00
|
|
|
|
object3DControlsLayer.AddControls(ControlTypes.Standard2D);
|
2020-09-12 19:44:18 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-19 18:29:32 -07:00
|
|
|
|
private ImageBuffer LoadImage()
|
|
|
|
|
|
{
|
|
|
|
|
|
// TODO: Consider non-awful alternatives
|
|
|
|
|
|
var resetEvent = new AutoResetEvent(false);
|
|
|
|
|
|
|
|
|
|
|
|
ImageBuffer imageBuffer = null;
|
|
|
|
|
|
|
|
|
|
|
|
this.LoadAsset(CancellationToken.None, null).ContinueWith((streamTask) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Stream assetStream = null;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
assetStream = streamTask.Result;
|
2021-04-26 13:52:36 -07:00
|
|
|
|
if (assetStream != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
imageBuffer = ImageIO.LoadImage(assetStream);
|
|
|
|
|
|
}
|
2018-03-19 18:29:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
catch { }
|
|
|
|
|
|
|
|
|
|
|
|
assetStream?.Dispose();
|
|
|
|
|
|
|
|
|
|
|
|
resetEvent.Set();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Wait up to 30 seconds for a given image asset
|
|
|
|
|
|
resetEvent.WaitOne(30 * 1000);
|
|
|
|
|
|
|
|
|
|
|
|
return imageBuffer;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override Mesh Mesh
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(this.AssetPath)
|
|
|
|
|
|
// TODO: Remove this hack needed to work around Persistable = false
|
2019-01-11 16:49:34 -08:00
|
|
|
|
&& (base.Mesh == null || base.Mesh.FaceTextures.Count <= 0))
|
2018-03-19 18:29:32 -07:00
|
|
|
|
{
|
2018-06-27 10:32:54 -07:00
|
|
|
|
using (this.RebuildLock())
|
|
|
|
|
|
{
|
|
|
|
|
|
// TODO: Revise fallback mesh
|
2020-07-15 18:06:28 -07:00
|
|
|
|
base.Mesh = this.InitMesh(this.Image) ?? PlatonicSolids.CreateCube(100, 100, 0.2);
|
2018-06-27 10:32:54 -07:00
|
|
|
|
}
|
2018-03-19 18:29:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return base.Mesh;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public double ScaleMmPerPixels { get; private set; }
|
|
|
|
|
|
|
2019-02-02 16:46:54 -08:00
|
|
|
|
public override Task Rebuild()
|
|
|
|
|
|
{
|
2020-07-15 18:06:28 -07:00
|
|
|
|
InitMesh(this.Image);
|
2019-02-02 16:46:54 -08:00
|
|
|
|
|
|
|
|
|
|
return base.Rebuild();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-15 18:06:28 -07:00
|
|
|
|
private Mesh InitMesh(ImageBuffer imageBuffer)
|
2018-03-19 18:29:32 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(this.AssetPath))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (imageBuffer != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
ScaleMmPerPixels = Math.Min(DefaultSizeMm / imageBuffer.Width, DefaultSizeMm / imageBuffer.Height);
|
|
|
|
|
|
|
|
|
|
|
|
// Create texture mesh
|
|
|
|
|
|
double width = ScaleMmPerPixels * imageBuffer.Width;
|
|
|
|
|
|
double height = ScaleMmPerPixels * imageBuffer.Height;
|
|
|
|
|
|
|
|
|
|
|
|
Mesh textureMesh = PlatonicSolids.CreateCube(width, height, 0.2);
|
2019-01-13 11:57:48 -08:00
|
|
|
|
textureMesh.PlaceTextureOnFaces(0, imageBuffer);
|
2018-03-19 18:29:32 -07:00
|
|
|
|
|
|
|
|
|
|
return textureMesh;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2021-08-01 21:24:33 -07:00
|
|
|
|
|
2021-08-17 11:31:40 -07:00
|
|
|
|
public void ModifyEditorWidget(GuiWidget widget, ThemeConfig theme, Action requestWidgetUpdate)
|
|
|
|
|
|
{
|
|
|
|
|
|
ModifyImageObjectEditorWidget(this, widget, theme, requestWidgetUpdate);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void ModifyImageObjectEditorWidget(ImageObject3D imageObject, GuiWidget widget, ThemeConfig theme, Action requestWidgetUpdate)
|
2021-08-01 21:24:33 -07:00
|
|
|
|
{
|
2021-08-17 11:31:40 -07:00
|
|
|
|
widget.Click += (s, e) =>
|
2021-08-01 21:24:33 -07:00
|
|
|
|
{
|
2021-08-04 17:59:05 -07:00
|
|
|
|
if (e.Button == MouseButtons.Left)
|
|
|
|
|
|
{
|
2021-08-17 11:31:40 -07:00
|
|
|
|
ShowOpenDialog(imageObject);
|
2021-08-04 17:59:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-01 21:24:33 -07:00
|
|
|
|
if (e.Button == MouseButtons.Right)
|
|
|
|
|
|
{
|
|
|
|
|
|
var popupMenu = new PopupMenu(theme);
|
|
|
|
|
|
|
2021-08-17 11:31:40 -07:00
|
|
|
|
var openMenu = popupMenu.CreateMenuItem("Open".Localize());
|
|
|
|
|
|
openMenu.Click += (s2, e2) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
popupMenu.Close();
|
|
|
|
|
|
ShowOpenDialog(imageObject);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
popupMenu.CreateSeparator();
|
|
|
|
|
|
|
|
|
|
|
|
var copyMenu = popupMenu.CreateMenuItem("Copy".Localize());
|
|
|
|
|
|
copyMenu.Click += (s2, e2) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Clipboard.Instance.SetImage(imageObject.Image);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2021-08-01 21:24:33 -07:00
|
|
|
|
var pasteMenu = popupMenu.CreateMenuItem("Paste".Localize());
|
|
|
|
|
|
pasteMenu.Click += (s2, e2) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var activeImage = Clipboard.Instance.GetImage();
|
|
|
|
|
|
|
|
|
|
|
|
// Persist
|
|
|
|
|
|
string filePath = ApplicationDataStorage.Instance.GetNewLibraryFilePath(".png");
|
|
|
|
|
|
ImageIO.SaveImageData(
|
|
|
|
|
|
filePath,
|
|
|
|
|
|
activeImage);
|
|
|
|
|
|
|
2021-08-17 11:31:40 -07:00
|
|
|
|
imageObject.AssetPath = filePath;
|
|
|
|
|
|
imageObject.Mesh = null;
|
2021-08-01 21:24:33 -07:00
|
|
|
|
|
2021-08-17 11:31:40 -07:00
|
|
|
|
requestWidgetUpdate();
|
2021-08-01 21:24:33 -07:00
|
|
|
|
|
2021-08-17 11:31:40 -07:00
|
|
|
|
imageObject.Invalidate(InvalidateType.Image);
|
2021-08-01 21:24:33 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
pasteMenu.Enabled = Clipboard.Instance.ContainsImage;
|
|
|
|
|
|
|
2021-08-17 11:31:40 -07:00
|
|
|
|
popupMenu.ShowMenu(widget, e);
|
2021-08-01 21:24:33 -07:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2021-08-04 17:59:05 -07:00
|
|
|
|
|
2021-08-17 11:31:40 -07:00
|
|
|
|
public static void ShowOpenDialog(IAssetObject assetObject)
|
2021-08-04 17:59:05 -07:00
|
|
|
|
{
|
|
|
|
|
|
UiThread.RunOnIdle(() =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// we do this using to make sure that the stream is closed before we try and insert the Picture
|
|
|
|
|
|
AggContext.FileDialogs.OpenFileDialog(
|
|
|
|
|
|
new OpenFileDialogParams(
|
|
|
|
|
|
"Select an image file|*.jpg;*.png;*.bmp;*.gif;*.pdf",
|
|
|
|
|
|
multiSelect: false,
|
|
|
|
|
|
title: "Add Image".Localize()),
|
|
|
|
|
|
(openParams) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!File.Exists(openParams.FileName))
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-08-17 11:31:40 -07:00
|
|
|
|
assetObject.AssetPath = openParams.FileName;
|
2021-08-04 17:59:05 -07:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2018-03-19 18:29:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|