96 lines
3.5 KiB
C#
96 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
|
|
namespace SheetPlayer {
|
|
public class animationObject : ICloneable {
|
|
//srcData
|
|
private string srcPath;
|
|
private long lastModificTime = 0;
|
|
public string metaData = "48f1";
|
|
public Point position;
|
|
public int size = 1;
|
|
|
|
//precalculatedData
|
|
public Bitmap spriteSheet;
|
|
private List<Rectangle> animationFrames = new List<Rectangle>();
|
|
|
|
public Rectangle currentFrame {
|
|
get {
|
|
if (animationFrames.Count != 0)
|
|
return animationFrames[mainForm.globalFrameCounter % animationFrames.Count];
|
|
else
|
|
return new Rectangle(0, 0, 48, 48);
|
|
}
|
|
}
|
|
|
|
public animationObject(string draggedImage, Point dropPosition) {
|
|
srcPath = draggedImage;
|
|
checkForModification();
|
|
updateMetaData();
|
|
position = dropPosition;
|
|
}
|
|
|
|
public object Clone() {
|
|
return this.MemberwiseClone();
|
|
}
|
|
|
|
public void checkForModification() {
|
|
try {
|
|
long currentModificTime = File.GetLastWriteTime(srcPath).Ticks;
|
|
if (lastModificTime != currentModificTime) {
|
|
var binary = File.ReadAllBytes(srcPath);
|
|
var memoryStream = new MemoryStream(binary);
|
|
spriteSheet = (Bitmap)Image.FromStream(memoryStream);
|
|
lastModificTime = currentModificTime;
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
public bool updateMetaData() {
|
|
try {
|
|
animationFrames.Clear();
|
|
string[] headerAndBody = metaData.Split('f');
|
|
string[] header = headerAndBody[0].Split('x');
|
|
int width = Convert.ToInt32(header[0]);
|
|
int height = header.Length > 1 ? Convert.ToInt32(header[1]) : Convert.ToInt32(header[0]);
|
|
int hFrames = spriteSheet.Width / width;
|
|
int vFrames = spriteSheet.Height / height;
|
|
string[] frameList = headerAndBody[1].Split('.');
|
|
foreach (string frame in frameList) {
|
|
string[] range = frame.Split(':');
|
|
if (range.Length == 1) {
|
|
int frameNumber = Convert.ToInt32(frame);
|
|
animationFrames.Add(new Rectangle(
|
|
frameNumber % hFrames * width,
|
|
frameNumber / hFrames * height,
|
|
width, height));
|
|
}
|
|
else {
|
|
int rangeBegin = Convert.ToInt32(range[0]);
|
|
int rangeEnd = Convert.ToInt32(range[1]);
|
|
for (int frameNumber = rangeBegin;
|
|
frameNumber <= Math.Max(rangeBegin, rangeEnd) &&
|
|
frameNumber >= Math.Min(rangeBegin, rangeEnd);
|
|
frameNumber += rangeEnd > rangeBegin ? 1 : -1) {
|
|
animationFrames.Add(new Rectangle(
|
|
frameNumber % hFrames * width,
|
|
frameNumber / hFrames * height,
|
|
width, height));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|