Subversion Repositories AndroidProjects

Rev

Rev 1489 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1386 chris 1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Linq;
7
using System.Text;
8
using System.Threading.Tasks;
9
using System.Windows.Forms;
1397 chris 10
using System.IO;
1386 chris 11
 
12
using Tao.OpenGl;
13
 
14
using BauzoidNET.app;
1400 chris 15
using BauzoidNET.graphics;
1408 chris 16
using BauzoidNET.graphics.renderstates;
1397 chris 17
using BauzoidNET.graphics.sprite;
1400 chris 18
using BauzoidNET.math;
1386 chris 19
 
20
using ShapeEditor.file;
1399 chris 21
using ShapeEditor.interaction;
1386 chris 22
 
23
namespace ShapeEditor
24
{
25
    public partial class MainForm : Form
26
    {
1397 chris 27
        public static BauzoidApp App = null;
1386 chris 28
 
1400 chris 29
        public const int GRID_SIZE = 32;
30
 
31
        public static readonly Vector4 GRID_COLOR = new Vector4(0.3f, 0.4f, 0.3f, 1);
32
 
1386 chris 33
        private Document mDocument = null;
1404 chris 34
        public Document CurrentDoc { get { return mDocument; } }
1386 chris 35
 
1399 chris 36
        private float mZoomFactor = 1.0f;
1401 chris 37
        public float ZoomFactor
38
        {
39
            get { return mZoomFactor; }
40
            set
41
            {
42
                mZoomFactor = value;
43
                mGlView.Invalidate();
44
            }
45
        }
46
 
1408 chris 47
        public float mCurrentX = 0.0f;
48
        public float mCurrentY = 0.0f;
49
 
1404 chris 50
        public ListBox ElementsListBox { get { return lbElements; } }
1451 chris 51
#pragma warning disable 0618
1404 chris 52
        public Tao.Platform.Windows.SimpleOpenGlControl GlView { get { return mGlView; } }
1451 chris 53
#pragma warning restore 0618
1402 chris 54
 
1399 chris 55
        private InteractionMode mInteractionMode = null;
1404 chris 56
        public InteractionMode Interaction { get { return mInteractionMode; } }
57
 
1402 chris 58
        private PanInteraction mPanInteraction = null;
1386 chris 59
 
60
        public MainForm()
61
        {
62
            InitializeComponent();
1399 chris 63
 
64
            mGlView.MouseWheel += new MouseEventHandler(GlView_MouseWheel);
1402 chris 65
            lbElements.DisplayMember = "ShapeName";
1386 chris 66
        }
67
 
68
        private void MainForm_Load(object sender, EventArgs e)
69
        {
1397 chris 70
            App = new BauzoidApp();
71
            App.init(mGlView.Width, mGlView.Height);
1386 chris 72
 
1489 chris 73
            String s = Path.Combine(Application.StartupPath, @".\\assets\\data\\textures");
1386 chris 74
            openFileDialog.InitialDirectory = Path.GetFullPath(s);
1398 chris 75
            openSpriteDialog.InitialDirectory = Path.GetFullPath(s);
1386 chris 76
            saveFileDialog.InitialDirectory = Path.GetFullPath(s);
77
 
1398 chris 78
            mDocument = new Document(this);
1402 chris 79
            mDocument.Init();
1397 chris 80
            mDocument.NewDocument();
1399 chris 81
 
1404 chris 82
            SetInteractionMode(InteractionMode.RECTANGLE);
1402 chris 83
            mPanInteraction = new PanInteraction(this);
1386 chris 84
        }
85
 
86
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
87
        {
88
            if (!CheckDocumentModifiedAndSave())
89
            {
90
                e.Cancel = true;
91
                return;
92
            }
93
 
1397 chris 94
            if (App != null)
95
                App.exit();
1386 chris 96
 
1402 chris 97
            mDocument.Exit();
1386 chris 98
            mDocument = null;
99
        }
100
 
101
        private void GlView_Paint(object sender, PaintEventArgs e)
102
        {
1397 chris 103
            App.getGraphics().clear(0.3f, 0.3f, 0.35f, 0);
104
            App.getRenderStates().projection.setOrtho(
1386 chris 105
                    0.0f,
1397 chris 106
                    App.getGraphics().getWidth(),
107
                    App.getGraphics().getHeight(),
1386 chris 108
                    0.0f,
109
                    0.0f,
110
                    1.0f
111
                );
112
 
1408 chris 113
            float posX = App.getGraphics().getWidth() / 2 + mCurrentX;
114
            float posY = App.getGraphics().getHeight() / 2 + mCurrentY;
1400 chris 115
 
116
            RenderGrid(posX, posY);
117
 
118
            // render sprite
1397 chris 119
            SpriteInstance s = mDocument.GetSpriteInstance();
1408 chris 120
            s.transform.w = s.getSprite().getTextureWidth() * s.getSpriteRegion().getWidth() * ZoomFactor;
121
            s.transform.h = s.getSprite().getTextureHeight() * s.getSpriteRegion().getHeight() * ZoomFactor;
122
            s.transform.x = posX - s.transform.w/2;
123
            s.transform.y = posY - s.transform.h / 2;
1407 chris 124
            s.transform.pivotX = 0;
125
            s.transform.pivotY = 0;
1397 chris 126
            s.render();
1400 chris 127
 
1401 chris 128
            mDocument.Render();
129
 
1400 chris 130
            mInteractionMode.Render();
1386 chris 131
        }
132
 
1400 chris 133
        private void RenderGrid(float posX, float posY)
134
        {
135
            // render grid
1408 chris 136
            float x = posX % (GRID_SIZE * ZoomFactor);
137
            while (x < App.getGraphics().getWidth())
1400 chris 138
            {
1408 chris 139
                RenderUtil.drawLine(App.getGraphics(), x, 0, x, App.getGraphics().getHeight(), GRID_COLOR);
140
                x += GRID_SIZE * ZoomFactor;
1400 chris 141
            }
1408 chris 142
            float y = posY % (GRID_SIZE * ZoomFactor);
143
            while (y < App.getGraphics().getHeight())
1400 chris 144
            {
1408 chris 145
                RenderUtil.drawLine(App.getGraphics(), 0, y, App.getGraphics().getWidth(), y, GRID_COLOR);
146
                y += GRID_SIZE * ZoomFactor;
1400 chris 147
            }
148
        }
149
 
1386 chris 150
        private void GlView_Resize(object sender, EventArgs e)
151
        {
1397 chris 152
            if (App != null)
1408 chris 153
                App.getGraphics().updateSurfaceDimensions((int)(mGlView.Width), (int)(mGlView.Height));
1386 chris 154
        }
155
 
156
        private void FileNew_Click(object sender, EventArgs e)
157
        {
158
            if (!CheckDocumentModifiedAndSave())
159
                return;
160
 
161
            // new file
162
            mDocument.NewDocument();
163
        }
164
 
165
        private void FileOpen_Click(object sender, EventArgs e)
166
        {
167
            if (!CheckDocumentModifiedAndSave())
168
                return;
169
 
170
            // open file
171
            PerformFileOpen();
172
        }
173
 
174
        /** Check if the document has been modified, and if yes, ask for saving.
175
         * Returns true if everything is ok and the operation should be continued, or false when Cancel has been pressed. */
176
        private bool CheckDocumentModifiedAndSave()
177
        {
178
            if (mDocument.IsDirty())
179
            {
1409 chris 180
                switch (MessageBox.Show(this, "File has been modified. Save?", "New File", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
1386 chris 181
                {
182
                    case DialogResult.Yes:
183
                        if (!PerformFileSave())                        
184
                            return false;
185
                        break;
186
                    case DialogResult.No:
187
                        break;
188
                    case DialogResult.Cancel:
189
                        return false;
190
                }
191
            }
192
 
193
            return true;
194
        }
195
 
196
        /** Perform a file save operation with a file save dialog.
197
         * Returns false if the operation has been canceled. */
198
        private bool PerformFileSave(bool alwaysShowDialog = false)
199
        {
200
            if (alwaysShowDialog || !mDocument.IsFilenameSet())
201
            {
1409 chris 202
                saveFileDialog.FileName = mDocument.GetFilename();
1386 chris 203
                if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
204
                    return false;
205
 
206
                mDocument.SetFilename(saveFileDialog.FileName);
207
            }
208
 
209
            return mDocument.SaveDocument();
210
        }
211
 
212
        /** Perform a file open operation.
213
         * Return false if the operation was canceled. */
214
        private bool PerformFileOpen()
215
        {
216
            if (openFileDialog.ShowDialog() == DialogResult.Cancel)
217
                return false;
218
 
219
            return mDocument.LoadDocument(openFileDialog.FileName);
220
        }
221
 
222
        private void FileSave_Click(object sender, EventArgs e)
223
        {
224
            PerformFileSave();
225
        }
226
 
227
        private void FileSaveAs_Click(object sender, EventArgs e)
228
        {
229
            PerformFileSave(true);
230
        }
231
 
232
        private void FileExit_Click(object sender, EventArgs e)
233
        {
234
            Close();
235
        }
236
 
1397 chris 237
        private void ChangeSprite_Click(object sender, EventArgs e)
238
        {
1398 chris 239
            if (openSpriteDialog.ShowDialog() == DialogResult.OK)
240
            {
241
                mDocument.ChangeSprite(openSpriteDialog.FileName);
242
                mGlView.Invalidate();
243
            }
1397 chris 244
        }
245
 
1399 chris 246
        private void StatusBarUpdate_Enter(object sender, EventArgs e)
247
        {
248
            if (sender is MenuItem)
249
                stStatusLabel.Text = ((MenuItem)sender).Text;
250
            else if (sender is ToolStripItem)
251
                stStatusLabel.Text = ((ToolStripItem)sender).Text;
252
            mStatusStrip.Refresh();
253
        }
254
 
255
        private void StatusBarUpdate_Leave(object sender, EventArgs e)
256
        {
257
            stStatusLabel.Text = "Ready.";
258
            mStatusStrip.Refresh();
259
        }
260
 
261
        private void GlView_MouseWheel(object sender, MouseEventArgs e)
262
        {
263
            if (e.Delta > 0)
264
            {
1413 chris 265
                SetZoom(mZoomFactor * 1.2f);
1399 chris 266
            }
267
            else if (e.Delta < 0)
268
            {
1413 chris 269
                SetZoom(mZoomFactor * 0.8f);
1399 chris 270
            }
1413 chris 271
        }
1399 chris 272
 
1413 chris 273
        private void SetZoom(float zoomFactor)
274
        {
275
            mZoomFactor = zoomFactor;
276
            if (mZoomFactor < 0.1f)
277
                mZoomFactor = 0.1f;
278
 
1399 chris 279
            mGlView.Invalidate();
1413 chris 280
            GlView_Resize(mGlView, new EventArgs());
281
 
282
            int z = (int)Math.Floor(mZoomFactor * 100);
283
            stZoomFactor.Text = z + "%";
284
            mStatusStrip.Refresh();
1399 chris 285
        }
286
 
1404 chris 287
        private void UpdateToolButtonCheckedState()
1399 chris 288
        {
1404 chris 289
            tbSelect.Checked = Interaction.Mode == InteractionMode.SELECT;
290
            tbRectangle.Checked = Interaction.Mode == InteractionMode.RECTANGLE;
291
            tbEllipse.Checked = Interaction.Mode == InteractionMode.ELLIPSE;
292
            tbPolygon.Checked = Interaction.Mode == InteractionMode.POLYGON;
1399 chris 293
        }
294
 
1404 chris 295
        private void SetInteractionMode(int mode)
296
        {
297
            switch (mode)
298
            {
299
                case InteractionMode.SELECT:
300
                    mInteractionMode = InteractionMode.CreateMode(this, InteractionMode.SELECT);
301
                    mGlView.Cursor = Cursors.Arrow;
302
                    break;
303
                case InteractionMode.RECTANGLE:
304
                    mInteractionMode = InteractionMode.CreateMode(this, InteractionMode.RECTANGLE);
305
                    mGlView.Cursor = Cursors.Cross;
306
                    break;
307
                case InteractionMode.ELLIPSE:
308
                    mInteractionMode = InteractionMode.CreateMode(this, InteractionMode.ELLIPSE);
309
                    mGlView.Cursor = Cursors.Cross;
310
                    break;
311
                case InteractionMode.POLYGON:
312
                    mInteractionMode = InteractionMode.CreateMode(this, InteractionMode.POLYGON);
313
                    mGlView.Cursor = Cursors.Cross;
314
                    break;
315
            }
316
 
317
            UpdateToolButtonCheckedState();
318
 
319
            mGlView.Invalidate();
320
        }
321
 
322
 
1399 chris 323
        private void Tool_Click(object sender, EventArgs e)
324
        {
325
            if (sender == tbSelect)
1404 chris 326
                SetInteractionMode(InteractionMode.SELECT);
1399 chris 327
            else if (sender == tbRectangle)
1404 chris 328
                SetInteractionMode(InteractionMode.RECTANGLE);                
1399 chris 329
            else if (sender == tbEllipse)
1404 chris 330
                SetInteractionMode(InteractionMode.ELLIPSE);                
1399 chris 331
            else if (sender == tbPolygon)
1404 chris 332
                SetInteractionMode(InteractionMode.POLYGON);                
1399 chris 333
        }
334
 
1418 chris 335
 
336
        private void GlView_DoubleClick(object sender, EventArgs e)
337
        {
338
            /*mInteractionMode.DoubleClick(e);
339
            mPanInteraction.DoubleClick(e);
340
            mGlView.Invalidate();*/
341
        }
342
 
1399 chris 343
        private void GlView_MouseDown(object sender, MouseEventArgs e)
344
        {
345
            //mInteractionMode.MouseDown(e);
1420 chris 346
            mInteractionMode.MouseMove(e, true);
347
            mPanInteraction.MouseMove(e, true);
1399 chris 348
            mGlView.Invalidate();
349
        }
350
 
351
        private void GlView_MouseMove(object sender, MouseEventArgs e)
352
        {
353
            mInteractionMode.MouseMove(e);
1402 chris 354
            mPanInteraction.MouseMove(e);
1412 chris 355
            mGlView.Refresh();
356
            pgProperties.Refresh();
1399 chris 357
        }
358
 
359
        private void GlView_MouseUp(object sender, MouseEventArgs e)
360
        {
361
            mInteractionMode.MouseUp(e);
1402 chris 362
            mPanInteraction.MouseUp(e);
1412 chris 363
            mGlView.Refresh();
1399 chris 364
        }
365
 
366
        public void PanBy(int dx, int dy)
367
        {
368
            mCurrentX += dx;
369
            mCurrentY += dy;
370
        }
371
 
1402 chris 372
        private void Elements_SelectedIndexChanged(object sender, EventArgs e)
373
        {
1412 chris 374
            pgProperties.SelectedObject = mDocument.GetCurrentShape();
1402 chris 375
            mGlView.Invalidate();
1412 chris 376
            //pgProperties.SelectedObject = mDocument.GetCurrentShape();
1402 chris 377
        }
378
 
1410 chris 379
        private void About_Click(object sender, EventArgs e)
380
        {
381
            AboutBox dlg = new AboutBox();
382
            dlg.ShowDialog();
383
        }
384
 
1411 chris 385
        private void DeleteShape_Click(object sender, EventArgs e)
386
        {
387
            mDocument.DeleteCurrent();
388
        }
389
 
390
        private void MoveUp_Click(object sender, EventArgs e)
391
        {
392
            mDocument.MoveUp();
393
        }
394
 
395
        private void MoveDown_Click(object sender, EventArgs e)
396
        {
397
            mDocument.MoveDown();
398
        }
399
 
1412 chris 400
        private void Properties_ValueChanged(object s, PropertyValueChangedEventArgs e)
401
        {
402
            mGlView.Refresh();
403
        }
404
 
1413 chris 405
        private void ZoomIn_Click(object sender, EventArgs e)
406
        {
407
            SetZoom(mZoomFactor * 1.2f);
408
        }
409
 
410
        private void ZoomOut_Click(object sender, EventArgs e)
411
        {
412
            SetZoom(mZoomFactor * 0.8f);            
413
        }
414
 
415
        private void ZoomReset_Click(object sender, EventArgs e)
416
        {
417
            SetZoom(1.0f);
418
        }
419
 
1533 chris 420
        private void PrevFrame_Click(object sender, EventArgs e)
421
        {
422
            mDocument.CurrentFrame = mDocument.CurrentFrame - 1;
423
        }
1418 chris 424
 
1533 chris 425
        private void NextFrame_Click(object sender, EventArgs e)
426
        {
427
            mDocument.CurrentFrame = mDocument.CurrentFrame + 1;
428
        }
429
 
430
 
1386 chris 431
    }
432
}