Subversion Repositories AndroidProjects

Rev

Rev 1427 | Rev 1435 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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 static BauzoidApp App = null;

        private Document mDocument = null;
        public Document Doc { get { return mDocument; } }

        private DocumentView mView = null;
        public DocumentView View { get { return mView; } }

        public Tao.Platform.Windows.SimpleOpenGlControl GlView { get { return mGlView; } }
        public ListBox CheckPoints { get { return lbCheckPoints; } }
        public PropertyGrid Properties { get { return pgProperties; } }


        private Panning mPanning = null;

        //===============================================================================================================

        public MainForm()
        {
            InitializeComponent();

            mGlView.MouseWheel += new MouseEventHandler(GlView_MouseWheel);
            lbCheckPoints.DisplayMember = "Name";
        }

        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);
        }

        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 CheckPoints_SelIndexChanged(object sender, EventArgs e)
        {
            pgProperties.SelectedObject = lbCheckPoints.SelectedItem;
            pgProperties.ExpandAllGridItems();
        }

        private void GlView_MouseDown(object sender, MouseEventArgs e)
        {
            mPanning.MouseDown(e);
        }

        private void GlView_MouseMove(object sender, MouseEventArgs e)
        {
            mPanning.MouseMove(e);
        }

        private void GlView_MouseUp(object sender, MouseEventArgs e)
        {
            mPanning.MouseUp(e);
        }

        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;
            }
        }

    }
}