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
|
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(0, 0, _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))
{
var textformat = new System.Drawing.StringFormat(System.Drawing.StringFormat.GenericTypographic);
textformat.FormatFlags = System.Drawing.StringFormatFlags.MeasureTrailingSpaces;
textformat.Trimming = System.Drawing.StringTrimming.None;
textformat.FormatFlags |= System.Drawing.StringFormatFlags.NoClip;
gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
gfx.DrawString(text, font, new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B)), 0, 0, textformat);
}
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);
for(int i = 0; i < data.Length; i += 4)
{
byte r = data[i];
byte b = data[i + 2];
data[i] = b;
data[i + 2] = r;
}
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;
}
}
}
|