-
Notifications
You must be signed in to change notification settings - Fork 324
/
DaggerfallBookReaderWindow.cs
340 lines (290 loc) · 12.5 KB
/
DaggerfallBookReaderWindow.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Project: Daggerfall Unity
// Copyright: Copyright (C) 2009-2022 Daggerfall Workshop
// Web Site: http://www.dfworkshop.net
// License: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Source Code: https://github.com/Interkarma/daggerfall-unity
// Original Author: Gavin Clayton (interkarma@dfworkshop.net)
// Contributors: InconsolableCellist
//
// Notes:
//
using UnityEngine;
using System;
using System.IO;
using System.Collections.Generic;
using DaggerfallWorkshop.Game.UserInterface;
using DaggerfallWorkshop.Game.Items;
using DaggerfallConnect.Arena2;
using DaggerfallWorkshop.Localization;
using System.Globalization;
namespace DaggerfallWorkshop.Game.UserInterfaceWindows
{
public class DaggerfallBookReaderWindow : DaggerfallBaseWindow
{
const string textFolderName = "Text";
const string bookImagesPath = "Books/BookImages";
const float scrollAmount = 24;
const string nativeImgName = "BOOK00I0.IMG";
char newline = '\n';
Vector2 pagePanelPosition = new Vector2(55, 21);
Vector2 pagePanelSize = new Vector2(210, 159);
Texture2D nativeTexture;
List<TextLabel> bookLabels = new List<TextLabel>();
Panel pagePanel = new Panel();
float maxHeight = 0;
float scrollPosition = 0;
int currentPage = 0; // Used for page turn sounds only
const SoundClips openBook = SoundClips.OpenBook;
public DaggerfallBookReaderWindow(IUserInterfaceManager uiManager)
: base(uiManager)
{
}
public bool IsBookOpen { get; private set; }
protected override void Setup()
{
// Load native texture
nativeTexture = DaggerfallUI.GetTextureFromImg(nativeImgName);
if (!nativeTexture)
throw new Exception("DaggerfallBookReaderWindow: Could not load native texture.");
// Setup native panel background
NativePanel.BackgroundTexture = nativeTexture;
// Setup panel to contain text labels
pagePanel.Position = pagePanelPosition;
pagePanel.Size = pagePanelSize;
pagePanel.RectRestrictedRenderArea = new Rect(pagePanel.Position, pagePanel.Size);
NativePanel.Components.Add(pagePanel);
// Add buttons
Button nextPageButton = DaggerfallUI.AddButton(new Rect(208, 188, 14, 8), NativePanel);
nextPageButton.OnMouseClick += NextPageButton_OnMouseClick;
Button previousPageButton = DaggerfallUI.AddButton(new Rect(181, 188, 14, 48), NativePanel);
previousPageButton.OnMouseClick += PreviousPageButton_OnMouseClick;
Button exitButton = DaggerfallUI.AddButton(new Rect(277, 187, 32, 10), NativePanel);
exitButton.OnMouseClick += ExitButton_OnMouseClick;
NativePanel.OnMouseScrollDown += Panel_OnMouseScrollDown;
NativePanel.OnMouseScrollUp += Panel_OnMouseScrollUp;
LayoutBookLabels();
DaggerfallUI.Instance.PlayOneShot(openBook);
}
private void NextPageButton_OnMouseClick(BaseScreenComponent sender, Vector2 position)
{
ScrollBook(-scrollAmount);
}
private void PreviousPageButton_OnMouseClick(BaseScreenComponent sender, Vector2 position)
{
ScrollBook(scrollAmount);
}
private void ExitButton_OnMouseClick(BaseScreenComponent sender, Vector2 position)
{
DaggerfallUI.Instance.PlayOneShot(SoundClips.ButtonClick);
CloseWindow();
}
private void Panel_OnMouseScrollDown(BaseScreenComponent sender)
{
NextPageButton_OnMouseClick(sender, Vector2.zero);
}
private void Panel_OnMouseScrollUp(BaseScreenComponent sender)
{
PreviousPageButton_OnMouseClick(sender, Vector2.zero);
}
public override void OnPush()
{
if (IsSetup)
{
LayoutBookLabels();
DaggerfallUI.Instance.PlayOneShot(openBook);
}
}
public override void Draw()
{
base.Draw();
}
public void OpenBook(int id)
{
OpenBook(DaggerfallUnity.Instance.ItemHelper.GetBookFileName(id));
}
public void OpenBook(DaggerfallUnityItem target)
{
if (target == null || target.ItemGroup != ItemGroups.Books || target.IsArtifact)
throw new Exception("Item is not a valid book for book reader UI.");
OpenBook(DaggerfallUnity.Instance.ItemHelper.GetBookFileName(target.message));
}
public void OpenBook(string filename)
{
// Open book file - localized book is first preference with fallback to classic book data from ARENA2 or mods
LocalizedBook localizedBook = new LocalizedBook();
if (localizedBook.OpenBookFile(filename))
{
IsBookOpen = true;
Debug.LogFormat("Opened LocalizedBook '{0}'", filename);
}
else
{
Debug.LogErrorFormat("Failed to open LocalizedBook '{0}'", filename);
return;
}
// Create book labels for UI
CreateBookLabels(localizedBook);
}
void ScrollBook(float amount)
{
if (!IsBookOpen)
return;
// Stop scrolling at top or bottom of book layout
if (amount < 0 && scrollPosition - pagePanel.Size.y - amount < -maxHeight)
return;
else if (amount > 0 && scrollPosition == 0)
return;
// Scroll label and only draw what can be seen
scrollPosition += amount;
foreach (TextLabel label in bookLabels)
{
label.Position = new Vector2(label.Position.x, label.Position.y + amount);
label.Enabled = label.Position.y < pagePanel.Size.y && label.Position.y + label.Size.y > 0;
}
// page displayed at the center of the panel
int centerPage = (int)(scrollPosition / pagePanelSize.y + 0.5f);
if (currentPage != centerPage)
{
currentPage = centerPage;
DaggerfallUI.Instance.PlayOneShot(SoundClips.PageTurn);
}
}
#region Layout Methods
void CreateBookLabels(LocalizedBook localizedBook)
{
DaggerfallFont currentFont = DaggerfallUI.DefaultFont;
HorizontalAlignment currentAlignment = HorizontalAlignment.Left;
Color currentColor = DaggerfallUI.DaggerfallDefaultTextColor;
float currentScale = 1.0f;
// Convert all book lines to labels
bookLabels.Clear();
string[] lines = localizedBook.Content.Split(newline);
foreach (string line in lines)
{
TextFile.Token[] lineTokens = DaggerfallStringTableImporter.ConvertStringToRSCTokens(line);
if (lineTokens == null || lineTokens.Length == 0)
{
// Empty or newline label - also resets alignment, color, scale
bookLabels.Add(CreateLabel(DaggerfallUI.DefaultFont, HorizontalAlignment.Left, DaggerfallUI.DaggerfallDefaultTextColor, string.Empty));
currentAlignment = HorizontalAlignment.Left;
currentColor = DaggerfallUI.DaggerfallDefaultTextColor;
currentScale = 1.0f;
}
else
{
foreach (TextFile.Token token in lineTokens)
{
switch (token.formatting)
{
case TextFile.Formatting.FontPrefix:
currentFont = DaggerfallUI.Instance.GetFont((DaggerfallFont.FontName)token.x - 1);
break;
case TextFile.Formatting.Color:
currentColor = TryParseColor(token.text);
break;
case TextFile.Formatting.JustifyLeft:
currentAlignment = HorizontalAlignment.Left;
break;
case TextFile.Formatting.JustifyCenter:
currentAlignment = HorizontalAlignment.Center;
break;
case TextFile.Formatting.Scale:
currentScale = TryParseScale(token.text);
break;
case TextFile.Formatting.Image:
bookLabels.Add(CreateImageLabel(token.text));
break;
default:
bookLabels.Add(CreateLabel(currentFont, currentAlignment, currentColor, token.text, currentScale));
break;
}
}
}
}
}
TextLabel CreateLabel(DaggerfallFont font, HorizontalAlignment alignment, Color color, string text, float scale = 1.0f)
{
// Every group is cast into a word-wrapping label
TextLabel label = new TextLabel();
label.Font = font;
label.HorizontalAlignment = alignment;
label.TextColor = color;
label.ShadowColor = DaggerfallUI.DaggerfallDefaultShadowColor;
label.ShadowPosition = DaggerfallUI.DaggerfallDefaultShadowPos;
label.WrapText = true;
label.WrapWords = true;
label.Text = text;
label.TextScale = scale;
if (label.HorizontalAlignment == HorizontalAlignment.Center)
label.HorizontalTextAlignment = TextLabel.HorizontalTextAlignmentSetting.Center;
return label;
}
ImageLabel CreateImageLabel(string filename)
{
ImageLabel label = new ImageLabel();
// TODO: Seek book images from mods
// Get path to localized book file and check it exists
string path = Path.Combine(Application.streamingAssetsPath, textFolderName, bookImagesPath, filename);
if (!File.Exists(path))
return label;
// Load image
byte[] data = File.ReadAllBytes(path);
if (data != null && data.Length > 0)
{
Texture2D image = new Texture2D(0, 0);
image.LoadImage(data);
label.Image = image;
}
return label;
}
void LayoutBookLabels()
{
if (!IsBookOpen)
return;
maxHeight = 0;
scrollPosition = 0;
pagePanel.Components.Clear();
float x = 0, y = 0;
foreach (TextLabel label in bookLabels)
{
label.Position = new Vector2(x, y);
label.MaxWidth = (int)pagePanel.Size.x;
label.RectRestrictedRenderArea = pagePanel.RectRestrictedRenderArea;
label.RestrictedRenderAreaCoordinateType = TextLabel.RestrictedRenderArea_CoordinateType.ParentCoordinates;
pagePanel.Components.Add(label);
y += label.Size.y;
maxHeight += label.Size.y;
}
}
Color32 TryParseColor(string color)
{
try
{
// Must be RGB value, eg. 1F0A2B
if (color.Length == 6)
{
byte r = byte.Parse(color.Substring(0, 2), NumberStyles.HexNumber);
byte g = byte.Parse(color.Substring(2, 2), NumberStyles.HexNumber);
byte b = byte.Parse(color.Substring(4, 2), NumberStyles.HexNumber);
byte a = 0xff;
return new Color32(r, g, b, a);
}
}
catch(Exception ex)
{
Debug.LogWarningFormat("DaggerfallBookReaderWindow.TryParseColor() exception: {0}", ex.Message);
}
// Fallback to default text color if parse failed
return DaggerfallUI.DaggerfallDefaultTextColor;
}
float TryParseScale(string scale)
{
float output;
if (float.TryParse(scale, out output))
return output;
// Fallback to 1.0 if parse failed
return 1.0f;
}
#endregion
}
}