<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using g3;

namespace f3
{
    public class GroupSO : TransformableSO, SOCollection, SOParent
    {
        GameObject gameObject;
        SOParent parent;
        protected string uuid;
        List<TransformableSO> vChildren;

        FScene parentScene;
        bool defer_origin_update;

        // [TODO] we need to check children timestamps, no??
        int _timestamp = 0;
        protected void increment_timestamp() { _timestamp++; }


        // enable/disable selection of group, or alternately allow selection
        // of child objects. *however* child object selection is not really
        // supported properly in other places, this is mainly meant for
        // special situations, like selection forwarding, etc
        public enum SelectionModes
        {
            NoSelection, 
            SelectGroup,
            SelectChildren
        }
        public SelectionModes SelectionMode = SelectionModes.SelectGroup;


        public GroupSO()
        {
            uuid = System.Guid.NewGuid().ToString();
            vChildren = new List<TransformableSO>();
            defer_origin_update = false;
        }
        ~GroupSO()
        {
            //foreach (var so in vChildren)
            //    so.OnTransformModified -= childTransformModified;
        }

        public void Create()
        {
            gameObject = new GameObject(UniqueNames.GetNext("Group"));
            increment_timestamp();
        }


        public void AddChild(TransformableSO so)
        {
            if (!vChildren.Contains(so)) {
                vChildren.Add(so);
                if (parentScene != null) {
                    if (so.Parent == null)
                        throw new Exception("GroupSO.AddChild: tried to re-parent SO to group that has no parent!");
                    parentScene.AddSceneObjectToParentSO(so, this);
                }
                update_shared_origin();
                increment_timestamp();
                //so.OnTransformModified += childTransformModified;
            }
        }
        public void AddChildren(IEnumerable<TransformableSO> v)
        {
            defer_origin_update = true;
            foreach (TransformableSO so in v)
                AddChild(so);
            defer_origin_update = false;
            update_shared_origin();
        }

        public void RemoveChild(TransformableSO so)
        {
            if ( vChildren.Contains(so) ) {
                //so.OnTransformModified -= childTransformModified;
                vChildren.Remove(so);
                parentScene.RemoveSceneObjectFromParentSO(so);
                update_shared_origin();
                increment_timestamp();
            }
        }

        public void RemoveAllChildren()
        {
            defer_origin_update = true;
            while (vChildren.Count > 0)
                RemoveChild(vChildren[0]);
            defer_origin_update = false;
            update_shared_origin();
        }


        void update_shared_origin()
        {
            if (defer_origin_update)
                return;

            if ( vChildren.Count == 0 ) {
                RootGameObject.SetPosition(Vector3f.Zero);
                return;
            }

            Vector3f origin = Vector3f.Zero;
            foreach (TransformableSO so in vChildren) {
                origin += so.GetLocalFrame(CoordSpace.WorldCoords).Origin;
                so.RootGameObject.SetParent(null);
            }
            origin *= 1.0f / (float)vChildren.Count;
            RootGameObject.SetPosition(origin);
            foreach (TransformableSO so in vChildren) {
                so.RootGameObject.SetParent(gameObject, true);
            }
        }


        //
        // SceneObject impl
        //

        public fGameObject RootGameObject
        {
            get { return gameObject; }
        }

        virtual public SOParent Parent
        {
            get {
                return parent;
            }
            set {
                parent = value;
            }
        }

        public virtual string Name
        {
            get { return gameObject.GetName(); }
            set { gameObject.SetName(value); }
        }

        virtual public SOType Type { get { return SOTypes.Group; } }

        virtual public string UUID
        {
            get { return uuid; }
        }
<commit_msg>removed unityengine<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using g3;

namespace f3
{
    public class GroupSO : TransformableSO, SOCollection, SOParent
    {
        fGameObject parentGO;
        SOParent parent;
        protected string uuid;
        List<TransformableSO> vChildren;

        FScene parentScene;
        bool defer_origin_update;

        // [TODO] we need to check children timestamps, no??
        int _timestamp = 0;
        protected void increment_timestamp() { _timestamp++; }


        // enable/disable selection of group, or alternately allow selection
        // of child objects. *however* child object selection is not really
        // supported properly in other places, this is mainly meant for
        // special situations, like selection forwarding, etc
        public enum SelectionModes
        {
            NoSelection, 
            SelectGroup,
            SelectChildren
        }
        public SelectionModes SelectionMode = SelectionModes.SelectGroup;


        public GroupSO()
        {
            uuid = System.Guid.NewGuid().ToString();
            vChildren = new List<TransformableSO>();
            defer_origin_update = false;
        }
        ~GroupSO()
        {
            //foreach (var so in vChildren)
            //    so.OnTransformModified -= childTransformModified;
        }

        public void Create()
        {
            parentGO = GameObjectFactory.CreateParentGO(UniqueNames.GetNext("Group"));
            increment_timestamp();
        }


        public void AddChild(TransformableSO so)
        {
            if (!vChildren.Contains(so)) {
                vChildren.Add(so);
                if (parentScene != null) {
                    if (so.Parent == null)
                        throw new Exception("GroupSO.AddChild: tried to re-parent SO to group that has no parent!");
                    parentScene.AddSceneObjectToParentSO(so, this);
                }
                update_shared_origin();
                increment_timestamp();
                //so.OnTransformModified += childTransformModified;
            }
        }
        public void AddChildren(IEnumerable<TransformableSO> v)
        {
            defer_origin_update = true;
            foreach (TransformableSO so in v)
                AddChild(so);
            defer_origin_update = false;
            update_shared_origin();
        }

        public void RemoveChild(TransformableSO so)
        {
            if ( vChildren.Contains(so) ) {
                //so.OnTransformModified -= childTransformModified;
                vChildren.Remove(so);
                parentScene.RemoveSceneObjectFromParentSO(so);
                update_shared_origin();
                increment_timestamp();
            }
        }

        public void RemoveAllChildren()
        {
            defer_origin_update = true;
            while (vChildren.Count > 0)
                RemoveChild(vChildren[0]);
            defer_origin_update = false;
            update_shared_origin();
        }


        void update_shared_origin()
        {
            if (defer_origin_update)
                return;

            if ( vChildren.Count == 0 ) {
                SetLocalFrame(Frame3f.Identity, CoordSpace.SceneCoords);
                return;
            }

            Vector3f origin = Vector3f.Zero;
            foreach (TransformableSO so in vChildren) {
                origin += so.GetLocalFrame(CoordSpace.SceneCoords).Origin;
                // remove from any existing parent
                so.GetScene().ReparentSceneObject(so);
            }
            origin *= 1.0f / (float)vChildren.Count;
            SetLocalFrame(new Frame3f(origin), CoordSpace.SceneCoords);
            foreach (TransformableSO so in vChildren) {
                so.GetScene().AddSceneObjectToParentSO(so, this);
            }
        }


        //
        // SceneObject impl
        //

        public fGameObject RootGameObject
        {
            get { return parentGO; }
        }

        virtual public SOParent Parent
        {
            get {
                return parent;
            }
            set {
                parent = value;
            }
        }

        public virtual string Name
        {
            get { return parentGO.GetName(); }
            set { parentGO.SetName(value); }
        }

        virtual public SOType Type { get { return SOTypes.Group; } }

        virtual public string UUID
        {
            get { return uuid; }
        }

{'fraction_non_alphanumeric': 0.07772596047079725, 'fraction_numerical': 0.0025538529868976236, 'mean_word_length': 1.49708899362351, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '1744350', 'n_tokens_mistral': 2253, 'n_tokens_neox': 2147, 'n_words': 522}