-
Notifications
You must be signed in to change notification settings - Fork 324
/
PaperDoll.cs
236 lines (192 loc) · 9.45 KB
/
PaperDoll.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
// Project: Daggerfall Unity
// Copyright: Copyright (C) 2009-2023 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:
//
// Notes:
//
using UnityEngine;
using DaggerfallConnect;
using DaggerfallConnect.Utility;
using DaggerfallWorkshop.Utility;
using DaggerfallWorkshop.Game.Utility;
using DaggerfallWorkshop.Game.Entity;
using DaggerfallWorkshop.Game.MagicAndEffects.MagicEffects;
namespace DaggerfallWorkshop.Game.UserInterface
{
/// <summary>
/// Implements character paper doll.
/// </summary>
public class PaperDoll : Panel
{
#region UI Rects
const int paperDollWidth = 110;
const int paperDollHeight = 184;
const int waistHeight = 40;
readonly DFSize backgroundFullSize = new DFSize(125, 198);
readonly Rect backgroundSubRect = new Rect(8, 7, paperDollWidth, paperDollHeight);
#endregion
#region Fields
static readonly Color32 maskColor = new Color(255, 0, 200, 0); // Special mask colour used on helmets, cloaks, etc.
const bool showBackgroundLayer = true;
const bool showCharacterLayer = true;
readonly Panel backgroundPanel = new Panel();
readonly Panel characterPanel = new Panel();
readonly TextLabel[] armourLabels = new TextLabel[DaggerfallEntity.NumberBodyParts];
readonly Vector2[] armourLabelPos = new Vector2[] { new Vector2(70, 12), new Vector2(20, 38), new Vector2(86, 38), new Vector2(12, 58), new Vector2(6, 90), new Vector2(18, 120), new Vector2(22, 168) };
string lastBackgroundName = string.Empty;
bool showArmorLabels;
#endregion
#region Properties
public static Color32 MaskColor
{
get { return maskColor; }
}
#endregion
#region Constructors
public PaperDoll(bool showArmorValues=true)
{
// Setup panels
Size = new Vector2(paperDollWidth, paperDollHeight);
characterPanel.Size = new Vector2(paperDollWidth, paperDollHeight);
// Add panels
Components.Add(backgroundPanel);
Components.Add(characterPanel);
// Set initial display flags
backgroundPanel.Enabled = showBackgroundLayer;
characterPanel.Enabled = showCharacterLayer;
showArmorLabels = showArmorValues;
if (showArmorLabels)
{
for (int bpIdx = 0; bpIdx < DaggerfallEntity.NumberBodyParts; bpIdx++)
{
armourLabels[bpIdx] = DaggerfallUI.AddDefaultShadowedTextLabel(armourLabelPos[bpIdx], characterPanel);
armourLabels[bpIdx].Text = "0";
}
}
}
#endregion
#region Overrides
public override void Update()
{
// Update display flags
backgroundPanel.Enabled = showBackgroundLayer;
characterPanel.Enabled = showCharacterLayer;
base.Update();
}
#endregion
#region Public Methods
/// <summary>
/// Redraws paper doll image and selection mask.
/// Call this after changing equipment, loading a new game, etc.
/// Only call when required as constructing paper doll image is expensive.
/// </summary>
/// <param name="playerEntity"></param>
public void Refresh(PlayerEntity playerEntity = null)
{
// Get current player entity if one not provided
if (playerEntity == null)
playerEntity = GameManager.Instance.PlayerEntity;
// Racial override can suppress body and items
bool suppressBody = false;
RacialOverrideEffect racialOverride = GameManager.Instance.PlayerEffectManager.GetRacialOverrideEffect();
if (racialOverride != null)
suppressBody = racialOverride.SuppressPaperDollBodyAndItems;
// Update background
RefreshBackground(playerEntity);
// Display paper doll render
DaggerfallUI.Instance.PaperDollRenderer.Refresh(PaperDollRenderer.LayerFlags.All, playerEntity);
characterPanel.BackgroundTexture = DaggerfallUI.Instance.PaperDollRenderer.PaperDollTexture;
// Update armour values
RefreshArmourValues(playerEntity, suppressBody);
}
/// <summary>
/// Gets equip index at position.
/// </summary>
/// <param name="x">X position to sample.</param>
/// <param name="y">Y position to sample.</param>
/// <returns>Equip index or 0xff if point empty.</returns>
public byte GetEquipIndex(int x, int y)
{
return DaggerfallUI.Instance.PaperDollRenderer.GetEquipIndex(x, y);
}
#endregion
#region Private Methods
// Refresh armour value labels
void RefreshArmourValues(PlayerEntity playerEntity, bool suppress = false)
{
if (!showArmorLabels)
return;
for (int bpIdx = 0; bpIdx < DaggerfallEntity.NumberBodyParts; bpIdx++)
{
int armorMod = playerEntity.DecreasedArmorValueModifier - playerEntity.IncreasedArmorValueModifier;
sbyte av = playerEntity.ArmorValues[bpIdx];
int bpAv = (100 - av) / 5 + armorMod;
armourLabels[bpIdx].Text = (!suppress) ? bpAv.ToString() : string.Empty;
if (armorMod < 0)
armourLabels[bpIdx].TextColor = DaggerfallUI.DaggerfallUnityStatDrainedTextColor;
else if (armorMod > 0)
armourLabels[bpIdx].TextColor = DaggerfallUI.DaggerfallUnityStatIncreasedTextColor;
else
armourLabels[bpIdx].TextColor = DaggerfallUI.DaggerfallDefaultTextColor;
}
}
// Update player background panel
void RefreshBackground(PlayerEntity entity)
{
// Allow racial override background (vampire / transformed were-creature)
// If racial override is not present or returns null then standard racial background will be used
// The racial override has full control over which texture is displayed, such as when were-creature transformed or not
Texture2D customBackground;
RacialOverrideEffect racialOverride = GameManager.Instance.PlayerEffectManager.GetRacialOverrideEffect();
if (racialOverride != null && racialOverride.GetCustomPaperDollBackgroundTexture(entity, out customBackground))
{
backgroundPanel.BackgroundTexture = customBackground;
backgroundPanel.Size = new Vector2(paperDollWidth, paperDollHeight);
lastBackgroundName = string.Empty;
return;
}
// Use standard racial background
string backgroundName = GetPaperDollBackground(entity);
if (lastBackgroundName != backgroundName)
{
Texture2D texture = ImageReader.GetTexture(backgroundName, 0, 0, false);
backgroundPanel.BackgroundTexture = ImageReader.GetSubTexture(texture, backgroundSubRect, backgroundFullSize);
backgroundPanel.Size = new Vector2(paperDollWidth, paperDollHeight);
lastBackgroundName = backgroundName;
}
}
readonly char[] regionBackgroundIdxChars =
{'3','1','2','2', '2','0','5','1', '5','2','1','1', '2','2','2','0', '2','0','2','2', '3','0','5','6', '2','2','2','2', '0','0','0','0',
'0','6','6','6', '0','6','6','0', '6','0','0','3', '3','3','3','3', '3','5','5','5', '5','1','3','3', '3','2','0','0', '2','3' };
string GetPaperDollBackground(PlayerEntity entity)
{
if (DaggerfallUnity.Settings.EnableGeographicBackgrounds)
{
PlayerGPS playerGPS = GameManager.Instance.PlayerGPS;
PlayerEnterExit playerEnterExit = GameManager.Instance.PlayerEnterExit;
DFPosition position = playerGPS.CurrentMapPixel;
int region = DaggerfallUnity.Instance.ContentReader.MapFileReader.GetPoliticIndex(position.X, position.Y) - 128;
if (region < 0 || region >= DaggerfallUnity.Instance.ContentReader.MapFileReader.RegionCount || region >= regionBackgroundIdxChars.Length)
return entity.RaceTemplate.PaperDollBackground;
// Set background based on location.
if (playerGPS.IsPlayerInTown(true))
return "SCBG04I0.IMG"; // Town
else if (playerEnterExit.IsPlayerInsideDungeon)
return "SCBG07I0.IMG"; // Dungeon
else if (playerGPS.CurrentLocation.MapTableData.LocationType == DFRegion.LocationTypes.Graveyard && playerGPS.IsPlayerInLocationRect)
return "SCBG08I0.IMG"; // Graveyard
else
return "SCBG0" + regionBackgroundIdxChars[region] + "I0.IMG"; // Region
}
else
{
return entity.RaceTemplate.PaperDollBackground;
}
}
#endregion
}
}