VeldridText.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Veldrid.SPIRV;
  7. using System.Numerics;
  8. using System.IO;
  9. using FontStashSharp;
  10. using FontStashSharp.Interfaces;
  11. using System.Diagnostics.CodeAnalysis;
  12. using System.Drawing;
  13. using Veldrid.Common.Tools;
  14. namespace Veldrid.Common
  15. {
  16. public enum HorizontalAlignment
  17. {
  18. Left,
  19. Center,
  20. Right,
  21. }
  22. public enum VerticalAlignment
  23. {
  24. Top,
  25. Center,
  26. Bottom,
  27. }
  28. public class VeldridText : BaseVeldridRender
  29. {
  30. //Framebuffer frameBuffer;
  31. Matrix4x4 matrix4 = Matrix4x4.Identity;
  32. //Texture textureBuffer;
  33. StashRenderer spriteBatch;
  34. [AllowNull]
  35. DynamicSpriteFont sizedFont;
  36. //VeldridSpriteBatch veldridSpriteBatch;
  37. [AllowNull]
  38. FontSystem veldridFont;
  39. bool needupdate = true;
  40. private Vector2 Scale => new Vector2(1f, 1f);
  41. private Vector2 _ImagePoint;
  42. private BlendStateDescription _NomalBlend = new BlendStateDescription()
  43. {
  44. AlphaToCoverageEnabled = false,
  45. BlendFactor = RgbaFloat.Clear,
  46. AttachmentStates = new BlendAttachmentDescription[]
  47. {
  48. new BlendAttachmentDescription()
  49. {
  50. AlphaFunction = BlendFunction.Maximum,
  51. ColorFunction = BlendFunction.Maximum,
  52. BlendEnabled = true,
  53. SourceAlphaFactor= BlendFactor.Zero,
  54. DestinationAlphaFactor = BlendFactor.DestinationAlpha,
  55. SourceColorFactor= BlendFactor.Zero,
  56. DestinationColorFactor = BlendFactor.DestinationColor,
  57. }
  58. }
  59. };
  60. public VeldridText(VeldridContent device,Boolean supportbackColor=false,Boolean supportbackTransparent = true) : base(device)
  61. {
  62. spriteBatch = new StashRenderer(GraphicsDevice, CommandList, CreateShader("TextRender"), CreateShader("TextRenderBackColor"),supportbackColor?BlendStateDescription.SingleAlphaBlend:BlendStateDescription.SingleOverrideBlend, supportbackTransparent? BlendStateDescription.SingleAlphaBlend:BlendStateDescription.SingleOverrideBlend);
  63. //veldridSpriteBatch = new VeldridSpriteBatch(GraphicsDevice, MainSwapchainBuffer.OutputDescription, CreateShader("ImageRender"),GraphicsDevice.LinearSampler, FaceCullMode.Back,_NomalBlend);
  64. BackColor = Color.Transparent;
  65. CreateBuffer();
  66. }
  67. private void CreateBuffer()
  68. {
  69. //var texturedesc = TextureDescription.Texture2D(MainSwapchainBuffer.Width, MainSwapchainBuffer.Height, 1, 1, MainSwapchainBuffer.ColorTargets[0].Target.Format, TextureUsage.RenderTarget | TextureUsage.Sampled);
  70. //if (frameBuffer == null || frameBuffer.Width != MainSwapchainBuffer.Width || frameBuffer.Height != MainSwapchainBuffer.Height)
  71. //{
  72. // textureBuffer?.Dispose();
  73. // textureBuffer = ResourceFactory.CreateTexture(texturedesc);
  74. // frameBuffer?.Dispose();
  75. // frameBuffer = ResourceFactory.CreateFramebuffer(new FramebufferDescription(null, textureBuffer));
  76. // needupdate = true;
  77. //}
  78. //matrix4 = Matrix4x4.CreateOrthographic(frameBuffer.Width, frameBuffer.Height, 0.01f, -100f);
  79. }
  80. internal DynamicSpriteFont VeldridFont => sizedFont;
  81. internal Texture Texture => spriteBatch.Texture;
  82. internal override void CreateResources()
  83. {
  84. base.CreateResources();
  85. LoadFont();
  86. }
  87. private void LoadFont()
  88. {
  89. if (String.IsNullOrEmpty(FontName))
  90. {
  91. _FontName = "MiSans";
  92. }
  93. if (String.IsNullOrEmpty(FontStyle))
  94. {
  95. _FontStyle = "Regular";
  96. }
  97. veldridFont?.Dispose();
  98. veldridFont = FontManger.Instance.LoadFromSystemDirectory(FontName, FontStyle);
  99. sizedFont = veldridFont.GetFont(FontSize);
  100. needupdate = true;
  101. }
  102. public HorizontalAlignment HorizontalAlignment
  103. {
  104. get => horizontalAlignment;
  105. set
  106. {
  107. if(horizontalAlignment!=value)
  108. {
  109. horizontalAlignment = value;
  110. needupdate = true;
  111. }
  112. }
  113. }
  114. public VerticalAlignment VerticalAlignment
  115. {
  116. get => verticalAlignment;
  117. set
  118. {
  119. if (verticalAlignment != value)
  120. {
  121. verticalAlignment = value;
  122. needupdate = true;
  123. }
  124. }
  125. }
  126. public override Vector2 VirtualSize => GetVirtualSize();
  127. private Vector2 GetImagePoint(PointF point)
  128. {
  129. float x = ContentSize.Width / Range.XLenght * (point.X - Range.MinX) + Margin.Left;
  130. float y = ContentSize.Height / Range.YLenght * (Range.MaxY - point.Y) + Margin.Top;
  131. return new Vector2(x, y);
  132. }
  133. public Vector2 GetSize()
  134. {
  135. if (sizedFont == null) return Vector2.Zero;
  136. return sizedFont.MeasureString(Text, Scale);
  137. }
  138. public Vector2 GetVirtualSize()
  139. {
  140. if(sizedFont ==null) return Vector2.Zero;
  141. Vector2 vector = sizedFont.MeasureString(Text, Scale);
  142. return this.LocalSizeToVirtualSize(vector);
  143. }
  144. internal override void PreDraw()
  145. {
  146. base.PreDraw();
  147. if(WindowSizeState)
  148. {
  149. CreateBuffer();
  150. WindowSizeState = false;
  151. }
  152. if (this[nameof(FontName), nameof(FontStyle)])
  153. {
  154. LoadFont();
  155. this[nameof(FontName), nameof(FontStyle)] = false;
  156. }
  157. }
  158. internal override void DrawData()
  159. {
  160. if (!String.IsNullOrEmpty(Text) && Visibily && spriteBatch!=null)
  161. {
  162. spriteBatch.Begin();
  163. Vector2 size = GetSize();
  164. _ImagePoint = this.GetImagePoint(Local);
  165. sizedFont.DrawText(spriteBatch, Text, _ImagePoint, Color.ColorConverToRGBA(), Scale, 0, GetOrigin(size), 1f);
  166. spriteBatch.End();
  167. needupdate = false;
  168. }
  169. }
  170. public Vector2 MeasureSize(String str)
  171. {
  172. if (sizedFont == null) return Vector2.Zero;
  173. return sizedFont.MeasureString(str, Scale);
  174. }
  175. private Vector2 GetOrigin(Vector2 size)
  176. {
  177. Vector2 origin = Vector2.Zero;
  178. switch (HorizontalAlignment)
  179. {
  180. case HorizontalAlignment.Left:
  181. default:
  182. break;
  183. case HorizontalAlignment.Right:
  184. origin.X = size.X;
  185. break;
  186. case HorizontalAlignment.Center:
  187. origin.X = size.X/2;
  188. break;
  189. }
  190. switch (VerticalAlignment)
  191. {
  192. case VerticalAlignment.Top:
  193. default:
  194. origin.Y = FontSize;
  195. break;
  196. case VerticalAlignment.Bottom:
  197. origin.Y = size.Y*1.5f;
  198. break;
  199. case VerticalAlignment.Center:
  200. origin.Y = size.Y*1.25f;
  201. break;
  202. }
  203. return origin;
  204. }
  205. private string text = "Veldrid Control";
  206. public string Text
  207. {
  208. get { return text; }
  209. set
  210. {
  211. if (value != text)
  212. {
  213. Set(ref text, value);
  214. needupdate = true;
  215. }
  216. }
  217. }
  218. private Color color = Color.Black;
  219. public Color Color
  220. {
  221. get { return color; }
  222. set
  223. {
  224. if (color != value)
  225. {
  226. Set(ref color, value);
  227. needupdate = true;
  228. }
  229. }
  230. }
  231. public SizeF BackColorFixedSize { get => spriteBatch.BackColorFixedSize; set => spriteBatch.BackColorFixedSize = value; }
  232. public Color BackColor
  233. {
  234. get => backColor;
  235. set
  236. {
  237. if (value != backColor && spriteBatch!=null)
  238. {
  239. backColor = value;
  240. spriteBatch.BackColor = value.ColorConverToRGBA();
  241. needupdate = true;
  242. }
  243. }
  244. }
  245. private float rotation = 0;
  246. public float Rotation
  247. {
  248. get { return rotation; }
  249. set
  250. {
  251. if (rotation != value)
  252. {
  253. Set(ref rotation, value);
  254. needupdate = true;
  255. }
  256. }
  257. }
  258. private float fontSize = 10;
  259. public float FontSize
  260. {
  261. get { return fontSize; }
  262. set
  263. {
  264. if (value != fontSize && value > 0 && sizedFont!=null)
  265. {
  266. fontSize = value;
  267. sizedFont = veldridFont.GetFont(value);
  268. needupdate = true;
  269. }
  270. }
  271. }
  272. private String _FontStyle = "Normal";
  273. public String FontStyle
  274. {
  275. get { return _FontStyle; }
  276. set { Set(ref _FontStyle, value); }
  277. }
  278. private String _FontName = "MiSans";
  279. public String FontName
  280. {
  281. get { return _FontName; }
  282. set
  283. {
  284. Set(ref _FontName, value);
  285. }
  286. }
  287. private PointF local = new PointF();
  288. private Color backColor = Color.Transparent;
  289. private HorizontalAlignment horizontalAlignment = HorizontalAlignment.Left;
  290. private VerticalAlignment verticalAlignment = VerticalAlignment.Bottom;
  291. public override PointF Local
  292. {
  293. get => local;
  294. set
  295. {
  296. if (local != value)
  297. {
  298. Set(ref local, value);
  299. needupdate = true;
  300. }
  301. }
  302. }
  303. internal override void DisposeResources()
  304. {
  305. lock (_Locker)
  306. {
  307. spriteBatch?.Dispose();
  308. spriteBatch = null;
  309. veldridFont?.Dispose();
  310. veldridFont = null;
  311. sizedFont = null;
  312. base.DisposeResources();
  313. }
  314. }
  315. }
  316. }