SampleSelector.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Linq;
  3. using AtomicEngine;
  4. namespace FeatureExamples
  5. {
  6. class SampleSelector : NETScriptObject
  7. {
  8. public static UIView UIView;
  9. public static Sample sampleRef = null;
  10. public SampleSelector()
  11. {
  12. sampleRef = null;
  13. var rootLayout = new UILayout();
  14. rootLayout.Axis = UI_AXIS.UI_AXIS_Y;
  15. rootLayout.Rect = UIView.Rect;
  16. UIView.AddChild(rootLayout);
  17. SubscribeToEvent<KeyDownEvent>(e =>
  18. {
  19. if (e.Key == Constants.KEY_ESCAPE)
  20. GetSubsystem<Engine>().Exit();
  21. });
  22. #if ATOMIC_DESKTOP || ATOMIC_MOBILE
  23. var sampleTypes = typeof(Sample).Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(Sample)) && t != typeof(Sample) ).ToArray();
  24. foreach (var sample in sampleTypes)
  25. {
  26. var button = new UIButton();
  27. button.Text = sample.Name;
  28. button.SubscribeToEvent<WidgetEvent>( button, e =>
  29. {
  30. // We're only interested in clicks
  31. if (e.Type != UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK)
  32. return;
  33. // set the event as handled, as the UI is about to go away
  34. e.Handled = true;
  35. // Goodbye UI
  36. UIView.RemoveChild(rootLayout);
  37. sampleRef = (Sample) Activator.CreateInstance(sample);
  38. sampleRef.Start();
  39. UnsubscribeFromEvent<KeyDownEvent>();
  40. });
  41. rootLayout.AddChild(button);
  42. }
  43. #endif
  44. }
  45. }
  46. }