// ==PREPROCESSOR==
// @name "BoxBlur"
// @author "T.P Wang"
// @import "%fb2k_component_path%docs\flags.txt"
// @import "%fb2k_component_path%docs\helpers.txt"
// ==/PREPROCESSOR==

var ww = 0, wh = 0;
var img_to_blur;
// FontStyle.Bold is defined in docs\flags.txt
var g_font = gdi.Font('Segoe UI', 20, FontStyle.Bold);
var text_to_draw = '"Box Blur" Sample';

function on_paint(gr) {
	img_to_blur && gr.DrawImage(img_to_blur, 0, 0, ww, wh, 0, 0, ww, wh);
	// TextRenderingHint.ClearTypeGridFit is defined in docs\flags.txt
	gr.SetTextRenderingHint(TextRenderingHint.ClearTypeGridFit);
	// RGB function is defined in docs\helpers.txt
	// String* are defined inside docs\flags.txt
	gr.DrawString(text_to_draw, g_font, RGB(0, 0, 255), 0, 0, ww, wh, StringFormat(StringAlignment.Center, StringAlignment.Center));
}

function on_size() {
	ww = window.Width;
	wh = window.Height;
	if (ww <= 0 || wh <= 0) return;
	// Create a image which background is true transparent
	img_to_blur = gdi.CreateImage(ww, wh);
	// Get graphics interface like "gr" in on_paint
	var g = img_to_blur.GetGraphics();
	// You cannot set it to 'ClearTypeGridFit' if background is TRANSPARENT
	g.SetTextRenderingHint(TextRenderingHint.AntiAlias);
	// If you wanto use clear type, fill a solid rect:
	// g.FillSolidRect(0, 0, ww, wh, RGB(255, 255, 255));
	// g.SetTextRenderingHint(TextRenderingHint.ClearTypeGridFit);
	
	// RGB function is defined inside docs\helpers.txt
	// String* are defined inside docs\flags.txt
	g.DrawString(text_to_draw, g_font, RGB(0, 0, 255), 0, 0, ww, wh, StringFormat(StringAlignment.Center, StringAlignment.Center));
	// Release graphics (or it will remain in memory util system collects GC)
	img_to_blur.ReleaseGraphics(g);
	// Make box blur, radius = 6, iteration = 2
	img_to_blur.BoxBlur(6, 2);
}
