Skip to content

Commit

Permalink
adjustments on menus, and mask2d
Browse files Browse the repository at this point in the history
  • Loading branch information
saint11 committed Jan 11, 2025
1 parent e48cdc6 commit 8804c1c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/Murder/Core/Graphics/AnimationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public readonly struct AnimationInfo

public float Start { get; init; } = 0f;
public float Duration { get; init; } = -1f;

/// <summary>
/// User Game.Now instead of Game.NowUnscaled. False by default
/// </summary>
public bool UseScaledTime { get; init; } = false;
public bool Loop { get; init; } = true;
public string Name { get; init; } = string.Empty;
Expand Down
11 changes: 11 additions & 0 deletions src/Murder/Core/Graphics/Mask2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class Mask2D : IDisposable
public readonly Vector2 Size;

private readonly RenderTarget2D _renderTarget;
private RenderTarget2D? _previousRenderTarget;

public RenderTarget2D RenderTarget => _renderTarget;
private readonly Batch2D _batch;
Expand Down Expand Up @@ -53,6 +54,11 @@ public Batch2D Begin(bool debug = false)

private void SetRenderTarget()
{
if (Game.GraphicsDevice.GetRenderTargets().Length>0)
{
_previousRenderTarget = (RenderTarget2D)Game.GraphicsDevice.GetRenderTargets()[0].RenderTarget;
}

Game.GraphicsDevice.SetRenderTarget(_renderTarget);
Game.GraphicsDevice.Clear(_color);
}
Expand All @@ -79,6 +85,11 @@ public void End(Batch2D targetBatch, Vector2 position, DrawInfo drawInfo)
drawInfo.Color,
drawInfo.Origin.ToXnaVector2(),
drawInfo.GetBlendMode());

if (_previousRenderTarget is not null)
{
Game.GraphicsDevice.SetRenderTarget(_previousRenderTarget);
}
}

/// <summary>
Expand Down
77 changes: 56 additions & 21 deletions src/Murder/Core/Input/PlayerInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,15 @@ internal bool Released(int button, bool raw = false)
throw new Exception($"Couldn't find button of type {button}");
}


[Flags]
public enum SimpleMenuFlags
{
None,
Clamp
}


public bool HorizontalMenu(ref int selectedOption, int length)
{
int move = 0;
Expand All @@ -420,26 +429,32 @@ public bool HorizontalMenu(ref MenuInfo currentInfo)
return false;
}

currentInfo.JustMoved = false;
VirtualAxis axis = GetAxis(MurderInputAxis.Ui);
return HorizontalOrVerticalMenu(ref currentInfo, input: axis.TickX ? Math.Sign(axis.Value.X) : null,
overflow: axis.TickY ? axis.IntValue.Y : 0);

// Check for vertical overflow
currentInfo.OverflowY = axis.TickY ? axis.IntValue.Y : 0;

return HorizontalOrVerticalMenu(ref currentInfo, input: axis.TickX ? Math.Sign(axis.Value.X) : null);
}

public bool VerticalMenu(ref MenuInfo currentInfo)
public bool VerticalMenu(ref MenuInfo currentInfo, SimpleMenuFlags flags = SimpleMenuFlags.None)
{
if (currentInfo.Disabled)
{
return false;
}

currentInfo.JustMoved = false;

VirtualAxis axis = GetAxis(MurderInputAxis.Ui);
return HorizontalOrVerticalMenu(ref currentInfo, axis.TickY ? Math.Sign(axis.Value.Y) : null,
axis.TickX ? axis.IntValue.X : 0);

// Check for horizontal overflow
currentInfo.OverflowX = axis.TickX ? axis.IntValue.X : 0;

return HorizontalOrVerticalMenu(ref currentInfo, axis.TickY ? Math.Sign(axis.Value.Y) : null, flags);
}

