Skip to content

Commit

Permalink
Add ability to load DataTables from multiple files
Browse files Browse the repository at this point in the history
We can simply add loaded DataTables to the current ActiveGameSet now and
saving will handle it by verifying the "FileName" propery in the
DataTable's ExtendedProperties collection. This will allow for resolving
issues #11, #12 and #37 once the UI functionality is there.
  • Loading branch information
sraboy committed Dec 19, 2015
1 parent 9df088a commit 5ad8da2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
20 changes: 10 additions & 10 deletions SkaaEditorUI/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public bool OpenGameSet(string filepath)

SetActiveSpriteSframeDbfDataView();

return false;
return true;
}
/// <summary>
/// Loads a palette file.
Expand Down Expand Up @@ -308,7 +308,7 @@ public static Frame LoadFrame(string filepath, ColorPalette pal)

return frame;
}
public static Tuple<Sprite, DataSet> LoadResIdxMultiBmp(string filepath, ColorPalette pal)
public static Tuple<Sprite, DataTable> LoadResIdxMultiBmp(string filepath, ColorPalette pal)
{
Sprite spr = new Sprite();
DataTable dt = new DataTable();
Expand Down Expand Up @@ -342,15 +342,12 @@ public static Tuple<Sprite, DataSet> LoadResIdxMultiBmp(string filepath, ColorPa
}
}

DataSet ds = new DataSet();
ds.Tables.Add(dt);

return new Tuple<Sprite, DataSet>( spr, ds );
return new Tuple<Sprite, DataTable>( spr, dt );
}
public static Tuple<Sprite, DataSet> LoadResDbf(string filepath, ColorPalette pal)
public static Tuple<Sprite, DataTable> LoadResDbf(string filepath, ColorPalette pal)
{
Sprite spr = new Sprite();
DataSet ds = new DataSet();
DataTable dt = new DataTable();

using (FileStream fs = new FileStream(filepath, FileMode.Open))
{
Expand All @@ -361,10 +358,10 @@ public static Tuple<Sprite, DataSet> LoadResDbf(string filepath, ColorPalette pa

file.DataTable.TableName = Path.GetFileNameWithoutExtension(filepath);
file.DataTable.ExtendedProperties.Add("FileName", filepath);
ds.Tables.Add(file.DataTable);
dt = file.DataTable;
}

return new Tuple<Sprite, DataSet>(spr, ds);
return new Tuple<Sprite, DataTable>(spr, dt);
}

public void SaveResIdxMultiBmp(string filepath)
Expand All @@ -374,6 +371,9 @@ public void SaveResIdxMultiBmp(string filepath)
using (MemoryStream headerstream = new MemoryStream())
{
DataTable dt = this.ActiveGameSet.Tables[this.ActiveSprite.SpriteId];
if (dt.TableName != Path.GetFileNameWithoutExtension((string) dt.ExtendedProperties["FileName"]))
throw new Exception("TableName does not match file's original file name!");

int headersize = (dt.Rows.Count + 1) * ResourceDatabase.ResIdxDefinitionSize;
byte[] recordcount = BitConverter.GetBytes((short) dt.Rows.Count + 1); //+1 for the empty record
headerstream.Write(recordcount, 0, recordcount.Length);
Expand Down
22 changes: 12 additions & 10 deletions SkaaEditorUI/SkaaEditorMainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,10 @@ private void BeginOpenFile(FileFormats format, string filepath = "")
dlg.FileName = filepath;
OpenFile(dlg, format, () =>
{
Tuple<Sprite, DataSet> tup = Project.LoadResDbf(dlg.FileName, this.ActiveProject.ActivePalette);
this.ActiveProject.ActiveSprite = tup.Item1;
this.ActiveProject.ActiveGameSet = tup.Item2;
Tuple<Sprite, DataTable> tup = Project.LoadResDbf(dlg.FileName, this.ActiveProject.ActivePalette);
//this.ActiveProject.ActiveSprite = tup.Item1;
this.ActiveProject.ActiveGameSet = this.ActiveProject.ActiveGameSet ?? new DataSet();
this.ActiveProject.ActiveGameSet.Tables.Add(tup.Item2);
});
break;
case FileFormats.Palette:
Expand All @@ -496,9 +497,10 @@ private void BeginOpenFile(FileFormats format, string filepath = "")
dlg.FileName = filepath;
OpenFile(dlg, format, () =>
{
Tuple<Sprite, DataSet> tup = Project.LoadResIdxMultiBmp(dlg.FileName, this.ActiveProject.ActivePalette);
Tuple<Sprite, DataTable> tup = Project.LoadResIdxMultiBmp(dlg.FileName, this.ActiveProject.ActivePalette);
this.ActiveProject.ActiveGameSet = this.ActiveProject.ActiveGameSet ?? new DataSet();
this.ActiveProject.ActiveGameSet.Tables.Add(tup.Item2);
this.ActiveProject.ActiveSprite = tup.Item1;
this.ActiveProject.ActiveGameSet = tup.Item2;
});
break;
case FileFormats.Any: //user did not specify file type via UI menus (drag/drop or generic Open File)
Expand Down Expand Up @@ -667,10 +669,10 @@ private void SaveFile(FileFormats format)
{
case FileFormats.GameSet:
dlg.InitialDirectory = props.ProjectDirectory == null || this._tempProjectFolder ? props.ProjectsDirectory : props.ProjectDirectory;
dlg.Filter = $"7KAA Game Set Files (.set)|*{props.SetFileExtension}";
dlg.Filter = $"7KAA Game Set Files|*{props.SetFileExtension}";
dlg.DefaultExt = props.SetFileExtension;
dlg.FileName = "std.set|All Files (*.*)|*.*";
ShowSaveFileDialog(dlg, () => this.ActiveProject.ActiveGameSet.Save(dlg.FileName));
dlg.FileName = "std.set";
ShowSaveFileDialog(dlg, () => this.ActiveProject.ActiveGameSet.SaveGameSet(dlg.FileName));
break;
case FileFormats.SpritePNG:
dlg.InitialDirectory = props.ProjectDirectory == null || this._tempProjectFolder ? props.ProjectsDirectory : props.ProjectDirectory;
Expand Down Expand Up @@ -755,13 +757,13 @@ private void timelineControl_ActiveFrameChanged(object sender, EventArgs e)
//todo: look into this and refactor as necessary... it's just bad design
//will end up setting ActiveFrame twice since this will be called because of ActiveProject_ActiveFrameChanged
//but it's needed for the tracking bar to be able to make this update
this.ActiveProject.ActiveFrame = this.ActiveProject.ActiveSprite.Frames[this.timelineControl.GetActiveFrameIndex()];
this.ActiveProject.ActiveFrame = this.ActiveProject.ActiveSprite?.Frames[this.timelineControl.GetActiveFrameIndex()];
}
private void ActiveSprite_SpriteUpdated(object sender, EventArgs e) { }
private void ActiveProject_ActiveSpriteChanged(object sender, EventArgs e)
{
//todo: implement Undo/Redo from here with pairs of old/new sprites
this.timelineControl.SetFrameList(this.ActiveProject.ActiveSprite.GetFrameImages());
this.timelineControl.SetFrameList(this.ActiveProject.ActiveSprite?.GetFrameImages());
this.ActiveProject.ActiveFrame = this.ActiveProject?.ActiveSprite?.Frames[0];

//since a sprite has been un/loaded
Expand Down

0 comments on commit 5ad8da2

Please sign in to comment.