private void ComposeElement(GuiComposer composer, JsonDialogSettings settings, DialogElement elem, int elemKey, double x, double y) { double factor = settings.SizeMultiplier; double labelWidth = 0; if (elem.Label != null) { CairoFont font = CairoFont.WhiteSmallText(); font.UnscaledFontsize *= factor; TextExtents extents = font.GetTextExtents(elem.Label); labelWidth = extents.Width / factor / RuntimeEnv.GUIScale + 1; FontExtents fext = font.GetFontExtents(); ElementBounds labelBounds = ElementBounds.Fixed(x, y + Math.Max(0, (elem.Height * factor - fext.Height / RuntimeEnv.GUIScale) / 2), labelWidth, elem.Height).WithScale(factor); composer.AddStaticText(elem.Label, font, labelBounds); labelWidth += 8; if (elem.Tooltip != null) { CairoFont tfont = CairoFont.WhiteSmallText(); tfont.UnscaledFontsize *= factor; composer.AddHoverText(elem.Tooltip, tfont, 350, labelBounds.FlatCopy(), "tooltip-" + elem.Code); composer.GetHoverText("tooltip-" + elem.Code).SetAutoWidth(true); } } ElementBounds bounds = ElementBounds.Fixed(x + labelWidth, y, elem.Width - labelWidth, elem.Height).WithScale(factor); string currentValue = settings.OnGet?.Invoke(elem.Code); switch (elem.Type) { case EnumDialogElementType.Slider: { string key = "slider-" + elemKey; composer.AddSlider((newval) => { settings.OnSet?.Invoke(elem.Code, newval + ""); return(true); }, bounds, key); int curVal = 0; int.TryParse(currentValue, out curVal); composer.GetSlider(key).SetValues(curVal, elem.MinValue, elem.MaxValue, elem.Step); composer.GetSlider(key).Scale = factor; //composer.GetSlider(key).TriggerOnlyOnMouseUp(true); break; } case EnumDialogElementType.Switch: { string key = "switch-" + elemKey; composer.AddSwitch((newval) => { settings.OnSet?.Invoke(elem.Code, newval ? "1" : "0"); }, bounds, key, 30 * factor, 5 * factor); composer.GetSwitch(key).SetValue(currentValue == "1"); } break; case EnumDialogElementType.Input: { string key = "input-" + elemKey; CairoFont font = CairoFont.WhiteSmallText(); font.UnscaledFontsize *= factor; composer.AddTextInput(bounds, (newval) => { settings.OnSet?.Invoke(elem.Code, newval); }, font, key); composer.GetTextInput(key).SetValue(currentValue); break; } case EnumDialogElementType.NumberInput: { string key = "numberinput-" + elemKey; CairoFont font = CairoFont.WhiteSmallText(); font.UnscaledFontsize *= factor; composer.AddNumberInput(bounds, (newval) => { settings.OnSet?.Invoke(elem.Code, newval); }, font, key); composer.GetNumberInput(key).SetValue(currentValue); break; } case EnumDialogElementType.Button: if (elem.Icon != null) { composer.AddIconButton(elem.Icon, (val) => { settings.OnSet?.Invoke(elem.Code, null); }, bounds); } else { CairoFont font = CairoFont.ButtonText(); font.WithFontSize(elem.FontSize); composer.AddButton(elem.Text, () => { settings.OnSet?.Invoke(elem.Code, null); return(true); }, bounds.WithFixedPadding(8, 0), font); } if (elem.Tooltip != null && elem.Label == null) { CairoFont tfont = CairoFont.WhiteSmallText(); tfont.UnscaledFontsize *= factor; composer.AddHoverText(elem.Tooltip, tfont, 350, bounds.FlatCopy(), "tooltip-" + elem.Code); composer.GetHoverText("tooltip-" + elem.Code).SetAutoWidth(true); } break; case EnumDialogElementType.Text: composer.AddStaticText(elem.Text, CairoFont.WhiteMediumText().WithFontSize(elem.FontSize), bounds); break; case EnumDialogElementType.Select: case EnumDialogElementType.DynamicSelect: { string[] values = elem.Values; string[] names = elem.Names; if (elem.Type == EnumDialogElementType.DynamicSelect) { string[] compos = currentValue.Split(new string[] { "\n" }, StringSplitOptions.None); values = compos[0].Split(new string[] { "||" }, StringSplitOptions.None); names = compos[1].Split(new string[] { "||" }, StringSplitOptions.None); currentValue = compos[2]; } int selectedIndex = Array.FindIndex(values, w => w.Equals(currentValue)); if (elem.Mode == EnumDialogElementMode.DropDown) { string key = "dropdown-" + elemKey; composer.AddDropDown(values, names, selectedIndex, (newval, on) => { settings.OnSet?.Invoke(elem.Code, newval); }, bounds, key); composer.GetDropDown(key).Scale = factor; composer.GetDropDown(key).Font.UnscaledFontsize *= factor; } else { if (elem.Icons != null && elem.Icons.Length > 0) { ElementBounds[] manybounds = new ElementBounds[elem.Icons.Length]; double elemHeight = (elem.Height - 4 * elem.Icons.Length) / elem.Icons.Length; for (int i = 0; i < manybounds.Length; i++) { manybounds[i] = bounds.FlatCopy().WithFixedHeight(elemHeight - 4).WithFixedOffset(0, i * (4 + elemHeight)).WithScale(factor); } string key = "togglebuttons-" + elemKey; CairoFont font = CairoFont.WhiteSmallText(); font.UnscaledFontsize *= factor; composer.AddIconToggleButtons(elem.Icons, font, (newval) => { settings.OnSet?.Invoke(elem.Code, elem.Values[newval]); }, manybounds, key); if (currentValue != null && currentValue.Length > 0) { composer.ToggleButtonsSetValue(key, selectedIndex); } if (elem.Tooltips != null) { for (int i = 0; i < elem.Tooltips.Length; i++) { CairoFont tfont = CairoFont.WhiteSmallText(); tfont.UnscaledFontsize *= factor; composer.AddHoverText(elem.Tooltips[i], tfont, 350, manybounds[i].FlatCopy()); } } } } } break; } elementNumber++; }
/// <summary> /// Gets the height of the font to calculate the height of the line. /// </summary> /// <param name="font">The font to calculate from.</param> /// <returns>The height of the line.</returns> public double GetLineHeight(CairoFont font) { return(font.GetFontExtents().Height *font.LineHeightMultiplier); }
/// <summary> /// Takes a string of text and applies a texture to it. /// </summary> /// <param name="text">The text to texture.</param> /// <param name="font">The font of the text.</param> /// <param name="width">The width of the text.</param> /// <param name="height">The height of the text.</param> /// <param name="background">The background of the text. (default: none/null)</param> /// <returns>The texturized text.</returns> public LoadedTexture GenTextTexture(string text, CairoFont font, int width, int height, TextBackground background = null) { if (background == null) { background = defaultBackground; } ImageSurface surface = new ImageSurface(Format.Argb32, width, height); Context ctx = new Context(surface); if (background?.FillColor != null) { ctx.SetSourceRGBA(background.FillColor); GuiElement.RoundRectangle(ctx, 0, 0, width, height, background.Radius); ctx.Fill(); } if (background?.Shade == true) { ctx.SetSourceRGBA(GuiStyle.DialogLightBgColor[0] * 1.4, GuiStyle.DialogStrongBgColor[1] * 1.4, GuiStyle.DialogStrongBgColor[2] * 1.4, 1); ctx.LineWidth = 5;// background.BorderWidth * 1.75; GuiElement.RoundRectangle(ctx, 0, 0, width, height, background.Radius); ctx.StrokePreserve(); surface.Blur(6.2); ctx.SetSourceRGBA(new double[] { 45 / 255.0, 35 / 255.0, 33 / 255.0, 1 }); ctx.LineWidth = background.BorderWidth; ctx.Stroke(); } if (background?.BorderColor != null) { ctx.SetSourceRGBA(background.BorderColor); GuiElement.RoundRectangle(ctx, 0, 0, width, height, background.Radius); ctx.LineWidth = background.BorderWidth; ctx.Stroke(); } font.SetupContext(ctx); double fontHeight = font.GetFontExtents().Height; string[] lines = text.Split('\n'); for (int i = 0; i < lines.Length; i++) { lines[i] = lines[i].TrimEnd(); ctx.MoveTo(background.Padding, background.Padding + ctx.FontExtents.Ascent + i * fontHeight); if (font.StrokeWidth > 0) { ctx.TextPath(lines[i]); ctx.LineWidth = font.StrokeWidth; ctx.SetSourceRGBA(font.StrokeColor); ctx.StrokePreserve(); ctx.SetSourceRGBA(font.Color); ctx.Fill(); } else { ctx.ShowText(lines[i]); if (font.RenderTwice) { ctx.ShowText(lines[i]); } } } int textureId = capi.Gui.LoadCairoTexture(surface, true); surface.Dispose(); ctx.Dispose(); return(new LoadedTexture(capi) { TextureId = textureId, Width = width, Height = height }); }