diff options
Diffstat (limited to 'AnodyneArchipelago/Menu/TextEntry.cs')
-rw-r--r-- | AnodyneArchipelago/Menu/TextEntry.cs | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/AnodyneArchipelago/Menu/TextEntry.cs b/AnodyneArchipelago/Menu/TextEntry.cs new file mode 100644 index 0000000..8be54f5 --- /dev/null +++ b/AnodyneArchipelago/Menu/TextEntry.cs | |||
@@ -0,0 +1,101 @@ | |||
1 | using AnodyneSharp.Input; | ||
2 | using AnodyneSharp.Sounds; | ||
3 | using AnodyneSharp.States; | ||
4 | using AnodyneSharp.UI; | ||
5 | using Microsoft.Xna.Framework; | ||
6 | |||
7 | namespace AnodyneArchipelago.Menu | ||
8 | { | ||
9 | internal class TextEntry : State | ||
10 | { | ||
11 | public delegate void CommitChange(string value); | ||
12 | |||
13 | private readonly string _header; | ||
14 | private readonly CommitChange _commitFunc; | ||
15 | |||
16 | private string _value; | ||
17 | |||
18 | private UILabel _headerLabel; | ||
19 | private UILabel _valueLabel; | ||
20 | private UIEntity _bgBox; | ||
21 | |||
22 | public TextEntry(string header, string value, CommitChange commitFunc) | ||
23 | { | ||
24 | _header = header; | ||
25 | _value = value; | ||
26 | _commitFunc = commitFunc; | ||
27 | |||
28 | _headerLabel = new(new Vector2(20f, 44f), false, _header, new Color(226, 226, 226), AnodyneSharp.Drawing.DrawOrder.TEXT); | ||
29 | _valueLabel = new(new Vector2(20f, 52f), false, "", new Color(), AnodyneSharp.Drawing.DrawOrder.TEXT); | ||
30 | _bgBox = new UIEntity(new Vector2(16f, 40f), "pop_menu", 16, 16, AnodyneSharp.Drawing.DrawOrder.TEXTBOX); | ||
31 | |||
32 | UpdateDisplay(); | ||
33 | } | ||
34 | |||
35 | public override void Update() | ||
36 | { | ||
37 | string inputtedCharacter = InputHandler.ReturnCharacter(); | ||
38 | if (inputtedCharacter.Length > 0) | ||
39 | { | ||
40 | _value += inputtedCharacter; | ||
41 | UpdateDisplay(); | ||
42 | } | ||
43 | else if (KeyInput.JustPressedKey(Microsoft.Xna.Framework.Input.Keys.Back)) | ||
44 | { | ||
45 | if (_value.Length > 0) | ||
46 | { | ||
47 | _value = _value.Substring(0, _value.Length - 1); | ||
48 | UpdateDisplay(); | ||
49 | } | ||
50 | } | ||
51 | else if (KeyInput.JustPressedKey(Microsoft.Xna.Framework.Input.Keys.Escape) || (KeyInput.ControllerMode && KeyInput.JustPressedRebindableKey(KeyFunctions.Cancel))) | ||
52 | { | ||
53 | SoundManager.PlaySoundEffect("menu_select"); | ||
54 | this.Exit = true; | ||
55 | } | ||
56 | else if (KeyInput.JustPressedKey(Microsoft.Xna.Framework.Input.Keys.Enter) || (KeyInput.ControllerMode && KeyInput.JustPressedRebindableKey(KeyFunctions.Accept))) | ||
57 | { | ||
58 | SoundManager.PlaySoundEffect("menu_select"); | ||
59 | _commitFunc(_value); | ||
60 | this.Exit = true; | ||
61 | } | ||
62 | } | ||
63 | |||
64 | public override void DrawUI() | ||
65 | { | ||
66 | _bgBox.Draw(); | ||
67 | _headerLabel.Draw(); | ||
68 | _valueLabel.Draw(); | ||
69 | } | ||
70 | |||
71 | private void UpdateDisplay() | ||
72 | { | ||
73 | if (_value.Length == 0) | ||
74 | { | ||
75 | _valueLabel.SetText("[empty]"); | ||
76 | _valueLabel.Color = new Color(116, 140, 144); | ||
77 | } | ||
78 | else | ||
79 | { | ||
80 | string finalText = ""; | ||
81 | string tempText = _value; | ||
82 | |||
83 | while (tempText.Length > 18) | ||
84 | { | ||
85 | finalText += tempText.Substring(0, 18); | ||
86 | finalText += "\n"; | ||
87 | tempText = tempText.Substring(18); | ||
88 | } | ||
89 | |||
90 | finalText += tempText; | ||
91 | |||
92 | _valueLabel.SetText(finalText); | ||
93 | _valueLabel.Color = new Color(184, 32, 0); | ||
94 | } | ||
95 | |||
96 | float innerHeight = 8f + _valueLabel.Writer.TotalTextHeight(); | ||
97 | |||
98 | _bgBox = new UIEntity(new Vector2(16f, 40f), "pop_menu", 136, (int)innerHeight + 8, AnodyneSharp.Drawing.DrawOrder.TEXTBOX); | ||
99 | } | ||
100 | } | ||
101 | } | ||