Пример #1
0
        public void TapeMeasure_DefineTextToBeDrawn_ShortTextWithPilcrow()
        {
            // Arrange
            var pageSize         = new SizeF(500, 2000);
            var startingLocation = new SizeF(0, 0);
            var shortText        = "Short Text";
            var pilcrow          = "¶";
            var text             = shortText + pilcrow;
            var font             = new Font(FontFamily.GenericSerif, 12, FontStyle.Bold);

            var mockGraphics = new Mock <IGraphics>();
            var measure      = new TapeMeasure(mockGraphics.Object);

            mockGraphics.Setup(mock => mock.MeasureString(It.IsAny <String>(), It.IsAny <Font>())).Returns(new SizeF(100, 12));

            // Act
            SectionToBeDrawn section = measure.DefineTextToBeDrawn(pageSize, startingLocation, text, font);

            // Assert
            Assert.That(section.SectionParts.Count, Is.EqualTo(1), "Only one text block.");
            Assert.That(section.SectionParts[0].Text, Is.EqualTo(shortText), "Text should be short text.");
            Assert.That(section.SectionParts[0].Font, Is.EqualTo(font), "Font must be the same.");
            Assert.That(section.SectionParts[0].LeftEdge, Is.EqualTo(0), "Block should start at the left side of the page.");
            Assert.That(section.SectionParts[0].TopEdge, Is.EqualTo(0), "Block should start at the top of the page.");
            Assert.That(section.SectionParts[0].Width, Is.EqualTo(500), "Block should reach the end of page.");
            Assert.That(section.SectionParts[0].Height, Is.EqualTo(12), "Block should be one line high.");
        }
Пример #2
0
        public void TapeMeasure_DefineTextToBeDrawn_Pilcrow()
        {
            // We want a Pilcrow to represent a new-line/carriage-return
            // by taking up the remainder of the line (and not printing the pilcrow)

            // Arrange
            var pageSize         = new SizeF(500, 2000);
            var startingLocation = new SizeF(0, 0);
            var text             = "¶";
            var font             = new Font(FontFamily.GenericSerif, 12, FontStyle.Bold);

            var mockGraphics = new Mock <IGraphics>();
            var measure      = new TapeMeasure(mockGraphics.Object);

            mockGraphics.Setup(mock => mock.MeasureString(It.IsAny <String>(), It.IsAny <Font>())).Returns(new SizeF(10, 12));

            // Act
            SectionToBeDrawn section = measure.DefineTextToBeDrawn(pageSize, startingLocation, text, font);

            // Assert
            Assert.That(section.SectionParts.Count, Is.EqualTo(1), "Only one text block.");
            Assert.That(section.SectionParts[0].Text, Is.EqualTo(""), "Text should be empty.");
            Assert.That(section.SectionParts[0].Font, Is.EqualTo(font), "Font must be the same.");
            Assert.That(section.SectionParts[0].LeftEdge, Is.EqualTo(0), "Block should start at the left side of the page.");
            Assert.That(section.SectionParts[0].TopEdge, Is.EqualTo(0), "Block should start at the top of the page.");
            Assert.That(section.SectionParts[0].Width, Is.EqualTo(500), "Block should reach the end of page.");
            Assert.That(section.SectionParts[0].Height, Is.EqualTo(12), "Block should be one line high.");
        }
Пример #3
0
        public void TapeMeasure_Constructor_NoParams()
        {
            // Arrange

            // Act
            var tm = new TapeMeasure();

            // Assert
            Assert.That(tm, Is.Not.Null, "Empty constructor should still construct");
        }
