Skip to content

Commit

Permalink
Controls: Hud text center, skcontrol bitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
meee1 committed Aug 31, 2021
1 parent 3de321b commit 93abb85
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
41 changes: 35 additions & 6 deletions ExtLibs/Controls/HUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2738,7 +2738,10 @@ void doPaint()
{
//if ((armedtimer.AddSeconds(8) > DateTime.Now))
{
drawstring(HUDT.DISARMED, font, fontsize + 10, (SolidBrush) Brushes.Red, -85,

var size = calcsize(HUDT.DISARMED, font, fontsize + 10, (SolidBrush)Brushes.Red, Width - 50 - 50);

drawstring(HUDT.DISARMED, font, fontsize + 10, (SolidBrush) Brushes.Red, size.Width/ -2/* - 85*/,
halfheight / -3);
statuslast = status;
}
Expand All @@ -2747,7 +2750,8 @@ void doPaint()
{
if ((armedtimer.AddSeconds(8) > DateTime.Now))
{
drawstring(HUDT.ARMED, font, fontsize + 20, (SolidBrush) Brushes.Red, -70,
var size = calcsize(HUDT.ARMED, font, fontsize + 10, (SolidBrush)Brushes.Red, Width - 50 - 50);
drawstring(HUDT.ARMED, font, fontsize + 20, (SolidBrush) Brushes.Red, size.Width / -2/* - 70*/,
halfheight / -3);
statuslast = status;
}
Expand All @@ -2770,9 +2774,11 @@ void doPaint()
else
brush = Brushes.White;

var newfontsize = calcsize(message, font, fontsize + 10, (SolidBrush) brush, Width - 50 - 50);
var newfontsize = calcfontsize(message, font, fontsize + 10, (SolidBrush) brush, Width - 50 - 50);

var size = calcsize(message, font, newfontsize, (SolidBrush)Brushes.Red, Width - 50 - 50);

drawstring(message, font, newfontsize, (SolidBrush) brush, -halfwidth + 50,
drawstring(message, font, newfontsize, (SolidBrush) brush, size.Width / -2,
halfheight / 3);
}

Expand Down Expand Up @@ -2904,7 +2910,7 @@ float wrap360(float noin)
/// </summary>
private readonly GraphicsPath pth = new GraphicsPath();

float calcsize(string text, Font font, float fontsize, SolidBrush brush, int targetwidth)
float calcfontsize(string text, Font font, float fontsize, SolidBrush brush, int targetwidth)
{
if (text == null)
return fontsize;
Expand All @@ -2925,11 +2931,34 @@ float calcsize(string text, Font font, float fontsize, SolidBrush brush, int tar
}

if (size > targetwidth && size > 3)
return calcsize(text, font, fontsize - 1, brush, targetwidth);
return calcfontsize(text, font, fontsize - 1, brush, targetwidth);

return fontsize;
}

Size calcsize(string text, Font font, float fontsize, SolidBrush brush, int targetwidth)
{
if (text == null)
return new Size(0, 0);
float size = 0;
foreach (char cha in text)
{
int charno = (int)cha;
int charid = charno ^ (int)(fontsize * 1000) ^ brush.Color.ToArgb();

if (!charDict.ContainsKey(charid))
{
size += fontsize;
}
else
{
size += charDict[charid].width;
}
}

return new Size((int)size, (int)fontsize);
}

int NextPowerOf2(int n)
{
n |= (n >> 16);
Expand Down
22 changes: 12 additions & 10 deletions ExtLibs/Controls/SKControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ public SKControl()
DoubleBuffered = true;
SetStyle(ControlStyles.ResizeRedraw, true);

designMode = DesignMode || LicenseManager.UsageMode == LicenseUsageMode.Designtime;
designMode = DesignMode || LicenseManager.UsageMode == LicenseUsageMode.Designtime;
}

[Bindable(false)]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsable(EditorBrowsableState.Never)]
public SKSize CanvasSize => bitmap == null ? SKSize.Empty : new SKSize(bitmap.Width, bitmap.Height);
public SKSize CanvasSize => Bitmap == null ? SKSize.Empty : new SKSize(Bitmap.Width, Bitmap.Height);

[Category("Appearance")]
public Bitmap Bitmap => bitmap;

[Category("Appearance")]
public event EventHandler<SKPaintSurfaceEventArgs> PaintSurface;

protected override void OnPaint(PaintEventArgs e)
Expand All @@ -44,7 +46,7 @@ protected override void OnPaint(PaintEventArgs e)
if (info.Width == 0 || info.Height == 0)
return;

var data = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.WriteOnly, bitmap.PixelFormat);
var data = Bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.WriteOnly, Bitmap.PixelFormat);

// create the surface
using (var surface = SKSurface.Create(info, data.Scan0, data.Stride))
Expand All @@ -56,8 +58,8 @@ protected override void OnPaint(PaintEventArgs e)
}

// write the bitmap to the graphics
bitmap.UnlockBits(data);
e.Graphics.DrawImage(bitmap, 0, 0);
Bitmap.UnlockBits(data);
e.Graphics.DrawImage(Bitmap, 0, 0);
}

protected virtual void OnPaintSurface(SKPaintSurfaceEventArgs e)
Expand All @@ -77,22 +79,22 @@ private SKImageInfo CreateBitmap()
{
var info = new SKImageInfo(Width, Height, SKImageInfo.PlatformColorType, SKAlphaType.Premul);

if (bitmap == null || bitmap.Width != info.Width || bitmap.Height != info.Height)
if (Bitmap == null || Bitmap.Width != info.Width || Bitmap.Height != info.Height)
{
FreeBitmap();

if (info.Width != 0 && info.Height != 0)
bitmap = new Bitmap(info.Width, info.Height, PixelFormat.Format32bppPArgb);
}
}

return info;
}

private void FreeBitmap()
{
if (bitmap != null)
if (Bitmap != null)
{
bitmap.Dispose();
Bitmap.Dispose();
bitmap = null;
}
}
Expand Down

0 comments on commit 93abb85

Please sign in to comment.