public async Task <string> SelectButtonAsync(string[] buttonTexts) { string result = ""; var tcs = new TaskCompletionSource <object>(); RoutedEventHandler lambda = async(s, e) => { Button source = (Button)e.OriginalSource; result = (string)source.Content; await FadeElements.FadeElementOpacityAsync(this, 1, 0, new TimeSpan(0, 0, 0, 0, 50)); tcs.TrySetResult(null); }; try { // First time we are called we need to make the buttons int buttonCount = 0; foreach (Button b in this.Children) { if (buttonCount < buttonTexts.Length) { // got text for this button b.Content = buttonTexts[buttonCount]; b.Visibility = Visibility.Visible; b.Click += lambda; } else { b.Visibility = Visibility.Collapsed; } buttonCount++; } await FadeElements.FadeElementOpacityAsync(this, 0, 1, new TimeSpan(0, 0, 0, 0, 50)); await tcs.Task; } finally { int buttonCount = 0; foreach (Button b in this.Children) { if (buttonCount < buttonTexts.Length) { b.Visibility = Visibility.Collapsed; b.Click -= lambda; } buttonCount++; } } return(result); }
public async Task <string> ReadStringAsync(string prompt) { string result = ""; var tcs = new TaskCompletionSource <object>(); RoutedEventHandler clickLambda = async(s, e) => { result = TextInputTextBox.Text; await FadeElements.FadeElementOpacityAsync(this, 1, 0, new TimeSpan(0, 0, 0, 0, 100)); this.Visibility = Visibility.Collapsed; tcs.TrySetResult(null); }; KeyEventHandler keyDownLambda = async(s, e) => { if (e.Key == Windows.System.VirtualKey.Enter) { result = TextInputTextBox.Text; await FadeElements.FadeElementOpacityAsync(this, 1, 0, new TimeSpan(0, 0, 0, 0, 100)); this.Visibility = Visibility.Collapsed; tcs.TrySetResult(null); } }; try { TextInputTextBox.Text = ""; InputPromptTextBlock.Text = prompt; this.Visibility = Visibility.Visible; await FadeElements.FadeElementOpacityAsync(this, 0, 1, new TimeSpan(0, 0, 1)); ReadTextButton.Click += clickLambda; TextInputTextBox.KeyDown += keyDownLambda; TextInputTextBox.Focus(FocusState.Keyboard); await tcs.Task; } finally { ReadTextButton.Click -= clickLambda; TextInputTextBox.KeyDown -= keyDownLambda; await FadeElements.FadeElementOpacityAsync(this, 1, 0, new TimeSpan(0, 0, 1)); this.Visibility = Visibility.Collapsed; } return(result); }
public LightPanel(SnapsManager manager, double width, double height, int xCells, int yCells) { this.manager = manager; this.Width = width; this.Height = height; if (xCells > 32 || yCells > 32) { throw new Exception("Maximum panel dimension exceeded. No more than 32 panels in each direction"); } AutoResetEvent lightPanelBuiltEvent = new AutoResetEvent(false); manager.InvokeOnUIThread( async() => { if (this.Opacity != 1) { await FadeElements.FadeElementOpacityAsync(this, 0, 1, new TimeSpan(0, 0, 1)); } clearLightPanel(); lightPanels = new Rectangle[xCells, yCells]; float panelWidth = (float)this.ActualWidth / xCells; float panelHeight = (float)this.ActualHeight / yCells; for (int x = 0; x < xCells; x++) { for (int y = 0; y < yCells; y++) { Rectangle p = new Rectangle(); p.Fill = new SolidColorBrush(Colors.White); p.Stroke = new SolidColorBrush(Colors.Black); p.Width = panelWidth; p.Height = panelHeight; Canvas.SetLeft(p, x * panelWidth); Canvas.SetTop(p, y * panelHeight); lightPanels[x, y] = p; this.Children.Add(p); } } lightPanelBuiltEvent.Set(); }); lightPanelBuiltEvent.WaitOne(); }
void clearDisplay() { // first fade out the display AutoResetEvent fadeCompleteEvent = new AutoResetEvent(false); InvokeOnUIThread( async() => { if (DisplayGrid.Opacity == 1) { await FadeElements.FadeElementOpacityAsync(DisplayGrid, 1, 0, new TimeSpan(0, 0, 0, 0, 200)); } fadeCompleteEvent.Set(); } ); fadeCompleteEvent.WaitOne(); ClearGameEngine(); InvokeOnUIThread( async() => { // clear the background image this.graphicsCanvas.BackgroundImage.Source = null; // clear the title string titleTextBlock.Text = ""; titleTextBlock.Foreground = originalTitleBrush; foreach (IClearableDisplayElement element in clearableComponents) { element.Clear(); } if (DisplayGrid.Opacity == 0) { await FadeElements.FadeElementOpacityAsync(DisplayGrid, 0, 1, new TimeSpan(0, 0, 0, 0, 200)); } fadeCompleteEvent.Set(); } ); fadeCompleteEvent.WaitOne(); }
public bool LoadGraphicsPNGImageFromLocalStore(string filename, Image graphicsImage) { AutoResetEvent DisplayComplete = new AutoResetEvent(false); bool displayedOK = true; InvokeOnUIThread( async() => { try { if (graphicsImage.Opacity == 1) { await FadeElements.FadeElementOpacityAsync(graphicsImage, 1, 0, new TimeSpan(0, 0, 1)); } var file = await ApplicationData.Current.LocalFolder.GetFileAsync(filename); var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage(); await bitmapImage.SetSourceAsync(stream); graphicsImage.Source = bitmapImage; graphicsImage.Width = graphicsCanvas.ActualWidth; graphicsImage.Height = graphicsCanvas.ActualHeight; } catch { displayedOK = false; graphicsImage.Source = null; } finally { await FadeElements.FadeElementOpacityAsync(graphicsImage, 0, 1, new TimeSpan(0, 0, 1)); } DisplayComplete.Set(); }); DisplayComplete.WaitOne(); return(displayedOK); }
public void SetTitleString(string title) { AutoResetEvent gotTitleStringEvent = new AutoResetEvent(false); InvokeOnUIThread( async() => { if (titleTextBlock.Opacity == 1) { await FadeElements.FadeElementOpacityAsync(titleTextBlock, 1, 0, new TimeSpan(0, 0, 0, 0, 200)); } titleTextBlock.Text = title; await FadeElements.FadeElementOpacityAsync(titleTextBlock, 0, 1, new TimeSpan(0, 0, 0, 0, 200)); gotTitleStringEvent.Set(); } ); gotTitleStringEvent.WaitOne(); }
public void ClearGraphics() { AutoResetEvent DisplayComplete = new AutoResetEvent(false); manager.InvokeOnUIThread( async() => { if (this.Opacity == 1) { await FadeElements.FadeElementOpacityAsync(this, 1, 0, new TimeSpan(0, 0, 1)); } Clear(); await FadeElements.FadeElementOpacityAsync(this, 0, 1, new TimeSpan(0, 0, 0, 0, 10)); DisplayComplete.Set(); } ); DisplayComplete.WaitOne(); }
public bool DoDisplayFileImage(StorageFile file, Image graphicsImage) { bool result = true; AutoResetEvent DisplayComplete = new AutoResetEvent(false); InvokeOnUIThread( async() => { if (graphicsImage.Opacity == 1) { await FadeElements.FadeElementOpacityAsync(graphicsImage, 1, 0, new TimeSpan(0, 0, 1)); } try { var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage(); await bitmapImage.SetSourceAsync(stream); graphicsImage.Source = bitmapImage; graphicsImage.Width = graphicsCanvas.ActualWidth; graphicsImage.Height = graphicsCanvas.ActualHeight; await FadeElements.FadeElementOpacityAsync(graphicsImage, 0, 1, new TimeSpan(0, 0, 1)); } catch { result = false; } finally { DisplayComplete.Set(); } }); DisplayComplete.WaitOne(); return(result); }
public async void RunSnapsApp(string requestedAppName) { MethodInfo selectedDemoMethod; string simpleAppName; if (requestedAppName.Contains(",")) { // fully qualified type name - just display the name of the class simpleAppName = requestedAppName.Remove(requestedAppName.IndexOf(',')); } else { simpleAppName = requestedAppName; } selectedDemoMethod = getMethod(requestedAppName, "StartProgram"); if (selectedDemoMethod == null) { DisplayDialog("App " + simpleAppName + " not found"); await InvokeOnUIThread( async() => { controlPanel.Visibility = Windows.UI.Xaml.Visibility.Collapsed; await FadeElements.FadeElementOpacityAsync(controlPanel, 0, 1, new TimeSpan(0, 0, 1)); } ); } else { clearDisplay(); DisplayString("Running: " + simpleAppName); Delay(1); // Make this the active method so that it runs if we select run again activeMethod = selectedDemoMethod; StartProgram(selectedDemoMethod); } }
async void StartProgram(MethodInfo methodToStart) { programTask = Task.Factory.StartNew( async() => { clearDisplay(); if (methodToStart == null) { DisplayDialog("No StartProgram method found."); } else { Object instance = Activator.CreateInstance(methodToStart.DeclaringType); methodToStart.Invoke(instance, null); } await InvokeOnUIThread( async() => { if (joyPad.Visibility == Windows.UI.Xaml.Visibility.Visible) { await FadeElements.FadeElementOpacityAsync(joyPad, 1, 0, new TimeSpan(0, 0, 1)); joyPad.Visibility = Windows.UI.Xaml.Visibility.Collapsed; joyPad.Opacity = 1; } if (SnapsEngine.DisplayControMenuAtProgramEnd) { controlPanel.Visibility = Windows.UI.Xaml.Visibility.Visible; await FadeElements.FadeElementOpacityAsync(controlPanel, 0, 1, new TimeSpan(0, 0, 1)); } } ); }, programCancellationSource.Token ); }
public void DoDisplayString(string message, SnapsTextAlignment alignment, SnapsFadeType fadeType) { AutoResetEvent displayDone = new AutoResetEvent(false); if (fadeType == SnapsFadeType.nofade) { // No fade - just display the new txt manager.InvokeOnUIThread( () => { displayTextBlock.Text = message; displayTextBlock.FontSize = fontSize; switch (alignment) { case SnapsTextAlignment.centre: displayTextBlock.TextAlignment = TextAlignment.Center; break; case SnapsTextAlignment.left: displayTextBlock.TextAlignment = TextAlignment.Left; break; case SnapsTextAlignment.right: displayTextBlock.TextAlignment = TextAlignment.Right; break; case SnapsTextAlignment.justify: displayTextBlock.TextAlignment = TextAlignment.Justify; break; } displayDone.Set(); } ); } else { TimeSpan fadeSpeed; if (fadeType == SnapsFadeType.fast) { fadeSpeed = new TimeSpan(0, 0, 0, 0, 200); } else { fadeSpeed = new TimeSpan(0, 0, 0, 0, 600); } manager.InvokeOnUIThread( async() => { if (displayTextBlock.Opacity == 1) { await FadeElements.FadeElementOpacityAsync(displayTextBlock, 1, 0, new TimeSpan(0, 0, 0, 0, 200)); } displayTextBlock.Text = message; displayTextBlock.FontSize = fontSize; switch (alignment) { case SnapsTextAlignment.centre: displayTextBlock.TextAlignment = TextAlignment.Center; break; case SnapsTextAlignment.left: displayTextBlock.TextAlignment = TextAlignment.Left; break; case SnapsTextAlignment.right: displayTextBlock.TextAlignment = TextAlignment.Right; break; case SnapsTextAlignment.justify: displayTextBlock.TextAlignment = TextAlignment.Justify; break; } await FadeElements.FadeElementOpacityAsync(displayTextBlock, 0, 1, new TimeSpan(0, 0, 0, 0, 200)); displayDone.Set(); } ); } displayDone.WaitOne(); }
public void MakeLightPanel(int xCells, int yCells) { StringBuilder error = new StringBuilder(); if (xCells < 1) { error.AppendLine("xCells value must be greater than 0"); } else { if (xCells > maxPanelSize) { error.AppendLine("xCells value must be less than " + maxPanelSize); } } if (yCells < 1) { error.AppendLine("yCells value must be greater than 0"); } else { if (yCells > maxPanelSize) { error.AppendLine("yCells value must be less than " + maxPanelSize); } } string errorString = error.ToString(); if (errorString.Length > 0) { throw new Exception(errorString); } AutoResetEvent lightPanelBuiltEvent = new AutoResetEvent(false); manager.InvokeOnUIThread( async() => { if (this.Opacity != 1) { await FadeElements.FadeElementOpacityAsync(this, 0, 1, new TimeSpan(0, 0, 1)); } if (LightPanel != null) { // remove the previous one for (int x = 0; x < LightPanel.GetLength(0); x++) { for (int y = 0; y < LightPanel.GetLength(1); y++) { Rectangle r = LightPanel[x, y]; this.Children.Remove(r); } } } LightPanel = new Rectangle[xCells, yCells]; float panelWidth = (float)this.ActualWidth / xCells; float panelHeight = (float)this.ActualHeight / yCells; for (int x = 0; x < xCells; x++) { for (int y = 0; y < yCells; y++) { Rectangle p = new Rectangle(); p.Fill = new SolidColorBrush(Colors.White); p.Stroke = new SolidColorBrush(Colors.Black); p.Width = panelWidth; p.Height = panelHeight; Canvas.SetLeft(p, x * panelWidth); Canvas.SetTop(p, y * panelHeight); LightPanel[x, y] = p; this.Children.Add(p); } } lightPanelBuiltEvent.Set(); }); lightPanelBuiltEvent.WaitOne(); }
public bool DoDisplayUrlImage(Uri imageURI, Image graphicsImage) { AutoResetEvent DisplayComplete = new AutoResetEvent(false); AutoResetEvent loadCompletedEvent = new AutoResetEvent(false); BitmapImage result = null; bool failed = false; var tcs = new TaskCompletionSource <object>(); RoutedEventHandler endedLambda = (s, e) => tcs.TrySetResult(null); ExceptionRoutedEventHandler endedFailed = (s, e) => { failed = true; tcs.TrySetResult(null); }; InvokeOnUIThread( async() => { if (graphicsImage.Opacity == 1) { await FadeElements.FadeElementOpacityAsync(graphicsImage, 1, 0, new TimeSpan(0, 0, 1)); } try { result = new BitmapImage(); result.ImageFailed += endedFailed; result.ImageOpened += endedLambda; try { result.UriSource = imageURI; graphicsImage.Width = graphicsCanvas.ActualWidth; graphicsImage.Height = graphicsCanvas.ActualHeight; graphicsImage.Source = result; await tcs.Task; } catch { failed = true; } if (failed) { imageURI = new Uri("ms-appx:///Images/ImageNotFound.png"); result.UriSource = imageURI; graphicsImage.Source = result; await tcs.Task; } } finally { result.ImageFailed -= endedFailed; result.ImageOpened -= endedLambda; } await FadeElements.FadeElementOpacityAsync(graphicsImage, 0, 1, new TimeSpan(0, 0, 1)); DisplayComplete.Set(); }); DisplayComplete.WaitOne(); return(!failed); }
public bool DoDisplayUrlImage(string imageURL, Image graphicsImage, int width, int height) { AutoResetEvent DisplayComplete = new AutoResetEvent(false); AutoResetEvent loadCompletedEvent = new AutoResetEvent(false); BitmapImage result = null; bool failed = false; var tcs = new TaskCompletionSource <object>(); RoutedEventHandler endedLambda = (s, e) => tcs.TrySetResult(null); ExceptionRoutedEventHandler endedFailed = (s, e) => { failed = true; tcs.TrySetResult(null); }; InvokeOnUIThread( async() => { if (graphicsImage.Opacity == 1) { await FadeElements.FadeElementOpacityAsync(graphicsImage, 1, 0, new TimeSpan(0, 0, 1)); } try { Uri imageURI = new Uri(imageURL, UriKind.RelativeOrAbsolute); result = new BitmapImage(); result.ImageFailed += endedFailed; result.ImageOpened += endedLambda; result.UriSource = imageURI; graphicsImage.Source = result; await tcs.Task; if (failed) { imageURI = new Uri("ms-appx:///Images/ImageNotFound.png"); result.UriSource = imageURI; graphicsImage.Source = result; await tcs.Task; } } finally { result.ImageFailed -= endedFailed; result.ImageOpened -= endedLambda; } graphicsImage.Width = width; graphicsImage.Height = height; graphicsImage.HorizontalAlignment = HorizontalAlignment.Stretch; await FadeElements.FadeElementOpacityAsync(graphicsImage, 0, 1, new TimeSpan(0, 0, 1)); DisplayComplete.Set(); }); DisplayComplete.WaitOne(); return(!failed); }