using System; using EnergyBarToolkit; using UnityEngine; public abstract class EnergyBarOnGUIBase : EnergyBarBase { protected enum MaterialType { StandardTransparent = 0, StandardTransparentPre = 1, HorizontalFill = 2, HorizontalFillPre = 3, VerticalFill = 4, VerticalFillPre = 5, ExpandFill = 6, ExpandFillPre = 7, RadialFill = 8, RadialFillPre = 9 } public enum ResizeMode { None = 0, Fixed = 1, Stretch = 2, KeepRatio = 3 } protected const string ShaderStandardTransparentName = "Custom/Energy Bar Toolkit/Unlit"; protected const string ShaderStandardTransparentPreName = "Custom/Energy Bar Toolkit/Unlit Pre"; protected const string ShaderHorizontalFillName = "Custom/Energy Bar Toolkit/Horizontal Fill"; protected const string ShaderHorizontalFillPreName = "Custom/Energy Bar Toolkit/Horizontal Fill Pre"; protected const string ShaderVerticalFillName = "Custom/Energy Bar Toolkit/Vertical Fill"; protected const string ShaderVerticalFillPreName = "Custom/Energy Bar Toolkit/Vertical Fill Pre"; protected const string ShaderExpandFillName = "Custom/Energy Bar Toolkit/Expand Fill"; protected const string ShaderExpandFillPreName = "Custom/Energy Bar Toolkit/Expand Fill Pre"; protected const string ShaderRadialFillName = "Custom/Energy Bar Toolkit/Radial Fill"; protected const string ShaderRadialFillPreName = "Custom/Energy Bar Toolkit/Radial Fill Pre"; protected const string ShaderParamProgress = "_Progress"; protected const string ShaderParamColor = "_Color"; protected const string ShaderParamInvert = "_Invert"; protected const string ShaderParamVisibleRect = "_Rect"; public Tex[] texturesBackground = new Tex[0]; public Tex[] texturesForeground = new Tex[0]; public bool premultipliedAlpha; public int guiDepth = 1; public GameObject anchorObject; public Camera anchorCamera; public ResizeMode resizeMode; public Pivot pivot; public bool positionSizeFromTransform; public bool positionSizeFromTransformNormalized; public bool labelOutlineEnabled = true; public Color labelOutlineColor = Color.black; public Pivot labelPivot; public GUISkin labelSkin; private Material[] materials; public abstract Vector2 SizePixels { get; set; } public abstract Vector2 TextureSizePixels { get; } protected override void OnEnable() { base.OnEnable(); CreateMaterial(); base.useGUILayout = true; if (Application.isPlaying) { Debug.LogWarning("OnGUI renderers are deprecated and shouldn't be used anymore. Please switch to new rendering methods. http://goo.gl/cNio5k"); } } protected override void OnDisable() { base.OnDisable(); } protected void OnGUI() { GUI.depth = guiDepth; } protected Material MaterialStandard() { if (premultipliedAlpha) { return materials[1]; } return materials[0]; } protected Material MaterialHorizFill() { if (premultipliedAlpha) { return materials[3]; } return materials[2]; } protected Material MaterialVertFill() { if (premultipliedAlpha) { return materials[5]; } return materials[4]; } protected Material MaterialExpandFill() { if (premultipliedAlpha) { return materials[7]; } return materials[6]; } protected Material MaterialRadialFill() { if (premultipliedAlpha) { return materials[9]; } return materials[8]; } private void CreateMaterial() { if (materials == null || materials.Length == 0) { int num = Enum.GetNames(typeof(MaterialType)).Length; materials = new Material[num]; materials[0] = new Material(Shader.Find("Custom/Energy Bar Toolkit/Unlit")); materials[1] = new Material(Shader.Find("Custom/Energy Bar Toolkit/Unlit Pre")); materials[2] = new Material(Shader.Find("Custom/Energy Bar Toolkit/Horizontal Fill")); materials[3] = new Material(Shader.Find("Custom/Energy Bar Toolkit/Horizontal Fill Pre")); materials[4] = new Material(Shader.Find("Custom/Energy Bar Toolkit/Vertical Fill")); materials[5] = new Material(Shader.Find("Custom/Energy Bar Toolkit/Vertical Fill Pre")); materials[6] = new Material(Shader.Find("Custom/Energy Bar Toolkit/Expand Fill")); materials[7] = new Material(Shader.Find("Custom/Energy Bar Toolkit/Expand Fill Pre")); materials[8] = new Material(Shader.Find("Custom/Energy Bar Toolkit/Radial Fill")); materials[9] = new Material(Shader.Find("Custom/Energy Bar Toolkit/Radial Fill Pre")); Material[] array = materials; foreach (Material material in array) { material.hideFlags = HideFlags.DontSave; } } } private void DestroyMaterial() { Material[] array = materials; foreach (Material obj in array) { if (Application.isPlaying) { UnityEngine.Object.Destroy(obj); } else { UnityEngine.Object.DestroyImmediate(obj); } } materials = null; } protected void ShaderSetProgress(float progress, Material mat) { mat.SetFloat("_Progress", progress); } protected void ShaderSetInvert(bool invert, Material mat) { mat.SetFloat("_Invert", invert ? 1 : 0); } protected void ShaderSetColor(Color color, Material mat) { mat.SetColor("_Color", color); } protected void ShaderSetVisibleRect(Rect visibleRect, Material mat) { mat.SetVector("_Rect", ToVector4(visibleRect)); } protected void GUIDrawBackground() { if (texturesBackground == null) { return; } Rect drawAreaRect = DrawAreaRect; Tex[] array = texturesBackground; foreach (Tex tex in array) { Texture2D texture = tex.texture; if (texture != null) { DrawTexture(drawAreaRect, texture, tex.color); } } } protected void GUIDrawForeground() { if (texturesForeground == null) { return; } Rect drawAreaRect = DrawAreaRect; Tex[] array = texturesForeground; foreach (Tex tex in array) { Texture2D texture = tex.texture; if (texture != null) { DrawTexture(drawAreaRect, texture, tex.color); } } } protected void DrawTexture(Rect rect, Texture2D texture, Color tint) { Material material = MaterialStandard(); material.SetColor("_Color", ComputeColor(tint)); Graphics.DrawTexture(rect, texture, material); } protected void DrawTexture(Rect rect, Texture2D texture, Rect coords, Color tint) { Material material = MaterialStandard(); material.SetColor("_Color", ComputeColor(tint)); Graphics.DrawTexture(rect, texture, coords, 0, 0, 0, 0, material); } protected void DrawTextureHorizFill(Rect rect, Texture2D texture, Rect visibleRect, Color color, bool invert, float progress) { Material material = MaterialHorizFill(); material.SetColor("_Color", ComputeColor(color)); material.SetFloat("_Invert", invert ? 1 : 0); material.SetFloat("_Progress", progress); material.SetVector("_Rect", ToVector4(visibleRect)); Graphics.DrawTexture(rect, texture, material); } protected void DrawTextureVertFill(Rect rect, Texture2D texture, Rect visibleRect, Color color, bool invert, float progress) { Material material = MaterialVertFill(); material.SetColor("_Color", ComputeColor(color)); material.SetFloat("_Invert", invert ? 1 : 0); material.SetFloat("_Progress", progress); material.SetVector("_Rect", ToVector4(visibleRect)); Graphics.DrawTexture(rect, texture, material); } protected void DrawTextureExpandFill(Rect rect, Texture2D texture, Rect visibleRect, Color color, bool invert, float progress) { Material material = MaterialExpandFill(); material.SetColor("_Color", ComputeColor(color)); material.SetFloat("_Invert", invert ? 1 : 0); material.SetFloat("_Progress", progress); material.SetVector("_Rect", ToVector4(visibleRect)); Graphics.DrawTexture(rect, texture, material); } protected void DrawTextureRadialFill(Rect rect, Texture2D texture, Color color, bool invert, float progress, float offset, float length) { Material material = MaterialRadialFill(); material.SetColor("_Color", ComputeColor(color)); material.SetFloat("_Invert", invert ? 1 : 0); material.SetFloat("_Progress", progress); material.SetFloat("_Offset", offset); material.SetFloat("_Length", length); Graphics.DrawTexture(rect, texture, material); } protected void GUIDrawLabel() { if (labelEnabled) { GUISkin skin = labelSkin; if (skin == null) { skin = GUI.skin; } float num = 1f; string text = LabelFormatResolve(labelFormat); Vector2 vector = skin.label.CalcSize(new GUIContent(text)); if (labelOutlineEnabled) { float num2 = num * 2f; vector.x += num2; vector.y += num2; } Vector2 labelPositionPixels = base.LabelPositionPixels; Rect r = new Rect(labelPositionPixels.x, labelPositionPixels.y, vector.x, vector.y); r = ApplyLabelPivot(r); GUI.color = labelColor; if (labelOutlineEnabled) { LabelWithOutline(r, text, skin.label, labelOutlineColor, num); } else { GUI.Label(r, text, skin.label); } GUI.color = Color.white; } } private Rect ApplyLabelPivot(Rect r) { switch (labelPivot) { case Pivot.TopLeft: return r; case Pivot.Top: return new Rect(r.x - r.width / 2f, r.y, r.width, r.height); case Pivot.TopRight: return new Rect(r.x - r.width, r.y, r.width, r.height); case Pivot.Right: return new Rect(r.x - r.width, r.y - r.height / 2f, r.width, r.height); case Pivot.BottomRight: return new Rect(r.x - r.width, r.y - r.height, r.width, r.height); case Pivot.Bottom: return new Rect(r.x - r.width / 2f, r.y - r.height, r.width, r.height); case Pivot.BottomLeft: return new Rect(r.x, r.y - r.height, r.width, r.height); case Pivot.Left: return new Rect(r.x, r.y - r.height / 2f, r.width, r.height); case Pivot.Center: return new Rect(r.x - r.width / 2f, r.y - r.height / 2f, r.width, r.height); default: Debug.LogError("Unknown pivot: " + labelPivot); return r; } } private void LabelWithOutline(Rect rect, string text, GUIStyle style, Color color, float size) { Color color2 = GUI.color; GUI.color = color; Vector2[] array = new Vector2[4] { new Vector2(1f, 0f), new Vector2(-1f, 0f), new Vector2(0f, 1f), new Vector2(0f, -1f) }; for (int i = 0; i < array.Length; i++) { Vector2 vector = array[i]; Rect position = new Rect(rect.x + vector.x * size, rect.y + vector.y * size, rect.width, rect.height); GUI.Label(position, text, style); } GUI.color = color2; GUI.Label(rect, text, style); } protected Vector2 TransformScale() { if (positionSizeFromTransform) { Vector3 lossyScale = base.transform.lossyScale; return new Vector2(lossyScale.x, lossyScale.y); } return Vector2.one; } protected Vector2 RealPosition(Vector2 pos, Vector2 bounds) { Vector2 vector = Vector2.zero; if (positionSizeFromTransform) { vector = ((!positionSizeFromTransformNormalized) ? new Vector2(base.transform.position.x, 0f - base.transform.position.y) : new Vector2(base.transform.position.x * (float)Screen2.width, (0f - base.transform.position.y) * (float)Screen2.height)); pos.Scale(base.transform.lossyScale); } Vector2 vector2 = Vector2.zero; Vector2 vector3 = Vector2.zero; switch (pivot) { case Pivot.TopLeft: vector2 = Vector2.zero; vector3 = Vector2.zero; break; case Pivot.Top: vector2 = new Vector2(Screen2.width / 2, 0f); vector3 = new Vector2(bounds.x / 2f, 0f); break; case Pivot.TopRight: vector2 = new Vector2(Screen2.width, 0f); vector3 = new Vector2(bounds.x, 0f); break; case Pivot.Right: vector2 = new Vector2(Screen2.width, Screen2.height / 2); vector3 = new Vector2(bounds.x, bounds.y / 2f); break; case Pivot.BottomRight: vector2 = new Vector2(Screen2.width, Screen2.height); vector3 = new Vector2(bounds.x, bounds.y); break; case Pivot.Bottom: vector2 = new Vector2(Screen2.width / 2, Screen2.height); vector3 = new Vector2(bounds.x / 2f, bounds.y); break; case Pivot.BottomLeft: vector2 = new Vector2(0f, Screen2.height); vector3 = new Vector2(0f, bounds.y); break; case Pivot.Left: vector2 = new Vector2(0f, Screen2.height / 2); vector3 = new Vector2(0f, bounds.y / 2f); break; case Pivot.Center: vector2 = new Vector2(Screen2.width / 2, Screen2.height / 2); vector3 = new Vector2(bounds.x / 2f, bounds.y / 2f); break; } if (anchorObject != null) { Camera camera = ((!(anchorCamera != null)) ? Camera.main : anchorCamera); vector2 = camera.WorldToScreenPoint(anchorObject.transform.position); vector2.y = (float)Screen2.height - vector2.y; } return vector + vector2 - vector3 + pos; } protected override Color ComputeColor(Color localColor) { Color color = base.ComputeColor(localColor); if (premultipliedAlpha) { color = PremultiplyAlpha(color); } return color; } protected Vector2 GetSizePixels(Vector2 manualSize) { Vector2 result; switch (resizeMode) { case ResizeMode.None: result = TextureSizePixels; break; case ResizeMode.Fixed: result = manualSize; break; case ResizeMode.Stretch: result = new Vector2(manualSize.x * (float)Screen2.width, manualSize.y * (float)Screen2.height); break; case ResizeMode.KeepRatio: { Vector2 textureSizePixels = TextureSizePixels; float num = manualSize.y * (float)Screen2.height; float x = textureSizePixels.x / textureSizePixels.y * num; result = new Vector2(x, num); break; } default: Debug.LogError("Unknown resize mode: " + resizeMode); result = manualSize; break; } result.Scale(TransformScale()); return result; } protected void SetSizePixels(ref Vector2 manualSize, Vector2 value) { switch (resizeMode) { case ResizeMode.None: break; case ResizeMode.Fixed: manualSize = value; break; case ResizeMode.Stretch: manualSize = new Vector2(value.x / (float)Screen2.width, value.y / (float)Screen2.height); break; case ResizeMode.KeepRatio: { Vector2 textureSizePixels = TextureSizePixels; float num = value.y / (float)Screen2.height; float x = textureSizePixels.x / textureSizePixels.y * num; manualSize = new Vector2(x, num); break; } default: Debug.LogError("Unknown resize mode: " + resizeMode); manualSize = value; break; } } public void ResetSize() { SizePixels = TextureSizePixels; } protected override bool IsVisible() { if (!base.IsVisible()) { return false; } if (anchorObject != null) { Camera camera = ((!(anchorCamera != null)) ? Camera.main : anchorCamera); Vector3 lhs = anchorObject.transform.position - camera.transform.position; float num = Vector3.Dot(lhs, camera.transform.forward); return num >= 0f; } return true; } }