From d31bf498846087923dfad03138df853eeff24680 Mon Sep 17 00:00:00 2001 From: Alexander Gorbatov Date: Fri, 19 Jan 2024 23:25:35 +0500 Subject: [PATCH 01/19] implemented a DI container, first working version --- .../App/Actions/DragonFractalAction.cs | 24 +++---- .../App/Actions/IDragonPainterFactory.cs | 9 +++ .../App/Actions/ImageSettingsAction.cs | 16 ++--- .../App/Actions/KochFractalAction.cs | 29 +++----- .../App/Actions/PaletteSettingsAction.cs | 7 +- FractalPainter/App/Actions/SaveImageAction.cs | 8 ++- FractalPainter/App/Fractals/DragonPainter.cs | 8 ++- FractalPainter/App/MainForm.cs | 41 ++--------- FractalPainter/App/Program.cs | 24 ++++++- .../Injection/DependencyInjector.cs | 19 ------ .../Infrastructure/Injection/INeed.cs | 7 -- .../UiActions/UiActionExtensions.cs | 3 +- FractalPainter/fractalPainter.csproj | 4 +- TagsCloudContainer/Common/Word.cs | 17 +++++ .../Drawing/Colorers/IColorer.cs | 6 ++ .../Drawing/DefaultImageDrawer.cs | 42 ++++++++++++ TagsCloudContainer/Drawing/IImageDrawer.cs | 8 +++ .../DrawingOptions/DefaultOptionsProvider.cs | 15 ++++ .../DrawingOptions/IOptionsProvider.cs | 6 ++ TagsCloudContainer/DrawingOptions/Options.cs | 12 ++++ TagsCloudContainer/Program.cs | 64 ++++++++++++++++++ .../DefaultWordCloudDistributor.cs | 38 +++++++++++ .../IWordCloudDistributorProvider.cs | 9 +++ TagsCloudContainer/TagsCloudContainer.csproj | 20 ++++++ TagsCloudContainer/Utils/DrawingUtils.cs | 15 ++++ .../Utils/WordProcessingUtils.cs | 9 +++ .../WordFiltering/DefaultWordFiler.cs | 23 +++++++ .../WordGrouping/DefaultWordGrouper.cs | 27 ++++++++ .../WordGrouping/IWordGrouperProvider.cs | 6 ++ .../WordProcessing/WordInput/IWordProvider.cs | 6 ++ .../WordInput/TxtFileWordParser.cs | 29 ++++++++ .../libs/TagsCloudVisualization.dll | Bin 0 -> 8192 bytes TagsCloudContainerTests/GlobalUsings.cs | 1 + .../TagsCloudContainerTests.csproj | 24 +++++++ TagsCloudWindorms/Program.cs | 11 +++ TagsCloudWindorms/TagsCloudWindorms.csproj | 11 +++ di.sln | 18 +++++ 37 files changed, 492 insertions(+), 124 deletions(-) create mode 100644 FractalPainter/App/Actions/IDragonPainterFactory.cs delete mode 100644 FractalPainter/Infrastructure/Injection/DependencyInjector.cs delete mode 100644 FractalPainter/Infrastructure/Injection/INeed.cs create mode 100644 TagsCloudContainer/Common/Word.cs create mode 100644 TagsCloudContainer/Drawing/Colorers/IColorer.cs create mode 100644 TagsCloudContainer/Drawing/DefaultImageDrawer.cs create mode 100644 TagsCloudContainer/Drawing/IImageDrawer.cs create mode 100644 TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs create mode 100644 TagsCloudContainer/DrawingOptions/IOptionsProvider.cs create mode 100644 TagsCloudContainer/DrawingOptions/Options.cs create mode 100644 TagsCloudContainer/Program.cs create mode 100644 TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs create mode 100644 TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs create mode 100644 TagsCloudContainer/TagsCloudContainer.csproj create mode 100644 TagsCloudContainer/Utils/DrawingUtils.cs create mode 100644 TagsCloudContainer/Utils/WordProcessingUtils.cs create mode 100644 TagsCloudContainer/WordProcessing/WordFiltering/DefaultWordFiler.cs create mode 100644 TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordGrouper.cs create mode 100644 TagsCloudContainer/WordProcessing/WordGrouping/IWordGrouperProvider.cs create mode 100644 TagsCloudContainer/WordProcessing/WordInput/IWordProvider.cs create mode 100644 TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs create mode 100644 TagsCloudContainer/libs/TagsCloudVisualization.dll create mode 100644 TagsCloudContainerTests/GlobalUsings.cs create mode 100644 TagsCloudContainerTests/TagsCloudContainerTests.csproj create mode 100644 TagsCloudWindorms/Program.cs create mode 100644 TagsCloudWindorms/TagsCloudWindorms.csproj diff --git a/FractalPainter/App/Actions/DragonFractalAction.cs b/FractalPainter/App/Actions/DragonFractalAction.cs index a9a37d562..48765c0fe 100644 --- a/FractalPainter/App/Actions/DragonFractalAction.cs +++ b/FractalPainter/App/Actions/DragonFractalAction.cs @@ -1,19 +1,18 @@ using System; using FractalPainting.App.Fractals; using FractalPainting.Infrastructure.Common; -using FractalPainting.Infrastructure.Injection; using FractalPainting.Infrastructure.UiActions; -using Ninject; namespace FractalPainting.App.Actions { - public class DragonFractalAction : IUiAction, INeed + public class DragonFractalAction : IUiAction { - private IImageHolder imageHolder; - - public void SetDependency(IImageHolder dependency) + private IDragonPainterFactory _factory; + private Func _settingsFactory; + public DragonFractalAction(IDragonPainterFactory dragonPainterFactory, Func dragonSettingsFactory) { - imageHolder = dependency; + _settingsFactory = dragonSettingsFactory; + _factory = dragonPainterFactory; } public string Category => "Фракталы"; @@ -23,18 +22,13 @@ public void SetDependency(IImageHolder dependency) public void Perform() { var dragonSettings = CreateRandomSettings(); - // редактируем настройки: SettingsForm.For(dragonSettings).ShowDialog(); - // создаём painter с такими настройками - var container = new StandardKernel(); - container.Bind().ToConstant(imageHolder); - container.Bind().ToConstant(dragonSettings); - container.Get().Paint(); + _factory.CreateDragonPainter(dragonSettings).Paint(); } - private static DragonSettings CreateRandomSettings() + private DragonSettings CreateRandomSettings() { - return new DragonSettingsGenerator(new Random()).Generate(); + return _settingsFactory(new Random()).Generate(); } } } \ No newline at end of file diff --git a/FractalPainter/App/Actions/IDragonPainterFactory.cs b/FractalPainter/App/Actions/IDragonPainterFactory.cs new file mode 100644 index 000000000..95866db20 --- /dev/null +++ b/FractalPainter/App/Actions/IDragonPainterFactory.cs @@ -0,0 +1,9 @@ +using FractalPainting.App.Fractals; +using FractalPainting.Infrastructure.Common; + +namespace FractalPainting.App.Actions; + +public interface IDragonPainterFactory +{ + DragonPainter CreateDragonPainter(DragonSettings settings); +} \ No newline at end of file diff --git a/FractalPainter/App/Actions/ImageSettingsAction.cs b/FractalPainter/App/Actions/ImageSettingsAction.cs index eb0ea0421..943b02448 100644 --- a/FractalPainter/App/Actions/ImageSettingsAction.cs +++ b/FractalPainter/App/Actions/ImageSettingsAction.cs @@ -1,22 +1,17 @@ using FractalPainting.Infrastructure.Common; -using FractalPainting.Infrastructure.Injection; using FractalPainting.Infrastructure.UiActions; namespace FractalPainting.App.Actions { - public class ImageSettingsAction : IUiAction, INeed, INeed + public class ImageSettingsAction : IUiAction { private IImageHolder imageHolder; - private IImageSettingsProvider imageSettingsProvider; + private ImageSettings imageSettings; - public void SetDependency(IImageHolder dependency) + public ImageSettingsAction(IImageHolder imageHolder, ImageSettings imageSettings) { - imageHolder = dependency; - } - - public void SetDependency(IImageSettingsProvider dependency) - { - imageSettingsProvider = dependency; + this.imageHolder = imageHolder; + this.imageSettings = imageSettings; } public string Category => "Настройки"; @@ -25,7 +20,6 @@ public void SetDependency(IImageSettingsProvider dependency) public void Perform() { - var imageSettings = imageSettingsProvider.ImageSettings; SettingsForm.For(imageSettings).ShowDialog(); imageHolder.RecreateImage(imageSettings); } diff --git a/FractalPainter/App/Actions/KochFractalAction.cs b/FractalPainter/App/Actions/KochFractalAction.cs index bec694807..a9dc18356 100644 --- a/FractalPainter/App/Actions/KochFractalAction.cs +++ b/FractalPainter/App/Actions/KochFractalAction.cs @@ -1,37 +1,24 @@ -using FractalPainting.App.Fractals; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Infrastructure.Injection; +using System; +using FractalPainting.App.Fractals; using FractalPainting.Infrastructure.UiActions; -using Ninject; namespace FractalPainting.App.Actions { - public class KochFractalAction : IUiAction, INeed, INeed + public class KochFractalAction : IUiAction { - private IImageHolder imageHolder; - private Palette palette; - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public void SetDependency(Palette dependency) + private readonly Lazy _kochPainter; + public KochFractalAction(Lazy kochPainter) { - palette = dependency; + _kochPainter = kochPainter; } - + public string Category => "Фракталы"; public string Name => "Кривая Коха"; public string Description => "Кривая Коха"; public void Perform() { - var container = new StandardKernel(); - container.Bind().ToConstant(imageHolder); - container.Bind().ToConstant(palette); - - container.Get().Paint(); + _kochPainter.Value.Paint(); } } } \ No newline at end of file diff --git a/FractalPainter/App/Actions/PaletteSettingsAction.cs b/FractalPainter/App/Actions/PaletteSettingsAction.cs index e73d195fb..db01e8a39 100644 --- a/FractalPainter/App/Actions/PaletteSettingsAction.cs +++ b/FractalPainter/App/Actions/PaletteSettingsAction.cs @@ -1,16 +1,15 @@ using FractalPainting.Infrastructure.Common; -using FractalPainting.Infrastructure.Injection; using FractalPainting.Infrastructure.UiActions; namespace FractalPainting.App.Actions { - public class PaletteSettingsAction : IUiAction, INeed + public class PaletteSettingsAction : IUiAction { private Palette palette; - public void SetDependency(Palette dependency) + public PaletteSettingsAction(Palette palette) { - palette = dependency; + this.palette = palette; } public string Category => "Настройки"; diff --git a/FractalPainter/App/Actions/SaveImageAction.cs b/FractalPainter/App/Actions/SaveImageAction.cs index 1c1ef2fab..78c798b70 100644 --- a/FractalPainter/App/Actions/SaveImageAction.cs +++ b/FractalPainter/App/Actions/SaveImageAction.cs @@ -1,16 +1,20 @@ using System.IO; using System.Windows.Forms; using FractalPainting.Infrastructure.Common; -using FractalPainting.Infrastructure.Injection; using FractalPainting.Infrastructure.UiActions; namespace FractalPainting.App.Actions { - public class SaveImageAction : IUiAction, INeed, INeed + public class SaveImageAction : IUiAction { private IImageDirectoryProvider imageDirectoryProvider; private IImageHolder imageHolder; + public SaveImageAction(IImageHolder imageHolder, IImageDirectoryProvider imageDirectoryProvider) + { + this.imageHolder = imageHolder; + this.imageDirectoryProvider = imageDirectoryProvider; + } public void SetDependency(IImageDirectoryProvider dependency) { imageDirectoryProvider = dependency; diff --git a/FractalPainter/App/Fractals/DragonPainter.cs b/FractalPainter/App/Fractals/DragonPainter.cs index 1f83d61c1..4e6cba7a9 100644 --- a/FractalPainter/App/Fractals/DragonPainter.cs +++ b/FractalPainter/App/Fractals/DragonPainter.cs @@ -11,11 +11,13 @@ public class DragonPainter private readonly DragonSettings settings; private readonly float size; private Size imageSize; + private Palette palette; - public DragonPainter(IImageHolder imageHolder, DragonSettings settings) + public DragonPainter(IImageHolder imageHolder, DragonSettings settings, Palette palette) { this.imageHolder = imageHolder; this.settings = settings; + this.palette = palette; imageSize = imageHolder.GetImageSize(); size = Math.Min(imageSize.Width, imageSize.Height)/2.1f; } @@ -24,7 +26,7 @@ public void Paint() { using (var graphics = imageHolder.StartDrawing()) { - graphics.FillRectangle(Brushes.Black, 0, 0, imageSize.Width, imageSize.Height); + graphics.FillRectangle(new SolidBrush(palette.BackgroundColor), 0, 0, imageSize.Width, imageSize.Height); var r = new Random(); var cosa = (float) Math.Cos(settings.Angle1); var sina = (float) Math.Sin(settings.Angle1); @@ -36,7 +38,7 @@ public void Paint() var p = new PointF(0, 0); foreach (var i in Enumerable.Range(0, settings.IterationsCount)) { - graphics.FillRectangle(Brushes.Yellow, imageSize.Width/3f + p.X, imageSize.Height/2f + p.Y, 1, 1); + graphics.FillRectangle(new SolidBrush(palette.PrimaryColor), imageSize.Width/3f + p.X, imageSize.Height/2f + p.Y, 1, 1); if (r.Next(0, 2) == 0) p = new PointF(scale*(p.X*cosa - p.Y*sina), scale*(p.X*sina + p.Y*cosa)); else diff --git a/FractalPainter/App/MainForm.cs b/FractalPainter/App/MainForm.cs index aa484e046..d3deab8c0 100644 --- a/FractalPainter/App/MainForm.cs +++ b/FractalPainter/App/MainForm.cs @@ -1,61 +1,30 @@ using System; using System.Drawing; using System.Windows.Forms; -using FractalPainting.App.Actions; using FractalPainting.Infrastructure.Common; -using FractalPainting.Infrastructure.Injection; using FractalPainting.Infrastructure.UiActions; -using Ninject; namespace FractalPainting.App { public class MainForm : Form { - public MainForm() - : this( - new IUiAction[] - { - new SaveImageAction(), - new DragonFractalAction(), - new KochFractalAction(), - new ImageSettingsAction(), - new PaletteSettingsAction() - }) + public MainForm(IUiAction[] actions, PictureBoxImageHolder pictureBoxImageHolder, ImageSettings imageSettings) { - } - - public MainForm(IUiAction[] actions) - { - var imageSettings = CreateSettingsManager().Load().ImageSettings; ClientSize = new Size(imageSettings.Width, imageSettings.Height); var mainMenu = new MenuStrip(); mainMenu.Items.AddRange(actions.ToMenuItems()); Controls.Add(mainMenu); - var pictureBox = new PictureBoxImageHolder(); - pictureBox.RecreateImage(imageSettings); - pictureBox.Dock = DockStyle.Fill; - Controls.Add(pictureBox); - - DependencyInjector.Inject(actions, pictureBox); - DependencyInjector.Inject(actions, CreateSettingsManager().Load()); - DependencyInjector.Inject(actions, CreateSettingsManager().Load()); - DependencyInjector.Inject(actions, new Palette()); - } - - private static SettingsManager CreateSettingsManager() - { - var container = new StandardKernel(); - container.Bind().To(); - container.Bind().To(); - return container.Get(); + pictureBoxImageHolder.RecreateImage(imageSettings); + pictureBoxImageHolder.Dock = DockStyle.Fill; + Controls.Add(pictureBoxImageHolder); } protected override void OnShown(EventArgs e) { base.OnShown(e); - Text = "Fractal Painter"; + Text = @"Fractal Painter"; } } } \ No newline at end of file diff --git a/FractalPainter/App/Program.cs b/FractalPainter/App/Program.cs index 48d4fb27f..6b2748b5e 100644 --- a/FractalPainter/App/Program.cs +++ b/FractalPainter/App/Program.cs @@ -1,6 +1,11 @@ using System; using System.Windows.Forms; +using FractalPainting.App.Actions; +using FractalPainting.Infrastructure.Common; +using FractalPainting.Infrastructure.UiActions; using Ninject; +using Ninject.Extensions.Conventions; +using Ninject.Extensions.Factory; namespace FractalPainting.App { @@ -15,13 +20,26 @@ private static void Main() try { var container = new StandardKernel(); + + container.Bind().To().InSingletonScope(); + container.Bind().ToSelf().InSingletonScope(); - // start here - // container.Bind().To(); + container.Bind().ToFactory(); + container.Bind(x => x + .FromThisAssembly() + .SelectAllClasses().InheritedFrom() + .BindAllInterfaces()); + + container.Bind().To(); + container.Bind().To(); + + container.Bind() + .ToMethod(context => context.Kernel.Get().Load()).InSingletonScope(); + container.Bind().ToMethod(context => context.Kernel.Get().ImageSettings).InSingletonScope(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new MainForm()); + Application.Run(container.Get()); } catch (Exception e) { diff --git a/FractalPainter/Infrastructure/Injection/DependencyInjector.cs b/FractalPainter/Infrastructure/Injection/DependencyInjector.cs deleted file mode 100644 index 60bbbd3fc..000000000 --- a/FractalPainter/Infrastructure/Injection/DependencyInjector.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Collections; - -namespace FractalPainting.Infrastructure.Injection -{ - public class DependencyInjector - { - public static void Inject(object service, TDependency dependency) - { - var need = service as INeed; - need?.SetDependency(dependency); - } - - public static void Inject(IEnumerable services, TDependency dependency) - { - foreach (var service in services) - Inject(service, dependency); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Infrastructure/Injection/INeed.cs b/FractalPainter/Infrastructure/Injection/INeed.cs deleted file mode 100644 index daed4b561..000000000 --- a/FractalPainter/Infrastructure/Injection/INeed.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace FractalPainting.Infrastructure.Injection -{ - public interface INeed - { - void SetDependency(T dependency); - } -} \ No newline at end of file diff --git a/FractalPainter/Infrastructure/UiActions/UiActionExtensions.cs b/FractalPainter/Infrastructure/UiActions/UiActionExtensions.cs index b7de09172..a5c521b07 100644 --- a/FractalPainter/Infrastructure/UiActions/UiActionExtensions.cs +++ b/FractalPainter/Infrastructure/UiActions/UiActionExtensions.cs @@ -8,7 +8,8 @@ public static class UiActionExtensions { public static ToolStripItem[] ToMenuItems(this IUiAction[] actions) { - var items = actions.GroupBy(a => a.Category) + var items = actions + .GroupBy(a => a.Category) .Select(g => CreateTopLevelMenuItem(g.Key, g.ToList())) .Cast() .ToArray(); diff --git a/FractalPainter/fractalPainter.csproj b/FractalPainter/fractalPainter.csproj index 80c740868..c6cc7f58d 100644 --- a/FractalPainter/fractalPainter.csproj +++ b/FractalPainter/fractalPainter.csproj @@ -2,9 +2,9 @@ WinExe - netcoreapp3.1 + net7.0-windows false - 8 + 11 true FractalPainting FractalPainter diff --git a/TagsCloudContainer/Common/Word.cs b/TagsCloudContainer/Common/Word.cs new file mode 100644 index 000000000..fabc3087f --- /dev/null +++ b/TagsCloudContainer/Common/Word.cs @@ -0,0 +1,17 @@ +using System.Drawing; + +namespace TagsCloudContainer.Common; + +public class Word +{ + public string Value { get; } + public Rectangle Rectangle { get; } + public int Frequency { get; } + + public Word(string value, Rectangle rectangle, int frequency) + { + Value = value; + Rectangle = rectangle; + Frequency = frequency; + } +} \ No newline at end of file diff --git a/TagsCloudContainer/Drawing/Colorers/IColorer.cs b/TagsCloudContainer/Drawing/Colorers/IColorer.cs new file mode 100644 index 000000000..4a8f5c5c8 --- /dev/null +++ b/TagsCloudContainer/Drawing/Colorers/IColorer.cs @@ -0,0 +1,6 @@ +namespace TagsCloudContainer.Drawing.Colorers; + +public interface IColorer +{ + +} \ No newline at end of file diff --git a/TagsCloudContainer/Drawing/DefaultImageDrawer.cs b/TagsCloudContainer/Drawing/DefaultImageDrawer.cs new file mode 100644 index 000000000..2dcb114b5 --- /dev/null +++ b/TagsCloudContainer/Drawing/DefaultImageDrawer.cs @@ -0,0 +1,42 @@ +using System.Drawing; +using System.Drawing.Imaging; +using TagsCloudContainer.Common; +using TagsCloudContainer.DrawingOptions; +using TagsCloudContainer.TagCloudForming; + +namespace TagsCloudContainer.Drawing; + +public class DefaultImageDrawer : IImageDrawer +{ + private readonly IReadOnlyDictionary _distributedWords; + private readonly Options _options; + + public DefaultImageDrawer(IWordCloudDistributorProvider cloudDistributorProvider, IOptionsProvider optionsProvider) + { + _distributedWords = cloudDistributorProvider.DistributedWords; + _options = optionsProvider.Options; + } + + public Bitmap DrawImage() + { + var bitmap = new Bitmap(_options.ImageSize.Width, _options.ImageSize.Height); + var offset = new Point(_options.ImageSize.Width / 2, _options.ImageSize.Height / 2); + var graphics = Graphics.FromImage(bitmap); + graphics.FillRectangle(new SolidBrush(_options.BackgroundColor), 0, 0, bitmap.Width, bitmap.Height); + + foreach (var (value, word) in _distributedWords) + { + var sizeAdd = _options.FrequencyScaling * (word.Frequency - 1); + var newFont = new Font(_options.Font.FontFamily, _options.Font.Size + sizeAdd, _options.Font.Style); + graphics.DrawString(value, newFont, new SolidBrush(_options.FontColor), + word.Rectangle with {X = word.Rectangle.X + offset.X, Y = word.Rectangle.Y + offset.Y}); + } + + return bitmap; + } + + public static void SaveImage(Bitmap bitmap) + { + bitmap.Save(Path.Combine(Directory.GetCurrentDirectory(), "image.jpeg"), ImageFormat.Jpeg); + } +} \ No newline at end of file diff --git a/TagsCloudContainer/Drawing/IImageDrawer.cs b/TagsCloudContainer/Drawing/IImageDrawer.cs new file mode 100644 index 000000000..dc8c30342 --- /dev/null +++ b/TagsCloudContainer/Drawing/IImageDrawer.cs @@ -0,0 +1,8 @@ +using System.Drawing; + +namespace TagsCloudContainer.Drawing; + +public interface IImageDrawer +{ + public Bitmap DrawImage(); +} \ No newline at end of file diff --git a/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs b/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs new file mode 100644 index 000000000..a8fa7eb80 --- /dev/null +++ b/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs @@ -0,0 +1,15 @@ +using System.Drawing; + +namespace TagsCloudContainer.DrawingOptions; + +public class DefaultOptionsProvider : IOptionsProvider +{ + public Options Options => new() + { + FontColor = Color.White, + BackgroundColor = Color.Black, + Font = new Font(FontFamily.GenericSansSerif, 26), + FrequencyScaling = 5, + ImageSize = new Size(2000,2000) + }; +} \ No newline at end of file diff --git a/TagsCloudContainer/DrawingOptions/IOptionsProvider.cs b/TagsCloudContainer/DrawingOptions/IOptionsProvider.cs new file mode 100644 index 000000000..87e09a5cf --- /dev/null +++ b/TagsCloudContainer/DrawingOptions/IOptionsProvider.cs @@ -0,0 +1,6 @@ +namespace TagsCloudContainer.DrawingOptions; + +public interface IOptionsProvider +{ + public Options Options { get; } +} \ No newline at end of file diff --git a/TagsCloudContainer/DrawingOptions/Options.cs b/TagsCloudContainer/DrawingOptions/Options.cs new file mode 100644 index 000000000..7bb616d3d --- /dev/null +++ b/TagsCloudContainer/DrawingOptions/Options.cs @@ -0,0 +1,12 @@ +using System.Drawing; + +namespace TagsCloudContainer.DrawingOptions; + +public class Options +{ + public Color FontColor { get; set; } + public Color BackgroundColor { get; set; } + public Size ImageSize { get; set; } + public Font Font { get; set; } + public int FrequencyScaling { get; set; } +} \ No newline at end of file diff --git a/TagsCloudContainer/Program.cs b/TagsCloudContainer/Program.cs new file mode 100644 index 000000000..b86a7ee0a --- /dev/null +++ b/TagsCloudContainer/Program.cs @@ -0,0 +1,64 @@ +using System.Drawing; +using Autofac; +using TagsCloudContainer.Drawing; +using TagsCloudContainer.DrawingOptions; +using TagsCloudContainer.TagCloudForming; +using TagsCloudContainer.WordProcessing.WordFiltering; +using TagsCloudContainer.WordProcessing.WordGrouping; +using TagsCloudContainer.WordProcessing.WordInput; +using TagsCloudVisualization; + +namespace TagsCloudContainer; + +public static class Program +{ + public static void Main(string[] args) + { + var containerBuilder = new ContainerBuilder(); + + containerBuilder.RegisterType() + .Named("filtered_words") + .WithParameter((pi, _) => pi.Name == "words", (_, c) => c.Resolve()) + .WithParameter((pi, _) => pi.Name == "wordsToExclude", (_, c) => c.ResolveNamed("filter")) + .SingleInstance(); + + containerBuilder.RegisterType() + .As() + .WithParameter((pi, _) => pi.Name == "words", (_, c) => c.ResolveNamed("filtered_words").Words) + .SingleInstance(); + + containerBuilder.RegisterType() + .As() + .WithParameter("center", new Point(0, 0)) + .SingleInstance(); + + containerBuilder.RegisterType() + .As() + .SingleInstance(); + + containerBuilder.RegisterType() + .As() + .SingleInstance(); + + containerBuilder.RegisterType() + .As() + .SingleInstance(); + + containerBuilder + .RegisterInstance(new TxtFileWordParser("words.txt")) + .As() + .SingleInstance(); + + containerBuilder + .RegisterInstance(new TxtFileWordParser("filter.txt")) + .Named("filter") + .SingleInstance(); + + var containter = containerBuilder.Build(); + + var drawer = containter.Resolve(); + + var bitmap = drawer.DrawImage(); + DefaultImageDrawer.SaveImage(bitmap); + } +} \ No newline at end of file diff --git a/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs b/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs new file mode 100644 index 000000000..58814758c --- /dev/null +++ b/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs @@ -0,0 +1,38 @@ +using TagsCloudContainer.Common; +using TagsCloudContainer.DrawingOptions; +using TagsCloudContainer.Utils; +using TagsCloudContainer.WordProcessing.WordGrouping; +using TagsCloudVisualization; + +namespace TagsCloudContainer.TagCloudForming; + +public class DefaultWordCloudDistributor : IWordCloudDistributorProvider +{ + private readonly Dictionary _words; + private readonly ICloudLayouter _cloudLayouter; + private readonly Options _options; + + public DefaultWordCloudDistributor(IWordGrouperProvider wordGrouper, ICloudLayouter cloudLayouter, + IOptionsProvider optionsProvider) + { + _words = wordGrouper.GrouppedWords; + _cloudLayouter = cloudLayouter; + _options = optionsProvider.Options; + } + + public IReadOnlyDictionary DistributedWords => DistributeWords().AsReadOnly(); + + private Dictionary DistributeWords() + { + var distributed = new Dictionary(); + + foreach (var (word, frequency) in _words) + { + var newWord = new Word(word, _cloudLayouter.PutNextRectangle(DrawingUtils.GetStringSize(word, frequency, + _options.FrequencyScaling, _options.Font)), frequency); + distributed.Add(word, newWord); + } + + return distributed; + } +} \ No newline at end of file diff --git a/TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs b/TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs new file mode 100644 index 000000000..55e0f9fd3 --- /dev/null +++ b/TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs @@ -0,0 +1,9 @@ +using System.Drawing; +using TagsCloudContainer.Common; + +namespace TagsCloudContainer.TagCloudForming; + +public interface IWordCloudDistributorProvider +{ + public IReadOnlyDictionary DistributedWords { get; } +} \ No newline at end of file diff --git a/TagsCloudContainer/TagsCloudContainer.csproj b/TagsCloudContainer/TagsCloudContainer.csproj new file mode 100644 index 000000000..154b69aac --- /dev/null +++ b/TagsCloudContainer/TagsCloudContainer.csproj @@ -0,0 +1,20 @@ + + + + net7.0 + enable + enable + Exe + + + + + + + + + + libs\TagsCloudVisualization.dll + + + diff --git a/TagsCloudContainer/Utils/DrawingUtils.cs b/TagsCloudContainer/Utils/DrawingUtils.cs new file mode 100644 index 000000000..517ebd4d0 --- /dev/null +++ b/TagsCloudContainer/Utils/DrawingUtils.cs @@ -0,0 +1,15 @@ +using System.Drawing; + +namespace TagsCloudContainer.Utils; + +public static class DrawingUtils +{ + private static readonly Graphics Graphics = Graphics.FromImage(new Bitmap(1, 1)); + + public static Size GetStringSize(string word, int frequency, int frequencyScaling, Font font) + { + var sizeIncrement = frequencyScaling * (frequency - 1); + var newFont = new Font(font.FontFamily, font.Size + sizeIncrement, font.Style); + return Size.Ceiling(Graphics.MeasureString(word, newFont)); + } +} \ No newline at end of file diff --git a/TagsCloudContainer/Utils/WordProcessingUtils.cs b/TagsCloudContainer/Utils/WordProcessingUtils.cs new file mode 100644 index 000000000..e27986db6 --- /dev/null +++ b/TagsCloudContainer/Utils/WordProcessingUtils.cs @@ -0,0 +1,9 @@ +namespace TagsCloudContainer.Utils; + +public static class WordProcessingUtils +{ + public static HashSet RemoveDuplicates(IEnumerable lines) + => lines + .Distinct() + .ToHashSet(); +} \ No newline at end of file diff --git a/TagsCloudContainer/WordProcessing/WordFiltering/DefaultWordFiler.cs b/TagsCloudContainer/WordProcessing/WordFiltering/DefaultWordFiler.cs new file mode 100644 index 000000000..b85788f0c --- /dev/null +++ b/TagsCloudContainer/WordProcessing/WordFiltering/DefaultWordFiler.cs @@ -0,0 +1,23 @@ +using TagsCloudContainer.WordProcessing.WordInput; + +namespace TagsCloudContainer.WordProcessing.WordFiltering; + +public class DefaultWordFiler : IWordProvider +{ + private readonly string[] _wordsToExclude; + private readonly string[] _words; + + public DefaultWordFiler(IWordProvider words, IWordProvider wordsToExclude) + { + _words = words.Words; + _wordsToExclude = wordsToExclude.Words; + } + + public string[] Words => FilterWords(); + + private string[] FilterWords() + => _words + .Where(w => !_wordsToExclude.Contains(w)) + .Select(w => w.ToLower()) + .ToArray(); +} \ No newline at end of file diff --git a/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordGrouper.cs b/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordGrouper.cs new file mode 100644 index 000000000..2f2b7443a --- /dev/null +++ b/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordGrouper.cs @@ -0,0 +1,27 @@ +using TagsCloudContainer.WordProcessing.WordInput; + +namespace TagsCloudContainer.WordProcessing.WordGrouping; + +public class DefaultWordGrouper : IWordGrouperProvider +{ + private readonly string[] _words; + + public DefaultWordGrouper(string[] words) + { + _words = words; + } + + public Dictionary GrouppedWords => GroupWords(); + + private Dictionary GroupWords() + { + var frequency = new Dictionary(); + foreach (var word in _words) + { + frequency.TryAdd(word, 0); + frequency[word]++; + } + + return frequency; + } +} \ No newline at end of file diff --git a/TagsCloudContainer/WordProcessing/WordGrouping/IWordGrouperProvider.cs b/TagsCloudContainer/WordProcessing/WordGrouping/IWordGrouperProvider.cs new file mode 100644 index 000000000..f41d1afde --- /dev/null +++ b/TagsCloudContainer/WordProcessing/WordGrouping/IWordGrouperProvider.cs @@ -0,0 +1,6 @@ +namespace TagsCloudContainer.WordProcessing.WordGrouping; + +public interface IWordGrouperProvider +{ + public Dictionary GrouppedWords { get; } +} \ No newline at end of file diff --git a/TagsCloudContainer/WordProcessing/WordInput/IWordProvider.cs b/TagsCloudContainer/WordProcessing/WordInput/IWordProvider.cs new file mode 100644 index 000000000..cf0d8960d --- /dev/null +++ b/TagsCloudContainer/WordProcessing/WordInput/IWordProvider.cs @@ -0,0 +1,6 @@ +namespace TagsCloudContainer.WordProcessing.WordInput; + +public interface IWordProvider +{ + public string[] Words { get; } +} \ No newline at end of file diff --git a/TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs b/TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs new file mode 100644 index 000000000..e143043b2 --- /dev/null +++ b/TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs @@ -0,0 +1,29 @@ +namespace TagsCloudContainer.WordProcessing.WordInput; + + +public class TxtFileWordParser : IWordProvider +{ + private readonly string _filePath; + + public TxtFileWordParser(string filePath) + { + _filePath = filePath; + } + + public string[] Words => Parse(); + + private string[] Parse() + { + string[] line; + try + { + line = File.ReadAllLines(_filePath); + } + catch (Exception e) + { + throw new IOException($"Failed to read from file {_filePath} Most likely the file path is incorrect.", e); + } + + return line; + } +} \ No newline at end of file diff --git a/TagsCloudContainer/libs/TagsCloudVisualization.dll b/TagsCloudContainer/libs/TagsCloudVisualization.dll new file mode 100644 index 0000000000000000000000000000000000000000..94b8e7e03c42d23287c810ddeb1c3f47f9f9254a GIT binary patch literal 8192 zcmeHMeQX@Zb$`3Jw|6{=6pyqh$+GO#(Q?kBc%mdbACxGOI#MGkUFUm2SMDX z2J&s?_hxtRj`~RC&!6}H_ zZaCXU&$Ya?{>#$XOY5f=OjoNocG1yGT3#=gZBLssG^bkD%(6DTFQ=94f{|%$P23om zo){q-lOpu>XPz-aZ$F?EEtC3)y1rp_zg&3g>Hb|eT)whor?y{=AR=(%R&G3HllYo^ZyvG)k7DWanQFn^~rd~aS!zI zl>m^5u?BI25=|pIl5t!o4@$%~2t>uR8c$OjMD!WQuxu!DtaJnq+qxc4QyV1O-9!pB z$9YBL*kp>*i

mZA21ixMFW_tEJ>L5}u%qa{(f+`uMdlx1lTY*^@A;Y}^?NBA3vJ zd5Ii_sceKT7)=VJTjdidRK@N9wec@yw-di7nG-0G961?dQaOppm5oH5eF~Wv0bD0Z zPNSMs`x^A>dP`as+q4!Py1HDQ9@SJ%fzUAu7xO#LN5eH z15oW%OsLCyC=R#q8=+5xnUn86FjBE5!vA~T8fTJBg^xvrtIuC9WKWR(`* z$_+w=3EzswdV!TdWqm_MSP?41ipWWniii#AMovU*!jOEA7!^l06R^2+z-AQey4$9X zvjN-J=CP(3opn=yi5^lnih0-``dlFke@I9-6jmRc1%o?)f$|d!UlcGdvU3b}P#P!+ zSw4X^tH>vKwr5T<-XXyzxBuMb3upYF^afadpLqr0O+uLFLXaadzZM9^fLd0U$9o@9 zTYIR*pI3!su4DEeS>7RcrQd;0#7LTvc;d3$Wj)8JL+YUP%}~OyU@Otu5fOQqS$zt7HCN(jMzS>)IkF=^YnvC|}df~!_NT=FENlYuPMV*_-Hd4>?;V-ZZ%p`hts`h3e z%MevlWf@K7WcZ^K%cw(&q_O2Emd7_Kt6I-ri*>pMWbfJY%hXLV)!qt%&UJL^67|5-;6R$OQ$0Vx}Tm0{ylKoXa^M# z^~+dZ9n`53_}c{MoXq$J;41x#u+UEL2WN%gtcb8RN#Ng59*(N?SvrjcN|a>)2}Xno z`wiV29jH@Nh=(c8WINr9Q4Rn{4nXw^Dk3t~PLIOYI3(<&oxX_TXDUvA1=~-6;>t+U zO>{K+GT^_<-vN9h@_lIUioOE)6unC8sb69EsPZ4+91(CFa6;nn@@r6EsLNE0=!=4Stz~1BB$>Wam$iWUfiIK3MeUM8 zGwTWMLv%X2N{Z5(f_i|?M%PIyEu?(eeM$<{nSgp0{o>Tu?aPiSJyHt|-sn@?)#s2G zzrE1P-o-)z`*8nEyX25pZBH()j91?I;z&iz;6;Kz@6z~|JN)G|9q)!RFCg9@&E~h>rv*9Q9r#-uSO<-Phu=j(}=?S8SuYE9}t|ML-Tdo zq)tHkY@{FXymFAfM%MuU8vRyCpNf{C^ID_?sc*;c%l4n9Uqn6x$TpuQMHz`McwLIlBEAe{kG!opRkTPp#2WoNHsZw9b&tbp-vhGTuXVt zZn6M-=o!Fm^a9{^`d7d#od`T1?Ne@c@ms4`5);7`u-4SZ&&`Ih%h2C-b4H0!J&^)44I3g9*8p0t&$IAa3bI( zu~ye2+Q+cklv7+U&5hS6&KC9dJpD-EI9Ju%^VExVezD%eyjfn<_fEkBdvv5+Eg6nJ zXBkJf(CDP07xtB{TGqBKBk!4ZnYn?EXFJV0W2WnY-Z5?$s+O^niiUT1hFk#|pB4Bl zS1lZa6l+=;5U7uSIX!>Nk`|C80SXO;}|tr|2^nllOoqcG&*6*gD( z49eQ2ifI{6J&cB^j!nCI(Wp~2Y|K@QMGmpS+gr7)rJNzxHA-_$-bF`Xw?x2~v_ zwafEnvFZrDhLK5QKBvzcUTqTd?Kb#+-h}d`Vd=*O)+7&|PdKQQyw~U$W(xlJF4qu* zJ=n5dt~CU~BnrM~&Y70!HA#2ZFpKqx&zX0lS}eU7R$$OM6O0?@mQXV6=yx$Wu6qlj zjt`gxV511jaZyO_0Tbl7nRjg0p7%2QO}DCBIj>qU?F<)iI31aNb0360mV{x2#+wVj zSv;i|-K=F-3%)h;ZjrZ4!Ln${VnEvXV?ba|gty!FJiBx?0YRrsIXvS!KrnJ)W6aJA zhhml1iOUBS4h0nuYzuT)th=0NRD`uDyF!N@v$){-v1M%sOC0OgnD?eD6~hS_xdn6H zo3gV8r zgG+vuiIqV6=I5~``!kNY5XdCYlfZ5_>S(s=ILJCp*d`!v;8`16CXL&R25+sXpyM>; zhHr?A7ROMz#%~#JBY*c;|Ic6f=Ga4(;)BlL{MT1W(IhDz(MXB{NhXY{jjWJD9PDY zRpS73R`FxQ_~9(6Cue=pbUclGaE~~j7Pwu{N8~BF>wu$I_S)r<<9VaPoA}g%W8dXU z(2EJ@0}`#y>>ZiHYBPo^l}*8dA6R@>rVj?%uBpFm_%&$hwY_-x#JPtCjkOYWU10;D zghWyCw!`HR-nQUwh(FvLz!wDJvP_8scsnB^7{g_ZL%K<#j~wMA@RZy-WLfrI<5kNu zD|nGl$t|Ph1)Kx$qGPo&d{nv$H9kE3F*VEtd?PF2d7X2(ea(yiK+^aG8@LYQbKCIn zx5J=uw4HeHF@k=13m)7@Cv(HO=fCi~x$@7>?f%@chkknI#Whc`Ubg?xw2NhUs0w&! z*l>?}cIA*)D2V6sIXrX)wmtVjSim{rP}%U1xL&-9IEPoz&r}L?h~R&|g?9vu~M)f7we4LTvhInh0+&s$J z5N}2yy|96_!AmXtui(hyyN5<}ia#+=;CE?QqB8kA_$7SSyNC&nufK4Kh^r9wi}U^` z2l4%1ocjd}IEoiHjvO5Z9KpRSN2B;czZdi<;2!^LJ$+Sq<1PP|;oI_YqFufjQt%r} z0u~Jm-hR??o7sb}Ru7Wc@ehPRIezWjwQx)G+_QQt@Ob5rLyYbr` z&wgPq9LX@OxZ3w%fb8#&#=$2hJ22wS{P3_v* znrKbz(1VMh1`}m>AXRnB{ce82DCur*DZE|u=Iv6y?v^r(TT)s{FPrnYV1zGn;Sa_% zt!`|zfMXl(K}%xdmvc%h8n59fV441LKciPFsm;Cx&#Af|UkR@=o~D8I`{%2C#u+mf4NJ2a4y1H86be3Xlh^d3 OyUL#bul9c+1HT90SmHAP literal 0 HcmV?d00001 diff --git a/TagsCloudContainerTests/GlobalUsings.cs b/TagsCloudContainerTests/GlobalUsings.cs new file mode 100644 index 000000000..cefced496 --- /dev/null +++ b/TagsCloudContainerTests/GlobalUsings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file diff --git a/TagsCloudContainerTests/TagsCloudContainerTests.csproj b/TagsCloudContainerTests/TagsCloudContainerTests.csproj new file mode 100644 index 000000000..057e75b81 --- /dev/null +++ b/TagsCloudContainerTests/TagsCloudContainerTests.csproj @@ -0,0 +1,24 @@ + + + + net7.0 + enable + enable + + false + true + + + + + + + + + + + + + + + diff --git a/TagsCloudWindorms/Program.cs b/TagsCloudWindorms/Program.cs new file mode 100644 index 000000000..9ede425f7 --- /dev/null +++ b/TagsCloudWindorms/Program.cs @@ -0,0 +1,11 @@ +namespace TagsCloudWindorms; + +public static class Program +{ + [STAThread] + public static void Main() + { + ApplicationConfiguration.Initialize(); + Application.Run(); + } +} \ No newline at end of file diff --git a/TagsCloudWindorms/TagsCloudWindorms.csproj b/TagsCloudWindorms/TagsCloudWindorms.csproj new file mode 100644 index 000000000..638e474cb --- /dev/null +++ b/TagsCloudWindorms/TagsCloudWindorms.csproj @@ -0,0 +1,11 @@ + + + + WinExe + net7.0-windows + enable + true + enable + + + \ No newline at end of file diff --git a/di.sln b/di.sln index b27b7c05d..6e5970969 100644 --- a/di.sln +++ b/di.sln @@ -2,6 +2,12 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FractalPainter", "FractalPainter\FractalPainter.csproj", "{4D70883B-6F8B-4166-802F-8EDC9BE93199}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudContainer", "TagsCloudContainer\TagsCloudContainer.csproj", "{D6C7C0D1-2CA2-4F45-8AA5-340A58A45B95}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudContainerTests", "TagsCloudContainerTests\TagsCloudContainerTests.csproj", "{405FAAB3-6E4C-487C-9429-43529876FB44}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudWindorms", "TagsCloudWindorms\TagsCloudWindorms.csproj", "{E8F573BE-2D9B-46E4-B0F5-BC08BF84A719}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -12,5 +18,17 @@ Global {4D70883B-6F8B-4166-802F-8EDC9BE93199}.Debug|Any CPU.Build.0 = Debug|Any CPU {4D70883B-6F8B-4166-802F-8EDC9BE93199}.Release|Any CPU.ActiveCfg = Release|Any CPU {4D70883B-6F8B-4166-802F-8EDC9BE93199}.Release|Any CPU.Build.0 = Release|Any CPU + {D6C7C0D1-2CA2-4F45-8AA5-340A58A45B95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D6C7C0D1-2CA2-4F45-8AA5-340A58A45B95}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D6C7C0D1-2CA2-4F45-8AA5-340A58A45B95}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D6C7C0D1-2CA2-4F45-8AA5-340A58A45B95}.Release|Any CPU.Build.0 = Release|Any CPU + {405FAAB3-6E4C-487C-9429-43529876FB44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {405FAAB3-6E4C-487C-9429-43529876FB44}.Debug|Any CPU.Build.0 = Debug|Any CPU + {405FAAB3-6E4C-487C-9429-43529876FB44}.Release|Any CPU.ActiveCfg = Release|Any CPU + {405FAAB3-6E4C-487C-9429-43529876FB44}.Release|Any CPU.Build.0 = Release|Any CPU + {E8F573BE-2D9B-46E4-B0F5-BC08BF84A719}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E8F573BE-2D9B-46E4-B0F5-BC08BF84A719}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E8F573BE-2D9B-46E4-B0F5-BC08BF84A719}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E8F573BE-2D9B-46E4-B0F5-BC08BF84A719}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal From b6d8d17d5b247cb2a6f5432441789a9c8d697ea6 Mon Sep 17 00:00:00 2001 From: Alexander Gorbatov Date: Sat, 20 Jan 2024 14:13:46 +0500 Subject: [PATCH 02/19] refactored word processing --- .../Drawing/DefaultImageDrawer.cs | 2 +- TagsCloudContainer/Program.cs | 36 +++++++----------- .../DefaultWordCloudDistributor.cs | 4 +- .../WordFiltering/DefaultWordFiler.cs | 23 ------------ .../WordFiltering/DefaultWordFilter.cs | 19 ++++++++++ .../WordFiltering/IWordFilter.cs | 6 +++ .../WordGrouping/DefaultWordGrouper.cs | 27 -------------- .../WordGrouping/DefaultWordProcessor.cs | 37 +++++++++++++++++++ .../WordGrouping/IProcessedWordProvider.cs | 6 +++ .../WordGrouping/IWordGrouperProvider.cs | 6 --- .../WordProcessing/WordInput/IWordProvider.cs | 4 +- .../WordInput/TxtFileWordParser.cs | 4 +- 12 files changed, 91 insertions(+), 83 deletions(-) delete mode 100644 TagsCloudContainer/WordProcessing/WordFiltering/DefaultWordFiler.cs create mode 100644 TagsCloudContainer/WordProcessing/WordFiltering/DefaultWordFilter.cs create mode 100644 TagsCloudContainer/WordProcessing/WordFiltering/IWordFilter.cs delete mode 100644 TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordGrouper.cs create mode 100644 TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs create mode 100644 TagsCloudContainer/WordProcessing/WordGrouping/IProcessedWordProvider.cs delete mode 100644 TagsCloudContainer/WordProcessing/WordGrouping/IWordGrouperProvider.cs diff --git a/TagsCloudContainer/Drawing/DefaultImageDrawer.cs b/TagsCloudContainer/Drawing/DefaultImageDrawer.cs index 2dcb114b5..51e01b2a5 100644 --- a/TagsCloudContainer/Drawing/DefaultImageDrawer.cs +++ b/TagsCloudContainer/Drawing/DefaultImageDrawer.cs @@ -37,6 +37,6 @@ public Bitmap DrawImage() public static void SaveImage(Bitmap bitmap) { - bitmap.Save(Path.Combine(Directory.GetCurrentDirectory(), "image.jpeg"), ImageFormat.Jpeg); + bitmap.Save(Path.Combine(Directory.GetCurrentDirectory(), "image.png"), ImageFormat.Png); } } \ No newline at end of file diff --git a/TagsCloudContainer/Program.cs b/TagsCloudContainer/Program.cs index b86a7ee0a..c481235cc 100644 --- a/TagsCloudContainer/Program.cs +++ b/TagsCloudContainer/Program.cs @@ -15,27 +15,25 @@ public static class Program public static void Main(string[] args) { var containerBuilder = new ContainerBuilder(); + + containerBuilder + .RegisterInstance(new TxtFileWordParser("words.txt")) + .As() + .SingleInstance(); - containerBuilder.RegisterType() - .Named("filtered_words") - .WithParameter((pi, _) => pi.Name == "words", (_, c) => c.Resolve()) - .WithParameter((pi, _) => pi.Name == "wordsToExclude", (_, c) => c.ResolveNamed("filter")) + containerBuilder + .RegisterInstance(new DefaultWordFilter(new TxtFileWordParser("filter.txt"))) + .As() .SingleInstance(); - containerBuilder.RegisterType() - .As() - .WithParameter((pi, _) => pi.Name == "words", (_, c) => c.ResolveNamed("filtered_words").Words) + containerBuilder.RegisterType() + .As() .SingleInstance(); - containerBuilder.RegisterType() + containerBuilder.RegisterInstance(new CircularCloudLayouter(Point.Empty)) .As() - .WithParameter("center", new Point(0, 0)) - .SingleInstance(); - - containerBuilder.RegisterType() - .As() .SingleInstance(); - + containerBuilder.RegisterType() .As() .SingleInstance(); @@ -44,14 +42,8 @@ public static void Main(string[] args) .As() .SingleInstance(); - containerBuilder - .RegisterInstance(new TxtFileWordParser("words.txt")) - .As() - .SingleInstance(); - - containerBuilder - .RegisterInstance(new TxtFileWordParser("filter.txt")) - .Named("filter") + containerBuilder.RegisterType() + .As() .SingleInstance(); var containter = containerBuilder.Build(); diff --git a/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs b/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs index 58814758c..7f0735430 100644 --- a/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs +++ b/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs @@ -12,10 +12,10 @@ public class DefaultWordCloudDistributor : IWordCloudDistributorProvider private readonly ICloudLayouter _cloudLayouter; private readonly Options _options; - public DefaultWordCloudDistributor(IWordGrouperProvider wordGrouper, ICloudLayouter cloudLayouter, + public DefaultWordCloudDistributor(IProcessedWordProvider processedWord, ICloudLayouter cloudLayouter, IOptionsProvider optionsProvider) { - _words = wordGrouper.GrouppedWords; + _words = processedWord.ProcessedWords; _cloudLayouter = cloudLayouter; _options = optionsProvider.Options; } diff --git a/TagsCloudContainer/WordProcessing/WordFiltering/DefaultWordFiler.cs b/TagsCloudContainer/WordProcessing/WordFiltering/DefaultWordFiler.cs deleted file mode 100644 index b85788f0c..000000000 --- a/TagsCloudContainer/WordProcessing/WordFiltering/DefaultWordFiler.cs +++ /dev/null @@ -1,23 +0,0 @@ -using TagsCloudContainer.WordProcessing.WordInput; - -namespace TagsCloudContainer.WordProcessing.WordFiltering; - -public class DefaultWordFiler : IWordProvider -{ - private readonly string[] _wordsToExclude; - private readonly string[] _words; - - public DefaultWordFiler(IWordProvider words, IWordProvider wordsToExclude) - { - _words = words.Words; - _wordsToExclude = wordsToExclude.Words; - } - - public string[] Words => FilterWords(); - - private string[] FilterWords() - => _words - .Where(w => !_wordsToExclude.Contains(w)) - .Select(w => w.ToLower()) - .ToArray(); -} \ No newline at end of file diff --git a/TagsCloudContainer/WordProcessing/WordFiltering/DefaultWordFilter.cs b/TagsCloudContainer/WordProcessing/WordFiltering/DefaultWordFilter.cs new file mode 100644 index 000000000..24c6e586e --- /dev/null +++ b/TagsCloudContainer/WordProcessing/WordFiltering/DefaultWordFilter.cs @@ -0,0 +1,19 @@ +using TagsCloudContainer.Utils; +using TagsCloudContainer.WordProcessing.WordInput; + +namespace TagsCloudContainer.WordProcessing.WordFiltering; + +public class DefaultWordFilter : IWordFilter +{ + private readonly HashSet _wordsToExclude; + + public DefaultWordFilter(IWordProvider wordsToExclude) + { + _wordsToExclude = WordProcessingUtils.RemoveDuplicates(wordsToExclude.Words.Select(w => w.ToLower())); + } + + public string[] FilterWords(string[] words) + => words + .Where(w => !_wordsToExclude.Contains(w)) + .ToArray(); +} \ No newline at end of file diff --git a/TagsCloudContainer/WordProcessing/WordFiltering/IWordFilter.cs b/TagsCloudContainer/WordProcessing/WordFiltering/IWordFilter.cs new file mode 100644 index 000000000..fbad4bf0d --- /dev/null +++ b/TagsCloudContainer/WordProcessing/WordFiltering/IWordFilter.cs @@ -0,0 +1,6 @@ +namespace TagsCloudContainer.WordProcessing.WordFiltering; + +public interface IWordFilter +{ + public string[] FilterWords(string[] words); +} \ No newline at end of file diff --git a/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordGrouper.cs b/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordGrouper.cs deleted file mode 100644 index 2f2b7443a..000000000 --- a/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordGrouper.cs +++ /dev/null @@ -1,27 +0,0 @@ -using TagsCloudContainer.WordProcessing.WordInput; - -namespace TagsCloudContainer.WordProcessing.WordGrouping; - -public class DefaultWordGrouper : IWordGrouperProvider -{ - private readonly string[] _words; - - public DefaultWordGrouper(string[] words) - { - _words = words; - } - - public Dictionary GrouppedWords => GroupWords(); - - private Dictionary GroupWords() - { - var frequency = new Dictionary(); - foreach (var word in _words) - { - frequency.TryAdd(word, 0); - frequency[word]++; - } - - return frequency; - } -} \ No newline at end of file diff --git a/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs b/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs new file mode 100644 index 000000000..17b19dd8d --- /dev/null +++ b/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs @@ -0,0 +1,37 @@ +using TagsCloudContainer.Utils; +using TagsCloudContainer.WordProcessing.WordFiltering; +using TagsCloudContainer.WordProcessing.WordInput; + +namespace TagsCloudContainer.WordProcessing.WordGrouping; + +public class DefaultWordProcessor : IProcessedWordProvider +{ + private readonly string[] _words; + private readonly IEnumerable _filters; + + public DefaultWordProcessor(IWordProvider words, IEnumerable filters) + { + _words = words.Words.Select(w => w.ToLower()).ToArray(); + _filters = filters; + } + + public Dictionary ProcessedWords => ProcessWords(); + + private Dictionary ProcessWords() + { + var filtered = _filters.Aggregate(_words, (current, filter) => filter.FilterWords(current)); + return GroupWords(filtered); + } + + private static Dictionary GroupWords(IEnumerable filtered) + { + var frequency = new Dictionary(); + foreach (var word in filtered) + { + frequency.TryAdd(word, 0); + frequency[word]++; + } + + return frequency; + } +} \ No newline at end of file diff --git a/TagsCloudContainer/WordProcessing/WordGrouping/IProcessedWordProvider.cs b/TagsCloudContainer/WordProcessing/WordGrouping/IProcessedWordProvider.cs new file mode 100644 index 000000000..7baadd7da --- /dev/null +++ b/TagsCloudContainer/WordProcessing/WordGrouping/IProcessedWordProvider.cs @@ -0,0 +1,6 @@ +namespace TagsCloudContainer.WordProcessing.WordGrouping; + +public interface IProcessedWordProvider +{ + public Dictionary ProcessedWords { get; } +} \ No newline at end of file diff --git a/TagsCloudContainer/WordProcessing/WordGrouping/IWordGrouperProvider.cs b/TagsCloudContainer/WordProcessing/WordGrouping/IWordGrouperProvider.cs deleted file mode 100644 index f41d1afde..000000000 --- a/TagsCloudContainer/WordProcessing/WordGrouping/IWordGrouperProvider.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace TagsCloudContainer.WordProcessing.WordGrouping; - -public interface IWordGrouperProvider -{ - public Dictionary GrouppedWords { get; } -} \ No newline at end of file diff --git a/TagsCloudContainer/WordProcessing/WordInput/IWordProvider.cs b/TagsCloudContainer/WordProcessing/WordInput/IWordProvider.cs index cf0d8960d..da10b303d 100644 --- a/TagsCloudContainer/WordProcessing/WordInput/IWordProvider.cs +++ b/TagsCloudContainer/WordProcessing/WordInput/IWordProvider.cs @@ -1,4 +1,6 @@ -namespace TagsCloudContainer.WordProcessing.WordInput; +using TagsCloudContainer.Common; + +namespace TagsCloudContainer.WordProcessing.WordInput; public interface IWordProvider { diff --git a/TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs b/TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs index e143043b2..f732022f0 100644 --- a/TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs +++ b/TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs @@ -1,4 +1,6 @@ -namespace TagsCloudContainer.WordProcessing.WordInput; +using TagsCloudContainer.WordProcessing.WordFiltering; + +namespace TagsCloudContainer.WordProcessing.WordInput; public class TxtFileWordParser : IWordProvider From ee6dc21a1b41b18f420ec7c65e10d3f3c63f72dc Mon Sep 17 00:00:00 2001 From: Alexander Gorbatov Date: Sat, 20 Jan 2024 21:42:47 +0500 Subject: [PATCH 03/19] Implemented Console UI --- TagsCloudConsoleUI/ConsoleUi.cs | 156 ++++++++++++++++++ TagsCloudConsoleUI/IUiManager.cs | 6 + TagsCloudConsoleUI/Program.cs | 21 +++ TagsCloudConsoleUI/TagsCloudConsoleUI.csproj | 24 +++ .../Drawing/Colorers/DefaultWordColorer.cs | 17 ++ .../Drawing/Colorers/IColorer.cs | 6 - .../Drawing/Colorers/IWordColorer.cs | 8 + .../Drawing/Colorers/RandomWordColorer.cs | 11 ++ .../Drawing/DefaultImageDrawer.cs | 8 +- .../DrawingOptions/DefaultOptionsProvider.cs | 12 +- TagsCloudContainer/DrawingOptions/Options.cs | 19 ++- TagsCloudContainer/Program.cs | 56 ------- .../Providers/CloudAlgorithmProviders.cs | 14 ++ .../Providers/ColorerProviders.cs | 13 ++ TagsCloudContainer/Providers/WordProviders.cs | 14 ++ TagsCloudContainer/TagsCloudContainer.csproj | 1 - TagsCloudContainer/Utils/DrawingUtils.cs | 23 +++ .../WordGrouping/DefaultWordProcessor.cs | 3 +- .../WordInput/TxtFileWordParser.cs | 2 +- .../TagsCloudContainerTests.csproj | 2 +- TagsCloudWinforms/MainForm.Designer.cs | 41 +++++ TagsCloudWinforms/MainForm.cs | 9 + .../Program.cs | 4 +- .../TagsCloudWinforms.csproj | 11 ++ di.sln | 10 +- 25 files changed, 403 insertions(+), 88 deletions(-) create mode 100644 TagsCloudConsoleUI/ConsoleUi.cs create mode 100644 TagsCloudConsoleUI/IUiManager.cs create mode 100644 TagsCloudConsoleUI/Program.cs create mode 100644 TagsCloudConsoleUI/TagsCloudConsoleUI.csproj create mode 100644 TagsCloudContainer/Drawing/Colorers/DefaultWordColorer.cs delete mode 100644 TagsCloudContainer/Drawing/Colorers/IColorer.cs create mode 100644 TagsCloudContainer/Drawing/Colorers/IWordColorer.cs create mode 100644 TagsCloudContainer/Drawing/Colorers/RandomWordColorer.cs delete mode 100644 TagsCloudContainer/Program.cs create mode 100644 TagsCloudContainer/Providers/CloudAlgorithmProviders.cs create mode 100644 TagsCloudContainer/Providers/ColorerProviders.cs create mode 100644 TagsCloudContainer/Providers/WordProviders.cs create mode 100644 TagsCloudWinforms/MainForm.Designer.cs create mode 100644 TagsCloudWinforms/MainForm.cs rename {TagsCloudWindorms => TagsCloudWinforms}/Program.cs (74%) rename TagsCloudWindorms/TagsCloudWindorms.csproj => TagsCloudWinforms/TagsCloudWinforms.csproj (51%) diff --git a/TagsCloudConsoleUI/ConsoleUi.cs b/TagsCloudConsoleUI/ConsoleUi.cs new file mode 100644 index 000000000..b6fcd76d8 --- /dev/null +++ b/TagsCloudConsoleUI/ConsoleUi.cs @@ -0,0 +1,156 @@ +using System.Drawing; +using System.Text; +using Autofac; +using McMaster.Extensions.CommandLineUtils; +using TagsCloudContainer.Drawing; +using TagsCloudContainer.Drawing.Colorers; +using TagsCloudContainer.DrawingOptions; +using TagsCloudContainer.Providers; +using TagsCloudContainer.TagCloudForming; +using TagsCloudContainer.Utils; +using TagsCloudContainer.WordProcessing.WordFiltering; +using TagsCloudContainer.WordProcessing.WordGrouping; +using TagsCloudContainer.WordProcessing.WordInput; + +namespace TagsCloudConsoleUI; + +public class ConsoleUi : IUiManager +{ + public void StartUi() + { + var containerBuilder = new ContainerBuilder(); + RegisterWordProvider(GetPathToWordsFile(), containerBuilder); + + var font = GetFont(); + var backgroundColor = GetRgbColor("Enter background color in RGB format separated by space"); + RegisterColoringAlgorithm(out var fontColor, containerBuilder); + var imageSide = + GetInteger( + "Enter the image's desired size in px. The image will be a square. It must range from 500 px to 5000 px.", + 500, 5000); + var imageSize = new Size(imageSide, imageSide); + var frequencyScaling = + GetInteger( + "Enter the frequency scaling (a positive integer). It Determines the scale to word frequency ratio.", 2, + 100); + containerBuilder.RegisterInstance(new DefaultOptionsProvider(new Options(fontColor, backgroundColor, imageSize, font, frequencyScaling))) + .As().SingleInstance(); + RegisterAlgorithm(CloudAlgorithmProviders.RegisteredProviders, "Choose the cloud forming algorithm:", containerBuilder); + + containerBuilder.RegisterType().As().SingleInstance(); + containerBuilder + .RegisterInstance(new DefaultWordFilter(new TxtFileWordParser("filter.txt"))) + .As() + .SingleInstance(); + + containerBuilder.RegisterType() + .As() + .SingleInstance(); + containerBuilder.RegisterType() + .As() + .SingleInstance(); + + containerBuilder.RegisterType() + .As() + .SingleInstance(); + var container = containerBuilder.Build(); + var drawer = container.Resolve(); + var bitmap = drawer.DrawImage(); + DefaultImageDrawer.SaveImage(bitmap); + Console.WriteLine("The image has been saved to the current directory."); + } + + private void RegisterAlgorithm(IReadOnlyDictionary> registeredAlgorithms, string prompt, ContainerBuilder containerBuilder) + { + var sb = new StringBuilder($"{prompt}\n"); + foreach (var algorithmProvider in registeredAlgorithms.Keys) + sb.AppendLine(algorithmProvider); + + while (true) + { + var algorithm = Prompt.GetString(sb.ToString(), "", promptColor: ConsoleColor.DarkGreen); + if (registeredAlgorithms.TryGetValue(algorithm!, out var provider)) + { + provider(containerBuilder); + break; + } + + Console.WriteLine("Specified algorithm isn't supported. Try again."); + } + } + + private static int GetInteger(string prompt, int lowerAllowedBoundary, int upperAllowedBoundary) + { + while (true) + { + var intString = Prompt.GetString(prompt, "", ConsoleColor.DarkGreen); + if (int.TryParse(intString, out var parsed) && parsed >= lowerAllowedBoundary && + parsed <= upperAllowedBoundary) + return parsed; + Console.WriteLine("Given number is invalid. Try again."); + } + } + + private void RegisterColoringAlgorithm(out Color fontColor, ContainerBuilder containerBuilder) + { + if (Prompt.GetYesNo("Do you want to use a custom coloring algorithm?", false, ConsoleColor.DarkGreen)) + { + RegisterAlgorithm(ColorerProviders.RegisteredProviders, "Choose the algorithm:", containerBuilder); + + fontColor = Color.White; + return; + } + + containerBuilder.RegisterType().As().SingleInstance(); + fontColor = GetRgbColor("Enter font color in RGB format separated by space"); + } + + private static Color GetRgbColor(string prompt) + { + while (true) + { + var colorString = Prompt.GetString(prompt, + promptColor: ConsoleColor.DarkGreen); + if (DrawingUtils.TryParseRgb(colorString, out var color)) + return color; + Console.WriteLine("Ivalid color format. Try again."); + } + } + + private static Font GetFont() + { + var fontName = Prompt.GetString("Enter font name", promptColor: ConsoleColor.DarkGreen) ?? + "Microsoft Sans Serif"; + while (true) + { + var fontSizeStr = Prompt.GetString("Enter font size in pt", promptColor: ConsoleColor.DarkGreen); + if (float.TryParse(fontSizeStr, out var fontSize) && fontSize > 0) + return new Font(fontName, fontSize); + Console.WriteLine("Font size must be a correct positive number. Try again."); + } + } + + private void RegisterWordProvider(string path, ContainerBuilder containerBuilder) + { + var ext = Path.GetExtension(path); + if (WordProviders.RegisteredProviders.TryGetValue(ext, out var provider)) + { + provider(containerBuilder, path); + return; + } + + Console.WriteLine("This extension is not supported. Try another one."); + StartUi(); + } + + private static string GetPathToWordsFile() + { + while (true) + { + var path = Prompt.GetString("Enter the path to the file with words", promptColor: ConsoleColor.DarkGreen); + if (File.Exists(path)) + return path; + Console.WriteLine("Provided path is invalid. Try again."); + } + } +} \ No newline at end of file diff --git a/TagsCloudConsoleUI/IUiManager.cs b/TagsCloudConsoleUI/IUiManager.cs new file mode 100644 index 000000000..073565b24 --- /dev/null +++ b/TagsCloudConsoleUI/IUiManager.cs @@ -0,0 +1,6 @@ +namespace TagsCloudConsoleUI; + +public interface IUiManager +{ + public void StartUi(); +} \ No newline at end of file diff --git a/TagsCloudConsoleUI/Program.cs b/TagsCloudConsoleUI/Program.cs new file mode 100644 index 000000000..7c77f8e61 --- /dev/null +++ b/TagsCloudConsoleUI/Program.cs @@ -0,0 +1,21 @@ +using Autofac; + +namespace TagsCloudConsoleUI; + +public static class Program +{ + public static void Main() + { + var containerBuilder = new ContainerBuilder(); + + containerBuilder.RegisterType() + .As() + .SingleInstance(); + + var containter = containerBuilder.Build(); + + var ui = containter.Resolve(); + + ui.StartUi(); + } +} \ No newline at end of file diff --git a/TagsCloudConsoleUI/TagsCloudConsoleUI.csproj b/TagsCloudConsoleUI/TagsCloudConsoleUI.csproj new file mode 100644 index 000000000..9ed349255 --- /dev/null +++ b/TagsCloudConsoleUI/TagsCloudConsoleUI.csproj @@ -0,0 +1,24 @@ + + + + net7.0 + enable + enable + Exe + + + + + + + + + ..\TagsCloudContainer\libs\TagsCloudVisualization.dll + + + + + + + + diff --git a/TagsCloudContainer/Drawing/Colorers/DefaultWordColorer.cs b/TagsCloudContainer/Drawing/Colorers/DefaultWordColorer.cs new file mode 100644 index 000000000..17ecdc9b6 --- /dev/null +++ b/TagsCloudContainer/Drawing/Colorers/DefaultWordColorer.cs @@ -0,0 +1,17 @@ +using System.Drawing; +using TagsCloudContainer.DrawingOptions; + +namespace TagsCloudContainer.Drawing.Colorers; + +public class DefaultWordColorer : IWordColorer +{ + private readonly Color _color; + + public DefaultWordColorer(IOptionsProvider optionsProvider) + { + _color = optionsProvider.Options.FontColor; + } + + public Color GetWordColor(string word, int wordFrequency) + => _color; +} \ No newline at end of file diff --git a/TagsCloudContainer/Drawing/Colorers/IColorer.cs b/TagsCloudContainer/Drawing/Colorers/IColorer.cs deleted file mode 100644 index 4a8f5c5c8..000000000 --- a/TagsCloudContainer/Drawing/Colorers/IColorer.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace TagsCloudContainer.Drawing.Colorers; - -public interface IColorer -{ - -} \ No newline at end of file diff --git a/TagsCloudContainer/Drawing/Colorers/IWordColorer.cs b/TagsCloudContainer/Drawing/Colorers/IWordColorer.cs new file mode 100644 index 000000000..94192de8e --- /dev/null +++ b/TagsCloudContainer/Drawing/Colorers/IWordColorer.cs @@ -0,0 +1,8 @@ +using System.Drawing; + +namespace TagsCloudContainer.Drawing.Colorers; + +public interface IWordColorer +{ + public Color GetWordColor(string word, int wordFrequency); +} \ No newline at end of file diff --git a/TagsCloudContainer/Drawing/Colorers/RandomWordColorer.cs b/TagsCloudContainer/Drawing/Colorers/RandomWordColorer.cs new file mode 100644 index 000000000..1aea320f1 --- /dev/null +++ b/TagsCloudContainer/Drawing/Colorers/RandomWordColorer.cs @@ -0,0 +1,11 @@ +using System.Drawing; + +namespace TagsCloudContainer.Drawing.Colorers; + +public class RandomWordColorer : IWordColorer +{ + private static readonly Random Random = new(); + + public Color GetWordColor(string word, int wordFrequency) + => Color.FromArgb(Random.Next(0, 256), Random.Next(0, 256), Random.Next(0, 256)); +} \ No newline at end of file diff --git a/TagsCloudContainer/Drawing/DefaultImageDrawer.cs b/TagsCloudContainer/Drawing/DefaultImageDrawer.cs index 51e01b2a5..c8bcc33bc 100644 --- a/TagsCloudContainer/Drawing/DefaultImageDrawer.cs +++ b/TagsCloudContainer/Drawing/DefaultImageDrawer.cs @@ -1,6 +1,7 @@ using System.Drawing; using System.Drawing.Imaging; using TagsCloudContainer.Common; +using TagsCloudContainer.Drawing.Colorers; using TagsCloudContainer.DrawingOptions; using TagsCloudContainer.TagCloudForming; @@ -10,11 +11,12 @@ public class DefaultImageDrawer : IImageDrawer { private readonly IReadOnlyDictionary _distributedWords; private readonly Options _options; - - public DefaultImageDrawer(IWordCloudDistributorProvider cloudDistributorProvider, IOptionsProvider optionsProvider) + private readonly IWordColorer _colorer; + public DefaultImageDrawer(IWordCloudDistributorProvider cloudDistributorProvider, IOptionsProvider optionsProvider, IWordColorer colorer) { _distributedWords = cloudDistributorProvider.DistributedWords; _options = optionsProvider.Options; + _colorer = colorer; } public Bitmap DrawImage() @@ -28,7 +30,7 @@ public Bitmap DrawImage() { var sizeAdd = _options.FrequencyScaling * (word.Frequency - 1); var newFont = new Font(_options.Font.FontFamily, _options.Font.Size + sizeAdd, _options.Font.Style); - graphics.DrawString(value, newFont, new SolidBrush(_options.FontColor), + graphics.DrawString(value, newFont, new SolidBrush(_colorer.GetWordColor(value, word.Frequency)), word.Rectangle with {X = word.Rectangle.X + offset.X, Y = word.Rectangle.Y + offset.Y}); } diff --git a/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs b/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs index a8fa7eb80..c9123f21a 100644 --- a/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs +++ b/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs @@ -4,12 +4,10 @@ namespace TagsCloudContainer.DrawingOptions; public class DefaultOptionsProvider : IOptionsProvider { - public Options Options => new() + public DefaultOptionsProvider(Options options) { - FontColor = Color.White, - BackgroundColor = Color.Black, - Font = new Font(FontFamily.GenericSansSerif, 26), - FrequencyScaling = 5, - ImageSize = new Size(2000,2000) - }; + Options = options; + } + + public Options Options { get; } } \ No newline at end of file diff --git a/TagsCloudContainer/DrawingOptions/Options.cs b/TagsCloudContainer/DrawingOptions/Options.cs index 7bb616d3d..60c57a56b 100644 --- a/TagsCloudContainer/DrawingOptions/Options.cs +++ b/TagsCloudContainer/DrawingOptions/Options.cs @@ -4,9 +4,18 @@ namespace TagsCloudContainer.DrawingOptions; public class Options { - public Color FontColor { get; set; } - public Color BackgroundColor { get; set; } - public Size ImageSize { get; set; } - public Font Font { get; set; } - public int FrequencyScaling { get; set; } + public Color FontColor { get; } + public Color BackgroundColor { get; } + public Size ImageSize { get; } + public Font Font { get; } + public int FrequencyScaling { get; } + + public Options(Color fontColor, Color backgroundColor, Size imageSize, Font font, int frequencyScaling) + { + FontColor = fontColor; + BackgroundColor = backgroundColor; + ImageSize = imageSize; + Font = font; + FrequencyScaling = frequencyScaling; + } } \ No newline at end of file diff --git a/TagsCloudContainer/Program.cs b/TagsCloudContainer/Program.cs deleted file mode 100644 index c481235cc..000000000 --- a/TagsCloudContainer/Program.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System.Drawing; -using Autofac; -using TagsCloudContainer.Drawing; -using TagsCloudContainer.DrawingOptions; -using TagsCloudContainer.TagCloudForming; -using TagsCloudContainer.WordProcessing.WordFiltering; -using TagsCloudContainer.WordProcessing.WordGrouping; -using TagsCloudContainer.WordProcessing.WordInput; -using TagsCloudVisualization; - -namespace TagsCloudContainer; - -public static class Program -{ - public static void Main(string[] args) - { - var containerBuilder = new ContainerBuilder(); - - containerBuilder - .RegisterInstance(new TxtFileWordParser("words.txt")) - .As() - .SingleInstance(); - - containerBuilder - .RegisterInstance(new DefaultWordFilter(new TxtFileWordParser("filter.txt"))) - .As() - .SingleInstance(); - - containerBuilder.RegisterType() - .As() - .SingleInstance(); - - containerBuilder.RegisterInstance(new CircularCloudLayouter(Point.Empty)) - .As() - .SingleInstance(); - - containerBuilder.RegisterType() - .As() - .SingleInstance(); - - containerBuilder.RegisterType() - .As() - .SingleInstance(); - - containerBuilder.RegisterType() - .As() - .SingleInstance(); - - var containter = containerBuilder.Build(); - - var drawer = containter.Resolve(); - - var bitmap = drawer.DrawImage(); - DefaultImageDrawer.SaveImage(bitmap); - } -} \ No newline at end of file diff --git a/TagsCloudContainer/Providers/CloudAlgorithmProviders.cs b/TagsCloudContainer/Providers/CloudAlgorithmProviders.cs new file mode 100644 index 000000000..1e670a477 --- /dev/null +++ b/TagsCloudContainer/Providers/CloudAlgorithmProviders.cs @@ -0,0 +1,14 @@ +using System.Drawing; +using Autofac; +using TagsCloudVisualization; + +namespace TagsCloudContainer.Providers; + +public static class CloudAlgorithmProviders +{ + public static readonly IReadOnlyDictionary> RegisteredProviders = + new Dictionary> + { + {"Circular", builder => builder.RegisterInstance(new CircularCloudLayouter(Point.Empty)).As().SingleInstance()} + }; +} \ No newline at end of file diff --git a/TagsCloudContainer/Providers/ColorerProviders.cs b/TagsCloudContainer/Providers/ColorerProviders.cs new file mode 100644 index 000000000..3efe2bc6c --- /dev/null +++ b/TagsCloudContainer/Providers/ColorerProviders.cs @@ -0,0 +1,13 @@ +using Autofac; +using TagsCloudContainer.Drawing.Colorers; + +namespace TagsCloudContainer.Providers; + +public static class ColorerProviders +{ + public static readonly IReadOnlyDictionary> RegisteredProviders = + new Dictionary> + { + {"Random", builder => builder.RegisterType().As().SingleInstance()} + }; +} \ No newline at end of file diff --git a/TagsCloudContainer/Providers/WordProviders.cs b/TagsCloudContainer/Providers/WordProviders.cs new file mode 100644 index 000000000..6c98e8896 --- /dev/null +++ b/TagsCloudContainer/Providers/WordProviders.cs @@ -0,0 +1,14 @@ +using System.Collections.Immutable; +using Autofac; +using TagsCloudContainer.WordProcessing.WordInput; + +namespace TagsCloudContainer.Providers; + +public static class WordProviders +{ + public static readonly IReadOnlyDictionary> RegisteredProviders = + new Dictionary> + { + {".txt", (builder, path) => builder.RegisterInstance(new TxtFileWordParser(path)).As()} + }; +} \ No newline at end of file diff --git a/TagsCloudContainer/TagsCloudContainer.csproj b/TagsCloudContainer/TagsCloudContainer.csproj index 154b69aac..7dd07364d 100644 --- a/TagsCloudContainer/TagsCloudContainer.csproj +++ b/TagsCloudContainer/TagsCloudContainer.csproj @@ -4,7 +4,6 @@ net7.0 enable enable - Exe diff --git a/TagsCloudContainer/Utils/DrawingUtils.cs b/TagsCloudContainer/Utils/DrawingUtils.cs index 517ebd4d0..9e96d3d57 100644 --- a/TagsCloudContainer/Utils/DrawingUtils.cs +++ b/TagsCloudContainer/Utils/DrawingUtils.cs @@ -12,4 +12,27 @@ public static Size GetStringSize(string word, int frequency, int frequencyScalin var newFont = new Font(font.FontFamily, font.Size + sizeIncrement, font.Style); return Size.Ceiling(Graphics.MeasureString(word, newFont)); } + + public static bool TryParseRgb(string? rgbString, out Color color, char separator = ' ') + { + if (string.IsNullOrWhiteSpace(rgbString)) + { + color = default; + return false; + } + + var numbers = rgbString.Split(separator); + if (numbers.Length != 3 || numbers.Any(n => !int.TryParse(n, out var parsed) || parsed < 0 || parsed > 255)) + { + color = default; + return false; + } + + var asInts = numbers + .Select(int.Parse) + .ToArray(); + + color = Color.FromArgb(asInts[0], asInts[1], asInts[2]); + return true; + } } \ No newline at end of file diff --git a/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs b/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs index 17b19dd8d..92d3d2e15 100644 --- a/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs +++ b/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs @@ -1,5 +1,4 @@ -using TagsCloudContainer.Utils; -using TagsCloudContainer.WordProcessing.WordFiltering; +using TagsCloudContainer.WordProcessing.WordFiltering; using TagsCloudContainer.WordProcessing.WordInput; namespace TagsCloudContainer.WordProcessing.WordGrouping; diff --git a/TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs b/TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs index f732022f0..bbb8ffaaf 100644 --- a/TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs +++ b/TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs @@ -23,7 +23,7 @@ private string[] Parse() } catch (Exception e) { - throw new IOException($"Failed to read from file {_filePath} Most likely the file path is incorrect.", e); + throw new IOException($"Failed to read from file {_filePath} Most likely the file path is incorrect or the file is corrupted.", e); } return line; diff --git a/TagsCloudContainerTests/TagsCloudContainerTests.csproj b/TagsCloudContainerTests/TagsCloudContainerTests.csproj index 057e75b81..af86e5851 100644 --- a/TagsCloudContainerTests/TagsCloudContainerTests.csproj +++ b/TagsCloudContainerTests/TagsCloudContainerTests.csproj @@ -1,7 +1,7 @@ - net7.0 + net7.0-windows enable enable diff --git a/TagsCloudWinforms/MainForm.Designer.cs b/TagsCloudWinforms/MainForm.Designer.cs new file mode 100644 index 000000000..250afedcf --- /dev/null +++ b/TagsCloudWinforms/MainForm.Designer.cs @@ -0,0 +1,41 @@ +using System.ComponentModel; + +namespace TagsCloudWindorms; + +partial class MainForm +{ + ///

+ /// Required designer variable. + /// + private IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.components = new System.ComponentModel.Container(); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Text = "MainForm"; + } + + #endregion +} \ No newline at end of file diff --git a/TagsCloudWinforms/MainForm.cs b/TagsCloudWinforms/MainForm.cs new file mode 100644 index 000000000..414c97a8f --- /dev/null +++ b/TagsCloudWinforms/MainForm.cs @@ -0,0 +1,9 @@ +namespace TagsCloudWindorms; + +public partial class MainForm : Form +{ + public MainForm() + { + InitializeComponent(); + } +} \ No newline at end of file diff --git a/TagsCloudWindorms/Program.cs b/TagsCloudWinforms/Program.cs similarity index 74% rename from TagsCloudWindorms/Program.cs rename to TagsCloudWinforms/Program.cs index 9ede425f7..5ce611e94 100644 --- a/TagsCloudWindorms/Program.cs +++ b/TagsCloudWinforms/Program.cs @@ -1,3 +1,5 @@ +using Autofac; + namespace TagsCloudWindorms; public static class Program @@ -6,6 +8,6 @@ public static class Program public static void Main() { ApplicationConfiguration.Initialize(); - Application.Run(); + Application.Run(new MainForm()); } } \ No newline at end of file diff --git a/TagsCloudWindorms/TagsCloudWindorms.csproj b/TagsCloudWinforms/TagsCloudWinforms.csproj similarity index 51% rename from TagsCloudWindorms/TagsCloudWindorms.csproj rename to TagsCloudWinforms/TagsCloudWinforms.csproj index 638e474cb..d718c83a8 100644 --- a/TagsCloudWindorms/TagsCloudWindorms.csproj +++ b/TagsCloudWinforms/TagsCloudWinforms.csproj @@ -6,6 +6,17 @@ enable true enable + TagsCloudWindorms
+ + + + + + + Form + + + \ No newline at end of file diff --git a/di.sln b/di.sln index 6e5970969..b7586c131 100644 --- a/di.sln +++ b/di.sln @@ -6,7 +6,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudContainer", "TagsC EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudContainerTests", "TagsCloudContainerTests\TagsCloudContainerTests.csproj", "{405FAAB3-6E4C-487C-9429-43529876FB44}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudWindorms", "TagsCloudWindorms\TagsCloudWindorms.csproj", "{E8F573BE-2D9B-46E4-B0F5-BC08BF84A719}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudConsoleUI", "TagsCloudConsoleUI\TagsCloudConsoleUI.csproj", "{EB9EA819-7DE4-4B51-83EE-5B7B1380DFBD}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -26,9 +26,9 @@ Global {405FAAB3-6E4C-487C-9429-43529876FB44}.Debug|Any CPU.Build.0 = Debug|Any CPU {405FAAB3-6E4C-487C-9429-43529876FB44}.Release|Any CPU.ActiveCfg = Release|Any CPU {405FAAB3-6E4C-487C-9429-43529876FB44}.Release|Any CPU.Build.0 = Release|Any CPU - {E8F573BE-2D9B-46E4-B0F5-BC08BF84A719}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E8F573BE-2D9B-46E4-B0F5-BC08BF84A719}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E8F573BE-2D9B-46E4-B0F5-BC08BF84A719}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E8F573BE-2D9B-46E4-B0F5-BC08BF84A719}.Release|Any CPU.Build.0 = Release|Any CPU + {EB9EA819-7DE4-4B51-83EE-5B7B1380DFBD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EB9EA819-7DE4-4B51-83EE-5B7B1380DFBD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EB9EA819-7DE4-4B51-83EE-5B7B1380DFBD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EB9EA819-7DE4-4B51-83EE-5B7B1380DFBD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal From a065bf570362fc374227d355b56271eeec89e02e Mon Sep 17 00:00:00 2001 From: Alexander Gorbatov Date: Sun, 21 Jan 2024 19:47:43 +0500 Subject: [PATCH 04/19] added doc and docx source files support --- TagsCloudConsoleUI/ConsoleUi.cs | 2 +- .../Providers/CloudAlgorithmProviders.cs | 2 +- .../Providers/ColorerProviders.cs | 2 +- TagsCloudConsoleUI/Providers/WordProviders.cs | 15 +++++++++ TagsCloudContainer/Providers/WordProviders.cs | 14 --------- TagsCloudContainer/TagsCloudContainer.csproj | 1 + .../WordInput/DocxFileWordParser.cs | 31 +++++++++++++++++++ 7 files changed, 50 insertions(+), 17 deletions(-) rename {TagsCloudContainer => TagsCloudConsoleUI}/Providers/CloudAlgorithmProviders.cs (91%) rename {TagsCloudContainer => TagsCloudConsoleUI}/Providers/ColorerProviders.cs (90%) create mode 100644 TagsCloudConsoleUI/Providers/WordProviders.cs delete mode 100644 TagsCloudContainer/Providers/WordProviders.cs create mode 100644 TagsCloudContainer/WordProcessing/WordInput/DocxFileWordParser.cs diff --git a/TagsCloudConsoleUI/ConsoleUi.cs b/TagsCloudConsoleUI/ConsoleUi.cs index b6fcd76d8..5854634e5 100644 --- a/TagsCloudConsoleUI/ConsoleUi.cs +++ b/TagsCloudConsoleUI/ConsoleUi.cs @@ -2,10 +2,10 @@ using System.Text; using Autofac; using McMaster.Extensions.CommandLineUtils; +using TagsCloudConsoleUI.Providers; using TagsCloudContainer.Drawing; using TagsCloudContainer.Drawing.Colorers; using TagsCloudContainer.DrawingOptions; -using TagsCloudContainer.Providers; using TagsCloudContainer.TagCloudForming; using TagsCloudContainer.Utils; using TagsCloudContainer.WordProcessing.WordFiltering; diff --git a/TagsCloudContainer/Providers/CloudAlgorithmProviders.cs b/TagsCloudConsoleUI/Providers/CloudAlgorithmProviders.cs similarity index 91% rename from TagsCloudContainer/Providers/CloudAlgorithmProviders.cs rename to TagsCloudConsoleUI/Providers/CloudAlgorithmProviders.cs index 1e670a477..1c061b3ed 100644 --- a/TagsCloudContainer/Providers/CloudAlgorithmProviders.cs +++ b/TagsCloudConsoleUI/Providers/CloudAlgorithmProviders.cs @@ -2,7 +2,7 @@ using Autofac; using TagsCloudVisualization; -namespace TagsCloudContainer.Providers; +namespace TagsCloudConsoleUI.Providers; public static class CloudAlgorithmProviders { diff --git a/TagsCloudContainer/Providers/ColorerProviders.cs b/TagsCloudConsoleUI/Providers/ColorerProviders.cs similarity index 90% rename from TagsCloudContainer/Providers/ColorerProviders.cs rename to TagsCloudConsoleUI/Providers/ColorerProviders.cs index 3efe2bc6c..c1b79d57d 100644 --- a/TagsCloudContainer/Providers/ColorerProviders.cs +++ b/TagsCloudConsoleUI/Providers/ColorerProviders.cs @@ -1,7 +1,7 @@ using Autofac; using TagsCloudContainer.Drawing.Colorers; -namespace TagsCloudContainer.Providers; +namespace TagsCloudConsoleUI.Providers; public static class ColorerProviders { diff --git a/TagsCloudConsoleUI/Providers/WordProviders.cs b/TagsCloudConsoleUI/Providers/WordProviders.cs new file mode 100644 index 000000000..2e1cfd150 --- /dev/null +++ b/TagsCloudConsoleUI/Providers/WordProviders.cs @@ -0,0 +1,15 @@ +using Autofac; +using TagsCloudContainer.WordProcessing.WordInput; + +namespace TagsCloudConsoleUI.Providers; + +public static class WordProviders +{ + public static readonly IReadOnlyDictionary> RegisteredProviders = + new Dictionary> + { + {".txt", (builder, path) => builder.RegisterInstance(new TxtFileWordParser(path)).As().SingleInstance()}, + {".docx", (builder, path) => builder.RegisterInstance(new DocxFileWordParser(path)).As().SingleInstance()}, + {".doc", (builder, path) => builder.RegisterInstance(new DocxFileWordParser(path)).As().SingleInstance()} + }; +} \ No newline at end of file diff --git a/TagsCloudContainer/Providers/WordProviders.cs b/TagsCloudContainer/Providers/WordProviders.cs deleted file mode 100644 index 6c98e8896..000000000 --- a/TagsCloudContainer/Providers/WordProviders.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System.Collections.Immutable; -using Autofac; -using TagsCloudContainer.WordProcessing.WordInput; - -namespace TagsCloudContainer.Providers; - -public static class WordProviders -{ - public static readonly IReadOnlyDictionary> RegisteredProviders = - new Dictionary> - { - {".txt", (builder, path) => builder.RegisterInstance(new TxtFileWordParser(path)).As()} - }; -} \ No newline at end of file diff --git a/TagsCloudContainer/TagsCloudContainer.csproj b/TagsCloudContainer/TagsCloudContainer.csproj index 7dd07364d..8ea252154 100644 --- a/TagsCloudContainer/TagsCloudContainer.csproj +++ b/TagsCloudContainer/TagsCloudContainer.csproj @@ -8,6 +8,7 @@ + diff --git a/TagsCloudContainer/WordProcessing/WordInput/DocxFileWordParser.cs b/TagsCloudContainer/WordProcessing/WordInput/DocxFileWordParser.cs new file mode 100644 index 000000000..00947fa58 --- /dev/null +++ b/TagsCloudContainer/WordProcessing/WordInput/DocxFileWordParser.cs @@ -0,0 +1,31 @@ +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Wordprocessing; + +namespace TagsCloudContainer.WordProcessing.WordInput; + +public class DocxFileWordParser : IWordProvider +{ + private readonly string _filePath; + + public DocxFileWordParser(string filePath) + { + _filePath = filePath; + } + + public string[] Words => Parse(); + + private string[] Parse() + { + using var wordDocument = WordprocessingDocument.Open(_filePath, false); + + var body = wordDocument.MainDocumentPart?.Document.Body; + + if (body is null) + throw new IOException( + $"Failed to read from file {_filePath} Most likely the file path is incorrect or the file is corrupted."); + + return body.Elements() + .Select(paragraph => paragraph.InnerText) + .ToArray(); + } +} \ No newline at end of file From 83ecf9ff1fa36f1cb1cdd4b196d95f9067dfc5c5 Mon Sep 17 00:00:00 2001 From: Alexander Gorbatov Date: Sun, 21 Jan 2024 20:30:32 +0500 Subject: [PATCH 05/19] small refactoring --- TagsCloudConsoleUI/ConsoleUi.cs | 60 +++++++++++-------- TagsCloudConsoleUI/Program.cs | 23 +++---- .../Drawing/DefaultImageDrawer.cs | 16 ++++- 3 files changed, 58 insertions(+), 41 deletions(-) diff --git a/TagsCloudConsoleUI/ConsoleUi.cs b/TagsCloudConsoleUI/ConsoleUi.cs index 5854634e5..ebe637394 100644 --- a/TagsCloudConsoleUI/ConsoleUi.cs +++ b/TagsCloudConsoleUI/ConsoleUi.cs @@ -1,4 +1,5 @@ using System.Drawing; +using System.Drawing.Imaging; using System.Text; using Autofac; using McMaster.Extensions.CommandLineUtils; @@ -21,9 +22,30 @@ public void StartUi() var containerBuilder = new ContainerBuilder(); RegisterWordProvider(GetPathToWordsFile(), containerBuilder); + RegisterCloudBuildingOptions(containerBuilder); + + RegisterAlgorithm(CloudAlgorithmProviders.RegisteredProviders, "Choose the cloud forming algorithm:", + containerBuilder); + + containerBuilder.RegisterType().As().SingleInstance(); + containerBuilder.RegisterInstance(new DefaultWordFilter(new TxtFileWordParser("filter.txt"))).As() + .SingleInstance(); + containerBuilder.RegisterType().As().SingleInstance(); + containerBuilder.RegisterType().As().SingleInstance(); + containerBuilder.RegisterType().As().SingleInstance(); + + var container = containerBuilder.Build(); + + BuildTagCloud(container.Resolve(), ".", "image.png", ImageFormat.Png); + } + + private void RegisterCloudBuildingOptions(ContainerBuilder containerBuilder) + { var font = GetFont(); var backgroundColor = GetRgbColor("Enter background color in RGB format separated by space"); + RegisterColoringAlgorithm(out var fontColor, containerBuilder); + var imageSide = GetInteger( "Enter the image's desired size in px. The image will be a square. It must range from 500 px to 5000 px.", @@ -33,34 +55,22 @@ public void StartUi() GetInteger( "Enter the frequency scaling (a positive integer). It Determines the scale to word frequency ratio.", 2, 100); - containerBuilder.RegisterInstance(new DefaultOptionsProvider(new Options(fontColor, backgroundColor, imageSize, font, frequencyScaling))) - .As().SingleInstance(); - RegisterAlgorithm(CloudAlgorithmProviders.RegisteredProviders, "Choose the cloud forming algorithm:", containerBuilder); - - containerBuilder.RegisterType().As().SingleInstance(); - containerBuilder - .RegisterInstance(new DefaultWordFilter(new TxtFileWordParser("filter.txt"))) - .As() - .SingleInstance(); - containerBuilder.RegisterType() - .As() - .SingleInstance(); - containerBuilder.RegisterType() - .As() - .SingleInstance(); + containerBuilder + .RegisterInstance(new DefaultOptionsProvider(new Options(fontColor, backgroundColor, imageSize, font, + frequencyScaling))) + .As().SingleInstance(); + } - containerBuilder.RegisterType() - .As() - .SingleInstance(); - var container = containerBuilder.Build(); - var drawer = container.Resolve(); - var bitmap = drawer.DrawImage(); - DefaultImageDrawer.SaveImage(bitmap); - Console.WriteLine("The image has been saved to the current directory."); + private static void BuildTagCloud(IImageDrawer imageDrawer, string dirPath, string fileName, ImageFormat imageFormat) + { + var bitmap = imageDrawer.DrawImage(); + DefaultImageDrawer.SaveImage(bitmap, dirPath, fileName, imageFormat); + Console.WriteLine($"The image has been saved to \"{dirPath}\""); } - private void RegisterAlgorithm(IReadOnlyDictionary> registeredAlgorithms, string prompt, ContainerBuilder containerBuilder) + private void RegisterAlgorithm(IReadOnlyDictionary> registeredAlgorithms, + string prompt, ContainerBuilder containerBuilder) { var sb = new StringBuilder($"{prompt}\n"); foreach (var algorithmProvider in registeredAlgorithms.Keys) @@ -96,7 +106,7 @@ private void RegisterColoringAlgorithm(out Color fontColor, ContainerBuilder con if (Prompt.GetYesNo("Do you want to use a custom coloring algorithm?", false, ConsoleColor.DarkGreen)) { RegisterAlgorithm(ColorerProviders.RegisteredProviders, "Choose the algorithm:", containerBuilder); - + fontColor = Color.White; return; } diff --git a/TagsCloudConsoleUI/Program.cs b/TagsCloudConsoleUI/Program.cs index 7c77f8e61..56c4c545f 100644 --- a/TagsCloudConsoleUI/Program.cs +++ b/TagsCloudConsoleUI/Program.cs @@ -1,21 +1,16 @@ -using Autofac; - -namespace TagsCloudConsoleUI; +namespace TagsCloudConsoleUI; public static class Program { public static void Main() { - var containerBuilder = new ContainerBuilder(); - - containerBuilder.RegisterType() - .As() - .SingleInstance(); - - var containter = containerBuilder.Build(); - - var ui = containter.Resolve(); - - ui.StartUi(); + try + { + new ConsoleUi().StartUi(); + } + catch (Exception e) + { + Console.WriteLine(e); + } } } \ No newline at end of file diff --git a/TagsCloudContainer/Drawing/DefaultImageDrawer.cs b/TagsCloudContainer/Drawing/DefaultImageDrawer.cs index c8bcc33bc..af984f1d8 100644 --- a/TagsCloudContainer/Drawing/DefaultImageDrawer.cs +++ b/TagsCloudContainer/Drawing/DefaultImageDrawer.cs @@ -37,8 +37,20 @@ public Bitmap DrawImage() return bitmap; } - public static void SaveImage(Bitmap bitmap) + public static void SaveImage(Bitmap bitmap, string dirPath, string filename, ImageFormat imageFormat) { - bitmap.Save(Path.Combine(Directory.GetCurrentDirectory(), "image.png"), ImageFormat.Png); + if (string.IsNullOrWhiteSpace(filename) || filename.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) + throw new ArgumentException("The provided filename is not valid."); + + try + { + Directory.CreateDirectory(dirPath); + } + catch (Exception e) + { + throw new ArgumentException("The provided directory path is not valid.", e); + } + + bitmap.Save(Path.Combine(dirPath, filename), imageFormat); } } \ No newline at end of file From 2d293f7462a3e2a6f1c637fe7c3ff7f5558d2db4 Mon Sep 17 00:00:00 2001 From: Alex <96792931+SilversShade@users.noreply.github.com> Date: Sun, 21 Jan 2024 21:10:19 +0500 Subject: [PATCH 06/19] Delete TagsCloudWinforms directory --- TagsCloudWinforms/MainForm.Designer.cs | 41 ---------------------- TagsCloudWinforms/MainForm.cs | 9 ----- TagsCloudWinforms/Program.cs | 13 ------- TagsCloudWinforms/TagsCloudWinforms.csproj | 22 ------------ 4 files changed, 85 deletions(-) delete mode 100644 TagsCloudWinforms/MainForm.Designer.cs delete mode 100644 TagsCloudWinforms/MainForm.cs delete mode 100644 TagsCloudWinforms/Program.cs delete mode 100644 TagsCloudWinforms/TagsCloudWinforms.csproj diff --git a/TagsCloudWinforms/MainForm.Designer.cs b/TagsCloudWinforms/MainForm.Designer.cs deleted file mode 100644 index 250afedcf..000000000 --- a/TagsCloudWinforms/MainForm.Designer.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System.ComponentModel; - -namespace TagsCloudWindorms; - -partial class MainForm -{ - /// - /// Required designer variable. - /// - private IContainer components = null; - - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - - base.Dispose(disposing); - } - - #region Windows Form Designer generated code - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - this.components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); - this.Text = "MainForm"; - } - - #endregion -} \ No newline at end of file diff --git a/TagsCloudWinforms/MainForm.cs b/TagsCloudWinforms/MainForm.cs deleted file mode 100644 index 414c97a8f..000000000 --- a/TagsCloudWinforms/MainForm.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace TagsCloudWindorms; - -public partial class MainForm : Form -{ - public MainForm() - { - InitializeComponent(); - } -} \ No newline at end of file diff --git a/TagsCloudWinforms/Program.cs b/TagsCloudWinforms/Program.cs deleted file mode 100644 index 5ce611e94..000000000 --- a/TagsCloudWinforms/Program.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Autofac; - -namespace TagsCloudWindorms; - -public static class Program -{ - [STAThread] - public static void Main() - { - ApplicationConfiguration.Initialize(); - Application.Run(new MainForm()); - } -} \ No newline at end of file diff --git a/TagsCloudWinforms/TagsCloudWinforms.csproj b/TagsCloudWinforms/TagsCloudWinforms.csproj deleted file mode 100644 index d718c83a8..000000000 --- a/TagsCloudWinforms/TagsCloudWinforms.csproj +++ /dev/null @@ -1,22 +0,0 @@ - - - - WinExe - net7.0-windows - enable - true - enable - TagsCloudWindorms - - - - - - - - - Form - - - - \ No newline at end of file From aa77c66fed1c511c598a566417861b79f781b288 Mon Sep 17 00:00:00 2001 From: Alex <96792931+SilversShade@users.noreply.github.com> Date: Mon, 22 Jan 2024 21:30:09 +0500 Subject: [PATCH 07/19] Delete FractalPainter directory --- .../App/Actions/DragonFractalAction.cs | 34 ----- .../App/Actions/IDragonPainterFactory.cs | 9 -- .../App/Actions/ImageSettingsAction.cs | 27 ---- .../App/Actions/KochFractalAction.cs | 24 ---- .../App/Actions/PaletteSettingsAction.cs | 24 ---- FractalPainter/App/Actions/SaveImageAction.cs | 47 ------- FractalPainter/App/AppSettings.cs | 10 -- FractalPainter/App/Fractals/DragonPainter.cs | 52 -------- FractalPainter/App/Fractals/DragonSettings.cs | 15 --- .../App/Fractals/DragonSettingsGenerator.cs | 27 ---- FractalPainter/App/Fractals/KochPainter.cs | 57 -------- FractalPainter/App/MainForm.cs | 30 ----- FractalPainter/App/MainForm.resx | 123 ------------------ FractalPainter/App/PictureBoxImageHolder.cs | 46 ------- FractalPainter/App/Program.cs | 50 ------- FractalPainter/App/SettingsManager.cs | 54 -------- .../Infrastructure/Common/FileBlobStorage.cs | 17 --- .../Infrastructure/Common/IBlobStorage.cs | 8 -- .../Common/IImageDirectoryProvider.cs | 7 - .../Infrastructure/Common/IImageHolder.cs | 13 -- .../Common/IImageSettingsProvider.cs | 7 - .../Common/IObjectSerializer.cs | 8 -- .../Infrastructure/Common/ImageSettings.cs | 8 -- .../Infrastructure/Common/Palette.cs | 11 -- .../Infrastructure/Common/SettingsForm.cs | 39 ------ .../Common/XmlObjectSerializer.cs | 23 ---- .../Infrastructure/UiActions/IUiAction.cs | 10 -- .../UiActions/UiActionExtensions.cs | 35 ----- FractalPainter/Readme.md | 67 ---------- .../Infrastructure/Infrastructure.csproj | 46 ------- FractalPainter/Solved/Solved.sln | 76 ----------- .../Step01/App/Actions/DragonFractalAction.cs | 40 ------ .../Step01/App/Actions/ImageSettingsAction.cs | 33 ----- .../Step01/App/Actions/KochFractalAction.cs | 37 ------ .../App/Actions/PaletteSettingsAction.cs | 25 ---- .../Step01/App/Actions/SaveImageAction.cs | 43 ------ .../Solved/Step01/App/AppSettings.cs | 10 -- .../Step01/App/Fractals/DragonPainter.cs | 50 ------- .../Step01/App/Fractals/DragonSettings.cs | 15 --- .../App/Fractals/DragonSettingsGenerator.cs | 27 ---- .../Solved/Step01/App/Fractals/KochPainter.cs | 57 -------- FractalPainter/Solved/Step01/App/MainForm.cs | 47 ------- .../Solved/Step01/App/MainForm.resx | 123 ------------------ .../Step01/App/PictureBoxImageHolder.cs | 46 ------- FractalPainter/Solved/Step01/App/Program.cs | 39 ------ .../Solved/Step01/App/SettingsManager.cs | 54 -------- .../Injection/DependencyInjector.cs | 19 --- .../Step01/Infrastructure/Injection/INeed.cs | 7 - .../Infrastructure/UiActions/IUiAction.cs | 10 -- .../UiActions/UiActionExtensions.cs | 34 ----- FractalPainter/Solved/Step01/Step01.csproj | 21 --- .../Step03/App/Actions/DragonFractalAction.cs | 40 ------ .../Step03/App/Actions/ImageSettingsAction.cs | 33 ----- .../Step03/App/Actions/KochFractalAction.cs | 32 ----- .../App/Actions/PaletteSettingsAction.cs | 25 ---- .../Step03/App/Actions/SaveImageAction.cs | 43 ------ .../Solved/Step03/App/AppSettings.cs | 10 -- .../Step03/App/Fractals/DragonPainter.cs | 50 ------- .../Step03/App/Fractals/DragonSettings.cs | 15 --- .../App/Fractals/DragonSettingsGenerator.cs | 27 ---- .../Solved/Step03/App/Fractals/KochPainter.cs | 57 -------- FractalPainter/Solved/Step03/App/MainForm.cs | 48 ------- .../Solved/Step03/App/MainForm.resx | 123 ------------------ .../Step03/App/PictureBoxImageHolder.cs | 46 ------- FractalPainter/Solved/Step03/App/Program.cs | 46 ------- .../Solved/Step03/App/SettingsManager.cs | 54 -------- .../Injection/DependencyInjector.cs | 19 --- .../Step03/Infrastructure/Injection/INeed.cs | 7 - .../Infrastructure/UiActions/IUiAction.cs | 10 -- .../UiActions/UiActionExtensions.cs | 34 ----- FractalPainter/Solved/Step03/Step03.csproj | 20 --- .../Step04/App/Actions/DragonFractalAction.cs | 40 ------ .../Step04/App/Actions/ImageSettingsAction.cs | 33 ----- .../Step04/App/Actions/KochFractalAction.cs | 24 ---- .../App/Actions/PaletteSettingsAction.cs | 25 ---- .../Step04/App/Actions/SaveImageAction.cs | 43 ------ .../Solved/Step04/App/AppSettings.cs | 10 -- .../Step04/App/Fractals/DragonPainter.cs | 50 ------- .../Step04/App/Fractals/DragonSettings.cs | 15 --- .../App/Fractals/DragonSettingsGenerator.cs | 27 ---- .../Solved/Step04/App/Fractals/KochPainter.cs | 56 -------- FractalPainter/Solved/Step04/App/MainForm.cs | 48 ------- .../Solved/Step04/App/MainForm.resx | 123 ------------------ .../Step04/App/PictureBoxImageHolder.cs | 46 ------- FractalPainter/Solved/Step04/App/Program.cs | 46 ------- .../Solved/Step04/App/SettingsManager.cs | 54 -------- .../Injection/DependencyInjector.cs | 19 --- .../Step04/Infrastructure/Injection/INeed.cs | 7 - .../Infrastructure/UiActions/IUiAction.cs | 10 -- .../UiActions/UiActionExtensions.cs | 34 ----- FractalPainter/Solved/Step04/Step04.csproj | 20 --- .../Step05/App/Actions/DragonFractalAction.cs | 35 ----- .../Step05/App/Actions/ImageSettingsAction.cs | 33 ----- .../Step05/App/Actions/KochFractalAction.cs | 24 ---- .../App/Actions/PaletteSettingsAction.cs | 25 ---- .../Step05/App/Actions/SaveImageAction.cs | 43 ------ .../Solved/Step05/App/AppSettings.cs | 10 -- .../Step05/App/Fractals/DragonPainter.cs | 50 ------- .../Step05/App/Fractals/DragonSettings.cs | 15 --- .../App/Fractals/DragonSettingsGenerator.cs | 27 ---- .../App/Fractals/IDragonPainterFactory.cs | 7 - .../Solved/Step05/App/Fractals/KochPainter.cs | 56 -------- FractalPainter/Solved/Step05/App/MainForm.cs | 48 ------- .../Solved/Step05/App/MainForm.resx | 123 ------------------ .../Step05/App/PictureBoxImageHolder.cs | 46 ------- FractalPainter/Solved/Step05/App/Program.cs | 50 ------- .../Solved/Step05/App/SettingsManager.cs | 54 -------- .../Injection/DependencyInjector.cs | 19 --- .../Step05/Infrastructure/Injection/INeed.cs | 7 - .../Infrastructure/UiActions/IUiAction.cs | 10 -- .../UiActions/UiActionExtensions.cs | 34 ----- FractalPainter/Solved/Step05/Step05.csproj | 21 --- .../Step06/App/Actions/DragonFractalAction.cs | 38 ------ .../Step06/App/Actions/ImageSettingsAction.cs | 33 ----- .../Step06/App/Actions/KochFractalAction.cs | 24 ---- .../App/Actions/PaletteSettingsAction.cs | 25 ---- .../Step06/App/Actions/SaveImageAction.cs | 43 ------ .../Solved/Step06/App/AppSettings.cs | 10 -- .../Step06/App/Fractals/DragonPainter.cs | 50 ------- .../Step06/App/Fractals/DragonSettings.cs | 15 --- .../App/Fractals/DragonSettingsGenerator.cs | 27 ---- .../App/Fractals/IDragonPainterFactory.cs | 7 - .../Solved/Step06/App/Fractals/KochPainter.cs | 56 -------- FractalPainter/Solved/Step06/App/MainForm.cs | 48 ------- .../Solved/Step06/App/MainForm.resx | 123 ------------------ .../Step06/App/PictureBoxImageHolder.cs | 46 ------- FractalPainter/Solved/Step06/App/Program.cs | 50 ------- .../Solved/Step06/App/SettingsManager.cs | 54 -------- .../Injection/DependencyInjector.cs | 19 --- .../Step06/Infrastructure/Injection/INeed.cs | 7 - .../Infrastructure/UiActions/IUiAction.cs | 10 -- .../UiActions/UiActionExtensions.cs | 34 ----- FractalPainter/Solved/Step06/Step06.csproj | 21 --- .../Step07/App/Actions/DragonFractalAction.cs | 38 ------ .../Step07/App/Actions/ImageSettingsAction.cs | 33 ----- .../Step07/App/Actions/KochFractalAction.cs | 24 ---- .../App/Actions/PaletteSettingsAction.cs | 25 ---- .../Step07/App/Actions/SaveImageAction.cs | 43 ------ .../Solved/Step07/App/AppSettings.cs | 10 -- .../Step07/App/Fractals/DragonPainter.cs | 54 -------- .../Step07/App/Fractals/DragonSettings.cs | 15 --- .../App/Fractals/DragonSettingsGenerator.cs | 27 ---- .../App/Fractals/IDragonPainterFactory.cs | 7 - .../Solved/Step07/App/Fractals/KochPainter.cs | 56 -------- FractalPainter/Solved/Step07/App/MainForm.cs | 48 ------- .../Solved/Step07/App/MainForm.resx | 123 ------------------ .../Step07/App/PictureBoxImageHolder.cs | 46 ------- FractalPainter/Solved/Step07/App/Program.cs | 50 ------- .../Solved/Step07/App/SettingsManager.cs | 54 -------- .../Injection/DependencyInjector.cs | 19 --- .../Step07/Infrastructure/Injection/INeed.cs | 7 - .../Infrastructure/UiActions/IUiAction.cs | 10 -- .../UiActions/UiActionExtensions.cs | 34 ----- FractalPainter/Solved/Step07/Step07.csproj | 21 --- .../Step08/App/Actions/DragonFractalAction.cs | 38 ------ .../Step08/App/Actions/ImageSettingsAction.cs | 28 ---- .../Step08/App/Actions/KochFractalAction.cs | 24 ---- .../App/Actions/PaletteSettingsAction.cs | 25 ---- .../Step08/App/Actions/SaveImageAction.cs | 43 ------ .../Solved/Step08/App/AppSettings.cs | 10 -- .../Step08/App/Fractals/DragonPainter.cs | 54 -------- .../Step08/App/Fractals/DragonSettings.cs | 15 --- .../App/Fractals/DragonSettingsGenerator.cs | 27 ---- .../App/Fractals/IDragonPainterFactory.cs | 7 - .../Solved/Step08/App/Fractals/KochPainter.cs | 56 -------- FractalPainter/Solved/Step08/App/MainForm.cs | 39 ------ .../Solved/Step08/App/MainForm.resx | 123 ------------------ .../Step08/App/PictureBoxImageHolder.cs | 46 ------- FractalPainter/Solved/Step08/App/Program.cs | 61 --------- .../Solved/Step08/App/SettingsManager.cs | 54 -------- .../Injection/DependencyInjector.cs | 19 --- .../Step08/Infrastructure/Injection/INeed.cs | 7 - .../Infrastructure/UiActions/IUiAction.cs | 10 -- .../UiActions/UiActionExtensions.cs | 34 ----- FractalPainter/Solved/Step08/Step08.csproj | 21 --- .../Step09/App/Actions/DragonFractalAction.cs | 38 ------ .../Step09/App/Actions/ImageSettingsAction.cs | 28 ---- .../Step09/App/Actions/KochFractalAction.cs | 24 ---- .../App/Actions/PaletteSettingsAction.cs | 24 ---- .../Step09/App/Actions/SaveImageAction.cs | 38 ------ .../Solved/Step09/App/AppSettings.cs | 10 -- .../Step09/App/Fractals/DragonPainter.cs | 54 -------- .../Step09/App/Fractals/DragonSettings.cs | 15 --- .../App/Fractals/DragonSettingsGenerator.cs | 27 ---- .../App/Fractals/IDragonPainterFactory.cs | 7 - .../Solved/Step09/App/Fractals/KochPainter.cs | 56 -------- FractalPainter/Solved/Step09/App/MainForm.cs | 32 ----- .../Solved/Step09/App/MainForm.resx | 123 ------------------ .../Step09/App/PictureBoxImageHolder.cs | 46 ------- FractalPainter/Solved/Step09/App/Program.cs | 61 --------- .../Solved/Step09/App/SettingsManager.cs | 54 -------- .../Infrastructure/UiActions/IUiAction.cs | 10 -- .../UiActions/UiActionExtensions.cs | 34 ----- FractalPainter/Solved/Step09/Step09.csproj | 21 --- .../Step10/App/Actions/DragonFractalAction.cs | 38 ------ .../Step10/App/Actions/ImageSettingsAction.cs | 28 ---- .../Step10/App/Actions/KochFractalAction.cs | 24 ---- .../App/Actions/PaletteSettingsAction.cs | 24 ---- .../Step10/App/Actions/SaveImageAction.cs | 38 ------ .../Solved/Step10/App/AppSettings.cs | 10 -- .../Step10/App/Fractals/DragonPainter.cs | 54 -------- .../Step10/App/Fractals/DragonSettings.cs | 15 --- .../App/Fractals/DragonSettingsGenerator.cs | 27 ---- .../App/Fractals/IDragonPainterFactory.cs | 7 - .../Solved/Step10/App/Fractals/KochPainter.cs | 56 -------- FractalPainter/Solved/Step10/App/MainForm.cs | 32 ----- .../Solved/Step10/App/MainForm.resx | 123 ------------------ .../Step10/App/PictureBoxImageHolder.cs | 46 ------- FractalPainter/Solved/Step10/App/Program.cs | 61 --------- .../Solved/Step10/App/SettingsManager.cs | 54 -------- .../Infrastructure/UiActions/IUiAction.cs | 10 -- .../UiActions/UiActionExtensions.cs | 34 ----- FractalPainter/Solved/Step10/Step10.csproj | 23 ---- .../Step11/App/Actions/DragonFractalAction.cs | 38 ------ .../Step11/App/Actions/ImageSettingsAction.cs | 28 ---- .../Step11/App/Actions/KochFractalAction.cs | 24 ---- .../App/Actions/PaletteSettingsAction.cs | 24 ---- .../Step11/App/Actions/SaveImageAction.cs | 38 ------ .../Solved/Step11/App/AppSettings.cs | 10 -- .../Step11/App/Fractals/DragonPainter.cs | 54 -------- .../Step11/App/Fractals/DragonSettings.cs | 15 --- .../App/Fractals/DragonSettingsGenerator.cs | 27 ---- .../App/Fractals/IDragonPainterFactory.cs | 7 - .../Solved/Step11/App/Fractals/KochPainter.cs | 56 -------- FractalPainter/Solved/Step11/App/MainForm.cs | 32 ----- .../Solved/Step11/App/MainForm.resx | 123 ------------------ .../Step11/App/PictureBoxImageHolder.cs | 46 ------- FractalPainter/Solved/Step11/App/Program.cs | 61 --------- .../Solved/Step11/App/SettingsManager.cs | 54 -------- .../UiActions/EnumExtensions.cs | 20 --- .../Infrastructure/UiActions/IUiAction.cs | 10 -- .../Infrastructure/UiActions/MenuCategory.cs | 16 --- .../UiActions/UiActionExtensions.cs | 35 ----- FractalPainter/Solved/Step11/Step11.csproj | 22 ---- FractalPainter/app.settings | 8 -- FractalPainter/fractalPainter.csproj | 37 ------ 236 files changed, 8363 deletions(-) delete mode 100644 FractalPainter/App/Actions/DragonFractalAction.cs delete mode 100644 FractalPainter/App/Actions/IDragonPainterFactory.cs delete mode 100644 FractalPainter/App/Actions/ImageSettingsAction.cs delete mode 100644 FractalPainter/App/Actions/KochFractalAction.cs delete mode 100644 FractalPainter/App/Actions/PaletteSettingsAction.cs delete mode 100644 FractalPainter/App/Actions/SaveImageAction.cs delete mode 100644 FractalPainter/App/AppSettings.cs delete mode 100644 FractalPainter/App/Fractals/DragonPainter.cs delete mode 100644 FractalPainter/App/Fractals/DragonSettings.cs delete mode 100644 FractalPainter/App/Fractals/DragonSettingsGenerator.cs delete mode 100644 FractalPainter/App/Fractals/KochPainter.cs delete mode 100644 FractalPainter/App/MainForm.cs delete mode 100644 FractalPainter/App/MainForm.resx delete mode 100644 FractalPainter/App/PictureBoxImageHolder.cs delete mode 100644 FractalPainter/App/Program.cs delete mode 100644 FractalPainter/App/SettingsManager.cs delete mode 100644 FractalPainter/Infrastructure/Common/FileBlobStorage.cs delete mode 100644 FractalPainter/Infrastructure/Common/IBlobStorage.cs delete mode 100644 FractalPainter/Infrastructure/Common/IImageDirectoryProvider.cs delete mode 100644 FractalPainter/Infrastructure/Common/IImageHolder.cs delete mode 100644 FractalPainter/Infrastructure/Common/IImageSettingsProvider.cs delete mode 100644 FractalPainter/Infrastructure/Common/IObjectSerializer.cs delete mode 100644 FractalPainter/Infrastructure/Common/ImageSettings.cs delete mode 100644 FractalPainter/Infrastructure/Common/Palette.cs delete mode 100644 FractalPainter/Infrastructure/Common/SettingsForm.cs delete mode 100644 FractalPainter/Infrastructure/Common/XmlObjectSerializer.cs delete mode 100644 FractalPainter/Infrastructure/UiActions/IUiAction.cs delete mode 100644 FractalPainter/Infrastructure/UiActions/UiActionExtensions.cs delete mode 100644 FractalPainter/Readme.md delete mode 100644 FractalPainter/Solved/Infrastructure/Infrastructure.csproj delete mode 100644 FractalPainter/Solved/Solved.sln delete mode 100644 FractalPainter/Solved/Step01/App/Actions/DragonFractalAction.cs delete mode 100644 FractalPainter/Solved/Step01/App/Actions/ImageSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step01/App/Actions/KochFractalAction.cs delete mode 100644 FractalPainter/Solved/Step01/App/Actions/PaletteSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step01/App/Actions/SaveImageAction.cs delete mode 100644 FractalPainter/Solved/Step01/App/AppSettings.cs delete mode 100644 FractalPainter/Solved/Step01/App/Fractals/DragonPainter.cs delete mode 100644 FractalPainter/Solved/Step01/App/Fractals/DragonSettings.cs delete mode 100644 FractalPainter/Solved/Step01/App/Fractals/DragonSettingsGenerator.cs delete mode 100644 FractalPainter/Solved/Step01/App/Fractals/KochPainter.cs delete mode 100644 FractalPainter/Solved/Step01/App/MainForm.cs delete mode 100644 FractalPainter/Solved/Step01/App/MainForm.resx delete mode 100644 FractalPainter/Solved/Step01/App/PictureBoxImageHolder.cs delete mode 100644 FractalPainter/Solved/Step01/App/Program.cs delete mode 100644 FractalPainter/Solved/Step01/App/SettingsManager.cs delete mode 100644 FractalPainter/Solved/Step01/Infrastructure/Injection/DependencyInjector.cs delete mode 100644 FractalPainter/Solved/Step01/Infrastructure/Injection/INeed.cs delete mode 100644 FractalPainter/Solved/Step01/Infrastructure/UiActions/IUiAction.cs delete mode 100644 FractalPainter/Solved/Step01/Infrastructure/UiActions/UiActionExtensions.cs delete mode 100644 FractalPainter/Solved/Step01/Step01.csproj delete mode 100644 FractalPainter/Solved/Step03/App/Actions/DragonFractalAction.cs delete mode 100644 FractalPainter/Solved/Step03/App/Actions/ImageSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step03/App/Actions/KochFractalAction.cs delete mode 100644 FractalPainter/Solved/Step03/App/Actions/PaletteSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step03/App/Actions/SaveImageAction.cs delete mode 100644 FractalPainter/Solved/Step03/App/AppSettings.cs delete mode 100644 FractalPainter/Solved/Step03/App/Fractals/DragonPainter.cs delete mode 100644 FractalPainter/Solved/Step03/App/Fractals/DragonSettings.cs delete mode 100644 FractalPainter/Solved/Step03/App/Fractals/DragonSettingsGenerator.cs delete mode 100644 FractalPainter/Solved/Step03/App/Fractals/KochPainter.cs delete mode 100644 FractalPainter/Solved/Step03/App/MainForm.cs delete mode 100644 FractalPainter/Solved/Step03/App/MainForm.resx delete mode 100644 FractalPainter/Solved/Step03/App/PictureBoxImageHolder.cs delete mode 100644 FractalPainter/Solved/Step03/App/Program.cs delete mode 100644 FractalPainter/Solved/Step03/App/SettingsManager.cs delete mode 100644 FractalPainter/Solved/Step03/Infrastructure/Injection/DependencyInjector.cs delete mode 100644 FractalPainter/Solved/Step03/Infrastructure/Injection/INeed.cs delete mode 100644 FractalPainter/Solved/Step03/Infrastructure/UiActions/IUiAction.cs delete mode 100644 FractalPainter/Solved/Step03/Infrastructure/UiActions/UiActionExtensions.cs delete mode 100644 FractalPainter/Solved/Step03/Step03.csproj delete mode 100644 FractalPainter/Solved/Step04/App/Actions/DragonFractalAction.cs delete mode 100644 FractalPainter/Solved/Step04/App/Actions/ImageSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step04/App/Actions/KochFractalAction.cs delete mode 100644 FractalPainter/Solved/Step04/App/Actions/PaletteSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step04/App/Actions/SaveImageAction.cs delete mode 100644 FractalPainter/Solved/Step04/App/AppSettings.cs delete mode 100644 FractalPainter/Solved/Step04/App/Fractals/DragonPainter.cs delete mode 100644 FractalPainter/Solved/Step04/App/Fractals/DragonSettings.cs delete mode 100644 FractalPainter/Solved/Step04/App/Fractals/DragonSettingsGenerator.cs delete mode 100644 FractalPainter/Solved/Step04/App/Fractals/KochPainter.cs delete mode 100644 FractalPainter/Solved/Step04/App/MainForm.cs delete mode 100644 FractalPainter/Solved/Step04/App/MainForm.resx delete mode 100644 FractalPainter/Solved/Step04/App/PictureBoxImageHolder.cs delete mode 100644 FractalPainter/Solved/Step04/App/Program.cs delete mode 100644 FractalPainter/Solved/Step04/App/SettingsManager.cs delete mode 100644 FractalPainter/Solved/Step04/Infrastructure/Injection/DependencyInjector.cs delete mode 100644 FractalPainter/Solved/Step04/Infrastructure/Injection/INeed.cs delete mode 100644 FractalPainter/Solved/Step04/Infrastructure/UiActions/IUiAction.cs delete mode 100644 FractalPainter/Solved/Step04/Infrastructure/UiActions/UiActionExtensions.cs delete mode 100644 FractalPainter/Solved/Step04/Step04.csproj delete mode 100644 FractalPainter/Solved/Step05/App/Actions/DragonFractalAction.cs delete mode 100644 FractalPainter/Solved/Step05/App/Actions/ImageSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step05/App/Actions/KochFractalAction.cs delete mode 100644 FractalPainter/Solved/Step05/App/Actions/PaletteSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step05/App/Actions/SaveImageAction.cs delete mode 100644 FractalPainter/Solved/Step05/App/AppSettings.cs delete mode 100644 FractalPainter/Solved/Step05/App/Fractals/DragonPainter.cs delete mode 100644 FractalPainter/Solved/Step05/App/Fractals/DragonSettings.cs delete mode 100644 FractalPainter/Solved/Step05/App/Fractals/DragonSettingsGenerator.cs delete mode 100644 FractalPainter/Solved/Step05/App/Fractals/IDragonPainterFactory.cs delete mode 100644 FractalPainter/Solved/Step05/App/Fractals/KochPainter.cs delete mode 100644 FractalPainter/Solved/Step05/App/MainForm.cs delete mode 100644 FractalPainter/Solved/Step05/App/MainForm.resx delete mode 100644 FractalPainter/Solved/Step05/App/PictureBoxImageHolder.cs delete mode 100644 FractalPainter/Solved/Step05/App/Program.cs delete mode 100644 FractalPainter/Solved/Step05/App/SettingsManager.cs delete mode 100644 FractalPainter/Solved/Step05/Infrastructure/Injection/DependencyInjector.cs delete mode 100644 FractalPainter/Solved/Step05/Infrastructure/Injection/INeed.cs delete mode 100644 FractalPainter/Solved/Step05/Infrastructure/UiActions/IUiAction.cs delete mode 100644 FractalPainter/Solved/Step05/Infrastructure/UiActions/UiActionExtensions.cs delete mode 100644 FractalPainter/Solved/Step05/Step05.csproj delete mode 100644 FractalPainter/Solved/Step06/App/Actions/DragonFractalAction.cs delete mode 100644 FractalPainter/Solved/Step06/App/Actions/ImageSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step06/App/Actions/KochFractalAction.cs delete mode 100644 FractalPainter/Solved/Step06/App/Actions/PaletteSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step06/App/Actions/SaveImageAction.cs delete mode 100644 FractalPainter/Solved/Step06/App/AppSettings.cs delete mode 100644 FractalPainter/Solved/Step06/App/Fractals/DragonPainter.cs delete mode 100644 FractalPainter/Solved/Step06/App/Fractals/DragonSettings.cs delete mode 100644 FractalPainter/Solved/Step06/App/Fractals/DragonSettingsGenerator.cs delete mode 100644 FractalPainter/Solved/Step06/App/Fractals/IDragonPainterFactory.cs delete mode 100644 FractalPainter/Solved/Step06/App/Fractals/KochPainter.cs delete mode 100644 FractalPainter/Solved/Step06/App/MainForm.cs delete mode 100644 FractalPainter/Solved/Step06/App/MainForm.resx delete mode 100644 FractalPainter/Solved/Step06/App/PictureBoxImageHolder.cs delete mode 100644 FractalPainter/Solved/Step06/App/Program.cs delete mode 100644 FractalPainter/Solved/Step06/App/SettingsManager.cs delete mode 100644 FractalPainter/Solved/Step06/Infrastructure/Injection/DependencyInjector.cs delete mode 100644 FractalPainter/Solved/Step06/Infrastructure/Injection/INeed.cs delete mode 100644 FractalPainter/Solved/Step06/Infrastructure/UiActions/IUiAction.cs delete mode 100644 FractalPainter/Solved/Step06/Infrastructure/UiActions/UiActionExtensions.cs delete mode 100644 FractalPainter/Solved/Step06/Step06.csproj delete mode 100644 FractalPainter/Solved/Step07/App/Actions/DragonFractalAction.cs delete mode 100644 FractalPainter/Solved/Step07/App/Actions/ImageSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step07/App/Actions/KochFractalAction.cs delete mode 100644 FractalPainter/Solved/Step07/App/Actions/PaletteSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step07/App/Actions/SaveImageAction.cs delete mode 100644 FractalPainter/Solved/Step07/App/AppSettings.cs delete mode 100644 FractalPainter/Solved/Step07/App/Fractals/DragonPainter.cs delete mode 100644 FractalPainter/Solved/Step07/App/Fractals/DragonSettings.cs delete mode 100644 FractalPainter/Solved/Step07/App/Fractals/DragonSettingsGenerator.cs delete mode 100644 FractalPainter/Solved/Step07/App/Fractals/IDragonPainterFactory.cs delete mode 100644 FractalPainter/Solved/Step07/App/Fractals/KochPainter.cs delete mode 100644 FractalPainter/Solved/Step07/App/MainForm.cs delete mode 100644 FractalPainter/Solved/Step07/App/MainForm.resx delete mode 100644 FractalPainter/Solved/Step07/App/PictureBoxImageHolder.cs delete mode 100644 FractalPainter/Solved/Step07/App/Program.cs delete mode 100644 FractalPainter/Solved/Step07/App/SettingsManager.cs delete mode 100644 FractalPainter/Solved/Step07/Infrastructure/Injection/DependencyInjector.cs delete mode 100644 FractalPainter/Solved/Step07/Infrastructure/Injection/INeed.cs delete mode 100644 FractalPainter/Solved/Step07/Infrastructure/UiActions/IUiAction.cs delete mode 100644 FractalPainter/Solved/Step07/Infrastructure/UiActions/UiActionExtensions.cs delete mode 100644 FractalPainter/Solved/Step07/Step07.csproj delete mode 100644 FractalPainter/Solved/Step08/App/Actions/DragonFractalAction.cs delete mode 100644 FractalPainter/Solved/Step08/App/Actions/ImageSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step08/App/Actions/KochFractalAction.cs delete mode 100644 FractalPainter/Solved/Step08/App/Actions/PaletteSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step08/App/Actions/SaveImageAction.cs delete mode 100644 FractalPainter/Solved/Step08/App/AppSettings.cs delete mode 100644 FractalPainter/Solved/Step08/App/Fractals/DragonPainter.cs delete mode 100644 FractalPainter/Solved/Step08/App/Fractals/DragonSettings.cs delete mode 100644 FractalPainter/Solved/Step08/App/Fractals/DragonSettingsGenerator.cs delete mode 100644 FractalPainter/Solved/Step08/App/Fractals/IDragonPainterFactory.cs delete mode 100644 FractalPainter/Solved/Step08/App/Fractals/KochPainter.cs delete mode 100644 FractalPainter/Solved/Step08/App/MainForm.cs delete mode 100644 FractalPainter/Solved/Step08/App/MainForm.resx delete mode 100644 FractalPainter/Solved/Step08/App/PictureBoxImageHolder.cs delete mode 100644 FractalPainter/Solved/Step08/App/Program.cs delete mode 100644 FractalPainter/Solved/Step08/App/SettingsManager.cs delete mode 100644 FractalPainter/Solved/Step08/Infrastructure/Injection/DependencyInjector.cs delete mode 100644 FractalPainter/Solved/Step08/Infrastructure/Injection/INeed.cs delete mode 100644 FractalPainter/Solved/Step08/Infrastructure/UiActions/IUiAction.cs delete mode 100644 FractalPainter/Solved/Step08/Infrastructure/UiActions/UiActionExtensions.cs delete mode 100644 FractalPainter/Solved/Step08/Step08.csproj delete mode 100644 FractalPainter/Solved/Step09/App/Actions/DragonFractalAction.cs delete mode 100644 FractalPainter/Solved/Step09/App/Actions/ImageSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step09/App/Actions/KochFractalAction.cs delete mode 100644 FractalPainter/Solved/Step09/App/Actions/PaletteSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step09/App/Actions/SaveImageAction.cs delete mode 100644 FractalPainter/Solved/Step09/App/AppSettings.cs delete mode 100644 FractalPainter/Solved/Step09/App/Fractals/DragonPainter.cs delete mode 100644 FractalPainter/Solved/Step09/App/Fractals/DragonSettings.cs delete mode 100644 FractalPainter/Solved/Step09/App/Fractals/DragonSettingsGenerator.cs delete mode 100644 FractalPainter/Solved/Step09/App/Fractals/IDragonPainterFactory.cs delete mode 100644 FractalPainter/Solved/Step09/App/Fractals/KochPainter.cs delete mode 100644 FractalPainter/Solved/Step09/App/MainForm.cs delete mode 100644 FractalPainter/Solved/Step09/App/MainForm.resx delete mode 100644 FractalPainter/Solved/Step09/App/PictureBoxImageHolder.cs delete mode 100644 FractalPainter/Solved/Step09/App/Program.cs delete mode 100644 FractalPainter/Solved/Step09/App/SettingsManager.cs delete mode 100644 FractalPainter/Solved/Step09/Infrastructure/UiActions/IUiAction.cs delete mode 100644 FractalPainter/Solved/Step09/Infrastructure/UiActions/UiActionExtensions.cs delete mode 100644 FractalPainter/Solved/Step09/Step09.csproj delete mode 100644 FractalPainter/Solved/Step10/App/Actions/DragonFractalAction.cs delete mode 100644 FractalPainter/Solved/Step10/App/Actions/ImageSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step10/App/Actions/KochFractalAction.cs delete mode 100644 FractalPainter/Solved/Step10/App/Actions/PaletteSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step10/App/Actions/SaveImageAction.cs delete mode 100644 FractalPainter/Solved/Step10/App/AppSettings.cs delete mode 100644 FractalPainter/Solved/Step10/App/Fractals/DragonPainter.cs delete mode 100644 FractalPainter/Solved/Step10/App/Fractals/DragonSettings.cs delete mode 100644 FractalPainter/Solved/Step10/App/Fractals/DragonSettingsGenerator.cs delete mode 100644 FractalPainter/Solved/Step10/App/Fractals/IDragonPainterFactory.cs delete mode 100644 FractalPainter/Solved/Step10/App/Fractals/KochPainter.cs delete mode 100644 FractalPainter/Solved/Step10/App/MainForm.cs delete mode 100644 FractalPainter/Solved/Step10/App/MainForm.resx delete mode 100644 FractalPainter/Solved/Step10/App/PictureBoxImageHolder.cs delete mode 100644 FractalPainter/Solved/Step10/App/Program.cs delete mode 100644 FractalPainter/Solved/Step10/App/SettingsManager.cs delete mode 100644 FractalPainter/Solved/Step10/Infrastructure/UiActions/IUiAction.cs delete mode 100644 FractalPainter/Solved/Step10/Infrastructure/UiActions/UiActionExtensions.cs delete mode 100644 FractalPainter/Solved/Step10/Step10.csproj delete mode 100644 FractalPainter/Solved/Step11/App/Actions/DragonFractalAction.cs delete mode 100644 FractalPainter/Solved/Step11/App/Actions/ImageSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step11/App/Actions/KochFractalAction.cs delete mode 100644 FractalPainter/Solved/Step11/App/Actions/PaletteSettingsAction.cs delete mode 100644 FractalPainter/Solved/Step11/App/Actions/SaveImageAction.cs delete mode 100644 FractalPainter/Solved/Step11/App/AppSettings.cs delete mode 100644 FractalPainter/Solved/Step11/App/Fractals/DragonPainter.cs delete mode 100644 FractalPainter/Solved/Step11/App/Fractals/DragonSettings.cs delete mode 100644 FractalPainter/Solved/Step11/App/Fractals/DragonSettingsGenerator.cs delete mode 100644 FractalPainter/Solved/Step11/App/Fractals/IDragonPainterFactory.cs delete mode 100644 FractalPainter/Solved/Step11/App/Fractals/KochPainter.cs delete mode 100644 FractalPainter/Solved/Step11/App/MainForm.cs delete mode 100644 FractalPainter/Solved/Step11/App/MainForm.resx delete mode 100644 FractalPainter/Solved/Step11/App/PictureBoxImageHolder.cs delete mode 100644 FractalPainter/Solved/Step11/App/Program.cs delete mode 100644 FractalPainter/Solved/Step11/App/SettingsManager.cs delete mode 100644 FractalPainter/Solved/Step11/Infrastructure/UiActions/EnumExtensions.cs delete mode 100644 FractalPainter/Solved/Step11/Infrastructure/UiActions/IUiAction.cs delete mode 100644 FractalPainter/Solved/Step11/Infrastructure/UiActions/MenuCategory.cs delete mode 100644 FractalPainter/Solved/Step11/Infrastructure/UiActions/UiActionExtensions.cs delete mode 100644 FractalPainter/Solved/Step11/Step11.csproj delete mode 100644 FractalPainter/app.settings delete mode 100644 FractalPainter/fractalPainter.csproj diff --git a/FractalPainter/App/Actions/DragonFractalAction.cs b/FractalPainter/App/Actions/DragonFractalAction.cs deleted file mode 100644 index 48765c0fe..000000000 --- a/FractalPainter/App/Actions/DragonFractalAction.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using FractalPainting.App.Fractals; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Infrastructure.UiActions; - -namespace FractalPainting.App.Actions -{ - public class DragonFractalAction : IUiAction - { - private IDragonPainterFactory _factory; - private Func _settingsFactory; - public DragonFractalAction(IDragonPainterFactory dragonPainterFactory, Func dragonSettingsFactory) - { - _settingsFactory = dragonSettingsFactory; - _factory = dragonPainterFactory; - } - - public string Category => "Фракталы"; - public string Name => "Дракон"; - public string Description => "Дракон Хартера-Хейтуэя"; - - public void Perform() - { - var dragonSettings = CreateRandomSettings(); - SettingsForm.For(dragonSettings).ShowDialog(); - _factory.CreateDragonPainter(dragonSettings).Paint(); - } - - private DragonSettings CreateRandomSettings() - { - return _settingsFactory(new Random()).Generate(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/App/Actions/IDragonPainterFactory.cs b/FractalPainter/App/Actions/IDragonPainterFactory.cs deleted file mode 100644 index 95866db20..000000000 --- a/FractalPainter/App/Actions/IDragonPainterFactory.cs +++ /dev/null @@ -1,9 +0,0 @@ -using FractalPainting.App.Fractals; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.App.Actions; - -public interface IDragonPainterFactory -{ - DragonPainter CreateDragonPainter(DragonSettings settings); -} \ No newline at end of file diff --git a/FractalPainter/App/Actions/ImageSettingsAction.cs b/FractalPainter/App/Actions/ImageSettingsAction.cs deleted file mode 100644 index 943b02448..000000000 --- a/FractalPainter/App/Actions/ImageSettingsAction.cs +++ /dev/null @@ -1,27 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Infrastructure.UiActions; - -namespace FractalPainting.App.Actions -{ - public class ImageSettingsAction : IUiAction - { - private IImageHolder imageHolder; - private ImageSettings imageSettings; - - public ImageSettingsAction(IImageHolder imageHolder, ImageSettings imageSettings) - { - this.imageHolder = imageHolder; - this.imageSettings = imageSettings; - } - - public string Category => "Настройки"; - public string Name => "Изображение..."; - public string Description => "Размеры изображения"; - - public void Perform() - { - SettingsForm.For(imageSettings).ShowDialog(); - imageHolder.RecreateImage(imageSettings); - } - } -} \ No newline at end of file diff --git a/FractalPainter/App/Actions/KochFractalAction.cs b/FractalPainter/App/Actions/KochFractalAction.cs deleted file mode 100644 index a9dc18356..000000000 --- a/FractalPainter/App/Actions/KochFractalAction.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using FractalPainting.App.Fractals; -using FractalPainting.Infrastructure.UiActions; - -namespace FractalPainting.App.Actions -{ - public class KochFractalAction : IUiAction - { - private readonly Lazy _kochPainter; - public KochFractalAction(Lazy kochPainter) - { - _kochPainter = kochPainter; - } - - public string Category => "Фракталы"; - public string Name => "Кривая Коха"; - public string Description => "Кривая Коха"; - - public void Perform() - { - _kochPainter.Value.Paint(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/App/Actions/PaletteSettingsAction.cs b/FractalPainter/App/Actions/PaletteSettingsAction.cs deleted file mode 100644 index db01e8a39..000000000 --- a/FractalPainter/App/Actions/PaletteSettingsAction.cs +++ /dev/null @@ -1,24 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Infrastructure.UiActions; - -namespace FractalPainting.App.Actions -{ - public class PaletteSettingsAction : IUiAction - { - private Palette palette; - - public PaletteSettingsAction(Palette palette) - { - this.palette = palette; - } - - public string Category => "Настройки"; - public string Name => "Палитра..."; - public string Description => "Цвета для рисования фракталов"; - - public void Perform() - { - SettingsForm.For(palette).ShowDialog(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/App/Actions/SaveImageAction.cs b/FractalPainter/App/Actions/SaveImageAction.cs deleted file mode 100644 index 78c798b70..000000000 --- a/FractalPainter/App/Actions/SaveImageAction.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System.IO; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Infrastructure.UiActions; - -namespace FractalPainting.App.Actions -{ - public class SaveImageAction : IUiAction - { - private IImageDirectoryProvider imageDirectoryProvider; - private IImageHolder imageHolder; - - public SaveImageAction(IImageHolder imageHolder, IImageDirectoryProvider imageDirectoryProvider) - { - this.imageHolder = imageHolder; - this.imageDirectoryProvider = imageDirectoryProvider; - } - public void SetDependency(IImageDirectoryProvider dependency) - { - imageDirectoryProvider = dependency; - } - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public string Category => "Файл"; - public string Name => "Сохранить..."; - public string Description => "Сохранить изображение в файл"; - - public void Perform() - { - var dialog = new SaveFileDialog - { - CheckFileExists = false, - InitialDirectory = Path.GetFullPath(imageDirectoryProvider.ImagesDirectory), - DefaultExt = "bmp", - FileName = "image.bmp", - Filter = "Изображения (*.bmp)|*.bmp" - }; - var res = dialog.ShowDialog(); - if (res == DialogResult.OK) - imageHolder.SaveImage(dialog.FileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/App/AppSettings.cs b/FractalPainter/App/AppSettings.cs deleted file mode 100644 index b4db60ea4..000000000 --- a/FractalPainter/App/AppSettings.cs +++ /dev/null @@ -1,10 +0,0 @@ -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.App -{ - public class AppSettings : IImageDirectoryProvider, IImageSettingsProvider - { - public string ImagesDirectory { get; set; } - public ImageSettings ImageSettings { get; set; } - } -} \ No newline at end of file diff --git a/FractalPainter/App/Fractals/DragonPainter.cs b/FractalPainter/App/Fractals/DragonPainter.cs deleted file mode 100644 index 4e6cba7a9..000000000 --- a/FractalPainter/App/Fractals/DragonPainter.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System; -using System.Drawing; -using System.Linq; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.App.Fractals -{ - public class DragonPainter - { - private readonly IImageHolder imageHolder; - private readonly DragonSettings settings; - private readonly float size; - private Size imageSize; - private Palette palette; - - public DragonPainter(IImageHolder imageHolder, DragonSettings settings, Palette palette) - { - this.imageHolder = imageHolder; - this.settings = settings; - this.palette = palette; - imageSize = imageHolder.GetImageSize(); - size = Math.Min(imageSize.Width, imageSize.Height)/2.1f; - } - - public void Paint() - { - using (var graphics = imageHolder.StartDrawing()) - { - graphics.FillRectangle(new SolidBrush(palette.BackgroundColor), 0, 0, imageSize.Width, imageSize.Height); - var r = new Random(); - var cosa = (float) Math.Cos(settings.Angle1); - var sina = (float) Math.Sin(settings.Angle1); - var cosb = (float) Math.Cos(settings.Angle2); - var sinb = (float) Math.Sin(settings.Angle2); - var shiftX = settings.ShiftX*size*0.8f; - var shiftY = settings.ShiftY*size*0.8f; - var scale = settings.Scale; - var p = new PointF(0, 0); - foreach (var i in Enumerable.Range(0, settings.IterationsCount)) - { - graphics.FillRectangle(new SolidBrush(palette.PrimaryColor), imageSize.Width/3f + p.X, imageSize.Height/2f + p.Y, 1, 1); - if (r.Next(0, 2) == 0) - p = new PointF(scale*(p.X*cosa - p.Y*sina), scale*(p.X*sina + p.Y*cosa)); - else - p = new PointF(scale*(p.X*cosb - p.Y*sinb) + shiftX, scale*(p.X*sinb + p.Y*cosb) + shiftY); - if (i%100 == 0) imageHolder.UpdateUi(); - } - } - imageHolder.UpdateUi(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/App/Fractals/DragonSettings.cs b/FractalPainter/App/Fractals/DragonSettings.cs deleted file mode 100644 index 084e55d38..000000000 --- a/FractalPainter/App/Fractals/DragonSettings.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace FractalPainting.App.Fractals -{ - public class DragonSettings - { - public double Angle1 { get; set; } = Math.PI / 4; - public double Angle2 { get; set; } = 3 * Math.PI / 4; - public float ShiftX { get; set; } = 1; - public float ShiftY { get; set; } = 0; - public float Scale { get; set; } = (float)(1 / Math.Sqrt(2)); - public int IterationsCount { get; set; } = 20000; - - } -} \ No newline at end of file diff --git a/FractalPainter/App/Fractals/DragonSettingsGenerator.cs b/FractalPainter/App/Fractals/DragonSettingsGenerator.cs deleted file mode 100644 index baec5b400..000000000 --- a/FractalPainter/App/Fractals/DragonSettingsGenerator.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace FractalPainting.App.Fractals -{ - public class DragonSettingsGenerator - { - private readonly Random random; - - public DragonSettingsGenerator(Random random) - { - this.random = random; - } - - public DragonSettings Generate() - { - return new DragonSettings - { - Angle1 = random.NextDouble() / 2 + 0.5, - Angle2 = 3 * (random.NextDouble() / 2 + 0.5), - ShiftX = 1, - ShiftY = 0, - Scale = (float)(1/Math.Sqrt(2) + (random.NextDouble() - 0.5)/10) - }; - } - - } -} \ No newline at end of file diff --git a/FractalPainter/App/Fractals/KochPainter.cs b/FractalPainter/App/Fractals/KochPainter.cs deleted file mode 100644 index db0dff4ea..000000000 --- a/FractalPainter/App/Fractals/KochPainter.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.Drawing; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.App.Fractals -{ - public class KochPainter - { - private readonly IImageHolder imageHolder; - private readonly Palette palette; - private Size imageSize; - - public KochPainter(IImageHolder imageHolder, Palette palette) - { - this.imageHolder = imageHolder; - this.palette = palette; - imageSize = imageHolder.GetImageSize(); - } - - public void Paint() - { - using (var graphics = imageHolder.StartDrawing()) - using (var backgroundBrush = new SolidBrush(palette.BackgroundColor)) - { - graphics.FillRectangle(backgroundBrush, 0, 0, imageSize.Width, imageSize.Height); - DrawSegment(graphics, 0, imageSize.Height*0.9f, imageSize.Width, imageSize.Height*0.9f, true); - } - imageHolder.UpdateUi(); - } - - private void DrawSegment(Graphics graphics, float x0, float y0, float x1, float y1, bool primaryColor) - { - var len2 = (x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1); - if (len2 < 4) - { - if (y0 < 0 || y1 < 0) return; - using (var penBrush = new SolidBrush(primaryColor ? palette.PrimaryColor : palette.SecondaryColor)) - { - var pen = new Pen(penBrush, 3); - graphics.DrawLine(pen, x0, y0, x1, y1); - } - } - else - { - var vx = (x1 - x0)/3; - var vy = (y1 - y0)/3; - DrawSegment(graphics, x0, y0, x0 + vx, y0 + vy, primaryColor); - var k = (float) Math.Sqrt(3)/2f; - var px = (x0 + x1)/2 + vy*k; - var py = (y0 + y1)/2 - vx*k; - DrawSegment(graphics, x0 + vx, y0 + vy, px, py, !primaryColor); - DrawSegment(graphics, px, py, x0 + 2*vx, y0 + 2*vy, !primaryColor); - DrawSegment(graphics, x0 + 2*vx, y0 + 2*vy, x1, y1, primaryColor); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/App/MainForm.cs b/FractalPainter/App/MainForm.cs deleted file mode 100644 index d3deab8c0..000000000 --- a/FractalPainter/App/MainForm.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using System.Drawing; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Infrastructure.UiActions; - -namespace FractalPainting.App -{ - public class MainForm : Form - { - public MainForm(IUiAction[] actions, PictureBoxImageHolder pictureBoxImageHolder, ImageSettings imageSettings) - { - ClientSize = new Size(imageSettings.Width, imageSettings.Height); - - var mainMenu = new MenuStrip(); - mainMenu.Items.AddRange(actions.ToMenuItems()); - Controls.Add(mainMenu); - - pictureBoxImageHolder.RecreateImage(imageSettings); - pictureBoxImageHolder.Dock = DockStyle.Fill; - Controls.Add(pictureBoxImageHolder); - } - - protected override void OnShown(EventArgs e) - { - base.OnShown(e); - Text = @"Fractal Painter"; - } - } -} \ No newline at end of file diff --git a/FractalPainter/App/MainForm.resx b/FractalPainter/App/MainForm.resx deleted file mode 100644 index 9d845151a..000000000 --- a/FractalPainter/App/MainForm.resx +++ /dev/null @@ -1,123 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 17, 17 - - \ No newline at end of file diff --git a/FractalPainter/App/PictureBoxImageHolder.cs b/FractalPainter/App/PictureBoxImageHolder.cs deleted file mode 100644 index 9fe74e328..000000000 --- a/FractalPainter/App/PictureBoxImageHolder.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Drawing; -using System.Drawing.Imaging; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.App -{ - public class PictureBoxImageHolder : PictureBox, IImageHolder - { - public Size GetImageSize() - { - FailIfNotInitialized(); - return Image.Size; - } - - public Graphics StartDrawing() - { - FailIfNotInitialized(); - return Graphics.FromImage(Image); - } - - private void FailIfNotInitialized() - { - if (Image == null) - throw new InvalidOperationException("Call PictureBoxImageHolder.RecreateImage before other method call!"); - } - - public void UpdateUi() - { - Refresh(); - Application.DoEvents(); - } - - public void RecreateImage(ImageSettings imageSettings) - { - Image = new Bitmap(imageSettings.Width, imageSettings.Height, PixelFormat.Format24bppRgb); - } - - public void SaveImage(string fileName) - { - FailIfNotInitialized(); - Image.Save(fileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/App/Program.cs b/FractalPainter/App/Program.cs deleted file mode 100644 index 6b2748b5e..000000000 --- a/FractalPainter/App/Program.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.App.Actions; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Infrastructure.UiActions; -using Ninject; -using Ninject.Extensions.Conventions; -using Ninject.Extensions.Factory; - -namespace FractalPainting.App -{ - internal static class Program - { - /// - /// The main entry point for the application. - /// - [STAThread] - private static void Main() - { - try - { - var container = new StandardKernel(); - - container.Bind().To().InSingletonScope(); - container.Bind().ToSelf().InSingletonScope(); - - container.Bind().ToFactory(); - - container.Bind(x => x - .FromThisAssembly() - .SelectAllClasses().InheritedFrom() - .BindAllInterfaces()); - - container.Bind().To(); - container.Bind().To(); - - container.Bind() - .ToMethod(context => context.Kernel.Get().Load()).InSingletonScope(); - container.Bind().ToMethod(context => context.Kernel.Get().ImageSettings).InSingletonScope(); - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - Application.Run(container.Get()); - } - catch (Exception e) - { - MessageBox.Show(e.Message); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/App/SettingsManager.cs b/FractalPainter/App/SettingsManager.cs deleted file mode 100644 index 580c1c422..000000000 --- a/FractalPainter/App/SettingsManager.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.App -{ - public class SettingsManager - { - private readonly IObjectSerializer serializer; - private readonly IBlobStorage storage; - private string settingsFilename; - - public SettingsManager(IObjectSerializer serializer, IBlobStorage storage) - { - this.serializer = serializer; - this.storage = storage; - } - - public AppSettings Load() - { - try - { - settingsFilename = "app.settings"; - var data = storage.Get(settingsFilename); - if (data == null) - { - var defaultSettings = CreateDefaultSettings(); - Save(defaultSettings); - return defaultSettings; - } - return serializer.Deserialize(data); - } - catch (Exception e) - { - MessageBox.Show(e.Message, "не удалось загрузить настройки"); - return CreateDefaultSettings(); - } - } - - private static AppSettings CreateDefaultSettings() - { - return new AppSettings - { - ImagesDirectory = ".", - ImageSettings = new ImageSettings() - }; - } - - public void Save(AppSettings settings) - { - storage.Set(settingsFilename, serializer.Serialize(settings)); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Infrastructure/Common/FileBlobStorage.cs b/FractalPainter/Infrastructure/Common/FileBlobStorage.cs deleted file mode 100644 index a5feff667..000000000 --- a/FractalPainter/Infrastructure/Common/FileBlobStorage.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.IO; - -namespace FractalPainting.Infrastructure.Common -{ - public class FileBlobStorage : IBlobStorage - { - public byte[] Get(string name) - { - return File.Exists(name) ? File.ReadAllBytes(name) : null; - } - - public void Set(string name, byte[] content) - { - File.WriteAllBytes(name, content); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Infrastructure/Common/IBlobStorage.cs b/FractalPainter/Infrastructure/Common/IBlobStorage.cs deleted file mode 100644 index 288c37530..000000000 --- a/FractalPainter/Infrastructure/Common/IBlobStorage.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace FractalPainting.Infrastructure.Common -{ - public interface IBlobStorage - { - byte[] Get(string name); - void Set(string name, byte[] content); - } -} \ No newline at end of file diff --git a/FractalPainter/Infrastructure/Common/IImageDirectoryProvider.cs b/FractalPainter/Infrastructure/Common/IImageDirectoryProvider.cs deleted file mode 100644 index 29ae0dbde..000000000 --- a/FractalPainter/Infrastructure/Common/IImageDirectoryProvider.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace FractalPainting.Infrastructure.Common -{ - public interface IImageDirectoryProvider - { - string ImagesDirectory { get; } - } -} \ No newline at end of file diff --git a/FractalPainter/Infrastructure/Common/IImageHolder.cs b/FractalPainter/Infrastructure/Common/IImageHolder.cs deleted file mode 100644 index 58e4f2d2d..000000000 --- a/FractalPainter/Infrastructure/Common/IImageHolder.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Drawing; - -namespace FractalPainting.Infrastructure.Common -{ - public interface IImageHolder - { - Size GetImageSize(); - Graphics StartDrawing(); - void UpdateUi(); - void RecreateImage(ImageSettings settings); - void SaveImage(string fileName); - } -} \ No newline at end of file diff --git a/FractalPainter/Infrastructure/Common/IImageSettingsProvider.cs b/FractalPainter/Infrastructure/Common/IImageSettingsProvider.cs deleted file mode 100644 index b9184171b..000000000 --- a/FractalPainter/Infrastructure/Common/IImageSettingsProvider.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace FractalPainting.Infrastructure.Common -{ - public interface IImageSettingsProvider - { - ImageSettings ImageSettings { get; } - } -} \ No newline at end of file diff --git a/FractalPainter/Infrastructure/Common/IObjectSerializer.cs b/FractalPainter/Infrastructure/Common/IObjectSerializer.cs deleted file mode 100644 index c8f56e81a..000000000 --- a/FractalPainter/Infrastructure/Common/IObjectSerializer.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace FractalPainting.Infrastructure.Common -{ - public interface IObjectSerializer - { - T Deserialize(byte[] bytes); - byte[] Serialize(T obj); - } -} \ No newline at end of file diff --git a/FractalPainter/Infrastructure/Common/ImageSettings.cs b/FractalPainter/Infrastructure/Common/ImageSettings.cs deleted file mode 100644 index 56f860ce0..000000000 --- a/FractalPainter/Infrastructure/Common/ImageSettings.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace FractalPainting.Infrastructure.Common -{ - public class ImageSettings - { - public int Width { get; set; } = 100; - public int Height { get; set; } = 100; - } -} \ No newline at end of file diff --git a/FractalPainter/Infrastructure/Common/Palette.cs b/FractalPainter/Infrastructure/Common/Palette.cs deleted file mode 100644 index db70c1472..000000000 --- a/FractalPainter/Infrastructure/Common/Palette.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Drawing; - -namespace FractalPainting.Infrastructure.Common -{ - public class Palette - { - public Color PrimaryColor { get; set; } = Color.Yellow; - public Color SecondaryColor { get; set; } = Color.Red; - public Color BackgroundColor { get; set; } = Color.DarkBlue; - } -} \ No newline at end of file diff --git a/FractalPainter/Infrastructure/Common/SettingsForm.cs b/FractalPainter/Infrastructure/Common/SettingsForm.cs deleted file mode 100644 index e5ac2aa98..000000000 --- a/FractalPainter/Infrastructure/Common/SettingsForm.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Windows.Forms; - -namespace FractalPainting.Infrastructure.Common -{ - public static class SettingsForm - { - public static SettingsForm For(TSettings settings) - { - return new SettingsForm(settings); - } - } - - public class SettingsForm : Form - { - public SettingsForm(TSettings settings) - { - var okButton = new Button - { - Text = "OK", - DialogResult = DialogResult.OK, - Dock = DockStyle.Bottom, - }; - Controls.Add(okButton); - Controls.Add(new PropertyGrid - { - SelectedObject = settings, - Dock = DockStyle.Fill - }); - AcceptButton = okButton; - } - - protected override void OnLoad(EventArgs e) - { - base.OnLoad(e); - Text = ""; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Infrastructure/Common/XmlObjectSerializer.cs b/FractalPainter/Infrastructure/Common/XmlObjectSerializer.cs deleted file mode 100644 index 3b2046fa2..000000000 --- a/FractalPainter/Infrastructure/Common/XmlObjectSerializer.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.IO; -using System.Xml.Serialization; - -namespace FractalPainting.Infrastructure.Common -{ - public class XmlObjectSerializer : IObjectSerializer - { - public T Deserialize(byte[] bytes) - { - using (var ms = new MemoryStream(bytes)) - return (T) new XmlSerializer(typeof(T)).Deserialize(ms); - } - - public byte[] Serialize(T obj) - { - using (var ms = new MemoryStream()) - { - new XmlSerializer(typeof(T)).Serialize(ms, obj); - return ms.ToArray(); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Infrastructure/UiActions/IUiAction.cs b/FractalPainter/Infrastructure/UiActions/IUiAction.cs deleted file mode 100644 index 1ea934ce0..000000000 --- a/FractalPainter/Infrastructure/UiActions/IUiAction.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace FractalPainting.Infrastructure.UiActions -{ - public interface IUiAction - { - string Category { get; } - string Name { get; } - string Description { get; } - void Perform(); - } -} \ No newline at end of file diff --git a/FractalPainter/Infrastructure/UiActions/UiActionExtensions.cs b/FractalPainter/Infrastructure/UiActions/UiActionExtensions.cs deleted file mode 100644 index a5c521b07..000000000 --- a/FractalPainter/Infrastructure/UiActions/UiActionExtensions.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Windows.Forms; - -namespace FractalPainting.Infrastructure.UiActions -{ - public static class UiActionExtensions - { - public static ToolStripItem[] ToMenuItems(this IUiAction[] actions) - { - var items = actions - .GroupBy(a => a.Category) - .Select(g => CreateTopLevelMenuItem(g.Key, g.ToList())) - .Cast() - .ToArray(); - return items; - } - - private static ToolStripMenuItem CreateTopLevelMenuItem(string name, IList items) - { - var menuItems = items.Select(a => a.ToMenuItem()).ToArray(); - return new ToolStripMenuItem(name, null, menuItems); - } - - public static ToolStripItem ToMenuItem(this IUiAction action) - { - return - new ToolStripMenuItem(action.Name, null, (sender, args) => action.Perform()) - { - ToolTipText = action.Description, - Tag = action - }; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Readme.md b/FractalPainter/Readme.md deleted file mode 100644 index e9674ef7b..000000000 --- a/FractalPainter/Readme.md +++ /dev/null @@ -1,67 +0,0 @@ -# Задание FractalPainter - -1. Разминка. В классе Program переделайте Main так, чтобы MainForm -создавался контейнером. Удалите у MainForm конструктор без параметров -и сделайте так, чтобы контейнер инжектировал в MainForm список IUiAction. - -2. INeed. Изучите код KochFractalAction. -Изучите механику работы INeed и DependencyInjector. -Оцените такой подход к управлению зависимостями. - -3. Рефакторинг. Измените класс KochFractalAction так, -чтобы его зависимости IImageHolder и Pallette инжектировались -явно через конструктор, без использования интерфейса INeed. - - Подсказка. Сложность в том, чтобы в MainForm и KochFractalAction - оказались ссылки на один и тот же объект PictureBoxImageHolder. - - Убедитесь, что настройка палитры для рисования кривой Коха всё ещё работает. - -4. Еще рефакторинг. Изучите KochFractalAction и поймите, что -на самом деле IImageHolder и Pallette ему не нужны. Измените его так, -чтобы он принимал только KochPainter. - -5. Фабрика. Аналогично удалите INeed, -и явное использование контейнера из класса DragonFractalAction. -Дополнительное ограничение — нельзя менять публичный интерфейс DragonPainter. -Особенность в том, что одна из зависимостей DragonPainter — -DragonSettings оказывается известной только в процессе работы экшена. -Из-за этого вы не можете просить инжектировать в конструктор уже готовый Painter. -Вместо этого инжектируйте фабрику DragonPainter-ов. -https://github.com/ninject/Ninject.Extensions.Factory/wiki/Factory-interface -Подсказка 1: достаточно описать интерфейс фабрики, -а класс фабрики, реализующий этот интерфейс Ninject сгенерирует самостоятельно. -Подсказка 2: проверьте, что изменение настроек работает. В фабриках есть явная -договоренность - имена параметров метода в фабрике должны совпадать с именами -параметров конструктора создаваемой сущности. - -6. Фабрика 2. Используйте для создания DragonSettingsGenerator Func-фабрику -и инжектируйте эту зависимость в DragonFractalAction. -https://github.com/ninject/Ninject.Extensions.Factory/wiki/Func - -7. Новая зависимость. Переведите DragonPainter на использование цветов палитры, -как это сделано в KochPainter. - - Убедитесь, что экшен настройки палитры работает как надо. - Если вы всё сделали правильно, то для добавления зависимости вам не пришлось - править код работы с контейнером вообще. Магия! - -8. Источник зависимости. Аналогично отрефакторите ImageSettingsAction. -Попробуйте придумать, как избавиться от класса IImageSettingsProvider. -Вам поможет ToMethod. -https://github.com/ninject/Ninject/wiki/Providers,-Factory-Methods-and-the-Activation-Context#factory-methods -Обратите внимание, что ToMethod позволяет доставать нужные зависимости из контейнера: -ToMethod(context => context.Kernel.Get() ... ) - - Убедитесь, что окно настройки размера изображения запоминает установленный размер. - -9. Избавьтесь от остальных использований INeed и удалите этот интерфейс -и класс DependencyInjector из проекта. - -10. Обратите внимание на многочисленные привязки к IUiAction. В реальных -проектах количество классов может исчисляться десятками и сотнями. Воспользуйтесь -документацией https://github.com/ninject/Ninject.Extensions.Conventions/wiki/Overview -и найдите, как все эти привязки сделать в одну строчку. - -11. Кажется после последнего рефакторинга пункты меню перемешались. -Что делать? diff --git a/FractalPainter/Solved/Infrastructure/Infrastructure.csproj b/FractalPainter/Solved/Infrastructure/Infrastructure.csproj deleted file mode 100644 index b80772fa4..000000000 --- a/FractalPainter/Solved/Infrastructure/Infrastructure.csproj +++ /dev/null @@ -1,46 +0,0 @@ - - - netcoreapp3.1 - 8 - false - true - FractalPainting.Infrastructure - FractalPainting.Infrastructure - - - - Common\FileBlobStorage.cs - - - Common\IBlobStorage.cs - - - Common\IImageDirectoryProvider.cs - - - Common\IImageHolder.cs - - - Common\IImageSettingsProvider.cs - - - Common\ImageSettings.cs - - - Common\IObjectSerializer.cs - - - Common\Palette.cs - - - Common\SettingsForm.cs - - - Common\XmlObjectSerializer.cs - - - - - - - \ No newline at end of file diff --git a/FractalPainter/Solved/Solved.sln b/FractalPainter/Solved/Solved.sln deleted file mode 100644 index e15469c42..000000000 --- a/FractalPainter/Solved/Solved.sln +++ /dev/null @@ -1,76 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Infrastructure", "Infrastructure\Infrastructure.csproj", "{DBF231B5-CAB6-49AD-AB64-7BC21ECC296F}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step01", "Step01\Step01.csproj", "{F2CCBD42-F636-4C39-B0D1-006331BE6925}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step03", "Step03\Step03.csproj", "{CF9051FC-B9B6-44E3-A50E-5292107A23CE}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step04", "Step04\Step04.csproj", "{8742DC1F-985A-425A-8C47-985EFFEEB2F4}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step05", "Step05\Step05.csproj", "{32FB9056-C1D2-443A-83DE-AA94561A0074}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step06", "Step06\Step06.csproj", "{3F871EDC-92FC-44B0-9C2F-CA89E8D3D0B9}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step07", "Step07\Step07.csproj", "{A34B95BC-86ED-4378-B42C-C02D2877959E}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step08", "Step08\Step08.csproj", "{D89A621B-BE8D-44FA-8C9D-3681380E57AA}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step09", "Step09\Step09.csproj", "{BAA8D352-44BD-4A51-9EF5-1027F5185A76}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step10", "Step10\Step10.csproj", "{6013A58C-CDDF-49E9-8BE1-26A6C4A7E210}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Step11", "Step11\Step11.csproj", "{3DBAA817-5853-47DE-8FDC-C18E7809B324}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {DBF231B5-CAB6-49AD-AB64-7BC21ECC296F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {DBF231B5-CAB6-49AD-AB64-7BC21ECC296F}.Debug|Any CPU.Build.0 = Debug|Any CPU - {DBF231B5-CAB6-49AD-AB64-7BC21ECC296F}.Release|Any CPU.ActiveCfg = Release|Any CPU - {DBF231B5-CAB6-49AD-AB64-7BC21ECC296F}.Release|Any CPU.Build.0 = Release|Any CPU - {F2CCBD42-F636-4C39-B0D1-006331BE6925}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F2CCBD42-F636-4C39-B0D1-006331BE6925}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F2CCBD42-F636-4C39-B0D1-006331BE6925}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F2CCBD42-F636-4C39-B0D1-006331BE6925}.Release|Any CPU.Build.0 = Release|Any CPU - {CF9051FC-B9B6-44E3-A50E-5292107A23CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CF9051FC-B9B6-44E3-A50E-5292107A23CE}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CF9051FC-B9B6-44E3-A50E-5292107A23CE}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CF9051FC-B9B6-44E3-A50E-5292107A23CE}.Release|Any CPU.Build.0 = Release|Any CPU - {8742DC1F-985A-425A-8C47-985EFFEEB2F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8742DC1F-985A-425A-8C47-985EFFEEB2F4}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8742DC1F-985A-425A-8C47-985EFFEEB2F4}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8742DC1F-985A-425A-8C47-985EFFEEB2F4}.Release|Any CPU.Build.0 = Release|Any CPU - {32FB9056-C1D2-443A-83DE-AA94561A0074}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {32FB9056-C1D2-443A-83DE-AA94561A0074}.Debug|Any CPU.Build.0 = Debug|Any CPU - {32FB9056-C1D2-443A-83DE-AA94561A0074}.Release|Any CPU.ActiveCfg = Release|Any CPU - {32FB9056-C1D2-443A-83DE-AA94561A0074}.Release|Any CPU.Build.0 = Release|Any CPU - {3F871EDC-92FC-44B0-9C2F-CA89E8D3D0B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {3F871EDC-92FC-44B0-9C2F-CA89E8D3D0B9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {3F871EDC-92FC-44B0-9C2F-CA89E8D3D0B9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {3F871EDC-92FC-44B0-9C2F-CA89E8D3D0B9}.Release|Any CPU.Build.0 = Release|Any CPU - {A34B95BC-86ED-4378-B42C-C02D2877959E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A34B95BC-86ED-4378-B42C-C02D2877959E}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A34B95BC-86ED-4378-B42C-C02D2877959E}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A34B95BC-86ED-4378-B42C-C02D2877959E}.Release|Any CPU.Build.0 = Release|Any CPU - {D89A621B-BE8D-44FA-8C9D-3681380E57AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D89A621B-BE8D-44FA-8C9D-3681380E57AA}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D89A621B-BE8D-44FA-8C9D-3681380E57AA}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D89A621B-BE8D-44FA-8C9D-3681380E57AA}.Release|Any CPU.Build.0 = Release|Any CPU - {BAA8D352-44BD-4A51-9EF5-1027F5185A76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {BAA8D352-44BD-4A51-9EF5-1027F5185A76}.Debug|Any CPU.Build.0 = Debug|Any CPU - {BAA8D352-44BD-4A51-9EF5-1027F5185A76}.Release|Any CPU.ActiveCfg = Release|Any CPU - {BAA8D352-44BD-4A51-9EF5-1027F5185A76}.Release|Any CPU.Build.0 = Release|Any CPU - {6013A58C-CDDF-49E9-8BE1-26A6C4A7E210}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6013A58C-CDDF-49E9-8BE1-26A6C4A7E210}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6013A58C-CDDF-49E9-8BE1-26A6C4A7E210}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6013A58C-CDDF-49E9-8BE1-26A6C4A7E210}.Release|Any CPU.Build.0 = Release|Any CPU - {3DBAA817-5853-47DE-8FDC-C18E7809B324}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {3DBAA817-5853-47DE-8FDC-C18E7809B324}.Debug|Any CPU.Build.0 = Debug|Any CPU - {3DBAA817-5853-47DE-8FDC-C18E7809B324}.Release|Any CPU.ActiveCfg = Release|Any CPU - {3DBAA817-5853-47DE-8FDC-C18E7809B324}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection -EndGlobal diff --git a/FractalPainter/Solved/Step01/App/Actions/DragonFractalAction.cs b/FractalPainter/Solved/Step01/App/Actions/DragonFractalAction.cs deleted file mode 100644 index 1fdc46169..000000000 --- a/FractalPainter/Solved/Step01/App/Actions/DragonFractalAction.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step01.App.Fractals; -using FractalPainting.Solved.Step01.Infrastructure.Injection; -using FractalPainting.Solved.Step01.Infrastructure.UiActions; -using Ninject; - -namespace FractalPainting.Solved.Step01.App.Actions -{ - public class DragonFractalAction : IUiAction, INeed - { - private IImageHolder imageHolder; - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public string Category => "Фракталы"; - public string Name => "Дракон"; - public string Description => "Дракон Хартера-Хейтуэя"; - - public void Perform() - { - var dragonSettings = CreateRandomSettings(); - // редактируем настройки: - SettingsForm.For(dragonSettings).ShowDialog(); - // создаём painter с такими настройками - var container = new StandardKernel(); - container.Bind().ToConstant(imageHolder); - container.Bind().ToConstant(dragonSettings); - container.Get().Paint(); - } - - private static DragonSettings CreateRandomSettings() - { - return new DragonSettingsGenerator(new Random()).Generate(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/App/Actions/ImageSettingsAction.cs b/FractalPainter/Solved/Step01/App/Actions/ImageSettingsAction.cs deleted file mode 100644 index bc6690657..000000000 --- a/FractalPainter/Solved/Step01/App/Actions/ImageSettingsAction.cs +++ /dev/null @@ -1,33 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step01.Infrastructure.Injection; -using FractalPainting.Solved.Step01.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step01.App.Actions -{ - public class ImageSettingsAction : IUiAction, INeed, INeed - { - private IImageHolder imageHolder; - private IImageSettingsProvider imageSettingsProvider; - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public void SetDependency(IImageSettingsProvider dependency) - { - imageSettingsProvider = dependency; - } - - public string Category => "Настройки"; - public string Name => "Изображение..."; - public string Description => "Размеры изображения"; - - public void Perform() - { - var imageSettings = imageSettingsProvider.ImageSettings; - SettingsForm.For(imageSettings).ShowDialog(); - imageHolder.RecreateImage(imageSettings); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/App/Actions/KochFractalAction.cs b/FractalPainter/Solved/Step01/App/Actions/KochFractalAction.cs deleted file mode 100644 index 897557894..000000000 --- a/FractalPainter/Solved/Step01/App/Actions/KochFractalAction.cs +++ /dev/null @@ -1,37 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step01.App.Fractals; -using FractalPainting.Solved.Step01.Infrastructure.Injection; -using FractalPainting.Solved.Step01.Infrastructure.UiActions; -using Ninject; - -namespace FractalPainting.Solved.Step01.App.Actions -{ - public class KochFractalAction : IUiAction, INeed, INeed - { - private IImageHolder imageHolder; - private Palette palette; - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public void SetDependency(Palette dependency) - { - palette = dependency; - } - - public string Category => "Фракталы"; - public string Name => "Кривая Коха"; - public string Description => "Кривая Коха"; - - public void Perform() - { - var container = new StandardKernel(); - container.Bind().ToConstant(imageHolder); - container.Bind().ToConstant(palette); - - container.Get().Paint(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/App/Actions/PaletteSettingsAction.cs b/FractalPainter/Solved/Step01/App/Actions/PaletteSettingsAction.cs deleted file mode 100644 index ed7f54255..000000000 --- a/FractalPainter/Solved/Step01/App/Actions/PaletteSettingsAction.cs +++ /dev/null @@ -1,25 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step01.Infrastructure.Injection; -using FractalPainting.Solved.Step01.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step01.App.Actions -{ - public class PaletteSettingsAction : IUiAction, INeed - { - private Palette palette; - - public void SetDependency(Palette dependency) - { - palette = dependency; - } - - public string Category => "Настройки"; - public string Name => "Палитра..."; - public string Description => "Цвета для рисования фракталов"; - - public void Perform() - { - SettingsForm.For(palette).ShowDialog(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/App/Actions/SaveImageAction.cs b/FractalPainter/Solved/Step01/App/Actions/SaveImageAction.cs deleted file mode 100644 index 0462ec96f..000000000 --- a/FractalPainter/Solved/Step01/App/Actions/SaveImageAction.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.IO; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step01.Infrastructure.Injection; -using FractalPainting.Solved.Step01.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step01.App.Actions -{ - public class SaveImageAction : IUiAction, INeed, INeed - { - private IImageDirectoryProvider imageDirectoryProvider; - private IImageHolder imageHolder; - - public void SetDependency(IImageDirectoryProvider dependency) - { - imageDirectoryProvider = dependency; - } - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public string Category => "Файл"; - public string Name => "Сохранить..."; - public string Description => "Сохранить изображение в файл"; - - public void Perform() - { - var dialog = new SaveFileDialog - { - CheckFileExists = false, - InitialDirectory = Path.GetFullPath(imageDirectoryProvider.ImagesDirectory), - DefaultExt = "bmp", - FileName = "image.bmp", - Filter = "Изображения (*.bmp)|*.bmp" - }; - var res = dialog.ShowDialog(); - if (res == DialogResult.OK) - imageHolder.SaveImage(dialog.FileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/App/AppSettings.cs b/FractalPainter/Solved/Step01/App/AppSettings.cs deleted file mode 100644 index eb9aa0b3a..000000000 --- a/FractalPainter/Solved/Step01/App/AppSettings.cs +++ /dev/null @@ -1,10 +0,0 @@ -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step01.App -{ - public class AppSettings : IImageDirectoryProvider, IImageSettingsProvider - { - public string ImagesDirectory { get; set; } - public ImageSettings ImageSettings { get; set; } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/App/Fractals/DragonPainter.cs b/FractalPainter/Solved/Step01/App/Fractals/DragonPainter.cs deleted file mode 100644 index 3e826bce8..000000000 --- a/FractalPainter/Solved/Step01/App/Fractals/DragonPainter.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Drawing; -using System.Linq; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step01.App.Fractals -{ - public class DragonPainter - { - private readonly IImageHolder imageHolder; - private readonly DragonSettings settings; - private readonly float size; - private Size imageSize; - - public DragonPainter(IImageHolder imageHolder, DragonSettings settings) - { - this.imageHolder = imageHolder; - this.settings = settings; - imageSize = imageHolder.GetImageSize(); - size = Math.Min(imageSize.Width, imageSize.Height)/2.1f; - } - - public void Paint() - { - using (var graphics = imageHolder.StartDrawing()) - { - graphics.FillRectangle(Brushes.Black, 0, 0, imageSize.Width, imageSize.Height); - var r = new Random(); - var cosa = (float) Math.Cos(settings.Angle1); - var sina = (float) Math.Sin(settings.Angle1); - var cosb = (float) Math.Cos(settings.Angle2); - var sinb = (float) Math.Sin(settings.Angle2); - var shiftX = settings.ShiftX*size*0.8f; - var shiftY = settings.ShiftY*size*0.8f; - var scale = settings.Scale; - var p = new PointF(0, 0); - foreach (var i in Enumerable.Range(0, settings.IterationsCount)) - { - graphics.FillRectangle(Brushes.Yellow, imageSize.Width/3f + p.X, imageSize.Height/2f + p.Y, 1, 1); - if (r.Next(0, 2) == 0) - p = new PointF(scale*(p.X*cosa - p.Y*sina), scale*(p.X*sina + p.Y*cosa)); - else - p = new PointF(scale*(p.X*cosb - p.Y*sinb) + shiftX, scale*(p.X*sinb + p.Y*cosb) + shiftY); - if (i%100 == 0) imageHolder.UpdateUi(); - } - } - imageHolder.UpdateUi(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/App/Fractals/DragonSettings.cs b/FractalPainter/Solved/Step01/App/Fractals/DragonSettings.cs deleted file mode 100644 index 67a538ed0..000000000 --- a/FractalPainter/Solved/Step01/App/Fractals/DragonSettings.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step01.App.Fractals -{ - public class DragonSettings - { - public double Angle1 { get; set; } = Math.PI / 4; - public double Angle2 { get; set; } = 3 * Math.PI / 4; - public float ShiftX { get; set; } = 1; - public float ShiftY { get; set; } = 0; - public float Scale { get; set; } = (float)(1 / Math.Sqrt(2)); - public int IterationsCount { get; set; } = 20000; - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/App/Fractals/DragonSettingsGenerator.cs b/FractalPainter/Solved/Step01/App/Fractals/DragonSettingsGenerator.cs deleted file mode 100644 index c0ff2437f..000000000 --- a/FractalPainter/Solved/Step01/App/Fractals/DragonSettingsGenerator.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step01.App.Fractals -{ - public class DragonSettingsGenerator - { - private readonly Random random; - - public DragonSettingsGenerator(Random random) - { - this.random = random; - } - - public DragonSettings Generate() - { - return new DragonSettings - { - Angle1 = random.NextDouble() / 2 + 0.5, - Angle2 = 3 * (random.NextDouble() / 2 + 0.5), - ShiftX = 1, - ShiftY = 0, - Scale = (float)(1/Math.Sqrt(2) + (random.NextDouble() - 0.5)/10) - }; - } - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/App/Fractals/KochPainter.cs b/FractalPainter/Solved/Step01/App/Fractals/KochPainter.cs deleted file mode 100644 index 3da908d99..000000000 --- a/FractalPainter/Solved/Step01/App/Fractals/KochPainter.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.Drawing; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step01.App.Fractals -{ - public class KochPainter - { - private readonly IImageHolder imageHolder; - private readonly Palette palette; - private Size imageSize; - - public KochPainter(IImageHolder imageHolder, Palette palette) - { - this.imageHolder = imageHolder; - this.palette = palette; - imageSize = imageHolder.GetImageSize(); - } - - public void Paint() - { - using (var graphics = imageHolder.StartDrawing()) - using (var backgroundBrush = new SolidBrush(palette.BackgroundColor)) - { - graphics.FillRectangle(backgroundBrush, 0, 0, imageSize.Width, imageSize.Height); - DrawSegment(graphics, 0, imageSize.Height*0.9f, imageSize.Width, imageSize.Height*0.9f, true); - } - imageHolder.UpdateUi(); - } - - private void DrawSegment(Graphics graphics, float x0, float y0, float x1, float y1, bool primaryColor) - { - var len2 = (x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1); - if (len2 < 4) - { - if (y0 < 0 || y1 < 0) return; - using (var penBrush = new SolidBrush(primaryColor ? palette.PrimaryColor : palette.SecondaryColor)) - { - var pen = new Pen(penBrush, 3); - graphics.DrawLine(pen, x0, y0, x1, y1); - } - } - else - { - var vx = (x1 - x0)/3; - var vy = (y1 - y0)/3; - DrawSegment(graphics, x0, y0, x0 + vx, y0 + vy, primaryColor); - var k = (float) Math.Sqrt(3)/2f; - var px = (x0 + x1)/2 + vy*k; - var py = (y0 + y1)/2 - vx*k; - DrawSegment(graphics, x0 + vx, y0 + vy, px, py, !primaryColor); - DrawSegment(graphics, px, py, x0 + 2*vx, y0 + 2*vy, !primaryColor); - DrawSegment(graphics, x0 + 2*vx, y0 + 2*vy, x1, y1, primaryColor); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/App/MainForm.cs b/FractalPainter/Solved/Step01/App/MainForm.cs deleted file mode 100644 index a30fd0de8..000000000 --- a/FractalPainter/Solved/Step01/App/MainForm.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.Drawing; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step01.Infrastructure.Injection; -using FractalPainting.Solved.Step01.Infrastructure.UiActions; -using Ninject; - -namespace FractalPainting.Solved.Step01.App -{ - public class MainForm : Form - { - public MainForm(IUiAction[] actions) - { - var imageSettings = CreateSettingsManager().Load().ImageSettings; - ClientSize = new Size(imageSettings.Width, imageSettings.Height); - - var mainMenu = new MenuStrip(); - mainMenu.Items.AddRange(actions.ToMenuItems()); - Controls.Add(mainMenu); - - var pictureBox = new PictureBoxImageHolder(); - pictureBox.RecreateImage(imageSettings); - pictureBox.Dock = DockStyle.Fill; - Controls.Add(pictureBox); - - DependencyInjector.Inject(actions, pictureBox); - DependencyInjector.Inject(actions, CreateSettingsManager().Load()); - DependencyInjector.Inject(actions, CreateSettingsManager().Load()); - DependencyInjector.Inject(actions, new Palette()); - } - - private static SettingsManager CreateSettingsManager() - { - var container = new StandardKernel(); - container.Bind().To(); - container.Bind().To(); - return container.Get(); - } - - protected override void OnShown(EventArgs e) - { - base.OnShown(e); - Text = "Fractal Painter"; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/App/MainForm.resx b/FractalPainter/Solved/Step01/App/MainForm.resx deleted file mode 100644 index 9d845151a..000000000 --- a/FractalPainter/Solved/Step01/App/MainForm.resx +++ /dev/null @@ -1,123 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 17, 17 - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/App/PictureBoxImageHolder.cs b/FractalPainter/Solved/Step01/App/PictureBoxImageHolder.cs deleted file mode 100644 index 2053ac5eb..000000000 --- a/FractalPainter/Solved/Step01/App/PictureBoxImageHolder.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Drawing; -using System.Drawing.Imaging; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step01.App -{ - public class PictureBoxImageHolder : PictureBox, IImageHolder - { - public Size GetImageSize() - { - FailIfNotInitialized(); - return Image.Size; - } - - public Graphics StartDrawing() - { - FailIfNotInitialized(); - return Graphics.FromImage(Image); - } - - private void FailIfNotInitialized() - { - if (Image == null) - throw new InvalidOperationException("Call PictureBoxImageHolder.RecreateImage before other method call!"); - } - - public void UpdateUi() - { - Refresh(); - Application.DoEvents(); - } - - public void RecreateImage(ImageSettings imageSettings) - { - Image = new Bitmap(imageSettings.Width, imageSettings.Height, PixelFormat.Format24bppRgb); - } - - public void SaveImage(string fileName) - { - FailIfNotInitialized(); - Image.Save(fileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/App/Program.cs b/FractalPainter/Solved/Step01/App/Program.cs deleted file mode 100644 index 6cbccda1d..000000000 --- a/FractalPainter/Solved/Step01/App/Program.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Solved.Step01.App.Actions; -using FractalPainting.Solved.Step01.Infrastructure.UiActions; -using Ninject; - -namespace FractalPainting.Solved.Step01.App -{ - internal static class Program - { - /// - /// The main entry point for the application. - /// - [STAThread] - private static void Main() - { - try - { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - - var container = new StandardKernel(); - - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - - var mainForm = container.Get(); - Application.Run(mainForm); - } - catch (Exception e) - { - MessageBox.Show(e.Message); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/App/SettingsManager.cs b/FractalPainter/Solved/Step01/App/SettingsManager.cs deleted file mode 100644 index 05d4b101a..000000000 --- a/FractalPainter/Solved/Step01/App/SettingsManager.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step01.App -{ - public class SettingsManager - { - private readonly IObjectSerializer serializer; - private readonly IBlobStorage storage; - private string settingsFilename; - - public SettingsManager(IObjectSerializer serializer, IBlobStorage storage) - { - this.serializer = serializer; - this.storage = storage; - } - - public AppSettings Load() - { - try - { - settingsFilename = "app.settings"; - var data = storage.Get(settingsFilename); - if (data == null) - { - var defaultSettings = CreateDefaultSettings(); - Save(defaultSettings); - return defaultSettings; - } - return serializer.Deserialize(data); - } - catch (Exception e) - { - MessageBox.Show(e.Message, "Не удалось загрузить настройки"); - return CreateDefaultSettings(); - } - } - - private static AppSettings CreateDefaultSettings() - { - return new AppSettings - { - ImagesDirectory = ".", - ImageSettings = new ImageSettings() - }; - } - - public void Save(AppSettings settings) - { - storage.Set(settingsFilename, serializer.Serialize(settings)); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/Infrastructure/Injection/DependencyInjector.cs b/FractalPainter/Solved/Step01/Infrastructure/Injection/DependencyInjector.cs deleted file mode 100644 index 8c8e67f7f..000000000 --- a/FractalPainter/Solved/Step01/Infrastructure/Injection/DependencyInjector.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Collections; - -namespace FractalPainting.Solved.Step01.Infrastructure.Injection -{ - public class DependencyInjector - { - public static void Inject(object service, TDependency dependency) - { - var need = service as INeed; - need?.SetDependency(dependency); - } - - public static void Inject(IEnumerable services, TDependency dependency) - { - foreach (var service in services) - Inject(service, dependency); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/Infrastructure/Injection/INeed.cs b/FractalPainter/Solved/Step01/Infrastructure/Injection/INeed.cs deleted file mode 100644 index 49b128ad4..000000000 --- a/FractalPainter/Solved/Step01/Infrastructure/Injection/INeed.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace FractalPainting.Solved.Step01.Infrastructure.Injection -{ - public interface INeed - { - void SetDependency(T dependency); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/Infrastructure/UiActions/IUiAction.cs b/FractalPainter/Solved/Step01/Infrastructure/UiActions/IUiAction.cs deleted file mode 100644 index 75f7d3b49..000000000 --- a/FractalPainter/Solved/Step01/Infrastructure/UiActions/IUiAction.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace FractalPainting.Solved.Step01.Infrastructure.UiActions -{ - public interface IUiAction - { - string Category { get; } - string Name { get; } - string Description { get; } - void Perform(); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/Infrastructure/UiActions/UiActionExtensions.cs b/FractalPainter/Solved/Step01/Infrastructure/UiActions/UiActionExtensions.cs deleted file mode 100644 index c3e42bf45..000000000 --- a/FractalPainter/Solved/Step01/Infrastructure/UiActions/UiActionExtensions.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Windows.Forms; - -namespace FractalPainting.Solved.Step01.Infrastructure.UiActions -{ - public static class UiActionExtensions - { - public static ToolStripItem[] ToMenuItems(this IUiAction[] actions) - { - var items = actions.GroupBy(a => a.Category) - .Select(g => CreateTopLevelMenuItem(g.Key, g.ToList())) - .Cast() - .ToArray(); - return items; - } - - private static ToolStripMenuItem CreateTopLevelMenuItem(string name, IList items) - { - var menuItems = items.Select(a => a.ToMenuItem()).ToArray(); - return new ToolStripMenuItem(name, null, menuItems); - } - - public static ToolStripItem ToMenuItem(this IUiAction action) - { - return - new ToolStripMenuItem(action.Name, null, (sender, args) => action.Perform()) - { - ToolTipText = action.Description, - Tag = action - }; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step01/Step01.csproj b/FractalPainter/Solved/Step01/Step01.csproj deleted file mode 100644 index 4866cf797..000000000 --- a/FractalPainter/Solved/Step01/Step01.csproj +++ /dev/null @@ -1,21 +0,0 @@ - - - - WinExe - netcoreapp3.1 - 8 - true - false - FractalPainting.Solved.Step01 - FractalPainter - - - - - - - - - - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/App/Actions/DragonFractalAction.cs b/FractalPainter/Solved/Step03/App/Actions/DragonFractalAction.cs deleted file mode 100644 index c2fa2db10..000000000 --- a/FractalPainter/Solved/Step03/App/Actions/DragonFractalAction.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step03.App.Fractals; -using FractalPainting.Solved.Step03.Infrastructure.Injection; -using FractalPainting.Solved.Step03.Infrastructure.UiActions; -using Ninject; - -namespace FractalPainting.Solved.Step03.App.Actions -{ - public class DragonFractalAction : IUiAction, INeed - { - private IImageHolder imageHolder; - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public string Category => "Фракталы"; - public string Name => "Дракон"; - public string Description => "Дракон Хартера-Хейтуэя"; - - public void Perform() - { - var dragonSettings = CreateRandomSettings(); - // редактируем настройки: - SettingsForm.For(dragonSettings).ShowDialog(); - // создаём painter с такими настройками - var container = new StandardKernel(); - container.Bind().ToConstant(imageHolder); - container.Bind().ToConstant(dragonSettings); - container.Get().Paint(); - } - - private static DragonSettings CreateRandomSettings() - { - return new DragonSettingsGenerator(new Random()).Generate(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/App/Actions/ImageSettingsAction.cs b/FractalPainter/Solved/Step03/App/Actions/ImageSettingsAction.cs deleted file mode 100644 index a4a095269..000000000 --- a/FractalPainter/Solved/Step03/App/Actions/ImageSettingsAction.cs +++ /dev/null @@ -1,33 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step03.Infrastructure.Injection; -using FractalPainting.Solved.Step03.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step03.App.Actions -{ - public class ImageSettingsAction : IUiAction, INeed, INeed - { - private IImageHolder imageHolder; - private IImageSettingsProvider imageSettingsProvider; - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public void SetDependency(IImageSettingsProvider dependency) - { - imageSettingsProvider = dependency; - } - - public string Category => "Настройки"; - public string Name => "Изображение..."; - public string Description => "Размеры изображения"; - - public void Perform() - { - var imageSettings = imageSettingsProvider.ImageSettings; - SettingsForm.For(imageSettings).ShowDialog(); - imageHolder.RecreateImage(imageSettings); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/App/Actions/KochFractalAction.cs b/FractalPainter/Solved/Step03/App/Actions/KochFractalAction.cs deleted file mode 100644 index 8c16376d1..000000000 --- a/FractalPainter/Solved/Step03/App/Actions/KochFractalAction.cs +++ /dev/null @@ -1,32 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step03.App.Fractals; -using FractalPainting.Solved.Step03.Infrastructure.UiActions; -using Ninject; - -namespace FractalPainting.Solved.Step03.App.Actions -{ - public class KochFractalAction : IUiAction - { - private readonly IImageHolder imageHolder; - private readonly Palette palette; - - public KochFractalAction(IImageHolder imageHolder, Palette palette) - { - this.imageHolder = imageHolder; - this.palette = palette; - } - - public string Category => "Фракталы"; - public string Name => "Кривая Коха"; - public string Description => "Кривая Коха"; - - public void Perform() - { - var container = new StandardKernel(); - container.Bind().ToConstant(imageHolder); - container.Bind().ToConstant(palette); - - container.Get().Paint(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/App/Actions/PaletteSettingsAction.cs b/FractalPainter/Solved/Step03/App/Actions/PaletteSettingsAction.cs deleted file mode 100644 index 452cfbc80..000000000 --- a/FractalPainter/Solved/Step03/App/Actions/PaletteSettingsAction.cs +++ /dev/null @@ -1,25 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step03.Infrastructure.Injection; -using FractalPainting.Solved.Step03.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step03.App.Actions -{ - public class PaletteSettingsAction : IUiAction, INeed - { - private Palette palette; - - public void SetDependency(Palette dependency) - { - palette = dependency; - } - - public string Category => "Настройки"; - public string Name => "Палитра..."; - public string Description => "Цвета для рисования фракталов"; - - public void Perform() - { - SettingsForm.For(palette).ShowDialog(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/App/Actions/SaveImageAction.cs b/FractalPainter/Solved/Step03/App/Actions/SaveImageAction.cs deleted file mode 100644 index 908cc494d..000000000 --- a/FractalPainter/Solved/Step03/App/Actions/SaveImageAction.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.IO; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step03.Infrastructure.Injection; -using FractalPainting.Solved.Step03.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step03.App.Actions -{ - public class SaveImageAction : IUiAction, INeed, INeed - { - private IImageDirectoryProvider imageDirectoryProvider; - private IImageHolder imageHolder; - - public void SetDependency(IImageDirectoryProvider dependency) - { - imageDirectoryProvider = dependency; - } - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public string Category => "Файл"; - public string Name => "Сохранить..."; - public string Description => "Сохранить изображение в файл"; - - public void Perform() - { - var dialog = new SaveFileDialog - { - CheckFileExists = false, - InitialDirectory = Path.GetFullPath(imageDirectoryProvider.ImagesDirectory), - DefaultExt = "bmp", - FileName = "image.bmp", - Filter = "Изображения (*.bmp)|*.bmp" - }; - var res = dialog.ShowDialog(); - if (res == DialogResult.OK) - imageHolder.SaveImage(dialog.FileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/App/AppSettings.cs b/FractalPainter/Solved/Step03/App/AppSettings.cs deleted file mode 100644 index 0d19d68ee..000000000 --- a/FractalPainter/Solved/Step03/App/AppSettings.cs +++ /dev/null @@ -1,10 +0,0 @@ -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step03.App -{ - public class AppSettings : IImageDirectoryProvider, IImageSettingsProvider - { - public string ImagesDirectory { get; set; } - public ImageSettings ImageSettings { get; set; } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/App/Fractals/DragonPainter.cs b/FractalPainter/Solved/Step03/App/Fractals/DragonPainter.cs deleted file mode 100644 index 69c51e3a3..000000000 --- a/FractalPainter/Solved/Step03/App/Fractals/DragonPainter.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Drawing; -using System.Linq; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step03.App.Fractals -{ - public class DragonPainter - { - private readonly IImageHolder imageHolder; - private readonly DragonSettings settings; - private readonly float size; - private Size imageSize; - - public DragonPainter(IImageHolder imageHolder, DragonSettings settings) - { - this.imageHolder = imageHolder; - this.settings = settings; - imageSize = imageHolder.GetImageSize(); - size = Math.Min(imageSize.Width, imageSize.Height)/2.1f; - } - - public void Paint() - { - using (var graphics = imageHolder.StartDrawing()) - { - graphics.FillRectangle(Brushes.Black, 0, 0, imageSize.Width, imageSize.Height); - var r = new Random(); - var cosa = (float) Math.Cos(settings.Angle1); - var sina = (float) Math.Sin(settings.Angle1); - var cosb = (float) Math.Cos(settings.Angle2); - var sinb = (float) Math.Sin(settings.Angle2); - var shiftX = settings.ShiftX*size*0.8f; - var shiftY = settings.ShiftY*size*0.8f; - var scale = settings.Scale; - var p = new PointF(0, 0); - foreach (var i in Enumerable.Range(0, settings.IterationsCount)) - { - graphics.FillRectangle(Brushes.Yellow, imageSize.Width/3f + p.X, imageSize.Height/2f + p.Y, 1, 1); - if (r.Next(0, 2) == 0) - p = new PointF(scale*(p.X*cosa - p.Y*sina), scale*(p.X*sina + p.Y*cosa)); - else - p = new PointF(scale*(p.X*cosb - p.Y*sinb) + shiftX, scale*(p.X*sinb + p.Y*cosb) + shiftY); - if (i%100 == 0) imageHolder.UpdateUi(); - } - } - imageHolder.UpdateUi(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/App/Fractals/DragonSettings.cs b/FractalPainter/Solved/Step03/App/Fractals/DragonSettings.cs deleted file mode 100644 index 86e6231bc..000000000 --- a/FractalPainter/Solved/Step03/App/Fractals/DragonSettings.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step03.App.Fractals -{ - public class DragonSettings - { - public double Angle1 { get; set; } = Math.PI / 4; - public double Angle2 { get; set; } = 3 * Math.PI / 4; - public float ShiftX { get; set; } = 1; - public float ShiftY { get; set; } = 0; - public float Scale { get; set; } = (float)(1 / Math.Sqrt(2)); - public int IterationsCount { get; set; } = 20000; - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/App/Fractals/DragonSettingsGenerator.cs b/FractalPainter/Solved/Step03/App/Fractals/DragonSettingsGenerator.cs deleted file mode 100644 index ddaec1fc5..000000000 --- a/FractalPainter/Solved/Step03/App/Fractals/DragonSettingsGenerator.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step03.App.Fractals -{ - public class DragonSettingsGenerator - { - private readonly Random random; - - public DragonSettingsGenerator(Random random) - { - this.random = random; - } - - public DragonSettings Generate() - { - return new DragonSettings - { - Angle1 = random.NextDouble() / 2 + 0.5, - Angle2 = 3 * (random.NextDouble() / 2 + 0.5), - ShiftX = 1, - ShiftY = 0, - Scale = (float)(1/Math.Sqrt(2) + (random.NextDouble() - 0.5)/10) - }; - } - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/App/Fractals/KochPainter.cs b/FractalPainter/Solved/Step03/App/Fractals/KochPainter.cs deleted file mode 100644 index 96b07c108..000000000 --- a/FractalPainter/Solved/Step03/App/Fractals/KochPainter.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.Drawing; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step03.App.Fractals -{ - public class KochPainter - { - private readonly IImageHolder imageHolder; - private readonly Palette palette; - private Size imageSize; - - public KochPainter(IImageHolder imageHolder, Palette palette) - { - this.imageHolder = imageHolder; - this.palette = palette; - imageSize = imageHolder.GetImageSize(); - } - - public void Paint() - { - using (var graphics = imageHolder.StartDrawing()) - using (var backgroundBrush = new SolidBrush(palette.BackgroundColor)) - { - graphics.FillRectangle(backgroundBrush, 0, 0, imageSize.Width, imageSize.Height); - DrawSegment(graphics, 0, imageSize.Height*0.9f, imageSize.Width, imageSize.Height*0.9f, true); - } - imageHolder.UpdateUi(); - } - - private void DrawSegment(Graphics graphics, float x0, float y0, float x1, float y1, bool primaryColor) - { - var len2 = (x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1); - if (len2 < 4) - { - if (y0 < 0 || y1 < 0) return; - using (var penBrush = new SolidBrush(primaryColor ? palette.PrimaryColor : palette.SecondaryColor)) - { - var pen = new Pen(penBrush, 3); - graphics.DrawLine(pen, x0, y0, x1, y1); - } - } - else - { - var vx = (x1 - x0)/3; - var vy = (y1 - y0)/3; - DrawSegment(graphics, x0, y0, x0 + vx, y0 + vy, primaryColor); - var k = (float) Math.Sqrt(3)/2f; - var px = (x0 + x1)/2 + vy*k; - var py = (y0 + y1)/2 - vx*k; - DrawSegment(graphics, x0 + vx, y0 + vy, px, py, !primaryColor); - DrawSegment(graphics, px, py, x0 + 2*vx, y0 + 2*vy, !primaryColor); - DrawSegment(graphics, x0 + 2*vx, y0 + 2*vy, x1, y1, primaryColor); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/App/MainForm.cs b/FractalPainter/Solved/Step03/App/MainForm.cs deleted file mode 100644 index 717318415..000000000 --- a/FractalPainter/Solved/Step03/App/MainForm.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System; -using System.Drawing; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step03.Infrastructure.Injection; -using FractalPainting.Solved.Step03.Infrastructure.UiActions; -using Ninject; - -namespace FractalPainting.Solved.Step03.App -{ - public class MainForm : Form - { - public MainForm(IUiAction[] actions, - PictureBoxImageHolder pictureBox, - Palette palette) - { - var imageSettings = CreateSettingsManager().Load().ImageSettings; - ClientSize = new Size(imageSettings.Width, imageSettings.Height); - - var mainMenu = new MenuStrip(); - mainMenu.Items.AddRange(actions.ToMenuItems()); - Controls.Add(mainMenu); - - pictureBox.RecreateImage(imageSettings); - pictureBox.Dock = DockStyle.Fill; - Controls.Add(pictureBox); - - DependencyInjector.Inject(actions, pictureBox); - DependencyInjector.Inject(actions, CreateSettingsManager().Load()); - DependencyInjector.Inject(actions, CreateSettingsManager().Load()); - DependencyInjector.Inject(actions, palette); - } - - private static SettingsManager CreateSettingsManager() - { - var container = new StandardKernel(); - container.Bind().To(); - container.Bind().To(); - return container.Get(); - } - - protected override void OnShown(EventArgs e) - { - base.OnShown(e); - Text = "Fractal Painter"; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/App/MainForm.resx b/FractalPainter/Solved/Step03/App/MainForm.resx deleted file mode 100644 index 9d845151a..000000000 --- a/FractalPainter/Solved/Step03/App/MainForm.resx +++ /dev/null @@ -1,123 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 17, 17 - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/App/PictureBoxImageHolder.cs b/FractalPainter/Solved/Step03/App/PictureBoxImageHolder.cs deleted file mode 100644 index a2cb38e22..000000000 --- a/FractalPainter/Solved/Step03/App/PictureBoxImageHolder.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Drawing; -using System.Drawing.Imaging; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step03.App -{ - public class PictureBoxImageHolder : PictureBox, IImageHolder - { - public Size GetImageSize() - { - FailIfNotInitialized(); - return Image.Size; - } - - public Graphics StartDrawing() - { - FailIfNotInitialized(); - return Graphics.FromImage(Image); - } - - private void FailIfNotInitialized() - { - if (Image == null) - throw new InvalidOperationException("Call PictureBoxImageHolder.RecreateImage before other method call!"); - } - - public void UpdateUi() - { - Refresh(); - Application.DoEvents(); - } - - public void RecreateImage(ImageSettings imageSettings) - { - Image = new Bitmap(imageSettings.Width, imageSettings.Height, PixelFormat.Format24bppRgb); - } - - public void SaveImage(string fileName) - { - FailIfNotInitialized(); - Image.Save(fileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/App/Program.cs b/FractalPainter/Solved/Step03/App/Program.cs deleted file mode 100644 index 42ef06d9b..000000000 --- a/FractalPainter/Solved/Step03/App/Program.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step03.App.Actions; -using FractalPainting.Solved.Step03.Infrastructure.UiActions; -using Ninject; - -namespace FractalPainting.Solved.Step03.App -{ - internal static class Program - { - /// - /// The main entry point for the application. - /// - [STAThread] - private static void Main() - { - try - { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - - var container = new StandardKernel(); - - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - - container.Bind().ToSelf() - .InSingletonScope(); - container.Bind() - .To() - .InSingletonScope(); - - var mainForm = container.Get(); - Application.Run(mainForm); - } - catch (Exception e) - { - MessageBox.Show(e.Message); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/App/SettingsManager.cs b/FractalPainter/Solved/Step03/App/SettingsManager.cs deleted file mode 100644 index 05c230c5a..000000000 --- a/FractalPainter/Solved/Step03/App/SettingsManager.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step03.App -{ - public class SettingsManager - { - private readonly IObjectSerializer serializer; - private readonly IBlobStorage storage; - private string settingsFilename; - - public SettingsManager(IObjectSerializer serializer, IBlobStorage storage) - { - this.serializer = serializer; - this.storage = storage; - } - - public AppSettings Load() - { - try - { - settingsFilename = "app.settings"; - var data = storage.Get(settingsFilename); - if (data == null) - { - var defaultSettings = CreateDefaultSettings(); - Save(defaultSettings); - return defaultSettings; - } - return serializer.Deserialize(data); - } - catch (Exception e) - { - MessageBox.Show(e.Message, "Не удалось загрузить настройки"); - return CreateDefaultSettings(); - } - } - - private static AppSettings CreateDefaultSettings() - { - return new AppSettings - { - ImagesDirectory = ".", - ImageSettings = new ImageSettings() - }; - } - - public void Save(AppSettings settings) - { - storage.Set(settingsFilename, serializer.Serialize(settings)); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/Infrastructure/Injection/DependencyInjector.cs b/FractalPainter/Solved/Step03/Infrastructure/Injection/DependencyInjector.cs deleted file mode 100644 index 4b1a9312f..000000000 --- a/FractalPainter/Solved/Step03/Infrastructure/Injection/DependencyInjector.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Collections; - -namespace FractalPainting.Solved.Step03.Infrastructure.Injection -{ - public class DependencyInjector - { - public static void Inject(object service, TDependency dependency) - { - var need = service as INeed; - need?.SetDependency(dependency); - } - - public static void Inject(IEnumerable services, TDependency dependency) - { - foreach (var service in services) - Inject(service, dependency); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/Infrastructure/Injection/INeed.cs b/FractalPainter/Solved/Step03/Infrastructure/Injection/INeed.cs deleted file mode 100644 index eedfda5c3..000000000 --- a/FractalPainter/Solved/Step03/Infrastructure/Injection/INeed.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace FractalPainting.Solved.Step03.Infrastructure.Injection -{ - public interface INeed - { - void SetDependency(T dependency); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/Infrastructure/UiActions/IUiAction.cs b/FractalPainter/Solved/Step03/Infrastructure/UiActions/IUiAction.cs deleted file mode 100644 index 2e6a805bf..000000000 --- a/FractalPainter/Solved/Step03/Infrastructure/UiActions/IUiAction.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace FractalPainting.Solved.Step03.Infrastructure.UiActions -{ - public interface IUiAction - { - string Category { get; } - string Name { get; } - string Description { get; } - void Perform(); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/Infrastructure/UiActions/UiActionExtensions.cs b/FractalPainter/Solved/Step03/Infrastructure/UiActions/UiActionExtensions.cs deleted file mode 100644 index c1226c864..000000000 --- a/FractalPainter/Solved/Step03/Infrastructure/UiActions/UiActionExtensions.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Windows.Forms; - -namespace FractalPainting.Solved.Step03.Infrastructure.UiActions -{ - public static class UiActionExtensions - { - public static ToolStripItem[] ToMenuItems(this IUiAction[] actions) - { - var items = actions.GroupBy(a => a.Category) - .Select(g => CreateTopLevelMenuItem(g.Key, g.ToList())) - .Cast() - .ToArray(); - return items; - } - - private static ToolStripMenuItem CreateTopLevelMenuItem(string name, IList items) - { - var menuItems = items.Select(a => a.ToMenuItem()).ToArray(); - return new ToolStripMenuItem(name, null, menuItems); - } - - public static ToolStripItem ToMenuItem(this IUiAction action) - { - return - new ToolStripMenuItem(action.Name, null, (sender, args) => action.Perform()) - { - ToolTipText = action.Description, - Tag = action - }; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step03/Step03.csproj b/FractalPainter/Solved/Step03/Step03.csproj deleted file mode 100644 index 2ecb6cdfe..000000000 --- a/FractalPainter/Solved/Step03/Step03.csproj +++ /dev/null @@ -1,20 +0,0 @@ - - - - WinExe - netcoreapp3.1 - true - false - FractalPainting.Solved.Step03 - FractalPainter - - - - - - - - - - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/App/Actions/DragonFractalAction.cs b/FractalPainter/Solved/Step04/App/Actions/DragonFractalAction.cs deleted file mode 100644 index 9500587c8..000000000 --- a/FractalPainter/Solved/Step04/App/Actions/DragonFractalAction.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step04.App.Fractals; -using FractalPainting.Solved.Step04.Infrastructure.Injection; -using FractalPainting.Solved.Step04.Infrastructure.UiActions; -using Ninject; - -namespace FractalPainting.Solved.Step04.App.Actions -{ - public class DragonFractalAction : IUiAction, INeed - { - private IImageHolder imageHolder; - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public string Category => "Фракталы"; - public string Name => "Дракон"; - public string Description => "Дракон Хартера-Хейтуэя"; - - public void Perform() - { - var dragonSettings = CreateRandomSettings(); - // редактируем настройки: - SettingsForm.For(dragonSettings).ShowDialog(); - // создаём painter с такими настройками - var container = new StandardKernel(); - container.Bind().ToConstant(imageHolder); - container.Bind().ToConstant(dragonSettings); - container.Get().Paint(); - } - - private static DragonSettings CreateRandomSettings() - { - return new DragonSettingsGenerator(new Random()).Generate(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/App/Actions/ImageSettingsAction.cs b/FractalPainter/Solved/Step04/App/Actions/ImageSettingsAction.cs deleted file mode 100644 index b65106a04..000000000 --- a/FractalPainter/Solved/Step04/App/Actions/ImageSettingsAction.cs +++ /dev/null @@ -1,33 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step04.Infrastructure.Injection; -using FractalPainting.Solved.Step04.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step04.App.Actions -{ - public class ImageSettingsAction : IUiAction, INeed, INeed - { - private IImageHolder imageHolder; - private IImageSettingsProvider imageSettingsProvider; - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public void SetDependency(IImageSettingsProvider dependency) - { - imageSettingsProvider = dependency; - } - - public string Category => "Настройки"; - public string Name => "Изображение..."; - public string Description => "Размеры изображения"; - - public void Perform() - { - var imageSettings = imageSettingsProvider.ImageSettings; - SettingsForm.For(imageSettings).ShowDialog(); - imageHolder.RecreateImage(imageSettings); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/App/Actions/KochFractalAction.cs b/FractalPainter/Solved/Step04/App/Actions/KochFractalAction.cs deleted file mode 100644 index 3ff168a70..000000000 --- a/FractalPainter/Solved/Step04/App/Actions/KochFractalAction.cs +++ /dev/null @@ -1,24 +0,0 @@ -using FractalPainting.Solved.Step04.App.Fractals; -using FractalPainting.Solved.Step04.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step04.App.Actions -{ - public class KochFractalAction : IUiAction - { - private readonly KochPainter kochPainter; - - public KochFractalAction(KochPainter kochPainter) - { - this.kochPainter = kochPainter; - } - - public string Category => "Фракталы"; - public string Name => "Кривая Коха"; - public string Description => "Кривая Коха"; - - public void Perform() - { - kochPainter.Paint(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/App/Actions/PaletteSettingsAction.cs b/FractalPainter/Solved/Step04/App/Actions/PaletteSettingsAction.cs deleted file mode 100644 index 85bb762ab..000000000 --- a/FractalPainter/Solved/Step04/App/Actions/PaletteSettingsAction.cs +++ /dev/null @@ -1,25 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step04.Infrastructure.Injection; -using FractalPainting.Solved.Step04.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step04.App.Actions -{ - public class PaletteSettingsAction : IUiAction, INeed - { - private Palette palette; - - public void SetDependency(Palette dependency) - { - palette = dependency; - } - - public string Category => "Настройки"; - public string Name => "Палитра..."; - public string Description => "Цвета для рисования фракталов"; - - public void Perform() - { - SettingsForm.For(palette).ShowDialog(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/App/Actions/SaveImageAction.cs b/FractalPainter/Solved/Step04/App/Actions/SaveImageAction.cs deleted file mode 100644 index a45f1e0ee..000000000 --- a/FractalPainter/Solved/Step04/App/Actions/SaveImageAction.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.IO; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step04.Infrastructure.Injection; -using FractalPainting.Solved.Step04.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step04.App.Actions -{ - public class SaveImageAction : IUiAction, INeed, INeed - { - private IImageDirectoryProvider imageDirectoryProvider; - private IImageHolder imageHolder; - - public void SetDependency(IImageDirectoryProvider dependency) - { - imageDirectoryProvider = dependency; - } - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public string Category => "Файл"; - public string Name => "Сохранить..."; - public string Description => "Сохранить изображение в файл"; - - public void Perform() - { - var dialog = new SaveFileDialog - { - CheckFileExists = false, - InitialDirectory = Path.GetFullPath(imageDirectoryProvider.ImagesDirectory), - DefaultExt = "bmp", - FileName = "image.bmp", - Filter = "Изображения (*.bmp)|*.bmp" - }; - var res = dialog.ShowDialog(); - if (res == DialogResult.OK) - imageHolder.SaveImage(dialog.FileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/App/AppSettings.cs b/FractalPainter/Solved/Step04/App/AppSettings.cs deleted file mode 100644 index cc0371a46..000000000 --- a/FractalPainter/Solved/Step04/App/AppSettings.cs +++ /dev/null @@ -1,10 +0,0 @@ -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step04.App -{ - public class AppSettings : IImageDirectoryProvider, IImageSettingsProvider - { - public string ImagesDirectory { get; set; } - public ImageSettings ImageSettings { get; set; } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/App/Fractals/DragonPainter.cs b/FractalPainter/Solved/Step04/App/Fractals/DragonPainter.cs deleted file mode 100644 index f5dd59704..000000000 --- a/FractalPainter/Solved/Step04/App/Fractals/DragonPainter.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Drawing; -using System.Linq; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step04.App.Fractals -{ - public class DragonPainter - { - private readonly IImageHolder imageHolder; - private readonly DragonSettings settings; - private readonly float size; - private Size imageSize; - - public DragonPainter(IImageHolder imageHolder, DragonSettings settings) - { - this.imageHolder = imageHolder; - this.settings = settings; - imageSize = imageHolder.GetImageSize(); - size = Math.Min(imageSize.Width, imageSize.Height)/2.1f; - } - - public void Paint() - { - using (var graphics = imageHolder.StartDrawing()) - { - graphics.FillRectangle(Brushes.Black, 0, 0, imageSize.Width, imageSize.Height); - var r = new Random(); - var cosa = (float) Math.Cos(settings.Angle1); - var sina = (float) Math.Sin(settings.Angle1); - var cosb = (float) Math.Cos(settings.Angle2); - var sinb = (float) Math.Sin(settings.Angle2); - var shiftX = settings.ShiftX*size*0.8f; - var shiftY = settings.ShiftY*size*0.8f; - var scale = settings.Scale; - var p = new PointF(0, 0); - foreach (var i in Enumerable.Range(0, settings.IterationsCount)) - { - graphics.FillRectangle(Brushes.Yellow, imageSize.Width/3f + p.X, imageSize.Height/2f + p.Y, 1, 1); - if (r.Next(0, 2) == 0) - p = new PointF(scale*(p.X*cosa - p.Y*sina), scale*(p.X*sina + p.Y*cosa)); - else - p = new PointF(scale*(p.X*cosb - p.Y*sinb) + shiftX, scale*(p.X*sinb + p.Y*cosb) + shiftY); - if (i%100 == 0) imageHolder.UpdateUi(); - } - } - imageHolder.UpdateUi(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/App/Fractals/DragonSettings.cs b/FractalPainter/Solved/Step04/App/Fractals/DragonSettings.cs deleted file mode 100644 index 9bd2043e2..000000000 --- a/FractalPainter/Solved/Step04/App/Fractals/DragonSettings.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step04.App.Fractals -{ - public class DragonSettings - { - public double Angle1 { get; set; } = Math.PI / 4; - public double Angle2 { get; set; } = 3 * Math.PI / 4; - public float ShiftX { get; set; } = 1; - public float ShiftY { get; set; } = 0; - public float Scale { get; set; } = (float)(1 / Math.Sqrt(2)); - public int IterationsCount { get; set; } = 20000; - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/App/Fractals/DragonSettingsGenerator.cs b/FractalPainter/Solved/Step04/App/Fractals/DragonSettingsGenerator.cs deleted file mode 100644 index 2a985a961..000000000 --- a/FractalPainter/Solved/Step04/App/Fractals/DragonSettingsGenerator.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step04.App.Fractals -{ - public class DragonSettingsGenerator - { - private readonly Random random; - - public DragonSettingsGenerator(Random random) - { - this.random = random; - } - - public DragonSettings Generate() - { - return new DragonSettings - { - Angle1 = random.NextDouble() / 2 + 0.5, - Angle2 = 3 * (random.NextDouble() / 2 + 0.5), - ShiftX = 1, - ShiftY = 0, - Scale = (float)(1/Math.Sqrt(2) + (random.NextDouble() - 0.5)/10) - }; - } - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/App/Fractals/KochPainter.cs b/FractalPainter/Solved/Step04/App/Fractals/KochPainter.cs deleted file mode 100644 index 468de7158..000000000 --- a/FractalPainter/Solved/Step04/App/Fractals/KochPainter.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Drawing; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step04.App.Fractals -{ - public class KochPainter - { - private readonly IImageHolder imageHolder; - private readonly Palette palette; - - public KochPainter(IImageHolder imageHolder, Palette palette) - { - this.imageHolder = imageHolder; - this.palette = palette; - } - - public void Paint() - { - var imageSize = imageHolder.GetImageSize(); - using (var graphics = imageHolder.StartDrawing()) - using (var backgroundBrush = new SolidBrush(palette.BackgroundColor)) - { - graphics.FillRectangle(backgroundBrush, 0, 0, imageSize.Width, imageSize.Height); - DrawSegment(graphics, 0, imageSize.Height*0.9f, imageSize.Width, imageSize.Height*0.9f, true); - } - imageHolder.UpdateUi(); - } - - private void DrawSegment(Graphics graphics, float x0, float y0, float x1, float y1, bool primaryColor) - { - var len2 = (x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1); - if (len2 < 4) - { - if (y0 < 0 || y1 < 0) return; - using (var penBrush = new SolidBrush(primaryColor ? palette.PrimaryColor : palette.SecondaryColor)) - { - var pen = new Pen(penBrush, 3); - graphics.DrawLine(pen, x0, y0, x1, y1); - } - } - else - { - var vx = (x1 - x0)/3; - var vy = (y1 - y0)/3; - DrawSegment(graphics, x0, y0, x0 + vx, y0 + vy, primaryColor); - var k = (float) Math.Sqrt(3)/2f; - var px = (x0 + x1)/2 + vy*k; - var py = (y0 + y1)/2 - vx*k; - DrawSegment(graphics, x0 + vx, y0 + vy, px, py, !primaryColor); - DrawSegment(graphics, px, py, x0 + 2*vx, y0 + 2*vy, !primaryColor); - DrawSegment(graphics, x0 + 2*vx, y0 + 2*vy, x1, y1, primaryColor); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/App/MainForm.cs b/FractalPainter/Solved/Step04/App/MainForm.cs deleted file mode 100644 index ce6a51f2b..000000000 --- a/FractalPainter/Solved/Step04/App/MainForm.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System; -using System.Drawing; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step04.Infrastructure.Injection; -using FractalPainting.Solved.Step04.Infrastructure.UiActions; -using Ninject; - -namespace FractalPainting.Solved.Step04.App -{ - public class MainForm : Form - { - public MainForm(IUiAction[] actions, - PictureBoxImageHolder pictureBox, - Palette palette) - { - var imageSettings = CreateSettingsManager().Load().ImageSettings; - ClientSize = new Size(imageSettings.Width, imageSettings.Height); - - var mainMenu = new MenuStrip(); - mainMenu.Items.AddRange(actions.ToMenuItems()); - Controls.Add(mainMenu); - - pictureBox.RecreateImage(imageSettings); - pictureBox.Dock = DockStyle.Fill; - Controls.Add(pictureBox); - - DependencyInjector.Inject(actions, pictureBox); - DependencyInjector.Inject(actions, CreateSettingsManager().Load()); - DependencyInjector.Inject(actions, CreateSettingsManager().Load()); - DependencyInjector.Inject(actions, palette); - } - - private static SettingsManager CreateSettingsManager() - { - var container = new StandardKernel(); - container.Bind().To(); - container.Bind().To(); - return container.Get(); - } - - protected override void OnShown(EventArgs e) - { - base.OnShown(e); - Text = "Fractal Painter"; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/App/MainForm.resx b/FractalPainter/Solved/Step04/App/MainForm.resx deleted file mode 100644 index 9d845151a..000000000 --- a/FractalPainter/Solved/Step04/App/MainForm.resx +++ /dev/null @@ -1,123 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 17, 17 - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/App/PictureBoxImageHolder.cs b/FractalPainter/Solved/Step04/App/PictureBoxImageHolder.cs deleted file mode 100644 index 296696f21..000000000 --- a/FractalPainter/Solved/Step04/App/PictureBoxImageHolder.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Drawing; -using System.Drawing.Imaging; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step04.App -{ - public class PictureBoxImageHolder : PictureBox, IImageHolder - { - public Size GetImageSize() - { - FailIfNotInitialized(); - return Image.Size; - } - - public Graphics StartDrawing() - { - FailIfNotInitialized(); - return Graphics.FromImage(Image); - } - - private void FailIfNotInitialized() - { - if (Image == null) - throw new InvalidOperationException("Call PictureBoxImageHolder.RecreateImage before other method call!"); - } - - public void UpdateUi() - { - Refresh(); - Application.DoEvents(); - } - - public void RecreateImage(ImageSettings imageSettings) - { - Image = new Bitmap(imageSettings.Width, imageSettings.Height, PixelFormat.Format24bppRgb); - } - - public void SaveImage(string fileName) - { - FailIfNotInitialized(); - Image.Save(fileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/App/Program.cs b/FractalPainter/Solved/Step04/App/Program.cs deleted file mode 100644 index b06132e3e..000000000 --- a/FractalPainter/Solved/Step04/App/Program.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step04.App.Actions; -using FractalPainting.Solved.Step04.Infrastructure.UiActions; -using Ninject; - -namespace FractalPainting.Solved.Step04.App -{ - internal static class Program - { - /// - /// The main entry point for the application. - /// - [STAThread] - private static void Main() - { - try - { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - - var container = new StandardKernel(); - - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - - container.Bind().ToSelf() - .InSingletonScope(); - container.Bind() - .To() - .InSingletonScope(); - - var mainForm = container.Get(); - Application.Run(mainForm); - } - catch (Exception e) - { - MessageBox.Show(e.Message); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/App/SettingsManager.cs b/FractalPainter/Solved/Step04/App/SettingsManager.cs deleted file mode 100644 index d2a8466b5..000000000 --- a/FractalPainter/Solved/Step04/App/SettingsManager.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step04.App -{ - public class SettingsManager - { - private readonly IObjectSerializer serializer; - private readonly IBlobStorage storage; - private string settingsFilename; - - public SettingsManager(IObjectSerializer serializer, IBlobStorage storage) - { - this.serializer = serializer; - this.storage = storage; - } - - public AppSettings Load() - { - try - { - settingsFilename = "app.settings"; - var data = storage.Get(settingsFilename); - if (data == null) - { - var defaultSettings = CreateDefaultSettings(); - Save(defaultSettings); - return defaultSettings; - } - return serializer.Deserialize(data); - } - catch (Exception e) - { - MessageBox.Show(e.Message, "Не удалось загрузить настройки"); - return CreateDefaultSettings(); - } - } - - private static AppSettings CreateDefaultSettings() - { - return new AppSettings - { - ImagesDirectory = ".", - ImageSettings = new ImageSettings() - }; - } - - public void Save(AppSettings settings) - { - storage.Set(settingsFilename, serializer.Serialize(settings)); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/Infrastructure/Injection/DependencyInjector.cs b/FractalPainter/Solved/Step04/Infrastructure/Injection/DependencyInjector.cs deleted file mode 100644 index 648a02758..000000000 --- a/FractalPainter/Solved/Step04/Infrastructure/Injection/DependencyInjector.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Collections; - -namespace FractalPainting.Solved.Step04.Infrastructure.Injection -{ - public class DependencyInjector - { - public static void Inject(object service, TDependency dependency) - { - var need = service as INeed; - need?.SetDependency(dependency); - } - - public static void Inject(IEnumerable services, TDependency dependency) - { - foreach (var service in services) - Inject(service, dependency); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/Infrastructure/Injection/INeed.cs b/FractalPainter/Solved/Step04/Infrastructure/Injection/INeed.cs deleted file mode 100644 index 08198ad0d..000000000 --- a/FractalPainter/Solved/Step04/Infrastructure/Injection/INeed.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace FractalPainting.Solved.Step04.Infrastructure.Injection -{ - public interface INeed - { - void SetDependency(T dependency); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/Infrastructure/UiActions/IUiAction.cs b/FractalPainter/Solved/Step04/Infrastructure/UiActions/IUiAction.cs deleted file mode 100644 index 717fc02ef..000000000 --- a/FractalPainter/Solved/Step04/Infrastructure/UiActions/IUiAction.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace FractalPainting.Solved.Step04.Infrastructure.UiActions -{ - public interface IUiAction - { - string Category { get; } - string Name { get; } - string Description { get; } - void Perform(); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/Infrastructure/UiActions/UiActionExtensions.cs b/FractalPainter/Solved/Step04/Infrastructure/UiActions/UiActionExtensions.cs deleted file mode 100644 index 5b9517d83..000000000 --- a/FractalPainter/Solved/Step04/Infrastructure/UiActions/UiActionExtensions.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Windows.Forms; - -namespace FractalPainting.Solved.Step04.Infrastructure.UiActions -{ - public static class UiActionExtensions - { - public static ToolStripItem[] ToMenuItems(this IUiAction[] actions) - { - var items = actions.GroupBy(a => a.Category) - .Select(g => CreateTopLevelMenuItem(g.Key, g.ToList())) - .Cast() - .ToArray(); - return items; - } - - private static ToolStripMenuItem CreateTopLevelMenuItem(string name, IList items) - { - var menuItems = items.Select(a => a.ToMenuItem()).ToArray(); - return new ToolStripMenuItem(name, null, menuItems); - } - - public static ToolStripItem ToMenuItem(this IUiAction action) - { - return - new ToolStripMenuItem(action.Name, null, (sender, args) => action.Perform()) - { - ToolTipText = action.Description, - Tag = action - }; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step04/Step04.csproj b/FractalPainter/Solved/Step04/Step04.csproj deleted file mode 100644 index ae813ceef..000000000 --- a/FractalPainter/Solved/Step04/Step04.csproj +++ /dev/null @@ -1,20 +0,0 @@ - - - - WinExe - netcoreapp3.1 - true - false - FractalPainting.Solved.Step04 - FractalPainter - - - - - - - - - - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/App/Actions/DragonFractalAction.cs b/FractalPainter/Solved/Step05/App/Actions/DragonFractalAction.cs deleted file mode 100644 index 32e083b34..000000000 --- a/FractalPainter/Solved/Step05/App/Actions/DragonFractalAction.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step05.App.Fractals; -using FractalPainting.Solved.Step05.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step05.App.Actions -{ - public class DragonFractalAction : IUiAction - { - private readonly IDragonPainterFactory dragonPainterFactory; - - public DragonFractalAction(IDragonPainterFactory dragonPainterFactory) - { - this.dragonPainterFactory = dragonPainterFactory; - } - - public string Category => "Фракталы"; - public string Name => "Дракон"; - public string Description => "Дракон Хартера-Хейтуэя"; - - public void Perform() - { - var dragonSettings = CreateRandomSettings(); - // редактируем настройки: - SettingsForm.For(dragonSettings).ShowDialog(); - // создаём painter с такими настройками - dragonPainterFactory.Create(dragonSettings).Paint(); - } - - private static DragonSettings CreateRandomSettings() - { - return new DragonSettingsGenerator(new Random()).Generate(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/App/Actions/ImageSettingsAction.cs b/FractalPainter/Solved/Step05/App/Actions/ImageSettingsAction.cs deleted file mode 100644 index 439aad5ac..000000000 --- a/FractalPainter/Solved/Step05/App/Actions/ImageSettingsAction.cs +++ /dev/null @@ -1,33 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step05.Infrastructure.Injection; -using FractalPainting.Solved.Step05.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step05.App.Actions -{ - public class ImageSettingsAction : IUiAction, INeed, INeed - { - private IImageHolder imageHolder; - private IImageSettingsProvider imageSettingsProvider; - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public void SetDependency(IImageSettingsProvider dependency) - { - imageSettingsProvider = dependency; - } - - public string Category => "Настройки"; - public string Name => "Изображение..."; - public string Description => "Размеры изображения"; - - public void Perform() - { - var imageSettings = imageSettingsProvider.ImageSettings; - SettingsForm.For(imageSettings).ShowDialog(); - imageHolder.RecreateImage(imageSettings); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/App/Actions/KochFractalAction.cs b/FractalPainter/Solved/Step05/App/Actions/KochFractalAction.cs deleted file mode 100644 index 18d59e5f3..000000000 --- a/FractalPainter/Solved/Step05/App/Actions/KochFractalAction.cs +++ /dev/null @@ -1,24 +0,0 @@ -using FractalPainting.Solved.Step05.App.Fractals; -using FractalPainting.Solved.Step05.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step05.App.Actions -{ - public class KochFractalAction : IUiAction - { - private readonly KochPainter kochPainter; - - public KochFractalAction(KochPainter kochPainter) - { - this.kochPainter = kochPainter; - } - - public string Category => "Фракталы"; - public string Name => "Кривая Коха"; - public string Description => "Кривая Коха"; - - public void Perform() - { - kochPainter.Paint(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/App/Actions/PaletteSettingsAction.cs b/FractalPainter/Solved/Step05/App/Actions/PaletteSettingsAction.cs deleted file mode 100644 index ccd89b290..000000000 --- a/FractalPainter/Solved/Step05/App/Actions/PaletteSettingsAction.cs +++ /dev/null @@ -1,25 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step05.Infrastructure.Injection; -using FractalPainting.Solved.Step05.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step05.App.Actions -{ - public class PaletteSettingsAction : IUiAction, INeed - { - private Palette palette; - - public void SetDependency(Palette dependency) - { - palette = dependency; - } - - public string Category => "Настройки"; - public string Name => "Палитра..."; - public string Description => "Цвета для рисования фракталов"; - - public void Perform() - { - SettingsForm.For(palette).ShowDialog(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/App/Actions/SaveImageAction.cs b/FractalPainter/Solved/Step05/App/Actions/SaveImageAction.cs deleted file mode 100644 index d06f8fb33..000000000 --- a/FractalPainter/Solved/Step05/App/Actions/SaveImageAction.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.IO; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step05.Infrastructure.Injection; -using FractalPainting.Solved.Step05.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step05.App.Actions -{ - public class SaveImageAction : IUiAction, INeed, INeed - { - private IImageDirectoryProvider imageDirectoryProvider; - private IImageHolder imageHolder; - - public void SetDependency(IImageDirectoryProvider dependency) - { - imageDirectoryProvider = dependency; - } - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public string Category => "Файл"; - public string Name => "Сохранить..."; - public string Description => "Сохранить изображение в файл"; - - public void Perform() - { - var dialog = new SaveFileDialog - { - CheckFileExists = false, - InitialDirectory = Path.GetFullPath(imageDirectoryProvider.ImagesDirectory), - DefaultExt = "bmp", - FileName = "image.bmp", - Filter = "Изображения (*.bmp)|*.bmp" - }; - var res = dialog.ShowDialog(); - if (res == DialogResult.OK) - imageHolder.SaveImage(dialog.FileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/App/AppSettings.cs b/FractalPainter/Solved/Step05/App/AppSettings.cs deleted file mode 100644 index 1c005975a..000000000 --- a/FractalPainter/Solved/Step05/App/AppSettings.cs +++ /dev/null @@ -1,10 +0,0 @@ -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step05.App -{ - public class AppSettings : IImageDirectoryProvider, IImageSettingsProvider - { - public string ImagesDirectory { get; set; } - public ImageSettings ImageSettings { get; set; } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/App/Fractals/DragonPainter.cs b/FractalPainter/Solved/Step05/App/Fractals/DragonPainter.cs deleted file mode 100644 index f164ca050..000000000 --- a/FractalPainter/Solved/Step05/App/Fractals/DragonPainter.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Drawing; -using System.Linq; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step05.App.Fractals -{ - public class DragonPainter - { - private readonly IImageHolder imageHolder; - private readonly DragonSettings settings; - private readonly float size; - private Size imageSize; - - public DragonPainter(IImageHolder imageHolder, DragonSettings settings) - { - this.imageHolder = imageHolder; - this.settings = settings; - imageSize = imageHolder.GetImageSize(); - size = Math.Min(imageSize.Width, imageSize.Height)/2.1f; - } - - public void Paint() - { - using (var graphics = imageHolder.StartDrawing()) - { - graphics.FillRectangle(Brushes.Black, 0, 0, imageSize.Width, imageSize.Height); - var r = new Random(); - var cosa = (float) Math.Cos(settings.Angle1); - var sina = (float) Math.Sin(settings.Angle1); - var cosb = (float) Math.Cos(settings.Angle2); - var sinb = (float) Math.Sin(settings.Angle2); - var shiftX = settings.ShiftX*size*0.8f; - var shiftY = settings.ShiftY*size*0.8f; - var scale = settings.Scale; - var p = new PointF(0, 0); - foreach (var i in Enumerable.Range(0, settings.IterationsCount)) - { - graphics.FillRectangle(Brushes.Yellow, imageSize.Width/3f + p.X, imageSize.Height/2f + p.Y, 1, 1); - if (r.Next(0, 2) == 0) - p = new PointF(scale*(p.X*cosa - p.Y*sina), scale*(p.X*sina + p.Y*cosa)); - else - p = new PointF(scale*(p.X*cosb - p.Y*sinb) + shiftX, scale*(p.X*sinb + p.Y*cosb) + shiftY); - if (i%100 == 0) imageHolder.UpdateUi(); - } - } - imageHolder.UpdateUi(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/App/Fractals/DragonSettings.cs b/FractalPainter/Solved/Step05/App/Fractals/DragonSettings.cs deleted file mode 100644 index 2cbc7d752..000000000 --- a/FractalPainter/Solved/Step05/App/Fractals/DragonSettings.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step05.App.Fractals -{ - public class DragonSettings - { - public double Angle1 { get; set; } = Math.PI / 4; - public double Angle2 { get; set; } = 3 * Math.PI / 4; - public float ShiftX { get; set; } = 1; - public float ShiftY { get; set; } = 0; - public float Scale { get; set; } = (float)(1 / Math.Sqrt(2)); - public int IterationsCount { get; set; } = 20000; - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/App/Fractals/DragonSettingsGenerator.cs b/FractalPainter/Solved/Step05/App/Fractals/DragonSettingsGenerator.cs deleted file mode 100644 index bd8526584..000000000 --- a/FractalPainter/Solved/Step05/App/Fractals/DragonSettingsGenerator.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step05.App.Fractals -{ - public class DragonSettingsGenerator - { - private readonly Random random; - - public DragonSettingsGenerator(Random random) - { - this.random = random; - } - - public DragonSettings Generate() - { - return new DragonSettings - { - Angle1 = random.NextDouble() / 2 + 0.5, - Angle2 = 3 * (random.NextDouble() / 2 + 0.5), - ShiftX = 1, - ShiftY = 0, - Scale = (float)(1/Math.Sqrt(2) + (random.NextDouble() - 0.5)/10) - }; - } - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/App/Fractals/IDragonPainterFactory.cs b/FractalPainter/Solved/Step05/App/Fractals/IDragonPainterFactory.cs deleted file mode 100644 index 6b1f6a01d..000000000 --- a/FractalPainter/Solved/Step05/App/Fractals/IDragonPainterFactory.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace FractalPainting.Solved.Step05.App.Fractals -{ - public interface IDragonPainterFactory - { - DragonPainter Create(DragonSettings settings); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/App/Fractals/KochPainter.cs b/FractalPainter/Solved/Step05/App/Fractals/KochPainter.cs deleted file mode 100644 index 69b65e003..000000000 --- a/FractalPainter/Solved/Step05/App/Fractals/KochPainter.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Drawing; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step05.App.Fractals -{ - public class KochPainter - { - private readonly IImageHolder imageHolder; - private readonly Palette palette; - - public KochPainter(IImageHolder imageHolder, Palette palette) - { - this.imageHolder = imageHolder; - this.palette = palette; - } - - public void Paint() - { - var imageSize = imageHolder.GetImageSize(); - using (var graphics = imageHolder.StartDrawing()) - using (var backgroundBrush = new SolidBrush(palette.BackgroundColor)) - { - graphics.FillRectangle(backgroundBrush, 0, 0, imageSize.Width, imageSize.Height); - DrawSegment(graphics, 0, imageSize.Height * 0.9f, imageSize.Width, imageSize.Height * 0.9f, true); - } - imageHolder.UpdateUi(); - } - - private void DrawSegment(Graphics graphics, float x0, float y0, float x1, float y1, bool primaryColor) - { - var len2 = (x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1); - if (len2 < 4) - { - if (y0 < 0 || y1 < 0) return; - using (var penBrush = new SolidBrush(primaryColor ? palette.PrimaryColor : palette.SecondaryColor)) - { - var pen = new Pen(penBrush, 3); - graphics.DrawLine(pen, x0, y0, x1, y1); - } - } - else - { - var vx = (x1 - x0)/3; - var vy = (y1 - y0)/3; - DrawSegment(graphics, x0, y0, x0 + vx, y0 + vy, primaryColor); - var k = (float) Math.Sqrt(3)/2f; - var px = (x0 + x1)/2 + vy*k; - var py = (y0 + y1)/2 - vx*k; - DrawSegment(graphics, x0 + vx, y0 + vy, px, py, !primaryColor); - DrawSegment(graphics, px, py, x0 + 2*vx, y0 + 2*vy, !primaryColor); - DrawSegment(graphics, x0 + 2*vx, y0 + 2*vy, x1, y1, primaryColor); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/App/MainForm.cs b/FractalPainter/Solved/Step05/App/MainForm.cs deleted file mode 100644 index 877c005ae..000000000 --- a/FractalPainter/Solved/Step05/App/MainForm.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System; -using System.Drawing; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step05.Infrastructure.Injection; -using FractalPainting.Solved.Step05.Infrastructure.UiActions; -using Ninject; - -namespace FractalPainting.Solved.Step05.App -{ - public class MainForm : Form - { - public MainForm(IUiAction[] actions, - PictureBoxImageHolder pictureBox, - Palette palette) - { - var imageSettings = CreateSettingsManager().Load().ImageSettings; - ClientSize = new Size(imageSettings.Width, imageSettings.Height); - - var mainMenu = new MenuStrip(); - mainMenu.Items.AddRange(actions.ToMenuItems()); - Controls.Add(mainMenu); - - pictureBox.RecreateImage(imageSettings); - pictureBox.Dock = DockStyle.Fill; - Controls.Add(pictureBox); - - DependencyInjector.Inject(actions, pictureBox); - DependencyInjector.Inject(actions, CreateSettingsManager().Load()); - DependencyInjector.Inject(actions, CreateSettingsManager().Load()); - DependencyInjector.Inject(actions, palette); - } - - private static SettingsManager CreateSettingsManager() - { - var container = new StandardKernel(); - container.Bind().To(); - container.Bind().To(); - return container.Get(); - } - - protected override void OnShown(EventArgs e) - { - base.OnShown(e); - Text = "Fractal Painter"; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/App/MainForm.resx b/FractalPainter/Solved/Step05/App/MainForm.resx deleted file mode 100644 index 9d845151a..000000000 --- a/FractalPainter/Solved/Step05/App/MainForm.resx +++ /dev/null @@ -1,123 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 17, 17 - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/App/PictureBoxImageHolder.cs b/FractalPainter/Solved/Step05/App/PictureBoxImageHolder.cs deleted file mode 100644 index ad016afba..000000000 --- a/FractalPainter/Solved/Step05/App/PictureBoxImageHolder.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Drawing; -using System.Drawing.Imaging; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step05.App -{ - public class PictureBoxImageHolder : PictureBox, IImageHolder - { - public Size GetImageSize() - { - FailIfNotInitialized(); - return Image.Size; - } - - public Graphics StartDrawing() - { - FailIfNotInitialized(); - return Graphics.FromImage(Image); - } - - private void FailIfNotInitialized() - { - if (Image == null) - throw new InvalidOperationException("Call PictureBoxImageHolder.RecreateImage before other method call!"); - } - - public void UpdateUi() - { - Refresh(); - Application.DoEvents(); - } - - public void RecreateImage(ImageSettings imageSettings) - { - Image = new Bitmap(imageSettings.Width, imageSettings.Height, PixelFormat.Format24bppRgb); - } - - public void SaveImage(string fileName) - { - FailIfNotInitialized(); - Image.Save(fileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/App/Program.cs b/FractalPainter/Solved/Step05/App/Program.cs deleted file mode 100644 index 25e582559..000000000 --- a/FractalPainter/Solved/Step05/App/Program.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step05.App.Actions; -using FractalPainting.Solved.Step05.App.Fractals; -using FractalPainting.Solved.Step05.Infrastructure.UiActions; -using Ninject; -using Ninject.Extensions.Factory; - -namespace FractalPainting.Solved.Step05.App -{ - internal static class Program - { - /// - /// The main entry point for the application. - /// - [STAThread] - private static void Main() - { - try - { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - - var container = new StandardKernel(); - - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - - container.Bind().ToSelf() - .InSingletonScope(); - container.Bind() - .To() - .InSingletonScope(); - - container.Bind().ToFactory(); - - var mainForm = container.Get(); - Application.Run(mainForm); - } - catch (Exception e) - { - MessageBox.Show(e.Message); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/App/SettingsManager.cs b/FractalPainter/Solved/Step05/App/SettingsManager.cs deleted file mode 100644 index 954131f26..000000000 --- a/FractalPainter/Solved/Step05/App/SettingsManager.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step05.App -{ - public class SettingsManager - { - private readonly IObjectSerializer serializer; - private readonly IBlobStorage storage; - private string settingsFilename; - - public SettingsManager(IObjectSerializer serializer, IBlobStorage storage) - { - this.serializer = serializer; - this.storage = storage; - } - - public AppSettings Load() - { - try - { - settingsFilename = "app.settings"; - var data = storage.Get(settingsFilename); - if (data == null) - { - var defaultSettings = CreateDefaultSettings(); - Save(defaultSettings); - return defaultSettings; - } - return serializer.Deserialize(data); - } - catch (Exception e) - { - MessageBox.Show(e.Message, "Не удалось загрузить настройки"); - return CreateDefaultSettings(); - } - } - - private static AppSettings CreateDefaultSettings() - { - return new AppSettings - { - ImagesDirectory = ".", - ImageSettings = new ImageSettings() - }; - } - - public void Save(AppSettings settings) - { - storage.Set(settingsFilename, serializer.Serialize(settings)); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/Infrastructure/Injection/DependencyInjector.cs b/FractalPainter/Solved/Step05/Infrastructure/Injection/DependencyInjector.cs deleted file mode 100644 index 148effd25..000000000 --- a/FractalPainter/Solved/Step05/Infrastructure/Injection/DependencyInjector.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Collections; - -namespace FractalPainting.Solved.Step05.Infrastructure.Injection -{ - public class DependencyInjector - { - public static void Inject(object service, TDependency dependency) - { - var need = service as INeed; - need?.SetDependency(dependency); - } - - public static void Inject(IEnumerable services, TDependency dependency) - { - foreach (var service in services) - Inject(service, dependency); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/Infrastructure/Injection/INeed.cs b/FractalPainter/Solved/Step05/Infrastructure/Injection/INeed.cs deleted file mode 100644 index 43aa14b8f..000000000 --- a/FractalPainter/Solved/Step05/Infrastructure/Injection/INeed.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace FractalPainting.Solved.Step05.Infrastructure.Injection -{ - public interface INeed - { - void SetDependency(T dependency); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/Infrastructure/UiActions/IUiAction.cs b/FractalPainter/Solved/Step05/Infrastructure/UiActions/IUiAction.cs deleted file mode 100644 index dd7f27909..000000000 --- a/FractalPainter/Solved/Step05/Infrastructure/UiActions/IUiAction.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace FractalPainting.Solved.Step05.Infrastructure.UiActions -{ - public interface IUiAction - { - string Category { get; } - string Name { get; } - string Description { get; } - void Perform(); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/Infrastructure/UiActions/UiActionExtensions.cs b/FractalPainter/Solved/Step05/Infrastructure/UiActions/UiActionExtensions.cs deleted file mode 100644 index 0624cad3e..000000000 --- a/FractalPainter/Solved/Step05/Infrastructure/UiActions/UiActionExtensions.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Windows.Forms; - -namespace FractalPainting.Solved.Step05.Infrastructure.UiActions -{ - public static class UiActionExtensions - { - public static ToolStripItem[] ToMenuItems(this IUiAction[] actions) - { - var items = actions.GroupBy(a => a.Category) - .Select(g => CreateTopLevelMenuItem(g.Key, g.ToList())) - .Cast() - .ToArray(); - return items; - } - - private static ToolStripMenuItem CreateTopLevelMenuItem(string name, IList items) - { - var menuItems = items.Select(a => a.ToMenuItem()).ToArray(); - return new ToolStripMenuItem(name, null, menuItems); - } - - public static ToolStripItem ToMenuItem(this IUiAction action) - { - return - new ToolStripMenuItem(action.Name, null, (sender, args) => action.Perform()) - { - ToolTipText = action.Description, - Tag = action - }; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step05/Step05.csproj b/FractalPainter/Solved/Step05/Step05.csproj deleted file mode 100644 index 5fc8b4a65..000000000 --- a/FractalPainter/Solved/Step05/Step05.csproj +++ /dev/null @@ -1,21 +0,0 @@ - - - - WinExe - netcoreapp3.1 - true - false - FractalPainting.Solved.Step05 - FractalPainter - - - - - - - - - - - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/App/Actions/DragonFractalAction.cs b/FractalPainter/Solved/Step06/App/Actions/DragonFractalAction.cs deleted file mode 100644 index 889438c69..000000000 --- a/FractalPainter/Solved/Step06/App/Actions/DragonFractalAction.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step06.App.Fractals; -using FractalPainting.Solved.Step06.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step06.App.Actions -{ - public class DragonFractalAction : IUiAction - { - private readonly IDragonPainterFactory dragonPainterFactory; - private readonly Func createDragonSettingsGenerator; - - public DragonFractalAction(IDragonPainterFactory dragonPainterFactory, - Func createDragonSettingsGenerator) - { - this.dragonPainterFactory = dragonPainterFactory; - this.createDragonSettingsGenerator = createDragonSettingsGenerator; - } - - public string Category => "Фракталы"; - public string Name => "Дракон"; - public string Description => "Дракон Хартера-Хейтуэя"; - - public void Perform() - { - var dragonSettings = CreateRandomSettings(); - // редактируем настройки: - SettingsForm.For(dragonSettings).ShowDialog(); - // создаём painter с такими настройками - dragonPainterFactory.Create(dragonSettings).Paint(); - } - - private DragonSettings CreateRandomSettings() - { - return createDragonSettingsGenerator(new Random()).Generate(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/App/Actions/ImageSettingsAction.cs b/FractalPainter/Solved/Step06/App/Actions/ImageSettingsAction.cs deleted file mode 100644 index 6e28c9e3e..000000000 --- a/FractalPainter/Solved/Step06/App/Actions/ImageSettingsAction.cs +++ /dev/null @@ -1,33 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step06.Infrastructure.Injection; -using FractalPainting.Solved.Step06.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step06.App.Actions -{ - public class ImageSettingsAction : IUiAction, INeed, INeed - { - private IImageHolder imageHolder; - private IImageSettingsProvider imageSettingsProvider; - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public void SetDependency(IImageSettingsProvider dependency) - { - imageSettingsProvider = dependency; - } - - public string Category => "Настройки"; - public string Name => "Изображение..."; - public string Description => "Размеры изображения"; - - public void Perform() - { - var imageSettings = imageSettingsProvider.ImageSettings; - SettingsForm.For(imageSettings).ShowDialog(); - imageHolder.RecreateImage(imageSettings); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/App/Actions/KochFractalAction.cs b/FractalPainter/Solved/Step06/App/Actions/KochFractalAction.cs deleted file mode 100644 index 03bfc0f0d..000000000 --- a/FractalPainter/Solved/Step06/App/Actions/KochFractalAction.cs +++ /dev/null @@ -1,24 +0,0 @@ -using FractalPainting.Solved.Step06.App.Fractals; -using FractalPainting.Solved.Step06.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step06.App.Actions -{ - public class KochFractalAction : IUiAction - { - private readonly KochPainter kochPainter; - - public KochFractalAction(KochPainter kochPainter) - { - this.kochPainter = kochPainter; - } - - public string Category => "Фракталы"; - public string Name => "Кривая Коха"; - public string Description => "Кривая Коха"; - - public void Perform() - { - kochPainter.Paint(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/App/Actions/PaletteSettingsAction.cs b/FractalPainter/Solved/Step06/App/Actions/PaletteSettingsAction.cs deleted file mode 100644 index dd25aa093..000000000 --- a/FractalPainter/Solved/Step06/App/Actions/PaletteSettingsAction.cs +++ /dev/null @@ -1,25 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step06.Infrastructure.Injection; -using FractalPainting.Solved.Step06.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step06.App.Actions -{ - public class PaletteSettingsAction : IUiAction, INeed - { - private Palette palette; - - public void SetDependency(Palette dependency) - { - palette = dependency; - } - - public string Category => "Настройки"; - public string Name => "Палитра..."; - public string Description => "Цвета для рисования фракталов"; - - public void Perform() - { - SettingsForm.For(palette).ShowDialog(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/App/Actions/SaveImageAction.cs b/FractalPainter/Solved/Step06/App/Actions/SaveImageAction.cs deleted file mode 100644 index 730d9bd7c..000000000 --- a/FractalPainter/Solved/Step06/App/Actions/SaveImageAction.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.IO; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step06.Infrastructure.Injection; -using FractalPainting.Solved.Step06.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step06.App.Actions -{ - public class SaveImageAction : IUiAction, INeed, INeed - { - private IImageDirectoryProvider imageDirectoryProvider; - private IImageHolder imageHolder; - - public void SetDependency(IImageDirectoryProvider dependency) - { - imageDirectoryProvider = dependency; - } - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public string Category => "Файл"; - public string Name => "Сохранить..."; - public string Description => "Сохранить изображение в файл"; - - public void Perform() - { - var dialog = new SaveFileDialog - { - CheckFileExists = false, - InitialDirectory = Path.GetFullPath(imageDirectoryProvider.ImagesDirectory), - DefaultExt = "bmp", - FileName = "image.bmp", - Filter = "Изображения (*.bmp)|*.bmp" - }; - var res = dialog.ShowDialog(); - if (res == DialogResult.OK) - imageHolder.SaveImage(dialog.FileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/App/AppSettings.cs b/FractalPainter/Solved/Step06/App/AppSettings.cs deleted file mode 100644 index 0e21cc558..000000000 --- a/FractalPainter/Solved/Step06/App/AppSettings.cs +++ /dev/null @@ -1,10 +0,0 @@ -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step06.App -{ - public class AppSettings : IImageDirectoryProvider, IImageSettingsProvider - { - public string ImagesDirectory { get; set; } - public ImageSettings ImageSettings { get; set; } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/App/Fractals/DragonPainter.cs b/FractalPainter/Solved/Step06/App/Fractals/DragonPainter.cs deleted file mode 100644 index 09dee4ca5..000000000 --- a/FractalPainter/Solved/Step06/App/Fractals/DragonPainter.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Drawing; -using System.Linq; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step06.App.Fractals -{ - public class DragonPainter - { - private readonly IImageHolder imageHolder; - private readonly DragonSettings settings; - private readonly float size; - private Size imageSize; - - public DragonPainter(IImageHolder imageHolder, DragonSettings settings) - { - this.imageHolder = imageHolder; - this.settings = settings; - imageSize = imageHolder.GetImageSize(); - size = Math.Min(imageSize.Width, imageSize.Height)/2.1f; - } - - public void Paint() - { - using (var graphics = imageHolder.StartDrawing()) - { - graphics.FillRectangle(Brushes.Black, 0, 0, imageSize.Width, imageSize.Height); - var r = new Random(); - var cosa = (float) Math.Cos(settings.Angle1); - var sina = (float) Math.Sin(settings.Angle1); - var cosb = (float) Math.Cos(settings.Angle2); - var sinb = (float) Math.Sin(settings.Angle2); - var shiftX = settings.ShiftX*size*0.8f; - var shiftY = settings.ShiftY*size*0.8f; - var scale = settings.Scale; - var p = new PointF(0, 0); - foreach (var i in Enumerable.Range(0, settings.IterationsCount)) - { - graphics.FillRectangle(Brushes.Yellow, imageSize.Width/3f + p.X, imageSize.Height/2f + p.Y, 1, 1); - if (r.Next(0, 2) == 0) - p = new PointF(scale*(p.X*cosa - p.Y*sina), scale*(p.X*sina + p.Y*cosa)); - else - p = new PointF(scale*(p.X*cosb - p.Y*sinb) + shiftX, scale*(p.X*sinb + p.Y*cosb) + shiftY); - if (i%100 == 0) imageHolder.UpdateUi(); - } - } - imageHolder.UpdateUi(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/App/Fractals/DragonSettings.cs b/FractalPainter/Solved/Step06/App/Fractals/DragonSettings.cs deleted file mode 100644 index 2157b7728..000000000 --- a/FractalPainter/Solved/Step06/App/Fractals/DragonSettings.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step06.App.Fractals -{ - public class DragonSettings - { - public double Angle1 { get; set; } = Math.PI / 4; - public double Angle2 { get; set; } = 3 * Math.PI / 4; - public float ShiftX { get; set; } = 1; - public float ShiftY { get; set; } = 0; - public float Scale { get; set; } = (float)(1 / Math.Sqrt(2)); - public int IterationsCount { get; set; } = 20000; - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/App/Fractals/DragonSettingsGenerator.cs b/FractalPainter/Solved/Step06/App/Fractals/DragonSettingsGenerator.cs deleted file mode 100644 index 29d5dcb73..000000000 --- a/FractalPainter/Solved/Step06/App/Fractals/DragonSettingsGenerator.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step06.App.Fractals -{ - public class DragonSettingsGenerator - { - private readonly Random random; - - public DragonSettingsGenerator(Random random) - { - this.random = random; - } - - public DragonSettings Generate() - { - return new DragonSettings - { - Angle1 = random.NextDouble() / 2 + 0.5, - Angle2 = 3 * (random.NextDouble() / 2 + 0.5), - ShiftX = 1, - ShiftY = 0, - Scale = (float)(1/Math.Sqrt(2) + (random.NextDouble() - 0.5)/10) - }; - } - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/App/Fractals/IDragonPainterFactory.cs b/FractalPainter/Solved/Step06/App/Fractals/IDragonPainterFactory.cs deleted file mode 100644 index f7cf84451..000000000 --- a/FractalPainter/Solved/Step06/App/Fractals/IDragonPainterFactory.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace FractalPainting.Solved.Step06.App.Fractals -{ - public interface IDragonPainterFactory - { - DragonPainter Create(DragonSettings settings); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/App/Fractals/KochPainter.cs b/FractalPainter/Solved/Step06/App/Fractals/KochPainter.cs deleted file mode 100644 index a84c57b24..000000000 --- a/FractalPainter/Solved/Step06/App/Fractals/KochPainter.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Drawing; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step06.App.Fractals -{ - public class KochPainter - { - private readonly IImageHolder imageHolder; - private readonly Palette palette; - - public KochPainter(IImageHolder imageHolder, Palette palette) - { - this.imageHolder = imageHolder; - this.palette = palette; - } - - public void Paint() - { - var imageSize = imageHolder.GetImageSize(); - using (var graphics = imageHolder.StartDrawing()) - using (var backgroundBrush = new SolidBrush(palette.BackgroundColor)) - { - graphics.FillRectangle(backgroundBrush, 0, 0, imageSize.Width, imageSize.Height); - DrawSegment(graphics, 0, imageSize.Height * 0.9f, imageSize.Width, imageSize.Height * 0.9f, true); - } - imageHolder.UpdateUi(); - } - - private void DrawSegment(Graphics graphics, float x0, float y0, float x1, float y1, bool primaryColor) - { - var len2 = (x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1); - if (len2 < 4) - { - if (y0 < 0 || y1 < 0) return; - using (var penBrush = new SolidBrush(primaryColor ? palette.PrimaryColor : palette.SecondaryColor)) - { - var pen = new Pen(penBrush, 3); - graphics.DrawLine(pen, x0, y0, x1, y1); - } - } - else - { - var vx = (x1 - x0)/3; - var vy = (y1 - y0)/3; - DrawSegment(graphics, x0, y0, x0 + vx, y0 + vy, primaryColor); - var k = (float) Math.Sqrt(3)/2f; - var px = (x0 + x1)/2 + vy*k; - var py = (y0 + y1)/2 - vx*k; - DrawSegment(graphics, x0 + vx, y0 + vy, px, py, !primaryColor); - DrawSegment(graphics, px, py, x0 + 2*vx, y0 + 2*vy, !primaryColor); - DrawSegment(graphics, x0 + 2*vx, y0 + 2*vy, x1, y1, primaryColor); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/App/MainForm.cs b/FractalPainter/Solved/Step06/App/MainForm.cs deleted file mode 100644 index bfe737ad1..000000000 --- a/FractalPainter/Solved/Step06/App/MainForm.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System; -using System.Drawing; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step06.Infrastructure.Injection; -using FractalPainting.Solved.Step06.Infrastructure.UiActions; -using Ninject; - -namespace FractalPainting.Solved.Step06.App -{ - public class MainForm : Form - { - public MainForm(IUiAction[] actions, - PictureBoxImageHolder pictureBox, - Palette palette) - { - var imageSettings = CreateSettingsManager().Load().ImageSettings; - ClientSize = new Size(imageSettings.Width, imageSettings.Height); - - var mainMenu = new MenuStrip(); - mainMenu.Items.AddRange(actions.ToMenuItems()); - Controls.Add(mainMenu); - - pictureBox.RecreateImage(imageSettings); - pictureBox.Dock = DockStyle.Fill; - Controls.Add(pictureBox); - - DependencyInjector.Inject(actions, pictureBox); - DependencyInjector.Inject(actions, CreateSettingsManager().Load()); - DependencyInjector.Inject(actions, CreateSettingsManager().Load()); - DependencyInjector.Inject(actions, palette); - } - - private static SettingsManager CreateSettingsManager() - { - var container = new StandardKernel(); - container.Bind().To(); - container.Bind().To(); - return container.Get(); - } - - protected override void OnShown(EventArgs e) - { - base.OnShown(e); - Text = "Fractal Painter"; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/App/MainForm.resx b/FractalPainter/Solved/Step06/App/MainForm.resx deleted file mode 100644 index 9d845151a..000000000 --- a/FractalPainter/Solved/Step06/App/MainForm.resx +++ /dev/null @@ -1,123 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 17, 17 - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/App/PictureBoxImageHolder.cs b/FractalPainter/Solved/Step06/App/PictureBoxImageHolder.cs deleted file mode 100644 index e7aebdf66..000000000 --- a/FractalPainter/Solved/Step06/App/PictureBoxImageHolder.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Drawing; -using System.Drawing.Imaging; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step06.App -{ - public class PictureBoxImageHolder : PictureBox, IImageHolder - { - public Size GetImageSize() - { - FailIfNotInitialized(); - return Image.Size; - } - - public Graphics StartDrawing() - { - FailIfNotInitialized(); - return Graphics.FromImage(Image); - } - - private void FailIfNotInitialized() - { - if (Image == null) - throw new InvalidOperationException("Call PictureBoxImageHolder.RecreateImage before other method call!"); - } - - public void UpdateUi() - { - Refresh(); - Application.DoEvents(); - } - - public void RecreateImage(ImageSettings imageSettings) - { - Image = new Bitmap(imageSettings.Width, imageSettings.Height, PixelFormat.Format24bppRgb); - } - - public void SaveImage(string fileName) - { - FailIfNotInitialized(); - Image.Save(fileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/App/Program.cs b/FractalPainter/Solved/Step06/App/Program.cs deleted file mode 100644 index 8a1a7b362..000000000 --- a/FractalPainter/Solved/Step06/App/Program.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step06.App.Actions; -using FractalPainting.Solved.Step06.App.Fractals; -using FractalPainting.Solved.Step06.Infrastructure.UiActions; -using Ninject; -using Ninject.Extensions.Factory; - -namespace FractalPainting.Solved.Step06.App -{ - internal static class Program - { - /// - /// The main entry point for the application. - /// - [STAThread] - private static void Main() - { - try - { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - - var container = new StandardKernel(); - - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - - container.Bind().ToSelf() - .InSingletonScope(); - container.Bind() - .To() - .InSingletonScope(); - - container.Bind().ToFactory(); - - var mainForm = container.Get(); - Application.Run(mainForm); - } - catch (Exception e) - { - MessageBox.Show(e.Message); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/App/SettingsManager.cs b/FractalPainter/Solved/Step06/App/SettingsManager.cs deleted file mode 100644 index 874e29fae..000000000 --- a/FractalPainter/Solved/Step06/App/SettingsManager.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step06.App -{ - public class SettingsManager - { - private readonly IObjectSerializer serializer; - private readonly IBlobStorage storage; - private string settingsFilename; - - public SettingsManager(IObjectSerializer serializer, IBlobStorage storage) - { - this.serializer = serializer; - this.storage = storage; - } - - public AppSettings Load() - { - try - { - settingsFilename = "app.settings"; - var data = storage.Get(settingsFilename); - if (data == null) - { - var defaultSettings = CreateDefaultSettings(); - Save(defaultSettings); - return defaultSettings; - } - return serializer.Deserialize(data); - } - catch (Exception e) - { - MessageBox.Show(e.Message, "Не удалось загрузить настройки"); - return CreateDefaultSettings(); - } - } - - private static AppSettings CreateDefaultSettings() - { - return new AppSettings - { - ImagesDirectory = ".", - ImageSettings = new ImageSettings() - }; - } - - public void Save(AppSettings settings) - { - storage.Set(settingsFilename, serializer.Serialize(settings)); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/Infrastructure/Injection/DependencyInjector.cs b/FractalPainter/Solved/Step06/Infrastructure/Injection/DependencyInjector.cs deleted file mode 100644 index c8ddb2147..000000000 --- a/FractalPainter/Solved/Step06/Infrastructure/Injection/DependencyInjector.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Collections; - -namespace FractalPainting.Solved.Step06.Infrastructure.Injection -{ - public class DependencyInjector - { - public static void Inject(object service, TDependency dependency) - { - var need = service as INeed; - need?.SetDependency(dependency); - } - - public static void Inject(IEnumerable services, TDependency dependency) - { - foreach (var service in services) - Inject(service, dependency); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/Infrastructure/Injection/INeed.cs b/FractalPainter/Solved/Step06/Infrastructure/Injection/INeed.cs deleted file mode 100644 index 86fd6ca5f..000000000 --- a/FractalPainter/Solved/Step06/Infrastructure/Injection/INeed.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace FractalPainting.Solved.Step06.Infrastructure.Injection -{ - public interface INeed - { - void SetDependency(T dependency); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/Infrastructure/UiActions/IUiAction.cs b/FractalPainter/Solved/Step06/Infrastructure/UiActions/IUiAction.cs deleted file mode 100644 index 623186a89..000000000 --- a/FractalPainter/Solved/Step06/Infrastructure/UiActions/IUiAction.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace FractalPainting.Solved.Step06.Infrastructure.UiActions -{ - public interface IUiAction - { - string Category { get; } - string Name { get; } - string Description { get; } - void Perform(); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/Infrastructure/UiActions/UiActionExtensions.cs b/FractalPainter/Solved/Step06/Infrastructure/UiActions/UiActionExtensions.cs deleted file mode 100644 index 4b281fb10..000000000 --- a/FractalPainter/Solved/Step06/Infrastructure/UiActions/UiActionExtensions.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Windows.Forms; - -namespace FractalPainting.Solved.Step06.Infrastructure.UiActions -{ - public static class UiActionExtensions - { - public static ToolStripItem[] ToMenuItems(this IUiAction[] actions) - { - var items = actions.GroupBy(a => a.Category) - .Select(g => CreateTopLevelMenuItem(g.Key, g.ToList())) - .Cast() - .ToArray(); - return items; - } - - private static ToolStripMenuItem CreateTopLevelMenuItem(string name, IList items) - { - var menuItems = items.Select(a => a.ToMenuItem()).ToArray(); - return new ToolStripMenuItem(name, null, menuItems); - } - - public static ToolStripItem ToMenuItem(this IUiAction action) - { - return - new ToolStripMenuItem(action.Name, null, (sender, args) => action.Perform()) - { - ToolTipText = action.Description, - Tag = action - }; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step06/Step06.csproj b/FractalPainter/Solved/Step06/Step06.csproj deleted file mode 100644 index 719caddb0..000000000 --- a/FractalPainter/Solved/Step06/Step06.csproj +++ /dev/null @@ -1,21 +0,0 @@ - - - - WinExe - netcoreapp3.1 - true - false - FractalPainting.Solved.Step06 - FractalPainter - - - - - - - - - - - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/App/Actions/DragonFractalAction.cs b/FractalPainter/Solved/Step07/App/Actions/DragonFractalAction.cs deleted file mode 100644 index ad4b07249..000000000 --- a/FractalPainter/Solved/Step07/App/Actions/DragonFractalAction.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step07.App.Fractals; -using FractalPainting.Solved.Step07.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step07.App.Actions -{ - public class DragonFractalAction : IUiAction - { - private readonly IDragonPainterFactory dragonPainterFactory; - private readonly Func createDragonSettingsGenerator; - - public DragonFractalAction(IDragonPainterFactory dragonPainterFactory, - Func createDragonSettingsGenerator) - { - this.dragonPainterFactory = dragonPainterFactory; - this.createDragonSettingsGenerator = createDragonSettingsGenerator; - } - - public string Category => "Фракталы"; - public string Name => "Дракон"; - public string Description => "Дракон Хартера-Хейтуэя"; - - public void Perform() - { - var dragonSettings = CreateRandomSettings(); - // редактируем настройки: - SettingsForm.For(dragonSettings).ShowDialog(); - // создаём painter с такими настройками - dragonPainterFactory.Create(dragonSettings).Paint(); - } - - private DragonSettings CreateRandomSettings() - { - return createDragonSettingsGenerator(new Random()).Generate(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/App/Actions/ImageSettingsAction.cs b/FractalPainter/Solved/Step07/App/Actions/ImageSettingsAction.cs deleted file mode 100644 index e249a2e7c..000000000 --- a/FractalPainter/Solved/Step07/App/Actions/ImageSettingsAction.cs +++ /dev/null @@ -1,33 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step07.Infrastructure.Injection; -using FractalPainting.Solved.Step07.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step07.App.Actions -{ - public class ImageSettingsAction : IUiAction, INeed, INeed - { - private IImageHolder imageHolder; - private IImageSettingsProvider imageSettingsProvider; - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public void SetDependency(IImageSettingsProvider dependency) - { - imageSettingsProvider = dependency; - } - - public string Category => "Настройки"; - public string Name => "Изображение..."; - public string Description => "Размеры изображения"; - - public void Perform() - { - var imageSettings = imageSettingsProvider.ImageSettings; - SettingsForm.For(imageSettings).ShowDialog(); - imageHolder.RecreateImage(imageSettings); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/App/Actions/KochFractalAction.cs b/FractalPainter/Solved/Step07/App/Actions/KochFractalAction.cs deleted file mode 100644 index d9f114af4..000000000 --- a/FractalPainter/Solved/Step07/App/Actions/KochFractalAction.cs +++ /dev/null @@ -1,24 +0,0 @@ -using FractalPainting.Solved.Step07.App.Fractals; -using FractalPainting.Solved.Step07.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step07.App.Actions -{ - public class KochFractalAction : IUiAction - { - private readonly KochPainter kochPainter; - - public KochFractalAction(KochPainter kochPainter) - { - this.kochPainter = kochPainter; - } - - public string Category => "Фракталы"; - public string Name => "Кривая Коха"; - public string Description => "Кривая Коха"; - - public void Perform() - { - kochPainter.Paint(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/App/Actions/PaletteSettingsAction.cs b/FractalPainter/Solved/Step07/App/Actions/PaletteSettingsAction.cs deleted file mode 100644 index b56a51b71..000000000 --- a/FractalPainter/Solved/Step07/App/Actions/PaletteSettingsAction.cs +++ /dev/null @@ -1,25 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step07.Infrastructure.Injection; -using FractalPainting.Solved.Step07.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step07.App.Actions -{ - public class PaletteSettingsAction : IUiAction, INeed - { - private Palette palette; - - public void SetDependency(Palette dependency) - { - palette = dependency; - } - - public string Category => "Настройки"; - public string Name => "Палитра..."; - public string Description => "Цвета для рисования фракталов"; - - public void Perform() - { - SettingsForm.For(palette).ShowDialog(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/App/Actions/SaveImageAction.cs b/FractalPainter/Solved/Step07/App/Actions/SaveImageAction.cs deleted file mode 100644 index b0ea5ee3c..000000000 --- a/FractalPainter/Solved/Step07/App/Actions/SaveImageAction.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.IO; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step07.Infrastructure.Injection; -using FractalPainting.Solved.Step07.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step07.App.Actions -{ - public class SaveImageAction : IUiAction, INeed, INeed - { - private IImageDirectoryProvider imageDirectoryProvider; - private IImageHolder imageHolder; - - public void SetDependency(IImageDirectoryProvider dependency) - { - imageDirectoryProvider = dependency; - } - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public string Category => "Файл"; - public string Name => "Сохранить..."; - public string Description => "Сохранить изображение в файл"; - - public void Perform() - { - var dialog = new SaveFileDialog - { - CheckFileExists = false, - InitialDirectory = Path.GetFullPath(imageDirectoryProvider.ImagesDirectory), - DefaultExt = "bmp", - FileName = "image.bmp", - Filter = "Изображения (*.bmp)|*.bmp" - }; - var res = dialog.ShowDialog(); - if (res == DialogResult.OK) - imageHolder.SaveImage(dialog.FileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/App/AppSettings.cs b/FractalPainter/Solved/Step07/App/AppSettings.cs deleted file mode 100644 index 26761818e..000000000 --- a/FractalPainter/Solved/Step07/App/AppSettings.cs +++ /dev/null @@ -1,10 +0,0 @@ -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step07.App -{ - public class AppSettings : IImageDirectoryProvider, IImageSettingsProvider - { - public string ImagesDirectory { get; set; } - public ImageSettings ImageSettings { get; set; } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/App/Fractals/DragonPainter.cs b/FractalPainter/Solved/Step07/App/Fractals/DragonPainter.cs deleted file mode 100644 index ff5dd293d..000000000 --- a/FractalPainter/Solved/Step07/App/Fractals/DragonPainter.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Drawing; -using System.Linq; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step07.App.Fractals -{ - public class DragonPainter - { - private readonly IImageHolder imageHolder; - private readonly DragonSettings settings; - private readonly Palette palette; - private readonly float size; - private Size imageSize; - - public DragonPainter(IImageHolder imageHolder, - DragonSettings settings, Palette palette) - { - this.imageHolder = imageHolder; - this.settings = settings; - this.palette = palette; - imageSize = imageHolder.GetImageSize(); - size = Math.Min(imageSize.Width, imageSize.Height)/2.1f; - } - - public void Paint() - { - using (var graphics = imageHolder.StartDrawing()) - { - graphics.FillRectangle(new SolidBrush(palette.BackgroundColor), 0, 0, imageSize.Width, imageSize.Height); - var r = new Random(); - var cosa = (float) Math.Cos(settings.Angle1); - var sina = (float) Math.Sin(settings.Angle1); - var cosb = (float) Math.Cos(settings.Angle2); - var sinb = (float) Math.Sin(settings.Angle2); - var shiftX = settings.ShiftX*size*0.8f; - var shiftY = settings.ShiftY*size*0.8f; - var scale = settings.Scale; - var p = new PointF(0, 0); - var pointBrush = new SolidBrush(palette.PrimaryColor); - foreach (var i in Enumerable.Range(0, settings.IterationsCount)) - { - graphics.FillRectangle(pointBrush, imageSize.Width/3f + p.X, imageSize.Height/2f + p.Y, 1, 1); - if (r.Next(0, 2) == 0) - p = new PointF(scale*(p.X*cosa - p.Y*sina), scale*(p.X*sina + p.Y*cosa)); - else - p = new PointF(scale*(p.X*cosb - p.Y*sinb) + shiftX, scale*(p.X*sinb + p.Y*cosb) + shiftY); - if (i%100 == 0) imageHolder.UpdateUi(); - } - } - imageHolder.UpdateUi(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/App/Fractals/DragonSettings.cs b/FractalPainter/Solved/Step07/App/Fractals/DragonSettings.cs deleted file mode 100644 index 9a3f21fc4..000000000 --- a/FractalPainter/Solved/Step07/App/Fractals/DragonSettings.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step07.App.Fractals -{ - public class DragonSettings - { - public double Angle1 { get; set; } = Math.PI / 4; - public double Angle2 { get; set; } = 3 * Math.PI / 4; - public float ShiftX { get; set; } = 1; - public float ShiftY { get; set; } = 0; - public float Scale { get; set; } = (float)(1 / Math.Sqrt(2)); - public int IterationsCount { get; set; } = 20000; - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/App/Fractals/DragonSettingsGenerator.cs b/FractalPainter/Solved/Step07/App/Fractals/DragonSettingsGenerator.cs deleted file mode 100644 index bb30c5e5f..000000000 --- a/FractalPainter/Solved/Step07/App/Fractals/DragonSettingsGenerator.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step07.App.Fractals -{ - public class DragonSettingsGenerator - { - private readonly Random random; - - public DragonSettingsGenerator(Random random) - { - this.random = random; - } - - public DragonSettings Generate() - { - return new DragonSettings - { - Angle1 = random.NextDouble() / 2 + 0.5, - Angle2 = 3 * (random.NextDouble() / 2 + 0.5), - ShiftX = 1, - ShiftY = 0, - Scale = (float)(1/Math.Sqrt(2) + (random.NextDouble() - 0.5)/10) - }; - } - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/App/Fractals/IDragonPainterFactory.cs b/FractalPainter/Solved/Step07/App/Fractals/IDragonPainterFactory.cs deleted file mode 100644 index 8f8702c9a..000000000 --- a/FractalPainter/Solved/Step07/App/Fractals/IDragonPainterFactory.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace FractalPainting.Solved.Step07.App.Fractals -{ - public interface IDragonPainterFactory - { - DragonPainter Create(DragonSettings settings); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/App/Fractals/KochPainter.cs b/FractalPainter/Solved/Step07/App/Fractals/KochPainter.cs deleted file mode 100644 index ab9247fe7..000000000 --- a/FractalPainter/Solved/Step07/App/Fractals/KochPainter.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Drawing; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step07.App.Fractals -{ - public class KochPainter - { - private readonly IImageHolder imageHolder; - private readonly Palette palette; - - public KochPainter(IImageHolder imageHolder, Palette palette) - { - this.imageHolder = imageHolder; - this.palette = palette; - } - - public void Paint() - { - var imageSize = imageHolder.GetImageSize(); - using (var graphics = imageHolder.StartDrawing()) - using (var backgroundBrush = new SolidBrush(palette.BackgroundColor)) - { - graphics.FillRectangle(backgroundBrush, 0, 0, imageSize.Width, imageSize.Height); - DrawSegment(graphics, 0, imageSize.Height * 0.9f, imageSize.Width, imageSize.Height * 0.9f, true); - } - imageHolder.UpdateUi(); - } - - private void DrawSegment(Graphics graphics, float x0, float y0, float x1, float y1, bool primaryColor) - { - var len2 = (x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1); - if (len2 < 4) - { - if (y0 < 0 || y1 < 0) return; - using (var penBrush = new SolidBrush(primaryColor ? palette.PrimaryColor : palette.SecondaryColor)) - { - var pen = new Pen(penBrush, 3); - graphics.DrawLine(pen, x0, y0, x1, y1); - } - } - else - { - var vx = (x1 - x0)/3; - var vy = (y1 - y0)/3; - DrawSegment(graphics, x0, y0, x0 + vx, y0 + vy, primaryColor); - var k = (float) Math.Sqrt(3)/2f; - var px = (x0 + x1)/2 + vy*k; - var py = (y0 + y1)/2 - vx*k; - DrawSegment(graphics, x0 + vx, y0 + vy, px, py, !primaryColor); - DrawSegment(graphics, px, py, x0 + 2*vx, y0 + 2*vy, !primaryColor); - DrawSegment(graphics, x0 + 2*vx, y0 + 2*vy, x1, y1, primaryColor); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/App/MainForm.cs b/FractalPainter/Solved/Step07/App/MainForm.cs deleted file mode 100644 index a00b981d1..000000000 --- a/FractalPainter/Solved/Step07/App/MainForm.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System; -using System.Drawing; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step07.Infrastructure.Injection; -using FractalPainting.Solved.Step07.Infrastructure.UiActions; -using Ninject; - -namespace FractalPainting.Solved.Step07.App -{ - public class MainForm : Form - { - public MainForm(IUiAction[] actions, - PictureBoxImageHolder pictureBox, - Palette palette) - { - var imageSettings = CreateSettingsManager().Load().ImageSettings; - ClientSize = new Size(imageSettings.Width, imageSettings.Height); - - var mainMenu = new MenuStrip(); - mainMenu.Items.AddRange(actions.ToMenuItems()); - Controls.Add(mainMenu); - - pictureBox.RecreateImage(imageSettings); - pictureBox.Dock = DockStyle.Fill; - Controls.Add(pictureBox); - - DependencyInjector.Inject(actions, pictureBox); - DependencyInjector.Inject(actions, CreateSettingsManager().Load()); - DependencyInjector.Inject(actions, CreateSettingsManager().Load()); - DependencyInjector.Inject(actions, palette); - } - - private static SettingsManager CreateSettingsManager() - { - var container = new StandardKernel(); - container.Bind().To(); - container.Bind().To(); - return container.Get(); - } - - protected override void OnShown(EventArgs e) - { - base.OnShown(e); - Text = "Fractal Painter"; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/App/MainForm.resx b/FractalPainter/Solved/Step07/App/MainForm.resx deleted file mode 100644 index 9d845151a..000000000 --- a/FractalPainter/Solved/Step07/App/MainForm.resx +++ /dev/null @@ -1,123 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 17, 17 - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/App/PictureBoxImageHolder.cs b/FractalPainter/Solved/Step07/App/PictureBoxImageHolder.cs deleted file mode 100644 index 3ce80855e..000000000 --- a/FractalPainter/Solved/Step07/App/PictureBoxImageHolder.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Drawing; -using System.Drawing.Imaging; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step07.App -{ - public class PictureBoxImageHolder : PictureBox, IImageHolder - { - public Size GetImageSize() - { - FailIfNotInitialized(); - return Image.Size; - } - - public Graphics StartDrawing() - { - FailIfNotInitialized(); - return Graphics.FromImage(Image); - } - - private void FailIfNotInitialized() - { - if (Image == null) - throw new InvalidOperationException("Call PictureBoxImageHolder.RecreateImage before other method call!"); - } - - public void UpdateUi() - { - Refresh(); - Application.DoEvents(); - } - - public void RecreateImage(ImageSettings imageSettings) - { - Image = new Bitmap(imageSettings.Width, imageSettings.Height, PixelFormat.Format24bppRgb); - } - - public void SaveImage(string fileName) - { - FailIfNotInitialized(); - Image.Save(fileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/App/Program.cs b/FractalPainter/Solved/Step07/App/Program.cs deleted file mode 100644 index 0dabd4967..000000000 --- a/FractalPainter/Solved/Step07/App/Program.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step07.App.Actions; -using FractalPainting.Solved.Step07.App.Fractals; -using FractalPainting.Solved.Step07.Infrastructure.UiActions; -using Ninject; -using Ninject.Extensions.Factory; - -namespace FractalPainting.Solved.Step07.App -{ - internal static class Program - { - /// - /// The main entry point for the application. - /// - [STAThread] - private static void Main() - { - try - { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - - var container = new StandardKernel(); - - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - - container.Bind().ToSelf() - .InSingletonScope(); - container.Bind() - .To() - .InSingletonScope(); - - container.Bind().ToFactory(); - - var mainForm = container.Get(); - Application.Run(mainForm); - } - catch (Exception e) - { - MessageBox.Show(e.Message); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/App/SettingsManager.cs b/FractalPainter/Solved/Step07/App/SettingsManager.cs deleted file mode 100644 index c887f51d7..000000000 --- a/FractalPainter/Solved/Step07/App/SettingsManager.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step07.App -{ - public class SettingsManager - { - private readonly IObjectSerializer serializer; - private readonly IBlobStorage storage; - private string settingsFilename; - - public SettingsManager(IObjectSerializer serializer, IBlobStorage storage) - { - this.serializer = serializer; - this.storage = storage; - } - - public AppSettings Load() - { - try - { - settingsFilename = "app.settings"; - var data = storage.Get(settingsFilename); - if (data == null) - { - var defaultSettings = CreateDefaultSettings(); - Save(defaultSettings); - return defaultSettings; - } - return serializer.Deserialize(data); - } - catch (Exception e) - { - MessageBox.Show(e.Message, "Не удалось загрузить настройки"); - return CreateDefaultSettings(); - } - } - - private static AppSettings CreateDefaultSettings() - { - return new AppSettings - { - ImagesDirectory = ".", - ImageSettings = new ImageSettings() - }; - } - - public void Save(AppSettings settings) - { - storage.Set(settingsFilename, serializer.Serialize(settings)); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/Infrastructure/Injection/DependencyInjector.cs b/FractalPainter/Solved/Step07/Infrastructure/Injection/DependencyInjector.cs deleted file mode 100644 index bdd65b3f4..000000000 --- a/FractalPainter/Solved/Step07/Infrastructure/Injection/DependencyInjector.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Collections; - -namespace FractalPainting.Solved.Step07.Infrastructure.Injection -{ - public class DependencyInjector - { - public static void Inject(object service, TDependency dependency) - { - var need = service as INeed; - need?.SetDependency(dependency); - } - - public static void Inject(IEnumerable services, TDependency dependency) - { - foreach (var service in services) - Inject(service, dependency); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/Infrastructure/Injection/INeed.cs b/FractalPainter/Solved/Step07/Infrastructure/Injection/INeed.cs deleted file mode 100644 index 97729b8ab..000000000 --- a/FractalPainter/Solved/Step07/Infrastructure/Injection/INeed.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace FractalPainting.Solved.Step07.Infrastructure.Injection -{ - public interface INeed - { - void SetDependency(T dependency); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/Infrastructure/UiActions/IUiAction.cs b/FractalPainter/Solved/Step07/Infrastructure/UiActions/IUiAction.cs deleted file mode 100644 index 7148b4a63..000000000 --- a/FractalPainter/Solved/Step07/Infrastructure/UiActions/IUiAction.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace FractalPainting.Solved.Step07.Infrastructure.UiActions -{ - public interface IUiAction - { - string Category { get; } - string Name { get; } - string Description { get; } - void Perform(); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/Infrastructure/UiActions/UiActionExtensions.cs b/FractalPainter/Solved/Step07/Infrastructure/UiActions/UiActionExtensions.cs deleted file mode 100644 index a1714e147..000000000 --- a/FractalPainter/Solved/Step07/Infrastructure/UiActions/UiActionExtensions.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Windows.Forms; - -namespace FractalPainting.Solved.Step07.Infrastructure.UiActions -{ - public static class UiActionExtensions - { - public static ToolStripItem[] ToMenuItems(this IUiAction[] actions) - { - var items = actions.GroupBy(a => a.Category) - .Select(g => CreateTopLevelMenuItem(g.Key, g.ToList())) - .Cast() - .ToArray(); - return items; - } - - private static ToolStripMenuItem CreateTopLevelMenuItem(string name, IList items) - { - var menuItems = items.Select(a => a.ToMenuItem()).ToArray(); - return new ToolStripMenuItem(name, null, menuItems); - } - - public static ToolStripItem ToMenuItem(this IUiAction action) - { - return - new ToolStripMenuItem(action.Name, null, (sender, args) => action.Perform()) - { - ToolTipText = action.Description, - Tag = action - }; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step07/Step07.csproj b/FractalPainter/Solved/Step07/Step07.csproj deleted file mode 100644 index 1b14d500e..000000000 --- a/FractalPainter/Solved/Step07/Step07.csproj +++ /dev/null @@ -1,21 +0,0 @@ - - - - WinExe - netcoreapp3.1 - true - false - FractalPainting.Solved.Step07 - FractalPainter - - - - - - - - - - - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/App/Actions/DragonFractalAction.cs b/FractalPainter/Solved/Step08/App/Actions/DragonFractalAction.cs deleted file mode 100644 index ec662949c..000000000 --- a/FractalPainter/Solved/Step08/App/Actions/DragonFractalAction.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step08.App.Fractals; -using FractalPainting.Solved.Step08.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step08.App.Actions -{ - public class DragonFractalAction : IUiAction - { - private readonly IDragonPainterFactory dragonPainterFactory; - private readonly Func createDragonSettingsGenerator; - - public DragonFractalAction(IDragonPainterFactory dragonPainterFactory, - Func createDragonSettingsGenerator) - { - this.dragonPainterFactory = dragonPainterFactory; - this.createDragonSettingsGenerator = createDragonSettingsGenerator; - } - - public string Category => "Фракталы"; - public string Name => "Дракон"; - public string Description => "Дракон Хартера-Хейтуэя"; - - public void Perform() - { - var dragonSettings = CreateRandomSettings(); - // редактируем настройки: - SettingsForm.For(dragonSettings).ShowDialog(); - // создаём painter с такими настройками - dragonPainterFactory.Create(dragonSettings).Paint(); - } - - private DragonSettings CreateRandomSettings() - { - return createDragonSettingsGenerator(new Random()).Generate(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/App/Actions/ImageSettingsAction.cs b/FractalPainter/Solved/Step08/App/Actions/ImageSettingsAction.cs deleted file mode 100644 index c910d5974..000000000 --- a/FractalPainter/Solved/Step08/App/Actions/ImageSettingsAction.cs +++ /dev/null @@ -1,28 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step08.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step08.App.Actions -{ - public class ImageSettingsAction : IUiAction - { - private readonly IImageHolder imageHolder; - private readonly ImageSettings imageSettings; - - public ImageSettingsAction(IImageHolder imageHolder, - ImageSettings imageSettings) - { - this.imageHolder = imageHolder; - this.imageSettings = imageSettings; - } - - public string Category => "Настройки"; - public string Name => "Изображение..."; - public string Description => "Размеры изображения"; - - public void Perform() - { - SettingsForm.For(imageSettings).ShowDialog(); - imageHolder.RecreateImage(imageSettings); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/App/Actions/KochFractalAction.cs b/FractalPainter/Solved/Step08/App/Actions/KochFractalAction.cs deleted file mode 100644 index 2884ae4a0..000000000 --- a/FractalPainter/Solved/Step08/App/Actions/KochFractalAction.cs +++ /dev/null @@ -1,24 +0,0 @@ -using FractalPainting.Solved.Step08.App.Fractals; -using FractalPainting.Solved.Step08.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step08.App.Actions -{ - public class KochFractalAction : IUiAction - { - private readonly KochPainter kochPainter; - - public KochFractalAction(KochPainter kochPainter) - { - this.kochPainter = kochPainter; - } - - public string Category => "Фракталы"; - public string Name => "Кривая Коха"; - public string Description => "Кривая Коха"; - - public void Perform() - { - kochPainter.Paint(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/App/Actions/PaletteSettingsAction.cs b/FractalPainter/Solved/Step08/App/Actions/PaletteSettingsAction.cs deleted file mode 100644 index eca05f5c6..000000000 --- a/FractalPainter/Solved/Step08/App/Actions/PaletteSettingsAction.cs +++ /dev/null @@ -1,25 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step08.Infrastructure.Injection; -using FractalPainting.Solved.Step08.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step08.App.Actions -{ - public class PaletteSettingsAction : IUiAction, INeed - { - private Palette palette; - - public void SetDependency(Palette dependency) - { - palette = dependency; - } - - public string Category => "Настройки"; - public string Name => "Палитра..."; - public string Description => "Цвета для рисования фракталов"; - - public void Perform() - { - SettingsForm.For(palette).ShowDialog(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/App/Actions/SaveImageAction.cs b/FractalPainter/Solved/Step08/App/Actions/SaveImageAction.cs deleted file mode 100644 index 2084c4233..000000000 --- a/FractalPainter/Solved/Step08/App/Actions/SaveImageAction.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.IO; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step08.Infrastructure.Injection; -using FractalPainting.Solved.Step08.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step08.App.Actions -{ - public class SaveImageAction : IUiAction, INeed, INeed - { - private IImageDirectoryProvider imageDirectoryProvider; - private IImageHolder imageHolder; - - public void SetDependency(IImageDirectoryProvider dependency) - { - imageDirectoryProvider = dependency; - } - - public void SetDependency(IImageHolder dependency) - { - imageHolder = dependency; - } - - public string Category => "Файл"; - public string Name => "Сохранить..."; - public string Description => "Сохранить изображение в файл"; - - public void Perform() - { - var dialog = new SaveFileDialog - { - CheckFileExists = false, - InitialDirectory = Path.GetFullPath(imageDirectoryProvider.ImagesDirectory), - DefaultExt = "bmp", - FileName = "image.bmp", - Filter = "Изображения (*.bmp)|*.bmp" - }; - var res = dialog.ShowDialog(); - if (res == DialogResult.OK) - imageHolder.SaveImage(dialog.FileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/App/AppSettings.cs b/FractalPainter/Solved/Step08/App/AppSettings.cs deleted file mode 100644 index 9f34426d9..000000000 --- a/FractalPainter/Solved/Step08/App/AppSettings.cs +++ /dev/null @@ -1,10 +0,0 @@ -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step08.App -{ - public class AppSettings : IImageDirectoryProvider//, IImageSettingsProvider - { - public string ImagesDirectory { get; set; } - public ImageSettings ImageSettings { get; set; } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/App/Fractals/DragonPainter.cs b/FractalPainter/Solved/Step08/App/Fractals/DragonPainter.cs deleted file mode 100644 index 7eb9a0ed0..000000000 --- a/FractalPainter/Solved/Step08/App/Fractals/DragonPainter.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Drawing; -using System.Linq; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step08.App.Fractals -{ - public class DragonPainter - { - private readonly IImageHolder imageHolder; - private readonly DragonSettings settings; - private readonly Palette palette; - private readonly float size; - private Size imageSize; - - public DragonPainter(IImageHolder imageHolder, - DragonSettings settings, Palette palette) - { - this.imageHolder = imageHolder; - this.settings = settings; - this.palette = palette; - imageSize = imageHolder.GetImageSize(); - size = Math.Min(imageSize.Width, imageSize.Height)/2.1f; - } - - public void Paint() - { - using (var graphics = imageHolder.StartDrawing()) - { - graphics.FillRectangle(new SolidBrush(palette.BackgroundColor), 0, 0, imageSize.Width, imageSize.Height); - var r = new Random(); - var cosa = (float) Math.Cos(settings.Angle1); - var sina = (float) Math.Sin(settings.Angle1); - var cosb = (float) Math.Cos(settings.Angle2); - var sinb = (float) Math.Sin(settings.Angle2); - var shiftX = settings.ShiftX*size*0.8f; - var shiftY = settings.ShiftY*size*0.8f; - var scale = settings.Scale; - var p = new PointF(0, 0); - var pointBrush = new SolidBrush(palette.PrimaryColor); - foreach (var i in Enumerable.Range(0, settings.IterationsCount)) - { - graphics.FillRectangle(pointBrush, imageSize.Width/3f + p.X, imageSize.Height/2f + p.Y, 1, 1); - if (r.Next(0, 2) == 0) - p = new PointF(scale*(p.X*cosa - p.Y*sina), scale*(p.X*sina + p.Y*cosa)); - else - p = new PointF(scale*(p.X*cosb - p.Y*sinb) + shiftX, scale*(p.X*sinb + p.Y*cosb) + shiftY); - if (i%100 == 0) imageHolder.UpdateUi(); - } - } - imageHolder.UpdateUi(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/App/Fractals/DragonSettings.cs b/FractalPainter/Solved/Step08/App/Fractals/DragonSettings.cs deleted file mode 100644 index 0e468942c..000000000 --- a/FractalPainter/Solved/Step08/App/Fractals/DragonSettings.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step08.App.Fractals -{ - public class DragonSettings - { - public double Angle1 { get; set; } = Math.PI / 4; - public double Angle2 { get; set; } = 3 * Math.PI / 4; - public float ShiftX { get; set; } = 1; - public float ShiftY { get; set; } = 0; - public float Scale { get; set; } = (float)(1 / Math.Sqrt(2)); - public int IterationsCount { get; set; } = 20000; - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/App/Fractals/DragonSettingsGenerator.cs b/FractalPainter/Solved/Step08/App/Fractals/DragonSettingsGenerator.cs deleted file mode 100644 index 39257a692..000000000 --- a/FractalPainter/Solved/Step08/App/Fractals/DragonSettingsGenerator.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step08.App.Fractals -{ - public class DragonSettingsGenerator - { - private readonly Random random; - - public DragonSettingsGenerator(Random random) - { - this.random = random; - } - - public DragonSettings Generate() - { - return new DragonSettings - { - Angle1 = random.NextDouble() / 2 + 0.5, - Angle2 = 3 * (random.NextDouble() / 2 + 0.5), - ShiftX = 1, - ShiftY = 0, - Scale = (float)(1/Math.Sqrt(2) + (random.NextDouble() - 0.5)/10) - }; - } - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/App/Fractals/IDragonPainterFactory.cs b/FractalPainter/Solved/Step08/App/Fractals/IDragonPainterFactory.cs deleted file mode 100644 index d9cc5f94a..000000000 --- a/FractalPainter/Solved/Step08/App/Fractals/IDragonPainterFactory.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace FractalPainting.Solved.Step08.App.Fractals -{ - public interface IDragonPainterFactory - { - DragonPainter Create(DragonSettings settings); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/App/Fractals/KochPainter.cs b/FractalPainter/Solved/Step08/App/Fractals/KochPainter.cs deleted file mode 100644 index 289f1eef4..000000000 --- a/FractalPainter/Solved/Step08/App/Fractals/KochPainter.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Drawing; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step08.App.Fractals -{ - public class KochPainter - { - private readonly IImageHolder imageHolder; - private readonly Palette palette; - - public KochPainter(IImageHolder imageHolder, Palette palette) - { - this.imageHolder = imageHolder; - this.palette = palette; - } - - public void Paint() - { - var imageSize = imageHolder.GetImageSize(); - using (var graphics = imageHolder.StartDrawing()) - using (var backgroundBrush = new SolidBrush(palette.BackgroundColor)) - { - graphics.FillRectangle(backgroundBrush, 0, 0, imageSize.Width, imageSize.Height); - DrawSegment(graphics, 0, imageSize.Height * 0.9f, imageSize.Width, imageSize.Height * 0.9f, true); - } - imageHolder.UpdateUi(); - } - - private void DrawSegment(Graphics graphics, float x0, float y0, float x1, float y1, bool primaryColor) - { - var len2 = (x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1); - if (len2 < 4) - { - if (y0 < 0 || y1 < 0) return; - using (var penBrush = new SolidBrush(primaryColor ? palette.PrimaryColor : palette.SecondaryColor)) - { - var pen = new Pen(penBrush, 3); - graphics.DrawLine(pen, x0, y0, x1, y1); - } - } - else - { - var vx = (x1 - x0)/3; - var vy = (y1 - y0)/3; - DrawSegment(graphics, x0, y0, x0 + vx, y0 + vy, primaryColor); - var k = (float) Math.Sqrt(3)/2f; - var px = (x0 + x1)/2 + vy*k; - var py = (y0 + y1)/2 - vx*k; - DrawSegment(graphics, x0 + vx, y0 + vy, px, py, !primaryColor); - DrawSegment(graphics, px, py, x0 + 2*vx, y0 + 2*vy, !primaryColor); - DrawSegment(graphics, x0 + 2*vx, y0 + 2*vy, x1, y1, primaryColor); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/App/MainForm.cs b/FractalPainter/Solved/Step08/App/MainForm.cs deleted file mode 100644 index 40662f7ec..000000000 --- a/FractalPainter/Solved/Step08/App/MainForm.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using System.Drawing; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step08.Infrastructure.Injection; -using FractalPainting.Solved.Step08.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step08.App -{ - public class MainForm : Form - { - public MainForm(IUiAction[] actions, - PictureBoxImageHolder pictureBox, - Palette palette, - ImageSettings imageSettings, - IImageDirectoryProvider imageDirectoryProvider) - { - ClientSize = new Size(imageSettings.Width, imageSettings.Height); - - var mainMenu = new MenuStrip(); - mainMenu.Items.AddRange(actions.ToMenuItems()); - Controls.Add(mainMenu); - - pictureBox.RecreateImage(imageSettings); - pictureBox.Dock = DockStyle.Fill; - Controls.Add(pictureBox); - - DependencyInjector.Inject(actions, pictureBox); - DependencyInjector.Inject(actions, imageDirectoryProvider); - DependencyInjector.Inject(actions, palette); - } - - protected override void OnShown(EventArgs e) - { - base.OnShown(e); - Text = "Fractal Painter"; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/App/MainForm.resx b/FractalPainter/Solved/Step08/App/MainForm.resx deleted file mode 100644 index 9d845151a..000000000 --- a/FractalPainter/Solved/Step08/App/MainForm.resx +++ /dev/null @@ -1,123 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 17, 17 - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/App/PictureBoxImageHolder.cs b/FractalPainter/Solved/Step08/App/PictureBoxImageHolder.cs deleted file mode 100644 index a475a16d0..000000000 --- a/FractalPainter/Solved/Step08/App/PictureBoxImageHolder.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Drawing; -using System.Drawing.Imaging; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step08.App -{ - public class PictureBoxImageHolder : PictureBox, IImageHolder - { - public Size GetImageSize() - { - FailIfNotInitialized(); - return Image.Size; - } - - public Graphics StartDrawing() - { - FailIfNotInitialized(); - return Graphics.FromImage(Image); - } - - private void FailIfNotInitialized() - { - if (Image == null) - throw new InvalidOperationException("Call PictureBoxImageHolder.RecreateImage before other method call!"); - } - - public void UpdateUi() - { - Refresh(); - Application.DoEvents(); - } - - public void RecreateImage(ImageSettings imageSettings) - { - Image = new Bitmap(imageSettings.Width, imageSettings.Height, PixelFormat.Format24bppRgb); - } - - public void SaveImage(string fileName) - { - FailIfNotInitialized(); - Image.Save(fileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/App/Program.cs b/FractalPainter/Solved/Step08/App/Program.cs deleted file mode 100644 index 1f69ea582..000000000 --- a/FractalPainter/Solved/Step08/App/Program.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step08.App.Actions; -using FractalPainting.Solved.Step08.App.Fractals; -using FractalPainting.Solved.Step08.Infrastructure.UiActions; -using Ninject; -using Ninject.Extensions.Factory; - -namespace FractalPainting.Solved.Step08.App -{ - internal static class Program - { - /// - /// The main entry point for the application. - /// - [STAThread] - private static void Main() - { - try - { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - - var container = new StandardKernel(); - - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - - container.Bind().ToSelf() - .InSingletonScope(); - container.Bind() - .To() - .InSingletonScope(); - - container.Bind().To() - .WhenInjectedInto(); - container.Bind().To() - .WhenInjectedInto(); - container.Bind() - .ToMethod(context => context.Kernel.Get().Load()) - .InSingletonScope(); - container.Bind() - .ToMethod(context => context.Kernel.Get().ImageSettings) - .InSingletonScope(); - - container.Bind().ToFactory(); - - var mainForm = container.Get(); - Application.Run(mainForm); - } - catch (Exception e) - { - MessageBox.Show(e.Message); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/App/SettingsManager.cs b/FractalPainter/Solved/Step08/App/SettingsManager.cs deleted file mode 100644 index 1c20cf68f..000000000 --- a/FractalPainter/Solved/Step08/App/SettingsManager.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step08.App -{ - public class SettingsManager - { - private readonly IObjectSerializer serializer; - private readonly IBlobStorage storage; - private string settingsFilename; - - public SettingsManager(IObjectSerializer serializer, IBlobStorage storage) - { - this.serializer = serializer; - this.storage = storage; - } - - public AppSettings Load() - { - try - { - settingsFilename = "app.settings"; - var data = storage.Get(settingsFilename); - if (data == null) - { - var defaultSettings = CreateDefaultSettings(); - Save(defaultSettings); - return defaultSettings; - } - return serializer.Deserialize(data); - } - catch (Exception e) - { - MessageBox.Show(e.Message, "Не удалось загрузить настройки"); - return CreateDefaultSettings(); - } - } - - private static AppSettings CreateDefaultSettings() - { - return new AppSettings - { - ImagesDirectory = ".", - ImageSettings = new ImageSettings() - }; - } - - public void Save(AppSettings settings) - { - storage.Set(settingsFilename, serializer.Serialize(settings)); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/Infrastructure/Injection/DependencyInjector.cs b/FractalPainter/Solved/Step08/Infrastructure/Injection/DependencyInjector.cs deleted file mode 100644 index e25c7a005..000000000 --- a/FractalPainter/Solved/Step08/Infrastructure/Injection/DependencyInjector.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Collections; - -namespace FractalPainting.Solved.Step08.Infrastructure.Injection -{ - public class DependencyInjector - { - public static void Inject(object service, TDependency dependency) - { - var need = service as INeed; - need?.SetDependency(dependency); - } - - public static void Inject(IEnumerable services, TDependency dependency) - { - foreach (var service in services) - Inject(service, dependency); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/Infrastructure/Injection/INeed.cs b/FractalPainter/Solved/Step08/Infrastructure/Injection/INeed.cs deleted file mode 100644 index c67d54e27..000000000 --- a/FractalPainter/Solved/Step08/Infrastructure/Injection/INeed.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace FractalPainting.Solved.Step08.Infrastructure.Injection -{ - public interface INeed - { - void SetDependency(T dependency); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/Infrastructure/UiActions/IUiAction.cs b/FractalPainter/Solved/Step08/Infrastructure/UiActions/IUiAction.cs deleted file mode 100644 index 0393088f8..000000000 --- a/FractalPainter/Solved/Step08/Infrastructure/UiActions/IUiAction.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace FractalPainting.Solved.Step08.Infrastructure.UiActions -{ - public interface IUiAction - { - string Category { get; } - string Name { get; } - string Description { get; } - void Perform(); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/Infrastructure/UiActions/UiActionExtensions.cs b/FractalPainter/Solved/Step08/Infrastructure/UiActions/UiActionExtensions.cs deleted file mode 100644 index 3f3a784bd..000000000 --- a/FractalPainter/Solved/Step08/Infrastructure/UiActions/UiActionExtensions.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Windows.Forms; - -namespace FractalPainting.Solved.Step08.Infrastructure.UiActions -{ - public static class UiActionExtensions - { - public static ToolStripItem[] ToMenuItems(this IUiAction[] actions) - { - var items = actions.GroupBy(a => a.Category) - .Select(g => CreateTopLevelMenuItem(g.Key, g.ToList())) - .Cast() - .ToArray(); - return items; - } - - private static ToolStripMenuItem CreateTopLevelMenuItem(string name, IList items) - { - var menuItems = items.Select(a => a.ToMenuItem()).ToArray(); - return new ToolStripMenuItem(name, null, menuItems); - } - - public static ToolStripItem ToMenuItem(this IUiAction action) - { - return - new ToolStripMenuItem(action.Name, null, (sender, args) => action.Perform()) - { - ToolTipText = action.Description, - Tag = action - }; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step08/Step08.csproj b/FractalPainter/Solved/Step08/Step08.csproj deleted file mode 100644 index f460f4e9f..000000000 --- a/FractalPainter/Solved/Step08/Step08.csproj +++ /dev/null @@ -1,21 +0,0 @@ - - - - WinExe - netcoreapp3.1 - true - false - FractalPainting.Solved.Step08 - FractalPainter - - - - - - - - - - - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/App/Actions/DragonFractalAction.cs b/FractalPainter/Solved/Step09/App/Actions/DragonFractalAction.cs deleted file mode 100644 index f8581fe2f..000000000 --- a/FractalPainter/Solved/Step09/App/Actions/DragonFractalAction.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step09.App.Fractals; -using FractalPainting.Solved.Step09.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step09.App.Actions -{ - public class DragonFractalAction : IUiAction - { - private readonly IDragonPainterFactory dragonPainterFactory; - private readonly Func createDragonSettingsGenerator; - - public DragonFractalAction(IDragonPainterFactory dragonPainterFactory, - Func createDragonSettingsGenerator) - { - this.dragonPainterFactory = dragonPainterFactory; - this.createDragonSettingsGenerator = createDragonSettingsGenerator; - } - - public string Category => "Фракталы"; - public string Name => "Дракон"; - public string Description => "Дракон Хартера-Хейтуэя"; - - public void Perform() - { - var dragonSettings = CreateRandomSettings(); - // редактируем настройки: - SettingsForm.For(dragonSettings).ShowDialog(); - // создаём painter с такими настройками - dragonPainterFactory.Create(dragonSettings).Paint(); - } - - private DragonSettings CreateRandomSettings() - { - return createDragonSettingsGenerator(new Random()).Generate(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/App/Actions/ImageSettingsAction.cs b/FractalPainter/Solved/Step09/App/Actions/ImageSettingsAction.cs deleted file mode 100644 index 6e3746573..000000000 --- a/FractalPainter/Solved/Step09/App/Actions/ImageSettingsAction.cs +++ /dev/null @@ -1,28 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step09.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step09.App.Actions -{ - public class ImageSettingsAction : IUiAction - { - private readonly IImageHolder imageHolder; - private readonly ImageSettings imageSettings; - - public ImageSettingsAction(IImageHolder imageHolder, - ImageSettings imageSettings) - { - this.imageHolder = imageHolder; - this.imageSettings = imageSettings; - } - - public string Category => "Настройки"; - public string Name => "Изображение..."; - public string Description => "Размеры изображения"; - - public void Perform() - { - SettingsForm.For(imageSettings).ShowDialog(); - imageHolder.RecreateImage(imageSettings); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/App/Actions/KochFractalAction.cs b/FractalPainter/Solved/Step09/App/Actions/KochFractalAction.cs deleted file mode 100644 index 0a826523a..000000000 --- a/FractalPainter/Solved/Step09/App/Actions/KochFractalAction.cs +++ /dev/null @@ -1,24 +0,0 @@ -using FractalPainting.Solved.Step09.App.Fractals; -using FractalPainting.Solved.Step09.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step09.App.Actions -{ - public class KochFractalAction : IUiAction - { - private readonly KochPainter kochPainter; - - public KochFractalAction(KochPainter kochPainter) - { - this.kochPainter = kochPainter; - } - - public string Category => "Фракталы"; - public string Name => "Кривая Коха"; - public string Description => "Кривая Коха"; - - public void Perform() - { - kochPainter.Paint(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/App/Actions/PaletteSettingsAction.cs b/FractalPainter/Solved/Step09/App/Actions/PaletteSettingsAction.cs deleted file mode 100644 index 8e1fd6b53..000000000 --- a/FractalPainter/Solved/Step09/App/Actions/PaletteSettingsAction.cs +++ /dev/null @@ -1,24 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step09.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step09.App.Actions -{ - public class PaletteSettingsAction : IUiAction - { - private readonly Palette palette; - - public PaletteSettingsAction(Palette palette) - { - this.palette = palette; - } - - public string Category => "Настройки"; - public string Name => "Палитра..."; - public string Description => "Цвета для рисования фракталов"; - - public void Perform() - { - SettingsForm.For(palette).ShowDialog(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/App/Actions/SaveImageAction.cs b/FractalPainter/Solved/Step09/App/Actions/SaveImageAction.cs deleted file mode 100644 index b2a71b374..000000000 --- a/FractalPainter/Solved/Step09/App/Actions/SaveImageAction.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System.IO; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step09.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step09.App.Actions -{ - public class SaveImageAction : IUiAction - { - private readonly IImageDirectoryProvider imageDirectoryProvider; - private readonly IImageHolder imageHolder; - - public SaveImageAction(IImageDirectoryProvider imageDirectoryProvider, IImageHolder imageHolder) - { - this.imageDirectoryProvider = imageDirectoryProvider; - this.imageHolder = imageHolder; - } - - public string Category => "Файл"; - public string Name => "Сохранить..."; - public string Description => "Сохранить изображение в файл"; - - public void Perform() - { - var dialog = new SaveFileDialog - { - CheckFileExists = false, - InitialDirectory = Path.GetFullPath(imageDirectoryProvider.ImagesDirectory), - DefaultExt = "bmp", - FileName = "image.bmp", - Filter = "Изображения (*.bmp)|*.bmp" - }; - var res = dialog.ShowDialog(); - if (res == DialogResult.OK) - imageHolder.SaveImage(dialog.FileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/App/AppSettings.cs b/FractalPainter/Solved/Step09/App/AppSettings.cs deleted file mode 100644 index 81399af6c..000000000 --- a/FractalPainter/Solved/Step09/App/AppSettings.cs +++ /dev/null @@ -1,10 +0,0 @@ -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step09.App -{ - public class AppSettings : IImageDirectoryProvider, IImageSettingsProvider - { - public string ImagesDirectory { get; set; } - public ImageSettings ImageSettings { get; set; } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/App/Fractals/DragonPainter.cs b/FractalPainter/Solved/Step09/App/Fractals/DragonPainter.cs deleted file mode 100644 index 97ccffde0..000000000 --- a/FractalPainter/Solved/Step09/App/Fractals/DragonPainter.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Drawing; -using System.Linq; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step09.App.Fractals -{ - public class DragonPainter - { - private readonly IImageHolder imageHolder; - private readonly DragonSettings settings; - private readonly Palette palette; - private readonly float size; - private Size imageSize; - - public DragonPainter(IImageHolder imageHolder, - DragonSettings settings, Palette palette) - { - this.imageHolder = imageHolder; - this.settings = settings; - this.palette = palette; - imageSize = imageHolder.GetImageSize(); - size = Math.Min(imageSize.Width, imageSize.Height)/2.1f; - } - - public void Paint() - { - using (var graphics = imageHolder.StartDrawing()) - { - graphics.FillRectangle(new SolidBrush(palette.BackgroundColor), 0, 0, imageSize.Width, imageSize.Height); - var r = new Random(); - var cosa = (float) Math.Cos(settings.Angle1); - var sina = (float) Math.Sin(settings.Angle1); - var cosb = (float) Math.Cos(settings.Angle2); - var sinb = (float) Math.Sin(settings.Angle2); - var shiftX = settings.ShiftX*size*0.8f; - var shiftY = settings.ShiftY*size*0.8f; - var scale = settings.Scale; - var p = new PointF(0, 0); - var pointBrush = new SolidBrush(palette.PrimaryColor); - foreach (var i in Enumerable.Range(0, settings.IterationsCount)) - { - graphics.FillRectangle(pointBrush, imageSize.Width/3f + p.X, imageSize.Height/2f + p.Y, 1, 1); - if (r.Next(0, 2) == 0) - p = new PointF(scale*(p.X*cosa - p.Y*sina), scale*(p.X*sina + p.Y*cosa)); - else - p = new PointF(scale*(p.X*cosb - p.Y*sinb) + shiftX, scale*(p.X*sinb + p.Y*cosb) + shiftY); - if (i%100 == 0) imageHolder.UpdateUi(); - } - } - imageHolder.UpdateUi(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/App/Fractals/DragonSettings.cs b/FractalPainter/Solved/Step09/App/Fractals/DragonSettings.cs deleted file mode 100644 index 52a6d0662..000000000 --- a/FractalPainter/Solved/Step09/App/Fractals/DragonSettings.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step09.App.Fractals -{ - public class DragonSettings - { - public double Angle1 { get; set; } = Math.PI / 4; - public double Angle2 { get; set; } = 3 * Math.PI / 4; - public float ShiftX { get; set; } = 1; - public float ShiftY { get; set; } = 0; - public float Scale { get; set; } = (float)(1 / Math.Sqrt(2)); - public int IterationsCount { get; set; } = 20000; - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/App/Fractals/DragonSettingsGenerator.cs b/FractalPainter/Solved/Step09/App/Fractals/DragonSettingsGenerator.cs deleted file mode 100644 index 4ba1e33bc..000000000 --- a/FractalPainter/Solved/Step09/App/Fractals/DragonSettingsGenerator.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step09.App.Fractals -{ - public class DragonSettingsGenerator - { - private readonly Random random; - - public DragonSettingsGenerator(Random random) - { - this.random = random; - } - - public DragonSettings Generate() - { - return new DragonSettings - { - Angle1 = random.NextDouble() / 2 + 0.5, - Angle2 = 3 * (random.NextDouble() / 2 + 0.5), - ShiftX = 1, - ShiftY = 0, - Scale = (float)(1/Math.Sqrt(2) + (random.NextDouble() - 0.5)/10) - }; - } - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/App/Fractals/IDragonPainterFactory.cs b/FractalPainter/Solved/Step09/App/Fractals/IDragonPainterFactory.cs deleted file mode 100644 index f0d6db8fc..000000000 --- a/FractalPainter/Solved/Step09/App/Fractals/IDragonPainterFactory.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace FractalPainting.Solved.Step09.App.Fractals -{ - public interface IDragonPainterFactory - { - DragonPainter Create(DragonSettings settings); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/App/Fractals/KochPainter.cs b/FractalPainter/Solved/Step09/App/Fractals/KochPainter.cs deleted file mode 100644 index f7fecc3f3..000000000 --- a/FractalPainter/Solved/Step09/App/Fractals/KochPainter.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Drawing; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step09.App.Fractals -{ - public class KochPainter - { - private readonly IImageHolder imageHolder; - private readonly Palette palette; - - public KochPainter(IImageHolder imageHolder, Palette palette) - { - this.imageHolder = imageHolder; - this.palette = palette; - } - - public void Paint() - { - var imageSize = imageHolder.GetImageSize(); - using (var graphics = imageHolder.StartDrawing()) - using (var backgroundBrush = new SolidBrush(palette.BackgroundColor)) - { - graphics.FillRectangle(backgroundBrush, 0, 0, imageSize.Width, imageSize.Height); - DrawSegment(graphics, 0, imageSize.Height * 0.9f, imageSize.Width, imageSize.Height * 0.9f, true); - } - imageHolder.UpdateUi(); - } - - private void DrawSegment(Graphics graphics, float x0, float y0, float x1, float y1, bool primaryColor) - { - var len2 = (x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1); - if (len2 < 4) - { - if (y0 < 0 || y1 < 0) return; - using (var penBrush = new SolidBrush(primaryColor ? palette.PrimaryColor : palette.SecondaryColor)) - { - var pen = new Pen(penBrush, 3); - graphics.DrawLine(pen, x0, y0, x1, y1); - } - } - else - { - var vx = (x1 - x0)/3; - var vy = (y1 - y0)/3; - DrawSegment(graphics, x0, y0, x0 + vx, y0 + vy, primaryColor); - var k = (float) Math.Sqrt(3)/2f; - var px = (x0 + x1)/2 + vy*k; - var py = (y0 + y1)/2 - vx*k; - DrawSegment(graphics, x0 + vx, y0 + vy, px, py, !primaryColor); - DrawSegment(graphics, px, py, x0 + 2*vx, y0 + 2*vy, !primaryColor); - DrawSegment(graphics, x0 + 2*vx, y0 + 2*vy, x1, y1, primaryColor); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/App/MainForm.cs b/FractalPainter/Solved/Step09/App/MainForm.cs deleted file mode 100644 index 23ab42d31..000000000 --- a/FractalPainter/Solved/Step09/App/MainForm.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Drawing; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step09.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step09.App -{ - public class MainForm : Form - { - public MainForm(IUiAction[] actions, - PictureBoxImageHolder pictureBox, - ImageSettings imageSettings) - { - ClientSize = new Size(imageSettings.Width, imageSettings.Height); - - var mainMenu = new MenuStrip(); - mainMenu.Items.AddRange(actions.ToMenuItems()); - Controls.Add(mainMenu); - - pictureBox.RecreateImage(imageSettings); - pictureBox.Dock = DockStyle.Fill; - Controls.Add(pictureBox); - } - - protected override void OnShown(EventArgs e) - { - base.OnShown(e); - Text = "Fractal Painter"; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/App/MainForm.resx b/FractalPainter/Solved/Step09/App/MainForm.resx deleted file mode 100644 index 9d845151a..000000000 --- a/FractalPainter/Solved/Step09/App/MainForm.resx +++ /dev/null @@ -1,123 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 17, 17 - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/App/PictureBoxImageHolder.cs b/FractalPainter/Solved/Step09/App/PictureBoxImageHolder.cs deleted file mode 100644 index c5557f1d8..000000000 --- a/FractalPainter/Solved/Step09/App/PictureBoxImageHolder.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Drawing; -using System.Drawing.Imaging; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step09.App -{ - public class PictureBoxImageHolder : PictureBox, IImageHolder - { - public Size GetImageSize() - { - FailIfNotInitialized(); - return Image.Size; - } - - public Graphics StartDrawing() - { - FailIfNotInitialized(); - return Graphics.FromImage(Image); - } - - private void FailIfNotInitialized() - { - if (Image == null) - throw new InvalidOperationException("Call PictureBoxImageHolder.RecreateImage before other method call!"); - } - - public void UpdateUi() - { - Refresh(); - Application.DoEvents(); - } - - public void RecreateImage(ImageSettings imageSettings) - { - Image = new Bitmap(imageSettings.Width, imageSettings.Height, PixelFormat.Format24bppRgb); - } - - public void SaveImage(string fileName) - { - FailIfNotInitialized(); - Image.Save(fileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/App/Program.cs b/FractalPainter/Solved/Step09/App/Program.cs deleted file mode 100644 index 0ad7082b6..000000000 --- a/FractalPainter/Solved/Step09/App/Program.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step09.App.Actions; -using FractalPainting.Solved.Step09.App.Fractals; -using FractalPainting.Solved.Step09.Infrastructure.UiActions; -using Ninject; -using Ninject.Extensions.Factory; - -namespace FractalPainting.Solved.Step09.App -{ - internal static class Program - { - /// - /// The main entry point for the application. - /// - [STAThread] - private static void Main() - { - try - { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - - var container = new StandardKernel(); - - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - container.Bind().To(); - - container.Bind().ToSelf() - .InSingletonScope(); - container.Bind() - .To() - .InSingletonScope(); - - container.Bind().To() - .WhenInjectedInto(); - container.Bind().To() - .WhenInjectedInto(); - container.Bind() - .ToMethod(context => context.Kernel.Get().Load()) - .InSingletonScope(); - container.Bind() - .ToMethod(context => context.Kernel.Get().ImageSettings) - .InSingletonScope(); - - container.Bind().ToFactory(); - - var mainForm = container.Get(); - Application.Run(mainForm); - } - catch (Exception e) - { - MessageBox.Show(e.Message); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/App/SettingsManager.cs b/FractalPainter/Solved/Step09/App/SettingsManager.cs deleted file mode 100644 index b687d75f8..000000000 --- a/FractalPainter/Solved/Step09/App/SettingsManager.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step09.App -{ - public class SettingsManager - { - private readonly IObjectSerializer serializer; - private readonly IBlobStorage storage; - private string settingsFilename; - - public SettingsManager(IObjectSerializer serializer, IBlobStorage storage) - { - this.serializer = serializer; - this.storage = storage; - } - - public AppSettings Load() - { - try - { - settingsFilename = "app.settings"; - var data = storage.Get(settingsFilename); - if (data == null) - { - var defaultSettings = CreateDefaultSettings(); - Save(defaultSettings); - return defaultSettings; - } - return serializer.Deserialize(data); - } - catch (Exception e) - { - MessageBox.Show(e.Message, "Не удалось загрузить настройки"); - return CreateDefaultSettings(); - } - } - - private static AppSettings CreateDefaultSettings() - { - return new AppSettings - { - ImagesDirectory = ".", - ImageSettings = new ImageSettings() - }; - } - - public void Save(AppSettings settings) - { - storage.Set(settingsFilename, serializer.Serialize(settings)); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/Infrastructure/UiActions/IUiAction.cs b/FractalPainter/Solved/Step09/Infrastructure/UiActions/IUiAction.cs deleted file mode 100644 index 60f2ce50a..000000000 --- a/FractalPainter/Solved/Step09/Infrastructure/UiActions/IUiAction.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace FractalPainting.Solved.Step09.Infrastructure.UiActions -{ - public interface IUiAction - { - string Category { get; } - string Name { get; } - string Description { get; } - void Perform(); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/Infrastructure/UiActions/UiActionExtensions.cs b/FractalPainter/Solved/Step09/Infrastructure/UiActions/UiActionExtensions.cs deleted file mode 100644 index 0dea9998e..000000000 --- a/FractalPainter/Solved/Step09/Infrastructure/UiActions/UiActionExtensions.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Windows.Forms; - -namespace FractalPainting.Solved.Step09.Infrastructure.UiActions -{ - public static class UiActionExtensions - { - public static ToolStripItem[] ToMenuItems(this IUiAction[] actions) - { - var items = actions.GroupBy(a => a.Category) - .Select(g => CreateTopLevelMenuItem(g.Key, g.ToList())) - .Cast() - .ToArray(); - return items; - } - - private static ToolStripMenuItem CreateTopLevelMenuItem(string name, IList items) - { - var menuItems = items.Select(a => a.ToMenuItem()).ToArray(); - return new ToolStripMenuItem(name, null, menuItems); - } - - public static ToolStripItem ToMenuItem(this IUiAction action) - { - return - new ToolStripMenuItem(action.Name, null, (sender, args) => action.Perform()) - { - ToolTipText = action.Description, - Tag = action - }; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step09/Step09.csproj b/FractalPainter/Solved/Step09/Step09.csproj deleted file mode 100644 index bff91e04a..000000000 --- a/FractalPainter/Solved/Step09/Step09.csproj +++ /dev/null @@ -1,21 +0,0 @@ - - - - WinExe - netcoreapp3.1 - true - false - FractalPainting.Solved.Step09 - FractalPainter - - - - - - - - - - - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/App/Actions/DragonFractalAction.cs b/FractalPainter/Solved/Step10/App/Actions/DragonFractalAction.cs deleted file mode 100644 index f2230e719..000000000 --- a/FractalPainter/Solved/Step10/App/Actions/DragonFractalAction.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using FractalPainting.Solved.Step10.App.Fractals; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step10.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step10.App.Actions -{ - public class DragonFractalAction : IUiAction - { - private readonly IDragonPainterFactory dragonPainterFactory; - private readonly Func createDragonSettingsGenerator; - - public DragonFractalAction(IDragonPainterFactory dragonPainterFactory, - Func createDragonSettingsGenerator) - { - this.dragonPainterFactory = dragonPainterFactory; - this.createDragonSettingsGenerator = createDragonSettingsGenerator; - } - - public string Category => "Фракталы"; - public string Name => "Дракон"; - public string Description => "Дракон Хартера-Хейтуэя"; - - public void Perform() - { - var dragonSettings = CreateRandomSettings(); - // редактируем настройки: - SettingsForm.For(dragonSettings).ShowDialog(); - // создаём painter с такими настройками - dragonPainterFactory.Create(dragonSettings).Paint(); - } - - private DragonSettings CreateRandomSettings() - { - return createDragonSettingsGenerator(new Random()).Generate(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/App/Actions/ImageSettingsAction.cs b/FractalPainter/Solved/Step10/App/Actions/ImageSettingsAction.cs deleted file mode 100644 index 8b6256b7c..000000000 --- a/FractalPainter/Solved/Step10/App/Actions/ImageSettingsAction.cs +++ /dev/null @@ -1,28 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step10.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step10.App.Actions -{ - public class ImageSettingsAction : IUiAction - { - private readonly IImageHolder imageHolder; - private readonly ImageSettings imageSettings; - - public ImageSettingsAction(IImageHolder imageHolder, - ImageSettings imageSettings) - { - this.imageHolder = imageHolder; - this.imageSettings = imageSettings; - } - - public string Category => "Настройки"; - public string Name => "Изображение..."; - public string Description => "Размеры изображения"; - - public void Perform() - { - SettingsForm.For(imageSettings).ShowDialog(); - imageHolder.RecreateImage(imageSettings); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/App/Actions/KochFractalAction.cs b/FractalPainter/Solved/Step10/App/Actions/KochFractalAction.cs deleted file mode 100644 index 381a1a1ad..000000000 --- a/FractalPainter/Solved/Step10/App/Actions/KochFractalAction.cs +++ /dev/null @@ -1,24 +0,0 @@ -using FractalPainting.Solved.Step10.App.Fractals; -using FractalPainting.Solved.Step10.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step10.App.Actions -{ - public class KochFractalAction : IUiAction - { - private readonly KochPainter kochPainter; - - public KochFractalAction(KochPainter kochPainter) - { - this.kochPainter = kochPainter; - } - - public string Category => "Фракталы"; - public string Name => "Кривая Коха"; - public string Description => "Кривая Коха"; - - public void Perform() - { - kochPainter.Paint(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/App/Actions/PaletteSettingsAction.cs b/FractalPainter/Solved/Step10/App/Actions/PaletteSettingsAction.cs deleted file mode 100644 index 5621c45ba..000000000 --- a/FractalPainter/Solved/Step10/App/Actions/PaletteSettingsAction.cs +++ /dev/null @@ -1,24 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step10.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step10.App.Actions -{ - public class PaletteSettingsAction : IUiAction - { - private readonly Palette palette; - - public PaletteSettingsAction(Palette palette) - { - this.palette = palette; - } - - public string Category => "Настройки"; - public string Name => "Палитра..."; - public string Description => "Цвета для рисования фракталов"; - - public void Perform() - { - SettingsForm.For(palette).ShowDialog(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/App/Actions/SaveImageAction.cs b/FractalPainter/Solved/Step10/App/Actions/SaveImageAction.cs deleted file mode 100644 index b5362635b..000000000 --- a/FractalPainter/Solved/Step10/App/Actions/SaveImageAction.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System.IO; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step10.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step10.App.Actions -{ - public class SaveImageAction : IUiAction - { - private readonly IImageDirectoryProvider imageDirectoryProvider; - private readonly IImageHolder imageHolder; - - public SaveImageAction(IImageDirectoryProvider imageDirectoryProvider, IImageHolder imageHolder) - { - this.imageDirectoryProvider = imageDirectoryProvider; - this.imageHolder = imageHolder; - } - - public string Category => "Файл"; - public string Name => "Сохранить..."; - public string Description => "Сохранить изображение в файл"; - - public void Perform() - { - var dialog = new SaveFileDialog - { - CheckFileExists = false, - InitialDirectory = Path.GetFullPath(imageDirectoryProvider.ImagesDirectory), - DefaultExt = "bmp", - FileName = "image.bmp", - Filter = "Изображения (*.bmp)|*.bmp" - }; - var res = dialog.ShowDialog(); - if (res == DialogResult.OK) - imageHolder.SaveImage(dialog.FileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/App/AppSettings.cs b/FractalPainter/Solved/Step10/App/AppSettings.cs deleted file mode 100644 index 79f87832a..000000000 --- a/FractalPainter/Solved/Step10/App/AppSettings.cs +++ /dev/null @@ -1,10 +0,0 @@ -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step10.App -{ - public class AppSettings : IImageDirectoryProvider, IImageSettingsProvider - { - public string ImagesDirectory { get; set; } - public ImageSettings ImageSettings { get; set; } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/App/Fractals/DragonPainter.cs b/FractalPainter/Solved/Step10/App/Fractals/DragonPainter.cs deleted file mode 100644 index 81280ed34..000000000 --- a/FractalPainter/Solved/Step10/App/Fractals/DragonPainter.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Drawing; -using System.Linq; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step10.App.Fractals -{ - public class DragonPainter - { - private readonly IImageHolder imageHolder; - private readonly DragonSettings settings; - private readonly Palette palette; - private readonly float size; - private Size imageSize; - - public DragonPainter(IImageHolder imageHolder, - DragonSettings settings, Palette palette) - { - this.imageHolder = imageHolder; - this.settings = settings; - this.palette = palette; - imageSize = imageHolder.GetImageSize(); - size = Math.Min(imageSize.Width, imageSize.Height)/2.1f; - } - - public void Paint() - { - using (var graphics = imageHolder.StartDrawing()) - { - graphics.FillRectangle(new SolidBrush(palette.BackgroundColor), 0, 0, imageSize.Width, imageSize.Height); - var r = new Random(); - var cosa = (float) Math.Cos(settings.Angle1); - var sina = (float) Math.Sin(settings.Angle1); - var cosb = (float) Math.Cos(settings.Angle2); - var sinb = (float) Math.Sin(settings.Angle2); - var shiftX = settings.ShiftX*size*0.8f; - var shiftY = settings.ShiftY*size*0.8f; - var scale = settings.Scale; - var p = new PointF(0, 0); - var pointBrush = new SolidBrush(palette.PrimaryColor); - foreach (var i in Enumerable.Range(0, settings.IterationsCount)) - { - graphics.FillRectangle(pointBrush, imageSize.Width/3f + p.X, imageSize.Height/2f + p.Y, 1, 1); - if (r.Next(0, 2) == 0) - p = new PointF(scale*(p.X*cosa - p.Y*sina), scale*(p.X*sina + p.Y*cosa)); - else - p = new PointF(scale*(p.X*cosb - p.Y*sinb) + shiftX, scale*(p.X*sinb + p.Y*cosb) + shiftY); - if (i%100 == 0) imageHolder.UpdateUi(); - } - } - imageHolder.UpdateUi(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/App/Fractals/DragonSettings.cs b/FractalPainter/Solved/Step10/App/Fractals/DragonSettings.cs deleted file mode 100644 index b1ecb4bb2..000000000 --- a/FractalPainter/Solved/Step10/App/Fractals/DragonSettings.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step10.App.Fractals -{ - public class DragonSettings - { - public double Angle1 { get; set; } = Math.PI / 4; - public double Angle2 { get; set; } = 3 * Math.PI / 4; - public float ShiftX { get; set; } = 1; - public float ShiftY { get; set; } = 0; - public float Scale { get; set; } = (float)(1 / Math.Sqrt(2)); - public int IterationsCount { get; set; } = 20000; - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/App/Fractals/DragonSettingsGenerator.cs b/FractalPainter/Solved/Step10/App/Fractals/DragonSettingsGenerator.cs deleted file mode 100644 index abc49a4c4..000000000 --- a/FractalPainter/Solved/Step10/App/Fractals/DragonSettingsGenerator.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step10.App.Fractals -{ - public class DragonSettingsGenerator - { - private readonly Random random; - - public DragonSettingsGenerator(Random random) - { - this.random = random; - } - - public DragonSettings Generate() - { - return new DragonSettings - { - Angle1 = random.NextDouble() / 2 + 0.5, - Angle2 = 3 * (random.NextDouble() / 2 + 0.5), - ShiftX = 1, - ShiftY = 0, - Scale = (float)(1/Math.Sqrt(2) + (random.NextDouble() - 0.5)/10) - }; - } - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/App/Fractals/IDragonPainterFactory.cs b/FractalPainter/Solved/Step10/App/Fractals/IDragonPainterFactory.cs deleted file mode 100644 index fbdaf903b..000000000 --- a/FractalPainter/Solved/Step10/App/Fractals/IDragonPainterFactory.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace FractalPainting.Solved.Step10.App.Fractals -{ - public interface IDragonPainterFactory - { - DragonPainter Create(DragonSettings settings); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/App/Fractals/KochPainter.cs b/FractalPainter/Solved/Step10/App/Fractals/KochPainter.cs deleted file mode 100644 index 03a2b8003..000000000 --- a/FractalPainter/Solved/Step10/App/Fractals/KochPainter.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Drawing; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step10.App.Fractals -{ - public class KochPainter - { - private readonly IImageHolder imageHolder; - private readonly Palette palette; - - public KochPainter(IImageHolder imageHolder, Palette palette) - { - this.imageHolder = imageHolder; - this.palette = palette; - } - - public void Paint() - { - var imageSize = imageHolder.GetImageSize(); - using (var graphics = imageHolder.StartDrawing()) - using (var backgroundBrush = new SolidBrush(palette.BackgroundColor)) - { - graphics.FillRectangle(backgroundBrush, 0, 0, imageSize.Width, imageSize.Height); - DrawSegment(graphics, 0, imageSize.Height * 0.9f, imageSize.Width, imageSize.Height * 0.9f, true); - } - imageHolder.UpdateUi(); - } - - private void DrawSegment(Graphics graphics, float x0, float y0, float x1, float y1, bool primaryColor) - { - var len2 = (x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1); - if (len2 < 4) - { - if (y0 < 0 || y1 < 0) return; - using (var penBrush = new SolidBrush(primaryColor ? palette.PrimaryColor : palette.SecondaryColor)) - { - var pen = new Pen(penBrush, 3); - graphics.DrawLine(pen, x0, y0, x1, y1); - } - } - else - { - var vx = (x1 - x0)/3; - var vy = (y1 - y0)/3; - DrawSegment(graphics, x0, y0, x0 + vx, y0 + vy, primaryColor); - var k = (float) Math.Sqrt(3)/2f; - var px = (x0 + x1)/2 + vy*k; - var py = (y0 + y1)/2 - vx*k; - DrawSegment(graphics, x0 + vx, y0 + vy, px, py, !primaryColor); - DrawSegment(graphics, px, py, x0 + 2*vx, y0 + 2*vy, !primaryColor); - DrawSegment(graphics, x0 + 2*vx, y0 + 2*vy, x1, y1, primaryColor); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/App/MainForm.cs b/FractalPainter/Solved/Step10/App/MainForm.cs deleted file mode 100644 index b53b7354d..000000000 --- a/FractalPainter/Solved/Step10/App/MainForm.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Drawing; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step10.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step10.App -{ - public class MainForm : Form - { - public MainForm(IUiAction[] actions, - PictureBoxImageHolder pictureBox, - ImageSettings imageSettings) - { - ClientSize = new Size(imageSettings.Width, imageSettings.Height); - - var mainMenu = new MenuStrip(); - mainMenu.Items.AddRange(actions.ToMenuItems()); - Controls.Add(mainMenu); - - pictureBox.RecreateImage(imageSettings); - pictureBox.Dock = DockStyle.Fill; - Controls.Add(pictureBox); - } - - protected override void OnShown(EventArgs e) - { - base.OnShown(e); - Text = "Fractal Painter"; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/App/MainForm.resx b/FractalPainter/Solved/Step10/App/MainForm.resx deleted file mode 100644 index 9d845151a..000000000 --- a/FractalPainter/Solved/Step10/App/MainForm.resx +++ /dev/null @@ -1,123 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 17, 17 - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/App/PictureBoxImageHolder.cs b/FractalPainter/Solved/Step10/App/PictureBoxImageHolder.cs deleted file mode 100644 index fb93cfe7f..000000000 --- a/FractalPainter/Solved/Step10/App/PictureBoxImageHolder.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Drawing; -using System.Drawing.Imaging; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step10.App -{ - public class PictureBoxImageHolder : PictureBox, IImageHolder - { - public Size GetImageSize() - { - FailIfNotInitialized(); - return Image.Size; - } - - public Graphics StartDrawing() - { - FailIfNotInitialized(); - return Graphics.FromImage(Image); - } - - private void FailIfNotInitialized() - { - if (Image == null) - throw new InvalidOperationException("Call PictureBoxImageHolder.RecreateImage before other method call!"); - } - - public void UpdateUi() - { - Refresh(); - Application.DoEvents(); - } - - public void RecreateImage(ImageSettings imageSettings) - { - Image = new Bitmap(imageSettings.Width, imageSettings.Height, PixelFormat.Format24bppRgb); - } - - public void SaveImage(string fileName) - { - FailIfNotInitialized(); - Image.Save(fileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/App/Program.cs b/FractalPainter/Solved/Step10/App/Program.cs deleted file mode 100644 index 15deaccb7..000000000 --- a/FractalPainter/Solved/Step10/App/Program.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step10.App.Fractals; -using FractalPainting.Solved.Step10.Infrastructure.UiActions; -using Ninject; -using Ninject.Extensions.Conventions; -using Ninject.Extensions.Factory; - -namespace FractalPainting.Solved.Step10.App -{ - internal static class Program - { - /// - /// The main entry point for the application. - /// - [STAThread] - private static void Main() - { - try - { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - - var container = new StandardKernel(); - - container.Bind(kernel => kernel - .FromThisAssembly() - .SelectAllClasses() - .InheritedFrom() - .BindAllInterfaces()); - - container.Bind().ToSelf() - .InSingletonScope(); - container.Bind() - .To() - .InSingletonScope(); - - container.Bind().To() - .WhenInjectedInto(); - container.Bind().To() - .WhenInjectedInto(); - container.Bind() - .ToMethod(context => context.Kernel.Get().Load()) - .InSingletonScope(); - container.Bind() - .ToMethod(context => context.Kernel.Get().ImageSettings) - .InSingletonScope(); - - container.Bind().ToFactory(); - - var mainForm = container.Get(); - Application.Run(mainForm); - } - catch (Exception e) - { - MessageBox.Show(e.Message); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/App/SettingsManager.cs b/FractalPainter/Solved/Step10/App/SettingsManager.cs deleted file mode 100644 index 0fc1ab253..000000000 --- a/FractalPainter/Solved/Step10/App/SettingsManager.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step10.App -{ - public class SettingsManager - { - private readonly IObjectSerializer serializer; - private readonly IBlobStorage storage; - private string settingsFilename; - - public SettingsManager(IObjectSerializer serializer, IBlobStorage storage) - { - this.serializer = serializer; - this.storage = storage; - } - - public AppSettings Load() - { - try - { - settingsFilename = "app.settings"; - var data = storage.Get(settingsFilename); - if (data == null) - { - var defaultSettings = CreateDefaultSettings(); - Save(defaultSettings); - return defaultSettings; - } - return serializer.Deserialize(data); - } - catch (Exception e) - { - MessageBox.Show(e.Message, "Не удалось загрузить настройки"); - return CreateDefaultSettings(); - } - } - - private static AppSettings CreateDefaultSettings() - { - return new AppSettings - { - ImagesDirectory = ".", - ImageSettings = new ImageSettings() - }; - } - - public void Save(AppSettings settings) - { - storage.Set(settingsFilename, serializer.Serialize(settings)); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/Infrastructure/UiActions/IUiAction.cs b/FractalPainter/Solved/Step10/Infrastructure/UiActions/IUiAction.cs deleted file mode 100644 index ea1712e15..000000000 --- a/FractalPainter/Solved/Step10/Infrastructure/UiActions/IUiAction.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace FractalPainting.Solved.Step10.Infrastructure.UiActions -{ - public interface IUiAction - { - string Category { get; } - string Name { get; } - string Description { get; } - void Perform(); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/Infrastructure/UiActions/UiActionExtensions.cs b/FractalPainter/Solved/Step10/Infrastructure/UiActions/UiActionExtensions.cs deleted file mode 100644 index e8aa25152..000000000 --- a/FractalPainter/Solved/Step10/Infrastructure/UiActions/UiActionExtensions.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Windows.Forms; - -namespace FractalPainting.Solved.Step10.Infrastructure.UiActions -{ - public static class UiActionExtensions - { - public static ToolStripItem[] ToMenuItems(this IUiAction[] actions) - { - var items = actions.GroupBy(a => a.Category) - .Select(g => CreateTopLevelMenuItem(g.Key, g.ToList())) - .Cast() - .ToArray(); - return items; - } - - private static ToolStripMenuItem CreateTopLevelMenuItem(string name, IList items) - { - var menuItems = items.Select(a => a.ToMenuItem()).ToArray(); - return new ToolStripMenuItem(name, null, menuItems); - } - - public static ToolStripItem ToMenuItem(this IUiAction action) - { - return - new ToolStripMenuItem(action.Name, null, (sender, args) => action.Perform()) - { - ToolTipText = action.Description, - Tag = action - }; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step10/Step10.csproj b/FractalPainter/Solved/Step10/Step10.csproj deleted file mode 100644 index f9955662a..000000000 --- a/FractalPainter/Solved/Step10/Step10.csproj +++ /dev/null @@ -1,23 +0,0 @@ - - - - WinExe - netcoreapp3.1 - true - 8 - FractalPainting.Solved.Step10 - FractalPainter - false - - - - - - - - - - - - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/App/Actions/DragonFractalAction.cs b/FractalPainter/Solved/Step11/App/Actions/DragonFractalAction.cs deleted file mode 100644 index 0ce675f64..000000000 --- a/FractalPainter/Solved/Step11/App/Actions/DragonFractalAction.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step11.App.Fractals; -using FractalPainting.Solved.Step11.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step11.App.Actions -{ - public class DragonFractalAction : IUiAction - { - private readonly IDragonPainterFactory dragonPainterFactory; - private readonly Func createDragonSettingsGenerator; - - public DragonFractalAction(IDragonPainterFactory dragonPainterFactory, - Func createDragonSettingsGenerator) - { - this.dragonPainterFactory = dragonPainterFactory; - this.createDragonSettingsGenerator = createDragonSettingsGenerator; - } - - public MenuCategory Category => MenuCategory.Fractals; - public string Name => "Дракон"; - public string Description => "Дракон Хартера-Хейтуэя"; - - public void Perform() - { - var dragonSettings = CreateRandomSettings(); - // редактируем настройки: - SettingsForm.For(dragonSettings).ShowDialog(); - // создаём painter с такими настройками - dragonPainterFactory.Create(dragonSettings).Paint(); - } - - private DragonSettings CreateRandomSettings() - { - return createDragonSettingsGenerator(new Random()).Generate(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/App/Actions/ImageSettingsAction.cs b/FractalPainter/Solved/Step11/App/Actions/ImageSettingsAction.cs deleted file mode 100644 index 8dc9ad738..000000000 --- a/FractalPainter/Solved/Step11/App/Actions/ImageSettingsAction.cs +++ /dev/null @@ -1,28 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step11.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step11.App.Actions -{ - public class ImageSettingsAction : IUiAction - { - private readonly IImageHolder imageHolder; - private readonly ImageSettings imageSettings; - - public ImageSettingsAction(IImageHolder imageHolder, - ImageSettings imageSettings) - { - this.imageHolder = imageHolder; - this.imageSettings = imageSettings; - } - - public MenuCategory Category => MenuCategory.Settings; - public string Name => "Изображение..."; - public string Description => "Размеры изображения"; - - public void Perform() - { - SettingsForm.For(imageSettings).ShowDialog(); - imageHolder.RecreateImage(imageSettings); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/App/Actions/KochFractalAction.cs b/FractalPainter/Solved/Step11/App/Actions/KochFractalAction.cs deleted file mode 100644 index 81fed6d6b..000000000 --- a/FractalPainter/Solved/Step11/App/Actions/KochFractalAction.cs +++ /dev/null @@ -1,24 +0,0 @@ -using FractalPainting.Solved.Step11.App.Fractals; -using FractalPainting.Solved.Step11.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step11.App.Actions -{ - public class KochFractalAction : IUiAction - { - private readonly KochPainter kochPainter; - - public KochFractalAction(KochPainter kochPainter) - { - this.kochPainter = kochPainter; - } - - public MenuCategory Category => MenuCategory.Fractals; - public string Name => "Кривая Коха"; - public string Description => "Кривая Коха"; - - public void Perform() - { - kochPainter.Paint(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/App/Actions/PaletteSettingsAction.cs b/FractalPainter/Solved/Step11/App/Actions/PaletteSettingsAction.cs deleted file mode 100644 index 7443386c7..000000000 --- a/FractalPainter/Solved/Step11/App/Actions/PaletteSettingsAction.cs +++ /dev/null @@ -1,24 +0,0 @@ -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step11.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step11.App.Actions -{ - public class PaletteSettingsAction : IUiAction - { - private readonly Palette palette; - - public PaletteSettingsAction(Palette palette) - { - this.palette = palette; - } - - public MenuCategory Category => MenuCategory.Settings; - public string Name => "Палитра..."; - public string Description => "Цвета для рисования фракталов"; - - public void Perform() - { - SettingsForm.For(palette).ShowDialog(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/App/Actions/SaveImageAction.cs b/FractalPainter/Solved/Step11/App/Actions/SaveImageAction.cs deleted file mode 100644 index 7a0851c2f..000000000 --- a/FractalPainter/Solved/Step11/App/Actions/SaveImageAction.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System.IO; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step11.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step11.App.Actions -{ - public class SaveImageAction : IUiAction - { - private readonly IImageDirectoryProvider imageDirectoryProvider; - private readonly IImageHolder imageHolder; - - public SaveImageAction(IImageDirectoryProvider imageDirectoryProvider, IImageHolder imageHolder) - { - this.imageDirectoryProvider = imageDirectoryProvider; - this.imageHolder = imageHolder; - } - - public MenuCategory Category => MenuCategory.File; - public string Name => "Сохранить..."; - public string Description => "Сохранить изображение в файл"; - - public void Perform() - { - var dialog = new SaveFileDialog - { - CheckFileExists = false, - InitialDirectory = Path.GetFullPath(imageDirectoryProvider.ImagesDirectory), - DefaultExt = "bmp", - FileName = "image.bmp", - Filter = "Изображения (*.bmp)|*.bmp" - }; - var res = dialog.ShowDialog(); - if (res == DialogResult.OK) - imageHolder.SaveImage(dialog.FileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/App/AppSettings.cs b/FractalPainter/Solved/Step11/App/AppSettings.cs deleted file mode 100644 index f8660efab..000000000 --- a/FractalPainter/Solved/Step11/App/AppSettings.cs +++ /dev/null @@ -1,10 +0,0 @@ -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step11.App -{ - public class AppSettings : IImageDirectoryProvider, IImageSettingsProvider - { - public string ImagesDirectory { get; set; } - public ImageSettings ImageSettings { get; set; } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/App/Fractals/DragonPainter.cs b/FractalPainter/Solved/Step11/App/Fractals/DragonPainter.cs deleted file mode 100644 index 5c0568a53..000000000 --- a/FractalPainter/Solved/Step11/App/Fractals/DragonPainter.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Drawing; -using System.Linq; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step11.App.Fractals -{ - public class DragonPainter - { - private readonly IImageHolder imageHolder; - private readonly DragonSettings settings; - private readonly Palette palette; - private readonly float size; - private Size imageSize; - - public DragonPainter(IImageHolder imageHolder, - DragonSettings settings, Palette palette) - { - this.imageHolder = imageHolder; - this.settings = settings; - this.palette = palette; - imageSize = imageHolder.GetImageSize(); - size = Math.Min(imageSize.Width, imageSize.Height)/2.1f; - } - - public void Paint() - { - using (var graphics = imageHolder.StartDrawing()) - { - graphics.FillRectangle(new SolidBrush(palette.BackgroundColor), 0, 0, imageSize.Width, imageSize.Height); - var r = new Random(); - var cosa = (float) Math.Cos(settings.Angle1); - var sina = (float) Math.Sin(settings.Angle1); - var cosb = (float) Math.Cos(settings.Angle2); - var sinb = (float) Math.Sin(settings.Angle2); - var shiftX = settings.ShiftX*size*0.8f; - var shiftY = settings.ShiftY*size*0.8f; - var scale = settings.Scale; - var p = new PointF(0, 0); - var pointBrush = new SolidBrush(palette.PrimaryColor); - foreach (var i in Enumerable.Range(0, settings.IterationsCount)) - { - graphics.FillRectangle(pointBrush, imageSize.Width/3f + p.X, imageSize.Height/2f + p.Y, 1, 1); - if (r.Next(0, 2) == 0) - p = new PointF(scale*(p.X*cosa - p.Y*sina), scale*(p.X*sina + p.Y*cosa)); - else - p = new PointF(scale*(p.X*cosb - p.Y*sinb) + shiftX, scale*(p.X*sinb + p.Y*cosb) + shiftY); - if (i%100 == 0) imageHolder.UpdateUi(); - } - } - imageHolder.UpdateUi(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/App/Fractals/DragonSettings.cs b/FractalPainter/Solved/Step11/App/Fractals/DragonSettings.cs deleted file mode 100644 index cf829827f..000000000 --- a/FractalPainter/Solved/Step11/App/Fractals/DragonSettings.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step11.App.Fractals -{ - public class DragonSettings - { - public double Angle1 { get; set; } = Math.PI / 4; - public double Angle2 { get; set; } = 3 * Math.PI / 4; - public float ShiftX { get; set; } = 1; - public float ShiftY { get; set; } = 0; - public float Scale { get; set; } = (float)(1 / Math.Sqrt(2)); - public int IterationsCount { get; set; } = 20000; - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/App/Fractals/DragonSettingsGenerator.cs b/FractalPainter/Solved/Step11/App/Fractals/DragonSettingsGenerator.cs deleted file mode 100644 index e75ca519f..000000000 --- a/FractalPainter/Solved/Step11/App/Fractals/DragonSettingsGenerator.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; - -namespace FractalPainting.Solved.Step11.App.Fractals -{ - public class DragonSettingsGenerator - { - private readonly Random random; - - public DragonSettingsGenerator(Random random) - { - this.random = random; - } - - public DragonSettings Generate() - { - return new DragonSettings - { - Angle1 = random.NextDouble() / 2 + 0.5, - Angle2 = 3 * (random.NextDouble() / 2 + 0.5), - ShiftX = 1, - ShiftY = 0, - Scale = (float)(1/Math.Sqrt(2) + (random.NextDouble() - 0.5)/10) - }; - } - - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/App/Fractals/IDragonPainterFactory.cs b/FractalPainter/Solved/Step11/App/Fractals/IDragonPainterFactory.cs deleted file mode 100644 index b38a718f0..000000000 --- a/FractalPainter/Solved/Step11/App/Fractals/IDragonPainterFactory.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace FractalPainting.Solved.Step11.App.Fractals -{ - public interface IDragonPainterFactory - { - DragonPainter Create(DragonSettings settings); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/App/Fractals/KochPainter.cs b/FractalPainter/Solved/Step11/App/Fractals/KochPainter.cs deleted file mode 100644 index fa3ebe8b3..000000000 --- a/FractalPainter/Solved/Step11/App/Fractals/KochPainter.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Drawing; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step11.App.Fractals -{ - public class KochPainter - { - private readonly IImageHolder imageHolder; - private readonly Palette palette; - - public KochPainter(IImageHolder imageHolder, Palette palette) - { - this.imageHolder = imageHolder; - this.palette = palette; - } - - public void Paint() - { - var imageSize = imageHolder.GetImageSize(); - using (var graphics = imageHolder.StartDrawing()) - using (var backgroundBrush = new SolidBrush(palette.BackgroundColor)) - { - graphics.FillRectangle(backgroundBrush, 0, 0, imageSize.Width, imageSize.Height); - DrawSegment(graphics, 0, imageSize.Height * 0.9f, imageSize.Width, imageSize.Height * 0.9f, true); - } - imageHolder.UpdateUi(); - } - - private void DrawSegment(Graphics graphics, float x0, float y0, float x1, float y1, bool primaryColor) - { - var len2 = (x0 - x1)*(x0 - x1) + (y0 - y1)*(y0 - y1); - if (len2 < 4) - { - if (y0 < 0 || y1 < 0) return; - using (var penBrush = new SolidBrush(primaryColor ? palette.PrimaryColor : palette.SecondaryColor)) - { - var pen = new Pen(penBrush, 3); - graphics.DrawLine(pen, x0, y0, x1, y1); - } - } - else - { - var vx = (x1 - x0)/3; - var vy = (y1 - y0)/3; - DrawSegment(graphics, x0, y0, x0 + vx, y0 + vy, primaryColor); - var k = (float) Math.Sqrt(3)/2f; - var px = (x0 + x1)/2 + vy*k; - var py = (y0 + y1)/2 - vx*k; - DrawSegment(graphics, x0 + vx, y0 + vy, px, py, !primaryColor); - DrawSegment(graphics, px, py, x0 + 2*vx, y0 + 2*vy, !primaryColor); - DrawSegment(graphics, x0 + 2*vx, y0 + 2*vy, x1, y1, primaryColor); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/App/MainForm.cs b/FractalPainter/Solved/Step11/App/MainForm.cs deleted file mode 100644 index 8de1ca083..000000000 --- a/FractalPainter/Solved/Step11/App/MainForm.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Drawing; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step11.Infrastructure.UiActions; - -namespace FractalPainting.Solved.Step11.App -{ - public class MainForm : Form - { - public MainForm(IUiAction[] actions, - PictureBoxImageHolder pictureBox, - ImageSettings imageSettings) - { - ClientSize = new Size(imageSettings.Width, imageSettings.Height); - - var mainMenu = new MenuStrip(); - mainMenu.Items.AddRange(actions.ToMenuItems()); - Controls.Add(mainMenu); - - pictureBox.RecreateImage(imageSettings); - pictureBox.Dock = DockStyle.Fill; - Controls.Add(pictureBox); - } - - protected override void OnShown(EventArgs e) - { - base.OnShown(e); - Text = "Fractal Painter"; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/App/MainForm.resx b/FractalPainter/Solved/Step11/App/MainForm.resx deleted file mode 100644 index 9d845151a..000000000 --- a/FractalPainter/Solved/Step11/App/MainForm.resx +++ /dev/null @@ -1,123 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 17, 17 - - \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/App/PictureBoxImageHolder.cs b/FractalPainter/Solved/Step11/App/PictureBoxImageHolder.cs deleted file mode 100644 index e54d0255a..000000000 --- a/FractalPainter/Solved/Step11/App/PictureBoxImageHolder.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Drawing; -using System.Drawing.Imaging; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step11.App -{ - public class PictureBoxImageHolder : PictureBox, IImageHolder - { - public Size GetImageSize() - { - FailIfNotInitialized(); - return Image.Size; - } - - public Graphics StartDrawing() - { - FailIfNotInitialized(); - return Graphics.FromImage(Image); - } - - private void FailIfNotInitialized() - { - if (Image == null) - throw new InvalidOperationException("Call PictureBoxImageHolder.RecreateImage before other method call!"); - } - - public void UpdateUi() - { - Refresh(); - Application.DoEvents(); - } - - public void RecreateImage(ImageSettings imageSettings) - { - Image = new Bitmap(imageSettings.Width, imageSettings.Height, PixelFormat.Format24bppRgb); - } - - public void SaveImage(string fileName) - { - FailIfNotInitialized(); - Image.Save(fileName); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/App/Program.cs b/FractalPainter/Solved/Step11/App/Program.cs deleted file mode 100644 index ff35b2526..000000000 --- a/FractalPainter/Solved/Step11/App/Program.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; -using FractalPainting.Solved.Step11.App.Fractals; -using FractalPainting.Solved.Step11.Infrastructure.UiActions; -using Ninject; -using Ninject.Extensions.Conventions; -using Ninject.Extensions.Factory; - -namespace FractalPainting.Solved.Step11.App -{ - internal static class Program - { - /// - /// The main entry point for the application. - /// - [STAThread] - private static void Main() - { - try - { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - - var container = new StandardKernel(); - - container.Bind(kernel => kernel - .FromThisAssembly() - .SelectAllClasses() - .InheritedFrom() - .BindAllInterfaces()); - - container.Bind().ToSelf() - .InSingletonScope(); - container.Bind() - .To() - .InSingletonScope(); - - container.Bind().To() - .WhenInjectedInto(); - container.Bind().To() - .WhenInjectedInto(); - container.Bind() - .ToMethod(context => context.Kernel.Get().Load()) - .InSingletonScope(); - container.Bind() - .ToMethod(context => context.Kernel.Get().ImageSettings) - .InSingletonScope(); - - container.Bind().ToFactory(); - - var mainForm = container.Get(); - Application.Run(mainForm); - } - catch (Exception e) - { - MessageBox.Show(e.Message); - } - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/App/SettingsManager.cs b/FractalPainter/Solved/Step11/App/SettingsManager.cs deleted file mode 100644 index e629f2582..000000000 --- a/FractalPainter/Solved/Step11/App/SettingsManager.cs +++ /dev/null @@ -1,54 +0,0 @@ -using System; -using System.Windows.Forms; -using FractalPainting.Infrastructure.Common; - -namespace FractalPainting.Solved.Step11.App -{ - public class SettingsManager - { - private readonly IObjectSerializer serializer; - private readonly IBlobStorage storage; - private string settingsFilename; - - public SettingsManager(IObjectSerializer serializer, IBlobStorage storage) - { - this.serializer = serializer; - this.storage = storage; - } - - public AppSettings Load() - { - try - { - settingsFilename = "app.settings"; - var data = storage.Get(settingsFilename); - if (data == null) - { - var defaultSettings = CreateDefaultSettings(); - Save(defaultSettings); - return defaultSettings; - } - return serializer.Deserialize(data); - } - catch (Exception e) - { - MessageBox.Show(e.Message, "Не удалось загрузить настройки"); - return CreateDefaultSettings(); - } - } - - private static AppSettings CreateDefaultSettings() - { - return new AppSettings - { - ImagesDirectory = ".", - ImageSettings = new ImageSettings() - }; - } - - public void Save(AppSettings settings) - { - storage.Set(settingsFilename, serializer.Serialize(settings)); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/Infrastructure/UiActions/EnumExtensions.cs b/FractalPainter/Solved/Step11/Infrastructure/UiActions/EnumExtensions.cs deleted file mode 100644 index 1b1b14e85..000000000 --- a/FractalPainter/Solved/Step11/Infrastructure/UiActions/EnumExtensions.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.ComponentModel; -using System.Linq; - -namespace FractalPainting.Solved.Step11.Infrastructure.UiActions -{ - public static class EnumExtensions - { - public static string GetDescription(this Enum enumValue) - { - var fieldInfo = enumValue.GetType().GetField(enumValue.ToString()); - var description = fieldInfo - .GetCustomAttributes(typeof(DescriptionAttribute), false) - .Cast() - .FirstOrDefault()?.Description; - - return description ?? enumValue.ToString(); - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/Infrastructure/UiActions/IUiAction.cs b/FractalPainter/Solved/Step11/Infrastructure/UiActions/IUiAction.cs deleted file mode 100644 index ec038d30e..000000000 --- a/FractalPainter/Solved/Step11/Infrastructure/UiActions/IUiAction.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace FractalPainting.Solved.Step11.Infrastructure.UiActions -{ - public interface IUiAction - { - MenuCategory Category { get; } - string Name { get; } - string Description { get; } - void Perform(); - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/Infrastructure/UiActions/MenuCategory.cs b/FractalPainter/Solved/Step11/Infrastructure/UiActions/MenuCategory.cs deleted file mode 100644 index 050b7910f..000000000 --- a/FractalPainter/Solved/Step11/Infrastructure/UiActions/MenuCategory.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.ComponentModel; - -namespace FractalPainting.Solved.Step11.Infrastructure.UiActions -{ - public enum MenuCategory - { - [Description("")] - File = 0, - - [Description("")] - Fractals = 1, - - [Description("")] - Settings = 2, - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/Infrastructure/UiActions/UiActionExtensions.cs b/FractalPainter/Solved/Step11/Infrastructure/UiActions/UiActionExtensions.cs deleted file mode 100644 index 67694d3af..000000000 --- a/FractalPainter/Solved/Step11/Infrastructure/UiActions/UiActionExtensions.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Windows.Forms; - -namespace FractalPainting.Solved.Step11.Infrastructure.UiActions -{ - public static class UiActionExtensions - { - public static ToolStripItem[] ToMenuItems(this IUiAction[] actions) - { - var items = actions.GroupBy(a => a.Category) - .OrderBy(a => a.Key) - .Select(g => CreateTopLevelMenuItem(g.Key, g.ToList())) - .Cast() - .ToArray(); - return items; - } - - private static ToolStripMenuItem CreateTopLevelMenuItem(MenuCategory category, IList items) - { - var menuItems = items.Select(a => a.ToMenuItem()).ToArray(); - return new ToolStripMenuItem(category.GetDescription(), null, menuItems); - } - - public static ToolStripItem ToMenuItem(this IUiAction action) - { - return - new ToolStripMenuItem(action.Name, null, (sender, args) => action.Perform()) - { - ToolTipText = action.Description, - Tag = action - }; - } - } -} \ No newline at end of file diff --git a/FractalPainter/Solved/Step11/Step11.csproj b/FractalPainter/Solved/Step11/Step11.csproj deleted file mode 100644 index a1693046f..000000000 --- a/FractalPainter/Solved/Step11/Step11.csproj +++ /dev/null @@ -1,22 +0,0 @@ - - - - WinExe - netcoreapp3.1 - true - false - FractalPainting.Solved.Step11 - FractalPainter - - - - - - - - - - - - - \ No newline at end of file diff --git a/FractalPainter/app.settings b/FractalPainter/app.settings deleted file mode 100644 index 2f21b81b6..000000000 --- a/FractalPainter/app.settings +++ /dev/null @@ -1,8 +0,0 @@ - - - . - - 800 - 600 - - \ No newline at end of file diff --git a/FractalPainter/fractalPainter.csproj b/FractalPainter/fractalPainter.csproj deleted file mode 100644 index c6cc7f58d..000000000 --- a/FractalPainter/fractalPainter.csproj +++ /dev/null @@ -1,37 +0,0 @@ - - - - WinExe - net7.0-windows - false - 11 - true - FractalPainting - FractalPainter - - - - - PreserveNewest - SettingsSingleFileGenerator - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From 74fba8ad9e9b5de2c3b1ee147f7a2a297ff037ad Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 23 Jan 2024 22:43:09 +0500 Subject: [PATCH 08/19] added a filter for boring words --- TagsCloudConsoleUI/ConsoleUi.cs | 2 +- .../Providers/ColorerProviders.cs | 3 +- .../Drawing/Colorers/BicolorColorer.cs | 13 + TagsCloudContainer/Resources/filter.txt | 240 ++++++++++++++++++ TagsCloudContainer/TagsCloudContainer.csproj | 1 + di.sln | 6 - 6 files changed, 257 insertions(+), 8 deletions(-) create mode 100644 TagsCloudContainer/Drawing/Colorers/BicolorColorer.cs create mode 100644 TagsCloudContainer/Resources/filter.txt diff --git a/TagsCloudConsoleUI/ConsoleUi.cs b/TagsCloudConsoleUI/ConsoleUi.cs index ebe637394..3c80968a6 100644 --- a/TagsCloudConsoleUI/ConsoleUi.cs +++ b/TagsCloudConsoleUI/ConsoleUi.cs @@ -28,7 +28,7 @@ public void StartUi() containerBuilder); containerBuilder.RegisterType().As().SingleInstance(); - containerBuilder.RegisterInstance(new DefaultWordFilter(new TxtFileWordParser("filter.txt"))).As() + containerBuilder.RegisterInstance(new DefaultWordFilter(new TxtFileWordParser("../../../../TagsCloudContainer/Resources/filter.txt"))).As() .SingleInstance(); containerBuilder.RegisterType().As().SingleInstance(); containerBuilder.RegisterType().As().SingleInstance(); diff --git a/TagsCloudConsoleUI/Providers/ColorerProviders.cs b/TagsCloudConsoleUI/Providers/ColorerProviders.cs index c1b79d57d..0f6fe939f 100644 --- a/TagsCloudConsoleUI/Providers/ColorerProviders.cs +++ b/TagsCloudConsoleUI/Providers/ColorerProviders.cs @@ -8,6 +8,7 @@ public static class ColorerProviders public static readonly IReadOnlyDictionary> RegisteredProviders = new Dictionary> { - {"Random", builder => builder.RegisterType().As().SingleInstance()} + {"Random", builder => builder.RegisterType().As().SingleInstance()}, + {"Bicolor", builder => builder.RegisterInstance(new BicolorColorer()).As().SingleInstance()} }; } \ No newline at end of file diff --git a/TagsCloudContainer/Drawing/Colorers/BicolorColorer.cs b/TagsCloudContainer/Drawing/Colorers/BicolorColorer.cs new file mode 100644 index 000000000..a0924c3b9 --- /dev/null +++ b/TagsCloudContainer/Drawing/Colorers/BicolorColorer.cs @@ -0,0 +1,13 @@ +using System.Drawing; + +namespace TagsCloudContainer.Drawing.Colorers; + +public class BicolorColorer : IWordColorer +{ + private readonly Color _first = Color.FromArgb(Random.Next(0, 256), Random.Next(0, 256), Random.Next(0, 256)); + private readonly Color _second = Color.FromArgb(Random.Next(0, 256), Random.Next(0, 256), Random.Next(0, 256)); + private static readonly Random Random = new(); + + public Color GetWordColor(string word, int wordFrequency) + => Random.Next(0, 2) == 0 ? _first : _second; +} \ No newline at end of file diff --git a/TagsCloudContainer/Resources/filter.txt b/TagsCloudContainer/Resources/filter.txt new file mode 100644 index 000000000..53e24c008 --- /dev/null +++ b/TagsCloudContainer/Resources/filter.txt @@ -0,0 +1,240 @@ +About +After +Ago +Around +At +Before +By +Circa +During +Following +For +From +Gone +In +On +Past +Prior to +Since +Until +till +Up to +Up until +Aboard +Above +Across +Against +Alongside +Amid +Among +Apart from +Astride +At +Atop +Behind +Below +Beneath +Beside +Between +Beyond +By +Close to +Far +Far from +Forward of +From +In +In between +In front of +Inside +Into +Minus +Near +Near to +Next to +Of +Off +On +On board +On top of +Onto +Upon +Opposite +Out +Out of +Outside +Outside of +Over +Round +Through +Throughout +To +Together with +Toward +towards +Under +Underneath +Up against +With +Within +Without +Above +Across +Against +Ahead +Along +Along with +Amid +Around +Away +Away from +Behind +Below +Beneath +By means of +Down +Further to +In between +Into +Off +Off of +On +Onto +Over +Out of +Past +Round +Through +Under +Up +Via +About +According to +Anti +As +As for +As per +As to +As well as +Aside from +Bar +Barring +Because of +Besides +But for +By +But +Concerning +Considering +Contrary to +Counting +Cum +Depending on +Despite +Due to +Except +Except for +Excepting +Excluding +Given +In addition to +in case of +In face of +In favor of +in favour of +In light of +In spite of +In view of +Including +Instead of +Less +Like +Notwithstanding +Of +On account of +On behalf of +Other than +Owing to +Pending +Per +Plus +Preparatory to +Pro +Re +Regarding +Regardless of +Save +Save for +Saving +Than +Thanks to +Unlike +Versus +With +With reference to +With regard to +Worth +I +you +he +she +it +we +you +they +me +you +him +her +it +us +you +them +mine +yours +his +hers +its +ours +yours +theirs +this +these +that +those +who +whom +which +what +who +whom +that +which +whoever +whichever +whomever +all +another +any +anybody +anyone +anything +each +everybody +everyone +everything +few +many +nobody +none +several +some +somebody +and someone +myself +yourself +himself +herself +ourselves +yourselves +themselves diff --git a/TagsCloudContainer/TagsCloudContainer.csproj b/TagsCloudContainer/TagsCloudContainer.csproj index 8ea252154..376f35d55 100644 --- a/TagsCloudContainer/TagsCloudContainer.csproj +++ b/TagsCloudContainer/TagsCloudContainer.csproj @@ -9,6 +9,7 @@ + diff --git a/di.sln b/di.sln index b7586c131..ed75980fa 100644 --- a/di.sln +++ b/di.sln @@ -1,7 +1,5 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FractalPainter", "FractalPainter\FractalPainter.csproj", "{4D70883B-6F8B-4166-802F-8EDC9BE93199}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudContainer", "TagsCloudContainer\TagsCloudContainer.csproj", "{D6C7C0D1-2CA2-4F45-8AA5-340A58A45B95}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudContainerTests", "TagsCloudContainerTests\TagsCloudContainerTests.csproj", "{405FAAB3-6E4C-487C-9429-43529876FB44}" @@ -14,10 +12,6 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {4D70883B-6F8B-4166-802F-8EDC9BE93199}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4D70883B-6F8B-4166-802F-8EDC9BE93199}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4D70883B-6F8B-4166-802F-8EDC9BE93199}.Release|Any CPU.ActiveCfg = Release|Any CPU - {4D70883B-6F8B-4166-802F-8EDC9BE93199}.Release|Any CPU.Build.0 = Release|Any CPU {D6C7C0D1-2CA2-4F45-8AA5-340A58A45B95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D6C7C0D1-2CA2-4F45-8AA5-340A58A45B95}.Debug|Any CPU.Build.0 = Debug|Any CPU {D6C7C0D1-2CA2-4F45-8AA5-340A58A45B95}.Release|Any CPU.ActiveCfg = Release|Any CPU From 1999cd6fcca5797aef10e36a77a8ae434c55a644 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 24 Jan 2024 14:48:04 +0500 Subject: [PATCH 09/19] Added partial unit test coverage --- TagsCloudConsoleUI/ConsoleUi.cs | 8 +-- .../Common/{Word.cs => WordData.cs} | 6 +- .../Drawing/DefaultImageDrawer.cs | 2 +- .../DrawingOptions/DefaultOptionsProvider.cs | 4 +- .../DefaultWordCloudDistributor.cs | 8 +-- .../IWordCloudDistributorProvider.cs | 2 +- TagsCloudContainer/Utils/DrawingUtils.cs | 13 ++-- .../Drawing/DefaultImageDrawerTests.cs | 19 ++++++ .../TagsCloudContainerTests.csproj | 2 + .../Utils/DrawingUtilsTests.cs | 67 +++++++++++++++++++ .../WordFiltering/DefaultWordFilterTests.cs | 20 ++++++ .../WordGrouping/DefaultWordProcessorTests.cs | 30 +++++++++ 12 files changed, 158 insertions(+), 23 deletions(-) rename TagsCloudContainer/Common/{Word.cs => WordData.cs} (60%) create mode 100644 TagsCloudContainerTests/Drawing/DefaultImageDrawerTests.cs create mode 100644 TagsCloudContainerTests/Utils/DrawingUtilsTests.cs create mode 100644 TagsCloudContainerTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs create mode 100644 TagsCloudContainerTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs diff --git a/TagsCloudConsoleUI/ConsoleUi.cs b/TagsCloudConsoleUI/ConsoleUi.cs index 3c80968a6..d7b0e5962 100644 --- a/TagsCloudConsoleUI/ConsoleUi.cs +++ b/TagsCloudConsoleUI/ConsoleUi.cs @@ -53,7 +53,7 @@ private void RegisterCloudBuildingOptions(ContainerBuilder containerBuilder) var imageSize = new Size(imageSide, imageSide); var frequencyScaling = GetInteger( - "Enter the frequency scaling (a positive integer). It Determines the scale to word frequency ratio.", 2, + "Enter the frequency scaling (a positive integer). It Determines the scale to word frequency ratio.", 1, 100); containerBuilder @@ -69,7 +69,7 @@ private static void BuildTagCloud(IImageDrawer imageDrawer, string dirPath, stri Console.WriteLine($"The image has been saved to \"{dirPath}\""); } - private void RegisterAlgorithm(IReadOnlyDictionary> registeredAlgorithms, + private static void RegisterAlgorithm(IReadOnlyDictionary> registeredAlgorithms, string prompt, ContainerBuilder containerBuilder) { var sb = new StringBuilder($"{prompt}\n"); @@ -119,9 +119,9 @@ private static Color GetRgbColor(string prompt) { while (true) { - var colorString = Prompt.GetString(prompt, + var colorString = Prompt.GetString(prompt, "", promptColor: ConsoleColor.DarkGreen); - if (DrawingUtils.TryParseRgb(colorString, out var color)) + if (DrawingUtils.TryParseRgb(colorString!, out var color)) return color; Console.WriteLine("Ivalid color format. Try again."); } diff --git a/TagsCloudContainer/Common/Word.cs b/TagsCloudContainer/Common/WordData.cs similarity index 60% rename from TagsCloudContainer/Common/Word.cs rename to TagsCloudContainer/Common/WordData.cs index fabc3087f..0ee2bf892 100644 --- a/TagsCloudContainer/Common/Word.cs +++ b/TagsCloudContainer/Common/WordData.cs @@ -2,15 +2,13 @@ namespace TagsCloudContainer.Common; -public class Word +public class WordData { - public string Value { get; } public Rectangle Rectangle { get; } public int Frequency { get; } - public Word(string value, Rectangle rectangle, int frequency) + public WordData(Rectangle rectangle, int frequency) { - Value = value; Rectangle = rectangle; Frequency = frequency; } diff --git a/TagsCloudContainer/Drawing/DefaultImageDrawer.cs b/TagsCloudContainer/Drawing/DefaultImageDrawer.cs index af984f1d8..04b0efd89 100644 --- a/TagsCloudContainer/Drawing/DefaultImageDrawer.cs +++ b/TagsCloudContainer/Drawing/DefaultImageDrawer.cs @@ -9,7 +9,7 @@ namespace TagsCloudContainer.Drawing; public class DefaultImageDrawer : IImageDrawer { - private readonly IReadOnlyDictionary _distributedWords; + private readonly IReadOnlyDictionary _distributedWords; private readonly Options _options; private readonly IWordColorer _colorer; public DefaultImageDrawer(IWordCloudDistributorProvider cloudDistributorProvider, IOptionsProvider optionsProvider, IWordColorer colorer) diff --git a/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs b/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs index c9123f21a..f56daaf2e 100644 --- a/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs +++ b/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs @@ -4,10 +4,10 @@ namespace TagsCloudContainer.DrawingOptions; public class DefaultOptionsProvider : IOptionsProvider { + public Options Options { get; } + public DefaultOptionsProvider(Options options) { Options = options; } - - public Options Options { get; } } \ No newline at end of file diff --git a/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs b/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs index 7f0735430..c28f72044 100644 --- a/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs +++ b/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs @@ -20,15 +20,15 @@ public DefaultWordCloudDistributor(IProcessedWordProvider processedWord, ICloudL _options = optionsProvider.Options; } - public IReadOnlyDictionary DistributedWords => DistributeWords().AsReadOnly(); + public IReadOnlyDictionary DistributedWords => DistributeWords().AsReadOnly(); - private Dictionary DistributeWords() + private Dictionary DistributeWords() { - var distributed = new Dictionary(); + var distributed = new Dictionary(); foreach (var (word, frequency) in _words) { - var newWord = new Word(word, _cloudLayouter.PutNextRectangle(DrawingUtils.GetStringSize(word, frequency, + var newWord = new WordData(_cloudLayouter.PutNextRectangle(DrawingUtils.GetStringSize(word, frequency, _options.FrequencyScaling, _options.Font)), frequency); distributed.Add(word, newWord); } diff --git a/TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs b/TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs index 55e0f9fd3..cfa6bcd6a 100644 --- a/TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs +++ b/TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs @@ -5,5 +5,5 @@ namespace TagsCloudContainer.TagCloudForming; public interface IWordCloudDistributorProvider { - public IReadOnlyDictionary DistributedWords { get; } + public IReadOnlyDictionary DistributedWords { get; } } \ No newline at end of file diff --git a/TagsCloudContainer/Utils/DrawingUtils.cs b/TagsCloudContainer/Utils/DrawingUtils.cs index 9e96d3d57..7fbbda3a4 100644 --- a/TagsCloudContainer/Utils/DrawingUtils.cs +++ b/TagsCloudContainer/Utils/DrawingUtils.cs @@ -8,19 +8,18 @@ public static class DrawingUtils public static Size GetStringSize(string word, int frequency, int frequencyScaling, Font font) { + if (frequency < 1) + throw new ArgumentException("Frequency must be a positive integer"); + if (frequencyScaling < 1) + throw new ArgumentException("Frequncy scaling must be a positive integer"); + var sizeIncrement = frequencyScaling * (frequency - 1); var newFont = new Font(font.FontFamily, font.Size + sizeIncrement, font.Style); return Size.Ceiling(Graphics.MeasureString(word, newFont)); } - public static bool TryParseRgb(string? rgbString, out Color color, char separator = ' ') + public static bool TryParseRgb(string rgbString, out Color color, char separator = ' ') { - if (string.IsNullOrWhiteSpace(rgbString)) - { - color = default; - return false; - } - var numbers = rgbString.Split(separator); if (numbers.Length != 3 || numbers.Any(n => !int.TryParse(n, out var parsed) || parsed < 0 || parsed > 255)) { diff --git a/TagsCloudContainerTests/Drawing/DefaultImageDrawerTests.cs b/TagsCloudContainerTests/Drawing/DefaultImageDrawerTests.cs new file mode 100644 index 000000000..20c6f601a --- /dev/null +++ b/TagsCloudContainerTests/Drawing/DefaultImageDrawerTests.cs @@ -0,0 +1,19 @@ +using System.Drawing; +using System.Drawing.Imaging; +using TagsCloudContainer.Drawing; + +namespace TagsCloudContainerTests.Drawing; + +public class DefaultImageDrawerTests +{ + [TestCase("test", "<>\\")] + [TestCase("test", "name|123")] + [TestCase("test", "")] + [TestCase("", "filename")] + [TestCase(" ", "filename")] + [TestCase(@"\:\", "filename")] + public void SaveImage_ThrowsArgumentException_OnInvalidParameters(string dirPath, string filename) + { + Assert.Throws(() => DefaultImageDrawer.SaveImage(new Bitmap(1, 1), dirPath, filename, ImageFormat.Png)); + } +} \ No newline at end of file diff --git a/TagsCloudContainerTests/TagsCloudContainerTests.csproj b/TagsCloudContainerTests/TagsCloudContainerTests.csproj index af86e5851..eb6e17ad1 100644 --- a/TagsCloudContainerTests/TagsCloudContainerTests.csproj +++ b/TagsCloudContainerTests/TagsCloudContainerTests.csproj @@ -10,7 +10,9 @@ + + diff --git a/TagsCloudContainerTests/Utils/DrawingUtilsTests.cs b/TagsCloudContainerTests/Utils/DrawingUtilsTests.cs new file mode 100644 index 000000000..8fd6dc645 --- /dev/null +++ b/TagsCloudContainerTests/Utils/DrawingUtilsTests.cs @@ -0,0 +1,67 @@ +using System.Drawing; +using FluentAssertions; +using TagsCloudContainer.Utils; + +namespace TagsCloudContainerTests.Utils; + +public class DrawingUtilsTests +{ + [TestCase(0, 2)] + [TestCase(2, 0)] + [TestCase(2, -2)] + [TestCase(-2, 2)] + public void GetStringSize_ThrowsArgumentException_OnIncorrectParameters(int frequency, int frequencyScaling) + { + Assert.Throws(() => + DrawingUtils.GetStringSize("word", frequency, frequencyScaling, new Font(FontFamily.GenericMonospace, 20))); + } + + [TestCase("word", 2, 3, "longer_word", 2, 3)] + [TestCase("word", 2, 3, "word", 5, 3)] + [TestCase("word", 2, 3, "word", 2, 5)] + public void GetStringSize_CorrectlyDeterminesWordSize_BasedOnWordLengthAndFrequency( + string word1, + int frequency1, + int frequencyScaling1, + string word2, + int frequency2, + int frequencyScaling2) + + { + var size1 = DrawingUtils.GetStringSize(word1, frequency1, frequencyScaling1, + new Font(FontFamily.GenericMonospace, 20)); + + var size2 = DrawingUtils.GetStringSize(word2, frequency2, frequencyScaling2, + new Font(FontFamily.GenericMonospace, 20)); + + var area1 = size1.Height * size1.Width; + var area2 = size2.Height * size2.Width; + + area2 + .Should() + .BeGreaterThan(area1); + } + + [TestCase("")] + [TestCase("255 255 256")] + [TestCase("-1 255 255")] + [TestCase("255 255 255 255")] + [TestCase("255 255")] + [TestCase("255 notint 255")] + public void TryParseRgb_ReturnsFalse_OnIncorrectInputData(string rgbString) + { + var result = DrawingUtils.TryParseRgb(rgbString, out _); + + result + .Should() + .BeFalse(); + } + + [Test] + public void TryParseRgb_ReturnsCorrectColor_OnCorrectInputData() + { + _ = DrawingUtils.TryParseRgb("123 43 5", out var color); + + CollectionAssert.AreEqual(new []{123, 43, 5}, new []{color.R, color.G, color.B}); + } +} \ No newline at end of file diff --git a/TagsCloudContainerTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs b/TagsCloudContainerTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs new file mode 100644 index 000000000..fbe77bea0 --- /dev/null +++ b/TagsCloudContainerTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs @@ -0,0 +1,20 @@ +using NSubstitute; +using TagsCloudContainer.WordProcessing.WordFiltering; +using TagsCloudContainer.WordProcessing.WordInput; + +namespace TagsCloudContainerTests.WordProcessing.WordFiltering; + +public class DefaultWordFilterTests +{ + [Test] + public void FilterWords_ReturnsCorrectResult_OnCorrectInputData() + { + var wordProvider = Substitute.For(); + wordProvider.Words.Returns(new[] {"word1", "word2", "123"}); + + var filter = new DefaultWordFilter(wordProvider); + var filtered = filter.FilterWords(new[] {"word1", "1234", "word2", "a", "another", "test"}); + + CollectionAssert.AreEqual(new []{"1234", "a", "another", "test"}, filtered); + } +} \ No newline at end of file diff --git a/TagsCloudContainerTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs b/TagsCloudContainerTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs new file mode 100644 index 000000000..fbd64d42c --- /dev/null +++ b/TagsCloudContainerTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs @@ -0,0 +1,30 @@ +using NSubstitute; +using TagsCloudContainer.WordProcessing.WordFiltering; +using TagsCloudContainer.WordProcessing.WordGrouping; +using TagsCloudContainer.WordProcessing.WordInput; + +namespace TagsCloudContainerTests.WordProcessing.WordGrouping; + +public class DefaultWordProcessorTests +{ + [Test] + public void ProcessedWords_ReturnsCorrectlyProcessedWords_OnCorrectInputData() + { + var words = Substitute.For(); + words.Words.Returns(new[] {"1", "2", "4", "4", "1", "1", "5"}); + var filter1 = GetWordFilter(new[] {"2"}); + var filter2 = GetWordFilter(new[] {"5"}); + var processor = new DefaultWordProcessor(words, new[] {filter1, filter2}); + + var result = processor.ProcessedWords; + + CollectionAssert.AreEqual(new Dictionary {{"1", 3}, {"4", 2}}, result); + } + + private static DefaultWordFilter GetWordFilter(string[] wordsToExclude) + { + var wordProvider = Substitute.For(); + wordProvider.Words.Returns(wordsToExclude); + return new DefaultWordFilter(wordProvider); + } +} \ No newline at end of file From b17d792f3e67ca6419a27b10e2e6e95a509f48db Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 24 Jan 2024 14:48:04 +0500 Subject: [PATCH 10/19] Added unit test coverage --- TagsCloudConsoleUI/ConsoleUi.cs | 11 ++- .../Common/{Word.cs => WordData.cs} | 6 +- .../Drawing/DefaultImageDrawer.cs | 2 +- .../DrawingOptions/DefaultOptionsProvider.cs | 4 +- .../DefaultWordCloudDistributor.cs | 8 +-- .../IWordCloudDistributorProvider.cs | 2 +- TagsCloudContainer/Utils/DrawingUtils.cs | 13 ++-- .../Drawing/DefaultImageDrawerTests.cs | 19 ++++++ .../TagsCloudContainerTests.csproj | 2 + .../Utils/DrawingUtilsTests.cs | 67 +++++++++++++++++++ .../WordFiltering/DefaultWordFilterTests.cs | 20 ++++++ .../WordGrouping/DefaultWordProcessorTests.cs | 30 +++++++++ 12 files changed, 159 insertions(+), 25 deletions(-) rename TagsCloudContainer/Common/{Word.cs => WordData.cs} (60%) create mode 100644 TagsCloudContainerTests/Drawing/DefaultImageDrawerTests.cs create mode 100644 TagsCloudContainerTests/Utils/DrawingUtilsTests.cs create mode 100644 TagsCloudContainerTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs create mode 100644 TagsCloudContainerTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs diff --git a/TagsCloudConsoleUI/ConsoleUi.cs b/TagsCloudConsoleUI/ConsoleUi.cs index 3c80968a6..ad70a6275 100644 --- a/TagsCloudConsoleUI/ConsoleUi.cs +++ b/TagsCloudConsoleUI/ConsoleUi.cs @@ -53,12 +53,11 @@ private void RegisterCloudBuildingOptions(ContainerBuilder containerBuilder) var imageSize = new Size(imageSide, imageSide); var frequencyScaling = GetInteger( - "Enter the frequency scaling (a positive integer). It Determines the scale to word frequency ratio.", 2, + "Enter the frequency scaling (a positive integer). It Determines the scale to word frequency ratio.", 1, 100); containerBuilder - .RegisterInstance(new DefaultOptionsProvider(new Options(fontColor, backgroundColor, imageSize, font, - frequencyScaling))) + .RegisterInstance(new DefaultOptionsProvider(new Options(fontColor, backgroundColor, imageSize, font, frequencyScaling))) .As().SingleInstance(); } @@ -69,7 +68,7 @@ private static void BuildTagCloud(IImageDrawer imageDrawer, string dirPath, stri Console.WriteLine($"The image has been saved to \"{dirPath}\""); } - private void RegisterAlgorithm(IReadOnlyDictionary> registeredAlgorithms, + private static void RegisterAlgorithm(IReadOnlyDictionary> registeredAlgorithms, string prompt, ContainerBuilder containerBuilder) { var sb = new StringBuilder($"{prompt}\n"); @@ -119,9 +118,9 @@ private static Color GetRgbColor(string prompt) { while (true) { - var colorString = Prompt.GetString(prompt, + var colorString = Prompt.GetString(prompt, "", promptColor: ConsoleColor.DarkGreen); - if (DrawingUtils.TryParseRgb(colorString, out var color)) + if (DrawingUtils.TryParseRgb(colorString!, out var color)) return color; Console.WriteLine("Ivalid color format. Try again."); } diff --git a/TagsCloudContainer/Common/Word.cs b/TagsCloudContainer/Common/WordData.cs similarity index 60% rename from TagsCloudContainer/Common/Word.cs rename to TagsCloudContainer/Common/WordData.cs index fabc3087f..0ee2bf892 100644 --- a/TagsCloudContainer/Common/Word.cs +++ b/TagsCloudContainer/Common/WordData.cs @@ -2,15 +2,13 @@ namespace TagsCloudContainer.Common; -public class Word +public class WordData { - public string Value { get; } public Rectangle Rectangle { get; } public int Frequency { get; } - public Word(string value, Rectangle rectangle, int frequency) + public WordData(Rectangle rectangle, int frequency) { - Value = value; Rectangle = rectangle; Frequency = frequency; } diff --git a/TagsCloudContainer/Drawing/DefaultImageDrawer.cs b/TagsCloudContainer/Drawing/DefaultImageDrawer.cs index af984f1d8..04b0efd89 100644 --- a/TagsCloudContainer/Drawing/DefaultImageDrawer.cs +++ b/TagsCloudContainer/Drawing/DefaultImageDrawer.cs @@ -9,7 +9,7 @@ namespace TagsCloudContainer.Drawing; public class DefaultImageDrawer : IImageDrawer { - private readonly IReadOnlyDictionary _distributedWords; + private readonly IReadOnlyDictionary _distributedWords; private readonly Options _options; private readonly IWordColorer _colorer; public DefaultImageDrawer(IWordCloudDistributorProvider cloudDistributorProvider, IOptionsProvider optionsProvider, IWordColorer colorer) diff --git a/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs b/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs index c9123f21a..f56daaf2e 100644 --- a/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs +++ b/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs @@ -4,10 +4,10 @@ namespace TagsCloudContainer.DrawingOptions; public class DefaultOptionsProvider : IOptionsProvider { + public Options Options { get; } + public DefaultOptionsProvider(Options options) { Options = options; } - - public Options Options { get; } } \ No newline at end of file diff --git a/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs b/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs index 7f0735430..c28f72044 100644 --- a/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs +++ b/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs @@ -20,15 +20,15 @@ public DefaultWordCloudDistributor(IProcessedWordProvider processedWord, ICloudL _options = optionsProvider.Options; } - public IReadOnlyDictionary DistributedWords => DistributeWords().AsReadOnly(); + public IReadOnlyDictionary DistributedWords => DistributeWords().AsReadOnly(); - private Dictionary DistributeWords() + private Dictionary DistributeWords() { - var distributed = new Dictionary(); + var distributed = new Dictionary(); foreach (var (word, frequency) in _words) { - var newWord = new Word(word, _cloudLayouter.PutNextRectangle(DrawingUtils.GetStringSize(word, frequency, + var newWord = new WordData(_cloudLayouter.PutNextRectangle(DrawingUtils.GetStringSize(word, frequency, _options.FrequencyScaling, _options.Font)), frequency); distributed.Add(word, newWord); } diff --git a/TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs b/TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs index 55e0f9fd3..cfa6bcd6a 100644 --- a/TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs +++ b/TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs @@ -5,5 +5,5 @@ namespace TagsCloudContainer.TagCloudForming; public interface IWordCloudDistributorProvider { - public IReadOnlyDictionary DistributedWords { get; } + public IReadOnlyDictionary DistributedWords { get; } } \ No newline at end of file diff --git a/TagsCloudContainer/Utils/DrawingUtils.cs b/TagsCloudContainer/Utils/DrawingUtils.cs index 9e96d3d57..7fbbda3a4 100644 --- a/TagsCloudContainer/Utils/DrawingUtils.cs +++ b/TagsCloudContainer/Utils/DrawingUtils.cs @@ -8,19 +8,18 @@ public static class DrawingUtils public static Size GetStringSize(string word, int frequency, int frequencyScaling, Font font) { + if (frequency < 1) + throw new ArgumentException("Frequency must be a positive integer"); + if (frequencyScaling < 1) + throw new ArgumentException("Frequncy scaling must be a positive integer"); + var sizeIncrement = frequencyScaling * (frequency - 1); var newFont = new Font(font.FontFamily, font.Size + sizeIncrement, font.Style); return Size.Ceiling(Graphics.MeasureString(word, newFont)); } - public static bool TryParseRgb(string? rgbString, out Color color, char separator = ' ') + public static bool TryParseRgb(string rgbString, out Color color, char separator = ' ') { - if (string.IsNullOrWhiteSpace(rgbString)) - { - color = default; - return false; - } - var numbers = rgbString.Split(separator); if (numbers.Length != 3 || numbers.Any(n => !int.TryParse(n, out var parsed) || parsed < 0 || parsed > 255)) { diff --git a/TagsCloudContainerTests/Drawing/DefaultImageDrawerTests.cs b/TagsCloudContainerTests/Drawing/DefaultImageDrawerTests.cs new file mode 100644 index 000000000..20c6f601a --- /dev/null +++ b/TagsCloudContainerTests/Drawing/DefaultImageDrawerTests.cs @@ -0,0 +1,19 @@ +using System.Drawing; +using System.Drawing.Imaging; +using TagsCloudContainer.Drawing; + +namespace TagsCloudContainerTests.Drawing; + +public class DefaultImageDrawerTests +{ + [TestCase("test", "<>\\")] + [TestCase("test", "name|123")] + [TestCase("test", "")] + [TestCase("", "filename")] + [TestCase(" ", "filename")] + [TestCase(@"\:\", "filename")] + public void SaveImage_ThrowsArgumentException_OnInvalidParameters(string dirPath, string filename) + { + Assert.Throws(() => DefaultImageDrawer.SaveImage(new Bitmap(1, 1), dirPath, filename, ImageFormat.Png)); + } +} \ No newline at end of file diff --git a/TagsCloudContainerTests/TagsCloudContainerTests.csproj b/TagsCloudContainerTests/TagsCloudContainerTests.csproj index af86e5851..eb6e17ad1 100644 --- a/TagsCloudContainerTests/TagsCloudContainerTests.csproj +++ b/TagsCloudContainerTests/TagsCloudContainerTests.csproj @@ -10,7 +10,9 @@ + + diff --git a/TagsCloudContainerTests/Utils/DrawingUtilsTests.cs b/TagsCloudContainerTests/Utils/DrawingUtilsTests.cs new file mode 100644 index 000000000..8fd6dc645 --- /dev/null +++ b/TagsCloudContainerTests/Utils/DrawingUtilsTests.cs @@ -0,0 +1,67 @@ +using System.Drawing; +using FluentAssertions; +using TagsCloudContainer.Utils; + +namespace TagsCloudContainerTests.Utils; + +public class DrawingUtilsTests +{ + [TestCase(0, 2)] + [TestCase(2, 0)] + [TestCase(2, -2)] + [TestCase(-2, 2)] + public void GetStringSize_ThrowsArgumentException_OnIncorrectParameters(int frequency, int frequencyScaling) + { + Assert.Throws(() => + DrawingUtils.GetStringSize("word", frequency, frequencyScaling, new Font(FontFamily.GenericMonospace, 20))); + } + + [TestCase("word", 2, 3, "longer_word", 2, 3)] + [TestCase("word", 2, 3, "word", 5, 3)] + [TestCase("word", 2, 3, "word", 2, 5)] + public void GetStringSize_CorrectlyDeterminesWordSize_BasedOnWordLengthAndFrequency( + string word1, + int frequency1, + int frequencyScaling1, + string word2, + int frequency2, + int frequencyScaling2) + + { + var size1 = DrawingUtils.GetStringSize(word1, frequency1, frequencyScaling1, + new Font(FontFamily.GenericMonospace, 20)); + + var size2 = DrawingUtils.GetStringSize(word2, frequency2, frequencyScaling2, + new Font(FontFamily.GenericMonospace, 20)); + + var area1 = size1.Height * size1.Width; + var area2 = size2.Height * size2.Width; + + area2 + .Should() + .BeGreaterThan(area1); + } + + [TestCase("")] + [TestCase("255 255 256")] + [TestCase("-1 255 255")] + [TestCase("255 255 255 255")] + [TestCase("255 255")] + [TestCase("255 notint 255")] + public void TryParseRgb_ReturnsFalse_OnIncorrectInputData(string rgbString) + { + var result = DrawingUtils.TryParseRgb(rgbString, out _); + + result + .Should() + .BeFalse(); + } + + [Test] + public void TryParseRgb_ReturnsCorrectColor_OnCorrectInputData() + { + _ = DrawingUtils.TryParseRgb("123 43 5", out var color); + + CollectionAssert.AreEqual(new []{123, 43, 5}, new []{color.R, color.G, color.B}); + } +} \ No newline at end of file diff --git a/TagsCloudContainerTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs b/TagsCloudContainerTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs new file mode 100644 index 000000000..fbe77bea0 --- /dev/null +++ b/TagsCloudContainerTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs @@ -0,0 +1,20 @@ +using NSubstitute; +using TagsCloudContainer.WordProcessing.WordFiltering; +using TagsCloudContainer.WordProcessing.WordInput; + +namespace TagsCloudContainerTests.WordProcessing.WordFiltering; + +public class DefaultWordFilterTests +{ + [Test] + public void FilterWords_ReturnsCorrectResult_OnCorrectInputData() + { + var wordProvider = Substitute.For(); + wordProvider.Words.Returns(new[] {"word1", "word2", "123"}); + + var filter = new DefaultWordFilter(wordProvider); + var filtered = filter.FilterWords(new[] {"word1", "1234", "word2", "a", "another", "test"}); + + CollectionAssert.AreEqual(new []{"1234", "a", "another", "test"}, filtered); + } +} \ No newline at end of file diff --git a/TagsCloudContainerTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs b/TagsCloudContainerTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs new file mode 100644 index 000000000..fbd64d42c --- /dev/null +++ b/TagsCloudContainerTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs @@ -0,0 +1,30 @@ +using NSubstitute; +using TagsCloudContainer.WordProcessing.WordFiltering; +using TagsCloudContainer.WordProcessing.WordGrouping; +using TagsCloudContainer.WordProcessing.WordInput; + +namespace TagsCloudContainerTests.WordProcessing.WordGrouping; + +public class DefaultWordProcessorTests +{ + [Test] + public void ProcessedWords_ReturnsCorrectlyProcessedWords_OnCorrectInputData() + { + var words = Substitute.For(); + words.Words.Returns(new[] {"1", "2", "4", "4", "1", "1", "5"}); + var filter1 = GetWordFilter(new[] {"2"}); + var filter2 = GetWordFilter(new[] {"5"}); + var processor = new DefaultWordProcessor(words, new[] {filter1, filter2}); + + var result = processor.ProcessedWords; + + CollectionAssert.AreEqual(new Dictionary {{"1", 3}, {"4", 2}}, result); + } + + private static DefaultWordFilter GetWordFilter(string[] wordsToExclude) + { + var wordProvider = Substitute.For(); + wordProvider.Words.Returns(wordsToExclude); + return new DefaultWordFilter(wordProvider); + } +} \ No newline at end of file From e4840eaa0e071ef5595c40901ed766a5cfeb47f2 Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 26 Jan 2024 20:56:28 +0500 Subject: [PATCH 11/19] removed unused usings --- TagsCloudConsoleUI/Providers/ConsoleOptionsProvider.cs | 8 ++++++++ .../DrawingOptions/DefaultOptionsProvider.cs | 4 +--- .../TagCloudForming/IWordCloudDistributorProvider.cs | 3 +-- .../WordProcessing/WordInput/IWordProvider.cs | 4 +--- .../WordProcessing/WordInput/TxtFileWordParser.cs | 4 +--- 5 files changed, 12 insertions(+), 11 deletions(-) create mode 100644 TagsCloudConsoleUI/Providers/ConsoleOptionsProvider.cs diff --git a/TagsCloudConsoleUI/Providers/ConsoleOptionsProvider.cs b/TagsCloudConsoleUI/Providers/ConsoleOptionsProvider.cs new file mode 100644 index 000000000..2d5199ff9 --- /dev/null +++ b/TagsCloudConsoleUI/Providers/ConsoleOptionsProvider.cs @@ -0,0 +1,8 @@ +using TagsCloudContainer.DrawingOptions; + +namespace TagsCloudConsoleUI.Providers; + +public class ConsoleOptionsProvider : IOptionsProvider +{ + public Options Options { get; } +} \ No newline at end of file diff --git a/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs b/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs index f56daaf2e..c0149c65d 100644 --- a/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs +++ b/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs @@ -1,6 +1,4 @@ -using System.Drawing; - -namespace TagsCloudContainer.DrawingOptions; +namespace TagsCloudContainer.DrawingOptions; public class DefaultOptionsProvider : IOptionsProvider { diff --git a/TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs b/TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs index cfa6bcd6a..cc8a03ead 100644 --- a/TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs +++ b/TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs @@ -1,5 +1,4 @@ -using System.Drawing; -using TagsCloudContainer.Common; +using TagsCloudContainer.Common; namespace TagsCloudContainer.TagCloudForming; diff --git a/TagsCloudContainer/WordProcessing/WordInput/IWordProvider.cs b/TagsCloudContainer/WordProcessing/WordInput/IWordProvider.cs index da10b303d..cf0d8960d 100644 --- a/TagsCloudContainer/WordProcessing/WordInput/IWordProvider.cs +++ b/TagsCloudContainer/WordProcessing/WordInput/IWordProvider.cs @@ -1,6 +1,4 @@ -using TagsCloudContainer.Common; - -namespace TagsCloudContainer.WordProcessing.WordInput; +namespace TagsCloudContainer.WordProcessing.WordInput; public interface IWordProvider { diff --git a/TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs b/TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs index bbb8ffaaf..670a40291 100644 --- a/TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs +++ b/TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs @@ -1,6 +1,4 @@ -using TagsCloudContainer.WordProcessing.WordFiltering; - -namespace TagsCloudContainer.WordProcessing.WordInput; +namespace TagsCloudContainer.WordProcessing.WordInput; public class TxtFileWordParser : IWordProvider From 3f8c0d799087c0ffaff7507f3eb38255937a4f5c Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 26 Jan 2024 23:51:59 +0500 Subject: [PATCH 12/19] Settings Provider refactored --- TagsCloudConsoleUI/ConsoleUi.cs | 154 ++---------------- TagsCloudConsoleUI/Program.cs | 6 +- .../Providers/CloudAlgorithmProviders.cs | 7 +- .../Providers/ColorerProviders.cs | 8 +- .../Providers/ConsoleOptionsProvider.cs | 8 - .../Providers/ConsoleSettingsProvider.cs | 141 ++++++++++++++++ TagsCloudConsoleUI/Providers/WordProviders.cs | 13 +- .../BuildingOptions/CommonOptions.cs | 21 +++ .../DefaultDrawingOptionsProvider.cs | 11 ++ .../DrawingOptions.cs} | 6 +- .../BuildingOptions/ICommonOptionsProvider.cs | 8 + .../IDrawingOptionsProvider.cs | 6 + .../Common/DiContainerBuilder.cs | 26 +++ .../Drawing/Colorers/DefaultWordColorer.cs | 6 +- .../Drawing/DefaultImageDrawer.cs | 28 ++-- .../DrawingOptions/DefaultOptionsProvider.cs | 11 -- .../DrawingOptions/IOptionsProvider.cs | 6 - .../DefaultWordCloudDistributor.cs | 16 +- .../WordGrouping/DefaultWordProcessor.cs | 16 +- 19 files changed, 283 insertions(+), 215 deletions(-) delete mode 100644 TagsCloudConsoleUI/Providers/ConsoleOptionsProvider.cs create mode 100644 TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs create mode 100644 TagsCloudContainer/BuildingOptions/CommonOptions.cs create mode 100644 TagsCloudContainer/BuildingOptions/DefaultDrawingOptionsProvider.cs rename TagsCloudContainer/{DrawingOptions/Options.cs => BuildingOptions/DrawingOptions.cs} (67%) create mode 100644 TagsCloudContainer/BuildingOptions/ICommonOptionsProvider.cs create mode 100644 TagsCloudContainer/BuildingOptions/IDrawingOptionsProvider.cs create mode 100644 TagsCloudContainer/Common/DiContainerBuilder.cs delete mode 100644 TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs delete mode 100644 TagsCloudContainer/DrawingOptions/IOptionsProvider.cs diff --git a/TagsCloudConsoleUI/ConsoleUi.cs b/TagsCloudConsoleUI/ConsoleUi.cs index ad70a6275..53a12a2f8 100644 --- a/TagsCloudConsoleUI/ConsoleUi.cs +++ b/TagsCloudConsoleUI/ConsoleUi.cs @@ -1,165 +1,35 @@ -using System.Drawing; -using System.Drawing.Imaging; -using System.Text; +using System.Drawing.Imaging; using Autofac; -using McMaster.Extensions.CommandLineUtils; using TagsCloudConsoleUI.Providers; +using TagsCloudContainer.BuildingOptions; using TagsCloudContainer.Drawing; -using TagsCloudContainer.Drawing.Colorers; -using TagsCloudContainer.DrawingOptions; -using TagsCloudContainer.TagCloudForming; -using TagsCloudContainer.Utils; -using TagsCloudContainer.WordProcessing.WordFiltering; -using TagsCloudContainer.WordProcessing.WordGrouping; -using TagsCloudContainer.WordProcessing.WordInput; namespace TagsCloudConsoleUI; public class ConsoleUi : IUiManager { + private readonly ContainerBuilder _containerBuilder; + + public ConsoleUi(ContainerBuilder containerBuilder) + { + _containerBuilder = containerBuilder; + } + public void StartUi() { - var containerBuilder = new ContainerBuilder(); - RegisterWordProvider(GetPathToWordsFile(), containerBuilder); - - RegisterCloudBuildingOptions(containerBuilder); - - RegisterAlgorithm(CloudAlgorithmProviders.RegisteredProviders, "Choose the cloud forming algorithm:", - containerBuilder); - - containerBuilder.RegisterType().As().SingleInstance(); - containerBuilder.RegisterInstance(new DefaultWordFilter(new TxtFileWordParser("../../../../TagsCloudContainer/Resources/filter.txt"))).As() + _containerBuilder.RegisterType() + .As() .SingleInstance(); - containerBuilder.RegisterType().As().SingleInstance(); - containerBuilder.RegisterType().As().SingleInstance(); - containerBuilder.RegisterType().As().SingleInstance(); - var container = containerBuilder.Build(); + var container = _containerBuilder.Build(); BuildTagCloud(container.Resolve(), ".", "image.png", ImageFormat.Png); } - private void RegisterCloudBuildingOptions(ContainerBuilder containerBuilder) - { - var font = GetFont(); - var backgroundColor = GetRgbColor("Enter background color in RGB format separated by space"); - - RegisterColoringAlgorithm(out var fontColor, containerBuilder); - - var imageSide = - GetInteger( - "Enter the image's desired size in px. The image will be a square. It must range from 500 px to 5000 px.", - 500, 5000); - var imageSize = new Size(imageSide, imageSide); - var frequencyScaling = - GetInteger( - "Enter the frequency scaling (a positive integer). It Determines the scale to word frequency ratio.", 1, - 100); - - containerBuilder - .RegisterInstance(new DefaultOptionsProvider(new Options(fontColor, backgroundColor, imageSize, font, frequencyScaling))) - .As().SingleInstance(); - } - private static void BuildTagCloud(IImageDrawer imageDrawer, string dirPath, string fileName, ImageFormat imageFormat) { var bitmap = imageDrawer.DrawImage(); DefaultImageDrawer.SaveImage(bitmap, dirPath, fileName, imageFormat); Console.WriteLine($"The image has been saved to \"{dirPath}\""); } - - private static void RegisterAlgorithm(IReadOnlyDictionary> registeredAlgorithms, - string prompt, ContainerBuilder containerBuilder) - { - var sb = new StringBuilder($"{prompt}\n"); - foreach (var algorithmProvider in registeredAlgorithms.Keys) - sb.AppendLine(algorithmProvider); - - while (true) - { - var algorithm = Prompt.GetString(sb.ToString(), "", promptColor: ConsoleColor.DarkGreen); - if (registeredAlgorithms.TryGetValue(algorithm!, out var provider)) - { - provider(containerBuilder); - break; - } - - Console.WriteLine("Specified algorithm isn't supported. Try again."); - } - } - - private static int GetInteger(string prompt, int lowerAllowedBoundary, int upperAllowedBoundary) - { - while (true) - { - var intString = Prompt.GetString(prompt, "", ConsoleColor.DarkGreen); - if (int.TryParse(intString, out var parsed) && parsed >= lowerAllowedBoundary && - parsed <= upperAllowedBoundary) - return parsed; - Console.WriteLine("Given number is invalid. Try again."); - } - } - - private void RegisterColoringAlgorithm(out Color fontColor, ContainerBuilder containerBuilder) - { - if (Prompt.GetYesNo("Do you want to use a custom coloring algorithm?", false, ConsoleColor.DarkGreen)) - { - RegisterAlgorithm(ColorerProviders.RegisteredProviders, "Choose the algorithm:", containerBuilder); - - fontColor = Color.White; - return; - } - - containerBuilder.RegisterType().As().SingleInstance(); - fontColor = GetRgbColor("Enter font color in RGB format separated by space"); - } - - private static Color GetRgbColor(string prompt) - { - while (true) - { - var colorString = Prompt.GetString(prompt, "", - promptColor: ConsoleColor.DarkGreen); - if (DrawingUtils.TryParseRgb(colorString!, out var color)) - return color; - Console.WriteLine("Ivalid color format. Try again."); - } - } - - private static Font GetFont() - { - var fontName = Prompt.GetString("Enter font name", promptColor: ConsoleColor.DarkGreen) ?? - "Microsoft Sans Serif"; - while (true) - { - var fontSizeStr = Prompt.GetString("Enter font size in pt", promptColor: ConsoleColor.DarkGreen); - if (float.TryParse(fontSizeStr, out var fontSize) && fontSize > 0) - return new Font(fontName, fontSize); - Console.WriteLine("Font size must be a correct positive number. Try again."); - } - } - - private void RegisterWordProvider(string path, ContainerBuilder containerBuilder) - { - var ext = Path.GetExtension(path); - if (WordProviders.RegisteredProviders.TryGetValue(ext, out var provider)) - { - provider(containerBuilder, path); - return; - } - - Console.WriteLine("This extension is not supported. Try another one."); - StartUi(); - } - - private static string GetPathToWordsFile() - { - while (true) - { - var path = Prompt.GetString("Enter the path to the file with words", promptColor: ConsoleColor.DarkGreen); - if (File.Exists(path)) - return path; - Console.WriteLine("Provided path is invalid. Try again."); - } - } } \ No newline at end of file diff --git a/TagsCloudConsoleUI/Program.cs b/TagsCloudConsoleUI/Program.cs index 56c4c545f..eeaa7c2ab 100644 --- a/TagsCloudConsoleUI/Program.cs +++ b/TagsCloudConsoleUI/Program.cs @@ -1,4 +1,6 @@ -namespace TagsCloudConsoleUI; +using TagsCloudContainer.Common; + +namespace TagsCloudConsoleUI; public static class Program { @@ -6,7 +8,7 @@ public static void Main() { try { - new ConsoleUi().StartUi(); + new ConsoleUi(DiContainerBuilder.BuildContainer()).StartUi(); } catch (Exception e) { diff --git a/TagsCloudConsoleUI/Providers/CloudAlgorithmProviders.cs b/TagsCloudConsoleUI/Providers/CloudAlgorithmProviders.cs index 1c061b3ed..0fd83d7f1 100644 --- a/TagsCloudConsoleUI/Providers/CloudAlgorithmProviders.cs +++ b/TagsCloudConsoleUI/Providers/CloudAlgorithmProviders.cs @@ -1,14 +1,13 @@ using System.Drawing; -using Autofac; using TagsCloudVisualization; namespace TagsCloudConsoleUI.Providers; public static class CloudAlgorithmProviders { - public static readonly IReadOnlyDictionary> RegisteredProviders = - new Dictionary> + public static readonly IReadOnlyDictionary RegisteredProviders = + new Dictionary { - {"Circular", builder => builder.RegisterInstance(new CircularCloudLayouter(Point.Empty)).As().SingleInstance()} + {"Circular", new CircularCloudLayouter(Point.Empty)} }; } \ No newline at end of file diff --git a/TagsCloudConsoleUI/Providers/ColorerProviders.cs b/TagsCloudConsoleUI/Providers/ColorerProviders.cs index 0f6fe939f..2cef9eb14 100644 --- a/TagsCloudConsoleUI/Providers/ColorerProviders.cs +++ b/TagsCloudConsoleUI/Providers/ColorerProviders.cs @@ -5,10 +5,10 @@ namespace TagsCloudConsoleUI.Providers; public static class ColorerProviders { - public static readonly IReadOnlyDictionary> RegisteredProviders = - new Dictionary> + public static readonly IReadOnlyDictionary RegisteredProviders = + new Dictionary { - {"Random", builder => builder.RegisterType().As().SingleInstance()}, - {"Bicolor", builder => builder.RegisterInstance(new BicolorColorer()).As().SingleInstance()} + {"Random", new RandomWordColorer()}, + {"Bicolor", new BicolorColorer()} }; } \ No newline at end of file diff --git a/TagsCloudConsoleUI/Providers/ConsoleOptionsProvider.cs b/TagsCloudConsoleUI/Providers/ConsoleOptionsProvider.cs deleted file mode 100644 index 2d5199ff9..000000000 --- a/TagsCloudConsoleUI/Providers/ConsoleOptionsProvider.cs +++ /dev/null @@ -1,8 +0,0 @@ -using TagsCloudContainer.DrawingOptions; - -namespace TagsCloudConsoleUI.Providers; - -public class ConsoleOptionsProvider : IOptionsProvider -{ - public Options Options { get; } -} \ No newline at end of file diff --git a/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs b/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs new file mode 100644 index 000000000..977811540 --- /dev/null +++ b/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs @@ -0,0 +1,141 @@ +using System.Drawing; +using System.Text; +using McMaster.Extensions.CommandLineUtils; +using TagsCloudContainer.BuildingOptions; +using TagsCloudContainer.Drawing.Colorers; +using TagsCloudContainer.Utils; +using TagsCloudContainer.WordProcessing.WordInput; + +namespace TagsCloudConsoleUI.Providers; + +public class ConsoleSettingsProvider : IDrawingOptionsProvider, ICommonOptionsProvider +{ + public DrawingOptions DrawingOptions => _drawingOptions ??= GetDrawingOptions(); + + public CommonOptions CommonOptions => _commonOptions ??= GetCommonOptions(); + + private DrawingOptions? _drawingOptions; + + private CommonOptions? _commonOptions; + + private bool _isCustomColoringUsed; + + private DrawingOptions GetDrawingOptions() + { + var font = GetFont(); + var backgroundColor = GetRgbColor("Enter background color in RGB format separated by space"); + + var fontColor = GetFontColor(); + + var imageSide = GetInteger( + "Enter the image's desired size in px. The image will be a square. It must range from 500 px to 5000 px.", + 500, 5000); + + var imageSize = new Size(imageSide, imageSide); + + var frequencyScaling = GetInteger( + "Enter the frequency scaling (a positive integer). It Determines the scale to word frequency ratio.", + 1, 100); + + return new DrawingOptions(fontColor, backgroundColor, imageSize, font, frequencyScaling); + } + + private CommonOptions GetCommonOptions() + { + var wordProvider = GetWordProvider(); + var wordColorer = GetWordColorer(); + var cloudLayouter = GetAlgorithm(CloudAlgorithmProviders.RegisteredProviders, + "Choose the cloud forming algorithm:"); + + return new CommonOptions(wordProvider, wordColorer, cloudLayouter); + } + + private Color GetFontColor() + { + return _isCustomColoringUsed + ? Color.White + : GetRgbColor("Enter font color in RGB format separated by space"); + } + + private static int GetInteger(string prompt, int lowerAllowedBoundary, int upperAllowedBoundary) + { + while (true) + { + var intString = Prompt.GetString(prompt, "", ConsoleColor.DarkGreen); + if (int.TryParse(intString, out var parsed) && parsed >= lowerAllowedBoundary && + parsed <= upperAllowedBoundary) + return parsed; + Console.WriteLine("Given number is invalid. Try again."); + } + } + + private static Color GetRgbColor(string prompt) + { + while (true) + { + var colorString = Prompt.GetString(prompt, "", + promptColor: ConsoleColor.DarkGreen); + if (DrawingUtils.TryParseRgb(colorString!, out var color)) + return color; + Console.WriteLine("Ivalid color format. Try again."); + } + } + + private static Font GetFont() + { + var fontName = Prompt.GetString("Enter font name", promptColor: ConsoleColor.DarkGreen) ?? + "Microsoft Sans Serif"; + while (true) + { + var fontSizeStr = Prompt.GetString("Enter font size in pt", promptColor: ConsoleColor.DarkGreen); + if (float.TryParse(fontSizeStr, out var fontSize) && fontSize > 0) + return new Font(fontName, fontSize); + Console.WriteLine("Font size must be a correct positive number. Try again."); + } + } + + private IWordColorer? GetWordColorer() + { + if (!Prompt.GetYesNo("Would you like to use a custom coloring algorithm?", false, ConsoleColor.DarkGreen)) + return null; + + _isCustomColoringUsed = true; + return GetAlgorithm(ColorerProviders.RegisteredProviders, "Choose the algorithm:"); + } + + private static T GetAlgorithm(IReadOnlyDictionary registeredAlgorithms, + string prompt) + { + var sb = new StringBuilder($"{prompt}\n"); + foreach (var algorithmProvider in registeredAlgorithms.Keys) + sb.AppendLine(algorithmProvider); + + while (true) + { + var algorithm = Prompt.GetString(sb.ToString(), "", promptColor: ConsoleColor.DarkGreen); + if (registeredAlgorithms.TryGetValue(algorithm!, out var provider)) + return provider; + + Console.WriteLine("Specified algorithm isn't supported. Try again."); + } + } + + private static IWordProvider GetWordProvider() + { + while (true) + { + var path = Prompt.GetString("Enter the path to the file with words", promptColor: ConsoleColor.DarkGreen); + if (!File.Exists(path)) + { + Console.WriteLine("Provided path is invalid. Try again."); + continue; + } + + var ext = Path.GetExtension(path); + if (WordProviders.RegisteredProviders.TryGetValue(ext, out var provider)) + return provider(path); + + Console.WriteLine("This extension is not supported. Try again."); + } + } +} \ No newline at end of file diff --git a/TagsCloudConsoleUI/Providers/WordProviders.cs b/TagsCloudConsoleUI/Providers/WordProviders.cs index 2e1cfd150..41fc11787 100644 --- a/TagsCloudConsoleUI/Providers/WordProviders.cs +++ b/TagsCloudConsoleUI/Providers/WordProviders.cs @@ -1,15 +1,14 @@ -using Autofac; -using TagsCloudContainer.WordProcessing.WordInput; +using TagsCloudContainer.WordProcessing.WordInput; namespace TagsCloudConsoleUI.Providers; public static class WordProviders { - public static readonly IReadOnlyDictionary> RegisteredProviders = - new Dictionary> + public static readonly IReadOnlyDictionary> RegisteredProviders = + new Dictionary> { - {".txt", (builder, path) => builder.RegisterInstance(new TxtFileWordParser(path)).As().SingleInstance()}, - {".docx", (builder, path) => builder.RegisterInstance(new DocxFileWordParser(path)).As().SingleInstance()}, - {".doc", (builder, path) => builder.RegisterInstance(new DocxFileWordParser(path)).As().SingleInstance()} + {".txt", path=> new TxtFileWordParser(path)}, + {".docx", path => new DocxFileWordParser(path)}, + {".doc", path => new DocxFileWordParser(path)} }; } \ No newline at end of file diff --git a/TagsCloudContainer/BuildingOptions/CommonOptions.cs b/TagsCloudContainer/BuildingOptions/CommonOptions.cs new file mode 100644 index 000000000..1e5817516 --- /dev/null +++ b/TagsCloudContainer/BuildingOptions/CommonOptions.cs @@ -0,0 +1,21 @@ +using TagsCloudContainer.Drawing.Colorers; +using TagsCloudContainer.WordProcessing.WordInput; +using TagsCloudVisualization; + +namespace TagsCloudContainer.BuildingOptions; + +public class CommonOptions +{ + public IWordProvider WordProvider { get; } + + public IWordColorer? WordColorer { get; } + + public ICloudLayouter CloudLayouter { get; } + + public CommonOptions(IWordProvider wordProvider, IWordColorer? wordColorer, ICloudLayouter cloudLayouter) + { + WordProvider = wordProvider; + WordColorer = wordColorer; + CloudLayouter = cloudLayouter; + } +} \ No newline at end of file diff --git a/TagsCloudContainer/BuildingOptions/DefaultDrawingOptionsProvider.cs b/TagsCloudContainer/BuildingOptions/DefaultDrawingOptionsProvider.cs new file mode 100644 index 000000000..251617074 --- /dev/null +++ b/TagsCloudContainer/BuildingOptions/DefaultDrawingOptionsProvider.cs @@ -0,0 +1,11 @@ +namespace TagsCloudContainer.BuildingOptions; + +public class DefaultDrawingOptionsProvider : IDrawingOptionsProvider +{ + public DrawingOptions DrawingOptions { get; } + + public DefaultDrawingOptionsProvider(DrawingOptions drawingOptions) + { + DrawingOptions = drawingOptions; + } +} \ No newline at end of file diff --git a/TagsCloudContainer/DrawingOptions/Options.cs b/TagsCloudContainer/BuildingOptions/DrawingOptions.cs similarity index 67% rename from TagsCloudContainer/DrawingOptions/Options.cs rename to TagsCloudContainer/BuildingOptions/DrawingOptions.cs index 60c57a56b..618723ac3 100644 --- a/TagsCloudContainer/DrawingOptions/Options.cs +++ b/TagsCloudContainer/BuildingOptions/DrawingOptions.cs @@ -1,8 +1,8 @@ using System.Drawing; -namespace TagsCloudContainer.DrawingOptions; +namespace TagsCloudContainer.BuildingOptions; -public class Options +public class DrawingOptions { public Color FontColor { get; } public Color BackgroundColor { get; } @@ -10,7 +10,7 @@ public class Options public Font Font { get; } public int FrequencyScaling { get; } - public Options(Color fontColor, Color backgroundColor, Size imageSize, Font font, int frequencyScaling) + public DrawingOptions(Color fontColor, Color backgroundColor, Size imageSize, Font font, int frequencyScaling) { FontColor = fontColor; BackgroundColor = backgroundColor; diff --git a/TagsCloudContainer/BuildingOptions/ICommonOptionsProvider.cs b/TagsCloudContainer/BuildingOptions/ICommonOptionsProvider.cs new file mode 100644 index 000000000..e0ffd5a27 --- /dev/null +++ b/TagsCloudContainer/BuildingOptions/ICommonOptionsProvider.cs @@ -0,0 +1,8 @@ +using TagsCloudContainer.WordProcessing.WordInput; + +namespace TagsCloudContainer.BuildingOptions; + +public interface ICommonOptionsProvider +{ + CommonOptions CommonOptions { get; } +} \ No newline at end of file diff --git a/TagsCloudContainer/BuildingOptions/IDrawingOptionsProvider.cs b/TagsCloudContainer/BuildingOptions/IDrawingOptionsProvider.cs new file mode 100644 index 000000000..1c215690c --- /dev/null +++ b/TagsCloudContainer/BuildingOptions/IDrawingOptionsProvider.cs @@ -0,0 +1,6 @@ +namespace TagsCloudContainer.BuildingOptions; + +public interface IDrawingOptionsProvider +{ + public DrawingOptions DrawingOptions { get; } +} \ No newline at end of file diff --git a/TagsCloudContainer/Common/DiContainerBuilder.cs b/TagsCloudContainer/Common/DiContainerBuilder.cs new file mode 100644 index 000000000..f20cd1a93 --- /dev/null +++ b/TagsCloudContainer/Common/DiContainerBuilder.cs @@ -0,0 +1,26 @@ +using Autofac; +using TagsCloudContainer.Drawing; +using TagsCloudContainer.TagCloudForming; +using TagsCloudContainer.WordProcessing.WordFiltering; +using TagsCloudContainer.WordProcessing.WordGrouping; +using TagsCloudContainer.WordProcessing.WordInput; + +namespace TagsCloudContainer.Common; + +public static class DiContainerBuilder +{ + public static ContainerBuilder BuildContainer() + { + var containerBuilder = new ContainerBuilder(); + + containerBuilder.RegisterType().As().SingleInstance(); + containerBuilder.RegisterInstance(new DefaultWordFilter(new TxtFileWordParser("../../../../TagsCloudContainer/Resources/filter.txt"))) + .As() + .SingleInstance(); + containerBuilder.RegisterType().As().SingleInstance(); + containerBuilder.RegisterType().As().SingleInstance(); + containerBuilder.RegisterType().As().SingleInstance(); + + return containerBuilder; + } +} \ No newline at end of file diff --git a/TagsCloudContainer/Drawing/Colorers/DefaultWordColorer.cs b/TagsCloudContainer/Drawing/Colorers/DefaultWordColorer.cs index 17ecdc9b6..da64ac159 100644 --- a/TagsCloudContainer/Drawing/Colorers/DefaultWordColorer.cs +++ b/TagsCloudContainer/Drawing/Colorers/DefaultWordColorer.cs @@ -1,5 +1,5 @@ using System.Drawing; -using TagsCloudContainer.DrawingOptions; +using TagsCloudContainer.BuildingOptions; namespace TagsCloudContainer.Drawing.Colorers; @@ -7,9 +7,9 @@ public class DefaultWordColorer : IWordColorer { private readonly Color _color; - public DefaultWordColorer(IOptionsProvider optionsProvider) + public DefaultWordColorer(IDrawingOptionsProvider drawingOptionsProvider) { - _color = optionsProvider.Options.FontColor; + _color = drawingOptionsProvider.DrawingOptions.FontColor; } public Color GetWordColor(string word, int wordFrequency) diff --git a/TagsCloudContainer/Drawing/DefaultImageDrawer.cs b/TagsCloudContainer/Drawing/DefaultImageDrawer.cs index 04b0efd89..50ad9fb16 100644 --- a/TagsCloudContainer/Drawing/DefaultImageDrawer.cs +++ b/TagsCloudContainer/Drawing/DefaultImageDrawer.cs @@ -1,8 +1,8 @@ using System.Drawing; using System.Drawing.Imaging; +using TagsCloudContainer.BuildingOptions; using TagsCloudContainer.Common; using TagsCloudContainer.Drawing.Colorers; -using TagsCloudContainer.DrawingOptions; using TagsCloudContainer.TagCloudForming; namespace TagsCloudContainer.Drawing; @@ -10,27 +10,31 @@ namespace TagsCloudContainer.Drawing; public class DefaultImageDrawer : IImageDrawer { private readonly IReadOnlyDictionary _distributedWords; - private readonly Options _options; - private readonly IWordColorer _colorer; - public DefaultImageDrawer(IWordCloudDistributorProvider cloudDistributorProvider, IOptionsProvider optionsProvider, IWordColorer colorer) + private readonly DrawingOptions _drawingOptions; + private readonly IWordColorer? _colorer; + + public DefaultImageDrawer(IWordCloudDistributorProvider cloudDistributorProvider, + IDrawingOptionsProvider drawingOptionsProvider, ICommonOptionsProvider commonOptionsProvider) { _distributedWords = cloudDistributorProvider.DistributedWords; - _options = optionsProvider.Options; - _colorer = colorer; + _drawingOptions = drawingOptionsProvider.DrawingOptions; + _colorer = commonOptionsProvider.CommonOptions.WordColorer; } public Bitmap DrawImage() { - var bitmap = new Bitmap(_options.ImageSize.Width, _options.ImageSize.Height); - var offset = new Point(_options.ImageSize.Width / 2, _options.ImageSize.Height / 2); + var bitmap = new Bitmap(_drawingOptions.ImageSize.Width, _drawingOptions.ImageSize.Height); + var offset = new Point(_drawingOptions.ImageSize.Width / 2, _drawingOptions.ImageSize.Height / 2); var graphics = Graphics.FromImage(bitmap); - graphics.FillRectangle(new SolidBrush(_options.BackgroundColor), 0, 0, bitmap.Width, bitmap.Height); + graphics.FillRectangle(new SolidBrush(_drawingOptions.BackgroundColor), 0, 0, bitmap.Width, bitmap.Height); foreach (var (value, word) in _distributedWords) { - var sizeAdd = _options.FrequencyScaling * (word.Frequency - 1); - var newFont = new Font(_options.Font.FontFamily, _options.Font.Size + sizeAdd, _options.Font.Style); - graphics.DrawString(value, newFont, new SolidBrush(_colorer.GetWordColor(value, word.Frequency)), + var sizeAdd = _drawingOptions.FrequencyScaling * (word.Frequency - 1); + var newFont = new Font(_drawingOptions.Font.FontFamily, _drawingOptions.Font.Size + sizeAdd, + _drawingOptions.Font.Style); + var color = _colorer?.GetWordColor(value, word.Frequency) ?? _drawingOptions.FontColor; + graphics.DrawString(value, newFont, new SolidBrush(color), word.Rectangle with {X = word.Rectangle.X + offset.X, Y = word.Rectangle.Y + offset.Y}); } diff --git a/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs b/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs deleted file mode 100644 index c0149c65d..000000000 --- a/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace TagsCloudContainer.DrawingOptions; - -public class DefaultOptionsProvider : IOptionsProvider -{ - public Options Options { get; } - - public DefaultOptionsProvider(Options options) - { - Options = options; - } -} \ No newline at end of file diff --git a/TagsCloudContainer/DrawingOptions/IOptionsProvider.cs b/TagsCloudContainer/DrawingOptions/IOptionsProvider.cs deleted file mode 100644 index 87e09a5cf..000000000 --- a/TagsCloudContainer/DrawingOptions/IOptionsProvider.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace TagsCloudContainer.DrawingOptions; - -public interface IOptionsProvider -{ - public Options Options { get; } -} \ No newline at end of file diff --git a/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs b/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs index c28f72044..c179b2dbf 100644 --- a/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs +++ b/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs @@ -1,5 +1,5 @@ -using TagsCloudContainer.Common; -using TagsCloudContainer.DrawingOptions; +using TagsCloudContainer.BuildingOptions; +using TagsCloudContainer.Common; using TagsCloudContainer.Utils; using TagsCloudContainer.WordProcessing.WordGrouping; using TagsCloudVisualization; @@ -10,14 +10,14 @@ public class DefaultWordCloudDistributor : IWordCloudDistributorProvider { private readonly Dictionary _words; private readonly ICloudLayouter _cloudLayouter; - private readonly Options _options; + private readonly DrawingOptions _drawingOptions; - public DefaultWordCloudDistributor(IProcessedWordProvider processedWord, ICloudLayouter cloudLayouter, - IOptionsProvider optionsProvider) + public DefaultWordCloudDistributor(IProcessedWordProvider processedWord, ICommonOptionsProvider commonOptionsProvider, + IDrawingOptionsProvider drawingOptionsProvider) { _words = processedWord.ProcessedWords; - _cloudLayouter = cloudLayouter; - _options = optionsProvider.Options; + _cloudLayouter = commonOptionsProvider.CommonOptions.CloudLayouter; + _drawingOptions = drawingOptionsProvider.DrawingOptions; } public IReadOnlyDictionary DistributedWords => DistributeWords().AsReadOnly(); @@ -29,7 +29,7 @@ private Dictionary DistributeWords() foreach (var (word, frequency) in _words) { var newWord = new WordData(_cloudLayouter.PutNextRectangle(DrawingUtils.GetStringSize(word, frequency, - _options.FrequencyScaling, _options.Font)), frequency); + _drawingOptions.FrequencyScaling, _drawingOptions.Font)), frequency); distributed.Add(word, newWord); } diff --git a/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs b/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs index 92d3d2e15..d81a373a4 100644 --- a/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs +++ b/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs @@ -1,4 +1,5 @@ -using TagsCloudContainer.WordProcessing.WordFiltering; +using TagsCloudContainer.BuildingOptions; +using TagsCloudContainer.WordProcessing.WordFiltering; using TagsCloudContainer.WordProcessing.WordInput; namespace TagsCloudContainer.WordProcessing.WordGrouping; @@ -7,10 +8,15 @@ public class DefaultWordProcessor : IProcessedWordProvider { private readonly string[] _words; private readonly IEnumerable _filters; - - public DefaultWordProcessor(IWordProvider words, IEnumerable filters) + + public DefaultWordProcessor(ICommonOptionsProvider commonOptionsProvider, IEnumerable filters) : this( + commonOptionsProvider.CommonOptions.WordProvider, filters) { - _words = words.Words.Select(w => w.ToLower()).ToArray(); + } + + public DefaultWordProcessor(IWordProvider wordProvider, IEnumerable filters) + { + _words = wordProvider.Words.Select(w => w.ToLower()).ToArray(); _filters = filters; } @@ -21,7 +27,7 @@ private Dictionary ProcessWords() var filtered = _filters.Aggregate(_words, (current, filter) => filter.FilterWords(current)); return GroupWords(filtered); } - + private static Dictionary GroupWords(IEnumerable filtered) { var frequency = new Dictionary(); From 335d0a4eb84973e56b97586fc798c807d51a700f Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 26 Jan 2024 23:51:59 +0500 Subject: [PATCH 13/19] Settings Provider refactored --- TagsCloudConsoleUI/ConsoleUi.cs | 165 ------------------ TagsCloudConsoleUI/IUiManager.cs | 6 - TagsCloudConsoleUI/Program.cs | 26 ++- .../Providers/CloudAlgorithmProviders.cs | 7 +- .../Providers/ColorerProviders.cs | 8 +- .../Providers/ConsoleOptionsProvider.cs | 8 - .../Providers/ConsoleSettingsProvider.cs | 141 +++++++++++++++ TagsCloudConsoleUI/Providers/WordProviders.cs | 13 +- .../BuildingOptions/CommonOptions.cs | 21 +++ .../DefaultDrawingOptionsProvider.cs | 11 ++ .../DrawingOptions.cs} | 6 +- .../BuildingOptions/ICommonOptionsProvider.cs | 8 + .../IDrawingOptionsProvider.cs | 6 + .../Common/DiContainerBuilder.cs | 26 +++ .../Drawing/Colorers/DefaultWordColorer.cs | 6 +- .../Drawing/DefaultImageDrawer.cs | 28 +-- .../DrawingOptions/DefaultOptionsProvider.cs | 11 -- .../DrawingOptions/IOptionsProvider.cs | 6 - .../DefaultWordCloudDistributor.cs | 16 +- .../WordGrouping/DefaultWordProcessor.cs | 16 +- 20 files changed, 291 insertions(+), 244 deletions(-) delete mode 100644 TagsCloudConsoleUI/ConsoleUi.cs delete mode 100644 TagsCloudConsoleUI/IUiManager.cs delete mode 100644 TagsCloudConsoleUI/Providers/ConsoleOptionsProvider.cs create mode 100644 TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs create mode 100644 TagsCloudContainer/BuildingOptions/CommonOptions.cs create mode 100644 TagsCloudContainer/BuildingOptions/DefaultDrawingOptionsProvider.cs rename TagsCloudContainer/{DrawingOptions/Options.cs => BuildingOptions/DrawingOptions.cs} (67%) create mode 100644 TagsCloudContainer/BuildingOptions/ICommonOptionsProvider.cs create mode 100644 TagsCloudContainer/BuildingOptions/IDrawingOptionsProvider.cs create mode 100644 TagsCloudContainer/Common/DiContainerBuilder.cs delete mode 100644 TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs delete mode 100644 TagsCloudContainer/DrawingOptions/IOptionsProvider.cs diff --git a/TagsCloudConsoleUI/ConsoleUi.cs b/TagsCloudConsoleUI/ConsoleUi.cs deleted file mode 100644 index ad70a6275..000000000 --- a/TagsCloudConsoleUI/ConsoleUi.cs +++ /dev/null @@ -1,165 +0,0 @@ -using System.Drawing; -using System.Drawing.Imaging; -using System.Text; -using Autofac; -using McMaster.Extensions.CommandLineUtils; -using TagsCloudConsoleUI.Providers; -using TagsCloudContainer.Drawing; -using TagsCloudContainer.Drawing.Colorers; -using TagsCloudContainer.DrawingOptions; -using TagsCloudContainer.TagCloudForming; -using TagsCloudContainer.Utils; -using TagsCloudContainer.WordProcessing.WordFiltering; -using TagsCloudContainer.WordProcessing.WordGrouping; -using TagsCloudContainer.WordProcessing.WordInput; - -namespace TagsCloudConsoleUI; - -public class ConsoleUi : IUiManager -{ - public void StartUi() - { - var containerBuilder = new ContainerBuilder(); - RegisterWordProvider(GetPathToWordsFile(), containerBuilder); - - RegisterCloudBuildingOptions(containerBuilder); - - RegisterAlgorithm(CloudAlgorithmProviders.RegisteredProviders, "Choose the cloud forming algorithm:", - containerBuilder); - - containerBuilder.RegisterType().As().SingleInstance(); - containerBuilder.RegisterInstance(new DefaultWordFilter(new TxtFileWordParser("../../../../TagsCloudContainer/Resources/filter.txt"))).As() - .SingleInstance(); - containerBuilder.RegisterType().As().SingleInstance(); - containerBuilder.RegisterType().As().SingleInstance(); - containerBuilder.RegisterType().As().SingleInstance(); - - var container = containerBuilder.Build(); - - BuildTagCloud(container.Resolve(), ".", "image.png", ImageFormat.Png); - } - - private void RegisterCloudBuildingOptions(ContainerBuilder containerBuilder) - { - var font = GetFont(); - var backgroundColor = GetRgbColor("Enter background color in RGB format separated by space"); - - RegisterColoringAlgorithm(out var fontColor, containerBuilder); - - var imageSide = - GetInteger( - "Enter the image's desired size in px. The image will be a square. It must range from 500 px to 5000 px.", - 500, 5000); - var imageSize = new Size(imageSide, imageSide); - var frequencyScaling = - GetInteger( - "Enter the frequency scaling (a positive integer). It Determines the scale to word frequency ratio.", 1, - 100); - - containerBuilder - .RegisterInstance(new DefaultOptionsProvider(new Options(fontColor, backgroundColor, imageSize, font, frequencyScaling))) - .As().SingleInstance(); - } - - private static void BuildTagCloud(IImageDrawer imageDrawer, string dirPath, string fileName, ImageFormat imageFormat) - { - var bitmap = imageDrawer.DrawImage(); - DefaultImageDrawer.SaveImage(bitmap, dirPath, fileName, imageFormat); - Console.WriteLine($"The image has been saved to \"{dirPath}\""); - } - - private static void RegisterAlgorithm(IReadOnlyDictionary> registeredAlgorithms, - string prompt, ContainerBuilder containerBuilder) - { - var sb = new StringBuilder($"{prompt}\n"); - foreach (var algorithmProvider in registeredAlgorithms.Keys) - sb.AppendLine(algorithmProvider); - - while (true) - { - var algorithm = Prompt.GetString(sb.ToString(), "", promptColor: ConsoleColor.DarkGreen); - if (registeredAlgorithms.TryGetValue(algorithm!, out var provider)) - { - provider(containerBuilder); - break; - } - - Console.WriteLine("Specified algorithm isn't supported. Try again."); - } - } - - private static int GetInteger(string prompt, int lowerAllowedBoundary, int upperAllowedBoundary) - { - while (true) - { - var intString = Prompt.GetString(prompt, "", ConsoleColor.DarkGreen); - if (int.TryParse(intString, out var parsed) && parsed >= lowerAllowedBoundary && - parsed <= upperAllowedBoundary) - return parsed; - Console.WriteLine("Given number is invalid. Try again."); - } - } - - private void RegisterColoringAlgorithm(out Color fontColor, ContainerBuilder containerBuilder) - { - if (Prompt.GetYesNo("Do you want to use a custom coloring algorithm?", false, ConsoleColor.DarkGreen)) - { - RegisterAlgorithm(ColorerProviders.RegisteredProviders, "Choose the algorithm:", containerBuilder); - - fontColor = Color.White; - return; - } - - containerBuilder.RegisterType().As().SingleInstance(); - fontColor = GetRgbColor("Enter font color in RGB format separated by space"); - } - - private static Color GetRgbColor(string prompt) - { - while (true) - { - var colorString = Prompt.GetString(prompt, "", - promptColor: ConsoleColor.DarkGreen); - if (DrawingUtils.TryParseRgb(colorString!, out var color)) - return color; - Console.WriteLine("Ivalid color format. Try again."); - } - } - - private static Font GetFont() - { - var fontName = Prompt.GetString("Enter font name", promptColor: ConsoleColor.DarkGreen) ?? - "Microsoft Sans Serif"; - while (true) - { - var fontSizeStr = Prompt.GetString("Enter font size in pt", promptColor: ConsoleColor.DarkGreen); - if (float.TryParse(fontSizeStr, out var fontSize) && fontSize > 0) - return new Font(fontName, fontSize); - Console.WriteLine("Font size must be a correct positive number. Try again."); - } - } - - private void RegisterWordProvider(string path, ContainerBuilder containerBuilder) - { - var ext = Path.GetExtension(path); - if (WordProviders.RegisteredProviders.TryGetValue(ext, out var provider)) - { - provider(containerBuilder, path); - return; - } - - Console.WriteLine("This extension is not supported. Try another one."); - StartUi(); - } - - private static string GetPathToWordsFile() - { - while (true) - { - var path = Prompt.GetString("Enter the path to the file with words", promptColor: ConsoleColor.DarkGreen); - if (File.Exists(path)) - return path; - Console.WriteLine("Provided path is invalid. Try again."); - } - } -} \ No newline at end of file diff --git a/TagsCloudConsoleUI/IUiManager.cs b/TagsCloudConsoleUI/IUiManager.cs deleted file mode 100644 index 073565b24..000000000 --- a/TagsCloudConsoleUI/IUiManager.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace TagsCloudConsoleUI; - -public interface IUiManager -{ - public void StartUi(); -} \ No newline at end of file diff --git a/TagsCloudConsoleUI/Program.cs b/TagsCloudConsoleUI/Program.cs index 56c4c545f..cb99a0a90 100644 --- a/TagsCloudConsoleUI/Program.cs +++ b/TagsCloudConsoleUI/Program.cs @@ -1,4 +1,11 @@ -namespace TagsCloudConsoleUI; +using System.Drawing.Imaging; +using Autofac; +using TagsCloudConsoleUI.Providers; +using TagsCloudContainer.BuildingOptions; +using TagsCloudContainer.Common; +using TagsCloudContainer.Drawing; + +namespace TagsCloudConsoleUI; public static class Program { @@ -6,11 +13,26 @@ public static void Main() { try { - new ConsoleUi().StartUi(); + var containterBuilder = DiContainerBuilder.RegisterDefaultDependencies(); + + containterBuilder.RegisterType() + .As() + .SingleInstance(); + + var container = containterBuilder.Build(); + + BuildTagCloud(container.Resolve(), ".", "image.png", ImageFormat.Png); } catch (Exception e) { Console.WriteLine(e); } } + + private static void BuildTagCloud(IImageDrawer imageDrawer, string dirPath, string fileName, ImageFormat imageFormat) + { + var bitmap = imageDrawer.DrawImage(); + DefaultImageDrawer.SaveImage(bitmap, dirPath, fileName, imageFormat); + Console.WriteLine($"The image has been saved to \"{dirPath}\""); + } } \ No newline at end of file diff --git a/TagsCloudConsoleUI/Providers/CloudAlgorithmProviders.cs b/TagsCloudConsoleUI/Providers/CloudAlgorithmProviders.cs index 1c061b3ed..0fd83d7f1 100644 --- a/TagsCloudConsoleUI/Providers/CloudAlgorithmProviders.cs +++ b/TagsCloudConsoleUI/Providers/CloudAlgorithmProviders.cs @@ -1,14 +1,13 @@ using System.Drawing; -using Autofac; using TagsCloudVisualization; namespace TagsCloudConsoleUI.Providers; public static class CloudAlgorithmProviders { - public static readonly IReadOnlyDictionary> RegisteredProviders = - new Dictionary> + public static readonly IReadOnlyDictionary RegisteredProviders = + new Dictionary { - {"Circular", builder => builder.RegisterInstance(new CircularCloudLayouter(Point.Empty)).As().SingleInstance()} + {"Circular", new CircularCloudLayouter(Point.Empty)} }; } \ No newline at end of file diff --git a/TagsCloudConsoleUI/Providers/ColorerProviders.cs b/TagsCloudConsoleUI/Providers/ColorerProviders.cs index 0f6fe939f..2cef9eb14 100644 --- a/TagsCloudConsoleUI/Providers/ColorerProviders.cs +++ b/TagsCloudConsoleUI/Providers/ColorerProviders.cs @@ -5,10 +5,10 @@ namespace TagsCloudConsoleUI.Providers; public static class ColorerProviders { - public static readonly IReadOnlyDictionary> RegisteredProviders = - new Dictionary> + public static readonly IReadOnlyDictionary RegisteredProviders = + new Dictionary { - {"Random", builder => builder.RegisterType().As().SingleInstance()}, - {"Bicolor", builder => builder.RegisterInstance(new BicolorColorer()).As().SingleInstance()} + {"Random", new RandomWordColorer()}, + {"Bicolor", new BicolorColorer()} }; } \ No newline at end of file diff --git a/TagsCloudConsoleUI/Providers/ConsoleOptionsProvider.cs b/TagsCloudConsoleUI/Providers/ConsoleOptionsProvider.cs deleted file mode 100644 index 2d5199ff9..000000000 --- a/TagsCloudConsoleUI/Providers/ConsoleOptionsProvider.cs +++ /dev/null @@ -1,8 +0,0 @@ -using TagsCloudContainer.DrawingOptions; - -namespace TagsCloudConsoleUI.Providers; - -public class ConsoleOptionsProvider : IOptionsProvider -{ - public Options Options { get; } -} \ No newline at end of file diff --git a/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs b/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs new file mode 100644 index 000000000..977811540 --- /dev/null +++ b/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs @@ -0,0 +1,141 @@ +using System.Drawing; +using System.Text; +using McMaster.Extensions.CommandLineUtils; +using TagsCloudContainer.BuildingOptions; +using TagsCloudContainer.Drawing.Colorers; +using TagsCloudContainer.Utils; +using TagsCloudContainer.WordProcessing.WordInput; + +namespace TagsCloudConsoleUI.Providers; + +public class ConsoleSettingsProvider : IDrawingOptionsProvider, ICommonOptionsProvider +{ + public DrawingOptions DrawingOptions => _drawingOptions ??= GetDrawingOptions(); + + public CommonOptions CommonOptions => _commonOptions ??= GetCommonOptions(); + + private DrawingOptions? _drawingOptions; + + private CommonOptions? _commonOptions; + + private bool _isCustomColoringUsed; + + private DrawingOptions GetDrawingOptions() + { + var font = GetFont(); + var backgroundColor = GetRgbColor("Enter background color in RGB format separated by space"); + + var fontColor = GetFontColor(); + + var imageSide = GetInteger( + "Enter the image's desired size in px. The image will be a square. It must range from 500 px to 5000 px.", + 500, 5000); + + var imageSize = new Size(imageSide, imageSide); + + var frequencyScaling = GetInteger( + "Enter the frequency scaling (a positive integer). It Determines the scale to word frequency ratio.", + 1, 100); + + return new DrawingOptions(fontColor, backgroundColor, imageSize, font, frequencyScaling); + } + + private CommonOptions GetCommonOptions() + { + var wordProvider = GetWordProvider(); + var wordColorer = GetWordColorer(); + var cloudLayouter = GetAlgorithm(CloudAlgorithmProviders.RegisteredProviders, + "Choose the cloud forming algorithm:"); + + return new CommonOptions(wordProvider, wordColorer, cloudLayouter); + } + + private Color GetFontColor() + { + return _isCustomColoringUsed + ? Color.White + : GetRgbColor("Enter font color in RGB format separated by space"); + } + + private static int GetInteger(string prompt, int lowerAllowedBoundary, int upperAllowedBoundary) + { + while (true) + { + var intString = Prompt.GetString(prompt, "", ConsoleColor.DarkGreen); + if (int.TryParse(intString, out var parsed) && parsed >= lowerAllowedBoundary && + parsed <= upperAllowedBoundary) + return parsed; + Console.WriteLine("Given number is invalid. Try again."); + } + } + + private static Color GetRgbColor(string prompt) + { + while (true) + { + var colorString = Prompt.GetString(prompt, "", + promptColor: ConsoleColor.DarkGreen); + if (DrawingUtils.TryParseRgb(colorString!, out var color)) + return color; + Console.WriteLine("Ivalid color format. Try again."); + } + } + + private static Font GetFont() + { + var fontName = Prompt.GetString("Enter font name", promptColor: ConsoleColor.DarkGreen) ?? + "Microsoft Sans Serif"; + while (true) + { + var fontSizeStr = Prompt.GetString("Enter font size in pt", promptColor: ConsoleColor.DarkGreen); + if (float.TryParse(fontSizeStr, out var fontSize) && fontSize > 0) + return new Font(fontName, fontSize); + Console.WriteLine("Font size must be a correct positive number. Try again."); + } + } + + private IWordColorer? GetWordColorer() + { + if (!Prompt.GetYesNo("Would you like to use a custom coloring algorithm?", false, ConsoleColor.DarkGreen)) + return null; + + _isCustomColoringUsed = true; + return GetAlgorithm(ColorerProviders.RegisteredProviders, "Choose the algorithm:"); + } + + private static T GetAlgorithm(IReadOnlyDictionary registeredAlgorithms, + string prompt) + { + var sb = new StringBuilder($"{prompt}\n"); + foreach (var algorithmProvider in registeredAlgorithms.Keys) + sb.AppendLine(algorithmProvider); + + while (true) + { + var algorithm = Prompt.GetString(sb.ToString(), "", promptColor: ConsoleColor.DarkGreen); + if (registeredAlgorithms.TryGetValue(algorithm!, out var provider)) + return provider; + + Console.WriteLine("Specified algorithm isn't supported. Try again."); + } + } + + private static IWordProvider GetWordProvider() + { + while (true) + { + var path = Prompt.GetString("Enter the path to the file with words", promptColor: ConsoleColor.DarkGreen); + if (!File.Exists(path)) + { + Console.WriteLine("Provided path is invalid. Try again."); + continue; + } + + var ext = Path.GetExtension(path); + if (WordProviders.RegisteredProviders.TryGetValue(ext, out var provider)) + return provider(path); + + Console.WriteLine("This extension is not supported. Try again."); + } + } +} \ No newline at end of file diff --git a/TagsCloudConsoleUI/Providers/WordProviders.cs b/TagsCloudConsoleUI/Providers/WordProviders.cs index 2e1cfd150..41fc11787 100644 --- a/TagsCloudConsoleUI/Providers/WordProviders.cs +++ b/TagsCloudConsoleUI/Providers/WordProviders.cs @@ -1,15 +1,14 @@ -using Autofac; -using TagsCloudContainer.WordProcessing.WordInput; +using TagsCloudContainer.WordProcessing.WordInput; namespace TagsCloudConsoleUI.Providers; public static class WordProviders { - public static readonly IReadOnlyDictionary> RegisteredProviders = - new Dictionary> + public static readonly IReadOnlyDictionary> RegisteredProviders = + new Dictionary> { - {".txt", (builder, path) => builder.RegisterInstance(new TxtFileWordParser(path)).As().SingleInstance()}, - {".docx", (builder, path) => builder.RegisterInstance(new DocxFileWordParser(path)).As().SingleInstance()}, - {".doc", (builder, path) => builder.RegisterInstance(new DocxFileWordParser(path)).As().SingleInstance()} + {".txt", path=> new TxtFileWordParser(path)}, + {".docx", path => new DocxFileWordParser(path)}, + {".doc", path => new DocxFileWordParser(path)} }; } \ No newline at end of file diff --git a/TagsCloudContainer/BuildingOptions/CommonOptions.cs b/TagsCloudContainer/BuildingOptions/CommonOptions.cs new file mode 100644 index 000000000..1e5817516 --- /dev/null +++ b/TagsCloudContainer/BuildingOptions/CommonOptions.cs @@ -0,0 +1,21 @@ +using TagsCloudContainer.Drawing.Colorers; +using TagsCloudContainer.WordProcessing.WordInput; +using TagsCloudVisualization; + +namespace TagsCloudContainer.BuildingOptions; + +public class CommonOptions +{ + public IWordProvider WordProvider { get; } + + public IWordColorer? WordColorer { get; } + + public ICloudLayouter CloudLayouter { get; } + + public CommonOptions(IWordProvider wordProvider, IWordColorer? wordColorer, ICloudLayouter cloudLayouter) + { + WordProvider = wordProvider; + WordColorer = wordColorer; + CloudLayouter = cloudLayouter; + } +} \ No newline at end of file diff --git a/TagsCloudContainer/BuildingOptions/DefaultDrawingOptionsProvider.cs b/TagsCloudContainer/BuildingOptions/DefaultDrawingOptionsProvider.cs new file mode 100644 index 000000000..251617074 --- /dev/null +++ b/TagsCloudContainer/BuildingOptions/DefaultDrawingOptionsProvider.cs @@ -0,0 +1,11 @@ +namespace TagsCloudContainer.BuildingOptions; + +public class DefaultDrawingOptionsProvider : IDrawingOptionsProvider +{ + public DrawingOptions DrawingOptions { get; } + + public DefaultDrawingOptionsProvider(DrawingOptions drawingOptions) + { + DrawingOptions = drawingOptions; + } +} \ No newline at end of file diff --git a/TagsCloudContainer/DrawingOptions/Options.cs b/TagsCloudContainer/BuildingOptions/DrawingOptions.cs similarity index 67% rename from TagsCloudContainer/DrawingOptions/Options.cs rename to TagsCloudContainer/BuildingOptions/DrawingOptions.cs index 60c57a56b..618723ac3 100644 --- a/TagsCloudContainer/DrawingOptions/Options.cs +++ b/TagsCloudContainer/BuildingOptions/DrawingOptions.cs @@ -1,8 +1,8 @@ using System.Drawing; -namespace TagsCloudContainer.DrawingOptions; +namespace TagsCloudContainer.BuildingOptions; -public class Options +public class DrawingOptions { public Color FontColor { get; } public Color BackgroundColor { get; } @@ -10,7 +10,7 @@ public class Options public Font Font { get; } public int FrequencyScaling { get; } - public Options(Color fontColor, Color backgroundColor, Size imageSize, Font font, int frequencyScaling) + public DrawingOptions(Color fontColor, Color backgroundColor, Size imageSize, Font font, int frequencyScaling) { FontColor = fontColor; BackgroundColor = backgroundColor; diff --git a/TagsCloudContainer/BuildingOptions/ICommonOptionsProvider.cs b/TagsCloudContainer/BuildingOptions/ICommonOptionsProvider.cs new file mode 100644 index 000000000..e0ffd5a27 --- /dev/null +++ b/TagsCloudContainer/BuildingOptions/ICommonOptionsProvider.cs @@ -0,0 +1,8 @@ +using TagsCloudContainer.WordProcessing.WordInput; + +namespace TagsCloudContainer.BuildingOptions; + +public interface ICommonOptionsProvider +{ + CommonOptions CommonOptions { get; } +} \ No newline at end of file diff --git a/TagsCloudContainer/BuildingOptions/IDrawingOptionsProvider.cs b/TagsCloudContainer/BuildingOptions/IDrawingOptionsProvider.cs new file mode 100644 index 000000000..1c215690c --- /dev/null +++ b/TagsCloudContainer/BuildingOptions/IDrawingOptionsProvider.cs @@ -0,0 +1,6 @@ +namespace TagsCloudContainer.BuildingOptions; + +public interface IDrawingOptionsProvider +{ + public DrawingOptions DrawingOptions { get; } +} \ No newline at end of file diff --git a/TagsCloudContainer/Common/DiContainerBuilder.cs b/TagsCloudContainer/Common/DiContainerBuilder.cs new file mode 100644 index 000000000..73e51ab63 --- /dev/null +++ b/TagsCloudContainer/Common/DiContainerBuilder.cs @@ -0,0 +1,26 @@ +using Autofac; +using TagsCloudContainer.Drawing; +using TagsCloudContainer.TagCloudForming; +using TagsCloudContainer.WordProcessing.WordFiltering; +using TagsCloudContainer.WordProcessing.WordGrouping; +using TagsCloudContainer.WordProcessing.WordInput; + +namespace TagsCloudContainer.Common; + +public static class DiContainerBuilder +{ + public static ContainerBuilder RegisterDefaultDependencies() + { + var containerBuilder = new ContainerBuilder(); + + containerBuilder.RegisterType().As().SingleInstance(); + containerBuilder.RegisterInstance(new DefaultWordFilter(new TxtFileWordParser("../../../../TagsCloudContainer/Resources/filter.txt"))) + .As() + .SingleInstance(); + containerBuilder.RegisterType().As().SingleInstance(); + containerBuilder.RegisterType().As().SingleInstance(); + containerBuilder.RegisterType().As().SingleInstance(); + + return containerBuilder; + } +} \ No newline at end of file diff --git a/TagsCloudContainer/Drawing/Colorers/DefaultWordColorer.cs b/TagsCloudContainer/Drawing/Colorers/DefaultWordColorer.cs index 17ecdc9b6..da64ac159 100644 --- a/TagsCloudContainer/Drawing/Colorers/DefaultWordColorer.cs +++ b/TagsCloudContainer/Drawing/Colorers/DefaultWordColorer.cs @@ -1,5 +1,5 @@ using System.Drawing; -using TagsCloudContainer.DrawingOptions; +using TagsCloudContainer.BuildingOptions; namespace TagsCloudContainer.Drawing.Colorers; @@ -7,9 +7,9 @@ public class DefaultWordColorer : IWordColorer { private readonly Color _color; - public DefaultWordColorer(IOptionsProvider optionsProvider) + public DefaultWordColorer(IDrawingOptionsProvider drawingOptionsProvider) { - _color = optionsProvider.Options.FontColor; + _color = drawingOptionsProvider.DrawingOptions.FontColor; } public Color GetWordColor(string word, int wordFrequency) diff --git a/TagsCloudContainer/Drawing/DefaultImageDrawer.cs b/TagsCloudContainer/Drawing/DefaultImageDrawer.cs index 04b0efd89..50ad9fb16 100644 --- a/TagsCloudContainer/Drawing/DefaultImageDrawer.cs +++ b/TagsCloudContainer/Drawing/DefaultImageDrawer.cs @@ -1,8 +1,8 @@ using System.Drawing; using System.Drawing.Imaging; +using TagsCloudContainer.BuildingOptions; using TagsCloudContainer.Common; using TagsCloudContainer.Drawing.Colorers; -using TagsCloudContainer.DrawingOptions; using TagsCloudContainer.TagCloudForming; namespace TagsCloudContainer.Drawing; @@ -10,27 +10,31 @@ namespace TagsCloudContainer.Drawing; public class DefaultImageDrawer : IImageDrawer { private readonly IReadOnlyDictionary _distributedWords; - private readonly Options _options; - private readonly IWordColorer _colorer; - public DefaultImageDrawer(IWordCloudDistributorProvider cloudDistributorProvider, IOptionsProvider optionsProvider, IWordColorer colorer) + private readonly DrawingOptions _drawingOptions; + private readonly IWordColorer? _colorer; + + public DefaultImageDrawer(IWordCloudDistributorProvider cloudDistributorProvider, + IDrawingOptionsProvider drawingOptionsProvider, ICommonOptionsProvider commonOptionsProvider) { _distributedWords = cloudDistributorProvider.DistributedWords; - _options = optionsProvider.Options; - _colorer = colorer; + _drawingOptions = drawingOptionsProvider.DrawingOptions; + _colorer = commonOptionsProvider.CommonOptions.WordColorer; } public Bitmap DrawImage() { - var bitmap = new Bitmap(_options.ImageSize.Width, _options.ImageSize.Height); - var offset = new Point(_options.ImageSize.Width / 2, _options.ImageSize.Height / 2); + var bitmap = new Bitmap(_drawingOptions.ImageSize.Width, _drawingOptions.ImageSize.Height); + var offset = new Point(_drawingOptions.ImageSize.Width / 2, _drawingOptions.ImageSize.Height / 2); var graphics = Graphics.FromImage(bitmap); - graphics.FillRectangle(new SolidBrush(_options.BackgroundColor), 0, 0, bitmap.Width, bitmap.Height); + graphics.FillRectangle(new SolidBrush(_drawingOptions.BackgroundColor), 0, 0, bitmap.Width, bitmap.Height); foreach (var (value, word) in _distributedWords) { - var sizeAdd = _options.FrequencyScaling * (word.Frequency - 1); - var newFont = new Font(_options.Font.FontFamily, _options.Font.Size + sizeAdd, _options.Font.Style); - graphics.DrawString(value, newFont, new SolidBrush(_colorer.GetWordColor(value, word.Frequency)), + var sizeAdd = _drawingOptions.FrequencyScaling * (word.Frequency - 1); + var newFont = new Font(_drawingOptions.Font.FontFamily, _drawingOptions.Font.Size + sizeAdd, + _drawingOptions.Font.Style); + var color = _colorer?.GetWordColor(value, word.Frequency) ?? _drawingOptions.FontColor; + graphics.DrawString(value, newFont, new SolidBrush(color), word.Rectangle with {X = word.Rectangle.X + offset.X, Y = word.Rectangle.Y + offset.Y}); } diff --git a/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs b/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs deleted file mode 100644 index c0149c65d..000000000 --- a/TagsCloudContainer/DrawingOptions/DefaultOptionsProvider.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace TagsCloudContainer.DrawingOptions; - -public class DefaultOptionsProvider : IOptionsProvider -{ - public Options Options { get; } - - public DefaultOptionsProvider(Options options) - { - Options = options; - } -} \ No newline at end of file diff --git a/TagsCloudContainer/DrawingOptions/IOptionsProvider.cs b/TagsCloudContainer/DrawingOptions/IOptionsProvider.cs deleted file mode 100644 index 87e09a5cf..000000000 --- a/TagsCloudContainer/DrawingOptions/IOptionsProvider.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace TagsCloudContainer.DrawingOptions; - -public interface IOptionsProvider -{ - public Options Options { get; } -} \ No newline at end of file diff --git a/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs b/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs index c28f72044..c179b2dbf 100644 --- a/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs +++ b/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs @@ -1,5 +1,5 @@ -using TagsCloudContainer.Common; -using TagsCloudContainer.DrawingOptions; +using TagsCloudContainer.BuildingOptions; +using TagsCloudContainer.Common; using TagsCloudContainer.Utils; using TagsCloudContainer.WordProcessing.WordGrouping; using TagsCloudVisualization; @@ -10,14 +10,14 @@ public class DefaultWordCloudDistributor : IWordCloudDistributorProvider { private readonly Dictionary _words; private readonly ICloudLayouter _cloudLayouter; - private readonly Options _options; + private readonly DrawingOptions _drawingOptions; - public DefaultWordCloudDistributor(IProcessedWordProvider processedWord, ICloudLayouter cloudLayouter, - IOptionsProvider optionsProvider) + public DefaultWordCloudDistributor(IProcessedWordProvider processedWord, ICommonOptionsProvider commonOptionsProvider, + IDrawingOptionsProvider drawingOptionsProvider) { _words = processedWord.ProcessedWords; - _cloudLayouter = cloudLayouter; - _options = optionsProvider.Options; + _cloudLayouter = commonOptionsProvider.CommonOptions.CloudLayouter; + _drawingOptions = drawingOptionsProvider.DrawingOptions; } public IReadOnlyDictionary DistributedWords => DistributeWords().AsReadOnly(); @@ -29,7 +29,7 @@ private Dictionary DistributeWords() foreach (var (word, frequency) in _words) { var newWord = new WordData(_cloudLayouter.PutNextRectangle(DrawingUtils.GetStringSize(word, frequency, - _options.FrequencyScaling, _options.Font)), frequency); + _drawingOptions.FrequencyScaling, _drawingOptions.Font)), frequency); distributed.Add(word, newWord); } diff --git a/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs b/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs index 92d3d2e15..d81a373a4 100644 --- a/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs +++ b/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs @@ -1,4 +1,5 @@ -using TagsCloudContainer.WordProcessing.WordFiltering; +using TagsCloudContainer.BuildingOptions; +using TagsCloudContainer.WordProcessing.WordFiltering; using TagsCloudContainer.WordProcessing.WordInput; namespace TagsCloudContainer.WordProcessing.WordGrouping; @@ -7,10 +8,15 @@ public class DefaultWordProcessor : IProcessedWordProvider { private readonly string[] _words; private readonly IEnumerable _filters; - - public DefaultWordProcessor(IWordProvider words, IEnumerable filters) + + public DefaultWordProcessor(ICommonOptionsProvider commonOptionsProvider, IEnumerable filters) : this( + commonOptionsProvider.CommonOptions.WordProvider, filters) { - _words = words.Words.Select(w => w.ToLower()).ToArray(); + } + + public DefaultWordProcessor(IWordProvider wordProvider, IEnumerable filters) + { + _words = wordProvider.Words.Select(w => w.ToLower()).ToArray(); _filters = filters; } @@ -21,7 +27,7 @@ private Dictionary ProcessWords() var filtered = _filters.Aggregate(_words, (current, filter) => filter.FilterWords(current)); return GroupWords(filtered); } - + private static Dictionary GroupWords(IEnumerable filtered) { var frequency = new Dictionary(); From 66b08db31e8d421f0b3f54a8c2d1c71345838e65 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 27 Jan 2024 00:23:59 +0500 Subject: [PATCH 14/19] TagsCloudContainer... renamed to TagsCloudCore --- TagsCloudConsoleUI/Program.cs | 6 +++--- TagsCloudConsoleUI/Providers/ColorerProviders.cs | 2 +- .../Providers/ConsoleSettingsProvider.cs | 8 ++++---- TagsCloudConsoleUI/Providers/WordProviders.cs | 2 +- TagsCloudConsoleUI/TagsCloudConsoleUI.csproj | 4 ++-- .../DefaultDrawingOptionsProvider.cs | 11 ----------- .../BuildingOptions/ICommonOptionsProvider.cs | 8 -------- .../BuildingOptions/CommonOptions.cs | 6 +++--- .../BuildingOptions/DrawingOptions.cs | 2 +- .../BuildingOptions/ICommonOptionsProvider.cs | 6 ++++++ .../BuildingOptions/IDrawingOptionsProvider.cs | 2 +- .../Common/DiContainerBuilder.cs | 14 +++++++------- .../Common/WordData.cs | 2 +- .../Drawing/Colorers/BicolorColorer.cs | 2 +- .../Drawing/Colorers/DefaultWordColorer.cs | 4 ++-- .../Drawing/Colorers/IWordColorer.cs | 2 +- .../Drawing/Colorers/RandomWordColorer.cs | 2 +- .../Drawing/DefaultImageDrawer.cs | 10 +++++----- .../Drawing/IImageDrawer.cs | 2 +- .../Resources/filter.txt | 0 .../DefaultWordCloudDistributor.cs | 10 +++++----- .../IWordCloudDistributorProvider.cs | 4 ++-- .../TagsCloudCore.csproj | 1 + .../Utils/DrawingUtils.cs | 2 +- .../Utils/WordProcessingUtils.cs | 2 +- .../WordFiltering/DefaultWordFilter.cs | 6 +++--- .../WordProcessing/WordFiltering/IWordFilter.cs | 2 +- .../WordGrouping/DefaultWordProcessor.cs | 8 ++++---- .../WordGrouping/IProcessedWordProvider.cs | 2 +- .../WordInput/DocxFileWordParser.cs | 2 +- .../WordProcessing/WordInput/IWordProvider.cs | 2 +- .../WordProcessing/WordInput/TxtFileWordParser.cs | 2 +- .../libs/TagsCloudVisualization.dll | Bin .../Drawing/DefaultImageDrawerTests.cs | 4 ++-- .../GlobalUsings.cs | 0 .../TagsCloudCoreTests.csproj | 3 ++- .../Utils/DrawingUtilsTests.cs | 4 ++-- .../WordFiltering/DefaultWordFilterTests.cs | 6 +++--- .../WordGrouping/DefaultWordProcessorTests.cs | 8 ++++---- di.sln | 4 ++-- 40 files changed, 78 insertions(+), 89 deletions(-) delete mode 100644 TagsCloudContainer/BuildingOptions/DefaultDrawingOptionsProvider.cs delete mode 100644 TagsCloudContainer/BuildingOptions/ICommonOptionsProvider.cs rename {TagsCloudContainer => TagsCloudCore}/BuildingOptions/CommonOptions.cs (75%) rename {TagsCloudContainer => TagsCloudCore}/BuildingOptions/DrawingOptions.cs (92%) create mode 100644 TagsCloudCore/BuildingOptions/ICommonOptionsProvider.cs rename {TagsCloudContainer => TagsCloudCore}/BuildingOptions/IDrawingOptionsProvider.cs (65%) rename {TagsCloudContainer => TagsCloudCore}/Common/DiContainerBuilder.cs (69%) rename {TagsCloudContainer => TagsCloudCore}/Common/WordData.cs (87%) rename {TagsCloudContainer => TagsCloudCore}/Drawing/Colorers/BicolorColorer.cs (90%) rename {TagsCloudContainer => TagsCloudCore}/Drawing/Colorers/DefaultWordColorer.cs (79%) rename {TagsCloudContainer => TagsCloudCore}/Drawing/Colorers/IWordColorer.cs (72%) rename {TagsCloudContainer => TagsCloudCore}/Drawing/Colorers/RandomWordColorer.cs (85%) rename {TagsCloudContainer => TagsCloudCore}/Drawing/DefaultImageDrawer.cs (92%) rename {TagsCloudContainer => TagsCloudCore}/Drawing/IImageDrawer.cs (70%) rename {TagsCloudContainer => TagsCloudCore}/Resources/filter.txt (100%) rename {TagsCloudContainer => TagsCloudCore}/TagCloudForming/DefaultWordCloudDistributor.cs (85%) rename {TagsCloudContainer => TagsCloudCore}/TagCloudForming/IWordCloudDistributorProvider.cs (60%) rename TagsCloudContainer/TagsCloudContainer.csproj => TagsCloudCore/TagsCloudCore.csproj (93%) rename {TagsCloudContainer => TagsCloudCore}/Utils/DrawingUtils.cs (97%) rename {TagsCloudContainer => TagsCloudCore}/Utils/WordProcessingUtils.cs (82%) rename {TagsCloudContainer => TagsCloudCore}/WordProcessing/WordFiltering/DefaultWordFilter.cs (75%) rename {TagsCloudContainer => TagsCloudCore}/WordProcessing/WordFiltering/IWordFilter.cs (56%) rename {TagsCloudContainer => TagsCloudCore}/WordProcessing/WordGrouping/DefaultWordProcessor.cs (84%) rename {TagsCloudContainer => TagsCloudCore}/WordProcessing/WordGrouping/IProcessedWordProvider.cs (62%) rename {TagsCloudContainer => TagsCloudCore}/WordProcessing/WordInput/DocxFileWordParser.cs (93%) rename {TagsCloudContainer => TagsCloudCore}/WordProcessing/WordInput/IWordProvider.cs (54%) rename {TagsCloudContainer => TagsCloudCore}/WordProcessing/WordInput/TxtFileWordParser.cs (91%) rename {TagsCloudContainer => TagsCloudCore}/libs/TagsCloudVisualization.dll (100%) rename {TagsCloudContainerTests => TagsCloudCoreTests}/Drawing/DefaultImageDrawerTests.cs (87%) rename {TagsCloudContainerTests => TagsCloudCoreTests}/GlobalUsings.cs (100%) rename TagsCloudContainerTests/TagsCloudContainerTests.csproj => TagsCloudCoreTests/TagsCloudCoreTests.csproj (86%) rename {TagsCloudContainerTests => TagsCloudCoreTests}/Utils/DrawingUtilsTests.cs (96%) rename {TagsCloudContainerTests => TagsCloudCoreTests}/WordProcessing/WordFiltering/DefaultWordFilterTests.cs (76%) rename {TagsCloudContainerTests => TagsCloudCoreTests}/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs (79%) diff --git a/TagsCloudConsoleUI/Program.cs b/TagsCloudConsoleUI/Program.cs index cb99a0a90..d176141b9 100644 --- a/TagsCloudConsoleUI/Program.cs +++ b/TagsCloudConsoleUI/Program.cs @@ -1,9 +1,9 @@ using System.Drawing.Imaging; using Autofac; using TagsCloudConsoleUI.Providers; -using TagsCloudContainer.BuildingOptions; -using TagsCloudContainer.Common; -using TagsCloudContainer.Drawing; +using TagsCloudCore.BuildingOptions; +using TagsCloudCore.Common; +using TagsCloudCore.Drawing; namespace TagsCloudConsoleUI; diff --git a/TagsCloudConsoleUI/Providers/ColorerProviders.cs b/TagsCloudConsoleUI/Providers/ColorerProviders.cs index 2cef9eb14..d2fafb235 100644 --- a/TagsCloudConsoleUI/Providers/ColorerProviders.cs +++ b/TagsCloudConsoleUI/Providers/ColorerProviders.cs @@ -1,5 +1,5 @@ using Autofac; -using TagsCloudContainer.Drawing.Colorers; +using TagsCloudCore.Drawing.Colorers; namespace TagsCloudConsoleUI.Providers; diff --git a/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs b/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs index 977811540..7eca405ff 100644 --- a/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs +++ b/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs @@ -1,10 +1,10 @@ using System.Drawing; using System.Text; using McMaster.Extensions.CommandLineUtils; -using TagsCloudContainer.BuildingOptions; -using TagsCloudContainer.Drawing.Colorers; -using TagsCloudContainer.Utils; -using TagsCloudContainer.WordProcessing.WordInput; +using TagsCloudCore.BuildingOptions; +using TagsCloudCore.Drawing.Colorers; +using TagsCloudCore.Utils; +using TagsCloudCore.WordProcessing.WordInput; namespace TagsCloudConsoleUI.Providers; diff --git a/TagsCloudConsoleUI/Providers/WordProviders.cs b/TagsCloudConsoleUI/Providers/WordProviders.cs index 41fc11787..37195268c 100644 --- a/TagsCloudConsoleUI/Providers/WordProviders.cs +++ b/TagsCloudConsoleUI/Providers/WordProviders.cs @@ -1,4 +1,4 @@ -using TagsCloudContainer.WordProcessing.WordInput; +using TagsCloudCore.WordProcessing.WordInput; namespace TagsCloudConsoleUI.Providers; diff --git a/TagsCloudConsoleUI/TagsCloudConsoleUI.csproj b/TagsCloudConsoleUI/TagsCloudConsoleUI.csproj index 9ed349255..4aa0bde9d 100644 --- a/TagsCloudConsoleUI/TagsCloudConsoleUI.csproj +++ b/TagsCloudConsoleUI/TagsCloudConsoleUI.csproj @@ -8,12 +8,12 @@ - + - ..\TagsCloudContainer\libs\TagsCloudVisualization.dll + ..\TagsCloudCore\libs\TagsCloudVisualization.dll diff --git a/TagsCloudContainer/BuildingOptions/DefaultDrawingOptionsProvider.cs b/TagsCloudContainer/BuildingOptions/DefaultDrawingOptionsProvider.cs deleted file mode 100644 index 251617074..000000000 --- a/TagsCloudContainer/BuildingOptions/DefaultDrawingOptionsProvider.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace TagsCloudContainer.BuildingOptions; - -public class DefaultDrawingOptionsProvider : IDrawingOptionsProvider -{ - public DrawingOptions DrawingOptions { get; } - - public DefaultDrawingOptionsProvider(DrawingOptions drawingOptions) - { - DrawingOptions = drawingOptions; - } -} \ No newline at end of file diff --git a/TagsCloudContainer/BuildingOptions/ICommonOptionsProvider.cs b/TagsCloudContainer/BuildingOptions/ICommonOptionsProvider.cs deleted file mode 100644 index e0ffd5a27..000000000 --- a/TagsCloudContainer/BuildingOptions/ICommonOptionsProvider.cs +++ /dev/null @@ -1,8 +0,0 @@ -using TagsCloudContainer.WordProcessing.WordInput; - -namespace TagsCloudContainer.BuildingOptions; - -public interface ICommonOptionsProvider -{ - CommonOptions CommonOptions { get; } -} \ No newline at end of file diff --git a/TagsCloudContainer/BuildingOptions/CommonOptions.cs b/TagsCloudCore/BuildingOptions/CommonOptions.cs similarity index 75% rename from TagsCloudContainer/BuildingOptions/CommonOptions.cs rename to TagsCloudCore/BuildingOptions/CommonOptions.cs index 1e5817516..290212da4 100644 --- a/TagsCloudContainer/BuildingOptions/CommonOptions.cs +++ b/TagsCloudCore/BuildingOptions/CommonOptions.cs @@ -1,8 +1,8 @@ -using TagsCloudContainer.Drawing.Colorers; -using TagsCloudContainer.WordProcessing.WordInput; +using TagsCloudCore.Drawing.Colorers; +using TagsCloudCore.WordProcessing.WordInput; using TagsCloudVisualization; -namespace TagsCloudContainer.BuildingOptions; +namespace TagsCloudCore.BuildingOptions; public class CommonOptions { diff --git a/TagsCloudContainer/BuildingOptions/DrawingOptions.cs b/TagsCloudCore/BuildingOptions/DrawingOptions.cs similarity index 92% rename from TagsCloudContainer/BuildingOptions/DrawingOptions.cs rename to TagsCloudCore/BuildingOptions/DrawingOptions.cs index 618723ac3..1ee99debd 100644 --- a/TagsCloudContainer/BuildingOptions/DrawingOptions.cs +++ b/TagsCloudCore/BuildingOptions/DrawingOptions.cs @@ -1,6 +1,6 @@ using System.Drawing; -namespace TagsCloudContainer.BuildingOptions; +namespace TagsCloudCore.BuildingOptions; public class DrawingOptions { diff --git a/TagsCloudCore/BuildingOptions/ICommonOptionsProvider.cs b/TagsCloudCore/BuildingOptions/ICommonOptionsProvider.cs new file mode 100644 index 000000000..51769a426 --- /dev/null +++ b/TagsCloudCore/BuildingOptions/ICommonOptionsProvider.cs @@ -0,0 +1,6 @@ +namespace TagsCloudCore.BuildingOptions; + +public interface ICommonOptionsProvider +{ + CommonOptions CommonOptions { get; } +} \ No newline at end of file diff --git a/TagsCloudContainer/BuildingOptions/IDrawingOptionsProvider.cs b/TagsCloudCore/BuildingOptions/IDrawingOptionsProvider.cs similarity index 65% rename from TagsCloudContainer/BuildingOptions/IDrawingOptionsProvider.cs rename to TagsCloudCore/BuildingOptions/IDrawingOptionsProvider.cs index 1c215690c..3fbf1074f 100644 --- a/TagsCloudContainer/BuildingOptions/IDrawingOptionsProvider.cs +++ b/TagsCloudCore/BuildingOptions/IDrawingOptionsProvider.cs @@ -1,4 +1,4 @@ -namespace TagsCloudContainer.BuildingOptions; +namespace TagsCloudCore.BuildingOptions; public interface IDrawingOptionsProvider { diff --git a/TagsCloudContainer/Common/DiContainerBuilder.cs b/TagsCloudCore/Common/DiContainerBuilder.cs similarity index 69% rename from TagsCloudContainer/Common/DiContainerBuilder.cs rename to TagsCloudCore/Common/DiContainerBuilder.cs index 73e51ab63..cf8c28b4b 100644 --- a/TagsCloudContainer/Common/DiContainerBuilder.cs +++ b/TagsCloudCore/Common/DiContainerBuilder.cs @@ -1,11 +1,11 @@ using Autofac; -using TagsCloudContainer.Drawing; -using TagsCloudContainer.TagCloudForming; -using TagsCloudContainer.WordProcessing.WordFiltering; -using TagsCloudContainer.WordProcessing.WordGrouping; -using TagsCloudContainer.WordProcessing.WordInput; +using TagsCloudCore.Drawing; +using TagsCloudCore.TagCloudForming; +using TagsCloudCore.WordProcessing.WordFiltering; +using TagsCloudCore.WordProcessing.WordGrouping; +using TagsCloudCore.WordProcessing.WordInput; -namespace TagsCloudContainer.Common; +namespace TagsCloudCore.Common; public static class DiContainerBuilder { @@ -14,7 +14,7 @@ public static ContainerBuilder RegisterDefaultDependencies() var containerBuilder = new ContainerBuilder(); containerBuilder.RegisterType().As().SingleInstance(); - containerBuilder.RegisterInstance(new DefaultWordFilter(new TxtFileWordParser("../../../../TagsCloudContainer/Resources/filter.txt"))) + containerBuilder.RegisterInstance(new DefaultWordFilter(new TxtFileWordParser("../../../../TagsCloudCore/Resources/filter.txt"))) .As() .SingleInstance(); containerBuilder.RegisterType().As().SingleInstance(); diff --git a/TagsCloudContainer/Common/WordData.cs b/TagsCloudCore/Common/WordData.cs similarity index 87% rename from TagsCloudContainer/Common/WordData.cs rename to TagsCloudCore/Common/WordData.cs index 0ee2bf892..2b17d9ff7 100644 --- a/TagsCloudContainer/Common/WordData.cs +++ b/TagsCloudCore/Common/WordData.cs @@ -1,6 +1,6 @@ using System.Drawing; -namespace TagsCloudContainer.Common; +namespace TagsCloudCore.Common; public class WordData { diff --git a/TagsCloudContainer/Drawing/Colorers/BicolorColorer.cs b/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs similarity index 90% rename from TagsCloudContainer/Drawing/Colorers/BicolorColorer.cs rename to TagsCloudCore/Drawing/Colorers/BicolorColorer.cs index a0924c3b9..80500cc9f 100644 --- a/TagsCloudContainer/Drawing/Colorers/BicolorColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs @@ -1,6 +1,6 @@ using System.Drawing; -namespace TagsCloudContainer.Drawing.Colorers; +namespace TagsCloudCore.Drawing.Colorers; public class BicolorColorer : IWordColorer { diff --git a/TagsCloudContainer/Drawing/Colorers/DefaultWordColorer.cs b/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs similarity index 79% rename from TagsCloudContainer/Drawing/Colorers/DefaultWordColorer.cs rename to TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs index da64ac159..a34e2303b 100644 --- a/TagsCloudContainer/Drawing/Colorers/DefaultWordColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs @@ -1,7 +1,7 @@ using System.Drawing; -using TagsCloudContainer.BuildingOptions; +using TagsCloudCore.BuildingOptions; -namespace TagsCloudContainer.Drawing.Colorers; +namespace TagsCloudCore.Drawing.Colorers; public class DefaultWordColorer : IWordColorer { diff --git a/TagsCloudContainer/Drawing/Colorers/IWordColorer.cs b/TagsCloudCore/Drawing/Colorers/IWordColorer.cs similarity index 72% rename from TagsCloudContainer/Drawing/Colorers/IWordColorer.cs rename to TagsCloudCore/Drawing/Colorers/IWordColorer.cs index 94192de8e..717482a35 100644 --- a/TagsCloudContainer/Drawing/Colorers/IWordColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/IWordColorer.cs @@ -1,6 +1,6 @@ using System.Drawing; -namespace TagsCloudContainer.Drawing.Colorers; +namespace TagsCloudCore.Drawing.Colorers; public interface IWordColorer { diff --git a/TagsCloudContainer/Drawing/Colorers/RandomWordColorer.cs b/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs similarity index 85% rename from TagsCloudContainer/Drawing/Colorers/RandomWordColorer.cs rename to TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs index 1aea320f1..ad935358b 100644 --- a/TagsCloudContainer/Drawing/Colorers/RandomWordColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs @@ -1,6 +1,6 @@ using System.Drawing; -namespace TagsCloudContainer.Drawing.Colorers; +namespace TagsCloudCore.Drawing.Colorers; public class RandomWordColorer : IWordColorer { diff --git a/TagsCloudContainer/Drawing/DefaultImageDrawer.cs b/TagsCloudCore/Drawing/DefaultImageDrawer.cs similarity index 92% rename from TagsCloudContainer/Drawing/DefaultImageDrawer.cs rename to TagsCloudCore/Drawing/DefaultImageDrawer.cs index 50ad9fb16..f1c935ac9 100644 --- a/TagsCloudContainer/Drawing/DefaultImageDrawer.cs +++ b/TagsCloudCore/Drawing/DefaultImageDrawer.cs @@ -1,11 +1,11 @@ using System.Drawing; using System.Drawing.Imaging; -using TagsCloudContainer.BuildingOptions; -using TagsCloudContainer.Common; -using TagsCloudContainer.Drawing.Colorers; -using TagsCloudContainer.TagCloudForming; +using TagsCloudCore.BuildingOptions; +using TagsCloudCore.Common; +using TagsCloudCore.Drawing.Colorers; +using TagsCloudCore.TagCloudForming; -namespace TagsCloudContainer.Drawing; +namespace TagsCloudCore.Drawing; public class DefaultImageDrawer : IImageDrawer { diff --git a/TagsCloudContainer/Drawing/IImageDrawer.cs b/TagsCloudCore/Drawing/IImageDrawer.cs similarity index 70% rename from TagsCloudContainer/Drawing/IImageDrawer.cs rename to TagsCloudCore/Drawing/IImageDrawer.cs index dc8c30342..9dbd0b733 100644 --- a/TagsCloudContainer/Drawing/IImageDrawer.cs +++ b/TagsCloudCore/Drawing/IImageDrawer.cs @@ -1,6 +1,6 @@ using System.Drawing; -namespace TagsCloudContainer.Drawing; +namespace TagsCloudCore.Drawing; public interface IImageDrawer { diff --git a/TagsCloudContainer/Resources/filter.txt b/TagsCloudCore/Resources/filter.txt similarity index 100% rename from TagsCloudContainer/Resources/filter.txt rename to TagsCloudCore/Resources/filter.txt diff --git a/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs b/TagsCloudCore/TagCloudForming/DefaultWordCloudDistributor.cs similarity index 85% rename from TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs rename to TagsCloudCore/TagCloudForming/DefaultWordCloudDistributor.cs index c179b2dbf..afddd7bc9 100644 --- a/TagsCloudContainer/TagCloudForming/DefaultWordCloudDistributor.cs +++ b/TagsCloudCore/TagCloudForming/DefaultWordCloudDistributor.cs @@ -1,10 +1,10 @@ -using TagsCloudContainer.BuildingOptions; -using TagsCloudContainer.Common; -using TagsCloudContainer.Utils; -using TagsCloudContainer.WordProcessing.WordGrouping; +using TagsCloudCore.BuildingOptions; +using TagsCloudCore.Common; +using TagsCloudCore.Utils; +using TagsCloudCore.WordProcessing.WordGrouping; using TagsCloudVisualization; -namespace TagsCloudContainer.TagCloudForming; +namespace TagsCloudCore.TagCloudForming; public class DefaultWordCloudDistributor : IWordCloudDistributorProvider { diff --git a/TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs b/TagsCloudCore/TagCloudForming/IWordCloudDistributorProvider.cs similarity index 60% rename from TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs rename to TagsCloudCore/TagCloudForming/IWordCloudDistributorProvider.cs index cc8a03ead..ec6c83d91 100644 --- a/TagsCloudContainer/TagCloudForming/IWordCloudDistributorProvider.cs +++ b/TagsCloudCore/TagCloudForming/IWordCloudDistributorProvider.cs @@ -1,6 +1,6 @@ -using TagsCloudContainer.Common; +using TagsCloudCore.Common; -namespace TagsCloudContainer.TagCloudForming; +namespace TagsCloudCore.TagCloudForming; public interface IWordCloudDistributorProvider { diff --git a/TagsCloudContainer/TagsCloudContainer.csproj b/TagsCloudCore/TagsCloudCore.csproj similarity index 93% rename from TagsCloudContainer/TagsCloudContainer.csproj rename to TagsCloudCore/TagsCloudCore.csproj index 376f35d55..dd9040b1f 100644 --- a/TagsCloudContainer/TagsCloudContainer.csproj +++ b/TagsCloudCore/TagsCloudCore.csproj @@ -4,6 +4,7 @@ net7.0 enable enable + TagsCloudCore diff --git a/TagsCloudContainer/Utils/DrawingUtils.cs b/TagsCloudCore/Utils/DrawingUtils.cs similarity index 97% rename from TagsCloudContainer/Utils/DrawingUtils.cs rename to TagsCloudCore/Utils/DrawingUtils.cs index 7fbbda3a4..e5debc121 100644 --- a/TagsCloudContainer/Utils/DrawingUtils.cs +++ b/TagsCloudCore/Utils/DrawingUtils.cs @@ -1,6 +1,6 @@ using System.Drawing; -namespace TagsCloudContainer.Utils; +namespace TagsCloudCore.Utils; public static class DrawingUtils { diff --git a/TagsCloudContainer/Utils/WordProcessingUtils.cs b/TagsCloudCore/Utils/WordProcessingUtils.cs similarity index 82% rename from TagsCloudContainer/Utils/WordProcessingUtils.cs rename to TagsCloudCore/Utils/WordProcessingUtils.cs index e27986db6..bbdae4d78 100644 --- a/TagsCloudContainer/Utils/WordProcessingUtils.cs +++ b/TagsCloudCore/Utils/WordProcessingUtils.cs @@ -1,4 +1,4 @@ -namespace TagsCloudContainer.Utils; +namespace TagsCloudCore.Utils; public static class WordProcessingUtils { diff --git a/TagsCloudContainer/WordProcessing/WordFiltering/DefaultWordFilter.cs b/TagsCloudCore/WordProcessing/WordFiltering/DefaultWordFilter.cs similarity index 75% rename from TagsCloudContainer/WordProcessing/WordFiltering/DefaultWordFilter.cs rename to TagsCloudCore/WordProcessing/WordFiltering/DefaultWordFilter.cs index 24c6e586e..1f79166dc 100644 --- a/TagsCloudContainer/WordProcessing/WordFiltering/DefaultWordFilter.cs +++ b/TagsCloudCore/WordProcessing/WordFiltering/DefaultWordFilter.cs @@ -1,7 +1,7 @@ -using TagsCloudContainer.Utils; -using TagsCloudContainer.WordProcessing.WordInput; +using TagsCloudCore.Utils; +using TagsCloudCore.WordProcessing.WordInput; -namespace TagsCloudContainer.WordProcessing.WordFiltering; +namespace TagsCloudCore.WordProcessing.WordFiltering; public class DefaultWordFilter : IWordFilter { diff --git a/TagsCloudContainer/WordProcessing/WordFiltering/IWordFilter.cs b/TagsCloudCore/WordProcessing/WordFiltering/IWordFilter.cs similarity index 56% rename from TagsCloudContainer/WordProcessing/WordFiltering/IWordFilter.cs rename to TagsCloudCore/WordProcessing/WordFiltering/IWordFilter.cs index fbad4bf0d..7fe4c2673 100644 --- a/TagsCloudContainer/WordProcessing/WordFiltering/IWordFilter.cs +++ b/TagsCloudCore/WordProcessing/WordFiltering/IWordFilter.cs @@ -1,4 +1,4 @@ -namespace TagsCloudContainer.WordProcessing.WordFiltering; +namespace TagsCloudCore.WordProcessing.WordFiltering; public interface IWordFilter { diff --git a/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs b/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs similarity index 84% rename from TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs rename to TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs index d81a373a4..7570280a2 100644 --- a/TagsCloudContainer/WordProcessing/WordGrouping/DefaultWordProcessor.cs +++ b/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs @@ -1,8 +1,8 @@ -using TagsCloudContainer.BuildingOptions; -using TagsCloudContainer.WordProcessing.WordFiltering; -using TagsCloudContainer.WordProcessing.WordInput; +using TagsCloudCore.BuildingOptions; +using TagsCloudCore.WordProcessing.WordFiltering; +using TagsCloudCore.WordProcessing.WordInput; -namespace TagsCloudContainer.WordProcessing.WordGrouping; +namespace TagsCloudCore.WordProcessing.WordGrouping; public class DefaultWordProcessor : IProcessedWordProvider { diff --git a/TagsCloudContainer/WordProcessing/WordGrouping/IProcessedWordProvider.cs b/TagsCloudCore/WordProcessing/WordGrouping/IProcessedWordProvider.cs similarity index 62% rename from TagsCloudContainer/WordProcessing/WordGrouping/IProcessedWordProvider.cs rename to TagsCloudCore/WordProcessing/WordGrouping/IProcessedWordProvider.cs index 7baadd7da..f07ac044d 100644 --- a/TagsCloudContainer/WordProcessing/WordGrouping/IProcessedWordProvider.cs +++ b/TagsCloudCore/WordProcessing/WordGrouping/IProcessedWordProvider.cs @@ -1,4 +1,4 @@ -namespace TagsCloudContainer.WordProcessing.WordGrouping; +namespace TagsCloudCore.WordProcessing.WordGrouping; public interface IProcessedWordProvider { diff --git a/TagsCloudContainer/WordProcessing/WordInput/DocxFileWordParser.cs b/TagsCloudCore/WordProcessing/WordInput/DocxFileWordParser.cs similarity index 93% rename from TagsCloudContainer/WordProcessing/WordInput/DocxFileWordParser.cs rename to TagsCloudCore/WordProcessing/WordInput/DocxFileWordParser.cs index 00947fa58..b444f7105 100644 --- a/TagsCloudContainer/WordProcessing/WordInput/DocxFileWordParser.cs +++ b/TagsCloudCore/WordProcessing/WordInput/DocxFileWordParser.cs @@ -1,7 +1,7 @@ using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; -namespace TagsCloudContainer.WordProcessing.WordInput; +namespace TagsCloudCore.WordProcessing.WordInput; public class DocxFileWordParser : IWordProvider { diff --git a/TagsCloudContainer/WordProcessing/WordInput/IWordProvider.cs b/TagsCloudCore/WordProcessing/WordInput/IWordProvider.cs similarity index 54% rename from TagsCloudContainer/WordProcessing/WordInput/IWordProvider.cs rename to TagsCloudCore/WordProcessing/WordInput/IWordProvider.cs index cf0d8960d..0a5ae5007 100644 --- a/TagsCloudContainer/WordProcessing/WordInput/IWordProvider.cs +++ b/TagsCloudCore/WordProcessing/WordInput/IWordProvider.cs @@ -1,4 +1,4 @@ -namespace TagsCloudContainer.WordProcessing.WordInput; +namespace TagsCloudCore.WordProcessing.WordInput; public interface IWordProvider { diff --git a/TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs b/TagsCloudCore/WordProcessing/WordInput/TxtFileWordParser.cs similarity index 91% rename from TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs rename to TagsCloudCore/WordProcessing/WordInput/TxtFileWordParser.cs index 670a40291..e7c7aaf1c 100644 --- a/TagsCloudContainer/WordProcessing/WordInput/TxtFileWordParser.cs +++ b/TagsCloudCore/WordProcessing/WordInput/TxtFileWordParser.cs @@ -1,4 +1,4 @@ -namespace TagsCloudContainer.WordProcessing.WordInput; +namespace TagsCloudCore.WordProcessing.WordInput; public class TxtFileWordParser : IWordProvider diff --git a/TagsCloudContainer/libs/TagsCloudVisualization.dll b/TagsCloudCore/libs/TagsCloudVisualization.dll similarity index 100% rename from TagsCloudContainer/libs/TagsCloudVisualization.dll rename to TagsCloudCore/libs/TagsCloudVisualization.dll diff --git a/TagsCloudContainerTests/Drawing/DefaultImageDrawerTests.cs b/TagsCloudCoreTests/Drawing/DefaultImageDrawerTests.cs similarity index 87% rename from TagsCloudContainerTests/Drawing/DefaultImageDrawerTests.cs rename to TagsCloudCoreTests/Drawing/DefaultImageDrawerTests.cs index 20c6f601a..e6b51f5f3 100644 --- a/TagsCloudContainerTests/Drawing/DefaultImageDrawerTests.cs +++ b/TagsCloudCoreTests/Drawing/DefaultImageDrawerTests.cs @@ -1,8 +1,8 @@ using System.Drawing; using System.Drawing.Imaging; -using TagsCloudContainer.Drawing; +using TagsCloudCore.Drawing; -namespace TagsCloudContainerTests.Drawing; +namespace TagsCloudCoreTests.Drawing; public class DefaultImageDrawerTests { diff --git a/TagsCloudContainerTests/GlobalUsings.cs b/TagsCloudCoreTests/GlobalUsings.cs similarity index 100% rename from TagsCloudContainerTests/GlobalUsings.cs rename to TagsCloudCoreTests/GlobalUsings.cs diff --git a/TagsCloudContainerTests/TagsCloudContainerTests.csproj b/TagsCloudCoreTests/TagsCloudCoreTests.csproj similarity index 86% rename from TagsCloudContainerTests/TagsCloudContainerTests.csproj rename to TagsCloudCoreTests/TagsCloudCoreTests.csproj index eb6e17ad1..dd1809df2 100644 --- a/TagsCloudContainerTests/TagsCloudContainerTests.csproj +++ b/TagsCloudCoreTests/TagsCloudCoreTests.csproj @@ -7,6 +7,7 @@ false true + TagsCloudCoreTests @@ -20,7 +21,7 @@ - + diff --git a/TagsCloudContainerTests/Utils/DrawingUtilsTests.cs b/TagsCloudCoreTests/Utils/DrawingUtilsTests.cs similarity index 96% rename from TagsCloudContainerTests/Utils/DrawingUtilsTests.cs rename to TagsCloudCoreTests/Utils/DrawingUtilsTests.cs index 8fd6dc645..c44c70757 100644 --- a/TagsCloudContainerTests/Utils/DrawingUtilsTests.cs +++ b/TagsCloudCoreTests/Utils/DrawingUtilsTests.cs @@ -1,8 +1,8 @@ using System.Drawing; using FluentAssertions; -using TagsCloudContainer.Utils; +using TagsCloudCore.Utils; -namespace TagsCloudContainerTests.Utils; +namespace TagsCloudCoreTests.Utils; public class DrawingUtilsTests { diff --git a/TagsCloudContainerTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs b/TagsCloudCoreTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs similarity index 76% rename from TagsCloudContainerTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs rename to TagsCloudCoreTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs index fbe77bea0..6666cd37e 100644 --- a/TagsCloudContainerTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs +++ b/TagsCloudCoreTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs @@ -1,8 +1,8 @@ using NSubstitute; -using TagsCloudContainer.WordProcessing.WordFiltering; -using TagsCloudContainer.WordProcessing.WordInput; +using TagsCloudCore.WordProcessing.WordFiltering; +using TagsCloudCore.WordProcessing.WordInput; -namespace TagsCloudContainerTests.WordProcessing.WordFiltering; +namespace TagsCloudCoreTests.WordProcessing.WordFiltering; public class DefaultWordFilterTests { diff --git a/TagsCloudContainerTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs b/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs similarity index 79% rename from TagsCloudContainerTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs rename to TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs index fbd64d42c..1675af636 100644 --- a/TagsCloudContainerTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs +++ b/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs @@ -1,9 +1,9 @@ using NSubstitute; -using TagsCloudContainer.WordProcessing.WordFiltering; -using TagsCloudContainer.WordProcessing.WordGrouping; -using TagsCloudContainer.WordProcessing.WordInput; +using TagsCloudCore.WordProcessing.WordFiltering; +using TagsCloudCore.WordProcessing.WordGrouping; +using TagsCloudCore.WordProcessing.WordInput; -namespace TagsCloudContainerTests.WordProcessing.WordGrouping; +namespace TagsCloudCoreTests.WordProcessing.WordGrouping; public class DefaultWordProcessorTests { diff --git a/di.sln b/di.sln index ed75980fa..fbc275c2d 100644 --- a/di.sln +++ b/di.sln @@ -1,8 +1,8 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudContainer", "TagsCloudContainer\TagsCloudContainer.csproj", "{D6C7C0D1-2CA2-4F45-8AA5-340A58A45B95}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudCore", "TagsCloudCore\TagsCloudCore.csproj", "{D6C7C0D1-2CA2-4F45-8AA5-340A58A45B95}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudContainerTests", "TagsCloudContainerTests\TagsCloudContainerTests.csproj", "{405FAAB3-6E4C-487C-9429-43529876FB44}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudCoreTests", "TagsCloudCoreTests\TagsCloudCoreTests.csproj", "{405FAAB3-6E4C-487C-9429-43529876FB44}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TagsCloudConsoleUI", "TagsCloudConsoleUI\TagsCloudConsoleUI.csproj", "{EB9EA819-7DE4-4B51-83EE-5B7B1380DFBD}" EndProject From f520f2585c14122d2afaa305b048b3371a7ca362 Mon Sep 17 00:00:00 2001 From: Alexander Gorbatov Date: Sun, 28 Jan 2024 13:19:45 +0500 Subject: [PATCH 15/19] Moved the path to words filter to app.config --- TagsCloudConsoleUI/Program.cs | 3 ++- TagsCloudConsoleUI/Providers/ColorerProviders.cs | 3 +-- TagsCloudConsoleUI/app.config | 6 ++++++ TagsCloudCore/Common/AppSettings.cs | 8 ++++++++ TagsCloudCore/Common/DiContainerBuilder.cs | 2 +- 5 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 TagsCloudConsoleUI/app.config create mode 100644 TagsCloudCore/Common/AppSettings.cs diff --git a/TagsCloudConsoleUI/Program.cs b/TagsCloudConsoleUI/Program.cs index d176141b9..bc51619a7 100644 --- a/TagsCloudConsoleUI/Program.cs +++ b/TagsCloudConsoleUI/Program.cs @@ -1,4 +1,5 @@ -using System.Drawing.Imaging; +using System.Configuration; +using System.Drawing.Imaging; using Autofac; using TagsCloudConsoleUI.Providers; using TagsCloudCore.BuildingOptions; diff --git a/TagsCloudConsoleUI/Providers/ColorerProviders.cs b/TagsCloudConsoleUI/Providers/ColorerProviders.cs index d2fafb235..2d7970bb7 100644 --- a/TagsCloudConsoleUI/Providers/ColorerProviders.cs +++ b/TagsCloudConsoleUI/Providers/ColorerProviders.cs @@ -1,5 +1,4 @@ -using Autofac; -using TagsCloudCore.Drawing.Colorers; +using TagsCloudCore.Drawing.Colorers; namespace TagsCloudConsoleUI.Providers; diff --git a/TagsCloudConsoleUI/app.config b/TagsCloudConsoleUI/app.config new file mode 100644 index 000000000..b86450164 --- /dev/null +++ b/TagsCloudConsoleUI/app.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/TagsCloudCore/Common/AppSettings.cs b/TagsCloudCore/Common/AppSettings.cs new file mode 100644 index 000000000..7c84e031d --- /dev/null +++ b/TagsCloudCore/Common/AppSettings.cs @@ -0,0 +1,8 @@ +using System.Configuration; + +namespace TagsCloudCore.Common; + +public static class AppSettings +{ + public static string PathToBoringWordsFilter => ConfigurationManager.AppSettings["BoringWordsFilterPath"]!; +} \ No newline at end of file diff --git a/TagsCloudCore/Common/DiContainerBuilder.cs b/TagsCloudCore/Common/DiContainerBuilder.cs index cf8c28b4b..93b34ba32 100644 --- a/TagsCloudCore/Common/DiContainerBuilder.cs +++ b/TagsCloudCore/Common/DiContainerBuilder.cs @@ -14,7 +14,7 @@ public static ContainerBuilder RegisterDefaultDependencies() var containerBuilder = new ContainerBuilder(); containerBuilder.RegisterType().As().SingleInstance(); - containerBuilder.RegisterInstance(new DefaultWordFilter(new TxtFileWordParser("../../../../TagsCloudCore/Resources/filter.txt"))) + containerBuilder.RegisterInstance(new DefaultWordFilter(new TxtFileWordParser(AppSettings.PathToBoringWordsFilter))) .As() .SingleInstance(); containerBuilder.RegisterType().As().SingleInstance(); From 30b60685438f3bd3ee4a8eb7b158590049b35484 Mon Sep 17 00:00:00 2001 From: Alexander Gorbatov Date: Mon, 29 Jan 2024 16:07:01 +0500 Subject: [PATCH 16/19] refactoring --- ...Providers.cs => CloudAlgorithmProvider.cs} | 2 +- .../Providers/ConsoleSettingsProvider.cs | 33 +++++++++++++++---- TagsCloudCore/Common/DiContainerBuilder.cs | 15 ++++++--- .../Drawing/Colorers/BicolorColorer.cs | 2 ++ .../Drawing/Colorers/DefaultWordColorer.cs | 10 +++--- .../Drawing/Colorers/IWordColorer.cs | 3 ++ .../Drawing/Colorers/RandomWordColorer.cs | 2 ++ .../WordGrouping/DefaultWordProcessor.cs | 10 ++---- TagsCloudCoreTests/TagsCloudCoreTests.csproj | 6 ++++ .../WordGrouping/DefaultWordProcessorTests.cs | 8 ++++- 10 files changed, 66 insertions(+), 25 deletions(-) rename TagsCloudConsoleUI/Providers/{CloudAlgorithmProviders.cs => CloudAlgorithmProvider.cs} (87%) diff --git a/TagsCloudConsoleUI/Providers/CloudAlgorithmProviders.cs b/TagsCloudConsoleUI/Providers/CloudAlgorithmProvider.cs similarity index 87% rename from TagsCloudConsoleUI/Providers/CloudAlgorithmProviders.cs rename to TagsCloudConsoleUI/Providers/CloudAlgorithmProvider.cs index 0fd83d7f1..029880672 100644 --- a/TagsCloudConsoleUI/Providers/CloudAlgorithmProviders.cs +++ b/TagsCloudConsoleUI/Providers/CloudAlgorithmProvider.cs @@ -3,7 +3,7 @@ namespace TagsCloudConsoleUI.Providers; -public static class CloudAlgorithmProviders +public static class CloudAlgorithmProvider { public static readonly IReadOnlyDictionary RegisteredProviders = new Dictionary diff --git a/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs b/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs index 7eca405ff..1971375f4 100644 --- a/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs +++ b/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs @@ -20,6 +20,12 @@ public class ConsoleSettingsProvider : IDrawingOptionsProvider, ICommonOptionsPr private bool _isCustomColoringUsed; + private readonly IEnumerable _registeredWordColorers; + public ConsoleSettingsProvider(IEnumerable registeredWordColorers) + { + _registeredWordColorers = registeredWordColorers; + } + private DrawingOptions GetDrawingOptions() { var font = GetFont(); @@ -44,9 +50,9 @@ private CommonOptions GetCommonOptions() { var wordProvider = GetWordProvider(); var wordColorer = GetWordColorer(); - var cloudLayouter = GetAlgorithm(CloudAlgorithmProviders.RegisteredProviders, + var cloudLayouter = GetAlgorithm(CloudAlgorithmProvider.RegisteredProviders, "Choose the cloud forming algorithm:"); - + return new CommonOptions(wordProvider, wordColorer, cloudLayouter); } @@ -94,13 +100,26 @@ private static Font GetFont() } } - private IWordColorer? GetWordColorer() + private IWordColorer GetWordColorer() { - if (!Prompt.GetYesNo("Would you like to use a custom coloring algorithm?", false, ConsoleColor.DarkGreen)) - return null; + var sb = new StringBuilder("Choose coloring algorithm:\n"); + foreach (var registeredWordColorer in _registeredWordColorers) + sb.AppendLine(registeredWordColorer.Name); + + while (true) + { + var input = Prompt.GetString(sb.ToString(), "", promptColor: ConsoleColor.DarkGreen); + var colorer = _registeredWordColorers.SingleOrDefault(c => c.Match(input!)); + if (colorer is null) + { + Console.WriteLine("The provided algorithm does not exist. Try again."); + continue; + } - _isCustomColoringUsed = true; - return GetAlgorithm(ColorerProviders.RegisteredProviders, "Choose the algorithm:"); + if (colorer.Name != "Default") + _isCustomColoringUsed = true; + return colorer; + } } private static T GetAlgorithm(IReadOnlyDictionary registeredAlgorithms, diff --git a/TagsCloudCore/Common/DiContainerBuilder.cs b/TagsCloudCore/Common/DiContainerBuilder.cs index 93b34ba32..2a9857ab4 100644 --- a/TagsCloudCore/Common/DiContainerBuilder.cs +++ b/TagsCloudCore/Common/DiContainerBuilder.cs @@ -1,5 +1,6 @@ using Autofac; using TagsCloudCore.Drawing; +using TagsCloudCore.Drawing.Colorers; using TagsCloudCore.TagCloudForming; using TagsCloudCore.WordProcessing.WordFiltering; using TagsCloudCore.WordProcessing.WordGrouping; @@ -12,15 +13,21 @@ public static class DiContainerBuilder public static ContainerBuilder RegisterDefaultDependencies() { var containerBuilder = new ContainerBuilder(); + + containerBuilder.RegisterAssemblyTypes(typeof(IWordColorer).Assembly).As().SingleInstance(); containerBuilder.RegisterType().As().SingleInstance(); - containerBuilder.RegisterInstance(new DefaultWordFilter(new TxtFileWordParser(AppSettings.PathToBoringWordsFilter))) + + containerBuilder + .RegisterInstance(new DefaultWordFilter(new TxtFileWordParser(AppSettings.PathToBoringWordsFilter))) .As() .SingleInstance(); + containerBuilder.RegisterType().As().SingleInstance(); - containerBuilder.RegisterType().As().SingleInstance(); - containerBuilder.RegisterType().As().SingleInstance(); + + containerBuilder.RegisterType().As() + .SingleInstance(); return containerBuilder; - } + } } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs b/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs index 80500cc9f..8bcaa4958 100644 --- a/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs @@ -10,4 +10,6 @@ public class BicolorColorer : IWordColorer public Color GetWordColor(string word, int wordFrequency) => Random.Next(0, 2) == 0 ? _first : _second; + + public string Name => "Bicolor"; } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs b/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs index a34e2303b..014ee5247 100644 --- a/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs @@ -5,13 +5,15 @@ namespace TagsCloudCore.Drawing.Colorers; public class DefaultWordColorer : IWordColorer { - private readonly Color _color; + private readonly Lazy _drawingOptionsProvider; - public DefaultWordColorer(IDrawingOptionsProvider drawingOptionsProvider) + public DefaultWordColorer(Lazy drawingOptionsProvider) { - _color = drawingOptionsProvider.DrawingOptions.FontColor; + _drawingOptionsProvider = drawingOptionsProvider; } public Color GetWordColor(string word, int wordFrequency) - => _color; + => _drawingOptionsProvider.Value.DrawingOptions.FontColor; + + public string Name => "Default"; } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/Colorers/IWordColorer.cs b/TagsCloudCore/Drawing/Colorers/IWordColorer.cs index 717482a35..e1ca21ece 100644 --- a/TagsCloudCore/Drawing/Colorers/IWordColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/IWordColorer.cs @@ -5,4 +5,7 @@ namespace TagsCloudCore.Drawing.Colorers; public interface IWordColorer { public Color GetWordColor(string word, int wordFrequency); + + public string Name { get; } + public bool Match(string colorerName) => colorerName == Name; } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs b/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs index ad935358b..97ee81b31 100644 --- a/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs @@ -8,4 +8,6 @@ public class RandomWordColorer : IWordColorer public Color GetWordColor(string word, int wordFrequency) => Color.FromArgb(Random.Next(0, 256), Random.Next(0, 256), Random.Next(0, 256)); + + public string Name => "Random"; } \ No newline at end of file diff --git a/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs b/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs index 7570280a2..4a7965e4d 100644 --- a/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs +++ b/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs @@ -1,6 +1,5 @@ using TagsCloudCore.BuildingOptions; using TagsCloudCore.WordProcessing.WordFiltering; -using TagsCloudCore.WordProcessing.WordInput; namespace TagsCloudCore.WordProcessing.WordGrouping; @@ -9,14 +8,9 @@ public class DefaultWordProcessor : IProcessedWordProvider private readonly string[] _words; private readonly IEnumerable _filters; - public DefaultWordProcessor(ICommonOptionsProvider commonOptionsProvider, IEnumerable filters) : this( - commonOptionsProvider.CommonOptions.WordProvider, filters) + public DefaultWordProcessor(ICommonOptionsProvider commonOptionsProvider, IEnumerable filters) { - } - - public DefaultWordProcessor(IWordProvider wordProvider, IEnumerable filters) - { - _words = wordProvider.Words.Select(w => w.ToLower()).ToArray(); + _words = commonOptionsProvider.CommonOptions.WordProvider.Words.Select(w => w.ToLower()).ToArray(); _filters = filters; } diff --git a/TagsCloudCoreTests/TagsCloudCoreTests.csproj b/TagsCloudCoreTests/TagsCloudCoreTests.csproj index dd1809df2..a54005d70 100644 --- a/TagsCloudCoreTests/TagsCloudCoreTests.csproj +++ b/TagsCloudCoreTests/TagsCloudCoreTests.csproj @@ -24,4 +24,10 @@ + + + ..\TagsCloudCore\libs\TagsCloudVisualization.dll + + + diff --git a/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs b/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs index 1675af636..480a18e35 100644 --- a/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs +++ b/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs @@ -1,7 +1,10 @@ using NSubstitute; +using TagsCloudCore.BuildingOptions; +using TagsCloudCore.Drawing.Colorers; using TagsCloudCore.WordProcessing.WordFiltering; using TagsCloudCore.WordProcessing.WordGrouping; using TagsCloudCore.WordProcessing.WordInput; +using TagsCloudVisualization; namespace TagsCloudCoreTests.WordProcessing.WordGrouping; @@ -10,11 +13,14 @@ public class DefaultWordProcessorTests [Test] public void ProcessedWords_ReturnsCorrectlyProcessedWords_OnCorrectInputData() { + var options = Substitute.For(); var words = Substitute.For(); + options.CommonOptions.Returns(new CommonOptions(words, Substitute.For(), + Substitute.For())); words.Words.Returns(new[] {"1", "2", "4", "4", "1", "1", "5"}); var filter1 = GetWordFilter(new[] {"2"}); var filter2 = GetWordFilter(new[] {"5"}); - var processor = new DefaultWordProcessor(words, new[] {filter1, filter2}); + var processor = new DefaultWordProcessor(options, new[] {filter1, filter2}); var result = processor.ProcessedWords; From 8e81e739e95904bd66550159ac6b0047f0ce1737 Mon Sep 17 00:00:00 2001 From: Alexander Gorbatov Date: Mon, 29 Jan 2024 16:07:01 +0500 Subject: [PATCH 17/19] refactoring --- TagsCloudConsoleUI/Program.cs | 3 +- ...Providers.cs => CloudAlgorithmProvider.cs} | 2 +- .../Providers/ColorerProviders.cs | 13 -------- .../Providers/ConsoleSettingsProvider.cs | 33 +++++++++++++++---- TagsCloudCore/Common/DiContainerBuilder.cs | 15 ++++++--- .../Drawing/Colorers/BicolorColorer.cs | 2 ++ .../Drawing/Colorers/DefaultWordColorer.cs | 10 +++--- .../Drawing/Colorers/IWordColorer.cs | 3 ++ .../Drawing/Colorers/RandomWordColorer.cs | 2 ++ .../WordGrouping/DefaultWordProcessor.cs | 10 ++---- TagsCloudCoreTests/TagsCloudCoreTests.csproj | 6 ++++ .../WordGrouping/DefaultWordProcessorTests.cs | 8 ++++- 12 files changed, 67 insertions(+), 40 deletions(-) rename TagsCloudConsoleUI/Providers/{CloudAlgorithmProviders.cs => CloudAlgorithmProvider.cs} (87%) delete mode 100644 TagsCloudConsoleUI/Providers/ColorerProviders.cs diff --git a/TagsCloudConsoleUI/Program.cs b/TagsCloudConsoleUI/Program.cs index bc51619a7..d176141b9 100644 --- a/TagsCloudConsoleUI/Program.cs +++ b/TagsCloudConsoleUI/Program.cs @@ -1,5 +1,4 @@ -using System.Configuration; -using System.Drawing.Imaging; +using System.Drawing.Imaging; using Autofac; using TagsCloudConsoleUI.Providers; using TagsCloudCore.BuildingOptions; diff --git a/TagsCloudConsoleUI/Providers/CloudAlgorithmProviders.cs b/TagsCloudConsoleUI/Providers/CloudAlgorithmProvider.cs similarity index 87% rename from TagsCloudConsoleUI/Providers/CloudAlgorithmProviders.cs rename to TagsCloudConsoleUI/Providers/CloudAlgorithmProvider.cs index 0fd83d7f1..029880672 100644 --- a/TagsCloudConsoleUI/Providers/CloudAlgorithmProviders.cs +++ b/TagsCloudConsoleUI/Providers/CloudAlgorithmProvider.cs @@ -3,7 +3,7 @@ namespace TagsCloudConsoleUI.Providers; -public static class CloudAlgorithmProviders +public static class CloudAlgorithmProvider { public static readonly IReadOnlyDictionary RegisteredProviders = new Dictionary diff --git a/TagsCloudConsoleUI/Providers/ColorerProviders.cs b/TagsCloudConsoleUI/Providers/ColorerProviders.cs deleted file mode 100644 index 2d7970bb7..000000000 --- a/TagsCloudConsoleUI/Providers/ColorerProviders.cs +++ /dev/null @@ -1,13 +0,0 @@ -using TagsCloudCore.Drawing.Colorers; - -namespace TagsCloudConsoleUI.Providers; - -public static class ColorerProviders -{ - public static readonly IReadOnlyDictionary RegisteredProviders = - new Dictionary - { - {"Random", new RandomWordColorer()}, - {"Bicolor", new BicolorColorer()} - }; -} \ No newline at end of file diff --git a/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs b/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs index 7eca405ff..1971375f4 100644 --- a/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs +++ b/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs @@ -20,6 +20,12 @@ public class ConsoleSettingsProvider : IDrawingOptionsProvider, ICommonOptionsPr private bool _isCustomColoringUsed; + private readonly IEnumerable _registeredWordColorers; + public ConsoleSettingsProvider(IEnumerable registeredWordColorers) + { + _registeredWordColorers = registeredWordColorers; + } + private DrawingOptions GetDrawingOptions() { var font = GetFont(); @@ -44,9 +50,9 @@ private CommonOptions GetCommonOptions() { var wordProvider = GetWordProvider(); var wordColorer = GetWordColorer(); - var cloudLayouter = GetAlgorithm(CloudAlgorithmProviders.RegisteredProviders, + var cloudLayouter = GetAlgorithm(CloudAlgorithmProvider.RegisteredProviders, "Choose the cloud forming algorithm:"); - + return new CommonOptions(wordProvider, wordColorer, cloudLayouter); } @@ -94,13 +100,26 @@ private static Font GetFont() } } - private IWordColorer? GetWordColorer() + private IWordColorer GetWordColorer() { - if (!Prompt.GetYesNo("Would you like to use a custom coloring algorithm?", false, ConsoleColor.DarkGreen)) - return null; + var sb = new StringBuilder("Choose coloring algorithm:\n"); + foreach (var registeredWordColorer in _registeredWordColorers) + sb.AppendLine(registeredWordColorer.Name); + + while (true) + { + var input = Prompt.GetString(sb.ToString(), "", promptColor: ConsoleColor.DarkGreen); + var colorer = _registeredWordColorers.SingleOrDefault(c => c.Match(input!)); + if (colorer is null) + { + Console.WriteLine("The provided algorithm does not exist. Try again."); + continue; + } - _isCustomColoringUsed = true; - return GetAlgorithm(ColorerProviders.RegisteredProviders, "Choose the algorithm:"); + if (colorer.Name != "Default") + _isCustomColoringUsed = true; + return colorer; + } } private static T GetAlgorithm(IReadOnlyDictionary registeredAlgorithms, diff --git a/TagsCloudCore/Common/DiContainerBuilder.cs b/TagsCloudCore/Common/DiContainerBuilder.cs index 93b34ba32..2a9857ab4 100644 --- a/TagsCloudCore/Common/DiContainerBuilder.cs +++ b/TagsCloudCore/Common/DiContainerBuilder.cs @@ -1,5 +1,6 @@ using Autofac; using TagsCloudCore.Drawing; +using TagsCloudCore.Drawing.Colorers; using TagsCloudCore.TagCloudForming; using TagsCloudCore.WordProcessing.WordFiltering; using TagsCloudCore.WordProcessing.WordGrouping; @@ -12,15 +13,21 @@ public static class DiContainerBuilder public static ContainerBuilder RegisterDefaultDependencies() { var containerBuilder = new ContainerBuilder(); + + containerBuilder.RegisterAssemblyTypes(typeof(IWordColorer).Assembly).As().SingleInstance(); containerBuilder.RegisterType().As().SingleInstance(); - containerBuilder.RegisterInstance(new DefaultWordFilter(new TxtFileWordParser(AppSettings.PathToBoringWordsFilter))) + + containerBuilder + .RegisterInstance(new DefaultWordFilter(new TxtFileWordParser(AppSettings.PathToBoringWordsFilter))) .As() .SingleInstance(); + containerBuilder.RegisterType().As().SingleInstance(); - containerBuilder.RegisterType().As().SingleInstance(); - containerBuilder.RegisterType().As().SingleInstance(); + + containerBuilder.RegisterType().As() + .SingleInstance(); return containerBuilder; - } + } } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs b/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs index 80500cc9f..8bcaa4958 100644 --- a/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs @@ -10,4 +10,6 @@ public class BicolorColorer : IWordColorer public Color GetWordColor(string word, int wordFrequency) => Random.Next(0, 2) == 0 ? _first : _second; + + public string Name => "Bicolor"; } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs b/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs index a34e2303b..014ee5247 100644 --- a/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs @@ -5,13 +5,15 @@ namespace TagsCloudCore.Drawing.Colorers; public class DefaultWordColorer : IWordColorer { - private readonly Color _color; + private readonly Lazy _drawingOptionsProvider; - public DefaultWordColorer(IDrawingOptionsProvider drawingOptionsProvider) + public DefaultWordColorer(Lazy drawingOptionsProvider) { - _color = drawingOptionsProvider.DrawingOptions.FontColor; + _drawingOptionsProvider = drawingOptionsProvider; } public Color GetWordColor(string word, int wordFrequency) - => _color; + => _drawingOptionsProvider.Value.DrawingOptions.FontColor; + + public string Name => "Default"; } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/Colorers/IWordColorer.cs b/TagsCloudCore/Drawing/Colorers/IWordColorer.cs index 717482a35..e1ca21ece 100644 --- a/TagsCloudCore/Drawing/Colorers/IWordColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/IWordColorer.cs @@ -5,4 +5,7 @@ namespace TagsCloudCore.Drawing.Colorers; public interface IWordColorer { public Color GetWordColor(string word, int wordFrequency); + + public string Name { get; } + public bool Match(string colorerName) => colorerName == Name; } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs b/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs index ad935358b..97ee81b31 100644 --- a/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs @@ -8,4 +8,6 @@ public class RandomWordColorer : IWordColorer public Color GetWordColor(string word, int wordFrequency) => Color.FromArgb(Random.Next(0, 256), Random.Next(0, 256), Random.Next(0, 256)); + + public string Name => "Random"; } \ No newline at end of file diff --git a/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs b/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs index 7570280a2..4a7965e4d 100644 --- a/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs +++ b/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs @@ -1,6 +1,5 @@ using TagsCloudCore.BuildingOptions; using TagsCloudCore.WordProcessing.WordFiltering; -using TagsCloudCore.WordProcessing.WordInput; namespace TagsCloudCore.WordProcessing.WordGrouping; @@ -9,14 +8,9 @@ public class DefaultWordProcessor : IProcessedWordProvider private readonly string[] _words; private readonly IEnumerable _filters; - public DefaultWordProcessor(ICommonOptionsProvider commonOptionsProvider, IEnumerable filters) : this( - commonOptionsProvider.CommonOptions.WordProvider, filters) + public DefaultWordProcessor(ICommonOptionsProvider commonOptionsProvider, IEnumerable filters) { - } - - public DefaultWordProcessor(IWordProvider wordProvider, IEnumerable filters) - { - _words = wordProvider.Words.Select(w => w.ToLower()).ToArray(); + _words = commonOptionsProvider.CommonOptions.WordProvider.Words.Select(w => w.ToLower()).ToArray(); _filters = filters; } diff --git a/TagsCloudCoreTests/TagsCloudCoreTests.csproj b/TagsCloudCoreTests/TagsCloudCoreTests.csproj index dd1809df2..a54005d70 100644 --- a/TagsCloudCoreTests/TagsCloudCoreTests.csproj +++ b/TagsCloudCoreTests/TagsCloudCoreTests.csproj @@ -24,4 +24,10 @@ + + + ..\TagsCloudCore\libs\TagsCloudVisualization.dll + + + diff --git a/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs b/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs index 1675af636..480a18e35 100644 --- a/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs +++ b/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs @@ -1,7 +1,10 @@ using NSubstitute; +using TagsCloudCore.BuildingOptions; +using TagsCloudCore.Drawing.Colorers; using TagsCloudCore.WordProcessing.WordFiltering; using TagsCloudCore.WordProcessing.WordGrouping; using TagsCloudCore.WordProcessing.WordInput; +using TagsCloudVisualization; namespace TagsCloudCoreTests.WordProcessing.WordGrouping; @@ -10,11 +13,14 @@ public class DefaultWordProcessorTests [Test] public void ProcessedWords_ReturnsCorrectlyProcessedWords_OnCorrectInputData() { + var options = Substitute.For(); var words = Substitute.For(); + options.CommonOptions.Returns(new CommonOptions(words, Substitute.For(), + Substitute.For())); words.Words.Returns(new[] {"1", "2", "4", "4", "1", "1", "5"}); var filter1 = GetWordFilter(new[] {"2"}); var filter2 = GetWordFilter(new[] {"5"}); - var processor = new DefaultWordProcessor(words, new[] {filter1, filter2}); + var processor = new DefaultWordProcessor(options, new[] {filter1, filter2}); var result = processor.ProcessedWords; From 9afbbc21727efbf4fbc14e7060458cc5cf03e642 Mon Sep 17 00:00:00 2001 From: Alexander Gorbatov Date: Mon, 29 Jan 2024 22:02:14 +0500 Subject: [PATCH 18/19] CommonOptions refactored --- .../ConsoleSettingsProvider.cs | 88 ++++++++----------- TagsCloudConsoleUI/Program.cs | 15 ++-- .../Providers/CloudAlgorithmProvider.cs | 13 --- TagsCloudConsoleUI/Providers/WordProviders.cs | 14 --- TagsCloudConsoleUI/TagsCloudConsoleUI.csproj | 10 +-- .../BuildingOptions/CommonOptions.cs | 24 ++--- .../BuildingOptions/DrawingOptions.cs | 12 +-- TagsCloudCore/Common/DiContainerBuilder.cs | 12 +-- .../Common/Enums/CloudBuildingAlgorithm.cs | 6 ++ .../Common/Enums/WordColorerAlgorithm.cs | 8 ++ .../Common/Enums/WordProviderType.cs | 8 ++ TagsCloudCore/Common/WordData.cs | 6 +- .../Drawing/Colorers/BicolorColorer.cs | 9 +- .../Drawing/Colorers/DefaultWordColorer.cs | 10 ++- .../Drawing/Colorers/IWordColorer.cs | 8 +- .../Drawing/Colorers/RandomWordColorer.cs | 9 +- TagsCloudCore/Drawing/DefaultImageDrawer.cs | 16 ++-- TagsCloudCore/Drawing/IImageDrawer.cs | 3 +- .../Providers/CloudAlgorithmProvider.cs | 14 +++ .../DefaultWordCloudDistributor.cs | 11 ++- TagsCloudCore/TagsCloudCore.csproj | 10 +-- TagsCloudCore/Utils/DrawingUtils.cs | 4 +- TagsCloudCore/Utils/WordProcessingUtils.cs | 4 +- .../WordFiltering/DefaultWordFilter.cs | 10 ++- .../WordGrouping/DefaultWordProcessor.cs | 14 ++- .../WordInput/DocxFileWordParser.cs | 23 +++-- .../WordProcessing/WordInput/IWordProvider.cs | 10 ++- .../WordInput/TxtFileWordParser.cs | 22 ++--- .../WordInput/WordProviderInfo.cs | 5 ++ .../Drawing/DefaultImageDrawerTests.cs | 3 +- TagsCloudCoreTests/TagsCloudCoreTests.csproj | 12 +-- TagsCloudCoreTests/Utils/DrawingUtilsTests.cs | 2 +- .../WordFiltering/DefaultWordFilterTests.cs | 8 +- .../WordGrouping/DefaultWordProcessorTests.cs | 23 ++--- 34 files changed, 244 insertions(+), 202 deletions(-) rename TagsCloudConsoleUI/{Providers => }/ConsoleSettingsProvider.cs (61%) delete mode 100644 TagsCloudConsoleUI/Providers/CloudAlgorithmProvider.cs delete mode 100644 TagsCloudConsoleUI/Providers/WordProviders.cs create mode 100644 TagsCloudCore/Common/Enums/CloudBuildingAlgorithm.cs create mode 100644 TagsCloudCore/Common/Enums/WordColorerAlgorithm.cs create mode 100644 TagsCloudCore/Common/Enums/WordProviderType.cs create mode 100644 TagsCloudCore/Providers/CloudAlgorithmProvider.cs create mode 100644 TagsCloudCore/WordProcessing/WordInput/WordProviderInfo.cs diff --git a/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs b/TagsCloudConsoleUI/ConsoleSettingsProvider.cs similarity index 61% rename from TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs rename to TagsCloudConsoleUI/ConsoleSettingsProvider.cs index ac6434069..eef010fe8 100644 --- a/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs +++ b/TagsCloudConsoleUI/ConsoleSettingsProvider.cs @@ -2,47 +2,39 @@ using System.Text; using McMaster.Extensions.CommandLineUtils; using TagsCloudCore.BuildingOptions; -using TagsCloudCore.Common; -using TagsCloudCore.Drawing.Colorers; +using TagsCloudCore.Common.Enums; using TagsCloudCore.Utils; using TagsCloudCore.WordProcessing.WordInput; -namespace TagsCloudConsoleUI.Providers; +namespace TagsCloudConsoleUI; public class ConsoleSettingsProvider : IDrawingOptionsProvider, ICommonOptionsProvider { - public DrawingOptions DrawingOptions => _drawingOptions ??= GetDrawingOptions(); - - public CommonOptions CommonOptions => _commonOptions ??= GetCommonOptions(); + private CommonOptions? _commonOptions; private DrawingOptions? _drawingOptions; - private CommonOptions? _commonOptions; - private bool _isCustomColoringUsed; - private readonly IEnumerable _registeredWordColorers; - public ConsoleSettingsProvider(IEnumerable registeredWordColorers) - { - _registeredWordColorers = registeredWordColorers; - } - + public CommonOptions CommonOptions => _commonOptions ??= GetCommonOptions(); + public DrawingOptions DrawingOptions => _drawingOptions ??= GetDrawingOptions(); + private DrawingOptions GetDrawingOptions() { var font = GetFont(); var backgroundColor = GetRgbColor("Enter background color in RGB format separated by space"); var fontColor = GetFontColor(); - + var imageSide = GetInteger( "Enter the image's desired size in px. The image will be a square. It must range from 500 px to 5000 px.", 500, 5000); - + var imageSize = new Size(imageSide, imageSide); - + var frequencyScaling = GetInteger( - "Enter the frequency scaling (a positive integer). It Determines the scale to word frequency ratio.", - 1, 100); + "Enter the frequency scaling (a positive integer). It Determines the scale to word frequency ratio.", + 1, 100); return new DrawingOptions(fontColor, backgroundColor, imageSize, font, frequencyScaling); } @@ -51,19 +43,18 @@ private CommonOptions GetCommonOptions() { var wordProvider = GetWordProvider(); var wordColorer = GetWordColorer(); - var cloudLayouter = GetAlgorithm(CloudAlgorithmProvider.RegisteredProviders, - "Choose the cloud forming algorithm:"); - + var cloudLayouter = GetCloudBuildingAlgorithm(); + return new CommonOptions(wordProvider, wordColorer, cloudLayouter); } private Color GetFontColor() { - return _isCustomColoringUsed - ? Color.White + return _isCustomColoringUsed + ? Color.White : GetRgbColor("Enter font color in RGB format separated by space"); } - + private static int GetInteger(string prompt, int lowerAllowedBoundary, int upperAllowedBoundary) { while (true) @@ -81,7 +72,7 @@ private static Color GetRgbColor(string prompt) while (true) { var colorString = Prompt.GetString(prompt, "", - promptColor: ConsoleColor.DarkGreen); + ConsoleColor.DarkGreen); if (DrawingUtils.TryParseRgb(colorString!, out var color)) return color; Console.WriteLine("Ivalid color format. Try again."); @@ -101,46 +92,43 @@ private static Font GetFont() } } - private IWordColorer GetWordColorer() + private WordColorerAlgorithm GetWordColorer() { var sb = new StringBuilder("Choose coloring algorithm:\n"); - foreach (var registeredWordColorer in _registeredWordColorers) - sb.AppendLine(registeredWordColorer.Name); + foreach (var registeredWordColorer in Enum.GetNames(typeof(WordColorerAlgorithm))) + sb.AppendLine(registeredWordColorer); while (true) { - var input = Prompt.GetString(sb.ToString(), "", promptColor: ConsoleColor.DarkGreen); - var colorer = _registeredWordColorers.SingleOrDefault(c => c.Match(input!)); - if (colorer is null) + var input = Prompt.GetString(sb.ToString(), "", ConsoleColor.DarkGreen); + if (Enum.TryParse(input, out WordColorerAlgorithm algorithm)) { - Console.WriteLine("The provided algorithm does not exist. Try again."); - continue; + if (algorithm != WordColorerAlgorithm.Default) + _isCustomColoringUsed = true; + return algorithm; } - if (colorer.Name != AppSettings.DefaultColorerName) - _isCustomColoringUsed = true; - return colorer; + Console.WriteLine("The provided word colorer does not exist. Try again."); } } - private static T GetAlgorithm(IReadOnlyDictionary registeredAlgorithms, - string prompt) + private static CloudBuildingAlgorithm GetCloudBuildingAlgorithm() { - var sb = new StringBuilder($"{prompt}\n"); - foreach (var algorithmProvider in registeredAlgorithms.Keys) - sb.AppendLine(algorithmProvider); + var sb = new StringBuilder("Choose cloud forming algorithm:\n"); + foreach (var algorithm in Enum.GetNames(typeof(CloudBuildingAlgorithm))) + sb.AppendLine(algorithm); while (true) { - var algorithm = Prompt.GetString(sb.ToString(), "", promptColor: ConsoleColor.DarkGreen); - if (registeredAlgorithms.TryGetValue(algorithm!, out var provider)) - return provider; + var input = Prompt.GetString(sb.ToString(), "", ConsoleColor.DarkGreen); + if (Enum.TryParse(input, out CloudBuildingAlgorithm algorithm)) + return algorithm; - Console.WriteLine("Specified algorithm isn't supported. Try again."); + Console.WriteLine("The provided algorithm does not exist. Try again."); } } - private static IWordProvider GetWordProvider() + private static WordProviderInfo GetWordProvider() { while (true) { @@ -151,9 +139,9 @@ private static IWordProvider GetWordProvider() continue; } - var ext = Path.GetExtension(path); - if (WordProviders.RegisteredProviders.TryGetValue(ext, out var provider)) - return provider(path); + var ext = Path.GetExtension(path)[1..]; + if (Enum.TryParse(ext, ignoreCase: true, out WordProviderType type)) + return new WordProviderInfo(type, path); Console.WriteLine("This extension is not supported. Try again."); } diff --git a/TagsCloudConsoleUI/Program.cs b/TagsCloudConsoleUI/Program.cs index d176141b9..2c3bf6ce8 100644 --- a/TagsCloudConsoleUI/Program.cs +++ b/TagsCloudConsoleUI/Program.cs @@ -1,8 +1,8 @@ using System.Drawing.Imaging; using Autofac; -using TagsCloudConsoleUI.Providers; using TagsCloudCore.BuildingOptions; using TagsCloudCore.Common; +using TagsCloudCore.Common.Enums; using TagsCloudCore.Drawing; namespace TagsCloudConsoleUI; @@ -14,24 +14,27 @@ public static void Main() try { var containterBuilder = DiContainerBuilder.RegisterDefaultDependencies(); - + containterBuilder.RegisterType() .As() .SingleInstance(); var container = containterBuilder.Build(); - BuildTagCloud(container.Resolve(), ".", "image.png", ImageFormat.Png); + BuildTagCloud(container.Resolve(), + container.Resolve().CommonOptions.WordColorer, ".", "image.png", + ImageFormat.Png); } catch (Exception e) { Console.WriteLine(e); } } - - private static void BuildTagCloud(IImageDrawer imageDrawer, string dirPath, string fileName, ImageFormat imageFormat) + + private static void BuildTagCloud(IImageDrawer imageDrawer, WordColorerAlgorithm colorerAlgorithm, string dirPath, + string fileName, ImageFormat imageFormat) { - var bitmap = imageDrawer.DrawImage(); + var bitmap = imageDrawer.DrawImage(colorerAlgorithm); DefaultImageDrawer.SaveImage(bitmap, dirPath, fileName, imageFormat); Console.WriteLine($"The image has been saved to \"{dirPath}\""); } diff --git a/TagsCloudConsoleUI/Providers/CloudAlgorithmProvider.cs b/TagsCloudConsoleUI/Providers/CloudAlgorithmProvider.cs deleted file mode 100644 index 029880672..000000000 --- a/TagsCloudConsoleUI/Providers/CloudAlgorithmProvider.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Drawing; -using TagsCloudVisualization; - -namespace TagsCloudConsoleUI.Providers; - -public static class CloudAlgorithmProvider -{ - public static readonly IReadOnlyDictionary RegisteredProviders = - new Dictionary - { - {"Circular", new CircularCloudLayouter(Point.Empty)} - }; -} \ No newline at end of file diff --git a/TagsCloudConsoleUI/Providers/WordProviders.cs b/TagsCloudConsoleUI/Providers/WordProviders.cs deleted file mode 100644 index 37195268c..000000000 --- a/TagsCloudConsoleUI/Providers/WordProviders.cs +++ /dev/null @@ -1,14 +0,0 @@ -using TagsCloudCore.WordProcessing.WordInput; - -namespace TagsCloudConsoleUI.Providers; - -public static class WordProviders -{ - public static readonly IReadOnlyDictionary> RegisteredProviders = - new Dictionary> - { - {".txt", path=> new TxtFileWordParser(path)}, - {".docx", path => new DocxFileWordParser(path)}, - {".doc", path => new DocxFileWordParser(path)} - }; -} \ No newline at end of file diff --git a/TagsCloudConsoleUI/TagsCloudConsoleUI.csproj b/TagsCloudConsoleUI/TagsCloudConsoleUI.csproj index 4aa0bde9d..9242f5bf1 100644 --- a/TagsCloudConsoleUI/TagsCloudConsoleUI.csproj +++ b/TagsCloudConsoleUI/TagsCloudConsoleUI.csproj @@ -8,17 +8,17 @@ - + - - ..\TagsCloudCore\libs\TagsCloudVisualization.dll - + + ..\TagsCloudCore\libs\TagsCloudVisualization.dll + - + diff --git a/TagsCloudCore/BuildingOptions/CommonOptions.cs b/TagsCloudCore/BuildingOptions/CommonOptions.cs index 290212da4..631f3cd85 100644 --- a/TagsCloudCore/BuildingOptions/CommonOptions.cs +++ b/TagsCloudCore/BuildingOptions/CommonOptions.cs @@ -1,21 +1,21 @@ -using TagsCloudCore.Drawing.Colorers; -using TagsCloudCore.WordProcessing.WordInput; -using TagsCloudVisualization; +using TagsCloudCore.Common.Enums; +using WordProviderInfo = TagsCloudCore.WordProcessing.WordInput.WordProviderInfo; namespace TagsCloudCore.BuildingOptions; public class CommonOptions { - public IWordProvider WordProvider { get; } - - public IWordColorer? WordColorer { get; } - - public ICloudLayouter CloudLayouter { get; } - - public CommonOptions(IWordProvider wordProvider, IWordColorer? wordColorer, ICloudLayouter cloudLayouter) + public CommonOptions(WordProviderInfo wordProviderInfo, WordColorerAlgorithm wordColorer, + CloudBuildingAlgorithm cloudBuildingAlgorithm) { - WordProvider = wordProvider; + WordProviderInfo = wordProviderInfo; WordColorer = wordColorer; - CloudLayouter = cloudLayouter; + CloudBuildingAlgorithm = cloudBuildingAlgorithm; } + + public WordProviderInfo WordProviderInfo { get; } + + public WordColorerAlgorithm WordColorer { get; } + + public CloudBuildingAlgorithm CloudBuildingAlgorithm { get; } } \ No newline at end of file diff --git a/TagsCloudCore/BuildingOptions/DrawingOptions.cs b/TagsCloudCore/BuildingOptions/DrawingOptions.cs index 1ee99debd..f103fab17 100644 --- a/TagsCloudCore/BuildingOptions/DrawingOptions.cs +++ b/TagsCloudCore/BuildingOptions/DrawingOptions.cs @@ -4,12 +4,6 @@ namespace TagsCloudCore.BuildingOptions; public class DrawingOptions { - public Color FontColor { get; } - public Color BackgroundColor { get; } - public Size ImageSize { get; } - public Font Font { get; } - public int FrequencyScaling { get; } - public DrawingOptions(Color fontColor, Color backgroundColor, Size imageSize, Font font, int frequencyScaling) { FontColor = fontColor; @@ -18,4 +12,10 @@ public DrawingOptions(Color fontColor, Color backgroundColor, Size imageSize, Fo Font = font; FrequencyScaling = frequencyScaling; } + + public Color FontColor { get; } + public Color BackgroundColor { get; } + public Size ImageSize { get; } + public Font Font { get; } + public int FrequencyScaling { get; } } \ No newline at end of file diff --git a/TagsCloudCore/Common/DiContainerBuilder.cs b/TagsCloudCore/Common/DiContainerBuilder.cs index 2a9857ab4..92d8ffcdd 100644 --- a/TagsCloudCore/Common/DiContainerBuilder.cs +++ b/TagsCloudCore/Common/DiContainerBuilder.cs @@ -15,16 +15,18 @@ public static ContainerBuilder RegisterDefaultDependencies() var containerBuilder = new ContainerBuilder(); containerBuilder.RegisterAssemblyTypes(typeof(IWordColorer).Assembly).As().SingleInstance(); - + + containerBuilder.RegisterAssemblyTypes(typeof(IWordProvider).Assembly).As().SingleInstance(); + containerBuilder.RegisterType().As().SingleInstance(); - + containerBuilder - .RegisterInstance(new DefaultWordFilter(new TxtFileWordParser(AppSettings.PathToBoringWordsFilter))) + .RegisterInstance(new DefaultWordFilter(new TxtFileWordParser(), AppSettings.PathToBoringWordsFilter)) .As() .SingleInstance(); - + containerBuilder.RegisterType().As().SingleInstance(); - + containerBuilder.RegisterType().As() .SingleInstance(); diff --git a/TagsCloudCore/Common/Enums/CloudBuildingAlgorithm.cs b/TagsCloudCore/Common/Enums/CloudBuildingAlgorithm.cs new file mode 100644 index 000000000..c45220ae0 --- /dev/null +++ b/TagsCloudCore/Common/Enums/CloudBuildingAlgorithm.cs @@ -0,0 +1,6 @@ +namespace TagsCloudCore.Common.Enums; + +public enum CloudBuildingAlgorithm +{ + Circular +} \ No newline at end of file diff --git a/TagsCloudCore/Common/Enums/WordColorerAlgorithm.cs b/TagsCloudCore/Common/Enums/WordColorerAlgorithm.cs new file mode 100644 index 000000000..61421d6a8 --- /dev/null +++ b/TagsCloudCore/Common/Enums/WordColorerAlgorithm.cs @@ -0,0 +1,8 @@ +namespace TagsCloudCore.Common.Enums; + +public enum WordColorerAlgorithm +{ + Default, + Random, + Bicolor +} \ No newline at end of file diff --git a/TagsCloudCore/Common/Enums/WordProviderType.cs b/TagsCloudCore/Common/Enums/WordProviderType.cs new file mode 100644 index 000000000..4b809f65d --- /dev/null +++ b/TagsCloudCore/Common/Enums/WordProviderType.cs @@ -0,0 +1,8 @@ +namespace TagsCloudCore.Common.Enums; + +public enum WordProviderType +{ + Txt, + Docx, + Doc +} \ No newline at end of file diff --git a/TagsCloudCore/Common/WordData.cs b/TagsCloudCore/Common/WordData.cs index 2b17d9ff7..bea4b4ce7 100644 --- a/TagsCloudCore/Common/WordData.cs +++ b/TagsCloudCore/Common/WordData.cs @@ -4,12 +4,12 @@ namespace TagsCloudCore.Common; public class WordData { - public Rectangle Rectangle { get; } - public int Frequency { get; } - public WordData(Rectangle rectangle, int frequency) { Rectangle = rectangle; Frequency = frequency; } + + public Rectangle Rectangle { get; } + public int Frequency { get; } } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs b/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs index 8bcaa4958..1021d2063 100644 --- a/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs @@ -1,15 +1,18 @@ using System.Drawing; +using TagsCloudCore.Common.Enums; namespace TagsCloudCore.Drawing.Colorers; public class BicolorColorer : IWordColorer { + private static readonly Random Random = new(); private readonly Color _first = Color.FromArgb(Random.Next(0, 256), Random.Next(0, 256), Random.Next(0, 256)); private readonly Color _second = Color.FromArgb(Random.Next(0, 256), Random.Next(0, 256), Random.Next(0, 256)); - private static readonly Random Random = new(); public Color GetWordColor(string word, int wordFrequency) - => Random.Next(0, 2) == 0 ? _first : _second; + { + return Random.Next(0, 2) == 0 ? _first : _second; + } - public string Name => "Bicolor"; + public WordColorerAlgorithm AlgorithmName => WordColorerAlgorithm.Bicolor; } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs b/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs index 733a0c163..3126d9d83 100644 --- a/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs @@ -1,20 +1,22 @@ using System.Drawing; using TagsCloudCore.BuildingOptions; -using TagsCloudCore.Common; +using TagsCloudCore.Common.Enums; namespace TagsCloudCore.Drawing.Colorers; public class DefaultWordColorer : IWordColorer { private readonly Lazy _drawingOptionsProvider; - + public DefaultWordColorer(Lazy drawingOptionsProvider) { _drawingOptionsProvider = drawingOptionsProvider; } public Color GetWordColor(string word, int wordFrequency) - => _drawingOptionsProvider.Value.DrawingOptions.FontColor; + { + return _drawingOptionsProvider.Value.DrawingOptions.FontColor; + } - public string Name => AppSettings.DefaultColorerName; + public WordColorerAlgorithm AlgorithmName => WordColorerAlgorithm.Default; } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/Colorers/IWordColorer.cs b/TagsCloudCore/Drawing/Colorers/IWordColorer.cs index e1ca21ece..393438e1d 100644 --- a/TagsCloudCore/Drawing/Colorers/IWordColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/IWordColorer.cs @@ -1,11 +1,15 @@ using System.Drawing; +using TagsCloudCore.Common.Enums; namespace TagsCloudCore.Drawing.Colorers; public interface IWordColorer { + public WordColorerAlgorithm AlgorithmName { get; } public Color GetWordColor(string word, int wordFrequency); - public string Name { get; } - public bool Match(string colorerName) => colorerName == Name; + public bool Match(WordColorerAlgorithm algorithmName) + { + return AlgorithmName == algorithmName; + } } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs b/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs index 97ee81b31..a8a87997e 100644 --- a/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs @@ -1,13 +1,16 @@ using System.Drawing; +using TagsCloudCore.Common.Enums; namespace TagsCloudCore.Drawing.Colorers; public class RandomWordColorer : IWordColorer { private static readonly Random Random = new(); - + public Color GetWordColor(string word, int wordFrequency) - => Color.FromArgb(Random.Next(0, 256), Random.Next(0, 256), Random.Next(0, 256)); + { + return Color.FromArgb(Random.Next(0, 256), Random.Next(0, 256), Random.Next(0, 256)); + } - public string Name => "Random"; + public WordColorerAlgorithm AlgorithmName => WordColorerAlgorithm.Random; } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/DefaultImageDrawer.cs b/TagsCloudCore/Drawing/DefaultImageDrawer.cs index f1c935ac9..178d697b0 100644 --- a/TagsCloudCore/Drawing/DefaultImageDrawer.cs +++ b/TagsCloudCore/Drawing/DefaultImageDrawer.cs @@ -2,6 +2,7 @@ using System.Drawing.Imaging; using TagsCloudCore.BuildingOptions; using TagsCloudCore.Common; +using TagsCloudCore.Common.Enums; using TagsCloudCore.Drawing.Colorers; using TagsCloudCore.TagCloudForming; @@ -11,17 +12,17 @@ public class DefaultImageDrawer : IImageDrawer { private readonly IReadOnlyDictionary _distributedWords; private readonly DrawingOptions _drawingOptions; - private readonly IWordColorer? _colorer; + private readonly IEnumerable _wordColorers; public DefaultImageDrawer(IWordCloudDistributorProvider cloudDistributorProvider, - IDrawingOptionsProvider drawingOptionsProvider, ICommonOptionsProvider commonOptionsProvider) + IDrawingOptionsProvider drawingOptionsProvider, IEnumerable wordColorers) { _distributedWords = cloudDistributorProvider.DistributedWords; _drawingOptions = drawingOptionsProvider.DrawingOptions; - _colorer = commonOptionsProvider.CommonOptions.WordColorer; + _wordColorers = wordColorers; } - public Bitmap DrawImage() + public Bitmap DrawImage(WordColorerAlgorithm colorerAlgorithm) { var bitmap = new Bitmap(_drawingOptions.ImageSize.Width, _drawingOptions.ImageSize.Height); var offset = new Point(_drawingOptions.ImageSize.Width / 2, _drawingOptions.ImageSize.Height / 2); @@ -33,7 +34,12 @@ public Bitmap DrawImage() var sizeAdd = _drawingOptions.FrequencyScaling * (word.Frequency - 1); var newFont = new Font(_drawingOptions.Font.FontFamily, _drawingOptions.Font.Size + sizeAdd, _drawingOptions.Font.Style); - var color = _colorer?.GetWordColor(value, word.Frequency) ?? _drawingOptions.FontColor; + + var colorer = _wordColorers.SingleOrDefault(c => c.Match(colorerAlgorithm)); + var color = colorer!.AlgorithmName == WordColorerAlgorithm.Default + ? _drawingOptions.FontColor + : colorer.GetWordColor(value, word.Frequency); + graphics.DrawString(value, newFont, new SolidBrush(color), word.Rectangle with {X = word.Rectangle.X + offset.X, Y = word.Rectangle.Y + offset.Y}); } diff --git a/TagsCloudCore/Drawing/IImageDrawer.cs b/TagsCloudCore/Drawing/IImageDrawer.cs index 9dbd0b733..7885d687b 100644 --- a/TagsCloudCore/Drawing/IImageDrawer.cs +++ b/TagsCloudCore/Drawing/IImageDrawer.cs @@ -1,8 +1,9 @@ using System.Drawing; +using TagsCloudCore.Common.Enums; namespace TagsCloudCore.Drawing; public interface IImageDrawer { - public Bitmap DrawImage(); + public Bitmap DrawImage(WordColorerAlgorithm colorerAlgorithm); } \ No newline at end of file diff --git a/TagsCloudCore/Providers/CloudAlgorithmProvider.cs b/TagsCloudCore/Providers/CloudAlgorithmProvider.cs new file mode 100644 index 000000000..dcac8d5cd --- /dev/null +++ b/TagsCloudCore/Providers/CloudAlgorithmProvider.cs @@ -0,0 +1,14 @@ +using System.Drawing; +using TagsCloudCore.Common.Enums; +using TagsCloudVisualization; + +namespace TagsCloudCore.Providers; + +public static class CloudAlgorithmProvider +{ + public static readonly IReadOnlyDictionary RegisteredProviders = + new Dictionary + { + {CloudBuildingAlgorithm.Circular, new CircularCloudLayouter(Point.Empty)} + }; +} \ No newline at end of file diff --git a/TagsCloudCore/TagCloudForming/DefaultWordCloudDistributor.cs b/TagsCloudCore/TagCloudForming/DefaultWordCloudDistributor.cs index afddd7bc9..904a94dac 100644 --- a/TagsCloudCore/TagCloudForming/DefaultWordCloudDistributor.cs +++ b/TagsCloudCore/TagCloudForming/DefaultWordCloudDistributor.cs @@ -1,5 +1,6 @@ using TagsCloudCore.BuildingOptions; using TagsCloudCore.Common; +using TagsCloudCore.Providers; using TagsCloudCore.Utils; using TagsCloudCore.WordProcessing.WordGrouping; using TagsCloudVisualization; @@ -8,15 +9,17 @@ namespace TagsCloudCore.TagCloudForming; public class DefaultWordCloudDistributor : IWordCloudDistributorProvider { - private readonly Dictionary _words; private readonly ICloudLayouter _cloudLayouter; private readonly DrawingOptions _drawingOptions; + private readonly Dictionary _words; - public DefaultWordCloudDistributor(IProcessedWordProvider processedWord, ICommonOptionsProvider commonOptionsProvider, + public DefaultWordCloudDistributor(IProcessedWordProvider processedWord, + ICommonOptionsProvider commonOptionsProvider, IDrawingOptionsProvider drawingOptionsProvider) { _words = processedWord.ProcessedWords; - _cloudLayouter = commonOptionsProvider.CommonOptions.CloudLayouter; + _cloudLayouter = + CloudAlgorithmProvider.RegisteredProviders[commonOptionsProvider.CommonOptions.CloudBuildingAlgorithm]; _drawingOptions = drawingOptionsProvider.DrawingOptions; } @@ -32,7 +35,7 @@ private Dictionary DistributeWords() _drawingOptions.FrequencyScaling, _drawingOptions.Font)), frequency); distributed.Add(word, newWord); } - + return distributed; } } \ No newline at end of file diff --git a/TagsCloudCore/TagsCloudCore.csproj b/TagsCloudCore/TagsCloudCore.csproj index dd9040b1f..a8b39d145 100644 --- a/TagsCloudCore/TagsCloudCore.csproj +++ b/TagsCloudCore/TagsCloudCore.csproj @@ -8,12 +8,12 @@ - - - - + + + + - + libs\TagsCloudVisualization.dll diff --git a/TagsCloudCore/Utils/DrawingUtils.cs b/TagsCloudCore/Utils/DrawingUtils.cs index e5debc121..a4d72f55b 100644 --- a/TagsCloudCore/Utils/DrawingUtils.cs +++ b/TagsCloudCore/Utils/DrawingUtils.cs @@ -5,14 +5,14 @@ namespace TagsCloudCore.Utils; public static class DrawingUtils { private static readonly Graphics Graphics = Graphics.FromImage(new Bitmap(1, 1)); - + public static Size GetStringSize(string word, int frequency, int frequencyScaling, Font font) { if (frequency < 1) throw new ArgumentException("Frequency must be a positive integer"); if (frequencyScaling < 1) throw new ArgumentException("Frequncy scaling must be a positive integer"); - + var sizeIncrement = frequencyScaling * (frequency - 1); var newFont = new Font(font.FontFamily, font.Size + sizeIncrement, font.Style); return Size.Ceiling(Graphics.MeasureString(word, newFont)); diff --git a/TagsCloudCore/Utils/WordProcessingUtils.cs b/TagsCloudCore/Utils/WordProcessingUtils.cs index bbdae4d78..1dad1cb62 100644 --- a/TagsCloudCore/Utils/WordProcessingUtils.cs +++ b/TagsCloudCore/Utils/WordProcessingUtils.cs @@ -3,7 +3,9 @@ public static class WordProcessingUtils { public static HashSet RemoveDuplicates(IEnumerable lines) - => lines + { + return lines .Distinct() .ToHashSet(); + } } \ No newline at end of file diff --git a/TagsCloudCore/WordProcessing/WordFiltering/DefaultWordFilter.cs b/TagsCloudCore/WordProcessing/WordFiltering/DefaultWordFilter.cs index 1f79166dc..cc6415f0b 100644 --- a/TagsCloudCore/WordProcessing/WordFiltering/DefaultWordFilter.cs +++ b/TagsCloudCore/WordProcessing/WordFiltering/DefaultWordFilter.cs @@ -6,14 +6,16 @@ namespace TagsCloudCore.WordProcessing.WordFiltering; public class DefaultWordFilter : IWordFilter { private readonly HashSet _wordsToExclude; - - public DefaultWordFilter(IWordProvider wordsToExclude) + + public DefaultWordFilter(IWordProvider wordsToExclude, string resourceLocation) { - _wordsToExclude = WordProcessingUtils.RemoveDuplicates(wordsToExclude.Words.Select(w => w.ToLower())); + _wordsToExclude = WordProcessingUtils.RemoveDuplicates(wordsToExclude.GetWords(resourceLocation).Select(w => w.ToLower())); } public string[] FilterWords(string[] words) - => words + { + return words .Where(w => !_wordsToExclude.Contains(w)) .ToArray(); + } } \ No newline at end of file diff --git a/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs b/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs index 4a7965e4d..d1d48b449 100644 --- a/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs +++ b/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs @@ -1,16 +1,20 @@ using TagsCloudCore.BuildingOptions; using TagsCloudCore.WordProcessing.WordFiltering; +using TagsCloudCore.WordProcessing.WordInput; namespace TagsCloudCore.WordProcessing.WordGrouping; public class DefaultWordProcessor : IProcessedWordProvider { - private readonly string[] _words; private readonly IEnumerable _filters; + private readonly WordProviderInfo _wordProviderInfo; + private readonly IEnumerable _wordProviders; - public DefaultWordProcessor(ICommonOptionsProvider commonOptionsProvider, IEnumerable filters) + public DefaultWordProcessor(ICommonOptionsProvider commonOptionsProvider, IEnumerable filters, + IEnumerable wordProviders) { - _words = commonOptionsProvider.CommonOptions.WordProvider.Words.Select(w => w.ToLower()).ToArray(); + _wordProviderInfo = commonOptionsProvider.CommonOptions.WordProviderInfo; + _wordProviders = wordProviders; _filters = filters; } @@ -18,7 +22,9 @@ public DefaultWordProcessor(ICommonOptionsProvider commonOptionsProvider, IEnume private Dictionary ProcessWords() { - var filtered = _filters.Aggregate(_words, (current, filter) => filter.FilterWords(current)); + var provider = _wordProviders.SingleOrDefault(p => p.Match(_wordProviderInfo.Type)); + var words = provider!.GetWords(_wordProviderInfo.ResourceLocation).Select(w => w.ToLower()).ToArray(); + var filtered = _filters.Aggregate(words, (current, filter) => filter.FilterWords(current)); return GroupWords(filtered); } diff --git a/TagsCloudCore/WordProcessing/WordInput/DocxFileWordParser.cs b/TagsCloudCore/WordProcessing/WordInput/DocxFileWordParser.cs index b444f7105..2ccceb7b7 100644 --- a/TagsCloudCore/WordProcessing/WordInput/DocxFileWordParser.cs +++ b/TagsCloudCore/WordProcessing/WordInput/DocxFileWordParser.cs @@ -1,31 +1,30 @@ using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; +using TagsCloudCore.Common.Enums; namespace TagsCloudCore.WordProcessing.WordInput; public class DocxFileWordParser : IWordProvider { - private readonly string _filePath; - - public DocxFileWordParser(string filePath) + public string[] GetWords(string resourceLocation) { - _filePath = filePath; - } - - public string[] Words => Parse(); + using var wordDocument = WordprocessingDocument.Open(resourceLocation, false); - private string[] Parse() - { - using var wordDocument = WordprocessingDocument.Open(_filePath, false); - var body = wordDocument.MainDocumentPart?.Document.Body; if (body is null) throw new IOException( - $"Failed to read from file {_filePath} Most likely the file path is incorrect or the file is corrupted."); + $"Failed to read from file {resourceLocation} Most likely the file path is incorrect or the file is corrupted."); return body.Elements() .Select(paragraph => paragraph.InnerText) .ToArray(); } + + public WordProviderType Info => WordProviderType.Docx; + + public bool Match(WordProviderType info) + { + return info == Info || info == WordProviderType.Doc; + } } \ No newline at end of file diff --git a/TagsCloudCore/WordProcessing/WordInput/IWordProvider.cs b/TagsCloudCore/WordProcessing/WordInput/IWordProvider.cs index 0a5ae5007..c7b25ee79 100644 --- a/TagsCloudCore/WordProcessing/WordInput/IWordProvider.cs +++ b/TagsCloudCore/WordProcessing/WordInput/IWordProvider.cs @@ -1,6 +1,12 @@ -namespace TagsCloudCore.WordProcessing.WordInput; +using TagsCloudCore.Common.Enums; + +namespace TagsCloudCore.WordProcessing.WordInput; public interface IWordProvider { - public string[] Words { get; } + public string[] GetWords(string resourceLocation); + + WordProviderType Info { get; } + + public bool Match(WordProviderType info) => info == Info; } \ No newline at end of file diff --git a/TagsCloudCore/WordProcessing/WordInput/TxtFileWordParser.cs b/TagsCloudCore/WordProcessing/WordInput/TxtFileWordParser.cs index e7c7aaf1c..b31ccd80f 100644 --- a/TagsCloudCore/WordProcessing/WordInput/TxtFileWordParser.cs +++ b/TagsCloudCore/WordProcessing/WordInput/TxtFileWordParser.cs @@ -1,29 +1,25 @@ -namespace TagsCloudCore.WordProcessing.WordInput; +using TagsCloudCore.Common.Enums; +namespace TagsCloudCore.WordProcessing.WordInput; public class TxtFileWordParser : IWordProvider { - private readonly string _filePath; - - public TxtFileWordParser(string filePath) - { - _filePath = filePath; - } - - public string[] Words => Parse(); - - private string[] Parse() + public string[] GetWords(string resourceLocation) { string[] line; try { - line = File.ReadAllLines(_filePath); + line = File.ReadAllLines(resourceLocation); } catch (Exception e) { - throw new IOException($"Failed to read from file {_filePath} Most likely the file path is incorrect or the file is corrupted.", e); + throw new IOException( + $"Failed to read from file {resourceLocation} Most likely the file path is incorrect or the file is corrupted.", + e); } return line; } + + public WordProviderType Info => WordProviderType.Txt; } \ No newline at end of file diff --git a/TagsCloudCore/WordProcessing/WordInput/WordProviderInfo.cs b/TagsCloudCore/WordProcessing/WordInput/WordProviderInfo.cs new file mode 100644 index 000000000..c65e8eab4 --- /dev/null +++ b/TagsCloudCore/WordProcessing/WordInput/WordProviderInfo.cs @@ -0,0 +1,5 @@ +using TagsCloudCore.Common.Enums; + +namespace TagsCloudCore.WordProcessing.WordInput; + +public record WordProviderInfo(WordProviderType Type, string ResourceLocation); \ No newline at end of file diff --git a/TagsCloudCoreTests/Drawing/DefaultImageDrawerTests.cs b/TagsCloudCoreTests/Drawing/DefaultImageDrawerTests.cs index e6b51f5f3..bd96e9d0c 100644 --- a/TagsCloudCoreTests/Drawing/DefaultImageDrawerTests.cs +++ b/TagsCloudCoreTests/Drawing/DefaultImageDrawerTests.cs @@ -14,6 +14,7 @@ public class DefaultImageDrawerTests [TestCase(@"\:\", "filename")] public void SaveImage_ThrowsArgumentException_OnInvalidParameters(string dirPath, string filename) { - Assert.Throws(() => DefaultImageDrawer.SaveImage(new Bitmap(1, 1), dirPath, filename, ImageFormat.Png)); + Assert.Throws(() => + DefaultImageDrawer.SaveImage(new Bitmap(1, 1), dirPath, filename, ImageFormat.Png)); } } \ No newline at end of file diff --git a/TagsCloudCoreTests/TagsCloudCoreTests.csproj b/TagsCloudCoreTests/TagsCloudCoreTests.csproj index a54005d70..fe0ce4078 100644 --- a/TagsCloudCoreTests/TagsCloudCoreTests.csproj +++ b/TagsCloudCoreTests/TagsCloudCoreTests.csproj @@ -11,9 +11,9 @@ - + - + @@ -21,13 +21,13 @@ - + - - ..\TagsCloudCore\libs\TagsCloudVisualization.dll - + + ..\TagsCloudCore\libs\TagsCloudVisualization.dll + diff --git a/TagsCloudCoreTests/Utils/DrawingUtilsTests.cs b/TagsCloudCoreTests/Utils/DrawingUtilsTests.cs index c44c70757..f9ee3eba9 100644 --- a/TagsCloudCoreTests/Utils/DrawingUtilsTests.cs +++ b/TagsCloudCoreTests/Utils/DrawingUtilsTests.cs @@ -62,6 +62,6 @@ public void TryParseRgb_ReturnsCorrectColor_OnCorrectInputData() { _ = DrawingUtils.TryParseRgb("123 43 5", out var color); - CollectionAssert.AreEqual(new []{123, 43, 5}, new []{color.R, color.G, color.B}); + CollectionAssert.AreEqual(new[] {123, 43, 5}, new[] {color.R, color.G, color.B}); } } \ No newline at end of file diff --git a/TagsCloudCoreTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs b/TagsCloudCoreTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs index 6666cd37e..f2a4f03a8 100644 --- a/TagsCloudCoreTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs +++ b/TagsCloudCoreTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs @@ -10,11 +10,11 @@ public class DefaultWordFilterTests public void FilterWords_ReturnsCorrectResult_OnCorrectInputData() { var wordProvider = Substitute.For(); - wordProvider.Words.Returns(new[] {"word1", "word2", "123"}); + wordProvider.GetWords(Arg.Any()).Returns(new[] {"word1", "word2", "123"}); - var filter = new DefaultWordFilter(wordProvider); + var filter = new DefaultWordFilter(wordProvider, ""); var filtered = filter.FilterWords(new[] {"word1", "1234", "word2", "a", "another", "test"}); - - CollectionAssert.AreEqual(new []{"1234", "a", "another", "test"}, filtered); + + CollectionAssert.AreEqual(new[] {"1234", "a", "another", "test"}, filtered); } } \ No newline at end of file diff --git a/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs b/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs index 480a18e35..5aaa2072b 100644 --- a/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs +++ b/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs @@ -1,10 +1,9 @@ using NSubstitute; using TagsCloudCore.BuildingOptions; -using TagsCloudCore.Drawing.Colorers; +using TagsCloudCore.Common.Enums; using TagsCloudCore.WordProcessing.WordFiltering; using TagsCloudCore.WordProcessing.WordGrouping; using TagsCloudCore.WordProcessing.WordInput; -using TagsCloudVisualization; namespace TagsCloudCoreTests.WordProcessing.WordGrouping; @@ -14,23 +13,25 @@ public class DefaultWordProcessorTests public void ProcessedWords_ReturnsCorrectlyProcessedWords_OnCorrectInputData() { var options = Substitute.For(); - var words = Substitute.For(); - options.CommonOptions.Returns(new CommonOptions(words, Substitute.For(), - Substitute.For())); - words.Words.Returns(new[] {"1", "2", "4", "4", "1", "1", "5"}); + options.CommonOptions.Returns(new CommonOptions(new WordProviderInfo(WordProviderType.Txt, ""), WordColorerAlgorithm.Default, + CloudBuildingAlgorithm.Circular)); + var source = Substitute.For(); + source.GetWords("").Returns(new[] {"1", "2", "4", "4", "1", "1", "5"}); + source.Match(WordProviderType.Txt).Returns(true); var filter1 = GetWordFilter(new[] {"2"}); var filter2 = GetWordFilter(new[] {"5"}); - var processor = new DefaultWordProcessor(options, new[] {filter1, filter2}); + var processor = new DefaultWordProcessor(options, new[] {filter1, filter2}, + new [] {source}); var result = processor.ProcessedWords; - + CollectionAssert.AreEqual(new Dictionary {{"1", 3}, {"4", 2}}, result); } - + private static DefaultWordFilter GetWordFilter(string[] wordsToExclude) { var wordProvider = Substitute.For(); - wordProvider.Words.Returns(wordsToExclude); - return new DefaultWordFilter(wordProvider); + wordProvider.GetWords("").Returns(wordsToExclude); + return new DefaultWordFilter(wordProvider, ""); } } \ No newline at end of file From ee590c8e87e479a9bf9bbcaea556572746038c74 Mon Sep 17 00:00:00 2001 From: Alexander Gorbatov Date: Mon, 29 Jan 2024 22:02:14 +0500 Subject: [PATCH 19/19] CommonOptions refactored --- .../ConsoleSettingsProvider.cs | 88 ++++++++----------- TagsCloudConsoleUI/Program.cs | 15 ++-- .../Providers/CloudAlgorithmProvider.cs | 13 --- TagsCloudConsoleUI/Providers/WordProviders.cs | 14 --- TagsCloudConsoleUI/TagsCloudConsoleUI.csproj | 10 +-- .../BuildingOptions/CommonOptions.cs | 24 ++--- .../BuildingOptions/DrawingOptions.cs | 18 +--- TagsCloudCore/Common/DiContainerBuilder.cs | 12 +-- .../Common/Enums/CloudBuildingAlgorithm.cs | 6 ++ .../Common/Enums/WordColorerAlgorithm.cs | 8 ++ .../Common/Enums/WordProviderType.cs | 8 ++ TagsCloudCore/Common/WordData.cs | 6 +- .../Drawing/Colorers/BicolorColorer.cs | 9 +- .../Drawing/Colorers/DefaultWordColorer.cs | 10 ++- .../Drawing/Colorers/IWordColorer.cs | 8 +- .../Drawing/Colorers/RandomWordColorer.cs | 9 +- TagsCloudCore/Drawing/DefaultImageDrawer.cs | 16 ++-- TagsCloudCore/Drawing/IImageDrawer.cs | 3 +- .../Providers/CloudAlgorithmProvider.cs | 14 +++ .../DefaultWordCloudDistributor.cs | 11 ++- TagsCloudCore/TagsCloudCore.csproj | 10 +-- TagsCloudCore/Utils/DrawingUtils.cs | 4 +- TagsCloudCore/Utils/WordProcessingUtils.cs | 4 +- .../WordFiltering/DefaultWordFilter.cs | 10 ++- .../WordGrouping/DefaultWordProcessor.cs | 14 ++- .../WordInput/DocxFileWordParser.cs | 23 +++-- .../WordProcessing/WordInput/IWordProvider.cs | 10 ++- .../WordInput/TxtFileWordParser.cs | 22 ++--- .../WordInput/WordProviderInfo.cs | 5 ++ .../Drawing/DefaultImageDrawerTests.cs | 3 +- TagsCloudCoreTests/TagsCloudCoreTests.csproj | 12 +-- TagsCloudCoreTests/Utils/DrawingUtilsTests.cs | 2 +- .../WordFiltering/DefaultWordFilterTests.cs | 8 +- .../WordGrouping/DefaultWordProcessorTests.cs | 23 ++--- 34 files changed, 233 insertions(+), 219 deletions(-) rename TagsCloudConsoleUI/{Providers => }/ConsoleSettingsProvider.cs (61%) delete mode 100644 TagsCloudConsoleUI/Providers/CloudAlgorithmProvider.cs delete mode 100644 TagsCloudConsoleUI/Providers/WordProviders.cs create mode 100644 TagsCloudCore/Common/Enums/CloudBuildingAlgorithm.cs create mode 100644 TagsCloudCore/Common/Enums/WordColorerAlgorithm.cs create mode 100644 TagsCloudCore/Common/Enums/WordProviderType.cs create mode 100644 TagsCloudCore/Providers/CloudAlgorithmProvider.cs create mode 100644 TagsCloudCore/WordProcessing/WordInput/WordProviderInfo.cs diff --git a/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs b/TagsCloudConsoleUI/ConsoleSettingsProvider.cs similarity index 61% rename from TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs rename to TagsCloudConsoleUI/ConsoleSettingsProvider.cs index ac6434069..eef010fe8 100644 --- a/TagsCloudConsoleUI/Providers/ConsoleSettingsProvider.cs +++ b/TagsCloudConsoleUI/ConsoleSettingsProvider.cs @@ -2,47 +2,39 @@ using System.Text; using McMaster.Extensions.CommandLineUtils; using TagsCloudCore.BuildingOptions; -using TagsCloudCore.Common; -using TagsCloudCore.Drawing.Colorers; +using TagsCloudCore.Common.Enums; using TagsCloudCore.Utils; using TagsCloudCore.WordProcessing.WordInput; -namespace TagsCloudConsoleUI.Providers; +namespace TagsCloudConsoleUI; public class ConsoleSettingsProvider : IDrawingOptionsProvider, ICommonOptionsProvider { - public DrawingOptions DrawingOptions => _drawingOptions ??= GetDrawingOptions(); - - public CommonOptions CommonOptions => _commonOptions ??= GetCommonOptions(); + private CommonOptions? _commonOptions; private DrawingOptions? _drawingOptions; - private CommonOptions? _commonOptions; - private bool _isCustomColoringUsed; - private readonly IEnumerable _registeredWordColorers; - public ConsoleSettingsProvider(IEnumerable registeredWordColorers) - { - _registeredWordColorers = registeredWordColorers; - } - + public CommonOptions CommonOptions => _commonOptions ??= GetCommonOptions(); + public DrawingOptions DrawingOptions => _drawingOptions ??= GetDrawingOptions(); + private DrawingOptions GetDrawingOptions() { var font = GetFont(); var backgroundColor = GetRgbColor("Enter background color in RGB format separated by space"); var fontColor = GetFontColor(); - + var imageSide = GetInteger( "Enter the image's desired size in px. The image will be a square. It must range from 500 px to 5000 px.", 500, 5000); - + var imageSize = new Size(imageSide, imageSide); - + var frequencyScaling = GetInteger( - "Enter the frequency scaling (a positive integer). It Determines the scale to word frequency ratio.", - 1, 100); + "Enter the frequency scaling (a positive integer). It Determines the scale to word frequency ratio.", + 1, 100); return new DrawingOptions(fontColor, backgroundColor, imageSize, font, frequencyScaling); } @@ -51,19 +43,18 @@ private CommonOptions GetCommonOptions() { var wordProvider = GetWordProvider(); var wordColorer = GetWordColorer(); - var cloudLayouter = GetAlgorithm(CloudAlgorithmProvider.RegisteredProviders, - "Choose the cloud forming algorithm:"); - + var cloudLayouter = GetCloudBuildingAlgorithm(); + return new CommonOptions(wordProvider, wordColorer, cloudLayouter); } private Color GetFontColor() { - return _isCustomColoringUsed - ? Color.White + return _isCustomColoringUsed + ? Color.White : GetRgbColor("Enter font color in RGB format separated by space"); } - + private static int GetInteger(string prompt, int lowerAllowedBoundary, int upperAllowedBoundary) { while (true) @@ -81,7 +72,7 @@ private static Color GetRgbColor(string prompt) while (true) { var colorString = Prompt.GetString(prompt, "", - promptColor: ConsoleColor.DarkGreen); + ConsoleColor.DarkGreen); if (DrawingUtils.TryParseRgb(colorString!, out var color)) return color; Console.WriteLine("Ivalid color format. Try again."); @@ -101,46 +92,43 @@ private static Font GetFont() } } - private IWordColorer GetWordColorer() + private WordColorerAlgorithm GetWordColorer() { var sb = new StringBuilder("Choose coloring algorithm:\n"); - foreach (var registeredWordColorer in _registeredWordColorers) - sb.AppendLine(registeredWordColorer.Name); + foreach (var registeredWordColorer in Enum.GetNames(typeof(WordColorerAlgorithm))) + sb.AppendLine(registeredWordColorer); while (true) { - var input = Prompt.GetString(sb.ToString(), "", promptColor: ConsoleColor.DarkGreen); - var colorer = _registeredWordColorers.SingleOrDefault(c => c.Match(input!)); - if (colorer is null) + var input = Prompt.GetString(sb.ToString(), "", ConsoleColor.DarkGreen); + if (Enum.TryParse(input, out WordColorerAlgorithm algorithm)) { - Console.WriteLine("The provided algorithm does not exist. Try again."); - continue; + if (algorithm != WordColorerAlgorithm.Default) + _isCustomColoringUsed = true; + return algorithm; } - if (colorer.Name != AppSettings.DefaultColorerName) - _isCustomColoringUsed = true; - return colorer; + Console.WriteLine("The provided word colorer does not exist. Try again."); } } - private static T GetAlgorithm(IReadOnlyDictionary registeredAlgorithms, - string prompt) + private static CloudBuildingAlgorithm GetCloudBuildingAlgorithm() { - var sb = new StringBuilder($"{prompt}\n"); - foreach (var algorithmProvider in registeredAlgorithms.Keys) - sb.AppendLine(algorithmProvider); + var sb = new StringBuilder("Choose cloud forming algorithm:\n"); + foreach (var algorithm in Enum.GetNames(typeof(CloudBuildingAlgorithm))) + sb.AppendLine(algorithm); while (true) { - var algorithm = Prompt.GetString(sb.ToString(), "", promptColor: ConsoleColor.DarkGreen); - if (registeredAlgorithms.TryGetValue(algorithm!, out var provider)) - return provider; + var input = Prompt.GetString(sb.ToString(), "", ConsoleColor.DarkGreen); + if (Enum.TryParse(input, out CloudBuildingAlgorithm algorithm)) + return algorithm; - Console.WriteLine("Specified algorithm isn't supported. Try again."); + Console.WriteLine("The provided algorithm does not exist. Try again."); } } - private static IWordProvider GetWordProvider() + private static WordProviderInfo GetWordProvider() { while (true) { @@ -151,9 +139,9 @@ private static IWordProvider GetWordProvider() continue; } - var ext = Path.GetExtension(path); - if (WordProviders.RegisteredProviders.TryGetValue(ext, out var provider)) - return provider(path); + var ext = Path.GetExtension(path)[1..]; + if (Enum.TryParse(ext, ignoreCase: true, out WordProviderType type)) + return new WordProviderInfo(type, path); Console.WriteLine("This extension is not supported. Try again."); } diff --git a/TagsCloudConsoleUI/Program.cs b/TagsCloudConsoleUI/Program.cs index d176141b9..2c3bf6ce8 100644 --- a/TagsCloudConsoleUI/Program.cs +++ b/TagsCloudConsoleUI/Program.cs @@ -1,8 +1,8 @@ using System.Drawing.Imaging; using Autofac; -using TagsCloudConsoleUI.Providers; using TagsCloudCore.BuildingOptions; using TagsCloudCore.Common; +using TagsCloudCore.Common.Enums; using TagsCloudCore.Drawing; namespace TagsCloudConsoleUI; @@ -14,24 +14,27 @@ public static void Main() try { var containterBuilder = DiContainerBuilder.RegisterDefaultDependencies(); - + containterBuilder.RegisterType() .As() .SingleInstance(); var container = containterBuilder.Build(); - BuildTagCloud(container.Resolve(), ".", "image.png", ImageFormat.Png); + BuildTagCloud(container.Resolve(), + container.Resolve().CommonOptions.WordColorer, ".", "image.png", + ImageFormat.Png); } catch (Exception e) { Console.WriteLine(e); } } - - private static void BuildTagCloud(IImageDrawer imageDrawer, string dirPath, string fileName, ImageFormat imageFormat) + + private static void BuildTagCloud(IImageDrawer imageDrawer, WordColorerAlgorithm colorerAlgorithm, string dirPath, + string fileName, ImageFormat imageFormat) { - var bitmap = imageDrawer.DrawImage(); + var bitmap = imageDrawer.DrawImage(colorerAlgorithm); DefaultImageDrawer.SaveImage(bitmap, dirPath, fileName, imageFormat); Console.WriteLine($"The image has been saved to \"{dirPath}\""); } diff --git a/TagsCloudConsoleUI/Providers/CloudAlgorithmProvider.cs b/TagsCloudConsoleUI/Providers/CloudAlgorithmProvider.cs deleted file mode 100644 index 029880672..000000000 --- a/TagsCloudConsoleUI/Providers/CloudAlgorithmProvider.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Drawing; -using TagsCloudVisualization; - -namespace TagsCloudConsoleUI.Providers; - -public static class CloudAlgorithmProvider -{ - public static readonly IReadOnlyDictionary RegisteredProviders = - new Dictionary - { - {"Circular", new CircularCloudLayouter(Point.Empty)} - }; -} \ No newline at end of file diff --git a/TagsCloudConsoleUI/Providers/WordProviders.cs b/TagsCloudConsoleUI/Providers/WordProviders.cs deleted file mode 100644 index 37195268c..000000000 --- a/TagsCloudConsoleUI/Providers/WordProviders.cs +++ /dev/null @@ -1,14 +0,0 @@ -using TagsCloudCore.WordProcessing.WordInput; - -namespace TagsCloudConsoleUI.Providers; - -public static class WordProviders -{ - public static readonly IReadOnlyDictionary> RegisteredProviders = - new Dictionary> - { - {".txt", path=> new TxtFileWordParser(path)}, - {".docx", path => new DocxFileWordParser(path)}, - {".doc", path => new DocxFileWordParser(path)} - }; -} \ No newline at end of file diff --git a/TagsCloudConsoleUI/TagsCloudConsoleUI.csproj b/TagsCloudConsoleUI/TagsCloudConsoleUI.csproj index 4aa0bde9d..9242f5bf1 100644 --- a/TagsCloudConsoleUI/TagsCloudConsoleUI.csproj +++ b/TagsCloudConsoleUI/TagsCloudConsoleUI.csproj @@ -8,17 +8,17 @@ - + - - ..\TagsCloudCore\libs\TagsCloudVisualization.dll - + + ..\TagsCloudCore\libs\TagsCloudVisualization.dll + - + diff --git a/TagsCloudCore/BuildingOptions/CommonOptions.cs b/TagsCloudCore/BuildingOptions/CommonOptions.cs index 290212da4..5b08c8a6f 100644 --- a/TagsCloudCore/BuildingOptions/CommonOptions.cs +++ b/TagsCloudCore/BuildingOptions/CommonOptions.cs @@ -1,21 +1,9 @@ -using TagsCloudCore.Drawing.Colorers; -using TagsCloudCore.WordProcessing.WordInput; -using TagsCloudVisualization; +using TagsCloudCore.Common.Enums; +using WordProviderInfo = TagsCloudCore.WordProcessing.WordInput.WordProviderInfo; namespace TagsCloudCore.BuildingOptions; -public class CommonOptions -{ - public IWordProvider WordProvider { get; } - - public IWordColorer? WordColorer { get; } - - public ICloudLayouter CloudLayouter { get; } - - public CommonOptions(IWordProvider wordProvider, IWordColorer? wordColorer, ICloudLayouter cloudLayouter) - { - WordProvider = wordProvider; - WordColorer = wordColorer; - CloudLayouter = cloudLayouter; - } -} \ No newline at end of file +public record CommonOptions( + WordProviderInfo WordProviderInfo, + WordColorerAlgorithm WordColorer, + CloudBuildingAlgorithm CloudBuildingAlgorithm); \ No newline at end of file diff --git a/TagsCloudCore/BuildingOptions/DrawingOptions.cs b/TagsCloudCore/BuildingOptions/DrawingOptions.cs index 1ee99debd..fd22038d1 100644 --- a/TagsCloudCore/BuildingOptions/DrawingOptions.cs +++ b/TagsCloudCore/BuildingOptions/DrawingOptions.cs @@ -2,20 +2,4 @@ namespace TagsCloudCore.BuildingOptions; -public class DrawingOptions -{ - public Color FontColor { get; } - public Color BackgroundColor { get; } - public Size ImageSize { get; } - public Font Font { get; } - public int FrequencyScaling { get; } - - public DrawingOptions(Color fontColor, Color backgroundColor, Size imageSize, Font font, int frequencyScaling) - { - FontColor = fontColor; - BackgroundColor = backgroundColor; - ImageSize = imageSize; - Font = font; - FrequencyScaling = frequencyScaling; - } -} \ No newline at end of file +public record DrawingOptions(Color FontColor, Color BackgroundColor, Size ImageSize, Font Font, int FrequencyScaling); \ No newline at end of file diff --git a/TagsCloudCore/Common/DiContainerBuilder.cs b/TagsCloudCore/Common/DiContainerBuilder.cs index 2a9857ab4..92d8ffcdd 100644 --- a/TagsCloudCore/Common/DiContainerBuilder.cs +++ b/TagsCloudCore/Common/DiContainerBuilder.cs @@ -15,16 +15,18 @@ public static ContainerBuilder RegisterDefaultDependencies() var containerBuilder = new ContainerBuilder(); containerBuilder.RegisterAssemblyTypes(typeof(IWordColorer).Assembly).As().SingleInstance(); - + + containerBuilder.RegisterAssemblyTypes(typeof(IWordProvider).Assembly).As().SingleInstance(); + containerBuilder.RegisterType().As().SingleInstance(); - + containerBuilder - .RegisterInstance(new DefaultWordFilter(new TxtFileWordParser(AppSettings.PathToBoringWordsFilter))) + .RegisterInstance(new DefaultWordFilter(new TxtFileWordParser(), AppSettings.PathToBoringWordsFilter)) .As() .SingleInstance(); - + containerBuilder.RegisterType().As().SingleInstance(); - + containerBuilder.RegisterType().As() .SingleInstance(); diff --git a/TagsCloudCore/Common/Enums/CloudBuildingAlgorithm.cs b/TagsCloudCore/Common/Enums/CloudBuildingAlgorithm.cs new file mode 100644 index 000000000..c45220ae0 --- /dev/null +++ b/TagsCloudCore/Common/Enums/CloudBuildingAlgorithm.cs @@ -0,0 +1,6 @@ +namespace TagsCloudCore.Common.Enums; + +public enum CloudBuildingAlgorithm +{ + Circular +} \ No newline at end of file diff --git a/TagsCloudCore/Common/Enums/WordColorerAlgorithm.cs b/TagsCloudCore/Common/Enums/WordColorerAlgorithm.cs new file mode 100644 index 000000000..61421d6a8 --- /dev/null +++ b/TagsCloudCore/Common/Enums/WordColorerAlgorithm.cs @@ -0,0 +1,8 @@ +namespace TagsCloudCore.Common.Enums; + +public enum WordColorerAlgorithm +{ + Default, + Random, + Bicolor +} \ No newline at end of file diff --git a/TagsCloudCore/Common/Enums/WordProviderType.cs b/TagsCloudCore/Common/Enums/WordProviderType.cs new file mode 100644 index 000000000..4b809f65d --- /dev/null +++ b/TagsCloudCore/Common/Enums/WordProviderType.cs @@ -0,0 +1,8 @@ +namespace TagsCloudCore.Common.Enums; + +public enum WordProviderType +{ + Txt, + Docx, + Doc +} \ No newline at end of file diff --git a/TagsCloudCore/Common/WordData.cs b/TagsCloudCore/Common/WordData.cs index 2b17d9ff7..bea4b4ce7 100644 --- a/TagsCloudCore/Common/WordData.cs +++ b/TagsCloudCore/Common/WordData.cs @@ -4,12 +4,12 @@ namespace TagsCloudCore.Common; public class WordData { - public Rectangle Rectangle { get; } - public int Frequency { get; } - public WordData(Rectangle rectangle, int frequency) { Rectangle = rectangle; Frequency = frequency; } + + public Rectangle Rectangle { get; } + public int Frequency { get; } } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs b/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs index 8bcaa4958..1021d2063 100644 --- a/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/BicolorColorer.cs @@ -1,15 +1,18 @@ using System.Drawing; +using TagsCloudCore.Common.Enums; namespace TagsCloudCore.Drawing.Colorers; public class BicolorColorer : IWordColorer { + private static readonly Random Random = new(); private readonly Color _first = Color.FromArgb(Random.Next(0, 256), Random.Next(0, 256), Random.Next(0, 256)); private readonly Color _second = Color.FromArgb(Random.Next(0, 256), Random.Next(0, 256), Random.Next(0, 256)); - private static readonly Random Random = new(); public Color GetWordColor(string word, int wordFrequency) - => Random.Next(0, 2) == 0 ? _first : _second; + { + return Random.Next(0, 2) == 0 ? _first : _second; + } - public string Name => "Bicolor"; + public WordColorerAlgorithm AlgorithmName => WordColorerAlgorithm.Bicolor; } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs b/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs index 733a0c163..3126d9d83 100644 --- a/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/DefaultWordColorer.cs @@ -1,20 +1,22 @@ using System.Drawing; using TagsCloudCore.BuildingOptions; -using TagsCloudCore.Common; +using TagsCloudCore.Common.Enums; namespace TagsCloudCore.Drawing.Colorers; public class DefaultWordColorer : IWordColorer { private readonly Lazy _drawingOptionsProvider; - + public DefaultWordColorer(Lazy drawingOptionsProvider) { _drawingOptionsProvider = drawingOptionsProvider; } public Color GetWordColor(string word, int wordFrequency) - => _drawingOptionsProvider.Value.DrawingOptions.FontColor; + { + return _drawingOptionsProvider.Value.DrawingOptions.FontColor; + } - public string Name => AppSettings.DefaultColorerName; + public WordColorerAlgorithm AlgorithmName => WordColorerAlgorithm.Default; } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/Colorers/IWordColorer.cs b/TagsCloudCore/Drawing/Colorers/IWordColorer.cs index e1ca21ece..393438e1d 100644 --- a/TagsCloudCore/Drawing/Colorers/IWordColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/IWordColorer.cs @@ -1,11 +1,15 @@ using System.Drawing; +using TagsCloudCore.Common.Enums; namespace TagsCloudCore.Drawing.Colorers; public interface IWordColorer { + public WordColorerAlgorithm AlgorithmName { get; } public Color GetWordColor(string word, int wordFrequency); - public string Name { get; } - public bool Match(string colorerName) => colorerName == Name; + public bool Match(WordColorerAlgorithm algorithmName) + { + return AlgorithmName == algorithmName; + } } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs b/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs index 97ee81b31..a8a87997e 100644 --- a/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs +++ b/TagsCloudCore/Drawing/Colorers/RandomWordColorer.cs @@ -1,13 +1,16 @@ using System.Drawing; +using TagsCloudCore.Common.Enums; namespace TagsCloudCore.Drawing.Colorers; public class RandomWordColorer : IWordColorer { private static readonly Random Random = new(); - + public Color GetWordColor(string word, int wordFrequency) - => Color.FromArgb(Random.Next(0, 256), Random.Next(0, 256), Random.Next(0, 256)); + { + return Color.FromArgb(Random.Next(0, 256), Random.Next(0, 256), Random.Next(0, 256)); + } - public string Name => "Random"; + public WordColorerAlgorithm AlgorithmName => WordColorerAlgorithm.Random; } \ No newline at end of file diff --git a/TagsCloudCore/Drawing/DefaultImageDrawer.cs b/TagsCloudCore/Drawing/DefaultImageDrawer.cs index f1c935ac9..178d697b0 100644 --- a/TagsCloudCore/Drawing/DefaultImageDrawer.cs +++ b/TagsCloudCore/Drawing/DefaultImageDrawer.cs @@ -2,6 +2,7 @@ using System.Drawing.Imaging; using TagsCloudCore.BuildingOptions; using TagsCloudCore.Common; +using TagsCloudCore.Common.Enums; using TagsCloudCore.Drawing.Colorers; using TagsCloudCore.TagCloudForming; @@ -11,17 +12,17 @@ public class DefaultImageDrawer : IImageDrawer { private readonly IReadOnlyDictionary _distributedWords; private readonly DrawingOptions _drawingOptions; - private readonly IWordColorer? _colorer; + private readonly IEnumerable _wordColorers; public DefaultImageDrawer(IWordCloudDistributorProvider cloudDistributorProvider, - IDrawingOptionsProvider drawingOptionsProvider, ICommonOptionsProvider commonOptionsProvider) + IDrawingOptionsProvider drawingOptionsProvider, IEnumerable wordColorers) { _distributedWords = cloudDistributorProvider.DistributedWords; _drawingOptions = drawingOptionsProvider.DrawingOptions; - _colorer = commonOptionsProvider.CommonOptions.WordColorer; + _wordColorers = wordColorers; } - public Bitmap DrawImage() + public Bitmap DrawImage(WordColorerAlgorithm colorerAlgorithm) { var bitmap = new Bitmap(_drawingOptions.ImageSize.Width, _drawingOptions.ImageSize.Height); var offset = new Point(_drawingOptions.ImageSize.Width / 2, _drawingOptions.ImageSize.Height / 2); @@ -33,7 +34,12 @@ public Bitmap DrawImage() var sizeAdd = _drawingOptions.FrequencyScaling * (word.Frequency - 1); var newFont = new Font(_drawingOptions.Font.FontFamily, _drawingOptions.Font.Size + sizeAdd, _drawingOptions.Font.Style); - var color = _colorer?.GetWordColor(value, word.Frequency) ?? _drawingOptions.FontColor; + + var colorer = _wordColorers.SingleOrDefault(c => c.Match(colorerAlgorithm)); + var color = colorer!.AlgorithmName == WordColorerAlgorithm.Default + ? _drawingOptions.FontColor + : colorer.GetWordColor(value, word.Frequency); + graphics.DrawString(value, newFont, new SolidBrush(color), word.Rectangle with {X = word.Rectangle.X + offset.X, Y = word.Rectangle.Y + offset.Y}); } diff --git a/TagsCloudCore/Drawing/IImageDrawer.cs b/TagsCloudCore/Drawing/IImageDrawer.cs index 9dbd0b733..7885d687b 100644 --- a/TagsCloudCore/Drawing/IImageDrawer.cs +++ b/TagsCloudCore/Drawing/IImageDrawer.cs @@ -1,8 +1,9 @@ using System.Drawing; +using TagsCloudCore.Common.Enums; namespace TagsCloudCore.Drawing; public interface IImageDrawer { - public Bitmap DrawImage(); + public Bitmap DrawImage(WordColorerAlgorithm colorerAlgorithm); } \ No newline at end of file diff --git a/TagsCloudCore/Providers/CloudAlgorithmProvider.cs b/TagsCloudCore/Providers/CloudAlgorithmProvider.cs new file mode 100644 index 000000000..dcac8d5cd --- /dev/null +++ b/TagsCloudCore/Providers/CloudAlgorithmProvider.cs @@ -0,0 +1,14 @@ +using System.Drawing; +using TagsCloudCore.Common.Enums; +using TagsCloudVisualization; + +namespace TagsCloudCore.Providers; + +public static class CloudAlgorithmProvider +{ + public static readonly IReadOnlyDictionary RegisteredProviders = + new Dictionary + { + {CloudBuildingAlgorithm.Circular, new CircularCloudLayouter(Point.Empty)} + }; +} \ No newline at end of file diff --git a/TagsCloudCore/TagCloudForming/DefaultWordCloudDistributor.cs b/TagsCloudCore/TagCloudForming/DefaultWordCloudDistributor.cs index afddd7bc9..904a94dac 100644 --- a/TagsCloudCore/TagCloudForming/DefaultWordCloudDistributor.cs +++ b/TagsCloudCore/TagCloudForming/DefaultWordCloudDistributor.cs @@ -1,5 +1,6 @@ using TagsCloudCore.BuildingOptions; using TagsCloudCore.Common; +using TagsCloudCore.Providers; using TagsCloudCore.Utils; using TagsCloudCore.WordProcessing.WordGrouping; using TagsCloudVisualization; @@ -8,15 +9,17 @@ namespace TagsCloudCore.TagCloudForming; public class DefaultWordCloudDistributor : IWordCloudDistributorProvider { - private readonly Dictionary _words; private readonly ICloudLayouter _cloudLayouter; private readonly DrawingOptions _drawingOptions; + private readonly Dictionary _words; - public DefaultWordCloudDistributor(IProcessedWordProvider processedWord, ICommonOptionsProvider commonOptionsProvider, + public DefaultWordCloudDistributor(IProcessedWordProvider processedWord, + ICommonOptionsProvider commonOptionsProvider, IDrawingOptionsProvider drawingOptionsProvider) { _words = processedWord.ProcessedWords; - _cloudLayouter = commonOptionsProvider.CommonOptions.CloudLayouter; + _cloudLayouter = + CloudAlgorithmProvider.RegisteredProviders[commonOptionsProvider.CommonOptions.CloudBuildingAlgorithm]; _drawingOptions = drawingOptionsProvider.DrawingOptions; } @@ -32,7 +35,7 @@ private Dictionary DistributeWords() _drawingOptions.FrequencyScaling, _drawingOptions.Font)), frequency); distributed.Add(word, newWord); } - + return distributed; } } \ No newline at end of file diff --git a/TagsCloudCore/TagsCloudCore.csproj b/TagsCloudCore/TagsCloudCore.csproj index dd9040b1f..a8b39d145 100644 --- a/TagsCloudCore/TagsCloudCore.csproj +++ b/TagsCloudCore/TagsCloudCore.csproj @@ -8,12 +8,12 @@ - - - - + + + + - + libs\TagsCloudVisualization.dll diff --git a/TagsCloudCore/Utils/DrawingUtils.cs b/TagsCloudCore/Utils/DrawingUtils.cs index e5debc121..a4d72f55b 100644 --- a/TagsCloudCore/Utils/DrawingUtils.cs +++ b/TagsCloudCore/Utils/DrawingUtils.cs @@ -5,14 +5,14 @@ namespace TagsCloudCore.Utils; public static class DrawingUtils { private static readonly Graphics Graphics = Graphics.FromImage(new Bitmap(1, 1)); - + public static Size GetStringSize(string word, int frequency, int frequencyScaling, Font font) { if (frequency < 1) throw new ArgumentException("Frequency must be a positive integer"); if (frequencyScaling < 1) throw new ArgumentException("Frequncy scaling must be a positive integer"); - + var sizeIncrement = frequencyScaling * (frequency - 1); var newFont = new Font(font.FontFamily, font.Size + sizeIncrement, font.Style); return Size.Ceiling(Graphics.MeasureString(word, newFont)); diff --git a/TagsCloudCore/Utils/WordProcessingUtils.cs b/TagsCloudCore/Utils/WordProcessingUtils.cs index bbdae4d78..1dad1cb62 100644 --- a/TagsCloudCore/Utils/WordProcessingUtils.cs +++ b/TagsCloudCore/Utils/WordProcessingUtils.cs @@ -3,7 +3,9 @@ public static class WordProcessingUtils { public static HashSet RemoveDuplicates(IEnumerable lines) - => lines + { + return lines .Distinct() .ToHashSet(); + } } \ No newline at end of file diff --git a/TagsCloudCore/WordProcessing/WordFiltering/DefaultWordFilter.cs b/TagsCloudCore/WordProcessing/WordFiltering/DefaultWordFilter.cs index 1f79166dc..cc6415f0b 100644 --- a/TagsCloudCore/WordProcessing/WordFiltering/DefaultWordFilter.cs +++ b/TagsCloudCore/WordProcessing/WordFiltering/DefaultWordFilter.cs @@ -6,14 +6,16 @@ namespace TagsCloudCore.WordProcessing.WordFiltering; public class DefaultWordFilter : IWordFilter { private readonly HashSet _wordsToExclude; - - public DefaultWordFilter(IWordProvider wordsToExclude) + + public DefaultWordFilter(IWordProvider wordsToExclude, string resourceLocation) { - _wordsToExclude = WordProcessingUtils.RemoveDuplicates(wordsToExclude.Words.Select(w => w.ToLower())); + _wordsToExclude = WordProcessingUtils.RemoveDuplicates(wordsToExclude.GetWords(resourceLocation).Select(w => w.ToLower())); } public string[] FilterWords(string[] words) - => words + { + return words .Where(w => !_wordsToExclude.Contains(w)) .ToArray(); + } } \ No newline at end of file diff --git a/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs b/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs index 4a7965e4d..d1d48b449 100644 --- a/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs +++ b/TagsCloudCore/WordProcessing/WordGrouping/DefaultWordProcessor.cs @@ -1,16 +1,20 @@ using TagsCloudCore.BuildingOptions; using TagsCloudCore.WordProcessing.WordFiltering; +using TagsCloudCore.WordProcessing.WordInput; namespace TagsCloudCore.WordProcessing.WordGrouping; public class DefaultWordProcessor : IProcessedWordProvider { - private readonly string[] _words; private readonly IEnumerable _filters; + private readonly WordProviderInfo _wordProviderInfo; + private readonly IEnumerable _wordProviders; - public DefaultWordProcessor(ICommonOptionsProvider commonOptionsProvider, IEnumerable filters) + public DefaultWordProcessor(ICommonOptionsProvider commonOptionsProvider, IEnumerable filters, + IEnumerable wordProviders) { - _words = commonOptionsProvider.CommonOptions.WordProvider.Words.Select(w => w.ToLower()).ToArray(); + _wordProviderInfo = commonOptionsProvider.CommonOptions.WordProviderInfo; + _wordProviders = wordProviders; _filters = filters; } @@ -18,7 +22,9 @@ public DefaultWordProcessor(ICommonOptionsProvider commonOptionsProvider, IEnume private Dictionary ProcessWords() { - var filtered = _filters.Aggregate(_words, (current, filter) => filter.FilterWords(current)); + var provider = _wordProviders.SingleOrDefault(p => p.Match(_wordProviderInfo.Type)); + var words = provider!.GetWords(_wordProviderInfo.ResourceLocation).Select(w => w.ToLower()).ToArray(); + var filtered = _filters.Aggregate(words, (current, filter) => filter.FilterWords(current)); return GroupWords(filtered); } diff --git a/TagsCloudCore/WordProcessing/WordInput/DocxFileWordParser.cs b/TagsCloudCore/WordProcessing/WordInput/DocxFileWordParser.cs index b444f7105..2ccceb7b7 100644 --- a/TagsCloudCore/WordProcessing/WordInput/DocxFileWordParser.cs +++ b/TagsCloudCore/WordProcessing/WordInput/DocxFileWordParser.cs @@ -1,31 +1,30 @@ using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; +using TagsCloudCore.Common.Enums; namespace TagsCloudCore.WordProcessing.WordInput; public class DocxFileWordParser : IWordProvider { - private readonly string _filePath; - - public DocxFileWordParser(string filePath) + public string[] GetWords(string resourceLocation) { - _filePath = filePath; - } - - public string[] Words => Parse(); + using var wordDocument = WordprocessingDocument.Open(resourceLocation, false); - private string[] Parse() - { - using var wordDocument = WordprocessingDocument.Open(_filePath, false); - var body = wordDocument.MainDocumentPart?.Document.Body; if (body is null) throw new IOException( - $"Failed to read from file {_filePath} Most likely the file path is incorrect or the file is corrupted."); + $"Failed to read from file {resourceLocation} Most likely the file path is incorrect or the file is corrupted."); return body.Elements() .Select(paragraph => paragraph.InnerText) .ToArray(); } + + public WordProviderType Info => WordProviderType.Docx; + + public bool Match(WordProviderType info) + { + return info == Info || info == WordProviderType.Doc; + } } \ No newline at end of file diff --git a/TagsCloudCore/WordProcessing/WordInput/IWordProvider.cs b/TagsCloudCore/WordProcessing/WordInput/IWordProvider.cs index 0a5ae5007..c7b25ee79 100644 --- a/TagsCloudCore/WordProcessing/WordInput/IWordProvider.cs +++ b/TagsCloudCore/WordProcessing/WordInput/IWordProvider.cs @@ -1,6 +1,12 @@ -namespace TagsCloudCore.WordProcessing.WordInput; +using TagsCloudCore.Common.Enums; + +namespace TagsCloudCore.WordProcessing.WordInput; public interface IWordProvider { - public string[] Words { get; } + public string[] GetWords(string resourceLocation); + + WordProviderType Info { get; } + + public bool Match(WordProviderType info) => info == Info; } \ No newline at end of file diff --git a/TagsCloudCore/WordProcessing/WordInput/TxtFileWordParser.cs b/TagsCloudCore/WordProcessing/WordInput/TxtFileWordParser.cs index e7c7aaf1c..b31ccd80f 100644 --- a/TagsCloudCore/WordProcessing/WordInput/TxtFileWordParser.cs +++ b/TagsCloudCore/WordProcessing/WordInput/TxtFileWordParser.cs @@ -1,29 +1,25 @@ -namespace TagsCloudCore.WordProcessing.WordInput; +using TagsCloudCore.Common.Enums; +namespace TagsCloudCore.WordProcessing.WordInput; public class TxtFileWordParser : IWordProvider { - private readonly string _filePath; - - public TxtFileWordParser(string filePath) - { - _filePath = filePath; - } - - public string[] Words => Parse(); - - private string[] Parse() + public string[] GetWords(string resourceLocation) { string[] line; try { - line = File.ReadAllLines(_filePath); + line = File.ReadAllLines(resourceLocation); } catch (Exception e) { - throw new IOException($"Failed to read from file {_filePath} Most likely the file path is incorrect or the file is corrupted.", e); + throw new IOException( + $"Failed to read from file {resourceLocation} Most likely the file path is incorrect or the file is corrupted.", + e); } return line; } + + public WordProviderType Info => WordProviderType.Txt; } \ No newline at end of file diff --git a/TagsCloudCore/WordProcessing/WordInput/WordProviderInfo.cs b/TagsCloudCore/WordProcessing/WordInput/WordProviderInfo.cs new file mode 100644 index 000000000..c65e8eab4 --- /dev/null +++ b/TagsCloudCore/WordProcessing/WordInput/WordProviderInfo.cs @@ -0,0 +1,5 @@ +using TagsCloudCore.Common.Enums; + +namespace TagsCloudCore.WordProcessing.WordInput; + +public record WordProviderInfo(WordProviderType Type, string ResourceLocation); \ No newline at end of file diff --git a/TagsCloudCoreTests/Drawing/DefaultImageDrawerTests.cs b/TagsCloudCoreTests/Drawing/DefaultImageDrawerTests.cs index e6b51f5f3..bd96e9d0c 100644 --- a/TagsCloudCoreTests/Drawing/DefaultImageDrawerTests.cs +++ b/TagsCloudCoreTests/Drawing/DefaultImageDrawerTests.cs @@ -14,6 +14,7 @@ public class DefaultImageDrawerTests [TestCase(@"\:\", "filename")] public void SaveImage_ThrowsArgumentException_OnInvalidParameters(string dirPath, string filename) { - Assert.Throws(() => DefaultImageDrawer.SaveImage(new Bitmap(1, 1), dirPath, filename, ImageFormat.Png)); + Assert.Throws(() => + DefaultImageDrawer.SaveImage(new Bitmap(1, 1), dirPath, filename, ImageFormat.Png)); } } \ No newline at end of file diff --git a/TagsCloudCoreTests/TagsCloudCoreTests.csproj b/TagsCloudCoreTests/TagsCloudCoreTests.csproj index a54005d70..fe0ce4078 100644 --- a/TagsCloudCoreTests/TagsCloudCoreTests.csproj +++ b/TagsCloudCoreTests/TagsCloudCoreTests.csproj @@ -11,9 +11,9 @@ - + - + @@ -21,13 +21,13 @@ - + - - ..\TagsCloudCore\libs\TagsCloudVisualization.dll - + + ..\TagsCloudCore\libs\TagsCloudVisualization.dll + diff --git a/TagsCloudCoreTests/Utils/DrawingUtilsTests.cs b/TagsCloudCoreTests/Utils/DrawingUtilsTests.cs index c44c70757..f9ee3eba9 100644 --- a/TagsCloudCoreTests/Utils/DrawingUtilsTests.cs +++ b/TagsCloudCoreTests/Utils/DrawingUtilsTests.cs @@ -62,6 +62,6 @@ public void TryParseRgb_ReturnsCorrectColor_OnCorrectInputData() { _ = DrawingUtils.TryParseRgb("123 43 5", out var color); - CollectionAssert.AreEqual(new []{123, 43, 5}, new []{color.R, color.G, color.B}); + CollectionAssert.AreEqual(new[] {123, 43, 5}, new[] {color.R, color.G, color.B}); } } \ No newline at end of file diff --git a/TagsCloudCoreTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs b/TagsCloudCoreTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs index 6666cd37e..f2a4f03a8 100644 --- a/TagsCloudCoreTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs +++ b/TagsCloudCoreTests/WordProcessing/WordFiltering/DefaultWordFilterTests.cs @@ -10,11 +10,11 @@ public class DefaultWordFilterTests public void FilterWords_ReturnsCorrectResult_OnCorrectInputData() { var wordProvider = Substitute.For(); - wordProvider.Words.Returns(new[] {"word1", "word2", "123"}); + wordProvider.GetWords(Arg.Any()).Returns(new[] {"word1", "word2", "123"}); - var filter = new DefaultWordFilter(wordProvider); + var filter = new DefaultWordFilter(wordProvider, ""); var filtered = filter.FilterWords(new[] {"word1", "1234", "word2", "a", "another", "test"}); - - CollectionAssert.AreEqual(new []{"1234", "a", "another", "test"}, filtered); + + CollectionAssert.AreEqual(new[] {"1234", "a", "another", "test"}, filtered); } } \ No newline at end of file diff --git a/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs b/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs index 480a18e35..5aaa2072b 100644 --- a/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs +++ b/TagsCloudCoreTests/WordProcessing/WordGrouping/DefaultWordProcessorTests.cs @@ -1,10 +1,9 @@ using NSubstitute; using TagsCloudCore.BuildingOptions; -using TagsCloudCore.Drawing.Colorers; +using TagsCloudCore.Common.Enums; using TagsCloudCore.WordProcessing.WordFiltering; using TagsCloudCore.WordProcessing.WordGrouping; using TagsCloudCore.WordProcessing.WordInput; -using TagsCloudVisualization; namespace TagsCloudCoreTests.WordProcessing.WordGrouping; @@ -14,23 +13,25 @@ public class DefaultWordProcessorTests public void ProcessedWords_ReturnsCorrectlyProcessedWords_OnCorrectInputData() { var options = Substitute.For(); - var words = Substitute.For(); - options.CommonOptions.Returns(new CommonOptions(words, Substitute.For(), - Substitute.For())); - words.Words.Returns(new[] {"1", "2", "4", "4", "1", "1", "5"}); + options.CommonOptions.Returns(new CommonOptions(new WordProviderInfo(WordProviderType.Txt, ""), WordColorerAlgorithm.Default, + CloudBuildingAlgorithm.Circular)); + var source = Substitute.For(); + source.GetWords("").Returns(new[] {"1", "2", "4", "4", "1", "1", "5"}); + source.Match(WordProviderType.Txt).Returns(true); var filter1 = GetWordFilter(new[] {"2"}); var filter2 = GetWordFilter(new[] {"5"}); - var processor = new DefaultWordProcessor(options, new[] {filter1, filter2}); + var processor = new DefaultWordProcessor(options, new[] {filter1, filter2}, + new [] {source}); var result = processor.ProcessedWords; - + CollectionAssert.AreEqual(new Dictionary {{"1", 3}, {"4", 2}}, result); } - + private static DefaultWordFilter GetWordFilter(string[] wordsToExclude) { var wordProvider = Substitute.For(); - wordProvider.Words.Returns(wordsToExclude); - return new DefaultWordFilter(wordProvider); + wordProvider.GetWords("").Returns(wordsToExclude); + return new DefaultWordFilter(wordProvider, ""); } } \ No newline at end of file