⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uiscreens.cs

📁 《3d游戏编程入门经典》中范例小游戏。使用C#+DirectX开发。
💻 CS
📖 第 1 页 / 共 2 页
字号:
            // Start rendering sprites
            base.BeginSprite();
            // Draw the background
            base.Draw();
            // Now the buttons
            leftButton.Draw();
            rightButton.Draw();
            okButton.Draw();

            // You're done rendering sprites
            base.EndSprite();
        }

        #endregion

        #region Cleanup members

        /// <summary>
        /// Clean up the loopy mesh objects
        /// </summary>
        public void CleanupLoopyMesh()
        {
            if (loopyMesh != null)
            {
                loopyMesh.Dispose();
            }
            if (loopyTexture != null)
            {
                loopyTexture.Dispose();
            }
        }
        /// <summary>
        /// Cleanup in case dispose isn't called
        /// </summary>
        ~SelectLoopyScreen()
        {
            Dispose();
        }
        /// <summary>
        /// Cleanup any resources
        /// </summary>
        public override void Dispose()
        {
            GC.SuppressFinalize(this);
            if (messageTexture != null)
            {
                messageTexture.Dispose();
            }
            messageTexture = null;
            base.Dispose();
        }
        #endregion

        #region Properties
        /// <summary>
        /// Return the mesh that represents loopy
        /// </summary>
        public Mesh LoopyMesh
        {
            get { return loopyMesh; }
        }
        /// <summary>
        /// Return the texture that represents loopy's
        /// color
        /// </summary>
        public Texture LoopyTexture
        {
            get { return loopyTexture; }
        }
        /// <summary>
        /// Return loopy's material
        /// </summary>
        public Material LoopyMaterial
        {
            get { return loopyMaterial; }
        }
        #endregion

        #region Button Click Event Handlers
        /// <summary>
        /// Fired when the left button is clicked
        /// </summary>
        private void OnLeftButton(object sender, EventArgs e)
        {
            loopyColor--;
            if (loopyColor < PlayerColor.Min)
                loopyColor = PlayerColor.Max;

            // Create the loop texture
            CreateTexture(storedDevice);
        }

        /// <summary>
        /// Fired when the right button is clicked
        /// </summary>
        private void OnRightButton(object sender, EventArgs e)
        {
            loopyColor++;
            if (loopyColor > PlayerColor.Max)
                loopyColor = PlayerColor.Min;

            // Create the loop texture
            CreateTexture(storedDevice);
        }

        /// <summary>
        /// Fired when the ok button is clicked
        /// </summary>
        private void OnSelectButton(object sender, EventArgs e)
        {
            if (Selected != null)
                Selected(this, e);
        }
        #endregion

        #region Winform Events
        /// <summary>
        /// Update the buttons if the mouse is over it
        /// </summary>
        public void OnMouseMove(int x, int y)
        {
            leftButton.OnMouseMove(x, y);
            rightButton.OnMouseMove(x, y);
            okButton.OnMouseMove(x, y);
        }
        /// <summary>
        /// See if the user clicked the buttons
        /// </summary>
        public void OnMouseClick(int x, int y)
        {
            leftButton.OnMouseClick(x, y);
            rightButton.OnMouseClick(x, y);
            okButton.OnMouseClick(x, y);
        }
        /// <summary>
        /// Called when a key is pressed
        /// </summary>
        public void OnKeyPress(System.Windows.Forms.Keys key)
        {
            switch(key)
            {
                case Keys.Enter:
                    // Enter is the same as selecting loopy
                    OnSelectButton(this, EventArgs.Empty);
                    break;
                case Keys.Left:
                    // Same as pressing the left arrow
                    OnLeftButton(this, EventArgs.Empty);
                    break;
                case Keys.Right:
                    // Same as pressing the left arrow
                    OnRightButton(this, EventArgs.Empty);
                    break;
            }
        }
        #endregion
    }
    /// <summary>
    /// The screen to confirm quiting
    /// </summary>
    public class QuitScreen : UiScreen, IDisposable
    {
        #region Instance Data
        private Texture buttonTextures = null;
        private Texture messageTexture = null;
        private UiButton yesButton = null;
        private UiButton noButton = null;

        // Events
        public event EventHandler QuitGame;
        public event EventHandler Cancel;
        #endregion

        #region Creation
        /// <summary>
        /// Create the quit screen
        /// </summary>
        public QuitScreen(Device device, Texture buttons, int width, int height) 
            : base(device, width, height)
        {
            // Create the textures for the buttons
            buttonTextures = buttons;

            // Create the texture for the background
            messageTexture = TextureLoader.FromFile(device, GameEngine.MediaPath + "QuitConfirm.png");
            StoreTexture(messageTexture, width, height, true);

            // Calculate the spacing for the buttons
            float horizSpacing = (float)(width - (SmallButtonWidth * 3))
                / 2.0f;

            // Create the yes/no buttons
            yesButton = new UiButton(renderSprite, buttonTextures, buttonTextures,
                new Rectangle(SmallButtonWidth,SmallButtonHeight * 3,
                SmallButtonWidth,SmallButtonHeight), 
                new Rectangle(0,SmallButtonHeight * 3,
                SmallButtonWidth,SmallButtonHeight), 
                new Point((int)horizSpacing,
                (int)(centerUpper.Y + backgroundSource.Height)
                - (SmallButtonHeight * 2)));

            yesButton.Click += new EventHandler(OnYesButton);

            noButton = new UiButton(renderSprite, buttonTextures, buttonTextures,
                new Rectangle(SmallButtonWidth,SmallButtonHeight * 2,
                SmallButtonWidth,SmallButtonHeight), 
                new Rectangle(0,SmallButtonHeight * 2,
                SmallButtonWidth,SmallButtonHeight), 
                new Point(width - ((int)horizSpacing + SmallButtonWidth),
                (int)(centerUpper.Y + backgroundSource.Height)
                - (SmallButtonHeight * 2)));

            noButton.Click += new EventHandler(OnNoButton);
        }

        #endregion

        #region Rendering
        /// <summary>
        /// Render the buttons
        /// </summary>
        public override void Draw()
        {
            // Start rendering sprites
            base.BeginSprite();
            // Draw the background
            base.Draw();
            // Now the buttons
            yesButton.Draw();
            noButton.Draw();

            // You're done rendering sprites
            base.EndSprite();
        }

        #endregion

        #region IDisposable Members
        /// <summary>
        /// Cleanup in case dispose isn't called
        /// </summary>
        ~QuitScreen()
        {
            Dispose();
        }
        /// <summary>
        /// Cleanup any resources
        /// </summary>
        public override void Dispose()
        {
            GC.SuppressFinalize(this);
            if (messageTexture != null)
            {
                messageTexture.Dispose();
            }
            messageTexture = null;
            buttonTextures = null;
            base.Dispose();
        }
        #endregion

        #region Button Click Event Handlers
        /// <summary>
        /// Fired when the yes button is clicked
        /// </summary>
        private void OnYesButton(object sender, EventArgs e)
        {
            if (QuitGame != null)
                QuitGame(this, EventArgs.Empty);
        }

        /// <summary>
        /// Fired when the no button is clicked
        /// </summary>
        private void OnNoButton(object sender, EventArgs e)
        {
            if (Cancel != null)
                Cancel(this, EventArgs.Empty);
        }
        #endregion

        #region Winform Events
        /// <summary>
        /// Update the buttons if the mouse is over it
        /// </summary>
        public void OnMouseMove(int x, int y)
        {
            yesButton.OnMouseMove(x, y);
            noButton.OnMouseMove(x, y);
        }
        /// <summary>
        /// See if the user clicked the buttons
        /// </summary>
        public void OnMouseClick(int x, int y)
        {
            yesButton.OnMouseClick(x, y);
            noButton.OnMouseClick(x, y);
        }
        /// <summary>
        /// Called when a key is pressed
        /// </summary>
        public void OnKeyPress(System.Windows.Forms.Keys key)
        {
            switch(key)
            {
                case Keys.Escape:
                case Keys.N:
                    // Escape will dismiss the dialog
                    OnNoButton(this, EventArgs.Empty);
                    break;
                case Keys.Enter:
                case Keys.Y:
                    // Same as pressing the Yes Button
                    OnYesButton(this, EventArgs.Empty);
                    break;
            }
        }
        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -