Skip to content

Commit d487379

Browse files
committed
feat: scrollable history, partially done
1 parent fb2b02c commit d487379

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

Source/Gameplay/ConsoleEnhancement.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Celeste.Mod.Core;
22
using Celeste.Mod.TASHelper.Utils;
3+
using Microsoft.Xna.Framework.Input;
34
using Mono.Cecil.Cil;
45
using Monocle;
56
using MonoMod.Cil;
@@ -14,7 +15,11 @@ public static class ConsoleEnhancement {
1415

1516
private static bool lastOpen = false;
1617

18+
private static int historyLineShift = 0;
1719

20+
private static int origValue;
21+
22+
private static bool historyScrollEnabled => TasHelperSettings.EnableScrollableHistoryLog;
1823
public static void SetOpenConsole() {
1924
if (Manager.Running && !lastOpen) {
2025
openConsole = true;
@@ -24,16 +29,24 @@ public static bool GetOpenConsole() { // openConsole.getter may not be called (e
2429
return openConsole;
2530
}
2631

32+
public static void GoBackToBottom() {
33+
historyLineShift = 0;
34+
}
35+
2736
[Load]
2837
public static void Load() {
2938
IL.Monocle.Commands.UpdateClosed += ILCommandUpdateClosed;
3039
On.Celeste.Level.BeforeRender += OnLevelBeforeRender;
40+
IL.Monocle.Commands.Render += ILCommandsRender;
41+
On.Monocle.Commands.UpdateOpen += OnCommandUpdateOpen;
3142
}
3243

3344
[Unload]
3445
public static void Unload() {
3546
IL.Monocle.Commands.UpdateClosed -= ILCommandUpdateClosed;
3647
On.Celeste.Level.BeforeRender -= OnLevelBeforeRender;
48+
IL.Monocle.Commands.Render -= ILCommandsRender;
49+
On.Monocle.Commands.UpdateOpen -= OnCommandUpdateOpen;
3750
}
3851

3952
[Initialize]
@@ -42,6 +55,74 @@ public static void Initialize() {
4255
typeof(Manager).GetMethod("DisableRun").HookAfter(MinorBugFixer);
4356
}
4457

58+
private static void ILCommandsRender(ILContext context) {
59+
ILCursor cursor = new ILCursor(context);
60+
if (cursor.TryGotoNext(
61+
ins => ins.MatchLdarg(0),
62+
ins => ins.MatchLdfld<Monocle.Commands>("drawCommands"),
63+
ins => ins.MatchCallOrCallvirt<List<Monocle.Commands.Line>>("get_Count"),
64+
ins => ins.MatchLdcI4(0),
65+
ins => ins.OpCode == OpCodes.Ble,
66+
ins => ins.MatchLdloc(1))) {
67+
cursor.Index += 5;
68+
ILLabel end = (ILLabel) cursor.Prev.Operand;
69+
cursor.Emit(OpCodes.Ldarg_0);
70+
cursor.EmitDelegate(BeforeAction);
71+
cursor.GotoLabel(end);
72+
cursor.Emit(OpCodes.Ldarg_0);
73+
cursor.EmitDelegate(AfterAction);
74+
}
75+
}
76+
77+
private static void BeforeAction(Monocle.Commands commands) {
78+
origValue = commands.firstLineIndexToDraw;
79+
commands.firstLineIndexToDraw = Calc.Clamp(commands.firstLineIndexToDraw + historyLineShift, 0, Math.Max(commands.drawCommands.Count - 1, 0));
80+
}
81+
82+
private static void AfterAction(Monocle.Commands commands) {
83+
if (commands.drawCommands.Count > 0) {
84+
historyLineShift = commands.firstLineIndexToDraw - origValue; // this automatically bounds our shift
85+
commands.firstLineIndexToDraw = origValue;
86+
}
87+
}
88+
89+
private static int repeatCounter = 0;
90+
91+
private static void OnCommandUpdateOpen(On.Monocle.Commands.orig_UpdateOpen orig, Monocle.Commands commands) {
92+
orig(commands);
93+
if (historyScrollEnabled) {
94+
bool controlPressed = commands.currentState[Keys.LeftControl] == KeyState.Down || commands.currentState[Keys.RightControl] == KeyState.Down;
95+
if (commands.currentState[Keys.PageUp] == KeyState.Down && commands.oldState[Keys.PageUp] == KeyState.Up) {
96+
historyLineShift += (Engine.ViewHeight - 100) / 30;
97+
}
98+
else if (commands.currentState[Keys.PageDown] == KeyState.Down && commands.oldState[Keys.PageDown] == KeyState.Up) {
99+
if (controlPressed) {
100+
historyLineShift = 0;
101+
}
102+
else {
103+
historyLineShift -= (Engine.ViewHeight - 100) / 30;
104+
}
105+
}
106+
else if (commands.currentState[Keys.Up] == KeyState.Down && controlPressed) {
107+
repeatCounter += 1;
108+
while (repeatCounter >= 6) {
109+
repeatCounter -= 2;
110+
historyLineShift += 1;
111+
}
112+
}
113+
else if (commands.currentState[Keys.Down] == KeyState.Down && controlPressed) {
114+
repeatCounter += 1;
115+
while (repeatCounter >= 6) {
116+
repeatCounter -= 2;
117+
historyLineShift -= 1;
118+
}
119+
}
120+
else {
121+
repeatCounter = 0;
122+
}
123+
}
124+
}
125+
45126
private static void MinorBugFixer() {
46127
// if open debugconsole and close it when in tas, then exit tas (without running any frame), debugconsole will show up
47128

Source/Module/Menu/TASHelperMenu.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ private static EaseInSubMenu CreateMoreOptionsSubMenu(TextMenu menu) {
148148
subMenu.Add(new TextMenu.OnOff("RotateSpinner Track".ToDialogText(), TasHelperSettings.UsingRotateSpinnerTrack).Change(value => TasHelperSettings.UsingRotateSpinnerTrack = value));
149149
subMenu.Add(new TextMenu.OnOff("TrackSpinner Track".ToDialogText(), TasHelperSettings.UsingTrackSpinnerTrack).Change(value => TasHelperSettings.UsingTrackSpinnerTrack = value));
150150
subMenu.Add(new TextMenu.OnOff("Open Console In Tas".ToDialogText(), TasHelperSettings.EnableOpenConsoleInTas).Change(value => TasHelperSettings.EnableOpenConsoleInTas = value));
151+
subMenu.Add(new TextMenu.OnOff("Scrollable History Log".ToDialogText(), TasHelperSettings.EnableScrollableHistoryLog).Change(value => TasHelperSettings.EnableScrollableHistoryLog = value));
151152
TextMenu.Item OoOItem;
152153
subMenu.Add(OoOItem = new TextMenu.OnOff("Order of Operation Stepping".ToDialogText(), TasHelperSettings.EnableOoO).Change(value => TasHelperSettings.EnableOoO = value));
153154
subMenu.AddDescription(menu, OoOItem, "Order of Operation Description".ToDialogText());

Source/Module/TASHelperSettings.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ internal void Sleep() {
9797
Awake_PredictFuture = false;
9898
Awake_EnableOoO = false;
9999
Awake_OpenConsoleInTas = false;
100+
Awake_ScrollableHistoryLog = false;
100101
}
101102
internal void Awake(bool awakeAll) {
102103
MainSwitch = awakeAll ? MainSwitchModes.AllowAll : MainSwitchModes.OnlyDefault;
@@ -115,6 +116,7 @@ internal void Awake(bool awakeAll) {
115116
Awake_PredictFuture = true;
116117
Awake_EnableOoO = true;
117118
Awake_OpenConsoleInTas = true;
119+
Awake_ScrollableHistoryLog = true;
118120
}
119121

120122
#endregion
@@ -652,6 +654,19 @@ public bool EnableOpenConsoleInTas {
652654
}
653655
}
654656

657+
public bool enableScrollableHistoryLog { get; set; } = true;
658+
659+
public bool Awake_ScrollableHistoryLog = true;
660+
661+
[YamlIgnore]
662+
public bool EnableScrollableHistoryLog {
663+
get => Enabled && Awake_ScrollableHistoryLog && enableScrollableHistoryLog;
664+
set {
665+
enableScrollableHistoryLog = value;
666+
Awake_ScrollableHistoryLog = true;
667+
}
668+
}
669+
655670
#endregion
656671

657672
#region HotKey

0 commit comments

Comments
 (0)