progress bars

This commit is contained in:
2017-02-11 14:13:03 +07:00
parent a3e9da5c73
commit dfcac503df
5 changed files with 84 additions and 30 deletions

View File

@@ -10,7 +10,8 @@ using FTRuntime;
namespace FTEditor.Postprocessors {
class SwfAssetPostprocessor : AssetPostprocessor {
static List<SwfAsset> _assetsForProcess = new List<SwfAsset>();
static SwfEditorUtils.ProgressBar _progressBar = new SwfEditorUtils.ProgressBar();
static List<SwfAsset> _assetsForProcess = new List<SwfAsset>();
static void OnPostprocessAllAssets(
string[] imported_assets,
@@ -42,6 +43,7 @@ namespace FTEditor.Postprocessors {
static void SwfAssetProcess(SwfAsset asset) {
try {
_progressBar.UpdateTitle(asset.name);
var new_data = ConfigureBitmaps(
asset,
SwfEditorUtils.DecompressAsset<SwfAssetData>(asset.Data));
@@ -62,6 +64,7 @@ namespace FTEditor.Postprocessors {
if ( asset ) {
UpdateAssetClips(asset);
}
_progressBar.HideProgress();
}
}
@@ -86,12 +89,18 @@ namespace FTEditor.Postprocessors {
// ---------------------------------------------------------------------
static SwfAssetData ConfigureBitmaps(SwfAsset asset, SwfAssetData data) {
var textures = data.Bitmaps
.Where (p => p.Redirect == 0)
.Select(p => new KeyValuePair<ushort, Texture2D>(
p.Id,
LoadTextureFromData(p)))
.ToList();
var textures = new List<KeyValuePair<ushort, Texture2D>>(data.Bitmaps.Count);
for ( var i = 0; i < data.Bitmaps.Count; ++i ) {
_progressBar.UpdateProgress(
"configure bitmaps",
(float)(i + 1) / data.Bitmaps.Count);
var bitmap = data.Bitmaps[i];
if ( bitmap.Redirect == 0 ) {
textures.Add(new KeyValuePair<ushort, Texture2D>(
bitmap.Id,
LoadTextureFromData(bitmap)));
}
}
var rects = PackAndSaveBitmapsAtlas(
GetAtlasPath(asset),
textures.Select(p => p.Value).ToArray(),
@@ -121,10 +130,13 @@ namespace FTEditor.Postprocessors {
static Rect[] PackAndSaveBitmapsAtlas(
string atlas_path, Texture2D[] textures, SwfSettingsData settings)
{
_progressBar.UpdateProgress("pack bitmaps", 0.25f);
var atlas_info = PackBitmapsAtlas(textures, settings);
RevertTexturePremultipliedAlpha(atlas_info.Atlas);
_progressBar.UpdateProgress("save atlas", 0.5f);
File.WriteAllBytes(atlas_path, atlas_info.Atlas.EncodeToPNG());
GameObject.DestroyImmediate(atlas_info.Atlas, true);
_progressBar.UpdateProgress("import atlas", 0.75f);
AssetDatabase.ImportAsset(atlas_path);
return atlas_info.Rects;
}
@@ -261,8 +273,11 @@ namespace FTEditor.Postprocessors {
static SwfAssetData ConfigureClips(SwfAsset asset, SwfAssetData data) {
asset.Clips = asset.Clips.Where(p => !!p).Distinct().ToList();
foreach ( var symbol in data.Symbols ) {
ConfigureClip(asset, data, symbol);
for ( var i = 0; i < data.Symbols.Count; ++i ) {
_progressBar.UpdateProgress(
"configure clips",
(float)(i + 1) / data.Symbols.Count);
ConfigureClip(asset, data, data.Symbols[i]);
}
return data;
}

View File

@@ -14,7 +14,8 @@ using FTSwfTools.SwfTypes;
namespace FTEditor.Postprocessors {
class SwfPostprocessor : AssetPostprocessor {
static List<string> _assetsForProcess = new List<string>();
static SwfEditorUtils.ProgressBar _progressBar = new SwfEditorUtils.ProgressBar();
static List<string> _assetsForProcess = new List<string>();
static void OnPostprocessAllAssets(
string[] imported_assets,
@@ -62,6 +63,7 @@ namespace FTEditor.Postprocessors {
static bool SafeLoadSwfAsset(string swf_path, SwfAsset swf_asset) {
try {
_progressBar.UpdateTitle(Path.GetFileName(swf_path));
var new_data = LoadSwfAssetData(swf_path);
swf_asset.Data = SwfEditorUtils.CompressAsset(new_data);
return true;
@@ -70,12 +72,16 @@ namespace FTEditor.Postprocessors {
"<b>[FlashTools]</b> Parsing swf error: {0}",
e.Message);
return false;
} finally {
_progressBar.HideProgress();
}
}
static SwfAssetData LoadSwfAssetData(string swf_path) {
var library = new SwfLibrary();
var decoder = new SwfDecoder(swf_path);
var decoder = new SwfDecoder(swf_path, progress => {
_progressBar.UpdateProgress("swf decoding", progress);
});
return new SwfAssetData{
FrameRate = decoder.UncompressedHeader.FrameRate,
Symbols = LoadSymbols(library, decoder),
@@ -95,10 +101,15 @@ namespace FTEditor.Postprocessors {
symbols.Add(LoadSymbol("_Stage_", library, decoder.Tags));
var sprite_defs = library.Defines.Values
.OfType<SwfLibrarySpriteDefine>()
.Where(p => !string.IsNullOrEmpty(p.ExportName));
foreach ( var sprite_def in sprite_defs ) {
var name = sprite_def.ExportName;
var tags = sprite_def.ControlTags.Tags;
.Where(p => !string.IsNullOrEmpty(p.ExportName))
.ToList();
for ( var i = 0; i < sprite_defs.Count; ++i ) {
_progressBar.UpdateProgress(
"load swf symbols",
(float)(i + 1) / sprite_defs.Count);
var def = sprite_defs[i];
var name = def.ExportName;
var tags = def.ControlTags.Tags;
symbols.Add(LoadSymbol(name, library, tags));
}
return symbols;

View File

@@ -225,6 +225,28 @@ namespace FTEditor {
}
#endif
// ---------------------------------------------------------------------
//
// ProgressBar
//
// ---------------------------------------------------------------------
public class ProgressBar {
string _title = "Flash Tools Process";
public void UpdateTitle(string title) {
_title = string.Format("Flash Tools Process: {0}", title);
}
public void UpdateProgress(string info, float progress) {
EditorUtility.DisplayProgressBar(_title, info, progress);
}
public void HideProgress() {
EditorUtility.ClearProgressBar();
}
}
// ---------------------------------------------------------------------
//
// Menu

View File

@@ -10,10 +10,13 @@ namespace FTSwfTools {
public SwfLongHeader UncompressedHeader;
public List<SwfTagBase> Tags = new List<SwfTagBase>();
public SwfDecoder(string swf_path) {
public SwfDecoder(string swf_path) : this(swf_path, null) {
}
public SwfDecoder(string swf_path, System.Action<float> progress_act) {
var raw_data = File.ReadAllBytes(swf_path);
var uncompressed_stream = DecompressSwfData(raw_data);
DecodeSwf(new SwfStreamReader(uncompressed_stream));
DecodeSwf(new SwfStreamReader(uncompressed_stream), progress_act);
}
MemoryStream DecompressSwfData(byte[] raw_swf_data) {
@@ -40,9 +43,12 @@ namespace FTSwfTools {
}
}
void DecodeSwf(SwfStreamReader reader) {
void DecodeSwf(SwfStreamReader reader, System.Action<float> progress_act) {
UncompressedHeader = SwfLongHeader.Read(reader);
while ( !reader.IsEOF ) {
if ( progress_act != null ) {
progress_act((float)(reader.Position + 1) / reader.Length);
}
var tag = SwfTagBase.Read(reader);
if ( tag.TagType == SwfTagType.End ) {
break;

View File

@@ -13,18 +13,6 @@ namespace FTSwfTools {
BitContext _bitContext;
BinaryReader _binaryReader;
long Length {
get { return _binaryReader.BaseStream.Length; }
}
long Position {
get { return _binaryReader.BaseStream.Position; }
}
long BytesLeft {
get { return Length - Position; }
}
// ---------------------------------------------------------------------
//
// Public
@@ -44,6 +32,18 @@ namespace FTSwfTools {
get { return Position >= Length; }
}
public long Length {
get { return _binaryReader.BaseStream.Length; }
}
public long Position {
get { return _binaryReader.BaseStream.Position; }
}
public long BytesLeft {
get { return Length - Position; }
}
public void AlignToByte() {
_bitContext.BitIndex = 0;
_bitContext.CachedByte = 0;