286 lines
12 KiB
C#
286 lines
12 KiB
C#
using SpriteStacker.Properties;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.Drawing;
|
|
using System.Drawing.Drawing2D;
|
|
using System.Drawing.Text;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace SpriteStacker
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
public static PrivateFontCollection pfc = new PrivateFontCollection();
|
|
public static Thread MainThread;
|
|
public static bool running = true;
|
|
public static bool ValidLocation = true;
|
|
Model model = new Model(Camera.CanvasWidth, Camera.CanvasHeight);
|
|
public double LastFT = 0;
|
|
public bool mouseLeftDown = false;
|
|
public bool mouseRightDown = false;
|
|
public bool Rotate = false;
|
|
public static Rectangle MouseCollider = new Rectangle(0, 0, 100, 100);
|
|
public static ColourPalette palette = new ColourPalette(24,new Color[] {
|
|
Color.Black,
|
|
Color.White,
|
|
Color.Gray,
|
|
Color.DarkGreen,
|
|
Color.Green,
|
|
Color.LightGreen,
|
|
Color.GreenYellow,
|
|
Color.Yellow,
|
|
Color.Orange,
|
|
Color.OrangeRed,
|
|
Color.Red,
|
|
Color.Purple,
|
|
Color.Pink,
|
|
Color.BlueViolet,
|
|
Color.DarkBlue,
|
|
Color.Blue,
|
|
Color.LightBlue,
|
|
Color.Brown,
|
|
Color.RosyBrown,
|
|
Color.SaddleBrown,
|
|
Color.SandyBrown,
|
|
Color.Wheat,
|
|
Color.Beige,
|
|
Color.Azure
|
|
});
|
|
private void initFont()
|
|
{
|
|
int fontLength = Resources.Monocraft.Length;
|
|
byte[] fontdata = Resources.Monocraft;
|
|
System.IntPtr data = Marshal.AllocCoTaskMem(fontLength);
|
|
Marshal.Copy(fontdata, 0, data, fontLength);
|
|
pfc.AddMemoryFont(data, fontLength);
|
|
}
|
|
public Form1()
|
|
{
|
|
InitializeComponent();
|
|
initFont();
|
|
ResizeDisplay();
|
|
MainThread = new Thread(mainThread);
|
|
MainThread.SetApartmentState(ApartmentState.STA);
|
|
MainThread.Start();
|
|
Camera.SelectedColor = palette.getSelectedColour();
|
|
model.SetBitmapLayers(new Bitmap[]
|
|
{
|
|
Resources._1, Resources._2, Resources._3, Resources._4, Resources._5, Resources._6,
|
|
Resources._7, Resources._8, Resources._9, Resources._10, Resources._11, Resources._12,
|
|
Resources._13,Resources._14, Resources._15, Resources._16, Resources._17, Resources._18,
|
|
Resources._19, Resources._20, Resources._21, Resources._22, Resources._23, Resources._24,
|
|
Resources._25,Resources._26
|
|
});
|
|
}
|
|
public void mainThread()
|
|
{
|
|
LastFT = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
|
|
double SecondCounter = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
|
|
while (running)
|
|
{
|
|
Display.Refresh();
|
|
if (Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds - SecondCounter > 1000)
|
|
{
|
|
SecondCounter = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
|
|
//model.DrawLayersToImages();
|
|
}
|
|
if (Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds - LastFT > 16)
|
|
{
|
|
LastFT = Process.GetCurrentProcess().TotalProcessorTime.TotalMilliseconds;
|
|
if (Camera.Rotation == 360 || !Rotate && Camera.Rotation != 0) Camera.Rotation = 0;
|
|
if(Rotate)Camera.Rotation++;
|
|
if (mouseLeftDown)
|
|
{
|
|
if(ValidLocation) DrawBrush(new Voxel(Camera.SelectedColor));
|
|
|
|
}
|
|
if (mouseRightDown)
|
|
{
|
|
if (ValidLocation) DrawBrush(new Voxel(Color.Transparent));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
private void PaintDisplay(object sender, PaintEventArgs e)
|
|
{
|
|
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
|
|
switch (Config.renderMode)
|
|
{
|
|
case RenderMode.Editing:
|
|
model.DrawSpriteStack(e.Graphics);
|
|
break;
|
|
case RenderMode.Rendering:
|
|
model.RenderModel(e.Graphics,Config.RenderResolution);
|
|
break;
|
|
|
|
}
|
|
palette.Draw(e.Graphics);
|
|
e.Graphics.ResetTransform();
|
|
e.Graphics.DrawString(String.Format("Selected Brush: {0}\nLayer: {1}/{2}\nBrush Size: {3}\nBrush Intensity: {4}\nRender Type: {5}\nRender Resolution: {6}", Camera.type, Camera.SelectedLayer + 1, model.LayerImages.Count, Camera.BrushSize, Camera.SprayIntensity,Config.renderMode,Config.renderMode == RenderMode.Editing ? "N/A": Config.RenderResolution.ToString()), new Font(pfc.Families[0], Config.FontSize), Brushes.DarkSlateBlue, new Point(0, palette.Bounds.Bottom));
|
|
}
|
|
|
|
private void ResizeDisplay()
|
|
{
|
|
Display.Left = 0;
|
|
Display.Top = 0;
|
|
Display.Width = Width;
|
|
Display.Height = Height - 32;
|
|
Camera.Width = Width; Camera.Height = Height - 32;
|
|
palette.CalculateSize();
|
|
}
|
|
private void ResizeWindow(object sender, EventArgs e)
|
|
{
|
|
ResizeDisplay();
|
|
}
|
|
|
|
private void KillThreads(object sender, FormClosingEventArgs e)
|
|
{
|
|
running = false;
|
|
}
|
|
|
|
private void FrameTimer(object sender, EventArgs e)
|
|
{
|
|
|
|
}
|
|
|
|
private void KeyPressEvent(object sender, KeyPressEventArgs e)
|
|
{
|
|
|
|
}
|
|
private void Reset()
|
|
{
|
|
model = new Model(Camera.CanvasWidth, Camera.CanvasHeight);
|
|
Camera.SelectedLayer = 0;
|
|
}
|
|
private void KeyDownEvent(object sender, KeyEventArgs e)
|
|
{
|
|
BrushType[] types = new BrushType[] { BrushType.Square, BrushType.Circle,BrushType.Spray };
|
|
RenderMode[] RenderModes = new RenderMode[] {RenderMode.Editing, RenderMode.Rendering };
|
|
if (e.KeyCode == Keys.W) Camera.BrushY = Math.Min(Camera.BrushY + 1,model.Length);
|
|
if (e.KeyCode == Keys.S) Camera.BrushY = Math.Max(Camera.BrushY -1, 0);
|
|
if (e.KeyCode == Keys.A) Camera.BrushX = Math.Min(Camera.BrushX + 1, model.Width);
|
|
if (e.KeyCode == Keys.D) Camera.BrushX = Math.Max(Camera.BrushX - 1, 0);
|
|
if (e.KeyCode == Keys.Q) Camera.scale++;
|
|
if (e.KeyCode == Keys.E) Camera.scale--;
|
|
if (e.KeyCode == Keys.Right) Camera.BrushSize++;
|
|
if (e.KeyCode == Keys.Left) Camera.BrushSize--;
|
|
if (e.KeyCode == Keys.Y) Camera.ViewAngle+=0.25f;
|
|
if (e.KeyCode == Keys.H) Camera.ViewAngle -= 0.25f;
|
|
if (e.KeyCode == Keys.D1) Config.BackgroundTile += 2;
|
|
if (e.KeyCode == Keys.D2) Config.BackgroundTile -= 2;
|
|
if (e.KeyCode == Keys.D3) Config.RenderResolution += 1;
|
|
if (e.KeyCode == Keys.D4) Config.RenderResolution -= 1;
|
|
if (e.KeyCode == Keys.R) Rotate = !Rotate;
|
|
if (e.KeyCode == Keys.ShiftKey) Camera.EditMode = !Camera.EditMode;
|
|
if (e.KeyCode == Keys.ControlKey)
|
|
{
|
|
Config.RenderIndex++;
|
|
if (Config.RenderIndex >= RenderModes.Length) Config.RenderIndex = 0;
|
|
Config.renderMode = RenderModes[Config.RenderIndex];
|
|
}
|
|
if (e.KeyCode == Keys.Up) model.SetCurrentLayer(Camera.SelectedLayer + 1);
|
|
if (e.KeyCode == Keys.Down) model.SetCurrentLayer(Math.Max(Camera.SelectedLayer - 1,0));
|
|
if (e.KeyCode == Keys.Delete) Reset();
|
|
if (e.KeyCode == Keys.Add)
|
|
{
|
|
Camera.CircleIntensity += 0.25;
|
|
Camera.SprayIntensity++;
|
|
}
|
|
if (e.KeyCode == Keys.Subtract) {
|
|
Camera.CircleIntensity -= 0.25;
|
|
Camera.SprayIntensity--;
|
|
}
|
|
if (e.KeyCode == Keys.U)
|
|
{
|
|
Camera.BrushIndex = Camera.BrushIndex+1 >= types.Length ? Camera.BrushIndex = 0: Camera.BrushIndex+1;
|
|
Camera.type = types[Camera.BrushIndex];
|
|
}
|
|
if (e.KeyCode == Keys.Enter)
|
|
{
|
|
DrawBrush(new Voxel(Camera.SelectedColor));
|
|
}
|
|
if (e.KeyCode == Keys.Back)
|
|
{
|
|
DrawBrush(new Voxel(Color.Transparent));
|
|
}
|
|
}
|
|
private void DrawBrush(Voxel voxel)
|
|
{
|
|
Random rnd = new Random();
|
|
for (int i = 0; i < Camera.BrushSize; i++)
|
|
{
|
|
for (int j = 0; j < Camera.BrushSize; j++)
|
|
{
|
|
int locx = (Camera.BrushX + i - Camera.BrushSize / 2);
|
|
int locy = (Camera.BrushY + j - Camera.BrushSize / 2);
|
|
if (locx < -4 || locx > model.Width + 4 || locy < -4 || locy > model.Length + 4) ValidLocation = false;
|
|
else ValidLocation = true;
|
|
locx = Math.Min(Math.Max(locx, 0), model.Width - 1);
|
|
locy = Math.Min(Math.Max(locy, 0), model.Length - 1);
|
|
bool paintPixel = false;
|
|
if (Camera.type == BrushType.Square) paintPixel = true;
|
|
if (Camera.type == BrushType.Circle && Camera.isWithinCircle(i, j, (int)Math.Floor(Camera.BrushSize / 2.0), (int)(Camera.BrushSize / 2.0), (int)(Camera.BrushSize / 2.0)))
|
|
{
|
|
paintPixel = true;
|
|
}
|
|
if(Camera.type == BrushType.Spray)
|
|
{
|
|
paintPixel = rnd.Next(0, Camera.SprayIntensity + 1) == 1;
|
|
}
|
|
if(paintPixel) model.SetData(Math.Min(locx, model.Width - 1), Math.Min(locy, model.Length - 1), Camera.SelectedLayer, voxel);
|
|
}
|
|
}
|
|
model.DrawSelectedLayerToImage();
|
|
}
|
|
|
|
private void MouseMoveEvent(object sender, MouseEventArgs e)
|
|
{
|
|
MouseCollider.Location = new Point(e.X - 50, e.Y - 50);
|
|
int PosX = Math.Max(Math.Min((int)Math.Floor((e.X - Width/2 - model.Bounds[Camera.SelectedLayer].Left)/Camera.scale + 0.0),model.Width - 1),0);
|
|
int PosY = Math.Max(Math.Min((int)Math.Floor((e.Y - Height/2 - model.Bounds[Camera.SelectedLayer].Top) / Camera.scale + 0.0),model.Length - 1) - 3, 0) ;
|
|
Camera.BrushX = PosX;
|
|
Camera.BrushY = PosY;
|
|
palette.SetHover(e.Location);
|
|
}
|
|
|
|
private void MouseDownEvent(object sender, MouseEventArgs e)
|
|
{
|
|
if (e.Button == MouseButtons.Left)
|
|
{
|
|
mouseLeftDown = true;
|
|
palette.SelectColour();
|
|
}
|
|
if (e.Button == MouseButtons.Right)
|
|
{
|
|
mouseRightDown = true;
|
|
|
|
}
|
|
}
|
|
|
|
private void MouseUpEvent(object sender, MouseEventArgs e)
|
|
{
|
|
if (e.Button == MouseButtons.Left)
|
|
{
|
|
mouseLeftDown = false;
|
|
model.DrawLayersToImages();
|
|
|
|
}
|
|
if (e.Button == MouseButtons.Right)
|
|
{
|
|
mouseRightDown = false;
|
|
model.DrawLayersToImages();
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|