Click here to Skip to main content
15,895,011 members
Articles / Programming Languages / C#

Mars Mission (2) : Explore the Solar System

Rate me:
Please Sign up or sign in to vote.
4.97/5 (36 votes)
20 Nov 2010CPOL19 min read 50.6K   2.2K   45  
strategy/action game defending the solar system : interplanetary space
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace CK_Controls
{
	public class classProBar : Panel
	{
		ProgressBar pro = new ProgressBar();
		Label lbl = new Label();

		public classProBar()
		{
			Controls.Add(pro);
			Controls.Add(lbl);
			SizeChanged += new EventHandler(classpro_SizeChanged);
			Size = new System.Drawing.Size(250, 75);
		}

		void classpro_SizeChanged(object sender, EventArgs e)
		{
			pro.Width
				= lbl.Width
				= Width - 10;

			pro.Left
				= lbl.Left
				= 5;

			lbl.Top = 5;
			pro.Top = lbl.Top + lbl.Height + 5;
			Height = pro.Top + pro.Height + 5;
		}

		public int Maximum
		{
			get { return pro.Maximum; }
			set
			{
				pro.Maximum = value;
			}
		}

		public int Minimum
		{
			get { return pro.Minimum; }
			set
			{
				pro.Minimum = value;
			}
		}

		public int Value
		{
			get { return pro.Value; }
			set { pro.Value = value; }
		}

		public void Inc()
		{
			if (pro.Value < pro.Maximum)
				pro.Value++;
			else
				pro.Value = pro.Minimum;
		}

		public Color TextForeColor
		{
			get { return lbl.ForeColor; }
			set { lbl.ForeColor = value; }
		}

		public Color TextBackColor
		{
			get { return lbl.BackColor; }
			set { lbl.BackColor = value; }
		}

		public string lblText
		{
			get { return lbl.Text; }
			set
			{
				lbl.Text = value;
				lbl.Refresh();
				pro.Refresh();
				Refresh();
			}
		}

		public Font lblFont
		{
			get { return lbl.Font; }
			set
			{
				lbl.Font = value;
				classpro_SizeChanged((object)this, new EventArgs());
			}
		}

	}

	public class labelWarning : Label 
	{
		public class classMinMaxDouble
		{
			public labelWarning lblWarning;
			public classMinMaxDouble(double dblMin, double dblMax)
			{
				_max = dblMax;
				_min = dblMin;
			}

			double _max;
			public double Max
			{
				get { return _max; }
				set 
				{
					if (_max != value)
					{
						_max = value;
						if (lblWarning != null) lblWarning.setColor();
					}
				}
			}
			double _min;
			public double Min
			{
				get { return _min; }
				set 
				{
					if (_min != value)
					{
						_min = value;
						if (lblWarning != null) lblWarning.setColor();
					}
				}
			}
		}

		public classMinMaxDouble cGreenRange = new classMinMaxDouble(0, 70);
		public classMinMaxDouble cYellowRange = new classMinMaxDouble(70, 90);
		public classMinMaxDouble cRedRange = new classMinMaxDouble(90, 100);
		public classMinMaxDouble cValueRange = new classMinMaxDouble(0, 1000);

		public labelWarning()
		{
			cRedRange.lblWarning 
			= cYellowRange.lblWarning 
			= cGreenRange.lblWarning
			= this;
			ForeColor = Color.Black;
		}

		double dblValue;
		public double Value
		{
			get { return dblValue; }
			set
			{
				if (dblValue != value)
				{
					dblValue = value; 
					setColor();
					Text = dblValue.ToString("f3");
				}
			}
		}

		void setColor()
		{
			if (cValueRange.Min == cValueRange.Max)
				return;

			double dblPercentage = (dblValue - cValueRange.Min) / (cValueRange.Max - cValueRange.Min) * 100;
			if (dblPercentage > cRedRange.Min && dblPercentage < cRedRange.Max)
				eColor = enuColor.Red;
			else if (dblPercentage > cYellowRange.Min && dblPercentage < cYellowRange.Max)
				eColor = enuColor.Yellow;
			else
				eColor = enuColor.Green;

			switch (eColor)
			{
				case enuColor.Red:
					BackColor = Color.Red;
					break;

				case enuColor.Yellow:
					BackColor = Color.Yellow;
					break;

				default:
					BackColor = Color.Green;
					break;
			}
			ForeColor = Color.Black;
		}

		public enum enuColor { Green, Yellow, Red};
		public enuColor eColor;
	}

	public class lblButton : Label
	{
		Color clrBackground;
		Color clrBackgroundDull = Color.FromArgb(255, 100, 100, 100);
		Color clrBackgroundHighlight = Color.Yellow;

		public Color BackgroundDull
		{
			get { return clrBackgroundDull; }
			set
			{
				clrBackgroundDull = value;
			}
		}

		public Color BackgroundHighlight
		{
			get { return clrBackgroundHighlight; }
			set
			{
				clrBackgroundHighlight = value;
			}
		}

		bool flag;
		public bool Flag
		{
			get { return flag; }
			set
			{
				flag = value;
				lblButton_MouseLeave((object)this, new EventArgs());
			}
		}

		bool highlightWhenFlagSet = true;
		public bool HighLightWhenFlagSet
		{
			get { return highlightWhenFlagSet; }
			set { highlightWhenFlagSet = value; }
		}

		bool _bolClickTogglesFlag = true;
		public bool ClickTogglesFlag
		{
			get { return _bolClickTogglesFlag; }
			set { _bolClickTogglesFlag = value; }
		}
		
		public lblButton()
		{
			MouseEnter += new EventHandler(lblButton_MouseEnter);
			MouseLeave += new EventHandler(lblButton_MouseLeave);
			MouseClick += new MouseEventHandler(lblButton_MouseClick);
			AutoSize = true;
			VisibleChanged += new EventHandler(lblButton_VisibleChanged);
		}

		void lblButton_VisibleChanged(object sender, EventArgs e)
		{
			reset();
		}

		public void toggle()
		{
			lblButton_MouseClick((object)this, new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 1, 0, 0, 0));
			lblButton_MouseLeave((object)this, new EventArgs());
		}

		public void set()
		{
			flag = true;
			setBackGroundColor();
			lblButton_MouseLeave((object)this, new EventArgs());
		}

		public void reset()
		{
			flag = false;
			setBackGroundColor();
			lblButton_MouseLeave((object)this, new EventArgs());
		}

		void lblButton_MouseClick(object sender, MouseEventArgs e)
		{
			if (ClickTogglesFlag)
				flag = !flag;
			setBackGroundColor();
		}

		void setBackGroundColor()
		{
			clrBackground = (flag && highlightWhenFlagSet ? clrBackgroundHighlight  : clrBackgroundDull);
		}

		void lblButton_MouseLeave(object sender, EventArgs e)
		{
			BackColor = clrBackground;
			ForeColor = Color.FromArgb(255,
									   (BackColor.R + 125) % 256,
									   (BackColor.G + 125) % 256,
									   (BackColor.B + 125) % 256);
		}

		void lblButton_MouseEnter(object sender, EventArgs e)
		{
			ForeColor = clrBackground;
			BackColor = Color.FromArgb(255,
									   (ForeColor.R + 125) % 256,
									   (ForeColor.G + 125) % 256,
									   (ForeColor.B + 125) % 256);
		}
	}

	public class popupListBox : Panel
	{
		public ListBox lst = new ListBox();

		public popupListBox()
		{
			Controls.Add(lst);
			lst.Visible = true;
			lst.MouseEnter += new EventHandler(lst_MouseEnter);
			lst.MouseLeave += new EventHandler(lst_MouseLeave);
			lst.Visible = true;
			//lst.Location = formMarsMission.ptTLScreen;
			Visible = true;
			BackColor = Color.Purple;
			SizeChanged += new EventHandler(popupListBox_SizeChanged);
			ShowList();
		}

		void popupListBox_SizeChanged(object sender, EventArgs e)
		{
			lst.Width = Width;
		}

		void HideList()
		{
			lst.Dock = DockStyle.None;
			Height = lst.ItemHeight + 2;
			lst.Top = -(lst.ItemHeight) * lst.SelectedIndex - 1;
		}

		void ShowList()
		{
			Height = lst.Height;
			lst.Dock = DockStyle.Fill;
			lst.Visible = true;
		}

		void lst_MouseLeave(object sender, EventArgs e)
		{
			HideList();
		}

		void lst_MouseEnter(object sender, EventArgs e)
		{
			ShowList();
		}

		public void add(string strText)
		{
			lst.Items.Add(strText);
			lst.Height = (lst.ItemHeight + 1) * lst.Items.Count;
			ShowList();
		}

		public void clear()
		{
			lst.Items.Clear();
		}

		public Font lblFont
		{
			get { return lst.Font; }
			set
			{
				lst.Font = value;
				ShowList();
			}
		}
	}

	public class classLabelTextAdjustToSize : Label
	{
		string[] strWords;
		string strSplit = " .,:;-+=-_!?";
		string strVowels = "aeiouyAEIOUY";
		string[] strDiphthongs = { "ae", "oi", "ou", "ie." };
		public string _strText;

		
		public override string Text
		{
			get
			{
				return _strText; 
				//return base.Text;
			}
			set
			{
				_strText = value;
				splitWords();
				handleSizeChange();
				//base.Text = value;
			}
		}

		void splitWords()
		{
			strWords = new string[0];
			int intLastWordEnd = 0;
			for (int intCharCounter = 0; intCharCounter < Text.Length; intCharCounter++)
			{
				if (strSplit.Contains(Text[intCharCounter]))
				{
					Array.Resize<string>(ref strWords, strWords.Length + 1);
					strWords[strWords.Length - 1] = Text.Substring(intLastWordEnd, intCharCounter - intLastWordEnd+1);
					intLastWordEnd = intCharCounter + 1;
					while (intCharCounter < Text.Length -1 && strSplit.Contains(Text[intCharCounter+1]))
					{
						strWords[strWords.Length - 1] += Text[++intCharCounter];
						intLastWordEnd ++;
					}
				}
			}
			Array.Resize<string>(ref strWords, strWords.Length + 1);
			strWords[strWords.Length - 1] = Text.Substring(intLastWordEnd);
					
		}

		static Form frmMain;

		Label lblSizer = new Label();

		/// <summary>
		/// will reprint the text to fit the label given its size constraint and FONT setting
		/// </summary>
		public classLabelTextAdjustToSize()
		{
			AutoSize = false;
			if (frmMain == null)
			{
				frmMain = new Form();
				frmMain.VisibleChanged += new EventHandler(frmMain_VisibleChanged);
				frmMain.Visible = true;
				frmMain.ShowInTaskbar = false;
			}

			frmMain.Controls.Add(lblSizer);
			lblSizer.AutoSize = true;
			lblSizer.Font = Font;

			SizeChanged +=new EventHandler(classLabelTextAdjustToSize_SizeChanged);
			FontChanged += new EventHandler(classLabelTextAdjustToSize_FontChanged);
		}

		void frmMain_VisibleChanged(object sender, EventArgs e)
		{
			frmMain.Top = Screen.PrimaryScreen.WorkingArea.Height + 10;				
		}

		void classLabelTextAdjustToSize_FontChanged(object sender, EventArgs e)
		{
			lblSizer.Font = Font;
			handleSizeChange();
		}

		void classLabelTextAdjustToSize_SizeChanged(object sender, EventArgs e)
		{
			handleSizeChange();
		}

		void handleSizeChange()
		{
			string strCurrentText ="";
			//splitWords();
			if (strWords == null)
				return;
			string[] strWordsCopy = new string[strWords.Length];
			Array.Copy(strWords, strWordsCopy, strWords.Length);

			for (int intWordCounter = 0; intWordCounter < strWords.Length; intWordCounter++)
			{
				strCurrentText += strWords[intWordCounter];
			}

			lblSizer.Text = strCurrentText;
			int intShrinkWordCounter = 0;
			while (lblSizer.Width > Width )
			{
				/// limit size of words
				strWordsCopy[intShrinkWordCounter] = ShrinkWord(strWordsCopy[intShrinkWordCounter]);
				strCurrentText = "";
				for (int intWordCounter = 0; intWordCounter < strWordsCopy.Length; intWordCounter++)
					strCurrentText += strWordsCopy[intWordCounter];
				intShrinkWordCounter = (intShrinkWordCounter + 1) % strWordsCopy.Length;
				lblSizer.Text = strCurrentText;
			}
			base.Text = strCurrentText;
		}

		string ShrinkWord(string strWord)
		{
			if (strWord.Length < 2)
				return "";

			// remove the first diphthong encountered and return
			for (int intDiphthongCounter = 0; intDiphthongCounter < strDiphthongs.Length; intDiphthongCounter++)
			{
				if (strWord.Contains(strDiphthongs[intDiphthongCounter]) && strWord.IndexOf(strDiphthongs[intDiphthongCounter])>0)
					return strWord.Replace(strDiphthongs[intDiphthongCounter], "");
			}

			// remove the first vowel encountered starting from the end and moving left, then return
			for (int intCharCounter = strWord.Length -2; intCharCounter >0 ; intCharCounter--)
			{
				if (strVowels.Contains(strWord[intCharCounter]))
					return (strWord.Substring(0, intCharCounter) +(intCharCounter < strWord.Length -1 ? strWord.Substring(intCharCounter + 1) : ""));
			}

			// remove any non-split character off the end then return 
			for (int intCharCounter = strWord.Length - 1; intCharCounter >= 0; intCharCounter--)
			{
				if (!strSplit.Contains(strWord[intCharCounter]))
					return (strWord.Substring(0, intCharCounter) + (intCharCounter < strWord.Length - 1 ? strWord.Substring(intCharCounter + 1) : ""));
			}
			// remove any character off the end and return;
			return strWord.Substring(0, strWord.Length - 2);
		}
		

	}

}

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 Code Project Open License (CPOL)


Written By
CEO unemployable
Canada Canada
Christ Kennedy grew up in the suburbs of Montreal and is a bilingual Quebecois with a bachelor’s degree in computer engineering from McGill University. He is unemployable and currently living in Moncton, N.B. writing his next novel.

Comments and Discussions