Beispiel #1
0
 private static void LayerDisposed(SceneLayer layer)
 {
     lock (staticMutationLock) {
         createdLayers.Remove(layer);
         UpdateEnabledLayers();
     }
 }
        public void ShouldCallEnableOnResourcesWhenConstructed()
        {
            SceneLayer        testLayer    = new SceneLayer("Test Layer");
            TestLayerResource testResource = testLayer.GetResource <TestLayerResource>();

            Assert.IsTrue(testResource.IsEnabled);
            testLayer.Dispose();
        }
        public void TestGetSetProperties()
        {
            // Define variables and constants
            const int    PROP1     = 34;
            const string PROP2     = "Hello";
            const bool   PROP3     = false;
            const object PROP4     = null;
            SceneLayer   testLayer = new SceneLayer("Test Layer");

            // Set up context
            object prop3Added = null;

            testLayer.PropertySet += (layer, s, arg3) => {
                if (s == "prop3")
                {
                    prop3Added = arg3;
                }
            };

            testLayer.SetProperty("prop1", PROP1);
            testLayer["prop2"] = PROP2;
            testLayer.SetProperty("prop3", PROP3);
            testLayer["prop4"] = PROP4;

            // Execute
            Assert.AreEqual(PROP1, testLayer["prop1"]);
            Assert.AreEqual(PROP2, testLayer["prop2"]);
            Assert.AreEqual(PROP3, testLayer.GetProperty("prop3"));
            Assert.AreEqual(PROP4, testLayer.GetProperty("prop4"));
            Assert.AreEqual(PROP3, prop3Added);

            object prop1Removed = null;

            testLayer.PropertyRemoved += (layer, s, arg3) => {
                if (s == "prop1")
                {
                    prop1Removed = arg3;
                }
            };

            testLayer.RemoveProperty("prop1");
            testLayer.RemoveProperty("prop3");

            // Assert outcome
            Assert.AreEqual(PROP1, prop1Removed);
            Assert.IsFalse(testLayer.ContainsProperty("prop1"));
            Assert.AreEqual(PROP2, testLayer["prop2"]);
            Assert.AreEqual(PROP4, testLayer.GetProperty("prop4"));

            try {
                var obj = testLayer["prop3"];
                Console.WriteLine(obj);
                Assert.Fail();
            }
            catch (KeyNotFoundException) { }

            testLayer.Dispose();
        }
 /// <summary>
 /// Creates this scene layer resource, setting the given <see cref="OwningLayer"/>.
 /// </summary>
 /// <param name="owningLayer">The <see cref="SceneLayer"/> that this resource is being attached to. Never null.</param>
 protected SceneLayerResource(SceneLayer owningLayer)
 {
     if (owningLayer == null)
     {
         throw new ArgumentNullException("owningLayer");
     }
     if (owningLayer.IsDisposed)
     {
         throw new ObjectDisposedException("owningLayer");
     }
     OwningLayer = owningLayer;
     Logger.Debug("Created new " + this + ".");
 }
        internal SceneLayer(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            Name = name;

            lock (staticMutationLock) {
                for (uint i = 0; i < activeLayers.Length; ++i)
                {
                    if (activeLayers[i] == null)
                    {
                        Index = i;
                        goto slotFound;
                    }
                }
                SceneLayer[] newMatArray = new SceneLayer[activeLayers.Length * 2];
                Array.Copy(activeLayers, newMatArray, activeLayers.Length);
                Index        = (uint)activeLayers.Length;
                activeLayers = newMatArray;
                slotFound : activeLayers[Index] = this;
            }

            foreach (Type resourceType in ReflectionUtils.GetChildTypes(typeof(SceneLayerResource)))
            {
                if (resourceType.IsInstantiable())
                {
                    ConstructorInfo sceneLayerCtor = resourceType.GetConstructor(
                        BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
                        null,
                        new[] { typeof(SceneLayer) },
                        new[] { new ParameterModifier() }
                        );
                    if (sceneLayerCtor == null)
                    {
                        Logger.Debug("Found instantiable scene layer resource type '" + resourceType.Name + "', but could not instantiate, " +
                                     "as no constructor with signature " + resourceType.Name + "(SceneLayer) was found.");
                    }
                    else
                    {
                        resources[resourceType] = (SceneLayerResource)sceneLayerCtor.Invoke(new object[] { this });
                    }
                }
            }

            OnLayerEnabled();

            Logger.Debug("Created new " + this + ".");
        }
