Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
2.0 kB
2
Indexable
Never
using SFML.Graphics;
using SFML.Window;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace dave_icon_generator
{
	class Program
	{
		static void Main()
		{
			var window = new RenderWindow(new(160 * 2, 90 * 2), "dave icon generator");
			var rend = new RenderTexture(16, 16);
			var files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory).ToList();
			var textures = new List<Texture>();
			var paths = new List<string>();

			for (int i = 0; i < files.Count; i++)
				if (files[i].EndsWith(".png"))
				{
					textures.Add(new(files[i]));
					paths.Add(Path.GetFileName(files[i]));
				}

			if (textures.Count == 0)
				return;

			var preview = new Sprite(rend.Texture) { Position = new(160 * 2 - (16 * 4), 90 * 2 - (16 * 4)), Scale = new(4, 4) };
			var textureIndex = 0;
			var view = new View(new(), new(16, 16));
			var sprite = new Sprite(textures.Count > 0 ? textures[0] : null);

			window.Closed += Window_Closed;
			window.MouseButtonPressed += Window_MouseButtonPressed;

			while (window.IsOpen)
			{
				window.DispatchEvents();

				window.Clear(new(40, 100, 60));
				rend.Clear(Color.Transparent);

				window.Draw(sprite);

				var p = window.MapPixelToCoords(Mouse.GetPosition(window));
				view.Center = new(p.X, p.Y);
				rend.SetView(view);
				rend.Draw(sprite);
				rend.Display();

				window.Draw(preview);

				window.Display();
			}

			void UpdateTexture()
			{
				textureIndex++;

				var img = rend.Texture.CopyToImage();
				img.SaveToFile($"icon-{paths[Math.Max(0, textureIndex - 1)]}");
				img.Dispose();

				if (textureIndex == textures.Count)
				{
					window.Close();
					return;
				}

				sprite.Texture = textures[textureIndex];
				sprite.TextureRect = new IntRect(new(), new((int)sprite.Texture.Size.X, (int)sprite.Texture.Size.Y));
			}
			void Window_Closed(object sender, EventArgs e)
			{
				window.Close();
			}
			void Window_MouseButtonPressed(object sender, MouseButtonEventArgs e)
			{
				UpdateTexture();
			}
		}
	}
}