Rev 1822 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
using System.Windows.Forms.Design;
using BurutaruEditor.file.elements;
using BurutaruEditor.file.enemies;
using BurutaruEditor.view;
using BauzoidNET.math;
using BauzoidNET.parser;
using BurutaruEditor.file.conditions;
namespace BurutaruEditor
.file
{
public class Document
{
//=================================================================================================================================
public const float VIRTUAL_WIDTH
= 800;
public const float VIRTUAL_HEIGHT
= 480;
public const float PASTE_OFFSET
= 15;
public static readonly Vector4 TEXT_COLOR
= new Vector4
(1,
1,
1,
1);
public const float TEXT_SCALE
= 0
.8f
;
//=================================================================================================================================
private MainForm mOwner
= null;
[Browsable
(false)]
public MainForm Owner
{ get
{ return mOwner
; } }
[Browsable
(false)]
public DocumentView View
{ get
{ return Owner
.View; } }
private bool mIsDirty
= false;
private String mFilename
= null;
private List
<CheckPoint
> mCheckPoints
= new List
<CheckPoint
>();
[Browsable
(false)]
public List
<CheckPoint
> CheckPoints
{ get
{ return mCheckPoints
; } }
private List
<EnemySpawner
> mSpawners
= new List
<EnemySpawner
>();
[Browsable
(false)]
public List
<EnemySpawner
> Spawners
{ get
{ return mSpawners
; } }
private List
<LevelElement
> mLevelElements
= new List
<LevelElement
>();
[Browsable
(false)]
public List
<LevelElement
> LevelElements
{ get
{ return mLevelElements
; } }
private List
<BaseCondition
> mScripting
= new List
<BaseCondition
>();
[Browsable
(false)]
public List
<BaseCondition
> Scripting
{ get
{ return mScripting
; } }
[Category
("Basic Parameters")]
public string LevelTitle
{ get
; set
; }
[Category
("Basic Parameters")]
public string StageTitle
{ get
; set
; }
private Vector2 mStartPosition
= new Vector2
();
[TypeConverter
(typeof(ExpandableObjectConverter
))]
[Category
("Basic Parameters")]
public Vector2 StartPosition
{ get
{ return mStartPosition
; } }
[Category
("Basic Parameters")]
public bool WarpIn
{ get
; set
; }
[Category
("Basic Parameters")]
public CheckPoint
.StarFieldControlStatus StarFieldStatus
{ get
; set
; }
private string mCopyPasteBuffer
= null;
private float mPasteOffset
= PASTE_OFFSET
;
//=================================================================================================================================
public class SpriteFileNameEditor
: FileNameEditor
{
protected override void InitializeDialog
(OpenFileDialog openFileDialog
)
{
base.InitializeDialog(openFileDialog
);
String s
= Path
.Combine(Application
.StartupPath,
@"..\\..\\..\\..\\..\\BauzoidApps\\BurutaruZ_v2\\burutaru-android\\assets\\data");
openFileDialog
.InitialDirectory = Path
.GetFullPath(s
) + "\\textures";
openFileDialog
.Filter = "Image files|*.png|All files|*.*";
}
}
public class ShapeFileNameEditor
: FileNameEditor
{
protected override void InitializeDialog
(OpenFileDialog openFileDialog
)
{
base.InitializeDialog(openFileDialog
);
String s
= Path
.Combine(Application
.StartupPath,
@"..\\..\\..\\..\\..\\BauzoidApps\\BurutaruZ_v2\\burutaru-android\\assets\\data");
openFileDialog
.InitialDirectory = Path
.GetFullPath(s
) + "\\textures";
openFileDialog
.Filter = "Shape files|*.shape|All files|*.*";
}
}
//=================================================================================================================================
public Document
(MainForm owner
)
{
mOwner
= owner
;
ResetDocument
();
StageTitle
= "Stage 01";
WarpIn
= false;
StarFieldStatus
= CheckPoint
.StarFieldControlStatus.NORMAL;
}
public void Init
()
{
ResetDocument
();
}
public void Exit
()
{
ResetDocument
();
}
public void ResetDocument
()
{
mFilename
= null;
LevelTitle
= "Level Title";
mIsDirty
= false;
mSpawners
.Clear();
for (int i
= 0; i
< mLevelElements
.Count; i
++)
mLevelElements
.ElementAt(i
).Exit();
mLevelElements
.Clear();
mCheckPoints
.Clear();
Owner
.CheckPoints.Items.Clear();
Owner
.Spawners.Items.Clear();
Owner
.LevelElements.Items.Clear();
Owner
.Scripting.Items.Clear();
Owner
.Properties.SelectedObject = this;
}
public bool NewDocument
()
{
ResetDocument
();
View
.ResetView();
Owner
.Text = "Unnamed.level - Burutaru Editor";
return true;
}
public bool LoadDocument
(String filename
)
{
ResetDocument
();
mFilename
= filename
;
// fill mLevelElements from file
if (!LevelUtil
.LoadLevel(mFilename,
this))
return false;
Owner
.GlView.Refresh();
Owner
.Text = Path
.GetFileName(mFilename
) + " - Burutaru Editor";
return true;
}
public bool SaveDocument
()
{
// save mLevelElements
if (!LevelUtil
.SaveLevel(mFilename,
this))
return false;
Owner
.Text = Path
.GetFileName(mFilename
) + " - Burutaru Editor";
SetDirty
(false);
return true;
}
public bool SaveDocument
(String filename
)
{
mFilename
= filename
;
return SaveDocument
();
}
public void CreateCheckPoint
(float x,
float y
)
{
float tx
; float ty
;
Owner
.View.TransformToWorldSpace(out tx,
out ty, x, y,
true);
CheckPoint cp
= new CheckPoint
(this,
"NewCheckPoint");
cp
.Position.x = tx
;
cp
.Position.y = ty
;
AddCheckPoint
(cp
);
}
public void AddCheckPoint
(CheckPoint checkPoint
)
{
mCheckPoints
.Add(checkPoint
);
Owner
.CheckPoints.Items.Add(checkPoint
);
Owner
.CheckPoints.SelectedIndex = Owner
.CheckPoints.Items.Count - 1;
}
public void CreateEnemySpawner
(float x,
float y
)
{
float tx
; float ty
;
Owner
.View.TransformToWorldSpace(out tx,
out ty, x, y
);
string className
= Owner
.CurrentSpawnerType;
Type t
= Type
.GetType("BurutaruEditor.file.enemies." + className
+ "Spawner");
Object[] args
= { this,
"New" + className
};
EnemySpawner sp
= (EnemySpawner
)Activator
.CreateInstance(t, args
);
sp
.SpawnTrigger.x = tx
- View
.CurrentPosition.x;
sp
.SpawnTrigger.y = ty
- View
.CurrentPosition.y;
AddEnemySpawner
(sp
);
/*Type t = Type.GetType("TestApp.Entry");
Object[] args = { "emo", false };
Object o = Activator.CreateInstance(t, args);*/
}
public void AddEnemySpawner
(EnemySpawner spawner
)
{
mSpawners
.Add(spawner
);
Owner
.Spawners.Items.Add(spawner
);
Owner
.Spawners.SelectedIndex = Owner
.Spawners.Items.Count - 1;
}
public void CreateScripting
(float x,
float y
)
{
float tx
; float ty
;
Owner
.View.TransformToWorldSpace(out tx,
out ty, x, y
);
string className
= Owner
.CurrentScriptingType;
Type t
= Type
.GetType("BurutaruEditor.file.conditions." + className
);
Object[] args
= { this,
"New" + className
};
BaseCondition bc
= (BaseCondition
)Activator
.CreateInstance(t, args
);
bc
.Position.x = tx
- View
.CurrentPosition.x;
bc
.Position.y = ty
- View
.CurrentPosition.y;
AddScripting
(bc
);
}
public void AddScripting
(BaseCondition condition
)
{
mScripting
.Add(condition
);
Owner
.Scripting.Items.Add(condition
);
Owner
.Scripting.SelectedIndex = Owner
.Scripting.Items.Count - 1;
}
public void CreateLevelElement
(float x,
float y,
string filename
)
{
float tx
; float ty
;
Owner
.View.TransformToWorldSpace(out tx,
out ty, x, y
);
string className
= Owner
.CurrentElementType;
if (className
.Equals("BackgroundElement"))
{
BackgroundElement e
= new BackgroundElement
(this,
"NewBackgroundElement");
e
.TextureFile = filename
;
e
.Transform.x = tx
- View
.CurrentPosition.x;
e
.Transform.y = ty
- View
.CurrentPosition.y;
e
.Transform.w = e
.Sprite.getSprite().getTextureWidth();
e
.Transform.h = e
.Sprite.getSprite().getTextureHeight();
e
.Transform.centerPivot();
AddLevelElement
(e
);
}
else if (className
.Equals("StaticElement"))
{
StaticElement e
= new StaticElement
(this,
"NewStaticElement");
e
.TextureFile = filename
;
e
.Transform.x = tx
- View
.CurrentPosition.x;
e
.Transform.y = ty
- View
.CurrentPosition.y;
e
.Transform.w = e
.Sprite.getSprite().getTextureWidth();
e
.Transform.h = e
.Sprite.getSprite().getTextureHeight();
e
.Transform.centerPivot();
AddLevelElement
(e
);
}
/*Type t = Type.GetType("BurutaruEditor.file.enemies." + className + "Spawner");
Object[] args = { this, "New" + className };
EnemySpawner sp = (EnemySpawner)Activator.CreateInstance(t, args);
sp.SpawnTrigger.x = tx;
sp.SpawnTrigger.y = ty;
AddEnemySpawner(sp);*/
}
public LevelElement FindLevelElement
(string name
)
{
for (int i
= 0; i
< mLevelElements
.Count; i
++)
{
LevelElement e
= mLevelElements
.ElementAt(i
);
if (e
.Name.Equals(name, StringComparison
.OrdinalIgnoreCase))
return e
;
}
return null;
}
public void AddLevelElement
(LevelElement element
)
{
mLevelElements
.Add(element
);
Owner
.LevelElements.Items.Add(element
);
Owner
.LevelElements.SelectedIndex = Owner
.LevelElements.Items.Count - 1;
}
public void DeleteCurrentObject
()
{
if (Owner
.CurrentObjectMode == MainForm
.ObjectMode.CHECKPOINTS)
{
int i
= Owner
.CheckPoints.SelectedIndex;
Owner
.CheckPoints.Items.RemoveAt(i
);
mCheckPoints
.RemoveAt(i
);
Owner
.CheckPoints.SelectedIndex = Math
.Min(i
-1, Owner
.CheckPoints.Items.Count - 1);
}
else if (Owner
.CurrentObjectMode == MainForm
.ObjectMode.SPAWNERS)
{
int i
= Owner
.Spawners.SelectedIndex;
Owner
.Spawners.Items.RemoveAt(i
);
mSpawners
.RemoveAt(i
);
Owner
.Spawners.SelectedIndex = Math
.Min(i
-1, Owner
.Spawners.Items.Count - 1);
}
else if (Owner
.CurrentObjectMode == MainForm
.ObjectMode.ELEMENTS)
{
int i
= Owner
.LevelElements.SelectedIndex;
Owner
.LevelElements.Items.RemoveAt(i
);
mLevelElements
.RemoveAt(i
);
Owner
.LevelElements.SelectedIndex = Math
.Min(i
-1, Owner
.LevelElements.Items.Count - 1);
}
else if (Owner
.CurrentObjectMode == MainForm
.ObjectMode.SCRIPTING)
{
int i
= Owner
.Scripting.SelectedIndex;
Owner
.Scripting.Items.RemoveAt(i
);
mScripting
.RemoveAt(i
);
Owner
.Scripting.SelectedIndex = Math
.Min(i
- 1, Owner
.Scripting.Items.Count - 1);
}
}
public void MoveObjectTo
<T
>(List
<T
> objList,
System.Windows.Forms.ListBox lb,
int fromIndex,
int toIndex
)
{
if ((fromIndex
< 0) || (fromIndex
>= objList
.Count) ||
(toIndex
< 0) || (toIndex
>= objList
.Count))
return;
T cp
= objList
.ElementAt(fromIndex
);
objList
.RemoveAt(fromIndex
);
objList
.Insert(toIndex, cp
);
lb
.Items.RemoveAt(fromIndex
);
lb
.Items.Insert(toIndex, cp
);
lb
.SelectedIndex = toIndex
;
}
public void MoveObjectUp
()
{
switch (Owner
.CurrentObjectMode)
{
case MainForm
.ObjectMode.CHECKPOINTS:
MoveObjectTo
(mCheckPoints, Owner
.CheckPoints, Owner
.CheckPoints.SelectedIndex, Owner
.CheckPoints.SelectedIndex - 1);
break;
case MainForm
.ObjectMode.SPAWNERS:
MoveObjectTo
(mSpawners, Owner
.Spawners, Owner
.Spawners.SelectedIndex, Owner
.Spawners.SelectedIndex - 1);
break;
case MainForm
.ObjectMode.ELEMENTS:
MoveObjectTo
(mLevelElements, Owner
.LevelElements, Owner
.LevelElements.SelectedIndex, Owner
.LevelElements.SelectedIndex - 1);
break;
case MainForm
.ObjectMode.SCRIPTING:
MoveObjectTo
(mScripting, Owner
.Scripting, Owner
.Scripting.SelectedIndex, Owner
.Scripting.SelectedIndex - 1);
break;
}
}
public void MoveObjectDown
()
{
switch (Owner
.CurrentObjectMode)
{
case MainForm
.ObjectMode.CHECKPOINTS:
MoveObjectTo
(mCheckPoints, Owner
.CheckPoints, Owner
.CheckPoints.SelectedIndex, Owner
.CheckPoints.SelectedIndex + 1);
break;
case MainForm
.ObjectMode.SPAWNERS:
MoveObjectTo
(mSpawners, Owner
.Spawners, Owner
.Spawners.SelectedIndex, Owner
.Spawners.SelectedIndex + 1);
break;
case MainForm
.ObjectMode.ELEMENTS:
MoveObjectTo
(mLevelElements, Owner
.LevelElements, Owner
.LevelElements.SelectedIndex, Owner
.LevelElements.SelectedIndex + 1);
break;
case MainForm
.ObjectMode.SCRIPTING:
MoveObjectTo
(mScripting, Owner
.Scripting, Owner
.Scripting.SelectedIndex, Owner
.Scripting.SelectedIndex + 1);
break;
}
}
public void Cut
()
{
if (View
.SelectedObject != null)
{
mCopyPasteBuffer
= LevelUtil
.GetObjectData(View
.SelectedObject,
"Copy");
mPasteOffset
= PASTE_OFFSET
;
// TODO: remove selected object
}
}
public void Copy
()
{
if (View
.SelectedObject != null)
{
mCopyPasteBuffer
= LevelUtil
.GetObjectData(View
.SelectedObject,
"Copy");
mPasteOffset
= PASTE_OFFSET
;
}
}
public void Paste
()
{
if (mCopyPasteBuffer
!= null)
{
System.Console.WriteLine(mCopyPasteBuffer
);
LevelUtil
.CreateObjectFromString(this, mCopyPasteBuffer
);
View
.SelectedObject.MoveBy(mPasteOffset, mPasteOffset,
-1);
mPasteOffset
+= PASTE_OFFSET
;
}
}
//=================================================================================================================================
public string GetPropertiesTitle
()
{
return "Level Parameters";
}
public string GetPropertiesType
()
{
return "";
}
public void SetDirty
(bool dirty
)
{
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 void WriteData
(TextWriter tw
)
{
LevelUtil
.WriteProperty(tw,
"name", LevelTitle
);
LevelUtil
.WriteProperty(tw,
"stage", StageTitle
);
LevelUtil
.WriteProperty(tw,
"startPosition", mStartPosition
);
LevelUtil
.WriteProperty(tw,
"warpIn", WarpIn
);
LevelUtil
.WriteProperty(tw,
"starFieldStatus",
(int)StarFieldStatus
);
/*tw.WriteLine("name \"" + LevelTitle + "\";");
tw.WriteLine("stage \"" + StageTitle + "\";");
tw.WriteLine("startPosition " + mStartPosition.x + ", " + mStartPosition.y + ";");
tw.WriteLine("warpIn " + (WarpIn ? "true" : "false") + ";");
tw.WriteLine("\tstarFieldStatus " + (int)StarFieldStatus + ";");*/
}
public bool ReadParameter
(Tokenizer t,
string id
)
{
if (id
.Equals("name", StringComparison
.OrdinalIgnoreCase))
{
LevelTitle
= t
.readString();
}
else if (id
.Equals("stage", StringComparison
.OrdinalIgnoreCase))
{
StageTitle
= t
.readString();
}
else if (id
.Equals("startPosition", StringComparison
.OrdinalIgnoreCase))
{
mStartPosition
= ParseUtil
.readVector2(t
);
}
else if (id
.Equals("warpIn", StringComparison
.OrdinalIgnoreCase))
{
WarpIn
= t
.readIdentifier().Equals("true", StringComparison
.OrdinalIgnoreCase);
}
else if (id
.Equals("starFieldStatus", StringComparison
.OrdinalIgnoreCase))
{
int status
= (int)t
.readNumber();
StarFieldStatus
= (CheckPoint
.StarFieldControlStatus)status
;
}
else
{
// unsupported id
return false;
}
t
.readToken(";");
return true;
}
}
}