1 using OpenBveApi.Colors;
2 using OpenBveApi.Math;
3 using OpenBveApi.Textures;
4 using OpenTK.Graphics.OpenGL;
5 
6 namespace LibRender2.Primitives
7 {
8 	public class Picturebox
9 	{
10 		/// <summary>Holds a reference to the base renderer</summary>
11 		private readonly BaseRenderer Renderer;
12 		/// <summary>The texture for the picturebox</summary>
13 		public Texture Texture;
14 		/// <summary>The background color for the picturebox</summary>
15 		public Color128 BackgroundColor;
16 		/// <summary>The image sizing mode</summary>
17 		public ImageSizeMode SizeMode;
18 		/// <summary>The stored location for the textbox</summary>
19 		public Vector2 Location;
20 		/// <summary>The stored size for the textbox</summary>
21 		public Vector2 Size;
22 
23 
Picturebox(BaseRenderer renderer)24 		public Picturebox(BaseRenderer renderer)
25 		{
26 			Renderer = renderer;
27 			SizeMode = ImageSizeMode.Zoom;
28 		}
29 
Draw()30 		public void Draw()
31 		{
32 			if (!Renderer.currentHost.LoadTexture(ref Texture, OpenGlTextureWrapMode.ClampClamp))
33 			{
34 				return;
35 			}
36 
37 			GL.DepthMask(true);
38 			Vector2 newSize;
39 			switch (SizeMode)
40 			{
41 				case ImageSizeMode.Normal:
42 					//Draw box containing backing color first
43 					Renderer.Rectangle.Draw(Texture, Location, Size, BackgroundColor);
44 					//Calculate the new size
45 					newSize = new Vector2(Texture.Width, Texture.Height);
46 					if (newSize.X > Size.X)
47 					{
48 						newSize.X = Size.X;
49 					}
50 
51 					if (newSize.Y > Size.Y)
52 					{
53 						newSize.Y = Size.Y;
54 					}
55 					//Two-pass draw the texture in appropriate place
56 					Renderer.Rectangle.DrawAlpha(Texture, Location, newSize, Color128.White, new Vector2(newSize / Size));
57 					break;
58 				case ImageSizeMode.Center:
59 					//Draw box containing backing color first
60 					Renderer.Rectangle.Draw(Texture, Location, Size, BackgroundColor);
61 					//Calculate the new size
62 					newSize = new Vector2(Texture.Width, Texture.Height);
63 					if (newSize.X > Size.X)
64 					{
65 						newSize.X = Size.X;
66 					}
67 
68 					if (newSize.Y > Size.Y)
69 					{
70 						newSize.Y = Size.Y;
71 					}
72 					//Two-pass draw the texture in appropriate place
73 					Renderer.Rectangle.DrawAlpha(Texture, Location + new Vector2(newSize - Size) / 2, newSize, Color128.White, new Vector2(newSize / Size));
74 					break;
75 				case ImageSizeMode.Stretch:
76 					//No neeed to draw a backing color box as texture covers the whole thing
77 					Renderer.Rectangle.Draw(Texture, Location, Size, BackgroundColor);
78 					break;
79 				case ImageSizeMode.Zoom:
80 					//Draw box containing backing color first
81 					Renderer.Rectangle.Draw(null, Location, Size, BackgroundColor);
82 					//Calculate the new size
83 					double ratioW = Size.X / Texture.Width;
84 					double ratioH = Size.Y / Texture.Height;
85 					double newRatio = ratioW < ratioH ? ratioW : ratioH;
86 					newSize = new Vector2(Texture.Width, Texture.Height) * newRatio;
87 					Renderer.Rectangle.DrawAlpha(Texture, new Vector2(Location.X + (Size.X - newSize.X) / 2,Location.Y + (Size.Y - newSize.Y) / 2), newSize, Color128.White);
88 					break;
89 			}
90 
91 		}
92 
93 	}
94 }
95