aboutsummaryrefslogtreecommitdiff
path: root/source/ui/elements/game/controls/ControlTips.cs
blob: 904b3cc605980f0d3d97dc28d59bd494b458675d (plain)
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
using System.Collections.Generic;
using Celesteia.Game.Input;
using Celesteia.Resources;
using Celesteia.UI.Properties;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Celesteia.UI.Elements.Game.Controls {
    public class ControlTips : Container {
        private TextProperties _properties;
        private Dictionary<Keys, string> _keyboardControls = new Dictionary<Keys, string>();
        private List<string> _lines = new List<string>();
        private List<Label> _labels = new List<Label>();

        public ControlTips(Rect rect) : base(rect) {
            _properties = new TextProperties()
                .SetColor(Color.White)
                .SetFont(ResourceManager.Fonts.DEFAULT)
                .SetFontSize(12f)
                .SetTextAlignment(TextAlignment.Left);
        }

        private int lineHeight = 16;
        private Rect LineRect(int line) => new Rect(
            AbsoluteUnit.WithValue(0f),
            AbsoluteUnit.WithValue(line * lineHeight),
            new RelativeUnit(1f, GetRect(), RelativeUnit.Orientation.Horizontal),
            AbsoluteUnit.WithValue(lineHeight)
        );

        private void UpdateLines() {
            _labels.Clear();

            foreach (Keys keys in _keyboardControls.Keys) _lines.Add($"[{keys}] {_keyboardControls[keys]}");

            for (int i = 0; i < _lines.Count; i++) {
                Label label = new Label(LineRect(i - (_lines.Count / 2)))
                    .SetTextProperties(_properties)
                    .SetText(_lines[i]);
                label.SetParent(this);
                _labels.Add(label);
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            foreach (Label l in _labels) l.Draw(spriteBatch);
        }
    }
}