private bool HorizontalOrVerticalMenu(ref MenuInfo currentInfo, float? input, int overflow)
private bool HorizontalOrVerticalMenu(ref MenuInfo currentInfo, float? input, SimpleMenuFlags flags = SimpleMenuFlags.None)
{
bool pressed = false;
if (Pressed(MurderInputButtons.Submit))
Expand All @@ -458,8 +473,6 @@ private bool HorizontalOrVerticalMenu(ref MenuInfo currentInfo, float? input, in
currentInfo.Canceled = false;
}

currentInfo.Overflow = overflow;

if (currentInfo.Disabled || currentInfo.Options == null || currentInfo.Length == 0)
return false;

Expand All @@ -472,6 +485,20 @@ private bool HorizontalOrVerticalMenu(ref MenuInfo currentInfo, float? input, in

if (input is not null)
{
// Fist check if we are clamping the menu
if (flags.HasFlag(SimpleMenuFlags.Clamp))
{
if (input.Value < 0 && currentInfo.Selection == 0)
{
return pressed;
}

if (input.Value > 0 && currentInfo.Selection == currentInfo.Length - 1)
{
return pressed;
}
}

// Pick the next option. However, we need to take into account options that can't be selected,
// so this gets slightly trickier.
int sign = Math.Sign(input.Value);
Expand Down Expand Up @@ -516,7 +543,7 @@ private bool HorizontalOrVerticalMenu<T>(ref GenericMenuInfo<T> currentInfo, flo
}

currentInfo.Canceled = canceled;
currentInfo.Overflow = overflow;
currentInfo.OverflowX = overflow;

if (currentInfo.Disabled || currentInfo.Options == null || currentInfo.Length == 0)
return false;
Expand Down Expand Up @@ -586,7 +613,8 @@ public bool GridMenu(ref MenuInfo currentInfo, int width, int _, int size, GridM

int selectedOptionX = currentInfo.Selection % width;
int selectedOptionY = Calculator.FloorToInt(currentInfo.Selection / width);
int overflow = 0;
int overflowX = 0;
int overflowY = 0;
if (axis.PressedX)
{
selectedOptionX += Math.Sign(axis.Value.X);
Expand All @@ -595,15 +623,15 @@ public bool GridMenu(ref MenuInfo currentInfo, int width, int _, int size, GridM

if (selectedOptionX >= currentWidth) // Is on last row and it has less than width.
{
overflow = 1;
overflowX = 1;
if (gridMenuFlags.HasFlag(GridMenuFlags.ClampRight))
{
selectedOptionX = currentWidth - 1;
}
}
else if (selectedOptionX < 0)
{
overflow = -1;
overflowX = -1;
if (gridMenuFlags.HasFlag(GridMenuFlags.ClampLeft))
{
selectedOptionX = 0;
Expand All @@ -622,10 +650,12 @@ public bool GridMenu(ref MenuInfo currentInfo, int width, int _, int size, GridM

if (selectedOptionY >= currentHeight && gridMenuFlags.HasFlag(GridMenuFlags.ClampBottom))
{
overflowY = 1;
selectedOptionY = currentHeight - 1;
}
else if (selectedOptionY < 0)
else if (selectedOptionY < 0 && gridMenuFlags.HasFlag(GridMenuFlags.ClampTop))
{
overflowY = -1;
selectedOptionY = 0;
}

Expand Down Expand Up @@ -659,7 +689,7 @@ public bool GridMenu(ref MenuInfo currentInfo, int width, int _, int size, GridM
else if (isDisabled)
{
int sign = Math.Sign(axis.Value.X) < 0 ? -1 : 1;
int newOption = currentInfo.NextAvailableOption(currentInfo.Selection, sign);
int newOption = currentInfo.NextAvailableOption(currentInfo.Selection, sign); // Ignore the overflow when skipping disabled options.

if (newOption == selectedOptionIndex)
{
Expand All @@ -672,7 +702,8 @@ public bool GridMenu(ref MenuInfo currentInfo, int width, int _, int size, GridM
}

currentInfo.Canceled = canceled;
currentInfo.Overflow = overflow;
currentInfo.OverflowX = overflowX;
currentInfo.OverflowY = overflowY;
return pressed;
}

Expand All @@ -692,7 +723,8 @@ public bool GridMenu<T>(ref GenericMenuInfo<T> currentInfo, int width, int size,

int selectedOptionX = currentInfo.Selection % width;
int selectedOptionY = Calculator.FloorToInt(currentInfo.Selection / width);
int overflow = 0;
int overflowX = 0;
int overflowY = 0;
if (axis.PressedX)
{
selectedOptionX += Math.Sign(axis.Value.X);
Expand All @@ -701,13 +733,13 @@ public bool GridMenu<T>(ref GenericMenuInfo<T> currentInfo, int width, int size,

if (selectedOptionX >= currentWidth) // Is on last row and it has less than width.
{
overflow = 1;
overflowX = 1;
if (gridMenuFlags.HasFlag(GridMenuFlags.ClampRight))
selectedOptionX = currentWidth - 1;
}
else if (selectedOptionX < 0)
{
overflow = -1;
overflowX = -1;
if (gridMenuFlags.HasFlag(GridMenuFlags.ClampLeft))
selectedOptionX = 0;
}
Expand All @@ -726,9 +758,11 @@ public bool GridMenu<T>(ref GenericMenuInfo<T> currentInfo, int width, int size,
if (selectedOptionY >= currentHeight && gridMenuFlags.HasFlag(GridMenuFlags.ClampBottom))
{
selectedOptionY = currentHeight - 1;
overflowY = 1;
}
else if (selectedOptionY < 0)
else if (selectedOptionY < 0 && gridMenuFlags.HasFlag(GridMenuFlags.ClampTop))
{
overflowY = -1;
selectedOptionY = 0;
}

Expand All @@ -754,7 +788,8 @@ public bool GridMenu<T>(ref GenericMenuInfo<T> currentInfo, int width, int size,
currentInfo.Select(selectedOptionIndex, lastMoved);

currentInfo.Canceled = canceled;
currentInfo.Overflow = overflow;
currentInfo.OverflowX = overflowX;
currentInfo.OverflowY = overflowY;
return pressed;
}

Expand Down
7 changes: 5 additions & 2 deletions src/Murder/Services/Info/MenuInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Murder.Core.Sounds;
using Murder.Services;
using Murder.Utilities;
using static Murder.Core.Input.PlayerInput;

namespace Murder.Core.Input
{
Expand All @@ -17,7 +18,8 @@ public struct GenericMenuInfo<T>
public float LastPressed = 0;
public float LastMoved;

public int Overflow = 0;
public int OverflowX = 0;
public int OverflowY = 0;
public int PreviousSelection;

/// <summary>
Expand Down Expand Up @@ -96,7 +98,8 @@ public struct MenuInfo
public float LastPressed;
public bool Canceled;
public bool Disabled = false;
public int Overflow = 0;
public int OverflowX = 0;
public int OverflowY = 0;
public bool JustMoved = false;
public int Scroll = 0;

Expand Down

0 comments on commit 8804c1c

Please sign in to comment.