summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Screen.cs
blob: 821a1e2b00b03b8cf42c949f317e11865349945d (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
using System;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using MonoGame.Extended.Gui.Controls;
using MonoGame.Extended.Gui.Serialization;

namespace MonoGame.Extended.Gui
{
    public class Screen //: Element<GuiSystem>, IDisposable
    {
        public Screen()
        {
            //Windows = new WindowCollection(this) { ItemAdded = w => _isLayoutRequired = true };
        }

        public virtual void Dispose()
        {
        }

        private Control _content;
        [JsonPropertyOrder(1)]
        public Control Content
        {
            get { return _content; }
            set
            {
                if (_content != value)
                {
                    _content = value;
                    _isLayoutRequired = true;
                }
            }
        }

        //[JsonIgnore]
        //public WindowCollection Windows { get; }

        public float Width { get; private set; }
        public float Height { get; private set; }
        public Size2 Size => new Size2(Width, Height);
        public bool IsVisible { get; set; } = true;

        private bool _isLayoutRequired;
        [JsonIgnore]
        public bool IsLayoutRequired => _isLayoutRequired || Content.IsLayoutRequired;

        public virtual void Update(GameTime gameTime)
        {

        }

        public void Show()
        {
            IsVisible = true;
        }

        public void Hide()
        {
            IsVisible = false;
        }

        public T FindControl<T>(string name)
            where T : Control
        {
            return FindControl<T>(Content, name);
        }

        private static T FindControl<T>(Control rootControl, string name)
            where T : Control
        {
            if (rootControl.Name == name)
                return rootControl as T;

            foreach (var childControl in rootControl.Children)
            {
                var control = FindControl<T>(childControl, name);

                if (control != null)
                    return control;
            }

            return null;
        }

        public void Layout(IGuiContext context, Rectangle rectangle)
        {
            Width = rectangle.Width;
            Height = rectangle.Height;

            LayoutHelper.PlaceControl(context, Content, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);

            //foreach (var window in Windows)
            //    LayoutHelper.PlaceWindow(context, window, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);

            _isLayoutRequired = false;
            Content.IsLayoutRequired = false;
        }

        //public override void Draw(IGuiContext context, IGuiRenderer renderer, float deltaSeconds)
        //{
        //    renderer.DrawRectangle(BoundingRectangle, Color.Green);
        //}

        public static Screen FromStream(ContentManager contentManager, Stream stream, params Type[] customControlTypes)
        {
            return FromStream<Screen>(contentManager, stream, customControlTypes);
        }

        public static TScreen FromStream<TScreen>(ContentManager contentManager, Stream stream, params Type[] customControlTypes)
            where TScreen : Screen
        {
            var skinService = new SkinService();
            var options = GuiJsonSerializerOptionsProvider.GetOptions(contentManager, customControlTypes);
            options.Converters.Add(new SkinJsonConverter(contentManager, skinService, customControlTypes));
            options.Converters.Add(new ControlJsonConverter(skinService, customControlTypes));
            return JsonSerializer.Deserialize<TScreen>(stream, options);
        }

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

        public static TScreen FromFile<TScreen>(ContentManager contentManager, string path, params Type[] customControlTypes)
            where TScreen : Screen
        {
            using (var stream = TitleContainer.OpenStream(path))
            {
                return FromStream<TScreen>(contentManager, stream, customControlTypes);
            }
        }
    }
}