Beispiel #1
0
 /// <summary>
 /// Lazier version of Label. Auto-calculates the size (assuming text is single line), and aligned to Left.
 /// <para>Use <seealso cref="OpLabel(Vector2, Vector2, string, FLabelAlignment, bool)"/> for more complicated setup.</para>
 /// </summary>
 /// <param name="posX">Leftmost Position</param>
 /// <param name="posY">Bottom Position</param>
 /// <param name="text">Max length is limited to 200 for this constructor.</param>
 /// <param name="bigText">Whether this uses bigFont or not</param>
 public OpLabel(float posX, float posY, string text = "TEXT", bool bigText = false)
     : this(new Vector2(posX, posY), new Vector2(100f, 20f), text, FLabelAlignment.Left, bigText)
 {
     this._text = text.Length < LabelTest.CharLimit(_bigText) / 3 ? text : text.Substring(0, LabelTest.CharLimit(_bigText) / 3);
     this._size = new Vector2((this._bigText ? 10f : 7f) * text.Length + 10f, this._bigText ? 30f : 20f);
     if (!_init)
     {
         return;
     }
     this.label.size = this._size;
     this.label.text = this._text;
     OnChange();
 }
Beispiel #2
0
        /// <summary>
        /// Simple Label that displays text. See also <seealso cref="OpLabel(float, float, string, bool)"/> for lazier version.
        /// </summary>
        /// <param name="pos">BottomLeft Position</param>
        /// <param name="size">Size of the Box</param>
        /// <param name="text">Text you want to display; max length 600. See also <seealso cref="OpLabelLong"/><para>Leaving this as TEXT will call <see cref="LoremIpsum"/>.</para></param>
        /// <param name="alignment">Alignment. Left/Center/Right. Center is default.</param>
        /// <param name="bigText">Whether this uses bigFont or not</param>
        public OpLabel(Vector2 pos, Vector2 size, string text = "TEXT", FLabelAlignment alignment = FLabelAlignment.Center, bool bigText = false) : base(pos, size)
        {
            this._size    = new Vector2(Mathf.Max(size.x, 20f), Mathf.Max(size.y, 20f));
            this._bigText = bigText;
            this.autoWrap = false;
            if (text == "TEXT")
            { //Lorem Ipsum
                int lc      = this.IsLong ? Math.Max(1, Mathf.FloorToInt(_size.y * 0.65f / LabelTest.LineHeight(_bigText))) : 1;
                int charLim = Mathf.FloorToInt(_size.x / LabelTest.CharMean(_bigText)) * lc;
                int sentLim = charLim / LoremIpsum.meanCharPerSentence;
                if (this.IsLong)
                {
                    int pn = UnityEngine.Random.Range(1, Mathf.CeilToInt(lc / 1.5f));
                    text = LoremIpsum.Generate(Math.Max(1, (sentLim - 2) / pn), Math.Max(2, sentLim / pn), pn);
                    //charLim = Mathf.FloorToInt(charLim * 0.65f);
                }
                else
                {
                    text = LoremIpsum.Generate(Math.Max(1, sentLim - 2), Math.Max(1, sentLim));
                }
                text = text.Length > charLim?text.Substring(0, charLim - 2).TrimEnd() + "." : text;
            }
            if (this.IsLong)
            {
                this._text = text; _verticalAlignment = LabelVAlignment.Top;
            }
            else
            {
                this._text = text; _verticalAlignment = LabelVAlignment.Center;
            }
            //this.lineLength = Mathf.FloorToInt((size.x - 10f) / 6f);
            string cleanText = Regex.Replace(_text, @"\s+", "");

            this.isVFont = cleanText.Length > 0 && !LabelTest.HasNonASCIIChars(cleanText);
            if (!_init)
            {
                return;
            }
            this.color      = Menu.Menu.MenuRGB(Menu.Menu.MenuColors.MediumGrey);
            this._alignment = alignment;
            if (!this.IsLong)
            {
                this.label             = new MenuLabel(menu, owner, _text, this.pos, this.size, this._bigText);
                this.label.label.color = this.color;
                this.subObjects.Add(this.label);
                OnChange();
            }
        }
        /// <summary>
        /// Simple TextBox.
        /// </summary>
        /// <param name="pos">LeftBottom <see cref="UIelement.pos"/>.</param>
        /// <param name="sizeX">Horizontal size (min = 30 pxl). The height is fixed to 24 pxl.</param>
        /// <param name="key">Unique <see cref="UIconfig.key"/></param>
        /// <param name="defaultValue">Default string value</param>
        /// <param name="accept">Which type of text you want to <see cref="Accept"/></param>
        public OpTextBox(Vector2 pos, float sizeX, string key, string defaultValue = "TEXT", Accept accept = Accept.StringASCII) : base(pos, new Vector2(30f, 24f), key, defaultValue)
        {
            this._size       = new Vector2(Mathf.Max(IsUpdown ? 40f : 30f, sizeX), IsUpdown ? 30f : 24f);
            this.description = InternalTranslator.Translate("Click and Type text");

            this.accept       = accept;
            this._value       = defaultValue;
            this._lastValue   = defaultValue;
            this.defaultValue = this.value;
            this.maxLength    = Mathf.FloorToInt((size.x - 20f) / 6f);
            if (this.accept == Accept.Float || this.accept == Accept.Int)
            {
                this.allowSpace = float.Parse(defaultValue) < 0f;
            }
            else
            {
                this.allowSpace = defaultValue.Contains(" ");
            }
            this.password  = false;
            this.mouseDown = false;

            if (!_init)
            {
                return;
            }

            //this.keyboardOn = false;
            this.colorEdge = Menu.Menu.MenuRGB(Menu.Menu.MenuColors.MediumGrey);
            this.colorText = Menu.Menu.MenuRGB(Menu.Menu.MenuColors.MediumGrey);
            this.colorFill = Color.black;
            this.rect      = new DyeableRect(menu, owner, this.pos, this.size, true)
            {
                fillAlpha = 0.5f
            };
            this.subObjects.Add(this.rect);

            this.label = new MenuLabel(menu, owner, defaultValue, this.pos + new Vector2(5f - this._size.x * 0.5f, IsUpdown ? 13f : 10f), new Vector2(this._size.x, 20f), false);
            this.label.label.alignment = FLabelAlignment.Left;
            this.label.label.SetAnchor(0f, 1f);
            this.label.label.color = this.colorText;
            //this.label.label.SetPosition(new Vector2(5f, 3f) - (this._size * 0.5f));
            this.subObjects.Add(this.label);

            this.cursor = new FCursor();
            cursor.SetPosition(LabelTest.GetWidth(value, false) + LabelTest.CharMean(false), this.size.y * 0.5f - 7f);
            this.myContainer.AddChild(this.cursor);
        }
 /// <summary>
 /// Circular Hold Button which can also be used as ProgressButton
 /// </summary>
 /// <param name="pos">BottomLeft <see cref="UIelement.pos"/>; <see cref="UIelement.fixedRad"/> is 55f (110f in diameter)</param>
 /// <param name="signal"><see cref="UItrigger.signal"/></param>
 /// <param name="fillTime">How long do you need to hold to call Signal (set to 0f for instant)</param>
 /// <param name="displayText">Text to be displayed (overriden when it's ProgressButton mode)</param>
 public OpHoldButton(Vector2 pos, string signal, string displayText, float fillTime = 80f) : base(pos, 55f, signal)
 {
     this.fillTime = Mathf.Max(0f, fillTime);
     fixedRad      = 55f;
     _text         = displayText;
     color         = Menu.Menu.MenuRGB(Menu.Menu.MenuColors.MediumGrey);
     if (!_init)
     {
         return;
     }
     circles    = new FSprite[5];
     circles[0] = new FSprite("Futile_White")
     {
         shader = menu.manager.rainWorld.Shaders["VectorCircleFadable"]
     };
     circles[1] = new FSprite("Futile_White")
     {
         shader = menu.manager.rainWorld.Shaders["VectorCircle"]
     };
     circles[2] = new FSprite("Futile_White")
     {
         shader = menu.manager.rainWorld.Shaders["HoldButtonCircle"]
     };
     circles[3] = new FSprite("Futile_White")
     {
         shader = menu.manager.rainWorld.Shaders["VectorCircle"]
     };
     circles[4] = new FSprite("Futile_White")
     {
         shader = menu.manager.rainWorld.Shaders["VectorCircleFadable"]
     };
     for (int i = 0; i < circles.Length; i++)
     {
         myContainer.AddChild(circles[i]); circles[i].SetPosition(55f, 55f);
     }
     label = new FLabel(LabelTest.GetFont(false), text)
     {
         alignment = FLabelAlignment.Center
     };
     label.SetPosition(new Vector2(55.01f, 55.01f));
     myContainer.AddChild(label);
 }
        public override void OnChange()
        {
            if (this.labels == null || !_init)
            {
                return;
            }
            base.OnChange();
            float lineHeight = LabelTest.LineHeight(false, isVFont);

            string[]      lines  = this._displayText.Replace(Environment.NewLine, "\n").Split(new char[] { '\n' });
            List <string> splits = new List <string>()
            {
                string.Empty
            };
            int num = 0, lblTxt = 0, lineMax = this.allowOverflow ? int.MaxValue : Mathf.FloorToInt(this.size.y / lineHeight);
            int sLines = 0;

            for (int l = 0; l < lines.Length; l++)
            {
                if (l > lineMax)
                {
                    break;
                }
                string a = lines[l].Trim('\n');
                if (string.IsNullOrEmpty(a))
                {
                    a = " ";
                }
                if (lblTxt + a.Length > LabelTest.CharLimit(false, isVFont) || sLines > 8)
                {
                    sLines = 0; lblTxt = 0; num++; splits.Add(string.Empty);
                }
                if (lblTxt > 0)
                {
                    splits[num] += '\n';
                }
                splits[num] += a;
                lblTxt      += a.Length + 1;
                sLines++;
            }
            while (this.labels.Count < splits.Count)
            {
                MenuLabel nl = new MenuLabel(this.menu, this.owner, isVFont ? "A" : "", this.pos, this.size, false)
                {
                    text = ""
                };
                this.labels.Add(nl);
                this.subObjects.Add(nl);
            }
            num = 0; // linebreak sum
            float lh = lineHeight * GetLineCount();

            for (int b = 0; b < labels.Count; b++)
            {
                if (splits.Count <= b)
                {
                    this.labels[b].text = string.Empty; continue;
                }
                int ln = 0; string[] s = splits[b].Split('\n');
                for (int i = 0; i < s.Length; i++)
                {
                    if (!string.IsNullOrEmpty(s[i]))
                    {
                        ln++;
                    }
                }
                switch (this._alignment)
                {
                default:
                case FLabelAlignment.Center:
                    this.labels[b].label.alignment = FLabelAlignment.Center;
                    this.labels[b].pos.x           = this.pos.x;
                    break;

                case FLabelAlignment.Left:
                    this.labels[b].label.alignment = FLabelAlignment.Left;
                    this.labels[b].pos.x           = this.pos.x - this.size.x * 0.5f;
                    break;

                case FLabelAlignment.Right:
                    this.labels[b].label.alignment = FLabelAlignment.Right;
                    this.labels[b].pos.x           = this.pos.x + this.size.x * 0.5f;
                    break;
                }
                this.labels[b].size.x = this._size.x;
                this.labels[b].size.y = ln * lineHeight;
                num += ln;
                switch (this.verticalAlignment)
                {
                default:
                case LabelVAlignment.Top:
                    this.labels[b].pos.y = this.pos.y + this.size.y - num * lineHeight;
                    break;

                case LabelVAlignment.Bottom:
                    this.labels[b].pos.y = this.pos.y + lh - num * lineHeight;
                    break;

                case LabelVAlignment.Center:
                    this.labels[b].pos.y = this.pos.y + (this.size.y + lh) / 2f - num * lineHeight;
                    break;
                }
                this.labels[b].text = splits[b];
            }
        }
