aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.Frontend/Apps/Shifter.cs
blob: 0572ed530e42bd5e24ba00f955b3ec71f8767078 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Newtonsoft.Json;
using ShiftOS.Engine;
using ShiftOS.Frontend.GUI;

namespace ShiftOS.Frontend.Apps
{
    [WinOpen("shifter")]
    [DefaultTitle("Shifter")]
    [Launcher("Shifter", false, null, "Customization")]
    public class Shifter : Control, IShiftOSWindow
    {
        private const int metaWidth = 150;
        private Button _apply = null;
        private List<Button> _meta = new List<Button>();
        private Skin _skin = null;
        private string _metaCurrent = "";
        private string _catCurrent = "";
        private List<Button> _catList = new List<Button>();

        public Shifter()
        {
            _apply = new GUI.Button();

            AddControl(_apply);
            _apply.Width = metaWidth;
            _apply.Height = 50;
            _skin = JsonConvert.DeserializeObject<Skin>(JsonConvert.SerializeObject(SkinEngine.LoadedSkin));
        }

        protected override void OnLayout(GameTime gameTime)
        {
            base.OnLayout(gameTime);
            _apply.X = 10;
            _apply.Y = Height - _apply.Height - 10;
            _apply.Text = "Apply Changes";

            int metay = 10;
            foreach(var btn in _meta)
            {
                btn.X = 10;
                btn.Y = metay;
                btn.Height = 25;
                btn.Width = metaWidth;
                metay += btn.Height + 10;
            }

            int catY = Height - 10;
            foreach(var btn in _catList)
            {
                btn.Width = metaWidth;
                btn.Height = 25;
                catY -= btn.Height;
                btn.Y = catY;
                catY -= 10;
                btn.X = 20 + metaWidth;
            }
        }

        public void ResetMetaListing()
        {
            var type = _skin.GetType();
            List<string> metanames = new List<string>();
            foreach (var field in type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
            {
                var meta = field.GetCustomAttributes(false).FirstOrDefault(x => x is ShifterMetaAttribute) as ShifterMetaAttribute;
                if(meta != null)
                {
                    if (!metanames.Contains(meta.Meta))
                        metanames.Add(meta.Meta);
                }
            }
            while(_meta.Count > 0)
            {
                RemoveControl(_meta[0]);
                ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; //IT'S THE SEMICOLON PARTYFEST
                //that's actually valid C#
                //like, VS isn't fucking freaking out
                //wtf
                //Microsoft, you're drunk.
                _meta.RemoveAt(0);

            }
            foreach (var meta in metanames)
            {
                var button = new Button();
                button.Click += () =>
                {
                    _metaCurrent = meta;
                    ResetCategoryListing();
                };
                button.Text = Localization.Parse(meta);
                AddControl(button);
                _meta.Add(button);
            }
        }

        public void ResetCategoryListing()
        {
            while(_catList.Count > 0)
            {
                RemoveControl(_catList[0]);
                _catList.RemoveAt(0);
            }
            List<string> catnames = new List<string>();
            var type = _skin.GetType();
            foreach (var field in type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
            {
                var meta = field.GetCustomAttributes(false).FirstOrDefault(x => x is ShifterMetaAttribute) as ShifterMetaAttribute;
                if (meta != null)
                {
                    if(meta.Meta == _metaCurrent)
                    {
                        var cat = field.GetCustomAttributes(false).FirstOrDefault(x => x is ShifterCategoryAttribute) as ShifterCategoryAttribute;
                        if(cat != null)
                        {
                            if (!catnames.Contains(cat.Category))
                                catnames.Add(cat.Category);
                        }
                    }
                }
            }

            foreach (var meta in catnames)
            {
                var button = new Button();
                button.Click += () =>
                {
                    _catCurrent = meta;
                    //ResetValueEditor();
                };
                button.Text = Localization.Parse(meta);
                AddControl(button);
                _catList.Add(button);
            }

        }

        public void OnLoad()
        {
            ResetMetaListing();
        }

        public void OnSkinLoad()
        {
        }

        public bool OnUnload()
        {
            return true;
        }

        public void OnUpgrade()
        {
        }
    }
}