private void BuildingAdded(Object sender, BuildingAddedEventArgs args)
        {
            BuildingUI b = new BuildingUI(controller, args.Building);

            b.Location = new Point((int)args.Building.PointLocation.X * PIXELS_PER_COORDINATE, (int)args.Building.PointLocation.Y * PIXELS_PER_COORDINATE);
            b.Size     = new Size(args.Building.Width * PIXELS_PER_COORDINATE, args.Building.Height * PIXELS_PER_COORDINATE);
            realizedComponents.Add(args.Building, b);
            mapPanel.Controls.Add(b);
            b.BringToFront();
        }
        private void RealizeView()
        {
            // Get the indices of the upper left hand cell that is visible.
            int xPos = HorizontalScroll.Value / PIXELS_PER_COORDINATE;
            int yPos = this.VerticalScroll.Value / PIXELS_PER_COORDINATE;

            HashSet<Object> componentsToVirtualize = new HashSet<Object>();
            foreach (Object o in realizedComponents.Keys)
            {
                componentsToVirtualize.Add(o);
            }
            mapPanel.SuspendLayout();
            if (context != null)
            {
                ZRTSModel.Map map = context.GetGameWorld().GetMap();
                int maxXPos = xPos + (Width / PIXELS_PER_COORDINATE) + 1;
                int mapMaxX = map.GetWidth();
                int maxX = Math.Min(maxXPos, mapMaxX);
                int maxYPos = yPos + (Height / PIXELS_PER_COORDINATE) + 1;
                int mapMaxY = map.GetHeight();
                int maxY = Math.Min(maxYPos, mapMaxY);
                List<Control> controlsToAdd = new List<Control>();
                for (int i = xPos; i < maxX; i++)
                {
                    for (int j = yPos; j < maxY; j++)
                    {
                        CellComponent cell = map.GetCellAt(i, j);
                        if (!realizedComponents.Contains(cell))
                        {
                            TileUI ui = new TileUI(controller, cell);
                            realizedComponents.Add(cell, ui);

                            ui.Location = new Point(cell.X * PIXELS_PER_COORDINATE, cell.Y * PIXELS_PER_COORDINATE);
                            ui.Size = new Size(PIXELS_PER_COORDINATE, PIXELS_PER_COORDINATE);
                            controlsToAdd.Add(ui);
                        }
                        else
                        {
                            componentsToVirtualize.Remove(cell);
                        }
                        foreach (ModelComponent m in cell.EntitiesContainedWithin)
                        {
                            if (m is Building)
                            {
                                Building building = (Building)m;
                                if (!realizedComponents.Contains(m))
                                {
                                    BuildingUI b = new BuildingUI(controller, building);
                                    b.Location = new Point((int)building.PointLocation.X * PIXELS_PER_COORDINATE, (int)building.PointLocation.Y * PIXELS_PER_COORDINATE);
                                    b.Size = new Size(building.Width * PIXELS_PER_COORDINATE, building.Height * PIXELS_PER_COORDINATE);
                                    realizedComponents.Add(building, b);
                                    // Don't bundle the buildings or else BringToFront won't work
                                    mapPanel.Controls.Add(b);
                                    b.BringToFront();
                                }
                                else
                                {
                                    componentsToVirtualize.Remove(m);
                                }
                            }

                        }
                    }
                }
                mapPanel.Controls.AddRange(controlsToAdd.ToArray());
                foreach (Object o in componentsToVirtualize)
                {
                    Control c = (Control)realizedComponents[o];
                    if (c is TileUI)
                    {
                        // TileUIs are set up to allow drop - turn this off so that the handle to it is destroyed and so the tileUI can be destroyed by the GC.
                        c.AllowDrop = false;
                        ((TileUI)c).UnregisterFromEvents();
                    }
                    mapPanel.Controls.Remove(c);
                    realizedComponents.Remove(o);
                }
                mapPanel.ResumeLayout();
            }
        }
 private void BuildingAdded(Object sender, BuildingAddedEventArgs args)
 {
     BuildingUI b = new BuildingUI(controller, args.Building);
     b.Location = new Point((int) args.Building.PointLocation.X * PIXELS_PER_COORDINATE, (int)args.Building.PointLocation.Y * PIXELS_PER_COORDINATE);
     b.Size = new Size(args.Building.Width * PIXELS_PER_COORDINATE, args.Building.Height * PIXELS_PER_COORDINATE);
     realizedComponents.Add(args.Building, b);
     mapPanel.Controls.Add(b);
     b.BringToFront();
 }
        private void RealizeView()
        {
            // Get the indices of the upper left hand cell that is visible.
            int xPos = HorizontalScroll.Value / PIXELS_PER_COORDINATE;
            int yPos = this.VerticalScroll.Value / PIXELS_PER_COORDINATE;

            HashSet <Object> componentsToVirtualize = new HashSet <Object>();

            foreach (Object o in realizedComponents.Keys)
            {
                componentsToVirtualize.Add(o);
            }
            mapPanel.SuspendLayout();
            if (context != null)
            {
                ZRTSModel.Map  map           = context.GetGameWorld().GetMap();
                int            maxXPos       = xPos + (Width / PIXELS_PER_COORDINATE) + 1;
                int            mapMaxX       = map.GetWidth();
                int            maxX          = Math.Min(maxXPos, mapMaxX);
                int            maxYPos       = yPos + (Height / PIXELS_PER_COORDINATE) + 1;
                int            mapMaxY       = map.GetHeight();
                int            maxY          = Math.Min(maxYPos, mapMaxY);
                List <Control> controlsToAdd = new List <Control>();
                for (int i = xPos; i < maxX; i++)
                {
                    for (int j = yPos; j < maxY; j++)
                    {
                        CellComponent cell = map.GetCellAt(i, j);
                        if (!realizedComponents.Contains(cell))
                        {
                            TileUI ui = new TileUI(controller, cell);
                            realizedComponents.Add(cell, ui);

                            ui.Location = new Point(cell.X * PIXELS_PER_COORDINATE, cell.Y * PIXELS_PER_COORDINATE);
                            ui.Size     = new Size(PIXELS_PER_COORDINATE, PIXELS_PER_COORDINATE);
                            controlsToAdd.Add(ui);
                        }
                        else
                        {
                            componentsToVirtualize.Remove(cell);
                        }
                        foreach (ModelComponent m in cell.EntitiesContainedWithin)
                        {
                            if (m is Building)
                            {
                                Building building = (Building)m;
                                if (!realizedComponents.Contains(m))
                                {
                                    BuildingUI b = new BuildingUI(controller, building);
                                    b.Location = new Point((int)building.PointLocation.X * PIXELS_PER_COORDINATE, (int)building.PointLocation.Y * PIXELS_PER_COORDINATE);
                                    b.Size     = new Size(building.Width * PIXELS_PER_COORDINATE, building.Height * PIXELS_PER_COORDINATE);
                                    realizedComponents.Add(building, b);
                                    // Don't bundle the buildings or else BringToFront won't work
                                    mapPanel.Controls.Add(b);
                                    b.BringToFront();
                                }
                                else
                                {
                                    componentsToVirtualize.Remove(m);
                                }
                            }
                        }
                    }
                }
                mapPanel.Controls.AddRange(controlsToAdd.ToArray());
                foreach (Object o in componentsToVirtualize)
                {
                    Control c = (Control)realizedComponents[o];
                    if (c is TileUI)
                    {
                        // TileUIs are set up to allow drop - turn this off so that the handle to it is destroyed and so the tileUI can be destroyed by the GC.
                        c.AllowDrop = false;
                        ((TileUI)c).UnregisterFromEvents();
                    }
                    mapPanel.Controls.Remove(c);
                    realizedComponents.Remove(o);
                }
                mapPanel.ResumeLayout();
            }
        }