Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Бабинцев Григорий #204

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions TagCloud/CloudDrawer/CloudDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Drawing;
using TagCloud.Config;
using TagCloud.Models;

namespace TagCloud.CloudDrawer
{
public class CloudDrawer : ICloudDrawer
{
private readonly AppConfig appConfig;

public CloudDrawer(AppConfig appConfig)
{
this.appConfig = appConfig;
}

public Result<Bitmap> DrawWords(IEnumerable<WordTag> words)
{
var imageConfig = appConfig.ImageConfig;

var bitmap = new Bitmap(imageConfig.Width, imageConfig.Height);

var graphics = Graphics.FromImage(bitmap);

graphics.Clear(imageConfig.Background);

foreach (var word in words)
{
var colorResult = appConfig.GetNextColor();
if (!colorResult.IsSuccess)
return Result.Fail<Bitmap>(colorResult.Error);

var brush = new SolidBrush(colorResult.Value);
graphics.DrawString(word.Content, word.Font, brush, word.Rectangle);
}

return bitmap;
}
}
}
10 changes: 10 additions & 0 deletions TagCloud/CloudDrawer/ICloudDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Drawing;
using TagCloud.Models;

namespace TagCloud.CloudDrawer
{
public interface ICloudDrawer
{
Result<Bitmap> DrawWords(IEnumerable<WordTag> words);
}
}
51 changes: 51 additions & 0 deletions TagCloud/CloudLayouter/CircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Drawing;
using TagCloud.Config;
using TagCloud.Models;

namespace TagCloud.CloudLayouter
{
public class CircularCloudLayouter : ICloudLayouter
{
private AppConfig appConfig;

public CircularCloudLayouter(AppConfig appConfig)
{
this.appConfig = appConfig;
}

public Result<RectangleF> GetPossibleNextRectangle(IEnumerable<RectangleF> cloudRectangles, SizeF rectangleSize)
{
if (rectangleSize.Width <= 0 || rectangleSize.Height <= 0)
return Result.Fail<RectangleF>("the width and height of the rectangle must be positive numbers");

return Result.Of(() =>FindPossibleNextRectangle(cloudRectangles, rectangleSize));
}

private RectangleF FindPossibleNextRectangle(IEnumerable<RectangleF> cloudRectangles, SizeF rectangleSize)
{
var cloudLayouterConfig = appConfig.CloudLayouterConfig;
var imageConfig = appConfig.ImageConfig;

var center = new Point(imageConfig.Width / 2, imageConfig.Height / 2);

var radius = cloudLayouterConfig.Radius;
var angle = cloudLayouterConfig.Angle;

while (true)
{
var point = new Point(
(int)(center.X + radius * Math.Cos(angle)),
(int)(center.Y + radius * Math.Sin(angle))
);

var possibleRectangle = new RectangleF(point, rectangleSize);

if (!cloudRectangles.Any(rectangle => rectangle.IntersectsWith(possibleRectangle)))
return possibleRectangle;

angle += cloudLayouterConfig.DeltaAngle;
radius += cloudLayouterConfig.DeltaRadius;
}
}
}
}
10 changes: 10 additions & 0 deletions TagCloud/CloudLayouter/ICloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Drawing;
using TagCloud.Models;

namespace TagCloud.CloudLayouter
{
public interface ICloudLayouter
{
Result<RectangleF> GetPossibleNextRectangle(IEnumerable<RectangleF> cloudRectangles, SizeF rectangleSize);
}
}
61 changes: 61 additions & 0 deletions TagCloud/Config/AppConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System.Drawing;
using TagCloud.Models;

namespace TagCloud.Config
{
public class AppConfig
{
public CloudLayouterConfig CloudLayouterConfig { get; set; }

public FontConfig FontConfig { get; set; }

public ImageConfig ImageConfig { get;set; }

private Dictionary<ColorScheme, Func<Result<Color>>> ColorSchemas;

private Random rnd;

private int index;

public AppConfig()
{
CloudLayouterConfig = DefaultConfigs.DefaultCloudLayouterConfig;

FontConfig = DefaultConfigs.DefaultFontConfig;

ImageConfig = DefaultConfigs.DefaultImageConfig;

rnd = new Random();
index = -1;

ColorSchemas = new Dictionary<ColorScheme, Func<Result<Color>>>
{
{ColorScheme.Random, ColorSchemeRandom},
{ColorScheme.RoundRobin, ColorSchemeRoundRobin},
};
}

public Result<Color> GetNextColor() => ColorSchemas[ImageConfig.ColorScheme]();

private Result<Color> ColorSchemeRandom()
{
try
{
index = rnd.Next(0, ImageConfig.WordColors.Length);

return ImageConfig.WordColors[index];
}
catch (Exception e)
{
return Result.Fail<Color>($"Не удалось выбрать цвет: {e.Message}");
}
}

private Result<Color> ColorSchemeRoundRobin()
{
index = (index + 1) % ImageConfig.WordColors.Length;

return ImageConfig.WordColors[index];
}
}
}
23 changes: 23 additions & 0 deletions TagCloud/Config/DefaultConfigs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Drawing;
using TagCloud.Models;

namespace TagCloud.Config
{
public static class DefaultConfigs
{
public static ImageConfig DefaultImageConfig
{
get { return new ImageConfig(400, 400, Color.Black, [Color.Red, Color.Blue, Color.Green, Color.White], ColorScheme.RoundRobin); }
}

public static CloudLayouterConfig DefaultCloudLayouterConfig
{
get { return new CloudLayouterConfig(2, 5, 2, 5); }
}

public static FontConfig DefaultFontConfig
{
get { return new FontConfig("GenericSerif", 14, FontStyle.Regular); }
}
}
}
4 changes: 4 additions & 0 deletions TagCloud/Models/CloudLayouterConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace TagCloud.Models
{
public record struct CloudLayouterConfig(int Radius, int DeltaRadius, int Angle, int DeltaAngle);
}
8 changes: 8 additions & 0 deletions TagCloud/Models/ColorScheme.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace TagCloud.Models
{
public enum ColorScheme
{
RoundRobin,
Random
}
}
6 changes: 6 additions & 0 deletions TagCloud/Models/FontConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.Drawing;

namespace TagCloud.Models
{
public record struct FontConfig(string FontFamily, int FontSize, FontStyle Style);
}
6 changes: 6 additions & 0 deletions TagCloud/Models/ImageConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.Drawing;

namespace TagCloud.Models
{
public record struct ImageConfig(int Width, int Height, Color Background, Color[] WordColors, ColorScheme ColorScheme);
}
4 changes: 4 additions & 0 deletions TagCloud/Models/Word.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace TagCloud.Models
{
public record struct Word(string Content, float FontSize);
}
6 changes: 6 additions & 0 deletions TagCloud/Models/WordTag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.Drawing;

namespace TagCloud.Models
{
public record struct WordTag(RectangleF Rectangle, string Content, Font Font);
}
26 changes: 26 additions & 0 deletions TagCloud/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Autofac;
using TagCloud.UI;
using TagCloud.CloudDrawer;
using TagCloud.Config;
using TagCloud.ReadWriter;
using TagCloud.TagCloudService;
using TagCloud.WordsProcessor;
using TagCloud.CloudLayouter;

var builder = new ContainerBuilder();

builder.RegisterType<AppConfig>().AsSelf().SingleInstance();

builder.RegisterType<ConsoleReadWriter>().As<IReadWriter>().SingleInstance();
builder.RegisterType<FileReader>().As<IFileReader>().SingleInstance();
builder.RegisterType<WordProcessor>().As<IWordProcessor>().SingleInstance();
builder.RegisterType<CircularCloudLayouter>().As<ICloudLayouter>().SingleInstance();
builder.RegisterType<TagCloudService>().As<ITagCloudService>().SingleInstance();
builder.RegisterType<CloudDrawer>().As<ICloudDrawer>().SingleInstance();

builder.RegisterType<ConsoleUI>().AsSelf().SingleInstance();

var container = builder.Build();

var ui = container.Resolve<ConsoleUI>();
ui.Start();
31 changes: 31 additions & 0 deletions TagCloud/ReadWriter/ConsoleReadWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace TagCloud.ReadWriter
{
public class ConsoleReadWriter : IReadWriter
{
public TOut ReadLine<TOut>(string beforeInput, Func<string, Result<TOut>> convert)
{
while (true)
{
try
{
WriteLine(beforeInput);

var input = Console.ReadLine().Trim();

var result = convert(input);

if (result.IsSuccess)
return result.Value;

WriteLine(result.Error);
}
catch (Exception ex)
{
WriteLine($"Ошибка: {ex.Message}");
}
}
}

public void WriteLine(string input) => Console.WriteLine(input);
}
}
13 changes: 13 additions & 0 deletions TagCloud/ReadWriter/FileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace TagCloud.ReadWriter
{
public class FileReader : IFileReader
{
public Result<IEnumerable<string>> ReadDataFromFile(string path)
{
//if(!File.Exists(path))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не тащи в Pull Request-ы закомментированный код.
Это мусор, который в лучшем случае затрудняет чтение. В худшем производит вопросы, почему код в репозитории закомментирован и надо ли его раскомментировать

// return Result.Fail<IEnumerable<string>>($"Файл {path} не найден");

return Result.Of(() => File.ReadAllLines(path).Select(line => line.Trim().ToLower())).RefineError($"Ошибка при чтении файла: {path}");
}
}
}
7 changes: 7 additions & 0 deletions TagCloud/ReadWriter/IFileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TagCloud.ReadWriter
{
public interface IFileReader
{
Result<IEnumerable<string>> ReadDataFromFile(string path);
}
}
9 changes: 9 additions & 0 deletions TagCloud/ReadWriter/IReadWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace TagCloud.ReadWriter
{
public interface IReadWriter
{
TOut ReadLine<TOut>(string beforeInputMsg, Func<string, Result<TOut>> convert);

void WriteLine(string msg);
}
}
Loading