diff --git a/ANAConversationStudio/ANAConversationStudio.csproj b/ANAConversationStudio/ANAConversationStudio.csproj index 8ecceeb..713b7bd 100644 --- a/ANAConversationStudio/ANAConversationStudio.csproj +++ b/ANAConversationStudio/ANAConversationStudio.csproj @@ -101,6 +101,7 @@ + ..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll diff --git a/ANAConversationStudio/Helpers/StudioContext.cs b/ANAConversationStudio/Helpers/StudioContext.cs index 6c88c0f..f4d11a7 100644 --- a/ANAConversationStudio/Helpers/StudioContext.cs +++ b/ANAConversationStudio/Helpers/StudioContext.cs @@ -191,6 +191,32 @@ private async Task HitPost(string api, TReq requestData, bool strict return respParsed; } } + + public static bool IsProjectLoaded(bool showMsg) + { + if (Current?.ChatServer?.ServerUrl == null) + { + if (showMsg) + MessageBox.Show("No chat flow is loaded. Please load a chat flow and try again."); + return false; + } + return true; + } + + public static string CurrentProjectUrl() + { + return Current?.ChatServer?.ServerUrl + "/api/Conversation/chat?projectId=" + CurrentProjectId(); + } + + public static string CurrentProjectId() + { + return Current?.ChatFlow?.ProjectId; + } + + public static ANAProject CurrentProject() + { + return Current?.ChatFlowProjects?.FirstOrDefault(x => x._id == Current?.ChatFlow?.ProjectId); + } } public class APIResponse diff --git a/ANAConversationStudio/Views/MainWindow.xaml b/ANAConversationStudio/Views/MainWindow.xaml index d551e0a..173043e 100644 --- a/ANAConversationStudio/Views/MainWindow.xaml +++ b/ANAConversationStudio/Views/MainWindow.xaml @@ -671,13 +671,15 @@ + - + + diff --git a/ANAConversationStudio/Views/MainWindow.xaml.cs b/ANAConversationStudio/Views/MainWindow.xaml.cs index c93e175..058ade5 100644 --- a/ANAConversationStudio/Views/MainWindow.xaml.cs +++ b/ANAConversationStudio/Views/MainWindow.xaml.cs @@ -17,6 +17,7 @@ using System.Diagnostics; using System.Collections.Generic; using System.Threading.Tasks; +using System.Windows.Media.Imaging; namespace ANAConversationStudio.Views { @@ -300,17 +301,92 @@ private void ConvSimWithChatMenuClick(object sender, RoutedEventArgs e) private void StartChatInSimulator() { - if (StudioContext.Current?.ChatServer?.ServerUrl == null) + if (StudioContext.IsProjectLoaded(true)) + Process.Start("anaconsim://app?chatflow=" + Uri.EscapeDataString(StudioContext.CurrentProjectUrl())); + } + + private void StartInSimulator_Executed(object sender, ExecutedRoutedEventArgs e) + { + StartChatInSimulator(); + } + + private void ExportAsImageClick(object sender, RoutedEventArgs e) + { + try { - System.Windows.MessageBox.Show("No project is loaded at the moment"); + Rect actualContentRect = DetermineAreaOfNodes(this.networkControl.Nodes); + ExportUIElementAsImage(this.networkControl, actualContentRect); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Oops! Something went wrong!"); + } + } + + public void ExportUIElementAsImage(UIElement element, Rect cropRect) + { + //Add some margin to the image + cropRect.Inflate(cropRect.Width / 40, cropRect.Height / 40); + + if (!StudioContext.IsProjectLoaded(true)) return; + + var currentProj = StudioContext.CurrentProject(); + + var resolution = 200; + var scale = resolution / 96d; + var target = new RenderTargetBitmap((int)(scale * (element.RenderSize.Width)), (int)(scale * (element.RenderSize.Height)), scale * 96, scale * 96, PixelFormats.Pbgra32); + target.Render(element); + + var encoder = new PngBitmapEncoder(); + var outputFrame = BitmapFrame.Create(target); + encoder.Frames.Add(outputFrame); + + var dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "ANA Conversation Studio"); + if (!Directory.Exists(dir)) + Directory.CreateDirectory(dir); + var fileName = currentProj.Name + " " + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".png"; + + var fullPath = Path.Combine(dir, fileName); + + using (var ms = new MemoryStream()) + { + encoder.Save(ms); + ms.Position = 0; + var bmp = new System.Drawing.Bitmap(ms); + var croppedBmp = CropImage(bmp, new System.Drawing.Rectangle((int)(cropRect.X * scale), (int)(cropRect.Y * scale), (int)(cropRect.Width * scale), (int)(cropRect.Height * scale))); + using (var file = File.OpenWrite(fullPath)) + croppedBmp.Save(file, System.Drawing.Imaging.ImageFormat.Jpeg); } - Process.Start("anaconsim://app?chatflow=" + Uri.EscapeDataString(StudioContext.Current.ChatServer.ServerUrl + "/api/Conversation/chat?projectId=" + StudioContext.Current.ChatFlow.ProjectId)); + + Process.Start("explorer", "/select," + fullPath); } - private void StartInSimulator_Executed(object sender, ExecutedRoutedEventArgs e) + static System.Drawing.Bitmap CropImage(System.Drawing.Image originalImage, System.Drawing.Rectangle sourceRectangle, System.Drawing.Rectangle? destinationRectangle = null) { - StartChatInSimulator(); + if (destinationRectangle == null) + destinationRectangle = new System.Drawing.Rectangle(System.Drawing.Point.Empty, sourceRectangle.Size); + + var croppedImage = new System.Drawing.Bitmap(destinationRectangle.Value.Width, + destinationRectangle.Value.Height); + using (var graphics = System.Drawing.Graphics.FromImage(croppedImage)) + { + graphics.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.White), 0, 0, croppedImage.Width, croppedImage.Height); + graphics.DrawImage(originalImage, destinationRectangle.Value, sourceRectangle, System.Drawing.GraphicsUnit.Pixel); + } + return croppedImage; + } + + private void CopyProjectIdClick(object sender, RoutedEventArgs e) + { + if (StudioContext.IsProjectLoaded(true)) + Clipboard.SetText(StudioContext.CurrentProjectId()); + } + + private void CopyChatURLClick(object sender, RoutedEventArgs e) + { + if (StudioContext.IsProjectLoaded(true)) + Clipboard.SetText(StudioContext.CurrentProjectUrl()); } }