SanAndreasUnity/Assets/Scripts/UI/HUD.cs

288 lines
8.5 KiB
C#
Raw Normal View History

2021-03-01 01:10:27 +00:00
using UnityEngine;
2020-05-31 17:07:22 +00:00
using SanAndreasUnity.Behaviours;
using SanAndreasUnity.Utilities;
using UnityEngine.UI;
2020-04-21 22:28:09 +00:00
using SanAndreasUnity.Importing.Conversion;
2020-05-05 15:45:06 +00:00
using SanAndreasUnity.Behaviours.Vehicles;
2021-05-04 18:55:01 +00:00
using SanAndreasUnity.Behaviours.World;
2020-05-31 17:07:22 +00:00
namespace SanAndreasUnity.UI {
public class HUD : MonoBehaviour {
public static HUD Instance { get; private set; }
public Canvas canvas;
public RawImage weaponImage;
public Text weaponAmmoText;
public RawImage healthBackgroundImage;
public RawImage healthForegroundImage;
public RawImage crosshairImage;
2020-04-22 21:58:05 +00:00
public Text pedStateText;
2020-04-24 22:01:03 +00:00
public Text pedVelocityText;
2020-05-05 15:45:06 +00:00
public Text radioStationText;
2021-05-04 18:55:01 +00:00
public Text dayTimeText;
[SerializeField] [Range(0.3f, 10f)] float m_radioStationLabelDuration = 3f;
2019-07-23 14:29:16 +00:00
public static Texture2D LeftArrowTexture { get; set; }
public static Texture2D RightArrowTexture { get; set; }
public static Texture2D UpArrowTexture { get; set; }
public static Texture2D DownArrowTexture { get; set; }
2020-12-21 00:00:05 +00:00
public float regularCrosshairTextureSizeMultiplier = 1.0f;
public float rocketCrosshairTextureSizeMultiplier = 1.5f;
public float regularCrosshairUiSizeMultiplier = 1.0f;
public float rocketCrosshairUiSizeMultiplier = 1.5f;
private Vector2 m_defaultCrosshairSize;
2020-05-31 17:07:22 +00:00
void Awake () {
Instance = this;
2020-04-21 22:28:09 +00:00
Loader.onLoadSpecialTextures += LoadTextures;
2020-12-21 00:00:05 +00:00
m_defaultCrosshairSize = this.crosshairImage.rectTransform.sizeDelta;
2020-04-21 22:28:09 +00:00
}
void LoadTextures()
{
// load arrow textures
F.RunExceptionSafe(() =>
{
var pcbtnsTxd = TextureDictionary.Load("pcbtns");
LeftArrowTexture = pcbtnsTxd.GetDiffuse("left").Texture;
RightArrowTexture = pcbtnsTxd.GetDiffuse("right").Texture;
UpArrowTexture = pcbtnsTxd.GetDiffuse("up").Texture;
DownArrowTexture = pcbtnsTxd.GetDiffuse("down").Texture;
});
2020-04-21 22:28:09 +00:00
2020-12-21 00:00:05 +00:00
LoadCrosshairTextures();
2020-04-21 22:28:09 +00:00
}
2020-12-21 00:00:05 +00:00
void LoadCrosshairTextures()
2020-04-21 22:28:09 +00:00
{
2020-12-21 00:00:05 +00:00
Texture2D regularCrosshairTex = ConstructCrosshairTexture("siteM16", this.regularCrosshairTextureSizeMultiplier);
Texture2D rocketCrosshairTex = ConstructCrosshairTexture("siterocket", this.rocketCrosshairTextureSizeMultiplier);
2020-04-21 22:28:09 +00:00
2020-12-21 00:00:05 +00:00
Weapon.CrosshairTexture = regularCrosshairTex;
Weapon.RocketCrosshairTexture = rocketCrosshairTex;
this.crosshairImage.enabled = true;
this.crosshairImage.texture = regularCrosshairTex;
}
static Texture2D ConstructCrosshairTexture(string textureName, float sizeMultiplier)
{
2020-04-21 22:28:09 +00:00
Texture2D originalTex = TextureDictionary.Load("hud")
2020-12-21 00:00:05 +00:00
.GetDiffuse(textureName, new TextureLoadParams { makeNoLongerReadable = false })
2020-04-21 22:28:09 +00:00
.Texture;
2020-12-21 00:00:05 +00:00
return ConstructCrosshairTexture(originalTex, sizeMultiplier);
}
2020-04-21 22:28:09 +00:00
2020-12-21 00:00:05 +00:00
static Texture2D ConstructCrosshairTexture(Texture2D originalTex, float sizeMultiplier)
{
2020-04-21 22:28:09 +00:00
int originalWidth = originalTex.width;
int originalHeight = originalTex.height;
2020-12-21 00:00:05 +00:00
int newWidth = Mathf.RoundToInt(originalWidth * 2 * sizeMultiplier);
int newHeight = Mathf.RoundToInt(originalHeight * 2 * sizeMultiplier);
2020-04-21 22:28:09 +00:00
2020-12-21 00:00:05 +00:00
Texture2D tex = new Texture2D(newWidth, newHeight, TextureFormat.ARGB32, false, true);
if (sizeMultiplier > 1)
{
// set all pixels to transparent
Color[] emptyColors = new Color[newWidth * newHeight];
tex.SetPixels(emptyColors);
}
2020-04-21 22:28:09 +00:00
// bottom left
for (int i = 0; i < originalWidth; i++)
{
for (int j = 0; j < originalHeight; j++)
{
tex.SetPixel(i, j, originalTex.GetPixel(i, j));
}
}
// bottom right - flip around X axis
for (int i = 0; i < originalWidth; i++)
{
for (int j = 0; j < originalHeight; j++)
{
2020-12-21 00:00:05 +00:00
tex.SetPixel(newWidth - i - 1 + newWidth, j, originalTex.GetPixel(i, j));
2020-04-21 22:28:09 +00:00
}
}
// top left - flip Y axis
for (int i = 0; i < originalWidth; i++)
{
for (int j = 0; j < originalHeight; j++)
{
2020-12-21 00:00:05 +00:00
tex.SetPixel(i, newHeight - j - 1 + newHeight, originalTex.GetPixel(i, j));
2020-04-21 22:28:09 +00:00
}
}
// top right - flip both X and Y axes
for (int i = 0; i < originalWidth; i++)
{
for (int j = 0; j < originalHeight; j++)
{
2020-12-21 00:00:05 +00:00
tex.SetPixel(newWidth - i - 1 + newWidth, newHeight - j - 1 + newHeight, originalTex.GetPixel(i, j));
2020-04-21 22:28:09 +00:00
}
}
2020-05-01 20:26:30 +00:00
tex.Apply(false, true);
2020-04-21 22:28:09 +00:00
2020-12-21 00:00:05 +00:00
return tex;
2020-05-31 17:07:22 +00:00
}
void Update()
{
if (!Loader.HasLoaded)
{
this.canvas.enabled = false;
return;
}
2021-03-01 01:10:27 +00:00
this.canvas.enabled = true;
var ped = Ped.Instance;
2021-03-01 01:10:27 +00:00
bool showPedUi = ped != null;
this.UpdateWeaponUi(ped, showPedUi);
this.UpdateOtherUi(ped, showPedUi);
this.UpdateDayTimeUi();
2021-03-01 01:10:27 +00:00
this.UpdateRadioStationUi(ped, showPedUi);
}
2021-03-01 01:10:27 +00:00
void UpdateWeaponUi(Ped ped, bool enableUi)
{
if (!enableUi)
{
2021-03-01 01:10:27 +00:00
this.crosshairImage.enabled = false;
this.weaponImage.enabled = false;
this.weaponAmmoText.enabled = false;
return;
}
2020-12-21 00:00:05 +00:00
var weapon = ped.CurrentWeapon;
this.crosshairImage.enabled = ped.IsAiming;
2020-12-21 00:00:05 +00:00
if (this.crosshairImage.enabled)
{
if (weapon != null)
{
Texture2D crosshairTextureToDisplay = Weapon.CrosshairTexture;
float uiSizeMultiplier = this.regularCrosshairUiSizeMultiplier;
2020-12-21 00:00:05 +00:00
if (weapon.Data.modelId1 == WeaponId.RocketLauncher || weapon.Data.modelId1 == WeaponId.RocketLauncherHS)
{
crosshairTextureToDisplay = Weapon.RocketCrosshairTexture;
uiSizeMultiplier = this.rocketCrosshairUiSizeMultiplier;
}
if (this.crosshairImage.texture != crosshairTextureToDisplay)
this.crosshairImage.texture = crosshairTextureToDisplay;
Vector2 uiSize = m_defaultCrosshairSize * uiSizeMultiplier;
if (this.crosshairImage.rectTransform.sizeDelta != uiSize)
this.crosshairImage.rectTransform.sizeDelta = uiSize;
}
}
2021-03-01 01:10:27 +00:00
this.weaponImage.enabled = true;
Texture2D weaponTextureToDisplay = weapon != null ? weapon.HudTexture : Weapon.FistTexture;
if (this.weaponImage.texture != weaponTextureToDisplay)
this.weaponImage.texture = weaponTextureToDisplay;
2021-03-01 01:10:27 +00:00
this.weaponAmmoText.enabled = true;
string ammoText = weapon != null ? weapon.AmmoOutsideOfClip + "-" + weapon.AmmoInClip : string.Empty;
if (this.weaponAmmoText.text != ammoText)
this.weaponAmmoText.text = ammoText;
2021-03-01 01:10:27 +00:00
}
2020-04-22 21:58:05 +00:00
2021-03-01 01:10:27 +00:00
void UpdateRadioStationUi(Ped ped, bool enableUi)
{
if (!enableUi)
2020-04-24 22:01:03 +00:00
{
2021-03-01 01:10:27 +00:00
this.radioStationText.enabled = false;
return;
2020-04-24 22:01:03 +00:00
}
string textForRadioStation = "";
2020-05-05 15:45:06 +00:00
var vehicle = ped.CurrentVehicle;
if (vehicle != null)
{
var seat = ped.CurrentVehicleSeat;
2021-03-01 01:10:27 +00:00
if (vehicle.TimeSinceRadioStationChanged < m_radioStationLabelDuration
|| (seat != null && seat.TimeSincePedChanged < m_radioStationLabelDuration))
{
2021-03-01 01:10:27 +00:00
textForRadioStation = vehicle.CurrentRadioStationIndex >= 0 ?
RadioStation.StationNames[vehicle.CurrentRadioStationIndex] : "Radio Off";
}
2020-05-05 15:45:06 +00:00
}
2021-03-01 01:10:27 +00:00
this.radioStationText.enabled = true;
if (this.radioStationText.text != textForRadioStation)
this.radioStationText.text = textForRadioStation;
2021-03-01 01:10:27 +00:00
}
void UpdateOtherUi(Ped ped, bool enableUi)
{
if (!enableUi)
{
this.healthBackgroundImage.enabled = false;
this.healthForegroundImage.enabled = false;
this.pedStateText.enabled = false;
this.pedVelocityText.enabled = false;
return;
}
this.healthForegroundImage.enabled = true;
this.healthBackgroundImage.enabled = true;
float healthPerc = Mathf.Clamp01( ped.Health / ped.MaxHealth );
this.healthForegroundImage.rectTransform.sizeDelta = new Vector2(this.healthBackgroundImage.rectTransform.sizeDelta.x * healthPerc, this.healthForegroundImage.rectTransform.sizeDelta.y);
this.pedStateText.enabled = true;
string pedStateDisplayText = "Current ped state: " + (ped.CurrentState != null ? ped.CurrentState.GetType().Name : "none");
if (pedStateDisplayText != this.pedStateText.text)
this.pedStateText.text = pedStateDisplayText;
this.pedVelocityText.enabled = PedManager.Instance.showPedSpeedometer;
if (this.pedVelocityText.enabled)
{
string pedVelocityDisplayText = string.Format("{0:0.0} km/h", ped.GetComponent<PlayerController>().CurVelocity);
if (pedVelocityDisplayText != this.pedVelocityText.text)
this.pedVelocityText.text = pedVelocityDisplayText;
}
}
void UpdateDayTimeUi()
{
2021-05-04 18:55:01 +00:00
this.dayTimeText.enabled = DayTimeManager.Singleton != null;
if (this.dayTimeText.enabled)
{
string dayTimeDisplayText = DayTimeManager.Singleton.CurrentTimeAsString;
if (this.dayTimeText.text != dayTimeDisplayText)
this.dayTimeText.text = dayTimeDisplayText;
}
}
2020-05-31 17:07:22 +00:00
}
}