Full implementation of new Sprite code throughout solution. Added mouse controls for sprite movement in AnimationEditor.
This commit is contained in:
@@ -5,8 +5,10 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using MonoGame.Forms.Controls;
|
||||
using QURO;
|
||||
using QURO.Animation;
|
||||
|
||||
namespace AnimationEditor
|
||||
@@ -17,6 +19,17 @@ namespace AnimationEditor
|
||||
public AnimatedSprite PreviewSprite { get; set; }
|
||||
public float ZoomLevel { get; set; }
|
||||
|
||||
public Sprite EditSprite { get; set; }
|
||||
private bool hovering;
|
||||
private bool dragging;
|
||||
private MouseState mState;
|
||||
private MouseState mState_old;
|
||||
|
||||
private Vector2 centerPoint
|
||||
{
|
||||
get { return new Vector2((Editor.graphics.PresentationParameters.BackBufferWidth / 2) / ZoomLevel, (Editor.graphics.PresentationParameters.BackBufferHeight / 2) / ZoomLevel); }
|
||||
}
|
||||
|
||||
private EventHandler onFrameChanged;
|
||||
public event EventHandler FrameChanged
|
||||
{
|
||||
@@ -30,6 +43,19 @@ namespace AnimationEditor
|
||||
}
|
||||
}
|
||||
|
||||
private EventHandler onSpriteMoved;
|
||||
public event EventHandler SpriteMoved
|
||||
{
|
||||
add
|
||||
{
|
||||
onSpriteMoved += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
onSpriteMoved -= value;
|
||||
}
|
||||
}
|
||||
|
||||
private EventHandler onAnimationEnded;
|
||||
public event EventHandler AnimationEnded
|
||||
{
|
||||
@@ -67,6 +93,72 @@ namespace AnimationEditor
|
||||
OnFrameChanged(new EventArgs());
|
||||
}
|
||||
}
|
||||
else if(PreviewSprite != null)
|
||||
{
|
||||
MouseControl();
|
||||
}
|
||||
}
|
||||
|
||||
private void MouseControl()
|
||||
{
|
||||
mState = Mouse.GetState();
|
||||
if(EditSprite != null)
|
||||
{
|
||||
Rectangle spriteRect = new Rectangle((int)(centerPoint.X * ZoomLevel), (int)(centerPoint.Y * ZoomLevel), (int)(EditSprite.Bounds.Width * ZoomLevel), (int)(EditSprite.Bounds.Height * ZoomLevel));
|
||||
spriteRect.Offset((EditSprite.Offset - EditSprite.Origin) * ZoomLevel);
|
||||
|
||||
if (Bounds.Contains(mState.X, mState.Y))
|
||||
{
|
||||
if (spriteRect.Contains(mState.Position))
|
||||
{
|
||||
if (!hovering)
|
||||
{
|
||||
Cursor = System.Windows.Forms.Cursors.SizeAll;
|
||||
hovering = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (hovering && !dragging)
|
||||
{
|
||||
Cursor = System.Windows.Forms.Cursors.Default;
|
||||
hovering = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (mState.LeftButton == ButtonState.Pressed && mState_old.LeftButton != ButtonState.Pressed)
|
||||
dragging = hovering;
|
||||
else if (mState.LeftButton != ButtonState.Pressed)
|
||||
dragging = false;
|
||||
|
||||
if (dragging)
|
||||
{
|
||||
var mousePosScaled = (mState.Position.ToVector2() / ZoomLevel).ToPoint();
|
||||
var mousePosOldScaled = (mState_old.Position.ToVector2() / ZoomLevel).ToPoint();
|
||||
|
||||
Vector2 distMoved = new Vector2(mousePosScaled.X - mousePosOldScaled.X, mousePosScaled.Y - mousePosOldScaled.Y);
|
||||
|
||||
if (distMoved != Vector2.Zero)
|
||||
{
|
||||
EditSprite.Offset += distMoved;
|
||||
OnSpriteMoved(new EventArgs());
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (hovering)
|
||||
Cursor = System.Windows.Forms.Cursors.Default;
|
||||
hovering = false;
|
||||
dragging = false;
|
||||
}
|
||||
}
|
||||
mState_old = mState;
|
||||
}
|
||||
|
||||
protected virtual void OnSpriteMoved(EventArgs e)
|
||||
{
|
||||
onSpriteMoved?.Invoke(this, e);
|
||||
}
|
||||
|
||||
protected virtual void OnFrameChanged(EventArgs e)
|
||||
@@ -90,7 +182,7 @@ namespace AnimationEditor
|
||||
Editor.spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: scaleMatrix);
|
||||
else
|
||||
Editor.spriteBatch.Begin(transformMatrix: scaleMatrix);
|
||||
PreviewSprite?.Draw(Editor.spriteBatch, SpriteSheet, new Vector2((Editor.graphics.PresentationParameters.BackBufferWidth / 2) / ZoomLevel, (Editor.graphics.PresentationParameters.BackBufferHeight / 2) / ZoomLevel), Color.White);
|
||||
PreviewSprite?.Draw(Editor.spriteBatch, SpriteSheet, centerPoint, Color.White);
|
||||
Editor.spriteBatch.End();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user