private void ToolDraw(Color penColor) { Sgml.surface_set_target(selectedFrame.layers[0].texture); Sgml.draw_set_aa(false); Sgml.draw_set_color(penColor); int x1 = 0; int y1 = 0; if (activeTool == Tools.Pixel) { if (PixelFree(Sgml.mouse.X, Sgml.mouse.Y)) { Sgml.draw_sprite(pixel, -2, tempVector); occupiedPositions.Add(tempVector); } if (size == 1) { num = 0; } if (size == 2) { num = 11; } if (size == 3) { num = 31; } if (size == 4) { num = 75; } if (size == 5) { num = 147; } if (size == 6) { num = 207; } for (int i = 0; i < num; i++) { if (PixelFree(Sgml.mouse.X + sizesx[i], Sgml.mouse.Y + sizesy[i])) { Sgml.draw_sprite(pixel, -2, tempVector); } } } else if (activeTool == Tools.Ellipse) { Sgml.draw_ellipse(new Vector2(Sgml.mouse.X - toolOrigin.X, Sgml.mouse.Y - toolOrigin.Y), toolOrigin, 1); } else if (activeTool == Tools.Fill) { // flood fill var data = new Color[selectedFrame.layers[0].texture.Width * selectedFrame.layers[0].texture.Height]; selectedFrame.layers[0].texture.GetData(data); var x = (int)Sgml.round(Sgml.mouse.X - .5f); var y = (int)Sgml.round(Sgml.mouse.Y - .5f); int w = Math.Max(selectedFrame.layers[0].texture.Height, selectedFrame.layers[0].texture.Width); if (inTexture(x, y) && inTextureP(x, y)) { var pixels = new Stack <Point>(); var used = new List <Point>(); var targetColor = data[w * y + x]; pixels.Push(new Point(x, y)); var i = 0; var max = data.Length - 1; var flag = !(data[w * y + x] == penColor); if (flag) { while (pixels.Count > 0 && i < max) { var a = pixels.Pop(); var xx = a.X; if (!inTextureP(a.X, a.Y)) { break; } while (xx >= 0 && data[w * a.Y + xx] == targetColor) { xx--; } xx++; var spanAbove = false; var spanBelow = false; while (xx < selectedFrame.layers[0].texture.Width && data[w * a.Y + xx] == targetColor) { data[w * a.Y + xx] = penColor; if (!spanAbove && a.Y > 0 && data[w * (a.Y - 1) + xx] == targetColor) { if (!used.Contains(new Point(xx, a.Y - 1))) { pixels.Push(new Point(xx, a.Y - 1)); used.Add(new Point(xx, a.Y - 1)); spanAbove = true; } } else if (spanAbove && a.Y > 0 && data[w * (a.Y - 1) + xx] != targetColor) { spanAbove = false; } if (!spanBelow && a.Y < selectedFrame.layers[0].texture.Height - 1 && data[w * (a.Y + 1) + xx] == targetColor) { if (!used.Contains(new Point(xx, a.Y + 1))) { pixels.Push(new Point(xx, a.Y + 1)); used.Add(new Point(xx, a.Y + 1)); spanBelow = true; } } else if (spanBelow && a.Y < selectedFrame.layers[0].texture.Height - 1 && data[w * (a.Y + 1) + xx] != targetColor) { spanBelow = false; } xx++; } i++; } selectedFrame.layers[0].texture.SetData(data); } } } else if (activeTool == Tools.Dropper) { } else if (activeTool == Tools.Line) { Sgml.draw_rectangle(new Vector2((float)Sgml.round(toolOriginINP.X - .5f), (float)Sgml.round(toolOriginINP.Y - .5f)), new Vector2((float)Sgml.round(toolOriginINP.X + .5f), (float)Sgml.round(toolOriginINP.Y + .5f)), false); Sgml.draw_line(mouseSubpixel, toolOriginSubpixel); } else if (activeTool == Tools.Polygon) { } else if (activeTool == Tools.Rectangle) { float my = Sgml.mouse.Y; my = (float)Sgml.floor(my) + .5f; Sgml.draw_rectangle(toolOrigin, new Vector2(Sgml.mouse.X, my), true); } else if (activeTool == Tools.RoundedRectangle) { Sgml.draw_roundrect(toolOrigin, new Vector2(Sgml.mouse.X, Sgml.mouse.Y), true, 1); } else if (activeTool == Tools.Rubber) { Color[] data = new Color[selectedFrame.layers[0].texture.Width * selectedFrame.layers[0].texture.Height]; selectedFrame.layers[0].texture.GetData(data); int x = (int)Sgml.round(Sgml.mouse.X - .5f); int y = (int)Sgml.round(Sgml.mouse.Y - .5f); int w = Math.Max(selectedFrame.layers[0].texture.Height, selectedFrame.layers[0].texture.Width); if (inTextureP(x, y)) { if (selectedFrame.layers[0].texture.Height * y + x >= 0 && selectedFrame.layers[0].texture.Height * y + x < selectedFrame.layers[0].texture.Width * selectedFrame.layers[0].texture.Height) { data[w * y + x] = Color.Transparent; selectedFrame.layers[0].texture.SetData(data); } occupiedPositions.Add(new Vector2((float)Sgml.round(Sgml.mouse.X - .5f) - x1, (float)Sgml.round(Sgml.mouse.Y - .5f) - y1)); } } else if (activeTool == Tools.Spray) { } else if (activeTool == Tools.SprayPaint) { } else if (activeTool == Tools.Star) { } else if (activeTool == Tools.Text) { } Sgml.surface_reset_target(); Sgml.draw_set_color(Color.White); UpdatePreview(selectedImageIndex); }
protected override void Draw() { if (parentForm != null) { ms = Mouse.GetState(); if (ttl < 10) { if (ttl == 9) { ScaleToFit(90); } ttl++; } base.Draw(); double framerate = Editor.GetFrameRate; Matrix transformMatrix = cam.Camera.GetViewMatrix(); MousePositionTranslated = cam.Camera.ScreenToWorld(new Vector2(ms.X, ms.Y)); BackgroundColor = Color.Black; Editor.graphics.Clear(BackgroundColor); Input.MousePosition = MousePositionTranslated; MousePosition = new Vector2(ms.X, ms.Y); Sgml.sb = Editor.spriteBatch; Sgml.vb = vertexBuffer; Sgml.be = basicEffect; Sgml.m = transformMatrix; Sgml.currentObject = representativeGameObject; Matrix view = cam.Camera.GetViewMatrix(); Matrix projection = m; basicEffect.World = world; basicEffect.View = view; basicEffect.Projection = projection; basicEffect.VertexColorEnabled = true; Sgml.mouse = MousePositionTranslated; // Actual logic int cellSize = 16; int x = 0; int y = 0; RectangleF rect = RectangleF.Empty; Color c1 = Color.FromNonPremultiplied(68, 68, 68, 255); Color c2 = Color.FromNonPremultiplied(77, 77, 77, 255); bool flag = true; bool lastFlag = flag; cam.Camera.Origin = new Vector2(Width / 2f, Height / 2f); basicEffect.View = Matrix.Identity; Sgml.m = Matrix.Identity; Vector2 origin = Vector2.Zero; Sgml.draw_surface(origin, gridSurface); basicEffect.View = view; Sgml.m = transformMatrix; if (selectedFrame != null) { Sgml.draw_set_aa(!parentForm.drawModeOn); if (selectedFrame.layers[0].texture != null) { Sgml.draw_surface(origin, selectedFrame.layers[selectedLayer].texture); Sgml.draw_surface(origin, selectedFrame.previewLayer.texture); } Sgml.draw_set_aa(true); // draw cells int xx = 0; int yy = 0; int xIndex = 0; int yIndex = 0; RectangleF temp = RectangleF.Empty; float x1 = origin.X; float y1 = origin.Y; float x2 = x1 + (int)parentForm.darkNumericUpDown1.Value; float y2 = y1 + (int)parentForm.darkNumericUpDown2.Value; Sgml.draw_set_alpha(0.8); Sgml.draw_set_color(Color.Black); for (var i = 0; i < parentForm.darkNumericUpDown1.Value + 1; i++) { Sgml.draw_line(x1, y1 + i, x2, y1 + i); } for (var i = 0; i < parentForm.darkNumericUpDown2.Value + 1; i++) { Sgml.draw_line(x1 + i, y1, x1 + i, y2); } Sgml.draw_set_alpha(1); Sgml.draw_set_color(Color.White); Sgml.draw_set_aa(false); if (selectedFrame.layers.Count > 0) { if (selectedFrame.layers[0].texture != null) { Sgml.draw_rectangle(origin, new Vector2(selectedFrame.layers[0].texture.Width, selectedFrame.layers[0].texture.Height), true); } } } basicEffect.View = Matrix.Identity; Sgml.m = Matrix.Identity; Sgml.draw_set_color(Color.White); Sgml.draw_text(new Vector2(10, 10), framerate.ToString()); Sgml.draw_text(new Vector2(10, 30), "[X: " + Sgml.round(Sgml.mouse.X - 0.5f) + " Y: " + Sgml.round(Sgml.mouse.Y - 0.5f) + "]"); // Sgml.draw_text(new Vector2(10, 50), parentForm.darkNumericUpDown1.Value.ToString()); Sgml.draw_text(new Vector2(10, 70), cam.Zoom.ToString()); Sgml.draw_text(new Vector2(10, 90), "DIR: " + Sgml.point_direction(toolOriginSubpixel, mouseSubpixel)); Sgml.draw_text(new Vector2(10, 110), "CLICK: " + toolOriginSubpixel.X + "x " + toolOriginSubpixel.Y + "y"); } }
protected override void Update(GameTime gameTime) { base.Update(gameTime); cam.UpdatePosition(); var originalPos = cam.Camera.Position; var originalZoom = cam.Camera.Zoom; Matrix view = cam.Camera.GetViewMatrix(); cam.Camera.Position = new Vector2(0, 0); cam.Camera.Zoom = 1; Matrix normalizedMatrix = cam.Camera.GetViewMatrix(); cam.Camera.Position = originalPos; cam.Camera.Zoom = originalZoom; Sgml.world = world; Sgml.view = view; Sgml.normalizedMatrix = normalizedMatrix; if (selectedFrame != null) { if (ms.LeftButton == ButtonState.Pressed ^ ms.RightButton == ButtonState.Pressed) { if (activeTool == Tools.Pixel || activeTool == Tools.Rubber || activeTool == Tools.Fill) { ToolDraw(ms.LeftButton == ButtonState.Pressed ? penColor : penColorRight); } } if ((ms.LeftButton == ButtonState.Released && msPrev.LeftButton == ButtonState.Pressed && toolPreview) ^ (ms.RightButton == ButtonState.Released && msPrev.RightButton == ButtonState.Pressed && toolPreview)) { if (activeTool == Tools.Ellipse || activeTool == Tools.Rectangle || activeTool == Tools.Line || activeTool == Tools.RoundedRectangle) { ToolDraw(msPrev.LeftButton == ButtonState.Pressed ? penColor : penColorRight); over = true; } toolPreview = false; } Sgml.draw_set_color(ms.LeftButton == ButtonState.Pressed ? penColor : penColorRight); // render preview toolOriginSubpixel.X = toolOrigin.X; //(float)Math.Floor(toolOrigin.X >= Sgml.mouse.X ? toolOrigin.X + .5f : toolOrigin.X - .5f); toolOriginSubpixel.Y = toolOrigin.Y; //(float)Math.Floor(toolOrigin.Y >= Sgml.mouse.Y ? toolOrigin.Y + .5f : toolOrigin.Y - .5f); mouseSubpixel.X = (float)Math.Floor(Sgml.mouse.X); mouseSubpixel.Y = (float)Math.Floor(Sgml.mouse.Y); mouseSubpixel.X += mouseSubpixel.X > toolOriginSubpixel.X ? 1 : 0; mouseSubpixel.Y += mouseSubpixel.Y > toolOriginSubpixel.Y ? 1 : 0; Sgml.surface_set_target(selectedFrame.previewLayer.texture); Sgml.draw_clear_transparent(); Sgml.draw_set_color(ms.LeftButton == ButtonState.Pressed ? penColor : penColorRight); if (activeTool == Tools.Pixel) { Sgml.draw_rectangle(new Vector2((float)Sgml.round(Sgml.mouse.X - .5f), (float)Sgml.round(Sgml.mouse.Y - .5f)), new Vector2((float)Sgml.round(Sgml.mouse.X + .5f), (float)Sgml.round(Sgml.mouse.Y + .5f)), false); } if (toolPreview) { if (activeTool == Tools.Ellipse) { Sgml.draw_ellipse(new Vector2(Sgml.mouse.X - toolOrigin.X, Sgml.mouse.Y - toolOrigin.Y), toolOrigin, 1); } else if (activeTool == Tools.Rectangle) { float my = Sgml.mouse.Y; my = (float)Sgml.floor(my) + .5f; Sgml.draw_rectangle(toolOrigin, new Vector2(Sgml.mouse.X, my), true); } else if (activeTool == Tools.Line) { Sgml.draw_rectangle(new Vector2((float)Sgml.round(toolOriginINP.X - .5f), (float)Sgml.round(toolOriginINP.Y - .5f)), new Vector2((float)Sgml.round(toolOriginINP.X + .5f), (float)Sgml.round(toolOriginINP.Y + .5f)), false); Sgml.draw_line(mouseSubpixel, toolOriginSubpixel); } else if (activeTool == Tools.RoundedRectangle) { Sgml.draw_roundrect(toolOrigin, new Vector2(Sgml.mouse.X, Sgml.mouse.Y), true, 1); } } Sgml.surface_reset_target(); if ((ms.LeftButton == ButtonState.Pressed && !toolPreview) ^ (ms.RightButton == ButtonState.Pressed && !toolPreview)) { if (!over) { toolOriginINP = new Vector2(Sgml.mouse.X, Sgml.mouse.Y); toolOrigin.X = (float)Sgml.round(Sgml.mouse.X - 0.5f) + .5f; toolOrigin.Y = (float)Sgml.round(Sgml.mouse.Y - 0.5f) + .5f; toolPreview = true; } else { over = false; } } } // Update frame if user pushed autoplay btn if (parentForm.darkButton33.Pushed) { animateIn--; if (animateIn <= 0) { animateIn = (int)parentForm.darkNumericUpDown5.Value; selectedImageIndex++; if (selectedImageIndex >= Frames.Count) { selectedImageIndex = 0; } selectedFrame = Frames[selectedImageIndex]; parentForm.darkImageIndex1.SelectedFrame = selectedImageIndex; parentForm.darkImageIndex1.Invalidate(); } } msPrev = ms; }