<commit_before>﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using OpenToolkit.Mathematics;
using OpenToolkit.Windowing.Common;
using OpenToolkit.Windowing.Common.Input;

namespace Bearded.Utilities.Input
{
    public sealed partial class InputManager
    {
        private readonly INativeWindow nativeWindow;
        private readonly KeyboardEvents keyboardEvents;
        private readonly MouseEvents mouseEvents;

        private readonly AsyncAtomicUpdating<KeyboardState> keyboardState = new AsyncAtomicUpdating<KeyboardState>();
        private readonly AsyncAtomicUpdating<MouseState> mouseState = new AsyncAtomicUpdating<MouseState>();

        public ReadOnlyCollection<GamePadStateManager> GamePads { get; }

        public InputManager(INativeWindow nativeWindow)
        {
            this.nativeWindow = nativeWindow;
            keyboardEvents = new KeyboardEvents(nativeWindow);
            mouseEvents = new MouseEvents(nativeWindow);

            GamePads = Enumerable.Empty<GamePadStateManager>().ToList().AsReadOnly();
            // GamePads = Enumerable.Range(0, int.MaxValue - 1)
            //     .TakeWhile(i => GamePad.GetState(i).IsConnected)
            //     .Select(GamePadStateManager.ForId)
            //     .ToList().AsReadOnly();
        }

        public void ProcessEventsAsync()
        {
            keyboardState.SetLastKnownState(nativeWindow.KeyboardState);
            mouseState.SetLastKnownState(nativeWindow.MouseState);

            foreach (var gamePad in GamePads)
            {
                gamePad.ProcessEventsAsync();
            }
        }

        public void Update(bool windowIsActive)
        {
            if (windowIsActive)
            {
                keyboardEvents.Update();
                mouseEvents.Update();
                keyboardState.Update();
                mouseState.Update();
                MousePosition = nativeWindow.PointToClient(
                    new Vector2i((int) mouseState.Current.X, (int) mouseState.Current.Y)).ToVector2();
            }
            else
            {
                keyboardState.UpdateToDefault();
            }

            foreach (var gamePad in GamePads)
            {
                gamePad.Update(windowIsActive);
            }
        }

        public Vector2 MousePosition { get; private set; }
        public int DeltaScroll => Mathf.RoundToInt(DeltaScrollF);
        public float DeltaScrollF => mouseEvents.DeltaScrollF;

        public bool MouseMoved => mouseState.Current.X != mouseState.Previous.X
                                  || mouseState.Current.Y != mouseState.Previous.Y;

        public bool LeftMousePressed => IsMouseButtonPressed(MouseButton.Left);
        public bool LeftMouseHit => IsMouseButtonHit(MouseButton.Left);
        public bool LeftMouseReleased => IsMouseButtonReleased(MouseButton.Left);

        public bool RightMousePressed => IsMouseButtonPressed(MouseButton.Right);
        public bool RightMouseHit => IsMouseButtonHit(MouseButton.Right);
        public bool RightMouseReleased => IsMouseButtonReleased(MouseButton.Right);<commit_msg>🐛 Don't transform the mouse position twice (#188)<commit_after>﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using OpenToolkit.Mathematics;
using OpenToolkit.Windowing.Common;
using OpenToolkit.Windowing.Common.Input;

namespace Bearded.Utilities.Input
{
    public sealed partial class InputManager
    {
        private readonly INativeWindow nativeWindow;
        private readonly KeyboardEvents keyboardEvents;
        private readonly MouseEvents mouseEvents;

        private readonly AsyncAtomicUpdating<KeyboardState> keyboardState = new AsyncAtomicUpdating<KeyboardState>();
        private readonly AsyncAtomicUpdating<MouseState> mouseState = new AsyncAtomicUpdating<MouseState>();

        public ReadOnlyCollection<GamePadStateManager> GamePads { get; }

        public InputManager(INativeWindow nativeWindow)
        {
            this.nativeWindow = nativeWindow;
            keyboardEvents = new KeyboardEvents(nativeWindow);
            mouseEvents = new MouseEvents(nativeWindow);

            GamePads = Enumerable.Empty<GamePadStateManager>().ToList().AsReadOnly();
            // GamePads = Enumerable.Range(0, int.MaxValue - 1)
            //     .TakeWhile(i => GamePad.GetState(i).IsConnected)
            //     .Select(GamePadStateManager.ForId)
            //     .ToList().AsReadOnly();
        }

        public void ProcessEventsAsync()
        {
            keyboardState.SetLastKnownState(nativeWindow.KeyboardState);
            mouseState.SetLastKnownState(nativeWindow.MouseState);

            foreach (var gamePad in GamePads)
            {
                gamePad.ProcessEventsAsync();
            }
        }

        public void Update(bool windowIsActive)
        {
            if (windowIsActive)
            {
                keyboardEvents.Update();
                mouseEvents.Update();
                keyboardState.Update();
                mouseState.Update();
                MousePosition = mouseState.Current.Position;
            }
            else
            {
                keyboardState.UpdateToDefault();
            }

            foreach (var gamePad in GamePads)
            {
                gamePad.Update(windowIsActive);
            }
        }

        public Vector2 MousePosition { get; private set; }
        public int DeltaScroll => Mathf.RoundToInt(DeltaScrollF);
        public float DeltaScrollF => mouseEvents.DeltaScrollF;

        public bool MouseMoved => mouseState.Current.X != mouseState.Previous.X
                                  || mouseState.Current.Y != mouseState.Previous.Y;

        public bool LeftMousePressed => IsMouseButtonPressed(MouseButton.Left);
        public bool LeftMouseHit => IsMouseButtonHit(MouseButton.Left);
        public bool LeftMouseReleased => IsMouseButtonReleased(MouseButton.Left);

        public bool RightMousePressed => IsMouseButtonPressed(MouseButton.Right);
        public bool RightMouseHit => IsMouseButtonHit(MouseButton.Right);
        public bool RightMouseReleased => IsMouseButtonReleased(MouseButton.Right);
{'fraction_non_alphanumeric': 0.0810897435897436, 'fraction_numerical': 0.0017628205128205128, 'mean_word_length': 2.353573347662547, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5549367', 'n_tokens_mistral': 1551, 'n_tokens_neox': 1515, 'n_words': 299}