Esempio n. 1
0
        public KsmGuiText(
            KsmGuiBase parent,
            string text,
            string tooltipText = null,
            TextAlignmentOptions alignement = TextAlignmentOptions.TopLeft,
            bool wordWrap = true,
            TextOverflowModes overflowMode = TextOverflowModes.Overflow
            ) : base(parent)
        {
            savedAlignement                  = alignement;
            TextComponent                    = TopObject.AddComponent <TextMeshProUGUI>();
            TextComponent.color              = KsmGuiStyle.textColor;
            TextComponent.font               = KsmGuiStyle.textFont;
            TextComponent.fontSize           = KsmGuiStyle.textSize;
            TextComponent.alignment          = alignement;
            TextComponent.enableWordWrapping = wordWrap;
            TextComponent.overflowMode       = overflowMode;
            TextComponent.text               = text;
            SetLayoutElement(true);
            //TextComponent.raycastTarget = false;

            if (tooltipText != null)
            {
                SetTooltipText(tooltipText);
            }
        }
Esempio n. 2
0
        public KsmGuiIcon(KsmGuiBase parent, Texture2D texture, string tooltipText = null, int width = 16, int height = 16) : base(parent)
        {
            // we use a child gameobject because KsmGuiIcon can be used as a button (we need it as a child in this case)
            // we directly set its size trough anchors / sizeDelta instead of using layout components, this way it can be used
            // both standalone or as a button without having to mess with the layout component
            // We still set a min height/min size layout on the top object to make sure other objects in a group won't overlap

            GameObject icon = new GameObject("icon");

            IconTransform = icon.AddComponent <RectTransform>();
            icon.AddComponent <CanvasRenderer>();

            Image = TopObject.AddComponent <RawImage>();

            // make sure pivot is at the center
            IconTransform.pivot = new Vector2(0.5f, 0.5f);

            // set anchors to middle-center
            IconTransform.anchorMin = new Vector2(0.5f, 0.5f);
            IconTransform.anchorMax = new Vector2(0.5f, 0.5f);

            // anchor-pivot distance
            IconTransform.anchoredPosition = new Vector2(0f, 0f);

            SetIconTexture(texture, width, height);

            IconTransform.SetParentFixScale(TopTransform);

            if (tooltipText != null)
            {
                SetTooltipText(tooltipText);
            }
        }
Esempio n. 3
0
        public KsmGuiTextBox(KsmGuiBase parent, string text, string tooltipText = null, TextAlignmentOptions alignement = TextAlignmentOptions.TopLeft) : base(parent)
        {
            SetLayoutElement(true, true);
            TextObject = new KsmGuiText(this, text, null, alignement);

            if (tooltipText != null)
            {
                SetTooltipText(text);
            }
        }
Esempio n. 4
0
        public static void AddImageComponentWithColor(this KsmGuiBase ksmGuiBase, Color color)
        {
            // Unity will trhow an exception, but it doesn't hurt to know why
            if (ksmGuiBase.TopObject.GetComponent <Graphic>() != null)
            {
                Lib.Log("KsmGui error : can't add a background color to " + ksmGuiBase.Name + ", the GameObject already has a graphic component", Lib.LogLevel.Warning);
            }

            Image image = ksmGuiBase.TopObject.AddComponent <Image>();

            image.color = color;
        }
Esempio n. 5
0
        public KsmGuiButton
        (
            KsmGuiBase parent,
            string buttonText,
            UnityAction onClick,
            string tooltipText    = null,
            int width             = -1,
            int height            = 18,
            Texture2D iconTexture = null,
            int iconWidth         = 16,
            int iconHeight        = 16
        ) : base(parent, 2, 3, 3, 0, 0, TextAnchor.MiddleCenter)
        {
            // buttons are 18 px high and expand horizontaly in their container by default, but a fixed width can be defined in the ctor
            // in any case, SetLayoutElement can be called after to customise the button size.
            if (width <= 0)
            {
                SetLayoutElement(true, false, -1, height);
            }
            else
            {
                SetLayoutElement(false, false, width, height);
            }

            ImageComponent            = TopObject.AddComponent <Image>();
            ImageComponent.sprite     = Textures.KsmGuiSpriteBtnNormal;
            ImageComponent.type       = Image.Type.Sliced;
            ImageComponent.fillCenter = true;

            ButtonComponent             = TopObject.AddComponent <Button>();
            ButtonComponent.transition  = Selectable.Transition.SpriteSwap;
            ButtonComponent.spriteState = KsmGuiStyle.buttonSpriteSwap;
            ButtonComponent.navigation  = new Navigation()
            {
                mode = Navigation.Mode.None
            };                                                                                         // fix the transitions getting stuck

            SetButtonOnClick(onClick);

            SetIconTexture(iconTexture, iconWidth, iconHeight);

            TextObject = new KsmGuiText(this, buttonText, null, TextAlignmentOptions.Center);
            TextObject.SetLayoutElement(true);
            TextObject.TopTransform.SetParentFixScale(TopTransform);

            if (tooltipText != null)
            {
                SetTooltipText(tooltipText);
            }
        }
