Skip to content

Commit

Permalink
摇杆记录溢出值以便在频繁靠近边时找到原位
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanheiii committed Apr 14, 2024
1 parent ec21cb7 commit 7b98096
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions Mageki/Mageki/Drawables/Lever.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,22 @@ public float Value
get => GetValue(default(float));
set
{
if (value < -1) value = -1;
else if (value > 1) value = 1;
if (value < -1)
{
valueOverflow += value + 1;
value = -1;
}
else if (value > 1)
{
valueOverflow += value - 1;
value = 1;
}
SetValueWithNotify(value);
}
}

public float valueOverflow = 0;

private SKPath capTopPath = new SKPath();
private SKPath capSidePath = new SKPath();
private SKPath holePath = new SKPath();
Expand Down Expand Up @@ -344,8 +354,26 @@ public override bool HandleTouchMoved(long id, SKPoint point)
sum = max;
}

var diff = sum / (boundingBox.Width / 2 - Padding.X);
Value += diff * Settings.LeverSensitivity;
var diffPx = sum / (boundingBox.Width / 2 - Padding.X);
var diff = diffPx * Settings.LeverSensitivity;
// 如果溢出值有剩余优先抵消溢出值
if(valueOverflow * diff < 0)
{
if(Math.Abs(valueOverflow) < Math.Abs(diff))
{
diff += valueOverflow;
valueOverflow = 0;
}
else
{
valueOverflow += diff;
diff = 0;
}
}
if(diff != 0)
{
Value += diff;
}

moveCache.Clear();
}
Expand All @@ -357,5 +385,11 @@ public override bool HandleTouchMoved(long id, SKPoint point)

return base.HandleTouchMoved(id, point);
}

public override void HandleTouchReleased(long id)
{
base.HandleTouchReleased(id);
if (touchPoints.Count == 0) valueOverflow = 0;
}
}
}

0 comments on commit 7b98096

Please sign in to comment.