sledgemapper/Sledgemapper/ExtensionMethods.cs
Michele Scandura 4179143fdb
All checks were successful
continuous-integration/drone/push Build is passing
even more cleanup
2021-09-16 15:27:03 +01:00

92 lines
2.9 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 Window GetContainingWindow(this Widget widget)
{
var container = widget.Parent;
while (!(container is Window) || container is null)
{
container = container.Parent;
}
return container as Window;
}
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;
}
public static void ShowInModalWindow(this Widget widget, Desktop desktop, string title)
{
Window window = new()
{
Title = title,
Content=widget
};
window.ShowModal(desktop);
}
}
//public interface IExtendedWidget
//{
// void ShowInModalWindow(this Widget widget, string title)
//}
}