Esempio n. 6
0
        public KsmGuiIconButton(KsmGuiBase parent, Texture2D texture, UnityAction onClick = null, string tooltipText = null, int width = 16, int height = 16)
            : base(parent, texture, tooltipText, width, height)
        {
            ButtonComponent = TopObject.AddComponent <Button>();
            ButtonComponent.targetGraphic = Image;
            ButtonComponent.interactable  = true;
            ButtonComponent.transition    = Selectable.Transition.ColorTint;
            ButtonComponent.colors        = KsmGuiStyle.iconTransitionColorBlock;
            ButtonComponent.navigation    = new Navigation()
            {
                mode = Navigation.Mode.None
            };                                                                                         // fix the transitions getting stuck

            SetButtonOnClick(onClick);
        }
Esempio n. 7
0
        public KsmGuiBase(KsmGuiBase parent)
        {
            TopObject    = new GameObject(Name);
            TopTransform = TopObject.AddComponent <RectTransform>();
            TopObject.AddComponent <CanvasRenderer>();

            if (parent != null)
            {
                layoutOptimizer = parent.layoutOptimizer;
                TopTransform.SetParentFixScale(parent.ParentTransformForChilds);
            }
            else
            {
                layoutOptimizer = TopObject.AddComponent <KsmGuiLayoutOptimizer>();
            }

            TopObject.SetLayerRecursive(5);
        }
Esempio n. 8
0
 public KsmGuiHorizontalLayout
 (
     KsmGuiBase parent,
     int spacing                = 0,
     int paddingLeft            = 0,
     int paddingRight           = 0,
     int paddingTop             = 0,
     int paddingBottom          = 0,
     TextAnchor childAlignement = TextAnchor.UpperLeft
 ) : base(parent)
 {
     LayoutGroup                        = TopObject.AddComponent <HorizontalLayoutGroup>();
     LayoutGroup.spacing                = spacing;
     LayoutGroup.padding                = new RectOffset(paddingLeft, paddingRight, paddingTop, paddingBottom);
     LayoutGroup.childAlignment         = childAlignement;
     LayoutGroup.childControlHeight     = true;
     LayoutGroup.childControlWidth      = true;
     LayoutGroup.childForceExpandHeight = false;
     LayoutGroup.childForceExpandWidth  = false;
 }
Esempio n. 9
0
        public KsmGuiHeader(KsmGuiBase parent, string title, Color backgroundColor = default, int textPreferredWidth = -1)
            : base(parent, 2, 0, 0, 0, 0, TextAnchor.UpperLeft)
        {
            // default : black background
            Image image = TopObject.AddComponent <Image>();

            if (backgroundColor == default)
            {
                image.color = Color.black;
            }
            else
            {
                image.color = backgroundColor;
            }


            TextObject = new KsmGuiText(this, title, null, TextAlignmentOptions.Center);
            TextObject.TextComponent.fontStyle = FontStyles.UpperCase;
            TextObject.SetLayoutElement(true, false, textPreferredWidth, -1, -1, 16);
        }
