<commit_before>﻿// Copyright (c) 2007-2018 ppy Pty Ltd <<EMAIL>>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu-framework/master/LICENCE

using System;
using System.Linq;
using osu.Framework.Configuration;
using osu.Framework.Logging;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.ES30;
using OpenTK.Platform;
using OpenTK.Input;
using System.ComponentModel;
using System.Drawing;

namespace osu.Framework.Platform
{
    public abstract class GameWindow : IGameWindow
    {
        /// <summary>
        /// The <see cref="IGraphicsContext"/> associated with this <see cref="GameWindow"/>.
        /// </summary>
        internal abstract IGraphicsContext Context { get; }

        /// <summary>
        /// Return value decides whether we should intercept and cancel this exit (if possible).
        /// </summary>
        public event Func<bool> ExitRequested;

        /// <summary>
        /// Invoked when the <see cref="GameWindow"/> has closed.
        /// </summary>
        public event Action Exited;

        /// <summary>
        /// Invoked when any key has been pressed.
        /// </summary>
        public event EventHandler<KeyboardKeyEventArgs> KeyDown;

        internal Version GLVersion;
        internal Version GLSLVersion;

        protected readonly IGameWindow Implementation;

        /// <summary>
        /// Whether the OS cursor is currently contained within the game window.
        /// </summary>
        public bool CursorInWindow { get; private set; }

