private async Task HandleActionRequest(UIActionSettings actionRequest) { await actionLock.WaitAsync(); try { switch (actionRequest.Action) { case UIActions.DrawTitle: await Connection.SetImageAsync((string)null); await Connection.SetTitleAsync(actionRequest.Title); break; case UIActions.DrawImage: await Connection.SetTitleAsync((string)null); await DrawImage(actionRequest); break; } } catch (Exception ex) { Logger.Instance.LogMessage(TracingLevel.ERROR, $"DisplayAction HandleActionRequest Exception: {ex}"); } finally { actionLock.Release(); } }
private async void Instance_UIActionEvent(object sender, Wrappers.UIActionEventArgs e) { try { UIActionSettings action = null; if (e.AllKeysAction) // If event is marked for "All Keys", get the first one { action = e.Settings[0]; } else { // Try and find an action in the list that is for this specific coordinates action = e.Settings.Where(actn => actn.Coordinates.IsCoordinatesSame(coordinates)).FirstOrDefault(); } if (action != null) { await HandleActionRequest(action); } } catch (Exception ex) { Logger.Instance.LogMessage(TracingLevel.ERROR, $"UIActionEvent Exception: {ex}"); } }
private async Task DrawImage(UIActionSettings actionRequest) { //Logger.Instance.LogMessage(TracingLevel.INFO, $"Row {coordinates.Row} Column {coordinates.Column} Color {(actionRequest.BackgroundColor.HasValue ? actionRequest.BackgroundColor.Value.ToString() : "")} Title {actionRequest.Title ?? ""}"); using (var img = await CreateImage(actionRequest)) { await Connection.SetImageAsync(img); } }
private void DrawPrevKey() { var action = new UIActionSettings() { Coordinates = PREV_KEY_LOCATION, Action = UIActions.DrawImage, Image = prefectchedImages[PREV_IMAGE], BackgroundColor = Color.Black }; UIManager.Instance.SendUIAction(action); }
private async Task <Bitmap> CreateImage(UIActionSettings actionRequest) { Bitmap img = Tools.GenerateGenericKeyImage(out Graphics graphics); if (img == null) { Logger.Instance.LogMessage(TracingLevel.ERROR, $"DrawImage failed, GenerateGenericKeyImage returned null"); return(null); } int height = img.Height; int width = img.Width; // If there is an image, draw it, otherwise, check for background if (actionRequest.Image != null) { var scaledImage = ScaleImage(await CloneImage(actionRequest.Image), new Size(width, height)); if (scaledImage != null) { graphics.DrawImage(scaledImage, new Point(0, 0)); } } else { // Background var bgBrush = new SolidBrush(actionRequest.BackgroundColor ?? Color.Black); graphics.FillRectangle(bgBrush, 0, 0, width, height); } // If a FontAwesome image is requested, draw it in the center if (actionRequest.FontAwesomeIcon.HasValue) { Image icon; // Special handling for Mute Icon if (actionRequest.FontAwesomeIcon == IconChar.VolumeMute) { icon = Image.FromFile(MUTE_ICON_PATH); } else { icon = actionRequest.FontAwesomeIcon.Value.ToBitmap(Color.Red, ICON_SIZE_PIXELS); } float iconWidth = (width - icon.Width) / 2; float iconHeight = (height - icon.Height) / 2; PointF iconStart = new PointF(iconWidth, iconHeight); graphics.DrawImage(icon, iconStart); icon.Dispose(); } // Draw text title if needed if (!String.IsNullOrEmpty(actionRequest.Title)) { using (var font = new Font("Verdana", 24, FontStyle.Bold, GraphicsUnit.Pixel)) { var fgBrush = Brushes.White; string[] titleLines = actionRequest.Title.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Reverse().ToArray(); SizeF stringSize = graphics.MeasureString(titleLines[0], font); float stringHeight = Math.Abs((height - stringSize.Height - 3)); foreach (string line in titleLines) { float textCenter = graphics.GetTextCenter(line, img.Width, font, out _); float newPosition = graphics.DrawAndMeasureString(line, font, fgBrush, new PointF(textCenter, stringHeight)); stringHeight -= (newPosition - stringHeight); } } } return(img); }
public void SendUIAction(UIActionSettings actionSettings) { UIActionEventArgs action = new UIActionEventArgs(new UIActionSettings[] { actionSettings }, false); UIActionEvent?.Invoke(this, action); }
public void SetAllKeys(UIActionSettings actionSettings) { UIActionEventArgs action = new UIActionEventArgs(new UIActionSettings[] { actionSettings }, true); UIActionEvent?.Invoke(this, action); }