Files
UltimateFishing/Assets/Scripts/Assembly-CSharp/EnergyBarToolkit/EnergyBarTransformRenderer.cs
2026-02-21 16:45:37 +08:00

234 lines
5.0 KiB
C#

using UnityEngine;
namespace EnergyBarToolkit
{
[RequireComponent(typeof(EnergyBar))]
[ExecuteInEditMode]
public class EnergyBarTransformRenderer : EnergyBarOnGUIBase
{
public enum TransformType
{
Translate = 0,
Scale = 1,
Rotate = 2
}
public Vector2 screenPosition = new Vector2(10f, 10f);
public bool screenPositionNormalized;
public Vector2 size = new Vector2(100f, 20f);
private Vector2 sizeReal;
private Vector2 sizeOrig;
private Vector2 screenPositionReal;
public Tex textureObject;
public bool transformTranslate;
public bool transformRotate;
public bool transformScale;
public Vector2 transformAnchor = new Vector2(0.5f, 0.5f);
public TranslateFunction translateFunction;
public RotateFunction rotateFunction;
public ScaleFunction scaleFunction;
[SerializeField]
private bool sizeNormalized;
[SerializeField]
private bool screenPositionCalculateSize = true;
public override Rect DrawAreaRect
{
get
{
return new Rect(screenPositionReal.x, screenPositionReal.y, sizeReal.x, sizeReal.y);
}
}
private Vector2 ScreenPositionPixels
{
get
{
if (screenPositionNormalized)
{
return new Vector2(screenPosition.x * (float)Screen.width, screenPosition.y * (float)Screen.height);
}
return screenPosition;
}
}
public override Vector2 SizePixels
{
get
{
return GetSizePixels(size);
}
set
{
SetSizePixels(ref size, value);
}
}
public override Vector2 TextureSizePixels
{
get
{
if (texturesBackground.Length > 0 && texturesBackground[0] != null && texturesBackground[0].Valid)
{
Tex tex = texturesBackground[0];
return new Vector2(tex.width, tex.height);
}
if (texturesForeground.Length > 0 && texturesForeground[0] != null && texturesForeground[0].Valid)
{
Tex tex2 = texturesForeground[0];
return new Vector2(tex2.width, tex2.height);
}
if (textureObject != null && textureObject.Valid)
{
return new Vector2(textureObject.width, textureObject.height);
}
return Vector2.one;
}
}
private new void Start()
{
base.Start();
if (version == 169)
{
Upgrade_169_171();
}
}
private void Upgrade_169_171()
{
if (sizeNormalized)
{
resizeMode = ResizeMode.Stretch;
}
else if (!screenPositionCalculateSize)
{
resizeMode = ResizeMode.Fixed;
}
version = 171;
}
protected override void Update()
{
base.Update();
Texture2D texture2D = AnyBackgroundOrForegroundTexture();
if (texture2D != null)
{
sizeOrig = new Vector2(texture2D.width, texture2D.height);
}
else if (textureObject.Valid)
{
sizeOrig = new Vector2(textureObject.texture.width, textureObject.texture.height);
}
UpdateSize();
}
private void UpdateSize()
{
sizeReal = Round(SizePixels);
screenPositionReal = RealPosition(Round(ScreenPositionPixels), SizePixels);
}
private Texture2D AnyBackgroundOrForegroundTexture()
{
return AnyBackgroundTexture() ?? AnyForegroundTexture();
}
private Texture2D AnyBackgroundTexture()
{
if (texturesBackground != null)
{
Tex[] array = texturesBackground;
foreach (Tex tex in array)
{
if (tex.texture != null)
{
return tex.texture;
}
}
}
return null;
}
private Texture2D AnyForegroundTexture()
{
if (texturesForeground != null)
{
Tex[] array = texturesForeground;
foreach (Tex tex in array)
{
if (tex.texture != null)
{
return tex.texture;
}
}
}
return null;
}
public new void OnGUI()
{
base.OnGUI();
if (RepaintPhase() && IsVisible())
{
UpdateSize();
GUIDrawBackground();
if (textureObject.Valid)
{
DrawObject();
}
GUIDrawForeground();
GUIDrawLabel();
}
}
private void DrawObject()
{
Vector3 vector = Vector3.zero;
if (transformTranslate)
{
vector = translateFunction.Value(ValueF2);
vector = new Vector3(vector.x * sizeReal.x, vector.y * sizeReal.y, 0f);
}
Quaternion rotate = Quaternion.identity;
if (transformRotate)
{
rotate = rotateFunction.Value(ValueF2);
}
Vector3 scale = Vector3.one;
if (transformScale)
{
scale = scaleFunction.Value(ValueF2);
}
float num = (float)textureObject.width * transformAnchor.x;
float num2 = (float)textureObject.height * transformAnchor.y;
Matrix4x4 matrix = Matrix4x4.identity;
MadMatrix.Translate(ref matrix, 0f - num, 0f - num2, 0f);
MadMatrix.Scale(ref matrix, scale);
MadMatrix.Rotate(ref matrix, rotate);
MadMatrix.Scale(ref matrix, sizeReal.x / sizeOrig.x, sizeReal.y / sizeOrig.y, 1f);
MadMatrix.Translate(ref matrix, screenPositionReal);
MadMatrix.Translate(ref matrix, sizeReal.x / 2f, sizeReal.y / 2f, 0f);
MadMatrix.Translate(ref matrix, vector);
GUI.matrix = matrix;
DrawTexture(new Rect(0f, 0f, textureObject.width, textureObject.height), textureObject.texture, textureObject.color);
GUI.matrix = Matrix4x4.identity;
}
}
}