Exemplo n.º 1
0
 public void selectTile(Tile i)
 {
     SelectedTiles.Clear();
     if (i != null)
     {
         SelectedTiles.Add(i);
         SelectedLayer = i.layer;
         updateTreeViewSelection();
         MainWindow.Instance.propertyGrid1.SelectedObject = i;
     }
     else
     {
         selectLayer(SelectedLayer);
     }
 }
Exemplo n.º 2
0
        public void Update(GameTime gameTime)
        {
            if (map == null)
                return;

            oldkstate = kstate;
            oldmstate = mstate;
            kstate = Keyboard.GetState();
            mstate = Mouse.GetState();
            int mwheeldelta = mstate.ScrollWheelValue - oldmstate.ScrollWheelValue;

            if (mwheeldelta > 0)
            {
                float zoom = (float)Math.Round(camera.Scale * 10) * 10.0f + 10.0f;
                MainWindow.Instance.zoomCombo.Text = zoom.ToString() + "%";
                camera.Scale = zoom / 100.0f;
            }
            if (mwheeldelta < 0 )
            {
                float zoom = (float)Math.Round(camera.Scale * 10) * 10.0f - 10.0f;
                if (zoom <= 0.0f) return;
                MainWindow.Instance.zoomCombo.Text = zoom.ToString() + "%";
                camera.Scale = zoom / 100.0f;
            }

            //Camera movement
            float delta;
            if (kstate.IsKeyDown(Keys.LeftShift))
                delta = Preferences.Instance.CameraFastSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            else
                delta = Preferences.Instance.CameraSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (kstate.IsKeyDown(Keys.W) && kstate.IsKeyUp(Keys.LeftControl))
                camera.Position += (new Vector2(0, -delta));
            if (kstate.IsKeyDown(Keys.S) && kstate.IsKeyUp(Keys.LeftControl))
                camera.Position += (new Vector2(0, +delta));
            if (kstate.IsKeyDown(Keys.A) && kstate.IsKeyUp(Keys.LeftControl))
                camera.Position += (new Vector2(-delta, 0));
            if (kstate.IsKeyDown(Keys.D) && kstate.IsKeyUp(Keys.LeftControl))
                camera.Position += (new Vector2(+delta, 0));

            if (kstate.IsKeyDown(Keys.Subtract))
            {
                float zoom = (float)(camera.Scale * 0.995);
                MainWindow.Instance.zoomCombo.Text = (zoom * 100).ToString("###0.0") + "%";
                camera.Scale = zoom;
            }
            if (kstate.IsKeyDown(Keys.Add))
            {
                float zoom = (float)(camera.Scale * 1.005);
                MainWindow.Instance.zoomCombo.Text = (zoom * 100).ToString("###0.0") + "%";
                camera.Scale = zoom;
            }

            //get mouse world position considering the ScrollSpeed of the current layer
            Vector2 maincameraposition = camera.Position;
            if (SelectedLayer != null)
                camera.Position *= SelectedLayer.ScrollSpeed;
            mouseworldpos = Vector2.Transform(new Vector2(mstate.X, mstate.Y), Matrix.Invert(camera.matrix));
            mouseworldpos = mouseworldpos.Round();
            camera.Position = maincameraposition;

            if (state == EditorState.idle)
            {
                //get item under mouse cursor
                Tile item = getItemAtPos(mouseworldpos);
                if (item != null)
                {
                    item.onMouseOver(mouseworldpos);
                    if (kstate.IsKeyDown(Keys.LeftControl))
                        MainWindow.Instance.drawingBox.Cursor = cursorDup;
                }
                if (item != lastTile && lastTile != null)
                    lastTile.onMouseOut();
                lastTile = item;

                //LEFT MOUSE BUTTON CLICK
                if ((mstate.LeftButton == ButtonState.Pressed && oldmstate.LeftButton == ButtonState.Released) ||
                    (kstate.IsKeyDown(Keys.D1) && oldkstate.IsKeyUp(Keys.D1)))
                {
                    if (item != null) item.onMouseButtonDown(mouseworldpos);
                    if (kstate.IsKeyDown(Keys.LeftControl) && item != null)
                    {
                        if (!SelectedTiles.Contains(item))
                            selectTile(item);

                        beginCommand("Add Item(s)");

                        List<Tile> selecteditemscopy = new List<Tile>();
                        foreach (Tile selitem in SelectedTiles)
                        {
                            Tile i2 = (Tile)selitem.clone();
                            selecteditemscopy.Add(i2);
                        }
                        foreach (Tile selitem in selecteditemscopy)
                        {
                            selitem.Name = selitem.getNamePrefix() + map.getNextItemNumber();
                            addItem(selitem);
                        }
                        selectTile(selecteditemscopy[0]);
                        updateTreeView();
                        for (int i = 1; i < selecteditemscopy.Count; i++) SelectedTiles.Add(selecteditemscopy[i]);
                        startMoving();
                    }
                    else if (kstate.IsKeyDown(Keys.LeftShift) && item != null)
                    {
                        if (SelectedTiles.Contains(item)) SelectedTiles.Remove(item);
                        else SelectedTiles.Add(item);
                    }
                    else if (SelectedTiles.Contains(item))
                    {
                        beginCommand("Change Item(s)");
                        startMoving();
                    }
                    else if (!SelectedTiles.Contains(item))
                    {
                        selectTile(item);
                        if (item != null)
                        {
                            beginCommand("Change Item(s)");
                            startMoving();
                        }
                        else
                        {
                            grabbedpoint = mouseworldpos;
                            selectionrectangle = Rectangle.Empty;
                            state = EditorState.selecting;
                        }

                    }
                }

                //MIDDLE MOUSE BUTTON CLICK
                if ((mstate.MiddleButton == ButtonState.Pressed && oldmstate.MiddleButton == ButtonState.Released) ||
                    (kstate.IsKeyDown(Keys.D2) && oldkstate.IsKeyUp(Keys.D2)))
                {
                    if (item != null) item.onMouseOut();
                    if (kstate.IsKeyDown(Keys.LeftControl))
                    {
                        grabbedpoint = new Vector2(mstate.X, mstate.Y);
                        initialcampos = camera.Position;
                        state = EditorState.cameramoving;
                        MainWindow.Instance.drawingBox.Cursor = Forms.Cursors.SizeAll;
                    }
                    else
                    {
                        if (SelectedTiles.Count > 0)
                        {
                            grabbedpoint = mouseworldpos - SelectedTiles[0].pPosition;

                            //save the initial rotation for each item
                            initialrot.Clear();
                            foreach (Tile selitem in SelectedTiles)
                            {
                                initialrot.Add(selitem.Rotation);
                            }

                            state = EditorState.rotating;
                            MainWindow.Instance.drawingBox.Cursor = cursorRot;

                            beginCommand("Rotate Item(s)");
                        }
                    }
                }

                //RIGHT MOUSE BUTTON CLICK
                if ((mstate.RightButton == ButtonState.Pressed && oldmstate.RightButton == ButtonState.Released) ||
                    (kstate.IsKeyDown(Keys.D3) && oldkstate.IsKeyUp(Keys.D3)))
                {
                    if (item != null) item.onMouseOut();
                    if (SelectedTiles.Count > 0)
                    {
                        grabbedpoint = mouseworldpos - SelectedTiles[0].pPosition;

                        //save the initial scale for each item
                        initialscale.Clear();
                        foreach (Tile selitem in SelectedTiles)
                        {
                            initialscale.Add(selitem.Scale);
                        }

                        state = EditorState.scaling;
                        MainWindow.Instance.drawingBox.Cursor = cursorScale;

                        beginCommand("Scale Item(s)");
                    }
                }
            }

            if (state == EditorState.moving)
            {
                int i = 0;
                foreach (Tile selitem in SelectedTiles)
                {
                    newPosition = initialpos[i] + mouseworldpos - grabbedpoint;
                    if (Preferences.Instance.SnapToGrid || kstate.IsKeyDown(Keys.G))
                        newPosition = snapToGrid(newPosition);
                    drawSnappedPoint = false;
                    selitem.pPosition = newPosition;
                    i++;
                }
                MainWindow.Instance.propertyGrid1.Refresh();
                if ((mstate.LeftButton == ButtonState.Released && oldmstate.LeftButton == ButtonState.Pressed) ||
                    (kstate.IsKeyUp(Keys.D1) && oldkstate.IsKeyDown(Keys.D1)))
                {

                    foreach (Tile selitem in SelectedTiles)
                        selitem.onMouseButtonUp(mouseworldpos);

                    state = EditorState.idle;
                    MainWindow.Instance.drawingBox.Cursor = Forms.Cursors.Default;
                    if (mouseworldpos != grabbedpoint) endCommand(); else abortCommand();
                }
            }

            if (state == EditorState.rotating)
            {
                Vector2 newpos = mouseworldpos - SelectedTiles[0].pPosition;
                float deltatheta = (float)Math.Atan2(grabbedpoint.Y, grabbedpoint.X) - (float)Math.Atan2(newpos.Y, newpos.X);
                int i = 0;
                foreach (Tile selitem in SelectedTiles)
                {
                        selitem.pRotation = initialrot[i] - deltatheta;

                        if (kstate.IsKeyDown(Keys.LeftControl))
                        {
                            selitem.pRotation = ((float)Math.Round(selitem.pRotation / MathHelper.PiOver4) * MathHelper.PiOver4);
                        }
                        i++;
                }
                MainWindow.Instance.propertyGrid1.Refresh();
                if ((mstate.MiddleButton == ButtonState.Released && oldmstate.MiddleButton == ButtonState.Pressed) ||
                    (kstate.IsKeyUp(Keys.D2) && oldkstate.IsKeyDown(Keys.D2)))
                {
                    state = EditorState.idle;
                    MainWindow.Instance.drawingBox.Cursor = Forms.Cursors.Default;
                    endCommand();
                }
            }

            if (state == EditorState.scaling)
            {
                Vector2 newdistance = mouseworldpos - SelectedTiles[0].pPosition;
                float factor = newdistance.Length() / grabbedpoint.Length();
                int i = 0;
                foreach (Tile selitem in SelectedTiles)
                {
                        Vector2 newscale = initialscale[i];
                        if (!kstate.IsKeyDown(Keys.Y)) newscale.X = initialscale[i].X * (((factor - 1.0f) * 0.5f) + 1.0f);
                        if (!kstate.IsKeyDown(Keys.X)) newscale.Y = initialscale[i].Y * (((factor - 1.0f) * 0.5f) + 1.0f);
                        selitem.pScale = (newscale);

                        if (kstate.IsKeyDown(Keys.LeftControl))
                        {
                            Vector2 scale;
                            scale.X = (float)Math.Round(selitem.pScale.X * 10) / 10;
                            scale.Y = (float)Math.Round(selitem.pScale.Y * 10) / 10;
                            selitem.pScale = (scale);
                        }
                        i++;
                }
                MainWindow.Instance.propertyGrid1.Refresh();
                if ((mstate.RightButton == ButtonState.Released && oldmstate.RightButton == ButtonState.Pressed) ||
                    (kstate.IsKeyUp(Keys.D3) && oldkstate.IsKeyDown(Keys.D3)))
                {
                    state = EditorState.idle;
                    MainWindow.Instance.drawingBox.Cursor = Forms.Cursors.Default;
                    endCommand();
                }
            }

            if (state == EditorState.cameramoving)
            {
                Vector2 newpos = new Vector2(mstate.X, mstate.Y);
                Vector2 distance = (newpos - grabbedpoint) / camera.Scale;
                if (distance.Length() > 0)
                {
                    camera.Position = initialcampos - distance;
                }
                if (mstate.MiddleButton == ButtonState.Released)
                {
                    state = EditorState.idle;
                    MainWindow.Instance.drawingBox.Cursor = Forms.Cursors.Default;
                }
            }

            if (state == EditorState.selecting)
            {
                if (SelectedLayer == null) return;
                Vector2 distance = mouseworldpos - grabbedpoint;
                if (distance.Length() > 0)
                {
                    SelectedTiles.Clear();
                    selectionrectangle = Extensions.RectangleFromVectors(grabbedpoint, mouseworldpos);
                    foreach (Tile i in SelectedLayer.Tiles)
                    {
                        if (i.Visible && selectionrectangle.Contains((int)i.pPosition.X, (int)i.pPosition.Y)) SelectedTiles.Add(i);
                    }
                    updateTreeViewSelection();
                }
                if (mstate.LeftButton == ButtonState.Released)
                {
                    state = EditorState.idle;
                    MainWindow.Instance.drawingBox.Cursor = Forms.Cursors.Default;
                }
            }

            if (state == EditorState.brush)
            {
                if (Preferences.Instance.SnapToGrid || kstate.IsKeyDown(Keys.G))
                {
                    mouseworldpos = snapToGrid(mouseworldpos);
                }
                if (mstate.RightButton == ButtonState.Pressed && oldmstate.RightButton == ButtonState.Released) state = EditorState.idle;
                if (mstate.LeftButton == ButtonState.Pressed && oldmstate.LeftButton == ButtonState.Released) paintTextureBrush(true);
            }
        }
