All pastes #631314 Raw Edit

Mine

public text v1 · immutable
#631314 ·published 2007-07-23 21:23 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace KS.GUI
{
    public class Window
    {
        [Flags]
        public enum CreateFlags
        {
            Center = 1,
            Hidden = 2,
            Top = 4
        }

        private List<Widget> Widgets = new List<Widget>( );

        private Texture RenderToTexture;
        private Surface RenderToSurface;

        private Rectangle Frame;
        private Vector2 Position;

        private Vector2 MouseDelta;
        private Vector2 MousePreviousPosition;

        private float totalTime;

        private string Caption;

        public bool Hidden = false;

        private System.Drawing.Color WindowColor = System.Drawing.Color.FromArgb( 128, 0, 204, 255 );
        private System.Drawing.Color TitlebarColor = System.Drawing.Color.FromArgb( 0, 204, 255 );
        private System.Drawing.Color TitleColor = System.Drawing.Color.FromArgb( 0, 255, 255 );

        private bool IsDragging;

        private Effect windowBlurEffect;

        float randomValue = 0;

        public Window( Rectangle frame, string caption, CreateFlags createFlags )
        {
            Frame = frame;
            Caption = caption;

            Position.X = frame.X;
            Position.Y = frame.Y;

            RenderToTexture = new Microsoft.DirectX.Direct3D.Texture( BEngine.EngineDevice.D3DDevice, Frame.Width, Frame.Height, 1, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default );
            RenderToSurface = RenderToTexture.GetSurfaceLevel( 0 );

            Game.EngineDevice.AddResource( RenderToTexture );
            Game.EngineDevice.AddResource( RenderToSurface );

            windowBlurEffect = Effect.FromFile
                (
                BEngine.EngineDevice.D3DDevice,
                Project.BaseDir + "Shaders\\GUI\\Window.fx",
                null,
                null,
                ShaderFlags.None,
                null
                );

            windowBlurEffect.Technique = "PostProcess";

            if( ( CreateFlags.Center & createFlags ) == CreateFlags.Center )
            {
                MoveToCenter( );
            }

            if( ( CreateFlags.Hidden & createFlags ) == CreateFlags.Hidden )
            {
                Hidden = true;
            }

            if( ( CreateFlags.Top & createFlags ) == CreateFlags.Top )
            {
                Game.WindowManager.MoveToFront( Game.WindowManager.WindowPool.Count - 1 );
            }
        }

        public void MoveToCenter( )
        {
            Position = new Vector2
                    (
                    ( frmMain.WindowWidth / 2 ) - ( Frame.Width / 2 ),
                    ( frmMain.WindowHeight / 2 ) - ( Frame.Height / 2 )
                    );
        }

        public void Update( float deltaTime )
        {
            if( Hidden )
                return;

            MouseDelta = Game.InputManager.MousePositionInGame - MousePreviousPosition;

            if
                (
                IsDraggableFrom( Game.InputManager.MouseDownFromInGame ) &&
                IsDraggableFrom( Game.InputManager.MousePositionInGame ) &&
                Game.InputManager.MouseDown &&
                Game.InputManager.MouseButtons == System.Windows.Forms.MouseButtons.Left
                )
                IsDragging = true;

            if( IsDragging )
            {
                Position += MouseDelta;
            }

            if( !Game.InputManager.MouseDown )
                IsDragging = false;

            MousePreviousPosition = Game.InputManager.MousePositionInGame;
        }

        private bool IsDraggableFrom( Vector2 position )
        {
            return
                (
                position.X > Position.X &&
                position.Y > Position.Y &&
                position.X < Position.X + Frame.Width &&
                position.Y < Position.Y + Game.EngineDevice.Text.MeasureSize( Caption ).Height
                );
        }

        public void Render( float deltaTime )
        {
            if( Hidden )
                return;

            BEngine.EngineDevice.D3DDevice.SetRenderTarget( 0, RenderToSurface );
            BEngine.EngineDevice.D3DDevice.Clear( ClearFlags.Target, WindowColor.ToArgb( ), 0, 0 );

            Game.EngineDevice.Text.RenderText( Caption, 4, 0, TitleColor.ToArgb( ) );

            if( Widgets.Count > 0 )
            {
                for( int i = 0; i < Widgets.Count; i++ )
                {
                    Widgets[ i ].Render( 0.0f );
                }
            }

            RenderOutlines( );

            BEngine.EngineDevice.D3DDevice.SetRenderTarget( 0, Game.EngineDevice.BackBuffer );

            Game.EngineDevice.TextureManager.SetTexture( 0, RenderToTexture );

            if( deltaTime > 2 )
            {
                randomValue = ( -deltaTime );
            }

            windowBlurEffect.SetValue( "timeraslow", randomValue );
            int numPasses = windowBlurEffect.Begin( 0 );

            for( int i = 0; i < numPasses; i++ )
            {
                windowBlurEffect.BeginPass( i );

                Game.EngineDevice.Render.Render2DTransformedBox
                              (
                              Position.X,
                              Position.Y,
                              Position.X + Frame.Width,
                              Position.Y + Frame.Height,
                              System.Drawing.Color.FromArgb( 255, 255, 255, 255 ).ToArgb( )
                              );

                windowBlurEffect.EndPass( );
            }
            windowBlurEffect.End( );

            Game.EngineDevice.TextureManager.SetNullTexture( 0 );
        }

        private void RenderOutlines( )
        {
            Game.EngineDevice.Render.Render2DLine
                (
                0,
                Game.EngineDevice.Text.MeasureSize( Caption ).Height,
                Frame.Width,
                Game.EngineDevice.Text.MeasureSize( Caption ).Height,
                TitleColor.ToArgb( )
                );

            Game.EngineDevice.Render.Render2DLine
                (
                0,
                0,
                0,
                Frame.Height,
                TitleColor.ToArgb( )
                );

            Game.EngineDevice.Render.Render2DLine
                (
                0,
                0,
                Frame.Width,
                0,
                TitleColor.ToArgb( )
                );

            Game.EngineDevice.Render.Render2DLine
                (
                Frame.Width - 1,
                0,
                Frame.Width - 1,
                Frame.Height,
                TitleColor.ToArgb( )
                );

            Game.EngineDevice.Render.Render2DLine
                (
                0,
                Frame.Height - 1,
                Frame.Width - 1,
                Frame.Height - 1,
                TitleColor.ToArgb( )
                );

            Game.EngineDevice.Render.Render2DLine
                (
                Frame.Width - 39,
                0,
                Frame.Width - 39,
                Game.EngineDevice.Text.MeasureSize( Caption ).Height,
                TitleColor.ToArgb( )
                );
        }
    }
}