2019-02-07 02:59:55 +01:00
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 {
2019-04-10 01:11:26 +02:00
if ( animationFrames . Count ! = 0 )
return animationFrames [ mainForm . globalFrameCounter % animationFrames . Count ] ;
else
return new Rectangle ( 0 , 0 , 48 , 48 ) ;
2019-02-07 02:59:55 +01:00
}
}
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 { }
}
2019-04-10 01:11:26 +02:00
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 ;
2019-02-07 02:59:55 +01:00
}
2019-04-10 01:11:26 +02:00
return true ;
2019-02-07 02:59:55 +01:00
}
}
}