// This updates the text if needed public void Update(float translatex, float translatey, float scalex, float scaley) { // Check if transformation changed and needs to be updated if (transformcoords && (translatex != lasttranslatex || translatey != lasttranslatey || scalex != lastscalex || scaley != lastscaley)) { lasttranslatex = translatex; //mxd lasttranslatey = translatey; //mxd lastscalex = scalex; //mxd lastscaley = scaley; //mxd updateneeded = true; } // Update if needed if (updateneeded || textureupdateneeded) { // Only build when there are any vertices if (text.Length > 0) { // Transform? Vector2D abspos = (transformcoords ? location.GetTransformed(translatex, translatey, scalex, scaley) : location); // Update text and texture sizes if (textsize.IsEmpty || texturesize.IsEmpty) { textorigin = new PointF(4, 3); textrect = new RectangleF(textorigin, General.Interface.MeasureString(text, font)); textrect.Width = (float)Math.Round(textrect.Width); textrect.Height = (float)Math.Round(textrect.Height); bgrect = new RectangleF(0, 0, textrect.Width + textorigin.X * 2, textrect.Height + textorigin.Y * 2); // Store calculated text size... textsize = new SizeF(textrect.Width + textorigin.X * 2, textrect.Height + textorigin.Y * 2); // Make PO2 image, for speed and giggles... texturesize = new Size(General.NextPowerOf2((int)textsize.Width), General.NextPowerOf2((int)textsize.Height)); switch (alignx) { case TextAlignmentX.Center: bgrect.X = (texturesize.Width - bgrect.Width) / 2; break; case TextAlignmentX.Right: bgrect.X = texturesize.Width - bgrect.Width; break; } switch (aligny) { case TextAlignmentY.Middle: bgrect.Y = (texturesize.Height - bgrect.Height) / 2; break; case TextAlignmentY.Bottom: bgrect.Y = texturesize.Height - bgrect.Height; break; } textrect.X += bgrect.X; textrect.Y += bgrect.Y; } // Align the text horizontally float beginx = 0; switch (alignx) { case TextAlignmentX.Left: beginx = abspos.x; break; case TextAlignmentX.Center: beginx = abspos.x - texturesize.Width * 0.5f; break; case TextAlignmentX.Right: beginx = abspos.x - texturesize.Width; break; } // Align the text vertically float beginy = 0; switch (aligny) { case TextAlignmentY.Top: beginy = abspos.y; break; case TextAlignmentY.Middle: beginy = abspos.y - texturesize.Height * 0.5f; break; case TextAlignmentY.Bottom: beginy = abspos.y - texturesize.Height; break; } //mxd. Skip when not on screen... RectangleF abssize = new RectangleF(beginx, beginy, texturesize.Width, texturesize.Height); Size windowsize = General.Map.Graphics.RenderTarget.ClientSize; skiprendering = (abssize.Right < 0.1f) || (abssize.Left > windowsize.Width) || (abssize.Bottom < 0.1f) || (abssize.Top > windowsize.Height); if (skiprendering) { return; } //mxd. Update texture if needed if (textureupdateneeded) { // Get rid of old texture if (texture != null) { texture.Dispose(); texture = null; } // Create label image Bitmap img = CreateLabelImage(text, font, color, backcolor, drawbg, textrect, bgrect, texturesize, textorigin); //texturesize = img.Size; // Create texture MemoryStream memstream = new MemoryStream((img.Size.Width * img.Size.Height * 4) + 4096); img.Save(memstream, ImageFormat.Bmp); memstream.Seek(0, SeekOrigin.Begin); texture = Texture.FromStream(General.Map.Graphics.Device, memstream, (int)memstream.Length, img.Size.Width, img.Size.Height, 1, Usage.None, Format.Unknown, Pool.Managed, General.Map.Graphics.PostFilter, General.Map.Graphics.MipGenerateFilter, 0); } //mxd. Create the buffer if (textbuffer == null || textbuffer.Disposed) { textbuffer = new VertexBuffer(General.Map.Graphics.Device, 4 * FlatVertex.Stride, Usage.Dynamic | Usage.WriteOnly, VertexFormat.None, Pool.Default); } //mxd. Lock the buffer using (DataStream stream = textbuffer.Lock(0, 4 * FlatVertex.Stride, LockFlags.Discard | LockFlags.NoSystemLock)) { FlatQuad quad = new FlatQuad(PrimitiveType.TriangleStrip, beginx, beginy, beginx + texturesize.Width, beginy + texturesize.Height); stream.WriteRange(quad.Vertices); } // Done filling the vertex buffer textbuffer.Unlock(); } else { // No faces in polygon textsize = SizeF.Empty; //mxd texturesize = Size.Empty; //mxd skiprendering = true; //mxd } // Text updated updateneeded = false; textureupdateneeded = false; //mxd } }
// This updates the text if needed public void Update(RenderDevice graphics, float translatex, float translatey, float scalex, float scaley) { // Check if transformation changed and needs to be updated if (transformcoords && (translatex != lasttranslatex || translatey != lasttranslatey || scalex != lastscalex || scaley != lastscaley)) { lasttranslatex = translatex; //mxd lasttranslatey = translatey; //mxd lastscalex = scalex; //mxd lastscaley = scaley; //mxd updateneeded = true; } // Update if needed if (updateneeded || textureupdateneeded) { // Only build when there are any vertices if (text.Length > 0) { // Transform? Vector2D abspos = (transformcoords ? location.GetTransformed(translatex, translatey, scalex, scaley) : location); // Update text and texture sizes if (textsize.IsEmpty || texturesize.IsEmpty) { textorigin = new PointF(4, 3); textrect = new RectangleF(textorigin, General.Interface.MeasureString(text, font)); textrect.Width = (float)Math.Round(textrect.Width); textrect.Height = (float)Math.Round(textrect.Height); bgrect = new RectangleF(0, 0, textrect.Width + textorigin.X * 2, textrect.Height + textorigin.Y * 2); // Store calculated text size... textsize = new SizeF(textrect.Width + textorigin.X * 2, textrect.Height + textorigin.Y * 2); // Make PO2 image, for speed and giggles... texturesize = new Size(General.NextPowerOf2((int)textsize.Width), General.NextPowerOf2((int)textsize.Height)); switch (alignx) { case TextAlignmentX.Center: bgrect.X = (texturesize.Width - bgrect.Width) / 2; break; case TextAlignmentX.Right: bgrect.X = texturesize.Width - bgrect.Width; break; } switch (aligny) { case TextAlignmentY.Middle: bgrect.Y = (texturesize.Height - bgrect.Height) / 2; break; case TextAlignmentY.Bottom: bgrect.Y = texturesize.Height - bgrect.Height; break; } textrect.X += bgrect.X; textrect.Y += bgrect.Y; } // Align the text horizontally double beginx = 0; switch (alignx) { case TextAlignmentX.Left: beginx = abspos.x; break; case TextAlignmentX.Center: beginx = abspos.x - texturesize.Width * 0.5f; break; case TextAlignmentX.Right: beginx = abspos.x - texturesize.Width; break; } // Align the text vertically double beginy = 0; switch (aligny) { case TextAlignmentY.Top: beginy = abspos.y; break; case TextAlignmentY.Middle: beginy = abspos.y - texturesize.Height * 0.5f; break; case TextAlignmentY.Bottom: beginy = abspos.y - texturesize.Height; break; } //mxd. Skip when not on screen... RectangleF abssize = new RectangleF((float)beginx, (float)beginy, texturesize.Width, texturesize.Height); Size windowsize = General.Map.Graphics.RenderTarget.ClientSize; skiprendering = (abssize.Right < 0.1f) || (abssize.Left > windowsize.Width) || (abssize.Bottom < 0.1f) || (abssize.Top > windowsize.Height); if (skiprendering) { return; } //mxd. Update texture if needed if (textureupdateneeded) { // Get rid of old texture if (texture != null) { texture.Dispose(); texture = null; } // Create label image using (Bitmap img = CreateLabelImage(text, font, color, backcolor, drawbg, textrect, bgrect, texturesize, textorigin)) { texture = new Texture(graphics, img); } } //mxd. Create the buffer if (textbuffer == null || textbuffer.Disposed) { textbuffer = new VertexBuffer(); } FlatQuad quad = new FlatQuad(PrimitiveType.TriangleStrip, (float)beginx, (float)beginy, (float)(beginx + texturesize.Width), (float)(beginy + texturesize.Height)); graphics.SetBufferData(textbuffer, quad.Vertices); } else { // No faces in polygon textsize = SizeF.Empty; //mxd texturesize = Size.Empty; //mxd skiprendering = true; //mxd } // Text updated updateneeded = false; textureupdateneeded = false; //mxd } }