Beispiel #6
0
        public override void OnChange()
        {
            this._size = new Vector2(Mathf.Max(this._size.x, 20f), Mathf.Max(this._size.y, 20f));
            base.OnChange();
            if (!_init)
            {
                return;
            }

            if (string.IsNullOrEmpty(this._text))
            {
                this._displayText = ""; goto displaySkip;
            }
            if (!this.autoWrap)
            {
                if (this.IsLong)
                {
                    this._displayText = _text;
                }
                else
                {
                    this._displayText = _text.Length < LabelTest.CharLimit(_bigText, isVFont) ? _text : _text.Substring(0, LabelTest.CharLimit(_bigText, isVFont));
                }
            }
            else
            {
                string ml;
                if (this.IsLong)
                {
                    ml = _text;
                }
                else
                {
                    ml = _text.Length < LabelTest.CharLimit(_bigText, isVFont) ? _text : _text.Substring(0, LabelTest.CharLimit(_bigText, isVFont));
                }

                this._displayText = LabelTest.WrapText(ml, _bigText, _size.x, isVFont);
            }
displaySkip:
            if (this.IsLong)
            {
                return;
            }
            if (this.GetLineCount() > 10)
            {
                int IndexOfOccurence(int occurence)
                {
                    int i     = 1;
                    int index = 0;

                    while (i <= occurence && (index = _displayText.IndexOf('\n', index + 1)) != -1)
                    {
                        if (i == occurence)
                        {
                            return(index);
                        }
                        i++;
                    }
                    return(-1);
                }

                int sp = IndexOfOccurence(10);
                if (sp > 0)
                {
                    _displayText = _displayText.Substring(0, sp);
                }
            }
            this.label.text = this._displayText;
            this.label.size = this.size;
            switch (this._alignment)
            {
            default:
            case FLabelAlignment.Center:
                this.label.label.alignment = FLabelAlignment.Center;
                this.label.pos             = this.pos;
                break;

            case FLabelAlignment.Left:
                this.label.label.alignment = FLabelAlignment.Left;
                this.label.pos             = this.pos - new Vector2(this.size.x * 0.5f, 0f);
                break;

            case FLabelAlignment.Right:
                this.label.label.alignment = FLabelAlignment.Right;
                this.label.pos             = this.pos + new Vector2(this.size.x * 0.5f, 0f);
                break;
            }

            float lh = LabelTest.LineHeight(_bigText, isVFont) * GetLineCount();

            switch (this._verticalAlignment)
            {
            default:
            case LabelVAlignment.Center:
                break;

            case LabelVAlignment.Top:
                this.label.size.y = lh;
                this.label.pos.y  = this.pos.y + this.size.y - lh;
                break;

            case LabelVAlignment.Bottom:
                this.label.size.y = lh;
                this.label.pos.y  = this.pos.y + lh;
                break;
            }
        }
        public override void Update(float dt)
        {
            this.col = this.bumpBehav.col;
            base.Update(dt);
            this.bumpBehav.col = this.col;
            if (disabled)
            {
                return;
            }

            if (KeyboardOn)
            {
                cursor.alpha = Mathf.Clamp(cursorAlpha, 0f, 1f);
                cursorAlpha -= 0.05f;
                if (cursorAlpha < -0.5f)
                {
                    cursorAlpha = 2f;
                }

                //input spotted! ->cursorAlpha = 2.5f;
                foreach (char c in Input.inputString)
                {
                    //Debug.Log(string.Concat("input: ", c));
                    if (c == '\b')
                    {
                        cursorAlpha = 2.5f; this.bumpBehav.flash = 2.5f;
                        if (this.value.Length > 0)
                        {
                            this._value = this.value.Substring(0, this.value.Length - 1);
                            if (!_soundFilled)
                            {
                                _soundFill += 12;
                                menu.PlaySound(SoundID.MENY_Already_Selected_MultipleChoice_Clicked);
                            }
                            OnChange();
                        }
                        break;
                    }
                    else if ((c == '\n') || (c == '\r')) // enter/return
                    {
                        KeyboardOn = false;
                        menu.PlaySound(SoundID.MENU_Checkbox_Check);
                        break;
                    }
                    else
                    {
                        cursorAlpha          = 2.5f;
                        this.bumpBehav.flash = 2.5f;
                        this.value          += c;
                    }
                }
            }
            else
            {
                cursorAlpha = 0f;
            }
            if (Input.GetMouseButton(0) && !mouseDown)
            {
                if (this is OpUpdown ud && ud.mouseOverArrow)
                {
                    return;
                }
                mouseDown = true;
                if (MouseOver && !KeyboardOn)
                { //Turn on
                    menu.PlaySound(SoundID.MENU_Button_Select_Gamepad_Or_Keyboard);
                    KeyboardOn            = true;
                    this.cursor.isVisible = true;
                    this.cursorAlpha      = 1f;
                    cursor.SetPosition(LabelTest.GetWidth(this.label.text, false) + LabelTest.CharMean(false), this.size.y * 0.5f - 7f);
                }
                else if (!MouseOver && KeyboardOn)
                { //Shutdown
                    KeyboardOn = false;
                    menu.PlaySound(SoundID.MENU_Checkbox_Uncheck);
                }
            }
            else if (!Input.GetMouseButton(0))
            {
                mouseDown = false;
            }
        }