<commit_before>// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input;
using osu.Framework.Input.Events;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Edit.Tools;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables;
using osuTK;

namespace osu.Game.Screens.Edit.Compose.Components
{
    /// <summary>
    /// A blueprint container generally displayed as an overlay to a ruleset's playfield.
    /// </summary>
    public class ComposeBlueprintContainer : BlueprintContainer
    {
        [Resolved]
        private HitObjectComposer composer { get; set; }

        private PlacementBlueprint currentPlacement;

        private readonly Container<PlacementBlueprint> placementBlueprintContainer;

        private InputManager inputManager;

        private readonly IEnumerable<DrawableHitObject> drawableHitObjects;

        public ComposeBlueprintContainer(IEnumerable<DrawableHitObject> drawableHitObjects)
        {
            this.drawableHitObjects = drawableHitObjects;

            placementBlueprintContainer = new Container<PlacementBlueprint>
            {
                RelativeSizeAxes = Axes.Both
            };
        }

        [BackgroundDependencyLoader]
        private void load()
        {
            AddInternal(placementBlueprintContainer);
        }

        protected override void LoadComplete()
        {
            base.LoadComplete();

            inputManager = GetContainingInputManager();
        }

        #region Placement

        /// <summary>
        /// Refreshes the current placement tool.
        /// </summary>
        private void refreshTool()
        {
            placementBlueprintContainer.Clear();

            removePlacement();

            var blueprint = CurrentTool?.CreatePlacementBlueprint();

            if (blueprint != null)
            {
                placementBlueprintContainer.Child = currentPlacement = blueprint;

                // Fixes a 1-frame position discrepancy due to the first mouse move event happening in the next frame
                updatePlacementPosition(inputManager.CurrentState.Mouse.Position);
            }
        }

        private void updatePlacementPosition(Vector2 screenSpacePosition)
        {
            Vector2 snappedGridPosition = composer.GetSnappedPosition(ToLocalSpace(screenSpacePosition), 0).position;
            Vector2 snappedScreenSpacePosition = ToScreenSpace(snappedGridPosition);

            currentPlacement.UpdatePosition(snappedScreenSpacePosition);
        }

        #endregion

        protected override bool OnMouseMove(MouseMoveEvent e)
        {
            if (currentPlacement != null)
            {
                updatePlacementPosition(e.ScreenSpaceMousePosition);
                return true;
            }

            return base.OnMouseMove(e);
        }

        protected override void Update()
        {
            base.Update();

            if (currentPlacement?.PlacementActive == false && !composer.CursorInPlacementArea)
                removePlacement();
        }

        protected sealed override SelectionBlueprint CreateBlueprintFor(HitObject hitObject)
        {
            var drawable = drawableHitObjects.FirstOrDefault(d => d.HitObject == hitObject);

            if (drawable == null)
                return null;

            return CreateBlueprintFor(drawable);
        }

        public virtual OverlaySelectionBlueprint CreateBlueprintFor(DrawableHitObject hitObject) => null;

        protected override void AddBlueprintFor(HitObject hitObject)
        {
            refreshTool();
            base.AddBlueprintFor(hitObject);
        }

        private void removePlacement()
        {
            if (currentPlacement == null) return;

            currentPlacement.EndPlacement(false);
            currentPlacement = null;
        }

        private HitObjectCompositionTool currentTool;

        /// <summary>
        /// The current placement tool.
        /// </summary>
        public HitObjectCompositionTool CurrentTool
        {
            get => currentTool;

            set
            {
                if (currentTool == value)
                    return;

                currentTool = value;

                refreshTool();
            }
        }
    }
}<commit_msg>Standardise placement blueprint creation and destruction<commit_after>// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input;
using osu.Framework.Input.Events;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Edit.Tools;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables;
using osuTK;

namespace osu.Game.Screens.Edit.Compose.Components
{
    /// <summary>
    /// A blueprint container generally displayed as an overlay to a ruleset's playfield.
    /// </summary>
    public class ComposeBlueprintContainer : BlueprintContainer
    {
        [Resolved]
        private HitObjectComposer composer { get; set; }

        private PlacementBlueprint currentPlacement;

        private readonly Container<PlacementBlueprint> placementBlueprintContainer;

        private InputManager inputManager;

        private readonly IEnumerable<DrawableHitObject> drawableHitObjects;

        public ComposeBlueprintContainer(IEnumerable<DrawableHitObject> drawableHitObjects)
        {
            this.drawableHitObjects = drawableHitObjects;

            placementBlueprintContainer = new Container<PlacementBlueprint>
            {
                RelativeSizeAxes = Axes.Both
            };
        }

        [BackgroundDependencyLoader]
        private void load()
        {
            AddInternal(placementBlueprintContainer);
        }

        protected override void LoadComplete()
        {
            base.LoadComplete();

            inputManager = GetContainingInputManager();
        }

        #region Placement

        /// <summary>
        /// Refreshes the current placement tool.
        /// </summary>
        private void refreshTool()
        {
            removePlacement();
            createPlacement();
        }

        private void updatePlacementPosition(Vector2 screenSpacePosition)
        {
            Vector2 snappedGridPosition = composer.GetSnappedPosition(ToLocalSpace(screenSpacePosition), 0).position;
            Vector2 snappedScreenSpacePosition = ToScreenSpace(snappedGridPosition);

            currentPlacement.UpdatePosition(snappedScreenSpacePosition);
        }

        #endregion

        protected override bool OnMouseMove(MouseMoveEvent e)
        {
            if (currentPlacement != null)
            {
                updatePlacementPosition(e.ScreenSpaceMousePosition);
                return true;
            }

            return base.OnMouseMove(e);
        }

        protected override void Update()
        {
            base.Update();

            if (composer.CursorInPlacementArea)
                createPlacement();
            else if (currentPlacement?.PlacementActive == false)
                removePlacement();
        }

        protected sealed override SelectionBlueprint CreateBlueprintFor(HitObject hitObject)
        {
            var drawable = drawableHitObjects.FirstOrDefault(d => d.HitObject == hitObject);

            if (drawable == null)
                return null;

            return CreateBlueprintFor(drawable);
        }

        public virtual OverlaySelectionBlueprint CreateBlueprintFor(DrawableHitObject hitObject) => null;

        protected override void AddBlueprintFor(HitObject hitObject)
        {
            refreshTool();
            base.AddBlueprintFor(hitObject);
        }

        private void createPlacement()
        {
            if (currentPlacement != null) return;

            var blueprint = CurrentTool?.CreatePlacementBlueprint();

            if (blueprint != null)
            {
                placementBlueprintContainer.Child = currentPlacement = blueprint;

                // Fixes a 1-frame position discrepancy due to the first mouse move event happening in the next frame
                updatePlacementPosition(inputManager.CurrentState.Mouse.Position);
            }
        }

        private void removePlacement()
        {
            if (currentPlacement == null) return;

            currentPlacement.EndPlacement(false);
            currentPlacement.Expire();
            currentPlacement = null;
        }

        private HitObjectCompositionTool currentTool;

        /// <summary>
        /// The current placement tool.
        /// </summary>
        public HitObjectCompositionTool CurrentTool
        {
            get => currentTool;

            set
            {
                if (currentTool == value)
                    return;

                currentTool = value;

                refreshTool();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.07326478149100257, 'fraction_numerical': 0.0010711225364181663, 'mean_word_length': 2.052304674730304, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5662175', 'n_tokens_mistral': 2289, 'n_tokens_neox': 2174, 'n_words': 516}