SheetPlayer/SheetPlayer/Form1.cs

160 lines
6.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace SheetPlayer {
public partial class mainForm : Form {
public mainForm() {
InitializeComponent();
}
bool isBaked = false;
private void cmdBakeForm_Click(object sender, EventArgs e) {
Point prevPosition;
if (isBaked) {
prevPosition = cmdBakeForm.PointToScreen(Point.Empty);
FormBorderStyle = FormBorderStyle.Sizable;
}
else {
prevPosition = cmdBakeForm.PointToScreen(Point.Empty);
FormBorderStyle = FormBorderStyle.None;
}
Point afterPosition = cmdBakeForm.PointToScreen(Point.Empty);
Location = new Point(Location.X - (afterPosition.X - prevPosition.X),
Location.Y - (afterPosition.Y - prevPosition.Y));
isBaked = !isBaked;
TopMost = isBaked;
}
static public List<animationObject> aoPool = new List<animationObject>();
static public int globalFrameCounter = 0;
private void mainForm_DragEnter(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
private void mainForm_DragDrop(object sender, DragEventArgs e) {
foreach (string entry in (string[])e.Data.GetData(DataFormats.FileDrop, false)) {
aoPool.Add(new animationObject(entry, this.PointToClient(new Point(e.X - 24, e.Y - 24))));
}
}
int grabbedObject = -1;
bool ctrl = false;
bool dragging = false;
Point startMousePosition = Point.Empty;
private void mainForm_MouseDown(object sender, MouseEventArgs e) {
if (!isBaked) {
switch (e.Button) {
case MouseButtons.Left:
grabbedObject = getUnderlyingObject();
if (grabbedObject >= 0) {
dragging = true;
startMousePosition = MousePosition;
}
break;
}
}
}
private void mainForm_MouseMove(object sender, MouseEventArgs e) {
if (grabbedObject >= 0) Invalidate();
}
private void mainForm_MouseUp(object sender, MouseEventArgs e) {
if (!isBaked) {
switch (e.Button) {
case MouseButtons.Left:
if (dragging) {
if (ctrl) {
aoPool.Add((animationObject)aoPool[grabbedObject].Clone());
grabbedObject = aoPool.Count - 1;
}
aoPool[grabbedObject].position.X += MousePosition.X - startMousePosition.X;
aoPool[grabbedObject].position.Y += MousePosition.Y - startMousePosition.Y;
dragging = false;
grabbedObject = -1;
}
break;
case MouseButtons.Right:
int objID = getUnderlyingObject();
if (dragging) {
grabbedObject = -1;
dragging = false;
}
else {
if (objID > -1) {
if (ctrl) {
aoPool.RemoveAt(objID);
}
else {
aoMenu ao = new aoMenu(objID);
ao.Show();
ao.Location = new Point(MousePosition.X, MousePosition.Y - 76);
}
}
}
break;
}
}
}
private void mainForm_KeyDown(object sender, KeyEventArgs e) { if (e.Modifiers == Keys.Control) ctrl = true; }
private void mainForm_KeyUp(object sender, KeyEventArgs e) { if (e.Modifiers != Keys.Control) ctrl = false; }
private int getUnderlyingObject() {
int nearestObject = -1;
for (int i = aoPool.Count - 1; i >= 0; i--) {
animationObject obj = aoPool[i];
if (this.PointToClient(MousePosition).X > obj.position.X &&
this.PointToClient(MousePosition).Y > obj.position.Y &&
this.PointToClient(MousePosition).X < obj.position.X + obj.currentFrame.Width * obj.size &&
this.PointToClient(MousePosition).Y < obj.position.Y + obj.currentFrame.Height * obj.size) {
if (nearestObject == -1) nearestObject = i;
}
}
return nearestObject;
}
private void drawCycle_Tick(object sender, EventArgs e) {
foreach (animationObject obj in aoPool) {
obj.checkForModification();
}
Invalidate();
globalFrameCounter++;
}
private void mainForm_Paint(object sender, PaintEventArgs e) {
Graphics g = e.Graphics;
g.InterpolationMode = InterpolationMode.NearestNeighbor;
for (int i = aoPool.Count - 1; i >= 0; i--) {
animationObject obj = aoPool[i];
List<Point> drawLocations = new List<Point>();
if (i == grabbedObject) {
if (ctrl) drawLocations.Add(obj.position);
drawLocations.Add(new Point(obj.position.X + MousePosition.X - startMousePosition.X,
obj.position.Y + MousePosition.Y - startMousePosition.Y));
}
else {
drawLocations.Add(obj.position);
}
foreach (Point pos in drawLocations) {
g.SetClip(new Rectangle(pos,
new Size(obj.currentFrame.Size.Width * obj.size,
obj.currentFrame.Size.Height * obj.size)),
CombineMode.Replace);
g.DrawImage(obj.spriteSheet, pos.X - obj.currentFrame.X * obj.size,
pos.Y - obj.currentFrame.Y * obj.size,
obj.spriteSheet.Size.Width * obj.size, obj.spriteSheet.Size.Height * obj.size);
}
}
}
}
}