Exemplo n.º 3
0
 public void paintTextureBrush(bool continueAfterPaint)
 {
     if (SelectedLayer == null)
     {
         System.Windows.Forms.MessageBox.Show("No Layer is selected");
         destroyTextureBrush();
         return;
     }
     Tile i = new Tile(currentbrush.fullPath, new Vector2((int)mouseworldpos.X, (int)mouseworldpos.Y));
     i.Name = i.getNamePrefix() + map.getNextItemNumber();
     i.layer = SelectedLayer;
     beginCommand("Add Item \"" + i.Name + "\"");
     addItem(i);
     endCommand();
     updateTreeView();
     if (!continueAfterPaint) destroyTextureBrush();
 }
Exemplo n.º 4
0
 public void moveTileUp(Tile i)
 {
     int index = i.layer.Tiles.IndexOf(i);
     i.layer.Tiles[index] = i.layer.Tiles[index - 1];
     i.layer.Tiles[index - 1] = i;
 }
Exemplo n.º 5
0
 public void moveTileToLayer(Tile i1, TileLayer l2, Tile i2)
 {
     int index2 = i2 == null ? 0 : l2.Tiles.IndexOf(i2);
     i1.layer.Tiles.Remove(i1);
     l2.Tiles.Insert(index2, i1);
     i1.layer = l2;
 }
Exemplo n.º 6
0
 public void moveTileDown(Tile i)
 {
     int index = i.layer.Tiles.IndexOf(i);
     i.layer.Tiles[index] = i.layer.Tiles[index + 1];
     i.layer.Tiles[index + 1] = i;
     selectTile(i);
 }
Exemplo n.º 7
0
 public void addItem(Tile i)
 {
     if (!i.layer.Tiles.Contains(i))
         i.layer.Tiles.Add(i);
 }