summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Skin.cs
blob: 77476be03f7c5cb29067f230b4dc215e4a793a4d (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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections.Generic;
using System.IO;
using MonoGame.Extended.BitmapFonts;
using System.Linq;
using System.Reflection;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using MonoGame.Extended.Collections;
using MonoGame.Extended.Gui.Controls;
using MonoGame.Extended.Gui.Serialization;
using MonoGame.Extended.TextureAtlases;
using System.Text.Json.Serialization;
using System.Text.Json;

namespace MonoGame.Extended.Gui
{
    public class Skin
    {
        public Skin()
        {
            TextureAtlases = new List<TextureAtlas>();
            Fonts = new List<BitmapFont>();
            NinePatches = new List<NinePatchRegion2D>();
            Styles = new KeyedCollection<string, ControlStyle>(s => s.Name ?? s.TargetType.Name);
        }

        [JsonPropertyOrder(0)]
        public string Name { get; set; }

        [JsonPropertyOrder(1)]
        public IList<TextureAtlas> TextureAtlases { get; set; }

        [JsonPropertyOrder(2)]
        public IList<BitmapFont> Fonts { get; set; }

       [JsonPropertyOrder(3)]
        public IList<NinePatchRegion2D> NinePatches { get; set; }

        [JsonPropertyOrder(4)]
        public BitmapFont DefaultFont => Fonts.FirstOrDefault();

        [JsonPropertyOrder(5)]
        public Cursor Cursor { get; set; }

        [JsonPropertyOrder(6)]
        public KeyedCollection<string, ControlStyle> Styles { get; private set; }

        public ControlStyle GetStyle(string name)
        {
            if (Styles.TryGetValue(name, out var controlStyle))
                return controlStyle;

            return null;
        }

        public ControlStyle GetStyle(Type controlType)
        {
            return GetStyle(controlType.FullName);
        }

        public void Apply(Control control)
        {
            // TODO: This allocates memory on each apply because it needs to apply styles in reverse
            var types = new List<Type>();
            var controlType = control.GetType();

            while (controlType != null)
            {
                types.Add(controlType);
                controlType = controlType.GetTypeInfo().BaseType;
            }

            for (var i = types.Count - 1; i >= 0; i--)
            {
                var style = GetStyle(types[i]);
                style?.Apply(control);
            }
        }

        public static Skin FromFile(ContentManager contentManager, string path, params Type[] customControlTypes)
        {
            using (var stream = TitleContainer.OpenStream(path))
            {
                return FromStream(contentManager, stream, customControlTypes);
            }
        }

        public static Skin FromStream(ContentManager contentManager, Stream stream, params Type[] customControlTypes)
        {
            var options =  GuiJsonSerializerOptionsProvider.GetOptions(contentManager, customControlTypes);
            return JsonSerializer.Deserialize<Skin>(stream, options);
        }


        public T Create<T>(string template, Action<T> onCreate)
            where T : Control, new()
        {
            var control = new T();
            GetStyle(template).Apply(control);
            onCreate(control);
            return control;
        }

        public Control Create(Type type, string template)
        {
            var control = (Control)Activator.CreateInstance(type);

            if (template != null)
            {
                var style = GetStyle(template);
                if (style != null)
                    style.Apply(control);
                else
                    throw new FormatException($"invalid style {template} for control {type.Name}");
            }

            return control;
        }

        public static Skin Default { get; set; }

        public static Skin CreateDefault(BitmapFont font)
        {
            Default = new Skin
            {
                Fonts = { font },
                Styles =
                {
                    new ControlStyle(typeof(Control)) {
                        {nameof(Control.BackgroundColor), new Color(51, 51, 55)},
                        {nameof(Control.BorderColor), new Color(67, 67, 70)},
                        {nameof(Control.BorderThickness), 1},
                        {nameof(Control.TextColor), new Color(241, 241, 241)},
                        {nameof(Control.Padding), new Thickness(5)},
                        {nameof(Control.DisabledStyle), new ControlStyle(typeof(Control)) {
                                { nameof(Control.TextColor), new Color(78,78,80) }
                            }
                        }
                    },
                    new ControlStyle(typeof(LayoutControl)) {
                        {nameof(Control.BackgroundColor), Color.Transparent},
                        {nameof(Control.BorderColor), Color.Transparent },
                        {nameof(Control.BorderThickness), 0},
                        {nameof(Control.Padding), new Thickness(0)},
                        {nameof(Control.Margin), new Thickness(0)},
                    },
                    new ControlStyle(typeof(ComboBox)) {
                        {nameof(ComboBox.DropDownColor), new Color(71, 71, 75)},
                        {nameof(ComboBox.SelectedItemColor), new Color(0, 122, 204)},
                        {nameof(ComboBox.HorizontalTextAlignment), HorizontalAlignment.Left }
                    },
                    new ControlStyle(typeof(CheckBox))
                    {
                        {nameof(CheckBox.HorizontalTextAlignment), HorizontalAlignment.Left },
                        {nameof(CheckBox.BorderThickness), 0},
                        {nameof(CheckBox.BackgroundColor), Color.Transparent},
                    },
                    new ControlStyle(typeof(ListBox))
                    {
                        {nameof(ListBox.SelectedItemColor), new Color(0, 122, 204)},
                        {nameof(ListBox.HorizontalTextAlignment), HorizontalAlignment.Left }
                    },
                    new ControlStyle(typeof(Label)) {
                        {nameof(Label.BackgroundColor), Color.Transparent},
                        {nameof(Label.TextColor), Color.White},
                        {nameof(Label.BorderColor), Color.Transparent},
                        {nameof(Label.BorderThickness), 0},
                        {nameof(Label.HorizontalTextAlignment), HorizontalAlignment.Left},
                        {nameof(Label.VerticalTextAlignment), VerticalAlignment.Bottom},
                        {nameof(Control.Margin), new Thickness(5,0)},
                        {nameof(Control.Padding), new Thickness(0)},
                    },
                    new ControlStyle(typeof(TextBox)) {
                        {nameof(Control.BackgroundColor), Color.DarkGray},
                        {nameof(Control.TextColor), Color.Black},
                        {nameof(Control.BorderColor), new Color(67, 67, 70)},
                        {nameof(Control.BorderThickness), 2},
                    },
                    new ControlStyle(typeof(TextBox2)) {
                        {nameof(Control.BackgroundColor), Color.DarkGray},
                        {nameof(Control.TextColor), Color.Black},
                        {nameof(Control.BorderColor), new Color(67, 67, 70)},
                        {nameof(Control.BorderThickness), 2},
                    },
                    new ControlStyle(typeof(Button)) {
                        {
                            nameof(Button.HoverStyle), new ControlStyle {
                                {nameof(Button.BackgroundColor), new Color(62, 62, 64)},
                                {nameof(Button.BorderColor), Color.WhiteSmoke }
                            }
                        },
                        {
                            nameof(Button.PressedStyle), new ControlStyle {
                                {nameof(Button.BackgroundColor), new Color(0, 122, 204)}
                            }
                        }
                    },
                    new ControlStyle(typeof(ToggleButton)) {
                        {
                            nameof(ToggleButton.CheckedStyle), new ControlStyle {
                                {nameof(Button.BackgroundColor), new Color(0, 122, 204)}
                            }
                        },
                        {
                            nameof(ToggleButton.CheckedHoverStyle), new ControlStyle {
                                {nameof(Button.BorderColor), Color.WhiteSmoke}
                            }
                        }
                    },
                    new ControlStyle(typeof(ProgressBar)) {
                        {nameof(ProgressBar.BarColor), new Color(0, 122, 204) },
                        {nameof(ProgressBar.Height), 32 },
                        {nameof(ProgressBar.Padding), new Thickness(5, 4)},
                    }
                }
            };
            return Default;
        }
    }
}