MegaMan.LevelEditor.JoinOverlay.Refresh C# (CSharp) Method

Refresh() public method

public Refresh ( int width, int height, IEnumerable joins, ScreenDrawingSurface>.IDictionary surfaces ) : void
width int
height int
joins IEnumerable
surfaces ScreenDrawingSurface>.IDictionary
return void
        public void Refresh(int width, int height, IEnumerable<Join> joins, IDictionary<string, ScreenDrawingSurface> surfaces)
        {
            if (image == null || image.Height != height || image.Width != width)
            {
                if (image != null) image.Dispose();
                image = new Bitmap(width, height);
            }

            using (Graphics g = Graphics.FromImage(image))
            {
                g.Clear(Color.Transparent);
                foreach (Join join in joins)
                {
                    if (surfaces.ContainsKey(join.screenOne) && surfaces.ContainsKey(join.screenTwo))
                    {
                        int mid = join.Size * 8;  // the 8 is from tilesize (16) / 2 for midpoint
                        int midOffsetOne = join.offsetOne * 16 + mid;
                        int midOffsetTwo = join.offsetTwo * 16 + mid;

                        if (join.type == JoinType.Horizontal)
                        {
                            if (surfaces[join.screenOne].Bottom != surfaces[join.screenTwo].Top)
                            {
                                int y1 = surfaces[join.screenOne].Left + midOffsetOne;
                                int y2 = surfaces[join.screenTwo].Left + midOffsetTwo;

                                int x1 = surfaces[join.screenOne].Bottom;
                                int x2 = surfaces[join.screenTwo].Top;

                                DrawJoinPath(g, x1, x2, y1, y2, true);
                            }
                        }
                        else
                        {
                            if (surfaces[join.screenOne].Right != surfaces[join.screenTwo].Left)
                            {
                                int x1 = surfaces[join.screenOne].Right;
                                int x2 = surfaces[join.screenTwo].Left;

                                int y1 = surfaces[join.screenOne].Top + midOffsetOne;
                                int y2 = surfaces[join.screenTwo].Top + midOffsetTwo;

                                DrawJoinPath(g, x1, x2, y1, y2, false);
                            }
                        }

                    }
                }
            }
            Invalidate();
        }

Usage Example

Example #1
0
        private void AlignScreenSurfaces()
        {
            if (surfaces.Count == 0)
            {
                return;
            }

            var placeable = new HashSet <string>();
            var orphans   = new List <string>();

            int oldHorizScroll = this.HorizontalScroll.Value;
            int oldVertScroll  = this.VerticalScroll.Value;

            this.HorizontalScroll.Value = 0;
            this.VerticalScroll.Value   = 0;

            int minX = 0, minY = 0, maxX = 0, maxY = 0;

            foreach (var surface in surfaces.Values)
            {
                surface.Placed = false;
            }

            var startScreen = surfaces.Values.First();

            if (surfaces.ContainsKey(stage.StartScreen))
            {
                startScreen = surfaces[stage.StartScreen];
            }

            // lay the screens out like a deep graph traversal
            LayoutFromScreen(startScreen, new Point(0, 0));

            // any remaining disconnected screens
            foreach (var surface in surfaces.Values.Where(s => !s.Placed))
            {
                LayoutFromScreen(surface, new Point(0, 0));
            }

            foreach (var surface in surfaces.Values)
            {
                minX = Math.Min(minX, surface.Location.X);
                minY = Math.Min(minY, surface.Location.Y);
            }

            if (minX < -this.HorizontalScroll.Value || minY < -this.VerticalScroll.Value)
            {
                // now readjust to all positive locations
                foreach (var surface in surfaces.Values)
                {
                    surface.Location = new Point(surface.Location.X - minX - this.HorizontalScroll.Value, surface.Location.Y - minY - this.VerticalScroll.Value);
                }
            }

            foreach (var surface in surfaces.Values)
            {
                maxX = Math.Max(maxX, surface.Right);
                maxY = Math.Max(maxY, surface.Bottom);
            }

            joinOverlay.Refresh(maxX + 20, maxY + 20, stage.Joins, surfaces);
            joinOverlay.Visible = MainForm.Instance.DrawJoins;

            this.HorizontalScroll.Value = oldHorizScroll;
            this.VerticalScroll.Value   = oldVertScroll;

            foreach (var surfacePair in surfaces)
            {
                surfaceLocations[surfacePair.Key] = surfacePair.Value.Location;
            }
        }