/// <summary> /// Builds a row selection prefab object for this combo box. /// </summary> /// <param name="style">The text style for the entries.</param> /// <param name="entryColor">The color for the entry backgrounds.</param> /// <returns>A template for each row in the dropdown.</returns> private GameObject BuildRowPrefab(TextStyleSetting style, ColorStyleSetting entryColor) { var im = ItemMargin; var rowPrefab = PUIElements.CreateUI(null, "RowEntry"); // Background of the entry var bgImage = rowPrefab.AddComponent <KImage>(); UIDetours.COLOR_STYLE_SETTING.Set(bgImage, entryColor); UIDetours.APPLY_COLOR_STYLE.Invoke(bgImage); // Checkmark for the front of the entry var isSelected = PUIElements.CreateUI(rowPrefab, "Selected"); var fgImage = isSelected.AddComponent <Image>(); fgImage.color = style.textColor; fgImage.preserveAspect = true; fgImage.sprite = PUITuning.Images.Checked; // Button for the entry to select it var entryButton = rowPrefab.AddComponent <KButton>(); PButton.SetupButton(entryButton, bgImage); UIDetours.FG_IMAGE.Set(entryButton, fgImage); // Tooltip for the entry rowPrefab.AddComponent <ToolTip>(); // Text for the entry var textContainer = PUIElements.CreateUI(rowPrefab, "Text"); PUIElements.AddLocText(textContainer, style).SetText(" "); // Configure the entire layout in 1 statement! (jk this is awful) var group = rowPrefab.AddComponent <RelativeLayoutGroup>(); group.AnchorYAxis(isSelected).OverrideSize(isSelected, CheckSize).SetLeftEdge( isSelected, fraction: 0.0f).SetMargin(isSelected, im).AnchorYAxis( textContainer).SetLeftEdge(textContainer, toRight: isSelected).SetRightEdge( textContainer, 1.0f).SetMargin(textContainer, new RectOffset(0, im.right, im.top, im.bottom)).LockLayout(); rowPrefab.SetActive(false); return(rowPrefab); }
public GameObject Build() { var combo = PUIElements.CreateUI(null, Name); var style = TextStyle ?? PUITuning.Fonts.UILightStyle; var entryColor = EntryColor ?? PUITuning.Colors.ButtonBlueStyle; RectOffset margin = Margin, im = ItemMargin; // Background color var bgImage = combo.AddComponent <KImage>(); var backColorStyle = BackColor ?? PUITuning.Colors.ButtonBlueStyle; UIDetours.COLOR_STYLE_SETTING.Set(bgImage, backColorStyle); PButton.SetupButtonBackground(bgImage); // Need a LocText (selected item) var selection = PUIElements.CreateUI(combo, "SelectedItem"); if (MinWidth > 0) { selection.SetMinUISize(new Vector2(MinWidth, 0.0f)); } var selectedLabel = PUIElements.AddLocText(selection, style); // Vertical flow panel with the choices var contentContainer = PUIElements.CreateUI(null, "Content"); contentContainer.AddComponent <VerticalLayoutGroup>().childForceExpandWidth = true; // Scroll pane with items is laid out below everything else var pullDown = new PScrollPane("PullDown") { ScrollHorizontal = false, ScrollVertical = true, AlwaysShowVertical = true, FlexSize = Vector2.right, TrackSize = 8.0f, BackColor = entryColor. inactiveColor }.BuildScrollPane(combo, contentContainer); // Add a black border (Does not work, covered by the combo box buttons...) #if false var pdImage = pullDown.GetComponent <Image>(); pdImage.sprite = PUITuning.Images.BoxBorder; pdImage.type = Image.Type.Sliced; #endif pullDown.rectTransform().pivot = new Vector2(0.5f, 1.0f); // Initialize the drop down var comboBox = combo.AddComponent <PComboBoxComponent>(); comboBox.CheckColor = style.textColor; comboBox.ContentContainer = contentContainer.rectTransform(); comboBox.EntryPrefab = BuildRowPrefab(style, entryColor); comboBox.MaxRowsShown = MaxRowsShown; comboBox.Pulldown = pullDown; comboBox.SelectedLabel = selectedLabel; comboBox.SetItems(Content); comboBox.SetSelectedItem(InitialItem); comboBox.OnSelectionChanged = (obj, item) => OnOptionSelected?.Invoke(obj. gameObject, item as T); // Inner component with the pulldown image var image = PUIElements.CreateUI(combo, "OpenImage"); var icon = image.AddComponent <Image>(); icon.sprite = PUITuning.Images.Contract; icon.color = style.textColor; // Button component var dropButton = combo.AddComponent <KButton>(); PButton.SetupButton(dropButton, bgImage); UIDetours.FG_IMAGE.Set(dropButton, icon); dropButton.onClick += comboBox.OnClick; // Add tooltip PUIElements.SetToolTip(selection, ToolTip); combo.SetActive(true); // Button gets laid out on the right, rest of space goes to the label // Scroll pane is laid out on the bottom var layout = combo.AddComponent <RelativeLayoutGroup>(); layout.AnchorYAxis(selection).SetLeftEdge(selection, fraction: 0.0f).SetRightEdge(selection, toLeft: image).AnchorYAxis(image).SetRightEdge( image, fraction: 1.0f).SetMargin(selection, new RectOffset(margin.left, im.right, margin.top, margin.bottom)).SetMargin(image, new RectOffset(0, margin.right, margin.top, margin.bottom)).OverrideSize(image, ArrowSize). AnchorYAxis(pullDown, 0.0f).OverrideSize(pullDown, Vector2.up); layout.LockLayout(); if (DynamicSize) { layout.UnlockLayout(); } // Disable sizing on the pulldown pullDown.AddOrGet <LayoutElement>().ignoreLayout = true; // Scroll pane is hidden right away pullDown.SetActive(false); layout.flexibleWidth = FlexSize.x; layout.flexibleHeight = FlexSize.y; OnRealize?.Invoke(combo); return(combo); }