Example #1
0
 public static void AddTextPathToGraphics(Graphics graphics, TitleParameters titleParameters, int imageHeight, int imageWidth, string text, int pixelsAlignment = 15)
 {
     if (graphics != null)
     {
         graphics.AddTextPath(titleParameters, imageHeight, imageWidth, text, pixelsAlignment);
     }
 }
Example #2
0
 public TitleParametersPayload(Coordinates coordinates, uint state, string title, TitleParameters titleParameters, JObject?settings = null)
 {
     Coordinates     = coordinates;
     State           = state;
     Title           = title;
     TitleParameters = titleParameters;
     Settings        = settings;
 }
Example #3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="settings"></param>
 /// <param name="coordinates"></param>
 /// <param name="state"></param>
 /// <param name="title"></param>
 /// <param name="titleParameters"></param>
 public TitleParametersPayload(JObject settings, KeyCoordinates coordinates, uint state, string title, TitleParameters titleParameters)
 {
     Settings        = settings;
     Coordinates     = coordinates;
     State           = state;
     Title           = title;
     TitleParameters = titleParameters;
 }
Example #4
0
        public async override void KeyReleased(KeyPayload payload)
        {
            TitleParameters tp = new TitleParameters(new FontFamily("Arial"), FontStyle.Bold, 20, Color.White, true, TitleVerticalAlignment.Middle);

            using (Image image = Tools.GenerateGenericKeyImage(out Graphics graphics))
            {
                graphics.FillRectangle(new SolidBrush(Color.White), 0, 0, image.Width, image.Height);
                graphics.AddTextPath(tp, image.Height, image.Width, "Test", Color.Black, 7);
                graphics.Dispose();

                await Connection.SetImageAsync(image);
            }
        }
        /// <summary>
        /// Adds a text path to an existing Graphics object. Uses TitleParameters to emulate the Text settings in the Property Inspector
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="titleParameters"></param>
        /// <param name="imageHeight"></param>
        /// <param name="imageWidth"></param>
        /// <param name="text"></param>
        /// <param name="strokeColor"></param>
        /// <param name="strokeThickness"></param>
        /// <param name="pixelsAlignment"></param>
        public static void AddTextPath(this Graphics graphics, TitleParameters titleParameters, int imageHeight, int imageWidth, string text, Color strokeColor, float strokeThickness, int pixelsAlignment = 15)
        {
            try
            {
                if (titleParameters == null)
                {
                    Logger.Instance.LogMessage(TracingLevel.ERROR, $"AddTextPath: titleParameters is null");
                    return;
                }

                Font  font  = new Font(titleParameters.FontFamily, (float)titleParameters.FontSizeInPixelsScaledToDefaultImage, titleParameters.FontStyle, GraphicsUnit.Pixel);
                Color color = titleParameters.TitleColor;
                graphics.PageUnit = GraphicsUnit.Pixel;
                float ratio       = graphics.DpiY / imageWidth;
                SizeF stringSize  = graphics.MeasureString(text, font);
                float textWidth   = stringSize.Width * (1 - ratio);
                float textHeight  = stringSize.Height * (1 - ratio);
                int   stringWidth = 0;
                if (textWidth < imageWidth)
                {
                    stringWidth = (int)(Math.Abs((imageWidth - textWidth)) / 2) - pixelsAlignment;
                }

                int stringHeight = pixelsAlignment; // Top
                if (titleParameters.VerticalAlignment == TitleVerticalAlignment.Middle)
                {
                    stringHeight = (imageHeight / 2) - pixelsAlignment;
                }
                else if (titleParameters.VerticalAlignment == TitleVerticalAlignment.Bottom)
                {
                    stringHeight = (int)(Math.Abs((imageHeight - textHeight)) - pixelsAlignment);
                }

                Pen          stroke = new Pen(strokeColor, strokeThickness);
                GraphicsPath gpath  = new GraphicsPath();
                gpath.AddString(text,
                                font.FontFamily,
                                (int)font.Style,
                                graphics.DpiY * font.SizeInPoints / imageWidth,
                                new Point(stringWidth, stringHeight),
                                new StringFormat());
                graphics.DrawPath(stroke, gpath);
                graphics.FillPath(new SolidBrush(color), gpath);
            }
            catch (Exception ex)
            {
                Logger.Instance.LogMessage(TracingLevel.ERROR, $"AddTextPath Exception {ex}");
            }
        }
        /// <summary>
        /// Adds line breaks ('\n') to the string every time the text would overflow the image
        /// </summary>
        /// <param name="str"></param>
        /// <param name="titleParameters"></param>
        /// <param name="leftPaddingPixels"></param>
        /// <param name="rightPaddingPixels"></param>
        /// <param name="imageWidthPixels"></param>
        /// <returns></returns>
        public static string WrapStringToFitImage(string str, TitleParameters titleParameters, int leftPaddingPixels = 3, int rightPaddingPixels = 3, int imageWidthPixels = 72)
        {
            try
            {
                if (titleParameters == null)
                {
                    return(str);
                }

                int           padding     = leftPaddingPixels + rightPaddingPixels;
                Font          font        = new Font(titleParameters.FontFamily, (float)titleParameters.FontSizeInPoints, titleParameters.FontStyle, GraphicsUnit.Pixel);
                StringBuilder finalString = new StringBuilder();
                StringBuilder currentLine = new StringBuilder();
                SizeF         currentLineSize;

                using (Bitmap img = new Bitmap(imageWidthPixels, imageWidthPixels))
                {
                    using (Graphics graphics = Graphics.FromImage(img))
                    {
                        for (int idx = 0; idx < str.Length; idx++)
                        {
                            currentLine.Append(str[idx]);
                            currentLineSize = graphics.MeasureString(currentLine.ToString(), font);
                            if (currentLineSize.Width <= img.Width - padding)
                            {
                                finalString.Append(str[idx]);
                            }
                            else // Overflow
                            {
                                finalString.Append("\n" + str[idx]);
                                currentLine = new StringBuilder(str[idx].ToString());
                            }
                        }
                    }
                }

                return(finalString.ToString());
            }
            catch (Exception ex)
            {
                Logger.Instance.LogMessage(TracingLevel.ERROR, $"SplitStringToFit Exception: {ex}");
                return(str);
            }
        }