Пример #4
0
        public void TapeMeasure_DefineTextToBeDrawn_LongText()
        {
            // Arrange
            var pageSize         = new SizeF(500, 2000);
            var startingLocation = new SizeF(0, 0);
            var text             = "Hello Elemental Purposeful Octopodes. Your activity is graceless but your results are without equal.";
            var font             = new Font(FontFamily.GenericSerif, 12, FontStyle.Bold);

            var mockGraphics = new Mock <IGraphics>();
            var measure      = new TapeMeasure(mockGraphics.Object);

            mockGraphics.SetupSequence(mock => mock.MeasureString(It.IsAny <String>(), It.IsAny <Font>()))
            .Returns(new SizeF(50, 12))
            .Returns(new SizeF(160, 12))
            .Returns(new SizeF(260, 12))
            .Returns(new SizeF(370, 12))
            .Returns(new SizeF(420, 12))
            .Returns(new SizeF(510, 12))
            .Returns(new SizeF(420, 12))
            .Returns(new SizeF(80, 12))
            .Returns(new SizeF(110, 12))
            .Returns(new SizeF(210, 12))
            .Returns(new SizeF(250, 12))
            .Returns(new SizeF(300, 12))
            .Returns(new SizeF(380, 12))
            .Returns(new SizeF(420, 12))
            .Returns(new SizeF(500, 12))
            .Returns(new SizeF(570, 12))
            .Returns(new SizeF(500, 12))
            .Returns(new SizeF(60, 12));

            // Act
            SectionToBeDrawn section = measure.DefineTextToBeDrawn(pageSize, startingLocation, text, font);

            // Assert
            Assert.That(section.SectionParts.Count, Is.EqualTo(3), "There should be three text blocks.");
            Assert.That(section.SectionParts[0].Text, Is.EqualTo("Hello Elemental Purposeful Octopodes. Your"), "Text should be 'Hello Elemental Purposeful Octopodes. Your'.");
            Assert.That(section.SectionParts[0].Font, Is.EqualTo(font), "Font must be the same.");
            Assert.That(section.SectionParts[0].LeftEdge, Is.EqualTo(0), "Block should start at the left side of the page.");
            Assert.That(section.SectionParts[0].TopEdge, Is.EqualTo(0), "Block should start at the top of the page.");
            Assert.That(section.SectionParts[0].Width, Is.EqualTo(420), "Block should be width 420.");
            Assert.That(section.SectionParts[0].Height, Is.EqualTo(12), "Block should be one line high.");
            Assert.That(section.SectionParts[1].Text, Is.EqualTo("activity is graceless but your results are without"), "Text should be 'activity is graceless but your results are without'.");
            Assert.That(section.SectionParts[1].Font, Is.EqualTo(font), "Font must be the same.");
            Assert.That(section.SectionParts[1].LeftEdge, Is.EqualTo(0), "Block should start at the left side of the page.");
            Assert.That(section.SectionParts[1].TopEdge, Is.EqualTo(12), "Block should start one line down .");
            Assert.That(section.SectionParts[1].Width, Is.EqualTo(500), "Block should be 500 wide.");
            Assert.That(section.SectionParts[1].Height, Is.EqualTo(12), "Block should be one line high.");
            Assert.That(section.SectionParts[2].Text, Is.EqualTo("equal."), "Text should be 'equal'.");
            Assert.That(section.SectionParts[2].Font, Is.EqualTo(font), "Font must be the same.");
            Assert.That(section.SectionParts[2].LeftEdge, Is.EqualTo(0), "Block should start at the left side of the page.");
            Assert.That(section.SectionParts[2].TopEdge, Is.EqualTo(24), "Block should start two lines down.");
            Assert.That(section.SectionParts[2].Width, Is.EqualTo(60), "Block should be width 60.");
            Assert.That(section.SectionParts[2].Height, Is.EqualTo(12), "Block should be one line high.");
        }
Пример #5
0
        public void TapeMeasure_Constructor_PassGraphics()
        {
            // Arrange
            var graphicsWrapper = GraphicsWrapper.Instance;

            // Act
            var tm = new TapeMeasure(graphicsWrapper);

            // Assert
            Assert.That(tm, Is.Not.Null, "Empty constructor should still construct");
        }
Пример #6
0
        public void TapeMeasure_FindEndOfNextWord_FromMiddle()
        {
            // Arrange
            String text = "Hello Elemental Purposeful Octopodes.";
            Int32  currentWordEnding = 16;

            // Act
            Int32 numberOfCharacters = TapeMeasure.FindNextWordEnding(text, currentWordEnding);

            // Assert
            Assert.AreEqual(26, numberOfCharacters, $"There are {numberOfCharacters} characters in the word, not the 26 expected.");
        }
Пример #7
0
        public void TapeMeasure_MeasureText_HasLength()
        {
            // Arrange
            String text         = "Hello I am a piece of text";
            Single textLength   = 10 * text.Length;
            var    font         = new Font(FontFamily.GenericSerif, 100.0f, FontStyle.Bold);
            var    mockGraphics = new Mock <IGraphics>();
            var    measure      = new TapeMeasure(mockGraphics.Object);

            mockGraphics.Setup(mock => mock.MeasureString(It.IsAny <String>(), It.IsAny <Font>())).Returns(new SizeF(textLength, 12));

            // Act
            SizeF textSize = measure.MeasureText(text, font);

            // Assert
            Assert.That(textSize.Width, Is.EqualTo(textLength), $"Text width is {textSize.Width}, not 10.0.");
            mockGraphics.Verify(mock => mock.MeasureString(It.IsAny <String>(), It.IsAny <Font>()), Times.Exactly(1));
        }
Пример #8
0
        public void TapeMeasure_DefineTextToBeDrawn_EmptyText()
        {
            // We want an empty string to be effectively ignored

            // Arrange
            var pageSize         = new SizeF(500, 2000);
            var startingLocation = new SizeF(0, 0);
            var text             = "";
            var font             = new Font(FontFamily.GenericSerif, 12, FontStyle.Bold);

            var mockGraphics = new Mock <IGraphics>();
            var measure      = new TapeMeasure(mockGraphics.Object);

            // Act
            SectionToBeDrawn section = measure.DefineTextToBeDrawn(pageSize, startingLocation, text, font);

            // Assert
            Assert.That(section.SectionParts.Count, Is.EqualTo(0), "No text blocks.");
        }
