-
Notifications
You must be signed in to change notification settings - Fork 113
/
CodePatches.cs
430 lines (358 loc) · 15.7 KB
/
CodePatches.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
using HarmonyLib;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StardewValley;
using StardewValley.BellsAndWhistles;
using StardewValley.Characters;
using StardewValley.Menus;
using System;
using System.Diagnostics;
namespace DialogueDisplayFramework
{
public partial class ModEntry
{
private static bool preventGetCurrentString;
private static ProfileMenu npcSpriteMenu;
[HarmonyPatch(typeof(DialogueBox), new Type[] { typeof(Dialogue) })]
[HarmonyPatch(MethodType.Constructor)]
public class DialogueBox_Patch
{
public static void Postfix(DialogueBox __instance, Dialogue dialogue)
{
if (!Config.EnableMod || dialogue?.speaker is null)
return;
try
{
npcSpriteMenu = new ProfileMenu(dialogue.speaker);
}
catch
{
}
LoadData();
if (!dataDict.TryGetValue(__instance.characterDialogue.speaker.Name, out DialogueDisplayData data))
{
if (!dataDict.TryGetValue(defaultKey, out data))
return;
}
__instance.x += data.xOffset;
__instance.y += data.yOffset;
if(data.width > 0)
__instance.width = data.width;
if (data.height > 0)
__instance.height = data.height;
}
}
[HarmonyPatch(typeof(DialogueBox), nameof(DialogueBox.receiveLeftClick))]
public class DialogueBox_receiveLeftClick_Patch
{
public static bool Prefix(DialogueBox __instance, int x, int y, bool playSound)
{
if (!Config.EnableMod || __instance.characterDialogue?.speaker is null)
return true;
if (!dataDict.TryGetValue(__instance.characterDialogue.speaker.Name, out DialogueDisplayData data))
data = dataDict[defaultKey];
if (data == null || data.disabled)
return true;
var sprite = data.sprite is null ? dataDict[defaultKey].sprite : data.sprite;
if (sprite is not null && !sprite.disabled)
{
if(new Rectangle(Utility.Vector2ToPoint(GetDataVector(__instance, sprite)), new Point(128, 192)).Contains(new Point(x, y)))
{
ProfileMenu menu = new ProfileMenu(__instance.characterDialogue.speaker);
Game1.activeClickableMenu = menu;
return false;
}
}
return true;
}
}
[HarmonyPatch(typeof(DialogueBox), nameof(DialogueBox.gameWindowSizeChanged))]
public class DialogueBox_gameWindowSizeChanged_Patch
{
public static void Postfix(DialogueBox __instance)
{
if (!Config.EnableMod || __instance.characterDialogue?.speaker is null)
return;
if (!dataDict.TryGetValue(__instance.characterDialogue.speaker.Name, out DialogueDisplayData data))
{
if (!dataDict.TryGetValue(defaultKey, out data))
return;
}
__instance.x += data.xOffset;
__instance.y += data.yOffset;
if (data.width > 0)
__instance.width = data.width;
if (data.height > 0)
__instance.height = data.height;
}
}
[HarmonyPatch(typeof(DialogueBox), nameof(DialogueBox.drawPortrait))]
public class DialogueBox_drawPortrait_Patch
{
public static bool Prefix(DialogueBox __instance, SpriteBatch b)
{
if (!Config.EnableMod)
return true;
string name = __instance.characterDialogue.speaker.Name;
if (!dataDict.TryGetValue(name, out DialogueDisplayData data))
data = dataDict[defaultKey];
if (data == null || data.disabled)
return true;
// Dividers
var dividers = data.dividers is null ? dataDict[defaultKey].dividers : data.dividers;
if (dividers != null)
{
foreach (var divider in dividers)
{
if (divider.horizontal)
{
DrawHorizontalPartition(b, __instance, divider);
}
else
{
DrawVerticalPartition(b, __instance, divider);
}
}
}
// Images
var images = data.images is null ? dataDict[defaultKey].images : data.images;
if (images != null)
{
foreach (var image in images)
{
b.Draw(imageDict[image.texturePath], GetDataVector(__instance, image), new Rectangle(image.x, image.y, image.w, image.h), Color.White * image.alpha, 0, Vector2.Zero, image.scale, SpriteEffects.None, image.layerDepth);
}
}
// NPC Portrait
var portrait = data.portrait is null ? dataDict[defaultKey].portrait : data.portrait;
if (portrait is not null && !portrait.disabled)
{
Texture2D portraitTexture;
Rectangle portraitSource;
if (portrait.texturePath != null)
{
portraitTexture = imageDict[portrait.texturePath];
}
else
{
if (__instance.characterDialogue.overridePortrait != null)
portraitTexture = __instance.characterDialogue.overridePortrait;
else
portraitTexture = __instance.characterDialogue.speaker.Portrait;
}
if (!portrait.tileSheet)
{
portraitSource = new Rectangle(portrait.x, portrait.y, portrait.w, portrait.h);
}
else
{
portraitSource = Game1.getSourceRectForStandardTileSheet(portraitTexture, __instance.characterDialogue.getPortraitIndex(), portrait.w, portrait.h);
if (!portraitTexture.Bounds.Contains(portraitSource))
{
portraitSource = new Rectangle(0, 0, portrait.w, portrait.h);
}
}
int xOffset = (bool)AccessTools.Method(typeof(DialogueBox), "shouldPortraitShake").Invoke(__instance, new object[] { __instance.characterDialogue }) ? Game1.random.Next(-1, 2) : 0;
b.Draw(portraitTexture, GetDataVector(__instance, portrait) + new Vector2(xOffset, 0), new Rectangle?(portraitSource), Color.White * portrait.alpha, 0f, Vector2.Zero, portrait.scale, SpriteEffects.None, portrait.layerDepth);
}
// Sprite
var sprite = data.sprite is null ? dataDict[defaultKey].sprite : data.sprite;
if (sprite is not null && !sprite.disabled && npcSpriteMenu is not null)
{
var pos = GetDataVector(__instance, sprite);
if (sprite.background)
{
b.Draw((Game1.timeOfDay >= 1900) ? Game1.nightbg : Game1.daybg, pos, Color.White);
}
if (sprite.frame >= 0)
{
AccessTools.FieldRefAccess<ProfileMenu, AnimatedSprite>(npcSpriteMenu, "_animatedSprite").CurrentFrame = sprite.frame;
}
else
{
npcSpriteMenu.update(Game1.currentGameTime);
}
AccessTools.FieldRefAccess<ProfileMenu, AnimatedSprite>(npcSpriteMenu, "_animatedSprite").draw(b, pos + new Vector2(32, 32), sprite.layerDepth, 0, 0, Color.White, false, sprite.scale, 0, false);
}
// NPC Name
var npcName = data.name != null ? data.name : dataDict[defaultKey].name;
if (npcName is not null && !npcName.disabled)
{
var namePos = GetDataVector(__instance, npcName);
var realName = __instance.characterDialogue.speaker.getName();
if (npcName.centered)
{
if (npcName.scroll)
{
SpriteText.drawStringWithScrollCenteredAt(b, realName, (int)namePos.X, (int)namePos.Y, npcName.placeholderText is null ? realName : npcName.placeholderText, npcName.alpha, npcName.color, npcName.scrollType, npcName.layerDepth, npcName.junimo);
}
else
{
SpriteText.drawStringHorizontallyCenteredAt(b, realName, (int)namePos.X, (int)namePos.Y, 999999, npcName.width, 999999, npcName.alpha, npcName.layerDepth, npcName.junimo, npcName.color);
}
}
else
{
if (npcName.right)
namePos.X -= SpriteText.getWidthOfString(realName);
if (npcName.scroll)
{
SpriteText.drawStringWithScrollBackground(b, realName, (int)namePos.X, (int)namePos.Y, npcName.placeholderText is null ? realName : npcName.placeholderText, npcName.alpha, npcName.color, npcName.alignment);
}
else
{
SpriteText.drawString(b, realName, (int)namePos.X, (int)namePos.Y, 999999, npcName.width, 999999, npcName.alpha, npcName.layerDepth, npcName.junimo, npcName.color);
}
}
}
// Texts
var texts = data.texts is null ? dataDict[defaultKey].texts : data.texts;
if (texts != null)
{
foreach (var text in texts)
{
var pos = GetDataVector(__instance, text);
if (text.centered)
{
if (text.variable && text.right)
pos.X -= SpriteText.getWidthOfString(text.text) / 2;
if (text.scroll)
{
SpriteText.drawStringWithScrollCenteredAt(b, text.text, (int)pos.X, (int)pos.Y, text.placeholderText, text.alpha, text.color, text.scrollType, text.layerDepth, text.junimo);
}
else
{
SpriteText.drawStringHorizontallyCenteredAt(b, text.text, (int)pos.X, (int)pos.Y, 999999, text.width, 999999, text.alpha, text.layerDepth, text.junimo, text.color);
}
}
else
{
if (text.variable && text.right)
pos.X -= SpriteText.getWidthOfString(text.text);
if (text.scroll)
{
SpriteText.drawStringWithScrollBackground(b, text.text, (int)pos.X, (int)pos.Y, text.placeholderText, text.alpha, text.color, text.alignment);
}
else
{
SpriteText.drawString(b, text.text, (int)pos.X, (int)pos.Y, 999999, text.width, 999999, text.alpha, text.layerDepth, text.junimo, text.color);
}
}
}
}
if (Game1.player.friendshipData.ContainsKey(name))
{
// Hearts
var hearts = data.hearts is null ? dataDict[defaultKey].hearts : data.hearts;
if (hearts is not null && !hearts.disabled)
{
var pos = GetDataVector(__instance, hearts);
int heartLevel = Game1.player.getFriendshipHeartLevelForNPC(name);
int extraFriendshipPixels = Game1.player.getFriendshipLevelForNPC(name) % 250;
bool datable = SocialPage.isDatable(name);
bool spouse = false;
if (Game1.player.friendshipData.TryGetValue(name, out Friendship friendship))
{
spouse = friendship.IsMarried();
}
int maxHearts = Math.Max(Utility.GetMaximumHeartsForCharacter(Game1.getCharacterFromName(name, true, false)), 10);
if (hearts.centered)
{
int maxDisplayedHearts = hearts.showEmptyHearts ? maxHearts : heartLevel;
if (extraFriendshipPixels > 0) maxDisplayedHearts++;
pos -= new Vector2(Math.Min(hearts.heartsPerRow, maxDisplayedHearts) * 32 / 2, 0);
}
for (int h = 0; h < maxHearts; h++)
{
if (h > heartLevel && !hearts.showEmptyHearts)
break;
if (h == heartLevel && extraFriendshipPixels == 0)
break;
int xSource = (h < heartLevel) ? 211 : 218;
if (datable && !friendship.IsDating() && !spouse && h >= 8)
{
xSource = 211;
}
int x = h % hearts.heartsPerRow;
int y = h / hearts.heartsPerRow;
b.Draw(Game1.mouseCursors, pos + new Vector2(x * 32, y * 32), new Rectangle?(new Rectangle(xSource, 428, 7, 6)), (datable && !friendship.IsDating() && !spouse && h >= 8) ? (Color.Black * 0.35f) : Color.White, 0f, Vector2.Zero, 4f, SpriteEffects.None, 0.88f);
if (h == heartLevel && extraFriendshipPixels > 0)
{
b.Draw(Game1.mouseCursors, pos + new Vector2(x * 32, y * 32), new Rectangle?(new Rectangle(211, 428, (int)Math.Round(7 * (extraFriendshipPixels / 250f)), 6)), Color.White, 0f, Vector2.Zero, 4f, SpriteEffects.None, 0.88f);
}
}
}
// Gifts
var gifts = data.gifts is null ? dataDict[defaultKey].gifts : data.gifts;
if (gifts is not null && !gifts.disabled && !Game1.player.friendshipData[name].IsMarried() && Game1.getCharacterFromName(name) is not Child)
{
var pos = GetDataVector(__instance, gifts);
Utility.drawWithShadow(b, Game1.mouseCursors2, pos + new Vector2(6, 0), new Rectangle(166, 174, 14, 12), Color.White, 0f, Vector2.Zero, 4f, false, 0.88f, 0, -1, 0.2f);
b.Draw(Game1.mouseCursors, pos + (gifts.inline ? new Vector2(64, 8) : new Vector2(0, 56)), new Rectangle?(new Rectangle(227 + ((Game1.player.friendshipData[name].GiftsThisWeek >= 2) ? 9 : 0), 425, 9, 9)), Color.White, 0f, Vector2.Zero, 4f, SpriteEffects.None, 0.88f);
b.Draw(Game1.mouseCursors, pos + (gifts.inline ? new Vector2(96, 8) : new Vector2(32, 56)), new Rectangle?(new Rectangle(227 + (Game1.player.friendshipData[name].GiftsThisWeek >= 1 ? 9 : 0), 425, 9, 9)), Color.White, 0f, Vector2.Zero, 4f, SpriteEffects.None, 0.88f);
}
// Jewel
if (__instance.shouldDrawFriendshipJewel())
{
var jewel = data.jewel is null ? dataDict[defaultKey].jewel : data.jewel;
if (jewel != null && !jewel.disabled)
{
var pos = GetDataVector(__instance, jewel);
b.Draw(Game1.mouseCursors, pos, new Rectangle?((Game1.player.getFriendshipHeartLevelForNPC(__instance.characterDialogue.speaker.Name) >= 10) ? new Rectangle(269, 494, 11, 11) : new Rectangle(Math.Max(140, 140 + (int)(Game1.currentGameTime.TotalGameTime.TotalMilliseconds % 1000.0 / 250.0) * 11), Math.Max(532, 532 + Game1.player.getFriendshipHeartLevelForNPC(__instance.characterDialogue.speaker.Name) / 2 * 11), 11, 11)), Color.White * jewel.alpha, 0f, Vector2.Zero, jewel.scale, SpriteEffects.None, jewel.layerDepth);
}
}
}
// Dialogue String
var dialogue = data.dialogue is null ? dataDict[defaultKey].dialogue : data.dialogue;
var dialoguePos = GetDataVector(__instance, dialogue);
preventGetCurrentString = false;
SpriteText.drawString(b, __instance.getCurrentString(), (int)dialoguePos.X, (int)dialoguePos.Y, __instance.characterIndexInDialogue, dialogue.width >= 0 ? dialogue.width : __instance.width - 8, 999999, dialogue.alpha, dialogue.layerDepth, false, -1, "", dialogue.color, dialogue.alignment);
// Close Icon
if(__instance.dialogueIcon != null)
{
var button = data.button is null ? dataDict[defaultKey].button : data.button;
if(button != null && !button.disabled)
__instance.dialogueIcon.position = GetDataVector(__instance, button);
}
preventGetCurrentString = true;
return false;
}
private static void DrawHorizontalPartition(SpriteBatch b, DialogueBox box, DividerData divider)
{
Color tint = (divider.red == -1) ? Color.White : new Color(divider.red, divider.green, divider.blue);
Texture2D texture = (divider.red == -1) ? Game1.menuTexture : Game1.uncoloredMenuTexture;
b.Draw(texture, new Rectangle(box.x + divider.xOffset, box.y + divider.yOffset, divider.width, 64), new Rectangle?(Game1.getSourceRectForStandardTileSheet(Game1.menuTexture, 6, -1, -1)), tint);
}
private static void DrawVerticalPartition(SpriteBatch b, DialogueBox instance, DividerData divider)
{
Color tint = (divider.red == -1) ? Color.White : new Color(divider.red, divider.green, divider.blue);
b.Draw(Game1.mouseCursors, new Rectangle(instance.x + divider.xOffset, instance.y + divider.yOffset, 36, divider.height), new Rectangle?(new Rectangle(278, 324, 9, 1)), tint);
}
}
private static Vector2 GetDataVector(DialogueBox box, BaseData data)
{
return new Vector2(box.x + (data.right ? box.width : 0) + data.xOffset, box.y + (data.bottom ? box.height : 0) + data.yOffset);
}
[HarmonyPatch(typeof(DialogueBox), nameof(DialogueBox.getCurrentString))]
public class DialogueBox_getCurrentString_Patch
{
public static bool Prefix(DialogueBox __instance, ref string __result)
{
if (!Config.EnableMod || !preventGetCurrentString)
return true;
__result = "";
return false;
}
}
[HarmonyPatch(typeof(DialogueBox), nameof(DialogueBox.draw))]
public class DialogueBox_draw_Patch
{
public static void Postfix(DialogueBox __instance, SpriteBatch b)
{
if (!Config.EnableMod)
return;
preventGetCurrentString = false;
}
}
}
}