Skip to content

Commit

Permalink
Add handling for map signs. Ensure that mouse cursor renderer and key…
Browse files Browse the repository at this point in the history
…board input don't work if a dialog is active.
  • Loading branch information
ethanmoffat committed Jan 23, 2021
1 parent e7ea02b commit 794c4ec
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 39 deletions.
10 changes: 9 additions & 1 deletion EOLib/Domain/Map/Sign.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ namespace EOLib.Domain.Map
{
public class Sign : ISign
{
public string Title { get; private set; }

public string Message { get; private set; }

public Sign(SignMapEntity sign)
{
Title = sign.Title;
Message = sign.Message;
}
}

public interface ISign
{
//todo: figure out properties
string Title { get; }

string Message { get; }
}
}
17 changes: 13 additions & 4 deletions EndlessClient/Controllers/MapInteractionController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Threading.Tasks;
using AutomaticTypeMapper;
using EndlessClient.ControlSets;
using EndlessClient.Dialogs.Factories;
using EndlessClient.HUD;
using EndlessClient.HUD.Controls;
using EndlessClient.HUD.Inventory;
Expand All @@ -21,21 +23,24 @@ public class MapInteractionController : IMapInteractionController
private readonly IStatusLabelSetter _statusLabelSetter;
private readonly IInventorySpaceValidator _inventorySpaceValidator;
private readonly IHudControlProvider _hudControlProvider;
private readonly IEOMessageBoxFactory _eoMessageBoxFactory;

public MapInteractionController(IMapActions mapActions,
ICurrentMapStateProvider currentMapStateProvider,
IStatusLabelSetter statusLabelSetter,
IInventorySpaceValidator inventorySpaceValidator,
IHudControlProvider hudControlProvider)
IHudControlProvider hudControlProvider,
IEOMessageBoxFactory eoMessageBoxFactory)
{
_mapActions = mapActions;
_currentMapStateProvider = currentMapStateProvider;
_statusLabelSetter = statusLabelSetter;
_inventorySpaceValidator = inventorySpaceValidator;
_hudControlProvider = hudControlProvider;
_eoMessageBoxFactory = eoMessageBoxFactory;
}

public void LeftClick(IMapCellState cellState, IMouseCursorRenderer mouseRenderer)
public async Task LeftClickAsync(IMapCellState cellState, IMouseCursorRenderer mouseRenderer)
{
var item = cellState.Items.OptionalFirst();
if (item.HasValue)
Expand All @@ -46,7 +51,11 @@ public void LeftClick(IMapCellState cellState, IMouseCursorRenderer mouseRendere
HandlePickupResult(_mapActions.PickUpItem(item.Value), item.Value);
}
else if (cellState.NPC.HasValue) { /* TODO: spell cast */ }
else if (cellState.Sign.HasValue) { /* TODO: sign interaction */ }
else if (cellState.Sign.HasValue)
{
var messageBox = _eoMessageBoxFactory.CreateMessageBox(cellState.Sign.Value.Message, cellState.Sign.Value.Title);
await messageBox.ShowDialogAsync();
}
else if (cellState.Chest.HasValue) { /* TODO: chest interaction */ }
else if (cellState.Character.HasValue) { /* TODO: character spell cast */ }
else if (cellState.InBounds)
Expand Down Expand Up @@ -95,7 +104,7 @@ private void HandlePickupResult(ItemPickupResult pickupResult, IItem item)

public interface IMapInteractionController
{
void LeftClick(IMapCellState cellState, IMouseCursorRenderer mouseRenderer);
Task LeftClickAsync(IMapCellState cellState, IMouseCursorRenderer mouseRenderer);

void RightClick(IMapCellState cellState);
}
Expand Down
8 changes: 4 additions & 4 deletions EndlessClient/Input/UserInputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
using EndlessClient.GameExecution;
using EOLib.Domain.Map;
using Microsoft.Xna.Framework;
using XNAControls;

namespace EndlessClient.Input
{
public class UserInputHandler : GameComponent, IUserInputHandler
public class UserInputHandler : XNAControl, IUserInputHandler
{
private readonly List<IInputHandler> _handlers;

Expand All @@ -20,7 +21,6 @@ public class UserInputHandler : GameComponent, IUserInputHandler
IFunctionKeyController functionKeyController,
ICurrentMapStateProvider currentMapStateProvider,
IHudControlProvider hudControlProvider)
: base((Game)endlessGameProvider.Game)
{
_handlers = new List<IInputHandler>
{
Expand All @@ -43,14 +43,14 @@ public class UserInputHandler : GameComponent, IUserInputHandler
};
}

public override void Update(GameTime gameTime)
protected override void OnUpdateControl(GameTime gameTime)
{
var timeAtBeginningOfUpdate = DateTime.Now;

foreach (var handler in _handlers)
handler.HandleKeyboardInput(timeAtBeginningOfUpdate);

base.Update(gameTime);
base.OnUpdateControl(gameTime);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class MouseCursorRendererFactory : IMouseCursorRendererFactory
private readonly IItemStringService _itemStringService;
private readonly IEIFFileProvider _eifFileProvider;
private readonly ICurrentMapProvider _currentMapProvider;
private readonly IGraphicsDeviceProvider _graphicsDeviceProvider;
private readonly IMapInteractionController _mapInteractionController;
private readonly IUserInputProvider _userInputProvider;

Expand All @@ -31,7 +30,6 @@ public class MouseCursorRendererFactory : IMouseCursorRendererFactory
IItemStringService itemStringService,
IEIFFileProvider eifFileProvider,
ICurrentMapProvider currentMapProvider,
IGraphicsDeviceProvider graphicsDeviceProvider,
IMapInteractionController mapInteractionController,
IUserInputProvider userInputProvider)
{
Expand All @@ -42,7 +40,6 @@ public class MouseCursorRendererFactory : IMouseCursorRendererFactory
_itemStringService = itemStringService;
_eifFileProvider = eifFileProvider;
_currentMapProvider = currentMapProvider;
_graphicsDeviceProvider = graphicsDeviceProvider;
_mapInteractionController = mapInteractionController;
_userInputProvider = userInputProvider;
}
Expand All @@ -56,7 +53,6 @@ public IMouseCursorRenderer Create()
_itemStringService,
_eifFileProvider,
_currentMapProvider,
_graphicsDeviceProvider,
_mapInteractionController,
_userInputProvider);
}
Expand Down
43 changes: 17 additions & 26 deletions EndlessClient/Rendering/MouseCursorRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EndlessClient.Controllers;
using EndlessClient.HUD;
using EndlessClient.Input;
Expand All @@ -21,7 +22,7 @@

