1 Replies - 4350 Views - Last Post: 28 December 2012 - 03:55 PM

#1 Mallaboro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 26-April 10

Trying to efficiently make windows in XNA

Posted 27 December 2012 - 09:26 AM

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);
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Trying to efficiently make windows in XNA

#2 AdoTheLimey  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 80
  • Joined: 28-June 10

Re: Trying to efficiently make windows in XNA

Posted 28 December 2012 - 03:55 PM

I watched your video, but I have not looked deeply at the code. The window seems functional enough - nice work :) What are you unsure about? How to give the windows different behaviours? If so, my recommendation would be to create classes that inherit from your window class. This way you can add window specific methods (such as displaying text, or a list of images for an inventory) and keep the functionality you have already built that allows you to move and interact with the window frames.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1