using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using BauzoidNET.graphics;
using BauzoidNET.graphics.sprite;
using BauzoidNET.math;
using ShapeEditor.file.shapes;
using ShapeEditor.interaction;
namespace ShapeEditor
.file
{
public class Document
{
public static readonly Vector4 SHAPE_COLOR
= new Vector4
(0,
0,
1,
1);
public static readonly Vector4 SHAPE_COLOR_SELECTED
= new Vector4
(0
.8f, 0
.2f,
1,
1);
public static readonly Vector4 SHAPE_COLOR_NOT_IN_FRAME_RANGE
= new Vector4
(0
.6f, 0
.6f, 0
.6f, 0
.9f
);
public const int HANDLE_SIZE
= 7;
private MainForm mOwner
= null;
public MainForm Owner
{ get
{ return mOwner
; } }
private bool mIsDirty
= false;
private string mFilename
= null;
private List
<BaseShapeElement
> mShapeElements
= new List
<BaseShapeElement
>();
public List
<BaseShapeElement
> ShapeElements
{ get
{ return mShapeElements
; } }
private Sprite mSprite
= null;
private SimpleSprite mHandle
= null;
private SpriteInstance mSpriteInstance
= null;
public SpriteInstance Sprite
{ get
{ return mSpriteInstance
; } }
private int mCurrentFrame
= 0;
public int CurrentFrame
{
get
{
return mCurrentFrame
;
}
set
{
mCurrentFrame
= MathUtil
.clamp(value,
0, mSpriteInstance
.getNumFrames()-1);
mSpriteInstance
.setCurrentFrame(mCurrentFrame
);
Owner
.lblCurrentFrame.Text = (mCurrentFrame
+ 1) + "/" + mSpriteInstance
.getNumFrames();
Owner
.GlView.Refresh();
}
}
public Document
(MainForm owner
)
{
mOwner
= owner
;
mHandle
= new SimpleSprite
(MainForm
.App.getGraphics(), ShapeEditor
.Properties.Resources.handle);
}
public void Init
()
{
mHandle
.init();
mHandle
.getSpriteTransform().w = HANDLE_SIZE
;
mHandle
.getSpriteTransform().h = HANDLE_SIZE
;
mHandle
.getSpriteTransform().centerPivot();
}
public void Exit
()
{
Destroy
();
mHandle
.dispose();
}
public void Destroy
()
{
DestroySprite
();
mOwner
.ZoomFactor = 1
.0f
;
mOwner
.mCurrentX = 0
.0f
;
mOwner
.mCurrentY = 0
.0f
;
mShapeElements
.Clear();
mOwner
.ElementsListBox.Items.Clear();
}
public void DestroySprite
()
{
if (mSprite
!= null)
{
mSprite
.dispose();
mSprite
= null;
}
mSpriteInstance
= null;
}
public void SetMainFormCaption
()
{
if (IsFilenameSet
())
mOwner
.Text = Path
.GetFileName(mFilename
) + " - Bauzoid ShapeEditor";
else
mOwner
.Text = "Bauzoid ShapeEditor";
}
public bool NewDocument
()
{
Destroy
();
mIsDirty
= false;
ChangeSprite
(null);
SetMainFormCaption
();
return true;
}
public bool LoadDocument
(String filename
)
{
Destroy
();
mIsDirty
= false;
mFilename
= filename
;
string spriteFile
= Path
.ChangeExtension(filename,
"png");
if (!File
.Exists(spriteFile
))
spriteFile
= "data/textures/test.png";
ChangeSprite
(spriteFile
);
if (!FileUtil
.LoadLevel(mFilename,
this))
return false;
SetMainFormCaption
();
return true;
}
public bool SaveDocument
()
{
// save mLevelElements
if (!FileUtil
.SaveLevel(mFilename,
this))
return false;
SetMainFormCaption
();
return true;
}
public bool SaveDocument
(string filename
)
{
mFilename
= filename
;
return SaveDocument
();
}
public void ChangeSprite
(string filename
)
{
DestroySprite
();
if (filename
== null)
{
mSprite
= new Sprite
(MainForm
.App.getGraphics(), ShapeEditor
.Properties.Resources.test);
}
else
{
string atlasFile
= Path
.ChangeExtension(filename,
"txt");
if (File
.Exists(atlasFile
))
{
mSprite
= new Sprite
(MainForm
.App.getGraphics(), filename, atlasFile
);
}
else
{
mSprite
= new Sprite
(MainForm
.App.getGraphics(), filename
);
}
}
mSprite
.init();
mSpriteInstance
= mSprite
.createSpriteInstanceForAll();
mSpriteInstance
.transform.x = 0;
mSpriteInstance
.transform.y = 0;
mSpriteInstance
.transform.w = mSpriteInstance
.getSprite().getTextureWidth() * mSpriteInstance
.getSpriteRegion().getWidth();
mSpriteInstance
.transform.h = mSpriteInstance
.getSprite().getTextureHeight() * mSpriteInstance
.getSpriteRegion().getHeight();
mSpriteInstance
.transform.pivotX = 0;
mSpriteInstance
.transform.pivotY = 0;
CurrentFrame
= 0;
}
public void Render
()
{
Graphics g
= MainForm
.App.getGraphics();
for (int i
= 0; i
< mShapeElements
.Count; i
++)
{
/*if (!mShapeElements.ElementAt(i).IsInFrameRange(CurrentFrame))
continue;*/
if (mOwner
.ElementsListBox.SelectedIndex == i
)
mShapeElements
.ElementAt(i
).RenderSelected(mSpriteInstance
.transform, mOwner
.Interaction.Mode == InteractionMode
.SELECT);
else
mShapeElements
.ElementAt(i
).Render(mSpriteInstance
.transform);
}
}
/*public bool IsInsideHandle(int x, int y)
{
for (int i = mShapeElements.Count - 1; i >= 0; i--)
{
if (mShapeElements.ElementAt(i).IsInsideHandle(x, y, mSpriteInstance.transform, mOwner.ZoomFactor))
return true;
}
return false;
}*/
public BaseShapeElement GetSelectedElement
()
{
if (mOwner
.ElementsListBox.Items.Count == 0)
return null;
if (mOwner
.ElementsListBox.SelectedIndex == -1)
return null;
return mShapeElements
.ElementAt(mOwner
.ElementsListBox.SelectedIndex);
}
public BaseShapeElement SelectAt
(float x,
float y
)
{
if (mOwner
.ElementsListBox.Items.Count == 0)
return null;
if (mOwner
.ElementsListBox.SelectedIndex == -1)
return null;
for (int i
= mShapeElements
.Count - 1; i
>= 0; i
--)
{
/*if (!mShapeElements.ElementAt(i).IsInFrameRange(CurrentFrame))
continue;*/
if (mShapeElements
.ElementAt(i
).IsInside(x, y
))
{
mOwner
.ElementsListBox.SelectedIndex = i
;
return mShapeElements
.ElementAt(i
);
}
}
return null;
}
public void RenderHandle
(float x,
float y, SpriteTransform t
)
{
//Vector2 p = t.spriteToWorld(x, y);
mHandle
.getSpriteTransform().x = x
* mOwner
.ZoomFactor + mSpriteInstance
.transform.x;
mHandle
.getSpriteTransform().y = y
* mOwner
.ZoomFactor + mSpriteInstance
.transform.y;
mHandle
.render();
}
public void AddRectangle
(float x,
float y,
float w,
float h,
int startFrame,
int endFrame
)
{
string name
= "[-1..-1] rect " + x
+ ", " + y
+ ", " + w
+ ", " + h
;
RectangleElement e
= new RectangleElement
(this, name, x, y, w, h
);
e
.StartFrame = startFrame
;
e
.EndFrame = endFrame
;
mShapeElements
.Add(e
);
mOwner
.ElementsListBox.Items.Add(e
);
mOwner
.ElementsListBox.SelectedIndex = mOwner
.ElementsListBox.Items.Count - 1;
mIsDirty
= true;
}
public void AddEllipse
(float centerX,
float centerY,
float radiusX,
float radiusY,
int startFrame,
int endFrame
)
{
string name
= "[-1..-1] ellipse " + centerX
+ ", " + centerY
+ ", " + radiusX
+ ", " + radiusY
;
EllipseElement e
= new EllipseElement
(this, name, centerX, centerY, radiusX, radiusY
);
e
.StartFrame = startFrame
;
e
.EndFrame = endFrame
;
mShapeElements
.Add(e
);
mOwner
.ElementsListBox.Items.Add(e
);
mOwner
.ElementsListBox.SelectedIndex = mOwner
.ElementsListBox.Items.Count - 1;
mIsDirty
= true;
}
public void AddPolygon
(List
<Vector2
> vertices,
int startFrame,
int endFrame
)
{
if (vertices
.Count < 3)
return;
string name
= "[-1..-1] polygon " + vertices
.First().x + ", " + vertices
.First().y + " -->";
PolygonElement e
= new PolygonElement
(this, name, vertices
);
e
.StartFrame = startFrame
;
e
.EndFrame = endFrame
;
mShapeElements
.Add(e
);
mOwner
.ElementsListBox.Items.Add(e
);
/*EllipseElement e = new EllipseElement(this, name, centerX, centerY, radiusX, radiusY);
mShapeElements.Add(e);
mOwner.ElementsListBox.Items.Add(e);*/
mOwner
.ElementsListBox.SelectedIndex = mOwner
.ElementsListBox.Items.Count - 1;
mIsDirty
= true;
}
public void DeleteCurrent
()
{
int i
= mOwner
.ElementsListBox.SelectedIndex;
if (i
== -1)
return;
mShapeElements
.RemoveAt(i
);
mOwner
.ElementsListBox.Items.RemoveAt(i
);
mOwner
.ElementsListBox.SelectedIndex = Math
.Min(i, mOwner
.ElementsListBox.Items.Count - 1);
}
public void MoveUp
()
{
int i
= mOwner
.ElementsListBox.SelectedIndex;
if (i
<= 0)
return;
BaseShapeElement e
= mShapeElements
.ElementAt(i
);
mShapeElements
.RemoveAt(i
);
mOwner
.ElementsListBox.Items.RemoveAt(i
);
mShapeElements
.Insert(i
- 1, e
);
mOwner
.ElementsListBox.Items.Insert(i
- 1, e
);
mOwner
.ElementsListBox.SelectedIndex = i
- 1;
}
public void MoveDown
()
{
int i
= mOwner
.ElementsListBox.SelectedIndex;
if ((i
== -1) || (i
>= (mOwner
.ElementsListBox.Items.Count-1)))
return;
BaseShapeElement e
= mShapeElements
.ElementAt(i
);
mShapeElements
.RemoveAt(i
);
mOwner
.ElementsListBox.Items.RemoveAt(i
);
mShapeElements
.Insert(i
+ 1, e
);
mOwner
.ElementsListBox.Items.Insert(i
+ 1, e
);
mOwner
.ElementsListBox.SelectedIndex = i
+ 1;
}
public BaseShapeElement GetCurrentShape
()
{
if (mOwner
.ElementsListBox.SelectedIndex == -1)
return null;
return mShapeElements
.ElementAt(mOwner
.ElementsListBox.SelectedIndex);
}
public void SetDirty
(bool dirty
= true)
{
mIsDirty
= dirty
;
}
public bool IsDirty
()
{
return mIsDirty
;
}
public String GetFilename
()
{
return mFilename
;
}
public void SetFilename
(String filename
)
{
mFilename
= filename
;
}
public bool IsFilenameSet
()
{
return (mFilename
!= null);
}
public SpriteInstance GetSpriteInstance
()
{
return mSpriteInstance
;
}
}
}