Exemplo n.º 1
0
        public static void RemoveTextOneWay(Text textToRemove)
        {
#if DEBUG
            if (!FlatRedBallServices.IsThreadPrimary())
            {
                throw new InvalidOperationException("Objects can only be added on the primary thread");
            }
#endif

            if (textToRemove.ListsBelongingTo.Contains(mDrawnTexts))
            {
                mDrawnTexts.Remove(textToRemove);
            }

            if (textToRemove.ListsBelongingTo.Contains(mAutomaticallyUpdatedTexts))
            {
                mAutomaticallyUpdatedTexts.Remove(textToRemove);
            }


            SpriteManager.UnderAllDrawnLayer.Remove(textToRemove);

            foreach (Layer sl in SpriteManager.Layers)
            {
                sl.Remove(textToRemove);
            }

            //This is to clean up the Sprite from the engine and
            // unloads its texture
            if (textToRemove.RenderOnTexture)
            {
                textToRemove.RenderOnTexture = false;
            }
        }
Exemplo n.º 2
0
        public static void RemoveWindow(IWindow windowToRemove, bool keepEvents)
        {
#if DEBUG
            if (!FlatRedBallServices.IsThreadPrimary())
            {
                throw new InvalidOperationException("Windows can only be removed on the primary thread");
            }
#endif
            if (mWindowArray.Contains(windowToRemove))
            {
                mWindowArray.Remove(windowToRemove);
            }
            else if (mPerishableArray.Contains(windowToRemove))
            {
                windowToRemove.Visible = false;
                mPerishableArray.Remove(windowToRemove);
            }
            // If an IWindow is made dominant, then it will be removed from the regular (mWindowArray) window list.
            else if (mDominantWindows.Contains(windowToRemove))
            {
                mDominantWindows.Remove(windowToRemove);
            }

            if (InputManager.InputReceiver == windowToRemove)
            {
                InputManager.InputReceiver = null;
            }
        }
Exemplo n.º 3
0
        private Texture2D LoadTextureFromFile(string loc)
        {
            bool canLoadNow = true;

#if ANDROID
            canLoadNow &= FlatRedBallServices.IsThreadPrimary();
#endif

            if (!canLoadNow)
            {
#if ANDROID
                AddTexturesToLoadOnPrimaryThread(loc);
#endif
                return(null);
            }
            else
            {
                Texture2D file = null;

                using (Stream titleStream = FileManager.GetStreamForFile(loc))
                {
                    file = Texture2D.FromStream(Renderer.GraphicsDevice, titleStream);
                }

                file = MakePremultiplied(file);
                return(file);
            }
        }
Exemplo n.º 4
0
        static int WindowComparisonForSorting(IWindow first, IWindow second)
        {
#if DEBUG
            if (!FlatRedBallServices.IsThreadPrimary())
            {
                throw new InvalidOperationException("Objects can only be added on the primary thread");
            }
#endif

            if (first.Layer == second.Layer)
            {
                if (first.Z == second.Z)
                {
                    return(mWindowArray.IndexOf(first).CompareTo(mWindowArray.IndexOf(second)));
                }
                else
                {
                    return(first.Z.CompareTo(second.Z));
                }
            }
            else
            {
                int firstLayerIndex  = SpriteManager.LayersWriteable.IndexOf(first.Layer);
                int secondLayerIndex = SpriteManager.LayersWriteable.IndexOf(second.Layer);
                return(firstLayerIndex.CompareTo(secondLayerIndex));
            }
        }
Exemplo n.º 5
0
        public static void AddWindow(IWindow windowToAdd)
        {
#if DEBUG
            if (windowToAdd == null)
            {
                throw new ArgumentException("Argument Window can't be null");
            }
            if (mWindowArray.Contains(windowToAdd))
            {
                int index = mWindowArray.IndexOf(windowToAdd);

                throw new ArgumentException("This window has already been added to the GuiManager.  It is at index " + index);
            }
            if (!FlatRedBallServices.IsThreadPrimary())
            {
                throw new InvalidOperationException("Windows can only be added on the primary thread");
            }
#endif
            mWindowArray.Add(windowToAdd);

            if (BringsClickedWindowsToFront == false)
            {
                InsertionSort(mWindowArray, WindowComparisonForSorting);
            }
        }
Exemplo n.º 6
0
        static public void RemoveCursor(Cursor cursorToRemove)
        {
#if DEBUG
            if (!FlatRedBallServices.IsThreadPrimary())
            {
                throw new InvalidOperationException("Cursors can only be removed on the primary thread");
            }
#endif
            mCursors.Remove(cursorToRemove);
        }
