PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

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