Exemplo n.º 1
0
        /// <summary>
        /// Adds multiple buttons with Text.
        /// </summary>
        /// <param name="texts">The texts on all the buttons.</param>
        /// <param name="font">The font for the buttons</param>
        /// <param name="onToggle">The event fired when the button is pressed.</param>
        /// <param name="bounds">The bounds of the buttons.</param>
        /// <param name="key">The key given to the bundle of buttons.</param>
        public static GuiComposer AddColorListPicker(this GuiComposer composer, int[] colors, Action <int> onToggle, ElementBounds startBounds, int maxLineWidth, string key = null)
        {
            if (!composer.Composed)
            {
                if (key == null)
                {
                    key = "colorlistpicker";
                }

                int    quantityButtons = colors.Length;
                double lineWidth       = 0;

                for (int i = 0; i < colors.Length; i++)
                {
                    int index = i;

                    if (lineWidth > maxLineWidth)
                    {
                        startBounds.fixedX -= lineWidth;
                        startBounds.fixedY += startBounds.fixedHeight + 5;
                        lineWidth           = 0;
                    }

                    composer.AddInteractiveElement(
                        new GuiElementColorListPicker(composer.Api, colors[i], (on) => {
                        if (on)
                        {
                            onToggle(index);
                            for (int j = 0; j < quantityButtons; j++)
                            {
                                if (j == index)
                                {
                                    continue;
                                }
                                composer.GetColorListPicker(key + "-" + j).SetValue(false);
                            }
                        }
                        else
                        {
                            composer.GetColorListPicker(key + "-" + index).SetValue(true);
                        }
                    }, startBounds.FlatCopy()),
                        key + "-" + i
                        );

                    startBounds.fixedX += startBounds.fixedWidth + 5;

                    lineWidth += startBounds.fixedWidth + 5;
                }
            }
            return(composer);
        }
Exemplo n.º 2
0
        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++;
        }