        protected GameWindow(IGameWindow implementation)
        {
            Implementation = implementation;
            Implementation.KeyDown += OnKeyDown;

            Closing += (sender, e) => e.Cancel = ExitRequested?.Invoke() ?? false;
            Closed += (sender, e) => Exited?.Invoke();

            MouseEnter += (sender, args) => CursorInWindow = true;
            MouseLeave += (sender, args) => CursorInWindow = false;

            MakeCurrent();

            string version = GL.GetString(StringName.Version);
            string versionNumberSubstring = getVersionNumberSubstring(version);
            GLVersion = new Version(versionNumberSubstring);
            version = GL.GetString(StringName.ShadingLanguageVersion);
            if (!string.IsNullOrEmpty(version))
            {
                try
                {
                    GLSLVersion = new Version(versionNumberSubstring);
                }
                catch (Exception e)
                {
                    Logger.Error(e, $@"couldn't set GLSL version using string '{version}'");
                }
            }

            if (GLSLVersion == null)
                GLSLVersion = new Version();

            //Set up OpenGL related characteristics
            GL.Disable(EnableCap.DepthTest);
            GL.Disable(EnableCap.StencilTest);
            GL.Enable(EnableCap.Blend);
            GL.Enable(EnableCap.ScissorTest);

            Logger.Log($@"GL Initialized
                        GL Version:                 {GL.GetString(StringName.Version)}
                        GL Renderer:                {GL.GetString(StringName.Renderer)}
                        GL Shader Language version: {GL.GetString(StringName.ShadingLanguageVersion)}
                        GL Vendor:                  {GL.GetString(StringName.Vendor)}
                        GL Extensions:              {GL.GetString(StringName.Extensions)}", LoggingTarget.Runtime, LogLevel.Important);

            Context.MakeCurrent(null);
        }

        protected GameWindow(int width, int height)
            : this(new OpenTK.GameWindow(width, height, new GraphicsMode(GraphicsMode.Default.ColorFormat, GraphicsMode.Default.Depth, GraphicsMode.Default.Stencil, GraphicsMode.Default.Samples, GraphicsMode.Default.AccumulatorFormat, 3)))
        {
        }

        private CursorState cursorState = CursorState.Default;

        /// <summary>
        /// Controls the state of the OS cursor.
        /// </summary>
        public CursorState CursorState
        {
            get { return cursorState; }
            set
            {
                cursorState = value;

                Implementation.Cursor = (cursorState & CursorState.Hidden) > 0 ? MouseCursor.Empty : MouseCursor.Default;

                try
                {
                    Implementation.CursorGrabbed = (cursorState & CursorState.Confined) > 0;
                }
                catch
                {
                    // may not be supported by platform.
                }
            }
        }

        /// <summary>
        /// We do not support directly using <see cref="Cursor"/>.<commit_msg>add xml docs and jetbrains annotations<commit_after>﻿// Copyright (c) 2007-2018 ppy <EMAIL> Ltd <<EMAIL>>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu-framework/master/LICENCE

using System;
using System.Linq;
using osu.Framework.Configuration;
using osu.Framework.Logging;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.ES30;
using OpenTK.Platform;
using OpenTK.Input;
using System.ComponentModel;
using System.Drawing;
using JetBrains.Annotations;

namespace osu.Framework.Platform
{
    public abstract class GameWindow : IGameWindow
    {
        /// <summary>
        /// The <see cref="IGraphicsContext"/> associated with this <see cref="GameWindow"/>.
        /// </summary>
        [NotNull]
        internal abstract IGraphicsContext Context { get; }

        /// <summary>
        /// Return value decides whether we should intercept and cancel this exit (if possible).
        /// </summary>
        [CanBeNull]
        public event Func<bool> ExitRequested;

        /// <summary>
        /// Invoked when the <see cref="GameWindow"/> has closed.
        /// </summary>
        [CanBeNull]
        public event Action Exited;

        /// <summary>
        /// Invoked when any key has been pressed.
        /// </summary>
        [CanBeNull]
        public event EventHandler<KeyboardKeyEventArgs> KeyDown;

        internal Version GLVersion;
        internal Version GLSLVersion;

        protected readonly IGameWindow Implementation;

        /// <summary>
        /// Whether the OS cursor is currently contained within the game window.
        /// </summary>
        public bool CursorInWindow { get; private set; }

        /// <summary>
        /// Creates a <see cref="GameWindow"/> with a given <see cref="IGameWindow"/> implementation.
        /// </summary>
        protected GameWindow([NotNull] IGameWindow implementation)
        {
            Implementation = implementation;
            Implementation.KeyDown += OnKeyDown;

            Closing += (sender, e) => e.Cancel = ExitRequested?.Invoke() ?? false;
            Closed += (sender, e) => Exited?.Invoke();

            MouseEnter += (sender, args) => CursorInWindow = true;
            MouseLeave += (sender, args) => CursorInWindow = false;

            MakeCurrent();

            string version = GL.GetString(StringName.Version);
            string versionNumberSubstring = getVersionNumberSubstring(version);
            GLVersion = new Version(versionNumberSubstring);
            version = GL.GetString(StringName.ShadingLanguageVersion);
            if (!string.IsNullOrEmpty(version))
            {
                try
                {
                    GLSLVersion = new Version(versionNumberSubstring);
                }
                catch (Exception e)
                {
                    Logger.Error(e, $@"couldn't set GLSL version using string '{version}'");
                }
            }

            if (GLSLVersion == null)
                GLSLVersion = new Version();

            //Set up OpenGL related characteristics
            GL.Disable(EnableCap.DepthTest);
            GL.Disable(EnableCap.StencilTest);
            GL.Enable(EnableCap.Blend);
            GL.Enable(EnableCap.ScissorTest);

            Logger.Log($@"GL Initialized
                        GL Version:                 {GL.GetString(StringName.Version)}
                        GL Renderer:                {GL.GetString(StringName.Renderer)}
                        GL Shader Language version: {GL.GetString(StringName.ShadingLanguageVersion)}
                        GL Vendor:                  {GL.GetString(StringName.Vendor)}
                        GL Extensions:              {GL.GetString(StringName.Extensions)}", LoggingTarget.Runtime, LogLevel.Important);

            Context.MakeCurrent(null);
        }

        /// <summary>
        /// Creates a <see cref="GameWindow"/> with given dimensions.
        /// <para>Note that this will use the default <see cref="OpenTK.GameWindow"/> implementation, which is not compatible with every platform.</para>
        /// </summary>
        protected GameWindow(int width, int height)
            : this(new OpenTK.GameWindow(width, height, new GraphicsMode(GraphicsMode.Default.ColorFormat, GraphicsMode.Default.Depth, GraphicsMode.Default.Stencil, GraphicsMode.Default.Samples, GraphicsMode.Default.AccumulatorFormat, 3)))
        {
        }

        private CursorState cursorState = CursorState.Default;

        /// <summary>
        /// Controls the state of the OS cursor.
        /// </summary>
        public CursorState CursorState
        {
            get => cursorState;
            set
            {
                cursorState = value;

                Implementation.Cursor = (cursorState & CursorState.Hidden) > 0 ? MouseCursor.Empty : MouseCursor.Default;

                try
                {
                    Implementation.CursorGrabbed = (cursorState & CursorState.Confined) > 0;
                }
                catch
                {
                    // may not be supported by platform.
                }
            }
        }

        /// <summary>
        /// We do not support directly using <see cref="Cursor"/>.
{'fraction_non_alphanumeric': 0.10020140986908359, 'fraction_numerical': 0.0026183282980866062, 'mean_word_length': 1.7888233642235327, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 69, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, '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': '4106828', 'n_tokens_mistral': 2517, 'n_tokens_neox': 2378, 'n_words': 626}