Initial implementation of cursor movement and mouse snapping to cursor location.
This commit is contained in:
		| @@ -3,6 +3,8 @@ using System.Collections.Generic; | |||||||
| using Microsoft.Xna.Framework; | using Microsoft.Xna.Framework; | ||||||
| using xTile; | using xTile; | ||||||
| using StardewValley; | using StardewValley; | ||||||
|  | using StardewValley.Menus; | ||||||
|  | using stardew_access.Features; | ||||||
|  |  | ||||||
|  |  | ||||||
| namespace stardew_access.Features | namespace stardew_access.Features | ||||||
| @@ -11,15 +13,18 @@ namespace stardew_access.Features | |||||||
|     { |     { | ||||||
|  |  | ||||||
|         private Vector2 ViewingOffset = Vector2.Zero; |         private Vector2 ViewingOffset = Vector2.Zero; | ||||||
|  |         private Vector2 relativeOffsetLockPosition = Vector2.Zero; | ||||||
|  |         private Boolean relativeOffsetLock = false; | ||||||
|  |         private Vector2 prevPlayerPosition = Vector2.Zero, prevFacing = Vector2.Zero; | ||||||
|  |  | ||||||
|         private Vector2 PlayerFacingVector |         private Vector2 PlayerFacingVector | ||||||
|         { |         { | ||||||
|             get |             get | ||||||
|             { |             { | ||||||
| switch (Game1.player.FacingDirection) |                 switch (Game1.player.FacingDirection) | ||||||
|                 { |                 { | ||||||
|                     case 0: |                     case 0: | ||||||
|                         return new Vector2(0, - Game1.tileSize); |                         return new Vector2(0, -Game1.tileSize); | ||||||
|                     case 1: |                     case 1: | ||||||
|                         return new Vector2(Game1.tileSize, 0); |                         return new Vector2(Game1.tileSize, 0); | ||||||
|                     case 2: |                     case 2: | ||||||
| @@ -36,24 +41,55 @@ switch (Game1.player.FacingDirection) | |||||||
|         { |         { | ||||||
|             get |             get | ||||||
|             { |             { | ||||||
|                 int x = Game1.player.GetBoundingBox().Center.X - Game1.viewport.X; |                 int x = Game1.player.GetBoundingBox().Center.X; | ||||||
|                 int y = Game1.player.GetBoundingBox().Center.Y - Game1.viewport.Y; |                 int y = Game1.player.GetBoundingBox().Center.Y; | ||||||
|                 return new Vector2(x, y); |                 return new Vector2(x, y); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         private static (int, int) GetMapTileDimensions() |         private Vector2 getTileCursorPosition() | ||||||
|         { |         { | ||||||
|             Map map = Game1.currentLocation.map; |             Vector2 target = this.PlayerPosition; | ||||||
|             return (map.Layers[0].LayerWidth, map.Layers[0].LayerHeight); |             if (this.relativeOffsetLock) | ||||||
|  |             { | ||||||
|  |                 target += this.relativeOffsetLockPosition; | ||||||
|  |             } | ||||||
|  |             else | ||||||
|  |             { | ||||||
|  |                 target += this.PlayerFacingVector + this.ViewingOffset; | ||||||
|  |             } | ||||||
|  |             return target; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         public bool MoveTileView(Vector2 delta) |         private void cursorMoveInput(Vector2 delta, Boolean precise = false) | ||||||
|         { |         { | ||||||
|             Vector2 dest = this.PlayerPosition + this.PlayerFacingVector + this.ViewingOffset + delta; |             if (!tryMoveTileView(delta)) return; | ||||||
|  |             Vector2 position = this.getTileCursorPosition(); | ||||||
|  |             String ?name = TileInfo.getNameAtTile(position / Game1.tileSize); | ||||||
|  |             if (name == null) | ||||||
|  |             { | ||||||
|  |                 name = "empty tile"; | ||||||
|  |             } | ||||||
|  |             if (precise) | ||||||
|  |             { | ||||||
|  |                 MainClass.ScreenReader.Say($"{position.X}, {position.Y}", true); | ||||||
|  |             } | ||||||
|  |             else | ||||||
|  |             { | ||||||
|  |                 MainClass.ScreenReader.Say($"{name}, {(int)(position.X / Game1.tileSize)}, {(int)(position.Y / Game1.tileSize)}", true); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         private bool tryMoveTileView(Vector2 delta) | ||||||
|  |         { | ||||||
|  |             Vector2 dest = this.getTileCursorPosition() + delta; | ||||||
|             if (Utility.isOnScreen(dest, 0)) |             if (Utility.isOnScreen(dest, 0)) | ||||||
|             { |             { | ||||||
|                 this.ViewingOffset += delta; |                 if (this.relativeOffsetLock) | ||||||
|  |                     this.relativeOffsetLockPosition += delta; | ||||||
|  |                 else | ||||||
|  |                     this.ViewingOffset += delta; | ||||||
|  |  | ||||||
|                 return true; |                 return true; | ||||||
|             } |             } | ||||||
|             return false; |             return false; | ||||||
| @@ -61,24 +97,54 @@ switch (Game1.player.FacingDirection) | |||||||
|  |  | ||||||
|         private void SnapMouseToPlayer() |         private void SnapMouseToPlayer() | ||||||
|         { |         { | ||||||
|             Vector2 snapPosition = this.PlayerPosition + this.PlayerFacingVector + this.ViewingOffset; |             Vector2 cursorPosition = this.getTileCursorPosition(); | ||||||
|             Point snapPoint = new Point((int)snapPosition.X, (int)snapPosition.Y); |                         if (allowMouseSnap(cursorPosition)) | ||||||
|             if (Utility.isOnScreen(snapPoint, 0)) |                 Game1.setMousePosition((int)cursorPosition.X - Game1.viewport.X, (int)cursorPosition.Y - Game1.viewport.Y); | ||||||
|             Game1.setMousePosition(snapPoint.X, snapPoint.Y); |  | ||||||
|         } |         } | ||||||
|  |  | ||||||
| public void update() |         public void update() | ||||||
|         { |         { | ||||||
|  |             if (this.prevFacing != this.PlayerFacingVector || this.prevPlayerPosition != this.PlayerPosition) | ||||||
|  |             { | ||||||
|  |                 this.ViewingOffset = Vector2.Zero; | ||||||
|  |             } | ||||||
|  |             this.prevFacing = this.PlayerFacingVector; | ||||||
|  |             this.prevPlayerPosition = this.PlayerPosition; | ||||||
|             if (MainClass.Config.SnapMouse) |             if (MainClass.Config.SnapMouse) | ||||||
|             this.SnapMouseToPlayer(); |                 this.SnapMouseToPlayer(); | ||||||
|  |             if (MainClass.Config.TileCursorUpKey.JustPressed()) | ||||||
|  |             { | ||||||
|  |                 this.cursorMoveInput(new Vector2(0, -Game1.tileSize)); | ||||||
|  |             } | ||||||
|  |             else if (MainClass.Config.TileCursorRightKey.JustPressed()) | ||||||
|  |             { | ||||||
|  |                 this.cursorMoveInput(new Vector2(Game1.tileSize, 0)); | ||||||
|  |             } | ||||||
|  |             else if (MainClass.Config.TileCursorDownKey.JustPressed()) | ||||||
|  |             { | ||||||
|  |                 this.cursorMoveInput(new Vector2(0, Game1.tileSize)); | ||||||
|  |             } | ||||||
|  |             else if (MainClass.Config.TileCursorLeftKey.JustPressed()) | ||||||
|  |             { | ||||||
|  |                 this.cursorMoveInput(new Vector2(-Game1.tileSize, 0)); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|     private static bool IsTileOnMap(Vector2 tile) |  | ||||||
|     { |         private static bool allowMouseSnap(Vector2 point) | ||||||
|         (int width, int height) dimensions = GetMapTileDimensions(); |         { | ||||||
|         if (tile.X < 0 || tile.X >= dimensions.width) return false; |             if (!Utility.isOnScreen(point, 0)) return false; | ||||||
|         if (tile.Y < 0 || tile.Y >= dimensions.height) return false; |  | ||||||
|         return true; |             //prevent mousing over the toolbar or any other UI component with the tile cursor | ||||||
|  |             foreach (IClickableMenu menu in Game1.onScreenMenus) | ||||||
|  |             { | ||||||
|  |                 if (menu.allClickableComponents == null) continue; | ||||||
|  |                 foreach (ClickableComponent component in menu.allClickableComponents) | ||||||
|  |                 { | ||||||
|  |                     if (component.containsPoint((int)point.X, (int)point.Y)) return false; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             return true; | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
| } |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user