aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.Frontend/GraphicsSubsystem/UIManager.cs
blob: fdd5f995825837f145b954b4291db7ee8604fc16 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ShiftOS.Engine;
using ShiftOS.Frontend.Desktop;

namespace ShiftOS.Frontend.GraphicsSubsystem
{
    public static class UIManager
    {
        private static List<GUI.Control> topLevels = new List<GUI.Control>();

        public static void DrawControls(GraphicsDevice graphics, SpriteBatch batch)
        {
            foreach (var ctrl in topLevels)
            {
                using(var bmp = new System.Drawing.Bitmap(ctrl.Width, ctrl.Height))
                {
                    var gfx = System.Drawing.Graphics.FromImage(bmp);
                    ctrl.Paint(gfx);
                    //get the bits of the bitmap
                    var data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                    byte[] rgb = new byte[Math.Abs(data.Stride) * data.Height];
                    Marshal.Copy(data.Scan0, rgb, 0, rgb.Length);
                    bmp.UnlockBits(data);
                    var tex2 = new Texture2D(graphics, bmp.Width, bmp.Height);
                    tex2.SetData<byte>(rgb);
                    batch.Draw(tex2, new Rectangle(ctrl.X, ctrl.Y, ctrl.Width, ctrl.Height), Color.White);
                }
            }
        }

        public static void AddTopLevel(GUI.Control ctrl)
        {
            if (!topLevels.Contains(ctrl))
                topLevels.Add(ctrl);
        }

        public static void ProcessMouseState(MouseState state)
        {
            foreach(var ctrl in topLevels)
            {
                if (ctrl.ProcessMouseState(state) == true)
                    break;
            }
        }



        public static void DrawBackgroundLayer(GraphicsDevice graphics, SpriteBatch batch, int width, int height)
        {
            if (SkinEngine.LoadedSkin == null)
                SkinEngine.Init();
            graphics.Clear(SkinEngine.LoadedSkin.DesktopColor.ToMonoColor());
            var desktopbg = SkinEngine.GetImage("desktopbackground");
            if(desktopbg != null)
            {
                var tex2 = new Texture2D(graphics, desktopbg.Width, desktopbg.Height);
                tex2.SetData<byte>(SkinEngine.LoadedSkin.DesktopBackgroundImage);
                batch.Draw(tex2, new Rectangle(0, 0, width, height), Color.White);
            }
        }

        public static Color ToMonoColor(this System.Drawing.Color color)
        {
            return new Color(color.R, color.G, color.B, color.A);
        }

        internal static void StopHandling(GUI.Control ctrl)
        {
            if (topLevels.Contains(ctrl))
                topLevels.Remove(ctrl);
            ctrl = null;
        }
    }
}