Beispiel #6
0
        public void TestLayerCreatedEvent()
        {
            // Define variables and constants
            const string TEST_LAYER_NAME = "Test Layer";
            SceneLayer   createdLayer    = null;

            // Set up context
            Scene.LayerCreated += layer => createdLayer = layer;

            // Execute
            Scene.CreateLayer(TEST_LAYER_NAME).Dispose();

            // Assert outcome
            Assert.AreEqual(TEST_LAYER_NAME, createdLayer.Name);
        }
Beispiel #7
0
 /// <summary>
 /// Creates a new <see cref="SceneLayer"/> and adds it to the scene.
 /// Scenes are created as disabled (e.g. <see cref="SceneLayer.IsEnabled"/> is false).
 /// </summary>
 /// <param name="layerName">The name for the layer. Must not be null.</param>
 /// <exception cref="InvalidOperationException">Thrown if a layer with the given name already exists in the scene.</exception>
 /// <returns>The newly created layer. The layer will also be added to the <see cref="AllLayers"/> collection,
 /// so you do not need to retain a reference to the returned object to keep it from becoming garbage.</returns>
 public static SceneLayer CreateLayer(string layerName)
 {
     lock (staticMutationLock) {
         if (createdLayers.Any(layer => layer.Name == layerName))
         {
             throw new InvalidOperationException("An undisposed layer with the name '" + layerName + "' already exists in the scene.");
         }
         SceneLayer result = new SceneLayer(layerName);
         result.LayerDisposed += LayerDisposed;
         createdLayers.Add(result);
         if (layerCreated != null)
         {
             layerCreated(result);
         }
         UpdateEnabledLayers();
         return(result);
     }
 }
        public void TestEnableDisableDispose()
        {
            // Define variables and constants
            SceneLayer testLayer = new SceneLayer("Test Layer");
            bool       wasEnabled = false, wasDisabled = false, wasDisposed = false;

            // Set up context
            testLayer.LayerEnabled  += layer => wasEnabled = true;
            testLayer.LayerDisabled += layer => wasDisabled = true;
            testLayer.LayerDisposed += layer => wasDisposed = true;

            // Execute
            testLayer.IsEnabled = false;
            testLayer.IsEnabled = true;
            testLayer.Dispose();

            // Assert outcome
            Assert.IsTrue(wasEnabled);
            Assert.IsTrue(wasDisabled);
            Assert.IsTrue(wasDisposed);
        }
        public void TestResources()
        {
            // Define variables and constants
            SceneLayer testLayer = new SceneLayer("Test Layer");

            // Set up context
            TestLayerResource testResource = testLayer.GetResource <TestLayerResource>();

            // Execute
            Assert.IsFalse(testResource.IsDisabled);
            testLayer.IsEnabled = false;
            Assert.IsTrue(testResource.IsDisabled);
            testLayer.IsEnabled = true;
            Assert.IsTrue(testResource.IsEnabled);
            Assert.AreEqual(testLayer.GetResource <TestLayerResource>(), testResource);
            Assert.IsFalse(testResource.IsDisposed);
            testLayer.Dispose();
            Assert.IsTrue(testResource.IsDisposed);

            // Assert outcome
        }
Beispiel #10
0
 public static SceneLayer GetLayerByIndex(uint index)
 {
     return(SceneLayer.GetLayerByIndex(index));
 }
 public TestLayerResource(SceneLayer owningLayer) : base(owningLayer)
 {
 }