Esempio n. 10
0
        public KsmGuiToggle(KsmGuiBase parent, string toggleText, bool initialOnState, UnityAction <bool> onClick, string tooltipText = null, int width = -1, int height = 18)
            : base(parent, 5)
        {
            onClickAction = onClick;

            if (width <= 0)
            {
                SetLayoutElement(true, false, -1, height);
            }
            else
            {
                SetLayoutElement(false, false, width, height);
            }

            ToggleComponent             = TopObject.AddComponent <Toggle>();
            ToggleComponent.transition  = Selectable.Transition.SpriteSwap;
            ToggleComponent.spriteState = KsmGuiStyle.buttonSpriteSwap;
            ToggleComponent.navigation  = new Navigation()
            {
                mode = Navigation.Mode.None
            };                                                                                         // fix the transitions getting stuck
            ToggleComponent.toggleTransition = Toggle.ToggleTransition.Fade;
            ToggleComponent.isOn             = initialOnState;
            ToggleComponent.onValueChanged.AddListener(onClickAction);

            GameObject    background          = new GameObject("Background");
            RectTransform backgroundTransform = background.AddComponent <RectTransform>();
            LayoutElement backgroundLayout    = background.AddComponent <LayoutElement>();

            backgroundLayout.preferredHeight = 18f;             // the toggle is always 18x18
            backgroundLayout.preferredWidth  = 18f;
            backgroundTransform.SetParentFixScale(TopTransform);
            background.AddComponent <CanvasRenderer>();
            Image backgroundImage = background.AddComponent <Image>();

            backgroundImage.sprite        = Textures.KsmGuiSpriteBtnNormal;
            backgroundImage.type          = Image.Type.Sliced;
            backgroundImage.fillCenter    = true;
            ToggleComponent.targetGraphic = backgroundImage;

            GameObject    checkmark          = new GameObject("Checkmark");
            RectTransform checkmarkTransform = checkmark.AddComponent <RectTransform>();

            checkmarkTransform.SetAnchorsAndPosition(TextAnchor.MiddleCenter, TextAnchor.MiddleCenter, 0, 0);
            checkmarkTransform.SetSizeDelta(10, 10);             // a checkbox is always 10x10, centered in the toggle
            checkmarkTransform.SetParentFixScale(backgroundTransform);
            checkmark.AddComponent <CanvasRenderer>();
            RawImage checkmarkImage = checkmark.AddComponent <RawImage>();

            checkmarkImage.texture  = Textures.KsmGuiTexCheckmark;
            ToggleComponent.graphic = checkmarkImage;

            TextObject = new KsmGuiText(this, toggleText, null, TextAlignmentOptions.Left, false, TextOverflowModes.Ellipsis);
            if (width <= 0)
            {
                SetLayoutElement(true, false, -1, height);
            }
            else
            {
                SetLayoutElement(false, false, width - 18 - 5, height);
            }
            TextObject.TopTransform.SetParentFixScale(TopTransform);

            if (tooltipText != null)
            {
                SetTooltipText(tooltipText);
            }
        }
Esempio n. 11
0
 public KsmGuiToggleList(KsmGuiBase parent, UnityAction <T> onChildToggleActivated)
     : base(parent, 2, 0, 0, 0, 0, TextAnchor.UpperLeft)
 {
     ToggleGroupComponent   = TopObject.AddComponent <ToggleGroup>();
     OnChildToggleActivated = onChildToggleActivated;
 }
        public KsmGuiVerticalSection(KsmGuiBase parent) : base(parent, 0, 5, 5, 5, 5, TextAnchor.UpperLeft)
        {
            Image background = TopObject.AddComponent <Image>();

            background.color = KsmGuiStyle.boxColor;
        }
