Click here to Skip to main content
15,896,063 members
Articles / Web Development / ASP.NET

Legion: Build your own virtual super computer with Silverlight

Rate me:
Please Sign up or sign in to vote.
4.87/5 (139 votes)
27 Oct 2008LGPL321 min read 423.2K   1.1K   335  
Legion is a grid computing framework that uses the Silverlight CLR to execute user definable tasks. It provides grid-wide thread-safe operations for web clients. Client performance metrics, such as bandwidth and processor speed, may be used to tailor jobs. Also includes a WPF Manager application.
/*
<File>
	<Copyright>Copyright © 2007, Daniel Vaughan. All rights reserved.</Copyright>
	<License see="prj:///Documentation/License.txt"/>
	<Owner Name="Daniel Vaughan" Email="dbvaughan@gmail.com"/>
	<CreationDate>2007-12-14 00:42:47Z</CreationDate>
	<LastSubmissionDate>$Date: $</LastSubmissionDate>
	<Version>$Revision: $</Version>
</File>
*/

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Orpius.GridComputing.Controls
{
	/// <summary>
	/// A glass button.
	/// </summary>
	public class ButtonControl : Control
	{
		readonly FrameworkElement root;
		readonly Canvas rootCanvas;
		readonly Rectangle rectangleColor;
		readonly Rectangle rectangleShine;
		readonly Rectangle hitShape;
		readonly Storyboard mouseOverStoryboard;
		readonly Storyboard mouseOutStoryboard;
		readonly TextBlock textBlock;

		/// <summary>
		/// The padding for the text within the control.
		/// </summary>
		const int padding = 5;

		/// <summary>
		/// Set when the control is loaded, and used
		/// to determine whether the control should be layed
		/// out when the text changes.
		/// </summary>
		bool loaded;

		/// <summary>
		/// Gets or sets the text on the button.
		/// </summary>
		/// <value>The text on the button's face.</value>
		public string Text
		{
			get
			{
				return textBlock.Text;
			}
			set
			{
				textBlock.Text = value;
				LayoutControl();
			}
		}

		#region Click event
		event EventHandler<MouseEventArgs> click;

		/// <summary>
		/// Occurs when the button is clicked.
		/// </summary>
		public event EventHandler<MouseEventArgs> Click
		{
			add
			{
				click += value;
			}
			remove
			{
				click -= value;
			}
		}

		protected void OnClick(MouseEventArgs e)
		{
			if (click != null)
			{
				click(this, e);
			}
		}
		#endregion
		public ButtonControl()
		{
			System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("Orpius.GridComputing.Controls.ButtonControl.xaml");
			root = InitializeFromXaml(new System.IO.StreamReader(s).ReadToEnd());
			rootCanvas = (Canvas)root.FindName("GlassButton");
			rectangleColor = (Rectangle)root.FindName("rectangleColor");
			rectangleShine = (Rectangle)root.FindName("rectangleShine");
			hitShape = (Rectangle)rootCanvas.FindName("HitShape");
			textBlock = (TextBlock)rootCanvas.FindName("Text");
			textBlock.Text = string.Empty;

			mouseOverStoryboard = (Storyboard)rootCanvas.FindName("MouseOver");
			mouseOutStoryboard = (Storyboard)rootCanvas.FindName("MouseOut");

			hitShape.MouseEnter += new MouseEventHandler(hitShape_MouseEnter);
			hitShape.MouseLeave += new EventHandler(hitShape_MouseLeave);
			hitShape.MouseLeftButtonUp += new MouseEventHandler(hitShape_MouseLeftButtonUp);
			Loaded += new EventHandler(ButtonControl_Loaded);
		}

		void ButtonControl_Loaded(object sender, EventArgs e)
		{
			loaded = true;
			LayoutControl();
		}

		void hitShape_MouseLeftButtonUp(object sender, MouseEventArgs e)
		{
			OnClick(e);
		}

		void hitShape_MouseLeave(object sender, EventArgs e)
		{
			mouseOutStoryboard.Begin();
		}

		void hitShape_MouseEnter(object sender, MouseEventArgs e)
		{
			mouseOverStoryboard.Begin();
		}

		void LayoutControl()
		{
			if (!loaded)
			{
				return;
			}
			rectangleColor.Width = hitShape.Width = Width;
			rectangleColor.Height = hitShape.Height = Height;
			rectangleShine.Height = Height / 2.5;

			if (Width < 2 * padding)
			{
				return;
			}
			textBlock.Width = Width - 2 * padding;
			double textWidth = textBlock.Width;
			double textWidthActual = textBlock.ActualWidth;
			double textLeft = (Width - textWidthActual) / 2;
			textBlock.SetValue(Canvas.LeftProperty, textLeft);

			rectangleShine.Width = Width - 2 * padding;
			double shineLeft = padding;
			rectangleShine.SetValue(Canvas.LeftProperty, shineLeft);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Engineer
Switzerland Switzerland
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.

Previously Daniel was a nine-time Microsoft MVP and co-founder of Outcoder, a Swiss software and consulting company.

Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.

Daniel is the developer behind several acclaimed mobile apps including Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably Codon.

Would you like Daniel to bring value to your organisation? Please contact

Blog | Twitter


Xamarin Experts
Windows 10 Experts

Comments and Discussions