Subversion Repositories AndroidProjects

Rev

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

Rev Author Line No. Line
1305 chris 1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
1467 chris 6
using System.ComponentModel;
1483 chris 7
using System.IO;
8
using System.Windows.Forms;
1496 chris 9
using System.ComponentModel;
10
using System.Windows.Forms.Design;
1305 chris 11
 
1308 chris 12
using BurutaruEditor.file.elements;
1432 chris 13
using BurutaruEditor.file.enemies;
1438 chris 14
using BurutaruEditor.view;
1308 chris 15
 
1423 chris 16
using BauzoidNET.math;
17
using BauzoidNET.parser;
1822 chris 18
using BurutaruEditor.file.conditions;
1423 chris 19
 
1305 chris 20
namespace BurutaruEditor.file
21
{
22
    public class Document
23
    {
1423 chris 24
        //=================================================================================================================================
25
 
1429 chris 26
        public const float VIRTUAL_WIDTH = 800;
27
        public const float VIRTUAL_HEIGHT = 480;
1423 chris 28
 
1494 chris 29
        public const float PASTE_OFFSET = 15;
30
 
1458 chris 31
        public static readonly Vector4 TEXT_COLOR = new Vector4(1, 1, 1, 1);
32
        public const float TEXT_SCALE = 0.8f;
33
 
1423 chris 34
        //=================================================================================================================================
35
 
36
        private MainForm mOwner = null;
1467 chris 37
        [Browsable(false)]
1423 chris 38
        public MainForm Owner { get { return mOwner; } }
39
 
1467 chris 40
        [Browsable(false)]
1438 chris 41
        public DocumentView View { get { return Owner.View; } }
42
 
1305 chris 43
        private bool mIsDirty = false;
44
        private String mFilename = null;
45
 
1423 chris 46
        private List<CheckPoint> mCheckPoints = new List<CheckPoint>();
1467 chris 47
        [Browsable(false)]
1428 chris 48
        public List<CheckPoint> CheckPoints { get { return mCheckPoints; } }
1432 chris 49
 
50
        private List<EnemySpawner> mSpawners = new List<EnemySpawner>();
1467 chris 51
        [Browsable(false)]
1432 chris 52
        public List<EnemySpawner> Spawners { get { return mSpawners; } }
1423 chris 53
 
1307 chris 54
        private List<LevelElement> mLevelElements = new List<LevelElement>();
1467 chris 55
        [Browsable(false)]
1435 chris 56
        public List<LevelElement> LevelElements { get { return mLevelElements; } }
1307 chris 57
 
1822 chris 58
        private List<BaseCondition> mScripting = new List<BaseCondition>();
59
        [Browsable(false)]
60
        public List<BaseCondition> Scripting { get { return mScripting; } }
61
 
1477 chris 62
        [Category("Basic Parameters")]
1423 chris 63
        public string LevelTitle { get; set; }
1467 chris 64
 
1556 chris 65
        [Category("Basic Parameters")]
66
        public string StageTitle { get; set; }
67
 
1477 chris 68
        private Vector2 mStartPosition = new Vector2();
1467 chris 69
        [TypeConverter(typeof(ExpandableObjectConverter))]
1477 chris 70
        [Category("Basic Parameters")]
1443 chris 71
        public Vector2 StartPosition { get { return mStartPosition; } }
1423 chris 72
 
1557 chris 73
        [Category("Basic Parameters")]
74
        public bool WarpIn { get; set; }
75
 
1588 chris 76
        [Category("Basic Parameters")]
77
        public CheckPoint.StarFieldControlStatus StarFieldStatus { get; set; }
1557 chris 78
 
1494 chris 79
        private string mCopyPasteBuffer = null;
80
        private float mPasteOffset = PASTE_OFFSET;
81
 
1423 chris 82
        //=================================================================================================================================
83
 
1496 chris 84
        public class SpriteFileNameEditor : FileNameEditor
85
        {
86
            protected override void InitializeDialog(OpenFileDialog openFileDialog)
87
            {
88
                base.InitializeDialog(openFileDialog);
89
                String s = Path.Combine(Application.StartupPath, @"..\\..\\..\\..\\..\\BauzoidApps\\BurutaruZ_v2\\burutaru-android\\assets\\data");
90
                openFileDialog.InitialDirectory = Path.GetFullPath(s) + "\\textures";
91
                openFileDialog.Filter = "Image files|*.png|All files|*.*";
92
            }
93
        }
94
 
95
        public class ShapeFileNameEditor : FileNameEditor
96
        {
97
            protected override void InitializeDialog(OpenFileDialog openFileDialog)
98
            {
99
                base.InitializeDialog(openFileDialog);
100
                String s = Path.Combine(Application.StartupPath, @"..\\..\\..\\..\\..\\BauzoidApps\\BurutaruZ_v2\\burutaru-android\\assets\\data");
101
                openFileDialog.InitialDirectory = Path.GetFullPath(s) + "\\textures";
102
                openFileDialog.Filter = "Shape files|*.shape|All files|*.*";
103
            }
104
        }
105
 
106
        //=================================================================================================================================
107
 
1423 chris 108
        public Document(MainForm owner)
1305 chris 109
        {
1423 chris 110
            mOwner = owner;
111
            ResetDocument();
1556 chris 112
            StageTitle = "Stage 01";
1557 chris 113
            WarpIn = false;
1588 chris 114
            StarFieldStatus = CheckPoint.StarFieldControlStatus.NORMAL;
1305 chris 115
        }
116
 
1425 chris 117
        public void Init()
118
        {
1436 chris 119
            ResetDocument();
1425 chris 120
        }
121
 
122
        public void Exit()
123
        {
1436 chris 124
            ResetDocument();
1425 chris 125
        }
126
 
1423 chris 127
        public void ResetDocument()
1305 chris 128
        {
1423 chris 129
            mFilename = null;
130
 
131
            LevelTitle = "Level Title";
132
 
1305 chris 133
            mIsDirty = false;
1436 chris 134
 
135
            mSpawners.Clear();
136
 
137
            for (int i = 0; i < mLevelElements.Count; i++)
138
                mLevelElements.ElementAt(i).Exit();
1307 chris 139
            mLevelElements.Clear();
1305 chris 140
 
1423 chris 141
            mCheckPoints.Clear();
1485 chris 142
 
143
            Owner.CheckPoints.Items.Clear();
144
            Owner.Spawners.Items.Clear();
145
            Owner.LevelElements.Items.Clear();
1822 chris 146
            Owner.Scripting.Items.Clear();
1485 chris 147
            Owner.Properties.SelectedObject = this;
1423 chris 148
        }
149
 
150
        public bool NewDocument()
151
        {
152
            ResetDocument();
153
 
1485 chris 154
            View.ResetView();
155
 
1486 chris 156
            Owner.Text = "Unnamed.level - Burutaru Editor";
157
 
1305 chris 158
            return true;
159
        }
160
 
161
        public bool LoadDocument(String filename)
162
        {
1423 chris 163
            ResetDocument();
164
 
1305 chris 165
            mFilename = filename;
166
 
1307 chris 167
            // fill mLevelElements from file
1428 chris 168
            if (!LevelUtil.LoadLevel(mFilename, this))
169
                return false;
170
 
171
            Owner.GlView.Refresh();
172
 
1486 chris 173
            Owner.Text = Path.GetFileName(mFilename) + " - Burutaru Editor";
174
 
1428 chris 175
            return true;
1305 chris 176
        }
177
 
178
        public bool SaveDocument()
179
        {
1307 chris 180
            // save mLevelElements
1486 chris 181
            if (!LevelUtil.SaveLevel(mFilename, this))
182
                return false;
183
 
184
            Owner.Text = Path.GetFileName(mFilename) + " - Burutaru Editor";
185
 
1509 chris 186
            SetDirty(false);
187
 
1486 chris 188
            return true;
1305 chris 189
        }
190
 
191
        public bool SaveDocument(String filename)
192
        {
193
            mFilename = filename;
194
            return SaveDocument();
195
        }
196
 
1461 chris 197
        public void CreateCheckPoint(float x, float y)
198
        {
1478 chris 199
            float tx; float ty;
200
            Owner.View.TransformToWorldSpace(out tx, out ty, x, y, true);
1461 chris 201
 
202
            CheckPoint cp = new CheckPoint(this, "NewCheckPoint");
203
            cp.Position.x = tx;
204
            cp.Position.y = ty;
205
 
206
            AddCheckPoint(cp);
207
        }
208
 
1423 chris 209
        public void AddCheckPoint(CheckPoint checkPoint)
210
        {
211
            mCheckPoints.Add(checkPoint);
212
            Owner.CheckPoints.Items.Add(checkPoint);
213
            Owner.CheckPoints.SelectedIndex = Owner.CheckPoints.Items.Count - 1;
214
        }
215
 
1478 chris 216
        public void CreateEnemySpawner(float x, float y)
217
        {
218
            float tx; float ty;
219
            Owner.View.TransformToWorldSpace(out tx, out ty, x, y);
220
 
221
            string className = Owner.CurrentSpawnerType;
222
 
1823 chris 223
            Type t = Type.GetType("BurutaruEditor.file.enemies." + className + "Spawner");
1480 chris 224
            Object[] args = { this, "New" + className };
225
 
226
            EnemySpawner sp = (EnemySpawner)Activator.CreateInstance(t, args);
1496 chris 227
            sp.SpawnTrigger.x = tx - View.CurrentPosition.x;
228
            sp.SpawnTrigger.y = ty - View.CurrentPosition.y;
1480 chris 229
            AddEnemySpawner(sp);
230
 
1478 chris 231
            /*Type t = Type.GetType("TestApp.Entry");
232
 
233
            Object[] args = { "emo", false };
234
 
235
            Object o = Activator.CreateInstance(t, args);*/
236
        }
237
 
1433 chris 238
        public void AddEnemySpawner(EnemySpawner spawner)
239
        {
240
            mSpawners.Add(spawner);
241
            Owner.Spawners.Items.Add(spawner);
242
            Owner.Spawners.SelectedIndex = Owner.Spawners.Items.Count - 1;
243
        }
244
 
1822 chris 245
        public void CreateScripting(float x, float y)
246
        {
247
            float tx; float ty;
248
            Owner.View.TransformToWorldSpace(out tx, out ty, x, y);
249
 
250
            string className = Owner.CurrentScriptingType;
251
 
252
            Type t = Type.GetType("BurutaruEditor.file.conditions." + className);
253
            Object[] args = { this, "New" + className };
254
 
255
            BaseCondition bc = (BaseCondition)Activator.CreateInstance(t, args);
1823 chris 256
            bc.Position.x = tx - View.CurrentPosition.x;
257
            bc.Position.y = ty - View.CurrentPosition.y;
1822 chris 258
            AddScripting(bc);
259
        }
260
 
261
        public void AddScripting(BaseCondition condition)
262
        {
263
            mScripting.Add(condition);
264
            Owner.Scripting.Items.Add(condition);
265
            Owner.Scripting.SelectedIndex = Owner.Scripting.Items.Count - 1;
266
        }
267
 
1481 chris 268
        public void CreateLevelElement(float x, float y, string filename)
269
        {
270
            float tx; float ty;
271
            Owner.View.TransformToWorldSpace(out tx, out ty, x, y);
272
 
1483 chris 273
            string className = Owner.CurrentElementType;
1481 chris 274
 
1484 chris 275
 
1483 chris 276
            if (className.Equals("BackgroundElement"))
277
            {
278
                BackgroundElement e = new BackgroundElement(this, "NewBackgroundElement");
279
 
1484 chris 280
                e.TextureFile = filename;
1496 chris 281
                e.Transform.x = tx - View.CurrentPosition.x;
282
                e.Transform.y = ty - View.CurrentPosition.y;
1483 chris 283
                e.Transform.w = e.Sprite.getSprite().getTextureWidth();
284
                e.Transform.h = e.Sprite.getSprite().getTextureHeight();
285
                e.Transform.centerPivot();
286
                AddLevelElement(e);
287
            }
288
            else if (className.Equals("StaticElement"))
289
            {
290
                StaticElement e = new StaticElement(this, "NewStaticElement");
291
                e.TextureFile = filename;
1496 chris 292
                e.Transform.x = tx - View.CurrentPosition.x;
293
                e.Transform.y = ty - View.CurrentPosition.y;
1483 chris 294
                e.Transform.w = e.Sprite.getSprite().getTextureWidth();
295
                e.Transform.h = e.Sprite.getSprite().getTextureHeight();
296
                e.Transform.centerPivot();
297
                AddLevelElement(e);
298
            }
299
 
1481 chris 300
            /*Type t = Type.GetType("BurutaruEditor.file.enemies." + className + "Spawner");
301
            Object[] args = { this, "New" + className };
302
 
303
            EnemySpawner sp = (EnemySpawner)Activator.CreateInstance(t, args);
304
            sp.SpawnTrigger.x = tx;
305
            sp.SpawnTrigger.y = ty;
306
            AddEnemySpawner(sp);*/
307
        }
308
 
1578 chris 309
        public LevelElement FindLevelElement(string name)
310
        {
311
            for (int i = 0; i < mLevelElements.Count; i++)
312
            {
313
                LevelElement e = mLevelElements.ElementAt(i);
314
 
315
                if (e.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
316
                    return e;
317
            }
318
 
319
            return null;
320
        }
321
 
1435 chris 322
        public void AddLevelElement(LevelElement element)
323
        {
324
            mLevelElements.Add(element);
325
            Owner.LevelElements.Items.Add(element);
326
            Owner.LevelElements.SelectedIndex = Owner.LevelElements.Items.Count - 1;
327
        }
328
 
1496 chris 329
        public void DeleteCurrentObject()
330
        {
331
            if (Owner.CurrentObjectMode == MainForm.ObjectMode.CHECKPOINTS)
332
            {
333
                int i = Owner.CheckPoints.SelectedIndex;
334
                Owner.CheckPoints.Items.RemoveAt(i);
335
                mCheckPoints.RemoveAt(i);
336
                Owner.CheckPoints.SelectedIndex = Math.Min(i-1, Owner.CheckPoints.Items.Count - 1);
337
            }
338
            else if (Owner.CurrentObjectMode == MainForm.ObjectMode.SPAWNERS)
339
            {
340
                int i = Owner.Spawners.SelectedIndex;
341
                Owner.Spawners.Items.RemoveAt(i);
342
                mSpawners.RemoveAt(i);
343
                Owner.Spawners.SelectedIndex = Math.Min(i-1, Owner.Spawners.Items.Count - 1);
344
            }
345
            else if (Owner.CurrentObjectMode == MainForm.ObjectMode.ELEMENTS)
346
            {
347
                int i = Owner.LevelElements.SelectedIndex;
348
                Owner.LevelElements.Items.RemoveAt(i);
349
                mLevelElements.RemoveAt(i);
350
                Owner.LevelElements.SelectedIndex = Math.Min(i-1, Owner.LevelElements.Items.Count - 1);
351
            }
1822 chris 352
            else if (Owner.CurrentObjectMode == MainForm.ObjectMode.SCRIPTING)
353
            {
354
                int i = Owner.Scripting.SelectedIndex;
355
                Owner.Scripting.Items.RemoveAt(i);
356
                mScripting.RemoveAt(i);
357
                Owner.Scripting.SelectedIndex = Math.Min(i - 1, Owner.Scripting.Items.Count - 1);
358
            }
1496 chris 359
        }
360
 
1461 chris 361
        public void MoveObjectTo<T>(List<T> objList, System.Windows.Forms.ListBox lb, int fromIndex, int toIndex)
362
        {
363
            if ((fromIndex < 0) || (fromIndex >= objList.Count) ||
364
                (toIndex < 0) || (toIndex >= objList.Count))
365
                return;
366
 
367
            T cp = objList.ElementAt(fromIndex);
368
            objList.RemoveAt(fromIndex);
369
            objList.Insert(toIndex, cp);
370
            lb.Items.RemoveAt(fromIndex);
371
            lb.Items.Insert(toIndex, cp);
372
            lb.SelectedIndex = toIndex;
373
        }
374
 
375
        public void MoveObjectUp()
376
        {
377
            switch (Owner.CurrentObjectMode)
378
            {
379
                case MainForm.ObjectMode.CHECKPOINTS:
380
                    MoveObjectTo(mCheckPoints, Owner.CheckPoints, Owner.CheckPoints.SelectedIndex, Owner.CheckPoints.SelectedIndex - 1);
381
                    break;
382
                case MainForm.ObjectMode.SPAWNERS:
383
                    MoveObjectTo(mSpawners, Owner.Spawners, Owner.Spawners.SelectedIndex, Owner.Spawners.SelectedIndex - 1);
384
                    break;
385
                case MainForm.ObjectMode.ELEMENTS:
386
                    MoveObjectTo(mLevelElements, Owner.LevelElements, Owner.LevelElements.SelectedIndex, Owner.LevelElements.SelectedIndex - 1);
387
                    break;
1822 chris 388
                case MainForm.ObjectMode.SCRIPTING:
389
                    MoveObjectTo(mScripting, Owner.Scripting, Owner.Scripting.SelectedIndex, Owner.Scripting.SelectedIndex - 1);
390
                    break;
1461 chris 391
            }
392
        }
393
 
394
        public void MoveObjectDown()
395
        {
396
            switch (Owner.CurrentObjectMode)
397
            {
398
                case MainForm.ObjectMode.CHECKPOINTS:
399
                    MoveObjectTo(mCheckPoints, Owner.CheckPoints, Owner.CheckPoints.SelectedIndex, Owner.CheckPoints.SelectedIndex + 1);
400
                    break;
401
                case MainForm.ObjectMode.SPAWNERS:
402
                    MoveObjectTo(mSpawners, Owner.Spawners, Owner.Spawners.SelectedIndex, Owner.Spawners.SelectedIndex + 1);
403
                    break;
404
                case MainForm.ObjectMode.ELEMENTS:
405
                    MoveObjectTo(mLevelElements, Owner.LevelElements, Owner.LevelElements.SelectedIndex, Owner.LevelElements.SelectedIndex + 1);
406
                    break;
1822 chris 407
                case MainForm.ObjectMode.SCRIPTING:
408
                    MoveObjectTo(mScripting, Owner.Scripting, Owner.Scripting.SelectedIndex, Owner.Scripting.SelectedIndex + 1);
409
                    break;
1461 chris 410
            }
411
        }
412
 
1494 chris 413
        public void Cut()
414
        {
415
            if (View.SelectedObject != null)
416
            {
417
                mCopyPasteBuffer = LevelUtil.GetObjectData(View.SelectedObject, "Copy");
418
                mPasteOffset = PASTE_OFFSET;
419
 
420
                // TODO: remove selected object
421
            }
422
        }
423
 
424
        public void Copy()
425
        {
426
            if (View.SelectedObject != null)
427
            {
428
                mCopyPasteBuffer = LevelUtil.GetObjectData(View.SelectedObject, "Copy");
429
                mPasteOffset = PASTE_OFFSET;
430
            }
431
        }
432
 
433
        public void Paste()
434
        {
435
            if (mCopyPasteBuffer != null)
436
            {
437
                System.Console.WriteLine(mCopyPasteBuffer);
438
                LevelUtil.CreateObjectFromString(this, mCopyPasteBuffer);
439
                View.SelectedObject.MoveBy(mPasteOffset, mPasteOffset, -1);
440
                mPasteOffset += PASTE_OFFSET;
441
            }            
442
        }
443
 
1423 chris 444
        //=================================================================================================================================
445
 
1477 chris 446
        public string GetPropertiesTitle()
447
        {
448
            return "Level Parameters";
449
        }
1423 chris 450
 
1477 chris 451
        public string GetPropertiesType()
452
        {
453
            return "";
454
        }
455
 
1305 chris 456
        public void SetDirty(bool dirty)
457
        {
458
            mIsDirty = dirty;
459
        }
460
 
461
        public bool IsDirty()
462
        {
463
            return mIsDirty;
464
        }
465
 
466
        public String GetFilename()
467
        {
468
            return mFilename;
469
        }
470
 
471
        public void SetFilename(String filename)
472
        {
473
            mFilename = filename;
474
        }
475
 
476
        public bool IsFilenameSet()
477
        {
478
            return (mFilename != null);
479
        }
1423 chris 480
 
1493 chris 481
        public void WriteData(TextWriter tw)
482
        {
1607 chris 483
            LevelUtil.WriteProperty(tw, "name", LevelTitle);
484
            LevelUtil.WriteProperty(tw, "stage", StageTitle);
485
            LevelUtil.WriteProperty(tw, "startPosition", mStartPosition);
486
            LevelUtil.WriteProperty(tw, "warpIn", WarpIn);
487
            LevelUtil.WriteProperty(tw, "starFieldStatus", (int)StarFieldStatus);
488
 
489
 
490
            /*tw.WriteLine("name \"" + LevelTitle + "\";");
1556 chris 491
            tw.WriteLine("stage \"" + StageTitle + "\";");
1493 chris 492
            tw.WriteLine("startPosition " + mStartPosition.x + ", " + mStartPosition.y + ";");
1580 chris 493
            tw.WriteLine("warpIn " + (WarpIn ? "true" : "false") + ";");
1607 chris 494
            tw.WriteLine("\tstarFieldStatus " + (int)StarFieldStatus + ";");*/
1493 chris 495
        }        
1423 chris 496
 
497
        public bool ReadParameter(Tokenizer t, string id)
498
        {
499
            if (id.Equals("name", StringComparison.OrdinalIgnoreCase))
500
            {
501
                LevelTitle = t.readString();
502
            }
1556 chris 503
            else if (id.Equals("stage", StringComparison.OrdinalIgnoreCase))
504
            {
505
                StageTitle = t.readString();
506
            }
1423 chris 507
            else if (id.Equals("startPosition", StringComparison.OrdinalIgnoreCase))
508
            {
1443 chris 509
                mStartPosition = ParseUtil.readVector2(t);
1423 chris 510
            }
1557 chris 511
            else if (id.Equals("warpIn", StringComparison.OrdinalIgnoreCase))
512
            {
513
                WarpIn = t.readIdentifier().Equals("true", StringComparison.OrdinalIgnoreCase);
514
            }
1588 chris 515
            else if (id.Equals("starFieldStatus", StringComparison.OrdinalIgnoreCase))
516
            {
517
                int status = (int)t.readNumber();
518
                StarFieldStatus = (CheckPoint.StarFieldControlStatus)status;
519
            }
1423 chris 520
            else
521
            {
522
                // unsupported id
523
                return false;
524
            }
525
 
526
            t.readToken(";");
527
            return true;
528
        }
1305 chris 529
    }
530
}