Skip to content

Commit

Permalink
Moving with mouse
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczy93 committed Dec 27, 2023
1 parent 3d42e5b commit 6e277b0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 107 deletions.
131 changes: 26 additions & 105 deletions CentrED/Map/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using static CentrED.Application;
using MathHelper = Microsoft.Xna.Framework.MathHelper;

namespace CentrED.Map;

public class MapManager
{
private static readonly Matrix MilitaryProjection = Matrix.CreateRotationZ(-MathHelper.ToRadians(45));
private readonly GraphicsDevice _gfxDevice;

private MapEffect _mapEffect;
Expand Down Expand Up @@ -207,7 +209,7 @@ public MapManager(GraphicsDevice gd)
Tools.Add(new ElevateTool());
Tools.Add(new HueTool());

CEDGame.MapManager.ActiveTool = DefaultTool;
ActiveTool = DefaultTool;
}

public void ReloadShader()
Expand Down Expand Up @@ -271,71 +273,6 @@ public Point Position
}
}

private enum MouseDirection
{
North,
Northeast,
East,
Southeast,
South,
Southwest,
West,
Northwest
}

// This is all just a fast math way to figure out what the direction of the mouse is.
private MouseDirection ProcessMouseMovement(ref MouseState mouseState, out float distance)
{
Vector2 vec = new Vector2
(mouseState.X - (Camera.ScreenSize.Width / 2), mouseState.Y - (Camera.ScreenSize.Height / 2));

int hashf = 100 * (Math.Sign(vec.X) + 2) + 10 * (Math.Sign(vec.Y) + 2);

distance = vec.Length();
if (distance == 0)
{
return MouseDirection.North;
}

vec.X = Math.Abs(vec.X);
vec.Y = Math.Abs(vec.Y);

if (vec.Y * 5 <= vec.X * 2)
{
hashf += 1;
}
else if (vec.Y * 2 >= vec.X * 5)
{
hashf += 3;
}
else
{
hashf += 2;
}

switch (hashf)
{
case 111: return MouseDirection.Southwest;
case 112: return MouseDirection.West;
case 113: return MouseDirection.Northwest;
case 120: return MouseDirection.Southwest;
case 131: return MouseDirection.Southwest;
case 132: return MouseDirection.South;
case 133: return MouseDirection.Southeast;
case 210: return MouseDirection.Northwest;
case 230: return MouseDirection.Southeast;
case 311: return MouseDirection.Northeast;
case 312: return MouseDirection.North;
case 313: return MouseDirection.Northwest;
case 320: return MouseDirection.Northeast;
case 331: return MouseDirection.Northeast;
case 332: return MouseDirection.East;
case 333: return MouseDirection.Southeast;
}

return MouseDirection.North;
}

private readonly float WHEEL_DELTA = 1200f;

public Dictionary<int, TileObject> AllTiles = new();
Expand Down Expand Up @@ -439,6 +376,7 @@ public void RemoveTiles(ushort x, ushort y)
}

private MouseState _prevMouseState = Mouse.GetState();
private bool _mouseDrag;
private KeyboardState _prevKeyState = Keyboard.GetState();
public Rectangle ViewRange { get; private set; }

Expand All @@ -447,50 +385,24 @@ public void Update(GameTime gameTime, bool isActive, bool processMouse, bool pro
if (CEDGame.Closing)
return;
Metrics.Start("UpdateMap");
var mouseState = Mouse.GetState();
if (isActive && processMouse)
{
var mouseState = Mouse.GetState();

// if (mouse.RightButton == ButtonState.Pressed)
// {
// var direction = ProcessMouseMovement(ref mouse, out var distance);
//
// int delta = distance > 200 ? 10 : 5;
// switch (direction)
// {
// case MouseDirection.North:
// Camera.Move(0, -delta);
// break;
// case MouseDirection.Northeast:
// Camera.Move(delta, -delta);
// break;
// case MouseDirection.East:
// Camera.Move(delta, 0);
// break;
// case MouseDirection.Southeast:
// Camera.Move(delta, delta);
// break;
// case MouseDirection.South:
// Camera.Move(0, delta);
// break;
// case MouseDirection.Southwest:
// Camera.Move(-delta, delta);
// break;
// case MouseDirection.West:
// Camera.Move(-delta, 0);
// break;
// case MouseDirection.Northwest:
// Camera.Move(-delta, -delta);
// break;
// }
// }

if (mouseState.ScrollWheelValue != _prevMouseState.ScrollWheelValue)
{
var delta = (mouseState.ScrollWheelValue - _prevMouseState.ScrollWheelValue) / WHEEL_DELTA;
Camera.ZoomIn(delta);
}

if (mouseState.LeftButton == ButtonState.Pressed && ActiveTool == DefaultTool)
{
var oldPos = new Vector2(_prevMouseState.X - mouseState.X, _prevMouseState.Y - mouseState.Y);
if (oldPos != Vector2.Zero)
{
var newPos = Vector2.Transform(oldPos, MilitaryProjection);
Camera.Move(newPos.X, newPos.Y);
_mouseDrag = true;
}
}
if (Client.Running && _gfxDevice.Viewport.Bounds.Contains(new Point(mouseState.X, mouseState.Y)))
{
var newTilePos = Unproject(mouseState.X, mouseState.Y, VirtualLayerZ);
Expand All @@ -510,6 +422,11 @@ public void Update(GameTime gameTime, bool isActive, bool processMouse, bool pro
}
if (Selected != null)
{
if (!_mouseDrag && _prevMouseState.LeftButton == ButtonState.Pressed &&
mouseState.LeftButton == ButtonState.Released)
{
ActiveTool.OnMouseClicked(Selected);
}
if (mouseState.LeftButton == ButtonState.Pressed)
{
ActiveTool.OnMousePressed(Selected);
Expand All @@ -520,12 +437,16 @@ public void Update(GameTime gameTime, bool isActive, bool processMouse, bool pro
}
}
}
_prevMouseState = mouseState;
if (_mouseDrag && mouseState.LeftButton == ButtonState.Released)
{
_mouseDrag = false;
}
}
else
{
ActiveTool?.OnMouseLeave(Selected);
ActiveTool.OnMouseLeave(Selected);
}
_prevMouseState = mouseState;

if (isActive && processKeyboard)
{
Expand Down
12 changes: 10 additions & 2 deletions CentrED/Tools/SelectTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ public class SelectTool : Tool
{
public override string Name => "Select";

public override void OnMousePressed(TileObject? selected)
public override void OnMouseClicked(TileObject? o)
{
CEDGame.UIManager.InfoWindow.Selected = selected;
CEDGame.UIManager.InfoWindow.Selected = o;
if (o is StaticObject)
{
CEDGame.UIManager.TilesWindow.SelectedStaticId = o.Tile.Id;
}
else if (o is LandObject)
{
CEDGame.UIManager.TilesWindow.SelectedLandId = o.Tile.Id;
}
}

public override void OnActivated(TileObject? o)
Expand Down
5 changes: 5 additions & 0 deletions CentrED/Tools/Tool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public virtual void OnMousePressed(TileObject? o)
public virtual void OnMouseReleased(TileObject? o)
{
}

public virtual void OnMouseClicked(TileObject? o)
{

}

public virtual void OnVirtualLayerTile(Vector3 tilePos)
{
Expand Down

0 comments on commit 6e277b0

Please sign in to comment.