sledgemapper/Sledgemapper/ExtensionMethods.cs
Michele Scandura 34863a9984
All checks were successful
continuous-integration/drone/push Build is passing
more refactoring
2021-09-09 10:53:32 +01:00

67 lines
2.2 KiB
C#

using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using System.Linq;
using Myra.Graphics2D.UI;
using Myra.Graphics2D.Brushes;
namespace Sledgemapper
{
public static class ExtensionMethods
{
public static Dictionary<string, T> LoadContentFolder<T>(this ContentManager contentManager, string contentFolder)
{
DirectoryInfo dir = new(contentManager.RootDirectory + "/" + contentFolder);
if (!dir.Exists)
throw new DirectoryNotFoundException();
Dictionary<string, T> result = new();
FileInfo[] files = dir.GetFiles("*.*");
foreach (FileInfo file in files.Where(f => f.Extension != ".ttf" && f.Extension != ".otf"))
{
result.Add(file.Name.Split('.')[0], contentManager.Load<T>(contentFolder + "/" + file.Name.Split('.')[0]));
}
return result;
}
public static IEnumerable<string> Split(this string str, int chunkSize) => Enumerable.Range(0, str.Length / chunkSize)
.Select(i => str.Substring(i * chunkSize, chunkSize));
public static Point AbsPoint(this Point point)
{
return new Point(System.Math.Abs(point.X), System.Math.Abs(point.Y));
}
public static (Window Window, C Content) GetParentContentInWindow<C>(this Widget widget) where C : Widget
{
Container container = widget.Parent;
while (!(container is Window))
{
container = container.Parent;
}
var localWindow = (Window)container;
var localContent = localWindow.Content as C;
return (localWindow, localContent);
}
public static bool ValidateTextbox(this TextBox textBox)
{
var valid = !string.IsNullOrWhiteSpace(textBox.Text);
if (!valid)
{
textBox.Background = new SolidBrush(Color.Red);
}
else
{
textBox.Background = new SolidBrush(new Color(51, 51, 51)); ;
}
return valid;
}
}
}