2011-06-26 14:36:41 +00:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Drawing ;
2011-07-05 23:16:54 +00:00
using System.IO ;
2011-06-26 14:36:41 +00:00
namespace BizHawk.MultiClient
{
2011-06-26 16:39:48 +00:00
public class SubtitleList
2011-06-26 14:36:41 +00:00
{
private List < Subtitle > subs = new List < Subtitle > ( ) ;
public SubtitleList ( )
{
}
2011-06-26 21:11:12 +00:00
public SubtitleList ( Movie m )
{
if ( m . Subtitles . Count ( ) = = 0 ) return ;
for ( int x = 0 ; x < m . Subtitles . Count ( ) ; x + + )
{
Subtitle s = new Subtitle ( m . Subtitles . GetSubtitleByIndex ( x ) ) ;
subs . Add ( s ) ;
}
}
public Subtitle GetSubtitleByIndex ( int index )
{
if ( index > = subs . Count | | index < 0 ) return new Subtitle ( ) ;
return subs [ index ] ;
}
2011-07-04 00:49:37 +00:00
public string GetSubtitleText ( int index )
{
if ( index > = subs . Count | | index < 0 ) return "" ;
StringBuilder sb = new StringBuilder ( "subtitle " ) ;
sb . Append ( subs [ index ] . Frame . ToString ( ) ) ;
sb . Append ( " " ) ;
sb . Append ( subs [ index ] . X . ToString ( ) ) ;
sb . Append ( " " ) ;
sb . Append ( subs [ index ] . Y . ToString ( ) ) ;
sb . Append ( " " ) ;
sb . Append ( subs [ index ] . Duration . ToString ( ) ) ;
sb . Append ( " " ) ;
sb . Append ( subs [ index ] . Color . ToString ( ) ) ;
sb . Append ( " " ) ;
sb . Append ( subs [ index ] . Message ) ;
return sb . ToString ( ) ;
}
2011-06-26 14:36:41 +00:00
/// <summary>
/// Manages the logic of what subtitle should be displayed on any given frame based on frame & duration
/// </summary>
/// <param name="frame"></param>
/// <returns></returns>
2011-07-04 18:04:56 +00:00
public string GetSubtitleMessage ( int frame )
2011-06-26 14:36:41 +00:00
{
if ( subs . Count = = 0 ) return "" ;
for ( int x = 0 ; x < subs . Count ; x + + )
{
2011-06-26 16:39:48 +00:00
if ( frame > = subs [ x ] . Frame & & frame < = subs [ x ] . Frame + subs [ x ] . Duration )
2011-06-26 14:36:41 +00:00
return subs [ x ] . Message ;
}
return "" ;
}
2011-07-04 18:04:56 +00:00
public Subtitle GetSubtitle ( int frame )
{
if ( subs . Count = = 0 ) return new Subtitle ( ) ;
for ( int x = 0 ; x < subs . Count ; x + + )
{
if ( frame > = subs [ x ] . Frame & & frame < = subs [ x ] . Frame + subs [ x ] . Duration )
return subs [ x ] ;
}
return new Subtitle ( ) ;
}
2011-06-26 21:11:12 +00:00
public int Count ( )
{
return subs . Count ;
}
2011-06-26 16:39:48 +00:00
//TODO
2011-06-26 14:36:41 +00:00
public Point GetSubtitlePoint ( int frame )
{
Point p = new Point ( 0 , 0 ) ;
return p ;
}
/// <summary>
/// Attempts to parse string for necessary subtitle information, required is a frame and a message, space delminated, the word subtitle assumed to be first
/// </summary>
/// <param name="subtitleStr"></param>
/// <returns></returns>
public bool AddSubtitle ( string subtitleStr )
{
if ( subtitleStr . Length = = 0 ) return false ;
Subtitle s = new Subtitle ( ) ;
int x = subtitleStr . IndexOf ( ' ' ) ;
if ( x < = 0 ) return false ;
2011-07-04 17:01:17 +00:00
//remove "subtitle"
2011-06-26 16:39:48 +00:00
string str = subtitleStr . Substring ( x + 1 , subtitleStr . Length - x - 1 ) ;
2011-07-04 17:01:17 +00:00
x = str . IndexOf ( ' ' ) ;
if ( x < = 0 ) return false ;
string frame = str . Substring ( 0 , x ) ;
str = str . Substring ( x + 1 , str . Length - x - 1 ) ;
try
{
s . Frame = int . Parse ( frame ) ;
}
catch
{
return false ;
}
x = str . IndexOf ( ' ' ) ;
if ( x < = 0 ) return false ;
string X = str . Substring ( 0 , x ) ;
str = str . Substring ( x + 1 , str . Length - x - 1 ) ;
try
{
s . X = int . Parse ( X ) ;
}
catch
{
s . Message = str ; //Assume it is a FCEUX subtitle
subs . Add ( s ) ;
return true ;
}
x = str . IndexOf ( ' ' ) ;
if ( x < = 0 ) return false ;
string Y = str . Substring ( 0 , x ) ;
str = str . Substring ( x + 1 , str . Length - x - 1 ) ;
2011-06-26 14:36:41 +00:00
try
{
2011-07-04 17:01:17 +00:00
s . Y = int . Parse ( Y ) ;
2011-06-26 14:36:41 +00:00
}
catch
{
return false ;
}
2011-07-04 18:04:56 +00:00
x = str . IndexOf ( ' ' ) ;
if ( x < = 0 ) return false ;
string Duration = str . Substring ( 0 , x ) ;
str = str . Substring ( x + 1 , str . Length - x - 1 ) ;
try
{
s . Duration = int . Parse ( Duration ) ;
}
catch
{
return false ;
}
//TODO: parse hex!
x = str . IndexOf ( ' ' ) ;
if ( x < = 0 ) return false ;
string Color = str . Substring ( 0 , x ) ;
str = str . Substring ( x + 1 , str . Length - x - 1 ) ;
try
{
s . Color = uint . Parse ( Color ) ;
}
catch
{
return false ;
}
2011-06-26 16:39:48 +00:00
s . Message = str ;
subs . Add ( s ) ;
2011-06-26 14:36:41 +00:00
return true ;
}
2011-07-04 01:23:32 +00:00
public void AddSubtitle ( Subtitle s )
{
subs . Add ( s ) ;
}
2011-07-04 13:37:51 +00:00
public void ClearSubtitles ( )
{
subs . Clear ( ) ;
}
2011-07-04 19:20:11 +00:00
public void Remove ( int index )
{
if ( index > = subs . Count ) return ;
subs . RemoveAt ( index ) ;
}
2011-07-05 23:16:54 +00:00
public void WriteText ( StreamWriter sw )
{
int length = subs . Count ;
for ( int x = 0 ; x < length ; x + + )
{
sw . WriteLine ( GetSubtitleText ( x ) ) ;
}
}
2011-06-26 14:36:41 +00:00
}
}