namespace EndlessClient.Rendering
{
public class MouseCursorRenderer : IMouseCursorRenderer
public class MouseCursorRenderer : XNAControl, IMouseCursorRenderer
{
private enum CursorIndex
{
Expand All @@ -46,8 +47,6 @@ private enum CursorIndex
private readonly IUserInputProvider _userInputProvider;
private readonly XNALabel _mapItemText;

private readonly SpriteBatch _spriteBatch;

private Rectangle _drawArea;
private int _gridX, _gridY;
private CursorIndex _cursorIndex;
Expand All @@ -65,7 +64,6 @@ private enum CursorIndex
IItemStringService itemStringService,
IEIFFileProvider eifFileProvider,
ICurrentMapProvider currentMapProvider,
IGraphicsDeviceProvider graphicsDeviceProvider,
IMapInteractionController mapInteractionController,
IUserInputProvider userInputProvider)
{
Expand All @@ -92,20 +90,21 @@ private enum CursorIndex
AutoSize = false,
DrawOrder = 10 //todo: make a better provider for draw orders (see also HudControlsFactory)
};

_spriteBatch = new SpriteBatch(graphicsDeviceProvider.GraphicsDevice);
}

public void Initialize()
public override void Initialize()
{
_mapItemText.AddControlToDefaultGame();
}

#region Update and Helpers

public void Update(GameTime gameTime)
public override async void Update(GameTime gameTime)
{
//todo: don't do anything if there are dialogs or a context menu and mouse is over context menu
// prevents updates if there is a dialog
if (!ShouldUpdate()) return;

// todo: don't do anything if there is a context menu and mouse is over context menu

var offsetX = MainCharacterOffsetX();
var offsetY = MainCharacterOffsetY();
Expand All @@ -116,7 +115,7 @@ public void Update(GameTime gameTime)
var cellState = _mapCellStateProvider.GetCellStateAt(_gridX, _gridY);
UpdateCursorSourceRectangle(cellState);

CheckForClicks(cellState);
await CheckForClicks(cellState);
}

private void SetGridCoordsBasedOnMousePosition(int offsetX, int offsetY)
Expand Down Expand Up @@ -279,15 +278,15 @@ private static Color GetColorForMapDisplay(EIFRecord record)
return Color.White;
}

private void CheckForClicks(IMapCellState cellState)
private async Task CheckForClicks(IMapCellState cellState)
{
var currentMouseState = _userInputProvider.CurrentMouseState;
var previousMouseState = _userInputProvider.PreviousMouseState;

if (currentMouseState.LeftButton == ButtonState.Released &&
previousMouseState.LeftButton == ButtonState.Pressed)
{
_mapInteractionController.LeftClick(cellState, this);
await _mapInteractionController.LeftClickAsync(cellState, this);
}
else if (currentMouseState.RightButton == ButtonState.Released &&
previousMouseState.RightButton == ButtonState.Pressed)
Expand Down Expand Up @@ -335,21 +334,13 @@ public void AnimateClick()
_clickPositionArea = _drawArea;
}

~MouseCursorRenderer()
{
Dispose(false);
}

public void Dispose()
protected override void Dispose(bool disposing)
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
_spriteBatch.Dispose();
_mapItemText.Dispose();
if (disposing)
{
_spriteBatch.Dispose();
_mapItemText.Dispose();
}
}
}

Expand Down

0 comments on commit 794c4ec

Please sign in to comment.