Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
Gif exporting, Bitmap objects
Browse files Browse the repository at this point in the history
Gif exporting will be improved soon.
  • Loading branch information
Evar Smith authored and Evar Smith committed Aug 20, 2015
1 parent 9b24a65 commit 7c2326d
Show file tree
Hide file tree
Showing 17 changed files with 488 additions and 230 deletions.
Binary file added Libraries/Gif.Components.dll
Binary file not shown.
10 changes: 5 additions & 5 deletions TISFAT/Properties/AssemblyInfo.cs
Expand Up @@ -5,12 +5,12 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("TisfatTest2")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyTitle("TISFAT Zero")]
[assembly: AssemblyDescription("A 2D Stick Figure Animation Software!")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TisfatTest2")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyCompany("Atomic Software")]
[assembly: AssemblyProduct("TISFAT Zero")]
[assembly: AssemblyCopyright("Copyright © 2015 Evar Smith")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand Down
6 changes: 6 additions & 0 deletions TISFAT/TISFAT.csproj
Expand Up @@ -36,13 +36,17 @@
<ApplicationIcon>Resources\T0.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="Gif.Components">
<HintPath>..\Libraries\Gif.Components.dll</HintPath>
</Reference>
<Reference Include="OpenTK, Version=1.1.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<HintPath>..\packages\OpenTK.1.1.1589.5942\lib\NET40\OpenTK.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="OpenTK.GLControl, Version=1.1.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<HintPath>..\packages\OpenTK.GLControl.dll</HintPath>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -54,9 +58,11 @@
<Reference Include="System.Net.Http" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="src\Core\Frameset.cs" />
<Compile Include="src\Entities\BitmapObject.cs" />
<Compile Include="src\Entities\StickFigure.cs" />
<Compile Include="src\Entities\StickFigure.Joint.cs">
<DependentUpon>StickFigure.cs</DependentUpon>
Expand Down
24 changes: 23 additions & 1 deletion TISFAT/UI/CanvasForm.cs
Expand Up @@ -4,6 +4,8 @@
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using System.Windows.Media.Imaging;
using System.Windows;

namespace TISFAT
{
Expand Down Expand Up @@ -66,14 +68,19 @@ private void CanvasForm_Resize(object sender, EventArgs e)
#endregion

public void GLContext_Paint(object sender, PaintEventArgs e)
{
DrawFrame(Program.Form.MainTimeline.GetCurrentFrame(), false);
}

public void DrawFrame(float time, bool render)
{
GLContext.MakeCurrent();

GL.ClearColor(Color.White);

GL.Clear(ClearBufferMask.ColorBufferBit);

Program.Form.ActiveProject.Draw(Program.Form.MainTimeline.GetCurrentFrame());
Program.Form.ActiveProject.Draw(time);

GLContext.SwapBuffers();
}
Expand Down Expand Up @@ -122,5 +129,20 @@ private void CanvasForm_Enter(object sender, EventArgs e)
{
BringToFront();
}

public IntPtr TakeScreenshot()
{
if (GraphicsContext.CurrentContext == null)
throw new GraphicsContextMissingException();

Bitmap bmp = new Bitmap(GLContext.ClientSize.Width, GLContext.ClientSize.Height);
System.Drawing.Imaging.BitmapData data = bmp.LockBits(GLContext.ClientRectangle, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
GL.ReadPixels(0, 0, GLContext.ClientSize.Width, GLContext.ClientSize.Height, PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0);
bmp.UnlockBits(data);

bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);

return bmp.GetHbitmap();
}
}
}
460 changes: 252 additions & 208 deletions TISFAT/UI/MainForm.Designer.cs

Large diffs are not rendered by default.

73 changes: 66 additions & 7 deletions TISFAT/UI/MainForm.cs
@@ -1,4 +1,5 @@
using System;
using Gif.Components;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
Expand Down Expand Up @@ -35,7 +36,6 @@ public MainForm()
#region General Init
ProjectNew();
AddTestLayer();
AddTestLayer();
#endregion
}

Expand All @@ -46,14 +46,19 @@ private void MainForm_Load(object sender, EventArgs e)
Form_Toolbox = new ToolboxForm(sc_MainContainer.Panel2);
Form_Canvas = new CanvasForm(sc_MainContainer.Panel2);

// Position Forms
Form_Timeline.Location = new Point(0, 0);
Form_Toolbox.Location = new Point(0, Form_Timeline.Location.Y + Form_Timeline.Height + 4);
Form_Canvas.Location = new Point(Form_Toolbox.Location.X + Form_Toolbox.Width + 20, Form_Timeline.Location.Y + Form_Timeline.Height + 4);

