/// <summary> /// Add a button outside of the normal DialogGUI* flow layout, /// with positioning relative to edges of a parent element. /// By DMagic, with modifications by HebaruSan. /// </summary> /// <param name="parentTransform">Transform of UI object within which to place this button</param> /// <param name="innerHorizOffset">Horizontal position; if positive, number of pixels between left edge of window and left edge of button, if negative, then vice versa on right side</param> /// <param name="innerVertOffset">Vertical position; if positive, number of pixels between bottom edge of window and bottom edge of button, if negative, then vice versa on top side</param> /// <param name="style">Style object containing the sprites for the button</param> /// <param name="tooltip">String to show when user hovers on button</param> /// <param name="onClick">Function to call when the user clicks the button</param> internal static void AddFloatingButton(Transform parentTransform, float innerHorizOffset, float innerVertOffset, UIStyle style, string text, string tooltip, UnityAction onClick) { // This creates a new button object using the prefab from KSP's UISkinManager. // The same prefab is used for the PopupDialog system buttons. // Anything we set on this object will be reflected in the button we create. GameObject btnGameObj = GameObject.Instantiate <GameObject>(UISkinManager.GetPrefab("UIButtonPrefab")); // Set the button's parent transform. btnGameObj.transform.SetParent(parentTransform, false); // Add a layout element and set it to be ignored. // Otherwise the button will end up on the bottom of the window. btnGameObj.AddComponent <LayoutElement>().ignoreLayout = true; // This is how we position the button. // The anchors and pivot make the button positioned relative to the top-right corner. // The anchored position sets the position with values in pixels. RectTransform rect = btnGameObj.GetComponent <RectTransform>(); rect.anchoredPosition = new Vector2(innerHorizOffset, innerVertOffset); rect.sizeDelta = new Vector2(buttonIconWidth, buttonIconWidth); rect.anchorMin = rect.anchorMax = rect.pivot = new Vector2( rect.anchoredPosition.x < 0 ? 1 : 0, rect.anchoredPosition.y < 0 ? 1 : 0 ); // Set the button's image component to the normal sprite. // Since this object comes from the button's GameObject, // changing it affects the button directly! Image btnImg = btnGameObj.GetComponent <Image>(); btnImg.sprite = style.normal.background; // Now set the different states to their respective sprites. Button button = btnGameObj.GetComponent <Button>(); button.transition = Selectable.Transition.SpriteSwap; button.spriteState = new SpriteState() { highlightedSprite = style.highlight.background, pressedSprite = style.active.background, disabledSprite = style.disabled.background }; // The text will be "Button" if we don't clear it. btnGameObj.GetChild("Text").GetComponent <TextMeshProUGUI>().text = text; // Set the tooltip btnGameObj.SetTooltip(tooltip); // Set the code to call when clicked. button.onClick.AddListener(onClick); // Activate the button object, making it visible. btnGameObj.SetActive(true); }
// DialogGUIButton public static GameObject CreateButton(Transform parent, Callback onClick, UISkinDef skin = null) { if (skin == null) { skin = HighLogic.UISkin; } GameObject gameObject = Instantiate(UISkinManager.GetPrefab("UIButtonPrefab")); Button button = gameObject.GetComponent <Button>(); gameObject.transform.SetParent(parent, false); gameObject.SetActive(true); // base.SetupTransformAndLayout(); --> add layoutelement and configure, do we need it ? gameObject.GetComponent <RectTransform>().localScale = Vector3.one; LayoutElement layoutElement = gameObject.GetComponent <LayoutElement>(); if (layoutElement == null) { layoutElement = gameObject.AddComponent <LayoutElement>(); } UIStyle uIStyle = skin.button; if (uIStyle.normal.background != null) { button.spriteState = new SpriteState { disabledSprite = uIStyle.disabled.background, highlightedSprite = uIStyle.highlight.background, pressedSprite = uIStyle.active.background }; } else { button.transition = Selectable.Transition.ColorTint; } button.onClick.AddListener(delegate { onClick(); }); return(gameObject); }
/// <summary> /// Adds a close button to the main window in the top-right corner /// </summary> /// <param name="textStyle">The text style for the button's X text; /// replace this with a style similar to that used for the settings button if /// you want to use an icon instead</param> private void AddCloseButton(UIStyle textStyle /*, UIStyle buttonStyle */) { if (dialog != null) { //This creates a new button object using the prefab from KSP's UISkinManager //The same prefab is used for the PopupDialog system buttons GameObject go = GameObject.Instantiate <GameObject>(UISkinManager.GetPrefab("UIButtonPrefab")); //This sets the button's parent to be the dialog window itself go.transform.SetParent(dialog.transform, false); //This activates the button object go.SetActive(true); //We need to add a layout element and set it to be ignored //Otherwise the button will end up on the bottom of the window LayoutElement layout = go.AddComponent <LayoutElement>(); layout.ignoreLayout = true; //This is how we position the button //The anchors and pivot make the button positioned relative to the top-right corner //The anchored position sets the position with values in pixels RectTransform rect = go.GetComponent <RectTransform>(); rect.anchorMax = new Vector2(1, 1); rect.anchorMin = new Vector2(1, 1); rect.pivot = new Vector2(1, 1); rect.anchoredPosition = new Vector2(-8, -8); rect.sizeDelta = new Vector2(16, 16); Button button = go.GetComponent <Button>(); /* Use this section if you want to use an icon for the button * It takes the button, sets its image component to the normal sprite * and sets the different states to their respective sprites * Image img = go.GetComponent<Image>(); * * img.sprite = buttonStyle.normal.background; * * SpriteState buttonSpriteSwap = new SpriteState() * { * highlightedSprite = buttonStyle.highlight.background, * pressedSprite = buttonStyle.active.background, * disabledSprite = buttonStyle.disabled.background * }; * * button.spriteState = buttonSpriteSwap; * button.transition = Selectable.Transition.SpriteSwap; */ //Remove this if you want to use an icon for the button //Clip here -> TextMeshProUGUI text = go.GetChild("Text").GetComponent <TextMeshProUGUI>(); text.text = "X"; text.font = UISkinManager.TMPFont; text.fontSize = textStyle.fontSize; text.color = textStyle.normal.textColor; text.fontStyle = FontStyles.Bold; text.alignment = TextAlignmentOptions.Center; // -> to here button.onClick.AddListener(delegate { Dismiss(); //This resets the App launcher button state, so it doesn't look like it's still open if (Astrogator.Instance != null && Astrogator.Instance.launcher != null) { Astrogator.Instance.launcher.SetFalse(false); } }); } }