Skip to content

Commit

Permalink
Re-implement textinputdialog. Implement Add logic for friend/ignore l…
Browse files Browse the repository at this point in the history
…ists.
  • Loading branch information
ethanmoffat committed Mar 21, 2022
1 parent 2b9e91a commit 883eeb7
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 72 deletions.
4 changes: 2 additions & 2 deletions EndlessClient/Dialogs/Actions/InGameDialogActions.cs
Expand Up @@ -22,7 +22,7 @@ public void ShowFriendListDialog()
_activeDialogRepository.FriendIgnoreDialog.MatchNone(() =>
{
var dlg = _friendIgnoreListDialogFactory.Create(isFriendList: true);
dlg.CloseAction += (_, _) => _activeDialogRepository.FriendIgnoreDialog = Option.None<ScrollingListDialog>();
dlg.DialogClosed += (_, _) => _activeDialogRepository.FriendIgnoreDialog = Option.None<ScrollingListDialog>();
_activeDialogRepository.FriendIgnoreDialog = Option.Some(dlg);
dlg.Show();
Expand All @@ -34,7 +34,7 @@ public void ShowIgnoreListDialog()
_activeDialogRepository.FriendIgnoreDialog.MatchNone(() =>
{
var dlg = _friendIgnoreListDialogFactory.Create(isFriendList: false);
dlg.CloseAction += (_, _) => _activeDialogRepository.FriendIgnoreDialog = Option.None<ScrollingListDialog>();
dlg.DialogClosed += (_, _) => _activeDialogRepository.FriendIgnoreDialog = Option.None<ScrollingListDialog>();
_activeDialogRepository.FriendIgnoreDialog = Option.Some(dlg);
dlg.Show();
Expand Down
116 changes: 62 additions & 54 deletions EndlessClient/Dialogs/Factories/FriendIgnoreListDialogFactory.cs
Expand Up @@ -10,6 +10,7 @@
using EOLib.Localization;
using System;
using System.Linq;
using XNAControls;

namespace EndlessClient.Dialogs.Factories
{
Expand All @@ -22,50 +23,48 @@ public class FriendIgnoreListDialogFactory : IFriendIgnoreListDialogFactory
private readonly ILocalizedStringFinder _localizedStringFinder;
private readonly ICharacterProvider _characterProvider;
private readonly IHudControlProvider _hudControlProvider;
private readonly ITextInputDialogFactory _textInputDialogFactory;
private readonly IEOMessageBoxFactory _eoMessageBoxFactory;

public FriendIgnoreListDialogFactory(IGameStateProvider gameStateProvider,
INativeGraphicsManager nativeGraphicsManager,
IEODialogButtonService dialogButtonService,
ILocalizedStringFinder localizedStringFinder,
ICharacterProvider characterProvider,
IHudControlProvider hudControlProvider)
IHudControlProvider hudControlProvider,
ITextInputDialogFactory textInputDialogFactory,
IEOMessageBoxFactory eoMessageBoxFactory)
{
_gameStateProvider = gameStateProvider;
_nativeGraphicsManager = nativeGraphicsManager;
_dialogButtonService = dialogButtonService;
_localizedStringFinder = localizedStringFinder;
_characterProvider = characterProvider;
_hudControlProvider = hudControlProvider;
_textInputDialogFactory = textInputDialogFactory;
_eoMessageBoxFactory = eoMessageBoxFactory;
}

public ScrollingListDialog Create(bool isFriendList)
{
// todo: refactor InteractList
var textFileLines = isFriendList ? InteractList.LoadAllFriend() : InteractList.LoadAllIgnore();
var friendOrIgnoreStr = _localizedStringFinder.GetString(isFriendList ? EOResourceID.STATUS_LABEL_FRIEND_LIST : EOResourceID.STATUS_LABEL_IGNORE_LIST);

var dialog = new ScrollingListDialog(_gameStateProvider, _nativeGraphicsManager, _dialogButtonService)
{
Title = $"{_characterProvider.MainCharacter.Name}'s {friendOrIgnoreStr} [{textFileLines.Count}]",
Buttons = ScrollingListDialogButtons.AddCancel,
ListItemType = ListDialogItem.ListItemStyle.Small,
};

var listItems = textFileLines.Select(x => new ListDialogItem(dialog, ListDialogItem.ListItemStyle.Small) { PrimaryText = x }).ToList();
foreach (var item in listItems)
{
item.OnLeftClick += (o, e) => _hudControlProvider.GetComponent<ChatTextBox>(HudControlIdentifier.ChatTextBox).Text = $"!{item.PrimaryText} ";
item.OnRightClick += (o, e) =>
{
dialog.RemoveFromList(item);
dialog.Title = $"{_characterProvider.MainCharacter.Name}'s {friendOrIgnoreStr} [{dialog.NamesList.Count}]";
};
}
SetClickEventHandlers(item, dialog, isFriendList);
dialog.SetItemList(listItems);

dialog.AddAction += InvokeAdd;
dialog.Title = GetDialogTitle(dialog, isFriendList);

dialog.CloseAction += (_, _) =>
dialog.AddAction += (_, _) => InvokeAdd(isFriendList, dialog);
dialog.DialogClosing += (_, _) =>
{
if (isFriendList)
InteractList.WriteFriendList(dialog.NamesList);
Expand All @@ -76,48 +75,57 @@ public ScrollingListDialog Create(bool isFriendList)
return dialog;
}

private void InvokeAdd(object sender, EventArgs e)
private void InvokeAdd(bool isFriendList, ScrollingListDialog parentDialog)
{
string prompt = _localizedStringFinder.GetString(isFriendList ? EOResourceID.DIALOG_WHO_TO_MAKE_FRIEND : EOResourceID.DIALOG_WHO_TO_MAKE_IGNORE);
var inputDialog = _textInputDialogFactory.Create(prompt);

inputDialog.DialogClosing += (_, e) =>
{
if (e.Result == XNADialogResult.Cancel)
return;
if (inputDialog.ResponseText.Length < 4)
{
e.Cancel = true;
var messageBox = _eoMessageBoxFactory.CreateMessageBox(DialogResourceID.CHARACTER_CREATE_NAME_TOO_SHORT);
messageBox.ShowDialog();
return;
}
if (parentDialog.NamesList.Any(name => string.Equals(name, inputDialog.ResponseText, StringComparison.InvariantCultureIgnoreCase)))
{
e.Cancel = true;
var messageBox = _eoMessageBoxFactory.CreateMessageBox("You are already friends with that person!", "Invalid entry!", EODialogButtons.Ok, EOMessageBoxStyle.SmallDialogSmallHeader);
messageBox.ShowDialog();
return;
}
var charName = char.ToUpper(inputDialog.ResponseText[0]) + inputDialog.ResponseText.Substring(1);
var newItem = new ListDialogItem(parentDialog, ListDialogItem.ListItemStyle.Small) { PrimaryText = charName };
SetClickEventHandlers(newItem, parentDialog, isFriendList);
parentDialog.AddItemToList(newItem, sortList: true);
parentDialog.Title = GetDialogTitle(parentDialog, isFriendList);
};

inputDialog.ShowDialog();
}

private void SetClickEventHandlers(ListDialogItem item, ScrollingListDialog dialog, bool isFriendList)
{
//e.CancelClose = true;
//string prompt = OldWorld.GetString(isIgnoreList ? EOResourceID.DIALOG_WHO_TO_MAKE_IGNORE : EOResourceID.DIALOG_WHO_TO_MAKE_FRIEND);
//TextInputDialog dlgInput = new TextInputDialog(prompt);
//dlgInput.DialogClosing += (_o, _e) =>
//{
// if (_e.Result == XNADialogResult.Cancel) return;

// if (dlgInput.ResponseText.Length < 4)
// {
// _e.CancelClose = true;
// EOMessageBox.Show(DialogResourceID.CHARACTER_CREATE_NAME_TOO_SHORT);
// dlgInput.SetAsKeyboardSubscriber();
// return;
// }

// if (dlg.NamesList.FindIndex(name => name.ToLower() == dlgInput.ResponseText.ToLower()) >= 0)
// {
// _e.CancelClose = true;
// EOMessageBox.Show("You are already friends with that person!", "Invalid entry!", EODialogButtons.Ok, EOMessageBoxStyle.SmallDialogSmallHeader);
// dlgInput.SetAsKeyboardSubscriber();
// return;
// }

// ListDialogItem newItem = new ListDialogItem(dlg, ListDialogItem.ListItemStyle.Small)
// {
// PrimaryText = dlgInput.ResponseText
// };
// newItem.OnLeftClick += (oo, ee) => EOGame.Instance.Hud.SetChatText("!" + newItem.PrimaryText + " ");
// newItem.OnRightClick += (oo, ee) =>
// {
// dlg.RemoveFromList(newItem);
// dlg.Title = string.Format("{0}'s {2} [{1}]",
// charName,
// dlg.NamesList.Count,
// OldWorld.GetString(isIgnoreList ? EOResourceID.STATUS_LABEL_IGNORE_LIST : EOResourceID.STATUS_LABEL_FRIEND_LIST));
// };
// dlg.AddItemToList(newItem, true);
// dlg.Title = string.Format("{0}'s {2} [{1}]", charName, dlg.NamesList.Count,
// OldWorld.GetString(isIgnoreList ? EOResourceID.STATUS_LABEL_IGNORE_LIST : EOResourceID.STATUS_LABEL_FRIEND_LIST));
//};
item.LeftClick += (o, e) => _hudControlProvider.GetComponent<ChatTextBox>(HudControlIdentifier.ChatTextBox).Text = $"!{item.PrimaryText} ";
item.RightClick += (o, e) =>
{
dialog.RemoveFromList(item);
dialog.Title = GetDialogTitle(dialog, isFriendList);
};
}

private string GetDialogTitle(ScrollingListDialog dialog, bool isFriendList)
{
var friendOrIgnoreStr = _localizedStringFinder.GetString(isFriendList ? EOResourceID.STATUS_LABEL_FRIEND_LIST : EOResourceID.STATUS_LABEL_IGNORE_LIST);
return $"{_characterProvider.MainCharacter.Name}'s {friendOrIgnoreStr} [{dialog.NamesList.Count}]";
}
}

Expand Down
48 changes: 48 additions & 0 deletions EndlessClient/Dialogs/Factories/TextInputDialogFactory.cs
@@ -0,0 +1,48 @@
using AutomaticTypeMapper;
using EndlessClient.Content;
using EndlessClient.Dialogs.Services;
using EndlessClient.GameExecution;
using EndlessClient.Input;
using EOLib.Graphics;

namespace EndlessClient.Dialogs.Factories
{
[AutoMappedType]
public class TextInputDialogFactory : ITextInputDialogFactory
{
private readonly IGameStateProvider _gameStateProvider;
private readonly INativeGraphicsManager _nativeGraphicsManager;
private readonly IEODialogButtonService _eoDialogButtonService;
private readonly IKeyboardDispatcherRepository _keyboardDispatcherRepository;
private readonly IContentManagerProvider _contentManagerProvider;

public TextInputDialogFactory(IGameStateProvider gameStateProvider,
INativeGraphicsManager nativeGraphicsManager,
IEODialogButtonService eoDialogButtonService,
IKeyboardDispatcherRepository keyboardDispatcherRepository,
IContentManagerProvider contentManagerProvider)
{
_gameStateProvider = gameStateProvider;
_nativeGraphicsManager = nativeGraphicsManager;
_eoDialogButtonService = eoDialogButtonService;
_keyboardDispatcherRepository = keyboardDispatcherRepository;
_contentManagerProvider = contentManagerProvider;
}

public TextInputDialog Create(string prompt, int maxInputChars = 12)
{
return new TextInputDialog(_gameStateProvider,
_nativeGraphicsManager,
_eoDialogButtonService,
_keyboardDispatcherRepository,
_contentManagerProvider,
prompt,
maxInputChars);
}
}

public interface ITextInputDialogFactory
{
TextInputDialog Create(string prompt, int maxInputChars = 12);
}
}
20 changes: 14 additions & 6 deletions EndlessClient/Dialogs/ListDialogItem.cs
Expand Up @@ -19,6 +19,8 @@ public class ListDialogItem : XNAControl
private readonly Texture2D _gfxPadThing;
private readonly Texture2D _backgroundColor;

private readonly ScrollingListDialog _parentList;

private bool _drawBackground;

public int Index
Expand Down Expand Up @@ -85,8 +87,8 @@ public string SubText

public bool ShowIconBackGround { get; set; }

public event EventHandler OnRightClick;
public event EventHandler OnLeftClick;
public event EventHandler RightClick;
public event EventHandler LeftClick;

public enum ListItemStyle
{
Expand All @@ -96,6 +98,8 @@ public enum ListItemStyle

public ListDialogItem(ScrollingListDialog parent, ListItemStyle style, int listIndex = -1)
{
_parentList = parent;

DrawPosition += new Vector2(17, 0);

Style = style;
Expand Down Expand Up @@ -166,7 +170,7 @@ public void SetPrimaryClickAction(EventHandler onClickAction)
oldText.Dispose();

if (Style == ListItemStyle.Small)
OnLeftClick += onClickAction;
LeftClick += onClickAction;
}

public void SetSubtextClickAction(EventHandler onClickAction)
Expand Down Expand Up @@ -208,9 +212,11 @@ protected override void OnUpdateControl(GameTime gameTime)
{
_drawBackground = true;
if (CurrentMouseState.RightButton == ButtonState.Released &&
PreviousMouseState.RightButton == ButtonState.Pressed)
PreviousMouseState.RightButton == ButtonState.Pressed &&
!_parentList.ChildControlClickHandled)
{
OnRightClick?.Invoke(this, EventArgs.Empty);
RightClick?.Invoke(this, EventArgs.Empty);
_parentList.ChildControlClickHandled = true;
}
else if(CurrentMouseState.LeftButton == ButtonState.Released &&
PreviousMouseState.LeftButton == ButtonState.Pressed)
Expand All @@ -219,7 +225,9 @@ protected override void OnUpdateControl(GameTime gameTime)
if (_subText is XNAHyperLink && _subText.MouseOver)
((XNAHyperLink)_subText).Click();
else
OnLeftClick(this, EventArgs.Empty);
LeftClick?.Invoke(this, EventArgs.Empty);

_parentList.ChildControlClickHandled = true;
}
}
else
Expand Down
18 changes: 9 additions & 9 deletions EndlessClient/Dialogs/ScrollingListDialog.cs
Expand Up @@ -72,7 +72,7 @@ public ScrollingListDialogButtons Buttons

public event EventHandler BackAction;

public event EventHandler<XNADialogResult> CloseAction;
public bool ChildControlClickHandled { get; set; }

static ScrollingListDialog()
{
Expand Down Expand Up @@ -107,7 +107,7 @@ static ScrollingListDialog()
Visible = false
};
_add.SetParentControl(this);
_add.OnClick += AddAction;
_add.OnClick += (o, e) => AddAction?.Invoke(o, e);

_back = new XNAButton(dialogButtonService.SmallButtonSheet,
new Vector2(48, 252),
Expand All @@ -117,7 +117,7 @@ static ScrollingListDialog()
Visible = false
};
_back.SetParentControl(this);
_back.OnClick += BackAction;
_back.OnClick += (o, e) => BackAction?.Invoke(o, e);

_cancel = new XNAButton(dialogButtonService.SmallButtonSheet,
_cancelButtonRightPosition,
Expand All @@ -127,13 +127,9 @@ static ScrollingListDialog()
Visible = true
};
_cancel.SetParentControl(this);
_cancel.OnClick += (_, _) =>
{
CloseAction?.Invoke(this, XNADialogResult.Cancel);
Close(XNADialogResult.Cancel);
};
_cancel.OnClick += (_, _) => Close(XNADialogResult.Cancel);

ItemsToShow = 5;
ItemsToShow = ListItemType == ListDialogItem.ListItemStyle.Large ? 5 : 12;

BackgroundTexture = nativeGraphicsManager.TextureFromResource(GFXTypes.PostLoginUI, 52);

Expand Down Expand Up @@ -183,6 +179,8 @@ public void RemoveFromList(ListDialogItem item)

for (int i = 0; i < _listItems.Count; ++i)
_listItems[i].Index = i;

item.Dispose();
}

public void HighlightTextByLabel(IReadOnlyList<string> activeLabels)
Expand Down Expand Up @@ -219,6 +217,8 @@ protected override void OnUpdateControl(GameTime gameTime)
{
base.OnUpdateControl(gameTime);

ChildControlClickHandled = false;

if (_listItems.Count > _scrollBar.LinesToRender)
{
for (int i = 0; i < _listItems.Count; ++i)
Expand Down

0 comments on commit 883eeb7

Please sign in to comment.