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 BauzoidNET.graphics;
using BauzoidNET.graphics.renderstates;
using BauzoidNET.graphics.sprite;
using BauzoidNET.math;
using ShapeEditor.file;
using ShapeEditor.interaction;
namespace ShapeEditor
{
public partial class MainForm
: Form
{
public static BauzoidApp App
= null;
public const int GRID_SIZE
= 32;
public static readonly Vector4 GRID_COLOR
= new Vector4
(0
.3f, 0
.4f, 0
.3f,
1);
private Document mDocument
= null;
public Document CurrentDoc
{ get
{ return mDocument
; } }
private float mZoomFactor
= 1
.0f
;
public float ZoomFactor
{
get
{ return mZoomFactor
; }
set
{
mZoomFactor
= value
;
mGlView
.Invalidate();
}
}
public float mCurrentX
= 0
.0f
;
public float mCurrentY
= 0
.0f
;
public ListBox ElementsListBox
{ get
{ return lbElements
; } }
#pragma warning disable 0618
public Tao
.Platform.Windows.SimpleOpenGlControl GlView
{ get
{ return mGlView
; } }
#pragma warning restore 0618
private InteractionMode mInteractionMode
= null;
public InteractionMode Interaction
{ get
{ return mInteractionMode
; } }
private PanInteraction mPanInteraction
= null;
public MainForm
()
{
InitializeComponent
();
mGlView
.MouseWheel += new MouseEventHandler
(GlView_MouseWheel
);
lbElements
.DisplayMember = "ShapeName";
}
private void MainForm_Load
(object sender, EventArgs e
)
{
App
= new BauzoidApp
();
App
.init(mGlView
.Width, mGlView
.Height);
String s
= Path
.Combine(Application
.StartupPath,
@".\\assets\\data\\textures");
openFileDialog
.InitialDirectory = Path
.GetFullPath(s
);
openSpriteDialog
.InitialDirectory = Path
.GetFullPath(s
);
saveFileDialog
.InitialDirectory = Path
.GetFullPath(s
);
mDocument
= new Document
(this);
mDocument
.Init();
mDocument
.NewDocument();
SetInteractionMode
(InteractionMode
.RECTANGLE);
mPanInteraction
= new PanInteraction
(this);
}
private void MainForm_FormClosing
(object sender, FormClosingEventArgs e
)
{
if (!CheckDocumentModifiedAndSave
())
{
e
.Cancel = true;
return;
}
if (App
!= null)
App
.exit();
mDocument
.Exit();
mDocument
= null;
}
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
);
float posX
= App
.getGraphics().getWidth() / 2 + mCurrentX
;
float posY
= App
.getGraphics().getHeight() / 2 + mCurrentY
;
RenderGrid
(posX, posY
);
// render sprite
SpriteInstance s
= mDocument
.GetSpriteInstance();
s
.transform.w = s
.getSprite().getTextureWidth() * s
.getSpriteRegion().getWidth() * ZoomFactor
;
s
.transform.h = s
.getSprite().getTextureHeight() * s
.getSpriteRegion().getHeight() * ZoomFactor
;
s
.transform.x = posX
- s
.transform.w/2;
s
.transform.y = posY
- s
.transform.h / 2;
s
.transform.pivotX = 0;
s
.transform.pivotY = 0;
s
.render();
mDocument
.Render();
mInteractionMode
.Render();
}
private void RenderGrid
(float posX,
float posY
)
{
// render grid
float x
= posX
% (GRID_SIZE
* ZoomFactor
);
while (x
< App
.getGraphics().getWidth())
{
RenderUtil
.drawLine(App
.getGraphics(), x,
0, x, App
.getGraphics().getHeight(), GRID_COLOR
);
x
+= GRID_SIZE
* ZoomFactor
;
}
float y
= posY
% (GRID_SIZE
* ZoomFactor
);
while (y
< App
.getGraphics().getHeight())
{
RenderUtil
.drawLine(App
.getGraphics(),
0, y, App
.getGraphics().getWidth(), y, GRID_COLOR
);
y
+= GRID_SIZE
* ZoomFactor
;
}
}
private void GlView_Resize
(object sender, EventArgs e
)
{
if (App
!= null)
App
.getGraphics().updateSurfaceDimensions((int)(mGlView
.Width),
(int)(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(this,
"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())
{
saveFileDialog
.FileName = mDocument
.GetFilename();
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 ChangeSprite_Click
(object sender, EventArgs e
)
{
if (openSpriteDialog
.ShowDialog() == DialogResult
.OK)
{
mDocument
.ChangeSprite(openSpriteDialog
.FileName);
mGlView
.Invalidate();
}
}
private void StatusBarUpdate_Enter
(object sender, EventArgs e
)
{
if (sender
is MenuItem
)
stStatusLabel
.Text = ((MenuItem
)sender
).Text;
else if (sender
is ToolStripItem
)
stStatusLabel
.Text = ((ToolStripItem
)sender
).Text;
mStatusStrip
.Refresh();
}
private void StatusBarUpdate_Leave
(object sender, EventArgs e
)
{
stStatusLabel
.Text = "Ready.";
mStatusStrip
.Refresh();
}
private void GlView_MouseWheel
(object sender, MouseEventArgs e
)
{
if (e
.Delta > 0)
{
SetZoom
(mZoomFactor
* 1
.2f
);
}
else if (e
.Delta < 0)
{
SetZoom
(mZoomFactor
* 0
.8f
);
}
}
private void SetZoom
(float zoomFactor
)
{
mZoomFactor
= zoomFactor
;
if (mZoomFactor
< 0
.1f
)
mZoomFactor
= 0
.1f
;
mGlView
.Invalidate();
GlView_Resize
(mGlView,
new EventArgs
());
int z
= (int)Math
.Floor(mZoomFactor
* 100);
stZoomFactor
.Text = z
+ "%";
mStatusStrip
.Refresh();
}
private void UpdateToolButtonCheckedState
()
{
tbSelect
.Checked = Interaction
.Mode == InteractionMode
.SELECT;
tbRectangle
.Checked = Interaction
.Mode == InteractionMode
.RECTANGLE;
tbEllipse
.Checked = Interaction
.Mode == InteractionMode
.ELLIPSE;
tbPolygon
.Checked = Interaction
.Mode == InteractionMode
.POLYGON;
}
private void SetInteractionMode
(int mode
)
{
switch (mode
)
{
case InteractionMode
.SELECT:
mInteractionMode
= InteractionMode
.CreateMode(this, InteractionMode
.SELECT);
mGlView
.Cursor = Cursors
.Arrow;
break;
case InteractionMode
.RECTANGLE:
mInteractionMode
= InteractionMode
.CreateMode(this, InteractionMode
.RECTANGLE);
mGlView
.Cursor = Cursors
.Cross;
break;
case InteractionMode
.ELLIPSE:
mInteractionMode
= InteractionMode
.CreateMode(this, InteractionMode
.ELLIPSE);
mGlView
.Cursor = Cursors
.Cross;
break;
case InteractionMode
.POLYGON:
mInteractionMode
= InteractionMode
.CreateMode(this, InteractionMode
.POLYGON);
mGlView
.Cursor = Cursors
.Cross;
break;
}
UpdateToolButtonCheckedState
();
mGlView
.Invalidate();
}
private void Tool_Click
(object sender, EventArgs e
)
{
if (sender
== tbSelect
)
SetInteractionMode
(InteractionMode
.SELECT);
else if (sender
== tbRectangle
)
SetInteractionMode
(InteractionMode
.RECTANGLE);
else if (sender
== tbEllipse
)
SetInteractionMode
(InteractionMode
.ELLIPSE);
else if (sender
== tbPolygon
)
SetInteractionMode
(InteractionMode
.POLYGON);
}
private void GlView_DoubleClick
(object sender, EventArgs e
)
{
/*mInteractionMode.DoubleClick(e);
mPanInteraction.DoubleClick(e);
mGlView.Invalidate();*/
}
private void GlView_MouseDown
(object sender, MouseEventArgs e
)
{
//mInteractionMode.MouseDown(e);
mInteractionMode
.MouseMove(e,
true);
mPanInteraction
.MouseMove(e,
true);
mGlView
.Invalidate();
}
private void GlView_MouseMove
(object sender, MouseEventArgs e
)
{
mInteractionMode
.MouseMove(e
);
mPanInteraction
.MouseMove(e
);
mGlView
.Refresh();
pgProperties
.Refresh();
}
private void GlView_MouseUp
(object sender, MouseEventArgs e
)
{
mInteractionMode
.MouseUp(e
);
mPanInteraction
.MouseUp(e
);
mGlView
.Refresh();
}
public void PanBy
(int dx,
int dy
)
{
mCurrentX
+= dx
;
mCurrentY
+= dy
;
}
private void Elements_SelectedIndexChanged
(object sender, EventArgs e
)
{
pgProperties
.SelectedObject = mDocument
.GetCurrentShape();
mGlView
.Invalidate();
//pgProperties.SelectedObject = mDocument.GetCurrentShape();
}
private void About_Click
(object sender, EventArgs e
)
{
AboutBox dlg
= new AboutBox
();
dlg
.ShowDialog();
}
private void DeleteShape_Click
(object sender, EventArgs e
)
{
mDocument
.DeleteCurrent();
}
private void MoveUp_Click
(object sender, EventArgs e
)
{
mDocument
.MoveUp();
}
private void MoveDown_Click
(object sender, EventArgs e
)
{
mDocument
.MoveDown();
}
private void Properties_ValueChanged
(object s, PropertyValueChangedEventArgs e
)
{
mGlView
.Refresh();
}
private void ZoomIn_Click
(object sender, EventArgs e
)
{
SetZoom
(mZoomFactor
* 1
.2f
);
}
private void ZoomOut_Click
(object sender, EventArgs e
)
{
SetZoom
(mZoomFactor
* 0
.8f
);
}
private void ZoomReset_Click
(object sender, EventArgs e
)
{
SetZoom
(1
.0f
);
}
private void PrevFrame_Click
(object sender, EventArgs e
)
{
mDocument
.CurrentFrame = mDocument
.CurrentFrame - 1;
}
private void NextFrame_Click
(object sender, EventArgs e
)
{
mDocument
.CurrentFrame = mDocument
.CurrentFrame + 1;
}
}
}