From 2723f397c330f47eb58f2f55f0f1aaca6a30336d Mon Sep 17 00:00:00 2001
From: James Lindsay <78500760+0HyperCube@users.noreply.github.com>
Date: Tue, 7 Jan 2025 15:00:20 +0000
Subject: [PATCH] Improve offset clamping on the evolutionary tree (#5787)
* Improve clamping on the evolutionary tree
* Format
---
src/auto-evo/EvolutionaryTree.cs | 32 +++++++++++++++-----------------
1 file changed, 15 insertions(+), 17 deletions(-)
diff --git a/src/auto-evo/EvolutionaryTree.cs b/src/auto-evo/EvolutionaryTree.cs
index 327750a3bd..bdf2d9a9d0 100644
--- a/src/auto-evo/EvolutionaryTree.cs
+++ b/src/auto-evo/EvolutionaryTree.cs
@@ -70,6 +70,11 @@ public partial class EvolutionaryTree : Control
///
private static readonly Vector2 DrawMargin = new(DRAW_MARGIN, DRAW_MARGIN);
+ ///
+ /// Additional padding to ensure some part of the tree remains on the screen
+ ///
+ private static readonly Vector2 TreePadding = new(GENERATION_SEPARATION, SPECIES_SEPARATION);
+
///
/// Stores the created nodes for species by the species ids
///
@@ -155,8 +160,7 @@ public partial class EvolutionaryTree : Control
public IReadOnlyDictionary SpeciesOrigin => speciesOrigin;
- private Vector2 TreeSize =>
- new(latestGeneration * GENERATION_SEPARATION + 200, maxSpeciesId * SPECIES_SEPARATION + 100);
+ private Vector2 TreeSize => new(latestGeneration * GENERATION_SEPARATION, maxSpeciesId * SPECIES_SEPARATION);
public override void _Ready()
{
@@ -389,8 +393,11 @@ private void TimelineDraw()
int increment = (int)Math.Ceiling(1 / sizeFactor);
// Draw time marks
- int firstDrawnGeneration =
- (int)Math.Ceiling((-dragOffset.X - TreeNodeSize.X / 2) / GENERATION_SEPARATION / increment) * increment;
+ int firstVisibleGeneration =
+ (int)Math.Ceiling((-dragOffset.X - TreeNodeSize.X / 2) / GENERATION_SEPARATION / increment);
+
+ // Don't draw lines past the first generation
+ int firstDrawnGeneration = Math.Max(0, firstVisibleGeneration) * increment;
int lastDrawnGeneration =
Math.Min((int)Math.Floor((Size.X / sizeFactor - dragOffset.X - TreeNodeSize.X / 2) /
@@ -469,19 +476,10 @@ private void GUIInput(InputEvent @event, bool horizontalOnly)
private void BindOffsetToTreeSize()
{
- // TreeSize may be less than RectSize, so the later Min and Max is not merged into Clamp.
- // Note that dragOffset's x and y should both be negative.
- var start = tree.Size / sizeFactor - TreeSize;
-
- float x = dragOffset.X;
- x = Math.Max(x, start.X);
- x = Math.Min(x, 0);
-
- float y = dragOffset.Y;
- y = Math.Max(y, start.Y);
- y = Math.Min(y, 0);
-
- dragOffset = new Vector2(x, y);
+ // The drag offset should go past the edge of the tree to allow zooming around the cursor:
+ // https://github.com/Revolutionary-Games/Thrive/issues/3716
+ // However some part of the tree should remain visible as determined by TREE_PADDING.
+ dragOffset = dragOffset.Max(-TreeSize + TreePadding).Min(tree.Size / sizeFactor - TreePadding);
}
///