about summary refs log tree commit diff stats
path: root/AnodyneArchipelago/Menu/TextEntry.cs
blob: e13331ae4f2e01360e9f90bbff1997934dbb4ee2 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using AnodyneSharp.Input;
using AnodyneSharp.Sounds;
using AnodyneSharp.States;
using AnodyneSharp.UI;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

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);

            TextInputEXT.TextInput += OnTextInput;
            TextInputEXT.StartTextInput();

            UpdateDisplay();
        }

        private void OnTextInput(char ch)
        {
            if (ch == '\b')
            {
                if (_value.Length > 0)
                {
                    _value = _value.Substring(0, _value.Length - 1);
                    UpdateDisplay();
                }
            }
            else if (ch == 22)
            {
                _value += SDL2.SDL.SDL_GetClipboardText();
                UpdateDisplay();
            }
            else if (!char.IsControl(ch))
            {
                _value += ch;
                UpdateDisplay();
            }
        }

        public override void Update()
        {
            if (KeyInput.JustPressedKey(Keys.Escape) || (KeyInput.ControllerMode && KeyInput.JustPressedRebindableKey(KeyFunctions.Cancel)))
            {
                SoundManager.PlaySoundEffect("menu_select");
                this.Exit = true;
            }
            else if (KeyInput.JustPressedKey(Keys.Enter) || (KeyInput.ControllerMode && KeyInput.JustPressedRebindableKey(KeyFunctions.Accept)))
            {
                SoundManager.PlaySoundEffect("menu_select");
                _commitFunc(_value);
                this.Exit = true;
            }

            if (this.Exit)
            {
                TextInputEXT.StopTextInput();
                TextInputEXT.TextInput -= OnTextInput;
            }
        }

        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);
        }
    }
}