Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Text Assembly for MainMenuMessages #149

Merged
merged 2 commits into from
Jun 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 51 additions & 5 deletions QModManager/Utility/MainMenuMessages.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace QModManager.Utility
{
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections;
using System.Collections.Generic;

using Harmony;
using UnityEngine;
using UnityEngine.SceneManagement;
Expand Down Expand Up @@ -63,6 +63,7 @@ private static void Init()
if (inited)
return;

LoadDynamicAssembly();
messageQueue = new List<string>();
messages = new List<ErrorMessage._Message>();
Patches.Patch();
Expand All @@ -77,7 +78,8 @@ private static void AddInternal(string msg)
var message = ErrorMessage.main.GetExistingMessage(msg);
messages.Add(message);
message.timeEnd += 1e6f;
message.entry.rectTransform.sizeDelta = new Vector2(1920f - ErrorMessage.main.offset.x * 2f, 0f);

GetRectTransform(message).sizeDelta = new Vector2(1920f - ErrorMessage.main.offset.x * 2f, 0f);
}

private static void OnSceneLoaded(Scene scene, LoadSceneMode _)
Expand All @@ -98,8 +100,10 @@ static IEnumerator _waitForLoad()
messages.ForEach(msg => msg.timeEnd = Time.time + 1f);
yield return new WaitForSeconds(1.1f); // wait for messages to dissapear

Vector2 originalSize = ErrorMessage.main.prefabMessage.rectTransform.sizeDelta;
messages.ForEach(msg => msg.entry.rectTransform.sizeDelta = originalSize);
Vector2 originalSize = GetRectTransform(ErrorMessage.main.prefabMessage.GetComponent(SelectedTextType)).sizeDelta;

messages.ForEach(msg => GetRectTransform(msg).sizeDelta = originalSize);

messages.Clear();

Patches.Unpatch();
Expand All @@ -109,6 +113,48 @@ static IEnumerator _waitForLoad()
}
}

#region Dynamic assembly loading

private static Type SelectedTextType;
private static Func<object, RectTransform> GetRectTransform;

private static void LoadDynamicAssembly()
{
if (SelectedTextType == null)
{
Type TxtType = typeof(UnityEngine.UI.Text);
Type TxtProType = Type.GetType("TMPro.TextMeshProUGUI, Unity.TextMeshPro", false, false);

SelectedTextType = TxtProType ?? TxtType;

FieldInfo entryField = SelectedTextType.GetField("entry");
if (TxtProType != null)
{
// Using TextMeshPro
FieldInfo recTransformField = TxtProType.GetField("m_rectTransform");

GetRectTransform = (obj) =>
{
var entry = entryField.GetValue(obj);
return (RectTransform)recTransformField.GetValue(obj);
};
}
else
{
// Using Text
PropertyInfo recTransformProperty = TxtType.GetProperty("rectTransform");

GetRectTransform = (obj) =>
{
var entry = entryField.GetValue(obj);
return (RectTransform)recTransformProperty.GetValue(obj, null);
};
}
}
}

#endregion Dynamic assembly loading

private static class Patches
{
public static HarmonyInstance hInstance { get; private set; }
Expand Down Expand Up @@ -157,7 +203,7 @@ public static void Postfix()
}
}

private static float _getVal(float val, ErrorMessage._Message message) => messages.Contains(message)? 1f: val;
private static float _getVal(float val, ErrorMessage._Message message) => messages.Contains(message) ? 1f : val;

// we changing result for 'float value = Mathf.Clamp01(MathExtensions.EvaluateLine(...' to 1.0f
// so text don't stay in the center of the screen (because of changed 'timeEnd')
Expand Down