-
Notifications
You must be signed in to change notification settings - Fork 16
/
QuestDialog.cs
181 lines (154 loc) · 6.23 KB
/
QuestDialog.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using EndlessClient.Content;
using EndlessClient.Dialogs.Services;
using EOLib;
using EOLib.Domain.Interact.Quest;
using EOLib.Domain.NPC;
using EOLib.Graphics;
using EOLib.IO.Repositories;
using Microsoft.Xna.Framework;
using Optional;
using System;
using System.Collections.Generic;
using System.Linq;
using XNAControls;
namespace EndlessClient.Dialogs
{
public class QuestDialog : ScrollingListDialog
{
private readonly IQuestActions _questActions;
private readonly IQuestDataProvider _questDataProvider;
private readonly IENFFileProvider _enfFileProvider;
private readonly IContentProvider _contentProvider;
private Option<IQuestDialogData> _cachedData;
private int _pageIndex = 0;
public QuestDialog(INativeGraphicsManager nativeGraphicsManager,
IQuestActions questActions,
IEODialogButtonService dialogButtonService,
IQuestDataProvider questDataProvider,
IENFFileProvider enfFileProvider,
IContentProvider contentProvider)
: base(nativeGraphicsManager, dialogButtonService, dialogSize: ScrollingListDialogSize.Small)
{
_questActions = questActions;
_questDataProvider = questDataProvider;
_enfFileProvider = enfFileProvider;
_contentProvider = contentProvider;
_cachedData = Option.None<IQuestDialogData>();
ListItemType = ListDialogItem.ListItemStyle.Small;
NextAction += NextPage;
BackAction += PreviousPage;
DialogClosing += (_, e) =>
{
if (e.Result == XNADialogResult.OK)
_questActions.RespondToQuestDialog(DialogReply.Ok);
};
}
protected override void OnUpdateControl(GameTime gameTime)
{
_questDataProvider.QuestDialogData.MatchSome(data => UpdateCachedDataIfNeeded(_cachedData, data));
base.OnUpdateControl(gameTime);
}
private void UpdateCachedDataIfNeeded(Option<IQuestDialogData> cachedData, IQuestDialogData repoData)
{
cachedData.Match(
some: cached =>
{
_cachedData = Option.Some(repoData);
if (!cached.Equals(repoData))
UpdateDialogControls(repoData);
},
none: () =>
{
_cachedData = Option.Some(repoData);
UpdateDialogControls(repoData);
});
}
private void UpdateDialogControls(IQuestDialogData repoData)
{
_pageIndex = 0;
UpdateTitle(repoData);
UpdateDialogDisplayText(repoData);
UpdateButtons(repoData);
}
private void UpdateTitle(IQuestDialogData repoData)
{
var npcName = _enfFileProvider.ENFFile[_questDataProvider.RequestedNPC.ID].Name;
var titleText = npcName;
if (!repoData.DialogTitles.ContainsKey(repoData.VendorID) && repoData.DialogTitles.Count == 1)
titleText += $" - {repoData.DialogTitles.Single().Value}";
else if (repoData.DialogTitles.ContainsKey(repoData.VendorID))
titleText += $" - {repoData.DialogTitles[repoData.VendorID]}";
_titleText.Text = titleText;
_titleText.ResizeBasedOnText();
}
private void UpdateDialogDisplayText(IQuestDialogData repoData)
{
ClearItemList();
var rows = new List<string>();
var ts = new TextSplitter(repoData.PageText[_pageIndex], _contentProvider.Fonts[Constants.FontSize09]);
if (!ts.NeedsProcessing)
rows.Add(repoData.PageText[_pageIndex]);
else
rows.AddRange(ts.SplitIntoLines());
int index = 0;
foreach (var row in rows)
{
var rowItem = new ListDialogItem(this, ListDialogItem.ListItemStyle.Small, index++)
{
PrimaryText = row,
};
AddItemToList(rowItem, sortList: false);
}
// The links are only shown on the last page of the quest dialog
if (_pageIndex < repoData.PageText.Count - 1)
return;
var item = new ListDialogItem(this, ListDialogItem.ListItemStyle.Small, index++) { PrimaryText = " " };
AddItemToList(item, sortList: false);
foreach (var action in repoData.Actions)
{
var actionItem = new ListDialogItem(this, ListDialogItem.ListItemStyle.Small, index++)
{
PrimaryText = action.DisplayText
};
var linkIndex = (byte)action.ActionID;
actionItem.SetPrimaryClickAction((_, _) =>
{
_questActions.RespondToQuestDialog(DialogReply.Link, linkIndex);
Close(XNADialogResult.Cancel);
});
AddItemToList(actionItem, sortList: false);
}
}
private void UpdateButtons(IQuestDialogData repoData)
{
bool morePages = _pageIndex < repoData.PageText.Count - 1;
bool firstPage = _pageIndex == 0;
if (firstPage && morePages)
Buttons = ScrollingListDialogButtons.CancelNext;
else if (!firstPage && morePages)
Buttons = ScrollingListDialogButtons.BackNext;
else if (firstPage)
Buttons = ScrollingListDialogButtons.CancelOk;
else
Buttons = ScrollingListDialogButtons.BackOk;
}
private void NextPage(object sender, EventArgs e)
{
_cachedData.MatchSome(data =>
{
_pageIndex++;
UpdateDialogDisplayText(data);
UpdateButtons(data);
});
}
private void PreviousPage(object sender, EventArgs e)
{
_cachedData.MatchSome(data =>
{
_pageIndex--;
UpdateDialogDisplayText(data);
UpdateButtons(data);
});
}
}
}