public static bool IsParentASibling(this InstanceSave instanceSave, List <ElementWithState> elementStack)
        {
            if (instanceSave == null)
            {
                throw new ArgumentException("InstanceSave must not be null");
            }

            RecursiveVariableFinder rvf = new RecursiveVariableFinder(instanceSave, elementStack);

            string parent = rvf.GetValue <string>("Parent");
            bool   found  = false;

            if (!string.IsNullOrEmpty(parent) && parent != AvailableInstancesConverter.ScreenBoundsName)
            {
                ElementSave parentElement = instanceSave.ParentContainer;

                found = parentElement.Instances.Any(item => item.Name == parent);
            }

            return(found);
        }
        private static void GetStatesToUse(InstanceSave instance, List <ElementWithState> elementStack, bool forceDefault, ElementSave instanceBase, RecursiveVariableFinder rvf, out List <StateSave> statesToPullFrom, out StateSave defaultState)
        {
            statesToPullFrom = null;
            defaultState     = null;

#if GUM
            if (SelectedState.Self.SelectedElement != null)
            {
                statesToPullFrom = new List <StateSave> {
                    SelectedState.Self.SelectedElement.DefaultState
                };
                defaultState = SelectedState.Self.SelectedElement.DefaultState;
            }
#endif

            if (elementStack.Count != 0)
            {
                if (elementStack.Last().Element == null)
                {
                    throw new InvalidOperationException("The ElementStack contains an ElementWithState with no Element");
                }
                statesToPullFrom = elementStack.Last().AllStates.ToList();
                defaultState     = elementStack.Last().Element.DefaultState;
            }


#if GUM
            if (elementStack.Count != 0 && elementStack.Last().Element == SelectedState.Self.SelectedElement &&
                SelectedState.Self.SelectedStateSave != null &&
                !forceDefault)
            {
                statesToPullFrom = new List <StateSave> {
                    SelectedState.Self.SelectedStateSave
                };
            }
#endif
        }
        public static VariableSave GetVariableFromThisOrBase(this InstanceSave instance,
                                                             List <ElementWithState> elementStack, RecursiveVariableFinder rvf, string variable, bool forceDefault, bool onlyIfSetsValue)
        {
            ElementSave instanceBase = ObjectFinder.Self.GetElementSave(instance.BaseType);

            List <StateSave> statesToPullFrom;
            StateSave        defaultState;

            GetStatesToUse(instance, elementStack, forceDefault, instanceBase, rvf, out statesToPullFrom, out defaultState);


            VariableSave variableSave = null;

            // See if the variable is set by the container of the instance:
            foreach (var stateToPullFrom in statesToPullFrom)
            {
                var possibleVariable = stateToPullFrom.GetVariableSave(instance.Name + "." + variable);
                if (possibleVariable != null)
                {
                    variableSave = possibleVariable;
                }
            }
            // non-default states can override the default state, so first
            // let's see if the selected state is non-default and has a value
            // for a given variable.  If not, we'll fall back to the default.
            if ((variableSave == null || (onlyIfSetsValue && variableSave.SetsValue == false)) && !statesToPullFrom.Contains(defaultState))
            {
                variableSave = defaultState.GetVariableSave(instance.Name + "." + variable);
            }

            // Still haven't found a variable yet, so look in the instanceBase if one exists
            if ((variableSave == null ||
                 (onlyIfSetsValue && (variableSave.SetsValue == false || variableSave.Value == null))) && instanceBase != null)
            {
                VariableSave foundVariableSave = TryGetVariableFromStatesOnInstance(instance, variable, instanceBase, statesToPullFrom);

                if (foundVariableSave != null)
                {
                    variableSave = foundVariableSave;
                }
            }

            // I don't think we have to do this because we're going to copy over
            // the variables to all components on load.
            //if (variableSave == null && instanceBase != null && instanceBase is ComponentSave)
            //{
            //    variableSave = StandardElementsManager.Self.DefaultStates["Component"].GetVariableSave(variable);
            //}

            if (variableSave != null && variableSave.Value == null && instanceBase != null && onlyIfSetsValue)
            {
                // This can happen if there is a tunneled variable that is null
                VariableSave possibleVariable = instanceBase.DefaultState.GetVariableSave(variable);
                if (possibleVariable != null && possibleVariable.Value != null && (!onlyIfSetsValue || possibleVariable.SetsValue))
                {
                    variableSave = possibleVariable;
                }
                else if (!string.IsNullOrEmpty(instanceBase.BaseType))
                {
                    ElementSave element = ObjectFinder.Self.GetElementSave(instanceBase.BaseType);

                    if (element != null)
                    {
                        variableSave = element.GetVariableFromThisOrBase(variable, forceDefault);
                    }
                }
            }

            return(variableSave);
        }