<commit_before>﻿/**
 * Copyright 2011-2013 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGdx.Graphics.G2D;
using MonoGdx.Scene2D.Utils;
using MonoGdx.Utils;

namespace MonoGdx.Scene2D
{
    public enum Touchable
    {
        Enabled,
        Disabled,
        ChildrenOnly,
    }

    public class Actor
    {
        //public static readonly RoutedEvent GotKeyboardFocusEvent = Stage.GotKeyboardFocusEvent;
        //public static readonly RoutedEvent LostKeyboardFocusEvent = Stage.LostKeyboardFocusEvent;

        private readonly DelayedRemovalList<EventListener> _listeners = new DelayedRemovalList<EventListener>(0);
        private readonly DelayedRemovalList<EventListener> _captureListeners = new DelayedRemovalList<EventListener>(0);
        private readonly List<SceneAction> _actions = new List<SceneAction>(0);

        private Dictionary<int, DelayedRemovalList<RoutedEventHandlerInfo>> _handlers = new Dictionary<int, DelayedRemovalList<RoutedEventHandlerInfo>>(0);

        static Actor ()
        {
            EventManager.RegisterClassHandler(typeof(Actor), Stage.GotKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(GotKeyboardFocusClass));
            EventManager.RegisterClassHandler(typeof(Actor), Stage.LostKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(LostKeyboardFocusClass));
            EventManager.RegisterClassHandler(typeof(Actor), Stage.GotScrollFocusEvent, new ScrollFocusChangedEventHandler(GotScrollFocusClass));
            EventManager.RegisterClassHandler(typeof(Actor), Stage.LostScrollFocusEvent, new ScrollFocusChangedEventHandler(LostScrollFocusClass));
        }

        public Actor ()
        {
            Touchable = Touchable.Enabled;
            ScaleX = 1;
            ScaleY = 1;
            IsVisible = true;
            Color = Color.White;
        }

        public virtual void Draw (GdxSpriteBatch spriteBatch, float parentAlpha)
        { }

        public virtual void Act (float delta)
        {
            for (int i = 0; i < _actions.Count; i++) {
                SceneAction action = _actions[i];
                if (action.Act(delta)) {
                    _actions.RemoveAt(i);
                    action.Actor = null;
                    i--;
                }
            }
        }

        public bool Fire (Event ev)
        {
            if (ev.Stage == null)
                ev.Stage = Stage;
            ev.TargetActor = this;

            // Collect ancestors so event propagation is unaffected by hierarchy changes.
            List<Group> ancestors = Pools<List<Group>>.Obtain();
            Group parent = Parent;
            while (parent != null) {
                ancestors.Add(parent);
                parent = parent.Parent;
            }

            try {
                // Notify all parent capture listeners, starting at the root. Ancestors may stop an event before children receive it.
                for (int i = ancestors.Count - 1; i >= 0; i--) {
                    Group currentTarget = ancestors[i];
                    currentTarget.Notify(ev, true);
                    if (ev.IsStopped)
                        return ev.IsCancelled;
                }

                // Notify the target capture listeners.
                Notify(ev, true);
                if (ev.IsStopped)
                    return ev.IsCancelled;

                // Notify the target listeners.
                Notify(ev, false);
                if (!ev.Bubbles)
                    return ev.IsCancelled;
                if (ev.IsStopped)
                    return ev.IsCancelled;

                // Notify all parent listeners, starting at the target. Children may stop an event before ancestors receive it.
                foreach (Group ancestor in ancestors) {
                    ancestor.Notify(ev, false);
                    if (ev.IsStopped)
                        return ev.IsCancelled;
                }

                return ev.IsCancelled;
            }
            finally {
                ancestors.Clear();
                Pools<List<Group>>.Release(ancestors);
            }
        }

        public bool RaiseEvent (RoutedEventArgs e)
        {
            if (e.Stage == null)
                e.Stage = Stage;

            if (e.OriginalSource == null) {
                e.OriginalSource = this;
                e.Source = this;
            }

            EventManager.InvokeClassHandlers(this, e);

            if (e.RoutedEvent.RoutingStrategy == RoutingStrategy.Direct) {
                InvokeHandler(e);
                return e.Cancelled;
            }

            // Collect ancestors so event propagation is unaffected by hierarchy changes.
            List<Group> ancestors = Pools<List<Group>>.Obtain();
            Group parent = Parent;
            while (parent != null) {
                ancestors.Add(parent);
                parent = parent.Parent;
            }

            try {
                if (e.RoutedEvent.RoutingStrategy == RoutingStrategy.Tunnel) {
                    // Notify all parent capture listeners, starting at the root. Ancestors may stop an event before children receive it.
                    for (int i = ancestors.Count - 1; i >= 0; i--) {
                        Group currentTarget = ancestors[i];
                        currentTarget.InvokeHandler(e);
                        if (e.Stopped)
                            return e.Cancelled;
                    }

                    InvokeHandler(e);
                    if (e.Stopped)
                        return e.Cancelled;
                }
                else if (e.RoutedEvent.RoutingStrategy == RoutingStrategy.Bubble) {
                    InvokeHandler(e);
                    if (e.Stopped)
                        return e.Cancelled;

                    // Notify all parent listeners, starting at the target. Children may stop an event before ancestors receive it.
                    foreach (Group ancestor in ancestors) {
                        ancestor.InvokeHandler(e);
                        if (e.Stopped)
                            return e.Cancelled;
                    }
                }

                return e.Cancelled;
            }
            finally {
                ancestors.Clear();
                Pools<List<Group>>.Release(ancestors);
            }
        }

        protected virtual RoutedEventArgs InitializeEventArgs (RoutedEventArgs e, RoutedEvent routedEvent)
        {
            e.RoutedEvent = routedEvent;
            e.OriginalSource = this;
            e.Source = this;

            return e;
        }

        public bool Notify (Event ev, bool capture)
        {
            if (ev.TargetActor == null)
                throw new ArgumentException("The event target cannot be null.");

            DelayedRemovalList<EventListener> listeners = capture ? _captureListeners : _listeners;<commit_msg>Invoke class handlers on each node of tree<commit_after>﻿/**
 * Copyright 2011-2013 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MonoGdx.Graphics.G2D;
using MonoGdx.Scene2D.Utils;
using MonoGdx.Utils;

namespace MonoGdx.Scene2D
{
    public enum Touchable
    {
        Enabled,
        Disabled,
        ChildrenOnly,
    }

    public class Actor
    {
        //public static readonly RoutedEvent GotKeyboardFocusEvent = Stage.GotKeyboardFocusEvent;
        //public static readonly RoutedEvent LostKeyboardFocusEvent = Stage.LostKeyboardFocusEvent;

        private readonly DelayedRemovalList<EventListener> _listeners = new DelayedRemovalList<EventListener>(0);
        private readonly DelayedRemovalList<EventListener> _captureListeners = new DelayedRemovalList<EventListener>(0);
        private readonly List<SceneAction> _actions = new List<SceneAction>(0);

        private Dictionary<int, DelayedRemovalList<RoutedEventHandlerInfo>> _handlers = new Dictionary<int, DelayedRemovalList<RoutedEventHandlerInfo>>(0);

        static Actor ()
        {
            EventManager.RegisterClassHandler(typeof(Actor), Stage.GotKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(GotKeyboardFocusClass));
            EventManager.RegisterClassHandler(typeof(Actor), Stage.LostKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(LostKeyboardFocusClass));
            EventManager.RegisterClassHandler(typeof(Actor), Stage.GotScrollFocusEvent, new ScrollFocusChangedEventHandler(GotScrollFocusClass));
            EventManager.RegisterClassHandler(typeof(Actor), Stage.LostScrollFocusEvent, new ScrollFocusChangedEventHandler(LostScrollFocusClass));
        }

        public Actor ()
        {
            Touchable = Touchable.Enabled;
            ScaleX = 1;
            ScaleY = 1;
            IsVisible = true;
            Color = Color.White;
        }

        public virtual void Draw (GdxSpriteBatch spriteBatch, float parentAlpha)
        { }

        public virtual void Act (float delta)
        {
            for (int i = 0; i < _actions.Count; i++) {
                SceneAction action = _actions[i];
                if (action.Act(delta)) {
                    _actions.RemoveAt(i);
                    action.Actor = null;
                    i--;
                }
            }
        }

        public bool Fire (Event ev)
        {
            if (ev.Stage == null)
                ev.Stage = Stage;
            ev.TargetActor = this;

            // Collect ancestors so event propagation is unaffected by hierarchy changes.
            List<Group> ancestors = Pools<List<Group>>.Obtain();
            Group parent = Parent;
            while (parent != null) {
                ancestors.Add(parent);
                parent = parent.Parent;
            }

            try {
                // Notify all parent capture listeners, starting at the root. Ancestors may stop an event before children receive it.
                for (int i = ancestors.Count - 1; i >= 0; i--) {
                    Group currentTarget = ancestors[i];
                    currentTarget.Notify(ev, true);
                    if (ev.IsStopped)
                        return ev.IsCancelled;
                }

                // Notify the target capture listeners.
                Notify(ev, true);
                if (ev.IsStopped)
                    return ev.IsCancelled;

                // Notify the target listeners.
                Notify(ev, false);
                if (!ev.Bubbles)
                    return ev.IsCancelled;
                if (ev.IsStopped)
                    return ev.IsCancelled;

                // Notify all parent listeners, starting at the target. Children may stop an event before ancestors receive it.
                foreach (Group ancestor in ancestors) {
                    ancestor.Notify(ev, false);
                    if (ev.IsStopped)
                        return ev.IsCancelled;
                }

                return ev.IsCancelled;
            }
            finally {
                ancestors.Clear();
                Pools<List<Group>>.Release(ancestors);
            }
        }

        public bool RaiseEvent (RoutedEventArgs e)
        {
            if (e.Stage == null)
                e.Stage = Stage;

            if (e.OriginalSource == null) {
                e.OriginalSource = this;
                e.Source = this;
            }

            EventManager.InvokeClassHandlers(this, e);

            if (e.RoutedEvent.RoutingStrategy == RoutingStrategy.Direct) {
                EventManager.InvokeClassHandlers(this, e);
                if (!e.Stopped)
                    InvokeHandler(e);
                return e.Cancelled;
            }

            // Collect ancestors so event propagation is unaffected by hierarchy changes.
            List<Group> ancestors = Pools<List<Group>>.Obtain();
            Group parent = Parent;
            while (parent != null) {
                ancestors.Add(parent);
                parent = parent.Parent;
            }

            try {
                if (e.RoutedEvent.RoutingStrategy == RoutingStrategy.Tunnel) {
                    // Notify all parent capture listeners, starting at the root. Ancestors may stop an event before children receive it.
                    for (int i = ancestors.Count - 1; i >= 0; i--) {
                        Group currentTarget = ancestors[i];
                        EventManager.InvokeClassHandlers(currentTarget, e);
                        if (!e.Stopped)
                            currentTarget.InvokeHandler(e);
                        if (e.Stopped)
                            return e.Cancelled;
                    }

                    EventManager.InvokeClassHandlers(this, e);
                    if (!e.Stopped)
                        InvokeHandler(e);
                    if (e.Stopped)
                        return e.Cancelled;
                }
                else if (e.RoutedEvent.RoutingStrategy == RoutingStrategy.Bubble) {
                    EventManager.InvokeClassHandlers(this, e);
                    if (!e.Stopped)
                        InvokeHandler(e);
                    if (e.Stopped)
                        return e.Cancelled;

                    // Notify all parent listeners, starting at the target. Children may stop an event before ancestors receive it.
                    foreach (Group ancestor in ancestors) {
                        EventManager.InvokeClassHandlers(ancestor, e);
                        if (!e.Stopped)
                            ancestor.InvokeHandler(e);
                        if (e.Stopped)
                            return e.Cancelled;
                    }
                }

                return e.Cancelled;
            }
            finally {
                ancestors.Clear();
                Pools<List<Group>>.Release(ancestors);
            }
        }

        protected virtual RoutedEventArgs InitializeEventArgs (RoutedEventArgs e, RoutedEvent routedEvent)
        {
            e.RoutedEvent = routedEvent;
            e.OriginalSource = this;
            e.Source = this;

            return e;
        }

        public bool Notify (Event ev, bool capture)
        {
            if (ev.TargetActor == null)
                throw new ArgumentException("The event target cannot be null.");

            DelayedRemovalList<EventListener> listeners = capture ? _captureListeners : _listeners;
{'fraction_non_alphanumeric': 0.07731273348952067, 'fraction_numerical': 0.0032925979864496927, 'mean_word_length': 1.605410755526229, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '778365', 'n_tokens_mistral': 3985, 'n_tokens_neox': 3716, 'n_words': 1048}