Esempio n. 13
0
        public KsmGuiVerticalScrollView(KsmGuiBase parent, int contentSpacing = 5, int contentPaddingLeft = 5, int contentPaddingRight = 5, int contentPaddingTop = 5, int contentPaddingBottom = 5) : base(parent)
        {
            ScrollRect scrollRect = TopObject.AddComponent <ScrollRect>();

            scrollRect.horizontal                  = false;
            scrollRect.vertical                    = true;
            scrollRect.movementType                = ScrollRect.MovementType.Elastic;
            scrollRect.elasticity                  = 0.1f;
            scrollRect.inertia                     = true;
            scrollRect.decelerationRate            = 0.15f;
            scrollRect.scrollSensitivity           = 10f;
            scrollRect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
            scrollRect.verticalScrollbarSpacing    = -3f;

            Image scrollRectBackground = TopObject.AddComponent <Image>();

            scrollRectBackground.color = KsmGuiStyle.boxColor;

            // viewport object (child of top object)
            GameObject    viewport          = new GameObject("viewport");
            RectTransform viewportTransform = viewport.AddComponent <RectTransform>();

            viewport.AddComponent <CanvasRenderer>();

            // Note : using a standard "Mask" has a bug where scrollrect content is visible
            // in other windows scollrects (like if all masks were "global" for all masked content)
            // see https://issuetracker.unity3d.com/issues/scroll-view-content-is-visible-outside-of-mask-when-there-is-another-masked-ui-element-in-the-same-canvas
            // using a RectMask2D fixes it, at the cost of the ability to use an image mask (but we don't care)
            viewport.AddComponent <RectMask2D>();

            //Mask viewportMask = viewport.AddComponent<Mask>();
            //viewportMask.showMaskGraphic = false;
            //Image viewportImage = viewport.AddComponent<Image>();
            //viewportImage.color = new Color(1f, 1f, 1f, 1f);

            viewportTransform.SetParentFixScale(TopTransform);
            scrollRect.viewport = viewportTransform;

            // content object (child of viewport)
            GameObject contentObject = new GameObject("content");

            Content                  = contentObject.AddComponent <RectTransform>();
            Content.anchorMin        = new Vector2(0f, 1f);
            Content.anchorMax        = new Vector2(1f, 1f);
            Content.pivot            = new Vector2(0f, 1f);
            Content.anchoredPosition = new Vector2(0f, 0f);
            Content.sizeDelta        = new Vector2(0f, 0f);

            ContentSizeFitter sizeFitter = contentObject.AddComponent <ContentSizeFitter>();

            sizeFitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
            sizeFitter.verticalFit   = ContentSizeFitter.FitMode.PreferredSize;

            ContentGroup                        = contentObject.AddComponent <VerticalLayoutGroup>();
            ContentGroup.padding                = new RectOffset(contentPaddingLeft, contentPaddingRight, contentPaddingTop, contentPaddingBottom);
            ContentGroup.spacing                = contentSpacing;
            ContentGroup.childAlignment         = TextAnchor.UpperLeft;
            ContentGroup.childControlHeight     = true;
            ContentGroup.childControlWidth      = true;
            ContentGroup.childForceExpandHeight = false;
            ContentGroup.childForceExpandWidth  = false;

            Content.SetParentFixScale(viewportTransform);
            scrollRect.content = Content;

            // scrollbar (child of top object)
            GameObject    scrollbar          = new GameObject("scrollbar");
            RectTransform scrollbarTransform = scrollbar.AddComponent <RectTransform>();

            scrollbarTransform.anchorMin        = new Vector2(1f, 0f);
            scrollbarTransform.anchorMax        = new Vector2(1f, 1f);
            scrollbarTransform.pivot            = new Vector2(1f, 1f);
            scrollbarTransform.anchoredPosition = new Vector2(0f, 0f);
            scrollbarTransform.sizeDelta        = new Vector2(10f, 0f);      // scrollbar width
            scrollbar.AddComponent <CanvasRenderer>();

            Image scrollBarImage = scrollbar.AddComponent <Image>();

            scrollBarImage.color = Color.black;

            Scrollbar scrollbarComponent = scrollbar.AddComponent <Scrollbar>();

            scrollbarComponent.interactable = true;
            scrollbarComponent.transition   = Selectable.Transition.ColorTint;
            scrollbarComponent.colors       = new ColorBlock()
            {
                normalColor      = Color.white,
                highlightedColor = Color.white,
                pressedColor     = new Color(0.8f, 0.8f, 0.8f),
                disabledColor    = new Color(0.8f, 0.8f, 0.8f, 0.5f),
                colorMultiplier  = 1f,
                fadeDuration     = 0.1f
            };
            scrollbarComponent.navigation = new Navigation()
            {
                mode = Navigation.Mode.None
            };
            scrollbarComponent.direction = Scrollbar.Direction.BottomToTop;
            scrollRect.verticalScrollbar = scrollbarComponent;

            scrollbarTransform.SetParentFixScale(TopTransform);

            // scrollbar sliding area
            GameObject    slidingArea          = new GameObject("slidingArea");
            RectTransform slidingAreaTransform = slidingArea.AddComponent <RectTransform>();

            slidingAreaTransform.anchorMin        = new Vector2(0f, 0f);
            slidingAreaTransform.anchorMax        = new Vector2(1f, 1f);
            slidingAreaTransform.pivot            = new Vector2(0.5f, 0.5f);
            slidingAreaTransform.anchoredPosition = new Vector2(5f, 5f);
            slidingAreaTransform.sizeDelta        = new Vector2(5f, 5f);      // scrollbar width / 2
            slidingAreaTransform.SetParentFixScale(scrollbarTransform);

            // scrollbar handle
            GameObject    scrollbarHandle = new GameObject("scrollbarHandle");
            RectTransform handleTransform = scrollbarHandle.AddComponent <RectTransform>();

            scrollbarHandle.AddComponent <CanvasRenderer>();
            handleTransform.anchorMin        = new Vector2(0f, 0f);
            handleTransform.anchorMax        = new Vector2(1f, 1f);
            handleTransform.pivot            = new Vector2(0.5f, 0.5f);
            handleTransform.anchoredPosition = new Vector2(-4f, -4f);      // relative to sliding area width
            handleTransform.sizeDelta        = new Vector2(-4f, -4f);      // relative to sliding area width
            scrollbarComponent.handleRect    = handleTransform;

            Image handleImage = scrollbarHandle.AddComponent <Image>();

            handleImage.color = new Color(0.4f, 0.4f, 0.4f);
            handleTransform.SetParentFixScale(slidingAreaTransform);
            scrollbarComponent.targetGraphic = handleImage;
        }