about summary refs log tree commit diff stats
path: root/AnodyneArchipelago/Menu/TextEntry.cs
blob: 8be54f5a43199abf47e08044fee1dad85f3aa8c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using AnodyneSharp.Input;
using AnodyneSharp.Sounds;
using AnodyneSharp.States;
using AnodyneSharp.UI;
using Microsoft.Xna.Framework;

namespace AnodyneArchipelago.Menu
{
    internal class TextEntry : State
    {
        public delegate void CommitChange(string value);

        private readonly string _header;
        private readonly CommitChange _commitFunc;

        private string _value;

        private UILabel _headerLabel;
        private UILabel _valueLabel;
        private UIEntity _bgBox;

        public TextEntry(string header, string value, CommitChange commitFunc)
        {
            _header = header;
            _value = value;
            _commitFunc = commitFunc;

            _headerLabel = new(new Vector2(20f, 44f), false, _header, new Color(226, 226, 226), AnodyneSharp.Drawing.DrawOrder.TEXT);
            _valueLabel = new(new Vector2(20f, 52f), false, "", new Color(), AnodyneSharp.Drawing.DrawOrder.TEXT);
            _bgBox = new UIEntity(new Vector2(16f, 40f), "pop_menu", 16, 16, AnodyneSharp.Drawing.DrawOrder.TEXTBOX);

            UpdateDisplay();
        }

        public override void Update()
        {
            string inputtedCharacter = InputHandler.ReturnCharacter();
            if (inputtedCharacter.Length > 0)
            {
                _value += inputtedCharacter;
                UpdateDisplay();
            }
            else if (KeyInput.JustPressedKey(Microsoft.Xna.Framework.Input.Keys.Back))
            {
                if (_value.Length > 0)
                {
                    _value = _value.Substring(0, _value.Length - 1);
                    UpdateDisplay();
                }
            }
            else if (KeyInput.JustPressedKey(Microsoft.Xna.Framework.Input.Keys.Escape) || (KeyInput.ControllerMode && KeyInput.JustPressedRebindableKey(KeyFunctions.Cancel)))
            {
                SoundManager.PlaySoundEffect("menu_select");
                this.Exit = true;
            }
            else if (KeyInput.JustPressedKey(Microsoft.Xna.Framework.Input.Keys.Enter) || (KeyInput.ControllerMode && KeyInput.JustPressedRebindableKey(KeyFunctions.Accept)))
            {
                SoundManager.PlaySoundEffect("menu_select");
                _commitFunc(_value);
                this.Exit = true;
            }
        }

        public override void DrawUI()
        {
            _bgBox.Draw();
            _headerLabel.Draw();
            _valueLabel.Draw();
        }

        private void UpdateDisplay()
        {
            if (_value.Length == 0)
            {
                _valueLabel.SetText("[empty]");
                _valueLabel.Color = new Color(116, 140, 144);
            }
            else
            {
                string finalText = "";
                string tempText = _value;

                while (tempText.Length > 18)
                {
                    finalText += tempText.Substring(0, 18);
                    finalText += "\n";
                    tempText = tempText.Substring(18);
                }

                finalText += tempText;

                _valueLabel.SetText(finalText);
                _valueLabel.Color = new Color(184, 32, 0);
            }

            float innerHeight = 8f + _valueLabel.Writer.TotalTextHeight();

            _bgBox = new UIEntity(new Vector2(16f, 40f), "pop_menu", 136, (int)innerHeight + 8, AnodyneSharp.Drawing.DrawOrder.TEXTBOX);
        }
    }
}