Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Trying to efficiently make windows in XNA

$
0
0
Hi there!

I'm working on a game and I've just made a little prototype window which I'd like a little feedback on.
It functions wonderfully but I'm not too convinced on how I'm going to manipulate this to work for multiple windows; ie, one window for an inventory which features a list of items, and the other for a chat log and so on.

Here's a little video to show how it functions.


using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

namespace Alpha_Venture
{
    class Window
    {
        private Vector2 mouseLockPosition;
        private Rectangle OldPosition;
        private bool Resize = true;
        private bool Move = false;

        private bool _Draggable;
        public bool Draggable
        {
            set { this._Draggable = value; }
            get { return this._Draggable; }
        }
        private bool _Resizable;
        public bool Resizble
        {
            set { this._Resizable = value; }
            get { return this._Resizable; }
        }
        private string _Title;
        public string Title
        {
            set { this._Title = value; }
            get { return this._Title; }
        }

        private Rectangle _Position;
        public Rectangle Position
        {
            set { this._Position = value; }
            get {return this._Position; }
        }

        private Color _Color;
        public Color color
        {
            set { this._Color = value; }
            get { return this._Color; }
        }

        private ContentManager Content;
        private Texture2D tx_Window;
        private SpriteFont font;

        public Window(Rectangle Position, ContentManager cm, string Title)
        {
            this._Position = Position;
            this.Content = cm;
            this.color = new Color(255, 255, 255);
            this.Title = Title;
            tx_Window = Content.Load<Texture2D>("ui_wnd");
            font = Content.Load<SpriteFont>("tiny");
        }

        public void Update()
        {
            MouseState mouseState;
            mouseState = Mouse.GetState();
            Rectangle mouseRect = new Rectangle(mouseState.X, mouseState.Y, 1, 1);

            ///<resize>
            if (mouseState.LeftButton == ButtonState.Pressed && Resize && mouseRect.Intersects(new Rectangle(Position.X + Position.Width - 10, Position.Y + Position.Height - 10, 10, 9))) 
            { 
                Resize = false;
                OldPosition = Position;
                mouseLockPosition = new Vector2(mouseState.X, mouseState.Y);
            }
            ///<move>
            if (mouseState.LeftButton == ButtonState.Pressed && !Move && Resize && mouseRect.Intersects(new Rectangle(Position.X, Position.Y, Position.Width, 17)))
            {
                Move = true;
                OldPosition = Position;
                mouseLockPosition = new Vector2(mouseState.X, mouseState.Y); 
            }

            if (Move)
            {
                _Position.X = mouseState.X - (int)mouseLockPosition.X + OldPosition.X;
                _Position.Y = mouseState.Y - (int)mouseLockPosition.Y + OldPosition.Y;
            }

            if (!Resize)
            {
                _Position.Width = mouseState.X - (int)mouseLockPosition.X + OldPosition.Width;
                _Position.Height = mouseState.Y - (int)mouseLockPosition.Y + OldPosition.Height;

                if (_Position.Width < font.MeasureString(Title).X * 2) _Position.Width = (int)font.MeasureString(Title).X * 2;
                if (_Position.Height < font.MeasureString(Title).Y * 2) _Position.Height = (int)font.MeasureString(Title).Y * 2;
            }

            ///<unlink>
            if (mouseState.LeftButton == ButtonState.Released) { Resize = true; Move = false; }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            ///<window background color>
            spriteBatch.Draw(tx_Window, new Rectangle(Position.X, Position.Y, Position.Width, Position.Height), new Rectangle(12, 12, 2, 2), color);
            ///<top left>
            spriteBatch.Draw(tx_Window, new Vector2(Position.X, Position.Y), new Rectangle(0, 0, 10, 10), color);
            ///<top right>
            spriteBatch.Draw(tx_Window, new Vector2(Position.X + Position.Width-10, Position.Y), new Rectangle(0, 0, 10, 10), color, 0f, Vector2.Zero, 1f, SpriteEffects.FlipHorizontally, 0f);
            ///<left/right>>
            for (int i = 0; i < (Position.Height - 20) / 5 +1; i++) {
                    spriteBatch.Draw(tx_Window, new Vector2(Position.X, Position.Y + 10 + (i * 5)), new Rectangle(0, 11, 10, 5), color);
                    spriteBatch.Draw(tx_Window, new Vector2(Position.X + Position.Width - 10, Position.Y + 10 + (i * 5)), new Rectangle(0, 11, 10, 5), color, 0f, Vector2.Zero, 1f, SpriteEffects.FlipHorizontally, 0f);
            }
            ///<top/bottom>
            for (int i = 0; i < (Position.Width - 17) / 4; i++)
            {
                spriteBatch.Draw(tx_Window, new Vector2(Position.X + 10 + (i * 4), Position.Y), new Rectangle(11, 0, 4, 10), color);
                spriteBatch.Draw(tx_Window, new Vector2(Position.X + 10 + (i * 4), Position.Y + Position.Height - 10), new Rectangle(11, 0, 4, 10), color, 0f, Vector2.Zero, 1f, SpriteEffects.FlipVertically, 0f);                
            }
            ///<bottom left>
            spriteBatch.Draw(tx_Window, new Vector2(Position.X, Position.Y + Position.Height - 10), new Rectangle(0, 17, 10, 9), color);
            ///<bottom right>
            spriteBatch.Draw(tx_Window, new Vector2(Position.X + Position.Width-10, Position.Y + Position.Height - 10), new Rectangle(16, 17, 10, 9), color);
            ///<bar under title>
            spriteBatch.Draw(tx_Window, new Rectangle(Position.X +2, Position.Y + 17, Position.Width-4, 10), new Rectangle(11, 0, 4, 10), color);
            ///<write title>
            spriteBatch.DrawString(font, this.Title, new Vector2(Position.X + Position.Width/2, Position.Y + 11), Color.Black, 0f, font.MeasureString(this.Title)/2, 1f, SpriteEffects.None, 0f);
        }
    }
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>