rework SymbolFactory
- rename & move to Pilz.UI..Telerik.Symbols.RadSymbolFactory - make default implementaiton available at Pilz.UI.Symbols.SymbolFactory - add interfaces
This commit is contained in:
12
Pilz.UI/Symbols/ISymbolFactory.cs
Normal file
12
Pilz.UI/Symbols/ISymbolFactory.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace Pilz.UI.Symbols;
|
||||
|
||||
public interface ISymbolFactory<TSymbols>
|
||||
{
|
||||
Image? GetImage(TSymbols svgImage, Size size);
|
||||
Image? GetImage(TSymbols svgImage, SymbolSize size);
|
||||
Assembly GetImageResourceAssembly();
|
||||
string GetImageRessourcePath(TSymbols svgImage);
|
||||
Stream? GetImageRessourceStream(TSymbols svgImage);
|
||||
}
|
||||
53
Pilz.UI/Symbols/SymbolFactory.cs
Normal file
53
Pilz.UI/Symbols/SymbolFactory.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace Pilz.UI.Symbols;
|
||||
|
||||
public abstract class SymbolFactory<TSymbols> : ISymbolFactory<TSymbols>
|
||||
{
|
||||
public abstract string GetImageRessourcePath(TSymbols svgImage);
|
||||
public abstract Assembly GetImageResourceAssembly();
|
||||
|
||||
protected virtual Size ResolveCommonSize(SymbolSize size)
|
||||
{
|
||||
return size switch
|
||||
{
|
||||
SymbolSize.Small => new Size(16, 16),
|
||||
SymbolSize.Medium => new Size(20, 20),
|
||||
SymbolSize.Large => new Size(32, 32),
|
||||
_ => Size.Empty,
|
||||
};
|
||||
}
|
||||
|
||||
public virtual Stream? GetImageRessourceStream(TSymbols svgImage)
|
||||
{
|
||||
var asm = GetImageResourceAssembly();
|
||||
var path = GetImageRessourcePath(svgImage);
|
||||
return asm.GetManifestResourceStream(path);
|
||||
}
|
||||
|
||||
public virtual Image? GetImage(TSymbols svgImage, SymbolSize size)
|
||||
{
|
||||
return GetImage(svgImage, ResolveCommonSize(size));
|
||||
}
|
||||
|
||||
public virtual Image? GetImage(TSymbols svgImage, Size size)
|
||||
{
|
||||
using var stream = GetImageRessourceStream(svgImage);
|
||||
|
||||
if (stream is null)
|
||||
return null;
|
||||
|
||||
var img = Image.FromStream(stream);
|
||||
|
||||
if (!size.IsEmpty)
|
||||
{
|
||||
var img2 = new Bitmap(size.Width, size.Height);
|
||||
using var g = Graphics.FromImage(img2);
|
||||
g.DrawImage(img2, 0, 0, size.Width, size.Height);
|
||||
img.Dispose();
|
||||
img = img2;
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
}
|
||||
21
Pilz.UI/Symbols/SymbolSize.cs
Normal file
21
Pilz.UI/Symbols/SymbolSize.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
namespace Pilz.UI.Symbols;
|
||||
|
||||
public enum SymbolSize
|
||||
{
|
||||
/// <summary>
|
||||
/// Don't resize and just returns the original size.
|
||||
/// </summary>
|
||||
Default,
|
||||
/// <summary>
|
||||
/// Resizes the symbol to 16 x 16 pixels.
|
||||
/// </summary>
|
||||
Small,
|
||||
/// <summary>
|
||||
/// Resizes the symbol to 20 x 20 pixels.
|
||||
/// </summary>
|
||||
Medium,
|
||||
/// <summary>
|
||||
/// Resizes the symbol to 32 x 32 pixels.
|
||||
/// </summary>
|
||||
Large
|
||||
}
|
||||
Reference in New Issue
Block a user