// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. #nullable disable using System.Collections.Generic; using System.ComponentModel; using System.Linq; using osu.Framework.Input; using osu.Framework.Input.Bindings; using osu.Framework.Input.Events; using osu.Framework.Input.StateChanges.Events; using osu.Game.Input.Bindings; using osu.Game.Rulesets.UI; namespace osu.Game.Rulesets.Osu { public class OsuInputManager : RulesetInputManager { public IEnumerable PressedActions => KeyBindingContainer.PressedActions; /// /// Whether gameplay input buttons should be allowed. /// Defaults to true, generally used for mods like Relax which turn off main inputs. /// /// /// Of note, auxiliary inputs like the "smoke" key are left usable. /// public bool AllowGameplayInputs { set => ((OsuKeyBindingContainer)KeyBindingContainer).AllowGameplayInputs = value; } /// /// Whether the user's cursor movement events should be accepted. /// Can be used to block only movement while still accepting button input. /// public bool AllowUserCursorMovement { get; set; } = true; protected override KeyBindingContainer CreateKeyBindingContainer(RulesetInfo ruleset, int variant, SimultaneousBindingMode unique) => new OsuKeyBindingContainer(ruleset, variant, unique); public OsuInputManager(RulesetInfo ruleset) : base(ruleset, 0, SimultaneousBindingMode.Unique) { } protected override bool Handle(UIEvent e) { if ((e is MouseMoveEvent || e is TouchMoveEvent) && !AllowUserCursorMovement) return false; return base.Handle(e); } protected override bool HandleMouseTouchStateChange(TouchStateChangeEvent e) { if (!AllowUserCursorMovement) { // Still allow for forwarding of the "touch" part, but replace the positional data with that of the mouse. // Primarily relied upon by the "autopilot" osu! mod. var touch = new Touch(e.Touch.Source, CurrentState.Mouse.Position); e = new TouchStateChangeEvent(e.State, e.Input, touch, e.IsActive, null); } return base.HandleMouseTouchStateChange(e); } private class OsuKeyBindingContainer : RulesetKeyBindingContainer { private bool allowGameplayInputs = true; /// /// Whether gameplay input buttons should be allowed. /// Defaults to true, generally used for mods like Relax which turn off main inputs. /// /// /// Of note, auxiliary inputs like the "smoke" key are left usable. /// public bool AllowGameplayInputs { get => allowGameplayInputs; set { allowGameplayInputs = value; ReloadMappings(); } } public OsuKeyBindingContainer(RulesetInfo ruleset, int variant, SimultaneousBindingMode unique) : base(ruleset, variant, unique) { } protected override void ReloadMappings(IQueryable realmKeyBindings) { base.ReloadMappings(realmKeyBindings); if (!AllowGameplayInputs) KeyBindings = KeyBindings.Where(b => b.GetAction() == OsuAction.Smoke).ToList(); } } } public enum OsuAction { [Description("Left button")] LeftButton, [Description("Right button")] RightButton, [Description("Smoke")] Smoke, } }