private void DiscoverSamples() { // Automatically find all samples using reflection. Samples are derived from // GameComponent and have a SampleAttribute. _samples = Assembly.GetCallingAssembly() .GetTypes() .Where(t => typeof(GameComponent).IsAssignableFrom(t) && SampleAttribute.GetSampleAttribute(t) != null) .ToList(); _samples.Sort(CompareSamples); if (_samples.Count == 0) { throw new Exception("No samples found."); } }
// Look for a game component marked with a SampleAttribute. private static bool FindSample(GameComponentCollection gameComponents, out string sampleName, out SampleAttribute sampleAttribute) { foreach (var gameComponent in gameComponents) { var type = gameComponent.GetType(); sampleAttribute = SampleAttribute.GetSampleAttribute(type); if (sampleAttribute != null) { // Sample found. sampleName = type.Name; return(true); } } // No sample found. sampleName = null; sampleAttribute = null; return(false); }
private static int CompareSamples(Type sampleA, Type sampleB) { var sampleAttributeA = SampleAttribute.GetSampleAttribute(sampleA); var sampleAttributeB = SampleAttribute.GetSampleAttribute(sampleB); // Sort by category ... var categoryA = sampleAttributeA.Category; var categoryB = sampleAttributeB.Category; if (categoryA < categoryB) { return(-1); } if (categoryA > categoryB) { return(+1); } // ... then by sample order. int orderA = sampleAttributeA.Order; int orderB = sampleAttributeB.Order; return(orderA - orderB); }
private void InitializeGui() { // Add the GuiGraphicsScreen to the graphics service. _guiGraphicsScreen = new GuiGraphicsScreen(_services); _graphicsService.Screens.Add(_guiGraphicsScreen); // Add the Settings window (v-sync, fixed/variable timing, parallel game loop). _settingsWindow = new SettingsWindow(_services) { X = 1000, Y = 500, MinHeight = 0, }; _settingsWindow.Show(_guiGraphicsScreen.UIScreen); // Create a window with a tab control. _menuWindow = new Window { Title = "Select Sample", X = 50, Y = 50, Width = 800, Height = 554, CloseButtonStyle = null }; _menuWindow.Show(_guiGraphicsScreen.UIScreen); // Each tab shows a sample category (Base, Mathematics, Geometry, ...). var samplesByCategory = _samples.GroupBy(t => SampleAttribute.GetSampleAttribute(t).Category); var tabControl = new TabControl { Margin = new Vector4F(4), HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch, }; _menuWindow.Content = tabControl; int category = -1; foreach (var grouping in samplesByCategory) { category++; var tabItem = new TabItem { Content = new TextBlock { Text = grouping.Key.ToString() }, }; tabControl.Items.Add(tabItem); // Optional: When the TabControl.SelectedIndex is changed, activate the // first sample of the category. //Type firstSample = grouping.First(); //var firstSampleIndex = _samples.IndexOf(firstSample); //var selectedIndexProperty = tabControl.Properties.Get<int>("SelectedIndex"); //int c = category; //selectedIndexProperty.Changed += (s, e) => //{ // if (tabControl.SelectedIndex == c) // _nextSampleIndex = firstSampleIndex; //}; // Each TabItem contains a horizontal stack panel. The horizontal panel // contains vertical stack panels with up to N buttons per vertical panel. var horizontalPanel = new StackPanel { Orientation = Orientation.Horizontal, Margin = new Vector4F(2, 0, 2, 0), }; tabItem.TabPage = horizontalPanel; StackPanel verticalPanel = null; int i = -1; foreach (Type sample in grouping) { i = (i + 1) % 15; if (i == 0) { verticalPanel = new StackPanel { Orientation = Orientation.Vertical, }; horizontalPanel.Children.Add(verticalPanel); } string summary = SampleAttribute.GetSampleAttribute(sample).Summary; var button = new Button { Content = new TextBlock { Text = sample.Name, Margin = new Vector4F(0, 2, 0, 2) }, HorizontalAlignment = HorizontalAlignment.Stretch, Margin = new Vector4F(4), ToolTip = string.IsNullOrEmpty(summary) ? null : summary }; var sampleIndex = _samples.IndexOf(sample); button.Click += (s, e) => _nextSampleIndex = sampleIndex; Debug.Assert(verticalPanel != null); verticalPanel.Children.Add(button); } } }