public static void DrawComplex(SpriteBatch sb, String s, SpriteFont sf, Rectangle box, OutLining ol, Color c, float scale, bool allowUpscaling = true, Matrix camera = default(Matrix), Color bgTColor = default(Color)) { if (camera == default(Matrix)) { camera = defaultCamera; } sb.End(); sb.Begin(SpriteSortMode.Immediate, null, null, null, null, null, camera); var test = lsi.Find(si => si.Equals(s, box)); bool generate = false; if (test.s == null) { generate = true; } else { generate = ((test.ogString.Equals(s) && test.box.Equals(box))) ? false : true; } if (!generate) { //s = bestMatchStringForScaledBox(s, sf, box, test.scale.X); } if (generate) { if (bgTColor == default(Color)) { bgTColor = Color.Black; } String ogString = (String)s.Clone(); Vector2 size = sf.MeasureString(s); //if (size.X > box.Width * 4 && size.X < box.Width * 6) //{ // float testScale = (float)(box.Width) / (size.X / 4); // s = bestMatchStringForScaledBox(s, sf, box, testScale); // size = sf.MeasureString(s); //} if (size.X > box.Width * 4) { int index = 4; while ((size.X > box.Width * index && !(size.X < box.Width * (index + 2)))) { index += 2; } if (index == 4) { index = 6; } float testScale = (float)(box.Width) / (size.X); testScale *= (index / 2); s = bestMatchStringForScaledBox(s, sf, box, testScale); size = sf.MeasureString(s); } else { s = bestMatchStringForScaledBox(s, sf, box, 1f); size = sf.MeasureString(s); } Vector2 offSetCenter = Vector2.Zero; Vector2 Location = box.Location.ToVector2(); bool bScaledX = false; bool bScaledY = false; bool bScaledUp = true; float scaleX = 1f; if ((size.ToPoint() + new Point(1)).X > box.Width) { scaleX = (float)box.Width / (float)(size.ToPoint() + new Point(1)).X; bScaledX = true; } else { scaleX = (float)box.Width / (float)(size.ToPoint() + new Point(1)).X; bScaledUp = true; } float scaleY = 1f; if ((size.ToPoint() + new Point(1)).Y > box.Height) { scaleY = (float)box.Height / (float)(size.ToPoint() + new Point(1)).Y; bScaledY = true; } else { scaleY = (float)box.Height / (float)(size.ToPoint() + new Point(1)).Y; bScaledUp = true; } if (bScaledUp) { if (scaleX >= scaleY) { scaleX = scaleY; } else { scaleY = scaleX; } } if (!allowUpscaling && scaleX > 1) { scaleX = 1.0f; scaleY = 1.0f; } switch (ol) { case OutLining.Center: Vector2 temp = (box.Center - box.Location).ToVector2(); temp.X -= size.X / 2 * scaleX; temp.Y -= size.Y / 2 * scaleY; Location += temp; break; case OutLining.Left: temp = (box.Center - box.Location).ToVector2(); temp.Y -= size.Y / 2 * scaleY; Location += new Vector2(0, temp.Y); break; case OutLining.Right: temp = (box.Center - box.Location).ToVector2(); temp.Y -= size.Y / 2 * scaleY; Location += new Vector2(0, temp.Y); Location.X = (box.Location.ToVector2().X + box.Size.ToVector2().X) - size.X * scaleX; break; default: break; } //if (Math.Abs(scaleX - 1) < 0.05f) //{ // scaleX = 1.0f; // scaleY = 1.0f; //} lsi.Add(new StringInfo(s, ogString, sf, Location, new Vector2(scaleX, scaleY), c, box, bgTColor)); test = lsi[lsi.Count - 1]; } sb.DrawString(sf, test.s, test.Location + new Vector2(1), test.bgTC * .7f, 0f, Vector2.Zero, test.scale, SpriteEffects.None, 0); sb.DrawString(sf, test.s, test.Location, test.c, 0f, Vector2.Zero, test.scale, SpriteEffects.None, 0); sb.End(); sb.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp, null, null, null, camera); }
public static void Draw(SpriteBatch sb, String s, SpriteFont sf, Rectangle box, OutLining ol, Color c, float scale, bool allowUpscaling = true, Matrix camera = default(Matrix), Color bgTColor = default(Color), bool bSaveToCache = true) { if (camera == default(Matrix)) { camera = defaultCamera; } //ClearCache(); sb.End(); sb.Begin(SpriteSortMode.Immediate, null, SamplerState.LinearWrap, null, null, null, camera); StringInfo test = default(StringInfo); if (bSaveToCache) { test = lsi.Find(si => si.Equals(s, box)); } bool generate = false; if (bSaveToCache) { if (test.s == null) { generate = true; } else { generate = ((test.ogString.Equals(s) && test.box.Equals(box))) ? false : true; } } if (generate || !bSaveToCache) { if (bgTColor == default(Color)) { bgTColor = Color.Black; } if (s.Equals("Leather vest", StringComparison.OrdinalIgnoreCase)) { } String ogString = (String)s.Clone(); Vector2 size = sf.MeasureString(s); Vector2 offSetCenter = Vector2.Zero; Vector2 Location = box.Location.ToVector2(); bool bScaledX = false; bool bScaledY = false; bool bScaledUp = true; float scaleX = 1f; if ((size.ToPoint() + new Point(1)).X > box.Width) { scaleX = (float)box.Width / (float)(size.ToPoint() + new Point(1)).X; bScaledX = true; } else { scaleX = (float)box.Width / (float)(size.ToPoint() + new Point(1)).X; bScaledUp = true; } float scaleY = 1f; if ((size.ToPoint() + new Point(1)).Y > box.Height) { scaleY = (float)box.Height / (float)(size.ToPoint() + new Point(1)).Y; bScaledY = true; } else { scaleY = (float)box.Height / (float)(size.ToPoint() + new Point(1)).Y; bScaledUp = true; } if (bScaledUp) { if (scaleX >= scaleY) { scaleX = scaleY; } else { scaleY = scaleX; } } if (!allowUpscaling && scaleX > 1) { scaleX = 1.0f; scaleY = 1.0f; } switch (ol) { case OutLining.Center: Vector2 temp = (box.Center - box.Location).ToVector2(); temp.X -= size.X / 2 * scaleX; temp.Y -= size.Y / 2 * scaleY; Location += temp; break; case OutLining.Left: temp = (box.Center - box.Location).ToVector2(); temp.Y -= size.Y / 2 * scaleY; Location += new Vector2(0, temp.Y); break; case OutLining.Right: temp = (box.Center - box.Location).ToVector2(); temp.Y -= size.Y / 2 * scaleY; Location += new Vector2(0, temp.Y); Location.X = (box.Location.ToVector2().X + box.Size.ToVector2().X) - size.X * scaleX; break; default: break; } //if (Math.Abs(scaleX - 1) < 0.05f) //{ // scaleX = 1.0f; // scaleY = 1.0f; //} if (bSaveToCache) { lsi.Add(new StringInfo(s, ogString, sf, Location, new Vector2(scaleX, scaleY), c, box, bgTColor)); test = lsi[lsi.Count - 1]; } else { test = new StringInfo(s, ogString, sf, Location, new Vector2(scaleX, scaleY), c, box, bgTColor); } } sb.DrawString(sf, s, test.Location + new Vector2(1), test.bgTC * .7f, 0f, Vector2.Zero, test.scale, SpriteEffects.None, 0); sb.DrawString(sf, s, test.Location, test.c, 0f, Vector2.Zero, test.scale, SpriteEffects.None, 0); sb.End(); sb.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp, null, null, null, camera); }