Esempio n. 1
0
        /// <summary>
        ///  Constructs a new Wall object by copying an existing Wall.
        /// </summary>
        public Wall(Wall otherWall)
        {
            Name = otherWall.Name;
            Colored = false;

            Line = new Line2D(otherWall.Line);

            //set center point
            Location = Line.midpoint();

            Dynamic = false;
            Visible = otherWall.Visible;
        }
 public static bool collide(Robot a, Wall b)
 {
     return EngineUtilities.collide(b, a);
 }
        public static bool collide(Wall wall, Robot robot)
        {
            Point2D a1 = new Point2D(wall.Line.Endpoint1);
            Point2D a2 = new Point2D(wall.Line.Endpoint2);
            Point2D b = new Point2D(robot.Location.X, robot.Location.Y);
            if (!wall.Visible)
                return false;
            double rad = robot.Radius;
            double r = ((b.X - a1.X) * (a2.X - a1.X) + (b.Y - a1.Y) * (a2.Y - a1.Y)) / wall.Line.squaredLength();
            double px = a1.X + r * (a2.X - a1.X);
            double py = a1.Y + r * (a2.Y - a1.Y);
            Point2D np = new Point2D(px, py);
            double rad_sq = rad * rad;

            if (r >= 0.0f && r <= 1.0f)
            {
                if (np.squaredDistance(b) < rad_sq)
                    return true;
                else
                    return false;
            }

            double d1 = b.squaredDistance(a1);
            double d2 = b.squaredDistance(a2);
            if (d1 < rad_sq || d2 < rad_sq)
                return true;
            else
                return false;
        }
 //wall-wall (should never collide)
 public static bool collide(Wall a, Wall b)
 {
     return false;
 }
        private void MouseMoveHandler(object sender, MouseEventArgs e)
        {
            if (drawMode == drawModes.selectMode)
            {
				if (bDragDisplay)
				{
					       //how far has the wall been dragged?
                    int dx = e.X - x1;
                    int dy = e.Y - y1;

                    x1 = e.X;
                    y1 = e.Y;
					
					float odx,ody;
					frame.convertFromDisplayOffset((float)dx,(float)dy,out odx, out ody);
					frame.X-=odx;
					frame.Y-=ody;
				}
				
                if (bSelected_wall)
                {
                    //wall-dragging code

                    //how far has the wall been dragged?
                    int dx = e.X - x1;
                    int dy = e.Y - y1;

                    x1 = e.X;
                    y1 = e.Y;
					
					float odx,ody;
					frame.convertFromDisplayOffset((float)dx,(float)dy,out odx, out ody);

                    if (selectMode == selectModes.dragMode)
                    {
                        //update wall's position
                        selected_wall.Line.Endpoint1.X += odx;
                        selected_wall.Line.Endpoint1.Y += ody;
                        selected_wall.Line.Endpoint2.X += odx;
                        selected_wall.Line.Endpoint2.Y += ody;
                    }

                    if (selectMode == selectModes.rotateMode)
                    {
                        Point2D midpoint = selected_wall.Line.midpoint();
                        selected_wall.Line.Endpoint1.rotate(dy / 180.0 * 3.14, midpoint);
                        selected_wall.Line.Endpoint2.rotate(dy / 180.0 * 3.14, midpoint);
                    }

                    if (selectMode == selectModes.scaleMode)
                    {
                        selected_wall.Line.scale(1.0 + 0.05 * dy);
                    }

                    Invalidate();
                }
                if (bSelected_robot)
                {
                    //robot-dragging code

                    //how far has the robot been dragged?
                    int dx = e.X - x1;
                    int dy = e.Y - y1;

                    x1 = e.X;
                    y1 = e.Y;
					
					float odx,ody;
					frame.convertFromDisplayOffset((float)dx,(float)dy,out odx, out ody);

					
                    //update robot's position
                    if (selectMode == selectModes.dragMode)
                    {
                        selected_robot.Location.X += odx;
                        selected_robot.Location.Y += ody;
                    }
                    if (selectMode == selectModes.rotateMode)
                    {
                        selected_robot.Heading += (dy / 10.0);
                    }
                    if (selectMode == selectModes.scaleMode)
                  {
                        selected_robot.Radius += (dy / 2.0);
                        if (selected_robot.Radius < 1.0)
                            selected_robot.Radius = 1.0;
                        selected_robot.AreaOfImpact.Radius = selected_robot.Radius;
                    }
                    Invalidate();
                }

            }

            if (drawMode == drawModes.selectMode && !bSelected_wall && !bSelected_robot)
            {
                selected_wall = null;
                selected_robot = null;
                display_snap = false;
                //find snap
                Point2D newpoint = new Point2D((double)e.X, (double)e.Y);

                if (experiment != null)
				{
					if(experiment.robots!=null)
                    foreach (Robot robot in experiment.robots)
                    {
						Point2D screen_location = frame.convertToDisplay(robot.Location);
                        if (screen_location.distance(newpoint) < 20.0)
                        {
                            display_snap = true;
                            snapx = (int)screen_location.X;
                            snapy = (int)screen_location.Y;
                            selected_robot = robot;
                        }
                    }

                    if(experiment.environment.walls!=null)
                    foreach (Wall wall in experiment.environment.walls)
                    {
                        Point2D screen_location = frame.convertToDisplay(wall.Location);
                        if (screen_location.distance(newpoint) < 20.0)
                        {
                            display_snap = true;
                            snapx = (int)screen_location.X;
                            snapy = (int)screen_location.Y;
                            selected_wall = wall;
                        }
                    }

                    int index = 0;
                    if (experiment.environment.walls != null)
                        foreach (Point p in experiment.environment.POIPosition)
                        {
                            Point2D screen_location = frame.convertToDisplay(new Point2D(p.X, p.Y));
                            if (screen_location.distance(newpoint) < 20.0)
                            {
                                display_snap = true;
                                snapx = (int)screen_location.X;
                                snapy = (int)screen_location.Y;
                                selected_POI = index;
                            }
                            index++;
                        }
				}
				
                
                Invalidate();
            }

            if (drawMode == drawModes.wallMode)
            {
                display_snap = false;
                //find snap
                Point2D newpoint = new Point2D((double)e.X, (double)e.Y);
                foreach (Wall wall in experiment.environment.walls)
                {
					Point2D screen_location1 = frame.convertToDisplay(wall.Line.Endpoint1);
					Point2D screen_location2 = frame.convertToDisplay(wall.Line.Endpoint2);
                    
                    if (screen_location1.distance(newpoint) < 10.0)
                    {
                        display_snap = true;
                        snapx = (int)screen_location1.X;
                        snapy = (int)screen_location1.Y;
                    }
                    if (screen_location2.distance(newpoint) < 10.0)
                    {
                        display_snap = true;
                        snapx = (int)screen_location2.X;
                        snapy = (int)screen_location2.Y;
                    }
                }
                x2 = e.X;
                y2 = e.Y;
                Invalidate();
            }
            if (drawMode == drawModes.AOIMode && displayAOIRectangle)
            {
				float ox,oy;
				frame.convertFromDisplay(e.X,e.Y,out ox,out oy);
                experiment.environment.AOIRectangle = new Rectangle(experiment.environment.AOIRectangle.X,
                    experiment.environment.AOIRectangle.Y,(int) ox - experiment.environment.AOIRectangle.X, (int) oy - experiment.environment.AOIRectangle.Y);
                Invalidate();
            }
        }
        private void MouseUpHandler(object sender, MouseEventArgs e)
        {
            if (drawMode == drawModes.selectMode)
            {
                if (bSelected_wall && selected_wall != null)
                    selected_wall.Location = selected_wall.Line.midpoint();
                bSelected_wall = false;
				bDragDisplay = false;
                bSelected_robot = false;
                Invalidate();
            }

            if (drawMode == drawModes.wallMode)
            {
                x2 = e.X;
                y2 = e.Y;

                if (display_snap)
                {
                    x2 = snapx;
                    y2 = snapy;
                }

                display_tempwall = false;
				float ox1,oy1,ox2,oy2;
				frame.convertFromDisplay((float)x1,(float)y1,out ox1, out oy1);
				frame.convertFromDisplay((float)x2,(float)y2,out ox2, out oy2);
                Wall new_wall = new Wall((double)ox1, (double)oy1, (double)ox2, (double)oy2, true, "");
                experiment.environment.walls.Add(new_wall);
                Invalidate();
            }

            if (drawMode == drawModes.AOIMode)
            {
                displayAOIRectangle = false;
            }
        }
        //handles key down event...experiment.g. control keys like delete
        private void KeyDownHandler(object sender, System.Windows.Forms.KeyEventArgs e)
        {

            if (drawMode == drawModes.selectMode)
            {
                if (e.KeyCode == Keys.W)
                {
                    if (selected_wall != null)
                    {
                        Wall thewall = selected_wall;
                        //InputBoxSample.InputBoxResult result;

                        ObjectRenamer or = new ObjectRenamer(selected_wall.Name);
                        DialogResult result = or.ShowDialog(this);

                        //result = InputBoxSample.InputBox.Show("Name wall", "Name wall", selected_wall.name, null);

                        if (result == DialogResult.OK)
                        {
                            //System.Console.WriteLine(selected_wall.name);
                            thewall.Name = or.textBox1.Text;
                        }
                    }
                }
				if (e.KeyCode == Keys.D) 
					robot_display_debug=!robot_display_debug;
                if (e.KeyCode == Keys.V)
                {
                    if (selected_wall != null)
                    {
                        selected_wall.Visible = !selected_wall.Visible;
                    }
                }
                if (e.KeyCode == Keys.Delete)
                {
                    if (selected_wall != null)
                        experiment.environment.walls.Remove(selected_wall);
                    selected_wall = null;

                    if (selected_POI != -1)
                    {
                        experiment.environment.POIPosition.RemoveAt(selected_POI);
                        selected_POI = -1;
                    }

                    display_snap = false;
                    Invalidate();
                }

                if (e.KeyCode == Keys.H)
                {
                    if (drawMode == drawModes.selectMode)
                    {
                        TeamVisualizerForm netForm = null;
                        if (experiment is MultiAgentExperiment) //if this is a multiagentexperiment use Brain class
                        {
                            INetwork brain = experiment.agentBrain.Brain;
                            //INetwork brain = experiment.Brain.getBrain(selected_robot.ID);
                            if (brain == null) return; //TODO maybe show dialog
                            netForm = new TeamVisualizerForm((ModularNetwork)brain,experiment.robots.Count);
                        }
                        else
                        {
                            //  TODO test with non MultiAgentExperiment
                            if (experiment.agentBrain.Brain == null) return;
                            netForm = new TeamVisualizerForm((ModularNetwork)experiment.agentBrain.Brain, experiment.robots.Count);
                        }

                        //netForm.genomeVisualizerForm = genomeVisualizerForm;
                        netForm.Show();

                        // this.selected_robot.displayPath = !this.selected_robot.displayPath;
                    }
                }

            }
        }