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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
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 MonoGame.Extended.Input.InputListeners;
using ShiftOS.Engine;
using ShiftOS.Frontend.Desktop;
using ShiftOS.Frontend.GUI;
namespace ShiftOS.Frontend.GraphicsSubsystem
{
public static class UIManager
{
private static List<GUI.Control> topLevels = new List<GUI.Control>();
public static System.Drawing.Size Viewport { get; set; }
public static GUI.Control FocusedControl = null;
private static ShiftOS _game = null;
public static void Init(ShiftOS sentience)
{
_game = sentience;
}
public static bool Fullscreen
{
get
{
return _game.graphicsDevice.IsFullScreen;
}
set
{
var uconf = Objects.UserConfig.Get();
uconf.Fullscreen = value;
System.IO.File.WriteAllText("config.json", Newtonsoft.Json.JsonConvert.SerializeObject(uconf, Newtonsoft.Json.Formatting.Indented));
_game.graphicsDevice.IsFullScreen = value;
_game.graphicsDevice.ApplyChanges();
}
}
public static System.Drawing.Size ScreenSize
{
get
{
return new System.Drawing.Size(_game.graphicsDevice.PreferredBackBufferWidth, _game.graphicsDevice.PreferredBackBufferHeight);
}
}
public static void BringToFront(GUI.Control ctrl)
{
topLevels.Remove(ctrl);
topLevels.Add(ctrl);
}
public static void LayoutUpdate(GameTime gameTime)
{
foreach (var toplevel in topLevels.ToArray())
toplevel.Layout(gameTime);
}
public static void Animate(object owner, System.Reflection.PropertyInfo prop, double from, double to, int timeMs)
{
var t = new System.Threading.Thread(() =>
{
for(int i = 0; i < timeMs; i++)
{
double value = ProgressBar.linear(i, 0, timeMs, from, to);
prop.SetValue(owner, value);
System.Threading.Thread.Sleep(1);
}
});
t.IsBackground = true;
t.Start();
}
public static Dictionary<int, RenderTarget2D> TextureCaches = new Dictionary<int, RenderTarget2D>();
public static void DrawTArgets(SpriteBatch batch)
{
foreach(var ctrl in topLevels.ToArray())
{
if (ctrl.Visible == true)
{
int hc = ctrl.GetHashCode();
if (!TextureCaches.ContainsKey(hc))
{
ctrl.Invalidate();
continue;
}
var _target = TextureCaches[hc];
if (ExperimentalEffects)
{
for (int i = 5; i > 0; i--)
{
batch.Draw(_target, new Rectangle(ctrl.X - i, ctrl.Y - i, ctrl.Width+(i*2), ctrl.Height+(i*2)), new Color(Color.Black, 255 / (i * 2)));
}
}
batch.Draw(_target, new Rectangle(ctrl.X, ctrl.Y, ctrl.Width, ctrl.Height), _game.UITint);
}
}
}
public static void SendToBack(Control ctrl)
{
topLevels.Remove(ctrl);
topLevels.Insert(0, ctrl);
}
public static void DrawControlsToTargets(GraphicsDevice graphics, SpriteBatch batch, int width, int height)
{
foreach (var ctrl in topLevels.ToArray().Where(x=>x.Visible==true))
{
RenderTarget2D _target;
int hc = ctrl.GetHashCode();
if (!TextureCaches.ContainsKey(hc))
{
_target = new RenderTarget2D(
graphics,
Math.Max(1,ctrl.Width),
Math.Max(1,ctrl.Height),
false,
graphics.PresentationParameters.BackBufferFormat,
DepthFormat.Depth24);
TextureCaches.Add(hc, _target);
}
else
{
_target = TextureCaches[hc];
if(_target.Width != ctrl.Width || _target.Height != ctrl.Height)
{
_target = new RenderTarget2D(
graphics,
Math.Max(1,ctrl.Width),
Math.Max(1,ctrl.Height),
false,
graphics.PresentationParameters.BackBufferFormat,
DepthFormat.Depth24);
TextureCaches[hc] = _target;
}
}
if (ctrl.RequiresPaint)
{
graphics.SetRenderTarget(_target);
graphics.DepthStencilState = new DepthStencilState() { DepthBufferEnable = true };
batch.Begin(SpriteSortMode.Immediate, BlendState.NonPremultiplied,
SamplerState.LinearClamp, DepthStencilState.Default,
RasterizerState.CullNone);
graphics.Clear(Color.Transparent);
var gfxContext = new GraphicsContext(graphics, batch, 0, 0, _target.Width, _target.Height);
ctrl.Paint(gfxContext);
graphics.SetRenderTarget(_game.GameRenderTarget);
TextureCaches[hc] = _target;
batch.End();
}
}
}
public static void AddTopLevel(GUI.Control ctrl)
{
if (!topLevels.Contains(ctrl))
topLevels.Add(ctrl);
FocusedControl = ctrl;
}
public static void InvalidateAll()
{
foreach(var ctrl in topLevels)
{
ctrl.Invalidate();
}
}
public static void ProcessMouseState(MouseState state, double lastLeftClickMS)
{
foreach(var ctrl in topLevels.ToArray())
{
ctrl.ProcessMouseState(state, lastLeftClickMS);
}
}
public static void ProcessKeyEvent(KeyEvent e)
{
if (e.ControlDown && e.Key == Keys.T)
{
AppearanceManager.SetupWindow(new Apps.Terminal());
return;
}
FocusedControl?.ProcessKeyEvent(e);
}
private static Texture2D DesktopBackground = null;
public static Dictionary<string, Texture2D> SkinTextures = new Dictionary<string, Texture2D>();
public static void ResetSkinTextures(GraphicsDevice graphics)
{
SkinTextures.Clear();
foreach(var byteArray in SkinEngine.LoadedSkin.GetType().GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Where(x=>x.FieldType == typeof(byte[])))
{
var imgAttrib = byteArray.GetCustomAttributes(false).FirstOrDefault(x => x is ImageAttribute) as ImageAttribute;
if(imgAttrib != null)
{
var img = SkinEngine.GetImage(imgAttrib.Name);
if(img != null)
{
var bmp = (System.Drawing.Bitmap)img;
var lck = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
var data = new byte[Math.Abs(lck.Stride) * lck.Height];
Marshal.Copy(lck.Scan0, data, 0, data.Length);
bmp.UnlockBits(lck);
var tex2 = new Texture2D(graphics, bmp.Width, bmp.Height);
for(int i = 0; i < data.Length; i += 4)
{
byte r = data[i];
byte b = data[i + 2];
if (r == 1 && b == 1 && data[i + 1] == 1)
{
data[i + 3] = 0;
}
data[i] = b;
data[i + 2] = r;
}
tex2.SetData<byte>(data);
SkinTextures.Add(imgAttrib.Name, tex2);
}
}
}
foreach(var colorfield in SkinEngine.LoadedSkin.GetType().GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Where(x=>x.FieldType == typeof(System.Drawing.Color)))
{
var color = (System.Drawing.Color)colorfield.GetValue(SkinEngine.LoadedSkin);
var tex2 = new Texture2D(graphics, 1, 1);
tex2.SetData<byte>(new[] { color.R, color.G, color.B, color.A });
SkinTextures.Add(colorfield.Name, tex2);
}
var pureWhite = new Texture2D(graphics, 1, 1);
pureWhite.SetData<byte>(new byte[] { 255, 255, 255, 255 });
SkinTextures.Add("PureWhite", pureWhite);
}
public static void SetUITint(Color color)
{
_game.UITint = color;
}
public static bool ExperimentalEffects = true;
public static Queue<Action> CrossThreadOperations = new Queue<Action>();
internal static GraphicsDevice GraphicsDevice;
public static void DrawBackgroundLayer(GraphicsDevice graphics, SpriteBatch batch, int width, int height)
{
if (SkinEngine.LoadedSkin == null)
SkinEngine.Init();
batch.Draw(SkinTextures["DesktopColor"], new Rectangle(0, 0, Viewport.Width, Viewport.Height), _game.UITint);
graphics.Clear(SkinEngine.LoadedSkin.DesktopColor.ToMonoColor());
if (SkinTextures.ContainsKey("desktopbackground"))
{
batch.Draw(SkinTextures["desktopbackground"], new Rectangle(0, 0, Viewport.Width, Viewport.Height), _game.UITint);
}
}
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);
int hc = ctrl.GetHashCode();
if (TextureCaches.ContainsKey(hc))
{
TextureCaches[hc].Dispose();
TextureCaches.Remove(hc);
}
ctrl = null;
}
}
public class KeyEvent
{
public KeyEvent(KeyboardEventArgs e)
{
ControlDown = false;
ShiftDown = e.Modifiers.HasFlag(KeyboardModifiers.Shift);
ControlDown = e.Modifiers.HasFlag(KeyboardModifiers.Control);
AltDown = e.Modifiers.HasFlag(KeyboardModifiers.Alt);
Key = e.Key;
KeyChar = e.Character ?? '\0' ;
}
public bool ControlDown { get; private set; }
public bool AltDown { get; private set; }
public bool ShiftDown { get; set; }
public Keys Key { get; private set; }
public char KeyChar { get; private set; }
}
}
|