Okay, I have a few questions about auto drawing a Texture2D, to be used in an 2D Windows Game I'm making for the fun of it.
But first, a few things,
1: The code is in my own custom .dll file that I'm making for this.
2: I've taught myself everything I know about C#, so any comment about my coding style isn't important to me, but is appreciated anyway.
3: And finally, a lot of code:
The following is a custom class that was used in the previous code:
HOW TO USE THE ABOVE CODE:
And finally, the questions:
1: How do I get rid of the use of the System.Drawing and System.Drawing. Drawing2D namespaces and replace them with something in the XNA framework?
2: For the rounded style, how would I make the progress bar look proper when the percentage gets increased? Is there a way to erase the outside of a GraphicsPath? Drawing on the outside won't help, it actually needs to be erased. (From what I'm aware of atleast.)
3: Is there a way to make it so I don't have to change the BackgroundPath depending on whether or not there is supposed to be a border?
4: The reason the GetTexture is private is because I can't seem to make it get stored, so every time the draw function is called, it gets redrawn, but I'd like to be able to store the texture, and change it when the percentage value changes, so that way it doesn't have to keep redrawing.
5: How would I go about making the Background Path into a BoundingBox to detect collisions?
6: If you think I should be doing something completely different, please let me know.
And that is all.
Any help is appreciated.
But first, a few things,
1: The code is in my own custom .dll file that I'm making for this.
2: I've taught myself everything I know about C#, so any comment about my coding style isn't important to me, but is appreciated anyway.
3: And finally, a lot of code:
Code:
public class ProgressBar
{
#region Overrides
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public override bool Equals(object obj) { return base.Equals(obj); }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public override int GetHashCode() { return base.GetHashCode(); }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public override string ToString() { return base.ToString(); }
#endregion
#region Texture Graphics Device & SpriteBatch
private Microsoft.Xna.Framework.Graphics.GraphicsDevice PGraphicsDevice;
public Microsoft.Xna.Framework.Graphics.GraphicsDevice GraphicsDevice
{
get { return this.PGraphicsDevice; }
set { this.PGraphicsDevice = value; }
}
private Microsoft.Xna.Framework.Graphics.SpriteBatch PSpriteBatch;
public Microsoft.Xna.Framework.Graphics.SpriteBatch SpriteBatch
{
get { return this.PSpriteBatch; }
set { this.PSpriteBatch = value; }
}
#endregion
public ProgressBar(Microsoft.Xna.Framework.Graphics.GraphicsDevice GraphicsDevice, Microsoft.Xna.Framework.Graphics.SpriteBatch SpriteBatch)
{
this.PGraphicsDevice = GraphicsDevice;
this.PSpriteBatch = SpriteBatch;
this.PBackColor = new Microsoft.Xna.Framework.Color(210, 210, 210, 255);
this.HighlightColor = new Microsoft.Xna.Framework.Color(255, 255, 255, 170);
this.ForeColor = new Microsoft.Xna.Framework.Color(0, 255, 0, 255);
}
#region Texture Drawing
/// <summary>
/// Draws the texture at the specified location.
/// </summary>
public void Draw()
{
SpriteBatch.Begin();
SpriteBatch.Draw(this.GetTexture, this.Location, Microsoft.Xna.Framework.Color.White);
SpriteBatch.End();
}
private Microsoft.Xna.Framework.Graphics.Texture2D GetTexture
{
get
{
System.Drawing.Bitmap Texture2DBitmap = new System.Drawing.Bitmap(this.Width, this.Height);
int BufferSize = Texture2DBitmap.Height * Texture2DBitmap.Width * 4;
System.Drawing.Graphics GraphicsImage = System.Drawing.Graphics.FromImage(Texture2DBitmap);
GraphicsImage.FillPath(new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(this.BackColor.A, this.BackColor.R, this.BackColor.G, this.BackColor.B)), this.BackgroundPath);
GraphicsImage.FillPath(new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(this.ForeColor.A, this.ForeColor.R, this.ForeColor.G, this.ForeColor.B)), this.PercentagePath);
if (this.Highlight) GraphicsImage.FillPath(new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(this.HighlightColor.A, this.HighlightColor.R, this.HighlightColor.G, this.HighlightColor.B)), this.HighlightPath);
if (this.Border) GraphicsImage.DrawPath(new System.Drawing.Pen(System.Drawing.Color.FromArgb(this.BorderColor.A,this.BorderColor.R,this.BorderColor.G,this.BorderColor.B), 1), this.BackgroundPath);
GraphicsImage.Dispose();
System.IO.MemoryStream MemoryStream = new System.IO.MemoryStream(BufferSize);
Texture2DBitmap.Save(MemoryStream, System.Drawing.Imaging.ImageFormat.Png);
return Microsoft.Xna.Framework.Graphics.Texture2D.FromStream(GraphicsDevice, MemoryStream);
}
}
private System.Drawing.Drawing2D.GraphicsPath BackgroundPath
{
get
{
System.Drawing.Drawing2D.GraphicsPath Path = new System.Drawing.Drawing2D.GraphicsPath();
if (this.Style == Styles.Sqaured)
{
if (this.Border)
{
Path.AddLine(0, 0, this.WidthM1, 0);
Path.AddLine(this.WidthM1, 0, this.WidthM1, this.HeightM1);
Path.AddLine(this.WidthM1, this.HeightM1, 0, this.HeightM1);
Path.AddLine(0, this.HeightM1, 0, 0);
}
else
{
Path.AddLine(0, 0, this.Width, 0);
Path.AddLine(this.Width, 0, this.Width, this.Height);
Path.AddLine(this.Width, this.Height, 0, this.Height);
Path.AddLine(0, this.Height, 0, 0);
}
}
else if (this.Style == Styles.Rounded)
{
if (this.Border)
{
Path.AddArc(0, 0, this.HeightM1, this.HeightM1, 90, 180);
Path.AddLine(this.HeightD2, 0, this.WidthM1 - this.HeightD2, 0);
Path.AddArc(this.WidthM1 - this.HeightM1, 0, this.HeightM1, this.HeightM1, -90, 180);
Path.AddLine(this.WidthM1 - this.HeightD2, this.HeightM1, this.HeightD2, this.HeightM1);
}
else
{
Path.AddArc(-1, 0, this.Height, this.Height, 90, 180);
Path.AddLine(this.HeightD2, 0, this.Width - this.HeightD2, 0);
Path.AddArc(this.Width - this.Height, 0, this.Height, this.Height, -90, 180);
Path.AddLine(this.Width - this.HeightD2, this.Height, this.HeightD2, this.Height);
}
}
return Path;
}
}
private System.Drawing.Drawing2D.GraphicsPath PercentagePath
{
get
{
System.Drawing.Drawing2D.GraphicsPath Path = new System.Drawing.Drawing2D.GraphicsPath();
if (Style == Styles.Sqaured)
{
Path.AddLine(0, 0, this.Width * this.PercentageD100, 0);
Path.AddLine(this.Width * this.PercentageD100, 0, this.Width * this.PercentageD100, this.Height);
Path.AddLine(this.Width * this.PercentageD100, this.Height, 0, this.Height);
Path.AddLine(0, this.Height, 0, 0);
}
else if (Style == Styles.Rounded)
{
Path.AddLine(0, 0, this.Width * this.PercentageD100, 0);
Path.AddLine(this.Width * this.PercentageD100, 0, this.Width * this.PercentageD100, this.Height);
Path.AddLine(this.Width * this.PercentageD100, this.Height, 0, this.Height);
Path.AddLine(0, this.Height, 0, 0);
}
return Path;
}
}
private System.Drawing.Drawing2D.GraphicsPath HighlightPath
{
get
{
System.Drawing.Drawing2D.GraphicsPath Path = new System.Drawing.Drawing2D.GraphicsPath();
if (this.Style == Styles.Sqaured)
{
Path.AddLine(0, 0, this.Width, 0);
Path.AddLine(this.Width, 0, this.Width, this.HeightD2);
Path.AddLine(this.Width, this.HeightD2, 0, this.HeightD2);
Path.AddLine(0, this.HeightD2, 0, 0);
}
else if (this.Style == Styles.Rounded)
{
Path.AddArc(-1, 0, this.Height, this.Height, 180, 90);
Path.AddLine(this.HeightD2, 0, this.Width - this.HeightD2, 0);
Path.AddArc(this.Width - this.Height, 0, this.Height, this.Height, -90, 90);
Path.AddLine(this.Width, this.HeightD2, 0, this.HeightD2);
}
return Path;
}
}
#endregion
#region Texture Location
private Microsoft.Xna.Framework.Vector2 PLocation;
/// <summary>
/// Gets or sets the location of the texture.
/// </summary>
public Microsoft.Xna.Framework.Vector2 Location
{
get { return this.PLocation; }
set { this.PLocation = value; }
}
/// <summary>
/// Gets or sets the center point of the texture.
/// </summary>
public Microsoft.Xna.Framework.Vector2 Center
{
get { return new Microsoft.Xna.Framework.Vector2(this.PLocation.X + (this.Width / 2), this.PLocation.Y + (this.Height / 2)); }
set { this.Location = new Microsoft.Xna.Framework.Vector2(value.X - (this.Width / 2), value.Y - (this.Height / 2)); }
}
#endregion
#region Texture Size
private Size PSize;
/// <summary>
/// Gets or sets the size of the texture.
/// </summary>
public Size Size
{
get
{
if (this.PSize != null && this.PSize.Width > 0 && this.PSize.Height > 0) return this.PSize;
else return new Size(200, 20);
}
set
{
if (value.Width > 0 && value.Height > 0) this.PSize = value;
}
}
/// <summary>
/// Gets or sets the width of the texture.
/// </summary>
public int Width
{
get { return this.Size.Width; }
set { this.PSize.Width = value; }
}
private int WidthM1 { get { return this.Width - 1; } }
/// <summary>
/// Gets or sets the height of the texture.
/// </summary>
public int Height
{
get { return this.Size.Height; }
set { this.PSize.Height = value; }
}
private int HeightM1 { get { return this.Height - 1; } }
private int HeightD2 { get { return this.Height / 2; } }
#endregion
#region Texture Styles
/// <summary>
/// A list of different styles that the texture can look like.
/// </summary>
public enum Styles : int
{
/// <summary>
/// The texture will be displayed as a square the exact size specified.
/// </summary>
Sqaured = 0,
/// <summary>
/// The texture will be displayed as a square with rounded ends.
/// </summary>
Rounded = 1,
}
private Styles PStyle;
/// <summary>
/// Gets or sets the style of the texture.
/// </summary>
public Styles Style
{
get { return this.PStyle; }
set { this.PStyle = value; }
}
#endregion
#region Texture Colors
private Microsoft.Xna.Framework.Color PBackColor;
/// <summary>
/// Gets or sets the back color of the texture.
/// </summary>
public Microsoft.Xna.Framework.Color BackColor
{
get { return this.PBackColor; }
set { this.PBackColor = value; }
}
private Microsoft.Xna.Framework.Color PForeColor;
/// <summary>
/// Gets or sets the fore color of the texture.
/// </summary>
public Microsoft.Xna.Framework.Color ForeColor
{
get { return this.PForeColor; }
set { this.PForeColor = value; }
}
private bool PHighlight;
/// <summary>
/// Gets or sets if a highlight should be displayed on the top half of the texture.
/// </summary>
public bool Highlight
{
get { return this.PHighlight; }
set { this.PHighlight = value; }
}
private Microsoft.Xna.Framework.Color PHighlightColor;
/// <summary>
/// Gets or sets the highlight color of the texture
/// </summary>
public Microsoft.Xna.Framework.Color HighlightColor
{
get { return this.PHighlightColor; }
set { this.PHighlightColor = value; }
}
private bool PBorder;
/// <summary>
/// Gets or sets if a border should be displayed around the texture.
/// </summary>
public bool Border
{
get { return this.PBorder; }
set
{
this.PBorder = value;
if (this.Border)
{
if (this.Size.Width < 3) this.PSize.Width = 3;
if (this.Size.Height < 3) this.PSize.Height = 3;
}
}
}
private Microsoft.Xna.Framework.Color PBorderColor;
/// <summary>
/// Gets or sets the border color of the texture
/// </summary>
public Microsoft.Xna.Framework.Color BorderColor
{
get { return this.PBorderColor; }
set { this.PBorderColor = value; }
}
#endregion
#region Texture Percentage
private float PPercentageD100;
private float PercentageD100 { get { return this.PPercentageD100; } }
/// <summary>
/// Gets or sets the percentage value of the texture.
/// </summary>
public float Percentage
{
get { return this.PPercentageD100 * 100; }
set
{
if (value >= 0 && value <= 100) this.PPercentageD100 = value / 100;
else if (value < 0) this.PPercentageD100 = 0;
else if (value > 100) this.PPercentageD100 = 1;
}
}
#endregion
}
The following is a custom class that was used in the previous code:
Code:
public class Size
{
#region Overrides
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public override bool Equals(object obj) { return base.Equals(obj); }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public override int GetHashCode() { return base.GetHashCode(); }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public override string ToString() { return base.ToString(); }
#endregion
private int PWidth;
public int Width
{
get { return this.PWidth; }
set { this.PWidth = value; }
}
private int PHeight;
public int Height
{
get { return this.PHeight; }
set { this.PHeight = value; }
}
public Size(int Width, int Height)
{
this.Width = Width;
this.Height = Height;
}
}
HOW TO USE THE ABOVE CODE:
Code:
public class Game1 : Microsoft.Xna.Framework.Game
{
private Microsoft.Xna.Framework.GraphicsDeviceManager GraphicsDeviceManager;
private Microsoft.Xna.Framework.Graphics.SpriteBatch SpriteBatch;
private ProgressBar ProgressBar1;
public Game1()
{
this.GraphicsDeviceManager = new Microsoft.Xna.Framework.GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
this.SpriteBatch = new Microsoft.Xna.Framework.Graphics.SpriteBatch(this.GraphicsDevice);
this.ProgressBar1 = new ProgressBar(this.GraphicsDevice, SpriteBatch);
this.ProgressBar1.Size = new Size(30, 30);
//this.ProgressBar1.Location = new Microsoft.Xna.Framework.Vector2(20, 10);
this.ProgressBar1.Center = new Microsoft.Xna.Framework.Vector2(275, 15);
this.ProgressBar1.Style = ProgressBar.Styles.Rounded;
this.ProgressBar1.BackColor = new Microsoft.Xna.Framework.Color(210, 210, 210, 255);
this.ProgressBar1.ForeColor = new Microsoft.Xna.Framework.Color(0, 255, 0, 255);
this.ProgressBar1.Highlight = true;
this.ProgressBar1.HighlightColor = new Microsoft.Xna.Framework.Color(255, 255, 255, 170);
this.ProgressBar1.Border = true;
this.ProgressBar1.BorderColor = new Microsoft.Xna.Framework.Color(255, 0, 0, 100);
}
protected override void LoadContent()
{
}
protected override void UnloadContent()
{
}
protected override void Update(Microsoft.Xna.Framework.GameTime GameTime)
{
if (Microsoft.Xna.Framework.Input.Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Left)) this.ProgressBar1.Percentage -= 1;
if (Microsoft.Xna.Framework.Input.Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Right)) this.ProgressBar1.Percentage += 1;
base.Update(GameTime);
}
protected override void Draw(Microsoft.Xna.Framework.GameTime GameTime)
{
GraphicsDevice.Clear(new Microsoft.Xna.Framework.Color(0, 0, 255, 0));
this.ProgressBar1.Draw();
base.Draw(GameTime);
}
}
And finally, the questions:
1: How do I get rid of the use of the System.Drawing and System.Drawing. Drawing2D namespaces and replace them with something in the XNA framework?
2: For the rounded style, how would I make the progress bar look proper when the percentage gets increased? Is there a way to erase the outside of a GraphicsPath? Drawing on the outside won't help, it actually needs to be erased. (From what I'm aware of atleast.)
3: Is there a way to make it so I don't have to change the BackgroundPath depending on whether or not there is supposed to be a border?
4: The reason the GetTexture is private is because I can't seem to make it get stored, so every time the draw function is called, it gets redrawn, but I'd like to be able to store the texture, and change it when the percentage value changes, so that way it doesn't have to keep redrawing.
5: How would I go about making the Background Path into a BoundingBox to detect collisions?
6: If you think I should be doing something completely different, please let me know.
And that is all.
Any help is appreciated.
Comment