1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 15:32:55 +08:00
osu-lazer/osu.Game.Rulesets.Catch/UI/CatchTouchInputMapper.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

278 lines
10 KiB
C#
Raw Normal View History

2022-08-13 18:55:31 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Linq;
2022-08-13 18:55:31 +08:00
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Game.Graphics;
using osuTK;
using osuTK.Input;
2022-08-13 18:55:31 +08:00
namespace osu.Game.Rulesets.Catch.UI
{
2022-09-08 18:41:04 +08:00
public class CatchTouchInputMapper : VisibilityContainer
2022-08-13 18:55:31 +08:00
{
public override bool PropagatePositionalInputSubTree => true;
public override bool PropagateNonPositionalInputSubTree => true;
private readonly Dictionary<object, TouchCatchAction> trackedActionSources = new Dictionary<object, TouchCatchAction>();
2022-08-13 18:55:31 +08:00
private KeyBindingContainer<CatchAction> keyBindingContainer = null!;
private Container mainContent = null!;
2022-09-09 14:11:26 +08:00
private InputArea leftBox = null!;
private InputArea rightBox = null!;
private InputArea leftDashBox = null!;
private InputArea rightDashBox = null!;
2022-08-13 18:55:31 +08:00
[BackgroundDependencyLoader]
private void load(CatchInputManager catchInputManager, OsuColour colours)
{
2022-09-09 14:58:43 +08:00
const float width = 0.15f;
2022-08-13 18:55:31 +08:00
keyBindingContainer = catchInputManager.KeyBindingContainer;
RelativeSizeAxes = Axes.Both;
Children = new Drawable[]
{
mainContent = new Container
{
RelativeSizeAxes = Axes.Both,
Alpha = 0,
2022-08-13 18:55:31 +08:00
Children = new Drawable[]
{
new Container
2022-08-13 18:55:31 +08:00
{
RelativeSizeAxes = Axes.Both,
2022-09-09 14:58:43 +08:00
Width = width,
Children = new Drawable[]
{
2022-09-09 15:03:36 +08:00
leftDashBox = new InputArea(TouchCatchAction.DashLeft, trackedActionSources)
{
RelativeSizeAxes = Axes.Both,
Width = 0.5f,
},
2022-09-09 15:03:36 +08:00
leftBox = new InputArea(TouchCatchAction.MoveLeft, trackedActionSources)
{
RelativeSizeAxes = Axes.Both,
Width = 0.5f,
Colour = colours.Gray9,
2022-09-09 15:03:36 +08:00
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
},
}
2022-08-13 18:55:31 +08:00
},
new Container
2022-08-13 18:55:31 +08:00
{
RelativeSizeAxes = Axes.Both,
2022-09-09 14:58:43 +08:00
Width = width,
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Children = new Drawable[]
{
2022-09-09 15:03:36 +08:00
rightBox = new InputArea(TouchCatchAction.MoveRight, trackedActionSources)
{
RelativeSizeAxes = Axes.Both,
Width = 0.5f,
Colour = colours.Gray9,
},
2022-09-09 15:03:36 +08:00
rightDashBox = new InputArea(TouchCatchAction.DashRight, trackedActionSources)
{
RelativeSizeAxes = Axes.Both,
Width = 0.5f,
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
},
}
2022-08-13 18:55:31 +08:00
},
},
2022-08-13 18:55:31 +08:00
},
};
}
protected override bool OnKeyDown(KeyDownEvent e)
{
// Hide whenever the keyboard is used.
2022-09-03 02:31:58 +08:00
Hide();
2022-08-13 18:55:31 +08:00
return false;
}
protected override bool OnMouseDown(MouseDownEvent e)
{
2022-09-09 14:58:43 +08:00
return updateAction(e.Button, getTouchCatchActionFromInput(e.ScreenSpaceMousePosition));
2022-08-13 18:55:31 +08:00
}
2022-09-09 14:28:40 +08:00
protected override bool OnTouchDown(TouchDownEvent e)
2022-08-13 18:55:31 +08:00
{
2022-09-09 14:58:43 +08:00
return updateAction(e.Touch.Source, getTouchCatchActionFromInput(e.ScreenSpaceTouch.Position));
2022-08-13 18:55:31 +08:00
}
protected override bool OnMouseMove(MouseMoveEvent e)
2022-09-03 02:31:58 +08:00
{
2022-09-09 14:58:43 +08:00
Show();
TouchCatchAction? action = getTouchCatchActionFromInput(e.ScreenSpaceMousePosition);
2022-09-09 14:28:40 +08:00
// multiple mouse buttons may be pressed and handling the same action.
foreach (MouseButton button in e.PressedButtons)
2022-09-09 14:58:43 +08:00
updateAction(button, action);
return false;
2022-09-03 02:31:58 +08:00
}
2022-09-08 18:44:29 +08:00
2022-09-03 02:31:58 +08:00
protected override void OnTouchMove(TouchMoveEvent e)
2022-08-13 18:55:31 +08:00
{
2022-09-09 14:58:43 +08:00
updateAction(e.Touch.Source, getTouchCatchActionFromInput(e.ScreenSpaceTouch.Position));
2022-08-13 18:55:31 +08:00
base.OnTouchMove(e);
2022-09-03 02:31:58 +08:00
}
2022-08-13 18:55:31 +08:00
2022-09-09 14:28:40 +08:00
protected override void OnMouseUp(MouseUpEvent e)
2022-08-13 18:55:31 +08:00
{
2022-09-09 14:58:43 +08:00
updateAction(e.Button, null);
2022-09-09 14:28:40 +08:00
base.OnMouseUp(e);
2022-08-13 18:55:31 +08:00
}
protected override void OnTouchUp(TouchUpEvent e)
{
2022-09-09 14:58:43 +08:00
updateAction(e.Touch.Source, null);
2022-08-13 18:55:31 +08:00
base.OnTouchUp(e);
}
2022-09-09 14:58:43 +08:00
private bool updateAction(object source, TouchCatchAction? newAction)
2022-09-09 14:28:40 +08:00
{
2022-09-09 14:58:43 +08:00
TouchCatchAction? actionBefore = null;
2022-09-09 14:58:43 +08:00
if (trackedActionSources.TryGetValue(source, out TouchCatchAction found))
actionBefore = found;
2022-09-09 14:58:43 +08:00
if (actionBefore != newAction)
{
if (newAction != null)
trackedActionSources[source] = newAction.Value;
else
trackedActionSources.Remove(source);
2022-08-13 18:55:31 +08:00
2022-09-09 14:28:40 +08:00
updatePressedActions();
2022-09-09 14:58:43 +08:00
}
return newAction != null;
2022-08-13 18:55:31 +08:00
}
2022-09-09 14:28:40 +08:00
private void updatePressedActions()
2022-09-09 14:11:26 +08:00
{
2022-09-09 14:58:43 +08:00
Show();
2022-09-09 14:11:26 +08:00
if (trackedActionSources.ContainsValue(TouchCatchAction.DashLeft) || trackedActionSources.ContainsValue(TouchCatchAction.MoveLeft))
keyBindingContainer.TriggerPressed(CatchAction.MoveLeft);
else
keyBindingContainer.TriggerReleased(CatchAction.MoveLeft);
if (trackedActionSources.ContainsValue(TouchCatchAction.DashRight) || trackedActionSources.ContainsValue(TouchCatchAction.MoveRight))
keyBindingContainer.TriggerPressed(CatchAction.MoveRight);
else
keyBindingContainer.TriggerReleased(CatchAction.MoveRight);
2022-09-09 14:58:43 +08:00
if (trackedActionSources.ContainsValue(TouchCatchAction.DashLeft) || trackedActionSources.ContainsValue(TouchCatchAction.DashRight))
2022-09-09 14:11:26 +08:00
keyBindingContainer.TriggerPressed(CatchAction.Dash);
else
keyBindingContainer.TriggerReleased(CatchAction.Dash);
}
2022-09-09 14:58:43 +08:00
private TouchCatchAction? getTouchCatchActionFromInput(Vector2 screenSpaceInputPosition)
2022-08-13 18:55:31 +08:00
{
2022-09-09 14:28:40 +08:00
if (leftDashBox.Contains(screenSpaceInputPosition))
2022-08-13 18:55:31 +08:00
return TouchCatchAction.DashLeft;
2022-09-09 14:28:40 +08:00
if (rightDashBox.Contains(screenSpaceInputPosition))
2022-08-13 18:55:31 +08:00
return TouchCatchAction.DashRight;
2022-09-09 14:28:40 +08:00
if (leftBox.Contains(screenSpaceInputPosition))
2022-08-13 18:55:31 +08:00
return TouchCatchAction.MoveLeft;
2022-09-09 14:28:40 +08:00
if (rightBox.Contains(screenSpaceInputPosition))
2022-08-13 18:55:31 +08:00
return TouchCatchAction.MoveRight;
2022-09-08 18:44:29 +08:00
2022-09-09 14:58:43 +08:00
return null;
2022-08-13 18:55:31 +08:00
}
2022-09-09 14:58:43 +08:00
protected override void PopIn() => mainContent.FadeIn(300, Easing.OutQuint);
2022-08-13 18:55:31 +08:00
2022-09-09 14:58:43 +08:00
protected override void PopOut() => mainContent.FadeOut(300, Easing.OutQuint);
2022-08-13 18:55:31 +08:00
2022-09-09 14:11:26 +08:00
private class InputArea : CompositeDrawable, IKeyBindingHandler<CatchAction>
2022-08-13 18:55:31 +08:00
{
private readonly TouchCatchAction handledAction;
2022-09-09 14:58:43 +08:00
private readonly Box highlightOverlay;
2022-08-13 18:55:31 +08:00
private readonly IEnumerable<KeyValuePair<object, TouchCatchAction>> trackedActions;
2022-08-13 18:55:31 +08:00
private bool isHighlighted;
2022-08-13 18:55:31 +08:00
2022-09-09 15:03:36 +08:00
public InputArea(TouchCatchAction handledAction, IEnumerable<KeyValuePair<object, TouchCatchAction>> trackedActions)
2022-08-13 18:55:31 +08:00
{
this.handledAction = handledAction;
this.trackedActions = trackedActions;
InternalChildren = new Drawable[]
{
new Container
{
RelativeSizeAxes = Axes.Both,
Masking = true,
CornerRadius = 10,
2022-08-13 18:55:31 +08:00
Children = new Drawable[]
{
2022-09-03 02:31:58 +08:00
new Box
2022-08-13 18:55:31 +08:00
{
2022-09-09 14:58:43 +08:00
RelativeSizeAxes = Axes.Both,
Alpha = 0.15f,
2022-08-13 18:55:31 +08:00
},
2022-09-09 14:58:43 +08:00
highlightOverlay = new Box
2022-08-13 18:55:31 +08:00
{
2022-09-09 14:58:43 +08:00
RelativeSizeAxes = Axes.Both,
2022-09-03 02:31:58 +08:00
Alpha = 0,
Blending = BlendingParameters.Additive,
2022-08-13 18:55:31 +08:00
}
}
}
};
}
public bool OnPressed(KeyBindingPressEvent<CatchAction> _)
{
updateHighlight();
2022-08-13 18:55:31 +08:00
return false;
}
public void OnReleased(KeyBindingReleaseEvent<CatchAction> _)
{
updateHighlight();
}
private void updateHighlight()
{
bool isHandling = trackedActions.Any(a => a.Value == handledAction);
if (isHandling == isHighlighted)
return;
isHighlighted = isHandling;
2022-09-09 14:58:43 +08:00
highlightOverlay.FadeTo(isHighlighted ? 0.1f : 0, isHighlighted ? 80 : 400, Easing.OutQuint);
2022-08-13 18:55:31 +08:00
}
}
2022-09-08 18:44:29 +08:00
public enum TouchCatchAction
{
2022-09-09 14:58:43 +08:00
MoveLeft,
MoveRight,
DashLeft,
DashRight,
2022-09-08 18:44:29 +08:00
}
2022-08-13 18:55:31 +08:00
}
}