Esempio n. 1
0
        /// <summary>Updates all variables in this mode.</summary>
        /// <param name="gameTime">The current game time.</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            FInt elapsed = (FInt)gameTime.ElapsedGameTime.TotalSeconds;

            // update gui
            if (Inp.OldMse.Position != Inp.Mse.Position)
                desktop.mouseMove(Inp.Mse.Position);

            GUI.Desktop.Event evnt = Desktop.Event.MouseRightUp;
            bool guiOwnedInput = false;

            if (Inp.OldMse.LeftButton == ButtonState.Pressed && Inp.Mse.LeftButton == ButtonState.Released)
                guiOwnedInput |= desktop.PerformMouseEvent(Desktop.Event.MouseLeftUp, Inp.Mse.Position, Inp);
            if (Inp.OldMse.LeftButton == ButtonState.Released && Inp.Mse.LeftButton == ButtonState.Pressed)
                guiOwnedInput |= desktop.PerformMouseEvent(Desktop.Event.MouseLeftDown, Inp.Mse.Position, Inp);
            if (Inp.OldMse.RightButton == ButtonState.Pressed && Inp.Mse.RightButton == ButtonState.Released)
                guiOwnedInput |= desktop.PerformMouseEvent(Desktop.Event.MouseRightUp, Inp.Mse.Position, Inp);
            if (Inp.OldMse.RightButton == ButtonState.Released && Inp.Mse.RightButton == ButtonState.Pressed)
                guiOwnedInput |= desktop.PerformMouseEvent(Desktop.Event.MouseRightUp, Inp.Mse.Position, Inp);

            Keys[] newKeys = Inp.Key.GetPressedKeys();

            if (newKeys.Length != 0)
                guiOwnedInput |= desktop.PerformKeyEvent(newKeys, Inp);

            if (guiOwnedInput)
            {
                hoveredNode = null;
                hoveredSeg = null;
                hoveredSegEnd = null;
                hoveredSegEndOwner = null;
                hoveredGeo = null;
                hoveredHotspot = null;
                isDragging = false;
            }
            else
            {
                // prepare grid manager
                world.Grid.startNewUpdate(gameTime);

                // move camera
                if (Inp.Key.IsKeyDown(Keys.OemPlus))
                    world.Cam.Zoom += world.Cam.Zoom * elapsed;

                if (Inp.Key.IsKeyDown(Keys.OemMinus))
                    world.Cam.Zoom -= world.Cam.Zoom * elapsed;

                if (Inp.Key.IsKeyDown(Keys.A))
                    world.Cam.CenterX -= (700 / world.Cam.Zoom) * elapsed;

                if (Inp.Key.IsKeyDown(Keys.D))
                    world.Cam.CenterX += (700 / world.Cam.Zoom) * elapsed;

                if (Inp.Key.IsKeyDown(Keys.W))
                    world.Cam.CenterY -= (700 / world.Cam.Zoom) * elapsed;

                if (Inp.Key.IsKeyDown(Keys.S))
                    world.Cam.CenterY += (700 / world.Cam.Zoom) * elapsed;

                world.Cam.Zoom += (FInt)(Inp.Mse.ScrollWheelValue - Inp.OldMse.ScrollWheelValue) / (FInt)120 * (FInt).1d * world.Cam.Zoom;

                world.Cam.refreshCorners();

                // get cursor world coordinates
                VectorF cursorPos = world.Cam.screenToWorld(new Vector2((float)Inp.Mse.X, (float)Inp.Mse.Y));

                // check for hovered node, segment, segment end, and hotspot
                hoveredNode = world.NodeAtPoint(cursorPos, false);
                hoveredSeg = (hoveredNode == null) ? world.segmentAtPoint(cursorPos, null) : null;
                hoveredSegEnd = world.SegmentEndAtPoint(cursorPos);

                if (hoveredSegEnd != null)
                {
                    foreach (Segment seg in world.Segments)
                    {
                        if (seg.Nodes[0] == hoveredSegEnd || seg.Nodes[1] == hoveredSegEnd)
                        {
                            hoveredSegEndOwner = seg.Owner;
                            break;
                        }
                    }
                }
                else
                {
                    hoveredSegEndOwner = null;
                }

                hoveredHotspot = world.HotspotAtPoint(cursorPos);

                // test geo vertices
                hoveredGeo = world.geoAtPoint(cursorPos, out hoveredGeoVertex, true);

                // test geo lines
                if (hoveredGeo == null)
                {
                    hoveredGeo = world.geoAtPoint(cursorPos, out hoveredGeoVertex, false);
                    hoveredGeoIsLine = true;
                }
                else
                {
                    hoveredGeoIsLine = false;
                }

                // if the user just released the left mouse button
                if (Inp.OldMse.LeftButton == ButtonState.Pressed && Inp.Mse.LeftButton == ButtonState.Released)
                {
                    if (selectedSegEnd != null)
                    {
                        // do nothing
                    }
                    // add node
                    else if (btnAddNode.Pressed && cmbNodeTypes.SelectedIndex != -1 && selectedGeo == null && (selectedNode == null || isDragging) && selectedSeg == null && selectedHotspot == null)
                    {
                        bool useHoveredEnd = (hoveredSegEnd != null && (selectedNode == null || selectedNode.Owner == hoveredSegEndOwner));

                        // add node
                        Node node = new Node(world, world.NodeTypes[cmbEditorAddNodeType.SelectedIndex]);
                        node.Pos = useHoveredEnd ? hoveredSegEnd.Pos : cursorPos;
                        node.Active = true;

                        if (selectedNode != null && isDragging)
                        {
                            node.Owner = selectedNode.Owner;

                            if (selectedNode.NumSegments < selectedNode.Segments.Length) // add segment too
                            {
                                Segment seg = new Segment(world);
                                seg.Owner = selectedNode.Owner;
                                seg.Nodes[0] = selectedNode;
                                seg.Nodes[1] = node;
                                selectedNode.addSegment(seg, false);
                                node.addSegment(seg, false);
                                seg.refreshMath();
                                seg.EndLength[0] = seg.Length;
                                seg.EndLength[1] = seg.Length;
                                seg.refreshEndLocs();
                                world.addSegment(seg);
                            }
                        }
                        else if (selectedNode == null && hoveredSegEnd != null)
                        {
                            node.Owner = hoveredSegEndOwner;
                        }
                        else if (cmbEditorAddNodeOwner.SelectedIndex != 0)
                        {
                            node.Owner = world.Players[cmbEditorAddNodeOwner.SelectedIndex - 1];
                        }

                        if (useHoveredEnd)
                        {
                            // link segments to it
                            foreach (Segment seg in (hoveredSegEndOwner == null ? world.Segments : hoveredSegEndOwner.Segments))
                            {
                                for (int i = 0; i < 2; i++)
                                {
                                    if (seg.Nodes[i] == hoveredSegEnd)
                                    {
                                        seg.Nodes[i] = node;
                                        break;
                                    }
                                }
                            }
                        }

                        world.addNode(node);
                        selectedNode = node;
                        selectedSeg = null;
                        loadObjectEditor();
                    }
                    // add segment
                    else if (btnAddSeg.Pressed && isDragging && selectedSeg == null && selectedGeo == null && (selectedNode == null || selectedNode.NumSegments < selectedNode.Segments.Length) && selectedHotspot == null)
                    {
                        Segment seg = new Segment(world);
                        seg.Owner = (selectedNode != null) ? selectedNode.Owner
                            : (hoveredNode != null) ? hoveredNode.Owner
                            : (cmbEditorAddSegOwner.SelectedIndex > 0) ? world.Players[cmbEditorAddSegOwner.SelectedIndex - 1]
                            : null;

                        if (selectedNode != null)
                        {
                            seg.Nodes[0] = selectedNode;
                            seg.Owner = selectedNode.Owner;
                            selectedNode.addSegment(seg, false);
                        }
                        else
                        {
                            seg.State[1] = SegmentSkel.SegState.Retracting;
                            seg.Nodes[0] = new Node(world);
                            seg.Nodes[0].Pos = lastClickedPoint;
                            seg.Nodes[0].Active = false;
                            seg.Nodes[0].Destroyed = true;
                            seg.Nodes[0].initSegArrays(0);
                        }

                        if (hoveredNode != null && hoveredNode.NumSegments != hoveredNode.Segments.Length && (selectedNode == null || (hoveredNode != selectedNode && hoveredNode.Owner == selectedNode.Owner && hoveredNode.relatedSeg(selectedNode) == -1)))
                        {
                            seg.Nodes[1] = hoveredNode;
                            hoveredNode.addSegment(seg, false);
                        }
                        else
                        {
                            seg.State[0] = SegmentSkel.SegState.Building;
                            seg.Nodes[1] = new Node(world);
                            seg.Nodes[1].Pos = cursorPos;
                            seg.Nodes[1].Active = false;
                            seg.Nodes[1].Destroyed = true;
                            seg.Nodes[1].initSegArrays(0);
                        }

                        seg.refreshMath();
                        seg.EndLength[0] = seg.Length;
                        seg.EndLength[1] = seg.Length;
                        seg.refreshEndLocs();
                        world.addSegment(seg);

                        selectedNode = null;
                        selectedSeg = seg;
                        loadObjectEditor();
                    }
                    // add geo vertex
                    else if (btnAddGeo.Pressed && selectedNode == null && selectedSeg == null && selectedSegEnd == null && selectedHotspot == null)
                    {
                        // add vertex to existing geo
                        if (selectedGeo != null && !selectedGeoIsLine && isDragging && (selectedGeoVertex == 0 || selectedGeoVertex == selectedGeo.Vertices.Length - 1))
                        {
                            VectorF[] vertices;
                            if (selectedGeo.Vertices.Length == 1)
                            {
                                vertices = new VectorF[selectedGeo.Vertices.Length + 1];
                                vertices[0] = cursorPos;
                                vertices[1] = selectedGeo.Vertices[0];
                            }
                            else if (selectedGeoVertex == 0)
                            {
                                vertices = new VectorF[selectedGeo.Vertices.Length + 2];
                                selectedGeo.Vertices.CopyTo(vertices, 2);
                                vertices[0] = cursorPos;
                                vertices[1] = selectedGeo.Vertices[0];
                            }
                            else
                            {
                                vertices = new VectorF[selectedGeo.Vertices.Length + 2];
                                selectedGeo.Vertices.CopyTo(vertices, 0);
                                vertices[selectedGeo.Vertices.Length] = selectedGeo.Vertices[selectedGeo.Vertices.Length - 1];
                                vertices[selectedGeo.Vertices.Length + 1] = cursorPos;
                                selectedGeoVertex = vertices.Length - 1;
                            }

                            world.Grid.Rect(selectedGeo.UpperLeft, selectedGeo.LowerRight, selectedGeo, world.gridRemoveGeo);
                            selectedGeo.Vertices = vertices;
                            selectedGeo.refreshMath(new Vector2((float)tGeo.Width, (float)tGeo.Height));
                            world.Grid.Rect(selectedGeo.UpperLeft, selectedGeo.LowerRight, selectedGeo, world.gridAddGeo);

                            selectedGeoIsLine = false;
                            loadObjectEditor();
                        }
                        // add vertex in the middle of a line
                        else if (hoveredGeo != null && hoveredGeoIsLine)
                        {
                            VectorF[] vertices = new VectorF[hoveredGeo.Vertices.Length + 2];
                            int v2 = (hoveredGeoVertex == hoveredGeo.Vertices.Length - 1) ? 0 : hoveredGeoVertex + 1;
                            float dist = (float)Calc.getAdj(VectorF.Distance(cursorPos, hoveredGeo.Vertices[hoveredGeoVertex]), Calc.LinePointDistance(cursorPos, hoveredGeo.Vertices[hoveredGeoVertex], hoveredGeo.Vertices[v2]));
                            Vector2 dir = Vector2.Normalize((Vector2)(hoveredGeo.Vertices[v2] - hoveredGeo.Vertices[hoveredGeoVertex]));
                            VectorF pos = (VectorF)((Vector2)hoveredGeo.Vertices[hoveredGeoVertex] + (dir * dist));

                            for (int i = 0; i <= hoveredGeoVertex; i++)
                            {
                                vertices[i] = hoveredGeo.Vertices[i];
                            }

                            if (hoveredGeoVertex == selectedGeo.Vertices.Length - 1)
                            {
                                vertices[hoveredGeoVertex + 1] = selectedGeo.Vertices[selectedGeo.Vertices.Length - 1];
                                vertices[hoveredGeoVertex + 2] = pos;
                            }
                            else
                            {
                                vertices[hoveredGeoVertex + 1] = pos;
                                vertices[hoveredGeoVertex + 2] = pos;

                                for (int i = hoveredGeoVertex + 1; i < hoveredGeo.Vertices.Length; i++)
                                {
                                    vertices[i + 2] = hoveredGeo.Vertices[i];
                                }
                            }

                            world.Grid.Rect(hoveredGeo.UpperLeft, hoveredGeo.LowerRight, hoveredGeo, world.gridRemoveGeo);
                            hoveredGeo.Vertices = vertices;
                            hoveredGeo.refreshMath(new Vector2((float)tGeo.Width, (float)tGeo.Height));
                            world.Grid.Rect(hoveredGeo.UpperLeft, hoveredGeo.LowerRight, hoveredGeo, world.gridAddGeo);

                            hoveredGeoIsLine = false;
                            hoveredGeoVertex += 2;
                            selectedGeo = hoveredGeo;
                            selectedGeoIsLine = false;
                            selectedGeoVertex = hoveredGeoVertex;
                            loadObjectEditor();
                        }
                        // add new geo
                        else if (hoveredGeo == null)
                        {
                            Geo geo = new Geo(world);
                            geo.Vertices = new VectorF[] { cursorPos };
                            geo.CloseLoop = false;
                            geo.Display = false;
                            geo.refreshMath(new Vector2((float)tGeo.Width, (float)tGeo.Height));
                            world.addGeo(geo);

                            selectedGeo = geo;
                            selectedGeoIsLine = false;
                            selectedGeoVertex = 0;
                            loadObjectEditor();
                        }
                    }
                    else if (btnAddHotspot.Pressed && selectedNode == null && selectedSeg == null && selectedSegEnd == null && selectedGeo == null)
                    {
                        // add hotspot
                        Hotspot hotspot = new Hotspot(world);
                        hotspot.Pos = cursorPos;

                        world.addHotspot(hotspot);
                        selectedHotspot = hotspot;
                        loadObjectEditor();
                    }

                    selectedSegEnd = null;
                    isDragging = false;
                }
                // if the player just released the right mouse button
                else if (Inp.OldMse.RightButton == ButtonState.Pressed && Inp.Mse.RightButton == ButtonState.Released)
                {

                }
                // user just pressed the left mouse button
                else if (Inp.OldMse.LeftButton == ButtonState.Released && Inp.Mse.LeftButton == ButtonState.Pressed)
                {
                    lastClickedPoint = world.Cam.screenToWorld(new Vector2((float)Inp.Mse.X, (float)Inp.Mse.Y));
                    isDragging = false;

                    // check for selected node
                    selectedNode = hoveredNode;

                    // check for selected segment end
                    selectedSegEnd = (selectedNode == null)
                        ? hoveredSegEnd
                        : null;

                    // check for selected segment
                    selectedSeg = (selectedNode == null && selectedSegEnd == null)
                        ? hoveredSeg
                        : null;

                    // check for selected hotspot
                    selectedHotspot = (selectedNode == null && selectedSegEnd == null && selectedSeg == null)
                        ? hoveredHotspot
                        : null;

                    // check for selected geo
                    if (selectedNode == null && selectedSeg == null && selectedSegEnd == null && selectedHotspot == null)
                    {
                        selectedGeo = hoveredGeo;
                        selectedGeoIsLine = hoveredGeoIsLine;
                        selectedGeoVertex = hoveredGeoVertex;
                    }
                    else
                    {
                        selectedGeo = null;
                    }

                    loadObjectEditor();
                }
                // user just pressed the right mouse button
                else if (Inp.OldMse.RightButton == ButtonState.Released && Inp.Mse.RightButton == ButtonState.Pressed)
                {

                }
                // if the left mouse button is still pressed
                else if (desktop.Focused == null && Inp.OldMse.LeftButton == ButtonState.Pressed && Inp.Mse.LeftButton == ButtonState.Pressed)
                {
                    if (!isDragging && Vector2.Distance(world.Cam.worldToScreen(lastClickedPoint), new Vector2((float)Inp.Mse.X, (float)Inp.Mse.Y)) >= distToDrag)
                    {
                        isDragging = true;

                        if (selectedNode != null)
                            dragOffset = selectedNode.Pos - lastClickedPoint;
                        else if (selectedSegEnd != null)
                            dragOffset = selectedSegEnd.Pos - lastClickedPoint;
                        else if (selectedSeg != null)
                            dragOffset = selectedSeg.Nodes[0].Pos - lastClickedPoint;
                        else if (selectedHotspot != null)
                            dragOffset = selectedHotspot.Pos - lastClickedPoint;
                        else if (selectedGeo != null)
                        {
                            if (selectedGeoVertex == -1)
                                dragOffset = selectedGeo.Center - lastClickedPoint;
                            else
                                dragOffset = selectedGeo.Vertices[selectedGeoVertex] - lastClickedPoint;
                        }
                    }
                }
                // if [Del] key is pressed
                else if (!Inp.OldKey.IsKeyDown(Keys.Delete) && Inp.Key.IsKeyDown(Keys.Delete))
                {
                    if (selectedNode != null)
                        removeSelNode();
                    else if (selectedSeg != null)
                        removeSelSeg();
                    else if (selectedHotspot != null)
                        removeSelHotspot();
                    else if (selectedGeo != null)
                        removeSelGeo();
                }

                // drag
                if (isDragging)
                {
                    // move node
                    if (selectedNode != null && !btnAddSeg.Pressed && (!btnAddNode.Pressed || cmbEditorAddNodeType.SelectedIndex < 0))
                    {
                        moveNode(selectedNode, cursorPos + dragOffset);
                    }
                    // move segment end
                    else if (selectedSegEnd != null)
                    {
                        moveNode(selectedSegEnd, cursorPos + dragOffset);
                    }
                    // move segment
                    else if (selectedSeg != null)
                    {
                        VectorF moveVect = cursorPos + dragOffset - selectedSeg.Nodes[0].Pos;
                        moveNode(selectedSeg.Nodes[0], selectedSeg.Nodes[0].Pos + moveVect);
                        moveNode(selectedSeg.Nodes[1], selectedSeg.Nodes[1].Pos + moveVect);
                    }
                    // move hotspot
                    else if (selectedHotspot != null)
                    {
                        moveHotspot(selectedHotspot, cursorPos + dragOffset);
                    }
                    // move geo/vertex
                    else if (selectedGeo != null && !btnAddGeo.Pressed)
                    {
                        world.Grid.Rect(selectedGeo.UpperLeft, selectedGeo.LowerRight, selectedGeo, world.gridRemoveGeo);
                        if (selectedGeoIsLine)
                        {
                            if (selectedGeoVertex == selectedGeo.Vertices.Length - 1)
                            {
                                selectedGeo.Vertices[0] += cursorPos + dragOffset - selectedGeo.Vertices[selectedGeoVertex];
                            }
                            else
                            {
                                if (selectedGeoVertex != 0)
                                    selectedGeo.Vertices[selectedGeoVertex - 1] = cursorPos + dragOffset;

                                selectedGeo.Vertices[selectedGeoVertex + 1] += cursorPos + dragOffset - selectedGeo.Vertices[selectedGeoVertex];

                                if (selectedGeoVertex != selectedGeo.Vertices.Length - 2)
                                    selectedGeo.Vertices[selectedGeoVertex + 2] += cursorPos + dragOffset - selectedGeo.Vertices[selectedGeoVertex];
                            }
                            selectedGeo.Vertices[selectedGeoVertex] = cursorPos + dragOffset;
                        }
                        else if (selectedGeoVertex == -1)
                        {
                            VectorF toMove = cursorPos + dragOffset - selectedGeo.Center;
                            for (int i = 0; i < selectedGeo.Vertices.Length; i++)
                            {
                                selectedGeo.Vertices[i] += toMove;
                            }
                        }
                        else
                        {
                            if (selectedGeoVertex != 0 && selectedGeoVertex != selectedGeo.Vertices.Length - 1)
                                selectedGeo.Vertices[selectedGeoVertex - 1] = cursorPos + dragOffset;
                            selectedGeo.Vertices[selectedGeoVertex] = cursorPos + dragOffset;
                        }
                        selectedGeo.refreshMath(new Vector2((float)tGeo.Width, (float)tGeo.Height));
                        world.Grid.Rect(selectedGeo.UpperLeft, selectedGeo.LowerRight, selectedGeo, world.gridAddGeo);
                    }
                }
            }
        }
