From 7dda94920afc06be8f04e0af97cf8fbafa278ab9 Mon Sep 17 00:00:00 2001 From: Sineme Date: Wed, 28 Dec 2022 16:15:16 +0100 Subject: [PATCH] Fix horizontal scroll area fixes #157, #159 and #164 The scroll area was using cachedSize for its output rectangle capping it at the screen width. As the "Work"-tab window is not as wide as the screen, ouput rectangle was too wide when there were many work types. To fix this the PawnTable was extended with the property methods void set_OutRect(this PawnTable, Rect) and Rect get_OutRect() 'fake adding' a new property that can be used to set and get the "Work"-tab window dimension instead of relying on the screen size. MainTabWindow_WorkTab.DoWindowContents calls set_OutRect with the window dimension PawnTable_PawnTableOnGUI.PawnTableOnGUI calls get_OutRect to clamp the output rectangle properly I think ideally PawnTable.PawnTableOnGUI's parameter would be Rect window instead of Vector2 position in the future. --- Source/Extensions/PawnTable_Extensions.cs | 35 +++++++++++++++++++ .../PawnTable/PawnTable_PawnTableOnGUI.cs | 7 +++- Source/PawnTable/MainTabWindow_WorkTab.cs | 2 ++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 Source/Extensions/PawnTable_Extensions.cs diff --git a/Source/Extensions/PawnTable_Extensions.cs b/Source/Extensions/PawnTable_Extensions.cs new file mode 100644 index 0000000..5d94289 --- /dev/null +++ b/Source/Extensions/PawnTable_Extensions.cs @@ -0,0 +1,35 @@ +using RimWorld; +using RimWorld.BaseGen; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; + +namespace WorkTab.Extensions { + internal static class PawnTable_Extensions { + private static ConditionalWeakTable> outRectDictionary=new ConditionalWeakTable>(); + /// + /// Sets the rectangle the is drawn in. + /// + /// The being extended. + /// The rectangle the will be drawn in. + internal static void set_OutRect(this PawnTable pawnTable, Rect outRect) { + var value = outRectDictionary.GetValue( + pawnTable, + a => new StrongBox(outRect) + ); + value.Value = outRect; + } + /// + /// Gets the rectangle the will be drawn in. + /// + /// The being extended. + /// The rectangle the will be drawn in. + internal static Rect get_OutRect(this PawnTable pawnTable) { + return outRectDictionary.GetOrCreateValue(pawnTable).Value; + } + } +} diff --git a/Source/HarmonyPatches/PawnTable/PawnTable_PawnTableOnGUI.cs b/Source/HarmonyPatches/PawnTable/PawnTable_PawnTableOnGUI.cs index 28462f6..3c0d8bd 100644 --- a/Source/HarmonyPatches/PawnTable/PawnTable_PawnTableOnGUI.cs +++ b/Source/HarmonyPatches/PawnTable/PawnTable_PawnTableOnGUI.cs @@ -8,6 +8,7 @@ using RimWorld; using UnityEngine; using Verse; +using WorkTab.Extensions; namespace WorkTab { [HarmonyPatch(typeof(PawnTable), nameof(PawnTable.PawnTableOnGUI))] @@ -79,7 +80,11 @@ public static bool Prefix(PawnTable __instance, // Instead, we want to limit outRect to the available view area, so a horizontal scrollbar can appear. float labelWidth = cachedColumnWidths[0]; var labelCol = columns[0]; - float outWidth = Mathf.Min( cachedSize.x - labelWidth, UI.screenWidth - (standardWindowMargin * 2f) ); + // ideally this method would be called with a Rect outRect + // indicating the window it is being drawn in instead + // of a Vector2 position + var outRect = __instance.get_OutRect(); + float outWidth = outRect.width - labelWidth; float viewWidth = cachedSize.x - labelWidth - 16f; Rect labelHeaderRect = new Rect( diff --git a/Source/PawnTable/MainTabWindow_WorkTab.cs b/Source/PawnTable/MainTabWindow_WorkTab.cs index 4e1bf05..f4c8009 100644 --- a/Source/PawnTable/MainTabWindow_WorkTab.cs +++ b/Source/PawnTable/MainTabWindow_WorkTab.cs @@ -10,6 +10,7 @@ using RimWorld.Planet; using UnityEngine; using Verse; +using WorkTab.Extensions; using static WorkTab.Constants; using static WorkTab.InteractionUtilities; using static WorkTab.Resources; @@ -122,6 +123,7 @@ public static void SelectWholeDay() { } public override void DoWindowContents(Rect rect) { + Instance.Table.set_OutRect(rect); if (_columnsChanged) { RebuildTable(); }