public FishBowl(FishWindow wndParent, IntPtr hBgd) : base(hBgd) { //changing these requires resetting the fishbowl _colorAgeGradient = 1; _FishInitPct = 10; _SharkInitPct = 10; CellWidth = CellHeight = 10; //these can be changed while running _SharkStarve = 6; _FishBreedAge = 3; _SharkBreedAge = 5; _FishLifeLength = 22; _SharkLifeLength = 33; _FishNumMovesPerYear = 1; _SharkNumMovesPerYear = 1; _wndParent = wndParent; _Instance = this; _BgdBrush = hBgd; _wndParent.Closing += (o, e) => { _StopRequested = true; }; }
public FishWindow() { InitializeComponent(); WindowState = System.Windows.WindowState.Maximized; this.Loaded += (ol, el) => { this.Top = 0; this.Left = 0; try { // Make a namespace referring to our namespace and assembly // using the prefix "l:" //xmlns:l=""clr-namespace:Fish;assembly=Fish""" var nameSpace = this.GetType().Namespace; var asm = System.IO.Path.GetFileNameWithoutExtension( Assembly.GetExecutingAssembly().Location); var xmlns = string.Format( @"xmlns:l=""clr-namespace:{0};assembly={1}""", nameSpace, asm); //there are a lot of quotes (and braces) in XAML //and the C# string requires quotes to be doubled var strxaml = @"<Grid xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" " + xmlns + // add our xaml namespace @" Margin=""5,5,5,5""> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height=""25"" /> </Grid.RowDefinitions> <DockPanel Grid.Row=""0""> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width = ""70""/> <ColumnDefinition/> </Grid.ColumnDefinitions> <StackPanel Name=""inputPanel"" Orientation=""Vertical"" > <CheckBox Content=""_Running"" IsChecked= ""{Binding Path=IsRunning}"" /> <CheckBox Content=""_Circles"" IsChecked= ""{Binding Path=UseCircles}"" /> <CheckBox Content=""_Action1PerYear"" IsChecked= ""{Binding Path=_OneActionPerYear}"" ToolTip=""Can do only one of move AND eat AND give birth in same year?"" /> <CheckBox Content=""_Torus"" IsChecked= ""{Binding Path=_Torus}"" ToolTip=""The ocean wraps around the edges: the top and the bottom are neighbors"" /> <Label Content=""ColorGrad""/> <l:MyTextBox Text =""{Binding Path=ColorAgeGradient}"" ToolTip=""As an animcal gets older, darken the color (256 is max color). 0 makes it go much faster"" /> <Label Content=""CellWidth""/> <l:MyTextBox Text =""{Binding Path=CellWidth}"" ToolTip=""Width of a cell"" /> <Label Content=""CellHeight""/> <l:MyTextBox Text =""{Binding Path=CellHeight}"" ToolTip=""Height of a cell"" /> <Label Content=""Starve""/> <l:MyTextBox Text =""{Binding Path=_SharkStarve}"" ToolTip=""How long a shark can go without eating"" /> <Label Content=""FishBreed""/> <l:MyTextBox Text =""{Binding Path=_FishBreedAge}"" ToolTip=""How old a Fish has to be before it can give birth (requires neighboring empty cell)"" /> <Label Content=""SharkBreed""/> <l:MyTextBox Text =""{Binding Path=_SharkBreedAge}"" ToolTip=""How old a Shark has to be before it can give birth (requires neighboring empty cell)"" /> <Label Content=""FishLifeLength""/> <l:MyTextBox Text =""{Binding Path=_FishLifeLength}"" ToolTip=""How old a Fish is before it dies of old age"" /> <Label Content=""SharkLifeLength""/> <l:MyTextBox Text =""{Binding Path=_SharkLifeLength}"" ToolTip=""How old a Shark is before it dies of old age"" /> <Label Content=""Rows""/> <TextBlock Text =""{Binding Path=nTotRows}"" /> <Label Content=""Cols""/> <TextBlock Text =""{Binding Path=nTotCols}"" /> </StackPanel> <UserControl Name=""MyUserControl"" Grid.Column=""1""></UserControl> </Grid> </DockPanel> <DockPanel Grid.Row=""1""> <TextBox Name=""tbxStatus"" HorizontalAlignment=""Left"" Height=""23"" Margin=""10,2,0,0"" IsReadOnly=""True"" TextWrapping=""Wrap"" VerticalAlignment=""Top"" Width=""420""/> <Slider HorizontalAlignment=""Left"" Minimum=""0"" Maximum=""1000"" Margin=""12,2,0,0"" Value=""{Binding Path=_nDelay}"" VerticalAlignment=""Top"" ToolTip=""Change the delay"" Width=""100""/> <Button Name=""btnGraph"" Content=""_Graph"" HorizontalAlignment=""Left"" Margin=""10,2,0,0"" VerticalAlignment=""Top"" ToolTip=""Write to Excel.Hit these keys 'Alt N, N, Enter' Alt activates Menu N(Insert) (N Line chart) Enter (choose default 2D line chart)"" Width=""55""/> <Button Name=""btnQuit"" Content=""_Quit"" HorizontalAlignment=""Left"" Margin=""10,2,0,0"" VerticalAlignment=""Top"" Width=""55""/> </DockPanel> </Grid> "; var bgdOcean = NativeMethods.CreateSolidBrush( new IntPtr(0xffffff)); var fishBowl = new FishBowl(this, bgdOcean); var strReader = new System.IO.StringReader(strxaml); var xamlreader = XmlReader.Create(strReader); var grid = (Grid)(XamlReader.Load(xamlreader)); grid.DataContext = fishBowl; _tbxStatus = (TextBox)grid.FindName("tbxStatus"); var btnQuit = (Button)grid.FindName("btnQuit"); btnQuit.Click += (ob, eb) => { App.Current.Shutdown(); }; var btnGraph = (Button)grid.FindName("btnGraph"); btnGraph.Click += (ob, eb) => { var oldIsRunning = fishBowl.IsRunning; try { fishBowl.IsRunning = false; // write to Excel // Hit these keys "Alt N, N, Enter" // Alt activates Menu // N(Insert) (N Line chart) Enter (choose default 2D line chart) var tempFileName = System.IO.Path.ChangeExtension( System.IO.Path.GetTempFileName(), "csv"); System.IO.File.WriteAllText( tempFileName, FishBowl._Instance._sbStatus.ToString()); System.Diagnostics.Process.Start(tempFileName); } catch (Exception) { // if file in use by Excel, just ignore } if (oldIsRunning) { fishBowl.IsRunning = true; } }; _tbxStatus = (TextBox)grid.FindName("tbxStatus"); var userCtrl = (UserControl)grid.FindName("MyUserControl"); userCtrl.Content = fishBowl; this.Content = grid; this.SizeChanged += (os, es) => { fishBowl.OnSizeChanged(); }; btnQuit.ToolTip = @"Fish vs Sharks Program by Calvin Hsia Per A.K. Dewdney article on Wator, December 1984 Scientific American "; } catch (Exception ex) { this.Content = ex.ToString(); } }; }