Example #7
0
        /// <summary>
        /// Adds a text path to an existing Graphics object. Uses TitleParser to emulate the Text settings in the Property Inspector
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="titleParameters"></param>
        /// <param name="imageHeight"></param>
        /// <param name="imageWidth"></param>
        /// <param name="text"></param>
        /// <param name="pixelsAlignment"></param>
        public static void AddTextPathToGraphics(Graphics graphics, TitleParameters titleParameters, int imageHeight, int imageWidth, string text, int pixelsAlignment = 15)
        {
            Font  font  = new Font(titleParameters.FontFamily, (float)titleParameters.FontSizeInPixelsScaledToDefaultImage, titleParameters.FontStyle);
            Color color = titleParameters.TitleColor;

            graphics.PageUnit = GraphicsUnit.Pixel;
            float ratio       = graphics.DpiY / imageWidth;
            SizeF stringSize  = graphics.MeasureString(text, font);
            float textWidth   = stringSize.Width * (1 - ratio);
            float textHeight  = stringSize.Height * (1 - ratio);
            int   stringWidth = 0;

            if (textWidth < imageWidth)
            {
                stringWidth = (int)(Math.Abs((imageWidth - textWidth)) / 2) - pixelsAlignment;
            }

            int stringHeight = pixelsAlignment; // Top

            if (titleParameters.VerticalAlignment == TitleVerticalAlignment.Middle)
            {
                stringHeight = (imageHeight / 2) - pixelsAlignment;
            }
            else if (titleParameters.VerticalAlignment == TitleVerticalAlignment.Bottom)
            {
                stringHeight = (int)(Math.Abs((imageHeight - textHeight)) - pixelsAlignment);
            }

            GraphicsPath gpath = new GraphicsPath();

            gpath.AddString(text,
                            font.FontFamily,
                            (int)font.Style,
                            graphics.DpiY * font.SizeInPoints / imageWidth,
                            new Point(stringWidth, stringHeight),
                            new StringFormat());
            graphics.DrawPath(Pens.Black, gpath);
            graphics.FillPath(new SolidBrush(color), gpath);
        }
        internal static string StreamDeckFormat(this CultureInfo culture, TitleParameters titleParameters, int textLengthLimit)
        {
            if (culture == null)
            {
                return(String.Empty);
            }

            if (titleParameters == null)
            {
                return(culture.DisplayName);
            }

            // Truncate size
            string displayName = culture.DisplayName ?? String.Empty;

            if (textLengthLimit > 0)
            {
                displayName = displayName.Truncate(textLengthLimit);
            }
            string[] parts = displayName.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            if (parts.Length == 1)
            {
                return(Tools.SplitStringToFit(parts[0], titleParameters));
            }
            else if (parts.Length == 2)
            {
                return($"{parts[0]}\n{parts[1]}");
            }
            else if (parts.Length > 2)
            {
                return($"{parts[0]}\n{Tools.SplitStringToFit($"{parts[1]} {parts[2]}", titleParameters)}");
            }

            return(String.Empty);
        }
Example #9
0
 private void Connection_OnTitleParametersDidChange(object sender, SDEventReceivedEventArgs <SdTools.Events.TitleParametersDidChange> e)
 {
     titleParameters = e?.Event?.Payload?.TitleParameters;
 }
 private void Connection_OnTitleParametersDidChange(object sender, BarRaider.SdTools.Wrappers.SDEventReceivedEventArgs <BarRaider.SdTools.Events.TitleParametersDidChange> e)
 {
     titleParameters = e?.Event?.Payload?.TitleParameters;
     lastKeyDraw     = DateTime.MinValue;
 }
 /// <summary>
 /// Adds a text path to an existing Graphics object. Uses TitleParameters to emulate the Text settings in the Property Inspector
 /// </summary>
 /// <param name="graphics"></param>
 /// <param name="titleParameters"></param>
 /// <param name="imageHeight"></param>
 /// <param name="imageWidth"></param>
 /// <param name="text"></param>
 /// <param name="pixelsAlignment"></param>
 public static void AddTextPath(this Graphics graphics, TitleParameters titleParameters, int imageHeight, int imageWidth, string text, int pixelsAlignment = 15)
 {
     AddTextPath(graphics, titleParameters, imageHeight, imageWidth, text, Color.Black, 1, pixelsAlignment);
 }
Example #12
0
 /// <summary>
 /// Called when the user changes the title or title parameters of the instance of an action.
 /// </summary>
 /// <param name="title">The new title.</param>
 /// <param name="parameters">A object describing the new title parameters.</param>
 public virtual Task TitleParametersDidChangeAsync(string title, TitleParameters parameters) => Task.CompletedTask;
Example #13
0
 public override Task TitleParametersDidChangeAsync(string title, TitleParameters parameters) => ShowOkAsync();
 private void Connection_OnTitleParametersDidChange(object sender, SDEventReceivedEventArgs <BarRaider.SdTools.Events.TitleParametersDidChange> e)
 {
     titleParameters = e.Event?.Payload?.TitleParameters;
     SetPathTitle();
 }