using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using Tao.OpenGl;
using BauzoidNET.app;
using BurutaruEditor.file;
using BurutaruEditor.file.elements;
using BurutaruEditor.view;
using BurutaruEditor.interaction;
namespace BurutaruEditor
{
public partial class MainForm
: Form
{
public enum InteractionMode
{
SELECT,
CREATE
}
public enum ObjectMode
{
CHECKPOINTS,
SPAWNERS,
ELEMENTS
}
//===============================================================================================================
public static BauzoidApp App
= null;
private Document mDocument
= null;
public Document Doc
{ get
{ return mDocument
; } }
private DocumentView mView
= null;
public DocumentView View
{ get
{ return mView
; } }
private InteractionMode mInteractionMode
= InteractionMode
.SELECT;
public InteractionMode CurrentInteraction
{
get
{ return mInteractionMode
; }
set
{
if (value
== InteractionMode
.SELECT)
{
EnableCreateButton
(false);
}
else if (value
== InteractionMode
.CREATE)
{
EnableCreateButton
(true);
}
mInteractionMode
= value
;
UpdateStatusBar
();
}
}
private ObjectMode mObjectMode
= ObjectMode
.CHECKPOINTS;
public ObjectMode CurrentObjectMode
{
get
{ return mObjectMode
; }
set
{
switch (value
)
{
case ObjectMode
.CHECKPOINTS:
tbcObjects
.SelectedTab = tabCheckPoints
;
break;
case ObjectMode
.SPAWNERS:
tbcObjects
.SelectedTab = tabSpawners
;
break;
case ObjectMode
.ELEMENTS:
tbcObjects
.SelectedTab = tabElements
;
break;
}
mObjectMode
= value
;
UpdateStatusBar
();
}
}
#pragma warning disable 0618
public Tao
.Platform.Windows.SimpleOpenGlControl GlView
{ get
{ return mGlView
; } }
#pragma warning restore 0618
public ListBox CheckPoints
{ get
{ return lbCheckPoints
; } }
public ListBox Spawners
{ get
{ return lbSpawners
; } }
public ListBox LevelElements
{ get
{ return lbElements
; } }
public PropertyGrid Properties
{ get
{ return pgProperties
; } }
public bool SnapToPath
{ get
; set
; }
private Panning mPanning
= null;
private SelectMove mSelectMode
= null;
private CreateObject mCreateObjectMode
= null;
private Bitmap
[] mLayerBitmaps
= new Bitmap
[LevelElement
.NUM_LAYERS];
//===============================================================================================================
public MainForm
()
{
InitializeComponent
();
mGlView
.MouseWheel += new MouseEventHandler
(GlView_MouseWheel
);
lbCheckPoints
.DisplayMember = "Name";
lbSpawners
.DisplayMember = "Name";
SetSnapToPath
(false);
CurrentObjectMode
= ObjectMode
.CHECKPOINTS;
cbSpawnerTypes
.SelectedIndex = 0;
cbElementTypes
.SelectedIndex = 0;
}
private void MainForm_Load
(object sender, EventArgs e
)
{
App
= new BauzoidApp
();
App
.init(mGlView
.Width, mGlView
.Height);
String s
= Path
.Combine(Application
.StartupPath,
@"..\\..\\..\\..\\..\\BauzoidApps\\BurutaruZ_v2\\burutaru-android\\assets\\data\\levels");
openFileDialog
.InitialDirectory = Path
.GetFullPath(s
);
saveFileDialog
.InitialDirectory = Path
.GetFullPath(s
);
mDocument
= new Document
(this);
mView
= new DocumentView
(this);
mView
.Init();
mPanning
= new Panning
(this);
mSelectMode
= new SelectMove
(this);
mCreateObjectMode
= new CreateObject
(this);
mLayerBitmaps
[LevelElement
.BACKGROUND_LAYER1] = new Bitmap
(BurutaruEditor
.Properties.Resources.layerBG1);
mLayerBitmaps
[LevelElement
.BACKGROUND_LAYER2] = new Bitmap
(BurutaruEditor
.Properties.Resources.layerBG2);
mLayerBitmaps
[LevelElement
.MAIN_LAYER] = new Bitmap
(BurutaruEditor
.Properties.Resources.layerMain);
mLayerBitmaps
[LevelElement
.FOREGROUND_LAYER] = new Bitmap
(BurutaruEditor
.Properties.Resources.layerFG);
pgProperties
.SelectedObject = Doc
;
pgProperties
.ExpandAllGridItems();
}
private void MainForm_FormClosing
(object sender, FormClosingEventArgs e
)
{
if (!CheckDocumentModifiedAndSave
())
{
e
.Cancel = true;
return;
}
if (mView
!= null)
{
mView
.Exit();
mView
= null;
}
if (mDocument
!= null)
{
mDocument
.Exit();
mDocument
= null;
}
if (App
!= null)
App
.exit();
}
private void GlView_Paint
(object sender, PaintEventArgs e
)
{
App
.getGraphics().clear(0
.3f, 0
.3f, 0
.35f,
0);
App
.getRenderStates().projection.setOrtho(
0
.0f,
App
.getGraphics().getWidth(),
App
.getGraphics().getHeight(),
0
.0f,
0
.0f,
1
.0f
);
mView
.Render();
}
public void RenderInteraction
()
{
if (CurrentInteraction
== InteractionMode
.SELECT)
mSelectMode
.Render();
else if (CurrentInteraction
== InteractionMode
.CREATE)
mCreateObjectMode
.Render();
mPanning
.Render();
}
private void GlView_Resize
(object sender, EventArgs e
)
{
if (App
!= null)
App
.getGraphics().updateSurfaceDimensions(mGlView
.Width, mGlView
.Height);
}
private void FileNew_Click
(object sender, EventArgs e
)
{
if (!CheckDocumentModifiedAndSave
())
return;
// new file
mDocument
.NewDocument();
}
private void FileOpen_Click
(object sender, EventArgs e
)
{
if (!CheckDocumentModifiedAndSave
())
return;
// open file
PerformFileOpen
();
}
/** Check if the document has been modified, and if yes, ask for saving.
* Returns true if everything is ok and the operation should be continued, or false when Cancel has been pressed. */
private bool CheckDocumentModifiedAndSave
()
{
if (mDocument
.IsDirty())
{
switch (MessageBox
.Show("File has been modified. Save?",
"New File", MessageBoxButtons
.YesNoCancel, MessageBoxIcon
.Question))
{
case DialogResult
.Yes:
if (!PerformFileSave
())
return false;
break;
case DialogResult
.No:
break;
case DialogResult
.Cancel:
return false;
}
}
return true;
}
/** Perform a file save operation with a file save dialog.
* Returns false if the operation has been canceled. */
private bool PerformFileSave
(bool alwaysShowDialog
= false)
{
if (alwaysShowDialog
|| !mDocument
.IsFilenameSet())
{
if (saveFileDialog
.ShowDialog() == DialogResult
.Cancel)
return false;
mDocument
.SetFilename(saveFileDialog
.FileName);
}
return mDocument
.SaveDocument();
}
/** Perform a file open operation.
* Return false if the operation was canceled. */
private bool PerformFileOpen
()
{
if (openFileDialog
.ShowDialog() == DialogResult
.Cancel)
return false;
return mDocument
.LoadDocument(openFileDialog
.FileName);
}
private void FileSave_Click
(object sender, EventArgs e
)
{
PerformFileSave
();
}
private void FileSaveAs_Click
(object sender, EventArgs e
)
{
PerformFileSave
(true);
}
private void FileExit_Click
(object sender, EventArgs e
)
{
Close
();
}
private void GlView_MouseDown
(object sender, MouseEventArgs e
)
{
if (CurrentInteraction
== InteractionMode
.SELECT)
mSelectMode
.MouseDown(e
);
else if (CurrentInteraction
== InteractionMode
.CREATE)
mCreateObjectMode
.MouseDown(e
);
mPanning
.MouseDown(e
);
mGlView
.Refresh();
}
private void GlView_MouseMove
(object sender, MouseEventArgs e
)
{
if (CurrentInteraction
== InteractionMode
.SELECT)
mSelectMode
.MouseMove(e
);
else if (CurrentInteraction
== InteractionMode
.CREATE)
mCreateObjectMode
.MouseMove(e
);
mPanning
.MouseMove(e
);
mGlView
.Refresh();
}
private void GlView_MouseUp
(object sender, MouseEventArgs e
)
{
if (CurrentInteraction
== InteractionMode
.SELECT)
mSelectMode
.MouseUp(e
);
else if (CurrentInteraction
== InteractionMode
.CREATE)
mCreateObjectMode
.MouseUp(e
);
mPanning
.MouseUp(e
);
mGlView
.Refresh();
}
private void GlView_MouseWheel
(object sender, MouseEventArgs e
)
{
if (e
.Delta > 0)
{
View
.ZoomFactor *= 1
.2f
;
}
else if (e
.Delta < 0)
{
View
.ZoomFactor *= 0
.8f
;
}
}
private void ObjectsList_SelIndexChanged
(object sender, EventArgs e
)
{
ListBox lb
= null;
switch (CurrentObjectMode
)
{
case ObjectMode
.CHECKPOINTS:
lb
= lbCheckPoints
;
break;
case ObjectMode
.SPAWNERS:
lb
= lbSpawners
;
break;
case ObjectMode
.ELEMENTS:
lb
= lbElements
;
break;
}
if (lb
.SelectedIndex == -1)
return;
pgProperties
.SelectedObject = lb
.SelectedItem;
View
.SelectedObject = (LevelObject
)lb
.SelectedItem;
View
.SelectedObjectPart = SelectMove
.MOVE_ALL;
pgProperties
.ExpandAllGridItems();
mGlView
.Refresh();
}
private void Properties_PropertyValueChanged
(object s, PropertyValueChangedEventArgs e
)
{
mGlView
.Refresh();
typeof(ListBox
).InvokeMember("RefreshItems", BindingFlags
.NonPublic | BindingFlags
.Instance | BindingFlags
.InvokeMethod,
null,
CheckPoints,
new object[] { });
typeof(ListBox
).InvokeMember("RefreshItems", BindingFlags
.NonPublic | BindingFlags
.Instance | BindingFlags
.InvokeMethod,
null,
Spawners,
new object[] { });
typeof(ListBox
).InvokeMember("RefreshItems", BindingFlags
.NonPublic | BindingFlags
.Instance | BindingFlags
.InvokeMethod,
null,
LevelElements,
new object[] { });
}
public void SetSnapToPath
(bool snap
= true)
{
SnapToPath
= snap
;
tbSnapToPath
.Checked = snap
;
mnuSnapToPath
.Checked = snap
;
}
private void SnapToPath_Click
(object sender, EventArgs e
)
{
SetSnapToPath
(!SnapToPath
);
mGlView
.Refresh();
}
private void EnableCreateButton
(bool enable
)
{
btnCreateCheckPoint
.Checked = enable
&& (CurrentObjectMode
== ObjectMode
.CHECKPOINTS);
btnCreateSpawner
.Checked = enable
&& (CurrentObjectMode
== ObjectMode
.SPAWNERS);
btnCreateElement
.Checked = enable
&& (CurrentObjectMode
== ObjectMode
.ELEMENTS);
}
private void Create_Click
(object sender, EventArgs e
)
{
if (((CheckBox
)sender
).Checked)
CurrentInteraction
= InteractionMode
.CREATE;
else
CurrentInteraction
= InteractionMode
.SELECT;
}
public void UpdateStatusBar
()
{
string text
= "";
switch (CurrentInteraction
)
{
case InteractionMode
.SELECT:
text
= "Select ";
break;
case InteractionMode
.CREATE:
text
= "Create ";
break;
}
switch (CurrentObjectMode
)
{
case ObjectMode
.CHECKPOINTS:
text
+= "CheckPoint";
break;
case ObjectMode
.SPAWNERS:
text
+= "EnemySpawner";
break;
case ObjectMode
.ELEMENTS:
text
+= "LevelElement";
break;
}
stInteractionObjectMode
.Text = text
;
if (View
!= null)
stZoom
.Text = ((int)Math
.Floor(View
.ZoomFactor * 100)) + "%";
mStatusStrip
.Refresh();
}
private void Objects_SelectedTabChanged
(object sender, EventArgs e
)
{
mObjectMode
= (ObjectMode
)tbcObjects
.SelectedIndex;
CurrentInteraction
= InteractionMode
.SELECT;
switch (CurrentObjectMode
)
{
case ObjectMode
.CHECKPOINTS:
ObjectsList_SelIndexChanged
(lbCheckPoints,
new EventArgs
());
break;
case ObjectMode
.SPAWNERS:
ObjectsList_SelIndexChanged
(lbSpawners,
new EventArgs
());
break;
case ObjectMode
.ELEMENTS:
ObjectsList_SelIndexChanged
(lbElements,
new EventArgs
());
break;
}
}
private void MainForm_KeyDown
(object sender, KeyEventArgs e
)
{
// implement shortcut keys
if (e
.KeyCode == Keys
.S)
{
// snap
SetSnapToPath
(!SnapToPath
);
mGlView
.Refresh();
}
}
private void MoveUp_Click
(object sender, EventArgs e
)
{
Doc
.MoveObjectUp();
mGlView
.Refresh();
}
private void MoveDown_Click
(object sender, EventArgs e
)
{
Doc
.MoveObjectDown();
mGlView
.Refresh();
}
private void Elements_DrawItem
(object sender, DrawItemEventArgs e
)
{
e
.DrawBackground();
LevelElement element
= (LevelElement
)LevelElements
.Items[e
.Index];
/*Brush myBrush = Brushes.Black;
// Determine the color of the brush to draw each item based
// on the index of the item to draw.
switch (e.Index)
{
case 0:
myBrush = Brushes.Red;
break;
case 1:
myBrush = Brushes.Orange;
break;
case 2:
myBrush = Brushes.Purple;
break;
}*/
Bitmap bm
= null;
switch (element
.Layer)
{
case LevelElement
.BACKGROUND_LAYER1:
case LevelElement
.BACKGROUND_LAYER2:
case LevelElement
.MAIN_LAYER:
case LevelElement
.FOREGROUND_LAYER:
bm
= mLayerBitmaps
[element
.Layer];
break;
}
Rectangle r
= e
.Bounds;
r
.X += 2;
r
.Width = 16;
r
.Height = 16;
e
.Graphics.DrawImageUnscaled(bm, r
);
// Draw the current item text based on the current Font
// and the custom brush settings.
r
= e
.Bounds;
r
.X += 20;
e
.Graphics.DrawString(element
.Name, e
.Font, SystemBrushes
.ControlText, r, StringFormat
.GenericDefault);
// If the ListBox has focus, draw a focus rectangle around the selected item.
e
.DrawFocusRectangle();
}
}
}