Form_Timeline.Show();
Form_Toolbox.Show();
Form_Canvas.Show();

Form_Timeline.Anchor = AnchorStyles.None;
Form_Toolbox.Anchor = AnchorStyles.None;
Form_Canvas.Anchor = AnchorStyles.None;

Form_Timeline.Location = new Point(5, 0);
Form_Toolbox.Location = new Point(50, Form_Timeline.Location.Y + Form_Timeline.Height + 4);
Form_Canvas.Location = new Point(Form_Toolbox.Location.X + Form_Toolbox.Width + 20, Form_Timeline.Location.Y + Form_Timeline.Height + 4);

AddTestBitmapLayer();
}

private void AddTestLayer()
Expand Down Expand Up @@ -95,6 +100,25 @@ private void AddTestLayer()
MainTimeline.GLContext.Invalidate();
}

private void AddTestBitmapLayer()
{
BitmapObject figure = new BitmapObject();

//figure.Texture = Properties.Resources.eye;
figure.Texture = (Bitmap)Bitmap.FromFile("C:\\Users\\Evar\\Desktop\\boy.png", true);
figure.TextureID = Drawing.GenerateTexID(figure.Texture);

Layer layer = new Layer(figure);
layer.Framesets.Add(new Frameset());
layer.Framesets[0].Keyframes.Add(new Keyframe(0, figure.CreateRefState()));
layer.Framesets[0].Keyframes.Add(new Keyframe(20, figure.CreateRefState()));

ActiveProject.Layers.Add(layer);

if (MainTimeline != null)
MainTimeline.GLContext.Invalidate();
}

public void SetDirty(bool dirty)
{
ProjectDirty = dirty;
Expand Down Expand Up @@ -188,5 +212,40 @@ private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
}
}
#endregion

private void animatedGifgifToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "Animated Gif|*.gif";

if (dlg.ShowDialog() != DialogResult.OK)
return;
FileStream fs = new FileStream(dlg.FileName, FileMode.Create);
AnimatedGifEncoder g = new AnimatedGifEncoder();

float fps = 10.0f;
float delta = 1.0f / fps;

float endTime = 0.0f;

foreach (Layer layer in ActiveProject.Layers)
{
endTime = Math.Max(endTime, layer.Framesets[layer.Framesets.Count - 1].EndTime);
}

g.Start(fs);

g.SetDelay((int)Math.Round(delta * 1000.0f));
g.SetQuality(2);
g.SetRepeat(0);

for (float time = 0; time <= endTime / ActiveProject.FPS; time += delta)
{
Form_Canvas.DrawFrame(time * ActiveProject.FPS, true);
g.AddFrame(Image.FromHbitmap(Form_Canvas.TakeScreenshot()));
}

g.Finish();
}
}
}
4 changes: 2 additions & 2 deletions TISFAT/UI/Timeline.cs
Expand Up @@ -162,7 +162,7 @@ public float GetCurrentFrame()
float frame;

if (PlayStart != null)
frame = ((float)(DateTime.Now - (DateTime)PlayStart).TotalSeconds) * 10.0f;
frame = ((float)(DateTime.Now - (DateTime)PlayStart).TotalSeconds) * Program.Form.ActiveProject.FPS;
else
frame = 0.0f;

Expand Down Expand Up @@ -537,7 +537,7 @@ public void InsertKeyframe()
if (prev == null)
return;

Keyframe frame = new Keyframe(TargetTime, prev.State.CreateRefState());
Keyframe frame = new Keyframe(TargetTime, prev.State.Copy());

SelectedFrameset.Keyframes.Add(frame);
SelectedFrameset.Keyframes = SelectedFrameset.Keyframes.OrderBy(o => o.Time).ToList();
Expand Down
2 changes: 1 addition & 1 deletion TISFAT/UI/TimelineDrawing.cs
Expand Up @@ -164,7 +164,7 @@ public void DrawLabels(List<Layer> Layers)

Drawing.Rectangle(new PointF(0, 16 * (i + 1)), new SizeF(80, 16), layer.TimelineColor);
Drawing.RectangleLine(new PointF(0, 16 * (i + 1)), new SizeF(80, 16), Color.Black);
Drawing.TextRect(layer.Name, new PointF(1, 16 * (i + 1) + 1), new Size(65, 16), new Font("Segoe UI", 9), Color.White, StringAlignment.Near);
Drawing.TextRect(layer.Name, new PointF(1, 16 * (i + 1) + 1), new Size(65, 16), new Font("Segoe UI", 9), Color.Black, StringAlignment.Near);

