private void ItemSelector_Load(object sender, EventArgs e) { treeView1.Nodes.Add((TreeNode)MainForm.Instance.treeView1.Nodes[0].Clone()); treeView1.ImageList = MainForm.Instance.treeView1.ImageList; treeView1.ImageIndex = treeView1.SelectedImageIndex = 5; treeView1.Nodes[0].Expand(); if (Value != null) { TreeNode[] nodes = treeView1.Nodes.Find(Value.Name, true); if (nodes.Length > 0) { treeView1.SelectedNode = nodes[0]; } else { Value = null; } } }
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { if (e.Node.Tag is MapObject) Value = (MapObject)e.Node.Tag; }
public void update(GameTime gt) { if (level == null) return; oldkstate = kstate; oldmstate = mstate; kstate = Keyboard.GetState(); mstate = Mouse.GetState(); int mwheeldelta = mstate.ScrollWheelValue - oldmstate.ScrollWheelValue; if (mwheeldelta > 0 /* && kstate.IsKeyDown(Keys.LeftControl)*/) { float zoom = (float)Math.Round(camera.Scale * 10) * 10.0f + 10.0f; MainForm.Instance.zoomcombo.Text = zoom.ToString() + "%"; camera.Scale = zoom / 100.0f; } if (mwheeldelta < 0 /* && kstate.IsKeyDown(Keys.LeftControl)*/) { float zoom = (float)Math.Round(camera.Scale * 10) * 10.0f - 10.0f; if (zoom <= 0.0f) return; MainForm.Instance.zoomcombo.Text = zoom.ToString() + "%"; camera.Scale = zoom / 100.0f; } //Camera movement float delta; if (kstate.IsKeyDown(Keys.LeftShift)) delta = Constants.Instance.CameraFastSpeed * (float)gt.ElapsedGameTime.TotalSeconds; else delta = Constants.Instance.CameraSpeed * (float)gt.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); MainForm.Instance.zoomcombo.Text = (zoom * 100).ToString("###0.0") + "%"; camera.Scale = zoom; } if (kstate.IsKeyDown(Keys.Add)) { float zoom = (float)(camera.Scale * 1.005); MainForm.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(); MainForm.Instance.toolStripStatusLabel3.Text = "Mouse: (" + mouseworldpos.X + ", " + mouseworldpos.Y + ")"; camera.Position = maincameraposition; if (state == EditorState.idle) { //get item under mouse cursor customEntity = false; MapObject item = getItemAtPos(mouseworldpos); if (item != null) { MainForm.Instance.toolStripStatusLabel1.Text = item.Name; item.onMouseOver(mouseworldpos); if (kstate.IsKeyDown(Keys.LeftControl)) MainForm.Instance.pictureBox1.Cursor = cursorDup; } else { MainForm.Instance.toolStripStatusLabel1.Text = ""; } if (item != lastitem && lastitem != null) lastitem.onMouseOut(); lastitem = 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 (!SelectedItems.Contains(item)) selectitem(item); beginCommand("Add MapObject(s)"); List<MapObject> selecteditemscopy = new List<MapObject>(); foreach (MapObject selitem in SelectedItems) { MapObject i2 = (MapObject)selitem.clone(); selecteditemscopy.Add(i2); } foreach (MapObject selitem in selecteditemscopy) { // selitem.Name = selitem.getNamePrefix() + level.getNextItemNumber(); int nextnum = level.getNextItemNumber(); selitem.Name = selitem.Name.TrimEnd(toTrim)+"_"+nextnum;//.TrimStart(trimStart); selitem.Id = generateID(nextnum); addItem(selitem); } selectitem(selecteditemscopy[0]); updatetreeview(); for (int i = 1; i < selecteditemscopy.Count; i++) SelectedItems.Add(selecteditemscopy[i]); startMoving(); } else if (kstate.IsKeyDown(Keys.LeftShift) && item != null) { if (SelectedItems.Contains(item)) SelectedItems.Remove(item); else SelectedItems.Add(item); } else if (SelectedItems.Contains(item)) { beginCommand("Change MapObject(s)"); startMoving(); } else if (!SelectedItems.Contains(item)) { selectitem(item); if (item != null) { beginCommand("Change MapObject(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; MainForm.Instance.pictureBox1.Cursor = Forms.Cursors.SizeAll; } else { if (SelectedItems.Count > 0) { grabbedpoint = mouseworldpos - SelectedItems[0].pPosition; //save the initial rotation for each item initialrot.Clear(); foreach (MapObject selitem in SelectedItems) { if (selitem.CanRotate()) { initialrot.Add(selitem.getRotation()); } } state = EditorState.rotating; MainForm.Instance.pictureBox1.Cursor = cursorRot; beginCommand("Rotate MapObject(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 (SelectedItems.Count > 0) { grabbedpoint = mouseworldpos - SelectedItems[0].pPosition; //save the initial scale for each item initialscale.Clear(); foreach (MapObject selitem in SelectedItems) { if (selitem.CanScale()) { initialscale.Add(selitem.getScale()); } } state = EditorState.scaling; MainForm.Instance.pictureBox1.Cursor = cursorScale; beginCommand("Scale MapObject(s)"); } } if (kstate.IsKeyDown(Keys.H) && oldkstate.GetPressedKeys().Length == 0 && SelectedItems.Count > 0) { beginCommand("Flip MapObject(s) Horizontally"); foreach (MapObject selitem in SelectedItems) { if (selitem is TileObject) { TileObject ti = (TileObject)selitem; ti.FlipHorizontally = !ti.FlipHorizontally; } } MainForm.Instance.propertyGrid1.Refresh(); endCommand(); } if (kstate.IsKeyDown(Keys.V) && oldkstate.GetPressedKeys().Length == 0 && SelectedItems.Count > 0) { beginCommand("Flip MapObject(s) Vertically"); foreach (MapObject selitem in SelectedItems) { if (selitem is TileObject) { TileObject ti = (TileObject)selitem; ti.FlipVertically = !ti.FlipVertically; } } MainForm.Instance.propertyGrid1.Refresh(); endCommand(); } } if (state == EditorState.moving) { int i = 0; foreach (MapObject selitem in SelectedItems) { newPosition = initialpos[i] + mouseworldpos - grabbedpoint; if (Constants.Instance.SnapToGrid || kstate.IsKeyDown(Keys.G)) newPosition = snapToGrid(newPosition); drawSnappedPoint = false; selitem.setPosition(newPosition); i++; } MainForm.Instance.propertyGrid1.Refresh(); if ((mstate.LeftButton == ButtonState.Released && oldmstate.LeftButton == ButtonState.Pressed) || (kstate.IsKeyUp(Keys.D1) && oldkstate.IsKeyDown(Keys.D1))) { foreach (MapObject selitem in SelectedItems) selitem.onMouseButtonUp(mouseworldpos); state = EditorState.idle; MainForm.Instance.pictureBox1.Cursor = Forms.Cursors.Default; if (mouseworldpos != grabbedpoint) endCommand(); else abortCommand(); } } if (state == EditorState.rotating) { Vector2 newpos = mouseworldpos - SelectedItems[0].pPosition; float deltatheta = (float)Math.Atan2(grabbedpoint.Y, grabbedpoint.X) - (float)Math.Atan2(newpos.Y, newpos.X); int i = 0; foreach (MapObject selitem in SelectedItems) { if (selitem.CanRotate()) { selitem.setRotation(initialrot[i] - deltatheta); if (kstate.IsKeyDown(Keys.LeftControl)) { selitem.setRotation((float)Math.Round(selitem.getRotation() / MathHelper.PiOver4) * MathHelper.PiOver4); } i++; } } MainForm.Instance.propertyGrid1.Refresh(); if ((mstate.MiddleButton == ButtonState.Released && oldmstate.MiddleButton == ButtonState.Pressed) || (kstate.IsKeyUp(Keys.D2) && oldkstate.IsKeyDown(Keys.D2))) { state = EditorState.idle; MainForm.Instance.pictureBox1.Cursor = Forms.Cursors.Default; endCommand(); } } if (state == EditorState.scaling) { Vector2 newdistance = mouseworldpos - SelectedItems[0].pPosition; float factor = newdistance.Length() / grabbedpoint.Length(); int i = 0; foreach (MapObject selitem in SelectedItems) { if (selitem.CanScale()) { if (selitem is TileObject) { MainForm.Instance.toolStripStatusLabel1.Text = "Hold down [X] or [Y] to limit scaling to the according dimension."; } 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.setScale(newscale); if (kstate.IsKeyDown(Keys.LeftControl)) { Vector2 scale; scale.X = (float)Math.Round(selitem.getScale().X * 10) / 10; scale.Y = (float)Math.Round(selitem.getScale().Y * 10) / 10; selitem.setScale(scale); } i++; } } MainForm.Instance.propertyGrid1.Refresh(); if ((mstate.RightButton == ButtonState.Released && oldmstate.RightButton == ButtonState.Pressed) || (kstate.IsKeyUp(Keys.D3) && oldkstate.IsKeyDown(Keys.D3))) { state = EditorState.idle; MainForm.Instance.pictureBox1.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; MainForm.Instance.pictureBox1.Cursor = Forms.Cursors.Default; } } if (state == EditorState.selecting) { if (SelectedLayer == null) return; Vector2 distance = mouseworldpos - grabbedpoint; if (distance.Length() > 0) { SelectedItems.Clear(); selectionrectangle = Extensions.RectangleFromVectors(grabbedpoint, mouseworldpos); foreach (MapObject i in SelectedLayer.MapObjects) { if (i.Visible && selectionrectangle.Contains((int)i.pPosition.X, (int)i.pPosition.Y)) SelectedItems.Add(i); } updatetreeviewselection(); } if (mstate.LeftButton == ButtonState.Released) { state = EditorState.idle; MainForm.Instance.pictureBox1.Cursor = Forms.Cursors.Default; } } if (state == EditorState.brush) { if (Constants.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); } if (state == EditorState.brush_primitive) { if (Constants.Instance.SnapToGrid || kstate.IsKeyDown(Keys.G)) mouseworldpos = snapToGrid(mouseworldpos); if (kstate.IsKeyDown(Keys.LeftControl) && primitivestarted && currentprimitive == PrimitiveType.CollisionRectangle) { Vector2 distance = mouseworldpos - clickedPoints[0]; float squareside = Math.Max(distance.X, distance.Y); mouseworldpos = clickedPoints[0] + new Vector2(squareside, squareside); } if ((mstate.LeftButton == ButtonState.Pressed && oldmstate.LeftButton == ButtonState.Released) || (kstate.IsKeyDown(Keys.D1) && oldkstate.IsKeyUp(Keys.D1))) { clickedPoints.Add(mouseworldpos); if (primitivestarted == false) { primitivestarted = true; switch (currentprimitive) { case PrimitiveType.CollisionRectangle: MainForm.Instance.toolStripStatusLabel1.Text = Resources.Rectangle_Started; break; case PrimitiveType.Circle: MainForm.Instance.toolStripStatusLabel1.Text = Resources.Circle_Started; break; case PrimitiveType.Path: MainForm.Instance.toolStripStatusLabel1.Text = Resources.Path_Started; break; } } else { if (currentprimitive != PrimitiveType.Path) { paintPrimitiveBrush(); clickedPoints.Clear(); primitivestarted = false; } } } if (kstate.IsKeyDown(Keys.Back) && oldkstate.IsKeyUp(Keys.Back)) { if (currentprimitive == PrimitiveType.Path && clickedPoints.Count > 1) { clickedPoints.RemoveAt(clickedPoints.Count-1); } } if ((mstate.MiddleButton == ButtonState.Pressed && oldmstate.MiddleButton == ButtonState.Released) || (kstate.IsKeyDown(Keys.D2) && oldkstate.IsKeyUp(Keys.D2))) { if (currentprimitive == PrimitiveType.Path && primitivestarted) { paintPrimitiveBrush(); clickedPoints.Clear(); primitivestarted = false; MainForm.Instance.toolStripStatusLabel1.Text = Resources.Path_Entered; } } if ((mstate.RightButton == ButtonState.Pressed && oldmstate.RightButton == ButtonState.Released) || (kstate.IsKeyDown(Keys.D3) && oldkstate.IsKeyUp(Keys.D3))) { if (primitivestarted) { clickedPoints.Clear(); primitivestarted = false; switch (currentprimitive) { case PrimitiveType.CollisionRectangle: MainForm.Instance.toolStripStatusLabel1.Text = Resources.Rectangle_Entered; break; case PrimitiveType.Circle: MainForm.Instance.toolStripStatusLabel1.Text = Resources.Circle_Entered; break; case PrimitiveType.Path: MainForm.Instance.toolStripStatusLabel1.Text = Resources.Path_Entered; break; } } else { destroyPrimitiveBrush(); clickedPoints.Clear(); primitivestarted = false; } } } }
public ItemSelector(MapObject item) { InitializeComponent(); Value = item; }
public void selectitem(MapObject i) { SelectedItems.Clear(); if (i != null) { SelectedItems.Add(i); SelectedLayer = i.layer; updatetreeviewselection(); MainForm.Instance.propertyGrid1.SelectedObject = i; } else { selectlayer(SelectedLayer); } }
public void moveItemUp(MapObject i) { int index = i.layer.MapObjects.IndexOf(i); i.layer.MapObjects[index] = i.layer.MapObjects[index - 1]; i.layer.MapObjects[index - 1] = i; //updatetreeview(); }
public void moveItemToLayer(MapObject i1, Layer l2, MapObject i2) { int index2 = i2 == null ? 0 : l2.MapObjects.IndexOf(i2); i1.layer.MapObjects.Remove(i1); l2.MapObjects.Insert(index2, i1); i1.layer = l2; }
public void moveItemDown(MapObject i) { int index = i.layer.MapObjects.IndexOf(i); i.layer.MapObjects[index] = i.layer.MapObjects[index + 1]; i.layer.MapObjects[index + 1] = i; selectitem(i); //updatetreeview(); }
public void addItem(MapObject i) { if (!i.layer.MapObjects.Contains(i)) i.layer.MapObjects.Add(i); }