-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.cs
101 lines (92 loc) · 3.57 KB
/
Plugin.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
using BepInEx;
using HarmonyLib;
using Aki.Reflection.Patching;
using System.Reflection;
using EFT.UI.DragAndDrop;
using UnityEngine;
using UnityEngine.UI;
using System;
using EFT.InventoryLogic;
using Comfort.Common;
using ItemSize = GStruct24;
using ItemTemplate = GClass2888;
using System.Linq;
namespace QuickSlots
{
[BepInPlugin("com.faupi.quickslots", "QuickSlots", "0.0.1")]
public class Plugin : BaseUnityPlugin
{
void Awake()
{
new UpdateScalePatch().Enable();
}
}
public class UpdateScalePatch : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return typeof(QuickSlotItemView).GetMethod("UpdateScale", BindingFlags.Instance | BindingFlags.Public);
}
[PatchPrefix]
private static bool Prefix(QuickSlotItemView __instance)
{
var __mainImage = (Image)AccessTools.Field(typeof(ItemView), "MainImage").GetValue(__instance);
bool mainImageInactive = !__mainImage.gameObject.activeSelf; // Image isn't visible
bool isWeapon = __instance.Item is Weapon; // Do not handle weapons, those generally should stay rotated
if (mainImageInactive || isWeapon)
{
// Skip handling (go to base method)
return true;
}
Quaternion quatRotation;
Vector3 localScale;
if (__instance.IconScale != null)
{
float rotation = 0f;
ItemSize itemSize = __instance.Item.CalculateCellSize();
bool itemIsWide = itemSize.X >= itemSize.Y * 1.5;
bool itemIsTall = itemSize.Y >= itemSize.X * 2.5; // 1x2 is fine upright but anything bigger should rotate the other way
bool itemIsStim = false;
try
{
ItemTemplate itemTemplate = Singleton<HandbookClass>.Instance.StructuredItems[__instance.Item.TemplateId];
itemIsStim = itemTemplate.Category.Contains("5b47574386f77428ca22b33a");
}
catch (Exception ex)
{
Logger.LogError($"Could not get item template: {ex.Message}");
}
// Only rotate items if they're meaningfully bigger horizontally
if (itemIsWide)
{
rotation = 45f;
}
else if (itemIsTall)
{
rotation = -45f;
}
// Stims are already -45f by default - we want to turn it to the other side
else if (itemIsStim)
{
rotation = -90f;
}
// From here on it's basically the vanilla handling
quatRotation = Quaternion.Euler(0f, 0f, rotation);
Vector3 vector = quatRotation * __mainImage.rectTransform.rect.size;
float scaleX = __instance.IconScale.Value.x / Mathf.Abs(vector.x);
float scaleY = __instance.IconScale.Value.y / Mathf.Abs(vector.y);
localScale = Vector3.one * Math.Min(scaleX, scaleY);
}
else
{
localScale = Vector3.one;
quatRotation = Quaternion.identity;
}
Transform transform = __mainImage.transform;
transform.localRotation = quatRotation;
transform.localScale = localScale;
// We did all we wanted with the customs, don't do anything else.
return false;
}
}
}