aboutsummaryrefslogtreecommitdiff
path: root/ShiftOS.Frontend/GraphicsSubsystem
diff options
context:
space:
mode:
authorMichael <[email protected]>2017-07-04 17:57:29 -0400
committerMichael <[email protected]>2017-07-04 17:57:29 -0400
commit2adb8859edb95921e8f6d3cbb41fdc349825d6f8 (patch)
treeaa1780fe7f8e2c7ab759429aa53717d7a14ca7ef /ShiftOS.Frontend/GraphicsSubsystem
parent5515881e922de087f4e0f5db51ae681bcd7f70d6 (diff)
downloadshiftos_thereturn-2adb8859edb95921e8f6d3cbb41fdc349825d6f8.tar.gz
shiftos_thereturn-2adb8859edb95921e8f6d3cbb41fdc349825d6f8.tar.bz2
shiftos_thereturn-2adb8859edb95921e8f6d3cbb41fdc349825d6f8.zip
abandon system.drawing for anything other than text
Diffstat (limited to 'ShiftOS.Frontend/GraphicsSubsystem')
-rw-r--r--ShiftOS.Frontend/GraphicsSubsystem/GraphicsContext.cs179
-rw-r--r--ShiftOS.Frontend/GraphicsSubsystem/UIManager.cs56
2 files changed, 222 insertions, 13 deletions
diff --git a/ShiftOS.Frontend/GraphicsSubsystem/GraphicsContext.cs b/ShiftOS.Frontend/GraphicsSubsystem/GraphicsContext.cs
new file mode 100644
index 0000000..173a024
--- /dev/null
+++ b/ShiftOS.Frontend/GraphicsSubsystem/GraphicsContext.cs
@@ -0,0 +1,179 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+using ShiftOS.Frontend.Apps;
+
+namespace ShiftOS.Frontend.GraphicsSubsystem
+{
+ public class GraphicsContext
+ {
+ public GraphicsDevice Device
+ {
+ get
+ {
+ return _graphicsDevice;
+ }
+ }
+
+ private int _startx = 0;
+ private int _starty = 0;
+
+ private int _maxwidth = 1;
+ private int _maxheight = 1;
+
+ public int X
+ {
+ get
+ {
+ return _startx;
+ }
+ set
+ {
+ _startx = value;
+ }
+ }
+
+ public int Y
+ {
+ get
+ {
+ return _starty;
+ }
+ set
+ {
+ _starty = value;
+ }
+ }
+
+ public int Width
+ {
+ get
+ {
+ return _maxwidth;
+ }
+ set
+ {
+ _maxwidth = value;
+ }
+ }
+
+ public int Height
+ {
+ get
+ {
+ return _maxheight;
+ }
+ set
+ {
+ _maxheight = value;
+ }
+ }
+
+
+ private GraphicsDevice _graphicsDevice;
+ private SpriteBatch _spritebatch;
+
+ public GraphicsContext(GraphicsDevice device, SpriteBatch batch, int x, int y, int width, int height)
+ {
+ _graphicsDevice = device;
+ _spritebatch = batch;
+ _maxwidth = width;
+ _maxheight = height;
+ _startx = x;
+ _starty = y;
+ }
+
+ public void Clear(Color c)
+ {
+ DrawRectangle(_startx, _starty, _maxwidth, _maxheight, c);
+ }
+
+ public void DrawLine(int x, int y, int x1, int y1, int thickness, Texture2D tex2)
+ {
+ x += _startx;
+ y += _starty;
+ int distance = (int)Vector2.Distance(new Vector2(x, y), new Vector2(x1, y1));
+ float rotation = getRotation(x, y, x1, y1);
+ _spritebatch.Draw(tex2, new Rectangle(x, y, distance, thickness), null, Color.White, rotation, Vector2.Zero, SpriteEffects.None, 0);
+ }
+
+ public void DrawLine(int x, int y, int x1, int y1, int thickness, Color color)
+ {
+ x += _startx;
+ y += _starty;
+ var tex2 = new Texture2D(_graphicsDevice, 1, 1, false, SurfaceFormat.Color);
+ byte[] colordata = new byte[] { color.B, color.G, color.R, color.A };
+ tex2.SetData<byte>(colordata);
+ int distance = (int)Vector2.Distance(new Vector2(x, y), new Vector2(x1, y1));
+ float rotation = getRotation(x, y, x1, y1);
+ _spritebatch.Draw(tex2, new Rectangle(x, y, distance, thickness), null, color, rotation, Vector2.Zero, SpriteEffects.None, 0);
+ }
+
+ public void DrawRectangle(int x, int y, int width, int height, Color color)
+ {
+ x += _startx;
+ y += _starty;
+ var tex2 = new Texture2D(_graphicsDevice, 1, 1, false, SurfaceFormat.Color);
+ byte[] colordata = new byte[] { color.B, color.G, color.R, color.A };
+ tex2.SetData<byte>(colordata);
+ _spritebatch.Draw(tex2, new Rectangle(x, y, width, height), color);
+ }
+
+ public void DrawRectangle(int x, int y, int width, int height, Texture2D tex2)
+ {
+ x += _startx;
+ y += _starty;
+ _spritebatch.Draw(tex2, new Rectangle(x, y, width, height), Color.White);
+ }
+
+ public Vector2 MeasureString(string text, System.Drawing.Font font, int wrapWidth = int.MaxValue)
+ {
+ using(var gfx = System.Drawing.Graphics.FromImage(new System.Drawing.Bitmap(1, 1)))
+ {
+ var s = gfx.SmartMeasureString(text, font, wrapWidth);
+ return new Vector2((float)Math.Ceiling(s.Width), (float)Math.Ceiling(s.Height));
+ }
+ }
+
+ public void DrawString(string text, int x, int y, Color color, System.Drawing.Font font, int wrapWidth = 0)
+ {
+ x += _startx;
+ y += _starty;
+ Vector2 measure;
+ if (wrapWidth == 0)
+ measure = MeasureString(text, font);
+ else
+ measure = MeasureString(text, font, wrapWidth);
+ using(var bmp = new System.Drawing.Bitmap((int)measure.X, (int)measure.Y))
+ {
+ using(var gfx = System.Drawing.Graphics.FromImage(bmp))
+ {
+ gfx.DrawString(text, font, new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B)), 0, 0);
+ }
+ 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];
+ System.Runtime.InteropServices.Marshal.Copy(lck.Scan0, data, 0, data.Length);
+ bmp.UnlockBits(lck);
+ var tex2 = new Texture2D(_graphicsDevice, bmp.Width, bmp.Height);
+ tex2.SetData<byte>(data);
+ _spritebatch.Draw(tex2, new Rectangle(x, y, bmp.Width, bmp.Height), Color.White);
+ }
+ }
+
+ private float getRotation(float x, float y, float x2, float y2)
+ {
+ float adj = x - x2;
+ float opp = y - y2;
+ float tan = opp / adj;
+ float res = MathHelper.ToDegrees((float)Math.Atan2(opp, adj));
+ res = (res - 180) % 360;
+ if (res < 0) { res += 360; }
+ res = MathHelper.ToRadians(res);
+ return res;
+ }
+ }
+}
diff --git a/ShiftOS.Frontend/GraphicsSubsystem/UIManager.cs b/ShiftOS.Frontend/GraphicsSubsystem/UIManager.cs
index b87dd32..bd173d2 100644
--- a/ShiftOS.Frontend/GraphicsSubsystem/UIManager.cs
+++ b/ShiftOS.Frontend/GraphicsSubsystem/UIManager.cs
@@ -40,26 +40,56 @@ namespace ShiftOS.Frontend.GraphicsSubsystem
t.Start();
}
- public static Dictionary<int, Texture2D> TextureCaches = new Dictionary<int, Texture2D>();
+ public static Dictionary<int, RenderTarget2D> TextureCaches = new Dictionary<int, RenderTarget2D>();
- public static void DrawControls(GraphicsDevice graphics, SpriteBatch batch)
+ public static void DrawTArgets(SpriteBatch batch)
{
- foreach (var ctrl in topLevels.ToArray())
+ foreach(var ctrl in topLevels)
{
+ if (ctrl.Visible == true)
+ {
+ int hc = ctrl.GetHashCode();
+ var _target = TextureCaches[hc];
+ batch.Draw(_target, new Rectangle(ctrl.X, ctrl.Y, ctrl.Width, ctrl.Height), Color.White);
+ }
+ }
+ }
+
+ 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,
+ ctrl.Width,
+ ctrl.Height,
+ false,
+ graphics.PresentationParameters.BackBufferFormat,
+ DepthFormat.Depth24);
+ TextureCaches.Add(hc, _target);
+ }
+ else
+ {
+ _target = TextureCaches[hc];
+ }
if (ctrl.RequiresPaint)
{
- var bmp = new System.Drawing.Bitmap(ctrl.Width, ctrl.Height);
- ctrl.Paint(System.Drawing.Graphics.FromImage(bmp));
- if (TextureCaches.ContainsKey(hc))
- {
- TextureCaches[hc].Dispose();
- TextureCaches.Remove(hc);
- }
- TextureCaches.Add(hc, new Texture2D(graphics, ctrl.Width, ctrl.Height));
- TextureCaches[hc].SetData<byte>(ctrl.PaintCache);
+ graphics.SetRenderTarget(_target);
+ graphics.DepthStencilState = new DepthStencilState() { DepthBufferEnable = true };
+ batch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
+ SamplerState.LinearClamp, DepthStencilState.Default,
+ RasterizerState.CullNone);
+ var gfxContext = new GraphicsContext(graphics, batch, 0, 0, _target.Width, _target.Height);
+ ctrl.Paint(gfxContext);
+
+ graphics.SetRenderTarget(null);
+ TextureCaches[hc] = _target;
+ batch.End();
}
- batch.Draw(TextureCaches[hc], new Rectangle(ctrl.X, ctrl.Y, ctrl.Width, ctrl.Height), Color.White);
}
}