PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/Main/src/Microsoft.Research.Visualization3D/DrawableComponentsManager.cs

#
C# | 115 lines | 102 code | 13 blank | 0 comment | 11 complexity | af51d18593dbde1cf37b7c77cfaeaa46 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using Microsoft.Research.Visualization3D.MainLoops;
  6
  7namespace Microsoft.Research.Visualization3D
  8{
  9    public class DrawableComponentsManager
 10    {
 11        private List<DrawableComponent> providers;
 12        private List<DrawableComponent> backgroundProviders;
 13
 14        public DrawableComponentsManager()
 15        {
 16            providers = new List<DrawableComponent>();
 17            backgroundProviders = new List<DrawableComponent>();
 18        }
 19
 20        public void Initialize()
 21        {
 22            foreach (DrawableComponent provider in providers)
 23            {
 24                if (provider.IsEnabled)
 25                    provider.Initialize();
 26            }
 27            foreach (DrawableComponent provider in backgroundProviders)
 28            {
 29                provider.Initialize();
 30            }
 31        }
 32
 33        public void Update(TimeEntity time)
 34        {
 35            foreach (DrawableComponent provider in providers)
 36            {
 37                if (provider.IsEnabled)
 38                    provider.Update(time);
 39            }
 40            foreach (DrawableComponent provider in backgroundProviders)
 41            {
 42                provider.Update(time);
 43            }
 44        }
 45
 46        public void Draw(TimeEntity time)
 47        {
 48            foreach (DrawableComponent provider in providers)
 49            {
 50                if (provider.IsEnabled)
 51                    provider.Draw(time);
 52            }
 53            foreach (DrawableComponent provider in backgroundProviders)
 54            {
 55                provider.Draw(time);
 56            }
 57        }
 58
 59        public void AddProvider(DrawableComponent newProvider, bool enable)
 60        {
 61            providers.Add(newProvider);
 62            if (enable)
 63            {
 64                foreach (DrawableComponent provider in providers)
 65                {
 66                    provider.IsEnabled = false;
 67                }
 68                newProvider.IsEnabled = true;
 69            }
 70            else
 71            {
 72                newProvider.IsEnabled = false;
 73            }
 74        }
 75
 76        public void AddBackgroundProvider(DrawableComponent newBackgroundProvider)
 77        {
 78            backgroundProviders.Add(newBackgroundProvider);
 79            newBackgroundProvider.IsEnabled = true;
 80            newBackgroundProvider.Initialize();
 81        }
 82
 83        public DrawableComponent GetProviderByType<T>()
 84        {
 85            DrawableComponent result = providers.Find(p => p is T);
 86            if (result != null)
 87                return result;
 88            else
 89            {
 90                result = backgroundProviders.Find(p => p is T);
 91                if (result != null)
 92                    return result;
 93            }
 94            throw new ArgumentException("Invalid Provider Type");
 95        }
 96
 97        public DrawableComponent SetCurrent<T>()
 98        {
 99            DrawableComponent provider = providers.Find(p => p is T);
100            if (provider != null && !provider.IsEnabled)
101            {
102                foreach (DrawableComponent otherProvdider in providers)
103                {
104                    otherProvdider.IsEnabled = false;
105                }
106                provider.IsEnabled = true;
107            }
108            return provider;
109        }
110
111        
112       
113
114    }
115}