Exemplo n.º 7
0
        public static void MakeRegularWindow(IWindow window)
        {
#if DEBUG
            if (!FlatRedBallServices.IsThreadPrimary())
            {
                throw new InvalidOperationException("Windows can only be added or modified on the primary thread");
            }
#endif

            if (mDominantWindows.Contains(window))
            {
                mDominantWindows.Remove(window);
            }

            if (!mWindowArray.Contains(window))
            {
                mWindowArray.Add(window);
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Adds a window as a Dominant Window.  If the window is a regular Window
        /// already managed by the GuiManager it will be removed from the regularly-managed
        /// windows.
        /// </summary>
        /// <param name="windowToSet">The window to add to the Dominant Window stack.</param>
        #endregion
        static public void AddDominantWindow(IWindow window)
        {
#if DEBUG
            if (!FlatRedBallServices.IsThreadPrimary())
            {
                throw new InvalidOperationException("Dominant windows can only be added or modified on the primary thread");
            }
#endif
            // Let's make these tolerant
            if (mWindowArray.Contains(window))
            {
                mWindowArray.Remove(window);
            }

            if (!mDominantWindows.Contains(window))
            {
                mDominantWindows.Add(window);
            }
        }
Exemplo n.º 9
0
        public static void RemoveText(Text textToRemove)
        {
#if DEBUG
            if (!FlatRedBallServices.IsThreadPrimary())
            {
                throw new InvalidOperationException("Objects can only be added on the primary thread");
            }
#endif
            if (textToRemove == null)
            {
                return;
            }

            textToRemove.ClearRelationships();

            textToRemove.RemoveSelfFromListsBelongingTo();

            //This is to clean up the Sprite from the engine and
            // unloads its texture
            if (textToRemove.RenderOnTexture)
            {
                textToRemove.RenderOnTexture = false;
            }
        }
Exemplo n.º 10
0
        public static Texture2D MakePremultiplied(Texture2D file)
        {
            bool useImageData = FlatRedBallServices.IsThreadPrimary() == false;

#if WINDOWS
            // Since the graphics device can get lost, we don't want to use render targets, so we'll fall
            // back to using ImageData:
            useImageData = true;
#endif

            if (useImageData)
            {
                // Victor Chelaru
                // April 22, 2015
                // The purpose of this
                // code is to support loading
                // textures from file (as opposed
                // to using the content pipeline) and
                // having them contain premultiplied alpha.
                // I believe at one point using render targets
                // may not have worked on a secondary thread. I
                // switched the code to using render targets (not
                // image data), and ran the automated tests which do
                // background texture loading on PC and all seemed to
                // work okay. Render targets will be much faster than ImageData
                // and imagedata seems to throw errors on iOS, so I'm going to try
                // render targets and see what happens.
                lock (mPremultipliedAlphaImageData)
                {
                    mPremultipliedAlphaImageData.ExpandIfNecessary(file.Width, file.Height);

                    mPremultipliedAlphaImageData.CopyFrom(file);

                    mPremultipliedAlphaImageData.MakePremultiplied(file.Width * file.Height);

                    mPremultipliedAlphaImageData.ToTexture2D(file);
                }
                return(file);
            }
            else
            {
                RenderTarget2D result = null;
                lock (Renderer.Graphics.GraphicsDevice)
                {
                    if (MathFunctions.IsPowerOfTwo(file.Width) &&
                        MathFunctions.IsPowerOfTwo(file.Height))
                    {
                        //Setup a render target to hold our final texture which will have premulitplied alpha values
                        result = new RenderTarget2D(Renderer.GraphicsDevice, file.Width, file.Height, CreateMipMaps, SurfaceFormat.Color, DepthFormat.None);
                    }
                    else
                    {
                        result = new RenderTarget2D(Renderer.GraphicsDevice, file.Width, file.Height);
                    }


                    Renderer.GraphicsDevice.SetRenderTarget(result);
                    Renderer.GraphicsDevice.Clear(Color.Black);

                    //Multiply each color by the source alpha, and write in just the color values into the final texture
                    BlendState blendColor = new BlendState();
                    blendColor.ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue;

                    blendColor.AlphaDestinationBlend = Blend.Zero;
                    blendColor.ColorDestinationBlend = Blend.Zero;

                    blendColor.AlphaSourceBlend = Blend.SourceAlpha;
                    blendColor.ColorSourceBlend = Blend.SourceAlpha;

                    var position = Vector2.Zero;

                    SpriteBatch spriteBatch = new SpriteBatch(Renderer.GraphicsDevice);

#if MONOGAME
                    spriteBatch.Begin(SpriteSortMode.Immediate, blendColor, samplerState: SamplerState.PointClamp);
#else
                    spriteBatch.Begin(SpriteSortMode.Immediate, blendColor, SamplerState.PointClamp, null, null);
#endif
                    spriteBatch.Draw(file, position, Color.White);
                    spriteBatch.End();

                    //Now copy over the alpha values from the PNG source texture to the final one, without multiplying them
                    BlendState blendAlpha = new BlendState();
                    blendAlpha.ColorWriteChannels = ColorWriteChannels.Alpha;

                    blendAlpha.AlphaDestinationBlend = Blend.Zero;
                    blendAlpha.ColorDestinationBlend = Blend.Zero;

                    blendAlpha.AlphaSourceBlend = Blend.One;
                    blendAlpha.ColorSourceBlend = Blend.One;

#if MONOGAME
                    spriteBatch.Begin(SpriteSortMode.Immediate, blendAlpha, samplerState: SamplerState.PointClamp);
#else
                    spriteBatch.Begin(SpriteSortMode.Immediate, blendAlpha, SamplerState.PointClamp, null, null);
#endif
                    spriteBatch.Draw(file, position, Color.White);
                    spriteBatch.End();

                    //Release the GPU back to drawing to the screen
                    Renderer.GraphicsDevice.SetRenderTarget(null);
                }

                Renderer.ForceSetBlendOperation();
                Renderer.ForceSetColorOperation(Renderer.mLastColorOperationSet);

                return(result as Texture2D);
            }
        }