Esempio n. 1
0
        public void Run()
        {
            var sw = new Stopwatch();

            while (rw.IsOpen())
            {
                rw.DispatchEvents();

                var dt = (float)TimeSpan.FromTicks(sw.ElapsedTicks).TotalSeconds;
                sw.Restart();

                // Update
                bool anyInput = Mouse.IsButtonPressed(Mouse.Button.Left) || Mouse.IsButtonPressed(Mouse.Button.Right);
                if (anyInput)
                {
                    var worldMouse = rw.MapPixelToCoords(Mouse.GetPosition(rw)) - position;
                    var sfmlPos    = worldMouse * (1f / qtMultiplier);
                    var aabbMin    = (worldMouse - new Vector2f(selectionRadius, selectionRadius)) * (1f / qtMultiplier);
                    var aabbMax    = (worldMouse + new Vector2f(selectionRadius, selectionRadius)) * (1f / qtMultiplier);
                    var qtAABB     = new AABB2i(new Point2i((int)aabbMin.X, (int)aabbMin.Y), new Point2i((int)aabbMax.X, (int)aabbMax.Y));
                    var qtPos      = new Point2i((int)sfmlPos.X, (int)sfmlPos.Y);

                    bool anyChanged = false;
                    if (Mouse.IsButtonPressed(Mouse.Button.Left))
                    {
                        //quadtree.Set(qtPos, selections[selection].Item2);
                        anyChanged |= quadtree.SetCircle(qtPos, (int)(selectionRadius / qtMultiplier), selections[selection].Item2);
                        //quadtree.SetAABB(qtAABB, selections[selection].Item2);
                    }
                    else if (Mouse.IsButtonPressed(Mouse.Button.Right))
                    {
                        //quadtree.Unset(qtPos);
                        anyChanged |= quadtree.UnsetCircle(qtPos, (int)(selectionRadius / qtMultiplier));
                    }

                    if (anyChanged)
                    {
                        lastRegions = quadtree.FindConnectedComponents();
                        lastRegions.Sort(new Comparison <List <RegionQuadtree <Color> > >((v1, v2) => v2.Count.CompareTo(v1.Count)));

                        StringBuilder sb = new StringBuilder();
                        sb.Append("{ ");
                        for (int i = 0; i < lastRegions.Count; i++)
                        {
                            sb.Append(lastRegions[i].Count);
                            sb.Append(", ");
                        }
                        sb.Append(" }");
                        Debug.WriteLine(sb.ToString());
                    }
                }

                rw.Clear();

                // Draw

                // Draw quadtree
                var states = RenderStates.Default;
                states.Transform.Translate(position);

                rw.Draw(quadVertexArray, states);
                rw.Draw(outlineVertexArray, states);

                for (int i = 0; i < lastRegions.Count; i++)
                {
                    var maxAABB = new AABB2i(new Point2i(), new Point2i());
                    for (int j = 0; j < lastRegions[i].Count; j++)
                    {
                        if (lastRegions[i][j].AABB.Width * lastRegions[i][j].AABB.Height > maxAABB.Width * maxAABB.Height)
                        {
                            maxAABB = lastRegions[i][j].AABB;
                        }
                    }

                    var center = new Vector2f(
                        maxAABB.LowerBound.X + maxAABB.Width / 2f,
                        maxAABB.LowerBound.Y + maxAABB.Height / 2f) * qtMultiplier;
                    var text = new Text((i + 1).ToString(), fontBold, 17u);
                    text.Position = center - new Vector2f(text.GetGlobalBounds().Width / 2f, text.GetGlobalBounds().Height / 1.4f);
                    text.Color    = Color.Black;
                    rw.Draw(text, states);
                }

                // For jonah
                var outerRect = new RectangleShape(new Vector2f(quadtree.AABB.Width * qtMultiplier, quadtree.AABB.Height * qtMultiplier));
                outerRect.Position         = position;
                outerRect.FillColor        = Color.Transparent;
                outerRect.OutlineColor     = new Color(255, 255, 255, 40);
                outerRect.OutlineThickness = 3f;
                rw.Draw(outerRect);

                // Draw help texts
                var view = rw.GetView();
                rw.SetView(rw.DefaultView);

                var sel = selections[selection];
                selectionText.DisplayedString = sel.Item1;
                selectionText.Color           = sel.Item2;
                rw.Draw(selectionText);
                rw.Draw(radiusText);
                rw.Draw(resolutionText);
                rw.Draw(helpText);

                rw.SetView(view);

                rw.Display();
            }
        }