Subversion Repositories AndroidProjects

Rev

Rev 1422 | Go to most recent revision | 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.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
1398 chris 6
using System.IO;
1386 chris 7
 
1397 chris 8
using BauzoidNET.graphics;
9
using BauzoidNET.graphics.sprite;
1401 chris 10
using BauzoidNET.math;
1397 chris 11
 
1401 chris 12
using ShapeEditor.file.shapes;
1404 chris 13
using ShapeEditor.interaction;
1401 chris 14
 
1386 chris 15
namespace ShapeEditor.file
16
{
17
    public class Document
18
    {
1402 chris 19
        public static readonly Vector4 SHAPE_COLOR = new Vector4(0, 0, 1, 1);
20
        public static readonly Vector4 SHAPE_COLOR_SELECTED = new Vector4(0.8f, 0.2f, 1, 1);
21
 
22
        public const int HANDLE_SIZE = 7;
23
 
1398 chris 24
        private MainForm mOwner = null;
1405 chris 25
        public MainForm Owner { get { return mOwner; } }
1398 chris 26
 
1386 chris 27
        private bool mIsDirty = false;
1398 chris 28
        private string mFilename = null;
1386 chris 29
 
1401 chris 30
        private List<BaseShapeElement> mShapeElements = new List<BaseShapeElement>();
1414 chris 31
        public List<BaseShapeElement> ShapeElements { get { return mShapeElements; } }
1386 chris 32
 
1397 chris 33
        private Sprite mSprite = null;
34
 
1402 chris 35
        private SimpleSprite mHandle = null;
36
 
1397 chris 37
        private SpriteInstance mSpriteInstance = null;
1405 chris 38
        public SpriteInstance Sprite { get { return mSpriteInstance; } }
1397 chris 39
 
1398 chris 40
        public Document(MainForm owner)
1386 chris 41
        {
1398 chris 42
            mOwner = owner;
1402 chris 43
 
1489 chris 44
            mHandle = new SimpleSprite(MainForm.App.getGraphics(), ShapeEditor.Properties.Resources.handle);
1386 chris 45
        }
46
 
1402 chris 47
        public void Init()
48
        {
49
            mHandle.init();
50
            mHandle.getSpriteTransform().w = HANDLE_SIZE;
51
            mHandle.getSpriteTransform().h = HANDLE_SIZE;
52
            mHandle.getSpriteTransform().centerPivot();
53
        }
54
 
55
        public void Exit()
56
        {
57
            Destroy();
58
            mHandle.dispose();
59
        }
60
 
1397 chris 61
        public void Destroy()
62
        {
1398 chris 63
            DestroySprite();
1408 chris 64
            mOwner.ZoomFactor = 1.0f;
65
            mOwner.mCurrentX = 0.0f;
66
            mOwner.mCurrentY = 0.0f;
67
            mShapeElements.Clear();
68
            mOwner.ElementsListBox.Items.Clear();
1398 chris 69
        }
70
 
71
        public void DestroySprite()
72
        {
1397 chris 73
            if (mSprite != null)
74
            {
75
                mSprite.dispose();
76
                mSprite = null;
77
            }
78
 
79
            mSpriteInstance = null;
80
        }
81
 
1409 chris 82
        public void SetMainFormCaption()
83
        {
84
            if (IsFilenameSet())
85
                mOwner.Text = Path.GetFileName(mFilename) + " - Bauzoid ShapeEditor";
86
            else
87
                mOwner.Text = "Bauzoid ShapeEditor";
88
        }
89
 
1386 chris 90
        public bool NewDocument()
1397 chris 91
        {  
92
            Destroy();
93
 
1386 chris 94
            mIsDirty = false;
95
 
1489 chris 96
            ChangeSprite(null);
1409 chris 97
 
98
            SetMainFormCaption();
1398 chris 99
 
1386 chris 100
            return true;
101
        }
102
 
103
        public bool LoadDocument(String filename)
104
        {
1397 chris 105
            Destroy();
106
 
107
            mIsDirty = false;
1386 chris 108
            mFilename = filename;
109
 
1407 chris 110
            string spriteFile = Path.ChangeExtension(filename, "png");
1422 chris 111
 
112
            if (!File.Exists(spriteFile))
113
                spriteFile = "data/textures/test.png";
114
 
1407 chris 115
            ChangeSprite(spriteFile);
116
 
1414 chris 117
            if (!FileUtil.LoadLevel(mFilename, this))
1407 chris 118
                return false;
119
 
1409 chris 120
            SetMainFormCaption();
121
 
1407 chris 122
            return true;
1386 chris 123
        }
124
 
125
        public bool SaveDocument()
126
        {
127
            // save mLevelElements
1414 chris 128
            if (!FileUtil.SaveLevel(mFilename, this))
129
                return false;
1409 chris 130
 
131
            SetMainFormCaption();
132
 
1414 chris 133
            return true;
1386 chris 134
        }
135
 
1398 chris 136
        public bool SaveDocument(string filename)
1386 chris 137
        {
138
            mFilename = filename;
139
            return SaveDocument();
140
        }
141
 
1398 chris 142
        public void ChangeSprite(string filename)
143
        {
144
            DestroySprite();
145
 
1489 chris 146
            if (filename == null)
1398 chris 147
            {
1489 chris 148
                mSprite = new Sprite(MainForm.App.getGraphics(), ShapeEditor.Properties.Resources.test);
1398 chris 149
            }
150
            else
151
            {
1489 chris 152
 
153
                string atlasFile = Path.ChangeExtension(filename, "txt");
154
                if (File.Exists(atlasFile))
155
                {
156
                    mSprite = new Sprite(MainForm.App.getGraphics(), filename, atlasFile);
157
                }
158
                else
159
                {
160
                    mSprite = new Sprite(MainForm.App.getGraphics(), filename);
161
                }
1398 chris 162
            }
1489 chris 163
 
1398 chris 164
            mSprite.init();
165
 
166
            mSpriteInstance = mSprite.createSpriteInstanceForAll();
1407 chris 167
 
168
            mSpriteInstance.transform.x = 0;
169
            mSpriteInstance.transform.y = 0;
170
            mSpriteInstance.transform.w = mSpriteInstance.getSprite().getTextureWidth() * mSpriteInstance.getSpriteRegion().getWidth();
171
            mSpriteInstance.transform.h = mSpriteInstance.getSprite().getTextureHeight() * mSpriteInstance.getSpriteRegion().getHeight();
172
            mSpriteInstance.transform.pivotX = 0;
173
            mSpriteInstance.transform.pivotY = 0;
1398 chris 174
        }
175
 
1397 chris 176
        public void Render()
177
        {
178
            Graphics g = MainForm.App.getGraphics();
179
 
1401 chris 180
            for (int i = 0; i < mShapeElements.Count; i++)
1397 chris 181
            {
1402 chris 182
                if (mOwner.ElementsListBox.SelectedIndex == i)
1408 chris 183
                    mShapeElements.ElementAt(i).RenderSelected(mSpriteInstance.transform, mOwner.Interaction.Mode == InteractionMode.SELECT);
1402 chris 184
                else
1408 chris 185
                    mShapeElements.ElementAt(i).Render(mSpriteInstance.transform);
1397 chris 186
            }
187
        }
188
 
1405 chris 189
        /*public bool IsInsideHandle(int x, int y)
1404 chris 190
        {
191
            for (int i = mShapeElements.Count - 1; i >= 0; i--)
192
            {
193
                if (mShapeElements.ElementAt(i).IsInsideHandle(x, y, mSpriteInstance.transform, mOwner.ZoomFactor))
194
                    return true;
195
            }
196
 
197
            return false;
1405 chris 198
        }*/
199
 
200
        public BaseShapeElement GetSelectedElement()
201
        {
202
            if (mOwner.ElementsListBox.Items.Count == 0)
203
                return null;
204
 
205
            if (mOwner.ElementsListBox.SelectedIndex == -1)
206
                return null;
207
 
208
            return mShapeElements.ElementAt(mOwner.ElementsListBox.SelectedIndex);
1404 chris 209
        }
210
 
1408 chris 211
        public BaseShapeElement SelectAt(float x, float y)
1406 chris 212
        {
213
            if (mOwner.ElementsListBox.Items.Count == 0)
214
                return null;
215
 
216
            if (mOwner.ElementsListBox.SelectedIndex == -1)
217
                return null;
218
 
219
            for (int i = mShapeElements.Count - 1; i >= 0; i--)
220
            {
221
                if (mShapeElements.ElementAt(i).IsInside(x, y))
222
                {
223
                    mOwner.ElementsListBox.SelectedIndex = i;
224
                    return mShapeElements.ElementAt(i);
225
                }
226
            }
227
 
228
            return null;
229
        }
230
 
1402 chris 231
        public void RenderHandle(float x, float y, SpriteTransform t)
232
        {
1408 chris 233
            //Vector2 p = t.spriteToWorld(x, y);
1402 chris 234
 
1408 chris 235
            mHandle.getSpriteTransform().x = x * mOwner.ZoomFactor + mSpriteInstance.transform.x;
236
            mHandle.getSpriteTransform().y = y * mOwner.ZoomFactor + mSpriteInstance.transform.y;
1402 chris 237
            mHandle.render();
238
        }
239
 
1401 chris 240
        public void AddRectangle(float x, float y, float w, float h)
241
        {
1408 chris 242
            string name = "rect " + x + ", " + y + ", " + w + ", " + h;
243
            RectangleElement e = new RectangleElement(this, name, x, y, w, h);
1402 chris 244
            mShapeElements.Add(e);
245
            mOwner.ElementsListBox.Items.Add(e);
246
            mOwner.ElementsListBox.SelectedIndex = mOwner.ElementsListBox.Items.Count - 1;
1409 chris 247
 
248
            mIsDirty = true;
1401 chris 249
        }
250
 
1416 chris 251
        public void AddEllipse(float centerX, float centerY, float radiusX, float radiusY)
252
        {
253
            string name = "ellipse " + centerX + ", " + centerY + ", " + radiusX + ", " + radiusY;
254
            EllipseElement e = new EllipseElement(this, name, centerX, centerY, radiusX, radiusY);
255
            mShapeElements.Add(e);
256
            mOwner.ElementsListBox.Items.Add(e);
257
            mOwner.ElementsListBox.SelectedIndex = mOwner.ElementsListBox.Items.Count - 1;
258
 
259
            mIsDirty = true;
260
        }
261
 
1419 chris 262
        public void AddPolygon(List<Vector2> vertices)
263
        {
264
            if (vertices.Count < 3)
265
                return;
266
 
267
            string name = "polygon " + vertices.First().x + ", " + vertices.First().y + " -->";
1421 chris 268
 
269
            PolygonElement e = new PolygonElement(this, name, vertices);
270
            mShapeElements.Add(e);
271
            mOwner.ElementsListBox.Items.Add(e);
272
 
1419 chris 273
            /*EllipseElement e = new EllipseElement(this, name, centerX, centerY, radiusX, radiusY);
274
            mShapeElements.Add(e);
275
            mOwner.ElementsListBox.Items.Add(e);*/
276
            mOwner.ElementsListBox.SelectedIndex = mOwner.ElementsListBox.Items.Count - 1;
277
 
278
            mIsDirty = true;
279
        }
280
 
1411 chris 281
        public void DeleteCurrent()
282
        {
283
            int i = mOwner.ElementsListBox.SelectedIndex;
284
            if (i == -1)
285
                return;
286
 
287
            mShapeElements.RemoveAt(i);
288
            mOwner.ElementsListBox.Items.RemoveAt(i);
289
            mOwner.ElementsListBox.SelectedIndex = Math.Min(i, mOwner.ElementsListBox.Items.Count - 1);            
290
        }
291
 
292
        public void MoveUp()
293
        {
294
            int i = mOwner.ElementsListBox.SelectedIndex;
295
            if (i <= 0)
296
                return;
297
 
298
            BaseShapeElement e = mShapeElements.ElementAt(i);
299
            mShapeElements.RemoveAt(i);
300
            mOwner.ElementsListBox.Items.RemoveAt(i);
301
            mShapeElements.Insert(i - 1, e);
302
            mOwner.ElementsListBox.Items.Insert(i - 1, e);
303
            mOwner.ElementsListBox.SelectedIndex = i - 1;
304
        }
305
 
306
        public void MoveDown()
307
        {
308
            int i = mOwner.ElementsListBox.SelectedIndex;
309
            if ((i == -1) || (i >= (mOwner.ElementsListBox.Items.Count-1)))
310
                return;
311
 
312
            BaseShapeElement e = mShapeElements.ElementAt(i);
313
            mShapeElements.RemoveAt(i);
314
            mOwner.ElementsListBox.Items.RemoveAt(i);
315
            mShapeElements.Insert(i + 1, e);
316
            mOwner.ElementsListBox.Items.Insert(i + 1, e);
317
            mOwner.ElementsListBox.SelectedIndex = i + 1;
318
        }
319
 
1412 chris 320
        public BaseShapeElement GetCurrentShape()
321
        {
322
            if (mOwner.ElementsListBox.SelectedIndex == -1)
323
                return null;
324
 
325
            return mShapeElements.ElementAt(mOwner.ElementsListBox.SelectedIndex);
326
        }
327
 
1409 chris 328
        public void SetDirty(bool dirty = true)
1386 chris 329
        {
330
            mIsDirty = dirty;
331
        }
332
 
333
        public bool IsDirty()
334
        {
335
            return mIsDirty;
336
        }
337
 
338
        public String GetFilename()
339
        {
340
            return mFilename;
341
        }
342
 
343
        public void SetFilename(String filename)
344
        {
345
            mFilename = filename;
346
        }
347
 
348
        public bool IsFilenameSet()
349
        {
350
            return (mFilename != null);
351
        }
1397 chris 352
 
353
        public SpriteInstance GetSpriteInstance()
354
        {
355
            return mSpriteInstance;
356
        }
1386 chris 357
    }
358
}