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