Esempio n. 2
0
        private static void readGeoVars(List<string[]> commands, World map, Dictionary<string, Geo> geos, List<Geo> geoNoID)
        {
            double tempDbl0 = 0f;
            double tempDbl1 = 0f;
            string id = null;
            bool display = true;
            bool closeLoop = true;
            List<VectorF> vertices = new List<VectorF>();

            for (int j = 1; j < commands.Count; j++)
            {
                string value = translateValue(commands[j][2], "[Grid]", map);
                bool valueValid = true;
                switch (commands[j][0])
                {
                    case "Vertex":
                        string[] tempStr = value.Split(',');

                        for (int i = 0; i < tempStr.Length; i += 2)
                        {
                            valueValid = double.TryParse(tempStr[i], out tempDbl0);
                            valueValid = double.TryParse(tempStr[i + 1], out tempDbl1);

                            // add previous vertex again to start new line
                            if (vertices.Count > 1)
                                vertices.Add(vertices[vertices.Count - 1]);

                            vertices.Add(new VectorF((FInt)tempDbl0, (FInt)tempDbl1));
                        }
                        break;
                    case "ID":
                        id = value;
                        break;
                    case "Display":
                        valueValid = value == "true" || value == "false";
                        display = value == "true";
                        break;
                    case "CloseLoop":
                        valueValid = value == "true" || value == "false";
                        closeLoop = value == "true";
                        break;
                    default:
                        throw new Exception("Geo variable not recognized: " + commands[j][0]);
                }

                if (!valueValid)
                    throw new Exception("Value '" + commands[j][2] + "' not valid.");
            }

            Geo geo = new Geo(map);
            if (id != null)
            {
                geo.ID = id;
                geos.Add(id, geo);
            }
            geo.Display = display;
            geo.CloseLoop = closeLoop;
            VectorF[] vArray = new VectorF[vertices.Count];
            vertices.CopyTo(vArray);
            geo.Vertices = vArray;
            geo.refreshMath(new Vector2(50f)); // TODO: find out texture size

            if (id != null)
                map.addGeo(geo);
            else
                geoNoID.Add(geo);
        }