ImageControl.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //-----------------------------------------------------------------------------
  2. // ImageControl.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. namespace UserInterfaceSample.Controls
  10. {
  11. /// <summary>
  12. /// ImageControl is a control that displays a single sprite. By default it displays an entire texture.
  13. ///
  14. /// If a null texture is given, this control will use DrawContext.BlankTexture. This allows it to be
  15. /// used to draw solid-colored rectangles.
  16. /// </summary>
  17. public class ImageControl : Control
  18. {
  19. private Texture2D texture;
  20. // Position within the source texture, in texels. Default is (0,0) for the upper-left corner.
  21. public Vector2 origin;
  22. // Size in texels of source rectangle. If null (the default), size will be the same as the size of the control.
  23. // You only need to set this property if you want texels scaled at some other size than 1-to-1; normally
  24. // you can just set the size of both the source and destination rectangles with the Size property.
  25. public Vector2? SourceSize;
  26. // Color to modulate the texture with. The default is white, which displays the original unmodified texture.
  27. public Color Color;
  28. // Texture to draw
  29. public Texture2D Texture
  30. {
  31. get { return texture; }
  32. set
  33. {
  34. if (texture != value)
  35. {
  36. texture = value;
  37. InvalidateAutoSize();
  38. }
  39. }
  40. }
  41. public ImageControl() : this(null, Vector2.Zero)
  42. {
  43. }
  44. public ImageControl(Texture2D texture, Vector2 position)
  45. {
  46. this.texture = texture;
  47. this.Position = position;
  48. this.Color = Color.White;
  49. }
  50. public override void Draw(DrawContext context)
  51. {
  52. base.Draw(context);
  53. Texture2D drawTexture = texture ?? context.BlankTexture;
  54. Vector2 actualSourceSize = SourceSize ?? Size;
  55. Rectangle sourceRectangle = new Rectangle
  56. {
  57. X = (int)origin.X,
  58. Y = (int)origin.Y,
  59. Width = (int)actualSourceSize.X,
  60. Height = (int)actualSourceSize.Y,
  61. };
  62. Rectangle destRectangle = new Rectangle
  63. {
  64. X = (int)context.DrawOffset.X,
  65. Y = (int)context.DrawOffset.Y,
  66. Width = (int)Size.X,
  67. Height = (int)Size.Y
  68. };
  69. context.SpriteBatch.Draw(drawTexture, destRectangle, sourceRectangle, Color);
  70. }
  71. override public Vector2 ComputeSize()
  72. {
  73. if(texture!=null)
  74. {
  75. return new Vector2(texture.Width, texture.Height);
  76. }
  77. return Vector2.Zero;
  78. }
  79. }
  80. }