private void Image2DPropertyViewContent_Loaded(object source, RoutedEventArgs e)
        {
            DebugHelper.AssertUIThread();

            this.Loaded -= Image2DPropertyViewContent_Loaded;

            if ((this.pluginService != null) && (this.viewSettings != null))
            {
                foreach (IPlugin plugin in pluginService.Plugins)
                {
                    XElement pluginViewSettingsElement = viewSettings.GetPluginViewSettings(plugin.Id);

                    I2DVisualPlugin visualPlugin = plugin as I2DVisualPlugin;
                    if (visualPlugin != null)
                    {
                        ContentControl hostControl = new ContentControl();

                        IPluginViewSettings pluginViewSettings = visualPlugin.Add2DPropertyView(hostControl);

                        if (pluginViewSettings != null)
                        {
                            XElement pluginViewDataElement = null;
                            if (pluginViewSettingsElement != null)
                            {
                                pluginViewDataElement = pluginViewSettingsElement.Element("data");
                            }
                            pluginViewSettings.ReadFrom(pluginViewDataElement);
                        }

                        if ((pluginViewSettings != null) || (hostControl.Content != null))
                        {
                            PluginViewState pluginViewState = new PluginViewState(plugin, pluginViewSettings, hostControl);

                            this.pluginViewStates.Add(pluginViewState);

                            if (hostControl.Content != null)
                            {
                                this.List.Children.Add(hostControl);
                            }
                        }
                    }
                }
            }
        }
        public void ShowSettings()
        {
            DebugHelper.AssertUIThread();

            if (this.viewSettings != null)
            {
                HashSet <KStudioEventStreamIdentifier> availableStreamIds = null;

                if (this.availableStreamsGetter != null)
                {
                    switch (this.eventType)
                    {
                    case EventType.Monitor:
                        availableStreamIds = this.availableStreamsGetter.GetAvailableMonitorStreams();
                        break;

                    case EventType.Inspection:
                        availableStreamIds = this.availableStreamsGetter.GetAvailableComboStreams();
                        break;
                    }
                }

                ObservableCollection <PluginViewState> pluginViewStatesCopy = new ObservableCollection <PluginViewState>();

                foreach (PluginViewState pluginViewState in this.pluginViewStates)
                {
                    PluginViewState     pluginViewStateCopy    = new PluginViewState(pluginViewState);
                    IPluginViewSettings pluginViewSettingsCopy = pluginViewStateCopy.PluginViewSettings;

                    if (pluginViewSettingsCopy != null)
                    {
                        pluginViewStatesCopy.Add(pluginViewStateCopy);

                        pluginViewSettingsCopy.CheckRequirementsSatisfied(availableStreamIds);
                    }
                }

                string viewTitle = this.Title;

                using (RenderViewSettings settings = new RenderViewSettings(viewTitle, pluginViewStatesCopy))
                {
                    RenderViewSettingsDialog settingsDialog = new RenderViewSettingsDialog()
                    {
                        DataContext = settings,
                        Owner       = Window.GetWindow(this),
                        Title       = this.SettingsTitle,
                    };

                    XElement viewSettingsElement = viewSettings.ViewSettingsElement;
                    settingsDialog.Width  = settingsDialog.Width = XmlExtensions.GetAttribute(viewSettingsElement, "editWidth", settingsDialog.Width);
                    settingsDialog.Height = settingsDialog.Height = XmlExtensions.GetAttribute(viewSettingsElement, "editHeight", settingsDialog.Height);

                    settingsDialog.ShowDialog();

                    if (settingsDialog.DialogResult == true)
                    {
                        int order = 0;

                        foreach (PluginViewState pluginViewState in pluginViewStatesCopy)
                        {
                            XElement pluginViewElement = new XElement("plugin");

                            if (pluginViewState.PluginViewSettings != null)
                            {
                                XElement pluginViewDataElement = new XElement("data");
                                pluginViewState.PluginViewSettings.WriteTo(pluginViewDataElement);
                                pluginViewElement.Add(pluginViewDataElement);
                            }

                            pluginViewState.Order = order;

                            pluginViewElement.SetAttributeValue("enabled", pluginViewState.IsEnabled.ToString(CultureInfo.InvariantCulture));
                            pluginViewElement.SetAttributeValue("order", pluginViewState.Order.ToString(CultureInfo.InvariantCulture));

                            viewSettings.SetPluginViewSettings(pluginViewState.Plugin.Id, pluginViewElement);
                            ++order;
                        }

                        if (viewSettingsElement != null)
                        {
                            viewSettingsElement.SetAttributeValue("editWidth", settingsDialog.ActualWidth.ToString(CultureInfo.InvariantCulture));
                            viewSettingsElement.SetAttributeValue("editHeight", settingsDialog.ActualHeight.ToString(CultureInfo.InvariantCulture));
                        }

                        this.pluginViewStates.Clear();
                        this.pluginViewStates.AddRange(pluginViewStatesCopy);

                        this.OnSettingsChanged();
                    }
                }
            }
        }
        private void InitializePlugins()
        {
            DebugHelper.AssertUIThread();

            if ((this.pluginService != null) && (this.viewSettings != null) && (this.pluginViewStates != null) && (this.pluginViewStates.Count == 0))
            {
                bool hasUserSet        = false;
                Guid depthPluginId     = new Guid(0x4fc932f6, 0x77a4, 0x4a22, 0xbe, 0x1f, 0x93, 0x42, 0x4d, 0x8e, 0xbb, 0x1a);
                Guid bodyPluginId      = new Guid(0x85a371bc, 0x7bb2, 0x4534, 0x86, 0x5d, 0xb7, 0x2, 0x67, 0x54, 0xe8, 0x76);
                Guid accessoryPluginId = new Guid(0xd1ec6fb2, 0xb19d, 0x4285, 0x9b, 0x69, 0xdc, 0x92, 0x1f, 0xeb, 0xf6, 0x9f);

                Dictionary <Guid, PluginViewState> defaultEnabled = new Dictionary <Guid, PluginViewState>();
                defaultEnabled[depthPluginId]     = null;
                defaultEnabled[bodyPluginId]      = null;
                defaultEnabled[accessoryPluginId] = null;

                foreach (IPlugin plugin in pluginService.Plugins)
                {
                    if ((this.filterFunc == null) || this.filterFunc(plugin))
                    {
                        bool enabled = false;
                        int  order   = int.MaxValue;

                        XElement pluginViewSettingsElement = viewSettings.GetPluginViewSettings(plugin.Id);
                        enabled = XmlExtensions.GetAttribute(pluginViewSettingsElement, "enabled", enabled);
                        order   = XmlExtensions.GetAttribute(pluginViewSettingsElement, "order", order);

                        if (enabled || (order != int.MaxValue)) // user has deliberately made changes
                        {
                            hasUserSet = true;
                        }

                        {
                            Panel hostControl;

                            IPluginViewSettings pluginViewSettings = AddView(plugin, out hostControl);
                            if (pluginViewSettings != null)
                            {
                                XElement pluginViewDataElement = null;
                                if (pluginViewSettingsElement != null)
                                {
                                    pluginViewDataElement = pluginViewSettingsElement.Element("data");
                                }
                                pluginViewSettings.ReadFrom(pluginViewDataElement);

                                PluginViewState pluginViewState = new PluginViewState(plugin, pluginViewSettings, hostControl)
                                {
                                    IsEnabled = enabled,
                                    Order     = order
                                };

                                this.pluginViewStates.Add(pluginViewState);

                                PluginViewState temp;
                                if (defaultEnabled.TryGetValue(plugin.Id, out temp))
                                {
                                    if (temp == null)
                                    {
                                        defaultEnabled[plugin.Id] = pluginViewState;
                                    }
                                }
                            }
                        }
                    }
                }

                if (!hasUserSet)
                {
                    foreach (PluginViewState pluginViewState in defaultEnabled.Values)
                    {
                        if (pluginViewState != null)
                        {
                            pluginViewState.IsEnabled = true;
                        }
                    }
                }

                this.pluginViewStates.Sort(delegate(PluginViewState a, PluginViewState b)
                {
                    int result = 0;
                    if (a == null)
                    {
                        if (b != null)
                        {
                            result = -1;
                        }
                    }
                    else
                    {
                        if (b == null)
                        {
                            result = 1;
                        }
                        else
                        {
                            result = a.Order.CompareTo(b.Order);
                            if (result == 0)
                            {
                                result = a.Plugin.Id.CompareTo(b.Plugin.Id);
                            }
                        }
                    }

                    return(result);
                });
            }
        }
        private void PluginViewSettings_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            DebugHelper.AssertUIThread();

            IPluginViewSettings pluginViewSettings      = null;
            PluginViewState     pluginViewStateChanging = sender as PluginViewState;

            if (pluginViewStateChanging != null)
            {
                pluginViewSettings = pluginViewStateChanging.PluginViewSettings;
            }
            else
            {
                pluginViewSettings = sender as IPluginViewSettings;
            }

            Debug.Assert(pluginViewSettings != null);

            IPlugin3DViewSettings plugin3DViewSettings = pluginViewSettings as IPlugin3DViewSettings;

            bool isSupplyingSurface = false;
            bool isSupplyingTexture = false;
            bool isRenderingOpaque  = false;

            switch (e.PropertyName)
            {
            case "IsEnabled":
            {
                Debug.Assert(pluginViewStateChanging != null);
                if (pluginViewStateChanging.IsEnabled)
                {
                    if (plugin3DViewSettings != null)
                    {
                        isSupplyingSurface = plugin3DViewSettings.IsSupplyingSurface;
                        isSupplyingTexture = plugin3DViewSettings.IsSupplyingTexture;
                    }
                    isRenderingOpaque = pluginViewSettings.IsRendingOpaque;
                }
            }
            break;

            case "IsSupplyingSurface":
                if (plugin3DViewSettings != null)
                {
                    isSupplyingSurface = plugin3DViewSettings.IsSupplyingSurface;
                }
                break;

            case "IsSupplyingTexture":
                if (plugin3DViewSettings != null)
                {
                    isSupplyingTexture = plugin3DViewSettings.IsSupplyingTexture;
                }
                break;

            case "IsRenderingOpaque":
                isRenderingOpaque = pluginViewSettings.IsRendingOpaque;
                break;
            }

            if (isSupplyingSurface || isSupplyingTexture || isRenderingOpaque)
            {
                Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
                {
                    if (isSupplyingSurface)
                    {
                        // tell any other plugin that thought it was supplying the texture that it might not be
                        foreach (PluginViewState pluginViewState in this.pluginViewStates)
                        {
                            IPlugin3DViewSettings other3DViewSettings = pluginViewState.PluginViewSettings as IPlugin3DViewSettings;
                            if ((other3DViewSettings != null) && pluginViewState.IsEnabled && (other3DViewSettings != plugin3DViewSettings))
                            {
                                pluginViewState.IsEnabled = other3DViewSettings.OtherIsSupplyingSurface();
                            }
                        }
                    }

                    if (isSupplyingTexture)
                    {
                        // tell any other plugin that thought it was supplying the texture that it might not be
                        foreach (PluginViewState pluginViewState in this.pluginViewStates)
                        {
                            IPlugin3DViewSettings other3DViewSettings = pluginViewState.PluginViewSettings as IPlugin3DViewSettings;
                            if ((other3DViewSettings != null) && pluginViewState.IsEnabled && (other3DViewSettings != plugin3DViewSettings))
                            {
                                pluginViewState.IsEnabled = other3DViewSettings.OtherIsSupplyingTexture();
                            }
                        }
                    }
                    else
                    if (isRenderingOpaque)
                    {
                        // tell any other plugin that thought it was rendering opaque that it might not be
                        foreach (PluginViewState pluginViewState in this.pluginViewStates)
                        {
                            IPluginViewSettings otherViewSettings = pluginViewState.PluginViewSettings;
                            if ((otherViewSettings != null) && pluginViewState.IsEnabled && (otherViewSettings != pluginViewSettings))
                            {
                                pluginViewState.IsEnabled = otherViewSettings.OtherIsRenderingOpaque();
                            }
                        }
                    }
                }));
            }
        }