Пример #9
0
        // When a button is clicked in the toolbar or context menu
        private void toolbar_ButtonClicked(object sender, EventArgs e)
        {
            // Get tag of button
            string tag = string.Empty;

            if (sender is ToolStripButton button)
            {
                if (button.Tag is string text)
                {
                    tag = text;
                }
            }
            else if (sender is ToolStripMenuItem item)
            {
                if (item.Tag is string text)
                {
                    tag = text;
                }
            }

            // Get action to perform and perform it
            switch (tag)
            {
            case "newFile":
                SaveFilePerformAction(NewFile);
                break;

            case "openFile":
                SaveFilePerformAction(OpenFile);
                break;

            case "saveFile":
                SaveFile();
                break;

            case "saveAs":
                SaveAs();
                break;

            case "exit":
                CloseEditor();
                break;

            case "screenshot":
                Screenshot();
                break;

            case "preferences":
                OpenPreferences();
                break;

            case "undo":
                simulation.LoadState(undoRedoStack.Undo());
                toolbar.SetUndoButtonState(undoRedoStack.CanUndo(), undoRedoStack.CanRedo());
                break;

            case "redo":
                simulation.LoadState(undoRedoStack.Redo());
                toolbar.SetUndoButtonState(undoRedoStack.CanUndo(), undoRedoStack.CanRedo());
                break;

            case "cut":
                clipboardObject = (SimulationObject)simulation.SelectedObject;
                simulation.RemoveObject(clipboardObject);
                inspector.SetDataSource(simulation.GetObjectsToDisplay());
                PerformedAction();
                break;

            case "copy":
                clipboardObject = (SimulationObject)simulation.SelectedObject;
                break;

            case "paste":
                if (clipboardObject != null)
                {
                    // Create copy of object
                    JsonSerializerSettings settings = new JsonSerializerSettings()
                    {
                        TypeNameHandling = TypeNameHandling.All, PreserveReferencesHandling = PreserveReferencesHandling.All
                    };
                    string           data    = JsonConvert.SerializeObject(clipboardObject, Formatting.Indented, settings);
                    SimulationObject @object = JsonConvert.DeserializeObject <SimulationObject>(data, settings);

                    @object.Position = simulation.Camera.GetSimulationPostion(simulation.MousePosition);

                    // Get name of object
                    CreateNewObject(@object);

                    PerformedAction();
                }
                break;

            case "deleteObject":
                SimulationObject selectedObject = (SimulationObject)simulation.SelectedObject;
                simulation.RemoveObject(selectedObject);
                inspector.SetDataSource(simulation.GetObjectsToDisplay());
                PerformedAction();
                break;

            case "fireCannon":
                if (simulation.SelectedObject is Cannon cannon)
                {
                    cannon.Fire();
                }
                break;

            case "newBox":
                CreateNewObject(new Box("box", simulation.ScreenCentre, "crate", 0.95f, new Vector2(64, 64)));
                PerformedAction();
                break;

            case "newWall":
                CreateNewObject(new Wall("wall", simulation.ScreenCentre, Microsoft.Xna.Framework.Color.SaddleBrown, 0.95f, new Vector2(25, 300)));
                break;

            case "newCannon":
                Projectile projectile = new Projectile("redTempProjectile", Vector2.Zero, "ball", 5, 0.9f, 16, 0.005f);
                CreateNewObject(new Cannon("cannon", simulation.ScreenCentre, "cannon", projectile));
                PerformedAction();
                break;

            case "newTapeMeasure":

                TapeMeasure tapeMeasure = new TapeMeasure("tape measure", simulation.ScreenCentre, simulation.ScreenCentre + new Vector2(100, 0), 8, "line", "Arial");
                bool        created     = CreateNewObject(tapeMeasure);
                if (created)
                {
                    simulation.AddObject(tapeMeasure.Start);
                    simulation.AddObject(tapeMeasure.End);
                }
                PerformedAction();
                break;

            case "newStopwatch":
                CreateNewObject(new Stopwatch("stopwatch", simulation.ScreenCentre, "stopwatch", "SevenSegment"));
                PerformedAction();
                break;

            case "newDetector":
                CreateNewObject(new Detector("detector", simulation.ScreenCentre, "detector", 150));
                PerformedAction();
                break;

            case "play":
                simulation.Paused        = false;
                toolbar.SimulationPaused = false;
                PerformedAction();
                break;

            case "pause":
                simulation.Paused        = true;
                toolbar.SimulationPaused = true;
                PerformedAction();
                break;

            case "addStartTrigger":
                AddTriggerToStopwatch(simulation.SelectedObject, Stopwatch.StopwatchInput.Start);
                PerformedAction();
                break;

            case "addStopTrigger":
                AddTriggerToStopwatch(simulation.SelectedObject, Stopwatch.StopwatchInput.Stop);
                PerformedAction();
                break;

            case "zoomIn":
                simulation.Camera.ZoomIn(simulation.Camera.GetSimulationPostion(0.25f * new Vector2(Width, Height)));
                break;

            case "zoomOut":
                simulation.Camera.ZoomOut(simulation.Camera.GetSimulationPostion(0.25f * new Vector2(Width, Height)));
                break;

            case "about":
                OpenWebPage("https://github.com/Liam-dev/Projectile-Simulator");
                break;
            }
        }