Skip to content

Commit

Permalink
Adding some test functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dend committed Jul 13, 2021
1 parent 4e8ecf6 commit 81a0c0f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/piglet/piglet.Plugin.Barn/Commands/ShowCPUUsage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Piglet.Plugin.Barn.Helpers;
using Piglet.SDK.Core;
using Piglet.SDK.Interfaces;
using Piglet.SDK.Models;
using Piglet.SDK.Util;
using System;
using System.Drawing;

namespace Piglet.Plugin.Barn.Commands
{
[CompatibleWith(DeviceModel.XL)]
[ExpectedInteractionFormat(CommandType.ACTIVATION)]
class ShowCPUUsage : IPigletCommand
{
public string Name => "Launch Application";
public string Description => "Launches an application on the machine.";

public void ExecuteOnAction(int keyIndex, string arguments)
{
throw new NotImplementedException();
}

public void ExecuteOnActivation(int keyIndex, string arguments)
{
var randomIconFromText = IconGenerator.GenerateTestImageFromText("92%", new Font("Consolas", 12), Color.Red, Color.Blue);
var resizeImage = ImageHelpers.ResizeImage(ImageHelpers.GetImageBuffer(randomIconFromText), DeviceConstants.XLButtonSize, DeviceConstants.XLButtonSize);


}
}
}
38 changes: 38 additions & 0 deletions src/piglet/piglet.Plugin.Barn/Helpers/IconGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Drawing;
using System.Drawing.Text;

namespace Piglet.Plugin.Barn.Helpers
{
internal class IconGenerator
{
// Adapted from this snippet: https://stackoverflow.com/a/2070493/303696
internal static Image GenerateTestImageFromText(string text, Font font, Color textColor, Color backgroundColor)
{
Image img = new Bitmap(1, 1);
Graphics drawing = Graphics.FromImage(img);

SizeF textSize = drawing.MeasureString(text, font);

img.Dispose();
drawing.Dispose();

img = new Bitmap((int)textSize.Width, (int)textSize.Height);

drawing = Graphics.FromImage(img);

drawing.Clear(backgroundColor);

Brush textBrush = new SolidBrush(textColor);

drawing.TextRenderingHint = TextRenderingHint.AntiAlias;
drawing.DrawString(text, font, textBrush, 0, 0);

drawing.Save();

textBrush.Dispose();
drawing.Dispose();

return img;
}
}
}
7 changes: 7 additions & 0 deletions src/piglet/piglet.SDK/Util/ImageHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,12 @@ public static Image GetImage(byte[] buffer)

return image;
}

public static byte[] GetImageBuffer(Image image)
{
ImageConverter converter = new();
byte[] buffer = (byte[])converter.ConvertTo(image, typeof(byte[]));
return buffer;
}
}
}

0 comments on commit 81a0c0f

Please sign in to comment.