-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZoomToMouse.cs
95 lines (80 loc) · 2.79 KB
/
ZoomToMouse.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HarmonyLib;
using Game.Session.Input;
using Game.Session.Board;
using Game.Services;
using UnityEngine;
namespace TDEnhancementPack
{
//------------------
//Stack zoom requests
//------------------
[HarmonyPatch(typeof(BaseInputMode), nameof(BaseInputMode.OnZoom))]
public static class StackZoomWheel
{
//public virtual bool OnZoom(Vector2? pos, float zoomfactor, bool tween)
public static bool Prefix(ref bool __result, BaseInputMode __instance, float zoomfactor, bool tween)
{
if (Game.Util.UIUtil.IsInputOverUI)
{
__result = false;
return false;
}
zoomfactor /= 3;//Honestly it's zooming too fast.
//float zoom = Game.Game.serv.camera.GetZoom();
//This is the current zoom, but where is it currently zooming to?
float zoom = DesiredZoom.desiredZoom;
float zoomFactor = 1f + Mathf.Abs(zoomfactor * __instance.ZoomMultiplier);
zoom = zoomfactor >= 0f ? (zoom / zoomFactor) : (zoom * zoomFactor);
__instance._isDown = false;
Game.Game.serv.camera.SetZoom(zoom, tween, 0.15f);
__result = true;
return false;
}
}
[HarmonyPatch(typeof(CameraService), nameof(CameraService.SetZoom))]
public static class DesiredZoom
{
public static float desiredZoom;
//public void SetZoom(float zoom, bool tween, float tweenSeconds = 0.3f)
public static void Prefix(CameraService __instance, float zoom)
{
desiredZoom = __instance.ClampZoom(zoom);//Whether it's instant or gradual, this is where we're headed.
}
}
//------------------
//Zoom to mouse
//------------------
[HarmonyPatch(typeof(CameraService), nameof(CameraService.CameraZoomHelper))]
public static class ZoomToMouse
{
//private void CameraZoomHelper(float val)
public static bool Prefix(CameraService __instance, float val)
{
//Get where the mouse is
Vector3 mousePosition = UnityEngine.Input.mousePosition;
WorldPos oldpos = __instance.ScreenToWorldPos(mousePosition);
//Apply zoom
val = __instance.ClampZoom(val);
__instance.main.orthographicSize = val;
//Find out where we are now:
WorldPos newpos = __instance.ScreenToWorldPos(mousePosition);
//Find how much we slide away from cursor and adjust:
Vector3 newTransform = new Vector3(
__instance.main.transform.position.x + oldpos.x - newpos.x,
__instance.main.transform.position.y + oldpos.y - newpos.y,
__instance.main.transform.position.z);
//clamp panning position for out of bounds
Vector2 transformPos = __instance.ClampPos(newTransform, val);
//ClampPos takes Vector3 spits out a Vector 2 :/
newTransform.x = transformPos.x; newTransform.y = transformPos.y;
//And set it
__instance.main.transform.position = newTransform;
__instance.OnCameraZoomChanged.Invoke();
return false;
}
}
}