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 Tao.OpenGl;
using BauzoidNET.app;
using BurutaruEditor.file;
using BurutaruEditor.view;
using BurutaruEditor.interaction;
namespace BurutaruEditor
{
public partial class MainForm
: Form
{
public enum InteractionMode
{
SELECT,
CREATE
}
public enum ObjectMode
{
CHECKPOINTS,
SPAWNER,
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
.SPAWNER:
tbcObjects
.SelectedTab = tabSpawners
;
break;
case ObjectMode
.ELEMENTS:
tbcObjects
.SelectedTab = tabElements
;
break;
}
mObjectMode
= value
;
UpdateStatusBar
();
}
}
public Tao
.Platform.Windows.SimpleOpenGlControl GlView
{ get
{ return mGlView
; } }
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;
//===============================================================================================================
public MainForm
()
{
InitializeComponent
();
mGlView
.MouseWheel += new MouseEventHandler
(GlView_MouseWheel
);
lbCheckPoints
.DisplayMember = "Name";
lbSpawners
.DisplayMember = "Name";
SetSnapToPath
(false);
CurrentObjectMode
= ObjectMode
.CHECKPOINTS;
}
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);
}
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();
/*BauzoidNET.graphics.renderstates.RenderStates rs = mApp.getGraphics().renderStates;
rs.view.identity();
rs.model.identity();
mShader.activate();
{
mTextureHandle.set(0);
rs.getTextureStage(0).bindTexture(mOffscreen.getColorTexture());
rs.blending.setEnabled(false);
rs.culling.setEnabled(false);
rs.depthTest.setEnabled(false);
rs.activate();
{
mQuad.render();
}
rs.deactivate();
rs.popModelMatrix();
rs.popViewMatrix();
}
mShader.deactivate();*/
}
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.SELECT)
mPanning
.MouseDown(e
);
mGlView
.Refresh();
}
private void GlView_MouseMove
(object sender, MouseEventArgs e
)
{
if (CurrentInteraction
== InteractionMode
.SELECT)
mSelectMode
.MouseMove(e
);
mPanning
.MouseMove(e
);
mGlView
.Refresh();
}
private void GlView_MouseUp
(object sender, MouseEventArgs e
)
{
if (CurrentInteraction
== InteractionMode
.SELECT)
mSelectMode
.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
)
{
pgProperties
.SelectedObject = ((ListBox
)sender
).SelectedItem;
pgProperties
.ExpandAllGridItems();
}
private void Properties_PropertyValueChanged
(object s, PropertyValueChangedEventArgs e
)
{
mGlView
.Refresh();
}
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
.SPAWNER);
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;
}
private 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
.SPAWNER:
text
+= "EnemySpawner";
break;
case ObjectMode
.ELEMENTS:
text
+= "LevelElement";
break;
}
stInteractionObjectMode
.Text = text
;
mStatusStrip
.Refresh();
}
private void Objects_SelectedTabChanged
(object sender, EventArgs e
)
{
mObjectMode
= (ObjectMode
)tbcObjects
.SelectedIndex;
CurrentInteraction
= InteractionMode
.SELECT;
}
}
}