if(HoveredLayerIndex == i || !layer.Visible)
Drawing.Bitmap(new PointF(65, 16 * (i + 1) + 2),
Expand Down
6 changes: 6 additions & 0 deletions TISFAT/src/Core/Project.cs
Expand Up @@ -8,6 +8,7 @@ namespace TISFAT
public class Project : ISaveable
{
public List<Layer> Layers;
public float FPS;

public Project()
{
Expand All @@ -34,11 +35,16 @@ public void Draw(float time)
public void Write(BinaryWriter writer)
{
FileFormat.WriteList(writer, Layers);
writer.Write(FPS);
}

public void Read(BinaryReader reader, UInt16 version)
{
Layers = FileFormat.ReadList<Layer>(reader, version);
if (version == 1)
{
FPS = (float)reader.ReadDouble();
}
}
#endregion
}
Expand Down
110 changes: 110 additions & 0 deletions TISFAT/src/Entities/BitmapObject.cs
@@ -0,0 +1,110 @@
using System;
using System.Drawing;
using System.IO;
using TISFAT.Util;

namespace TISFAT.Entities
{
public class BitmapObject : IEntity
{
public Bitmap Texture;
public int TextureID;

public class State : IEntityState
{
public RectangleF Bounds;

public State() { }

public IEntityState Copy()
{
State state = new State();
state.Bounds = new RectangleF(Bounds.Location, Bounds.Size);
return state;
}

public void Write(BinaryWriter writer)
{

}

public void Read(BinaryReader reader, UInt16 version)
{

}
}

public BitmapObject() { }

public IEntityState Interpolate(float t, IEntityState _current, IEntityState _target)
{
State current = _current as State;
State target = _target as State;
State state = new State();

state.Bounds = Interpolation.Linear(t, current.Bounds, target.Bounds);

return state;
}

public void Draw(IEntityState _state)
{
State state = _state as State;
Drawing.Bitmap(state.Bounds.Location, state.Bounds.Size, TextureID);
}

private void DrawHandle(State state, Color c)
{
float size = Math.Min(12, Texture.Width / 2);

Drawing.RectangleLine(state.Bounds.Location, state.Bounds.Size, c);
Drawing.Rectangle(new PointF(state.Bounds.Left, state.Bounds.Top), new SizeF(size, size), c);
Drawing.Rectangle(new PointF(state.Bounds.Right - size, state.Bounds.Top), new SizeF(size, size), c);
Drawing.Rectangle(new PointF(state.Bounds.Left, state.Bounds.Bottom - size), new SizeF(size, size), c);
Drawing.Rectangle(new PointF(state.Bounds.Right - size, state.Bounds.Bottom - size), new SizeF(size, size), c);
}

public void DrawEditable(IEntityState _state)
{
State state = _state as State;

Drawing.Bitmap(state.Bounds.Location, state.Bounds.Size, TextureID);
DrawHandle(state, Color.Cyan);
}

public ManipulateResult TryManipulate(IEntityState state, Point location, System.Windows.Forms.MouseButtons button, System.Windows.Forms.Keys modifiers)
{
return null;
}

public void ManipulateStart(IManipulatable target, IManipulatableParams mparams, Point location)
{

}

public void ManipulateUpdate(IManipulatable target, IManipulatableParams mparams, Point location)
{

}

public void ManipulateEnd(IManipulatable target, IManipulatableParams mparams, Point location)
{

}

public IEntityState CreateRefState()
{
return new State() { Bounds = new RectangleF(10, 10, Texture.Width, Texture.Height) };
}

public void Write(BinaryWriter writer)
{

}

public void Read(BinaryReader reader, UInt16 version)
{

}
}
}
4 changes: 2 additions & 2 deletions TISFAT/src/Entities/StickFigure.Joint.cs
Expand Up @@ -97,15 +97,15 @@ public void DrawHandle(State state)
}
#endregion

public State CreateRefState(State parent = null)
public State Copy(State parent = null)
{
State state = new State(parent);
state.Location = Location;
state.Thickness = Thickness;
state.JointColor = JointColor;

foreach (Joint child in Children)
state.Children.Add(child.CreateRefState(state));
state.Children.Add(child.Copy(state));

return state;
}
Expand Down
2 changes: 1 addition & 1 deletion TISFAT/src/Entities/StickFigure.State.cs
Expand Up @@ -11,7 +11,7 @@ public class State : IEntityState, ISaveable

public State() { }

public IEntityState CreateRefState()
public IEntityState Copy()
{
return new State { Root = this.Root.Clone() };
}
Expand Down

0 comments on commit 7c2326d

Please sign in to comment.