<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RenderingLibrary;
using Gum.DataTypes;
using Gum.Managers;
using RenderingLibrary.Graphics;
using RenderingLibrary.Content;
using Gum.DataTypes.Variables;
using RenderingLibrary.Math.Geometry;
using Gum.DataTypes;
using Gum.RenderingLibrary;
using Gum.ToolStates;
using RenderingLibrary.Graphics.Fonts;
using System.Collections;
using ToolsUtilities;
using Microsoft.Xna.Framework;

namespace Gum.Wireframe
{
    public partial class WireframeObjectManager
    {

        #region Fields


        public string[] PositionAndSizeVariables = new string[]{
            "Width",
            "Height",
            "Width Units",
            "Height Units",
            "X Origin",
            "Y Origin",
            "X",
            "Y",
            "X Units",
            "Y Units",
            "Guide"
        
        };

        public string[] ColorAndAlpha = new string[]{
            "Red",
            "Green",
            "Blue",
            "Alpha"
        };

        public string[] Color = new string[]{
            "Red",
            "Green",
            "Blue"
        };


        Dictionary<NineSliceSections, string> PossibleNineSliceEndings = new Dictionary<NineSliceSections, string>()
        {
            {NineSliceSections.Center, "_center"},
            {NineSliceSections.Left, "_left"},
            {NineSliceSections.Right, "_right"},
            {NineSliceSections.TopLeft, "_topLeft"},
            {NineSliceSections.Top, "_topCenter"},
            {NineSliceSections.TopRight, "_topRight"},
            {NineSliceSections.BottomLeft, "_bottomLeft"},
            {NineSliceSections.Bottom, "_bottomCenter"},
            {NineSliceSections.BottomRight, "_bottomRight"}
        };

        #endregion


        private IPositionedSizedObject CreateRepresentationForInstance(InstanceSave instance, InstanceSave parentInstance, List<ElementWithState> elementStack, IPositionedSizedObject parentIpso)
        {
            IPositionedSizedObject toReturn = null;


            List<VariableSave> exposedVariables = GetExposedVariablesForThisInstance(instance, parentInstance, elementStack);


            if (instance.BaseType == "Sprite")
            {
                toReturn = CreateSpriteFor(instance, elementStack, parentIpso, exposedVariables);
            }
            else if (instance.BaseType == "Text")
            {
                toReturn = CreateTextFor(instance, elementStack, parentIpso, exposedVariables);
            }
            else if (instance.BaseType == "ColoredRectangle")
            {
                toReturn = CreateSolidRectangleFor(instance, elementStack, parentIpso, exposedVariables);
            }
            else if (instance.BaseType == "NineSlice")
            {
                toReturn = CreateNineSliceFor(instance, elementStack, parentIpso, exposedVariables);
            }
            else if (instance.IsComponent())
            {
                // Here we need to prefix the name before going any deeper, so we won't won't prefix the name at the end of the method
                //shouldPrefixParentName = false;

                //string prefixName = null;
                //if (parentIpso != null)
                //{
                //prefixName = parentIpso.Name + ".";
                //}
                toReturn = CreateRepresentationsForInstanceFromComponent(instance, elementStack, parentInstance, parentIpso, ObjectFinder.Self.GetComponent(instance.BaseType));
            }
            else
            {
                // Make sure the base type is valid.
                // This could be null if a base type changed
                // its name but the derived wasn't updated, or 
                // if someone screwed with the XML files.  Who knows...
                var baseElement = ObjectFinder.Self.GetElementSave(instance.BaseType);

                if (baseElement != null)
                {
                    toReturn = CreateRectangleFor(instance, elementStack, parentIpso, exposedVariables);
                }
            }
            //if (shouldPrefixParentName)
            //{
            //    string prefixName = null;
            //    if (parentIpso != null)
            //    {
            //        prefixName = parentIpso.Name + ".";
            //    }

            //    toReturn.Name = prefixName + toReturn.Name;

            //}

            // This was an attempt to fix sorting when parallelized
            //if (toReturn != null)
            //{
            //    if(parentInstance == null)
            //    {
            //        var parent = instance.ParentContainer;

            //        if(parent != null)
            //        {
            //            toReturn.Z = parent.Instances.IndexOf(instance);
            //        }
            //    }
            //}

            return toReturn;
        }

