Skip to content

Commit 4059b91

Browse files
author
hamstar0
committed
v5.0.0 - Added failsafe screen space constraint enforcing for widgets
1 parent 94f69c7 commit 4059b91

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

HUDElementsLib/Elements/VanillaHUDElement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ internal VanillaHUDElement( VanillaHUDElementDefinition def )
6767
public override Vector2 GetHUDComputedPosition( bool applyDisplacement ) {
6868
(Vector2 relPos, Vector2 percPos) = this.DynamicIntendedPosition();
6969

70-
this.CurrentRelativePosition = relPos;
70+
this.CurrentPositionOffset = relPos;
7171
this.CurrentPositionPercent = percPos;
7272

7373
return base.GetHUDComputedPosition( applyDisplacement );

HUDElementsLib/HUDElement.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ public partial class HUDElement : UIElement {
2222
/// <summary>
2323
/// Original starting screen offset amount from percent anchors. Element will return here when user sets it to reset.
2424
/// </summary>
25-
protected Vector2 OriginalRelativePosition;
25+
protected Vector2 OriginalPositionOffset;
2626
/// <summary></summary>
2727
protected Vector2 OriginalPositionPercent;
2828

2929
/// <summary>Get screen offset amount from percent anchors.</summary>
30-
protected Vector2 CurrentRelativePosition;
30+
protected Vector2 CurrentPositionOffset;
3131
/// <summary></summary>
3232
protected Vector2 CurrentPositionPercent;
3333

@@ -68,8 +68,8 @@ public HUDElement(
6868
Func<bool> enabler ) : base() {
6969
this.Name = name;
7070

71-
this.OriginalRelativePosition = positionOffset;
72-
this.CurrentRelativePosition = positionOffset;
71+
this.OriginalPositionOffset = positionOffset;
72+
this.CurrentPositionOffset = positionOffset;
7373

7474
this.OriginalPositionPercent = positionPercent;
7575
this.CurrentPositionPercent = positionPercent;
@@ -128,7 +128,7 @@ public virtual bool ConsumesCursor() {
128128
////////////////
129129

130130
public void ResetPositionToDefault() {
131-
this.CurrentRelativePosition = this.OriginalRelativePosition;
131+
this.CurrentPositionOffset = this.OriginalPositionOffset;
132132
}
133133
}
134134
}

HUDElementsLib/HUDElement_Physicality.cs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@
55

66
namespace HUDElementsLib {
77
public partial class HUDElement : UIElement {
8+
public static Vector2 ComputeScreenPosition( Vector2 positionOffset, Vector2 positionPercent ) {
9+
return new Vector2(
10+
(positionPercent.X * (float)Main.screenWidth ) + (float)positionOffset.X,
11+
(positionPercent.Y * (float)Main.screenHeight ) + (float)positionOffset.Y
12+
);
13+
}
14+
15+
public static void FitOffsetToScreen( ref Vector2 positionOffset, Vector2 positionPercent, Vector2 dim ) {
16+
Vector2 origScrPos = HUDElement.ComputeScreenPosition( positionOffset, positionPercent );
17+
Vector2 origEdges = origScrPos + dim;
18+
19+
if( origScrPos.X < 0f ) {
20+
positionOffset.X += -origScrPos.X;
21+
} else if( origEdges.X > Main.screenWidth ) {
22+
positionOffset.X += origEdges.X - Main.screenWidth;
23+
}
24+
25+
if( origScrPos.Y < 0f ) {
26+
positionOffset.Y += -origScrPos.Y;
27+
} else if( origEdges.Y > Main.screenHeight ) {
28+
positionOffset.Y += origEdges.Y - Main.screenHeight;
29+
}
30+
}
31+
32+
33+
34+
////////////////
35+
836
public virtual bool AutoAnchors() {
937
return true;
1038
}
@@ -17,18 +45,15 @@ public virtual bool AutoAnchors() {
1745
/// </summary>
1846
/// <returns></returns>
1947
public virtual (Vector2 relative, Vector2 percent) GetIntendedPosition() {
20-
return (this.CurrentRelativePosition, this.CurrentPositionPercent);
48+
return (this.CurrentPositionOffset, this.CurrentPositionPercent);
2149
}
2250

2351
/// <summary>
2452
/// Gets intended screen position of element.
2553
/// </summary>
2654
/// <returns></returns>
2755
public virtual Vector2 GetComputedIntendedPosition() {
28-
return new Vector2(
29-
(this.CurrentPositionPercent.X * (float)Main.screenWidth) + (float)this.CurrentRelativePosition.X,
30-
(this.CurrentPositionPercent.Y * (float)Main.screenHeight) + (float)this.CurrentRelativePosition.Y
31-
);
56+
return HUDElement.ComputeScreenPosition( this.CurrentPositionOffset, this.CurrentPositionPercent );
3257
}
3358

3459
/// <summary>
@@ -37,12 +62,12 @@ public virtual Vector2 GetComputedIntendedPosition() {
3762
/// <param name="applyDisplacement">Gets the position of the element after being displaced by collisions.</param>
3863
/// <returns></returns>
3964
public virtual Vector2 GetHUDComputedPosition( bool applyDisplacement ) {
40-
Vector2 pos = this.GetComputedIntendedPosition();
65+
Vector2 scrPos = this.GetComputedIntendedPosition();
4166

4267
if( applyDisplacement && this.DisplacedOffset.HasValue ) {
43-
return pos + this.DisplacedOffset.Value;
68+
return scrPos + this.DisplacedOffset.Value;
4469
} else {
45-
return pos;
70+
return scrPos;
4671
}
4772
}
4873

@@ -105,7 +130,7 @@ public void SetIntendedPosition( Vector2 screenPos, bool conveyPercent ) {
105130
/// <param name="relPos"></param>
106131
/// <param name="percPos"></param>
107132
public void SetIntendedPosition( Vector2 relPos, Vector2 percPos ) {
108-
this.CurrentRelativePosition = relPos;
133+
this.CurrentPositionOffset = relPos;
109134
this.CurrentPositionPercent = percPos;
110135
}
111136

HUDElementsLib/HUDElement_Update.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ private void UpdateHUD() {
4747
////
4848

4949
private void UpdateHUDPosition() {
50+
this.UpdateScreenSpaceConstraints();
51+
52+
//
53+
5054
Vector2 pos = this.GetHUDComputedPosition( true );
5155
Vector2 dim = this.GetHUDComputedDimensions();
5256

@@ -58,9 +62,20 @@ private void UpdateHUDPosition() {
5862

5963

6064
////////////////
61-
65+
6266
protected virtual void PreUpdateWhileActive() { }
6367

6468
protected virtual void PostUpdateWhileActive() { }
69+
70+
71+
////////////////
72+
73+
private void UpdateScreenSpaceConstraints() {
74+
Vector2 dim = this.GetHUDComputedDimensions();
75+
76+
HUDElement.FitOffsetToScreen( ref this.OriginalPositionOffset, this.OriginalPositionPercent, dim );
77+
78+
HUDElement.FitOffsetToScreen( ref this.CurrentPositionOffset, this.CurrentPositionPercent, dim );
79+
}
6580
}
6681
}

0 commit comments

Comments
 (0)