public static SpriteboxJson ToJsonElement(Spritebox spriteBox) { if (spriteBox == null) { return(null); } SpriteboxJson sprBoxJson = new SpriteboxJson() { posX = spriteBox.GetPosition().X, posY = spriteBox.GetPosition().Y, width = spriteBox.GetWidth(), height = spriteBox.GetHeight(), rotation = spriteBox._rotation, sourceHeight = spriteBox.GetSourceRectangle().Height, sourceWidth = spriteBox.GetSourceRectangle().Width, sourceX = spriteBox.GetSourceRectangle().X, sourceY = spriteBox.GetSourceRectangle().Y, textureKey = spriteBox.GetTextureKey(), layer = spriteBox.GetLayer(), visible = spriteBox.Visible() }; return(sprBoxJson); }
public override void MouseMove(object sender, MouseEventArgs e, Vector2 worldMousePosition) { if (e == null) { return; } if (_selectionBox.GetBoundObject() == null) { return; } // Resizing object if (_selected) { // Vertical-Top if (VerticalPos == ResizeBoxEnumVertical.TOP) { Vector2 NewPos = new Vector2(_selectionBox.GetBoundObject().GetPosition().X, worldMousePosition.Y + _distanceOnSelect.Y + GetHoverBox().Width / 2); Vector2 SprOldPos = _selectionBox.GetBoundObject().GetPosition(); Spritebox sprBox = _selectionBox.GetBoundObject(); sprBox.SetPosition(NewPos); int posDifference = ((int)sprBox.GetPosition().Y - (int)SprOldPos.Y); sprBox.SetHeight(sprBox.GetHeight() - posDifference); sprBox.SetSourceRectangle(new Rectangle(sprBox.GetSourceRectangle().X, sprBox.GetSourceRectangle().Y + posDifference, sprBox.GetSourceRectangle().Width, sprBox.GetHeight())); } // Vertical-Bottom if (VerticalPos == ResizeBoxEnumVertical.BOTTOM) { Vector2 SprOldPos = _selectionBox.GetBoundObject().GetPosition(); Spritebox sprBox = _selectionBox.GetBoundObject(); int posDifference = ((int)(worldMousePosition.Y) - (int)oldMousePos.Y); Rectangle newRect = sprBox.GetRectangle(); sprBox.SetHeight((int)worldMousePosition.Y - (int)sprBox.GetPosition().Y + (int)_distanceOnSelect.Y + GetHoverBox().Width / 2); sprBox.SetSourceRectangle(new Rectangle(sprBox.GetSourceRectangle().X, sprBox.GetSourceRectangle().Y, sprBox.GetSourceRectangle().Width, sprBox.GetHeight())); } // Horizontal-Left if (HorizontalPos == ResizeBoxEnumHorizontal.LEFT) { Vector2 NewPos = new Vector2(worldMousePosition.X + _distanceOnSelect.X + GetHoverBox().Width / 2, _selectionBox.GetBoundObject().GetPosition().Y); Vector2 SprOldPos = _selectionBox.GetBoundObject().GetPosition(); Spritebox sprBox = _selectionBox.GetBoundObject(); sprBox.SetPosition(NewPos); int posDifference = ((int)sprBox.GetPosition().X - (int)SprOldPos.X); sprBox.SetWidth(sprBox.GetWidth() - posDifference); sprBox.SetSourceRectangle(new Rectangle(sprBox.GetSourceRectangle().X + posDifference, sprBox.GetSourceRectangle().Y, sprBox.GetWidth(), sprBox.GetSourceRectangle().Height)); } // Horizontal-Right if (HorizontalPos == ResizeBoxEnumHorizontal.RIGHT) { Vector2 SprOldPos = _selectionBox.GetBoundObject().GetPosition(); Spritebox sprBox = _selectionBox.GetBoundObject(); int posDifference = ((int)(worldMousePosition.X) - (int)oldMousePos.X); Rectangle newRect = sprBox.GetRectangle(); sprBox.SetWidth((int)worldMousePosition.X - (int)sprBox.GetPosition().X + (int)_distanceOnSelect.X + GetHoverBox().Width / 2); sprBox.SetSourceRectangle(new Rectangle(sprBox.GetSourceRectangle().X, sprBox.GetSourceRectangle().Y, sprBox.GetWidth(), sprBox.GetSourceRectangle().Height)); } } oldMousePos = worldMousePosition; base.MouseMove(sender, e, worldMousePosition); }
public override void Draw(GraphicsDevice device, SpriteBatch spriteBatch) { if (_boundObject == null || spriteBatch == null) { return; } SpriteRenderer.DrawRectangle(spriteBatch, device, new Rectangle((int)GetPosition().X, (int)GetPosition().Y, _boundObject.GetWidth(), _boundObject.GetHeight()), Color.White); spriteBatch.DrawString(MainWindowViewModel.MonogameWindow.DefaultFont, $"{_boundObject.GetRectangle().Width}, {_boundObject.GetRectangle().Height}", new Vector2((int)_boundObject.GetPosition().X, (int)_boundObject.GetPosition().Y - 15), Color.White); base.Draw(device, spriteBatch); }
/// <summary> /// Overridable, allows us to control how to draw this object directly. /// Not recommended if you can achieve your goal with more higher level methods. /// </summary> /// <param name="device"></param> /// <param name="spriteBatch"></param> public virtual void Draw(GraphicsDevice device, SpriteBatch spriteBatch) { var orderedSprites = SpriteBoxes.OrderBy(f => f.Value.GetLayer()).ToList(); foreach (KeyValuePair <string, Spritebox> pair in orderedSprites) { Spritebox spriteBox = pair.Value; if (spriteBatch != null && ContentManager.GetTexture(spriteBox.GetTextureKey()) != null && spriteBox.Visible()) { spriteBatch.Draw(ContentManager.GetTexture(spriteBox.GetTextureKey()), new Rectangle((int)(spriteBox.GetPosition().X + GetPosition().X), (int)(spriteBox.GetPosition().Y + GetPosition().Y), spriteBox.GetWidth(), spriteBox.GetHeight()), spriteBox.GetSourceRectangle(), Color.White); } } // Draw children foreach (Drawable d in _children) { d.Draw(device, spriteBatch); } }