        private List<VariableSave> GetExposedVariablesForThisInstance(DataTypes.InstanceSave instance, InstanceSave parentInstance, List<ElementWithState> elementStack)
        {
            List<VariableSave> exposedVariables = new List<VariableSave>();
            if (elementStack.Count > 1)
            {
                ElementSave containerOfVariables = elementStack[elementStack.Count - 2].Element;
                ElementSave definerOfVariables = elementStack[elementStack.Count - 1].Element;

                foreach (VariableSave variable in definerOfVariables.DefaultState.Variables)
                {
                    if (!string.IsNullOrEmpty(variable.ExposedAsName) && variable.SourceObject == instance.Name)
                    {
                        // This variable is exposed, let's see if the container does anything with it

                        VariableSave foundVariable = containerOfVariables.DefaultState.GetVariableSave(parentInstance.Name + "." + variable.ExposedAsName);

                        if (foundVariable != null)
                        {
                            VariableSave variableToAdd = new VariableSave();
                            variableToAdd.Type = variable.Type;
                            variableToAdd.Value = foundVariable.Value;
                            variableToAdd.Name = variable.Name.Substring(variable.Name.IndexOf('.') + 1);
                            exposedVariables.Add(variableToAdd);
                        }

                    }<commit_msg>States can now set exposed variables and they show up in Gum.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RenderingLibrary;
using Gum.DataTypes;
using Gum.Managers;
using RenderingLibrary.Graphics;
using RenderingLibrary.Content;
using Gum.DataTypes.Variables;
using RenderingLibrary.Math.Geometry;
using Gum.DataTypes;
using Gum.RenderingLibrary;
using Gum.ToolStates;
using RenderingLibrary.Graphics.Fonts;
using System.Collections;
using ToolsUtilities;
using Microsoft.Xna.Framework;

namespace Gum.Wireframe
{
    public partial class WireframeObjectManager
    {

        #region Fields


        public string[] PositionAndSizeVariables = new string[]{
            "Width",
            "Height",
            "Width Units",
            "Height Units",
            "X Origin",
            "Y Origin",
            "X",
            "Y",
            "X Units",
            "Y Units",
            "Guide"
        
        };

        public string[] ColorAndAlpha = new string[]{
            "Red",
            "Green",
            "Blue",
            "Alpha"
        };

        public string[] Color = new string[]{
            "Red",
            "Green",
            "Blue"
        };


        Dictionary<NineSliceSections, string> PossibleNineSliceEndings = new Dictionary<NineSliceSections, string>()
        {
            {NineSliceSections.Center, "_center"},
            {NineSliceSections.Left, "_left"},
            {NineSliceSections.Right, "_right"},
            {NineSliceSections.TopLeft, "_topLeft"},
            {NineSliceSections.Top, "_topCenter"},
            {NineSliceSections.TopRight, "_topRight"},
            {NineSliceSections.BottomLeft, "_bottomLeft"},
            {NineSliceSections.Bottom, "_bottomCenter"},
            {NineSliceSections.BottomRight, "_bottomRight"}
        };

        #endregion


        private IPositionedSizedObject CreateRepresentationForInstance(InstanceSave instance, InstanceSave parentInstance, List<ElementWithState> elementStack, IPositionedSizedObject parentIpso)
        {
            IPositionedSizedObject toReturn = null;


            List<VariableSave> exposedVariables = GetExposedVariablesForThisInstance(instance, parentInstance, elementStack);


            if (instance.BaseType == "Sprite")
            {
                toReturn = CreateSpriteFor(instance, elementStack, parentIpso, exposedVariables);
            }
            else if (instance.BaseType == "Text")
            {
                toReturn = CreateTextFor(instance, elementStack, parentIpso, exposedVariables);
            }
            else if (instance.BaseType == "ColoredRectangle")
            {
                toReturn = CreateSolidRectangleFor(instance, elementStack, parentIpso, exposedVariables);
            }
            else if (instance.BaseType == "NineSlice")
            {
                toReturn = CreateNineSliceFor(instance, elementStack, parentIpso, exposedVariables);
            }
            else if (instance.IsComponent())
            {
                // Here we need to prefix the name before going any deeper, so we won't won't prefix the name at the end of the method
                //shouldPrefixParentName = false;

                //string prefixName = null;
                //if (parentIpso != null)
                //{
                //prefixName = parentIpso.Name + ".";
                //}
                toReturn = CreateRepresentationsForInstanceFromComponent(instance, elementStack, parentInstance, parentIpso, ObjectFinder.Self.GetComponent(instance.BaseType));
            }
            else
            {
                // Make sure the base type is valid.
                // This could be null if a base type changed
                // its name but the derived wasn't updated, or 
                // if someone screwed with the XML files.  Who knows...
                var baseElement = ObjectFinder.Self.GetElementSave(instance.BaseType);

                if (baseElement != null)
                {
                    toReturn = CreateRectangleFor(instance, elementStack, parentIpso, exposedVariables);
                }
            }
            //if (shouldPrefixParentName)
            //{
            //    string prefixName = null;
            //    if (parentIpso != null)
            //    {
            //        prefixName = parentIpso.Name + ".";
            //    }

            //    toReturn.Name = prefixName + toReturn.Name;

            //}

            // This was an attempt to fix sorting when parallelized
            //if (toReturn != null)
            //{
            //    if(parentInstance == null)
            //    {
            //        var parent = instance.ParentContainer;

            //        if(parent != null)
            //        {
            //            toReturn.Z = parent.Instances.IndexOf(instance);
            //        }
            //    }
            //}

            return toReturn;
        }

        private List<VariableSave> GetExposedVariablesForThisInstance(DataTypes.InstanceSave instance, InstanceSave parentInstance, List<ElementWithState> elementStack)
        {
            List<VariableSave> exposedVariables = new List<VariableSave>();
            if (elementStack.Count > 1)
            {
                ElementWithState containerOfVariables = elementStack[elementStack.Count - 2];
                ElementWithState definerOfVariables = elementStack[elementStack.Count - 1];

                foreach (VariableSave variable in definerOfVariables.Element.DefaultState.Variables)
                {
                    if (!string.IsNullOrEmpty(variable.ExposedAsName) && variable.SourceObject == instance.Name)
                    {
                        // This variable is exposed, let's see if the container does anything with it

                        VariableSave foundVariable = containerOfVariables.StateSave.GetVariableSave(parentInstance.Name + "." + variable.ExposedAsName);

                        if (foundVariable != null)
                        {
                            VariableSave variableToAdd = new VariableSave();
                            variableToAdd.Type = variable.Type;
                            variableToAdd.Value = foundVariable.Value;
                            variableToAdd.Name = variable.Name.Substring(variable.Name.IndexOf('.') + 1);
                            exposedVariables.Add(variableToAdd);
                        }

                    }
{'fraction_non_alphanumeric': 0.08871775125144397, 'fraction_numerical': 0.0006160954948016943, 'mean_word_length': 1.6448065173116089, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, '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': '6887920', 'n_tokens_mistral': 3189, 'n_tokens_neox': 3033, 'n_words': 709}