示例#1
0
        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);
        }
示例#2
0
        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;
        }