Changed how time is dealt with in Athenasub.

Originally committed to SVN as r2368.
This commit is contained in:
Rodrigo Braz Monteiro 2008-09-20 03:04:21 +00:00
parent 1397871e45
commit 2baf7bd2c4
10 changed files with 147 additions and 88 deletions

View File

@ -131,7 +131,6 @@
PrecompiledHeaderThrough="prec.h"
WarningLevel="4"
WarnAsError="true"
Detect64BitPortabilityProblems="false"
DebugInformationFormat="3"
DisableSpecificWarnings="4996"
ForcedIncludeFiles="prec.h"

View File

@ -38,6 +38,8 @@
#include "api.h"
#include "tr1.h"
#include "exception.h"
#include "athenastring.h"
#include "athenatime.h"
#include "interfaces.h"
#include "range.h"

View File

@ -34,27 +34,29 @@
//
#pragma once
#include "athenasub.h"
#include "utils.h"
namespace Athenasub {
// Time class
class CTime : public ITime {
class Time {
private:
int ms;
int ms; // Time in milliseconds
public:
CTime() { ms = 0; }
CTime(int _ms) { ms = _ms; }
Time() { ms = 0; }
Time(int milliseconds) { ms = milliseconds; }
inline void SetMS(int milliseconds) { ms = milliseconds; }
inline int GetMS() const { return ms; }
String GetString(int ms_precision,int h_precision) const;
void ParseString(const String &data);
Time Clone() const { return Time(new CTime(*this)); }
};
// Operators
inline Time operator+ (const Time& p1,int p2) { return Time(p1.GetMS()+p2); }
inline Time operator- (const Time& p1,int p2) { return Time(p1.GetMS()-p2); }
inline bool operator== (const Time& p1,const Time& p2) { return p1.GetMS() == p2.GetMS(); }
inline bool operator!= (const Time& p1,const Time& p2) { return p1.GetMS() != p2.GetMS(); }
inline bool operator<= (const Time& p1,const Time& p2) { return p1.GetMS() <= p2.GetMS(); }
inline bool operator>= (const Time& p1,const Time& p2) { return p1.GetMS() >= p2.GetMS(); }
inline bool operator< (const Time& p1,const Time& p2) { return p1.GetMS() < p2.GetMS(); }
inline bool operator> (const Time& p1,const Time& p2) { return p1.GetMS() > p2.GetMS(); }
}

View File

@ -36,6 +36,7 @@
#pragma once
#include "tr1.h"
#include "athenatime.h"
#include <wx/string.h>
#include <vector>
#include <list>
@ -53,7 +54,6 @@ namespace Athenasub {
class IController;
class IView;
class IFormatHandler;
class ITime;
class IColour;
class IEntry;
class IDialogue;
@ -73,7 +73,6 @@ namespace Athenasub {
typedef shared_ptr<IController> Controller;
typedef shared_ptr<IView> View;
typedef shared_ptr<IFormatHandler> FormatHandler;
typedef shared_ptr<ITime> Time;
typedef shared_ptr<IColour> Colour;
typedef shared_ptr<IEntry> Entry;
typedef shared_ptr<IDialogue> Dialogue;
@ -196,21 +195,6 @@ namespace Athenasub {
};
// Time
class ITime {
public:
virtual ~ITime() {}
virtual void SetMS(int milliseconds) = 0;
virtual int GetMS() const = 0;
virtual String GetString(int ms_precision,int h_precision) const = 0;
virtual void ParseString(const String &data) = 0;
virtual Time Clone() const = 0;
};
// Color
class IColour {
public:
@ -276,8 +260,8 @@ namespace Athenasub {
// Read accessors
virtual const String& GetText() const = 0;
virtual const ITime& GetStartTime() const = 0;
virtual const ITime& GetEndTime() const = 0;
virtual const Time& GetStartTime() const = 0;
virtual const Time& GetEndTime() const = 0;
virtual int GetStartFrame() const = 0;
virtual int GetEndFrame() const = 0;
virtual bool IsComment() const = 0;
@ -289,8 +273,8 @@ namespace Athenasub {
// Write accessors
virtual void SetText(const String& text) = 0;
virtual void SetStartTime(const ITime& start) = 0;
virtual void SetEndTime(const ITime& end) = 0;
virtual void SetStartTime(const Time& start) = 0;
virtual void SetEndTime(const Time& end) = 0;
virtual void SetStartFrame(int start) = 0;
virtual void SetEndFrame(int end) = 0;
virtual void SetComment(bool isComment) = 0;
@ -437,11 +421,4 @@ namespace Athenasub {
virtual Model CreateModel()=0;
};
typedef shared_ptr<ILibAthenaSub> LibAthenaSub;
// Operators
inline Time operator+(const ITime& p1,int p2) { Time res = p1.Clone(); res->SetMS(res->GetMS()+p2); return res; }
inline Time operator-(const ITime& p1,int p2) { Time res = p1.Clone(); res->SetMS(res->GetMS()-p2); return res; }
inline bool operator==(const ITime& p1,const ITime& p2) { return p1.GetMS() == p2.GetMS(); }
}

View File

@ -62,6 +62,96 @@ DialogueASS::DialogueASS(const String &data,int version)
}
//////////////////////////////////
// Generates a string from a time
String GetTimeString(const Time& time,int ms_precision,int h_precision)
{
// Enforce sanity
ms_precision = Mid(0,ms_precision,3);
h_precision = Mid(0,h_precision,2);
// Generate values
int _ms = time.GetMS();
int h = _ms / 3600000;
_ms -= h*3600000;
int min = _ms / 60000;
_ms -= min*60000;
int s = _ms / 1000;
_ms -= s*1000;
// Cap hour value
if (h > 9 && h_precision == 1) {
h = 9;
min = 59;
s = 59;
_ms = 999;
}
// Modify ms to account for precision
if (ms_precision == 2) _ms /= 10;
else if (ms_precision == 1) _ms /= 100;
else if (ms_precision == 0) _ms = 0;
// Get write buffer
String final;
size_t size = 7+h_precision+ms_precision;
size_t pos = 0;
wxChar *buffer = final.GetWriteBuf(size);
wxChar temp[16];
// Write time
WriteNumber(buffer,temp,h,h_precision,pos);
WriteChar(buffer,_T(':'),pos);
WriteNumber(buffer,temp,min,2,pos);
WriteChar(buffer,_T(':'),pos);
WriteNumber(buffer,temp,s,2,pos);
WriteChar(buffer,_T('.'),pos);
WriteNumber(buffer,temp,_ms,ms_precision,pos);
// Write terminator
WriteText(buffer,_T("\0"),1,pos);
// Restore string's state and return
final.UngetWriteBuf(pos-1);
return final;
}
///////////////////////////////
// Parses a string into a time
Time ParseTimeString(const String &data)
{
// Break into an array of values
array<size_t,4> values;
size_t last = 0;
size_t len = data.Length();
size_t curIndex = 0;
wxChar cur = 0;
for (size_t i=0;i<len;i++) {
cur = data[i];
if (cur == ':' || cur == '.' || cur == ',' || cur == ';') {
values.at(curIndex++) = SubStringToInteger(data,last,i);
last = i+1;
}
if (i == len-1) {
int value = SubStringToInteger(data,last,len);
size_t digits = len - last;
if (digits == 2) value *= 10;
if (digits == 1) value *= 100;
values.at(curIndex++) = value;
}
}
// Turn into milliseconds
size_t mult[] = { 0, 1, 1000, 60000, 3600000 };
size_t accum = 0;
for (int i=(int)curIndex;--i>=0;) {
accum += values[i] * mult[curIndex-i];
}
return Time((int)accum);
}
//////////////////
// Parse ASS Data
bool DialogueASS::Parse(wxString rawData, int version)
@ -97,8 +187,8 @@ bool DialogueASS::Parse(wxString rawData, int version)
}
// Get times
time[0].ParseString(tkn.GetString());
time[1].ParseString(tkn.GetString());
time[0] = ParseTimeString(tkn.GetString());
time[1] = ParseTimeString(tkn.GetString());
// Get style and actor
text[1] = tkn.GetString(true);
@ -169,7 +259,7 @@ String DialogueASS::ToText(int version) const
// Write times
for (size_t i=0;i<2;i++) {
wxString tempStr = time[i].GetString(2,1);
wxString tempStr = GetTimeString(time[i],2,1);
WriteText(buffer,&tempStr[0],10,pos);
WriteChar(buffer,_T(','),pos);
}

View File

@ -48,7 +48,7 @@ namespace Athenasub {
private:
array<String,4> text; // 0 = text, 1 = style, 2 = actor, 3 = effect
array<CTime,2> time;
array<Time,2> time;
array<short,4> margin;
int layer;
bool isComment;
@ -73,8 +73,8 @@ namespace Athenasub {
bool HasMargins() const { return true; }
// Read accessors
const ITime& GetStartTime() const { return time[0]; }
const ITime& GetEndTime() const { return time[1]; }
const Time& GetStartTime() const { return time[0]; }
const Time& GetEndTime() const { return time[1]; }
bool IsComment() const { return isComment; }
int GetLayer() const { return layer; }
int GetMargin(int n) const { return margin.at(n); }
@ -84,8 +84,8 @@ namespace Athenasub {
const String& GetUserField() const { return text[3]; }
// Write acessors
void SetStartTime(const ITime &setStart) { time[0].SetMS(setStart.GetMS()); }
void SetEndTime(const ITime &setEnd) { time[1].SetMS(setEnd.GetMS()); }
void SetStartTime(const Time &setStart) { time[0].SetMS(setStart.GetMS()); }
void SetEndTime(const Time &setEnd) { time[1].SetMS(setEnd.GetMS()); }
void SetComment(bool _isComment) { isComment = _isComment; }
void SetLayer(int _layer) { layer = _layer; }
void SetMargin(int _margin,int value) { margin.at(_margin) = value; }

View File

@ -67,8 +67,8 @@ namespace Athenasub {
// Read accessors
virtual const String& GetText() const { ThrowUnsupported(); return EmptyString(); }
// virtual ITime& GetStartTime() const { ThrowUnsupported(); return Time(); }
// virtual ITime& GetEndTime() const { ThrowUnsupported(); return Time(); }
//virtual Time& GetStartTime() const { ThrowUnsupported(); return Time(); }
//virtual Time& GetEndTime() const { ThrowUnsupported(); return Time(); }
virtual int GetStartFrame() const { ThrowUnsupported(); return 0; }
virtual int GetEndFrame() const { ThrowUnsupported(); return 0; }
virtual bool IsComment() const { ThrowUnsupported(); return false; }
@ -80,8 +80,8 @@ namespace Athenasub {
// Write accessors
virtual void SetText(const String& text) { (void) text; ThrowUnsupported(); }
virtual void SetStartTime(const ITime& start) { (void) start; ThrowUnsupported(); }
virtual void SetEndTime(const ITime& end) { (void) end; ThrowUnsupported(); }
virtual void SetStartTime(const Time& start) { (void) start; ThrowUnsupported(); }
virtual void SetEndTime(const Time& end) { (void) end; ThrowUnsupported(); }
virtual void SetStartFrame(int start) { (void) start; ThrowUnsupported(); }
virtual void SetEndFrame(int end) { (void) end; ThrowUnsupported(); }
virtual void SetComment(bool isComment) { (void) isComment; ThrowUnsupported(); }

View File

@ -35,9 +35,11 @@
#include "Athenasub.h"
#include "athenatime.h"
#include "utils.h"
using namespace Athenasub;
#if 0
//////////////////////
// Generates a string
String CTime::GetString(int ms_precision,int h_precision) const
@ -68,22 +70,8 @@ String CTime::GetString(int ms_precision,int h_precision) const
else if (ms_precision == 1) _ms /= 100;
else if (ms_precision == 0) _ms = 0;
// Old code
if (false) {
// Generate mask string
wxString mask = wxString::Format(_T("%%0%ii:%%0%ii:%%0%ii.%%0%ii"),h_precision,2,2,ms_precision);
// Generate final string
wxString final = wxString::Format(mask,h,min,s,_ms);
// Done
return final;
}
// New code
else {
// Get write buffer
wxString final;
String final;
size_t size = 7+h_precision+ms_precision;
size_t pos = 0;
wxChar *buffer = final.GetWriteBuf(size);
@ -105,7 +93,6 @@ String CTime::GetString(int ms_precision,int h_precision) const
final.UngetWriteBuf(pos-1);
return final;
}
}
///////////////////
@ -141,3 +128,5 @@ void CTime::ParseString(const String &data)
}
ms = (int)accum;
}
#endif

View File

@ -108,8 +108,8 @@ int main()
size_t len = entries.size();
for (size_t i=0;i<len;i++) {
Dialogue diag = dynamic_pointer_cast<IDialogue> (entries[i]);
diag->SetStartTime(*(diag->GetStartTime() - 55555));
diag->SetEndTime(*(diag->GetEndTime() + 5555));
diag->SetStartTime(diag->GetStartTime() - 55555);
diag->SetEndTime(diag->GetEndTime() + 5555);
}
actions->Finish();
}